Data Types. Guest Lecture: Andrew Myers Spring 2018

Similar documents
Data Types. Prof. Clarkson Fall Today s music: Pokémon Theme by Jason Paige

Introduction to OCaml

CS Lecture 7: The dynamic environment. Prof. Clarkson Spring Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

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

CSE 341: Programming Languages

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

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

CS Lecture 5: Pattern Matching. Prof. Clarkson Fall Today s music: Puff, the Magic Dragon by Peter, Paul & Mary

Functions. Nate Foster Spring 2018

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

The Environment Model. Nate Foster Spring 2018

To figure this out we need a more precise understanding of how ML works

The Substitution Model

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

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

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

The Substitution Model. Nate Foster Spring 2018

The Environment Model

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CSE 130 [Winter 2014] Programming Languages

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

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

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

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Dan Grossman Fall 2011

CSE 3302 Programming Languages Lecture 8: Functional Programming

Functional Programming. Lecture 2: Algebra

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

Object-Oriented Principles and Practice / C++

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

Overloading, Type Classes, and Algebraic Datatypes

What does my program mean?

The story so far. Elements of Programming Languages. Pairs in various languages. Pairs

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

Programming Languages and Compilers (CS 421)

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

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

CSE 341 Section 5. Winter 2018

Programming Languages

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

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

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

Mutable Data Types. Prof. Clarkson Fall A New Despair Mutability Strikes Back Return of Imperative Programming

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

Programming Languages and Compilers (CS 421)

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

Standard ML. Data types. ML Datatypes.1

Fall 2018 Stephen Brookes. LECTURE 2 Thursday, August 30

Begin at the beginning

CSCI-GA Scripting Languages

Lecture #23: Conversion and Type Inference

CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey

A Functional Evaluation Model

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

Programming Languages

To figure this out we need a more precise understanding of how ML works

Fall Lecture 3 September 4. Stephen Brookes

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

From last time. News. Example. Example. Typechecking let. Typechecking let. Pick up your first two quizzes after class. Assignment #2 due tomorrow

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Recap: Functions as first-class values

Programming Languages

Programming Languages

Programming Languages

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

Monads. Prof. Clarkson Fall Today s music: Vámanos Pal Monte by Eddie Palmieri

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

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

Programming Languages

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

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

Recap: ML s Holy Trinity

Types, Expressions, and States

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

Programming Languages

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

Goal. CS152: Programming Languages. Lecture 15 Parametric Polymorphism. What the Library Likes. What The Client Likes. Start simpler.

CSE 341 : Programming Languages

Products and Records

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

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

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

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

CSE 341: Programming Languages

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

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

Lecture 5: The Untyped λ-calculus

Advanced Programming Handout 7. Monads and Friends (SOE Chapter 18)

Programming Languages

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

CS1 Lecture 3 Jan. 22, 2018

A Functional Space Model. COS 326 David Walker Princeton University

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

Monads. Functional Programming (CS4011) Monads

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

Programming Languages and Techniques (CIS120)

(Refer Slide Time: 4:00)

Overview. Elements of Programming Languages. Evaluation order. Evaluation order

Functional Programming. Pure Functional Programming

Transcription:

Data Types Guest Lecture: Andrew Myers Spring 2018

Review Previously in 3110: Functions Lists Today: Let expressions Ways to define your own data types: variants, records, tuples

LET EXPRESSIONS

Let expressions Slightly different than the let definitions we've been using at the toplevel Enable binding of variables to values inside another expression Since they are expressions, they evaluate to values let x = 2 in x+x (* ==> 4 *) let inc x = x+1 in inc 10 (* ==> 11 *) let y = "big" in let z = "red" in y^z (* ==> "bigred" *)

let expressions Syntax: let x = e1 in e2 x is an identifier e1 is the binding expression e2 is the body expression let x = e1 in e2 is itself an expression

let expressions let x = e1 in e2 Evaluation: Evaluate e1 to a value v1 Substitute v1 for x in e2, yielding a new expression e2 Evaluate e2 to v2 Result of evaluation is v2

Let expressions let x = 1+4 in x*3 --> Evaluate e1 to a value v1 let x = 5 in x*3 --> Substitute v1 for x in e2, yielding a new expression e2 5*3 --> Evaluate e2 to v2 15 Result of evaluation is v2

let expressions let x = e1 in e2 Type-checking: If e1:t1, and if e2:t2 (assuming that x:t1), then (let x = e1 in e2) : t2

Question Which of these does not evaluate to 3? A. let x = 3 B. let x = 2 in x+1 C. (fun x -> x+1) 2 D. let f x = x+1 in f 2 E. let f = fun x -> x+1 in f 2

Question Which of these does not evaluate to 3? A. let x = 3 B. let x = 2 in x+1 C. (fun x -> x+1) 2 D. let f x = x+1 in f 2 E. let f = fun x -> x+1 in f 2

Anonymous functions These two expressions are syntactically different but semantically equivalent: let x = 2 in x+1 (fun x -> x+1) 2 Let expressions are syntactic sugar for anonymous function application

Let definitions in toplevel Syntax: let x = e Implicitly, in rest of what you type E.g., you type: let a="big";; let b="red";; let c=a^b;; Toplevel understands as let a="big" in let b="red" in let c=a^b in

VARIANTS

