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

Similar documents
News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

Programming Languages

Programming Languages

Programming Languages

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

OCaml. History, Variants. ML s holy trinity. Interacting with ML. Base type: Integers. Base type: Strings. *Notes from Sorin Lerner at UCSD*

Any questions. Say hello to OCaml. Say hello to OCaml. Why readability matters. History, Variants. Plan (next 4 weeks)

So what does studying PL buy me?

Programming Languages

Programming Languages

CSE 130 [Winter 2014] Programming Languages

Begin at the beginning

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

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

Programming Languages

Recap: ML s Holy Trinity

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

CSE 130 Programming Languages. Datatypes. Ranjit Jhala UC San Diego

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

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

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

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

Programming Languages

Recap: Functions as first-class values

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

A Programming Language. A different language g is a different vision of life. CSE 130 : Fall Two variables. L1: x++; y--; (y=0)?

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

A Programming Language. A different language g is a different vision of life. CSE 130 : Spring Two variables. L1: x++; y--; (y=0)?

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

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

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

Programming Languages

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

Variables and Bindings

CMSC 330, Fall 2013, Practice Problem 3 Solutions

CSE 130 [Spring 2014] Programming Languages

Programming Languages. So why study PL? A Programming Language. A different language is a different vision of life. CSE 130 : Fall 2015

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

Programming Languages. So why study PL? A Programming Language. A different language is a different vision of life. CSE 130 : Spring 2015

Programming Languages. Tail Recursion. CSE 130: Winter Lecture 8: NOT TR. last thing function does is a recursive call

Exercises on ML. Programming Languages. Chanseok Oh

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

CSCI-GA Scripting Languages

sum: tree -> int sum_leaf: tree -> int sum_leaf: tree -> int Representing Trees Next: Lets get cosy with Recursion Sum up the leaf values. E.g.

Which of the following expressions has the type int list?

Introduction to OCaml

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

Today s Plan. Programming Languages. Example : Factorial. Recursion. CSE 130 : Spring Lecture 6: Higher-Order Functions

Programming in Standard ML: Continued

CMSC 330: Organization of Programming Languages. Lets, Tuples, Records

A quick introduction to SML

CMSC 330, Fall 2013, Practice Problems 3

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

Functional Programming

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

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

News. CSE 130: Programming Languages. Environments & Closures. Functions are first-class values. Recap: Functions as first-class values

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

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Lists. Prof. Clarkson Fall Today s music: "Blank Space" by Taylor Swift

SML A F unctional Functional Language Language Lecture 19

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

CS 11 Haskell track: lecture 1

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

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

Chapter 3 Linear Structures: Lists

Chapter 15. Functional Programming Languages

Lecture #23: Conversion and Type Inference

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Zach Tatlock Winter 2018

Function definitions. CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Example, extended. Some gotchas. Zach Tatlock Winter 2018

Introduction to OCaml

Thinking Induc,vely. COS 326 David Walker Princeton University

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

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

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Chapter 3 Linear Structures: Lists

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

Haskell 5.1 INTERACTIVE SESSIONS AND THE RUN-TIME SYSTEM

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

CPS 506 Comparative Programming Languages. Programming Language Paradigm

An introduction introduction to functional functional programming programming using usin Haskell

Lists. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

CSc 372 Comparative Programming Languages

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

