24 [C;M;I], Le, [M;I]; (* ??? *)
26 [I;M;C], Ge, [I;M]; (* ??? *)
43 | he::tl -> aux (4 * acc + (match he with I -> 1 | C -> 2 | M -> 3)) tl
50 type t = int * int * w
51 let compare (h1,l1,_) (h2,l2,_) = compare (h1,l1) (h2,l2)
53 let equal ((h1 : int),(l1 : int),_) (h2,l2,_) = l1=l2 && h1=h2
56 module G = Graph.Imperative.Digraph.Concrete(V);;
62 let hash = Hashtbl.hash
66 module GL = Graph.Imperative.Digraph.Concrete(VL);;
68 let swap = function Le -> Ge | Ge -> Le;;
73 | M::tl -> new_dir (swap dir) tl
74 | (C|I)::tl -> new_dir dir tl
79 (List.map (function I -> "i" | C -> "c" | M -> "-") w)
84 (List.map (function I -> "i" | C -> "c" | M -> "m") w)
87 let string_of_eqclass l =
88 let s = String.concat "=" (List.map string_of_w l) in
89 if s = "" then "." else s
92 let name_of_eqclass l =
93 let s = String.concat "_" (List.map string_of_w' l) in
94 if s = "" then "E" else s
99 let (@@) l1 ll2 = List.map (function l2 -> l1 @ l2) ll2;;
102 let rec aux acc = function
105 | h1::h2::tl when h1=h2 -> aux (h2::acc) tl
106 | h1::tl (* when h1 <> h2 *) -> aux (h1::acc) tl
108 List.rev (aux [] (List.sort compare l))
111 let rec apply_rule_at_beginning (lhs,dir',rhs) (w,dir) =
118 | x::lhs,x'::w when x = x' -> aux (lhs,w)
119 | _,_ -> raise NoMatch
121 rhs @@ apply_rules (aux (lhs,w),new_dir dir lhs)
122 and apply_rules (w,_ as w_and_dir) =
129 (try apply_rule_at_beginning rule w_and_dir
137 let apply_rules (w,dir as w_and_dir) =
138 List.map (fun w' -> w,dir,w')
139 (uniq (apply_rules w_and_dir))
142 let step (l : w list) =
147 List.map (fun x -> x@w)
148 (if List.length (List.filter (fun w -> w = M) w) >= 3 then
160 if i mod 1000 = 0 then
162 print_string ("@" ^ string_of_int i ^ " ");
165 aux (f he :: acc) (i+1) tl
167 let res = List.rev (aux [] 1 l) in
177 if i mod 1000 = 0 then
179 print_string ("@" ^ string_of_int i ^ " ");
188 let normalize (l : w list) =
189 print_endline (string_of_int (List.length l) ^ " nodes to be normalized");
192 (mapi (fun x -> apply_rules (x,Le) @ apply_rules (x,Ge)) l) in
195 (function (x,rel,y) ->
198 match rel with Le -> x,y | Ge -> y,x) rels
203 let visualize graph =
207 let edge_attributes _ = []
208 let default_edge_attributes _ = []
209 let get_subgraph _ = None
210 let vertex_attributes v = [`Label (string_of_eqclass (GL.V.label v))]
211 let vertex_name v = name_of_eqclass (GL.V.label v)
212 let default_vertex_attributes _ = []
213 let graph_attributes _ = []
215 let module D = Graph.Graphviz.Dot(GL) in
216 let ch = open_out "/tmp/comb.dot" in
217 D.output_graph ch graph;
219 ignore (Unix.system ("tred /tmp/comb.dot > /tmp/red.dot"));
220 ignore (Unix.system ("dot -Tps /tmp/red.dot > /tmp/red.ps"));
221 (*Unix.system ("ggv /tmp/red.ps");*)
224 let w_compare s1 s2 =
225 let c = compare (List.length s1) (List.length s2) in
226 if c = 0 then compare s1 s2 else c
229 let normalize_and_describe norm =
230 let cache = Hashtbl.create 5393 in
231 let canonicals = Hashtbl.create 5393 in
232 let descriptions = Hashtbl.create 5393 in
234 let normalized = norm v in
235 let _,_,dsc = G.V.label v in
236 if not (List.mem dsc (Hashtbl.find_all cache normalized)) then
237 Hashtbl.add cache normalized dsc;
240 let vertexes = uniq (Hashtbl.fold (fun k _ l -> k::l) cache []) in
243 (fun v -> v, List.sort w_compare (Hashtbl.find_all cache v)) vertexes in
244 iteri (function (_,w::_) -> Hashtbl.add canonicals w () | _ -> ()) xx;
245 let is_not_redundant =
249 try Hashtbl.find canonicals w; true with Not_found -> false
253 Hashtbl.add descriptions v ((List.filter is_not_redundant x) : eqclass)) xx),
254 Hashtbl.find descriptions
258 print_endline (string_of_int (List.length arcs) ^ " arcs to be classified");
259 let graph = G.create () in
260 iteri (fun (x,y) -> G.add_edge graph x y) arcs;
261 print_endline ("<scc>");
263 let module SCC = Graph.Components.Make(G) in SCC.scc graph in
264 print_endline (string_of_int classes ^ " classes");
265 print_endline ("-----");
269 let analyze (norm,arcs) =
270 print_endline ("building class graph (" ^ string_of_int (List.length arcs) ^ ")");
271 let normalize,finish,describe = normalize_and_describe norm in
272 let arcs = uniq (mapi (fun (x,y) -> normalize x,normalize y) arcs) in
273 print_endline "finish";
275 print_endline ("collapse " ^ string_of_int (List.length arcs) ^ " arcs");
276 let arcs = uniq (mapi (function (x,y) -> describe x,describe y) arcs) in
277 print_endline ("build (" ^ string_of_int (List.length arcs) ^ " arcs)");
278 let cgraph = GL.create () in
279 iteri (function (x,y) -> if x <> y then GL.add_edge cgraph x y) arcs;
280 print_endline "visualize";
282 print_endline ("/////");
286 print_endline ("STEP " ^ string_of_int n);
287 let pkg = classify (normalize l) in
289 iter (n - 1) (step l)