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

Similar documents
Programming Languages. Datatypes

Programming Languages

Recap: ML s Holy Trinity

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

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

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

Programming Languages

Programming Languages

CSE 130 [Winter 2014] Programming Languages

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

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.

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

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

Begin at the beginning

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

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

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

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

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

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

Programming Languages

Inductive Data Types

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

Programming Languages

Homework 3 COSE212, Fall 2018

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

Typed Racket: Racket with Static Types

Programming Languages

CMSC 330: Organization of Programming Languages

Informatics 1 Functional Programming Lecture 9. Algebraic Data Types. Don Sannella University of Edinburgh

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

Type Checking and Type Inference

Ornaments in ML. Thomas Williams, Didier Rémy. April 18, Inria - Gallium

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

Logic - CM0845 Introduction to Haskell

Introduction to Functional Programming in Haskell 1 / 56

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

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

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

GADTs. Wouter Swierstra. Advanced functional programming - Lecture 7. Faculty of Science Information and Computing Sciences

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

Administrative Stuff

The Typed Racket Guide

GADTs. Alejandro Serrano. AFP Summer School. [Faculty of Science Information and Computing Sciences]

CMSC 330: Organization of Programming Languages. OCaml Data Types

Functions & First Class Function Values

Intro. Scheme Basics. scm> 5 5. scm>

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

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

Which of the following expressions has the type int list?

Lists. Michael P. Fourman. February 2, 2010

CSE 307: Principles of Programming Languages

Random Testing in 321

CSCE 314 Programming Languages

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

Standard ML. Data types. ML Datatypes.1

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

Programming Languages Lecture 14: Sum, Product, Recursive Types

CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion. Zach Tatlock Winter 2018

Handout 2 August 25, 2008

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

CSE 341: Programming Languages

CS109A ML Notes for the Week of 1/16/96. Using ML. ML can be used as an interactive language. We. shall use a version running under UNIX, called

So what does studying PL buy me?

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

CS 11 Haskell track: lecture 1

Fall 2018 Lecture N

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

Programming with Math and Logic

Computer Science CSC324 Wednesday February 13, Homework Assignment #3 Due: Thursday February 28, 2013, by 10 p.m.

Thinking Induc,vely. COS 326 David Walker Princeton University

Exercises on ML. Programming Languages. Chanseok Oh

Functional Programming

Bindings & Substitution

CSE 307: Principles of Programming Languages

Random Testing in 321

Lab 10: OCaml sequences, comparators, stable sorting 12:00 PM, Nov 12, 2017

GADTs gone mild. Code at Gabriel RADANNE 23 Mars 2017

CSCI-GA Scripting Languages

Lecture 2: Big-Step Semantics

Introduction to OCaml

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

CS558 Programming Languages

COMP520 - GoLite Type Checking Specification

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

CS558 Programming Languages

Generic polymorphism on steroids

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

CS 360: Programming Languages Lecture 10: Introduction to Haskell

Datatype declarations

An introduction to functional programming. July 23, 2010

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Transcription:

Review so far Programming Languages Expressions Values Datatypes Types Many kinds of expressions: 1. Simple 2. Variables 3. Functions Review so far 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 Next: Building datatypes 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 Next: Building datatypes Three key ways to build complex types/values 1. Each-of types (T1 * T2) Value of T contains value of T1 and a value of T2 Suppose I wanted a program that processed lists of attributes Name (string) Age (integer) 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 1

Suppose 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 (too many to put in a record) can have multiple names, addresses, phones, emails etc. Want to store them in a list. Can I? 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 t Label=C1 Label=C2 Label=Cn Value:t1 OR Value:t2 OR Value:tn All have the type t Suppose 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;; How to PUT values into box? How to PUT values into box? How to create values of type attrib? # let a1 = Name Bob ;; val x : attrib = Name Bob # 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;; 2

Constructing Datatypes One-of types 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 Bob OR Age 34 OR DOB (9,8,77) Name Bob Age 34 DOB (9,8,77) All have type attrib 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 Say 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 TEST & TAKE whats in box? Is it a... string? or an int? or an int*int*int? or... How to TEST & TAKE whats in box? Look at TAG! How to tell whats in the box? match e with Name s -> printf "%s" 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 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 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 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: int *) Alive b ->...(*b: bool*) Phone(a,r)->...(*a: int, r: int*) 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 3

