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
53 let empty_coerc_db = []
55 let rec coerc_carr_of_term t a =
58 | Cic.Sort s, 0 -> Sort s
59 | Cic.Appl (t::_), 0 -> coerc_carr_of_term t a
60 | t, 0 -> Uri (CicUtil.uri_of_term t)
62 with Invalid_argument _ -> Dead
65 let string_of_carr = function
66 | Uri u -> UriManager.name_of_uri u
67 | Sort s -> CicPp.ppsort s
68 | Fun i -> "FunClass_" ^ string_of_int i
69 | Dead -> "UnsupportedCarrier"
72 let eq_carr ?(exact=false) src tgt =
75 let coarse_eq = UriManager.eq src tgt in
76 let t = CicUtil.term_of_uri src in
77 let ty,_ = CicTypeChecker.type_of_aux' [] [] t CicUniv.oblivion_ugraph in
79 | Cic.Prod _, true -> false
80 | Cic.Prod _, false -> coarse_eq
82 | Sort _, Sort _ -> 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
149 (* this code reorders the list so that adding an already declared
150 * coercion moves it to the begging of the list *)
151 let item = List.find (fun (x,_,_,_) -> UriManager.eq u x) l in
152 let rest=List.filter (fun (x,_,_,_) -> not (UriManager.eq u x)) l in
156 (fun (x,n,x_saturations,x_cpos) as e ->
157 if UriManager.eq u x then
158 (* not sure, this may be a feature *)
159 (assert (x_saturations = saturations && x_cpos = cpos);
160 (x,n+1,saturations,cpos))
164 db := (src,tgt,l)::tl @ rest
166 db := (src,tgt,(u,1,saturations,cpos)::l)::tl @ rest
171 let lb,la = List.partition (fun (uri,_,_,_) -> UriManager.eq uri u) l in
174 db := List.map prefer !db;