CSE 130 [Winter 2014] Programming Languages

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

Programming Languages

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

Recap: ML s Holy Trinity

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

Programming Languages. Datatypes

Programming Languages

Programming Languages. Next: Building datatypes. Suppose I wanted. Next: Building datatypes 10/4/2017. Review so far. Datatypes.

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

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

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

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*

Programming Languages

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

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

Programming Languages

So what does studying PL buy me?

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

Programming Languages

Programming Languages

Begin at the beginning

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

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

Recap: Functions as first-class values

Introduction to OCaml

Programming Languages

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

CSE 130 [Winter 2014] Programming Languages

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. Lets, Tuples, Records

Exercises on ML. Programming Languages. Chanseok Oh

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

CMSC 330, Fall 2013, Practice Problem 3 Solutions

Which of the following expressions has the type int list?

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

CSC324 Functional Programming Typing, Exceptions in ML

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

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

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

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

CSCI-GA Scripting Languages

CS 11 Haskell track: lecture 1

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

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

CSE 341 Section 5. Winter 2018

CS 321 Programming Languages

Programming Languages and Techniques (CIS120)

Variables and Bindings

CMSC 330: Organization of Programming Languages

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

CSE 341, Autumn 2005, Assignment 3 ML - MiniML Interpreter

A quick introduction to SML

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CSCI 2041: Functions, Mutation, and Arrays

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

Handout 2 August 25, 2008

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

Programming in Standard ML: Continued

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

Programming Languages and Compilers (CS 421)

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

CMSC 330: Organization of Programming Languages. Rust Basics

An introduction introduction to functional functional programming programming using usin Haskell

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

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

Functions & First Class Function Values

CMSC 330: Organization of Programming Languages. OCaml Data Types

Programming Languages Lecture 14: Sum, Product, Recursive Types

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

Data Types. Guest Lecture: Andrew Myers Spring 2018

Lecture #23: Conversion and Type Inference

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

CIS 120 Midterm I February 16, 2015 SOLUTIONS

CMSC 430 Introduction to Compilers. Fall Everything (else) you always wanted to know about OCaml (but were afraid to ask)

CSE341 Spring 2016, Midterm Examination April 29, 2016

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

CMSC 330, Fall 2013, Practice Problems 3

CS558 Programming Languages

CSE341 Spring 2017, Midterm Examination April 28, 2017

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

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

CMSC 330: Organization of Programming Languages

CSE 130, Fall 2006: Final Examination

CSE 130: Programming Languages. Polymorphism. Ranjit Jhala UC San Diego

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

L3 Programming September 19, OCaml Cheatsheet

CSE341, Fall 2011, Midterm Examination October 31, 2011

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

Standard ML. Data types. ML Datatypes.1

Programming Language Concepts, cs2104 Lecture 04 ( )

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Programming Languages and Techniques (CIS120)

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Programming Languages. Programming with λ-calculus. Lecture 11: Type Systems. Special Hour to discuss HW? if-then-else int

Transcription:

CSE 130 [Winter 2014] Programming Languages Introduction to OCaml (Continued) Ravi Chugh! Jan 14

Announcements HW #1 due Mon Jan 20 Post questions/discussion to Piazza Check Piazza for TA/Tutor lab hours Must bring clickers starting Thurs

Variables and Bindings let x = e;;! Bind the value of expression e to the variable x # let x = 2 + 2;; val x : int = 4

Local Bindings: let x = e1 in e2 for expressions using temporary variables let tempvar = x + 2 * y in tempvar * tempvar ;; 17424 int tempvar is bound only inside expr body from in Not visible ( not in scope ) outside

Clicker Frequency for This Room 1. Press and hold until blinking 2. Enter BD

Clicker Question What is the result of let x = 10 in (let z = 10 in x + z) + z? (a) Syntax Error (b) 30 (c) Unbound Var Error (x) (d) Unbound Var Error (z) (e) Other Type Error

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]

Binding by Pattern-Matching But: # 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 : int list = [1;2;3] # let h::t = xs;; Warning: Binding not exhaustive val h : int = 1 val t : int list = [2;3] In general, xs may be empty (match failure!) Another useful early warning

What About Functions!

Remember the Holy Trinity Expression Value Type Everything is an expression Everything has a value Everything has a type Functions are values!

