SLIDE 1
Reflection
Suppose you were going to program a system to play card games in which suits (CLUBS, DIAMONDS, HEARTS, and SPADES) matter, such as poker, solitaire, bridge, or gin rummy. Briefly describe how you would represent the suits in C and in
Scheme.
Now consider ML’s datatype mechanism that we used to define the itree type. Briefly describe how you could use ML’s datatype mechanism to represent suits.
SLIDE 2 Pain points from Scheme
Unsolved:
- Clunky records (no dot notation)
Solved:
- Land of Infinite Parentheses
- Algebraic laws are just comments
- car or cdr of empty list
- car or cdr of non-list
- Too many ways to use cons
- Return value of wrong type
- Wrong types/number of arguments
SLIDE 3 Pain points from Scheme
Unsolved:
- Clunky records (no dot notation)
Solved:
- Land of Infinite Parentheses
- Algebraic laws are just comments
(Clausal definition, case expression)
- car or cdr of empty list (pattern match)
- car or cdr of non-list (typecheck)
- Too many ways to use cons
(Define as many forms as you like: datatype)
- Return value of wrong type (typecheck)
- Wrong types/number of arguments (typecheck)
SLIDE 4 Programming with Types
Three tasks:
- Define a type
- Create value (“introduction”)
- Observe a value (“elimination”)
For functions, All you can do with a function is apply it For constructed data, “How were you made & from what parts?”
SLIDE 5 Check your understanding: Basic Datatypes
Consider the following partially written isRed function that is supposed to determine if a given suit is one of the two red suits (ie, is either a heart
fun isRed HEART = BLANK1 | BLANK2 BLANK3 = true | isRed _ = false [Answer: BLANK1 = true BLANK2 = isRed
SLIDE 6
BLANK3 = DIAMOND ]{.answer}
SLIDE 7
Reflection
Consider the int tree datatype and the code we’ve written to manipulate it: datatype int_tree = LEAF | NODE of int * int_tree * int_tree fun inOrder LEAF = [] | inOrder (NODE (v, left, right)) = inOrder left @ [v] @ inOrder right fun preOrder LEAF = [] | preOrder (NODE (v, left, right)) = v :: preOrder left @ preOrder right
SLIDE 8
Discuss the extent to which this code requires the value stored at each node to be an integer. Type errors aside, would the code still work if we stored a different kind of value in the tree?
SLIDE 9
Check your understanding: Defining Datatypes
We can define a subset of the S-expressions in Scheme using the following specification: An SX1 is either an ATOM or a list of SX1s, where ATOM is represented by the ML type atom. Complete the encoding of this definition by filling in the blanks in the following ML datatype: datatype sx1 = ATOM of BLANK1 | LIST of BLANK2 list [Answer:
SLIDE 10
BLANK1 = ‘atom‘ BLANK2 = ‘sx1‘ ]{.answer} Another way of defining a subset of the S-expressions in Scheme uses a different specification: An SX2 is either an ATOM or the result of consing together two values v1 and v2, where both v1 and v2 are themselves members of the set SX2. Complete the encoding of this definition by filling in the blanks in the following ML datatype: datatype sx2 = ATOM of BLANK1
SLIDE 11
| PAIR of BLANK2 BLANK3 BLANK4 [Answer: BLANK1 = ‘atom‘ BLANK2 = ‘sx2‘ BLANK3 = ‘*‘ BLANK4 = ‘sx2‘ ]{.answer}
SLIDE 12 Practice: Defining a datatype
Designing a datatype is a three step process:
- 1. For each form, choose a value constructor
- 2. Identify the “parts” type that each constructor is
- f
- 3. Write the datatype definition
Another definition of a Scheme S-expression is that it is one of:
- A symbol (string)
- A number (int)
- A Boolean (bool)
- A list of S-expressions
Define an ML datatype sx that encodes this
SLIDE 13 version of an S-expression. [Answer: datatype sx = SYMBOL of string | NUMBER of int | BOOL
| SXLIST of sx list ]{.answer}
SLIDE 14
Bonus content
The rest of this slide deck is “bonus content”
SLIDE 15 New vocabulary for ML
Data:
- Constructed data
- Value constructor
Code:
- Pattern
- Pattern matching
- Clausal definition
- Clause
Types:
SLIDE 16 Structure of algebraic types
An algebraic data type is a collection of alternatives
- Each alternative must have a name
The thing named is the value constructor (Also called “datatype constructor”)
SLIDE 17 ”Eliminate” values of algebraic types
New language construct case (an expression) fun length xs = case xs
=> 0 | (x::xs) => 1 + length xs Clausal definition is preferred (sugar for val rec, fn, case)
SLIDE 18 case works for any datatype
fun toStr t = case t
| HEAP (v, left, right) => "nonempty heap" But often a clausal definition is better style: fun toStr’ EHEAP = "empty heap" | toStr’ (HEAP (v,left,right)) = "nonempty heap"
SLIDE 19
Exception handling in action
loop (evaldef (reader (), rho, echo)) handle EOF => finish () | Div => continue "Division by zero" | Overflow => continue "Arith overflow" | RuntimeError msg => continue ("error: " ˆ msg) | IO.Io {name, ...} => continue ("I/O error: " ˆ name) | SyntaxError msg => continue ("error: " ˆ msg) | NotFound n => continue (n ˆ "not found")
SLIDE 20
Datatype definitions
datatype suit = HEARTS | DIAMONDS | CLUBS | SPADES datatype ’a list = nil (* copy me NOT! *) | op :: of ’a * ’a list datatype ’a heap = EHEAP | HEAP of ’a * ’a heap * ’a heap type suit val HEARTS : suit, ... type ’a list val nil : forall ’a . ’a list val op :: : forall ’a . ’a * ’a list -> ’a list type ’a heap val EHEAP: forall ’a. ’a heap val HEAP : forall ’a.’a * ’a heap * ’a heap -> ’a heap
SLIDE 21
ML Traps and pitfalls
SLIDE 22
Order of clauses matters
fun take n (x::xs) = x :: take (n-1) xs | take 0 xs = [] | take n [] = [] (* what goes wrong? *)
SLIDE 23 Gotcha — overloading
> val plus = fn : int -> int -> int
- fun plus x y = x + y : real;
> val plus = fn : real -> real -> real
SLIDE 24 Gotcha — equality types
> val it = fn :
8 ’’a . ’’a * ’’a -> bool
Tyvar ’’a is “equality type variable”:
- values must “admit equality”
- (functions don’t admit equality)
SLIDE 25
Gotcha — parentheses
Put parentheses around anything with | case, handle, fn Function application has higher precedence than any infix operator
SLIDE 26 Syntactic sugar for lists
- 1 :: 2 :: 3 :: 4 :: nil; (* :: associates to the right *)
> val it = [1, 2, 3, 4] : int list
- "the" :: "ML" :: "follies" :: [];
> val it = ["the", "ML", "follies"] : string list > concat it; val it = "theMLfollies" : string
SLIDE 27
ML from 10,000 feet
SLIDE 28
The value environment
Names bound to immutable values Immutable ref and array values point to mutable locations ML has no binding-changing assignment Definitions add new bindings (hide old ones): val pattern = exp val rec pattern = exp fun ident patterns = exp datatype . . . = . . .
SLIDE 29
Nesting environments
At top level, definitions Definitions contain expressions: def ::= val pattern = exp Expressions contain definitions: exp ::= let defs in exp end Sequence of defs has let-star semantics
SLIDE 30 What is a pattern?
pattern ::= variable | wildcard | value-constructor [pattern] | tuple-pattern | record-pattern | integer-literal | list-pattern Design bug: no lexical distinction between
- VALUE CONSTRUCTORS
- variables
Workaround: programming convention
SLIDE 31
Function pecularities: 1 argument
Each function takes 1 argument, returns 1 result For “multiple arguments,” use tuples!
fun factorial n = let fun f (i, prod) = if i > n then prod else f (i+1, i*prod) in f (1, 1) end fun factorial n = (* you can also Curry *) let fun f i prod = if i > n then prod else f (i+1) (i*prod) in f 1 1 end
SLIDE 32
Mutual recursion
Let-star semantics will not do. Use and (different from andalso)! fun a x =
: : : b (x-1) : : :
and b y =
: : : a (y-1) : : :
SLIDE 33 Syntax of ML types
Abstract syntax for types: ty
) TYVAR of string
type variable
j TYCON of string * ty list
apply type constructor Each tycon takes fixed number of arguments. nullary int, bool, string, . . . unary list, option, . . . binary
n-ary tuples (infix *)
SLIDE 34
Syntax of ML types
Concrete syntax is baroque:
ty
) tyvar
type variable
j tycon
(nullary) type constructor
j ty tycon
(unary) type constructor
j (ty, : : :, ty) tycon
(n-ary) type constructor
j ty * : : : * ty
tuple type
j ty -> ty
arrow (function) type
j (ty)
tyvar
) ’identifier
’a, ’b, ’c,
: : :
tycon
) identifier
list, int, bool,
: : :
SLIDE 35 Polymorphic types
Abstract syntax of type scheme
:
- ) FORALL of tyvar list * ty
Bad decision:
8 left out of concrete syntax
(fn (f,g) => fn x => f (g x)) :
8 ’a, ’b, ’c .
(’a -> ’b) * (’c -> ’a) -> (’c -> ’b)
Key idea: subtitute for quantified type variables
SLIDE 36 Old and new friends
:
8 ’a, ’b, ’c .
(’a -> ’b) * (’c -> ’a) -> ’c -> ’b length :
8 ’a . ’a list -> int
map :
8 ’a, ’b .
(’a -> ’b) -> (’a list -> ’b list) curry :
8 ’a, ’b, ’c .
(’a * ’b -> ’c) -> ’a -> ’b -> ’c id :
8 ’a . ’a -> ’a