Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* Moscow ML has some weird syntax for structure packing
- * due to it being compatible with Standard ML.
- * For example, structure and value identifiers are in separate namespaces
- * I put a comment above each section with the syntax if merged
- *
- * Run with `mosml -P full -liberal test.sml`
- *)
- (*
- type 'a set =
- sig
- type t
- val empty : t
- val insert : t * 'a -> t
- val remove : t * 'a -> t
- val member : t * 'a -> bool
- end
- *)
- signature SET =
- sig
- type t
- type elem
- val empty : unit -> t
- val insert : t * elem -> t
- val remove : t * elem -> t
- val member : t * elem -> bool
- end
- type 'a set = [ SET where type elem = 'a ]
- (*
- fun with_set (Set : int set) =
- let val s =
- ...
- *)
- fun with_set (set_impl : int set) : unit =
- let structure Set as SET where type elem = int = set_impl
- val s : Set.t =
- List.foldr
- (fn (x, s) => Set.insert (s, x))
- (Set.empty ())
- [1, 5, 2, 0]
- fun pr n =
- if Set.member (s, n)
- then TextIO.print (Int.toString n ^ " is in the set\n")
- else ()
- in List.app pr [0, 1, 2, 3, 4, 5, 6]
- end
- (*
- type 'a eq_key =
- sig
- val equals : 'a * 'a -> bool
- end
- *)
- signature EQ_KEY =
- sig
- type t
- val equals : t * t -> bool
- end
- type 'a eq_key = [ EQ_KEY where type t = 'a ]
- (*
- fun eq_set (Key : 'a eq_key) =
- struct
- type t = Key.t list
- fun empty () = []
- ...
- end
- *)
- fun eq_set (key : 'a eq_key) : 'a set =
- let structure Key as EQ_KEY where type t = 'a = key
- structure Set =
- struct
- type t = Key.t list
- type elem = Key.t
- fun empty () = []
- fun insert (xs, x) = x :: xs
- fun remove (xs, x) = List.filter (fn y => not (Key.equals (x, y))) xs
- fun member (xs, x) = List.exists (fn y => Key.equals (x, y)) xs
- end
- in [ structure Set as SET where type elem = 'a ]
- end
- (*
- val int_key : int eq_key =
- struct
- val equals = op =
- end
- *)
- val int_key : int eq_key =
- let structure K =
- struct
- type t = int
- val equals = op =
- end
- in [ structure K as EQ_KEY where type t = int ]
- end
- (* val () = with_set (eq_set int_key) *)
- val () = with_set (eq_set int_key)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement