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

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

Programming Languages

Programming Languages

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

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

Programming Languages

Recap: ML s Holy Trinity

Programming Languages

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

Programming Languages. Datatypes

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

Programming Languages

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

CSE 130 [Winter 2014] Programming Languages

Programming Languages

Programming Languages

Programming Languages

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

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

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

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

Begin at the beginning

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

Programming Languages Lecture 14: Sum, Product, Recursive Types

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.

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

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

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

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

Exercises on ML. Programming Languages. Chanseok Oh

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

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

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

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

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

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

Programming Languages. Example 5. Example 4. CSE 130 : Fall type, can reuse code for all types! let rec cat l = match l with

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

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

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

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

CSE 341: Programming Languages

Programming Languages Lecture 15: Recursive Types & Subtyping

CS558 Programming Languages

Handout 2 August 25, 2008

Lists. Michael P. Fourman. February 2, 2010

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

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

Homework 3 COSE212, Fall 2018

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

Programming Languages and Techniques (CIS120)

CS 11 Haskell track: lecture 1

Programming Languages

CS558 Programming Languages

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

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

Metaprogramming assignment 3

CSE 341: Programming Languages

Fall 2018 Lecture N

Recursion. Q: What does this evaluate to? Q: What does this evaluate to? CSE 130 : Programming Languages. Higher-Order Functions

Functional Programming

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

F28PL1 Programming Languages. Lecture 14: Standard ML 4

Type Checking and Type Inference

Cornell University 12 Oct Solutions. (a) [9 pts] For each of the 3 functions below, pick an appropriate type for it from the list below.

CS558 Programming Languages

Datatype declarations

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

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Logic - CM0845 Introduction to Haskell

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

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

Programming Languages

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

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

User-defined Functions. Conditional Expressions in Scheme

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Recap: Functions as first-class values

Introduction to OCaml

Inductive Data Types

Lecture 3: Recursion; Structural Induction

Standard ML. Data types. ML Datatypes.1

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

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

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

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

SCHEME AND CALCULATOR 5b

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

An introduction introduction to functional functional programming programming using usin Haskell

Lecture #23: Conversion and Type Inference

CSci 4223 Lecture 12 March 4, 2013 Topics: Lexical scope, dynamic scope, closures

Introduction to SML Getting Started

Transcription:

CSE 130 : Fall 2011 Recap from last time Programming Languages Lecture 3: Data Types Ranjit Jhala UC San Diego 1 2 A shorthand for function binding Put it together: a filter function # let neg = fun f -> fun x -> not (f x); # let neg f x = not (f x); val neg : ( a -> bool) -> a -> bool = fn # let is5gte = neg is5lt; val is5gte : int -> bool = fn; # is5gte 10; val it : bool = false; # is5gte 2; val it : bool = true; If arg matches this pattern then use this Body Expr - let rec filter f l = match l with [] -> [] (h::t)-> if f h then h::(filter f t) else (filter f t);; val filter : ( a->bool)-> a list-> a list) = 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] 3 4

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; 5 6 Put it together: a quicksort function Begin at the beginning 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 Expressions (Syntax) Compile-time Static Exec-time Dynamic Types Values (Semantics) 1. Programmer enters expression 2. ML checks if expression is well-typed Using a precise set of rules, ML tries to find a unique type for the expression meaningful type for the expr 3. ML evaluates expression to compute value Of the same type found in step 2 7 8

Today: Different Types of Data In Class Exercise #1 We ve seen some base types and values: Integers, Floats, Bool, String etc. Some ways to build up types: Products (tuples), records, lists Functions 9 10 9 10 What About More Complex Data? How To Build Datatypes We ve seen some base types and values: Integers, Floats, Bool, String etc. Some ways to build up types: Products (tuples), records, lists Functions Design Principle: Orthogonality Don t clutter core language with stuff Few, powerful orthogonal building techniques Put derived types, values, functions in libraries Three key ways to build complex types/values 1. Each-of types Value of T contains value of T1 and a value of T2 2. One-of types Value of T contains value of T1 or a value of T2 3. Recursive Value of T contains (sub)-value of same type T 11 12

