]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_unification/coercGraph.ml
added coercions to Prod
[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
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   match src,tgt with
71   | CoercDb.Uri _, CoercDb.Uri _ ->
72       let c_from_tgt = 
73         List.filter (fun (f,_,_) -> CoercDb.eq_carr f tgt) coercions 
74       in
75       let c_to_src = 
76         List.filter (fun (_,t,_) -> CoercDb.eq_carr t src) coercions 
77       in
78         (List.map (fun (_,t,u) -> src,[uri; u],t) c_from_tgt) @
79         (List.map (fun (s,_,u) -> s,[u; uri],tgt) c_to_src) @
80         (List.fold_left (
81           fun l (s,_,u1) ->
82             ((List.map (fun (_,t,u2) ->
83               (s,[u1;uri;u2],t)
84             )c_from_tgt)@l) )
85         [] c_to_src)
86   | _ -> [] (* do not close in case source or target is not an indty ?? *)
87 ;;
88
89 let obj_attrs = [`Class `Coercion; `Generated]
90
91 (* generate_composite_closure (c2 (c1 s)) in the universe graph univ *)
92 let generate_composite_closure c1 c2 univ =
93   let c1_ty,univ = CicTypeChecker.type_of_aux' [] [] c1 univ in
94   let rec mk_rels n =
95     match n with 
96     | 0 -> []
97     | _ -> (Cic.Rel n) :: (mk_rels (n-1))
98   in
99   let rec compose k =
100     function 
101     | Cic.Prod (name,src,tgt) -> 
102         let name =
103           match name with
104           | Cic.Anonymous -> Cic.Name "x"
105           | _ -> name
106         in
107           Cic.Lambda (name,src,compose (k+1) tgt)
108     | Cic.Appl (he::tl) -> 
109         Cic.Appl (c2 :: tl @ [Cic.Appl (c1 :: (mk_rels k)) ])
110     | _ -> Cic.Appl (c2 :: [Cic.Appl (c1 :: (mk_rels k)) ])
111   in
112   let c = compose 0 c1_ty in
113   let c_ty,univ = 
114     try 
115       CicTypeChecker.type_of_aux' [] [] c univ
116     with CicTypeChecker.TypeCheckerFailure s as exn ->
117       debug_print (lazy (sprintf "Generated composite coercion:\n%s\n%s" 
118         (CicPp.ppterm c) s));
119       raise exn
120   in
121   let cleaned_ty =
122     FreshNamesGenerator.clean_dummy_dependent_types c_ty 
123   in
124   let obj = Cic.Constant ("xxxx",Some c,cleaned_ty,[],obj_attrs) in 
125     obj,univ
126 ;;
127
128 (* removes from l the coercions that are in !coercions *)
129 let filter_duplicates l coercions =
130   List.filter (
131       fun (src,_,tgt) ->
132         not (List.exists (fun (s,t,u) -> 
133           CoercDb.eq_carr s src && 
134           CoercDb.eq_carr t tgt)
135         coercions))
136   l
137
138 let term_of_carr = function
139   | CoercDb.Uri u -> CicUtil.term_of_uri u
140   | CoercDb.Sort _ -> assert false 
141   | CoercDb.Term _ -> assert false
142   
143 (* given a new coercion uri from src to tgt returns 
144  * a list of (new coercion uri, coercion obj, universe graph) 
145  *)
146 let close_coercion_graph src tgt uri =
147   (* check if the coercion already exists *)
148   let coercions = CoercDb.to_list () in
149   let todo_list = get_closure_coercions src tgt uri coercions in
150   let todo_list = filter_duplicates todo_list coercions in
151   let new_coercions, new_coercions_obj = 
152     List.split (
153       List.map (
154         fun (src, l , tgt) ->
155           match l with
156           | [] -> assert false 
157           | he :: tl ->
158               let first_step = 
159                 Cic.Constant ("", 
160                   Some (term_of_carr (CoercDb.Uri he)), Cic.Sort Cic.Prop, [], obj_attrs)
161               in
162               let o,u = 
163                 List.fold_left (fun (o,univ) coer ->
164                   match o with 
165                   | Cic.Constant (_,Some c,_,[],_) ->
166                       generate_composite_closure c (term_of_carr (CoercDb.Uri
167                       coer)) univ
168                   | _ -> assert false 
169                 ) (first_step, CicUniv.empty_ugraph) tl
170               in
171               let name_src = CoercDb.name_of_carr src in
172               let name_tgt = CoercDb.name_of_carr tgt in
173               let name = name_tgt ^ "_of_" ^ name_src in
174               let buri = UriManager.buri_of_uri uri in
175               let c_uri = 
176                 UriManager.uri_of_string (buri ^ "/" ^ name ^ ".con") 
177               in
178               let named_obj = 
179                 match o with
180                 | Cic.Constant (_,bo,ty,vl,attrs) ->
181                     Cic.Constant (name,bo,ty,vl,attrs)
182                 | _ -> assert false 
183               in
184                 ((src,tgt,c_uri),(c_uri,named_obj,u))
185       ) todo_list)
186   in
187   List.iter CoercDb.add_coercion (new_coercions @ [src,tgt,uri]);
188   new_coercions_obj
189 ;;
190
191 (* EOF *)