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

Similar documents
CSCI-GA Scripting Languages

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

Introduction to OCaml

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

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

Functional programming Primer I

Lists. Michael P. Fourman. February 2, 2010

Introduction to SML Getting Started

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Types, Type Inference and Unification

Lecture 19: Functions, Types and Data Structures in Haskell

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

CSE 341 Section 5. Winter 2018

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

Polymorphism and Type Inference

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

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

CS558 Programming Languages

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

Introduction to OCaml

CS558 Programming Languages

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

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.

A Brief Introduction to Standard ML

Types and Type Inference

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

Programming Languages and Compilers (CS 421)

Polymorphism and Type Inference

Types and Type Inference

Functional Programming Languages (FPL)

Testing. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 2. [Faculty of Science Information and Computing Sciences]

CSCC24 Functional Programming Typing, Scope, Exceptions ML

Announcements. CSCI 334: Principles of Programming Languages. Thanks! Lecture 23: Exam Review. Announcements. Announcements

Functional Paradigm II

Programming Languages

CSci 4223 Principles of Programming Languages

Inductive Data Types

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

CSE3322 Programming Languages and Implementation

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

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

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

Functional Programming

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

Chapter 3 Linear Structures: Lists

Overloading, Type Classes, and Algebraic Datatypes

Some Advanced ML Features

A Fourth Look At ML. Chapter Eleven Modern Programming Languages, 2nd ed. 1

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

CS 11 Haskell track: lecture 1

All the operations used in the expression are over integers. a takes a pair as argument. (a pair is also a tuple, or more specifically, a 2-tuple)

Chapter 3 Linear Structures: Lists

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

CSE341 Autumn 2017, Midterm Examination October 30, 2017

Type Checking and Type Inference

CMSC 330: Organization of Programming Languages

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)

Homework 5. Notes. Turn-In Instructions. Reading. Problems. Handout 19 CSCI 334: Spring (Required) Read Mitchell, Chapters 6 and 7.

G Programming Languages - Fall 2012

How does ML deal with +?

Lecture 2: The Basis of SML

CSE 3302 Programming Languages Lecture 8: Functional Programming

Lambda Calculus. Variables and Functions. cs3723 1

Lambda Calculus and Type Inference

Problem Set CVO 103, Spring 2018

CSE 130 Programming Languages. Lecture 3: Datatypes. Ranjit Jhala UC San Diego

CSE 130 [Winter 2014] Programming Languages

CSE-321 Programming Languages 2012 Midterm

Lecture #23: Conversion and Type Inference

Exercises on ML. Programming Languages. Chanseok Oh

Handout 2 August 25, 2008

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

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

301AA - Advanced Programming [AP-2017]

F28PL1 Programming Languages. Lecture 11: Standard ML 1

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

Type Systems, Type Inference, and Polymorphism

Functional Programming

A quick introduction to SML

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group

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

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

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

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

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

Datatype declarations

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

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

Recursive Definitions, Fixed Points and the Combinator

Lecture 15 CIS 341: COMPILERS

CS153: Compilers Lecture 15: Local Optimization

The Algol family and ML

Functional Paradigm II

CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions. Dan Grossman Spring 2013

Principles of Programming Languages

Recap: ML s Holy Trinity. Story So Far... CSE 130 Programming Languages. Datatypes. A function is a value! Next: functions, but remember.

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Transcription:

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 in CS!), TBL 211 (also: pizza and ice cream) midterm: before or after spring break? M ::= x λx.m MM variable abstraction function application MMM (MM)M or M(MM)?

Order does not matter Reduction strategies M (λx.y)((λx.x x)(λx.x x)) M 1 M 2 If M M 1 and M M 2 then M 1 * N and M 2 * N for some N function argument N confluence Reduction strategies Reduction strategies (λx.y)((λx.x x)(λx.x x)) function argument (λx.y)((λx.x x)(λx.x x)) function argument Normal-order reduction: Choose the left-most redex first. 1. ([(λx.x x)(λx.x x)/x]y) 2. y

