]> matita.cs.unibo.it Git - helm.git/blob - components/library/coercDb.ml
tagged 0.5.0-rc1
[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 t = CicUtil.term_of_uri src in
77       let ty,_ = CicTypeChecker.type_of_aux' [] [] t CicUniv.oblivion_ugraph in
78       (match ty, exact with
79       | Cic.Prod _, true -> false
80       | Cic.Prod _, false -> coarse_eq
81       | _ -> coarse_eq) 
82   | Sort (Cic.Type _), Sort (Cic.Type _) -> true
83   | Sort src, Sort tgt when src = tgt -> true
84   | Term t1, Term t2 ->
85       if t1 = t2 then true
86       else
87         if CicUtil.is_meta_closed t1 && CicUtil.is_meta_closed t2 then
88           raise 
89           (EqCarrNotImplemented 
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 *)
95   | Term t, _
96   | _, Term t when not (CicUtil.is_meta_closed t) -> raise EqCarrOnNonMetaClosed
97   | _, _ -> false
98 ;;
99
100 let to_list () =
101   List.map (fun (s,t,l) -> s,t,List.map (fun a,_,b -> a,b) l) !db
102 ;;
103
104 let rec myfilter p = function
105   | [] -> []
106   | (s,t,l)::tl ->
107       let l = 
108         HExtlib.filter_map 
109           (fun (u,n,saturations) -> 
110             if p (s,t,u,saturations) then
111               if n = 1 then
112                 None
113               else
114                 Some (u,n-1,saturations)
115             else
116               Some (u,n,saturations)) 
117           l 
118       in
119       if l = [] then myfilter p tl else (s,t,l)::myfilter p tl
120 ;;
121   
122 let remove_coercion p = db := myfilter p !db;;
123
124 let find_coercion f =
125     List.map
126     (fun uri,_,saturations -> uri,saturations)
127     (List.flatten
128     (HExtlib.filter_map (fun (s,t,l) -> if f (s,t) then Some l else None) !db))
129 ;;
130
131 let get_carr uri =
132   try
133     let src, tgt, _ = 
134       List.find 
135         (fun (_,_,xl) -> List.exists (fun (x,_,_) -> UriManager.eq uri x) xl) 
136         !db 
137     in
138     src, tgt
139   with Not_found -> assert false (* uri must be a coercion *)
140 ;;
141
142 let is_a_coercion u = 
143   List.exists 
144     (fun (_,_,xl) -> List.exists (fun (x,_,_) -> UriManager.eq u x) xl) 
145     !db
146 ;;
147
148 let is_a_coercion' t = 
149   try
150     let uri = CicUtil.uri_of_term t in
151      is_a_coercion uri
152   with Invalid_argument _ -> false
153 ;;
154
155 let is_a_coercion_to_funclass t =
156   try
157     let uri = CicUtil.uri_of_term t in
158     match snd (get_carr uri) with
159     | Fun i -> Some i
160     | _ -> None
161   with Invalid_argument _ -> None
162   
163
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
169 ;;
170   
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
175   match where with
176   | [] -> db := (src,tgt,[u,1,saturations]) :: !db
177   | (src,tgt,l)::tl ->
178       assert (tl = []); (* not sure, this may be a feature *)
179       if List.exists (fun (x,_,_) -> UriManager.eq u x) l then
180         let l' = List.map
181           (fun (x,n,saturations') ->
182             if UriManager.eq u x then
183              (x,n+1,saturations)
184             else
185              (x,n,saturations))
186           l
187         in
188         db := (src,tgt,l')::tl @ rest
189       else
190         db := (src,tgt,(u,1,saturations)::l)::tl @ rest
191       
192 ;;
193
194 type coerc_db = (coerc_carr * coerc_carr * (UriManager.uri * int * int) list) list 
195 let dump () = !db 
196 let restore coerc_db = db := coerc_db