SML Tutorial PDF
SML Tutorial PDF
let val x = 3
fun f y = (y, x*y)
in f(4+x)
end
ML Tutorial 3
Function expressions
is equivalent to
fun inc x = x + 1
ML Tutorial 4
Compound values
constructor patterns:
val list = [1,2,3]
val fst::rest = list
⇒ fst = 1, rest = [2,3]
val [x,_,y] = list
⇒ x = 1, y = 3
ML Tutorial 7
Pattern matching
fun length [] = 0
| length [a] = 1
| length (_ :: r) = 1 + length r
type tycon = ty
examples:
type point = real * real
type line = point * point
type car = {make: string, year: int}
examples:
type ‘a pair = ‘a * ‘a
type point = real pair
ML Tutorial 11
Datatypes
type id = string
val prog =
SEQ(ASSIGN(”a”,BINOP(PLUS,CONST 5,CONST 3)),
PRINT[VAR “a”])
ML Tutorial 14
Computing properties of programs: size
and sizeEL [] = 0
| sizeEL (e::es) = sizeE e + sizeEL es
sizeS prog 8
Types Review
Primitive types
unit, int, real, char, string, ..., instream, outstream, ...
Composite types
unit, tuples, records
function types
Datatypes
types and n-ary type operators, tagged unions, recursive
nominal type equality
bool, list
user defined: trees, expressions, etc.
Type Abbreviations
types and n-ary type operators
structural type equality
type ‘a pair = ‘a * ‘a
Type Inference
When defining values (including functions), types do not need to be
declared -- they will be inferred by the compiler.
- fun f x = x + 1;
val f = fn : int -> int
- fun ident x = x;
val ident = fn : ‘a -> ‘a
- fst foo;
val it = 4.0: real
Polymorphic Types
The most general type is inferred, which may be polymorphic
- fun ident x = x;
val ident = fn : ‘a -> ‘a
type variable
- fun pair x = (x, x);
val pair = fn : ‘a -> ‘a * ‘a
val x = lookup(”foo”,dict);
val x = 2 : int
val y = lookup(”moo”,dict);
uncaught exception NotFound
local val x = 1
in fun new1 () = let val x = x + 1 in x end
end (* new1 always returns 2 *)
structure Year =
struct A structure is an
type year = int encapsulated, named,
val first = 1900 collection of declarations
val second = 2000
fun newYear(y: year) = y+1
fun show(y: year) = Int.toString y
end
structure MutableCar =
struct
structure C = Ford
structure Y = Year
end
Module Interfaces -- Signature
signature MANUFACTURER =
sig
type car
val first : car
val built : car -> int
val mutate : car -> int -> car
val show : car -> string
end
signature YEAR =
sig
eqtype year A signature is a
val first : year
val second : year
collection of specifications
val newYear : year -> year for module components --
val show : year -> string types, values, structures
end
signature MCSIG =
sig
structure C : MANUFACTURER
structure Y : YEAR
end
Signature Matching
structure Year1 : YEAR = Structure S matches SIG if S
struct
type year = int if every spec in SIG is
type decade = string matched by a component of S.
val first = 1900
val second = 2000 S can have more components
fun newYear(y: year) = y+1
fun leap(y: year) = y mod 4 = 0 than are specified in SIG.
fun show(y: year) = Int.toString y
end
val x = Year1.leap(Year1.first)
signature UID =
sig
type uid
val same : (uid * uid) -> bool
val compare : (uid * uid) -> order
val gensym : unit -> uid
end
Modules --- type abstraction
end
Readers