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

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

Principles of Programming Languages

The Algol family and ML

The Algol family and ML

Introduction to Haskell

CSCI-GA Scripting Languages

Famous Equations. Rewrite Systems. Subject. Beautiful Results. Today. Tentative Course Outline. a 2 +b 2 = c 2 F = ma e iπ +1 = 0.

10/21/08. Kathleen Fisher! Question:! Interesting answer in Algol:!

Introduction to OCaml

Introduction to Haskell

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

Principles of Programming Languages

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

9/28/09. Kathleen Fisher. Question: Interesting answer in Algol:

4/13/15. Kathleen Fisher for cs242 at Tufts Lightly edited with permission, Michelle Strout 4/10/15

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

Introduction to Haskell

Introduction to SML Getting Started

Types and Type Inference

Polymorphism and Type Inference

Types and Type Inference

Functional Programming

301AA - Advanced Programming [AP-2017]

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

SML A F unctional Functional Language Language Lecture 19

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides.

CMSC 330: Organization of Programming Languages. Functional Programming with OCaml

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Polymorphism and Type Inference

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Functional programming Primer I

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

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

CSCC24 Functional Programming Typing, Scope, Exceptions ML

The type checker will complain that the two branches have different types, one is string and the other is int

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

CS 457/557: Functional Languages

Lists. Michael P. Fourman. February 2, 2010

Homework 3 COSE212, Fall 2018

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group

Some Advanced ML Features

02157 Functional Programming Lecture 1: Introduction and Getting Started

301AA - Advanced Programming [AP-2017]

Principles of Programming Languages

Chapter 11 :: Functional Languages

Types, Type Inference and Unification

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Haskell 5.1 INTERACTIVE SESSIONS AND THE RUN-TIME SYSTEM

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

CS Lectures 2-3. Introduction to OCaml. Polyvios Pratikakis

L3 Programming September 19, OCaml Cheatsheet

How does ML deal with +?

IA014: Advanced Functional Programming

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

Functional Paradigm II

Provably Correct Software

Combining Programming with Theorem Proving

Handout 2 August 25, 2008

301AA - Advanced Programming

The Substitution Model

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

F28PL1 Programming Languages. Lecture 11: Standard ML 1

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

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Overloading, Type Classes, and Algebraic Datatypes

Type Checking and Type Inference

Programming in Standard ML: Continued

Functional Programming. Big Picture. Design of Programming Languages

CMSC 330: Organization of Programming Languages

CSE 307: Principles of Programming Languages

An introduction introduction to functional functional programming programming using usin Haskell

Advanced features of Functional Programming (Haskell)

Programming Languages 3. Definition and Proof by Induction

CSE 307: Principles of Programming Languages

CS 242. Fundamentals. Reading: See last slide

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

Background. From my PhD (2009): Verified Lisp interpreter in ARM, x86 and PowerPC machine code

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

Introduction. chapter Functions

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

Scala : an LLVM-targeted Scala compiler

Week 5 Tutorial Structural Induction

CS 11 Haskell track: lecture 1

4/19/2018. Chapter 11 :: Functional Languages

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

Programming Languages and Compilers (CS 421)

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

So what does studying PL buy me?

Imperative languages

CSCI-GA Final Exam

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

CS558 Programming Languages

ITT8060 Advanced Programming

G Programming Languages - Fall 2012

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

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe!

Transcription:

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) Expression-oriented Higher-order functions Abstract data types Module system Exceptions Originally intended for interactive use slide 2

Why Study ML? ML is clean and powerful, and has many traits that language designers consider hallmarks of a good high-level language: Types and type checking ML is a statically typed, strict functional programming language. Memory management Static scope and block structure, activation records Higher-order functions Garbage collection slide 3 History of ML Robin Milner Stanford, U. of Edinburgh, Cambridge 1991 Turing Award Logic for Computable Functions (LCF) One of the first automated theorem provers Meta-Language of the LCF system slide 4

LCF Logic of Computable Functions ML was invented as part of the University of Edinburgh's LCF project, led by Robin Milner et al., who were conducting research in constructing automated theorem provers. Eventually observed that the "Meta Language" they used for proving theorems was more generally useful as a programming language. slide 5 Logic for Computable Functions Dana Scott (1969) Formulated a logic for proving properties of typed functional programs Robin Milner (1972) Project to automate logic Notation for programs Notation for assertions and proofs Need to write programs that find proofs Too much work to construct full formal proof by hand Make sure proofs are correct slide 6

The interactive ML interpreter We'll use the Moscow ML implementation of ML97 (revision of the 80 Standard ML). Like most ML implementations, it provides a readeval-print loop ("repl"), i.e. the interpreter repeatedly performs the following: reads an expression or declaration from standard input, evaluates the expression/declaration, and. prints the value of expressions, or perhaps the type and initial value of declarations. slide 7 Basic Overview of ML Interactive compiler: read-eval-print Compiler infers type before compiling or executing No need for name declarations Examples - (5+3)-2; > val it = 6 : int - if 5>3 then Bob else Fido ; > val it = Bob : string - 5=4; > val it = false : bool slide 8

REPL The primary advantage of programming in a repl is immediate feedback. The read-eval-print cycle is much faster than the edit-compile-run cycle in a typical compiled programming environment. You can quickly and easily experiment with different snippets of code. If a function doesn't work, you can try out a different version in a second or two, and re-run your program. slide 9 Basic Types Booleans true, false : bool if then else Integers 0, 1, 2, : int +, *, : int * int int Strings Austin Powers Reals 1.0, 2.2, 3.14159, (types must match) and so on decimal point used to disambiguate slide 10