Suppose I wanted Constructing Datatypes a program that processed lists of attributes Name (string) Age (integer) type t = C1 of t1 C2 of t2 Cn of tn DOB (int-int-int) Address (string) Height (float) Alive (boolean) t is a new datatype. A value of type t is either: a value of type t1 placed in a box labeled C1 Phone (int-int) email (string) Many kinds of attributes: too many to put in a record Or Or Or a value of type t2 placed in a box labeled C2 a value of type tn placed in a box labeled Cn can have multiple names, addresses, phones, emails etc. 13 14 Suppose I wanted Creating Values 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;; How to create values of type attrib? # let a1 = Name Ranjit ;; val x : attrib = Name Ranjit # let a2 = Height 5.83;; val a2 : attrib = Height 5.83 # let year = 1977 ;; val year : int = 1977 # let a3 = DOB (9,8,year) ;; val a3 : attrib = DOB (9,8,1977) # let a_l = [a1;a2;a3];; val a3 : attrib list = 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;; 15 16

One-of types We ve defined a one-of type named attrib Elements are one of: string, int, int*int*int, float, bool Can create uniform attrib lists Suppose I want a function to print attribs datatype attrib = Name of string Age of int DOB of int*int*int Address of string Height of real Alive of bool Phone of int*int Email of string; How to tell whats in the 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 Email of string;; match e with Name s -> e1 Age i -> e2 DOB (m,d,y) -> e3 Address addr -> e4 Height h -> e5 Alive b -> e6 Phone (a,n) -> e7 Email e -> e8 Pattern-match expression: check if e is of the form On match: value in box bound to pattern variable matching result expression is evaluated Simultaneously test and extract contents of box 17 18 match-with is an Expression match-with is an Expression match e with Name s -> e1 Age i -> e2 DOB (m,d,y) -> e3 Address addr -> e4 Height h -> e5 Alive b -> e6 Phone (a,n) -> e7 Email e -> e8 match e with C1 x1 -> e1 C2 x2 -> e2 Cn xn -> en Type rules? e1, e2,,en must have same type Which is type of whole expression 19 20

Benefits of match-with What about Recursive types? match e with C1 x1 -> e1 C2 x2 -> e2 Cn xn -> en type t = C1 of t1 C2 of t2 Cn of tn type int_list = Nil Cons of int * int_list 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 Think about this! What are values of int_list? Cons(1,Cons(2,Cons(3,Nil))) Cons(2,Cons(3,Nil)) Cons(3,Nil) Nil Cons Cons Cons 1, 2, 3, Nil 21 22 Lists aren t built-in! Some functions on Lists: Length type int_list = Nil Cons of int * int_list Base pattern Ind pattern let rec len l = match l with Nil -> 0 Cons(h,t) -> 1 + (len t) Base Expression Inductive Expression Lists are derived using an elegant core! 1. Each-of 2. One-of 3. Recursive :: is just a pretty way to say Cons [] is just a pretty way to say Nil let rec len l = match l with Nil -> 0 Cons(_,t) -> 1 + (len t) Matches everything, no binding let rec len l = match l with Cons(_,t) -> 1 + (len t) _ -> 0 Pattern-matching in order - Must match with Nil 23 24

Some functions on Lists : Append null, hd, tl are all functions let rec append (l1,l2) = Base pattern Base Expression Bad ML style: More than aesthetics! Ind pattern Find the right induction strategy Base case: pattern + expression Induction case: pattern + expression Inductive Expression Well designed datatype gives strategy Pattern-matching better than test-extract: ML checks all cases covered ML checks no redundant cases at compile-time: fewer errors (crashes) during execution get the bugs out ASAP! 25 26 Another Example: Calculator Another Example: Calculator We want an arithmetic calculator to evaluate expressions like: 4.0 + 2.9 = 6.9 3.78 5.92 = -2.14 (4.0 + 2.9) * (3.78-5.92) = -14.766 We want an arithmetic calculator to evaluate expressions like: 4.0 + 2.9 = 6.9 3.78 5.92 = -2.14 (4.0 + 2.9) * (3.78-5.92) = -14.766 Q: Whats a ML datatype for such expressions? Whats a ML function for evaluating such expressions? 27 28

Random Art from Expressions PA #2 Build more funky expressions, evaluate them, to produce: 29