1 (* Copyright (C) 2005, 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://helm.cs.unibo.it/
29 let debug_print s = if debug then prerr_endline (Lazy.force s) else ()
31 (* given the new coercion uri from src to tgt returns the list
32 * of new coercions to create. hte list elements are
33 * (source, list of coercions to follow, target)
35 let get_closure_coercions src tgt uri coercions =
40 | CoercDb.EqCarrNotImplemented _ | CoercDb.EqCarrOnNonMetaClosed -> false
43 | CoercDb.Uri _, CoercDb.Uri _ ->
45 List.filter (fun (f,_,_) -> eq_carr f tgt) coercions
48 List.filter (fun (_,t,_) -> eq_carr t src) coercions
50 (List.map (fun (_,t,u) -> src,[uri; u],t) c_from_tgt) @
51 (List.map (fun (s,_,u) -> s,[u; uri],tgt) c_to_src) @
54 ((List.map (fun (_,t,u2) ->
58 | _ -> [] (* do not close in case source or target is not an indty ?? *)
61 let obj_attrs = [`Class `Coercion; `Generated]
63 (* generate_composite_closure (c2 (c1 s)) in the universe graph univ *)
64 let generate_composite_closure c1 c2 univ =
65 let c1_ty,univ = CicTypeChecker.type_of_aux' [] [] c1 univ in
69 | _ -> (Cic.Rel n) :: (mk_rels (n-1))
73 | Cic.Prod (name,src,tgt) ->
76 | Cic.Anonymous -> Cic.Name "x"
79 Cic.Lambda (name,src,compose (k+1) tgt)
80 | Cic.Appl (he::tl) ->
81 Cic.Appl (c2 :: tl @ [Cic.Appl (c1 :: (mk_rels k)) ])
82 | _ -> Cic.Appl (c2 :: [Cic.Appl (c1 :: (mk_rels k)) ])
84 let c = compose 0 c1_ty in
87 CicTypeChecker.type_of_aux' [] [] c univ
88 with CicTypeChecker.TypeCheckerFailure s as exn ->
89 debug_print (lazy (Printf.sprintf "Generated composite coercion:\n%s\n%s"
90 (CicPp.ppterm c) (Lazy.force s)));
94 FreshNamesGenerator.clean_dummy_dependent_types c_ty
96 let obj = Cic.Constant ("xxxx",Some c,cleaned_ty,[],obj_attrs) in
100 (* removes from l the coercions that are in !coercions *)
101 let filter_duplicates l coercions =
104 not (List.exists (fun (s,t,u) ->
105 CoercDb.eq_carr s src &&
106 CoercDb.eq_carr t tgt)
110 (* given a new coercion uri from src to tgt returns
111 * a list of (new coercion uri, coercion obj, universe graph)
113 let close_coercion_graph src tgt uri =
114 (* check if the coercion already exists *)
115 let coercions = CoercDb.to_list () in
116 let todo_list = get_closure_coercions src tgt uri coercions in
117 let todo_list = filter_duplicates todo_list coercions in
120 fun (src, l , tgt) ->
126 Some (CoercDb.term_of_carr (CoercDb.Uri he)),
127 Cic.Sort Cic.Prop, [], obj_attrs)
130 List.fold_left (fun (o,univ) coer ->
132 | Cic.Constant (_,Some c,_,[],_) ->
133 generate_composite_closure c (CoercDb.term_of_carr (CoercDb.Uri
136 ) (first_step, CicUniv.empty_ugraph) tl
138 let name_src = CoercDb.name_of_carr src in
139 let name_tgt = CoercDb.name_of_carr tgt in
140 let name = name_tgt ^ "_of_" ^ name_src in
141 let buri = UriManager.buri_of_uri uri in
143 UriManager.uri_of_string (buri ^ "/" ^ name ^ ".con")
147 | Cic.Constant (_,bo,ty,vl,attrs) ->
148 Cic.Constant (name,bo,ty,vl,attrs)
151 ((src,tgt,c_uri,named_obj))