17 [C;M;I], Le, [M;I]; (* ??? *)
19 [I;M;C], Ge, [I;M]; (* ??? *)
27 let swap = function Le -> Ge | Ge -> Le;;
32 | M::tl -> new_dir (swap dir) tl
33 | (C|I)::tl -> new_dir dir tl
38 (List.map (function I -> "i" | C -> "c" | M -> "-") w)
43 let (@@) l1 ll2 = List.map (function l2 -> l1 @ l2) ll2;;
46 let rec aux acc = function
49 | h1::h2::tl when h1=h2 -> aux (h2::acc) tl
50 | h1::tl (* when h1 <> h2 *) -> aux (h1::acc) tl
52 List.rev (aux [] (List.sort compare l))
55 let rec apply_rule_at_beginning (lhs,dir',rhs) (w,dir) =
62 | x::lhs,x'::w when x = x' -> aux (lhs,w)
63 | _,_ -> raise NoMatch
65 rhs @@ apply_rules (aux (lhs,w),new_dir dir lhs)
66 and apply_rules (w,_ as w_and_dir) =
73 (try apply_rule_at_beginning rule w_and_dir
81 let apply_rules (w,dir as w_and_dir) =
82 List.map (fun w' -> w,dir,w')
83 (uniq (apply_rules w_and_dir))
86 let step (l : w list) =
91 List.map (fun x -> x@w)
92 (if List.length (List.filter (fun w -> w = M) w) >= 2 then
103 | he::tl -> aux (f i he :: acc) (i+1) tl
105 List.rev (aux [] 1 l)
108 let normalize (l : w list) =
109 print_endline (string_of_int (List.length l) ^ " nodes to be normalized");
114 if i mod 100 = 0 then print_string ("@" ^ string_of_int i ^ " ");
115 apply_rules (x,Le) @ apply_rules (x,Ge)) l) in
118 List.rev (List.rev_map
119 (function (x,rel,y) -> match rel with Le -> x,y | Ge -> y,x) rels) in
120 let res = uniq arcs in
124 let visualize describe graph =
127 include Graph.Pack.Digraph;;
128 let edge_attributes _ = []
129 let default_edge_attributes _ = []
130 let get_subgraph _ = None
131 let vertex_attributes v = [`Label (describe (Graph.Pack.Digraph.V.label v))]
132 let vertex_name v = "v" ^ string_of_int (Graph.Pack.Digraph.V.label v)
133 let default_vertex_attributes _ = []
134 let graph_attributes _ = []
136 let module D = Graph.Graphviz.Dot(G) in
137 let ch = open_out "/tmp/comb.dot" in
138 D.output_graph ch graph;
140 ignore (Unix.system ("tred /tmp/comb.dot > /tmp/red.dot"));
141 ignore (Unix.system ("dot -Tps /tmp/red.dot > /tmp/red.ps"));
142 (*Unix.system ("ggv /tmp/red.ps");*)
145 let mk_vertex_and_dsc_vertex =
147 let cache1 = Hashtbl.create 37 in
148 let cache2 = Hashtbl.create 37 in
151 Hashtbl.find cache1 w
158 | he::tl -> aux (acc * 4 + (match he with I -> 1 | C -> 2 | M -> 3)) tl
162 let v = Graph.Pack.Digraph.V.create n in
163 Hashtbl.add cache1 w v;
164 Hashtbl.add cache2 v w;
166 (Hashtbl.find cache2)
169 let string_compare s1 s2 =
170 let c = compare (String.length s1) (String.length s2) in
171 if c = 0 then String.compare s1 s2 else c
174 let normalize_and_describe norm mk_vertex dsc_vertex =
175 let cache = Hashtbl.create 37 in
177 let v = mk_vertex n in
178 let normalized = norm v in
179 let dsc = dsc_vertex v in
180 if not (List.mem dsc (Hashtbl.find_all cache normalized)) then
181 Hashtbl.add cache normalized dsc;
184 let vertexes = uniq (Hashtbl.fold (fun k _ l -> k::l) cache []) in
189 List.sort string_compare
190 (List.map string_of_w (Hashtbl.find_all cache v))
192 let is_not_redundant s =
193 let len = String.length s in
194 if len <= 1 then true
196 let w = String.sub s 1 (len - 1) in
197 List.exists (function w'::_ -> w=w' | [] -> false) (List.map snd ll)
199 let l = List.filter is_not_redundant (List.assoc v ll) in
200 let s = String.concat "=" l in
201 if s = "" then "." else s)
205 print_endline (string_of_int (List.length arcs) ^ " arcs to be classified");
206 let mk_vertex,dsc_vertex = mk_vertex_and_dsc_vertex () in
207 let graph = Graph.Pack.Digraph.create () in
210 Graph.Pack.Digraph.add_edge graph (mk_vertex x) (mk_vertex y)) arcs;
211 print_endline ("<scc>");
212 let classes,norm = Graph.Pack.Digraph.Components.scc graph in
213 print_endline (string_of_int classes ^ " classes");
214 norm,mk_vertex,dsc_vertex,arcs
217 let analyze (norm,mk_vertex,dsc_vertex,arcs) =
218 print_endline "building class graph";
219 let normalize,describe = normalize_and_describe norm mk_vertex dsc_vertex in
220 let arcs = uniq (List.map (fun (x,y) -> normalize x,normalize y) arcs) in
221 let cgraph = Graph.Pack.Digraph.create () in
225 Graph.Pack.Digraph.add_edge cgraph (Graph.Pack.Digraph.V.create x
226 ) (Graph.Pack.Digraph.V.create y)) arcs;
227 print_endline "visualize";
228 visualize describe cgraph;
229 print_endline ("-----");
233 let pkg = classify (normalize l) in
235 iter (n - 1) (step l)