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 exception CannotSubstInMeta;;
29 exception RelToHiddenHypothesis;;
30 exception ReferenceToVariable;;
31 exception ReferenceToConstant;;
32 exception ReferenceToCurrentProof;;
33 exception ReferenceToInductiveDefinition;;
38 fun m -> prerr_endline (Lazy.force m)
52 | C.Var (uri,exp_named_subst) ->
53 let exp_named_subst' =
54 List.map (function (uri,t) -> (uri,liftaux k t)) exp_named_subst
56 C.Var (uri,exp_named_subst')
62 | Some t -> Some (liftaux k t)
67 | C.Implicit _ as t -> t
68 | C.Cast (te,ty) -> C.Cast (liftaux k te, liftaux k ty)
69 | C.Prod (n,s,t) -> C.Prod (n, liftaux k s, liftaux (k+1) t)
70 | C.Lambda (n,s,t) -> C.Lambda (n, liftaux k s, liftaux (k+1) t)
71 | C.LetIn (n,s,ty,t) ->
72 C.LetIn (n, liftaux k s, liftaux k ty, liftaux (k+1) t)
73 | C.Appl l -> C.Appl (List.map (liftaux k) l)
74 | C.Const (uri,exp_named_subst) ->
75 let exp_named_subst' =
76 List.map (function (uri,t) -> (uri,liftaux k t)) exp_named_subst
78 C.Const (uri,exp_named_subst')
79 | C.MutInd (uri,tyno,exp_named_subst) ->
80 let exp_named_subst' =
81 List.map (function (uri,t) -> (uri,liftaux k t)) exp_named_subst
83 C.MutInd (uri,tyno,exp_named_subst')
84 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
85 let exp_named_subst' =
86 List.map (function (uri,t) -> (uri,liftaux k t)) exp_named_subst
88 C.MutConstruct (uri,tyno,consno,exp_named_subst')
89 | C.MutCase (sp,i,outty,t,pl) ->
90 C.MutCase (sp, i, liftaux k outty, liftaux k t,
91 List.map (liftaux k) pl)
93 let len = List.length fl in
96 (fun (name, i, ty, bo) -> (name, i, liftaux k ty, liftaux (k+len) bo))
101 let len = List.length fl in
104 (fun (name, ty, bo) -> (name, liftaux k ty, liftaux (k+len) bo))
107 C.CoFix (i, liftedfl)
119 (* substitutes [t1] for [Rel 1] in [t2] *)
120 (* if avoid_beta_redexes is true (default: false) no new beta redexes *)
121 (* are generated. WARNING: the substitution can diverge when t2 is not *)
122 (* well typed and avoid_beta_redexes is true. *)
123 let rec subst ?(avoid_beta_redexes=false) arg =
125 let module C = Cic in
129 n when n = k -> lift (k - 1) arg
133 | C.Var (uri,exp_named_subst) ->
134 let exp_named_subst' =
135 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst
137 C.Var (uri,exp_named_subst')
143 | Some t -> Some (substaux k t)
148 | C.Implicit _ as t -> t
149 | C.Cast (te,ty) -> C.Cast (substaux k te, substaux k ty)
150 | C.Prod (n,s,t) -> C.Prod (n, substaux k s, substaux (k + 1) t)
151 | C.Lambda (n,s,t) -> C.Lambda (n, substaux k s, substaux (k + 1) t)
152 | C.LetIn (n,s,ty,t) ->
153 C.LetIn (n, substaux k s, substaux k ty, substaux (k + 1) t)
155 (* Invariant: no Appl applied to another Appl *)
156 let tl' = List.map (substaux k) tl in
158 match substaux k he with
159 C.Appl l -> C.Appl (l@tl')
161 | C.Lambda (_,_,bo) when avoid_beta_redexes ->
164 | [he] -> subst ~avoid_beta_redexes he bo
165 | he::tl -> C.Appl (subst he bo::tl))
166 | _ as he' -> C.Appl (he'::tl')
168 | C.Appl _ -> assert false
169 | C.Const (uri,exp_named_subst) ->
170 let exp_named_subst' =
171 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst
173 C.Const (uri,exp_named_subst')
174 | C.MutInd (uri,typeno,exp_named_subst) ->
175 let exp_named_subst' =
176 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst
178 C.MutInd (uri,typeno,exp_named_subst')
179 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
180 let exp_named_subst' =
181 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst
183 C.MutConstruct (uri,typeno,consno,exp_named_subst')
184 | C.MutCase (sp,i,outt,t,pl) ->
185 C.MutCase (sp,i,substaux k outt, substaux k t,
186 List.map (substaux k) pl)
188 let len = List.length fl in
191 (fun (name,i,ty,bo) -> (name, i, substaux k ty, substaux (k+len) bo))
194 C.Fix (i, substitutedfl)
196 let len = List.length fl in
199 (fun (name,ty,bo) -> (name, substaux k ty, substaux (k+len) bo))
202 C.CoFix (i, substitutedfl)
207 (*CSC: i controlli di tipo debbono essere svolti da destra a *)
208 (*CSC: sinistra: i{B/A;b/a} ==> a{B/A;b/a} ==> a{b/a{B/A}} ==> b *)
209 (*CSC: la sostituzione ora e' implementata in maniera simultanea, ma *)
210 (*CSC: dovrebbe diventare da sinistra verso destra: *)
211 (*CSC: t{a=a/A;b/a} ==> \H:a=a.H{b/a} ==> \H:b=b.H *)
212 (*CSC: per la roba che proviene da Coq questo non serve! *)
213 let subst_vars exp_named_subst t =
215 debug_print (lazy ("@@@POSSIBLE BUG: SUBSTITUTION IS NOT SIMULTANEOUS")) ;
218 let module C = Cic in
221 | C.Var (uri,exp_named_subst') ->
225 (function (varuri,_) -> UriManager.eq uri varuri) exp_named_subst
231 let obj,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
233 C.Constant _ -> raise ReferenceToConstant
234 | C.Variable (_,_,_,params,_) -> params
235 | C.CurrentProof _ -> raise ReferenceToCurrentProof
236 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
239 let exp_named_subst'' =
240 substaux_in_exp_named_subst uri k exp_named_subst' params
242 C.Var (uri,exp_named_subst'')
249 | Some t -> Some (substaux k t)
254 | C.Implicit _ as t -> t
255 | C.Cast (te,ty) -> C.Cast (substaux k te, substaux k ty)
256 | C.Prod (n,s,t) -> C.Prod (n, substaux k s, substaux (k + 1) t)
257 | C.Lambda (n,s,t) -> C.Lambda (n, substaux k s, substaux (k + 1) t)
258 | C.LetIn (n,s,ty,t) ->
259 C.LetIn (n, substaux k s, substaux k ty, substaux (k + 1) t)
261 (* Invariant: no Appl applied to another Appl *)
262 let tl' = List.map (substaux k) tl in
264 match substaux k he with
265 C.Appl l -> C.Appl (l@tl')
266 | _ as he' -> C.Appl (he'::tl')
268 | C.Appl _ -> assert false
269 | C.Const (uri,exp_named_subst') ->
271 let obj,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
273 C.Constant (_,_,_,params,_) -> params
274 | C.Variable _ -> raise ReferenceToVariable
275 | C.CurrentProof (_,_,_,_,params,_) -> params
276 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
279 let exp_named_subst'' =
280 substaux_in_exp_named_subst uri k exp_named_subst' params
282 C.Const (uri,exp_named_subst'')
283 | C.MutInd (uri,typeno,exp_named_subst') ->
285 let obj,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
287 C.Constant _ -> raise ReferenceToConstant
288 | C.Variable _ -> raise ReferenceToVariable
289 | C.CurrentProof _ -> raise ReferenceToCurrentProof
290 | C.InductiveDefinition (_,params,_,_) -> params
293 let exp_named_subst'' =
294 substaux_in_exp_named_subst uri k exp_named_subst' params
296 C.MutInd (uri,typeno,exp_named_subst'')
297 | C.MutConstruct (uri,typeno,consno,exp_named_subst') ->
299 let obj,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
301 C.Constant _ -> raise ReferenceToConstant
302 | C.Variable _ -> raise ReferenceToVariable
303 | C.CurrentProof _ -> raise ReferenceToCurrentProof
304 | C.InductiveDefinition (_,params,_,_) -> params
307 let exp_named_subst'' =
308 substaux_in_exp_named_subst uri k exp_named_subst' params
310 if (List.map fst exp_named_subst'' <> List.map fst (List.filter (fun (uri,_) -> List.mem uri params) exp_named_subst) @ List.map fst exp_named_subst') then (
311 debug_print (lazy "\n\n---- BEGIN ") ;
312 debug_print (lazy ("----params: " ^ String.concat " ; " (List.map UriManager.string_of_uri params))) ;
313 debug_print (lazy ("----S(" ^ UriManager.string_of_uri uri ^ "): " ^ String.concat " ; " (List.map (function (uri,_) -> UriManager.string_of_uri uri) exp_named_subst))) ;
314 debug_print (lazy ("----P: " ^ String.concat " ; " (List.map (function (uri,_) -> UriManager.string_of_uri uri) exp_named_subst'))) ;
315 debug_print (lazy ("----D: " ^ String.concat " ; " (List.map (function (uri,_) -> UriManager.string_of_uri uri) exp_named_subst''))) ;
316 debug_print (lazy "---- END\n\n ") ;
318 C.MutConstruct (uri,typeno,consno,exp_named_subst'')
319 | C.MutCase (sp,i,outt,t,pl) ->
320 C.MutCase (sp,i,substaux k outt, substaux k t,
321 List.map (substaux k) pl)
323 let len = List.length fl in
326 (fun (name,i,ty,bo) -> (name, i, substaux k ty, substaux (k+len) bo))
329 C.Fix (i, substitutedfl)
331 let len = List.length fl in
334 (fun (name,ty,bo) -> (name, substaux k ty, substaux (k+len) bo))
337 C.CoFix (i, substitutedfl)
338 and substaux_in_exp_named_subst uri k exp_named_subst' params =
339 let rec filter_and_lift =
344 (function (uri',_) -> not (UriManager.eq uri uri')) exp_named_subst'
348 (uri,lift (k-1) t)::(filter_and_lift tl)
349 | _::tl -> filter_and_lift tl
352 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst' @
353 (filter_and_lift exp_named_subst)
361 [uri,List.assoc uri res]
369 if exp_named_subst = [] then t
373 (* subst_meta [t_1 ; ... ; t_n] t *)
374 (* returns the term [t] where [Rel i] is substituted with [t_i] *)
375 (* [t_i] is lifted as usual when it crosses an abstraction *)
377 let module C = Cic in
378 if l = [] then t else
379 let rec aux k = function
381 if n <= k then t else
383 match List.nth l (n-k-1) with
384 None -> raise RelToHiddenHypothesis
387 (Failure _) -> assert false
389 | C.Var (uri,exp_named_subst) ->
390 let exp_named_subst' =
391 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
393 C.Var (uri,exp_named_subst')
403 RelToHiddenHypothesis -> None
408 | C.Implicit _ as t -> t
409 | C.Cast (te,ty) -> C.Cast (aux k te, aux k ty) (*CSC ??? *)
410 | C.Prod (n,s,t) -> C.Prod (n, aux k s, aux (k + 1) t)
411 | C.Lambda (n,s,t) -> C.Lambda (n, aux k s, aux (k + 1) t)
412 | C.LetIn (n,s,ty,t) -> C.LetIn (n, aux k s, aux k ty, aux (k + 1) t)
413 | C.Appl l -> C.Appl (List.map (aux k) l)
414 | C.Const (uri,exp_named_subst) ->
415 let exp_named_subst' =
416 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
418 C.Const (uri,exp_named_subst')
419 | C.MutInd (uri,typeno,exp_named_subst) ->
420 let exp_named_subst' =
421 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
423 C.MutInd (uri,typeno,exp_named_subst')
424 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
425 let exp_named_subst' =
426 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
428 C.MutConstruct (uri,typeno,consno,exp_named_subst')
429 | C.MutCase (sp,i,outt,t,pl) ->
430 C.MutCase (sp,i,aux k outt, aux k t, List.map (aux k) pl)
432 let len = List.length fl in
435 (fun (name,i,ty,bo) -> (name, i, aux k ty, aux (k+len) bo))
438 C.Fix (i, substitutedfl)
440 let len = List.length fl in
443 (fun (name,ty,bo) -> (name, aux k ty, aux (k+len) bo))
446 C.CoFix (i, substitutedfl)
451 Deannotate.lift := lift;;