Advertisement
Skyb0rg

Untitled

Aug 14th, 2021
3,200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (* Moscow ML has some weird syntax for structure packing
  2.  * due to it being compatible with Standard ML.
  3.  * For example, structure and value identifiers are in separate namespaces
  4.  * I put a comment above each section with the syntax if merged
  5.  *
  6.  * Run with `mosml -P full -liberal test.sml`
  7.  *)
  8.  
  9.  
  10. (*
  11. type 'a set =
  12.     sig
  13.         type t
  14.         val empty : t
  15.         val insert : t * 'a -> t
  16.         val remove : t * 'a -> t
  17.         val member : t * 'a -> bool
  18.     end
  19. *)
  20. signature SET =
  21. sig
  22.     type t
  23.     type elem
  24.  
  25.     val empty : unit -> t
  26.     val insert : t * elem -> t
  27.     val remove : t * elem -> t
  28.     val member : t * elem -> bool
  29. end
  30. type 'a set = [ SET where type elem = 'a ]
  31.  
  32. (*
  33. fun with_set (Set : int set) =
  34.     let val s =
  35.         ...
  36. *)
  37. fun with_set (set_impl : int set) : unit =
  38.     let structure Set as SET where type elem = int = set_impl
  39.         val s : Set.t =
  40.             List.foldr
  41.             (fn (x, s) => Set.insert (s, x))
  42.             (Set.empty ())
  43.             [1, 5, 2, 0]
  44.         fun pr n =
  45.             if Set.member (s, n)
  46.                 then TextIO.print (Int.toString n ^ " is in the set\n")
  47.                 else ()
  48.     in  List.app pr [0, 1, 2, 3, 4, 5, 6]
  49.     end
  50.  
  51. (*
  52. type 'a eq_key =
  53.     sig
  54.         val equals : 'a * 'a -> bool
  55.     end
  56. *)
  57. signature EQ_KEY =
  58. sig
  59.     type t
  60.     val equals : t * t -> bool
  61. end
  62. type 'a eq_key = [ EQ_KEY where type t = 'a ]
  63.  
  64. (*
  65. fun eq_set (Key : 'a eq_key) =
  66.     struct
  67.         type t = Key.t list
  68.         fun empty () = []
  69.         ...
  70.     end
  71. *)
  72. fun eq_set (key : 'a eq_key) : 'a set =
  73.     let structure Key as EQ_KEY where type t = 'a = key
  74.         structure Set =
  75.         struct
  76.             type t = Key.t list
  77.             type elem = Key.t
  78.  
  79.             fun empty () = []
  80.             fun insert (xs, x) = x :: xs
  81.             fun remove (xs, x) = List.filter (fn y => not (Key.equals (x, y))) xs
  82.             fun member (xs, x) = List.exists (fn y => Key.equals (x, y)) xs
  83.         end
  84.     in  [ structure Set as SET where type elem = 'a ]
  85.     end
  86.  
  87. (*
  88. val int_key : int eq_key =
  89.     struct
  90.         val equals = op =
  91.     end
  92. *)
  93. val int_key : int eq_key =
  94.     let structure K =
  95.         struct
  96.             type t = int
  97.             val equals = op =
  98.         end
  99.     in  [ structure K as EQ_KEY where type t = int ]
  100.     end
  101.  
  102. (* val () = with_set (eq_set int_key) *)
  103. val () = with_set (eq_set int_key)
  104.  
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement