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
45 (* BUG: this should be the real arity. The computation
46 requires menv, context etc.., but since carrs are compared discharging Fun
49 | t -> Uri (CicUtil.uri_of_term t)
50 with Invalid_argument _ ->
54 let uri_of_carr = function
58 let rec name_of_carr = function
59 | Uri u -> UriManager.name_of_uri u
60 | Sort s -> CicPp.ppsort s
61 | Term (Cic.Appl ((Cic.Const (uri, _))::_))
62 | Term (Cic.Appl ((Cic.MutInd (uri, _, _))::_))
63 | Term (Cic.Appl ((Cic.MutConstruct (uri, _, _, _))::_)) ->
64 UriManager.name_of_uri uri
65 | Term (Cic.Prod (_,_,t)) -> name_of_carr (Term t)
67 prerr_endline ("CoercDb.name_of_carr:" ^ CicPp.ppterm t);
69 | Fun i -> "FunClass_" ^ string_of_int i
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 (Cic.Type _), Sort (Cic.Type _) -> true
83 | Sort src, Sort tgt when src = tgt -> true
87 if CicUtil.is_meta_closed t1 && CicUtil.is_meta_closed t2 then
90 (lazy ("Unsupported carr for coercions: " ^
91 CicPp.ppterm t1 ^ " or " ^ CicPp.ppterm t2)))
92 else raise EqCarrOnNonMetaClosed
93 | Fun _,Fun _ -> true (* only one Funclass? *)
94 (* | Fun i,Fun j when i=j -> true *)
96 | _, Term t when not (CicUtil.is_meta_closed t) -> raise EqCarrOnNonMetaClosed
101 List.map (fun (s,t,l) -> s,t,List.map (fun a,_,b -> a,b) l) !db
104 let rec myfilter p = function
109 (fun (u,n,saturations) ->
110 if p (s,t,u,saturations) then
114 Some (u,n-1,saturations)
116 Some (u,n,saturations))
119 if l = [] then myfilter p tl else (s,t,l)::myfilter p tl
122 let remove_coercion p = db := myfilter p !db;;
124 let find_coercion f =
126 (fun uri,_,saturations -> uri,saturations)
128 (HExtlib.filter_map (fun (s,t,l) -> if f (s,t) then Some l else None) !db))
135 (fun (_,_,xl) -> List.exists (fun (x,_,_) -> UriManager.eq uri x) xl)
139 with Not_found -> assert false (* uri must be a coercion *)
142 let is_a_coercion u =
144 (fun (_,_,xl) -> List.exists (fun (x,_,_) -> UriManager.eq u x) xl)
148 let is_a_coercion' t =
150 let uri = CicUtil.uri_of_term t in
152 with Invalid_argument _ -> false
155 let is_a_coercion_to_funclass t =
157 let uri = CicUtil.uri_of_term t in
158 match snd (get_carr uri) with
161 with Invalid_argument _ -> None
164 let term_of_carr = function
165 | Uri u -> CicUtil.term_of_uri u
166 | Sort s -> Cic.Sort s
167 | Fun _ -> assert false
168 | Term _ -> assert false
171 let add_coercion (src,tgt,u,saturations) =
172 let f s t = eq_carr s src && eq_carr t tgt in
173 let where = List.filter (fun (s,t,_) -> f s t) !db in
174 let rest = List.filter (fun (s,t,_) -> not (f s t)) !db in
176 | [] -> db := (src,tgt,[u,1,saturations]) :: !db
178 assert (tl = []); (* not sure, this may be a feature *)
179 if List.exists (fun (x,_,_) -> UriManager.eq u x) l then
181 (fun (x,n,saturations') ->
182 if UriManager.eq u x then
188 db := (src,tgt,l')::tl @ rest
190 db := (src,tgt,(u,1,saturations)::l)::tl @ rest