Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* Here is a basic example of the Moscow ML first-class-modules extension
- * The comments following each declaration denote what the interpreter responds with (modified for readability)
- *)
- signature MY_SIG = sig
- type t;
- val x : t;
- val print : unit -> unit;
- end;
- (* signature MY_SIG = ∃t . sig type t = t; val x : t; val print : unit -> unit; end *)
- (* Note: Moscow ML represents abstract types as existentials *)
- structure StructA : MY_SIG where type t = int = struct
- type t = int;
- val x = 12;
- fun print () = TextIO.print "Hello from StructA\n";
- end;
- (* structure StructA : sig type t = int; val x : t; val print : unit -> unit; end *)
- structure StructB : MY_SIG where type t = string = struct
- type t = string;
- val x = "foo";
- fun print () = TextIO.print "Hello from StructB\n";
- end;
- (* structure StructB : sig type t = string; val x : t; val print : unit -> unit; end *)
- val structA = [ structure StructA as MY_SIG ];
- (* val structA : [ MY_SIG ] *)
- (* This syntax represents "structure packing".
- *
- * structure S : SIG
- * -------------------------------------- Structure Packing
- * Γ ⊢ [ structure S as SIG ] : [ SIG ]
- *)
- val structB = [ structure StructB as MY_SIG ];
- (* val structB : [ MY_SIG ] *)
- fun print_struct s =
- let structure S as MY_SIG = s
- in S.print ()
- end;
- (* val print_struct : [ MY_SIG ] -> unit *)
- (*
- * Γ ⊢ e : [ SIG ]
- * -------------------------------------------- Structure Unpacking
- * structure S as SIG = e ⇒ structure S : SIG
- *)
- fun conditional_print b =
- let structure S as MY_SIG = if b then structA else structB
- in S.print ()
- end;
- (* conditional_print : bool -> unit *)
- fun get_x_mono s =
- let structure S as MY_SIG where type t = int = s
- in S.x
- end;
- (* val get_x_mono : [ MY_SIG where type t = int ] -> int *)
- (* First-class modules require annotations for dependent types *)
- functor Dependent(structure S : MY_SIG) = struct
- val x = S.x
- end;
- (* functor Dependent : (structure S : MY_SIG) -> (sig val x : S.t end) *)
- fun dependent s =
- let structure S as MY_SIG = s
- in S.x
- end;
- (* Type error: Scope violation *)
- fun dependent s =
- let structure S as MY_SIG where type t = 'a = s
- in S.x
- end;
- (* val 'a dependent : [ MY_SIG where type t = 'a ] -> 'a *)
Add Comment
Please, Sign In to add comment