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