Handout 2 August 25, 2008

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

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Programming Languages and Compilers (CS 421)

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Parametric types. map: ( a (a b) a list b list. filter: ( a bool) a list a list. Why? a and b are type variables!

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

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

Datatype declarations

CSE 307: Principles of Programming Languages

Shell CSCE 314 TAMU. Haskell Functions

Data Types The ML Type System

CSE 307: Principles of Programming Languages

Specification and Verification in Higher Order Logic

Transcription:

OCaml The PL for the discerning hacker. ML Flow Expressions (Syntax) Compile-time Static 1. Enter expression 2. ML infers a type Exec-time Dynamic Types 3. ML crunches expression down to a value 4. Value guaranteed to have type Values (Semantics) Typing -> Eval Always Works 1 4 []; [] a list [1;2;3]; [1;2;3] int list [1+1;2+2;3+3;4+4]; [2;4;6;8] int list [ a ; b ; c ^ d ]; [ a ; b ; cd ] string list [(1, a ^ b );(3+4, c )]; [(1, ab );(7, c )] (int*string) list [[1];[2;3];[4;5;6]]; [[1];[2;3];[4;5;6]]; (int list) list [1; pq ]; All elements must have same type Unbounded size Can have lists of anything (e.g. lists of lists) But 5 6

Question 1 Which of these causes a type error? List operator Cons :: (a) [1; 2; 3] (b) [ 1, 2, 3] (c) [1; 2; 3] (d) (1, 2, 3) (e) [ 1 ; 2; 3] 1::[]; [1] int list 1::[2;3]; [1;2;3] int list a ::[ b ; c ]; [ a ; b ; c ] string list Can only cons element to a list of same type 1::[ b ; cd ]; 7 8 Lists: Construct Nil operator List operator Append @ [] Cons operator []: a list [] => [] [1;2]@[3;4;5]; [1;2;3;4;5] int list [ a ]@[ b ]; [ a ; b ] string list []@[1]; [1] int list 1::[2;3] [1;2;3] int list Can only append two lists 1 @ [2;3]; e1:t e2: T list e1=>v1 e2=> v2 of the same type [1] @ [ a ; b ]; e1::e2 : T list e1::e2 => v1::v2 9 10

List operator head hd List operator tail tl hd [1;2]; 1 int tl [1;2;3]; [2;3] int list hd ([ a ]@[ b ]); a string tl ([ a ]@[ b ]); [ b ] string list Only take the head a nonempty list hd []; Only take the tail of nonempty list tl []; 11 12 Question 2: What is result of? (hd [[];[1;2;3]]) = (hd [[];[ a ]]) (b) true : bool (c) false : bool (d) Type Error (hd) (e) Type Error (=) Lists: Deconstruct e :T list e => v1::v2 Head hd e : T hd e => v1 Tail e :T list e => v1::v2 tl e : T list tl e => v2 (hd [[];[1;2;3]]) = (hd [[];[ a ]]) int list e 1 :T e 2 :T string list e 1 =e 2 : bool 13 14

Recap: Tuples vs. Lists? What s the difference? Tuples: Different types, but fixed number: Lists: pair = 2 elts (3, abcd,(3.5,4.2)) (int * string * (float * float)) triple = 3 elts Same type, unbounded number: Syntax: (3, abcd ) (int * string) [3;4;5;6;7] int list Tuples = comma L i s t s = semicolon So far, a fancy calculator what do we need next? 15 16 So far, a fancy calculator Question 3: What is result of? Branches if (1 < 2) then true else false (b) true (c) false (d) Type Error 17 18

Question 4: What is result of? if (1 < 2) then [1;2] else 5 (b) [1;2] (c) 5 (d) Type Error If-then-else expressions e1 : bool e2: T e3: T if e1 then e2 else e3 : T Then-subexp, Else-subexp must have same type! Equals type of resulting expression if 1>2 then [1,2] else [] [] if 1<2 then [] else [ a ] [] int list string list (if 1>2 then [1,2] else [])=(if 1<2 then [] else [ a ]) 19 20 If-then-else expressions So far, a fancy calculator if (1 < 2) then [1;2] else 5 if false then [1;2] else 5 then-subexp, else-subexp must have same type! which is the type of resulting expression Variables e1 : bool e2: T e3: T if e1 then e2 else e3 : T 21 22

Question 5: I got this @ prompt # [x+x; x*x] ;; - : int list = [20; 100] What had I typed before? (a) x = 10; (b) int x = 10; (c) x == 10; (d) let x = 10; (e) x := 10; Variables and bindings let x = e;; Bind the value of expression e to the variable x # let x = 2+2;; val x : int = 4 23 24 Variables and bindings Later declared expressions can use x Most recent bound value used for evaluation # let x = 2+2;; val x : int = 4 # let y = x * x * x;; val y : int = 64 # let z = [x;y;x+y];; val z : int list = [4;64;68] # Variables and bindings Undeclared variables (i.e. without a value binding) are not accepted! # let p = a + 1; Characters 8-9: let p = a + 1 ;; ^ Unbound value a Catches many bugs due to typos 25 26

Local bindings for expressions using temporary variables let tempvar = x + 2 * y in tempvar * tempvar ;; 17424 tempvar is bound only inside expr body from in Not visible ( not in scope ) outside int Question 6: What is result of? let x = 10 in (let z = 10 in x + z) + z (b) 30 (c) Unbound Error -- x (d) Unbound Error -- z (e) Type Error 27 28 Binding by Pattern-Matching Simultaneously bind several variables # let (x,y,z) = (2+3, a ^ b, 1::[2]);; val x : int = 5 val y : string = ab val z : int list = [1;2] 29 Binding by Pattern-Matching But what of: # let h::t = [1;2;3];; Warning P: this pattern-matching not exhaustive. val h : int = 1 val t : int list = [2;3] Why is it whining? # let h::t = []; Exception: Match_failure # let XS = [1;2;3]; val xs = [1;2;3]: list - val h::t = xs; Warning: Binding not exhaustive val h = 1 : int val t = [2;3] : int In general xs may be empty (match failure!) Another useful early warning 30

Binding by Pattern-Matching But what of: # let h::t = [1;2;3];; Warning NEVER P: this USE pattern-matching PATTERN MATCHING not exhaustive. val h : int = 1 LIKE THIS val t : int list = [2;3] Why is it whining? let h::t =... # let h::t = []; Exception: Match_failure # let XS = [1;2;3]; val xs = [1;2;3]: list - val h::t = xs; match l with... Warning: Binding not exhaustive val h = 1 : int val (coming t = [2;3] up : soon, int but this is important) ALWAYS USE THIS FORM INSTEAD In general xs may be empty (match failure!) Functions Another useful early warning 31 32 Functions up now, remember Complex types: Functions! Expression Value Parameter (formal) Body Expr fun x -> x+1;; fn Type int -> int Everything is an expression Everything has a value Everything has a type A function is a value! # let inc = fun x -> x+1 ; val inc : int -> int = fn # inc 0; val it : int = 1 # inc 10; val it : int = 11 33 34

A Problem A Solution: Simultaneous Binding Parameter (formal) Body Expr Parameter (formal) Body Expr fun x -> x+1;; fn fun (x,y) -> x<y; fn int -> int (int * int) -> bool Functions only have ONE parameter?! How a call ( application ) is evaluated: 1. Evaluate argument 2. Bind formal to arg value 3. Evaluate Body expr Functions only have ONE parameter? How a call ( application ) is evaluated: 1. Evaluate argument 2. Bind formal to arg value 3. Evaluate Body expr 35 36 Another Solution ( Currying ) Parameter (formal) Body Expr fun x -> fun y-> x<y; int -> (int -> bool) Whoa! A function can return a function fn # let lt = fun x -> fun y -> x < y ; val lt : int -> int -> bool = fn # let is5lt = lt 5; val is5lt : int -> bool = fn; # is5lt 10; val it : bool = true; # is5lt 2; val it : bool = false; Question 7: What is result of? (fun x -> not x) (b) <fun> : int -> int (c) <fun> : int -> bool (d) <fun> : bool -> int (e) <fun> : bool -> bool 37 38

And how about Parameter (formal) Body Expr fun f -> fun x -> not(f x); ( a ->bool) -> ( a -> bool) A function can also take a function argument # let neg = fun f -> fun x -> not (f x); val lt : int -> int -> bool = fn # let is5gte = neg is5lt; val is5gte : int -> bool = fn # is5gte 10; val it : bool = false; # is5gte 2; val it : bool = true; (* odd, even *) fn 39 Question 8: What is result of? (fun f -> (fun x -> (f x)+x)) (b) Type Error (c) <fun> : int -> int -> int (d) <fun> : int -> int (e) <fun> : (int->int)-> int -> int 40 A shorthand for function binding # let neg = fun f -> fun x -> not (f x); # let neg f x = not (f x); val neg : int -> int -> bool = fn # let is5gte = neg is5lt; val is5gte : int -> bool = fn; # is5gte 10; val it : bool = false; # is5gte 2; val it : bool = true; 41 Put it together: a filter function If arg matches this pattern then use this Body Expr - let rec filter f xs = match xs with [] -> [] (x::xs )-> if f x then x::(filter f xs ) else (filter f xs );; val filter : ( a->bool)-> a list-> a lisi) = fn # let list1 = [1;31;12;4;7;2;10];; # filter is5lt list1 ;; val it : int list = [31;12;7;10] # filter is5gte list1;; val it : int list = [1;4;2] # filter even list1;; val it : int list = [12;4;2;10] 42

Put it together: a partition function A little trick # 2 <= 3;; val it : bool = true # ba <= ab ;; val it : bool = false # let partition f l = (filter f l, filter (neg f) l); val partition :( a->bool)-> a list-> a list * a list = fn # let list1 = [1,31,12,4,7,2,10]; - # partition is5lt list1 ; val it : (int list * int list) = ([31,12,7,10],[1,2,10] # partition even list1; val it : (int list * int list) = ([12,4,2,10],[1,31,7]) # let lt = (<) ;; val it : a -> a -> bool = fn # lt 2 3;; val it : bool = true; # lt ba ab ;; val it : bool = false; # let is5lt = lt 5; val is5lt : int -> bool = fn; # is5lt 10; val it : bool = true; # is5lt 2; val it : bool = false; 43 44 Put it together: a quicksort function let rec sort xs = match xs with [] -> [] (h::t) -> let (l,r) = partition ((<) h) t in (sort l)@(h::(sort r)) Now, lets begin at the beginning 45