28 [C;M;I], Le, [M;I]; (* ??? *)
30 [I;M;C], Ge, [I;M]; (* ??? *)
47 | he::tl -> aux (4 * acc + (match he with I -> 1 | C -> 2 | M -> 3)) tl
54 type t = int * int * w
55 let compare (h1,l1,_) (h2,l2,_) = compare (h1,l1) (h2,l2)
57 let equal ((h1 : int),(l1 : int),_) (h2,l2,_) = l1=l2 && h1=h2
60 module G = Graph.Imperative.Digraph.Concrete(V);;
66 let hash = Hashtbl.hash
70 module GL = Graph.Imperative.Digraph.Concrete(VL);;
72 let swap = function Le -> Ge | Ge -> Le;;
77 | M::tl -> new_dir (swap dir) tl
78 | (C|I)::tl -> new_dir dir tl
84 (List.map (function I -> "i" | C -> "c" | M -> "-") w)
86 if s = "" then "." else s
92 (List.map (function I -> "i" | C -> "c" | M -> "m") w)
94 if s = "" then "E" else s
97 let string_of_eqclass l = String.concat "=" (List.map string_of_w l);;
99 let name_of_eqclass l = String.concat "_" (List.map string_of_w' l);;
103 let (@@) l1 ll2 = List.map (function l2 -> l1 @ l2) ll2;;
106 let rec aux acc = function
109 | h1::h2::tl when h1=h2 -> aux (h2::acc) tl
110 | h1::tl (* when h1 <> h2 *) -> aux (h1::acc) tl
112 List.rev (aux [] (List.sort compare l))
115 let rec apply_rule_at_beginning (lhs,dir',rhs) (w,dir) =
122 | x::lhs,x'::w when x = x' -> aux (lhs,w)
123 | _,_ -> raise NoMatch in
124 let w' = aux (lhs,w) in
125 if List.length rhs < List.length lhs then rhs @@ [w']
126 else rhs @@ apply_rules (aux (lhs,w),new_dir dir lhs)
127 and apply_rules (w,_ as w_and_dir) =
134 (try apply_rule_at_beginning rule w_and_dir
142 let apply_rules (w,dir as w_and_dir) =
143 List.map (fun w' -> w,dir,w')
144 (uniq (apply_rules w_and_dir))
147 let step (l : w list) =
152 List.map (fun x -> x@w)
153 (if List.length (List.filter (fun w -> w = M) w) >= 1 then
165 if i mod 1000 = 0 then
167 print_string ("@" ^ string_of_int i ^ " ");
170 aux (f he :: acc) (i+1) tl
172 let res = List.rev (aux [] 1 l) in
182 if i mod 1000 = 0 then
184 print_string ("@" ^ string_of_int i ^ " ");
193 let normalize (l : w list) =
194 print_endline (string_of_int (List.length l) ^ " nodes to be normalized");
197 (mapi (fun x -> apply_rules (x,Le) @ apply_rules (x,Ge)) l) in
200 (function (x,rel,y) ->
203 match rel with Le -> x,y | Ge -> y,x) rels
208 let visualize graph =
212 let edge_attributes _ = []
213 let default_edge_attributes _ = []
214 let get_subgraph _ = None
215 let vertex_attributes v = [`Label (string_of_eqclass (GL.V.label v))]
216 let vertex_name v = name_of_eqclass (GL.V.label v)
217 let default_vertex_attributes _ = []
218 let graph_attributes _ = []
220 let module D = Graph.Graphviz.Dot(GL) in
221 let ch = open_out "/tmp/comb.dot" in
222 D.output_graph ch graph;
224 ignore (Unix.system ("tred /tmp/comb.dot > /tmp/red.dot"));
225 ignore (Unix.system ("dot -Tps /tmp/red.dot > /tmp/red.ps"));
226 (*Unix.system ("ggv /tmp/red.ps");*)
229 let w_compare s1 s2 =
230 let c = compare (List.length s1) (List.length s2) in
231 if c = 0 then compare s1 s2 else c
234 let normalize_and_describe norm =
235 let cache = Hashtbl.create 5393 in
236 let canonicals = Hashtbl.create 5393 in
237 let descriptions = Hashtbl.create 5393 in
239 let normalized = norm v in
240 let _,_,dsc = G.V.label v in
241 if not (List.mem dsc (Hashtbl.find_all cache normalized)) then
242 Hashtbl.add cache normalized dsc;
245 let vertexes = uniq (Hashtbl.fold (fun k _ l -> k::l) cache []) in
248 (fun v -> v, List.sort w_compare (Hashtbl.find_all cache v)) vertexes in
249 iteri (function (_,w::_) -> Hashtbl.add canonicals w () | _ -> ()) xx;
250 let is_not_redundant =
254 try Hashtbl.find canonicals w; true with Not_found -> false
258 Hashtbl.add descriptions v ((List.filter is_not_redundant x) : eqclass)) xx),
259 Hashtbl.find descriptions
263 print_endline (string_of_int (List.length arcs) ^ " arcs to be classified");
264 let graph = G.create () in
265 iteri (fun (x,y) -> G.add_edge graph x y) arcs;
266 print_endline ("<scc>");
268 let module SCC = Graph.Components.Make(G) in SCC.scc graph in
269 print_endline (string_of_int classes ^ " classes");
270 print_endline ("-----");
274 let analyze (norm,arcs) =
275 print_endline ("building class graph (" ^ string_of_int (List.length arcs) ^ ")");
276 let normalize,finish,describe = normalize_and_describe norm in
277 let arcs = uniq (mapi (fun (x,y) -> normalize x,normalize y) arcs) in
278 print_endline "finish";
280 print_endline ("collapse " ^ string_of_int (List.length arcs) ^ " arcs");
281 let arcs = uniq (mapi (function (x,y) -> describe x,describe y) arcs) in
282 print_endline ("build (" ^ string_of_int (List.length arcs) ^ " arcs)");
283 let cgraph = GL.create () in
284 iteri (function (x,y) -> if x <> y then GL.add_edge cgraph x y) arcs;
285 print_endline "visualize";
287 print_endline ("/////");
290 let rec iter n nodes old_arcs =
291 print_endline ("STEP " ^ string_of_int n);
292 let arcs = old_arcs @ normalize nodes in
293 let pkg = classify arcs in
295 iter (n - 1) (step nodes) arcs