]> matita.cs.unibo.it Git - helm.git/blob - matita/components/ng_paramodulation/nCicParamod.ml
Most warnings turned into errors and avoided
[helm.git] / matita / 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(C : NCicBlob.NCicContext): Orderings.Blob 
24   with type t = NCic.term and type input = NCic.term 
25   = Orderings.LPO(NCicBlob.NCicBlob(C))
26
27 module NCicParamod(C : NCicBlob.NCicContext) = Paramod.Paramod(B(C))
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 C =
68     struct 
69       let metasenv = metasenv
70       let subst = subst
71       let context = context 
72     end 
73   in
74   let module B = B(C) in
75   let module P = NCicParamod(C) in
76   let module Pp = Pp.Pp(B) in
77   let bag, maxvar = Terms.empty_bag, 0 in
78   let (bag,maxvar), goals = 
79     HExtlib.list_mapi_acc (fun x _ a -> P.mk_goal a x) (bag,maxvar) [t]
80   in
81   let (bag,maxvar), passives = 
82     HExtlib.list_mapi_acc (fun x _ a -> P.mk_passive a x) (bag,maxvar) table
83   in
84   match 
85     P.paramod ~useage:true ~max_steps:max_int ~timeout:(Unix.gettimeofday () +. 300.0) 
86       ~g_passives:goals ~passives (bag,maxvar) 
87   with 
88   | P.Error _ | P.GaveUp | P.Timeout _ -> []
89   | P.Unsatisfiable solutions -> 
90       List.map (readback status metasenv subst context) solutions
91 ;;
92   
93 module EmptyC = 
94   struct
95     let metasenv = []
96     let subst = []
97     let context = []
98   end
99
100 module CB = NCicBlob.NCicBlob(EmptyC)
101 module P = NCicParamod(EmptyC)
102
103 type state = P.state
104 let empty_state = P.empty_state
105
106 exception NotEmbeddable 
107
108 let not_embeddable status subst context ty =
109   let rec aux = function
110     | NCic.Meta _
111     | NCic.Const _
112     | NCic.Rel _
113     | NCic.Sort _ -> ()
114     | NCic.Appl l -> List.iter aux l
115     | t -> 
116         (* cannot embed a blob term containing metas *)
117         if (NCicUntrusted.metas_of_term status subst context t = [])
118         then () 
119         else raise NotEmbeddable
120   in 
121   try aux ty; noprint (lazy ("Embeddable")); false
122   with NotEmbeddable -> debug (lazy ("Not embeddable")); true
123 ;;
124
125 let tooflex (_,l,_,_) =
126   match l with
127     | Terms.Equation (l,r,_,o) ->
128       (match l,r,o with
129        | (Terms.Var _ | Terms.Node (Terms.Var _::_)), _, (Terms.Incomparable | Terms.Invertible) -> true
130        | _, (Terms.Var _ | Terms.Node (Terms.Var _::_)),(Terms.Incomparable | Terms.Invertible) -> true
131        | _ -> false)
132     | _ -> false
133 ;;  
134
135 let forward_infer_step0 status metasenv subst context s t ty =
136   let bag = P.bag_of_state s in
137   let not_embed =
138     let sty,_,_ = 
139       NCicMetaSubst.saturate status ~delta:0  metasenv subst context ty 0 in
140       not_embeddable status subst context sty in
141   if not_embed then (debug (lazy "theorem not embeddable"); s,None)
142   else
143   let bag,clause = P.mk_passive bag (t,ty) in
144     if Terms.is_eq_clause clause then
145       if tooflex clause then (debug (lazy "pruning tooflex"); s,None)
146       else
147        P.forward_infer_step (P.replace_bag s bag) clause 0, Some clause
148     else (debug (lazy "not eq"); s,None)
149 ;;
150
151 let forward_infer_step status metasenv subst context s t ty =
152  fst (forward_infer_step0 status metasenv subst context s t ty)
153 ;;
154
155 let index_obj status s uri =
156   let obj = NCicEnvironment.get_checked_obj status uri in
157   debug (lazy ("indexing : " ^ (NUri.string_of_uri uri)));
158   debug (lazy ("no : " ^ (string_of_int (fst (Obj.magic uri)))));
159   match obj with
160     | (_,_,[],[],NCic.Constant(_,_,None,ty,_)) ->
161         let nref = NReference.reference_of_spec uri NReference.Decl in
162         forward_infer_step0 status [] [] [] s (NCic.Const nref) ty
163     | (_,d,[],[],NCic.Constant(_,_,Some(_),ty,_)) ->
164         let nref = NReference.reference_of_spec uri (NReference.Def d) in
165         forward_infer_step0 status [] [] [] s (NCic.Const nref) ty
166     | _ -> s,None
167 ;;
168
169 let demod status metasenv subst context s goal =
170   if not_embeddable status subst context (snd goal) then [] else
171   (* let stamp = Unix.gettimeofday () in *)
172   match P.demod s goal with
173     | P.Error _ | P.GaveUp | P.Timeout _ -> []
174     | P.Unsatisfiable solutions -> 
175       (* print (lazy (Printf.sprintf "Got solutions in %fs"
176                      (Unix.gettimeofday() -. stamp))); *)
177       List.map (readback ~demod:true status metasenv subst context) solutions
178 ;;
179
180 let paramod status metasenv subst context s goal =
181   if not_embeddable status subst context (snd goal) then [] else
182   (* let stamp = Unix.gettimeofday () in *)
183   match P.nparamod ~useage:true ~max_steps:max_int 
184     ~timeout:(Unix.gettimeofday () +. 300.0) s goal with
185   | P.Error _ | P.GaveUp | P.Timeout _ -> []
186   | P.Unsatisfiable solutions -> 
187       (* print (lazy (Printf.sprintf "Got solutions in %fs"
188                      (Unix.gettimeofday() -. stamp))); *)
189       List.map (readback status metasenv subst context) solutions
190 ;;
191
192 let fast_eq_check status metasenv subst context s goal =
193   if not_embeddable status subst context (snd goal) then [] else
194   (* let stamp = Unix.gettimeofday () in *)
195   match P.fast_eq_check s goal with
196   | P.Error _ | P.GaveUp | P.Timeout _ -> []
197   | P.Unsatisfiable solutions -> 
198       (* print (lazy (Printf.sprintf "Got solutions in %fs"
199                      (Unix.gettimeofday() -. stamp))); *)
200       List.map (readback status metasenv subst context) solutions
201 ;;
202
203 let is_equation status metasenv subst context ty =
204   let hty, _, _ = 
205     NCicMetaSubst.saturate status ~delta:0 metasenv subst context
206       ty 0 
207   in match hty with
208     | NCic.Appl (eq ::_) when eq = CB.eqP -> true
209     | _ -> false
210 ;;
211
212 (*
213 let demodulate status metasenv subst context s goal =
214   (* let stamp = Unix.gettimeofday () in *)
215   match P.fast_eq_check s goal with
216   | P.Error _ | P.GaveUp | P.Timeout _ -> []
217   | P.Unsatisfiable solutions -> 
218       (* print (lazy (Printf.sprintf "Got solutions in %fs"
219                      (Unix.gettimeofday() -. stamp))); *)
220       List.map (readback status metasenv subst context) solutions
221 ;;
222 *)