Compound Types Tuples (4, 5, noxious ) : int * int * string Lists nil 1 :: [2, 3, 4] Records {name = Fido, hungry=true} : {name : string, hungry : bool} type type slide 11 Patterns and Declarations Patterns can be used in place of variables <pat> ::= <var> <tuple> <cons> <record> Value declarations General form: val <pat> = <exp> val mytuple = ( Conrad, Lorenz ); val (x,y) = mytuple; val mylist = [1, 2, 3, 4]; val x::rest = mylist; Local declarations let val x = 2+3 in x*4 end; slide 12

Functions and Pattern Matching Anonymous function fn x => x+1; Declaration form like function ( ) in JavaScript fun <name> <pat 1 > = <exp 1 > <name> <pat 2 > = <exp 2 > <name> <pat n > = <exp n > Examples fun f (x,y) = x+y; fun length nil = 0 length (x::s) = 1 + length(s); actual argument must match pattern (x,y) slide 13 Functions on Lists Apply function to every element of list fun map (f, nil) = nil map (f, x::xs) = f(x) :: map (f,xs); Example: map (fn x => x+1, [1,2,3]); [2,3,4] Reverse a list fun reverse nil = nil reverse (x::xs) = append ((reverse xs), [x]); Append lists fun append (nil, ys) = ys append (x::xs, ys) = x :: append(xs, ys); slide 14

More Efficient Reverse Function fun reverse xs = let fun rev(nil, z) = z end; rev(y::ys, z) = rev(ys, y::z) in rev( xs, nil ) 1 3 2 2 2 2 3 3 1 3 1 1 slide 15 Datatype Declarations General form datatype <name> = <clause> <clause> <clause> ::= <constructor> <constructor> of <type> Examples datatype color = red yellow blue Elements are red, yellow, blue datatype atom = atm of string nmbr of int Elements are atm( A ), atm( B ),, nmbr(0), nmbr(1),... datatype list = nil cons of atom*list Elements are nil, cons(atm( A ), nil), cons(nmbr(2), cons(atm( ugh ), nil)),... slide 16

Datatypes and Pattern Matching Recursively defined data structure datatype tree = leaf of int node of int*tree*tree node(4, node(3,leaf(1), leaf(2)), 4 ) node(5,leaf(6), leaf(7)) 3 5 Recursive function fun sum (leaf n) = n sum (node(n,t1,t2)) = n + sum(t1) + sum(t2) 1 2 6 7 slide 17 Example: Evaluating Expressions Define datatype of expressions datatype exp = Var of int Const of int Plus of exp*exp; Write (x+3)+y as Plus(Plus(Var(1),Const(3)), Var(2)) Evaluation function fun ev(var(n)) = Var(n) ev(const(n)) = Const(n) ev(plus(e1,e2)) = ev(plus(const(3),const(2))) ev(plus(var(1),plus(const(2),const(3)))) Const(5) ev(plus(var(1), Const(5)) slide 18

Case Expression Datatype datatype exp = Var of int Const of int Plus of exp*exp; Case expression case e of Var(n) => Const(n) =>. Plus(e1,e2) => slide 19 Evaluation by Cases datatype exp = Var of int Const of int Plus of exp*exp; fun ev(var(n)) = Var(n) ev(const(n)) = Const(n) ev(plus(e1,e2)) = (case ev(e1) of Var(n) => Plus(Var(n),ev(e2)) Const(n) => (case ev(e2) of Var(m) => Plus(Const(n),Var(m)) Const(m) => Const(n+m) Plus(e3,e4) => Plus(Const(n),Plus(e3,e4)) ) Plus(e3,e4) => Plus(Plus(e3,e4),ev(e2)) ); slide 20

ML Imperative Features Remember l-values and r-values? Assignment y := x+3 Refers to location (l-value) Refers to contents (r-value) ML reference cells and assignment Different types for location and contents x : int non-assignable integer value y : int ref location whose contents must be integer!y the contents of cell y ref x ML form of assignment y := x + 3 y :=!y + 3 expression creating new cell initialized to x place value of x+3 in location (cell) y add 3 to contents of y and store in location y slide 21 Reference Cells in ML Variables in most languages Variable names a storage location Contents of location can be read, can be changed ML reference cells A mutable cell is another type of value Explicit operations to read contents or change contents Separates naming (declaration of identifiers) from variables slide 22

Imperative Examples in ML Create cell and change contents val x = ref Bob ; x := Bill ; Create cell and increment val y = ref 0; y :=!y + 1; while loop val i = ref 0; while!i < 10 do i :=!i +1;!i; x y Bob Bill 01 slide 23 Core ML Basic Types Unit Booleans Integers Strings Reals Tuples Lists Records Patterns Declarations ass name to exp Functions Polymorphism Overloading Type declarations Exceptions Reference cells slide 24

Related Languages ML family Standard ML Edinburgh, Bell Labs, Princeton, CAML, OCAML INRIA (France) Some syntactic differences from Standard ML (SML) Object system Haskell Lazy evaluation, extended type system, monads F# ML-like language for Microsoft.NET platform Combining the efficiency, scripting, strong typing and productivity of ML with the stability, libraries, cross-language working and tools of.net. Compiler produces.net intermediate language slide 25