(* File: GRFNLIB.ML *) (* Project: GR *) fun fst ( a, _) = a; fun snd ( _, b) = b; fun pair x y = (x,y); exception hd'; fun hd (x::_) = x | hd [] = raise hd'; exception tl'; fun tl (_::xs) = xs | tl [] = raise tl'; fun null [] = true | null (_::_) = false; fun flat [] = nil | flat (x::xs) = x @ flat xs; fun member a [] = false | member a (x::xs) = if a=x then true else member a xs; fun flip f y x = f x y; datatype 'a Option = None | Some of 'a; fun find p [] = None | find p (x::xs) = if p x then Some x else find p xs; fun filter p [] = [] | filter p (x::xs) = if p x then x :: filter p xs else filter p xs; fun foreach f [] = () | foreach f (a::x) = (f a; foreach f x); fun foldleft f (e, []) = e | foldleft f (e, x::xs) = foldleft f (f (e,x), xs); fun foldleft_map f g (e, []) = e | foldleft_map f g (e, x::xs) = foldleft_map f g (f (e, g x), xs); fun foldleft_tl f (e, []) = e | foldleft_tl f (e, x::xs) = foldleft_tl f (f(e,x,xs), xs); fun foldright f ([], e) = e | foldright f (x::xs, e) = f (x, foldright f (xs, e)); fun foldright_map f g ([], e) = e | foldright_map f g (x::xs, e) = f (g x, foldright_map f g (xs, e)); fun exists pred [] = false | exists pred (x::xs) = (pred x) orelse exists pred xs; fun forall pred [] = true | forall pred (x::xs) = (pred x) andalso forall pred xs; fun disjoint (xs, ys) = forall (fn x => forall (fn y => x <> y) ys) xs;