]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/contribs/formal_topology/bin/theory_explorer.ml
0e3e86684dfc4acb6f7f5d5ecd9f3aef416289d8
[helm.git] / helm / software / matita / contribs / formal_topology / bin / theory_explorer.ml
1 type rel = Equal | SubsetEqual | SupersetEqual
2
3 let string_of_rel =
4  function
5     Equal -> "="
6   | SubsetEqual -> "⊆"
7   | SupersetEqual -> "⊇"
8
9 (* operator *)
10 type op = I | C | M
11
12 let string_of_op = function I -> "i" | C -> "c" | M -> "-"
13 let matita_of_op = function I -> "i" | C -> "c" | M -> "m"
14
15 (* compound operator *)
16 type compound_operator = op list
17
18 let string_of_cop op =
19  if op = [] then "id" else String.concat "" (List.map string_of_op op)
20
21 let dot_of_cop op = "\"" ^ string_of_cop op ^ "\""
22
23 let matita_of_cop v =
24  let rec aux =
25   function
26    | [] -> v
27    | [op] -> matita_of_op op ^ " " ^ v
28    | op::tl -> matita_of_op op ^ " (" ^ aux tl ^ ")"
29  in
30   aux
31
32 let name_of_theorem cop rel cop' =
33  let cop,rel,cop' =
34   match rel with
35      Equal -> cop,"eq",cop'
36    | SubsetEqual -> cop,"leq",cop'
37    | SupersetEqual -> cop',"leq",cop
38  in
39   rel ^ "_" ^
40    String.concat "" (List.map matita_of_op cop) ^ "_" ^
41    String.concat "" (List.map matita_of_op cop')
42 ;;
43
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
49
50 let (===) (repr,_,_,_) (repr',_,_,_) = repr = repr';;
51 let (<=>) (repr,_,_,_) (repr',_,_,_) = repr <> repr';;
52
53 let string_of_equivalence_class (repr,others,leq,_) =
54  String.concat " = " (List.map string_of_cop (repr::others)) ^
55   (if !leq <> [] then
56     "\n" ^
57      String.concat "\n" 
58       (List.map
59         (function (repr',_,_,_) ->
60            string_of_cop repr ^ " ⊆ " ^ string_of_cop repr') !leq)
61    else
62     "")
63
64 let dot_of_equivalence_class (repr,others,leq,_) =
65  (if others <> [] then
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
70    dot_of_cop repr ^ ";"
71   else
72    "") ^
73    String.concat "\n" 
74     (List.map
75       (function (repr',_,_,_) ->
76          dot_of_cop repr' ^ " -> " ^ dot_of_cop repr ^ ";") !leq)
77
78 (* set of equivalence classes, infima, suprema *)
79 type set =
80  equivalence_class list * equivalence_class list * equivalence_class list
81
82 let string_of_set (s,_,_) =
83  String.concat "\n" (List.map string_of_equivalence_class s)
84
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
89       None -> ()
90     | Some repr ->
91        output_string ch (dot_of_cop repr ^ " [color=yellow];"));
92   List.iter
93    (function (repr,_,_,_) ->
94      if List.exists (function (repr',_,_,_) -> repr=repr') sup then
95       output_string ch (dot_of_cop repr ^ " [shape=Mdiamond];")
96      else
97       output_string ch (dot_of_cop repr ^ " [shape=diamond];")
98    ) inf ;
99   List.iter
100    (function (repr,_,_,_) ->
101      if not (List.exists (function (repr',_,_,_) -> repr=repr') inf) then
102       output_string ch (dot_of_cop repr ^ " [shape=polygon];")
103    ) sup ;
104   List.iter
105    (function repr -> output_string ch (dot_of_cop repr ^ " [color=green];")
106    ) to_be_considered ;
107   List.iter
108    (function repr -> output_string ch (dot_of_cop repr ^ " [color=navy];")
109    ) news ;
110   output_string ch (String.concat "\n" (List.map dot_of_equivalence_class s));
111   output_string ch "\n";
112   (match processing with
113       None -> ()
114     | Some (repr,rel,repr') ->
115        output_string ch (dot_of_cop repr ^ " [color=red];");
116        let repr,repr' =
117         match rel with
118            SupersetEqual -> repr',repr
119          | Equal
120          | SubsetEqual -> repr,repr'
121        in
122         output_string ch
123          (dot_of_cop repr' ^ " -> " ^ dot_of_cop repr ^
124           " [" ^
125           (match rel with Equal -> "arrowhead=none " | _ -> "") ^
126           "style=dashed];\n"));
127   output_string ch "}\n";
128   close_out ch;
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 ())*)
132 ;;
133
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;
136  print_string
137   (string_of_cop candidate ^ " " ^ string_of_rel rel ^ " " ^ string_of_cop repr ^ "? ");
138  flush stdout;
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
141 (*
142  let i = ref 0 in
143   List.iter
144    (function (repr,others,leq,_) ->
145      List.iter
146       (function repr' ->
147         incr i;
148         output_string ch
149          ("axiom ax" ^ string_of_int !i ^
150           ": \\forall A." ^
151            matita_of_cop "A" repr ^ " = " ^ matita_of_cop "A" repr' ^ ".\n");
152       ) others;
153      List.iter
154       (function (repr',_,_,_) ->
155         incr i;
156         output_string ch
157          ("axiom ax" ^ string_of_int !i ^
158           ": \\forall A." ^
159            matita_of_cop "A" repr ^ " ⊆ " ^ matita_of_cop "A" repr' ^ ".\n");
160       ) !leq;
161    ) s;
162 *)
163   let candidate',rel',repr' =
164    match rel with
165       SupersetEqual -> repr,SubsetEqual,candidate
166     | Equal
167     | SubsetEqual -> candidate,rel,repr in
168   let query =
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");
174   close_out ch;
175   let res =
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
178   in
179    let ch = open_out_gen [Open_append] 0o0600 "log.ma" in
180    if res then
181     output_string ch (query ^ "\n")
182    else
183     output_string ch ("(* " ^ query ^ "*)\n");
184    close_out ch;
185    print_endline (if res then "y" else "n");
186    res
187
188 let remove node = List.filter (fun node' -> node <=> node');;
189
190 let add_leq_arc ((_,_,leq,_) as node) ((_,_,_,geq') as node') =
191  leq := node' :: !leq;
192  geq' := node :: !geq'
193 ;;
194
195 let add_geq_arc ((_,_,_,geq) as node) ((_,_,leq',_) as node') =
196  geq := node' :: !geq;
197  leq' := node :: !leq'
198 ;;
199
200 let remove_leq_arc ((_,_,leq,_) as node) ((_,_,_,geq') as node') =
201  leq := remove node' !leq;
202  geq' := remove node !geq'
203 ;;
204
205 let remove_geq_arc ((_,_,_,geq) as node) ((_,_,leq',_) as node') =
206  geq := remove node' !geq;
207  leq' := remove node !leq'
208 ;;
209
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 =
214    function
215       [] -> ()
216     | (_,_,leq,_) as node'::tl ->
217        remove_leq_arc node node';
218        remove_arcs_to_ascendents (!leq@tl)
219   in
220    remove_arcs_to_ascendents !leq';
221    List.iter (function son -> remove_transitive_arcs son node) !geq
222  in
223   remove_transitive_arcs node node'
224 ;;
225
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 =
230    function
231       [] -> ()
232     | (_,_,_,geq) as node'::tl ->
233        remove_geq_arc node node';
234        remove_arcs_to_descendents (!geq@tl)
235   in
236    remove_arcs_to_descendents !geq';
237    List.iter (function father -> remove_transitive_arcs father node) !leq
238  in
239   remove_transitive_arcs node node'
240 ;;
241
242 let (@@) l1 n = if List.exists (function n' -> n===n') l1 then l1 else l1@[n]
243
244 let rec leq_reachable node =
245  function
246     [] -> false
247   | node'::_ when node === node' -> true
248   | (_,_,leq,_)::tl -> leq_reachable node (!leq@tl)
249 ;;
250
251 let rec geq_reachable node =
252  function
253     [] -> false
254   | node'::_ when node === node' -> true
255   | (_,_,_,geq)::tl -> geq_reachable node (!geq@tl)
256 ;;
257
258 let locate_using_leq to_be_considered_and_now ((repr,_,leq,geq) as node)
259  set start
260 =
261  let rec aux ((nodes,inf,sup) as set) already_visited =
262   function
263      [] -> set
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
271         begin
272          let sup = remove node sup in
273          let inf =
274           if !geq' = [] then
275            let inf = remove node' inf in
276             if !geq = [] then
277              inf@@node
278             else
279              inf
280           else
281            inf
282           in
283            leq_transitive_closure node node';
284            aux (nodes,inf,sup) (node'::already_visited) (!geq'@tl)
285         end
286        else
287         aux set (node'::already_visited) tl
288  in
289   aux set [] start
290 ;;
291
292 exception SameEquivalenceClass of set * equivalence_class * equivalence_class;;
293
294 let locate_using_geq to_be_considered_and_now ((repr,_,leq,geq) as node)
295  set start
296 =
297  let rec aux ((nodes,inf,sup) as set) already_visited =
298   function
299      [] -> set
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
307         begin
308          if List.exists (function n -> n===node') !leq then
309           (* We have found two equal nodes! *)
310           raise (SameEquivalenceClass (set,node,node'))
311          else
312           begin
313            let inf = remove node inf in
314            let sup =
315             if !leq' = [] then
316              let sup = remove node' sup in
317              if !leq = [] then
318               sup@@node
319              else
320               sup
321             else
322              sup
323            in
324             geq_transitive_closure node node';
325             aux (nodes,inf,sup) (node'::already_visited) (!leq'@tl)
326           end
327         end
328        else
329         aux set (node'::already_visited) tl
330  in
331   aux set [] start
332 ;;
333
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
339    news,set
340   else
341    try
342     let leq = ref [] in
343     let geq = ref [] in
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 =
348      let repr_node =
349       match List.filter (fun (repr',_,_,_) -> repr=repr') nodes with
350          [node] -> node
351        | _ -> assert false
352      in
353 inf,sup(*
354      match hecandidate with
355         I -> inf,[repr_node]
356       | C -> [repr_node],sup
357       | M -> inf,sup
358 *)
359     in
360     let set =
361      locate_using_leq (to_be_considered,Some repr,news) node set start_sup in
362 (
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);
366 );
367     let set =
368      locate_using_geq (to_be_considered,Some repr,news) node set start_inf
369     in
370 (
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);
374 );
375      news@[candidate],set
376    with
377     SameEquivalenceClass ((nodes,inf,sup) as set,((r,_,leq_d,geq_d) as node_to_be_deleted),node')->
378 (
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);
382 );
383      let rec clean inf sup res =
384       function
385          [] -> inf,sup,res
386        | node::tl when node===node_to_be_deleted ->
387           clean inf sup res tl
388        | (repr',others,leq,geq) as node::tl ->
389           leq :=
390            (let rec aux res =
391              function
392                 [] -> res
393               | (_,_,leq,_) as node::tl ->
394                  if node_to_be_deleted <=> node then
395                   aux (res@[node]) tl
396                  else
397                   (List.filter (fun n ->not (leq_reachable n (res@tl))) !leq)@tl
398             in
399              aux [] !leq);
400           let sup = if !leq = [] then sup@@node else sup in
401           geq :=
402            (let rec aux res =
403              function
404                 [] -> res
405               | (_,_,_,geq) as node::tl ->
406                  if node_to_be_deleted <=> node then
407                   aux (res@[node]) tl
408                  else
409                   (List.filter (fun n ->not (geq_reachable n (res@tl))) !geq)@tl
410             in
411              aux [] !geq);
412           let inf = if !geq = [] then inf@@node else inf in
413           if node===node' then
414            clean inf sup ((repr',others@[candidate],leq,geq)::res) tl
415           else
416            clean inf sup (node::res) tl
417      in
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
422 (
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);
426 );
427       news,(nodes,inf,sup)
428 ;;
429
430 let rec explore i (set:set) news =
431  let rec aux news set =
432   function
433      [] -> news,set
434    | repr::tl ->
435       let news,set =
436        List.fold_right (analyze_one tl repr) [I;C;M] (news,set)
437       in
438        aux news set tl
439  in
440   let news,set = aux [] set news in
441    if news = [] then
442     begin
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
446     end
447    else
448     begin
449      print_endline ("NUOVA ITERAZIONE, i=" ^ string_of_int i);
450      print_endline (string_of_set set ^ "\n----------------");
451      explore (i+1) set news
452     end
453 in
454  let id = [] in
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;
462   explore 1 set [id]
463 ;;