1 (* Copyright (C) 2000, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
29 let debug_print = if debug then prerr_endline else ignore
31 (* searches a coercion fron src to tgt in the !coercions list *)
32 let look_for_coercion src tgt =
34 let src = CicUtil.uri_of_term src in
35 let tgt = CicUtil.uri_of_term tgt in
38 (fun (s,t) -> UriManager.eq s src && UriManager.eq t tgt)
41 | [] -> debug_print ":( coercion non trovata"; None
44 sprintf ":) TROVATE %d coercion(s) da %s a %s, prendo la prima: %s"
46 (UriManager.name_of_uri src)
47 (UriManager.name_of_uri tgt)
48 (UriManager.name_of_uri u));
49 Some (CicUtil.term_of_uri u)
50 with Invalid_argument s ->
51 debug_print (":( coercion non trovata (fallita la uri_of_term): " ^ s);
55 (* given the new coercion uri from src to tgt returns the list
56 * of new coercions to create. hte list elements are
57 * (source, list of coercions to follow, target)
59 let get_closure_coercions src tgt uri coercions =
60 let c_from_tgt = List.filter (fun (f,_,_) -> UriManager.eq f tgt) coercions in
61 let c_to_src = List.filter (fun (_,t,_) -> UriManager.eq t src) coercions in
62 (List.map (fun (_,t,u) -> src,[uri; u],t) c_from_tgt) @
63 (List.map (fun (s,_,u) -> s,[u; uri],tgt) c_to_src) @
66 ((List.map (fun (_,t,u2) ->
72 let obj_attrs = [`Class `Coercion; `Generated]
74 (* generate_composite_closure (c2 (c1 s)) in the universe graph univ *)
75 let generate_composite_closure c1 c2 univ =
76 let c1_ty,univ = CicTypeChecker.type_of_aux' [] [] c1 univ in
80 | _ -> (Cic.Rel n) :: (mk_rels (n-1))
84 | Cic.Prod (name,src,tgt) ->
87 | Cic.Anonymous -> Cic.Name "x"
90 Cic.Lambda (name,src,compose (k+1) tgt)
91 | Cic.Appl (he::tl) ->
92 Cic.Appl (c2 :: tl @ [Cic.Appl (c1 :: (mk_rels k)) ])
93 | _ -> Cic.Appl (c2 :: [Cic.Appl (c1 :: (mk_rels k)) ])
95 let c = compose 0 c1_ty in
98 CicTypeChecker.type_of_aux' [] [] c univ
99 with CicTypeChecker.TypeCheckerFailure s as exn ->
100 debug_print (sprintf "Generated composite coercion:\n%s\n%s"
105 FreshNamesGenerator.clean_dummy_dependent_types c_ty
107 let obj = Cic.Constant ("xxxx",Some c,cleaned_ty,[],obj_attrs) in
111 (* removes from l the coercions that are in !coercions *)
112 let filter_duplicates l coercions =
115 not (List.exists (fun (s,t,u) ->
116 UriManager.eq s src &&
121 (* given a new coercion uri from src to tgt returns
122 * a list of (new coercion uri, coercion obj, universe graph)
124 let close_coercion_graph src tgt uri =
125 (* check if the coercion already exists *)
126 let coercions = CoercDb.to_list () in
127 let todo_list = get_closure_coercions src tgt uri coercions in
128 let todo_list = filter_duplicates todo_list coercions in
129 let new_coercions, new_coercions_obj =
132 fun (src, l , tgt) ->
136 let term_of_uri' uri = CicUtil.term_of_uri uri in
138 Cic.Constant ("", Some (term_of_uri' he), Cic.Sort Cic.Prop, [],
142 List.fold_left (fun (o,u) coer ->
144 | Cic.Constant (_,Some c,_,[],_) ->
145 generate_composite_closure c (term_of_uri' coer) u
147 ) (first_step, CicUniv.empty_ugraph) tl
149 let name_src = UriManager.name_of_uri src in
150 let name_tgt = UriManager.name_of_uri tgt in
151 let name = name_tgt ^ "_of_" ^ name_src in
152 let buri = UriManager.buri_of_uri uri in
154 UriManager.uri_of_string (buri ^ "/" ^ name ^ ".con")
158 | Cic.Constant (_,bo,ty,vl,attrs) ->
159 Cic.Constant (name,bo,ty,vl,attrs)
162 ((src,tgt,c_uri),(c_uri,named_obj,u))
165 List.iter CoercDb.add_coercion (new_coercions @ [src,tgt,uri]);