ML Structure TutoriaL
(* IN *)
- structure test =
= struct
= val x = 2;
= end;
(* OUT *)
structure test : sig val x : int end
(* IN *)
- structure test2 =
= struct
= structure test1 = test
= val y = test1.x
= end;
(* OUT *)
structure test2 :
sig
structure test1 :
val y : int
end
(* IN *)
-test2.y;
(* OUT *)
val it = 2 : int
(* IN *)
-test2.test1.x;
(* OUT *)
val it = 2 : int
=========================
(* IN *)
- structure test =
= struct val x = 2
= end;
REPEATED DEFINITION FROM ABOVE
(* OUT *)
structure test : sig val x : int end
(* IN *)
-functor testFnc() =
= struct
= val x = 2;
= end;
(* OUT *)
functor testFnc :
EACH INSTANCE OF A STRUCTURE CREATED VIA A PARTICULAR
FUNCTOR IS DISTINCT, WITH ITS OWN PLACE IN MEMORY.
(* IN *)
-structure test2 = testFnc();
(* OUT *)
structure test2 : sig val x : int end
(* IN *)
-test2.x;
(* OUT *)
val it = 2 : int
==================================
(* IN *)
- signature TEST = sig
= val x : int
= end;
(* OUT *)
signature TEST = sig val x : int end
RECALL FUNCTOR DEFINITION ABOVE
(* IN *)
- functor test2Fnc(test: TEST) =
= struct
= structure test1: TEST = test
= val y = test1.x;
= end;
(* OUT *)
functor test2Fnc :
(* IN *)
- structure atest: TEST = testFnc();
(* OUT *)
structure atest : TEST
(* IN *)
- structure btest = test2Fnc(atest);
(* OUT *)
structure btest :
sig
structure test1 :
val y : int
end
(* IN *)
- btest.y;
(* OUT *)
val it = 2 : int
(* IN *)
- signature TEST1 =
= sig
= val x1 : int
= end;
(* OUT *)
signature TEST1 = sig val x1 : int end
(* IN *)
- signature TEST2 =
= sig
= val x2 : int
= end;
(* OUT *)
signature TEST2 = sig val x2 : int end
(* IN *)
- signature TEST3PIECES =
= sig
= structure test1 : TEST1
= structure test2 : TEST2
= end;
(* OUT *)
signature TEST3PIECES =
sig
structure test1 : sig val x1 : int end
structure test2 : sig val x2 : int end
end
(* IN *)
- functor test1Fnc() =
= struct
= val x1 = 1;
= end;
(* OUT *)
functor test1Fnc :
(* IN *)
- functor test2Fnc() =
= struct
= val x2 = 2;
= end;
(* OUT *)
functor test2Fnc :
(* IN *)
- functor test3Fnc( pieces : TEST3PIECES ) =
= struct
= structure myTest1 = pieces.test1
= structure myTest2 = pieces.test2
=
= val x3 = myTest1.x1 + myTest2.x2;
= end;
(* OUT *)
functor test3Fnc :
NOW WE USE THESE PIECES TO MAKE AN INSTANCE
OF A STRUCTURE CALLED HERE test3
(* IN *)
- structure test3Pieces : TEST3PIECES =
= struct
= structure test1 : TEST1 = test1Fnc()
= structure test2 : TEST2 = test2Fnc()
= end;
(* OUT *)
structure test3Pieces : TEST3PIECES
(* IN *)
- structure test3 = test3Fnc(test3Pieces);
(* OUT *)
structure test3 :
sig
structure myTest1 :
structure myTest2 :
val x3 : int
end
(* IN *)
- test3.x3;
(* OUT *)
val it = 3 : int