Reduction strategies (λx.y)((λx.x x)(λx.x x)) function argument Applicative-order reduction: Choose the right-most redex first. 1. (λx.y)(([(λx.x x)/x]x x)) 2. (λx.y)((λx.x x)(λx.x x)) 3. (λx.y)(([(λx.x x)/x]x x)) 4. uh oh M M 1 M 2 N Order does not matter If M M 1 and M M 2 then M 1 * N and M 2 * N for some N Normal-order reduction will always find a normal form if one exists. Applicative-order reduction will always find a normal form if reduction terminates. ML (λf.λx.f(f x))(λz.x+z)2 = (λf.λa.f(f a))(λz.x+z)2 -> (rename x) ([(λz.x+z)/f]λa.f(f a))2 = (reduce f) (λa.(λz.x+z)((λz.x+z)a))2 = (substitution) (λa.(λb.x+b)((λz.x+z)a))2 -> (rename z) (λa.(λb.x+b)([a/z](x+z))2 = (reduce z) (λa.(λb.x+b)(x+a))2 -> (substitution) (λa.[(x+a)/b](x+b))2 = (reduce b) (λa.x+(x+a))2 -> (substitution) [2/a](x+(x+a))= (reduce a) x+x+2 (substitution) 1950 1960 1970 1980 1990 2000 2010 Miranda Haskell LISP ML Standard ML F# Caml OCaml Java C#

ML Dana Scott Logic of Computable Functions (LCF) Automated proofs! Theorem proving is essentially a search problem. It is (essentially) NP-Complete But works in practice with the right tactics ML Robin Milner How to program tactics? A meta-language is needed ML is born LCF/ML influence: Dafny method MultipleReturns(x: int, y: int) returns (more: int, less: int) ensures less < x ensures x < more { more := x + y; less := x - y; } K. Rustan Leino Dafny programs can often be proven correct (wrt spec) Rustan also famous for his hair :) ML Features: static types Core: LISP + static types types are checked before program runs Static types guarantee correctness of programs Why does this not violate halting problem? All well-typed programs do not fail at runtime

ML Features: parametric polymorphism ML Features: type inference fun swapint(x: int, y: int): int*int = (y,x) fun swapreal(x: real, y: real): real*real = (y,x) fun swapstring(x: string, y: string): string*string = (y,x) abstract types allow programmers to write generic programs; reveal underlying idea without boilerplate fun swap(x: 'a, y: 'b): 'b * 'a = (y,x) writing types is hard (and sometimes ugly!) fun swap(x, y) = (y,x) fun swap(x: 'a, y: 'b): 'b * 'a = (y,x) ML Features: exceptions ML Features: side effects; mutability Milner: it s hard to write well-typed programs mechanism to allow programs to signal error and correct for them at runtime fun foo = exception DivByZero of string if x = 0 then raise DivByZero( no zeros! ) foo handle DivByZero msg => do something else These are features? For real-world programs, yes. fun foo() = let val name = Dan in print (name ^ \n ) end; side effect Both are often essential for speed let val x : int ref = ref 3 val y : int =!x in x := (!x) + 1; y + (!x) end mutability But can be largely avoided in many programs for safety

Running ML Type sml on Unix machines Ctrl-D to quit Enter expression or declarations to evaluate: - 3 + 5; val it = 8 : int - it * 2; val it = 16 : int - val six = 3 + 3; val six = 6 : int Or "sml < file.ml" Defining Functions Example - fun succ x = x + 1; val succ = fn : int -> int - succ 12; val it = 13 : int - 17 * (succ 3); val it = 68 : int; Or: - val succ = fn x => x + 1; val succ = fn : int -> int No type info given- compiler infers it 21 Recursion All functions written using recursion and if.. then.. else (and patterns): - fun fact n = if n = 0 then 1 else n * fact (n-1); if..then..else is an expression: - if 3<4 then "moo" else "cow"; val it = "moo" : string - types of branches must match Local Declarations - fun cylindervolume diameter height = let val radius = diameter / 2.0; fun square y = y * y in 3.14 * square radius * height end; val cylindervolume = fn : real -> real -> real - cylindervolume 6.0 6.0; val it = 169.56 : real

