SML/NJ
Lecture slide: click here
Pattern matching, type inference, data types, pattern matching, continuations. Readings: Ullman, ch 1-4, 5 (optional)
Syntax in SML: SML syntax fast loop-up book SML Help: SML tutorial written by TAs at CMU.
Overview
Functional: functions as first-class values
Garbage collected
Strict evaluation (applicative order)
No coercion
Strong and static typing
parametric polymorphism
structural equivalence
all with type inference
Advanced module system
Exceptions
Miscellaneous features
datatypes (merge of enumerated literals and variant records)
pattern matching
reftype constructor (like const pointers)
Functions
Type notation α → β → δ means α → (β → δ)
let local scope
let local scopeRecords
Type declaration
Variable declaration
Field selection: #x v
Pattern matching in a function:
Tuples
Tuples are actually records:
is actually:
Index starting from 1.
Datatypes
For example:
tree is a type constructor.
Leaf and Node are data constructors:
Leaf : int → treeNode : tree * tree → tree
Pattern matching for datatypes
We can define functions by pattern matching:
or
Functions accepting data constructors as arguments must provide an exhaustive definition(cover every data constructor for the datatype).
Parameterized datatypes (with type variables)
Common idiom: option
option is a built-in datatype:
A lookup function using option:
Type of lookup is (α1*α2→bool) → α1 → (α2*β)list → βoption.
Signature and structures
An ML signature specifies an interface for a module.
A structure implementing it would be:
Signature ascription
Opaque ascription (:>) hides the identity of types beyond that which is conveyed in the signature. That is, additional type information provided by the structure will be considered abstract.
Transparent ascription (:) exposes the identity of types beyond that conveyed in the signature. That is, additional type information provided by the structure will augment the signature.
both prohibit the introduction of identifiers not already present in the signature. This is component hiding.
both permit types (in structures) which are broader than the signature.
Examples:
Functor
A functor creates a structure from a structure.
Invoke the functor:
Last updated