]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/library/coercDb.ml
Preparing for 0.5.9 release.
[helm.git] / helm / software / 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 let debug = false
29 let debug_print =
30   if debug then fun x -> prerr_endline (Lazy.force x)
31   else ignore
32 ;;
33
34 type coerc_carr = 
35   | Uri of UriManager.uri 
36   | Sort of Cic.sort 
37   | Fun of int 
38   | Dead
39 ;;
40
41 type saturations = int
42 type coerced_pos = int
43 type coercion_entry = 
44   coerc_carr * coerc_carr * UriManager.uri * saturations * coerced_pos
45
46 type coerc_db = (* coercion_entry grouped by carrier with molteplicity *)
47  (coerc_carr * coerc_carr * 
48    (UriManager.uri * int * saturations * coerced_pos) list) list
49
50 let db =  ref ([] : coerc_db)
51 let dump () = !db 
52 let restore coerc_db = db := coerc_db
53 let empty_coerc_db = []
54
55 let rec coerc_carr_of_term t a =
56  try
57   match t, a with
58    | Cic.Sort s, 0 -> Sort s
59    | Cic.Appl (t::_), 0 -> coerc_carr_of_term t a
60    | t, 0 -> Uri (CicUtil.uri_of_term t)
61    | _, n -> Fun n
62  with Invalid_argument _ -> Dead
63 ;;
64
65 let string_of_carr = function
66   | Uri u -> UriManager.name_of_uri u
67   | Sort s -> CicPp.ppsort s
68   | Fun i -> "FunClass_" ^ string_of_int i   
69   | Dead -> "UnsupportedCarrier"
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 _, Sort _ -> true
83   | Fun _,Fun _ when not exact -> true (* only one Funclass *)
84   | Fun i,Fun j when i = j -> true (* only one Funclass *)
85   | _, _ -> false
86 ;;
87
88 let to_list db =
89   List.map (fun (s,t,l) -> s,t,List.map (fun a,_,b,c -> a,b,c) 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,saturations,cpos) as e -> 
98             if p (s,t,u,saturations,cpos) then
99               if n = 1 then None
100               else Some (u,n-1,saturations,cpos)
101             else Some e) 
102           l 
103       in
104       if l = [] then myfilter p tl else (s,t,l)::myfilter p tl
105 ;;
106   
107 let remove_coercion p = db := myfilter p !db;;
108
109 let find_coercion f =
110   List.map
111    (fun (uri,_,saturations,_) -> uri,saturations)
112    (List.flatten
113     (HExtlib.filter_map (fun (s,t,l) -> if f (s,t) then Some l else None) !db))
114 ;;
115
116 let is_a_coercion t = 
117   try
118    let uri = CicUtil.uri_of_term t in
119    match 
120      HExtlib.filter_map
121       (fun (src,tgt,xl) -> 
122          let xl = List.filter (fun (x,_,_,_) -> UriManager.eq uri x) xl in
123          if xl = [] then None else Some (src,tgt,xl))
124       !db
125    with
126    | [] -> None
127    | (_,_,[])::_ -> assert false
128    | [src,tgt,[u,_,s,p]] -> Some (src,tgt,u,s,p)
129    | (src,tgt,(u,_,s,p)::_)::_ -> 
130        debug_print 
131          (lazy "coercion has multiple entries, returning the first one");
132        Some (src,tgt,u,s,p)
133   with Invalid_argument _ -> 
134     debug_print (lazy "this term is not a constant");      
135     None
136 ;;
137
138 let add_coercion (src,tgt,u,saturations,cpos) =
139   let f s t = eq_carr s src && eq_carr t tgt in
140   let where = List.filter (fun (s,t,_) -> f s t) !db in
141   let rest = List.filter (fun (s,t,_) -> not (f s t)) !db in
142   match where with
143   | [] -> db := (src,tgt,[u,1,saturations,cpos]) :: !db
144   | (src,tgt,l)::tl ->
145       assert (tl = []); (* not sure, this may be a feature *)
146       if List.exists (fun (x,_,_,_) -> UriManager.eq u x) l then
147         let l = 
148           let l = 
149             (* this code reorders the list so that adding an already declared 
150              * coercion moves it to the begging of the list *)
151             let item = List.find (fun (x,_,_,_) -> UriManager.eq u x) l in
152             let rest=List.filter (fun (x,_,_,_) -> not (UriManager.eq u x)) l in
153             item :: rest
154           in
155           List.map
156           (fun (x,n,x_saturations,x_cpos) as e ->
157             if UriManager.eq u x then
158              (* not sure, this may be a feature *)
159              (assert (x_saturations = saturations && x_cpos = cpos);       
160              (x,n+1,saturations,cpos))
161             else e)
162           l
163         in
164         db := (src,tgt,l)::tl @ rest
165       else
166         db := (src,tgt,(u,1,saturations,cpos)::l)::tl @ rest
167 ;;
168
169 let prefer u = 
170   let prefer (s,t,l) =
171     let lb,la = List.partition (fun (uri,_,_,_) -> UriManager.eq uri u) l in
172     s,t,lb@la
173   in
174   db := List.map prefer !db;
175 ;;