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