Complex Type: Functions! Parameter (formal) Body Expr fun x -> x+1 <fun> int -> int # let inc = fun x -> x+1 ;; val inc : int -> int = <fun> # inc 0 ;; - : int = 1 # inc 10 ;; - : int = 11 How a call ( application ) is evaluated: 1. Evaluate argument 2. Bind formal to arg value 3. Evaluate Body expr

Complex Type: Functions! Parameter (formal) Body Expr fun x -> x+1 <fun> int -> int Functions only have ONE parameter?!?

Solution #1: Simultaneous Binding Parameter (formal) fun (x,y) -> x<y <fun> (int * int) -> bool Functions only have ONE parameter?!?

Solution #2: Currying Parameter (formal) Body Expr fun x -> fun y-> x<y <fun> int -> (int -> bool) Whoa! A function can return a function # let lt = fun x -> fun y -> x < y ;; val lt : int -> int -> bool = <fun> # let is5lt = lt 5 ;; val is5lt : int -> bool = <fun> # is5lt 10 ;; - : bool = true # is5lt 2 ;; - : bool = false

Clicker Question What is the result of (fun x -> not x)? (a) Syntax Error (b) <fun> : int -> int (c) <fun> : int -> bool (d) <fun> : bool -> int (e) <fun> : bool -> bool

