(* Some general-purpose functions about sets, functions, etc. *) (* $Id: library.ml,v 1.16 2000/07/07 20:22:27 alamstei Exp $ *) (* returns the list [0;1;...;n-1], n>=0 *) let create n = let rec do_create count n = if (count set1 | (hd2::tl2) -> begin let set1 = add_element hd2 set1 in set_union set1 tl2 end (* return set \ {element} *) let rec delete_element element set = if not (set_member element set) then set else match set with ([]) -> set | (hd::tl) -> begin if (hd = element) then tl else set_union (singleton hd) (delete_element element tl) end (* return set1 \ set2 *) let rec set_difference set1 set2 = match set2 with ([]) -> set1 | (hd2::tl2) -> begin let set1 = delete_element hd2 set1 in set_difference set1 tl2 end (* return set1 intersect set2 *) let rec set_intersection set1 set2 = match set2 with ([]) -> [] | (hd2::tl2) -> if (set_member hd2 set1) then (set_union (singleton hd2) (set_intersection set1 tl2)) else set_intersection set1 tl2 (* return the union of a list of sets *) let rec set_flatten sets = match sets with hd::tl -> set_union hd (set_flatten tl) | [] -> [] (* operations on functions *) (* return the domain of the function sigma *) let rec get_domain sigma = match sigma with (tvar,t)::tl -> tvar::(get_domain tl) | [] -> [] (* return the modified function sigma whose domain is (dom (sigma)) intersect domain *) let rec restrict_domain sigma domain = match sigma with [] -> [] | (x,sx)::tl -> if List.mem x domain then (x,sx)::(restrict_domain tl domain) else restrict_domain tl domain (* miscellaneous *) (* return a string of the list list using pretty-printer pp. separate the elements with the separator sep. *) let rec string_of_list pp sep list = match list with [] -> "" | (hd::[]) -> pp(hd) | (hd::tl) -> pp(hd) ^sep^ (string_of_list pp sep tl)