]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/contribs/formal_topology/bin/theory_explorer.ml
xxx.dot improved
[helm.git] / helm / software / matita / contribs / formal_topology / bin / theory_explorer.ml
1 (* operator *)
2 type op = I | C | M
3
4 let string_of_op =
5  function
6     I -> "i"
7   | C -> "c"
8   | M -> "-"
9
10 (* compound operator *)
11 type compound_operator = op list
12
13 let string_of_cop op =
14  if op = [] then "id" else String.concat "" (List.map string_of_op op)
15
16 let dot_of_cop op = "\"" ^ string_of_cop op ^ "\""
17
18 let rec matita_of_cop v =
19  function
20   | [] -> v
21   | I::tl -> "i (" ^ matita_of_cop v tl ^ ")"
22   | C::tl -> "c (" ^ matita_of_cop v tl ^ ")"
23   | M::tl -> "m (" ^ matita_of_cop v tl ^ ")"
24
25 (* representative, other elements in the equivalence class,
26    leq classes, geq classes *)
27 type equivalence_class =
28  compound_operator * compound_operator list *
29   equivalence_class list ref * equivalence_class list ref
30
31 let string_of_equivalence_class (repr,others,leq,_) =
32  String.concat " = " (List.map string_of_cop (repr::others)) ^
33   (if !leq <> [] then
34     "\n" ^
35      String.concat "\n" 
36       (List.map
37         (function (repr',_,_,_) ->
38            string_of_cop repr ^ " <= " ^ string_of_cop repr') !leq)
39    else
40     "")
41
42 let dot_of_equivalence_class (repr,others,leq,_) =
43  (if others <> [] then
44    let eq = String.concat " = " (List.map string_of_cop (repr::others)) in
45     dot_of_cop repr ^ "[label=\"" ^ eq ^ "\"];" ^
46      if !leq = [] then "" else "\n"
47   else "") ^
48    String.concat "\n" 
49     (List.map
50       (function (repr',_,_,_) ->
51          dot_of_cop repr' ^ " -> " ^ dot_of_cop repr ^ ";") !leq)
52
53 (* set of equivalence classes *)
54 type set = equivalence_class list
55
56 let string_of_set s =
57  String.concat "\n" (List.map string_of_equivalence_class s)
58
59 let ps_of_set ?processing s =
60  let ch = open_out "xxx.dot" in
61   output_string ch "digraph G {\n";
62   output_string ch (String.concat "\n" (List.map dot_of_equivalence_class s));
63   output_string ch "\n";
64   (match processing with
65       None -> ()
66     | Some (repr,rel,repr') ->
67        output_string ch
68         (dot_of_cop repr' ^ " -> " ^ dot_of_cop repr ^
69          " [" ^
70          (if rel="=" then "arrowhead=none " else "") ^
71          "style=dashed];\n"));
72   output_string ch "}\n";
73   close_out ch;
74   ignore (Unix.system "dot -Tps xxx.dot > xxx.ps")
75
76 let test set rel candidate repr =
77  ps_of_set ~processing:(candidate,rel,repr) set;
78  print_string
79   (string_of_cop candidate ^ " " ^ rel ^ " " ^ string_of_cop repr ^ "? ");
80  flush stdout;
81  assert (Unix.system "cp formal_topology.ma xxx.ma" = Unix.WEXITED 0);
82  let ch = open_out_gen [Open_append] 0 "xxx.ma" in
83  let i = ref 0 in
84   List.iter
85    (function (repr,others,leq,_) ->
86      List.iter
87       (function repr' ->
88         incr i;
89         output_string ch
90          ("axiom ax" ^ string_of_int !i ^
91           ": \\forall A." ^
92            matita_of_cop "A" repr ^ " = " ^ matita_of_cop "A" repr' ^ ".\n");
93       ) others;
94      List.iter
95       (function (repr',_,_,_) ->
96         incr i;
97         output_string ch
98          ("axiom ax" ^ string_of_int !i ^
99           ": \\forall A." ^
100            matita_of_cop "A" repr ^ " ⊆ " ^ matita_of_cop "A" repr' ^ ".\n");
101       ) !leq;
102    ) set;
103   output_string ch
104    ("theorem foo: \\forall A." ^ matita_of_cop "A" candidate ^ " " ^ rel ^ " " ^
105      matita_of_cop "A" repr ^ ". intros; auto size=6 depth=4. qed.\n");
106   close_out ch;
107   let res =
108    Unix.system "../../../matitac.opt xxx.ma >> log 2>&1" = Unix.WEXITED 0
109   in
110    print_endline (if res then "y" else "n");
111    res
112
113 let normalize candidate set =
114  let rec aux =
115   function
116      [] -> raise Not_found
117    | (repr,others,leq,geq) as eqclass :: tl ->
118        if test set "=" candidate repr then
119         (repr,others@[candidate],leq,geq)::tl
120        else
121         eqclass::(aux tl) 
122  in
123   aux set
124 ;;
125
126 let locate ((repr,_,leq,geq) as node) set =
127  let rec aux =
128   function
129      [] -> ()
130    | (repr',_,leq',geq') as node' :: tl ->
131        if repr = repr' then ()
132        else if test set "⊆" repr repr' then
133         begin
134          leq  := node' :: !leq;
135          geq' := node  :: !geq'
136         end
137        else if test set "⊆" repr' repr then
138         begin
139          geq  := node' :: !geq;
140          leq' := node  :: !leq'
141         end ;
142        aux tl
143  in
144   aux set
145 ;;
146
147 let analyze_one i repr hecandidate (news,set) =
148  let candidate = hecandidate::repr in
149   if List.length (List.filter ((=) M) candidate) > i then
150    news,set
151   else
152    try
153     let set = normalize candidate set in
154      news,set
155    with
156     Not_found ->
157      let leq = ref [] in
158      let geq = ref [] in
159      let node = candidate,[],leq,geq in
160      let set = node::set in
161       locate node set;
162       candidate::news,set
163 ;;
164
165 let rec explore i j set news =
166  let rec aux news set =
167   function
168      [] -> news,set
169    | repr::tl ->
170       let news,set =
171        List.fold_right (analyze_one i repr) [I;C;M] (news,set)
172       in
173        aux news set tl
174  in
175   let news,set = aux [] set news in
176    if news = [] then
177     begin
178      print_endline ("PUNTO FISSO RAGGIUNTO! i=" ^ string_of_int i ^ " j=" ^ string_of_int j);
179      print_endline (string_of_set set ^ "\n----------------");
180      if i < 2 then
181       explore (i+1) 1 set (List.map (function (repr,_,_,_) -> repr) set)
182      else
183       ps_of_set set
184     end
185    else
186     begin
187      print_endline ("NUOVA ITERAZIONE, i=" ^ string_of_int i ^ " j=" ^ string_of_int j);
188      print_endline (string_of_set set ^ "\n----------------");
189      explore i (j+1) set news
190     end
191 in
192  let id = [] in
193  let set = [id,[],ref [], ref []] in
194   print_endline ("PRIMA ITERAZIONE, i=0, j=0");
195   print_endline (string_of_set set ^ "\n----------------");
196   ignore (Unix.system "rm -f log");
197   ps_of_set set;
198   explore 0 1 set [id]
199 ;;