Introduction to SML Getting Started

Similar documents
ITT8060: Advanced Programming (in F#)

02157 Functional Programming Lecture 1: Introduction and Getting Started

02157 Functional Programming. Michael R. Ha. Lecture 1: Introduction and Getting Started. Michael R. Hansen

ITT8060 Advanced Programming

Scope and Introduction to Functional Languages. Review and Finish Scoping. Announcements. Assignment 3 due Thu at 11:55pm. Website has SML resources

CIS24 Project #3. Student Name: Chun Chung Cheung Course Section: SA Date: 4/28/2003 Professor: Kopec. Subject: Functional Programming Language (ML)

Introduction to OCaml

Introduction to SML Basic Types, Tuples, Lists, Trees and Higher-Order Functions

A Brief Introduction to Standard ML

Introduction to ML. Based on materials by Vitaly Shmatikov. General-purpose, non-c-like, non-oo language. Related languages: Haskell, Ocaml, F#,

A quick introduction to SML

CSCI-GA Scripting Languages

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Lists. Michael P. Fourman. February 2, 2010

02157 Functional Programming. Michael R. Ha. Lecture 2: Functions, Types and Lists. Michael R. Hansen

CSE 341 Section 5. Winter 2018

F28PL1 Programming Languages. Lecture 11: Standard ML 1

02157 Functional Programming Lecture 2: Functions, Basic Types and Tuples

Mini-ML. CS 502 Lecture 2 8/28/08

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

The design of a programming language for provably correct programs: success and failure

Lecture 3: Recursion; Structural Induction

Announcements. CSCI 334: Principles of Programming Languages. Lecture 5: Fundamentals III & ML

Handout 2 August 25, 2008

SML A F unctional Functional Language Language Lecture 19

Fall Lecture 3 September 4. Stephen Brookes

Products and Records

15 212: Principles of Programming. Some Notes on Continuations

Variables and Bindings

Polymorphism and Type Inference

Functional Programming

15 212: Principles of Programming. Some Notes on Induction

Functors signature Order = sig functor name type elem (structname:signature) = structure definition; val le : elem*elem -> bool end; elem

Functional programming Primer I

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CSE 505: Concepts of Programming Languages

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

CMSC 330, Fall 2013, Practice Problem 3 Solutions

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Lecture 2: SML Basics

Note that pcall can be implemented using futures. That is, instead of. we can use

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

CSE3322 Programming Languages and Implementation

15 150: Principles of Functional Programming. More about Higher-Order Functions

If we have a call. Now consider fastmap, a version of map that uses futures: Now look at the call. That is, instead of

Fall 2018 Stephen Brookes. LECTURE 2 Thursday, August 30

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

Recap: Functions as first-class values

Begin at the beginning

Language Sequence. The Algol Family and ML. Algol 60 Sample. Algol 60. Some trouble spots in Algol 60. Algol Joke. John Mitchell

At build time, not application time. Types serve as statically checkable invariants.

Programming Paradigms Languages F28PL, Lecture 1

Overloading, Type Classes, and Algebraic Datatypes

Programming Languages

Inductive Data Types

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Lecture #23: Conversion and Type Inference

Functional Programming

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

02157 Functional Programming. Michael R. Ha. Lecture 3: Programming as a model-based activity. Michael R. Hansen

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Lecture 2: The Basis of SML

CS558 Programming Languages

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Polymorphism and Type Inference

Chapter 15. Functional Programming Languages

Part VI. Imperative Functional Programming

CMSC 330, Fall 2013, Practice Problems 3

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

Side note: Tail Recursion. Begin at the beginning. Side note: Tail Recursion. Base Types. Base Type: int. Base Type: int

CSE 505. Lecture #9. October 1, Lambda Calculus. Recursion and Fixed-points. Typed Lambda Calculi. Least Fixed Point

Chapter 3 Linear Structures: Lists

Programming Languages Lecture 14: Sum, Product, Recursive Types

Functional Programming

Programming Languages Third Edition

Datatype declarations

Com S 541. Programming Languages I

CMSC 330: Organization of Programming Languages

The Substitution Model

Combining Static and Dynamic Contract Checking for Curry

Mutable References. Chapter 1

Programming in Standard ML: Continued

Data Types The ML Type System

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

Redefinition of an identifier is OK, but this is redefinition not assignment; Thus

Functional Programming

CSE505, Fall 2012, Midterm Examination October 30, 2012

Lambda Calculus and Type Inference

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

Fall 2018 Lecture 1

Exercises on ML. Programming Languages. Chanseok Oh

CS 11 Ocaml track: lecture 2

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Programming Languages and Compilers (CS 421)

How does ML deal with +?

Hard deadline: 3/28/15 1:00pm. Using software development tools like source control. Understanding the environment model and type inference.

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

Transcription:

Introduction to SML Getting Started Michael R. Hansen mrh@imm.dtu.dk Informatics and Mathematical Modelling Technical University of Denmark c Michael R. Hansen, Fall 2004 p.1/15

Background Standard Meta Language (SML) was originally designed for theorem proving Logic for Computable Functions (Edinburgh LCF) Gordon, Milner, Wadsworth (1977) c Michael R. Hansen, Fall 2004 p.2/15

Background Standard Meta Language (SML) was originally designed for theorem proving Logic for Computable Functions (Edinburgh LCF) Gordon, Milner, Wadsworth (1977) High quality compilers, e.g. Standard ML of New Jersey and Moscow ML, based on a formal semantics Milner, Tofte, Harper, MacQueen 1990 & 1997 c Michael R. Hansen, Fall 2004 p.2/15

Background Standard Meta Language (SML) was originally designed for theorem proving Logic for Computable Functions (Edinburgh LCF) Gordon, Milner, Wadsworth (1977) High quality compilers, e.g. Standard ML of New Jersey and Moscow ML, based on a formal semantics Milner, Tofte, Harper, MacQueen 1990 & 1997 SML have now may applications far away from its origins Compilers, Artificial Intelligence, Web-applications,... c Michael R. Hansen, Fall 2004 p.2/15

Background Standard Meta Language (SML) was originally designed for theorem proving Logic for Computable Functions (Edinburgh LCF) Gordon, Milner, Wadsworth (1977) High quality compilers, e.g. Standard ML of New Jersey and Moscow ML, based on a formal semantics Milner, Tofte, Harper, MacQueen 1990 & 1997 SML have now may applications far away from its origins Compilers, Artificial Intelligence, Web-applications,... Used to teach functional program design and programming style c Michael R. Hansen, Fall 2004 p.2/15

Background Standard Meta Language (SML) was originally designed for theorem proving Logic for Computable Functions (Edinburgh LCF) Gordon, Milner, Wadsworth (1977) High quality compilers, e.g. Standard ML of New Jersey and Moscow ML, based on a formal semantics Milner, Tofte, Harper, MacQueen 1990 & 1997 SML have now may applications far away from its origins Compilers, Artificial Intelligence, Web-applications,... Used to teach functional program design and programming style Also useful when programming using non-functional languages c Michael R. Hansen, Fall 2004 p.2/15

Special Features SML supports Functions as first class citizens c Michael R. Hansen, Fall 2004 p.3/15

Special Features SML supports Functions as first class citizens Structured values like lists, trees,... c Michael R. Hansen, Fall 2004 p.3/15

Special Features SML supports Functions as first class citizens Structured values like lists, trees,... Strong and flexible type discipline, including type inference and polymorphism c Michael R. Hansen, Fall 2004 p.3/15

Special Features SML supports Functions as first class citizens Structured values like lists, trees,... Strong and flexible type discipline, including type inference and polymorphism Powerful module system supporting abstract data types c Michael R. Hansen, Fall 2004 p.3/15

Special Features SML supports Functions as first class citizens Structured values like lists, trees,... Strong and flexible type discipline, including type inference and polymorphism Powerful module system supporting abstract data types Imperative programming assignments, loops, arrays, Input/Output, etc. c Michael R. Hansen, Fall 2004 p.3/15

Special Features SML supports Functions as first class citizens Structured values like lists, trees,... Strong and flexible type discipline, including type inference and polymorphism Powerful module system supporting abstract data types Imperative programming assignments, loops, arrays, Input/Output, etc. Programming as a modelling discipline High-level programming, declarative programming, executable specifications VDM, RAISE Fast prototyping correctness, time-to-market, program designs c Michael R. Hansen, Fall 2004 p.3/15

Overview The interactive environment Values, expressions, types, patterns Declarations of values and recursive functions Binding, environment and evaluation Type inference c Michael R. Hansen, Fall 2004 p.4/15

Overview The interactive environment Values, expressions, types, patterns Declarations of values and recursive functions Binding, environment and evaluation Type inference GOAL: By the end of the day you have constructed succinct, elegant and understandable SML programs, e.g. for sum(m, n) = n i=m i Fibonacci numbers (F 0 = 0, F 1 = 1, F n = F n 1 + F n 2 ) ( ) n Binomial coefficients k c Michael R. Hansen, Fall 2004 p.4/15

The Interactive Environment 2*3 +4; val it = 10 : int c Michael R. Hansen, Fall 2004 p.5/15

The Interactive Environment 2*3 +4; val it = 10 : int Input to the SML system Answer from the SML system c Michael R. Hansen, Fall 2004 p.5/15

The Interactive Environment 2*3 +4; val it = 10 : int Input to the SML system Answer from the SML system The keyword val indicates a value is computed The integer 10 is the computed value int is the type of the computed value The identifier it names the (last) computed value c Michael R. Hansen, Fall 2004 p.5/15

The Interactive Environment 2*3 +4; val it = 10 : int Input to the SML system Answer from the SML system The keyword val indicates a value is computed The integer 10 is the computed value int is the type of the computed value The identifier it names the (last) computed value The notion binding explains which entities are named by identifiers. it 10 reads: it is bound to 10 c Michael R. Hansen, Fall 2004 p.5/15

Value Declarations A value declaration has the form: val identifier = expression val price = 25 * 5; val price = 125 : int A declaration as input Answer from the SML system The effect of a declaration is a binding price 125 c Michael R. Hansen, Fall 2004 p.6/15

Value Declarations A value declaration has the form: val identifier = expression val price = 25 * 5; val price = 125 : int A declaration as input Answer from the SML system The effect of a declaration is a binding price 125 Bound identifiers can be used in expressions and declarations, e.g. val newprice = 2*price; val newprice = 250 : int newprice > 500; val it = false : bool c Michael R. Hansen, Fall 2004 p.6/15

Value Declarations A value declaration has the form: val identifier = expression val price = 25 * 5; val price = 125 : int A declaration as input Answer from the SML system The effect of a declaration is a binding price 125 Bound identifiers can be used in expressions and declarations, e.g. val newprice = 2*price; val newprice = 250 : int newprice > 500; val it = false : bool A collection of bindings price 125 newprice 250 it false is called an environment c Michael R. Hansen, Fall 2004 p.6/15

Function Declarations 1: fun f x = e Declaration of the circle area function: fun circlearea r = Math.pi * r * r; Math is a program library pi is an identifier (with type real) for π declared in Math c Michael R. Hansen, Fall 2004 p.7/15

Function Declarations 1: fun f x = e Declaration of the circle area function: fun circlearea r = Math.pi * r * r; Math is a program library pi is an identifier (with type real) for π declared in Math The type is automatically inferred in the answer: val circlearea = fn : real -> real c Michael R. Hansen, Fall 2004 p.7/15

Function Declarations 1: fun f x = e Declaration of the circle area function: fun circlearea r = Math.pi * r * r; Math is a program library pi is an identifier (with type real) for π declared in Math The type is automatically inferred in the answer: val circlearea = fn : real -> real Applications of the function: circlearea 1.0; (* this is a comment *) val it = 3.14159265359 : real circlearea(3.2); (* brackets are optional *) val it = 32.1699087728 : real c Michael R. Hansen, Fall 2004 p.7/15

Recursion: n! = 1 2... n, n 0 Mathematical definition: 0! = 1 (i) n! = n (n 1)!, for n > 0 (ii) recursion formula Computation: 3! = 3 (3 1)! (ii) = 3 2 (2 1)! (ii) = 3 2 1 (1 1)! (ii) = 3 2 1 1 (i) = 6 c Michael R. Hansen, Fall 2004 p.8/15

Recursive declaration: n! Function declaration: fun fact 0 = 1 (* i *) fact n = n * fact(n-1) (* ii *) val fact = fn : int -> int Evaluation: fact(3) 3 fact(3 1) (ii) 3 2 fact(2 1) (ii) 3 2 1 fact(1 1) (ii) 3 2 1 1 (i) 6 e 1 e 2 reads: e 1 evaluates to e 2 c Michael R. Hansen, Fall 2004 p.9/15

Recursive declaration: n! Function declaration: fun fact 0 = 1 (* i *) fact n = n * fact(n-1) (* ii *) val fact = fn : int -> int Evaluation: fact(3) 3 fact(3 1) (ii) [n 3] 3 2 fact(2 1) (ii) [n 2] 3 2 1 fact(1 1) (ii) [n 1] 3 2 1 1 (i) [n 0] 6 e 1 e 2 reads: e 1 evaluates to e 2 c Michael R. Hansen, Fall 2004 p.9/15

Recursion: x n = x... x, n occurrences of x Mathematical definition: x 0 = 1 (1) x n = x x n 1, for n > 0 (2) recursion formula Function declaration: fun power(_,0) = 1.0 (* 1 *) power(x,n) = x * power(x,n-1) (* 2 *) Patterns: (, 0) matches any pair of the form (x, 0). The wildcard pattern _ matches any value. (x, n) matches any pair (u, i) yielding the bindings x u, n i c Michael R. Hansen, Fall 2004 p.10/15

Evaluation: power(4.0, 2) Function declaration: fun power(_,0) = 1.0 (* 1 *) power(x,n) = x * power(x,n-1) (* 2 *) Evaluation: power(4.0, 2) 4.0 power(4.0, 2 1) Clause 2, [x 4.0, n 2] 4.0 power(4.0, 1) 4.0 (4.0 power(4.0, 1 1)) Clause 2, [x 4.0, n 1] 4.0 (4.0 power(4.0, 0)) 4.0 (4.0 1) Clause 1 16.0 c Michael R. Hansen, Fall 2004 p.11/15

If-then-else expressions Form: Evaluation rules: if b then e 1 else e 2 if true then e 1 else e 2 e 1 if false then e 1 else e 2 e 2 Alternative declarations: fun fact n = if n=0 then 1 else n * fact(n-1); fun power(x,n) = if n=0 then 1.0 else x * power(x,n-1); c Michael R. Hansen, Fall 2004 p.12/15

If-then-else expressions Form: Evaluation rules: if b then e 1 else e 2 if true then e 1 else e 2 e 1 if false then e 1 else e 2 e 2 Alternative declarations: fun fact n = if n=0 then 1 else n * fact(n-1); fun power(x,n) = if n=0 then 1.0 else x * power(x,n-1); Use of clauses and patterns often give more understandable programs c Michael R. Hansen, Fall 2004 p.12/15

Types every expression has a type e : τ type name example of values Integers int 27, 0, 15, 21000 Basic types: Reals real 27.3, 0.0, 48.21 Booleans bool true, false Pairs: If e 1 : τ 1 and e 2 : τ 2 then (e 1, e 2 ) : τ 1 τ 2 pair (tuple) type constructor Examples: (4.0, 2): real*int c Michael R. Hansen, Fall 2004 p.13/15

Types every expression has a type e : τ type name example of values Integers int 27, 0, 15, 21000 Basic types: Reals real 27.3, 0.0, 48.21 Booleans bool true, false If e 1 : τ 1 and e 2 : τ 2 Pairs: then (e 1, e 2 ) : τ 1 τ 2 pair (tuple) type constructor if f : τ 1 -> τ 2 and a : τ 1 function type constructor Functions: then f(a) : τ 2 Examples: (4.0, 2): real*int power: real*int -> real power(4.0, 2): real c Michael R. Hansen, Fall 2004 p.13/15

Types every expression has a type e : τ type name example of values Integers int 27, 0, 15, 21000 Basic types: Reals real 27.3, 0.0, 48.21 Booleans bool true, false If e 1 : τ 1 and e 2 : τ 2 Pairs: then (e 1, e 2 ) : τ 1 τ 2 pair (tuple) type constructor if f : τ 1 -> τ 2 and a : τ 1 function type constructor Functions: then f(a) : τ 2 Examples: (4.0, 2): real*int power: real*int -> real power(4.0, 2): real * has higher precedence that -> c Michael R. Hansen, Fall 2004 p.13/15

Type inference: power fun power(_,0) = 1.0 (* 1 *) power(x,n) = x * power(x,n-1) (* 2 *) c Michael R. Hansen, Fall 2004 p.14/15

Type inference: power fun power(_,0) = 1.0 (* 1 *) power(x,n) = x * power(x,n-1) (* 2 *) The type of the function must have the form: τ 1 * τ 2 -> τ 3, because argument is a pair. c Michael R. Hansen, Fall 2004 p.14/15

Type inference: power fun power(_,0) = 1.0 (* 1 *) power(x,n) = x * power(x,n-1) (* 2 *) The type of the function must have the form: τ 1 * τ 2 -> τ 3, because argument is a pair. τ 3 = real because 1.0:real (Clause 1, function value.) c Michael R. Hansen, Fall 2004 p.14/15

Type inference: power fun power(_,0) = 1.0 (* 1 *) power(x,n) = x * power(x,n-1) (* 2 *) The type of the function must have the form: τ 1 * τ 2 -> τ 3, because argument is a pair. τ 3 = real because 1.0:real τ 2 = int because 0:int. (Clause 1, function value.) c Michael R. Hansen, Fall 2004 p.14/15

Type inference: power fun power(_,0) = 1.0 (* 1 *) power(x,n) = x * power(x,n-1) (* 2 *) The type of the function must have the form: τ 1 * τ 2 -> τ 3, because argument is a pair. τ 3 = real because 1.0:real τ 2 = int because 0:int. x*power(x,n-1):real, because τ 3 = real. (Clause 1, function value.) c Michael R. Hansen, Fall 2004 p.14/15

Type inference: power fun power(_,0) = 1.0 (* 1 *) power(x,n) = x * power(x,n-1) (* 2 *) The type of the function must have the form: τ 1 * τ 2 -> τ 3, because argument is a pair. τ 3 = real because 1.0:real τ 2 = int because 0:int. x*power(x,n-1):real, because τ 3 = real. multiplication can have (Clause 1, function value.) int*int -> int or real*real -> real as types, but no mixture of int and real c Michael R. Hansen, Fall 2004 p.14/15

Type inference: power fun power(_,0) = 1.0 (* 1 *) power(x,n) = x * power(x,n-1) (* 2 *) The type of the function must have the form: τ 1 * τ 2 -> τ 3, because argument is a pair. τ 3 = real because 1.0:real τ 2 = int because 0:int. x*power(x,n-1):real, because τ 3 = real. multiplication can have (Clause 1, function value.) int*int -> int or real*real -> real as types, but no mixture of int and real Therefore x:real and τ 1 =real. c Michael R. Hansen, Fall 2004 p.14/15

Type inference: power fun power(_,0) = 1.0 (* 1 *) power(x,n) = x * power(x,n-1) (* 2 *) The type of the function must have the form: τ 1 * τ 2 -> τ 3, because argument is a pair. τ 3 = real because 1.0:real τ 2 = int because 0:int. x*power(x,n-1):real, because τ 3 = real. multiplication can have (Clause 1, function value.) int*int -> int or real*real -> real as types, but no mixture of int and real Therefore x:real and τ 1 =real. The SML system determines the type real*int -> real c Michael R. Hansen, Fall 2004 p.14/15

Summary The interactive environment Values, expressions, types, patterns Declarations of values and recursive functions Binding, environment and evaluation Type inference Breath first round through many concepts aiming at program construction from the first day. We will go deaper into each of the concepts later in the course. c Michael R. Hansen, Fall 2004 p.15/15