functor ORDSET ( type Item val < : Item * Item -> bool structure Basic : sig val fold : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a end) = struct type Item = Item type Set = Item list exception Select val empty = [] fun isEmpty [] = true | isEmpty _ = false fun sing x = [x] fun member [] e = false | member (h :: t) e = if e < h then false else if h < e then member t e else true fun insert [] e = [e] | insert (h :: t) e = if e < h then e :: h :: t else if h < e then h :: insert t e else h :: t fun delete(e, []) = [] | delete(e, h::t) = if e < h then h :: t else if h < e then h :: delete (e,t) else t fun select [] = raise Select | select (h :: t) = (h, t) fun union [] s = s | union s [] = s | union (h::t) (h' :: t') = if h < h' then h :: union t (h' :: t') else if h' < h then h' :: union (h :: t) t' else h :: union t t' fun image f = Basic.fold (fn s => fn j => union s (f j)) [] fun intersect [] s = [] | intersect s [] = [] | intersect(h::t) (h':: t') = if h < h' then intersect t (h' :: t') else if h' < h then intersect (h :: t) t' else h :: intersect t t' fun contains [] x = true | contains (h :: t) (h' :: t') = if h < h' then false else if h' < h then contains (h :: t) t' else contains t t' | contains (h :: t) [] = false end;