How to tell whats in the box How to TEST & TAKE whats in box? # match (Name Bob ) with Name s -> printf "Hello %s\n" s Age i -> printf "%d years old" i ;; Hello Bob - : unit = () None of the cases matched the tag (Name) Causes nasty Run-Time Error BEWARE!! Be sure to handle all TAGS! Beware! Handle All TAGS! Compiler to the Rescue! # match (Name Bob ) with Age i -> Printf.printf "%d" I Email s -> Printf.printf "%s" s ;; Exception: Match Failure!! # match (Name Bob ) with Age i -> Printf.printf "%d" I Email s -> Printf.printf "%s" s ;; Exception: Match Failure!! None of the cases matched the tag (Name) Causes nasty Run-Time Error None of the cases matched the tag (Name) Causes nasty 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 Address addr -> Printf.printf "%s" addr Height h -> Printf.printf "%f" h Alive b -> Printf.printf "%b" b Email e -> Printf.printf "%s" e ;; Warning P: this pattern-matching is not exhaustive.here is an example of a value that is not matched:phone (_, _) Compile-time checks for: missed cases: ML warns if you miss a case! 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. Compile-time checks for: redundant cases: ML warns if a case never matches 4

Another Few Examples match-with is an Expression # 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. match e with C1 x1 -> e1 C2 x2 -> e2 Cn xn -> en See code text file Type Rule e1, e2,,en must have same type T Type of whole expression is T match-with is an Expression { T 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 Type Rule e1, e2,,en must have same type T Type of whole expression is T Benefits of match-with match e with C1 x1 -> e1 C2 x2 -> e2 Cn xn -> en type t = C1 of t1 C2 of t2 Cn of tn 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 Next: Building datatypes Three key ways to build complex types/values 1. Each-of types t1 * t2 Value of T contains value of T1 and a value of T2 Zero of nat 2. One-of types type t = C1 of t1 C2 of t2 Value of T contains value of T1 or a value of T2 3. Recursive type Value of T contains (sub)-value of same type T 5

Zero of nat Zero of nat Wait a minute! Zero of what?! Wait a minute! Zero of what?! Relax. Means empty box with label Zero Zero of nat Zero of nat What are values of nat? What are values of nat? Zero Zero of nat Zero of nat What are values of nat? One nat contains another! What are values of nat? One nat contains another! Zero Zero 6

Zero of nat Zero of nat What are values of nat? One nat contains another! What are values of nat? One nat contains another! nat = recursive type Zero Zero Next: Building datatypes Three key ways to build complex types/values 1. Each-of types t1 * t2 Value of T contains value of T1 and a value of T2 2. One-of types type t = C1 of t1 C2 of t2 Value of T contains value of T1 or a value of T2 Next: Lets get cosy with Recursion Recursive Code Mirrors Recursive Data 3. Recursive type type t =... C of (...*t) Value of T contains (sub)-value of same type T to_int : nat -> int Next: Lets get cosy with Recursion Code Structure = Type Structure!!! Zero of nat let rec to_int n = 7

to_int : nat -> int to_int : nat -> int let rec to_int n = let rec to_int n = match n with -> 0 Base Expression Inductive pattern m -> 1 + to_int m Inductive Expression of_int : int -> nat Zero of nat of_int : int -> nat let rec of_int n = let rec of_int n = of_int : int -> nat of_int : int -> nat Base pattern Inductive pattern let rec of_int n = if n <= 0 then else let rec of_int n = Base pattern if n <= 0 then Zero Base Expression Inductive pattern else (of_int (n-1)) Inductive Expression 8

plus : nat*nat -> nat Zero of nat plus : nat*nat -> nat let rec plus n m = let rec plus n m = plus : nat*nat -> nat plus : nat*nat -> nat let rec plus n m = match m with -> Inductive pattern m -> let rec plus n m = match m with -> n Base Expression Inductive pattern m -> (plus n m ) Inductive Expression times: nat*nat -> nat Zero of nat times: nat*nat -> nat let rec times n m = let rec times n m = 9

times: nat*nat -> nat times: nat*nat -> nat let rec times n m = match m with -> Inductive pattern m -> let rec times n m = match m with -> Zero Base Expression Inductive pattern m -> plus n (times n m ) Inductive Expression Lists are recursive types! Next: Lets get cosy with Recursion Recursive Code Mirrors Recursive Data type int_list = Nil Cons of int * int_list 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 1, Cons 2, Cons 3, Nil Lists aren t built-in! Lists are a derived type: built using elegant core! 1. Each-of 2. One-of 3. Recursive datatype int_list = Nil Cons of int * int_list :: is just a pretty way to say Cons [] is just a pretty way to say Nil Some functions on Lists : Length Base pattern let rec len l = match l with Nil -> 0 Base Expression Inductive pattern Cons(h,t) -> 1 + (len t) let rec len l = match l with Nil -> 0 Cons(_,t) -> 1 + (len t) No binding for head Inductive Expression let rec len l = match l with Cons(_,t) -> 1 + (len t) _ -> 0 Pattern-matching in order 10

Some functions on Lists : Append let rec append (l1,l2) = Some functions on Lists : Max let rec max xs = Find the right induction strategy Base case: pattern + expression Induction case: pattern + expression Find the right induction strategy Base case: pattern + expression Induction case: pattern + expression Well designed datatype gives strategy Well designed datatype gives strategy null, hd, tl are all functions Bad ML style: More than aesthetics! 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! Next: Lets get cosy with Recursion Recursive Code Mirrors Recursive Data Representing Trees Representing Trees 1 2 3 1 1 2 3 2 Node(Node( 1, 2), 3) Node(Node( 1, 2), 3) 11

Representing Trees Representing Trees Node 1 2 3 1 2 1 2 3 3 Node(Node( 1, 2), 3) Node(Node( 1, 2), 3) Representing Trees Node 1 2 3 1 Node 2 3 Next: Lets get cosy with Recursion Recursive Code Mirrors Recursive Data Node(Node( 1, 2), 3) sum_leaf: tree -> int sum_leaf: tree -> int Sum up the leaf values. E.g. # let t0 = Node(Node( 1, 2), 3);; - : int = 6 let rec sum_leaf t = 12

sum_leaf: tree -> int sum_leaf: tree -> int Base pattern Inductive pattern Base pattern Inductive pattern let rec sum_leaf t = let rec sum_leaf t = match t with Base pattern n -> Inductive pattern Node(t1,t2)-> sum_leaf: tree -> int Base pattern Inductive pattern let rec sum_leaf t = match t with Base pattern n -> n Base Expression Inductive pattern Node(t1,t2)-> sum_leaf t1 + sum_leaf t2 Inductive Expression Recursive Code Mirrors Recursive Data Code almost writes itself! Another Example: Calculator Want an arithmetic calculator to evaluate expressions like: 4.0 + 2.9 3.78 5.92 (4.0 + 2.9) * (3.78-5.92) Another Example: Calculator 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 Whats a ML TYPE for REPRESENTING expressions? 13

Another Example: Calculator 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 Whats a ML TYPE for REPRESENTING expressions? Another Example: Calculator 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 Whats a ML FUNCTION for EVALUATING expressions? type expr = Num of float Add of expr*expr Sub of expr*expr Mul of expr*expr type expr = Num of float Add of expr*expr Sub of expr*expr Mul of expr*expr Another Example: Calculator 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 Whats a ML FUNCTION for EVALUATING expressions? Another Example: Calculator 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 Whats a ML FUNCTION for EVALUATING expressions? type expr = Num of float Add of expr*expr Sub of expr*expr Mul of expr*expr let rec eval e = match e with Num f -> Add(e1,e2)-> Sub(e1,e2)-> Mul(e1,e2)-> type expr = Num of float Add of expr*expr Sub of expr*expr Mul of expr*expr let rec eval e = match e with Num f -> f Add(e1,e2)-> eval e1 +. eval e2 Sub(e1,e2)-> eval e1 -. eval e2 Mul(e1,e2)-> eval e1 *. eval e2 Random Art from Expressions PA #2 Build more funky expressions, evaluate them, to produce: 14