]> matita.cs.unibo.it Git - helm.git/blob - matitaB/components/ng_paramodulation/nCicParamod.ml
Filtering all equations that cannot be embedded (containing metas in the blob).
[helm.git] / matitaB / components / ng_paramodulation / nCicParamod.ml
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic        
3     ||A||  Library of Mathematics, developed at the Computer Science     
4     ||T||  Department, University of Bologna, Italy.                     
5     ||I||                                                                
6     ||T||  HELM is free software; you can redistribute it and/or         
7     ||A||  modify it under the terms of the GNU General Public License   
8     \   /  version 2 or (at your option) any later version.      
9      \ /   This software is distributed as is, NO WARRANTY.     
10       V_______________________________________________________________ *)
11
12 (* $Id: orderings.ml 9869 2009-06-11 22:52:38Z denes $ *)
13
14 NCicBlob.set_default_eqP()
15 ;;
16 NCicProof.set_default_sig()
17 ;;
18
19 let noprint _ = ();;
20 let print s = prerr_endline (Lazy.force s);; 
21 let debug = noprint;; 
22
23 module B : Orderings.Blob 
24   with type t = NCic.term and type input = NCic.term 
25   = Orderings.LPO(NCicBlob.NCicBlob)
26
27 module NCicParamod = Paramod.Paramod(B)
28
29 let readback status ?(demod=false) metasenv subst context (bag,i,fo_subst,l) =
30 (*
31   List.iter (fun x ->
32      print_endline (Pp.pp_unit_clause ~margin:max_int
33      (fst(Terms.M.find x bag)))) l; 
34 *)
35   (* let stamp = Unix.gettimeofday () in *)
36   let proofterm,prooftype = NCicProof.mk_proof status ~demod bag i fo_subst l in
37   (* debug (lazy (Printf.sprintf "Got proof term in %fs"
38                      (Unix.gettimeofday() -. stamp))); *)
39 (*
40   let metasenv, proofterm = 
41     let rec aux k metasenv = function
42       | NCic.Meta _ as t -> metasenv, t
43       | NCic.Implicit _ -> 
44           let metasenv, i, _, _ =
45             NCicMetaSubst.mk_meta metasenv context `IsTerm 
46           in
47             metasenv, NCic.Meta (i,(k,NCic.Irl (List.length context)))
48       | t -> NCicUntrusted.map_term_fold_a 
49           (fun _ k -> k+1) k aux metasenv t
50     in
51       aux 0 metasenv proofterm
52   in *)
53   debug (lazy (status#ppterm ~metasenv ~subst ~context proofterm));
54 (*
55   let stamp = Unix.gettimeofday () in
56   let metasenv, subst, proofterm, _prooftype = 
57     NCicRefiner.typeof 
58       (status#set_coerc_db NCicCoercion.empty_db) 
59       metasenv subst context proofterm None
60   in
61     print (lazy (Printf.sprintf "Refined in %fs"
62                      (Unix.gettimeofday() -. stamp)));
63 *)
64     proofterm, prooftype, metasenv, subst
65
66 let nparamod status metasenv subst context t table =
67   let module P = NCicParamod in
68   let module Pp = Pp.Pp(B) in
69   let bag, maxvar = Terms.empty_bag, 0 in
70   let saturate (t,ty) = 
71     NCicBlob.saturate status metasenv subst context t ty in
72   let (bag,maxvar), goals = 
73     HExtlib.list_mapi_acc 
74       (fun x _ a -> P.mk_goal a (saturate x)) (bag,maxvar) [t]
75   in
76   let (bag,maxvar), passives =
77     HExtlib.list_mapi_acc 
78       (fun x _ a -> P.mk_passive a (saturate x))  (bag,maxvar) table
79   in
80   match 
81     P.paramod ~useage:true ~max_steps:max_int ~timeout:(Unix.gettimeofday () +. 300.0) 
82       ~g_passives:goals ~passives (bag,maxvar) 
83   with 
84   | P.Error _ | P.GaveUp | P.Timeout _ -> []
85   | P.Unsatisfiable solutions -> 
86       List.map (readback status metasenv subst context) solutions
87 ;;
88   
89 module CB = NCicBlob.NCicBlob
90 module P = NCicParamod
91
92 type state = P.state
93 let empty_state = P.empty_state
94
95 let size_of_state = P.size_of_state
96
97 exception NotEmbeddable 
98
99 let not_embeddable status subst context ty =
100   let rec aux = function
101     | NCic.Meta _
102     | NCic.Const _
103     | NCic.Rel _
104     | NCic.Sort _ -> ()
105     | NCic.Appl l -> List.iter aux l
106     | t -> 
107         (* cannot embed a blob term containing metas *)
108         if (NCicUntrusted.metas_of_term status subst context t = [])
109         then () 
110         else raise NotEmbeddable
111   in 
112   try aux ty; noprint (lazy ("Embeddable")); false
113   with NotEmbeddable -> debug (lazy ("Not embeddable")); true
114 ;;
115
116 let tooflex (_,l,_,_) =
117   match l with
118     | Terms.Equation (l,r,_,o) ->
119       (match l,r,o with
120        | Terms.Var _, _, (Terms.Incomparable | Terms.Invertible) -> true
121        | _, Terms.Var _,(Terms.Incomparable | Terms.Invertible) -> true
122        | _ -> false)
123     | _ -> false
124 ;;  
125
126 let forward_infer_step status metasenv subst context s t ty =
127   let bag = P.bag_of_state s in
128   let saturate (t,ty) = 
129     NCicBlob.saturate status metasenv subst context t ty in
130   if not_embeddable status subst context ty 
131     then (debug (lazy ("theorem not embeddable")); s)
132   else
133   let bag,clause = P.mk_passive bag (saturate (t,ty)) in
134     if Terms.is_eq_clause clause then
135       if tooflex clause then (debug (lazy "pruning tooflex"); s)
136       else P.forward_infer_step (P.replace_bag s bag) clause 0
137     else ((*print (lazy "not eq");*) s)
138 ;;
139
140 let index_obj status s uri =
141   let obj = NCicEnvironment.get_checked_obj status uri in
142   debug (lazy ("indexing : " ^ (NUri.string_of_uri uri)));
143   debug (lazy ("no : " ^ (string_of_int (fst (Obj.magic uri)))));
144   match obj with
145     | (_,_,[],[],NCic.Constant(_,_,None,ty,_)) ->
146         let nref = NReference.reference_of_spec uri NReference.Decl in
147         forward_infer_step status [] [] [] s (NCic.Const nref) ty
148     | (_,d,[],[],NCic.Constant(_,_,Some(_),ty,_)) ->
149         let nref = NReference.reference_of_spec uri (NReference.Def d) in
150         forward_infer_step status [] [] [] s (NCic.Const nref) ty
151     | _ -> s
152 ;;
153
154 let demod status metasenv subst context s goal =
155   if not_embeddable status subst context (snd goal) then [] else
156   (* let stamp = Unix.gettimeofday () in *)
157   match P.demod s goal with
158     | P.Error _ | P.GaveUp | P.Timeout _ -> []
159     | P.Unsatisfiable solutions -> 
160       (* print (lazy (Printf.sprintf "Got solutions in %fs"
161                      (Unix.gettimeofday() -. stamp))); *)
162       List.map (readback ~demod:true status metasenv subst context) solutions
163 ;;
164
165 let paramod status metasenv subst context s goal =
166   (* let stamp = Unix.gettimeofday () in *)
167   if not_embeddable status subst context (snd goal) then [] else
168   match P.nparamod ~useage:true ~max_steps:max_int 
169     ~timeout:(Unix.gettimeofday () +. 300.0) s goal with
170   | P.Error _ | P.GaveUp | P.Timeout _ -> []
171   | P.Unsatisfiable solutions -> 
172       (* print (lazy (Printf.sprintf "Got solutions in %fs"
173                      (Unix.gettimeofday() -. stamp))); *)
174       List.map (readback status metasenv subst context) solutions
175 ;;
176
177 let fast_eq_check status metasenv subst context s goal =
178   if not_embeddable status subst context (snd goal) then [] else
179   (* let stamp = Unix.gettimeofday () in *)
180   match P.fast_eq_check s goal with
181   | P.Error _ | P.GaveUp | P.Timeout _ -> []
182   | P.Unsatisfiable solutions -> 
183       (* print (lazy (Printf.sprintf "Got solutions in %fs"
184                      (Unix.gettimeofday() -. stamp))); *)
185       List.map (readback status metasenv subst context) solutions
186 ;;
187
188 let is_equation status metasenv subst context ty =
189   let hty, _, _ = 
190     NCicMetaSubst.saturate status ~delta:0 metasenv subst context
191       ty 0 
192   in match hty with
193     | NCic.Appl (eq ::tl) when eq = CB.eqP() -> true
194     | _ -> false
195 ;;
196
197 (*
198 let demodulate status metasenv subst context s goal =
199   (* let stamp = Unix.gettimeofday () in *)
200   match P.fast_eq_check s goal with
201   | P.Error _ | P.GaveUp | P.Timeout _ -> []
202   | P.Unsatisfiable solutions -> 
203       (* print (lazy (Printf.sprintf "Got solutions in %fs"
204                      (Unix.gettimeofday() -. stamp))); *)
205       List.map (readback status metasenv subst context) solutions
206 ;;
207 *)