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 let debug s = prerr_endline (Lazy.force s);;
17 module HOT : Set.OrderedType
18 with type t = int * NCic.term * NCic.term * NCic.term =
20 (* precedence, skel1, skel2, term *)
21 type t = int * NCic.term * NCic.term * NCic.term
22 let compare = Pervasives.compare
25 module EOT : Set.OrderedType
26 with type t = int * NCic.term =
28 type t = int * NCic.term
29 let compare = Pervasives.compare
32 module HintSet = Set.Make(HOT)
33 module EqSet = Set.Make(EOT)
36 Discrimination_tree.Make(NDiscriminationTree.NCicIndexable)(HintSet)
39 Discrimination_tree.Make(NDiscriminationTree.NCicIndexable)(EqSet)
42 HDB.t * (* hint database: (dummy A B)[?] |-> \forall X.(summy a b)[X] *)
43 EQDB.t (* eqclass DB: A[?] |-> \forall X.B[X] and viceversa *)
45 exception HintNotValid
47 let skel_dummy = NCic.Implicit `Type;;
56 val db = HDB.empty, EQDB.empty
58 method set_uhint_db v = {< db = v >}
59 method set_unifhint_status
60 : 'status. #g_status as 'status -> 'self
61 = fun o -> {< db = o#uhint_db >}
64 let dummy = NCic.Const (NReference.reference_of_string "cic:/dummy_conv.dec");;
65 let pair t1 t2 = (NCic.Appl [dummy;t1;t2]) ;;
67 let index_hint hdb context t1 t2 precedence =
70 | NCic.Meta _ | NCic.Appl (NCic.Meta _ :: _) -> false | _ -> true)
73 | NCic.Meta _ | NCic.Appl (NCic.Meta _ :: _) -> false | _ -> true)
75 (* here we do not use skel_dummy since it could cause an assert false in
76 * the subst function that lives in the kernel *)
77 let hole = NCic.Meta (-1,(0,NCic.Irl 0)) in
79 List.fold_left (fun t _ -> NCicSubstitution.subst hole t) t1 context
82 List.fold_left (fun t _ -> NCicSubstitution.subst hole t) t2 context
84 let rec cleanup_skeleton () = function
85 | NCic.Meta _ -> skel_dummy
86 | t -> NCicUtils.map (fun _ () -> ()) () cleanup_skeleton t
88 let t1_skeleton = cleanup_skeleton () t1_skeleton in
89 let t2_skeleton = cleanup_skeleton () t2_skeleton in
90 let src = pair t1_skeleton t2_skeleton in
91 let ctx2abstractions context t =
95 | NCic.Decl ty -> NCic.Prod (n,ty,t)
96 | NCic.Def (b,ty) -> NCic.LetIn (n,ty,b,t))
100 precedence, t1_skeleton, t2_skeleton, ctx2abstractions context (pair t1 t2)
102 let data_t1 = t2_skeleton in
103 let data_t2 = t1_skeleton in
105 debug(lazy ("INDEXING: " ^
106 NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] src ^ " |==> " ^
107 NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[]
108 (let _,x,_,_ = data_hint in x)));
111 HDB.index (fst (hdb#uhint_db)) src data_hint,
113 (EQDB.index (snd (hdb#uhint_db)) t2_skeleton (precedence, data_t2))
114 t1_skeleton (precedence, data_t1))
117 let add_user_provided_hint db t precedence =
119 let rec aux ctx = function
121 (match List.rev l with
125 NCicTypeChecker.typeof ~metasenv:[] ~subst:[] ctx a
128 NCicTypeChecker.typeof ~metasenv:[] ~subst:[] ctx b
130 NCicReduction.are_convertible
131 ~metasenv:[] ~subst:[] ctx ty_a ty_b
133 NCicReduction.are_convertible
134 ~metasenv:[] ~subst:[] ctx a b
136 else raise HintNotValid
138 | NCic.Prod (n,s,t) -> aux ((n, NCic.Decl s) :: ctx) t
139 | NCic.LetIn (n,ty,t,b) -> aux ((n, NCic.Def (t,ty)) :: ctx) b
144 index_hint db c a b precedence
150 (let rec aux = function
151 | u1 :: tl -> List.map (f u1) tl :: aux tl
155 let mk_hint (u1,_,_) (u2,_,_) =
156 let l = OCic2NCic.convert_obj u1
157 (fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph u1)) in
158 let r = OCic2NCic.convert_obj u2
159 (fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph u2)) in
160 match List.hd l,List.hd r with
161 | (_,h1,_,_,NCic.Constant (_,_,Some l,_,_)),
162 (_,h2,_,_,NCic.Constant (_,_,Some r,_,_)) ->
163 let rec aux ctx t1 t2 =
165 | NCic.Lambda (n1,s1,b1), NCic.Lambda(_,s2,b2) ->
166 if NCicReduction.are_convertible ~subst:[] ~metasenv:[] ctx s1 s2
167 then aux ((n1, NCic.Decl s1) :: ctx) b1 b2
170 if NCicReduction.are_convertible ~subst:[] ~metasenv:[] ctx b1 b2
173 function 0 -> [] | n -> NCic.Rel n :: mk_rels (n-1)
176 NCic.Appl (NCic.Const(OCic2NCic.reference_of_ouri
177 u1 (NReference.Def h1)) :: mk_rels (List.length ctx))
180 NCic.Appl (NCic.Const(OCic2NCic.reference_of_ouri
181 u2 (NReference.Def h2)) :: mk_rels (List.length ctx))
183 [ctx,b1,b2; ctx,b1,n2; ctx,n1,b2; ctx,n1,n2]
193 if List.length l > 1 then
196 [] (CoercDb.to_list (CoercDb.dump ()))
198 prerr_endline "MISTERO";
202 | (ctx,b1,b2) -> index_hint db ctx b1 b2 0)
203 !user_db (List.flatten hints)
207 let saturate ?(delta=0) metasenv subst context ty goal_arity =
208 assert (goal_arity >= 0);
209 let rec aux metasenv = function
210 | NCic.Prod (name,s,t) as ty ->
211 let metasenv1, _, arg,_ =
212 NCicMetaSubst.mk_meta ~name:name metasenv context (`WithType s) in
213 let t, metasenv1, args, pno =
214 aux metasenv1 (NCicSubstitution.subst arg t)
216 if pno + 1 = goal_arity then
217 ty, metasenv, [], goal_arity+1
219 t, metasenv1, arg::args, pno+1
221 match NCicReduction.whd ~subst context ty ~delta with
222 | NCic.Prod _ as ty -> aux metasenv ty
223 | _ -> ty, metasenv, [], 0 (* differs from the other impl in this line*)
225 let res, newmetasenv, arguments, _ = aux metasenv ty in
226 res, newmetasenv, arguments
229 let eq_class_of hdb t1 =
231 if NDiscriminationTree.NCicIndexable.path_string_of t1 =
232 [Discrimination_tree.Variable]
234 [] (* if the trie is unable to handle the key, we skip the query since
235 it sould retulr the whole content of the trie *)
237 let candidates = EQDB.retrieve_unifiables (snd hdb#uhint_db) t1 in
238 let candidates = EqSet.elements candidates in
239 let candidates = List.sort (fun (x,_) (y,_) -> compare x y) candidates in
240 List.map snd candidates
242 debug(lazy("eq_class of: " ^ NCicPp.ppterm ~metasenv:[] ~context:[] ~subst:[]
243 t1 ^ " is\n" ^ String.concat "\n"
244 (List.map (NCicPp.ppterm ~subst:[] ~metasenv:[] ~context:[]) eq_class)));
248 let look_for_hint hdb metasenv subst context t1 t2 =
249 debug(lazy ("KEY1: "^NCicPp.ppterm ~metasenv ~subst ~context t1));
250 debug(lazy ("KEY2: "^NCicPp.ppterm ~metasenv ~subst ~context t2));
254 prerr_endline ("ENTRY: " ^
255 NDiscriminationTree.NCicIndexable.string_of_path p ^ " |--> " ^
256 String.concat "|" (List.map (NCicPp.ppterm ~metasenv:[] ~subst:[]
257 ~context:[]) (HintSet.elements ds))));
259 let candidates1 = HDB.retrieve_unifiables (fst hdb#uhint_db) (pair t1 t2) in
260 let candidates2 = HDB.retrieve_unifiables (fst hdb#uhint_db) (pair t2 t1) in
262 List.map (fun (prec,_,_,ty) ->
263 prec,true,saturate ~delta:max_int metasenv subst context ty 0)
264 (HintSet.elements candidates1)
267 List.map (fun (prec,_,_,ty) ->
268 prec,false,saturate ~delta:max_int metasenv subst context ty 0)
269 (HintSet.elements candidates2)
273 (fun (p,b,(t,m,_)) ->
274 let rec aux () (m,l as acc) = function
275 | NCic.Meta _ as t -> acc, t
276 | NCic.LetIn (name,ty,bo,t) ->
278 NCicMetaSubst.mk_meta ~name m context (`WithType ty)in
279 let t = NCicSubstitution.subst i t in
280 aux () (m, (i,bo)::l) t
281 | t -> NCicUntrusted.map_term_fold_a (fun _ () -> ()) () aux acc t
283 let (m,l), t = aux () (m,[]) t in
285 (candidates1 @ candidates2)
290 | (prec,true,(NCic.Appl [_; t1; t2],metasenv,l))-> prec,metasenv,(t1,t2),l
291 | (prec,false,(NCic.Appl [_; t1; t2],metasenv,l))-> prec,metasenv,(t2,t1),l
296 List.sort (fun (x,_,_,_) (y,_,_,_) -> Pervasives.compare x y) rc
298 let rc = List.map (fun (_,x,y,z) -> x,y,z) rc in
300 debug(lazy ("Hints:"^
301 String.concat "\n" (List.map
302 (fun (metasenv, (t1, t2), premises) ->
303 ("\t" ^ String.concat "; "
304 (List.map (fun (a,b) ->
305 NCicPp.ppterm ~margin:max_int ~metasenv ~subst ~context a ^
307 NCicPp.ppterm ~margin:max_int ~metasenv ~subst ~context b)
310 NCicPp.ppterm ~margin:max_int ~metasenv ~subst ~context t1 ^
311 " = "^NCicPp.ppterm ~margin:max_int ~metasenv ~subst ~context t2))
318 let generate_dot_file status fmt =
319 let module Pp = GraphvizPp.Dot in
320 let h_db, _ = status#uhint_db in
321 let names = ref [] in
324 try List.assoc l !names
327 names := (l,"node"^string_of_int!id) :: !names;
330 let nodes = ref [] in
331 let edges = ref [] in
332 HDB.iter h_db (fun _key dataset ->
334 (fun (precedence, l,r, hint) ->
336 Str.global_substitute (Str.regexp "\n") (fun _ -> "")
338 ~margin:max_int ~metasenv:[] ~context:[] ~subst:[] l)
341 Str.global_substitute (Str.regexp "\n") (fun _ -> "")
343 ~margin:max_int ~metasenv:[] ~context:[] ~subst:[] r)
346 string_of_int precedence ^ "..." ^
347 Str.global_substitute (Str.regexp "\n") (fun _ -> "")
349 ~margin:max_int ~metasenv:[] ~context:[] ~subst:[] hint)*)
351 nodes := (mangle l,l) :: (mangle r,r) :: !nodes;
352 edges := (mangle l, mangle r, hint) :: !edges)
353 (HintSet.elements dataset);
355 List.iter (fun x, l -> Pp.node x ~attrs:["label",l] fmt) !nodes;
356 List.iter (fun x, y, l ->
357 Pp.raw (Printf.sprintf "%s -- %s [ label=\"%s\" ];\n" x y "?") fmt)