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.
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_______________________________________________________________ *)
12 (* $Id: nCicRefiner.mli 9227 2008-11-21 16:00:06Z tassi $ *)
14 module COT : Set.OrderedType with type t = int * NCic.term =
16 type t = int * NCic.term
17 let compare = Pervasives.compare
20 module HintSet = Set.Make(COT)
23 Discrimination_tree.Make(NDiscriminationTree.NCicIndexable)(HintSet)
26 Discrimination_tree.Make(NDiscriminationTree.NCicIndexable)(HintSet)
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 *)
32 exception HintNotValid
36 val db = HDB.empty, EQDB.empty
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 >}
44 let dummy = NCic.Const (NReference.reference_of_string "cic:/dummy_conv.dec");;
45 let pair t1 t2 = (NCic.Appl [dummy;t1;t2]) ;;
47 let index_hint hdb context t1 t2 precedence =
50 | NCic.Meta _ | NCic.Appl (NCic.Meta _ :: _) -> false | _ -> true)
53 | NCic.Meta _ | NCic.Appl (NCic.Meta _ :: _) -> false | _ -> true)
55 let hole = NCic.Meta (-1,(0,NCic.Irl 0)) in
57 List.fold_left (fun t _ -> NCicSubstitution.subst hole t) t1 context
60 List.fold_left (fun t _ -> NCicSubstitution.subst hole t) t2 context
62 let src = pair t1_skeleton t2_skeleton in
63 let ctx2abstractions context t =
67 | NCic.Decl ty -> NCic.Prod (n,ty,t)
68 | NCic.Def (b,ty) -> NCic.LetIn (n,ty,b,t))
71 let data_hint = ctx2abstractions context (pair t1 t2) in
72 let data_t1 = t2_skeleton in
73 let data_t2 = t1_skeleton in
75 prerr_endline ("INDEXING: " ^
76 NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] src ^ " |==> " ^
77 NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] data);
80 HDB.index (fst (hdb#uhint_db)) src (precedence, data_hint),
82 (EQDB.index (snd (hdb#uhint_db)) t2_skeleton (precedence, data_t2))
83 t1_skeleton (precedence, data_t1))
86 let add_user_provided_hint db t precedence =
88 let rec aux ctx = function
90 (match List.rev l with
94 NCicTypeChecker.typeof ~metasenv:[] ~subst:[] ctx a
97 NCicTypeChecker.typeof ~metasenv:[] ~subst:[] ctx b
99 NCicReduction.are_convertible
100 ~metasenv:[] ~subst:[] ctx ty_a ty_b
102 NCicReduction.are_convertible
103 ~metasenv:[] ~subst:[] ctx a b
105 else raise HintNotValid
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
113 index_hint db c a b precedence
119 (let rec aux = function
120 | u1 :: tl -> List.map (f u1) tl :: aux tl
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 =
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
139 if NCicReduction.are_convertible ~subst:[] ~metasenv:[] ctx b1 b2
142 prerr_endline ("hint: " ^ NCicPp.ppterm ~metasenv:[] ~subst:[]
143 ~context:ctx b1 ^ " === " ^ NCicPp.ppterm ~metasenv:[]
144 ~subst:[] ~context:ctx b2);
147 function 0 -> [] | n -> NCic.Rel n :: mk_rels (n-1)
150 NCic.Appl (NCic.Const(OCic2NCic.reference_of_ouri
151 u1 (NReference.Def h1)) :: mk_rels (List.length ctx))
154 NCic.Appl (NCic.Const(OCic2NCic.reference_of_ouri
155 u2 (NReference.Def h2)) :: mk_rels (List.length ctx))
157 [ctx,b1,b2; ctx,b1,n2; ctx,n1,b2; ctx,n1,n2]
167 if List.length l > 1 then
170 [] (CoercDb.to_list (CoercDb.dump ()))
172 prerr_endline "MISTERO";
176 | (ctx,b1,b2) -> index_hint db ctx b1 b2 0)
177 !user_db (List.flatten hints)
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)
190 if pno + 1 = goal_arity then
191 ty, metasenv, [], goal_arity+1
193 t, metasenv1, arg::args, pno+1
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*)
199 let res, newmetasenv, arguments, _ = aux metasenv ty in
200 res, newmetasenv, arguments
203 let eq_class_of hdb t1 =
204 if NDiscriminationTree.NCicIndexable.path_string_of t1 =
205 [Discrimination_tree.Variable]
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 *)
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)
215 let look_for_hint hdb metasenv subst context t1 t2 =
217 prerr_endline ("KEY1: "^NCicPp.ppterm ~metasenv ~subst ~context (pair t1 t2));
218 prerr_endline ("KEY2: "^NCicPp.ppterm ~metasenv ~subst ~context (pair t2 t1));
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))));
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
229 List.map (fun (prec,ty) ->
230 prec,true,saturate ~delta:max_int metasenv subst context ty 0)
231 (HintSet.elements candidates1)
234 List.map (fun (prec,ty) ->
235 prec,false,saturate ~delta:max_int metasenv subst context ty 0)
236 (HintSet.elements candidates2)
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) ->
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
250 let (m,l), t = aux () (m,[]) t in
252 (candidates1 @ candidates2)
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
263 List.sort (fun (x,_,_,_) (y,_,_,_) -> Pervasives.compare x y) rc
265 let rc = List.map (fun (_,x,y,z) -> x,y,z) rc in
267 prerr_endline "Hints:";
269 (fun (metasenv, (t1, t2), premises) ->
271 ("\t" ^ String.concat "; "
272 (List.map (fun (a,b) ->
273 NCicPp.ppterm ~margin:max_int ~metasenv ~subst ~context a ^
275 NCicPp.ppterm ~margin:max_int ~metasenv ~subst ~context b)
278 NCicPp.ppterm ~margin:max_int ~metasenv ~subst ~context t1 ^
279 " = "^NCicPp.ppterm ~margin:max_int ~metasenv ~subst ~context t2))