]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_refiner/nCicUnifHint.ml
ce88085521c2ddabda8d96069f9fba79bb90a876
[helm.git] / helm / software / components / ng_refiner / nCicUnifHint.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: nCicRefiner.mli 9227 2008-11-21 16:00:06Z tassi $ *)
13
14 let debug s = prerr_endline (Lazy.force s);;
15 let debug _ = ();;
16
17 module COT : Set.OrderedType with type t = int * NCic.term = 
18   struct
19         type t = int * NCic.term
20         let compare = Pervasives.compare
21   end
22
23 module HintSet = Set.Make(COT)
24
25 module HDB = 
26   Discrimination_tree.Make(NDiscriminationTree.NCicIndexable)(HintSet)
27
28 module EQDB = 
29   Discrimination_tree.Make(NDiscriminationTree.NCicIndexable)(HintSet)
30
31 type db =
32   HDB.t * (* hint database: (dummy A B)[?] |-> \forall X.(summy a b)[X] *)
33   EQDB.t (* eqclass DB: A[?] |-> \forall X.B[X] and viceversa *)
34
35 exception HintNotValid
36
37 let skel_dummy = NCic.Implicit `Type;;
38
39 class type g_status =
40  object
41   method uhint_db: db
42  end
43
44 class status =
45  object
46   val db = HDB.empty, EQDB.empty
47   method uhint_db = db
48   method set_uhint_db v = {< db = v >}
49   method set_unifhint_status
50    : 'status. #g_status as 'status -> 'self
51    = fun o -> {< db = o#uhint_db >}
52  end
53
54 let dummy = NCic.Const (NReference.reference_of_string "cic:/dummy_conv.dec");;
55 let pair t1 t2 = (NCic.Appl [dummy;t1;t2]) ;;
56
57 let index_hint hdb context t1 t2 precedence =
58   assert (
59     (match t1 with
60     | NCic.Meta _ | NCic.Appl (NCic.Meta _ :: _) -> false | _ -> true)
61     &&
62     (match t2 with
63     | NCic.Meta _ | NCic.Appl (NCic.Meta _ :: _) -> false | _ -> true)
64   );
65   (* here we do not use skel_dummy since it could cause an assert false in 
66    * the subst function that lives in the kernel *)
67   let hole = NCic.Meta (-1,(0,NCic.Irl 0)) in
68   let t1_skeleton = 
69     List.fold_left (fun t _ -> NCicSubstitution.subst hole t) t1 context
70   in
71   let t2_skeleton = 
72     List.fold_left (fun t _ -> NCicSubstitution.subst hole t) t2 context
73   in
74   let rec cleanup_skeleton () = function
75     | NCic.Meta _ -> skel_dummy 
76     | t -> NCicUtils.map (fun _ () -> ()) () cleanup_skeleton t
77   in
78   let t1_skeleton = cleanup_skeleton () t1_skeleton in
79   let t2_skeleton = cleanup_skeleton () t2_skeleton in
80   let src = pair t1_skeleton t2_skeleton in
81   let ctx2abstractions context t = 
82     List.fold_left 
83      (fun t (n,e) -> 
84         match e with
85         | NCic.Decl ty -> NCic.Prod (n,ty,t)
86         | NCic.Def (b,ty) -> NCic.LetIn (n,ty,b,t))
87       t context 
88   in
89   let data_hint = ctx2abstractions context (pair t1 t2) in
90   let data_t1 = t2_skeleton in
91   let data_t2 = t1_skeleton in
92
93   debug(lazy ("INDEXING: " ^ 
94     NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] src ^ " |==> " ^
95     NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] data_hint));
96
97   hdb#set_uhint_db (
98       HDB.index (fst (hdb#uhint_db)) src (precedence, data_hint),
99       EQDB.index  
100         (EQDB.index (snd (hdb#uhint_db)) t2_skeleton (precedence, data_t2))
101         t1_skeleton (precedence, data_t1))
102 ;;
103
104 let add_user_provided_hint db t precedence =
105   let c, a, b = 
106     let rec aux ctx = function
107       | NCic.Appl l ->
108           (match List.rev l with
109           | b::a::_ -> 
110               if
111                  let ty_a = 
112                    NCicTypeChecker.typeof ~metasenv:[] ~subst:[] ctx a
113                  in
114                  let ty_b = 
115                    NCicTypeChecker.typeof ~metasenv:[] ~subst:[] ctx b
116                  in
117                  NCicReduction.are_convertible 
118                   ~metasenv:[] ~subst:[] ctx ty_a ty_b              
119                  &&     
120                  NCicReduction.are_convertible 
121                   ~metasenv:[] ~subst:[] ctx a b              
122               then ctx, a, b
123               else raise HintNotValid
124           | _ -> assert false)
125       | NCic.Prod (n,s,t) -> aux ((n, NCic.Decl s) :: ctx) t
126       | NCic.LetIn (n,ty,t,b) -> aux  ((n, NCic.Def (t,ty)) :: ctx) b
127       | _ -> assert false
128     in
129       aux [] t
130   in
131    index_hint db c a b precedence
132 ;;
133
134 let db () = 
135   let combine f l =
136    List.flatten
137      (let rec aux = function 
138       | u1 :: tl -> List.map (f u1) tl :: aux tl
139       | [] -> []
140      in aux l)
141   in
142   let mk_hint (u1,_,_) (u2,_,_) = 
143     let l = OCic2NCic.convert_obj u1 
144       (fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph u1)) in
145     let r = OCic2NCic.convert_obj u2 
146       (fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph u2)) in
147     match List.hd l,List.hd r with
148     | (_,h1,_,_,NCic.Constant (_,_,Some l,_,_)), 
149       (_,h2,_,_,NCic.Constant (_,_,Some r,_,_)) ->
150         let rec aux ctx t1 t2 =
151           match t1, t2 with
152           | NCic.Lambda (n1,s1,b1), NCic.Lambda(_,s2,b2) ->
153               if NCicReduction.are_convertible ~subst:[] ~metasenv:[] ctx s1 s2
154               then aux ((n1, NCic.Decl s1) :: ctx) b1 b2
155               else []
156           | b1,b2 -> 
157               if NCicReduction.are_convertible ~subst:[] ~metasenv:[] ctx b1 b2 
158               then begin
159               let rec mk_rels =  
160                  function 0 -> [] | n -> NCic.Rel n :: mk_rels (n-1) 
161               in 
162               let n1 = 
163                 NCic.Appl (NCic.Const(OCic2NCic.reference_of_ouri 
164                  u1 (NReference.Def h1)) :: mk_rels (List.length ctx))
165               in
166               let n2 = 
167                 NCic.Appl (NCic.Const(OCic2NCic.reference_of_ouri 
168                  u2 (NReference.Def h2)) :: mk_rels (List.length ctx))
169               in
170                 [ctx,b1,b2; ctx,b1,n2; ctx,n1,b2; ctx,n1,n2]
171               end else []
172         in
173           aux [] l r
174     | _ -> []
175   in
176   let _hints = 
177     List.fold_left 
178       (fun acc (_,_,l) -> 
179           acc @ 
180           if List.length l > 1 then 
181            combine mk_hint l
182           else [])
183       [] (CoercDb.to_list (CoercDb.dump ()))
184   in
185   prerr_endline "MISTERO";
186   assert false (* ERA
187   List.fold_left 
188     (fun db -> function 
189      | (ctx,b1,b2) -> index_hint db ctx b1 b2 0)
190     !user_db (List.flatten hints)
191 *)
192 ;;
193
194 let saturate ?(delta=0) metasenv subst context ty goal_arity =
195  assert (goal_arity >= 0);
196   let rec aux metasenv = function
197    | NCic.Prod (name,s,t) as ty ->
198        let metasenv1, _, arg,_ = 
199           NCicMetaSubst.mk_meta ~name:name metasenv context (`WithType s) in
200        let t, metasenv1, args, pno = 
201            aux metasenv1 (NCicSubstitution.subst arg t) 
202        in
203        if pno + 1 = goal_arity then
204          ty, metasenv, [], goal_arity+1
205        else
206          t, metasenv1, arg::args, pno+1
207    | ty ->
208         match NCicReduction.whd ~subst context ty ~delta with
209         | NCic.Prod _ as ty -> aux metasenv ty
210         | _ -> ty, metasenv, [], 0 (* differs from the other impl in this line*)
211   in
212   let res, newmetasenv, arguments, _ = aux metasenv ty in
213   res, newmetasenv, arguments
214 ;;
215
216 let eq_class_of hdb t1 = 
217  let eq_class =
218   if NDiscriminationTree.NCicIndexable.path_string_of t1 = 
219      [Discrimination_tree.Variable]
220   then
221     [] (* if the trie is unable to handle the key, we skip the query since
222           it sould retulr the whole content of the trie *)
223   else
224     let candidates = EQDB.retrieve_unifiables (snd hdb#uhint_db) t1 in
225     let candidates = HintSet.elements candidates in
226     let candidates = List.sort (fun (x,_) (y,_) -> compare x y) candidates in
227     List.map snd candidates
228  in
229  debug(lazy("eq_class of: " ^ NCicPp.ppterm ~metasenv:[] ~context:[] ~subst:[]
230    t1 ^ " is\n" ^ String.concat "\n" 
231    (List.map (NCicPp.ppterm ~subst:[] ~metasenv:[] ~context:[]) eq_class)));
232  eq_class   
233 ;;
234
235 let look_for_hint hdb metasenv subst context t1 t2 =
236   debug(lazy ("KEY1: "^NCicPp.ppterm ~metasenv ~subst ~context t1));
237   debug(lazy ("KEY2: "^NCicPp.ppterm ~metasenv ~subst ~context t2));
238 (*
239   HDB.iter hdb
240    (fun p ds ->
241       prerr_endline ("ENTRY: " ^
242       NDiscriminationTree.NCicIndexable.string_of_path p ^ " |--> " ^
243       String.concat "|" (List.map (NCicPp.ppterm ~metasenv:[] ~subst:[]
244       ~context:[]) (HintSet.elements ds))));
245 *)
246   let candidates1 = HDB.retrieve_unifiables (fst hdb#uhint_db) (pair t1 t2) in
247   let candidates2 = HDB.retrieve_unifiables (fst hdb#uhint_db) (pair t2 t1) in
248   let candidates1 = 
249     List.map (fun (prec,ty) -> 
250        prec,true,saturate ~delta:max_int metasenv subst context ty 0) 
251     (HintSet.elements candidates1) 
252   in
253   let candidates2 = 
254     List.map (fun (prec,ty) -> 
255        prec,false,saturate ~delta:max_int metasenv subst context ty 0) 
256     (HintSet.elements candidates2) 
257   in
258   let rc = 
259     List.map
260       (fun (p,b,(t,m,_)) ->
261          let rec aux () (m,l as acc) = function
262            | NCic.Meta _ as t -> acc, t
263            | NCic.LetIn (name,ty,bo,t) ->
264                let m,_,i,_=
265                  NCicMetaSubst.mk_meta ~name m context (`WithType ty)in
266                let t = NCicSubstitution.subst i t in
267                aux () (m, (i,bo)::l) t
268            | t -> NCicUntrusted.map_term_fold_a (fun _ () -> ()) () aux acc t
269          in
270          let (m,l), t = aux () (m,[]) t in
271          p,b,(t,m,l))
272    (candidates1 @ candidates2)
273   in
274   let rc = 
275   List.map
276    (function 
277     | (prec,true,(NCic.Appl [_; t1; t2],metasenv,l))-> prec,metasenv,(t1,t2),l
278     | (prec,false,(NCic.Appl [_; t1; t2],metasenv,l))-> prec,metasenv,(t2,t1),l
279     | _ -> assert false)
280     rc
281   in
282   let rc = 
283     List.sort (fun (x,_,_,_) (y,_,_,_) -> Pervasives.compare x y) rc
284   in 
285   let rc = List.map (fun (_,x,y,z) -> x,y,z) rc in
286
287   debug(lazy ("Hints:"^
288     String.concat "\n" (List.map 
289      (fun (metasenv, (t1, t2), premises) ->
290          ("\t" ^ String.concat ";  "
291                (List.map (fun (a,b) -> 
292                   NCicPp.ppterm ~margin:max_int ~metasenv ~subst ~context a ^
293                   " =?= "^
294                   NCicPp.ppterm ~margin:max_int ~metasenv ~subst ~context b)
295                premises) ^     
296              "  ==> "^
297              NCicPp.ppterm ~margin:max_int ~metasenv ~subst ~context t1 ^
298              " = "^NCicPp.ppterm ~margin:max_int ~metasenv ~subst ~context t2))
299     rc)));
300
301   rc
302 ;;