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;;
35 let debug_print = fun _ -> ()
46 | C.Var (uri,exp_named_subst) ->
47 let exp_named_subst' =
48 List.map (function (uri,t) -> (uri,liftaux k t)) exp_named_subst
50 C.Var (uri,exp_named_subst')
56 | Some t -> Some (liftaux k t)
61 | C.Implicit _ as t -> t
62 | C.Cast (te,ty) -> C.Cast (liftaux k te, liftaux k ty)
63 | C.Prod (n,s,t) -> C.Prod (n, liftaux k s, liftaux (k+1) t)
64 | C.Lambda (n,s,t) -> C.Lambda (n, liftaux k s, liftaux (k+1) t)
65 | C.LetIn (n,s,t) -> C.LetIn (n, liftaux k s, liftaux (k+1) t)
66 | C.Appl l -> C.Appl (List.map (liftaux k) l)
67 | C.Const (uri,exp_named_subst) ->
68 let exp_named_subst' =
69 List.map (function (uri,t) -> (uri,liftaux k t)) exp_named_subst
71 C.Const (uri,exp_named_subst')
72 | C.MutInd (uri,tyno,exp_named_subst) ->
73 let exp_named_subst' =
74 List.map (function (uri,t) -> (uri,liftaux k t)) exp_named_subst
76 C.MutInd (uri,tyno,exp_named_subst')
77 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
78 let exp_named_subst' =
79 List.map (function (uri,t) -> (uri,liftaux k t)) exp_named_subst
81 C.MutConstruct (uri,tyno,consno,exp_named_subst')
82 | C.MutCase (sp,i,outty,t,pl) ->
83 C.MutCase (sp, i, liftaux k outty, liftaux k t,
84 List.map (liftaux k) pl)
86 let len = List.length fl in
89 (fun (name, i, ty, bo) -> (name, i, liftaux k ty, liftaux (k+len) bo))
94 let len = List.length fl in
97 (fun (name, ty, bo) -> (name, liftaux k ty, liftaux (k+len) bo))
100 C.CoFix (i, liftedfl)
112 (* substitutes [t1] for [Rel 1] in [t2] *)
113 (* if avoid_beta_redexes is true (default: false) no new beta redexes *)
114 (* are generated. WARNING: the substitution can diverge when t2 is not *)
115 (* well typed and avoid_beta_redexes is true. *)
116 let rec subst ?(avoid_beta_redexes=false) arg =
118 let module C = Cic in
122 n when n = k -> lift (k - 1) arg
126 | C.Var (uri,exp_named_subst) ->
127 let exp_named_subst' =
128 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst
130 C.Var (uri,exp_named_subst')
136 | Some t -> Some (substaux k t)
141 | C.Implicit _ as t -> t
142 | C.Cast (te,ty) -> C.Cast (substaux k te, substaux k ty)
143 | C.Prod (n,s,t) -> C.Prod (n, substaux k s, substaux (k + 1) t)
144 | C.Lambda (n,s,t) -> C.Lambda (n, substaux k s, substaux (k + 1) t)
145 | C.LetIn (n,s,t) -> C.LetIn (n, substaux k s, substaux (k + 1) t)
147 (* Invariant: no Appl applied to another Appl *)
148 let tl' = List.map (substaux k) tl in
150 match substaux k he with
151 C.Appl l -> C.Appl (l@tl')
153 | C.Lambda (_,_,bo) when avoid_beta_redexes ->
156 | [he] -> subst ~avoid_beta_redexes he bo
157 | he::tl -> C.Appl (subst he bo::tl))
158 | _ as he' -> C.Appl (he'::tl')
160 | C.Appl _ -> assert false
161 | C.Const (uri,exp_named_subst) ->
162 let exp_named_subst' =
163 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst
165 C.Const (uri,exp_named_subst')
166 | C.MutInd (uri,typeno,exp_named_subst) ->
167 let exp_named_subst' =
168 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst
170 C.MutInd (uri,typeno,exp_named_subst')
171 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
172 let exp_named_subst' =
173 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst
175 C.MutConstruct (uri,typeno,consno,exp_named_subst')
176 | C.MutCase (sp,i,outt,t,pl) ->
177 C.MutCase (sp,i,substaux k outt, substaux k t,
178 List.map (substaux k) pl)
180 let len = List.length fl in
183 (fun (name,i,ty,bo) -> (name, i, substaux k ty, substaux (k+len) bo))
186 C.Fix (i, substitutedfl)
188 let len = List.length fl in
191 (fun (name,ty,bo) -> (name, substaux k ty, substaux (k+len) bo))
194 C.CoFix (i, substitutedfl)
199 (*CSC: i controlli di tipo debbono essere svolti da destra a *)
200 (*CSC: sinistra: i{B/A;b/a} ==> a{B/A;b/a} ==> a{b/a{B/A}} ==> b *)
201 (*CSC: la sostituzione ora e' implementata in maniera simultanea, ma *)
202 (*CSC: dovrebbe diventare da sinistra verso destra: *)
203 (*CSC: t{a=a/A;b/a} ==> \H:a=a.H{b/a} ==> \H:b=b.H *)
204 (*CSC: per la roba che proviene da Coq questo non serve! *)
205 let subst_vars exp_named_subst t =
207 debug_print (lazy ("@@@POSSIBLE BUG: SUBSTITUTION IS NOT SIMULTANEOUS")) ;
210 let module C = Cic in
213 | C.Var (uri,exp_named_subst') ->
217 (function (varuri,_) -> UriManager.eq uri varuri) exp_named_subst
223 let obj,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
225 C.Constant _ -> raise ReferenceToConstant
226 | C.Variable (_,_,_,params,_) -> params
227 | C.CurrentProof _ -> raise ReferenceToCurrentProof
228 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
232 debug_print (lazy "\n\n---- BEGIN ") ;
233 debug_print (lazy ("----params: " ^ String.concat " ; " (List.map UriManager.string_of_uri params))) ;
234 debug_print (lazy ("----S(" ^ UriManager.string_of_uri uri ^ "): " ^ String.concat " ; " (List.map (function (uri,_) -> UriManager.string_of_uri uri) exp_named_subst))) ;
235 debug_print (lazy ("----P: " ^ String.concat " ; " (List.map (function (uri,_) -> UriManager.string_of_uri uri) exp_named_subst'))) ;
237 let exp_named_subst'' =
238 substaux_in_exp_named_subst uri k exp_named_subst' params
241 debug_print (lazy ("----D: " ^ String.concat " ; " (List.map (function (uri,_) -> UriManager.string_of_uri uri) exp_named_subst''))) ;
242 debug_print (lazy "---- END\n\n ") ;
244 C.Var (uri,exp_named_subst'')
251 | Some t -> Some (substaux k t)
256 | C.Implicit _ as t -> t
257 | C.Cast (te,ty) -> C.Cast (substaux k te, substaux k ty)
258 | C.Prod (n,s,t) -> C.Prod (n, substaux k s, substaux (k + 1) t)
259 | C.Lambda (n,s,t) -> C.Lambda (n, substaux k s, substaux (k + 1) t)
260 | C.LetIn (n,s,t) -> C.LetIn (n, substaux k s, substaux (k + 1) t)
262 (* Invariant: no Appl applied to another Appl *)
263 let tl' = List.map (substaux k) tl in
265 match substaux k he with
266 C.Appl l -> C.Appl (l@tl')
267 | _ as he' -> C.Appl (he'::tl')
269 | C.Appl _ -> assert false
270 | C.Const (uri,exp_named_subst') ->
272 let obj,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
274 C.Constant (_,_,_,params,_) -> params
275 | C.Variable _ -> raise ReferenceToVariable
276 | C.CurrentProof (_,_,_,_,params,_) -> params
277 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
280 let exp_named_subst'' =
281 substaux_in_exp_named_subst uri k exp_named_subst' params
283 C.Const (uri,exp_named_subst'')
284 | C.MutInd (uri,typeno,exp_named_subst') ->
286 let obj,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
288 C.Constant _ -> raise ReferenceToConstant
289 | C.Variable _ -> raise ReferenceToVariable
290 | C.CurrentProof _ -> raise ReferenceToCurrentProof
291 | C.InductiveDefinition (_,params,_,_) -> params
294 let exp_named_subst'' =
295 substaux_in_exp_named_subst uri k exp_named_subst' params
297 C.MutInd (uri,typeno,exp_named_subst'')
298 | C.MutConstruct (uri,typeno,consno,exp_named_subst') ->
300 let obj,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
302 C.Constant _ -> raise ReferenceToConstant
303 | C.Variable _ -> raise ReferenceToVariable
304 | C.CurrentProof _ -> raise ReferenceToCurrentProof
305 | C.InductiveDefinition (_,params,_,_) -> params
308 let exp_named_subst'' =
309 substaux_in_exp_named_subst uri k exp_named_subst' params
311 C.MutConstruct (uri,typeno,consno,exp_named_subst'')
312 | C.MutCase (sp,i,outt,t,pl) ->
313 C.MutCase (sp,i,substaux k outt, substaux k t,
314 List.map (substaux k) pl)
316 let len = List.length fl in
319 (fun (name,i,ty,bo) -> (name, i, substaux k ty, substaux (k+len) bo))
322 C.Fix (i, substitutedfl)
324 let len = List.length fl in
327 (fun (name,ty,bo) -> (name, substaux k ty, substaux (k+len) bo))
330 C.CoFix (i, substitutedfl)
331 and substaux_in_exp_named_subst uri k exp_named_subst' params =
332 (*CSC: invece di concatenare sarebbe meglio rispettare l'ordine dei params *)
333 (*CSC: e' vero???? una veloce prova non sembra confermare la teoria *)
334 let rec filter_and_lift =
339 (function (uri',_) -> not (UriManager.eq uri uri')) exp_named_subst'
343 (uri,lift (k-1) t)::(filter_and_lift tl)
344 | _::tl -> filter_and_lift tl
347 debug_print (lazy ("---- SKIPPO " ^ UriManager.string_of_uri uri)) ;
348 if List.for_all (function (uri',_) -> not (UriManager.eq uri uri'))
349 exp_named_subst' then debug_print (lazy "---- OK1") ;
350 debug_print (lazy ("++++ uri " ^ UriManager.string_of_uri uri ^ " not in " ^ String.concat " ; " (List.map UriManager.string_of_uri params))) ;
351 if List.mem uri params then debug_print (lazy "---- OK2") ;
355 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst' @
356 (filter_and_lift exp_named_subst)
358 if exp_named_subst = [] then t
362 (* subst_meta [t_1 ; ... ; t_n] t *)
363 (* returns the term [t] where [Rel i] is substituted with [t_i] *)
364 (* [t_i] is lifted as usual when it crosses an abstraction *)
366 let module C = Cic in
367 if l = [] then t else
368 let rec aux k = function
370 if n <= k then t else
372 match List.nth l (n-k-1) with
373 None -> raise RelToHiddenHypothesis
376 (Failure _) -> assert false
378 | C.Var (uri,exp_named_subst) ->
379 let exp_named_subst' =
380 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
382 C.Var (uri,exp_named_subst')
392 RelToHiddenHypothesis -> None
397 | C.Implicit _ as t -> t
398 | C.Cast (te,ty) -> C.Cast (aux k te, aux k ty) (*CSC ??? *)
399 | C.Prod (n,s,t) -> C.Prod (n, aux k s, aux (k + 1) t)
400 | C.Lambda (n,s,t) -> C.Lambda (n, aux k s, aux (k + 1) t)
401 | C.LetIn (n,s,t) -> C.LetIn (n, aux k s, aux (k + 1) t)
402 | C.Appl l -> C.Appl (List.map (aux k) l)
403 | C.Const (uri,exp_named_subst) ->
404 let exp_named_subst' =
405 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
407 C.Const (uri,exp_named_subst')
408 | C.MutInd (uri,typeno,exp_named_subst) ->
409 let exp_named_subst' =
410 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
412 C.MutInd (uri,typeno,exp_named_subst')
413 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
414 let exp_named_subst' =
415 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
417 C.MutConstruct (uri,typeno,consno,exp_named_subst')
418 | C.MutCase (sp,i,outt,t,pl) ->
419 C.MutCase (sp,i,aux k outt, aux k t, List.map (aux k) pl)
421 let len = List.length fl in
424 (fun (name,i,ty,bo) -> (name, i, aux k ty, aux (k+len) bo))
427 C.Fix (i, substitutedfl)
429 let len = List.length fl in
432 (fun (name,ty,bo) -> (name, aux k ty, aux (k+len) bo))
435 C.CoFix (i, substitutedfl)