(* naive persistent implementation using a map: * a mapping i->j means that repr(i)=repr(j) * no mapping for i means repr(i)=i *) module M = Map.Make(struct type t = int let compare = compare end) type t = int M.t let create n = M.empty let find m i = let rec lookup i = try lookup (M.find i m) with Not_found -> i in lookup i let union m i j = let ri = find m i in let rj = find m j in if ri <> rj then M.add ri rj m else m