X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Fsoftware%2Fmatita%2Fcontribs%2Fformal_topology%2Fbin%2Ftheory_explorer.ml;h=dd52efb459010035db31d8789625e33aa119e8b3;hb=7822de9170b068737d90203880d955790cb12984;hp=d857ab93e1f1f22d77c46786aa1f258cf57ec611;hpb=855c09b06dd6c952063e61cf00df52685f400665;p=helm.git diff --git a/helm/software/matita/contribs/formal_topology/bin/theory_explorer.ml b/helm/software/matita/contribs/formal_topology/bin/theory_explorer.ml index d857ab93e..dd52efb45 100644 --- a/helm/software/matita/contribs/formal_topology/bin/theory_explorer.ml +++ b/helm/software/matita/contribs/formal_topology/bin/theory_explorer.ml @@ -36,6 +36,9 @@ type equivalence_class = compound_operator * compound_operator list * equivalence_class list ref * equivalence_class list ref +let (===) (repr,_,_,_) (repr',_,_,_) = repr = repr';; +let (<=>) (repr,_,_,_) (repr',_,_,_) = repr <> repr';; + let string_of_equivalence_class (repr,others,leq,_) = String.concat " = " (List.map string_of_cop (repr::others)) ^ (if !leq <> [] then @@ -43,7 +46,7 @@ let string_of_equivalence_class (repr,others,leq,_) = String.concat "\n" (List.map (function (repr',_,_,_) -> - string_of_cop repr ^ " <= " ^ string_of_cop repr') !leq) + string_of_cop repr ^ " ⊆ " ^ string_of_cop repr') !leq) else "") @@ -61,19 +64,32 @@ let dot_of_equivalence_class (repr,others,leq,_) = (function (repr',_,_,_) -> dot_of_cop repr' ^ " -> " ^ dot_of_cop repr ^ ";") !leq) -(* set of equivalence classes *) -type set = equivalence_class list +(* set of equivalence classes, infima, suprema *) +type set = + equivalence_class list * equivalence_class list * equivalence_class list -let string_of_set s = +let string_of_set (s,_,_) = String.concat "\n" (List.map string_of_equivalence_class s) -let ps_of_set (to_be_considered,under_consideration,news) ?processing s = +let ps_of_set (to_be_considered,under_consideration,news) ?processing (s,inf,sup) = let ch = open_out "xxx.dot" in output_string ch "digraph G {\n"; (match under_consideration with None -> () | Some repr -> output_string ch (dot_of_cop repr ^ " [color=yellow];")); + List.iter + (function (repr,_,_,_) -> + if List.exists (function (repr',_,_,_) -> repr=repr') sup then + output_string ch (dot_of_cop repr ^ " [shape=Mdiamond];") + else + output_string ch (dot_of_cop repr ^ " [shape=diamond];") + ) inf ; + List.iter + (function (repr,_,_,_) -> + if not (List.exists (function (repr',_,_,_) -> repr=repr') inf) then + output_string ch (dot_of_cop repr ^ " [shape=polygon];") + ) sup ; List.iter (function repr -> output_string ch (dot_of_cop repr ^ " [color=green];") ) to_be_considered ; @@ -99,9 +115,12 @@ let ps_of_set (to_be_considered,under_consideration,news) ?processing s = "style=dashed];\n")); output_string ch "}\n"; close_out ch; - ignore (Unix.system "dot -Tps xxx.dot > xxx.ps") + (*ignore (Unix.system "tred xxx.dot > yyy.dot && dot -Tps yyy.dot > xxx.ps")*) + ignore (Unix.system "cp xxx.ps xxx_old.ps && dot -Tps xxx.dot > xxx.ps"); + (*ignore (read_line ())*) +;; -let test to_be_considered_and_now set rel candidate repr = +let test to_be_considered_and_now ((s,_,_) as set) rel candidate repr = ps_of_set to_be_considered_and_now ~processing:(candidate,rel,repr) set; print_string (string_of_cop candidate ^ " " ^ string_of_rel rel ^ " " ^ string_of_cop repr ^ "? "); @@ -127,7 +146,7 @@ let test to_be_considered_and_now set rel candidate repr = ": \\forall A." ^ matita_of_cop "A" repr ^ " ⊆ " ^ matita_of_cop "A" repr' ^ ".\n"); ) !leq; - ) set; + ) s; let candidate',rel',repr' = match rel with SupersetEqual -> repr,SubsetEqual,candidate @@ -137,7 +156,7 @@ let test to_be_considered_and_now set rel candidate repr = output_string ch ("theorem foo: \\forall A." ^ matita_of_cop "A" candidate' ^ " " ^ string_of_rel rel' ^ " " ^ - matita_of_cop "A" repr' ^ ". intros; auto size=6 depth=4. qed.\n"); + matita_of_cop "A" repr' ^ ". intros; autobatch size=8 depth=4. qed.\n"); close_out ch; let res = (*Unix.system "../../../matitac.opt xxx.ma >> log 2>&1" = Unix.WEXITED 0*) @@ -146,59 +165,240 @@ let test to_be_considered_and_now set rel candidate repr = print_endline (if res then "y" else "n"); res -let normalize to_be_considered_and_now candidate set = - let rec aux = - function - [] -> raise Not_found - | (repr,others,leq,geq) as eqclass :: tl -> - if test to_be_considered_and_now set Equal candidate repr then - (repr,others@[candidate],leq,geq)::tl - else - eqclass::(aux tl) +let remove node = List.filter (fun node' -> node <=> node');; + +let add_leq_arc ((_,_,leq,_) as node) ((_,_,_,geq') as node') = + leq := node' :: !leq; + geq' := node :: !geq' +;; + +let add_geq_arc ((_,_,_,geq) as node) ((_,_,leq',_) as node') = + geq := node' :: !geq; + leq' := node :: !leq' +;; + +let remove_leq_arc ((_,_,leq,_) as node) ((_,_,_,geq') as node') = + leq := remove node' !leq; + geq' := remove node !geq' +;; + +let remove_geq_arc ((_,_,_,geq) as node) ((_,_,leq',_) as node') = + geq := remove node' !geq; + leq' := remove node !leq' +;; + +let leq_transitive_closure node node' = + add_leq_arc node node'; + let rec remove_transitive_arcs ((_,_,_,geq) as node) (_,_,leq',_) = + let rec remove_arcs_to_ascendents = + function + [] -> () + | (_,_,leq,_) as node'::tl -> + remove_leq_arc node node'; + remove_arcs_to_ascendents (!leq@tl) + in + remove_arcs_to_ascendents !leq'; + List.iter (function son -> remove_transitive_arcs son node) !geq in - aux set + remove_transitive_arcs node node' ;; -let locate to_be_considered_and_now ((repr,_,leq,geq) as node) set = - let rec aux = +let geq_transitive_closure node node' = + add_geq_arc node node'; + let rec remove_transitive_arcs ((_,_,leq,_) as node) (_,_,_,geq') = + let rec remove_arcs_to_descendents = + function + [] -> () + | (_,_,_,geq) as node'::tl -> + remove_geq_arc node node'; + remove_arcs_to_descendents (!geq@tl) + in + remove_arcs_to_descendents !geq'; + List.iter (function father -> remove_transitive_arcs father node) !leq + in + remove_transitive_arcs node node' +;; + +let (@@) l1 n = if List.exists (function n' -> n===n') l1 then l1 else l1@[n] + +let rec leq_reachable node = + function + [] -> false + | node'::_ when node === node' -> true + | (_,_,leq,_)::tl -> leq_reachable node (!leq@tl) +;; + +let rec geq_reachable node = + function + [] -> false + | node'::_ when node === node' -> true + | (_,_,_,geq)::tl -> geq_reachable node (!geq@tl) +;; + +let locate_using_leq to_be_considered_and_now ((repr,_,leq,geq) as node) + set start += + let rec aux ((nodes,inf,sup) as set) = function - [] -> () - | (repr',_,leq',geq') as node' :: tl -> - if repr = repr' then () + [] -> set + | (repr',_,_,geq') as node' :: tl -> + if repr=repr' then aux set (!geq'@tl) + else if leq_reachable node' !leq then + aux set tl else if test to_be_considered_and_now set SubsetEqual repr repr' then begin - leq := node' :: !leq; - geq' := node :: !geq' + let sup = remove node sup in + let inf = + if !geq' = [] then + let inf = remove node' inf in + if !geq = [] then + inf@@node + else + inf + else + inf + in + leq_transitive_closure node node'; + aux (nodes,inf,sup) (!geq'@tl) end + else + aux set tl + in + aux set start +;; + +exception SameEquivalenceClass of set * equivalence_class * equivalence_class;; + +let locate_using_geq to_be_considered_and_now ((repr,_,leq,geq) as node) + set start += + let rec aux ((nodes,inf,sup) as set) = + function + [] -> set + | (repr',_,leq',_) as node' :: tl -> + if repr=repr' then aux set (!leq'@tl) + else if geq_reachable node' !geq then + aux set tl else if test to_be_considered_and_now set SupersetEqual repr repr' then begin - geq := node' :: !geq; - leq' := node :: !leq' - end ; - aux tl + if List.exists (function n -> n===node') !leq then + (* We have found two equal nodes! *) + raise (SameEquivalenceClass (set,node,node')) + else + begin + let inf = remove node inf in + let sup = + if !leq' = [] then + let sup = remove node' sup in + if !leq = [] then + sup@@node + else + sup + else + sup + in + geq_transitive_closure node node'; + aux (nodes,inf,sup) (!leq'@tl) + end + end + else + aux set tl in - aux set + aux set start ;; -let analyze_one to_be_considered repr hecandidate (news,set) = +let analyze_one to_be_considered repr hecandidate (news,((nodes,inf,sup) as set)) = +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); +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); let candidate = hecandidate::repr in if List.length (List.filter ((=) M) candidate) > 1 then news,set else try - let set = normalize (to_be_considered,Some repr,news) candidate set in - news,set + let leq = ref [] in + let geq = ref [] in + let node = candidate,[],leq,geq in + let nodes = nodes@[node] in + let set = nodes,inf@[node],sup@[node] in + let start_inf,start_sup = + let repr_node = + match List.filter (fun (repr',_,_,_) -> repr=repr') nodes with + [node] -> node + | _ -> assert false + in +inf,sup(* + match hecandidate with + I -> inf,[repr_node] + | C -> [repr_node],sup + | M -> inf,sup +*) + in + let set = + locate_using_leq (to_be_considered,Some repr,news) node set start_sup in +( +let _,inf,sup = set in +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); +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); +); + let set = + locate_using_geq (to_be_considered,Some repr,news) node set start_inf + in +( +let _,inf,sup = set in +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); +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); +); + news@[candidate],set with - Not_found -> - let leq = ref [] in - let geq = ref [] in - let node = candidate,[],leq,geq in - let set = node::set in - locate (to_be_considered,Some repr,news) node set; - candidate::news,set + SameEquivalenceClass ((nodes,inf,sup) as set,((r,_,leq_d,geq_d) as node_to_be_deleted),node')-> +prerr_endline ("SAMEEQCLASS: " ^ string_of_cop r); +( +let _,inf,sup = set in +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); +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); +); + let rec clean inf sup res = + function + [] -> inf,sup,res + | node::tl when node===node_to_be_deleted -> + clean inf sup res tl + | (repr',others,leq,geq) as node::tl -> + leq := + List.fold_right + (fun node l -> + if node_to_be_deleted <=> node then + node::l + else + !leq_d@l + ) !leq []; + let sup = if !leq = [] then sup@@node else sup in + geq := + List.fold_right + (fun node l -> + if node_to_be_deleted <=> node then + node::l + else + !geq_d@l + ) !geq []; + let inf = if !geq = [] then inf@@node else inf in + if node===node' then + clean inf sup ((repr',others@[candidate],leq,geq)::res) tl + else + clean inf sup (node::res) tl + in + let inf,sup,nodes = clean inf sup [] nodes in + let inf = remove node_to_be_deleted inf in + let sup = remove node_to_be_deleted sup in +let set = nodes,inf,sup in +( +let _,inf,sup = set in +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); +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); +); + news,(nodes,inf,sup) ;; -let rec explore i set news = +let rec explore i (set:set) news = let rec aux news set = function [] -> news,set @@ -223,7 +423,8 @@ let rec explore i set news = end in let id = [] in - let set = [id,[],ref [], ref []] in + let id_node = id,[],ref [], ref [] in + let set = [id_node],[id_node],[id_node] in print_endline ("PRIMA ITERAZIONE, i=0, j=0"); print_endline (string_of_set set ^ "\n----------------"); (*ignore (Unix.system "rm -f log");*)