1 (* Copyright (C) 2004, 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/.
26 let debug_print = fun _ -> ()
28 (* mk_fresh_name context name typ *)
29 (* returns an identifier which is fresh in the context *)
30 (* and that resembles [name] as much as possible. *)
31 (* [typ] will be the type of the variable *)
32 let mk_fresh_name ~subst metasenv context name ~typ =
37 (*CSC: great space for improvements here *)
40 CicTypeChecker.type_of_aux' ~subst metasenv context typ
41 CicUniv.empty_ugraph in
44 | C.Sort C.CProp -> "H"
48 with CicTypeChecker.TypeCheckerFailure _ -> "H"
51 Str.global_replace (Str.regexp "[0-9]*$") "" name
53 let already_used name =
54 List.exists (function Some (C.Name n,_) -> n=name | _ -> false) context
56 if not (already_used basename) then
60 let name' = basename ^ string_of_int n in
61 if already_used name' then
69 (* clean_dummy_dependent_types term *)
70 (* returns a copy of [term] where every dummy dependent product *)
71 (* have been replaced with a non-dependent product and where *)
72 (* dummy let-ins have been removed. *)
73 let clean_dummy_dependent_types t =
77 C.Rel m as t -> t,[k - m]
78 | C.Var (uri,exp_named_subst) ->
79 let exp_named_subst',rels =
81 (fun (uri,t) (exp_named_subst,rels) ->
82 let t',rels' = aux k t in
83 (uri,t')::exp_named_subst, rels' @ rels
84 ) exp_named_subst ([],[])
86 C.Var (uri,exp_named_subst'),rels
95 let t',rels' = aux k t in
102 | C.Sort _ as t -> t,[]
103 | C.Implicit _ as t -> t,[]
105 let te',rels1 = aux k te in
106 let ty',rels2 = aux k ty in
107 C.Cast (te', ty'), rels1@rels2
109 let s',rels1 = aux k s in
110 let t',rels2 = aux (k+1) t in
114 if List.mem k rels2 then
116 debug_print "If this happens often, we can do something about it (i.e. we can generate a new fresh name; problem: we need the metasenv and context ;-(. Alternative solution: mk_implicit does not generate entries for the elements in the context that have no name" ;
122 if List.mem k rels2 then n else C.Anonymous
124 C.Prod (n', s', t'), rels1@rels2
125 | C.Lambda (n,s,t) ->
126 let s',rels1 = aux k s in
127 let t',rels2 = aux (k+1) t in
128 C.Lambda (n, s', t'), rels1@rels2
130 let s',rels1 = aux k s in
131 let t',rels2 = aux (k+1) t in
132 let rels = rels1 @ rels2 in
133 if List.mem k rels2 then
134 C.LetIn (n, s', t'), rels
136 (* (C.Rel 1) is just a dummy term; any term would fit *)
137 CicSubstitution.subst (C.Rel 1) t', rels
141 (fun t (exp_named_subst,rels) ->
142 let t',rels' = aux k t in
143 t'::exp_named_subst, rels' @ rels
147 | C.Const (uri,exp_named_subst) ->
148 let exp_named_subst',rels =
150 (fun (uri,t) (exp_named_subst,rels) ->
151 let t',rels' = aux k t in
152 (uri,t')::exp_named_subst, rels' @ rels
153 ) exp_named_subst ([],[])
155 C.Const (uri,exp_named_subst'),rels
156 | C.MutInd (uri,tyno,exp_named_subst) ->
157 let exp_named_subst',rels =
159 (fun (uri,t) (exp_named_subst,rels) ->
160 let t',rels' = aux k t in
161 (uri,t')::exp_named_subst, rels' @ rels
162 ) exp_named_subst ([],[])
164 C.MutInd (uri,tyno,exp_named_subst'),rels
165 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
166 let exp_named_subst',rels =
168 (fun (uri,t) (exp_named_subst,rels) ->
169 let t',rels' = aux k t in
170 (uri,t')::exp_named_subst, rels' @ rels
171 ) exp_named_subst ([],[])
173 C.MutConstruct (uri,tyno,consno,exp_named_subst'),rels
174 | C.MutCase (sp,i,outty,t,pl) ->
175 let outty',rels1 = aux k outty in
176 let t',rels2 = aux k t in
179 (fun t (exp_named_subst,rels) ->
180 let t',rels' = aux k t in
181 t'::exp_named_subst, rels' @ rels
184 C.MutCase (sp, i, outty', t', pl'), rels1 @ rels2 @rels3
186 let len = List.length fl in
189 (fun (name,i,ty,bo) (fl,rels) ->
190 let ty',rels1 = aux k ty in
191 let bo',rels2 = aux (k + len) bo in
192 (name,i,ty',bo')::fl, rels1 @ rels2 @ rels
197 let len = List.length fl in
200 (fun (name,ty,bo) (fl,rels) ->
201 let ty',rels1 = aux k ty in
202 let bo',rels2 = aux (k + len) bo in
203 (name,ty',bo')::fl, rels1 @ rels2 @ rels
206 C.CoFix (i, fl'),rels