]> matita.cs.unibo.it Git - helm.git/blob - components/library/coercDb.ml
Coercions are now generalized to the general form
[helm.git] / components / library / coercDb.ml
1 (* Copyright (C) 2005, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 (* $Id$ *)
27
28 type coerc_carr = 
29   | Uri of UriManager.uri 
30   | Sort of Cic.sort 
31   | Term of Cic.term
32   | Fun of int 
33 ;;
34
35 exception EqCarrNotImplemented of string Lazy.t
36 exception EqCarrOnNonMetaClosed
37
38 let db = ref []
39
40 let coerc_carr_of_term t =
41  try
42   match t with
43    | Cic.Sort s -> Sort s
44    | Cic.Prod _ -> assert false
45    | Cic.Appl (t::_)
46    | t -> Uri (CicUtil.uri_of_term t)
47  with Invalid_argument _ ->
48   Term t
49 ;;
50
51 let uri_of_carr = function
52   | Uri u -> Some u
53   | _ -> None
54
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)
63   | Term t -> 
64       prerr_endline ("CoercDb.name_of_carr:" ^ CicPp.ppterm t); 
65       "FixTheNameMangler"
66   | Fun i -> "FunClass_" ^ string_of_int i   
67 ;;
68
69 let eq_carr ?(exact=false) src tgt =
70   match src, tgt with
71   | Uri src, Uri tgt -> 
72       let coarse_eq = UriManager.eq src tgt in
73       let src_noxpointer = UriManager.strip_xpointer src in
74       if exact && coarse_eq && UriManager.uri_is_ind src_noxpointer then
75         match 
76           fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph src_noxpointer)
77         with
78         | Cic.InductiveDefinition (_,[],m,_) when m = 0 -> true
79         | Cic.Constant _ -> true
80         | _ -> false
81       else
82         coarse_eq
83   | Sort (Cic.Type _), Sort (Cic.Type _) -> true
84   | Sort src, Sort tgt when src = tgt -> true
85   | Term t1, Term t2 ->
86       if t1 = t2 then true
87       else
88         if CicUtil.is_meta_closed t1 && CicUtil.is_meta_closed t2 then
89           raise 
90           (EqCarrNotImplemented 
91           (lazy ("Unsupported carr for coercions: " ^ 
92           CicPp.ppterm t1 ^ " or " ^ CicPp.ppterm t2)))
93       else raise EqCarrOnNonMetaClosed
94   | Fun _,Fun _ -> true (* only one Funclass? *)
95 (*   | Fun i,Fun j when i=j -> true *)
96   | Term t, _
97   | _, Term t when not (CicUtil.is_meta_closed t) -> raise EqCarrOnNonMetaClosed
98   | _, _ -> false
99 ;;
100
101 let to_list () =
102   List.map (fun (s,t,l) -> s,t,List.map (fun a,_,b -> a,b) l) !db
103 ;;
104
105 let rec myfilter p = function
106   | [] -> []
107   | (s,t,l)::tl ->
108       let l = 
109         HExtlib.filter_map 
110           (fun (u,n,saturations) -> 
111             if p (s,t,u,saturations) then
112               if n = 1 then
113                 None
114               else
115                 Some (u,n-1,saturations)
116             else
117               Some (u,n,saturations)) 
118           l 
119       in
120       if l = [] then myfilter p tl else (s,t,l)::myfilter p tl
121 ;;
122   
123 let remove_coercion p = db := myfilter p !db;;
124
125 let find_coercion f =
126     List.map
127     (fun uri,_,saturations -> uri,saturations)
128     (List.flatten
129     (HExtlib.filter_map (fun (s,t,l) -> if f (s,t) then Some l else None) !db))
130 ;;
131
132 let get_carr uri =
133   try
134     let src, tgt, _ = 
135       List.find 
136         (fun (_,_,xl) -> List.exists (fun (x,_,_) -> UriManager.eq uri x) xl) 
137         !db 
138     in
139     src, tgt
140   with Not_found -> assert false (* uri must be a coercion *)
141 ;;
142
143 let is_a_coercion u = 
144   List.exists 
145     (fun (_,_,xl) -> List.exists (fun (x,_,_) -> UriManager.eq u x) xl) 
146     !db
147 ;;
148
149 let is_a_coercion' t = 
150   try
151     let uri = CicUtil.uri_of_term t in
152      is_a_coercion uri
153   with Invalid_argument _ -> false
154 ;;
155
156 let is_a_coercion_to_funclass t =
157   try
158     let uri = CicUtil.uri_of_term t in
159     match snd (get_carr uri) with
160     | Fun i -> Some i
161     | _ -> None
162   with Invalid_argument _ -> None
163   
164
165 let term_of_carr = function
166   | Uri u -> CicUtil.term_of_uri u
167   | Sort s -> Cic.Sort s
168   | Fun _ -> assert false
169   | Term _ -> assert false
170 ;;
171   
172 let add_coercion (src,tgt,u,saturations) =
173   let f s t = eq_carr s src && eq_carr t tgt in
174   let where = List.filter (fun (s,t,_) -> f s t) !db in
175   let rest = List.filter (fun (s,t,_) -> not (f s t)) !db in
176   match where with
177   | [] -> db := (src,tgt,[u,1,saturations]) :: !db
178   | (src,tgt,l)::tl ->
179       assert (tl = []); (* not sure, this may be a feature *)
180       if List.exists (fun (x,_,_) -> UriManager.eq u x) l then
181         let l' = List.map
182           (fun (x,n,saturations') ->
183             assert (saturations=saturations');
184             if UriManager.eq u x then
185              (x,n+1,saturations)
186             else
187              (x,n,saturations))
188           l
189         in
190         db := (src,tgt,l')::tl @ rest
191       else
192         db := (src,tgt,(u,1,saturations)::l)::tl @ rest
193       
194 ;;