Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Module System in Standard ML
1. Module System of Standard ML
Jiten, Keheliya, Tobias
Tuesday 21st April, 2015
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 1 / 39
2. Outline
1 Modularity
2 Introduction to Standard ML
3 Modules in Standard ML
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 2 / 39
3. Outline
1 Modularity
2 Introduction to Standard ML
3 Modules in Standard ML
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 3 / 39
4. Why Modularity?
Writing large programs is difficult!
Lots of interdependencies
Changing code breaks other code
Concurrent development difficult
Maintenance is a nightmare.
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 4 / 39
5. Why Modularity?
Writing large programs is difficult!
Lots of interdependencies
Changing code breaks other code
Concurrent development difficult
Maintenance is a nightmare.
The solution: Modularity
Implement program as independent modules
Hide information between modules to obscure implementation
details (abstraction)
Sharing: Interaction of program modules
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 4 / 39
6. How do programmers use modularity in C?
Write .h file (Header) and .c file (Source)
Header specifies variables and functions
Source is the full implementation
Compile library from both files and provide Header file as
interface.
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 5 / 39
7. Outline
1 Modularity
2 Introduction to Standard ML
3 Modules in Standard ML
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 6 / 39
8. History of Standard ML
ML (stands for metalanguage)
Developed in Edinburgh in late ’70s
Designed by Michael J.C. Gordon, Robin Milner, and Christopher
P. Wadsworth
Standard ML (originated from ML)
Has a formal specification, given as typing rules and operational
semantics
The Definition of Standard ML (Revised) in 1997
Other siblings of the family: CAML, Caml Light, OCaml
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 7 / 39
9. Features of Standard ML
Higher-order functions
Interactive
Strongly Typed
Call-by-value
Polymorphism
Algebraic datatypes and pattern matching
Statically scoped
Type-safe exceptions
Modularity
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 8 / 39
10. Language Basics
Primitive Data Types: unit,bool,int,real,string
Function Expressions
val inc = fn x => x + 1
fun inc x = x + 1
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 9 / 39
11. Tuples, Records, Lists
Tuples
> val x = ("Prius", 1.0 / 2.0, false)
: string * real * bool
Records
> val dob = {day=21,month="July",year="1815"}
: {day:int, month:string, year:string}
Lists
> val price = [1.0,2.0,3.0];
> val price = 1.0 :: 2.0 :: 3.0 :: nil
val price = [1.0,2.0,3.0] : real list
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 10 / 39
12. Higher-order Functions
Functions can consume functions as arguments
fun applyTwice f x = f(f(x));
Functions can return functions
val add = fn x => fn y => x + y
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 11 / 39
13. Static Scoping
What is the output of this program?
val x = 2
fun f y = x+y
val x = 3
val z = f 4
Refers to the nearest lexically enclosing declaration!
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 12 / 39
14. Type Inference
Examples of type inference
fun ident (x) = x;
val ident = fn : ’a -> ’a
fun plus (x,y) = x+y;
val plus = fn : int * int -> int
fun new_if(A,B,C) = if A then B else C;
val new_if = fn : bool * ’a * ’a -> ’a
fun first (x,y) = x;
val first = fn : ’a * ’b -> ’a
’a standing for any type at all.
Function body makes no commitment to type of x at all.
Type inference produces the most general type, which may be
polymorphic.
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 13 / 39
15. Pattern Matching
Wildcard Patterns
val (x,y,_) = (4,"May",1987);
val x = 4 : int
val y = "May" : string
Nested Patterns
val (a,b)::c::_ = [(3,true),(5,false),(9,false)];
val a = 3 : int
val b = true : bool
val c = (5,false) : int * bool
Function definition by cases
fun is_nil( nil ) = true
| is_nil(_::_) = false;
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 14 / 39
16. Recursive Types
datatype ’a tree = empty
| leaf of ’a
| node of ’a tree * ’a tree;
datatype ’a list = nil | :: of ’a * ’a list;
fun append(nil,l) = l
| append(hd::tl,l) = hd :: append(tl,l);
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 15 / 39
17. Recursive Typing
To type infinte data-structures (Lists, queues, trees, streams etc.)
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 16 / 39
18. Recursive Typing
To type infinte data-structures (Lists, queues, trees, streams etc.)
Without Recursive typing:-
t ::“ nilT | consT t t | headT t | tailT t
v ::“ nilT | consT v v
T ::“ List T
Γ $ t1 : T1 Γ $ t2 : List T1
Γ $ consT1 t1 t2 : List T1
(T-cons)
Γ $ t1 : List T1
Γ $ headT1 t1 : T1
(T-head)
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 16 / 39
19. Recursive Typing
To type infinte data-structures (Lists, queues, trees, streams etc.)
Without Recursive typing:-
t ::“ nilT | consT t t | headT t | tailT t
v ::“ nilT | consT v v
T ::“ List T
Γ $ t1 : T1 Γ $ t2 : List T1
Γ $ consT1 t1 t2 : List T1
(T-cons)
Γ $ t1 : List T1
Γ $ headT1 t1 : T1
(T-head)
Recursive typing:-
T ::“ X | rec X.T
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 16 / 39
20. Recursive Typing
To type infinte data-structures (Lists, queues, trees, streams etc.)
Without Recursive typing:-
t ::“ nilT | consT t t | headT t | tailT t
v ::“ nilT | consT v v
T ::“ List T
Γ $ t1 : T1 Γ $ t2 : List T1
Γ $ consT1 t1 t2 : List T1
(T-cons)
Γ $ t1 : List T1
Γ $ headT1 t1 : T1
(T-head)
Recursive typing:-
T ::“ X | rec X.T
IntList “ rec X. xnil : Unit, cons : tInt, Xuy
Nat “ rec X. xZero : Unit, succ : Xy
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 16 / 39
21. Recursive Typing...
t ::“ foldT t | unfoldT t
v ::“ foldT v
unfoldS pfoldT vq ÝÑ v(E-unfldfld)
U “ rec X.T1 Γ $ t1 : rU{XsT1
Γ $ foldU t1 : U
(T-fld)
U “ rec X.T1 Γ $ t1 : U
Γ $ unfoldU t1 : rU{XsT1
(T-unfld)
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 17 / 39
22. Recursive Typing...
t ::“ foldT t | unfoldT t
v ::“ foldT v
unfoldS pfoldT vq ÝÑ v(E-unfldfld)
U “ rec X.T1 Γ $ t1 : rU{XsT1
Γ $ foldU t1 : U
(T-fld)
U “ rec X.T1 Γ $ t1 : U
Γ $ unfoldU t1 : rU{XsT1
(T-unfld)
nil “ foldrIntLists xnil “ unity
cons = λn : Int.λl : IntList.foldrIntLists xcons “ tn, luy
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 17 / 39
23. Parametric Polymorphism
t ::“ x | λx : T.t | t t | λX.t | t rTs
v ::“ λx : T.t | λX.t
T ::“ X | T Ñ T | @X.T
Γ ::“ Φ | Γ, x : T | Γ, X
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 18 / 39
24. Parametric Polymorphism
t ::“ x | λx : T.t | t t | λX.t | t rTs
v ::“ λx : T.t | λX.t
T ::“ X | T Ñ T | @X.T
Γ ::“ Φ | Γ, x : T | Γ, X
pλx : T11.t12q v2 rv2{xst12
pλX.t12q rT2s rT2{Xst12
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 18 / 39
25. Parametric Polymorphism
t ::“ x | λx : T.t | t t | λX.t | t rTs
v ::“ λx : T.t | λX.t
T ::“ X | T Ñ T | @X.T
Γ ::“ Φ | Γ, x : T | Γ, X
pλx : T11.t12q v2 rv2{xst12
pλX.t12q rT2s rT2{Xst12
Γ, X $ t2 : T2
Γ $ λX.t2 : @X.T2
(T-Tabs)
Γ $ t1 : @X.T12
Γ $ t1 rT2s : rT2{XsT12
T-Tapp)
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 18 / 39
26. Parametric Polymorphism
t ::“ x | λx : T.t | t t | λX.t | t rTs
v ::“ λx : T.t | λX.t
T ::“ X | T Ñ T | @X.T
Γ ::“ Φ | Γ, x : T | Γ, X
pλx : T11.t12q v2 rv2{xst12
pλX.t12q rT2s rT2{Xst12
Γ, X $ t2 : T2
Γ $ λX.t2 : @X.T2
(T-Tabs)
Γ $ t1 : @X.T12
Γ $ t1 rT2s : rT2{XsT12
T-Tapp)
Example:-
@X. rec Y. xnil : Unit, cons : tX, Yuy
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 18 / 39
27. Parametric Polymorphism
t ::“ x | λx : T.t | t t | λX.t | t rTs
v ::“ λx : T.t | λX.t
T ::“ X | T Ñ T | @X.T
Γ ::“ Φ | Γ, x : T | Γ, X
pλx : T11.t12q v2 rv2{xst12
pλX.t12q rT2s rT2{Xst12
Γ, X $ t2 : T2
Γ $ λX.t2 : @X.T2
(T-Tabs)
Γ $ t1 : @X.T12
Γ $ t1 rT2s : rT2{XsT12
T-Tapp)
Example:-
@X. rec Y. xnil : Unit, cons : tX, Yuy
Polymorphism in ML:-
Self-Application: Type of λx.x x?
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 18 / 39
28. Parametric Polymorphism
t ::“ x | λx : T.t | t t | λX.t | t rTs
v ::“ λx : T.t | λX.t
T ::“ X | T Ñ T | @X.T
Γ ::“ Φ | Γ, x : T | Γ, X
pλx : T11.t12q v2 rv2{xst12
pλX.t12q rT2s rT2{Xst12
Γ, X $ t2 : T2
Γ $ λX.t2 : @X.T2
(T-Tabs)
Γ $ t1 : @X.T12
Γ $ t1 rT2s : rT2{XsT12
T-Tapp)
Example:-
@X. rec Y. xnil : Unit, cons : tX, Yuy
Polymorphism in ML:-
Self-Application: Type of λx.x x?
λx : @X.X Ñ X. x r@X.X Ñ Xs x : p@X.X Ñ Xq Ñ p@X.X Ñ Xq
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 18 / 39
29. Example: Stack
exception EmptyStack;
val empty = [];
fun push x r = x::r;
fun pop r = case r of
x::s => (x,s)
| [] => raise EmptyStack;
Type of push:
’a -> ’a list -> ’a list
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 19 / 39
30. Example: Stack
Type of push:
’a -> ’a list -> ’a list
This exposes the implementation datastructure (list)!
We want the view of the type to be abstract:
’a -> ’a stack -> ’a stack
ñ Need Standard ML’s abstract types in modules.
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 20 / 39
31. Outline
1 Modularity
2 Introduction to Standard ML
3 Modules in Standard ML
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 21 / 39
32. Signatures
Signatures encode structural information
Specify a module’s value and function types
Provided explicitly by the programmer or discovered by type
inference
signature EQ =
sig
type elem
val eq: elem * elem -> bool
end;
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 22 / 39
33. Structures
structure is Standard ML’s Module
structure IntEq : EQ =
struct
type elem = int
val eq = (op =)
end;
Here, the signature is specified explicitly.
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 23 / 39
34. All put together: Signature, Module and Access
signature EQ =
sig
type elem
val eq: elem * elem -> bool
end;
structure IntEq : EQ =
struct
type elem = int
val eq = (op =)
end;
IntEq.eq(3,4);
IntEq.eq(4,4);
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 24 / 39
35. Structures with explicit Signatures (1)
structure IntEq : EQ means that the structure’s inferred
signature is checked against the signature EQ
IntEq must provide at least what’s in EQ.
signature EQ =
sig
type elem
val eq: elem * elem -> bool
end;
structure IntEq : EQ =
struct
type elem = int
end;
> Error: unmatched value specification: eq
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 25 / 39
36. Structures with explicit Signatures (2)
We can make structure IntEq more powerful than EQ. But the
view is still restricted by the explicit signature EQ.
signature EQ =
sig
type elem
val eq: elem * elem -> bool
end;
structure IntEq : EQ =
struct
type elem = int
val eq = (op =)
val internalvalue = 1337
end;
IntEq.internalvalue;
> Error: unbound variable or constructor: internalvalue in
path IntEq.internalvalue
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 26 / 39
37. Opaque vs. Transparent Signatures (1)
Remember we could evaluate IntEq.eq(3,4);?
IntEq has signature EQ, which means IntEq.eq takes elem
variables as parameter – not integers!
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 27 / 39
38. Opaque vs. Transparent Signatures (1)
Remember we could evaluate IntEq.eq(3,4);?
IntEq has signature EQ, which means IntEq.eq takes elem
variables as parameter – not integers!
Well duh, calling with integers works, since IntEq.elem = int.
Signatures are transparent. We can make them opaque to strictly
restrict the view of IntEq to EQ.
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 27 / 39
39. Opaque vs. Transparent Signatures (2)
structure IntEq :> EQ =
struct
type elem = int
val eq = (op =)
end;
IntEq.eq(3,4);
> Error: operator and operand don’t agree [literal]
operator domain: IntEq.elem * IntEq.elem
operand: int * int
in expression:
IntEq.eq (3,4)
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 28 / 39
40. Stack Module
signature STACK =
sig
type ’a stack
val empty: ’a stack
val push: ’a -> ’a stack -> ’a stack
val pop: ’a stack -> ’a * ’a stack
end;
structure stack :> STACK =
struct
exception EmptyStack;
type ’a stack = ’a list;
val empty = [];
fun push x r = x::r;
fun pop r = case r of
x::s => (x,s)
| [] => raise EmptyStack;
end;
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 29 / 39
41. Stack Module
stack.push now has type:
’a -> ’a stack.stack -> ’a stack.stack.
Thanks to opaqueness, stack.push 3 [5]; is illegal.
Implementation protected. Yay!
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 30 / 39
42. Functors
A Functor takes a Structure as an argument and returns a
Structure.
They are defined using functor bindings at the top level (like
function definitions)
Limited to a single argument (Workaround: Several structures can
be packaged into one as substructures and then passed to a
functor)
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 31 / 39
43. Functors: Example
functor F( P: EQ ) : EQ =
struct
type t = P.t * P.t
fun eq((x,y),(u,v)) = P.eq(x,u) andalso P.eq(y,v)
end;
structure IntEq : EQ =
struct
type t = int
val eq = (op =)
end;
structure IntTupleEq = F(IntEq);
IntTupleEq.eq((3,4),(3,4));
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 32 / 39
44. Functors with Submodules: Example
What if we want tuples of two different datatypes?
For instance tuples of Integers and Strings: eq((3,"a"),(3,"c"))
First, we introduce StringEq, similarly to IntEq:
structure StringEq : EQ =
struct
type t = string
val eq = (op =)
end;
Problem: functors only take a single structure as an argument. We
need to pass IntEq and StringEq!
Solution: Create a module containing both.
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 33 / 39
45. Functors with Submodules: Example
signature TupleOfEQ =
sig
structure first : EQ
structure second : EQ
end;
functor F( P: TupleOfEQ ) : EQ =
struct
type t = P.first.t * P.second.t
fun eq((x,y),(u,v)) = P.first.eq(x,u) andalso P.second.eq(y,v)
end;
structure IntStringTuple : TupleOfEQ =
struct
structure first = IntEq
structure second = StringEq
end;
structure IntStringTupleCombined = F(IntStringTuple);
IntStringTupleCombined.eq((3,"a"),(3,"c"));
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 34 / 39
46. Sums and Products :-
Πx : A.B “
ą
t Bpxq | x P A u
Σx : A.B “ t Bpxq | x P A u
Elements of Πx : A.B fpaq “ ra{xsB.
Elements of Σx : A.B xa, by , a P A, b “ ra{xsB.
Strong sums:
sndppq : Bpfstppqq fstppq “ a.
Corresponds to the transparent signatures.
Weak sums:
Corresponds to the opaque ascription.
A form of existential type.
Transclucent sums:-
Optionally contain information about the representation of type.
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 35 / 39
47. Module system Calculus
Syntax:-
K ::“ Ω | K ñ K1
A ::“ α | Πx : A.A1 | D1, .., Dn | λα :: K | AA1 | V.b
D ::“ b Ź α :: K | b Ź α :: K “ A | y Ź x : A
M ::“ x | λx : A.M | MM1 | M : A | B1, .., Bn | M.y
B ::“ b Ź α “ A | y Ź x “ M
V ::“ x | λx : A.M | Bv1, .., Bvn | V.y
Bv ::“ b Ź α “ A | y Ź x “ V
Γ ::“ ˝ | Γ, α :: K | Γ, α :: K “ A | Γ, x : A
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 36 / 39
48. Module system Calculus
Syntax:-
K ::“ Ω | K ñ K1
A ::“ α | Πx : A.A1 | D1, .., Dn | λα :: K | AA1 | V.b
D ::“ b Ź α :: K | b Ź α :: K “ A | y Ź x : A
M ::“ x | λx : A.M | MM1 | M : A | B1, .., Bn | M.y
B ::“ b Ź α “ A | y Ź x “ M
V ::“ x | λx : A.M | Bv1, .., Bvn | V.y
Bv ::“ b Ź α “ A | y Ź x “ V
Γ ::“ ˝ | Γ, α :: K | Γ, α :: K “ A | Γ, x : A
Example:-
t elem Ź elem “ int, eq Ź eq “ λx : pelem ˚ elemq.pop “q, ival Ź ival “ 1337 u :
t elem Ź elem :: Ω “ int, eq Ź eq : pelem, elemq Ñ bool u
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 36 / 39
49. Module system Calculus..
Introduction and elimination rules:-
$ Γvalid @i P r1.ns. Γ, D1, .., Di´1 $ Bi : Di
Γ $ B1, .., Bn : D1, .., Dn
(TSUM)
Γ $ V : b Ź α :: K
Γ $ V.b :: K
(C-EXT-O)
Γ $ M : y Ź x : A
Γ $ M.y : A
(EXT-V)
Translucency:-
Γ $ V : b Ź α :: K “ A
Γ $ V.b “ A :: K
(ABBREV)
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 37 / 39
50. Module system Calculus..
Value Rules:-
Γ $ V : b Ź α :: K, D1, .., Dn
Γ $ V : b Ź α :: K “ V.b, D1, .., Dn
(VALUE-O)
Γ $ V.y : A1 Γ $ V : y Ź x : A, D1, .., Dn
Γ $ V : y Ź x : A1, D1, .., Dn
(VALUE-V)
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 38 / 39
51. Summary and Outlook
ML has a powerful module system, using signatures, structures
and functors
Modules in ML are not part of the core language
Beyond ML Modules: Recursive modules, higher-order functors,
applicative functors
Jiten, Keheliya, Tobias Module System of Standard ML Tuesday 21st
April, 2015 39 / 39