]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/paramod.ml
8903ec02fc1c24082fe54d53ddbb32006ec761c4
[helm.git] / helm / software / components / ng_paramodulation / paramod.ml
1 let debug s =
2   () (* prerr_endline s *)
3 ;;
4
5 let nparamod rdb metasenv subst context t table =
6   let nb_iter = ref 200 in
7   prerr_endline "========================================";
8   let module C = struct
9     let metasenv = metasenv
10     let subst = subst
11     let context = context
12   end
13   in
14   let module B = NCicBlob.NCicBlob(C) in
15   let module Pp = Pp.Pp (B) in
16   let module FU = FoUnif.Founif(B) in
17   let module IDX = Index.Index(B) in
18   let module Sup = Superposition.Superposition(B) in
19   let module Utils = FoUtils.Utils(B) in
20     
21   let module OrderedPassives =
22       struct
23         type t = B.t Terms.passive_clause
24             
25         let compare = Utils.compare_passive_clauses
26       end
27   in
28   let module PassiveSet = Set.Make(OrderedPassives)
29   in
30   let add_passive_clause passives cl =
31     PassiveSet.add (Utils.mk_passive_clause cl) passives
32   in
33     (* TODO : fairness condition *)
34   let select passives =
35     if PassiveSet.is_empty passives then None
36     else let cl = PassiveSet.min_elt passives in
37       Some (snd cl,PassiveSet.remove cl passives)
38   in
39   let rec given_clause bag maxvar actives      
40       passives g_actives g_passives =
41     
42     decr nb_iter; if !nb_iter = 0 then 
43       (*(prerr_endline "Bag :"; prerr_endline (Pp.pp_bag bag);
44       prerr_endline "Active table :"; 
45        (List.iter (fun x -> prerr_endline (Pp.pp_unit_clause x))
46           (fst actives));*)
47     raise (Failure "Timeout !");
48
49
50       
51     (* superposition left, simplifications on goals *)
52       debug "infer_left step...";
53       let bag, maxvar, g_actives, g_passives =
54         match select g_passives with
55       | None -> bag, maxvar, g_actives, g_passives
56       | Some (g_current, g_passives) -> 
57           debug ("Selected goal : " ^ Pp.pp_unit_clause g_current);
58           let bag, g_current = 
59             Sup.simplify_goal maxvar (snd actives) bag g_current
60           in
61           let bag, maxvar, new_goals = 
62             Sup.infer_left bag maxvar g_current actives 
63           in
64           let new_goals = List.fold_left add_passive_clause
65             PassiveSet.empty new_goals
66           in
67           bag, maxvar, g_current::g_actives,
68           (PassiveSet.union new_goals g_passives)
69     in
70       prerr_endline
71         (Printf.sprintf "Number of active goals : %d"
72            (List.length g_actives));
73       prerr_endline
74         (Printf.sprintf "Number of passive goals : %d"
75            (PassiveSet.cardinal g_passives));
76   
77       (* forward step *)
78  
79       (* e = select P           *
80        * e' = demod A e         *
81        * A' = demod [e'] A      *
82        * A'' = A' + e'          *
83        * e'' = fresh e'         *
84        * new = supright e'' A'' *
85        * new'= demod A'' new    *
86        * P' = P + new'          *)
87       debug "Forward infer step...";
88     let bag, maxvar, actives, passives, g_passives =
89       let rec aux_simplify passives =
90         match select passives with
91           | None -> assert false
92           | Some (current, passives) -> 
93               debug ("Selected fact : " ^ Pp.pp_unit_clause current);
94               match Sup.keep_simplified current actives bag with
95               (* match Sup.one_pass_simplification current actives bag with *)
96                 | None -> aux_simplify passives
97                 | Some x -> x,passives
98       in
99       let (current, bag, actives),passives = aux_simplify passives
100       in                    
101         debug ("Fact after simplification :"
102                ^ Pp.pp_unit_clause current);
103         let bag, maxvar, actives, new_clauses = 
104           Sup.infer_right bag maxvar current actives 
105         in
106   debug "Demodulating goals with actives...";
107           (* keep goals demodulated w.r.t. actives and check if solved *)
108         let bag, g_actives = 
109           List.fold_left 
110             (fun (bag,acc) c -> 
111                let bag, c = Sup.simplify_goal maxvar (snd actives) bag c in
112                  bag, c::acc) 
113             (bag,[]) g_actives 
114         in
115         let ctable = IDX.index_unit_clause IDX.DT.empty current in
116         let bag, maxvar, new_goals = 
117           List.fold_left 
118             (fun (bag,m,acc) g -> 
119                let bag, m, ng = Sup.infer_left bag maxvar g
120                  ([current],ctable) in
121                  bag,m,ng@acc) 
122             (bag,maxvar,[]) g_actives 
123         in
124         let new_clauses = List.fold_left add_passive_clause
125           PassiveSet.empty new_clauses in
126         let new_goals = List.fold_left add_passive_clause
127           PassiveSet.empty new_goals in
128           bag, maxvar, actives,
129       PassiveSet.union new_clauses passives,
130       PassiveSet.union new_goals g_passives
131     in
132       prerr_endline
133         (Printf.sprintf "Number of actives : %d" (List.length (fst actives)));
134       prerr_endline
135         (Printf.sprintf "Number of passives : %d"
136            (PassiveSet.cardinal passives));
137       given_clause bag maxvar actives passives g_actives g_passives
138   in
139
140   let mk_clause bag maxvar (t,ty) =
141     let (proof,ty) = B.saturate t ty in
142     let c, maxvar = Utils.mk_unit_clause maxvar ty proof in
143     let bag, c = Utils.add_to_bag bag c in
144     bag, maxvar, c
145   in
146   let bag, maxvar, goal = mk_clause Terms.M.empty 0 t in
147   let g_actives = [] in
148   let g_passives = PassiveSet.singleton (Utils.mk_passive_clause goal) in
149   let passives, bag, maxvar = 
150     List.fold_left
151       (fun (cl, bag, maxvar) t -> 
152          let bag, maxvar, c = mk_clause bag maxvar t in
153          (add_passive_clause cl c), bag, maxvar)
154       (PassiveSet.empty, bag, maxvar) table 
155   in
156   let actives = [], IDX.DT.empty in
157   try given_clause bag maxvar actives passives g_actives g_passives
158   with 
159   | Sup.Success (bag, _, (i,_,_,_)) ->
160       let l =
161         let module S  = 
162           HTopoSort.Make(struct type t=int let compare=Pervasives.compare end)
163         in
164         let module C : Set.S with type elt = int = 
165           Set.Make(struct type t=int let compare=Pervasives.compare end)
166         in
167         let all id = 
168           let rec traverse acc i =
169             match Terms.M.find i bag with
170             | (_,_,_,Terms.Exact _) -> acc
171             | (_,_,_,Terms.Step (_,i1,i2,_,_,_)) -> 
172                traverse (traverse (C.add i1 (C.add i2 acc)) i1) i2    
173           in
174            C.elements (traverse C.empty id)
175         in
176         S.topological_sort (all i) all
177       in
178       prerr_endline "YES!";
179       prerr_endline "Proof:"; 
180       List.iter (fun x -> 
181               prerr_endline (Pp.pp_unit_clause (Terms.M.find x bag))) l;
182       let proofterm = B.mk_proof bag l in
183       prerr_endline
184        (NCicPp.ppterm ~metasenv:C.metasenv ~subst:C.subst ~context:C.context
185          proofterm); 
186       let _metasenv, _subst, _proofterm, _prooftype = 
187         NCicRefiner.typeof rdb C.metasenv C.subst C.context proofterm None
188       in
189       ()
190   | Failure _ -> prerr_endline "FAILURE";
191 ;;