1 (* Copyright (C) 2005, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
30 if debug then fun x -> prerr_endline (Lazy.force x)
35 | Uri of UriManager.uri
41 type saturations = int
42 type coerced_pos = int
44 coerc_carr * coerc_carr * UriManager.uri * saturations * coerced_pos
46 type coerc_db = (* coercion_entry grouped by carrier with molteplicity *)
47 (coerc_carr * coerc_carr *
48 (UriManager.uri * int * saturations * coerced_pos) list) list
50 let db = ref ([] : coerc_db)
52 let restore coerc_db = db := coerc_db
54 let rec coerc_carr_of_term t a =
57 | Cic.Sort s, 0 -> Sort s
58 | Cic.Appl (t::_), 0 -> coerc_carr_of_term t a
59 | t, 0 -> Uri (CicUtil.uri_of_term t)
61 with Invalid_argument _ -> Dead
64 let string_of_carr = function
65 | Uri u -> UriManager.name_of_uri u
66 | Sort s -> CicPp.ppsort s
67 | Fun i -> "FunClass_" ^ string_of_int i
68 | Dead -> "UnsupportedCarrier"
71 let eq_carr ?(exact=false) src tgt =
74 let coarse_eq = UriManager.eq src tgt in
75 let t = CicUtil.term_of_uri src in
76 let ty,_ = CicTypeChecker.type_of_aux' [] [] t CicUniv.oblivion_ugraph in
78 | Cic.Prod _, true -> false
79 | Cic.Prod _, false -> coarse_eq
81 | Sort (Cic.Type _), Sort (Cic.Type _) -> true
82 | Sort src, Sort tgt when src = tgt -> true
83 | Fun _,Fun _ when not exact -> true (* only one Funclass *)
84 | Fun i,Fun j when i = j -> true (* only one Funclass *)
89 List.map (fun (s,t,l) -> s,t,List.map (fun a,_,b,c -> a,b,c) l) !db
92 let rec myfilter p = function
97 (fun (u,n,saturations,cpos) as e ->
98 if p (s,t,u,saturations,cpos) then
100 else Some (u,n-1,saturations,cpos)
104 if l = [] then myfilter p tl else (s,t,l)::myfilter p tl
107 let remove_coercion p = db := myfilter p !db;;
109 let find_coercion f =
111 (fun (uri,_,saturations,_) -> uri,saturations)
113 (HExtlib.filter_map (fun (s,t,l) -> if f (s,t) then Some l else None) !db))
116 let is_a_coercion t =
118 let uri = CicUtil.uri_of_term t in
122 let xl = List.filter (fun (x,_,_,_) -> UriManager.eq uri x) xl in
123 if xl = [] then None else Some (src,tgt,xl))
127 | (_,_,[])::_ -> assert false
128 | [src,tgt,[u,_,s,p]] -> Some (src,tgt,u,s,p)
129 | (src,tgt,(u,_,s,p)::_)::_ ->
131 (lazy "coercion has multiple entries, returning the first one");
133 with Invalid_argument _ ->
134 debug_print (lazy "this term is not a constant");
138 let add_coercion (src,tgt,u,saturations,cpos) =
139 let f s t = eq_carr s src && eq_carr t tgt in
140 let where = List.filter (fun (s,t,_) -> f s t) !db in
141 let rest = List.filter (fun (s,t,_) -> not (f s t)) !db in
143 | [] -> db := (src,tgt,[u,1,saturations,cpos]) :: !db
145 assert (tl = []); (* not sure, this may be a feature *)
146 if List.exists (fun (x,_,_,_) -> UriManager.eq u x) l then
148 (fun (x,n,x_saturations,x_cpos) as e ->
149 if UriManager.eq u x then
150 (* not sure, this may be a feature *)
151 (assert (x_saturations = saturations && x_cpos = cpos);
152 (x,n+1,saturations,cpos))
156 db := (src,tgt,l)::tl @ rest
158 db := (src,tgt,(u,1,saturations,cpos)::l)::tl @ rest