]> matita.cs.unibo.it Git - helm.git/blob - components/cic_unification/coercGraph.ml
Added check for duplicate (convertible) composite coercions,
[helm.git] / components / cic_unification / coercGraph.ml
1 (* Copyright (C) 2000, 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://cs.unibo.it/helm/.
24  *)
25
26 (* $Id$ *)
27
28 open Printf;;
29
30 type coercion_search_result = 
31      (* metasenv, last coercion argument, fully saturated coercion *)
32      (* to apply the coercion it is sufficient to unify the last coercion
33         argument (that is a Meta) with the term to be coerced *)
34   | SomeCoercion of (Cic.metasenv * Cic.term * Cic.term) list
35   | NoCoercion
36   | NotMetaClosed
37   | NotHandled of string Lazy.t
38
39 let debug = false
40 let debug_print s = if debug then prerr_endline (Lazy.force s) else ()
41
42 let saturate_coercion ul metasenv subst context =
43   let cl = List.map CicUtil.term_of_uri ul in
44   let funclass_arityl = 
45     let _,tgtcarl = List.split (List.map CoercDb.get_carr ul) in
46     List.map (function CoercDb.Fun i -> i | _ -> 0) tgtcarl
47   in
48   let freshmeta = CicMkImplicit.new_meta metasenv subst in
49    List.map2
50     (fun arity c -> 
51       let ty,_ =
52        CicTypeChecker.type_of_aux' ~subst metasenv context c
53         CicUniv.empty_ugraph in
54       let _,metasenv,args,lastmeta =
55        TermUtil.saturate_term freshmeta metasenv context ty arity in
56       let irl =
57        CicMkImplicit.identity_relocation_list_for_metavariable context
58       in
59        metasenv, Cic.Meta (lastmeta-1,irl),
60         match args with
61            [] -> c
62          | _ -> Cic.Appl (c::args)
63     ) funclass_arityl cl
64 ;;
65           
66 (* searches a coercion fron src to tgt in the !coercions list *)
67 let look_for_coercion' metasenv subst context src tgt =
68   try 
69     let l = 
70       CoercDb.find_coercion 
71         (fun (s,t) -> CoercDb.eq_carr s src && CoercDb.eq_carr t tgt) in
72     let uri =
73      match l with
74      | [] -> 
75          debug_print 
76            (lazy 
77              (sprintf ":-( coercion non trovata da %s a %s" 
78                (CoercDb.name_of_carr src) 
79                (CoercDb.name_of_carr tgt)));
80          None
81      | _::_ -> 
82          debug_print (lazy (
83            sprintf ":-) TROVATE %d coercion(s) da %s a %s" 
84              (List.length l)
85              (CoercDb.name_of_carr src) 
86              (CoercDb.name_of_carr tgt)));
87          Some l
88     in
89      (match uri with
90          None -> NoCoercion
91        | Some ul -> SomeCoercion (saturate_coercion ul metasenv subst context))
92   with
93     | CoercDb.EqCarrNotImplemented s -> NotHandled s
94     | CoercDb.EqCarrOnNonMetaClosed -> NotMetaClosed
95 ;;
96
97 let look_for_coercion metasenv subst context src tgt = 
98   let src_uri = CoercDb.coerc_carr_of_term src in
99   let tgt_uri = CoercDb.coerc_carr_of_term tgt in
100   look_for_coercion' metasenv subst context src_uri tgt_uri
101
102 let source_of t = 
103   try
104     let uri = CicUtil.uri_of_term t in
105     CoercDb.term_of_carr (fst (CoercDb.get_carr uri))
106   with Invalid_argument _ -> assert false (* t must be a coercion *)
107
108 let generate_dot_file () =
109   let module Pp = GraphvizPp.Dot in
110   let buf = Buffer.create 10240 in
111   let fmt = Format.formatter_of_buffer buf in
112   Pp.header ~node_attrs:["fontsize", "9"; "width", ".4"; "height", ".4"]
113     ~edge_attrs:["fontsize", "10"] fmt;
114   let l = CoercDb.to_list () in
115   let pp_description carr =
116     match CoercDb.uri_of_carr carr with
117     | None -> ()
118     | Some uri ->
119         Pp.node (CoercDb.name_of_carr carr)
120           ~attrs:["href", UriManager.string_of_uri uri] fmt in
121   List.iter
122     (fun (src, tgt, cl) ->
123       let src_name = CoercDb.name_of_carr src in
124       let tgt_name = CoercDb.name_of_carr tgt in
125       pp_description src;
126       pp_description tgt;
127       List.iter
128         (fun c ->
129           Pp.edge src_name tgt_name
130             ~attrs:[ "label", UriManager.name_of_uri c;
131               "href", UriManager.string_of_uri c ]
132             fmt)
133         cl)
134     l;
135   Pp.trailer fmt;
136   Buffer.contents buf
137 ;;
138     
139 let is_composite t =
140   try
141     let uri = 
142       match t with 
143       | Cic.Appl (he::_) -> CicUtil.uri_of_term he
144       | _ -> CicUtil.uri_of_term t
145     in
146     match CicEnvironment.get_obj CicUniv.empty_ugraph uri with
147     | Cic.Constant (_,_, _, _, attrs),_  ->
148         List.exists (function `Class (`Coercion _) -> true | _ -> false) attrs
149     | _ -> false
150   with Invalid_argument _ -> false
151 ;;
152
153 let coerced_arg l =
154   match l with
155   | [] | [_] -> assert false
156   | c::_ when not (CoercDb.is_a_coercion' c) -> assert false
157   | c::tl -> 
158      let arity = 
159        match CoercDb.is_a_coercion_to_funclass c with None -> 0 | Some a -> a 
160     in
161     (* decide a decent structure for coercion carriers so that all this stuff is
162      * useless *)
163     let pi = 
164       (* this calculation is not complete, since we have strange carriers *)
165       let rec count_pi = function
166         | Cic.Prod(_,_,t) -> 1+ (count_pi t)
167         | _ -> 0
168       in
169       let uri = CicUtil.uri_of_term c in
170       match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
171       | Cic.Constant (_, _, ty, _, _) -> count_pi ty
172       | _ -> assert false
173     in
174     try Some (List.nth tl (pi - arity)) with Invalid_argument _ -> None
175 ;;
176
177 (************************* meet calculation stuff ********************)
178 let eq_uri_opt u1 u2 = 
179   match u1,u2 with
180   | Some u1, Some u2 -> UriManager.eq u1 u2
181   | None,Some _ | Some _, None -> false
182   | None, None -> true
183 ;;
184
185 let eq_carr_uri (c1,u1) (c2,u2) = CoercDb.eq_carr c1 c2 && eq_uri_opt u1 u2;;
186
187 let eq_carr_uri_uri (c1,u1,u11) (c2,u2,u22) = 
188   CoercDb.eq_carr c1 c2 && eq_uri_opt u1 u2 && eq_uri_opt u11 u22
189 ;;
190
191 let uniq = HExtlib.list_uniq ~eq:eq_carr_uri;;
192
193 let uniq2 = HExtlib.list_uniq ~eq:eq_carr_uri_uri;;
194
195 let splat e l = List.map (fun x -> e, Some x) l;;
196
197 (* : carr -> (carr * uri option) where the option is always Some *)
198 let get_coercions_to carr = 
199   let l = CoercDb.to_list () in
200   let splat_coercion_to carr (src,tgt,cl) =
201     if CoercDb.eq_carr tgt carr then Some (splat src cl) else None
202   in
203   let l = List.flatten (HExtlib.filter_map (splat_coercion_to carr) l) in
204   l
205 ;;
206
207 (* : carr -> (carr * uri option) where the option is always Some *)
208 let get_coercions_from carr = 
209   let l = CoercDb.to_list () in
210   let splat_coercion_from carr (src,tgt,cl) =
211     if CoercDb.eq_carr src carr then Some (splat tgt cl) else None
212   in
213   List.flatten (HExtlib.filter_map (splat_coercion_from carr) l)
214 ;;
215
216 (* intersect { (s1,u1) | u1:s1->t1 } { (s2,u2) | u2:s2->t2 } 
217  * gives the set { (s,u1,u2) | u1:s->t1 /\ u2:s->t2 } *)
218 let intersect l1 l2 = 
219   let is_in_l1 (x,u2) = 
220     HExtlib.filter_map 
221       (fun (src,u1) -> 
222          if CoercDb.eq_carr x src then Some (src,u1,u2) else None)
223     l1
224   in
225   uniq2 (List.flatten (List.map is_in_l1 l2))
226 ;;
227
228 (* grow tgt gives all the (src,u) such that u:tgt->src *)
229 let grow tgt = 
230   uniq ((tgt,None)::(get_coercions_to tgt))
231 ;;
232
233 let lb (c,_,_) = 
234   let l = get_coercions_from c in
235   fun (x,_,_) -> List.exists (fun (y,_) -> CoercDb.eq_carr x y) l
236 ;;
237
238 (* given the set { (s,u1,u2) | u1:s->t1 /\ u2:s->t2 } removes the elements 
239  * (s,_,_) such that (s',_,_) is in the set and there exists a coercion s->s' *)
240 let rec min acc = function
241   | c::tl -> 
242     if List.exists (lb c) (tl@acc) then min acc tl else min (c::acc) tl
243   | [] -> acc
244 ;;
245
246 let meets metasenv subst context left right =
247   let saturate metasenv uo =
248     match uo with 
249     | None -> metasenv, None
250     | Some u -> 
251         match saturate_coercion [u] metasenv subst context with
252         | [metasenv, sat, last] -> metasenv, Some (sat, last)
253         | _ -> assert false
254   in
255   List.map 
256     (fun (c,uo1,uo2) -> 
257       let metasenv, uo1 = saturate metasenv uo1 in
258       let metasenv, uo2 = saturate metasenv uo2 in
259       c,metasenv, uo1, uo2)
260     (min [] (intersect (grow left) (grow right)))
261 ;;
262
263 (* EOF *)