Variant type day = Sun Mon Tue Wed Thu Fri Sat let int_of_day d = match d with Sun -> 1 Mon -> 2 Tue -> 3 Wed -> 4 Thu -> 5 Fri -> 6 Sat -> 7

Building and accessing variants Syntax: type t = C1... Cn the Ci are called constructors Evaluation: a constructor is already a value Type checking: Ci : t Accessing: use pattern matching; constructor name is a pattern

Pokémon variant

Pokémon variant type ptype = TNormal TFire TWater type peff = ENormal ENotVery ESuper let eff_to_float = function ENormal -> 1.0 ENotVery -> 0.5 ESuper -> 2.0

RECORDS AND TUPLES

Records Several pieces of data glued together A record contains several named fields Before you can use a record, must define a record type: Why? Clean type inference. type mon = {name: string; hp : int; ptype: ptype}

Records To build a record: Write a record expression: {name="charmander"; hp=39; ptype=tfire} Order of fields doesn t matter: {name="charmander"; ptype=tfire; hp=39} is equivalent To access a record's field: r.hp Or can use pattern matching with record patterns: {f1=p1;...; fn=pn} I guess you could call that record breaking

Pattern matching records (* OK *) let get_hp m = match m with {name=n; hp=h; ptype=t} -> h (* better *) let get_hp m = match m with {name=_; hp=h; ptype=_} -> h

Advanced pattern matching records (* better *) let get_hp m = match m with {name; hp; ptype} -> hp (* better *) let get_hp m = match m with {hp} -> hp (* best *) let get_hp m = m.hp

By name vs. by position Fields of record are identified by name order we write fields in expression is irrelevant Opposite choice: identify by position

Tuples Several pieces of data glued together A tuple contains several components (Don't have to define tuple type before use) e.g., (1,2,10) 1,2,10 (true, "Hello") ([1;2;3], (0.5,'X'))

Tuple types (1,2,10) : int*int*int 1,2,10 : int*int*int (true, "Hello") : bool*string ([1;2;3], (0.5,'X')) : int list * (float*char)

Tuples 2-tuple: pair 3-tuple: triple beyond that: maybe better to use records We need language constructs to build tuples and to access the components Building is easy: just write the tuple, as before Accessing uses pattern matching...

Accessing tuples New kind of pattern, the tuple pattern: (p1,..., pn) match (1,2,3) with (x,y,z) -> x+y+z (* ==> 6 *) let thrd t = match t with (x,y,z) -> z (* thrd : 'a*'b*'c -> 'c *) Note: we never needed more than one branch in the match expression...

Pattern matching without match (* OK *) let thrd t = match t with (x,y,z) -> z (* good *) let thrd t = let (x,y,z) = t in z (* better *) let thrd t = let (_,_,z) = t in z (* best *) let thrd (_,_,z) = z

Extended syntax for let Previously we had this syntax: let x = e1 in e2 let [rec] f x1... xn = e1 in e2 Everywhere we had a variable identifier x, we can really use a pattern! let p = e1 in e2 let [rec] f p1... pn = e1 in e2 Old syntax is just a special case of new syntax, since a variable identifier is a pattern

Pattern matching arguments (* OK *) let sum_triple t = let (x,y,z) = t in x+y+z (* better *) let sum_triple (x,y,z) = x+y+z Note how that last version looks syntactically like a function in C/Java!

Accessing pairs Built-in projection functions for first and second components: let fst (x,_) = x let snd (_,y) = y

Question What is the type of this expression? let (x,y) = snd("big",("red",42)) in (42,y) A. {x:string; y:int} B. int*int C. string*int D. int*string E. string*(string*int)

Question What is the type of this expression? let (x,y) = snd("big",("red",42)) in (42,y) A. {x:string; y:int} B. int*int C. string*int D. int*string E. string*(string*int)

Pokémon effectiveness

Pokémon effectiveness let eff = function (TFire,TFire) -> ENotVery (TWater,TWater) -> ENotVery (TFire,TWater) -> ENotVery (TWater,TFire) -> ESuper _ -> ENormal

Semantics of tuples and records Straightforward: see the notes, and slides at the end of this lecture

Upcoming events [Wed] A0 due

FOR RECITATION

Record expressions Syntax: {f1 = e1; ; fn = en} Evaluation: If e1 evaluates to v1, and en evaluates to vn Then {f1 = e1; ; fn = en} evaluates to {f1 = v1,, fn = vn} Result is a record value Type-checking: If e1 : t1 and e2 : t2 and en : tn, and if t is a defined type of the form {f1:t1,, fn:tn} then {f1 = e1; ; fn = en}: t

Record field access Syntax: e.f Evaluation: If e evaluates to {f = v, } Then e.f evaluates to v Type-checking: If e : t1 and if t1 is a defined type of the form {f:t2, } then e.f : t2

Evaluation notation We keep writing statements like: If e evaluates to {f = v, } then e.f evaluates to v Let's introduce a shorthand notation: Instead of "e evaluates to v" write "e ==> v" So we can now write: If e ==> {f = v, } then e.f ==> v

Building tuples Syntax: (e1,e2,...,en) parens are optional Evaluation: If ei ==> vi Then (e1,...,en) ==> (v1,...,vn) A tuple of values is itself a value Type-checking: If ei:ti then (e1,...,en):t1*...*tn