]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_unification/coercGraph.ml
dded missing catch for coercions
[helm.git] / helm / ocaml / 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 open Printf;;
27
28 type coercion_search_result = 
29   | SomeCoercion of Cic.term
30   | NoCoercion
31   | NotMetaClosed
32   | NotHandled of string Lazy.t
33
34 let debug = false
35 let debug_print s = if debug then prerr_endline (Lazy.force s) else ()
36
37
38 (* searches a coercion fron src to tgt in the !coercions list *)
39 let look_for_coercion src tgt =
40   try 
41     let l = 
42       CoercDb.find_coercion 
43         (fun (s,t) -> CoercDb.eq_carr s src && CoercDb.eq_carr t tgt) 
44     in
45     match l with
46     | [] -> debug_print (lazy ":( coercion non trovata"); NoCoercion
47     | u::_ -> 
48         debug_print (lazy (
49           sprintf ":) TROVATE %d coercion(s) da %s a %s, prendo la prima: %s" 
50             (List.length l)
51             (CoercDb.name_of_carr src) 
52             (CoercDb.name_of_carr tgt)
53             (UriManager.name_of_uri u)));
54         SomeCoercion (CicUtil.term_of_uri u)
55   with
56     | CoercDb.EqCarrNotImplemented s -> NotHandled s
57     | CoercDb.EqCarrOnNonMetaClosed -> NotMetaClosed
58 ;;
59
60 let look_for_coercion src tgt = 
61   let src_uri = CoercDb.coerc_carr_of_term src in
62   let tgt_uri = CoercDb.coerc_carr_of_term tgt in
63   look_for_coercion src_uri tgt_uri
64
65 (* given the new coercion uri from src to tgt returns the list 
66  * of new coercions to create. hte list elements are
67  * (source, list of coercions to follow, target)
68  *)
69 let get_closure_coercions src tgt uri coercions =
70   let eq_carr s t = 
71     try
72       CoercDb.eq_carr s t
73     with
74     | CoercDb.EqCarrNotImplemented _ | CoercDb.EqCarrOnNonMetaClosed -> false
75   in
76   match src,tgt with
77   | CoercDb.Uri _, CoercDb.Uri _ ->
78       let c_from_tgt = 
79         List.filter (fun (f,_,_) -> eq_carr f tgt) coercions 
80       in
81       let c_to_src = 
82         List.filter (fun (_,t,_) -> eq_carr t src) coercions 
83       in
84         (List.map (fun (_,t,u) -> src,[uri; u],t) c_from_tgt) @
85         (List.map (fun (s,_,u) -> s,[u; uri],tgt) c_to_src) @
86         (List.fold_left (
87           fun l (s,_,u1) ->
88             ((List.map (fun (_,t,u2) ->
89               (s,[u1;uri;u2],t)
90             )c_from_tgt)@l) )
91         [] c_to_src)
92   | _ -> [] (* do not close in case source or target is not an indty ?? *)
93 ;;
94
95 let obj_attrs = [`Class `Coercion; `Generated]
96
97 (* generate_composite_closure (c2 (c1 s)) in the universe graph univ *)
98 let generate_composite_closure c1 c2 univ =
99   let c1_ty,univ = CicTypeChecker.type_of_aux' [] [] c1 univ in
100   let rec mk_rels n =
101     match n with 
102     | 0 -> []
103     | _ -> (Cic.Rel n) :: (mk_rels (n-1))
104   in
105   let rec compose k =
106     function 
107     | Cic.Prod (name,src,tgt) -> 
108         let name =
109           match name with
110           | Cic.Anonymous -> Cic.Name "x"
111           | _ -> name
112         in
113           Cic.Lambda (name,src,compose (k+1) tgt)
114     | Cic.Appl (he::tl) -> 
115         Cic.Appl (c2 :: tl @ [Cic.Appl (c1 :: (mk_rels k)) ])
116     | _ -> Cic.Appl (c2 :: [Cic.Appl (c1 :: (mk_rels k)) ])
117   in
118   let c = compose 0 c1_ty in
119   let c_ty,univ = 
120     try 
121       CicTypeChecker.type_of_aux' [] [] c univ
122     with CicTypeChecker.TypeCheckerFailure s as exn ->
123       debug_print (lazy (sprintf "Generated composite coercion:\n%s\n%s" 
124         (CicPp.ppterm c) (Lazy.force s)));
125       raise exn
126   in
127   let cleaned_ty =
128     FreshNamesGenerator.clean_dummy_dependent_types c_ty 
129   in
130   let obj = Cic.Constant ("xxxx",Some c,cleaned_ty,[],obj_attrs) in 
131     obj,univ
132 ;;
133
134 (* removes from l the coercions that are in !coercions *)
135 let filter_duplicates l coercions =
136   List.filter (
137       fun (src,_,tgt) ->
138         not (List.exists (fun (s,t,u) -> 
139           CoercDb.eq_carr s src && 
140           CoercDb.eq_carr t tgt)
141         coercions))
142   l
143
144 let term_of_carr = function
145   | CoercDb.Uri u -> CicUtil.term_of_uri u
146   | CoercDb.Sort _ -> assert false 
147   | CoercDb.Term _ -> assert false
148   
149 (* given a new coercion uri from src to tgt returns 
150  * a list of (new coercion uri, coercion obj, universe graph) 
151  *)
152 let close_coercion_graph src tgt uri =
153   (* check if the coercion already exists *)
154   let coercions = CoercDb.to_list () in
155   let todo_list = get_closure_coercions src tgt uri coercions in
156   let todo_list = filter_duplicates todo_list coercions in
157   let new_coercions, new_coercions_obj = 
158     List.split (
159       List.map (
160         fun (src, l , tgt) ->
161           match l with
162           | [] -> assert false 
163           | he :: tl ->
164               let first_step = 
165                 Cic.Constant ("", 
166                   Some (term_of_carr (CoercDb.Uri he)), Cic.Sort Cic.Prop, [], obj_attrs)
167               in
168               let o,u = 
169                 List.fold_left (fun (o,univ) coer ->
170                   match o with 
171                   | Cic.Constant (_,Some c,_,[],_) ->
172                       generate_composite_closure c (term_of_carr (CoercDb.Uri
173                       coer)) univ
174                   | _ -> assert false 
175                 ) (first_step, CicUniv.empty_ugraph) tl
176               in
177               let name_src = CoercDb.name_of_carr src in
178               let name_tgt = CoercDb.name_of_carr tgt in
179               let name = name_tgt ^ "_of_" ^ name_src in
180               let buri = UriManager.buri_of_uri uri in
181               let c_uri = 
182                 UriManager.uri_of_string (buri ^ "/" ^ name ^ ".con") 
183               in
184               let named_obj = 
185                 match o with
186                 | Cic.Constant (_,bo,ty,vl,attrs) ->
187                     Cic.Constant (name,bo,ty,vl,attrs)
188                 | _ -> assert false 
189               in
190                 ((src,tgt,c_uri),(c_uri,named_obj,u))
191       ) todo_list)
192   in
193   List.iter CoercDb.add_coercion (new_coercions @ [src,tgt,uri]);
194   new_coercions_obj
195 ;;
196
197 (* EOF *)