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/.
26 exception CannotSubstInMeta;;
27 exception RelToHiddenHypothesis;;
28 exception ReferenceToVariable;;
29 exception ReferenceToConstant;;
30 exception ReferenceToCurrentProof;;
31 exception ReferenceToInductiveDefinition;;
42 | C.Var (uri,exp_named_subst) ->
43 let exp_named_subst' =
44 List.map (function (uri,t) -> (uri,liftaux k t)) exp_named_subst
46 C.Var (uri,exp_named_subst')
52 | Some t -> Some (liftaux k t)
57 | C.Implicit as t -> t
58 | C.Cast (te,ty) -> C.Cast (liftaux k te, liftaux k ty)
59 | C.Prod (n,s,t) -> C.Prod (n, liftaux k s, liftaux (k+1) t)
60 | C.Lambda (n,s,t) -> C.Lambda (n, liftaux k s, liftaux (k+1) t)
61 | C.LetIn (n,s,t) -> C.LetIn (n, liftaux k s, liftaux (k+1) t)
62 | C.Appl l -> C.Appl (List.map (liftaux k) l)
63 | C.Const (uri,exp_named_subst) ->
64 let exp_named_subst' =
65 List.map (function (uri,t) -> (uri,liftaux k t)) exp_named_subst
67 C.Const (uri,exp_named_subst')
68 | C.MutInd (uri,tyno,exp_named_subst) ->
69 let exp_named_subst' =
70 List.map (function (uri,t) -> (uri,liftaux k t)) exp_named_subst
72 C.MutInd (uri,tyno,exp_named_subst')
73 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
74 let exp_named_subst' =
75 List.map (function (uri,t) -> (uri,liftaux k t)) exp_named_subst
77 C.MutConstruct (uri,tyno,consno,exp_named_subst')
78 | C.MutCase (sp,i,outty,t,pl) ->
79 C.MutCase (sp, i, liftaux k outty, liftaux k t,
80 List.map (liftaux k) pl)
82 let len = List.length fl in
85 (fun (name, i, ty, bo) -> (name, i, liftaux k ty, liftaux (k+len) bo))
90 let len = List.length fl in
93 (fun (name, ty, bo) -> (name, liftaux k ty, liftaux (k+len) bo))
106 let module C = Cic in
110 n when n = k -> lift (k - 1) arg
114 | C.Var (uri,exp_named_subst) ->
115 let exp_named_subst' =
116 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst
118 C.Var (uri,exp_named_subst')
119 | C.Meta (i, l) as t ->
124 | Some t -> Some (substaux k t)
129 | C.Implicit as t -> t
130 | C.Cast (te,ty) -> C.Cast (substaux k te, substaux k ty)
131 | C.Prod (n,s,t) -> C.Prod (n, substaux k s, substaux (k + 1) t)
132 | C.Lambda (n,s,t) -> C.Lambda (n, substaux k s, substaux (k + 1) t)
133 | C.LetIn (n,s,t) -> C.LetIn (n, substaux k s, substaux (k + 1) t)
135 (* Invariant: no Appl applied to another Appl *)
136 let tl' = List.map (substaux k) tl in
138 match substaux k he with
139 C.Appl l -> C.Appl (l@tl')
140 | _ as he' -> C.Appl (he'::tl')
142 | C.Appl _ -> assert false
143 | C.Const (uri,exp_named_subst) ->
144 let exp_named_subst' =
145 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst
147 C.Const (uri,exp_named_subst')
148 | C.MutInd (uri,typeno,exp_named_subst) ->
149 let exp_named_subst' =
150 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst
152 C.MutInd (uri,typeno,exp_named_subst')
153 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
154 let exp_named_subst' =
155 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst
157 C.MutConstruct (uri,typeno,consno,exp_named_subst')
158 | C.MutCase (sp,i,outt,t,pl) ->
159 C.MutCase (sp,i,substaux k outt, substaux k t,
160 List.map (substaux k) pl)
162 let len = List.length fl in
165 (fun (name,i,ty,bo) -> (name, i, substaux k ty, substaux (k+len) bo))
168 C.Fix (i, substitutedfl)
170 let len = List.length fl in
173 (fun (name,ty,bo) -> (name, substaux k ty, substaux (k+len) bo))
176 C.CoFix (i, substitutedfl)
181 (*CSC: i controlli di tipo debbono essere svolti da destra a *)
182 (*CSC: sinistra: i{B/A;b/a} ==> a{B/A;b/a} ==> a{b/a{B/A}} ==> b *)
183 (*CSC: la sostituzione ora e' implementata in maniera simultanea, ma *)
184 (*CSC: dovrebbe diventare da sinistra verso destra: *)
185 (*CSC: t{a=a/A;b/a} ==> \H:a=a.H{b/a} ==> \H:b=b.H *)
186 (*CSC: per la roba che proviene da Coq questo non serve! *)
187 let subst_vars exp_named_subst =
189 prerr_endline ("@@@POSSIBLE BUG: SUBSTITUTION IS NOT SIMULTANEOUS") ;
192 let module C = Cic in
195 | C.Var (uri,exp_named_subst') ->
199 (function (varuri,_) -> UriManager.eq uri varuri) exp_named_subst
205 (match CicEnvironment.get_cooked_obj ~trust:true uri with
206 C.Constant _ -> raise ReferenceToConstant
207 | C.Variable (_,_,_,params) -> params
208 | C.CurrentProof _ -> raise ReferenceToCurrentProof
209 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
213 prerr_endline "\n\n---- BEGIN " ;
214 prerr_endline ("----params: " ^ String.concat " ; " (List.map UriManager.string_of_uri params)) ;
215 prerr_endline ("----S(" ^ UriManager.string_of_uri uri ^ "): " ^ String.concat " ; " (List.map (function (uri,_) -> UriManager.string_of_uri uri) exp_named_subst)) ;
216 prerr_endline ("----P: " ^ String.concat " ; " (List.map (function (uri,_) -> UriManager.string_of_uri uri) exp_named_subst')) ;
218 let exp_named_subst'' =
219 substaux_in_exp_named_subst uri k exp_named_subst' params
222 prerr_endline ("----D: " ^ String.concat " ; " (List.map (function (uri,_) -> UriManager.string_of_uri uri) exp_named_subst'')) ;
223 prerr_endline "---- END\n\n " ;
225 C.Var (uri,exp_named_subst'')
227 | C.Meta (i, l) as t ->
232 | Some t -> Some (substaux k t)
237 | C.Implicit as t -> t
238 | C.Cast (te,ty) -> C.Cast (substaux k te, substaux k ty)
239 | C.Prod (n,s,t) -> C.Prod (n, substaux k s, substaux (k + 1) t)
240 | C.Lambda (n,s,t) -> C.Lambda (n, substaux k s, substaux (k + 1) t)
241 | C.LetIn (n,s,t) -> C.LetIn (n, substaux k s, substaux (k + 1) t)
243 (* Invariant: no Appl applied to another Appl *)
244 let tl' = List.map (substaux k) tl in
246 match substaux k he with
247 C.Appl l -> C.Appl (l@tl')
248 | _ as he' -> C.Appl (he'::tl')
250 | C.Appl _ -> assert false
251 | C.Const (uri,exp_named_subst') ->
253 (match CicEnvironment.get_cooked_obj ~trust:true uri with
254 C.Constant (_,_,_,params) -> params
255 | C.Variable _ -> raise ReferenceToVariable
256 | C.CurrentProof (_,_,_,_,params) -> params
257 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
260 let exp_named_subst'' =
261 substaux_in_exp_named_subst uri k exp_named_subst' params
263 C.Const (uri,exp_named_subst'')
264 | C.MutInd (uri,typeno,exp_named_subst') ->
266 (match CicEnvironment.get_cooked_obj ~trust:true uri with
267 C.Constant _ -> raise ReferenceToConstant
268 | C.Variable _ -> raise ReferenceToVariable
269 | C.CurrentProof _ -> raise ReferenceToCurrentProof
270 | C.InductiveDefinition (_,params,_) -> params
273 let exp_named_subst'' =
274 substaux_in_exp_named_subst uri k exp_named_subst' params
276 C.MutInd (uri,typeno,exp_named_subst'')
277 | C.MutConstruct (uri,typeno,consno,exp_named_subst') ->
279 (match CicEnvironment.get_cooked_obj ~trust:true uri with
280 C.Constant _ -> raise ReferenceToConstant
281 | C.Variable _ -> raise ReferenceToVariable
282 | C.CurrentProof _ -> raise ReferenceToCurrentProof
283 | C.InductiveDefinition (_,params,_) -> params
286 let exp_named_subst'' =
287 substaux_in_exp_named_subst uri k exp_named_subst' params
289 C.MutConstruct (uri,typeno,consno,exp_named_subst'')
290 | C.MutCase (sp,i,outt,t,pl) ->
291 C.MutCase (sp,i,substaux k outt, substaux k t,
292 List.map (substaux k) pl)
294 let len = List.length fl in
297 (fun (name,i,ty,bo) -> (name, i, substaux k ty, substaux (k+len) bo))
300 C.Fix (i, substitutedfl)
302 let len = List.length fl in
305 (fun (name,ty,bo) -> (name, substaux k ty, substaux (k+len) bo))
308 C.CoFix (i, substitutedfl)
309 and substaux_in_exp_named_subst uri k exp_named_subst' params =
310 (*CSC: invece di concatenare sarebbe meglio rispettare l'ordine dei params *)
311 (*CSC: e' vero???? una veloce prova non sembra confermare la teoria *)
312 let rec filter_and_lift =
317 (function (uri',_) -> not (UriManager.eq uri uri')) exp_named_subst'
321 (uri,lift (k-1) t)::(filter_and_lift tl)
322 | _::tl -> filter_and_lift tl
325 prerr_endline ("---- SKIPPO " ^ UriManager.string_of_uri uri) ;
326 if List.for_all (function (uri',_) -> not (UriManager.eq uri uri')) exp_named_subst' then prerr_endline "---- OK1" ;
327 prerr_endline ("++++ uri " ^ UriManager.string_of_uri uri ^ " not in " ^ String.concat " ; " (List.map UriManager.string_of_uri params)) ;
328 if List.mem uri params then prerr_endline "---- OK2" ;
332 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst' @
333 (filter_and_lift exp_named_subst)
338 (* lift_meta [t_1 ; ... ; t_n] t *)
339 (* returns the term [t] where [Rel i] is substituted with [t_i] *)
340 (* [t_i] is lifted as usual when it crosses an abstraction *)
342 let module C = Cic in
343 if l = [] then t else
344 let rec aux k = function
346 if n <= k then t else
348 match List.nth l (n-k-1) with
349 None -> raise RelToHiddenHypothesis
352 (Failure _) -> assert false
354 | C.Var (uri,exp_named_subst) ->
355 let exp_named_subst' =
356 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
358 C.Var (uri,exp_named_subst')
368 RelToHiddenHypothesis -> None
373 | C.Implicit as t -> t
374 | C.Cast (te,ty) -> C.Cast (aux k te, aux k ty) (*CSC ??? *)
375 | C.Prod (n,s,t) -> C.Prod (n, aux k s, aux (k + 1) t)
376 | C.Lambda (n,s,t) -> C.Lambda (n, aux k s, aux (k + 1) t)
377 | C.LetIn (n,s,t) -> C.LetIn (n, aux k s, aux (k + 1) t)
378 | C.Appl l -> C.Appl (List.map (aux k) l)
379 | C.Const (uri,exp_named_subst) ->
380 let exp_named_subst' =
381 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
383 C.Const (uri,exp_named_subst')
384 | C.MutInd (uri,typeno,exp_named_subst) ->
385 let exp_named_subst' =
386 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
388 C.MutInd (uri,typeno,exp_named_subst')
389 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
390 let exp_named_subst' =
391 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
393 C.MutConstruct (uri,typeno,consno,exp_named_subst')
394 | C.MutCase (sp,i,outt,t,pl) ->
395 C.MutCase (sp,i,aux k outt, aux k t, List.map (aux k) pl)
397 let len = List.length fl in
400 (fun (name,i,ty,bo) -> (name, i, aux k ty, aux (k+len) bo))
403 C.Fix (i, substitutedfl)
405 let len = List.length fl in
408 (fun (name,ty,bo) -> (name, aux k ty, aux (k+len) bo))
411 C.CoFix (i, substitutedfl)