Built-in Data Types unit only value is () bool true, false operators not, andalso, orelse int..., ~2, ~1, 0, 1, 2,... +,-,*,div,mod,abs =,<,<=, etc. Built-in Data Types real 3.17, 2.2,... +, -, *, / <, <=, etc. no implicit conversions from int to real: 2 + 3.3 is bad no equality (test that -0.001 < x-y < 0.001, etc.) strings "moo" "moo" ^ cow" Overloaded Operators +,-,etc. defined on both int and real Which variant inferred depends on operands: - fun succ x = x + 1 val succ = fn : int -> int - fun double x = x * 2.0 val double = fn : real -> real Type Declarations Can add types when type inference does not work - fun double (x:real) = x + x; val double = fn : real -> real - fun double (x:real) : real = x + x; val double = fn : real -> real - fun double x = x + x val double = fn : int -> int

Compound Types Tuples, Records, Lists Tuples (14, "moo", true): int * string * bool Functions can take tuple argument - fun power (exp,base) = if exp = 0 then 1 else base * power(exp-1,base); val power = fn : int * int -> int - power(3,2); Curried Functions (named after Haskell Curry) Previous power - fun power (exp,base) = if exp = 0 then 1 else base * power(exp-1,base); val power = fn : int * int -> int Curried power function - fun cpower exp = fn base => if exp = 0 then 1 else base * cpower (exp-1) base; val cpower = fn : int -> (int -> int) Curried Functions (named after Haskell Curry) Why is this useful? - fun cpower exp base = if exp = 0 then 1 else base * cpower (exp-1) base; val cpower = fn : int -> (int -> int) Can define - val square = cpower 2 val square = fn : int -> int - square 3; val it = 9 : int Records Like tuple, but with labeled elements: - val x = { name="gus", salary=3.33, id=11 }; Selector operator: - #salary(x); val it = 3.33 : real - #name(x); val it = "Gus" : string

Lists Examples [1, 2, 3, 4], ["wombat", "numbat"] nil is empty list (sometimes written []) all elements must be same type Operations length length [1,2,3] 3 @ - append [1,2]@[3,4] [1, 2, 3, 4] :: - prefix 1::[2,3] [1, 2, 3] map map succ [1,2,3] [2,3,4] Lists Functions on Lists - fun product (nums) = if (nums = nil) then 1 else (hd nums) * product(tl nums); val product = fn : int list -> int - product([5, 2, 3]); val it = 30 : int; Pattern Matching List is one of two things: nil "first elem" :: "rest of elems" [1, 2, 3] = 1::[2,3] = 1::2::[3] = 1::2::3::nil Can define function by cases fun product (nil) = 1 product (x::xs) = x * product (xs); Patterns on Integers Patterns on integers fun listints 0 = [0] listints n = n::listints(n-1); listints 3 [3, 2, 1, 0]; More on patterns for other data types next time

Many Types Of Lists 1::2::nil : int list "wombat"::"numbat"::nil : string list What type of list is nil? - nil; val it = [] : 'a list Polymorphic type 'a is a type variable that represents any type 1::nil : int list "a"::nil : string list The Length Function Another Example fun length (nil) = 0 length (x::xs) = 1 + length (xs); What is the type of length? How about this one: fun id x = x; Polymorphism fun length (nil) = 0 length (x::xs) = 1 + length (xs); - val it = fun 'a list -> int fun id x = x; - val it = fun 'a -> 'a Type variable represents any type Patterns and Other Declarations Patterns can be used in place of variables Most basic pattern form val <pattern> = <exp>; Examples val x = 3; val tuple = ("moo", "cow"); val (x,y) = tuple; val mylist = [1, 2, 3]; val w::rest = mylist; val v::_ = mylist;

Datatype public static final int NORTH = 1; public static final int SOUTH = 2; public static final int EAST = 3; public static final int WEST = 4; public move(int x, int y, int dir) { switch (dir) { case NORTH:... case... } } Datatype datatype Direction = North South East West; fun move((x,y),north) = (x,y-1) move((x,y),south) = (x,y+1) ; Above is an incomplete pattern ML will warn you when you ve missed a case! proof by exhaustion