]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/paramod.ml
first proof reconstruction attempt, still bugged since it
[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 100 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 raise (Failure "Timeout !");
43
44     (* keep goals demodulated w.r.t. actives and check if solved *)
45     (* we may move this at the end of infer_right *) 
46     let bag, g_actives = 
47       List.fold_left 
48         (fun (bag,acc) c -> 
49            let bag, c = Sup.simplify_goal maxvar (snd actives) bag c in
50              bag, c::acc) 
51         (bag,[]) g_actives 
52     in
53       
54       (* superposition left, simplifications on goals *)
55       debug "infer_left step...";
56       let bag, maxvar, g_actives, g_passives =
57         match select g_passives with
58       | None -> bag, maxvar, g_actives, g_passives
59       | Some (g_current, g_passives) -> 
60           debug ("Selected goal : " ^ Pp.pp_unit_clause g_current);
61           let bag, g_current = 
62             Sup.simplify_goal maxvar (snd actives) bag g_current
63           in
64           let bag, maxvar, new_goals = 
65             Sup.infer_left bag maxvar g_current actives 
66           in
67           let new_goals = List.fold_left add_passive_clause
68             PassiveSet.empty new_goals
69           in
70           bag, maxvar, g_current::g_actives,
71           (PassiveSet.union new_goals g_passives)
72     in
73       prerr_endline
74         (Printf.sprintf "Number of active goals : %d"
75            (List.length g_actives));
76       prerr_endline
77         (Printf.sprintf "Number of passive goals : %d"
78            (PassiveSet.cardinal g_passives));
79   
80       (* forward step *)
81  
82       (* e = select P           *
83        * e' = demod A e         *
84        * A' = demod [e'] A      *
85        * A'' = A' + e'          *
86        * e'' = fresh e'         *
87        * new = supright e'' A'' *
88        * new'= demod A'' new    *
89        * P' = P + new'          *)
90       debug "Forward infer step...";
91     let bag, maxvar, actives, passives, g_passives =
92       let rec aux_simplify passives =
93         match select passives with
94           | None -> assert false
95           | Some (current, passives) -> 
96               debug ("Selected fact : " ^ Pp.pp_unit_clause current);
97               match Sup.keep_simplified current actives bag with
98                 | None -> aux_simplify passives
99                 | Some x -> x
100       in
101       let (current, bag, actives) = aux_simplify passives
102       in                    
103         debug ("Fact after simplification :"
104                ^ Pp.pp_unit_clause current);
105         let bag, maxvar, actives, new_clauses = 
106           Sup.infer_right bag maxvar current actives 
107         in
108         let ctable = IDX.index_unit_clause IDX.DT.empty current in
109         let bag, maxvar, new_goals = 
110           List.fold_left 
111             (fun (bag,m,acc) g -> 
112                let bag, m, ng = Sup.infer_left bag maxvar g
113                  ([current],ctable) in
114                  bag,m,ng@acc) 
115             (bag,maxvar,[]) g_actives 
116         in
117         let new_clauses = List.fold_left add_passive_clause
118           PassiveSet.empty new_clauses in
119         let new_goals = List.fold_left add_passive_clause
120           PassiveSet.empty new_goals in
121           bag, maxvar, actives,
122       PassiveSet.union new_clauses passives,
123       PassiveSet.union new_goals g_passives
124     in
125       prerr_endline
126         (Printf.sprintf "Number of actives : %d" (List.length (fst actives)));
127       prerr_endline
128         (Printf.sprintf "Number of passives : %d"
129            (PassiveSet.cardinal passives));
130       given_clause bag maxvar actives passives g_actives g_passives
131   in
132
133   let mk_clause bag maxvar (t,ty) =
134     let (proof,ty) = B.saturate t ty in
135     let c, maxvar = Utils.mk_unit_clause maxvar ty proof in
136     let bag, c = Utils.add_to_bag bag c in
137     bag, maxvar, c
138   in
139   let bag, maxvar, goal = mk_clause Terms.M.empty 0 t in
140   let g_actives = [] in
141   let g_passives = PassiveSet.singleton (Utils.mk_passive_clause goal) in
142   let passives, bag, maxvar = 
143     List.fold_left
144       (fun (cl, bag, maxvar) t -> 
145          let bag, maxvar, c = mk_clause bag maxvar t in
146          (add_passive_clause cl c), bag, maxvar)
147       (PassiveSet.empty, bag, maxvar) table 
148   in
149   let actives = [], IDX.DT.empty in
150   try given_clause bag maxvar actives passives g_actives g_passives
151   with 
152   | Sup.Success (bag, _, (i,_,_,_)) ->
153       let l =
154         let module S  = 
155           HTopoSort.Make(struct type t=int let compare=Pervasives.compare end)
156         in
157         let module C : Set.S with type elt = int = 
158           Set.Make(struct type t=int let compare=Pervasives.compare end)
159         in
160         let all id = 
161           let rec traverse acc i =
162             match Terms.M.find i bag with
163             | (_,_,_,Terms.Exact _) -> acc
164             | (_,_,_,Terms.Step (_,i1,i2,_,_,_)) -> 
165                traverse (traverse (C.add i1 (C.add i2 acc)) i1) i2    
166           in
167            C.elements (traverse C.empty id)
168         in
169         S.topological_sort (all i) all
170       in
171       prerr_endline "YES!";
172       prerr_endline "Proof:"; 
173       List.iter (fun x -> 
174               prerr_endline (Pp.pp_unit_clause (Terms.M.find x bag))) l;
175       let proofterm = B.mk_proof bag l in
176       prerr_endline
177        (NCicPp.ppterm ~metasenv:C.metasenv ~subst:C.subst ~context:C.context
178          proofterm); 
179       let _metasenv, _subst, _proofterm, _prooftype = 
180         NCicRefiner.typeof rdb C.metasenv C.subst C.context proofterm None
181       in
182       ()
183   | Failure _ -> prerr_endline "FAILURE";
184 ;;