]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_unification/coercGraph.ml
removed spurious load of a local gTopLevel.conf.xml
[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 (* the list of known coercions (MUST be transitively closed) *)
29 let coercions = ref [
30   (UriManager.uri_of_string "cic:/Coq/Init/Datatypes/nat.ind#xpointer(1/1)",
31    UriManager.uri_of_string "cic:/Coq/Reals/Rdefinitions/R.con",
32    UriManager.uri_of_string "cic://Coq/Reals/Raxioms/INR.con") ;
33
34    (
35      UriManager.uri_of_string "cic:/CoRN/algebra/CFields/CField.ind#xpointer(1/1)",
36      UriManager.uri_of_string "cic:/CoRN/algebra/CRings/CRing.ind#xpointer(1/1)",
37      UriManager.uri_of_string "cic:/CoRN/algebra/CFields/cf_crr.con"
38   
39     );
40     (
41      UriManager.uri_of_string "cic:/CoRN/algebra/CAbGroups/CAbGroup.ind#xpointer(1/1)",
42      UriManager.uri_of_string "cic:/CoRN/algebra/CGroups/CGroup.ind#xpointer(1/1)",
43      UriManager.uri_of_string "cic:/CoRN/algebra/CAbGroups/cag_crr.con"
44   
45     );
46
47 ]
48 ;;
49
50 (* searches a coercion fron src to tgt in the !coercions list *)
51 let look_for_coercion src tgt =
52   try
53     let s,t,u = 
54       List.find (fun (s,t,_) -> 
55         UriManager.eq s src && 
56         UriManager.eq t tgt) 
57       !coercions 
58     in
59     prerr_endline (sprintf ":) TROVATA la coercion %s %s" 
60       (UriManager.name_of_uri src) 
61       (UriManager.name_of_uri tgt));
62     Some (CicUtil.term_of_uri (UriManager.string_of_uri u))
63   with 
64     Not_found -> 
65       prerr_endline (sprintf ":( NON TROVATA la coercion %s %s" 
66         (UriManager.name_of_uri src) (UriManager.name_of_uri tgt));
67       None
68 ;;
69
70 (* given the new coercion uri from src to tgt returns the list 
71  * of new coercions to create. hte list elements are
72  * (source, list of coercions to follow, target)
73  *)
74 let get_closure_coercions src tgt uri =
75   let c_from_tgt = 
76     List.filter (fun (f,_,_) -> 
77       UriManager.eq f tgt) 
78     !coercions   
79   in
80   let c_to_src = 
81     List.filter (fun (_,t,_) -> 
82       UriManager.eq t src) 
83     !coercions
84   in
85     (List.map (fun (_,t,u) -> src,[uri; u],t) c_from_tgt) @
86     (List.map (fun (s,_,u) -> s,[u; uri],tgt) c_to_src) @
87     (List.fold_left (
88       fun l (s,_,u1) ->
89         ((List.map (fun (_,t,u2) ->
90           (s,[u1;uri;u2],t)
91         )c_from_tgt)@l) )
92     [] c_to_src)
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       prerr_endline (sprintf "Generated composite coercion:\n%s\n%s" 
124         (CicPp.ppterm c) 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 =
136   List.filter (
137       fun (src,_,tgt) ->
138         not (List.exists (fun (s,t,u) -> 
139           UriManager.eq s src && 
140           UriManager.eq t tgt)
141         !coercions))
142   l
143
144 (* given a new coercion uri from src to tgt returns 
145  * a list of (new coercion uri, coercion obj, universe graph) 
146  *)
147 let close_coercion_graph src tgt uri =
148   (* check if the coercion already exists *)
149   let todo_list = get_closure_coercions src tgt uri in
150   let todo_list = filter_duplicates todo_list 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 term_of_uri' uri = 
159                 CicUtil.term_of_uri (UriManager.string_of_uri uri)
160               in
161               let first_step = 
162                 Cic.Constant ("", Some (term_of_uri' he), Cic.Sort Cic.Prop, [],
163                   obj_attrs)
164               in
165               let o,u = 
166                 List.fold_left (fun (o,u) coer ->
167                   match o with 
168                   | Cic.Constant (_,Some c,_,[],_) ->
169                       generate_composite_closure c (term_of_uri' coer) u
170                   | _ -> assert false 
171                 ) (first_step, CicUniv.empty_ugraph) tl
172               in
173               let name_src = UriManager.name_of_uri src in
174               let name_tgt = UriManager.name_of_uri tgt in
175               let name = name_tgt ^ "_of_" ^ name_src in
176               let buri = UriManager.buri_of_uri uri in
177               let c_uri = 
178                 UriManager.uri_of_string (buri ^ "/" ^ name ^ ".con") 
179               in
180               let named_obj = 
181                 match o with
182                 | Cic.Constant (_,bo,ty,vl,attrs) ->
183                     Cic.Constant (name,bo,ty,vl,attrs)
184                 | _ -> assert false 
185               in
186                 ((src,tgt,c_uri),(c_uri,named_obj,u))
187       ) todo_list)
188   in
189   coercions := !coercions @ new_coercions @ [src,tgt,uri];
190   new_coercions_obj
191 ;;
192
193 let get_coercions_list () =
194   !coercions
195
196
197 (* stupid case *)
198 (*
199 let l = close_coercion_graph 
200  (UriManager.uri_of_string
201  "cic:/CoRN/algebra/CRings/CRing.ind#xpointer(1/1)")
202  (UriManager.uri_of_string
203  "cic:/CoRN/algebra/CAbGroups/CAbGroup.ind#xpointer(1/1)")
204  (UriManager.uri_of_string
205  "cic:/CoRN/algebra/CRings/cr_crr.con")
206 in
207  List.iter (fun (u,o,g) -> 
208    prerr_endline (CicPp.ppobj o);
209    prerr_endline (UriManager.string_of_uri u);
210    prerr_endline "")
211  l
212 *) 
213  
214
215 (* EOF *)