Download Fourth Look at ML - Modern Programming Languages - Lecture Slides and more Slides Advanced Computer Programming in PDF only on Docsity!
A Fourth Look At ML
Chapter Eleven Modern Programming Languages, 2nd ed. (^) Docsity.com 1
Type Definitions
Predefined, but not primitive in ML:
Type constructor for lists:
Defined for ML in ML
Chapter Eleven Modern Programming Languages, 2nd ed. 2
datatype bool = true | false;
datatype 'element list = nil |
:: of 'element * 'element list
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
Chapter Eleven Modern Programming Languages, 2nd ed. (^) Docsity.com 4
Example
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…
Chapter Eleven Modern Programming Languages, 2nd ed. 5
- 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
Strict Typing
ML is strict about these new types, just as
you would expect
Unlike C enum , no implementation details
are exposed to the programmer
Chapter Eleven Modern Programming Languages, 2nd ed. 7
- 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
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
Chapter Eleven Modern Programming Languages, 2nd ed. 8
fun isWeekDay Sat = false
| isWeekDay Sun = false
| isWeekDay _ = true;
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
Chapter Eleven Modern Programming Languages, 2nd ed. 10
PlusInf
Value
MinusInf Value
Value
Some things
of type 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
Chapter Eleven Modern Programming Languages, 2nd ed. 11
- 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
- Value 3; val it = Value 3 : exint
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
Note that this example only works because
x actually is a Value here
Chapter Eleven Modern Programming Languages, 2nd ed. 13
val y = 5 : int
An Exhaustive Pattern
An exint can be a PlusInf , a
MinusInf , or a Value
Unlike the previous example, this one says
what to do for all possible values of x
Chapter Eleven Modern Programming Languages, 2nd ed. 14
= PlusInf => "infinity" |
= MinusInf => "-infinity" |
= Value y => Int.toString y;
val s = "5" : string
Exception Handling (A Peek)
Patterns are also used in ML for exception
handling, as in this example
We’ll see it in Java, but skip it in ML
Chapter Eleven Modern Programming Languages, 2nd ed. 16
- fun square PlusInf = PlusInf
= | square MinusInf = PlusInf
= | square (Value x) = Value (x*x)
= handle Overflow => PlusInf;
val square = fn : exint -> exint
val it = Value 100000000 : exint
val it = PlusInf : exint
Outline
Enumerations
Data constructors with parameters
Type constructors with parameters
Recursively defined type constructors
Farewell to ML
Chapter Eleven Modern Programming Languages, 2nd ed. (^) Docsity.com 17
Parameter Before Name
Type constuctor 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
Chapter Eleven Modern Programming Languages, 2nd ed. 19
- 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
Uses For option
Predefined type constructor in ML
Used by predefined functions (or your own)
when the result is not always defined
Chapter Eleven Modern Programming Languages, 2nd ed. 20
= if b = 0 then NONE else SOME (a div b);
val optdiv = fn : int -> int -> int option
val it = SOME 3 : int option
val it = NONE : int option