]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/contribs/formal_topology/bin/theory_explorer.ml
log.ma is now created. It records all the tests (both failure and successes).
[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 =
13  function
14     I -> "i"
15   | C -> "c"
16   | M -> "-"
17
18 (* compound operator *)
19 type compound_operator = op list
20
21 let string_of_cop op =
22  if op = [] then "id" else String.concat "" (List.map string_of_op op)
23
24 let dot_of_cop op = "\"" ^ string_of_cop op ^ "\""
25
26 let matita_of_cop v =
27  let matita_of_op = function I -> "i" | C -> "c" | M -> "m" in
28  let rec aux =
29   function
30    | [] -> v
31    | [op] -> matita_of_op op ^ " " ^ v
32    | op::tl -> matita_of_op op ^ " (" ^ aux tl ^ ")"
33  in
34   aux
35
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
41
42 let (===) (repr,_,_,_) (repr',_,_,_) = repr = repr';;
43 let (<=>) (repr,_,_,_) (repr',_,_,_) = repr <> repr';;
44
45 let string_of_equivalence_class (repr,others,leq,_) =
46  String.concat " = " (List.map string_of_cop (repr::others)) ^
47   (if !leq <> [] then
48     "\n" ^
49      String.concat "\n" 
50       (List.map
51         (function (repr',_,_,_) ->
52            string_of_cop repr ^ " ⊆ " ^ string_of_cop repr') !leq)
53    else
54     "")
55
56 let dot_of_equivalence_class (repr,others,leq,_) =
57  (if others <> [] then
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
62    dot_of_cop repr ^ ";"
63   else
64    "") ^
65    String.concat "\n" 
66     (List.map
67       (function (repr',_,_,_) ->
68          dot_of_cop repr' ^ " -> " ^ dot_of_cop repr ^ ";") !leq)
69
70 (* set of equivalence classes, infima, suprema *)
71 type set =
72  equivalence_class list * equivalence_class list * equivalence_class list
73
74 let string_of_set (s,_,_) =
75  String.concat "\n" (List.map string_of_equivalence_class s)
76
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
81       None -> ()
82     | Some repr ->
83        output_string ch (dot_of_cop repr ^ " [color=yellow];"));
84   List.iter
85    (function (repr,_,_,_) ->
86      if List.exists (function (repr',_,_,_) -> repr=repr') sup then
87       output_string ch (dot_of_cop repr ^ " [shape=Mdiamond];")
88      else
89       output_string ch (dot_of_cop repr ^ " [shape=diamond];")
90    ) inf ;
91   List.iter
92    (function (repr,_,_,_) ->
93      if not (List.exists (function (repr',_,_,_) -> repr=repr') inf) then
94       output_string ch (dot_of_cop repr ^ " [shape=polygon];")
95    ) sup ;
96   List.iter
97    (function repr -> output_string ch (dot_of_cop repr ^ " [color=green];")
98    ) to_be_considered ;
99   List.iter
100    (function repr -> output_string ch (dot_of_cop repr ^ " [color=navy];")
101    ) news ;
102   output_string ch (String.concat "\n" (List.map dot_of_equivalence_class s));
103   output_string ch "\n";
104   (match processing with
105       None -> ()
106     | Some (repr,rel,repr') ->
107        output_string ch (dot_of_cop repr ^ " [color=red];");
108        let repr,repr' =
109         match rel with
110            SupersetEqual -> repr',repr
111          | Equal
112          | SubsetEqual -> repr,repr'
113        in
114         output_string ch
115          (dot_of_cop repr' ^ " -> " ^ dot_of_cop repr ^
116           " [" ^
117           (match rel with Equal -> "arrowhead=none " | _ -> "") ^
118           "style=dashed];\n"));
119   output_string ch "}\n";
120   close_out ch;
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 ())*)
124 ;;
125
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;
128  print_string
129   (string_of_cop candidate ^ " " ^ string_of_rel rel ^ " " ^ string_of_cop repr ^ "? ");
130  flush stdout;
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
133  let i = ref 0 in
134   List.iter
135    (function (repr,others,leq,_) ->
136      List.iter
137       (function repr' ->
138         incr i;
139         output_string ch
140          ("axiom ax" ^ string_of_int !i ^
141           ": \\forall A." ^
142            matita_of_cop "A" repr ^ " = " ^ matita_of_cop "A" repr' ^ ".\n");
143       ) others;
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       ) !leq;
152    ) s;
153   let candidate',rel',repr' =
154    match rel with
155       SupersetEqual -> repr,SubsetEqual,candidate
156     | Equal
157     | SubsetEqual -> candidate,rel,repr in
158   let query =
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");
163   close_out ch;
164   let res =
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
167   in
168    let ch = open_out_gen [Open_append] 0o0600 "log.ma" in
169    if res then
170     output_string ch (query ^ "\n")
171    else
172     output_string ch ("(* " ^ query ^ "*)\n");
173    close_out ch;
174    print_endline (if res then "y" else "n");
175    res
176
177 let remove node = List.filter (fun node' -> node <=> node');;
178
179 let add_leq_arc ((_,_,leq,_) as node) ((_,_,_,geq') as node') =
180  leq := node' :: !leq;
181  geq' := node :: !geq'
182 ;;
183
184 let add_geq_arc ((_,_,_,geq) as node) ((_,_,leq',_) as node') =
185  geq := node' :: !geq;
186  leq' := node :: !leq'
187 ;;
188
189 let remove_leq_arc ((_,_,leq,_) as node) ((_,_,_,geq') as node') =
190  leq := remove node' !leq;
191  geq' := remove node !geq'
192 ;;
193
194 let remove_geq_arc ((_,_,_,geq) as node) ((_,_,leq',_) as node') =
195  geq := remove node' !geq;
196  leq' := remove node !leq'
197 ;;
198
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 =
203    function
204       [] -> ()
205     | (_,_,leq,_) as node'::tl ->
206        remove_leq_arc node node';
207        remove_arcs_to_ascendents (!leq@tl)
208   in
209    remove_arcs_to_ascendents !leq';
210    List.iter (function son -> remove_transitive_arcs son node) !geq
211  in
212   remove_transitive_arcs node node'
213 ;;
214
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 =
219    function
220       [] -> ()
221     | (_,_,_,geq) as node'::tl ->
222        remove_geq_arc node node';
223        remove_arcs_to_descendents (!geq@tl)
224   in
225    remove_arcs_to_descendents !geq';
226    List.iter (function father -> remove_transitive_arcs father node) !leq
227  in
228   remove_transitive_arcs node node'
229 ;;
230
231 let (@@) l1 n = if List.exists (function n' -> n===n') l1 then l1 else l1@[n]
232
233 let rec leq_reachable node =
234  function
235     [] -> false
236   | node'::_ when node === node' -> true
237   | (_,_,leq,_)::tl -> leq_reachable node (!leq@tl)
238 ;;
239
240 let rec geq_reachable node =
241  function
242     [] -> false
243   | node'::_ when node === node' -> true
244   | (_,_,_,geq)::tl -> geq_reachable node (!geq@tl)
245 ;;
246
247 let locate_using_leq to_be_considered_and_now ((repr,_,leq,geq) as node)
248  set start
249 =
250  let rec aux ((nodes,inf,sup) as set) =
251   function
252      [] -> set
253    | (repr',_,_,geq') as node' :: tl ->
254        if repr=repr' then aux set (!geq'@tl)
255        else if leq_reachable node' !leq then
256         aux set tl
257        else if test to_be_considered_and_now set SubsetEqual repr repr' then
258         begin
259          let sup = remove node sup in
260          let inf =
261           if !geq' = [] then
262            let inf = remove node' inf in
263             if !geq = [] then
264              inf@@node
265             else
266              inf
267           else
268            inf
269           in
270            leq_transitive_closure node node';
271            aux (nodes,inf,sup) (!geq'@tl)
272         end
273        else
274         aux set tl
275  in
276   aux set start
277 ;;
278
279 exception SameEquivalenceClass of set * equivalence_class * equivalence_class;;
280
281 let locate_using_geq to_be_considered_and_now ((repr,_,leq,geq) as node)
282  set start
283 =
284  let rec aux ((nodes,inf,sup) as set) =
285   function
286      [] -> set
287    | (repr',_,leq',_) as node' :: tl ->
288        if repr=repr' then aux set (!leq'@tl)
289        else if geq_reachable node' !geq then
290         aux set tl
291        else if test to_be_considered_and_now set SupersetEqual repr repr' then
292         begin
293          if List.exists (function n -> n===node') !leq then
294           (* We have found two equal nodes! *)
295           raise (SameEquivalenceClass (set,node,node'))
296          else
297           begin
298            let inf = remove node inf in
299            let sup =
300             if !leq' = [] then
301              let sup = remove node' sup in
302              if !leq = [] then
303               sup@@node
304              else
305               sup
306             else
307              sup
308            in
309             geq_transitive_closure node node';
310             aux (nodes,inf,sup) (!leq'@tl)
311           end
312         end
313        else
314         aux set tl
315  in
316   aux set start
317 ;;
318
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
324    news,set
325   else
326    try
327     let leq = ref [] in
328     let geq = ref [] in
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 =
333      let repr_node =
334       match List.filter (fun (repr',_,_,_) -> repr=repr') nodes with
335          [node] -> node
336        | _ -> assert false
337      in
338 inf,sup(*
339      match hecandidate with
340         I -> inf,[repr_node]
341       | C -> [repr_node],sup
342       | M -> inf,sup
343 *)
344     in
345     let set =
346      locate_using_leq (to_be_considered,Some repr,news) node set start_sup in
347 (
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);
351 );
352     let set =
353      locate_using_geq (to_be_considered,Some repr,news) node set start_inf
354     in
355 (
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);
359 );
360      news@[candidate],set
361    with
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);
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      let rec clean inf sup res =
370       function
371          [] -> inf,sup,res
372        | node::tl when node===node_to_be_deleted ->
373           clean inf sup res tl
374        | (repr',others,leq,geq) as node::tl ->
375           leq :=
376            List.fold_right
377             (fun node l ->
378               if node_to_be_deleted <=> node then
379                node::l
380               else
381                !leq_d@l
382             ) !leq [];
383           let sup = if !leq = [] then sup@@node else sup in
384           geq :=
385            List.fold_right
386             (fun node l ->
387               if node_to_be_deleted <=> node then
388                node::l
389               else
390                !geq_d@l
391             ) !geq [];
392           let inf = if !geq = [] then inf@@node else inf in
393           if node===node' then
394            clean inf sup ((repr',others@[candidate],leq,geq)::res) tl
395           else
396            clean inf sup (node::res) tl
397      in
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
402 (
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);
406 );
407       news,(nodes,inf,sup)
408 ;;
409
410 let rec explore i (set:set) news =
411  let rec aux news set =
412   function
413      [] -> news,set
414    | repr::tl ->
415       let news,set =
416        List.fold_right (analyze_one tl repr) [I;C;M] (news,set)
417       in
418        aux news set tl
419  in
420   let news,set = aux [] set news in
421    if news = [] then
422     begin
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
426     end
427    else
428     begin
429      print_endline ("NUOVA ITERAZIONE, i=" ^ string_of_int i);
430      print_endline (string_of_set set ^ "\n----------------");
431      explore (i+1) set news
432     end
433 in
434  let id = [] in
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;
442   explore 1 set [id]
443 ;;