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;;
54 class virtual status (uid : string option) =
56 inherit NCicEnvironment.status uid
57 val db = HDB.empty, EQDB.empty
59 method set_uhint_db v = {< db = v >}
60 method set_unifhint_status
61 : 'status. #g_status as 'status -> 'self
62 = fun o -> {< db = o#uhint_db >}
65 let dummy = NCic.Const (NReference.reference_of_string "cic:/dummy_conv.dec");;
66 let pair t1 t2 = (NCic.Appl [dummy;t1;t2]) ;;
68 let index_hint status context t1 t2 precedence =
71 | NCic.Meta _ | NCic.Appl (NCic.Meta _ :: _) -> false | _ -> true)
74 | NCic.Meta _ | NCic.Appl (NCic.Meta _ :: _) -> false | _ -> true)
76 (* here we do not use skel_dummy since it could cause an assert false in
77 * the subst function that lives in the kernel *)
78 let hole = NCic.Meta (-1,(0,NCic.Irl 0)) in
80 List.fold_left (fun t _ -> NCicSubstitution.subst status hole t) t1 context
83 List.fold_left (fun t _ -> NCicSubstitution.subst status hole t) t2 context
85 let rec cleanup_skeleton () = function
86 | NCic.Meta _ -> skel_dummy
87 | t -> NCicUtils.map status (fun _ () -> ()) () cleanup_skeleton t
89 let t1_skeleton = cleanup_skeleton () t1_skeleton in
90 let t2_skeleton = cleanup_skeleton () t2_skeleton in
91 let src = pair t1_skeleton t2_skeleton in
92 let ctx2abstractions context t =
96 | NCic.Decl ty -> NCic.Prod (n,ty,t)
97 | NCic.Def (b,ty) -> NCic.LetIn (n,ty,b,t))
101 precedence, t1_skeleton, t2_skeleton, ctx2abstractions context (pair t1 t2)
103 let data_t1 = t2_skeleton in
104 let data_t2 = t1_skeleton in
106 debug(lazy ("INDEXING: " ^
107 status#ppterm ~metasenv:[] ~subst:[] ~context:[] src ^ " |==> " ^
108 status#ppterm ~metasenv:[] ~subst:[] ~context:[]
109 (let _,x,_,_ = data_hint in x)));
111 status#set_uhint_db (
112 HDB.index (fst (status#uhint_db)) src data_hint,
114 (EQDB.index (snd (status#uhint_db)) t2_skeleton (precedence, data_t2))
115 t1_skeleton (precedence, data_t1))
118 let add_user_provided_hint status t precedence =
120 let rec aux ctx = function
122 (match List.rev l with
126 NCicTypeChecker.typeof status ~metasenv:[] ~subst:[] ctx a
129 NCicTypeChecker.typeof status ~metasenv:[] ~subst:[] ctx b
131 NCicReduction.are_convertible status
132 ~metasenv:[] ~subst:[] ctx ty_a ty_b
134 NCicReduction.are_convertible status
135 ~metasenv:[] ~subst:[] ctx a b
137 else raise HintNotValid
139 | NCic.Prod (n,s,t) -> aux ((n, NCic.Decl s) :: ctx) t
140 | NCic.LetIn (n,ty,t,b) -> aux ((n, NCic.Def (t,ty)) :: ctx) b
145 index_hint status c a b precedence
152 (let rec aux = function
153 | u1 :: tl -> List.map (f u1) tl :: aux tl
157 let mk_hint (u1,_,_) (u2,_,_) =
158 let l = OCic2NCic.convert_obj u1
159 (fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph u1)) in
160 let r = OCic2NCic.convert_obj u2
161 (fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph u2)) in
162 match List.hd l,List.hd r with
163 | (_,h1,_,_,NCic.Constant (_,_,Some l,_,_)),
164 (_,h2,_,_,NCic.Constant (_,_,Some r,_,_)) ->
165 let rec aux ctx t1 t2 =
167 | NCic.Lambda (n1,s1,b1), NCic.Lambda(_,s2,b2) ->
168 if NCicReduction.are_convertible ~subst:[] ~metasenv:[] ctx s1 s2
169 then aux ((n1, NCic.Decl s1) :: ctx) b1 b2
172 if NCicReduction.are_convertible ~subst:[] ~metasenv:[] ctx b1 b2
175 function 0 -> [] | n -> NCic.Rel n :: mk_rels (n-1)
178 NCic.Appl (NCic.Const(OCic2NCic.reference_of_ouri
179 u1 (NReference.Def h1)) :: mk_rels (List.length ctx))
182 NCic.Appl (NCic.Const(OCic2NCic.reference_of_ouri
183 u2 (NReference.Def h2)) :: mk_rels (List.length ctx))
185 [ctx,b1,b2; ctx,b1,n2; ctx,n1,b2; ctx,n1,n2]
195 if List.length l > 1 then
198 [] (CoercDb.to_list (CoercDb.dump ()))
200 prerr_endline "MISTERO";
204 | (ctx,b1,b2) -> index_hint db ctx b1 b2 0)
205 !user_db (List.flatten hints)
210 let saturate status ?(delta=0) metasenv subst context ty goal_arity =
211 assert (goal_arity >= 0);
212 let rec aux metasenv = function
213 | NCic.Prod (name,s,t) as ty ->
214 let metasenv1, _, arg,_ =
215 NCicMetaSubst.mk_meta ~attrs:[`Name name] metasenv context
218 let t, metasenv1, args, pno =
219 aux metasenv1 (NCicSubstitution.subst status arg t)
221 if pno + 1 = goal_arity then
222 ty, metasenv, [], goal_arity+1
224 t, metasenv1, arg::args, pno+1
226 match NCicReduction.whd status ~subst context ty ~delta with
227 | NCic.Prod _ as ty -> aux metasenv ty
228 | _ -> ty, metasenv, [], 0 (* differs from the other impl in this line*)
230 let res, newmetasenv, arguments, _ = aux metasenv ty in
231 res, newmetasenv, arguments
234 let eq_class_of (status:#status) t1 =
236 if NDiscriminationTree.NCicIndexable.path_string_of t1 =
237 [Discrimination_tree.Variable]
239 [] (* if the trie is unable to handle the key, we skip the query since
240 it sould retulr the whole content of the trie *)
242 let candidates = EQDB.retrieve_unifiables (snd status#uhint_db) t1 in
243 let candidates = EqSet.elements candidates in
244 let candidates = List.sort (fun (x,_) (y,_) -> compare x y) candidates in
245 List.map snd candidates
247 debug(lazy("eq_class of: " ^ status#ppterm ~metasenv:[] ~context:[] ~subst:[]
248 t1 ^ " is\n" ^ String.concat "\n"
249 (List.map (status#ppterm ~metasenv:[] ~context:[] ~subst:[]) eq_class)));
253 let look_for_hint (status:#status) metasenv subst context t1 t2 =
254 if NDiscriminationTree.NCicIndexable.path_string_of t1 =
255 [Discrimination_tree.Variable] ||
256 NDiscriminationTree.NCicIndexable.path_string_of t2 =
257 [Discrimination_tree.Variable] then [] else begin
259 debug(lazy ("KEY1: "^status#ppterm ~metasenv ~subst ~context t1));
260 debug(lazy ("KEY2: "^status#ppterm ~metasenv ~subst ~context t2));
264 prerr_endline ("ENTRY: " ^
265 NDiscriminationTree.NCicIndexable.string_of_path p ^ " |--> " ^
266 String.concat "|" (List.map (status#ppterm ~metasenv:[] ~subst:[]
267 ~context:[]) (HintSet.elements ds))));
269 let candidates1 = HDB.retrieve_unifiables (fst status#uhint_db) (pair t1 t2) in
270 let candidates2 = HDB.retrieve_unifiables (fst status#uhint_db) (pair t2 t1) in
272 List.map (fun (prec,_,_,ty) ->
273 prec,true,saturate status ~delta:max_int metasenv subst context ty 0)
274 (HintSet.elements candidates1)
277 List.map (fun (prec,_,_,ty) ->
278 prec,false,saturate status ~delta:max_int metasenv subst context ty 0)
279 (HintSet.elements candidates2)
283 (fun (p,b,(t,m,_)) ->
284 let rec aux () (m,l as acc) = function
285 | NCic.Meta _ as t -> acc, t
286 | NCic.LetIn (name,ty,bo,t) ->
288 NCicMetaSubst.mk_meta ~attrs:[`Name name] m context
289 ~with_type:ty `IsTerm
291 let t = NCicSubstitution.subst status i t in
292 aux () (m, (i,bo)::l) t
293 | t -> NCicUntrusted.map_term_fold_a status (fun _ () -> ()) () aux acc t
295 let (m,l), t = aux () (m,[]) t in
297 (candidates1 @ candidates2)
302 | (prec,true,(NCic.Appl [_; t1; t2],metasenv,l))-> prec,metasenv,(t1,t2),l
303 | (prec,false,(NCic.Appl [_; t1; t2],metasenv,l))-> prec,metasenv,(t2,t1),l
308 List.sort (fun (x,_,_,_) (y,_,_,_) -> Pervasives.compare x y) rc
310 let rc = List.map (fun (_,x,y,z) -> x,y,z) rc in
312 debug(lazy ("Hints:"^
313 String.concat "\n" (List.map
314 (fun (metasenv, (t1, t2), premises) ->
315 ("\t" ^ String.concat "; "
316 (List.map (fun (a,b) ->
317 status#ppterm ~margin:max_int ~metasenv ~subst ~context a ^
319 status#ppterm ~margin:max_int ~metasenv ~subst ~context b)
322 status#ppterm ~margin:max_int ~metasenv ~subst ~context t1 ^
323 " = "^status#ppterm ~margin:max_int ~metasenv ~subst ~context t2))
332 let rec aux ctx = function
333 | NCic.Prod (name, ty, rest) -> aux ((name, NCic.Decl ty) :: ctx) rest
338 let recproblems, concl =
339 let rec aux ctx = function
340 | NCic.LetIn (name,ty,bo,rest) -> aux ((name, NCic.Def(bo,ty))::ctx) rest
345 let buff = Buffer.create 100 in
346 let fmt = Format.formatter_of_buffer buff in
350 (* pp_ctx [] context *)
354 (* pp_ctx context recproblems *)
356 F.fprintf "\vdash@;";
357 status#ppterm ~fmt ~context:(recproblems@context) ~subst:[] ~metasenv:[];
359 F.fprintf formatter "@?";
360 prerr_endline (Buffer.contents buff);
365 let generate_dot_file (status:#status) fmt =
366 let module Pp = GraphvizPp.Dot in
367 let h_db, _ = status#uhint_db in
368 let names = ref [] in
371 try List.assoc l !names
374 names := (l,"node"^string_of_int!id) :: !names;
377 let nodes = ref [] in
378 let edges = ref [] in
379 HDB.iter h_db (fun _key dataset ->
381 (fun (precedence, l,r, hint) ->
383 Str.global_substitute (Str.regexp "\n") (fun _ -> "")
385 ~margin:max_int ~metasenv:[] ~context:[] ~subst:[] l)
388 Str.global_substitute (Str.regexp "\n") (fun _ -> "")
390 ~margin:max_int ~metasenv:[] ~context:[] ~subst:[] r)
393 string_of_int precedence ^ "..." ^
394 Str.global_substitute (Str.regexp "\n") (fun _ -> "")
396 ~margin:max_int ~metasenv:[] ~context:[] ~subst:[] hint)*)
398 nodes := (mangle l,l) :: (mangle r,r) :: !nodes;
399 edges := (mangle l, mangle r, shint, precedence, hint) :: !edges)
400 (HintSet.elements dataset);
402 List.iter (fun x, l -> Pp.node x ~attrs:["label",l] fmt) !nodes;
403 List.iter (fun x, y, l, _, _ ->
404 Pp.raw (Printf.sprintf "%s -- %s [ label=\"%s\" ];\n" x y "?") fmt)
406 edges := List.sort (fun (_,_,_,p1,_) (_,_,_,p2,_) -> p1 - p2) !edges;
407 List.iter (fun x, y, _, p, l -> pp_hint l p) !edges;