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/
29 | Uri of UriManager.uri
35 exception EqCarrNotImplemented of string Lazy.t
36 exception EqCarrOnNonMetaClosed
40 let coerc_carr_of_term t =
43 | Cic.Sort s -> Sort s
46 | t -> Uri (CicUtil.uri_of_term t)
47 with Invalid_argument _ ->
51 let uri_of_carr = function
55 let rec name_of_carr = function
56 | Uri u -> UriManager.name_of_uri u
57 | Sort s -> CicPp.ppsort s
58 | Term (Cic.Appl ((Cic.Const (uri, _))::_))
59 | Term (Cic.Appl ((Cic.MutInd (uri, _, _))::_))
60 | Term (Cic.Appl ((Cic.MutConstruct (uri, _, _, _))::_)) ->
61 UriManager.name_of_uri uri
62 | Term (Cic.Prod (_,_,t)) -> name_of_carr (Term t)
64 prerr_endline ("CoercDb.name_of_carr:" ^ CicPp.ppterm t);
66 | Fun i -> "FunClass_" ^ string_of_int i
71 | Uri src, Uri tgt -> UriManager.eq src tgt
72 | Sort (Cic.Type _), Sort (Cic.Type _) -> true
73 | Sort src, Sort tgt when src = tgt -> true
77 if CicUtil.is_meta_closed t1 && CicUtil.is_meta_closed t2 then
80 (lazy ("Unsupported carr for coercions: " ^
81 CicPp.ppterm t1 ^ " or " ^ CicPp.ppterm t2)))
82 else raise EqCarrOnNonMetaClosed
83 | Fun _,Fun _ -> true (* only one Funclass? *)
84 (* | Fun i,Fun j when i=j -> true *)
86 | _, Term t when not (CicUtil.is_meta_closed t) -> raise EqCarrOnNonMetaClosed
91 List.map (fun (s,t,l) -> s,t,List.map fst l) !db
94 let rec myfilter p = function
109 if l = [] then myfilter p tl else (s,t,l)::myfilter p tl
112 let remove_coercion p = db := myfilter p !db;;
114 let find_coercion f =
118 (HExtlib.filter_map (fun (s,t,l) -> if f (s,t) then Some l else None) !db))
125 (fun (_,_,xl) -> List.exists (fun (x,_) -> UriManager.eq uri x) xl)
129 with Not_found -> assert false (* uri must be a coercion *)
132 let is_a_coercion u =
134 (fun (_,_,xl) -> List.exists (fun (x,_) -> UriManager.eq u x) xl)
138 let is_a_coercion' t =
140 let uri = CicUtil.uri_of_term t in
142 with Invalid_argument _ -> false
145 let is_a_coercion_to_funclass t =
147 let uri = CicUtil.uri_of_term t in
148 match snd (get_carr uri) with
151 with Invalid_argument _ -> None
154 let term_of_carr = function
155 | Uri u -> CicUtil.term_of_uri u
156 | Sort s -> Cic.Sort s
157 | Fun _ -> assert false
158 | Term _ -> assert false
161 let add_coercion (src,tgt,u) =
162 let f s t = eq_carr s src && eq_carr t tgt in
163 let where = List.filter (fun (s,t,_) -> f s t) !db in
164 let rest = List.filter (fun (s,t,_) -> not (f s t)) !db in
166 | [] -> db := (src,tgt,[u,1]) :: !db
168 assert (tl = []); (* not sure, this may be a feature *)
169 if List.exists (fun (x,_) -> UriManager.eq u x) l then
171 (fun (x,n) -> if UriManager.eq u x then (x,n+1) else (x,n))
174 db := (src,tgt,l')::tl @ rest
176 db := (src,tgt,(u,1)::l)::tl @ rest