]> matita.cs.unibo.it Git - helm.git/blob - components/library/coercDb.ml
carr_of_term now returns Fun if a Prod is encountered
[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   | _, _ -> false
86 ;;
87
88 let to_list () =
89   List.map (fun (s,t,l) -> s,t,List.map fst l) !db
90 ;;
91
92 let rec myfilter p = function
93   | [] -> []
94   | (s,t,l)::tl ->
95       let l = 
96         HExtlib.filter_map 
97           (fun (u,n) -> 
98             if p (s,t,u) then
99               if n = 1 then
100                 None
101               else
102                 Some (u,n-1)
103             else
104               Some (u,n)) 
105           l 
106       in
107       if l = [] then myfilter p tl else (s,t,l)::myfilter p tl
108 ;;
109   
110 let remove_coercion p = db := myfilter p !db;;
111
112 let find_coercion f =
113     List.map
114     fst
115     (List.flatten
116     (HExtlib.filter_map (fun (s,t,l) -> if f (s,t) then Some l else None) !db))
117 ;;
118
119 let is_a_coercion u = 
120   List.exists 
121     (fun (_,_,xl) -> List.exists (fun (x,_) -> UriManager.eq u x) xl) 
122     !db
123 ;;
124
125 let get_carr uri =
126   try
127     let src, tgt, _ = 
128       List.find 
129         (fun (_,_,xl) -> List.exists (fun (x,_) -> UriManager.eq uri x) xl) 
130         !db 
131     in
132     src, tgt
133   with Not_found -> assert false (* uri must be a coercion *)
134 ;;
135
136 let term_of_carr = function
137   | Uri u -> CicUtil.term_of_uri u
138   | Sort s -> Cic.Sort s
139   | Fun _ -> assert false
140   | Term _ -> assert false
141 ;;
142   
143 let add_coercion (src,tgt,u) =
144   let f s t = eq_carr s src && eq_carr t tgt in
145   let where = List.filter (fun (s,t,_) -> f s t) !db in
146   let rest = List.filter (fun (s,t,_) -> not (f s t)) !db in
147   match where with
148   | [] -> db := (src,tgt,[u,1]) :: !db
149   | (src,tgt,l)::tl ->
150       assert (tl = []); (* not sure, this may be a feature *)
151       if List.exists (fun (x,_) -> UriManager.eq u x) l then
152         let l' = List.map 
153           (fun (x,n) -> if UriManager.eq u x then (x,n+1) else (x,n))
154           l
155         in
156         db := (src,tgt,l')::tl @ rest
157       else
158         db := (src,tgt,(u,1)::l)::tl @ rest
159       
160 ;;
161
162
163