]> matita.cs.unibo.it Git - helm.git/blob - components/library/coercDb.ml
14ddf7c8631b5054aca10a8accc0268e9c12b05f
[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 _ -> Fun 0 
45      (* BUG: this should be the real arity. The computation
46       requires menv, context etc.., but since carrs are compared discharging Fun
47       arity... it works *)
48    | Cic.Appl (t::_)
49    | t -> Uri (CicUtil.uri_of_term t)
50  with Invalid_argument _ ->
51   Term t
52 ;;
53
54 let uri_of_carr = function
55   | Uri u -> Some u
56   | _ -> None
57
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)
66   | Term t -> 
67       prerr_endline ("CoercDb.name_of_carr:" ^ CicPp.ppterm t); 
68       "FixTheNameMangler"
69   | Fun i -> "FunClass_" ^ string_of_int i   
70 ;;
71
72 let eq_carr ?(exact=false) src tgt =
73   match src, tgt with
74   | Uri src, Uri tgt -> 
75       let coarse_eq = UriManager.eq src tgt in
76       let src_noxpointer = UriManager.strip_xpointer src in
77       if exact && coarse_eq && UriManager.uri_is_ind src_noxpointer then
78         match 
79           fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph src_noxpointer)
80         with
81         | Cic.InductiveDefinition (_,[],m,_) when m = 0 -> true
82         | Cic.Constant _ -> true
83         | _ -> false
84       else
85         coarse_eq
86   | Sort (Cic.Type _), Sort (Cic.Type _) -> true
87   | Sort src, Sort tgt when src = tgt -> true
88   | Term t1, Term t2 ->
89       if t1 = t2 then true
90       else
91         if CicUtil.is_meta_closed t1 && CicUtil.is_meta_closed t2 then
92           raise 
93           (EqCarrNotImplemented 
94           (lazy ("Unsupported carr for coercions: " ^ 
95           CicPp.ppterm t1 ^ " or " ^ CicPp.ppterm t2)))
96       else raise EqCarrOnNonMetaClosed
97   | Fun _,Fun _ -> true (* only one Funclass? *)
98 (*   | Fun i,Fun j when i=j -> true *)
99   | Term t, _
100   | _, Term t when not (CicUtil.is_meta_closed t) -> raise EqCarrOnNonMetaClosed
101   | _, _ -> false
102 ;;
103
104 let to_list () =
105   List.map (fun (s,t,l) -> s,t,List.map (fun a,_,b -> a,b) l) !db
106 ;;
107
108 let rec myfilter p = function
109   | [] -> []
110   | (s,t,l)::tl ->
111       let l = 
112         HExtlib.filter_map 
113           (fun (u,n,saturations) -> 
114             if p (s,t,u,saturations) then
115               if n = 1 then
116                 None
117               else
118                 Some (u,n-1,saturations)
119             else
120               Some (u,n,saturations)) 
121           l 
122       in
123       if l = [] then myfilter p tl else (s,t,l)::myfilter p tl
124 ;;
125   
126 let remove_coercion p = db := myfilter p !db;;
127
128 let find_coercion f =
129     List.map
130     (fun uri,_,saturations -> uri,saturations)
131     (List.flatten
132     (HExtlib.filter_map (fun (s,t,l) -> if f (s,t) then Some l else None) !db))
133 ;;
134
135 let get_carr uri =
136   try
137     let src, tgt, _ = 
138       List.find 
139         (fun (_,_,xl) -> List.exists (fun (x,_,_) -> UriManager.eq uri x) xl) 
140         !db 
141     in
142     src, tgt
143   with Not_found -> assert false (* uri must be a coercion *)
144 ;;
145
146 let is_a_coercion u = 
147   List.exists 
148     (fun (_,_,xl) -> List.exists (fun (x,_,_) -> UriManager.eq u x) xl) 
149     !db
150 ;;
151
152 let is_a_coercion' t = 
153   try
154     let uri = CicUtil.uri_of_term t in
155      is_a_coercion uri
156   with Invalid_argument _ -> false
157 ;;
158
159 let is_a_coercion_to_funclass t =
160   try
161     let uri = CicUtil.uri_of_term t in
162     match snd (get_carr uri) with
163     | Fun i -> Some i
164     | _ -> None
165   with Invalid_argument _ -> None
166   
167
168 let term_of_carr = function
169   | Uri u -> CicUtil.term_of_uri u
170   | Sort s -> Cic.Sort s
171   | Fun _ -> assert false
172   | Term _ -> assert false
173 ;;
174   
175 let add_coercion (src,tgt,u,saturations) =
176   let f s t = eq_carr s src && eq_carr t tgt in
177   let where = List.filter (fun (s,t,_) -> f s t) !db in
178   let rest = List.filter (fun (s,t,_) -> not (f s t)) !db in
179   match where with
180   | [] -> db := (src,tgt,[u,1,saturations]) :: !db
181   | (src,tgt,l)::tl ->
182       assert (tl = []); (* not sure, this may be a feature *)
183       if List.exists (fun (x,_,_) -> UriManager.eq u x) l then
184         let l' = List.map
185           (fun (x,n,saturations') ->
186             if UriManager.eq u x then
187              (x,n+1,saturations)
188             else
189              (x,n,saturations))
190           l
191         in
192         db := (src,tgt,l')::tl @ rest
193       else
194         db := (src,tgt,(u,1,saturations)::l)::tl @ rest
195       
196 ;;