1 type rel = Equal | SubsetEqual | SupersetEqual
12 let string_of_op = function I -> "i" | C -> "c" | M -> "-"
13 let matita_of_op = function I -> "i" | C -> "c" | M -> "m"
15 (* compound operator *)
16 type compound_operator = op list
18 let string_of_cop op =
19 if op = [] then "id" else String.concat "" (List.map string_of_op op)
21 let dot_of_cop op = "\"" ^ string_of_cop op ^ "\""
27 | [op] -> matita_of_op op ^ " " ^ v
28 | op::tl -> matita_of_op op ^ " (" ^ aux tl ^ ")"
32 let name_of_theorem cop rel cop' =
35 Equal -> cop,"eq",cop'
36 | SubsetEqual -> cop,"leq",cop'
37 | SupersetEqual -> cop',"leq",cop
40 String.concat "" (List.map matita_of_op cop) ^ "_" ^
41 String.concat "" (List.map matita_of_op cop')
44 (* representative, other elements in the equivalence class,
45 leq classes, geq classes *)
46 type equivalence_class =
47 compound_operator * compound_operator list *
48 equivalence_class list ref * equivalence_class list ref
50 let (===) (repr,_,_,_) (repr',_,_,_) = repr = repr';;
51 let (<=>) (repr,_,_,_) (repr',_,_,_) = repr <> repr';;
53 let string_of_equivalence_class (repr,others,leq,_) =
54 String.concat " = " (List.map string_of_cop (repr::others)) ^
59 (function (repr',_,_,_) ->
60 string_of_cop repr ^ " ⊆ " ^ string_of_cop repr') !leq)
64 let dot_of_equivalence_class (repr,others,leq,_) =
66 let eq = String.concat " = " (List.map string_of_cop (repr::others)) in
67 dot_of_cop repr ^ "[label=\"" ^ eq ^ "\"];" ^
68 if !leq = [] then "" else "\n"
69 else if !leq = [] then
75 (function (repr',_,_,_) ->
76 dot_of_cop repr' ^ " -> " ^ dot_of_cop repr ^ ";") !leq)
78 (* set of equivalence classes, infima, suprema *)
80 equivalence_class list * equivalence_class list * equivalence_class list
82 let string_of_set (s,_,_) =
83 String.concat "\n" (List.map string_of_equivalence_class s)
85 let ps_of_set (to_be_considered,under_consideration,news) ?processing (s,inf,sup) =
86 let ch = open_out "xxx.dot" in
87 output_string ch "digraph G {\n";
88 (match under_consideration with
91 output_string ch (dot_of_cop repr ^ " [color=yellow];"));
93 (function (repr,_,_,_) ->
94 if List.exists (function (repr',_,_,_) -> repr=repr') sup then
95 output_string ch (dot_of_cop repr ^ " [shape=Mdiamond];")
97 output_string ch (dot_of_cop repr ^ " [shape=diamond];")
100 (function (repr,_,_,_) ->
101 if not (List.exists (function (repr',_,_,_) -> repr=repr') inf) then
102 output_string ch (dot_of_cop repr ^ " [shape=polygon];")
105 (function repr -> output_string ch (dot_of_cop repr ^ " [color=green];")
108 (function repr -> output_string ch (dot_of_cop repr ^ " [color=navy];")
110 output_string ch (String.concat "\n" (List.map dot_of_equivalence_class s));
111 output_string ch "\n";
112 (match processing with
114 | Some (repr,rel,repr') ->
115 output_string ch (dot_of_cop repr ^ " [color=red];");
118 SupersetEqual -> repr',repr
120 | SubsetEqual -> repr,repr'
123 (dot_of_cop repr' ^ " -> " ^ dot_of_cop repr ^
125 (match rel with Equal -> "arrowhead=none " | _ -> "") ^
126 "style=dashed];\n"));
127 output_string ch "}\n";
129 (*ignore (Unix.system "tred xxx.dot > yyy.dot && dot -Tps yyy.dot > xxx.ps")*)
130 ignore (Unix.system "cp xxx.ps xxx_old.ps && dot -Tps xxx.dot > xxx.ps");
131 (*ignore (read_line ())*)
134 let test to_be_considered_and_now ((s,_,_) as set) rel candidate repr =
135 ps_of_set to_be_considered_and_now ~processing:(candidate,rel,repr) set;
137 (string_of_cop candidate ^ " " ^ string_of_rel rel ^ " " ^ string_of_cop repr ^ "? ");
139 assert (Unix.system "cat log.ma | sed s/^theorem/axiom/g | sed 's/\\. intros.*qed\\././g' > xxx.ma" = Unix.WEXITED 0);
140 let ch = open_out_gen [Open_append] 0 "xxx.ma" in
144 (function (repr,others,leq,_) ->
149 ("axiom ax" ^ string_of_int !i ^
151 matita_of_cop "A" repr ^ " = " ^ matita_of_cop "A" repr' ^ ".\n");
154 (function (repr',_,_,_) ->
157 ("axiom ax" ^ string_of_int !i ^
159 matita_of_cop "A" repr ^ " ⊆ " ^ matita_of_cop "A" repr' ^ ".\n");
163 let candidate',rel',repr' =
165 SupersetEqual -> repr,SubsetEqual,candidate
167 | SubsetEqual -> candidate,rel,repr in
169 let name = name_of_theorem candidate' rel' repr' in
170 ("theorem " ^ name ^ ": \\forall A." ^ matita_of_cop "A" candidate' ^
171 " " ^ string_of_rel rel' ^ " " ^
172 matita_of_cop "A" repr' ^ ". intros; autobatch size=8 depth=3 width=2. qed.") in
173 output_string ch (query ^ "\n");
176 (*Unix.system "../../../matitac.opt xxx.ma >> log 2>&1" = Unix.WEXITED 0*)
177 Unix.system "../../../matitac.opt xxx.ma > /dev/null 2>&1" = Unix.WEXITED 0
179 let ch = open_out_gen [Open_append] 0o0600 "log.ma" in
181 output_string ch (query ^ "\n")
183 output_string ch ("(* " ^ query ^ "*)\n");
185 print_endline (if res then "y" else "n");
188 let remove node = List.filter (fun node' -> node <=> node');;
190 let add_leq_arc ((_,_,leq,_) as node) ((_,_,_,geq') as node') =
191 leq := node' :: !leq;
192 geq' := node :: !geq'
195 let add_geq_arc ((_,_,_,geq) as node) ((_,_,leq',_) as node') =
196 geq := node' :: !geq;
197 leq' := node :: !leq'
200 let remove_leq_arc ((_,_,leq,_) as node) ((_,_,_,geq') as node') =
201 leq := remove node' !leq;
202 geq' := remove node !geq'
205 let remove_geq_arc ((_,_,_,geq) as node) ((_,_,leq',_) as node') =
206 geq := remove node' !geq;
207 leq' := remove node !leq'
210 let leq_transitive_closure node node' =
211 add_leq_arc node node';
212 let rec remove_transitive_arcs ((_,_,_,geq) as node) (_,_,leq',_) =
213 let rec remove_arcs_to_ascendents =
216 | (_,_,leq,_) as node'::tl ->
217 remove_leq_arc node node';
218 remove_arcs_to_ascendents (!leq@tl)
220 remove_arcs_to_ascendents !leq';
221 List.iter (function son -> remove_transitive_arcs son node) !geq
223 remove_transitive_arcs node node'
226 let geq_transitive_closure node node' =
227 add_geq_arc node node';
228 let rec remove_transitive_arcs ((_,_,leq,_) as node) (_,_,_,geq') =
229 let rec remove_arcs_to_descendents =
232 | (_,_,_,geq) as node'::tl ->
233 remove_geq_arc node node';
234 remove_arcs_to_descendents (!geq@tl)
236 remove_arcs_to_descendents !geq';
237 List.iter (function father -> remove_transitive_arcs father node) !leq
239 remove_transitive_arcs node node'
242 let (@@) l1 n = if List.exists (function n' -> n===n') l1 then l1 else l1@[n]
244 let rec leq_reachable node =
247 | node'::_ when node === node' -> true
248 | (_,_,leq,_)::tl -> leq_reachable node (!leq@tl)
251 let rec geq_reachable node =
254 | node'::_ when node === node' -> true
255 | (_,_,_,geq)::tl -> geq_reachable node (!geq@tl)
258 let locate_using_leq to_be_considered_and_now ((repr,_,leq,geq) as node)
261 let rec aux ((nodes,inf,sup) as set) already_visited =
264 | (repr',_,_,geq') as node' :: tl ->
265 if List.exists (function n -> n===node') already_visited then
266 aux set already_visited tl
267 else if repr=repr' then aux set (node'::already_visited) (!geq'@tl)
268 else if leq_reachable node' !leq then
269 aux set (node'::already_visited) tl
270 else if test to_be_considered_and_now set SubsetEqual repr repr' then
272 let sup = remove node sup in
275 let inf = remove node' inf in
283 leq_transitive_closure node node';
284 aux (nodes,inf,sup) (node'::already_visited) (!geq'@tl)
287 aux set (node'::already_visited) tl
292 exception SameEquivalenceClass of set * equivalence_class * equivalence_class;;
294 let locate_using_geq to_be_considered_and_now ((repr,_,leq,geq) as node)
297 let rec aux ((nodes,inf,sup) as set) already_visited =
300 | (repr',_,leq',_) as node' :: tl ->
301 if List.exists (function n -> n===node') already_visited then
302 aux set already_visited tl
303 else if repr=repr' then aux set (node'::already_visited) (!leq'@tl)
304 else if geq_reachable node' !geq then
305 aux set (node'::already_visited) tl
306 else if test to_be_considered_and_now set SupersetEqual repr repr' then
308 if List.exists (function n -> n===node') !leq then
309 (* We have found two equal nodes! *)
310 raise (SameEquivalenceClass (set,node,node'))
313 let inf = remove node inf in
316 let sup = remove node' sup in
324 geq_transitive_closure node node';
325 aux (nodes,inf,sup) (node'::already_visited) (!leq'@tl)
329 aux set (node'::already_visited) tl
334 let analyze_one to_be_considered repr hecandidate (news,((nodes,inf,sup) as set)) =
335 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);
336 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);
337 let candidate = hecandidate::repr in
338 if List.length (List.filter ((=) M) candidate) > 1 then
344 let node = candidate,[],leq,geq in
345 let nodes = nodes@[node] in
346 let set = nodes,inf@[node],sup@[node] in
347 let start_inf,start_sup =
349 match List.filter (fun (repr',_,_,_) -> repr=repr') nodes with
354 match hecandidate with
356 | C -> [repr_node],sup
361 locate_using_leq (to_be_considered,Some repr,news) node set start_sup in
363 let _,inf,sup = set in
364 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);
365 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);
368 locate_using_geq (to_be_considered,Some repr,news) node set start_inf
371 let _,inf,sup = set in
372 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);
373 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);
377 SameEquivalenceClass ((nodes,inf,sup) as set,((r,_,leq_d,geq_d) as node_to_be_deleted),node')->
379 let _,inf,sup = set in
380 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);
381 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);
383 let rec clean inf sup res =
386 | node::tl when node===node_to_be_deleted ->
388 | (repr',others,leq,geq) as node::tl ->
393 | (_,_,leq,_) as node::tl ->
394 if node_to_be_deleted <=> node then
397 (List.filter (fun n ->not (leq_reachable n (res@tl))) !leq)@tl
400 let sup = if !leq = [] then sup@@node else sup in
405 | (_,_,_,geq) as node::tl ->
406 if node_to_be_deleted <=> node then
409 (List.filter (fun n ->not (geq_reachable n (res@tl))) !geq)@tl
412 let inf = if !geq = [] then inf@@node else inf in
414 clean inf sup ((repr',others@[candidate],leq,geq)::res) tl
416 clean inf sup (node::res) tl
418 let inf,sup,nodes = clean inf sup [] nodes in
419 let inf = remove node_to_be_deleted inf in
420 let sup = remove node_to_be_deleted sup in
421 let set = nodes,inf,sup in
423 let _,inf,sup = set in
424 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);
425 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);
430 let rec explore i (set:set) news =
431 let rec aux news set =
436 List.fold_right (analyze_one tl repr) [I;C;M] (news,set)
440 let news,set = aux [] set news in
443 print_endline ("PUNTO FISSO RAGGIUNTO! i=" ^ string_of_int i);
444 print_endline (string_of_set set ^ "\n----------------");
445 ps_of_set ([],None,[]) set
449 print_endline ("NUOVA ITERAZIONE, i=" ^ string_of_int i);
450 print_endline (string_of_set set ^ "\n----------------");
451 explore (i+1) set news
455 let id_node = id,[],ref [], ref [] in
456 let set = [id_node],[id_node],[id_node] in
457 print_endline ("PRIMA ITERAZIONE, i=0, j=0");
458 print_endline (string_of_set set ^ "\n----------------");
459 (*ignore (Unix.system "rm -f log");*)
460 assert (Unix.system "cp formal_topology.ma log.ma" = Unix.WEXITED 0);
461 ps_of_set ([id],None,[]) set;