]> matita.cs.unibo.it Git - helm.git/blob - components/library/coercDb.ml
BIG FAT COMMIT REGARDING COERCIONS:
[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.Appl (t::_)
45    | t -> Uri (CicUtil.uri_of_term t)
46  with Invalid_argument _ ->
47   Term t
48 ;;
49
50 let uri_of_carr = function
51   | Uri u -> Some u
52   | _ -> None
53
54 let rec name_of_carr = function
55   | Uri u -> UriManager.name_of_uri u
56   | Sort s -> CicPp.ppsort s
57   | Term (Cic.Appl ((Cic.Const (uri, _))::_)) 
58   | Term (Cic.Appl ((Cic.MutInd (uri, _, _))::_)) 
59   | Term (Cic.Appl ((Cic.MutConstruct (uri, _, _, _))::_)) -> 
60         UriManager.name_of_uri uri
61   | Term (Cic.Prod (_,_,t)) -> name_of_carr (Term t)
62   | Term t -> 
63       prerr_endline ("CoercDb.name_of_carr:" ^ CicPp.ppterm t); 
64       "FixTheNameMangler"
65   | Fun i -> "FunClass_" ^ string_of_int i   
66 ;;
67
68 let eq_carr src tgt =
69   match src, tgt with
70   | Uri src, Uri tgt -> UriManager.eq src tgt
71   | Sort (Cic.Type _), Sort (Cic.Type _) -> true
72   | Sort src, Sort tgt when src = tgt -> true
73   | Term t1, Term t2 ->
74       if t1 = t2 then true
75       else
76         if CicUtil.is_meta_closed t1 && CicUtil.is_meta_closed t2 then
77           raise 
78           (EqCarrNotImplemented 
79           (lazy ("Unsupported carr for coercions: " ^ 
80           CicPp.ppterm t1 ^ " or " ^ CicPp.ppterm t2)))
81       else raise EqCarrOnNonMetaClosed
82   | Fun _,Fun _ -> true (* only one Funclass? *)
83 (*   | Fun i,Fun j when i=j -> true *)
84   | _, _ -> false
85 ;;
86
87 let to_list () =
88   List.map (fun (s,t,l) -> s,t,List.map fst l) !db
89 ;;
90
91 let rec myfilter p = function
92   | [] -> []
93   | (s,t,l)::tl ->
94       let l = 
95         HExtlib.filter_map 
96           (fun (u,n) -> 
97             if p (s,t,u) then
98               if n = 1 then
99                 None
100               else
101                 Some (u,n-1)
102             else
103               Some (u,n)) 
104           l 
105       in
106       if l = [] then myfilter p tl else (s,t,l)::myfilter p tl
107 ;;
108   
109 let remove_coercion p = db := myfilter p !db;;
110
111 let find_coercion f =
112     List.map
113     fst
114     (List.flatten
115     (HExtlib.filter_map (fun (s,t,l) -> if f (s,t) then Some l else None) !db))
116 ;;
117
118 let is_a_coercion u = 
119   List.exists 
120     (fun (_,_,xl) -> List.exists (fun (x,_) -> UriManager.eq u x) xl) 
121     !db
122 ;;
123
124 let get_carr uri =
125   try
126     let src, tgt, _ = 
127       List.find 
128         (fun (_,_,xl) -> List.exists (fun (x,_) -> UriManager.eq uri x) xl) 
129         !db 
130     in
131     src, tgt
132   with Not_found -> assert false (* uri must be a coercion *)
133 ;;
134
135 let term_of_carr = function
136   | Uri u -> CicUtil.term_of_uri u
137   | Sort s -> Cic.Sort s
138   | Fun _ -> assert false
139   | Term _ -> assert false
140 ;;
141   
142 let add_coercion (src,tgt,u) =
143   let f s t = eq_carr s src && eq_carr t tgt in
144   let where = List.filter (fun (s,t,_) -> f s t) !db in
145   let rest = List.filter (fun (s,t,_) -> not (f s t)) !db in
146   match where with
147   | [] -> db := (src,tgt,[u,1]) :: !db
148   | (src,tgt,l)::tl ->
149       assert (tl = []); (* not sure, this may be a feature *)
150       if List.exists (fun (x,_) -> UriManager.eq u x) l then
151         let l' = List.map 
152           (fun (x,n) -> if UriManager.eq u x then (x,n+1) else (x,n))
153           l
154         in
155         db := (src,tgt,l')::tl @ rest
156       else
157         db := (src,tgt,(u,1)::l)::tl @ rest
158       
159 ;;
160
161
162