Higher-Order Function Parameter (formal) Body Expr fun f -> fun x -> not(f x) <fun> ( 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 = <fun> # let is5gte = neg is5lt ;; val is5gte : int -> bool = <fun> # is5gte 10 ;; - : bool = false # is5gte 2 ;; - : bool = true (* odd, even *)

Clicker Question What is the result of (fun f -> (fun x -> (f x) + x))? (a) Syntax Error (b) <fun> : int -> int -> int (c) <fun> : int -> int (d) <fun> : (int -> int) -> int -> int

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 = <fun> # let is5gte = neg is5lt ;; val is5gte : int -> bool = <fun> # is5gte 10 ;; - : bool = false # is5gte 2 ;; - : bool = true

If xs matches this pattern A Filter Function # let rec filter f xs = match xs with [] -> [] (x::xs )-> if f x then use this Body Expr then x::(filter f xs ) else (filter f xs ) ;; val filter : ( a->bool)-> a list-> a list = <fun> # let list1 = [1; 31; 12; 4; 7; 2; 10] ;; # filter is5lt list1 ;; - : int list = [31; 12; 7; 10] # filter is5gte list1 ;; - : int list = [1; 4; 2] # filter even list1 ;; - : int list = [12; 4; 2; 10]

A Partition Function # let partition f l = (filter f l, filter (neg f) l) ;; val partition : ( a->bool)-> a list-> a list * a list # let list1 = [1; 31; 12; 4; 7; 2; 10] ;; - # partition is5lt list1 ;; - : (int list * int list) = ([31;12;7;10],[1;4;2]) # partition even list1 ;; - : (int list * int list) = ([12;4;2;10],[1;31;7])

Operators are Functions # 2 <= 3 ;; - : bool = true # ba <= ab ;; - : bool = false # let lt = (<) ;; val lt : a -> a -> bool = <fun> # lt 2 3 ;; - : bool = true # lt ba ab ;; - : bool = false # let is5lt = lt 5 ;; val is5lt : int -> bool = <fun> # is5lt 10 ;; - : bool = true # is5lt 2 ;; - : bool = false

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, let s begin at the beginning

CSE 130 [Winter 2014] Programming Languages Datatypes Ravi Chugh!

What About More Complex Data? We ve seen some base types and values: Integers, Floats, Bool, String, etc. Some ways to build up types: Tuples (products) and lists Functions Records (we will see in a few weeks) Design Principle: Orthogonality Don t clutter core language with stuff Few, powerful orthogonal building techniques Put derived types, values, functions in libraries!

3 Ways to Build Complex Values Tuple (a.k.a. Each-of, Product ) Type type t = (t1 * t2) Value of t contains value of t1 and a value of t2 Data (a.k.a. One-of, Variant ) Type Recursive Datatype

3 Ways to Build Complex Values Tuple (a.k.a. Each-of, Product ) Type type t = (t1 * t2) Value of t contains value of t1 and a value of t2 Data (a.k.a. One-of, Variant ) Type type t = C1 of t1 C2 of t2 Value of t contains value of t1 or a value of t2 Recursive Datatype

Supposed I Wanted a program that processed lists of attributes Name (string) Age (integer) DOB (int-int-int) Address (string) Height (float) Alive (boolean) Phone (int-int) Email (string) Many kinds of attributes Can have multiple names, phones, emails, etc. Want to store them in a list. Can I?!

Supposed I Wanted Attributes: Name (string) Age (integer) DOB (int-int-int) Address (string) Height (real) Alive (boolean) Phone (int-int) email (string)! type attrib = Name of string Age of int DOB of int*int*int Address of string Height of float Alive of bool Phone of int*int Email of string;;

Clicker Question type attrib = Name of string Age of int Height of float What is the result of Name Tony Stark? (a) Syntax Error (b) Type Error (c) string (d) attrib (e) a

Clicker Question type attrib = Name of string Age of int Height of float What is the result of Name Tony ^ Name Stark? (a) Syntax Error (b) Type Error (c) string (d) attrib (e) a

Constructing Datatypes type t = C1 of t1 C2 of t2 Cn of tn t is a new datatype A value of type t is either: a value of type t1 placed in a box labeled C1 Or a value of type t2 placed in a box labeled C2 Or Or a value of type tn placed in a box labeled Cn!

Constructing Datatypes type t = C1 of t1 C2 of t2 Cn of tn Label=C1 Value=t1 OR Label=C2 Value=t2 OR Label=Cn Value=tn All have the type t

Clicker Question type attrib = Name of string Age of int Height of float What is the result of Age Tony Stark? (a) Syntax Error (b) Type Error (c) string (d) attrib (e) a

How to PUT values into box?

How to PUT values into box? How to create values of type attrib?! type attrib = Name of string Age of int DOB of int*int*int Address of string Height of float Alive of bool Phone of int*int Email of string;; # let a1 = Name Ravi ;; val a1 : attrib = Name Ravi # let a2 = Height 5.58 ;; val a2 : attrib = Height 5.58 # let year = 1984 ;; val year : int = 1984 # let a3 = DOB (11,5,year) ;; val a3 : attrib = DOB (11,5,1984) # let attrs = [a1;a2;a3] ;; val attrs : attrib list =

Constructing Datatypes type attrib = Name of string Age of int DOB of int*int*int Address of string Height of float Alive of bool Phone of int*int Email of string ;; Name Height DOB Ravi OR 5.58 OR (11,5,84) Name Ravi Height 5.58 DOB (11,5,84) All have type attrib

Clicker Question type attrib = Name of string Age of int Height of float What is the result of [Name Ravi ; Height 5.58; DOB(11,5,84)]? (a) Syntax Error (b) Type Error (c) attrib list (d) (string*float*(int*int*int)) list (e) a list

One-of Types We ve defined a one-of type named attrib Elements are one of: string int int*int*int float bool type attrib = Name of string Age of int DOB of int*int*int Address of string Height of float Alive of bool Phone of int*int Email of string;; Can create uniform attrib lists Say I want a function to print attributes!

How to TEST & GET what s in box? Is it a... string? or an int? or an int*int*int? or...

How to TEST & GET what s in box? Look at TAG!

Clicker Question type attrib = Name of string Age of int What is the result of let welcome a = match a with Name s -> s in welcome (Name Ravi )? (a) Type Error (b) Name Ravi : a (c) Name Ravi : attrib (d) Ravi : string (e) Run-time Error

How to TEST & GET what s in box? type attrib =! Name of string! Age of int! DOB of int*int*int! Address of string! Height of float! Alive of bool! Phone of int*int! match e with Name s -> (*s: string *)! Age i -> (*i: int *)! DOB(d,m,y) -> (*d: int, m:int, y:int*)! Address a -> (*a: string *)! Height h -> (*h: float *)! Alive b -> (*b: bool *)! Phone(a,r) -> (*a: int, r: int *) Pattern-match expression Simultaneously test and extract contents of box If e matches the pattern form, then: value in box bound to pattern variable matching result expression is evaluated Else: try next pattern

How to TEST & GET what s in box? match e with Name s -> printf Hello %s\n" s! Age i -> printf "%d" i! DOB(d,m,y) -> printf "%d/%d/%d" d m y! Address s -> printf "%s" s! Height h -> printf "%f" h! Alive b -> printf "%b" b s! Phone(a,r) -> printf "(%d)-%d" a r

How to TEST & GET what s in box? match match (Name e with Ravi ) with Name s -> printf Hello %s\n" s! Age i -> printf "%d" i! DOB(d,m,y) -> printf "%d/%d/%d" d m y! Address s -> printf "%s" s! Height h -> printf "%f" h! Alive b -> printf "%b" b s! Phone(a,r) -> printf "(%d)-%d" a r Hello Ravi - : unit = () First case matches the tag (Name) Evals branch with s bound to string contents!

match-with is an Expression { t match e with Name s -> e1 Age i -> e2 DOB (m,d,y) -> e3 Address a -> e4 Height h -> e5 Alive b -> e6 Phone (a,n) -> e7 Email e -> e8 t Type Rule e1, e2,,en must have same type t! Type of whole expression is t!

Clicker Question type attrib = Name of string Age of int What is the result of let welcome a = match a with Name s -> s in welcome (Age 29)? (a) Type Error (b) Name Ravi : a (c) Name Ravi : attrib (d) Ravi : string (e) Run-time Error

How to TEST & GET what s in box? BEWARE! Be sure to handle all TAGS!

Beware! Handle All Tags! # match (Name Ravi ) with Age i - > Printf.printf "%d" i Email s - > Printf.printf "%s" s ;; Exception: Match Failure!! None of the cases matched the tag (Name), so crash with nasty Run-Time Error!

Beware! Handle All Tags! # match (Name Ravi ) with Age i - > Printf.printf "%d" i Email s - > Printf.printf "%s" s ;; Warning P: this pattern- matching is not exhaustive. Here is an example of a value that is not matched: Phone (_, _) Exception: Match Failure!! ML gives a compile-time warning about missing cases!!

Clicker Question type attrib = Name of string Age of int What is the result of let welcome a = match a with Name s -> Hello, ^ s ^! Name s -> Hi, ^ s ^! in welcome (Name Ravi )? (a) Type Error (b) Hello, Ravi! (c) Hi, Ravi! : string : string (d) Hello, Ravi! Hi, Ravi! : string (e) Run-time Error

Compiler to the Rescue! # let printattrib a = match a with Name s - > Printf.printf "%s" s Age i - > Printf.printf "%d" i DOB (d,m,y) - > Printf.printf "%d / %d / %d" d m y... Age i - > Printf.printf "%d" i ;; Warning U: this match case is unused. val printattrib : attrib - > unit = <fun> ML gives a compile-time warning about redundant cases (which will never match)!!

Clicker Question type attrib = Name of string Age of int What is the result of let welcome a = match a with Name s -> s Age i -> i in welcome (Name Ravi )? (a) Type Error (b) Name Ravi : attrib (d) Ravi : string (e) Run-time Error

Clicker Question type attrib = Name of string Age of int What is the result of let welcome a = match a with Name s -> a Age i -> a in welcome (Name Ravi )? (a) Type Error (b) Name Ravi : attrib (d) Ravi : string (e) Run-time Error

Benefits of match-with type t = C1 of t1 C2 of t2 Cn of tn match e with C1 x1 -> e1 C2 x2 -> e2 Cn xn -> en 1. Simultaneous test-extract-bind 2. Compile-time checks for: missed cases: ML warns if you miss a t value redundant cases: ML warns if a case never matches!

3 Ways to Build Complex Values Tuple (a.k.a. Each-of, Product ) Type type t = (t1 * t2) Value of t contains value of t1 and a value of t2 Data (a.k.a. One-of, Variant ) Type type t = C1 of t1 C2 of t2 Value of t contains value of t1 or a value of t2 Recursive Datatype type t =... C of (... * t) Value of t contains (sub)-value of same type t

Recursive Types type nat = Zero Succ of nat Wait a minute! Zero of what?! Means empty box with label Zero

Recursive Types type nat = Zero Succ of nat What are values of nat? Zero

Recursive Types type nat = Zero Succ of nat What are values of nat? One nat contains another! Succ Zero

Recursive Types type nat = Zero Succ of nat What are values of nat? One nat contains another! Succ Succ Zero

Recursive Types type nat = Zero Succ of nat What are values of nat? One nat contains another! Succ Succ Succ Zero

3 Ways to Build Complex Values Tuple (a.k.a. Each-of, Product ) Type type t = (t1 * t2) Value of t contains value of t1 and a value of t2 Data (a.k.a. One-of, Variant ) Type type t = C1 of t1 C2 of t2 Value of t contains value of t1 or a value of t2 Recursive Datatype type t =... C of (... * t) Value of t contains (sub)-value of same type t