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)
27 let dummy = NCic.Const (NReference.reference_of_string "cic:/dummy_conv.dec");;
28 let pair t1 t2 = (NCic.Appl [dummy;t1;t2]) ;;
30 let index_hint hdb context t1 t2 precedence =
33 | NCic.Meta _ | NCic.Appl (NCic.Meta _ :: _) -> false | _ -> true)
36 | NCic.Meta _ | NCic.Appl (NCic.Meta _ :: _) -> false | _ -> true)
38 let pair' = pair t1 t2 in
43 | NCic.Decl ty -> NCic.Prod (n,ty,t)
44 | NCic.Def (b,ty) -> NCic.LetIn (n,ty,b,t))
51 NCicSubstitution.subst (NCic.Meta (-1,(0,NCic.Irl 0))) t
53 NCicSubstitution.subst (NCic.Meta (-1,(0,NCic.Irl 0))) t)
56 prerr_endline ("INDEXING: " ^
57 NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] src ^ " |==> " ^
58 NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] data);
60 DB.index hdb src (precedence, data)
63 let empty_db = DB.empty ;;
65 let user_db = ref empty_db ;;
67 let add_user_provided_hint t precedence =
68 let u = UriManager.uri_of_string "cic:/foo/bar.con" in
70 let rec aux ctx = function
72 (match List.rev l with
73 | b::a::_ -> ctx, a, b
75 | NCic.Prod (n,s,t) ->
76 aux ((n, NCic.Decl s) :: ctx) t
77 | NCic.LetIn (n,ty,t,b) ->
78 aux ((n, NCic.Def (t,ty)) :: ctx) b
81 aux [] (fst (OCic2NCic.convert_term u t))
83 user_db := index_hint !user_db c a b precedence
89 (let rec aux = function
90 | u1 :: tl -> List.map (f u1) tl :: aux tl
94 let mk_hint (u1,_,_) (u2,_,_) =
95 let l = OCic2NCic.convert_obj u1
96 (fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph u1)) in
97 let r = OCic2NCic.convert_obj u2
98 (fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph u2)) in
99 match List.hd l,List.hd r with
100 | (_,h1,_,_,NCic.Constant (_,_,Some l,_,_)),
101 (_,h2,_,_,NCic.Constant (_,_,Some r,_,_)) ->
102 let rec aux ctx t1 t2 =
104 | NCic.Lambda (n1,s1,b1), NCic.Lambda(_,s2,b2) ->
105 if NCicReduction.are_convertible ~subst:[] ~metasenv:[] ctx s1 s2
106 then aux ((n1, NCic.Decl s1) :: ctx) b1 b2
109 if NCicReduction.are_convertible ~subst:[] ~metasenv:[] ctx b1 b2
112 prerr_endline ("hint: " ^ NCicPp.ppterm ~metasenv:[] ~subst:[]
113 ~context:ctx b1 ^ " === " ^ NCicPp.ppterm ~metasenv:[]
114 ~subst:[] ~context:ctx b2);
117 function 0 -> [] | n -> NCic.Rel n :: mk_rels (n-1)
120 NCic.Appl (NCic.Const(OCic2NCic.reference_of_ouri
121 u1 (NReference.Def h1)) :: mk_rels (List.length ctx))
124 NCic.Appl (NCic.Const(OCic2NCic.reference_of_ouri
125 u2 (NReference.Def h2)) :: mk_rels (List.length ctx))
127 [ctx,b1,b2; ctx,b1,n2; ctx,n1,b2; ctx,n1,n2]
137 if List.length l > 1 then
140 [] (CoercDb.to_list ())
144 | (ctx,b1,b2) -> index_hint db ctx b1 b2 0)
145 !user_db (List.flatten hints)
149 let look_for_hint hdb metasenv subst context t1 t2 =
151 prerr_endline ("KEY1: "^NCicPp.ppterm ~metasenv ~subst ~context (pair t1 t2));
152 prerr_endline ("KEY2: "^NCicPp.ppterm ~metasenv ~subst ~context (pair t2 t1));
155 prerr_endline ("ENTRY: " ^
156 NDiscriminationTree.NCicIndexable.string_of_path p ^ " |--> " ^
157 String.concat "|" (List.map (NCicPp.ppterm ~metasenv:[] ~subst:[]
158 ~context:[]) (HintSet.elements ds))));
160 let candidates1 = DB.retrieve_unifiables hdb (pair t1 t2) in
161 let candidates2 = DB.retrieve_unifiables hdb (pair t2 t1) in
163 List.map (fun (prec,ty) ->
164 prec,true,NCicMetaSubst.saturate ~delta:max_int metasenv subst context ty 0)
165 (HintSet.elements candidates1)
168 List.map (fun (prec,ty) ->
169 prec,false,NCicMetaSubst.saturate ~delta:max_int metasenv subst context ty 0)
170 (HintSet.elements candidates2)
175 | (prec,true,(NCic.Appl [_; t1; t2],metasenv,_)) -> prec,metasenv, t1, t2
176 | (prec,false,(NCic.Appl [_; t1; t2],metasenv,_)) -> prec,metasenv, t2, t1
178 (candidates1 @ candidates2)
181 List.sort (fun (x,_,_,_) (y,_,_,_) -> Pervasives.compare x y) rc
183 let rc = List.map (fun (_,x,y,z) -> x,y,z) rc in
186 (fun (metasenv, t1, t2) ->
188 ("CAND: "^NCicPp.ppterm ~metasenv ~subst ~context t1 ^
189 " == "^NCicPp.ppterm ~metasenv ~subst ~context t2))