]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/contribs/formal_topology/bin/theory_explorer.ml
b5ccd463240cc502fef9a23757c1acc528891d1d
[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 "cp formal_topology.ma xxx.ma" = Unix.WEXITED 0);
140  let ch = open_out_gen [Open_append ; Open_creat] 0 "xxx.ma" in
141  let i = ref 0 in
142   List.iter
143    (function (repr,others,leq,_) ->
144      List.iter
145       (function repr' ->
146         incr i;
147         output_string ch
148          ("axiom ax" ^ string_of_int !i ^
149           ": \\forall A." ^
150            matita_of_cop "A" repr ^ " = " ^ matita_of_cop "A" repr' ^ ".\n");
151       ) others;
152      List.iter
153       (function (repr',_,_,_) ->
154         incr i;
155         output_string ch
156          ("axiom ax" ^ string_of_int !i ^
157           ": \\forall A." ^
158            matita_of_cop "A" repr ^ " ⊆ " ^ matita_of_cop "A" repr' ^ ".\n");
159       ) !leq;
160    ) s;
161   let candidate',rel',repr' =
162    match rel with
163       SupersetEqual -> repr,SubsetEqual,candidate
164     | Equal
165     | SubsetEqual -> candidate,rel,repr in
166   let query =
167    let name = name_of_theorem candidate' rel' repr' in
168    ("theorem " ^ name ^ ": \\forall A." ^ matita_of_cop "A" candidate' ^
169       " " ^ string_of_rel rel' ^ " " ^
170       matita_of_cop "A" repr' ^ ". intros; autobatch size=8 depth=4 width=2. qed.") in
171   output_string ch (query ^ "\n");
172   close_out ch;
173   let res =
174    (*Unix.system "../../../matitac.opt xxx.ma >> log 2>&1" = Unix.WEXITED 0*)
175    Unix.system "../../../matitac.opt xxx.ma > /dev/null 2>&1" = Unix.WEXITED 0
176   in
177    let ch = open_out_gen [Open_append] 0o0600 "log.ma" in
178    if res then
179     output_string ch (query ^ "\n")
180    else
181     output_string ch ("(* " ^ query ^ "*)\n");
182    close_out ch;
183    print_endline (if res then "y" else "n");
184    res
185
186 let remove node = List.filter (fun node' -> node <=> node');;
187
188 let add_leq_arc ((_,_,leq,_) as node) ((_,_,_,geq') as node') =
189  leq := node' :: !leq;
190  geq' := node :: !geq'
191 ;;
192
193 let add_geq_arc ((_,_,_,geq) as node) ((_,_,leq',_) as node') =
194  geq := node' :: !geq;
195  leq' := node :: !leq'
196 ;;
197
198 let remove_leq_arc ((_,_,leq,_) as node) ((_,_,_,geq') as node') =
199  leq := remove node' !leq;
200  geq' := remove node !geq'
201 ;;
202
203 let remove_geq_arc ((_,_,_,geq) as node) ((_,_,leq',_) as node') =
204  geq := remove node' !geq;
205  leq' := remove node !leq'
206 ;;
207
208 let leq_transitive_closure node node' =
209  add_leq_arc node node';
210  let rec remove_transitive_arcs ((_,_,_,geq) as node) (_,_,leq',_) =
211   let rec remove_arcs_to_ascendents =
212    function
213       [] -> ()
214     | (_,_,leq,_) as node'::tl ->
215        remove_leq_arc node node';
216        remove_arcs_to_ascendents (!leq@tl)
217   in
218    remove_arcs_to_ascendents !leq';
219    List.iter (function son -> remove_transitive_arcs son node) !geq
220  in
221   remove_transitive_arcs node node'
222 ;;
223
224 let geq_transitive_closure node node' =
225  add_geq_arc node node';
226  let rec remove_transitive_arcs ((_,_,leq,_) as node) (_,_,_,geq') =
227   let rec remove_arcs_to_descendents =
228    function
229       [] -> ()
230     | (_,_,_,geq) as node'::tl ->
231        remove_geq_arc node node';
232        remove_arcs_to_descendents (!geq@tl)
233   in
234    remove_arcs_to_descendents !geq';
235    List.iter (function father -> remove_transitive_arcs father node) !leq
236  in
237   remove_transitive_arcs node node'
238 ;;
239
240 let (@@) l1 n = if List.exists (function n' -> n===n') l1 then l1 else l1@[n]
241
242 let rec leq_reachable node =
243  function
244     [] -> false
245   | node'::_ when node === node' -> true
246   | (_,_,leq,_)::tl -> leq_reachable node (!leq@tl)
247 ;;
248
249 let rec geq_reachable node =
250  function
251     [] -> false
252   | node'::_ when node === node' -> true
253   | (_,_,_,geq)::tl -> geq_reachable node (!geq@tl)
254 ;;
255
256 let locate_using_leq to_be_considered_and_now ((repr,_,leq,geq) as node)
257  set start
258 =
259  let rec aux ((nodes,inf,sup) as set) =
260   function
261      [] -> set
262    | (repr',_,_,geq') as node' :: tl ->
263        if repr=repr' then aux set (!geq'@tl)
264        else if leq_reachable node' !leq then
265         aux set tl
266        else if test to_be_considered_and_now set SubsetEqual repr repr' then
267         begin
268          let sup = remove node sup in
269          let inf =
270           if !geq' = [] then
271            let inf = remove node' inf in
272             if !geq = [] then
273              inf@@node
274             else
275              inf
276           else
277            inf
278           in
279            leq_transitive_closure node node';
280            aux (nodes,inf,sup) (!geq'@tl)
281         end
282        else
283         aux set tl
284  in
285   aux set start
286 ;;
287
288 exception SameEquivalenceClass of set * equivalence_class * equivalence_class;;
289
290 let locate_using_geq to_be_considered_and_now ((repr,_,leq,geq) as node)
291  set start
292 =
293  let rec aux ((nodes,inf,sup) as set) =
294   function
295      [] -> set
296    | (repr',_,leq',_) as node' :: tl ->
297        if repr=repr' then aux set (!leq'@tl)
298        else if geq_reachable node' !geq then
299         aux set tl
300        else if test to_be_considered_and_now set SupersetEqual repr repr' then
301         begin
302          if List.exists (function n -> n===node') !leq then
303           (* We have found two equal nodes! *)
304           raise (SameEquivalenceClass (set,node,node'))
305          else
306           begin
307            let inf = remove node inf in
308            let sup =
309             if !leq' = [] then
310              let sup = remove node' sup in
311              if !leq = [] then
312               sup@@node
313              else
314               sup
315             else
316              sup
317            in
318             geq_transitive_closure node node';
319             aux (nodes,inf,sup) (!leq'@tl)
320           end
321         end
322        else
323         aux set tl
324  in
325   aux set start
326 ;;
327
328 let analyze_one to_be_considered repr hecandidate (news,((nodes,inf,sup) as set)) =
329 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);
330 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);
331  let candidate = hecandidate::repr in
332   if List.length (List.filter ((=) M) candidate) > 1 then
333    news,set
334   else
335    try
336     let leq = ref [] in
337     let geq = ref [] in
338     let node = candidate,[],leq,geq in
339     let nodes = nodes@[node] in
340     let set = nodes,inf@[node],sup@[node] in
341     let start_inf,start_sup =
342      let repr_node =
343       match List.filter (fun (repr',_,_,_) -> repr=repr') nodes with
344          [node] -> node
345        | _ -> assert false
346      in
347 inf,sup(*
348      match hecandidate with
349         I -> inf,[repr_node]
350       | C -> [repr_node],sup
351       | M -> inf,sup
352 *)
353     in
354     let set =
355      locate_using_leq (to_be_considered,Some repr,news) node set start_sup in
356 (
357 let _,inf,sup = set in
358 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);
359 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);
360 );
361     let set =
362      locate_using_geq (to_be_considered,Some repr,news) node set start_inf
363     in
364 (
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);
368 );
369      news@[candidate],set
370    with
371     SameEquivalenceClass ((nodes,inf,sup) as set,((r,_,leq_d,geq_d) as node_to_be_deleted),node')->
372 (
373 let _,inf,sup = set in
374 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);
375 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);
376 );
377      let rec clean inf sup res =
378       function
379          [] -> inf,sup,res
380        | node::tl when node===node_to_be_deleted ->
381           clean inf sup res tl
382        | (repr',others,leq,geq) as node::tl ->
383           leq :=
384            (let rec aux res =
385              function
386                 [] -> res
387               | (_,_,leq,_) as node::tl ->
388                  if node_to_be_deleted <=> node then
389                   aux (res@[node]) tl
390                  else
391                   (List.filter (fun n ->not (leq_reachable n (res@tl))) !leq)@tl
392             in
393              aux [] !leq);
394           let sup = if !leq = [] then sup@@node else sup in
395           geq :=
396            (let rec aux res =
397              function
398                 [] -> res
399               | (_,_,_,geq) as node::tl ->
400                  if node_to_be_deleted <=> node then
401                   aux (res@[node]) tl
402                  else
403                   (List.filter (fun n ->not (geq_reachable n (res@tl))) !geq)@tl
404             in
405              aux [] !geq);
406           let inf = if !geq = [] then inf@@node else inf in
407           if node===node' then
408            clean inf sup ((repr',others@[candidate],leq,geq)::res) tl
409           else
410            clean inf sup (node::res) tl
411      in
412      let inf,sup,nodes = clean inf sup [] nodes in
413      let inf = remove node_to_be_deleted inf in
414      let sup = remove node_to_be_deleted sup in
415 let set = nodes,inf,sup in
416 (
417 let _,inf,sup = set in
418 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);
419 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);
420 );
421       news,(nodes,inf,sup)
422 ;;
423
424 let rec explore i (set:set) news =
425  let rec aux news set =
426   function
427      [] -> news,set
428    | repr::tl ->
429       let news,set =
430        List.fold_right (analyze_one tl repr) [I;C;M] (news,set)
431       in
432        aux news set tl
433  in
434   let news,set = aux [] set news in
435    if news = [] then
436     begin
437      print_endline ("PUNTO FISSO RAGGIUNTO! i=" ^ string_of_int i);
438      print_endline (string_of_set set ^ "\n----------------");
439      ps_of_set ([],None,[]) set
440     end
441    else
442     begin
443      print_endline ("NUOVA ITERAZIONE, i=" ^ string_of_int i);
444      print_endline (string_of_set set ^ "\n----------------");
445      explore (i+1) set news
446     end
447 in
448  let id = [] in
449  let id_node = id,[],ref [], ref [] in
450  let set = [id_node],[id_node],[id_node] in
451   print_endline ("PRIMA ITERAZIONE, i=0, j=0");
452   print_endline (string_of_set set ^ "\n----------------");
453   (*ignore (Unix.system "rm -f log");*)
454   assert (Unix.system "cp formal_topology.ma log.ma" = Unix.WEXITED 0);
455   ps_of_set ([id],None,[]) set;
456   explore 1 set [id]
457 ;;