1 type rel = Equal | SubsetEqual | SupersetEqual
18 (* compound operator *)
19 type compound_operator = op list
21 let string_of_cop op =
22 if op = [] then "id" else String.concat "" (List.map string_of_op op)
24 let dot_of_cop op = "\"" ^ string_of_cop op ^ "\""
27 let matita_of_op = function I -> "i" | C -> "c" | M -> "m" in
31 | [op] -> matita_of_op op ^ " " ^ v
32 | op::tl -> matita_of_op op ^ " (" ^ aux tl ^ ")"
36 (* representative, other elements in the equivalence class,
37 leq classes, geq classes *)
38 type equivalence_class =
39 compound_operator * compound_operator list *
40 equivalence_class list ref * equivalence_class list ref
42 let (===) (repr,_,_,_) (repr',_,_,_) = repr = repr';;
43 let (<=>) (repr,_,_,_) (repr',_,_,_) = repr <> repr';;
45 let string_of_equivalence_class (repr,others,leq,_) =
46 String.concat " = " (List.map string_of_cop (repr::others)) ^
51 (function (repr',_,_,_) ->
52 string_of_cop repr ^ " ⊆ " ^ string_of_cop repr') !leq)
56 let dot_of_equivalence_class (repr,others,leq,_) =
58 let eq = String.concat " = " (List.map string_of_cop (repr::others)) in
59 dot_of_cop repr ^ "[label=\"" ^ eq ^ "\"];" ^
60 if !leq = [] then "" else "\n"
61 else if !leq = [] then
67 (function (repr',_,_,_) ->
68 dot_of_cop repr' ^ " -> " ^ dot_of_cop repr ^ ";") !leq)
70 (* set of equivalence classes, infima, suprema *)
72 equivalence_class list * equivalence_class list * equivalence_class list
74 let string_of_set (s,_,_) =
75 String.concat "\n" (List.map string_of_equivalence_class s)
77 let ps_of_set (to_be_considered,under_consideration,news) ?processing (s,inf,sup) =
78 let ch = open_out "xxx.dot" in
79 output_string ch "digraph G {\n";
80 (match under_consideration with
83 output_string ch (dot_of_cop repr ^ " [color=yellow];"));
85 (function (repr,_,_,_) ->
86 if List.exists (function (repr',_,_,_) -> repr=repr') sup then
87 output_string ch (dot_of_cop repr ^ " [shape=Mdiamond];")
89 output_string ch (dot_of_cop repr ^ " [shape=diamond];")
92 (function (repr,_,_,_) ->
93 if not (List.exists (function (repr',_,_,_) -> repr=repr') inf) then
94 output_string ch (dot_of_cop repr ^ " [shape=polygon];")
97 (function repr -> output_string ch (dot_of_cop repr ^ " [color=green];")
100 (function repr -> output_string ch (dot_of_cop repr ^ " [color=navy];")
102 output_string ch (String.concat "\n" (List.map dot_of_equivalence_class s));
103 output_string ch "\n";
104 (match processing with
106 | Some (repr,rel,repr') ->
107 output_string ch (dot_of_cop repr ^ " [color=red];");
110 SupersetEqual -> repr',repr
112 | SubsetEqual -> repr,repr'
115 (dot_of_cop repr' ^ " -> " ^ dot_of_cop repr ^
117 (match rel with Equal -> "arrowhead=none " | _ -> "") ^
118 "style=dashed];\n"));
119 output_string ch "}\n";
121 (*ignore (Unix.system "tred xxx.dot > yyy.dot && dot -Tps yyy.dot > xxx.ps")*)
122 ignore (Unix.system "cp xxx.ps xxx_old.ps && dot -Tps xxx.dot > xxx.ps");
123 (*ignore (read_line ())*)
126 let test to_be_considered_and_now ((s,_,_) as set) rel candidate repr =
127 ps_of_set to_be_considered_and_now ~processing:(candidate,rel,repr) set;
129 (string_of_cop candidate ^ " " ^ string_of_rel rel ^ " " ^ string_of_cop repr ^ "? ");
131 assert (Unix.system "cp formal_topology.ma xxx.ma" = Unix.WEXITED 0);
132 let ch = open_out_gen [Open_append ; Open_creat] 0 "xxx.ma" in
135 (function (repr,others,leq,_) ->
140 ("axiom ax" ^ string_of_int !i ^
142 matita_of_cop "A" repr ^ " = " ^ matita_of_cop "A" repr' ^ ".\n");
145 (function (repr',_,_,_) ->
148 ("axiom ax" ^ string_of_int !i ^
150 matita_of_cop "A" repr ^ " ⊆ " ^ matita_of_cop "A" repr' ^ ".\n");
153 let candidate',rel',repr' =
155 SupersetEqual -> repr,SubsetEqual,candidate
157 | SubsetEqual -> candidate,rel,repr in
159 ("theorem foo: \\forall A." ^ matita_of_cop "A" candidate' ^
160 " " ^ string_of_rel rel' ^ " " ^
161 matita_of_cop "A" repr' ^ ". intros; autobatch size=8 depth=4. qed.") in
162 output_string ch (query ^ "\n");
165 (*Unix.system "../../../matitac.opt xxx.ma >> log 2>&1" = Unix.WEXITED 0*)
166 Unix.system "../../../matitac.opt xxx.ma > /dev/null 2>&1" = Unix.WEXITED 0
168 let ch = open_out_gen [Open_append] 0o0600 "log.ma" in
170 output_string ch (query ^ "\n")
172 output_string ch ("(* " ^ query ^ "*)\n");
174 print_endline (if res then "y" else "n");
177 let remove node = List.filter (fun node' -> node <=> node');;
179 let add_leq_arc ((_,_,leq,_) as node) ((_,_,_,geq') as node') =
180 leq := node' :: !leq;
181 geq' := node :: !geq'
184 let add_geq_arc ((_,_,_,geq) as node) ((_,_,leq',_) as node') =
185 geq := node' :: !geq;
186 leq' := node :: !leq'
189 let remove_leq_arc ((_,_,leq,_) as node) ((_,_,_,geq') as node') =
190 leq := remove node' !leq;
191 geq' := remove node !geq'
194 let remove_geq_arc ((_,_,_,geq) as node) ((_,_,leq',_) as node') =
195 geq := remove node' !geq;
196 leq' := remove node !leq'
199 let leq_transitive_closure node node' =
200 add_leq_arc node node';
201 let rec remove_transitive_arcs ((_,_,_,geq) as node) (_,_,leq',_) =
202 let rec remove_arcs_to_ascendents =
205 | (_,_,leq,_) as node'::tl ->
206 remove_leq_arc node node';
207 remove_arcs_to_ascendents (!leq@tl)
209 remove_arcs_to_ascendents !leq';
210 List.iter (function son -> remove_transitive_arcs son node) !geq
212 remove_transitive_arcs node node'
215 let geq_transitive_closure node node' =
216 add_geq_arc node node';
217 let rec remove_transitive_arcs ((_,_,leq,_) as node) (_,_,_,geq') =
218 let rec remove_arcs_to_descendents =
221 | (_,_,_,geq) as node'::tl ->
222 remove_geq_arc node node';
223 remove_arcs_to_descendents (!geq@tl)
225 remove_arcs_to_descendents !geq';
226 List.iter (function father -> remove_transitive_arcs father node) !leq
228 remove_transitive_arcs node node'
231 let (@@) l1 n = if List.exists (function n' -> n===n') l1 then l1 else l1@[n]
233 let rec leq_reachable node =
236 | node'::_ when node === node' -> true
237 | (_,_,leq,_)::tl -> leq_reachable node (!leq@tl)
240 let rec geq_reachable node =
243 | node'::_ when node === node' -> true
244 | (_,_,_,geq)::tl -> geq_reachable node (!geq@tl)
247 let locate_using_leq to_be_considered_and_now ((repr,_,leq,geq) as node)
250 let rec aux ((nodes,inf,sup) as set) =
253 | (repr',_,_,geq') as node' :: tl ->
254 if repr=repr' then aux set (!geq'@tl)
255 else if leq_reachable node' !leq then
257 else if test to_be_considered_and_now set SubsetEqual repr repr' then
259 let sup = remove node sup in
262 let inf = remove node' inf in
270 leq_transitive_closure node node';
271 aux (nodes,inf,sup) (!geq'@tl)
279 exception SameEquivalenceClass of set * equivalence_class * equivalence_class;;
281 let locate_using_geq to_be_considered_and_now ((repr,_,leq,geq) as node)
284 let rec aux ((nodes,inf,sup) as set) =
287 | (repr',_,leq',_) as node' :: tl ->
288 if repr=repr' then aux set (!leq'@tl)
289 else if geq_reachable node' !geq then
291 else if test to_be_considered_and_now set SupersetEqual repr repr' then
293 if List.exists (function n -> n===node') !leq then
294 (* We have found two equal nodes! *)
295 raise (SameEquivalenceClass (set,node,node'))
298 let inf = remove node inf in
301 let sup = remove node' sup in
309 geq_transitive_closure node node';
310 aux (nodes,inf,sup) (!leq'@tl)
319 let analyze_one to_be_considered repr hecandidate (news,((nodes,inf,sup) as set)) =
320 if not (List.for_all (fun ((_,_,_,geq) as node) -> !geq = [] && let rec check_sups = function [] -> true | (_,_,leq,_) as node::tl -> if !leq = [] then List.exists (fun n -> n===node) sup && check_sups tl else check_sups (!leq@tl) in check_sups [node]) inf) then ((*ps_of_set ([],None,[]) set;*) assert false);
321 if not (List.for_all (fun ((_,_,leq,_) as node) -> !leq = [] && let rec check_infs = function [] -> true | (_,_,_,geq) as node::tl -> if !geq = [] then List.exists (fun n -> n===node) inf && check_infs tl else check_infs (!geq@tl) in check_infs [node]) sup) then (ps_of_set ([],None,[]) set; assert false);
322 let candidate = hecandidate::repr in
323 if List.length (List.filter ((=) M) candidate) > 1 then
329 let node = candidate,[],leq,geq in
330 let nodes = nodes@[node] in
331 let set = nodes,inf@[node],sup@[node] in
332 let start_inf,start_sup =
334 match List.filter (fun (repr',_,_,_) -> repr=repr') nodes with
339 match hecandidate with
341 | C -> [repr_node],sup
346 locate_using_leq (to_be_considered,Some repr,news) node set start_sup in
348 let _,inf,sup = set in
349 if not (List.for_all (fun ((_,_,_,geq) as node) -> !geq = [] && let rec check_sups = function [] -> true | (_,_,leq,_) as node::tl -> if !leq = [] then List.exists (fun n -> n===node) sup && check_sups tl else check_sups (!leq@tl) in check_sups [node]) inf) then (ps_of_set ([],None,[]) set; assert false);
350 if not (List.for_all (fun ((_,_,leq,_) as node) -> !leq = [] && let rec check_infs = function [] -> true | (_,_,_,geq) as node::tl -> if !geq = [] then List.exists (fun n -> n===node) inf && check_infs tl else check_infs (!geq@tl) in check_infs [node]) sup) then (ps_of_set ([],None,[]) set; assert false);
353 locate_using_geq (to_be_considered,Some repr,news) node set start_inf
356 let _,inf,sup = set in
357 if not (List.for_all (fun ((_,_,_,geq) as node) -> !geq = [] && let rec check_sups = function [] -> true | (_,_,leq,_) as node::tl -> if !leq = [] then List.exists (fun n -> n===node) sup && check_sups tl else check_sups (!leq@tl) in check_sups [node]) inf) then (ps_of_set ([],None,[]) set; assert false);
358 if not (List.for_all (fun ((_,_,leq,_) as node) -> !leq = [] && let rec check_infs = function [] -> true | (_,_,_,geq) as node::tl -> if !geq = [] then List.exists (fun n -> n===node) inf && check_infs tl else check_infs (!geq@tl) in check_infs [node]) sup) then ((*ps_of_set ([],None,[]) set;*) assert false);
362 SameEquivalenceClass ((nodes,inf,sup) as set,((r,_,leq_d,geq_d) as node_to_be_deleted),node')->
363 prerr_endline ("SAMEEQCLASS: " ^ string_of_cop r);
365 let _,inf,sup = set in
366 if not (List.for_all (fun ((_,_,_,geq) as node) -> !geq = [] && let rec check_sups = function [] -> true | (_,_,leq,_) as node::tl -> if !leq = [] then List.exists (fun n -> n===node) sup && check_sups tl else check_sups (!leq@tl) in check_sups [node]) inf) then (ps_of_set ([],None,[]) set; assert false);
367 if not (List.for_all (fun ((_,_,leq,_) as node) -> !leq = [] && let rec check_infs = function [] -> true | (_,_,_,geq) as node::tl -> if !geq = [] then List.exists (fun n -> n===node) inf && check_infs tl else check_infs (!geq@tl) in check_infs [node]) sup) then ((*ps_of_set ([],None,[]) set;*) assert false);
369 let rec clean inf sup res =
372 | node::tl when node===node_to_be_deleted ->
374 | (repr',others,leq,geq) as node::tl ->
378 if node_to_be_deleted <=> node then
383 let sup = if !leq = [] then sup@@node else sup in
387 if node_to_be_deleted <=> node then
392 let inf = if !geq = [] then inf@@node else inf in
394 clean inf sup ((repr',others@[candidate],leq,geq)::res) tl
396 clean inf sup (node::res) tl
398 let inf,sup,nodes = clean inf sup [] nodes in
399 let inf = remove node_to_be_deleted inf in
400 let sup = remove node_to_be_deleted sup in
401 let set = nodes,inf,sup in
403 let _,inf,sup = set in
404 if not (List.for_all (fun ((_,_,_,geq) as node) -> !geq = [] && let rec check_sups = function [] -> true | (_,_,leq,_) as node::tl -> if !leq = [] then List.exists (fun n -> n===node) sup && check_sups tl else check_sups (!leq@tl) in check_sups [node]) inf) then (ps_of_set ([],None,[]) set; assert false);
405 if not (List.for_all (fun ((_,_,leq,_) as node) -> !leq = [] && let rec check_infs = function [] -> true | (_,_,_,geq) as node::tl -> if !geq = [] then List.exists (fun n -> n===node) inf && check_infs tl else check_infs (!geq@tl) in check_infs [node]) sup) then (ps_of_set ([],None,[]) set; assert false);
410 let rec explore i (set:set) news =
411 let rec aux news set =
416 List.fold_right (analyze_one tl repr) [I;C;M] (news,set)
420 let news,set = aux [] set news in
423 print_endline ("PUNTO FISSO RAGGIUNTO! i=" ^ string_of_int i);
424 print_endline (string_of_set set ^ "\n----------------");
425 ps_of_set ([],None,[]) set
429 print_endline ("NUOVA ITERAZIONE, i=" ^ string_of_int i);
430 print_endline (string_of_set set ^ "\n----------------");
431 explore (i+1) set news
435 let id_node = id,[],ref [], ref [] in
436 let set = [id_node],[id_node],[id_node] in
437 print_endline ("PRIMA ITERAZIONE, i=0, j=0");
438 print_endline (string_of_set set ^ "\n----------------");
439 (*ignore (Unix.system "rm -f log");*)
440 assert (Unix.system "cp formal_topology.ma log.ma" = Unix.WEXITED 0);
441 ps_of_set ([id],None,[]) set;