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