1 / 61

A Fourth Look At ML

A Fourth Look At ML. Chapter 11 Type-Safe Data Structures. Type Definitions. Predefined, but not primitive in ML: Type constructor for lists: Defined for ML in ML. datatype bool = true | false;. datatype 'element list = nil | :: of 'element * 'element list. Outline. Enumerations

olaf
Download Presentation

A Fourth Look At ML

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Fourth Look At ML Chapter 11 Type-Safe Data Structures Modern Programming Languages

  2. Type Definitions • Predefined, but not primitive in ML: • Type constructor for lists: • Defined for ML in ML datatype bool = true | false; datatype 'element list = nil | :: of 'element * 'element list Modern Programming Languages

  3. Outline • Enumerations • Data constructors with parameters • Type constructors with parameters • Recursively defined type constructors • Farewell to ML  • We hardly knew ye! Modern Programming Languages

  4. Defining Your Own Types • New types can be defined using the keyword datatype • These declarations define both: • type constructors for making new (possibly polymorphic) types • data constructors for making values of those new types Modern Programming Languages

  5. Example - datatype day = Mon | Tue | Wed | Thu | Fri | Sat | Sun;datatype day = Fri | Mon | Sat | Sun | Thu | Tue | Wed- fun isWeekDay x = not (x = Sat orelse x = Sun);val isWeekDay = fn : day -> bool- isWeekDay Mon;val it = true : bool- isWeekDay Sat;val it = false : bool • day is the new type constructor and Mon, Tue, etc. are the new data constructors • Why “constructors”? In a moment we will see how both can have parameters… Modern Programming Languages

  6. No Parameters - datatype day = Mon | Tue | Wed | Thu | Fri | Sat | Sun;datatype day = Fri | Mon | Sat | Sun | Thu | Tue | Wed • The type constructor day takes no parameters: it is not polymorphic, there is only oneday type • The data constructors Mon, Tue, etc. take no parameters: they are constant values of the day type • Capitalize the names of data constructors Modern Programming Languages

  7. Strict Typing - datatype flip = Heads | Tails;datatype flip = Heads | Tails- fun isHeads x = (x = Heads);val isHeads = fn : flip -> bool- isHeads Tails;val it = false : bool- isHeads Mon;Error: operator and operand don't agree [tycon mismatch] operator domain: flip operand: day • ML is strict about these new types, just as you would expect • Unlike C enum, no implementation details are exposed to the programmer Modern Programming Languages

  8. Data Constructors In Patterns • You can use the data constructors in patterns • In this simple case, they are like constants • But we will see more general cases next… fun isWeekDay Sat = false| isWeekDay Sun = false| isWeekDay _ = true; Modern Programming Languages

  9. The order Datatype • Used with pre-defined compare functions • Int.compare( ) • Real, String, Char, Time, Date, … • datatype order = LESS | GREATER | EQUAL; - Real.compare(1.0,2.0); val it = LESS : order • Char.compare(#"b", #"a"); val it = GREATER : order Modern Programming Languages

  10. Outline • Enumerations • Data constructors with parameters • Type constructors with parameters • Recursively defined type constructors • Farewell to ML Modern Programming Languages

  11. Wrappers • You can add a parameter of any type to a data constructor, using the keyword of:datatype exint = Value of int | PlusInf | MinusInf; • In effect, such a constructor is a wrapper that contains a data item of the given type Value PlusInf Value 38 36 Some things of type exint: MinusInf Value 26 Modern Programming Languages

  12. - datatype exint = Value of int | PlusInf | MinusInf; datatype exint = MinusInf | PlusInf | Value of int - PlusInf;val it = PlusInf : exint- MinusInf;val it = MinusInf : exint- Value;val it = fn : int -> exint (* Function notation! *)- Value 3;val it = Value 3 : exint • Value is a data constructor that takes a parameter: the value of the int to store • It looks like a function that takes an int and returns an exint containing that int Modern Programming Languages

  13. A Value Is Not An int • Value 5 is an exint • It is not an int, though it contains one • How can we get the int out again? • By pattern matching… - val x = Value 5;val x = Value 5 : exint- x+x;Error: overloaded variable not defined at type symbol: + type: exint Modern Programming Languages

  14. Patterns With Data Constructors • To recover a data constructor’s parameters, use pattern matching • So Value is no ordinary function: ordinary functions can't be pattern-matched this way - val (Value y) = x;Warning: binding not exhaustive Value y = ...val y = 5 : int Modern Programming Languages

  15. An Exhaustive Pattern - val s = case x of = PlusInf => "infinity" |= MinusInf => "-infinity" | = Value y => Int.toString y;val s = "5" : string • Like most uses of the match construct, you get a warning if it is not exhaustive • An exint can be a PlusInf, a MinusInf, or a Value • This one says what to do in all cases Modern Programming Languages

  16. Pattern-Matching Function - fun square PlusInf = PlusInf= | square MinusInf = PlusInf= | square (Value x) = Value (x*x);val square = fn : exint -> exint- square MinusInf;val it = PlusInf : exint- square (Value 3);val it = Value 9 : exint • Pattern-matching function definitions are especially important when working with your own datatypes Modern Programming Languages

  17. Exception Handling (A Peek) - fun square PlusInf = PlusInf= | square MinusInf = PlusInf= | square (Value x) = Value (x*x)= handle Overflow => PlusInf;val square = fn : exint -> exint- square (Value 10000);val it = Value 100000000 : exint- square (Value 100000);val it = PlusInf : exint • Patterns are also used in ML for exception handling, as in this example Modern Programming Languages

  18. Outline • Enumerations • Data constructors with parameters • Type constructors with parameters • Recursively defined type constructors • Farewell to ML Modern Programming Languages

  19. Partial Functions • A function that is not defined for all values of its domain type is called a partial function • Examples: • 1/x; functions that expect a non-null pointer • In ML, the option data type indicates undefined valuesdatatype ‘a option = NONE | SOME of ‘a; Modern Programming Languages

  20. Type Constructors With Parameters • Type constructors can also use parameters: datatype 'a option = NONE | SOME of 'a; • The parameters of a type constructor are type variables, which are used in the data constructors • The result: a new polymorphic type SOME SOME 1.5 "Hello" SOME NONE SOME NONE "world" 123.4 Values of typestring option Values of typereal option Modern Programming Languages

  21. Parameter Before Name - SOME 4;val it = SOME 4 : int option- SOME 1.2;val it = SOME 1.2 : real option - SOME "pig";val it = SOME "pig" : string option • Type constructor parameter comes before the type constructor name:datatype 'a option = NONE | SOME of 'a; • We have types 'a option and int option, just like 'a list and int list Modern Programming Languages

  22. Uses For option • Predefined type constructor in ML • Used by predefined functions (or your own) when the result is not always defined - fun optdiv a b == if b = 0 then NONE else SOME (a div b);val optdiv = fn : int -> int -> int option- optdiv 7 2;val it = SOME 3 : int option- optdiv 7 0;val it = NONE : int option Modern Programming Languages

  23. Working with option • Retrieve the value of an option, other than NONE, with valOf • Provide a default value for NONE with getOpt val x = SOME 2 : int option - valOf x; val it = 2 : int - getOpt (x,0); val it = 2 : int - getOpt (NONE, 0); val it = 0 : int • See fact.sml Modern Programming Languages

  24. Longer Example: bunch • An 'x bunch is either a thing of type 'x, or a list of things of type 'x • As usual, ML infers types: datatype 'x bunch = One of 'x | Group of 'x list; - One 1.0;val it = One 1.0 : real bunch- Group [true,false];val it = Group [true,false] : bool bunch Modern Programming Languages

  25. Example: Polymorphism • ML can infer bunch types, but does not always have to resolve them, just as with list types - fun size (One _) = 1= | size (Group x) = length x;val size = fn : 'a bunch -> int- size (One 1.0);val it = 1 : int- size (Group [true,false]);val it = 2 : int Modern Programming Languages

  26. Example: No Polymorphism - fun sum (One x) = x= | sum (Group xlist) = foldr (op +) 0 xlist;val sum = fn : int bunch -> int- sum (One 5);val it = 5 : int- sum (Group [1,2,3]);val it = 6 : int • We applied the + operator (through foldr) to the list elements • So ML knows the parameter type must be int bunch Modern Programming Languages

  27. Outline • Enumerations • Data constructors with parameters • Type constructors with parameters • Recursively defined type constructors • Farewell to ML Modern Programming Languages

  28. Recursively Defined Type Constructors • The type constructor being defined may be used in its own data constructors:datatype intlist = INTNIL | INTCONS of int * intlist; Some values of type intlist: Modern Programming Languages

  29. Constructing Those Values - INTNIL;val it = INTNIL : intlist- INTCONS (1,INTNIL);val it = INTCONS (1,INTNIL) : intlist- INTCONS (1,INTCONS(2,INTNIL));val it = INTCONS (1,INTCONS (2,INTNIL)) : intlist Modern Programming Languages

  30. An intlist Length Function fun intlistLength INTNIL = 0 | intlistLength (INTCONS(_,tail)) = 1 + (intListLength tail);fun listLength nil = 0 | listLength (_::tail) = 1 + (listLength tail); • A length function • Much like you would write for native lists • Except, of course, that native lists are not always lists of integers… Modern Programming Languages

  31. Parametric List Type datatype 'element mylist = NIL | CONS of 'element * 'element mylist; • A parametric list type, almost like the predefined list • ML handles type inference in the usual way: - CONS(1.0, NIL);val it = CONS (1.0,NIL) : real mylist- CONS(1, CONS(2, NIL));val it = CONS (1,CONS (2,NIL)) : int mylist Modern Programming Languages

  32. Some mylist Functions fun myListLength NIL = 0 | myListLength (CONS(_,tail)) = 1 + myListLength(tail); • This now works almost exactly like the predefined list type constructor • Of course, to add up a list you would use foldr… fun addup NIL = 0 | addup (CONS(head,tail)) = head + addup tail; Modern Programming Languages

  33. A foldr For mylist • Definition of a function like foldr that works on 'a mylist • Can now add up an int mylist x with: myfoldr (op +) 0 x • One remaining difference: :: is an operator and CONS is not fun myfoldr f c NIL = c | myfoldr f c (CONS(a,b)) = f(a, myfoldr f c b); Modern Programming Languages

  34. Defining Operators • ML allows new operators to be defined • Like this: - infixr 5 CONS;infixr 5 CONS- 1 CONS 2 CONS NIL;val it = 1 CONS 2 CONS NIL : int mylist Modern Programming Languages

  35. Chinese Boxes • Nested, square boxes • like Russian Dolls • Each box has a size (length of side) and a color datatype color = Red | Green | Blue; datatype 'a cbox = Contents of 'a option | Trio of int * color * 'a cbox; Modern Programming Languages

  36. Chinese Box FunctionsSee cbox.sml • makebox: int * color * 'a cbox -> 'a cbox • boxcount: 'a cbox -> int • tracebox: 'a cbox -> unit • prints the box attributes outside-in • openbox: 'a cbox -> 'a option • returns the inner contents • insert: int * color * 'a cbox -> 'a cbox • makebox2: (int * color) list * 'a option -> 'a cbox Modern Programming Languages

  37. One More Function • difflist = fn : 'a cbox -> int list - difflist b; val it = [1,1,1,1] : int list fun difflist1 (Trio (s, _, Trio(s2,c,box))) = (s-s2)::difflist1 (Trio(s2,c,box)) | difflist1 _ = nil; Modern Programming Languages

  38. Layered Patterns with as fun difflist (Trio (s,_,b as Trio(s2,_,_))) = (s-s2)::difflist b | difflist _ = nil; Modern Programming Languages

  39. Polymorphic Binary Tree datatype 'data tree = Empty | Node of 'data tree * 'data * 'data tree; Some values of type int tree: Modern Programming Languages

  40. Constructing Those Values - val treeEmpty = Empty;val treeEmpty = Empty : 'a tree- val tree2 = Node(Empty,2,Empty);val tree2 = Node (Empty,2,Empty) : int tree- val tree123 = Node(Node(Empty,1,Empty),= 2,= Node(Empty,3,Empty)); Modern Programming Languages

  41. Increment All Elements fun incall Empty = Empty | incall (Node(x,y,z)) = Node(incall x, y+1, incall z); - incall tree123;val it = Node (Node (Empty,2,Empty), 3, Node (Empty,4,Empty)) : int tree Modern Programming Languages

  42. Add Up The Elements fun sumall Empty = 0 | sumall (Node(x,y,z)) = sumall x + y + sumall z; - sumall tree123;val it = 6 : int Modern Programming Languages

  43. Convert To List (Polymorphic) (* In-order traversal *) fun listall Empty = nil | listall (Node(x,y,z)) = listall x @ y :: listall z; - listall tree123;val it = [1,2,3] : int list Modern Programming Languages

  44. Tree Search fun isintree x Empty = false | isintree x (Node(left,y,right)) = x=y orelse isintree x left orelse isintree x right; - isintree 4 tree123;val it = false : bool- isintree 3 tree123;val it = true : bool Modern Programming Languages

  45. Infinite Listsaka “Streams” • It is possible, and often useful, to define infinite lists • The elements are computed on demand • Allows a high degree of separation between software components (functions) Modern Programming Languages

  46. The unit Data Type • Like void in C++ • a “nothing” return type, or • an “empty” parameter list • Denoted by ( ) in ML - print "hello\n"; hello val it = () : unit - fun f () = "goodbye"; val f = fn : unit -> string - f(); val it = "goodbye" : string Modern Programming Languages

  47. Nodes for Infinite Linked Lists • Recall the list type definition: • The first node slot is the data • The second is a “pointer” to the tail • For infinite lists, we will delay the evaluation of the tail: datatype 'element list = nil | :: of 'element * 'element list datatype 'a stream = Nil | Cons of 'a * (unit -> 'a stream); Modern Programming Languages

  48. An Infinite Sequence of Integers - fun intsfrom k = Cons(k,fn()=>intsfrom(k+1)); val intsfrom = fn : int -> int stream - val nums = intsfrom 5; val nums = Cons (5,fn) : int stream - val Cons(head,_) = nums; val head = 5 : int - val Cons(_,f) = nums; val f = fn : unit -> int stream - val rest = f(); val rest = Cons (6,fn) : int stream - val Cons(head,_) = rest; val head = 6 : int Modern Programming Languages

  49. Head and Tail for Streams fun head Nil = raise Empty | head (Cons(h,_)) = h; fun thunk Nil = raise Empty | thunk (Cons(_,t)) = t; fun force f = f (); fun tail s = force (thunk s); (* Like the real list type, just gives the next Cons *) Modern Programming Languages

  50. Traversing the Stream - nums; val it = Cons (5,fn) : int stream - head nums; val it = 5 : int - thunk nums; val it = fn : unit -> int stream - tail nums; val it = Cons (6,fn) : int stream - val rest = tail nums; val rest = Cons (6,fn) : int stream - head rest; val it = 6 : int - tail rest; val it = Cons (7,fn) : int stream Modern Programming Languages

More Related