1 type rel = Equal | SubsetEqual | SupersetEqual
18 (* compound operator *)
19 type compound_operator = op list
21 let string_of_cop op =
22 if op = [] then "id" else String.concat "" (List.map string_of_op op)
24 let dot_of_cop op = "\"" ^ string_of_cop op ^ "\""
26 let rec matita_of_cop v =
29 | I::tl -> "i (" ^ matita_of_cop v tl ^ ")"
30 | C::tl -> "c (" ^ matita_of_cop v tl ^ ")"
31 | M::tl -> "m (" ^ matita_of_cop v tl ^ ")"
33 (* representative, other elements in the equivalence class,
34 leq classes, geq classes *)
35 type equivalence_class =
36 compound_operator * compound_operator list *
37 equivalence_class list ref * equivalence_class list ref
39 let (===) (repr,_,_,_) (repr',_,_,_) = repr = repr';;
40 let (<=>) (repr,_,_,_) (repr',_,_,_) = repr <> repr';;
42 let string_of_equivalence_class (repr,others,leq,_) =
43 String.concat " = " (List.map string_of_cop (repr::others)) ^
48 (function (repr',_,_,_) ->
49 string_of_cop repr ^ " ⊆ " ^ string_of_cop repr') !leq)
53 let dot_of_equivalence_class (repr,others,leq,_) =
55 let eq = String.concat " = " (List.map string_of_cop (repr::others)) in
56 dot_of_cop repr ^ "[label=\"" ^ eq ^ "\"];" ^
57 if !leq = [] then "" else "\n"
58 else if !leq = [] then
64 (function (repr',_,_,_) ->
65 dot_of_cop repr' ^ " -> " ^ dot_of_cop repr ^ ";") !leq)
67 (* set of equivalence classes, infima, suprema *)
69 equivalence_class list * equivalence_class list * equivalence_class list
71 let string_of_set (s,_,_) =
72 String.concat "\n" (List.map string_of_equivalence_class s)
74 let ps_of_set (to_be_considered,under_consideration,news) ?processing (s,inf,sup) =
75 let ch = open_out "xxx.dot" in
76 output_string ch "digraph G {\n";
77 (match under_consideration with
80 output_string ch (dot_of_cop repr ^ " [color=yellow];"));
82 (function (repr,_,_,_) ->
83 if List.exists (function (repr',_,_,_) -> repr=repr') sup then
84 output_string ch (dot_of_cop repr ^ " [shape=Mdiamond];")
86 output_string ch (dot_of_cop repr ^ " [shape=diamond];")
89 (function (repr,_,_,_) ->
90 if not (List.exists (function (repr',_,_,_) -> repr=repr') inf) then
91 output_string ch (dot_of_cop repr ^ " [shape=polygon];")
94 (function repr -> output_string ch (dot_of_cop repr ^ " [color=green];")
97 (function repr -> output_string ch (dot_of_cop repr ^ " [color=navy];")
99 output_string ch (String.concat "\n" (List.map dot_of_equivalence_class s));
100 output_string ch "\n";
101 (match processing with
103 | Some (repr,rel,repr') ->
104 output_string ch (dot_of_cop repr ^ " [color=red];");
107 SupersetEqual -> repr',repr
109 | SubsetEqual -> repr,repr'
112 (dot_of_cop repr' ^ " -> " ^ dot_of_cop repr ^
114 (match rel with Equal -> "arrowhead=none " | _ -> "") ^
115 "style=dashed];\n"));
116 output_string ch "}\n";
118 (*ignore (Unix.system "tred xxx.dot > yyy.dot && dot -Tps yyy.dot > xxx.ps")*)
119 ignore (Unix.system "cp xxx.ps xxx_old.ps && dot -Tps xxx.dot > xxx.ps");
120 (*ignore (read_line ())*)
123 let test to_be_considered_and_now ((s,_,_) as set) rel candidate repr =
124 ps_of_set to_be_considered_and_now ~processing:(candidate,rel,repr) set;
126 (string_of_cop candidate ^ " " ^ string_of_rel rel ^ " " ^ string_of_cop repr ^ "? ");
128 assert (Unix.system "cp formal_topology.ma xxx.ma" = Unix.WEXITED 0);
129 let ch = open_out_gen [Open_append] 0 "xxx.ma" in
132 (function (repr,others,leq,_) ->
137 ("axiom ax" ^ string_of_int !i ^
139 matita_of_cop "A" repr ^ " = " ^ matita_of_cop "A" repr' ^ ".\n");
142 (function (repr',_,_,_) ->
145 ("axiom ax" ^ string_of_int !i ^
147 matita_of_cop "A" repr ^ " ⊆ " ^ matita_of_cop "A" repr' ^ ".\n");
150 let candidate',rel',repr' =
152 SupersetEqual -> repr,SubsetEqual,candidate
154 | SubsetEqual -> candidate,rel,repr
157 ("theorem foo: \\forall A." ^ matita_of_cop "A" candidate' ^
158 " " ^ string_of_rel rel' ^ " " ^
159 matita_of_cop "A" repr' ^ ". intros; auto size=6 depth=4. qed.\n");
162 (*Unix.system "../../../matitac.opt xxx.ma >> log 2>&1" = Unix.WEXITED 0*)
163 Unix.system "../../../matitac.opt xxx.ma > /dev/null 2>&1" = Unix.WEXITED 0
165 print_endline (if res then "y" else "n");
168 let remove node = List.filter (fun node' -> node <=> node');;
170 let add_leq_arc ((_,_,leq,_) as node) ((_,_,_,geq') as node') =
171 leq := node' :: !leq;
172 geq' := node :: !geq'
175 let add_geq_arc ((_,_,_,geq) as node) ((_,_,leq',_) as node') =
176 geq := node' :: !geq;
177 leq' := node :: !leq'
180 let remove_leq_arc ((_,_,leq,_) as node) ((_,_,_,geq') as node') =
181 leq := remove node' !leq;
182 geq' := remove node !geq'
185 let remove_geq_arc ((_,_,_,geq) as node) ((_,_,leq',_) as node') =
186 geq := remove node' !geq;
187 leq' := remove node !leq'
190 let leq_transitive_closure node node' =
191 add_leq_arc node node';
192 let rec remove_transitive_arcs ((_,_,_,geq) as node) (_,_,leq',_) =
193 let rec remove_arcs_to_ascendents =
196 | (_,_,leq,_) as node'::tl ->
197 remove_leq_arc node node';
198 remove_arcs_to_ascendents (!leq@tl)
200 remove_arcs_to_ascendents !leq';
201 List.iter (function son -> remove_transitive_arcs son node) !geq
203 remove_transitive_arcs node node'
206 let geq_transitive_closure node node' =
207 add_geq_arc node node';
208 let rec remove_transitive_arcs ((_,_,leq,_) as node) (_,_,_,geq') =
209 let rec remove_arcs_to_descendents =
212 | (_,_,_,geq) as node'::tl ->
213 remove_geq_arc node node';
214 remove_arcs_to_descendents (!geq@tl)
216 remove_arcs_to_descendents !geq';
217 List.iter (function father -> remove_transitive_arcs father node) !leq
219 remove_transitive_arcs node node'
222 let (@@) l1 n = if List.exists (function n' -> n===n') l1 then l1 else l1@[n]
224 let rec leq_reachable node =
227 | node'::_ when node === node' -> true
228 | (_,_,leq,_)::tl -> leq_reachable node (!leq@tl)
231 let rec geq_reachable node =
234 | node'::_ when node === node' -> true
235 | (_,_,_,geq)::tl -> geq_reachable node (!geq@tl)
238 let locate_using_leq to_be_considered_and_now ((repr,_,leq,_) as node)
241 let rec aux is_sup ((nodes,inf,sup) as set) =
248 | (repr',_,_,geq') as node' :: tl ->
249 if repr=repr' then aux is_sup set (!geq'@tl)
250 else if leq_reachable node' !leq then
252 else if test to_be_considered_and_now set SubsetEqual repr repr' then
254 let inf = if !geq' = [] then (remove node' inf)@@node else inf in
255 leq_transitive_closure node node';
256 aux false (nodes,inf,sup) (!geq'@tl)
264 exception SameEquivalenceClass of equivalence_class * equivalence_class;;
266 let locate_using_geq to_be_considered_and_now ((repr,_,leq,geq) as node)
269 let rec aux is_inf ((nodes,inf,sup) as set) =
276 | (repr',_,leq',_) as node' :: tl ->
277 if repr=repr' then aux is_inf set (!leq'@tl)
278 else if geq_reachable node' !geq then
280 else if test to_be_considered_and_now set SupersetEqual repr repr' then
282 if List.exists (function n -> n===node') !leq then
283 (* We have found two equal nodes! *)
284 raise (SameEquivalenceClass (node,node'))
287 let sup = if !leq' = [] then (remove node' sup)@@node else sup in
288 geq_transitive_closure node node';
289 aux false (nodes,inf,sup) (!leq'@tl)
298 let analyze_one to_be_considered repr hecandidate (news,((nodes,inf,sup) as set)) =
299 assert (List.for_all (fun (_,_,leq,geq) -> !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 !leq) inf);
300 assert (List.for_all (fun (_,_,leq,geq) -> !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 !geq) sup);
301 let candidate = hecandidate::repr in
302 if List.length (List.filter ((=) M) candidate) > 1 then
308 let node = candidate,[],leq,geq in
309 let nodes = nodes@[node] in
310 let set = nodes,inf,sup in
311 let start_inf,start_sup =
313 match List.filter (fun (repr',_,_,_) -> repr=repr') nodes with
318 match hecandidate with
320 | C -> [repr_node],sup
325 locate_using_leq (to_be_considered,Some repr,news) node set start_sup in
327 locate_using_geq (to_be_considered,Some repr,news) node set start_inf
331 SameEquivalenceClass ((_,_,leq_d,geq_d) as node_to_be_deleted,node') ->
335 | (repr',others,leq,geq) as node::tl ->
339 if node_to_be_deleted <=> node then
347 if node_to_be_deleted <=> node then
353 (repr',others@[candidate],leq,geq)::clean tl
357 let nodes = clean nodes in
361 let rec explore i (set:set) news =
362 let rec aux news set =
367 List.fold_right (analyze_one tl repr) [I;C;M] (news,set)
371 let news,set = aux [] set news in
374 print_endline ("PUNTO FISSO RAGGIUNTO! i=" ^ string_of_int i);
375 print_endline (string_of_set set ^ "\n----------------");
376 ps_of_set ([],None,[]) set
380 print_endline ("NUOVA ITERAZIONE, i=" ^ string_of_int i);
381 print_endline (string_of_set set ^ "\n----------------");
382 explore (i+1) set news
386 let id_node = id,[],ref [], ref [] in
387 let set = [id_node],[id_node],[id_node] in
388 print_endline ("PRIMA ITERAZIONE, i=0, j=0");
389 print_endline (string_of_set set ^ "\n----------------");
390 (*ignore (Unix.system "rm -f log");*)
391 ps_of_set ([id],None,[]) set;