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/.
28 type coercion_search_result =
29 | SomeCoercion of Cic.term
32 | NotHandled of string Lazy.t
35 let debug_print s = if debug then prerr_endline (Lazy.force s) else ()
38 (* searches a coercion fron src to tgt in the !coercions list *)
39 let look_for_coercion src tgt =
43 (fun (s,t) -> CoercDb.eq_carr s src && CoercDb.eq_carr t tgt)
46 | [] -> debug_print (lazy ":( coercion non trovata"); NoCoercion
49 sprintf ":) TROVATE %d coercion(s) da %s a %s, prendo la prima: %s"
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)
56 | CoercDb.EqCarrNotImplemented s -> NotHandled s
57 | CoercDb.EqCarrOnNonMetaClosed -> NotMetaClosed
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
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)
69 let get_closure_coercions src tgt uri coercions =
71 | CoercDb.Uri _, CoercDb.Uri _ ->
73 List.filter (fun (f,_,_) -> CoercDb.eq_carr f tgt) coercions
76 List.filter (fun (_,t,_) -> CoercDb.eq_carr t src) coercions
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) @
82 ((List.map (fun (_,t,u2) ->
86 | _ -> [] (* do not close in case source or target is not an indty ?? *)
89 let obj_attrs = [`Class `Coercion; `Generated]
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
97 | _ -> (Cic.Rel n) :: (mk_rels (n-1))
101 | Cic.Prod (name,src,tgt) ->
104 | Cic.Anonymous -> Cic.Name "x"
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)) ])
112 let c = compose 0 c1_ty in
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) (Lazy.force s)));
122 FreshNamesGenerator.clean_dummy_dependent_types c_ty
124 let obj = Cic.Constant ("xxxx",Some c,cleaned_ty,[],obj_attrs) in
128 (* removes from l the coercions that are in !coercions *)
129 let filter_duplicates l coercions =
132 not (List.exists (fun (s,t,u) ->
133 CoercDb.eq_carr s src &&
134 CoercDb.eq_carr t tgt)
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
143 (* given a new coercion uri from src to tgt returns
144 * a list of (new coercion uri, coercion obj, universe graph)
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 =
154 fun (src, l , tgt) ->
160 Some (term_of_carr (CoercDb.Uri he)), Cic.Sort Cic.Prop, [], obj_attrs)
163 List.fold_left (fun (o,univ) coer ->
165 | Cic.Constant (_,Some c,_,[],_) ->
166 generate_composite_closure c (term_of_carr (CoercDb.Uri
169 ) (first_step, CicUniv.empty_ugraph) tl
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
176 UriManager.uri_of_string (buri ^ "/" ^ name ^ ".con")
180 | Cic.Constant (_,bo,ty,vl,attrs) ->
181 Cic.Constant (name,bo,ty,vl,attrs)
184 ((src,tgt,c_uri),(c_uri,named_obj,u))
187 List.iter CoercDb.add_coercion (new_coercions @ [src,tgt,uri]);