Introduction to SML Getting Started

Size: px
Start display at page:

Download "Introduction to SML Getting Started"

Transcription

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

2 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

3 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

4 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

5 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

6 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

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

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

9 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

10 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

11 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

12 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

13 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

14 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

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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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 = : real circlearea(3.2); (* brackets are optional *) val it = : real c Michael R. Hansen, Fall 2004 p.7/15

25 Recursion: n! = 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) = (1 1)! (ii) = (i) = 6 c Michael R. Hansen, Fall 2004 p.8/15

26 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) fact(1 1) (ii) (i) 6 e 1 e 2 reads: e 1 evaluates to e 2 c Michael R. Hansen, Fall 2004 p.9/15

27 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] fact(1 1) (ii) [n 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

28 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

29 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 c Michael R. Hansen, Fall 2004 p.11/15

30 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

31 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

32 Types every expression has a type e : τ type name example of values Integers int 27, 0, 15, Basic types: Reals real 27.3, 0.0, 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

33 Types every expression has a type e : τ type name example of values Integers int 27, 0, 15, Basic types: Reals real 27.3, 0.0, 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

34 Types every expression has a type e : τ type name example of values Integers int 27, 0, 15, Basic types: Reals real 27.3, 0.0, 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

35 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

36 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

37 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

38 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

39 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

40 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

41 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

42 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

43 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

ITT8060: Advanced Programming (in F#)

ITT8060: Advanced Programming (in F#) based on slides by Michael R. Hansen ITT8060: Advanced Programming (in F#) Lecture 2: Identifiers, values, expressions, functions and types Juhan Ernits Department of Software Science, Tallinn University

More information

02157 Functional Programming Lecture 1: Introduction and Getting Started

02157 Functional Programming Lecture 1: Introduction and Getting Started Lecture 1: Introduction and Getting Started nsen 1 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012 WELCOME to Teacher: nsen DTU Informatics, mrh@imm.dtu.dk

More information

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

02157 Functional Programming. Michael R. Ha. Lecture 1: Introduction and Getting Started. Michael R. Hansen Lecture 1: Introduction and Getting Started nsen 1 DTU Compute, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 06/09/2018 WELCOME to Teacher: nsen, DTU Compute mire@dtu.dk

More information

ITT8060 Advanced Programming

ITT8060 Advanced Programming ITT8060 Advanced Programming In F# Juhan Ernits Welcome to Advanced Programming (in F#)! Teachers: Juhan Ernits Hendrik Maarand Course web page http://courses.cs.ttu.ee/pages/itt8060 Contact: juhan.ernits@ttu.ee

More information

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

Scope and Introduction to Functional Languages. Review and Finish Scoping. Announcements. Assignment 3 due Thu at 11:55pm. Website has SML resources Scope and Introduction to Functional Languages Prof. Evan Chang Meeting 7, CSCI 3155, Fall 2009 Announcements Assignment 3 due Thu at 11:55pm Submit in pairs Website has SML resources Text: Harper, Programming

More information

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

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

More information

Introduction to OCaml

Introduction to OCaml Fall 2018 Introduction to OCaml Yu Zhang Course web site: http://staff.ustc.edu.cn/~yuzhang/tpl References Learn X in Y Minutes Ocaml Real World OCaml Cornell CS 3110 Spring 2018 Data Structures and Functional

More information

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

Introduction to SML Basic Types, Tuples, Lists, Trees and Higher-Order Functions Introduction to SML Basic Types, Tuples, Lists, Trees and Higher-Order Functions Michael R. Hansen mrh@imm.dtu.dk Informatics and Mathematical Modelling Technical University of Denmark c Michael R. Hansen,

More information

A Brief Introduction to Standard ML

A Brief Introduction to Standard ML A Brief Introduction to Standard ML Specification and Verification with Higher-Order Logic Arnd Poetzsch-Heffter (Slides by Jens Brandt) Software Technology Group Fachbereich Informatik Technische Universität

More information

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

Introduction to ML. Based on materials by Vitaly Shmatikov. General-purpose, non-c-like, non-oo language. Related languages: Haskell, Ocaml, F#, Introduction to ML Based on materials by Vitaly Shmatikov slide 1 ML General-purpose, non-c-like, non-oo language Related languages: Haskell, Ocaml, F#, Combination of Lisp and Algol-like features (1958)

More information

A quick introduction to SML

A quick introduction to SML A quick introduction to SML CMSC 15300 April 9, 2004 1 Introduction Standard ML (SML) is a functional language (or higherorder language) and we will use it in this course to illustrate some of the important

More information

CSCI-GA Scripting Languages

CSCI-GA Scripting Languages CSCI-GA.3033.003 Scripting Languages 12/02/2013 OCaml 1 Acknowledgement The material on these slides is based on notes provided by Dexter Kozen. 2 About OCaml A functional programming language All computation

More information

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

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1 A First Look at ML Chapter Five Modern Programming Languages, 2nd ed. 1 ML Meta Language One of the more popular functional languages (which, admittedly, isn t saying much) Edinburgh, 1974, Robin Milner

More information

Lists. Michael P. Fourman. February 2, 2010

Lists. Michael P. Fourman. February 2, 2010 Lists Michael P. Fourman February 2, 2010 1 Introduction The list is a fundamental datatype in most functional languages. ML is no exception; list is a built-in ML type constructor. However, to introduce

More information

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

02157 Functional Programming. Michael R. Ha. Lecture 2: Functions, Types and Lists. Michael R. Hansen Lecture 2: Functions, Types and Lists nsen 1 DTU Compute, Technical University of Denmark Lecture 2: Functions, Types and Lists MRH 13/09/2018 Outline Functions as first-class citizens Types, polymorphism

More information

CSE 341 Section 5. Winter 2018

CSE 341 Section 5. Winter 2018 CSE 341 Section 5 Winter 2018 Midterm Review! Variable Bindings, Shadowing, Let Expressions Boolean, Comparison and Arithmetic Operations Equality Types Types, Datatypes, Type synonyms Tuples, Records

More information

F28PL1 Programming Languages. Lecture 11: Standard ML 1

F28PL1 Programming Languages. Lecture 11: Standard ML 1 F28PL1 Programming Languages Lecture 11: Standard ML 1 Imperative languages digital computers are concrete realisations of von Neumann machines stored program memory associations between addresses and

More information

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

02157 Functional Programming Lecture 2: Functions, Basic Types and Tuples Lecture 2: Functions, Basic Types and Tuples nsen 1 DTU Informatics, Technical University of Denmark Lecture 2: Functions, Basic Types and Tuples MRH 13/09/2012 Outline A further look at functions, including

More information

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

Mini-ML. CS 502 Lecture 2 8/28/08 Mini-ML CS 502 Lecture 2 8/28/08 ML This course focuses on compilation techniques for functional languages Programs expressed in Standard ML Mini-ML (the source language) is an expressive core subset of

More information

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

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont. CMSC 330: Organization of Programming Languages OCaml 1 Functional Programming Dialects of ML ML (Meta Language) Univ. of Edinburgh,1973 Part of a theorem proving system LCF The Logic of Computable Functions

More information

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

The design of a programming language for provably correct programs: success and failure The design of a programming language for provably correct programs: success and failure Don Sannella Laboratory for Foundations of Computer Science School of Informatics, University of Edinburgh http://homepages.inf.ed.ac.uk/dts

More information

Lecture 3: Recursion; Structural Induction

Lecture 3: Recursion; Structural Induction 15-150 Lecture 3: Recursion; Structural Induction Lecture by Dan Licata January 24, 2012 Today, we are going to talk about one of the most important ideas in functional programming, structural recursion

More information

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

Announcements. CSCI 334: Principles of Programming Languages. Lecture 5: Fundamentals III & ML Announcements CSCI 334: Principles of Programming Languages Lecture 5: Fundamentals III & ML Instructor: Dan Barowy Claire Booth Luce info session tonight for women interested in summer research (hopefully

More information

Handout 2 August 25, 2008

Handout 2 August 25, 2008 CS 502: Compiling and Programming Systems Handout 2 August 25, 2008 Project The project you will implement will be a subset of Standard ML called Mini-ML. While Mini- ML shares strong syntactic and semantic

More information

SML A F unctional Functional Language Language Lecture 19

SML A F unctional Functional Language Language Lecture 19 SML A Functional Language Lecture 19 Introduction to SML SML is a functional programming language and acronym for Standard d Meta Language. SML has basic data objects as expressions, functions and list

More information

Fall Lecture 3 September 4. Stephen Brookes

Fall Lecture 3 September 4. Stephen Brookes 15-150 Fall 2018 Lecture 3 September 4 Stephen Brookes Today A brief remark about equality types Using patterns Specifying what a function does equality in ML e1 = e2 Only for expressions whose type is

More information

Products and Records

Products and Records Products and Records Michael P. Fourman February 2, 2010 1 Simple structured types Tuples Given a value v 1 of type t 1 and a value v 2 of type t 2, we can form a pair, (v 1, v 2 ), containing these values.

More information

15 212: Principles of Programming. Some Notes on Continuations

15 212: Principles of Programming. Some Notes on Continuations 15 212: Principles of Programming Some Notes on Continuations Michael Erdmann Spring 2011 These notes provide a brief introduction to continuations as a programming technique. Continuations have a rich

More information

Variables and Bindings

Variables and Bindings Net: Variables Variables and Bindings Q: How to use variables in ML? Q: How to assign to a variable? # let = 2+2;; val : int = 4 let = e;; Bind the value of epression e to the variable Variables and Bindings

More information

Polymorphism and Type Inference

Polymorphism and Type Inference Polymorphism and Type Inference Volker Stolz stolz@ifi.uio.no Department of Informatics University of Oslo Initially by Gerardo Schneider. Based on John C. Mitchell s slides (Stanford U.) Compile-time

More information

Functional Programming

Functional Programming The Meta Language (ML) and Functional Programming Daniel S. Fava danielsf@ifi.uio.no Department of informatics University of Oslo, Norway Motivation ML Demo Which programming languages are functional?

More information

15 212: Principles of Programming. Some Notes on Induction

15 212: Principles of Programming. Some Notes on Induction 5 22: Principles of Programming Some Notes on Induction Michael Erdmann Spring 20 These notes provide a brief introduction to induction for proving properties of ML programs. We assume that the reader

More information

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

Functors signature Order = sig functor name type elem (structname:signature) = structure definition; val le : elem*elem -> bool end; elem Functors The general form of a functor is functor name (structname:signature) = structure definition; This functor will create a specific version of the structure definition using the structure parameter

More information

Functional programming Primer I

Functional programming Primer I Functional programming Primer I COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer 1 Characteristics of functional programming Primary notions: functions and expressions (not

More information

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml COSE212: Programming Languages Lecture 3 Functional Programming in OCaml Hakjoo Oh 2017 Fall Hakjoo Oh COSE212 2017 Fall, Lecture 3 September 18, 2017 1 / 44 Why learn ML? Learning ML is a good way of

More information

CSE 505: Concepts of Programming Languages

CSE 505: Concepts of Programming Languages CSE 505: Concepts of Programming Languages Dan Grossman Fall 2009 Lecture 4 Proofs about Operational Semantics; Pseudo-Denotational Semantics Dan Grossman CSE505 Fall 2009, Lecture 4 1 This lecture Continue

More information

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

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules. Outline Type Checking General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time

More information

CMSC 330, Fall 2013, Practice Problem 3 Solutions

CMSC 330, Fall 2013, Practice Problem 3 Solutions CMSC 330, Fall 2013, Practice Problem 3 Solutions 1. OCaml and Functional Programming a. Define functional programming Programs are expression evaluations b. Define imperative programming Programs change

More information

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

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference Type Checking Outline General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time

More information

Lecture 2: SML Basics

Lecture 2: SML Basics 15-150 Lecture 2: SML Basics Lecture by Dan Licata January 19, 2012 I d like to start off by talking about someone named Alfred North Whitehead. With someone named Bertrand Russell, Whitehead wrote Principia

More information

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

Note that pcall can be implemented using futures. That is, instead of. we can use Note that pcall can be implemented using futures. That is, instead of (pcall F X Y Z) we can use ((future F) (future X) (future Y) (future Z)) In fact the latter version is actually more parallel execution

More information

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

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference Lecture #13: Type Inference and Unification Typing In the Language ML Examples from the language ML: fun map f [] = [] map f (a :: y) = (f a) :: (map f y) fun reduce f init [] = init reduce f init (a ::

More information

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

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually Plan (next 4 weeks) 1. Fast forward Rapid introduction to what s in OCaml 2. Rewind 3. Slow motion Go over the pieces individually History, Variants Meta Language Designed by Robin Milner @ Edinburgh Language

More information

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

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc. CSC312 Principles of Programming Languages : Functional Programming Language Overview of Functional Languages They emerged in the 1960 s with Lisp Functional programming mirrors mathematical functions:

More information

CSE3322 Programming Languages and Implementation

CSE3322 Programming Languages and Implementation Monash University School of Computer Science & Software Engineering Sample Exam 2004 CSE3322 Programming Languages and Implementation Total Time Allowed: 3 Hours 1. Reading time is of 10 minutes duration.

More information

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

15 150: Principles of Functional Programming. More about Higher-Order Functions 15 150: Principles of Functional Programming More about Higher-Order Functions Michael Erdmann Spring 2018 1 Topics Currying and uncurrying Staging computation Partial evaluation Combinators 2 Currying

More information

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

If we have a call. Now consider fastmap, a version of map that uses futures: Now look at the call. That is, instead of If we have a call (map slow-function long-list where slow-function executes slowly and long-list is a large data structure, we can expect to wait quite a while for computation of the result list to complete.

More information

Fall 2018 Stephen Brookes. LECTURE 2 Thursday, August 30

Fall 2018 Stephen Brookes. LECTURE 2 Thursday, August 30 15-150 Fall 2018 Stephen Brookes LECTURE 2 Thursday, August 30 Announcements HOMEWORK 1 is out Read course policy Must be your OWN work Integrity No collaboration (only limited discussion) Rules will be

More information

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

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function CSE 130 : Fall 2011 Recap from last time Programming Languages Lecture 3: Data Types Ranjit Jhala UC San Diego 1 2 A shorthand for function binding Put it together: a filter function # let neg = fun f

More information

Recap: Functions as first-class values

Recap: Functions as first-class values Recap: Functions as first-class values Arguments, return values, bindings What are the benefits? Parameterized, similar functions (e.g. Testers) Creating, (Returning) Functions Iterator, Accumul, Reuse

More information

Begin at the beginning

Begin at the beginning Begin at the beginning Expressions (Syntax) Exec-time Dynamic Values (Semantics) Compile-time Static Types 1. Programmer enters expression 2. ML checks if expression is well-typed Using a precise set of

More information

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

Language Sequence. The Algol Family and ML. Algol 60 Sample. Algol 60. Some trouble spots in Algol 60. Algol Joke. John Mitchell CS 4 Language Sequence The Algol Family and ML Lisp Algol 60 John Mitchell Algol 68 Pascal ML Modula Reading: Chapter 5 Many other languages: Algol 58, Algol W, Euclid, EL, Mesa (PARC), Modula-, Oberon,

More information

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

At build time, not application time. Types serve as statically checkable invariants. Static Typing Thus far we have only considered statically typed languages. CMPSCI 630: Programming Languages Static and Dynamic Typing Spring 2008 (with thanks to Robert Harper) Type system is based on

More information

Programming Paradigms Languages F28PL, Lecture 1

Programming Paradigms Languages F28PL, Lecture 1 Programming Paradigms Languages F28PL, Lecture 1 Jamie Gabbay October 14, 2016 1 / 37 About me My name is Murdoch James Gabbay everybody calls me Jamie. I got a PhD in Cambridge in 2001. Since then I have

More information

Overloading, Type Classes, and Algebraic Datatypes

Overloading, Type Classes, and Algebraic Datatypes Overloading, Type Classes, and Algebraic Datatypes Delivered by Michael Pellauer Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. September 28, 2006 September 28, 2006 http://www.csg.csail.mit.edu/6.827

More information

Programming Languages

Programming Languages CSE 130 : Winter 2009 Programming Languages News PA 2 out, and due Mon 1/26 5pm Lecture 5: Functions and Datatypes t UC San Diego Recap: Environments Phone book Variables = names Values = phone number

More information

Inductive Data Types

Inductive Data Types Inductive Data Types Lars-Henrik Eriksson Functional Programming 1 Original slides by Tjark Weber Lars-Henrik Eriksson (UU) Inductive Data Types 1 / 42 Inductive Data Types Today Today New names for old

More information

CPS 506 Comparative Programming Languages. Programming Language Paradigm

CPS 506 Comparative Programming Languages. Programming Language Paradigm CPS 506 Comparative Programming Languages Functional Programming Language Paradigm Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming

More information

Lecture #23: Conversion and Type Inference

Lecture #23: Conversion and Type Inference Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). Last modified: Fri Oct 20 10:46:40 2006 CS164:

More information

Functional Programming

Functional Programming Functional Programming Björn B. Brandenburg The University of North Carolina at Chapel Hill Based in part on slides and notes by S. Olivier, A. Block, N. Fisher, F. Hernandez-Campos, and D. Stotts. Brief

More information

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

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = Hello; Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). In Java, this is legal: Object x = "Hello";

More information

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

02157 Functional Programming. Michael R. Ha. Lecture 3: Programming as a model-based activity. Michael R. Hansen Lecture 3: as a model-based activity nsen 1 DTU Compute, Technical University of Denmark Lecture 3: as a model-based activity MRH 20/09/2018 Overview RECAP Higher-order functions (lists) Type inference

More information

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

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics Recall Architecture of Compilers, Interpreters CMSC 330: Organization of Programming Languages Source Scanner Parser Static Analyzer Operational Semantics Intermediate Representation Front End Back End

More information

Lecture 2: The Basis of SML

Lecture 2: The Basis of SML Lecture 2: The Basis of SML Jean-Noël Monette Functional Programming 1 September 9, 2013 Based on notes by Pierre Flener, Sven-Olof Nyström Jean-Noël Monette (UU) Lecture 2: The Basis of SML 1 / 73 Today

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Values and Types We divide the universe of values according to types A type is a set of values and

More information

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

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes CSE 130 : Fall 2007 Programming Languages News PA deadlines shifted PA #2 now due 10/24 (next Wed) Lecture 5: Functions and Datatypes Ranjit Jhala UC San Diego Recap: Environments Phone book Variables

More information

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

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II) Processadors de Llenguatge II Functional Paradigm Pratt A.7 Robert Harper s SML tutorial (Sec II) Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra Paradigm Shift Imperative Paradigm State Machine

More information

Polymorphism and Type Inference

Polymorphism and Type Inference Polymorphism and Type Inference Volker Stolz stolz@ifi.uio.no INF 3110-2008 Department of Informatics University of Oslo Initially by Gerardo Schneider. Based on John C. Mitchell s slides (Stanford U.)

More information

Chapter 15. Functional Programming Languages

Chapter 15. Functional Programming Languages Chapter 15 Functional Programming Languages Copyright 2009 Addison-Wesley. All rights reserved. 1-2 Chapter 15 Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages

More information

Part VI. Imperative Functional Programming

Part VI. Imperative Functional Programming Part VI Imperative Functional Programming Chapter 14 Mutable Storage MinML is said to be a pure language because the execution model consists entirely of evaluating an expression for its value. ML is

More information

CMSC 330, Fall 2013, Practice Problems 3

CMSC 330, Fall 2013, Practice Problems 3 CMSC 330, Fall 2013, Practice Problems 3 1. OCaml and Functional Programming a. Define functional programming b. Define imperative programming c. Define higher-order functions d. Describe the relationship

More information

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

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial Begin at the beginning Epressions (Synta) Compile-time Static Eec-time Dynamic Types Values (Semantics) 1. Programmer enters epression 2. ML checks if epression is well-typed Using a precise set of rules,

More information

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

Side note: Tail Recursion. Begin at the beginning. Side note: Tail Recursion. Base Types. Base Type: int. Base Type: int Begin at the beginning Epressions (Synta) Compile-time Static Eec-time Dynamic Types Values (Semantics) 1. Programmer enters epression 2. ML checks if epression is well-typed Using a precise set of rules,

More information

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

CSE 505. Lecture #9. October 1, Lambda Calculus. Recursion and Fixed-points. Typed Lambda Calculi. Least Fixed Point Lambda Calculus CSE 505 Lecture #9 October 1, 2012 Expr ::= Var λ Var. Expr (Expr Expr) Key Concepts: Bound and Free Occurrences Substitution, Reduction Rules: α, β, η Confluence and Unique Normal Form

More information

Chapter 3 Linear Structures: Lists

Chapter 3 Linear Structures: Lists Plan Chapter 3 Linear Structures: Lists 1. Two constructors for lists... 3.2 2. Lists... 3.3 3. Basic operations... 3.5 4. Constructors and pattern matching... 3.6 5. Simple operations on lists... 3.9

More information

Programming Languages Lecture 14: Sum, Product, Recursive Types

Programming Languages Lecture 14: Sum, Product, Recursive Types CSE 230: Winter 200 Principles of Programming Languages Lecture 4: Sum, Product, Recursive Types The end is nigh HW 3 No HW 4 (= Final) Project (Meeting + Talk) Ranjit Jhala UC San Diego Recap Goal: Relate

More information

Functional Programming

Functional Programming Functional Programming Function evaluation is the basic concept for a programming paradigm that has been implemented in functional programming languages. The language ML ( Meta Language ) was originally

More information

Programming Languages Third Edition

Programming Languages Third Edition Programming Languages Third Edition Chapter 12 Formal Semantics Objectives Become familiar with a sample small language for the purpose of semantic specification Understand operational semantics Understand

More information

Datatype declarations

Datatype declarations Datatype declarations datatype suit = HEARTS DIAMONDS CLUBS SPADES datatype a list = nil (* copy me NOT! *) op :: of a * a list datatype a heap = EHEAP HEAP of a * a heap * a heap type suit val HEARTS

More information

Com S 541. Programming Languages I

Com S 541. Programming Languages I Programming Languages I Lecturer: TA: Markus Lumpe Department of Computer Science 113 Atanasoff Hall http://www.cs.iastate.edu/~lumpe/coms541.html TR 12:40-2, W 5 Pramod Bhanu Rama Rao Office hours: TR

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Operational Semantics CMSC 330 Summer 2018 1 Formal Semantics of a Prog. Lang. Mathematical description of the meaning of programs written in that language

More information

The Substitution Model

The Substitution Model The Substitution Model Prof. Clarkson Fall 2017 Today s music: Substitute by The Who Review Previously in 3110: simple interpreter for expression language abstract syntax tree (AST) evaluation based on

More information

Combining Static and Dynamic Contract Checking for Curry

Combining Static and Dynamic Contract Checking for Curry Michael Hanus (CAU Kiel) Combining Static and Dynamic Contract Checking for Curry LOPSTR 2017 1 Combining Static and Dynamic Contract Checking for Curry Michael Hanus University of Kiel Programming Languages

More information

Mutable References. Chapter 1

Mutable References. Chapter 1 Chapter 1 Mutable References In the (typed or untyped) λ-calculus, or in pure functional languages, a variable is immutable in that once bound to a value as the result of a substitution, its contents never

More information

Programming in Standard ML: Continued

Programming in Standard ML: Continued Programming in Standard ML: Continued Specification and Verification with Higher-Order Logic Arnd Poetzsch-Heffter (Slides by Jens Brandt) Software Technology Group Fachbereich Informatik Technische Universität

More information

Data Types The ML Type System

Data Types The ML Type System 7 Data Types 7.2.4 The ML Type System The following is an ML version of the tail-recursive Fibonacci function introduced Fibonacci function in ML in Section 6.6.1: EXAMPLE 7.96 1. fun fib (n) = 2. let

More information

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

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions Recap Epressions (Synta) Compile-time Static Eec-time Dynamic Types (Semantics) Recap Integers: +,-,* floats: +,-,* Booleans: =,

More information

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

Redefinition of an identifier is OK, but this is redefinition not assignment; Thus Redefinition of an identifier is OK, but this is redefinition not assignment; Thus val x = 100; val x = (x=100); is fine; there is no type error even though the first x is an integer and then it is a boolean.

More information

Functional Programming

Functional Programming Functional Programming COMS W4115 Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science Original version by Prof. Simon Parsons Functional vs. Imperative Imperative programming

More information

CSE505, Fall 2012, Midterm Examination October 30, 2012

CSE505, Fall 2012, Midterm Examination October 30, 2012 CSE505, Fall 2012, Midterm Examination October 30, 2012 Rules: The exam is closed-book, closed-notes, except for one side of one 8.5x11in piece of paper. Please stop promptly at Noon. You can rip apart

More information

Lambda Calculus and Type Inference

Lambda Calculus and Type Inference Lambda Calculus and Type Inference Björn Lisper Dept. of Computer Science and Engineering Mälardalen University bjorn.lisper@mdh.se http://www.idt.mdh.se/ blr/ August 17, 2007 Lambda Calculus and Type

More information

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

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions CVO103: Programming Languages Lecture 5 Design and Implementation of PLs (1) Expressions Hakjoo Oh 2018 Spring Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 1 / 23 Plan Part 1 (Preliminaries):

More information

Fall 2018 Lecture 1

Fall 2018 Lecture 1 15-150 Fall 2018 Lecture 1 Stephen Brookes 1 Themes functional programming correctness, termination, and performance types, specifications, and proofs evaluation and equivalence referential transparency

More information

Exercises on ML. Programming Languages. Chanseok Oh

Exercises on ML. Programming Languages. Chanseok Oh Exercises on ML Programming Languages Chanseok Oh chanseok@cs.nyu.edu Dejected by an arcane type error? - foldr; val it = fn : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b - foldr (fn x=> fn y => fn z => (max

More information

CS 11 Ocaml track: lecture 2

CS 11 Ocaml track: lecture 2 Today: CS 11 Ocaml track: lecture 2 comments algebraic data types more pattern matching records polymorphic types ocaml libraries exception handling Previously... ocaml interactive interpreter compiling

More information

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

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions CMSC 330: Organization of Programming Languages OCaml Expressions and Functions CMSC330 Spring 2018 1 Lecture Presentation Style Our focus: semantics and idioms for OCaml Semantics is what the language

More information

Programming Languages and Compilers (CS 421)

Programming Languages and Compilers (CS 421) Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC http://courses.engr.illinois.edu/cs421 Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha 10/3/17

More information

How does ML deal with +?

How does ML deal with +? How does ML deal with +? Moscow ML version 2.00 (June 2000) - op +; > val it = fn : int * int -> int - 1 + 1; > val it = 2 : int - 1.0 + 1.0; > val it = 2.0 : real - false + false;! Overloaded + cannot

More information

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

Hard deadline: 3/28/15 1:00pm. Using software development tools like source control. Understanding the environment model and type inference. CS 3110 Spring 2015 Problem Set 3 Version 0 (last modified March 12, 2015) Soft deadline: 3/26/15 11:59pm Hard deadline: 3/28/15 1:00pm Overview In this assignment you will implement several functions

More information

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

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides CSC324- TUTORIAL 5 ML Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides Assignment 1 2 More questions were added Questions regarding the assignment? Starting ML Who am I? Shems Saleh

More information