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 let convert_term = ref (fun _ _ -> assert false);;
18 let set_convert_term f = convert_term := f;;
20 module COT : Set.OrderedType
21 with type t = string * NCic.term * int * int * NCic.term *
24 type t = string * NCic.term * int * int * NCic.term * NCic.term
25 let compare = Pervasives.compare
28 module CoercionSet = Set.Make(COT)
31 Discrimination_tree.Make(NDiscriminationTree.NCicIndexable)(CoercionSet)
35 let empty_db = DB.empty,DB.empty
39 inherit NCicUnifHint.g_status
45 inherit NCicUnifHint.status
48 method set_coerc_db v = {< db = v >}
49 method set_coercion_status
50 : 'status. #g_status as 'status -> 'self
51 = fun o -> {< db = o#coerc_db >}#set_unifhint_status o
54 let index_coercion status name c src tgt arity arg =
55 let db_src,db_tgt = status#coerc_db in
56 let data = (name,c,arity,arg,src,tgt) in
57 debug (lazy ("INDEX:" ^
58 NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] src ^ " ===> " ^
59 NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] tgt ^ " := " ^
60 NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] c ^ " " ^
61 string_of_int arg ^ " " ^ string_of_int arity));
62 let db_src = DB.index db_src src data in
63 let db_tgt = DB.index db_tgt tgt data in
64 status#set_coerc_db (db_src, db_tgt)
67 let index_old_db odb (status : #status) =
69 (fun status (_,tgt,clist) ->
71 (fun status (uri,_,arg) ->
73 let c=fst (!convert_term uri (CicUtil.term_of_uri uri)) in
74 let arity = match tgt with | CoercDb.Fun i -> i | _ -> 0 in
76 let cty = NCicTypeChecker.typeof ~subst:[] ~metasenv:[] [] c in
77 let scty, metasenv,_ =
78 NCicMetaSubst.saturate ~delta:max_int [] [] [] cty (arity+1)
81 | NCic.Prod (_, src, tgt) ->
83 NCicSubstitution.subst (NCic.Meta (-1,(0,NCic.Irl 0))) tgt
86 debug (lazy (Printf.sprintf "indicizzo %s (%d)) : %s ===> %s"
87 (NCicPp.ppterm ~metasenv ~subst:[] ~context:[] scty) (arity+1)
88 (NCicPp.ppterm ~metasenv ~subst:[] ~context:[] src)
89 (NCicPp.ppterm ~metasenv ~subst:[] ~context:[] tgt));
94 NCicPp.ppterm ~metasenv ~subst:[] ~context:[] t));
97 index_coercion status "foo" c src tgt arity arg
99 | NCicEnvironment.BadDependency _
100 | NCicTypeChecker.TypeCheckerFailure _ -> status)
102 status (CoercDb.to_list odb)
105 let look_for_coercion status metasenv subst context infty expty =
106 let db_src,db_tgt = status#coerc_db in
108 NCicUntrusted.apply_subst subst context infty,
109 NCicUntrusted.apply_subst subst context expty
111 | (NCic.Meta _ | NCic.Appl (NCic.Meta _::_)),
112 (NCic.Meta _ | NCic.Appl (NCic.Meta _::_)) -> []
115 debug (lazy ("LOOK FOR COERCIONS: " ^
116 NCicPp.ppterm ~metasenv ~subst ~context infty ^ " |===> " ^
117 NCicPp.ppterm ~metasenv ~subst ~context expty));
119 let src_class = infty :: NCicUnifHint.eq_class_of status infty in
120 let tgt_class = expty :: NCicUnifHint.eq_class_of status expty in
125 CoercionSet.union (DB.retrieve_unifiables db_src infty) set)
126 CoercionSet.empty src_class
131 CoercionSet.union (DB.retrieve_unifiables db_tgt expty) set)
132 CoercionSet.empty tgt_class
135 debug (lazy ("CANDIDATES SRC: " ^
136 String.concat "," (List.map (fun (name,t,_,_,_,_) ->
137 name ^ " :: " ^ NCicPp.ppterm ~metasenv ~subst ~context t)
138 (CoercionSet.elements set_src))));
139 debug (lazy ("CANDIDATES TGT: " ^
140 String.concat "," (List.map (fun (name,t,_,_,_,_) ->
141 name ^ " :: " ^ NCicPp.ppterm ~metasenv ~subst ~context t)
142 (CoercionSet.elements set_tgt))));
144 let candidates = CoercionSet.inter set_src set_tgt in
146 debug (lazy ("CANDIDATES: " ^
147 String.concat "," (List.map (fun (name,t,_,_,_,_) ->
148 name ^ " :: " ^ NCicPp.ppterm ~metasenv ~subst ~context t)
149 (CoercionSet.elements candidates))));
152 (fun (name,t,arity,arg,_,_) ->
154 try NCicTypeChecker.typeof ~metasenv:[] ~subst:[] [] t
155 with NCicTypeChecker.TypeCheckerFailure s ->
156 prerr_endline ("illtyped coercion: "^Lazy.force s);
157 prerr_endline (NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] t);
160 let ty, metasenv, args =
161 NCicMetaSubst.saturate ~delta:max_int metasenv subst context ty arity
165 NCicPp.ppterm ~metasenv ~subst:[] ~context:[] ty ^ " --- " ^
166 NCicPp.ppterm ~metasenv ~subst ~context
167 (NCicUntrusted.mk_appl t args) ^ " --- " ^
168 string_of_int (List.length args) ^ " == " ^ string_of_int arg));
170 name,metasenv, NCicUntrusted.mk_appl t args, ty, List.nth args arg)
171 (CoercionSet.elements candidates)
174 (* CSC: very inefficient implementation!
175 Enrico, can we use a discrimination tree here? *)
176 let match_coercion status ~metasenv ~subst ~context t =
178 DB.fold (fst (status#coerc_db)) (fun _ v l -> (CoercionSet.elements v)@l) []
180 (HExtlib.list_findopt
181 (fun (_,p,arity,cpos,_,_) _ ->
185 NCic.Appl lp, NCic.Appl lt ->
186 (match fst (HExtlib.split_nth (List.length lp) lt) with
189 | _,NCic.Appl (he::_) -> he
192 let b = NCicReduction.alpha_eq metasenv subst context p t in
193 if not b then None else
194 let ty = NCicTypeChecker.typeof ~metasenv:[] ~subst:[] [] p in
196 let rec aux = function NCic.Prod (_,_,t) -> 1+aux t | _ -> 0 in
199 Some (p,pis - arity - cpos - 1,cpos)
201 Failure _ -> None (* raised by split_nth *)
205 let generate_dot_file status fmt =
206 let module Pp = GraphvizPp.Dot in
207 let src_db, _ = status#coerc_db in
208 let edges = ref [] in
209 DB.iter src_db (fun _ dataset ->
212 (fun (name,t,a,g,sk,dk) ->
213 debug(lazy (let p = NCicPp.ppterm ~metasenv:[] ~context:[]
214 ~subst:[] in p t ^ " ::: " ^ p sk ^ " |--> " ^ p dk));
215 let eq_s= sk::NCicUnifHint.eq_class_of status sk in
216 let eq_t= dk::NCicUnifHint.eq_class_of status dk in
217 (name,t,a,g),eq_s,eq_t
219 (CoercionSet.elements dataset);
231 let names = ref [] in
234 try List.assoc l !names
237 names := (l,"node"^string_of_int!id) :: !names;
243 ~attrs:["label",String.concat "\\n"
245 NCicPp.ppterm ~metasenv:[] ~subst:[]
246 ~context:[] t ~margin:max_int
251 (fun ((name,_,_,_),src,tgt) ->
252 Pp.edge (mangle src) (mangle tgt)
253 ~attrs:["label", name] fmt)