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)
113 let module C = Cic in
117 n when n = k -> lift (k - 1) arg
121 | C.Var (uri,exp_named_subst) ->
122 let exp_named_subst' =
123 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst
125 C.Var (uri,exp_named_subst')
131 | Some t -> Some (substaux k t)
136 | C.Implicit _ as t -> t
137 | C.Cast (te,ty) -> C.Cast (substaux k te, substaux k ty)
138 | C.Prod (n,s,t) -> C.Prod (n, substaux k s, substaux (k + 1) t)
139 | C.Lambda (n,s,t) -> C.Lambda (n, substaux k s, substaux (k + 1) t)
140 | C.LetIn (n,s,t) -> C.LetIn (n, substaux k s, substaux (k + 1) t)
142 (* Invariant: no Appl applied to another Appl *)
143 let tl' = List.map (substaux k) tl in
145 match substaux k he with
146 C.Appl l -> C.Appl (l@tl')
147 | _ as he' -> C.Appl (he'::tl')
149 | C.Appl _ -> assert false
150 | C.Const (uri,exp_named_subst) ->
151 let exp_named_subst' =
152 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst
154 C.Const (uri,exp_named_subst')
155 | C.MutInd (uri,typeno,exp_named_subst) ->
156 let exp_named_subst' =
157 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst
159 C.MutInd (uri,typeno,exp_named_subst')
160 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
161 let exp_named_subst' =
162 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst
164 C.MutConstruct (uri,typeno,consno,exp_named_subst')
165 | C.MutCase (sp,i,outt,t,pl) ->
166 C.MutCase (sp,i,substaux k outt, substaux k t,
167 List.map (substaux k) pl)
169 let len = List.length fl in
172 (fun (name,i,ty,bo) -> (name, i, substaux k ty, substaux (k+len) bo))
175 C.Fix (i, substitutedfl)
177 let len = List.length fl in
180 (fun (name,ty,bo) -> (name, substaux k ty, substaux (k+len) bo))
183 C.CoFix (i, substitutedfl)
188 (*CSC: i controlli di tipo debbono essere svolti da destra a *)
189 (*CSC: sinistra: i{B/A;b/a} ==> a{B/A;b/a} ==> a{b/a{B/A}} ==> b *)
190 (*CSC: la sostituzione ora e' implementata in maniera simultanea, ma *)
191 (*CSC: dovrebbe diventare da sinistra verso destra: *)
192 (*CSC: t{a=a/A;b/a} ==> \H:a=a.H{b/a} ==> \H:b=b.H *)
193 (*CSC: per la roba che proviene da Coq questo non serve! *)
194 let subst_vars exp_named_subst t =
196 debug_print (lazy ("@@@POSSIBLE BUG: SUBSTITUTION IS NOT SIMULTANEOUS")) ;
199 let module C = Cic in
202 | C.Var (uri,exp_named_subst') ->
206 (function (varuri,_) -> UriManager.eq uri varuri) exp_named_subst
212 let obj,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
214 C.Constant _ -> raise ReferenceToConstant
215 | C.Variable (_,_,_,params,_) -> params
216 | C.CurrentProof _ -> raise ReferenceToCurrentProof
217 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
221 debug_print (lazy "\n\n---- BEGIN ") ;
222 debug_print (lazy ("----params: " ^ String.concat " ; " (List.map UriManager.string_of_uri params))) ;
223 debug_print (lazy ("----S(" ^ UriManager.string_of_uri uri ^ "): " ^ String.concat " ; " (List.map (function (uri,_) -> UriManager.string_of_uri uri) exp_named_subst))) ;
224 debug_print (lazy ("----P: " ^ String.concat " ; " (List.map (function (uri,_) -> UriManager.string_of_uri uri) exp_named_subst'))) ;
226 let exp_named_subst'' =
227 substaux_in_exp_named_subst uri k exp_named_subst' params
230 debug_print (lazy ("----D: " ^ String.concat " ; " (List.map (function (uri,_) -> UriManager.string_of_uri uri) exp_named_subst''))) ;
231 debug_print (lazy "---- END\n\n ") ;
233 C.Var (uri,exp_named_subst'')
240 | Some t -> Some (substaux k t)
245 | C.Implicit _ as t -> t
246 | C.Cast (te,ty) -> C.Cast (substaux k te, substaux k ty)
247 | C.Prod (n,s,t) -> C.Prod (n, substaux k s, substaux (k + 1) t)
248 | C.Lambda (n,s,t) -> C.Lambda (n, substaux k s, substaux (k + 1) t)
249 | C.LetIn (n,s,t) -> C.LetIn (n, substaux k s, substaux (k + 1) t)
251 (* Invariant: no Appl applied to another Appl *)
252 let tl' = List.map (substaux k) tl in
254 match substaux k he with
255 C.Appl l -> C.Appl (l@tl')
256 | _ as he' -> C.Appl (he'::tl')
258 | C.Appl _ -> assert false
259 | C.Const (uri,exp_named_subst') ->
261 let obj,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
263 C.Constant (_,_,_,params,_) -> params
264 | C.Variable _ -> raise ReferenceToVariable
265 | C.CurrentProof (_,_,_,_,params,_) -> params
266 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
269 let exp_named_subst'' =
270 substaux_in_exp_named_subst uri k exp_named_subst' params
272 C.Const (uri,exp_named_subst'')
273 | C.MutInd (uri,typeno,exp_named_subst') ->
275 let obj,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
277 C.Constant _ -> raise ReferenceToConstant
278 | C.Variable _ -> raise ReferenceToVariable
279 | C.CurrentProof _ -> raise ReferenceToCurrentProof
280 | C.InductiveDefinition (_,params,_,_) -> params
283 let exp_named_subst'' =
284 substaux_in_exp_named_subst uri k exp_named_subst' params
286 C.MutInd (uri,typeno,exp_named_subst'')
287 | C.MutConstruct (uri,typeno,consno,exp_named_subst') ->
289 let obj,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
291 C.Constant _ -> raise ReferenceToConstant
292 | C.Variable _ -> raise ReferenceToVariable
293 | C.CurrentProof _ -> raise ReferenceToCurrentProof
294 | C.InductiveDefinition (_,params,_,_) -> params
297 let exp_named_subst'' =
298 substaux_in_exp_named_subst uri k exp_named_subst' params
300 C.MutConstruct (uri,typeno,consno,exp_named_subst'')
301 | C.MutCase (sp,i,outt,t,pl) ->
302 C.MutCase (sp,i,substaux k outt, substaux k t,
303 List.map (substaux k) pl)
305 let len = List.length fl in
308 (fun (name,i,ty,bo) -> (name, i, substaux k ty, substaux (k+len) bo))
311 C.Fix (i, substitutedfl)
313 let len = List.length fl in
316 (fun (name,ty,bo) -> (name, substaux k ty, substaux (k+len) bo))
319 C.CoFix (i, substitutedfl)
320 and substaux_in_exp_named_subst uri k exp_named_subst' params =
321 (*CSC: invece di concatenare sarebbe meglio rispettare l'ordine dei params *)
322 (*CSC: e' vero???? una veloce prova non sembra confermare la teoria *)
323 let rec filter_and_lift =
328 (function (uri',_) -> not (UriManager.eq uri uri')) exp_named_subst'
332 (uri,lift (k-1) t)::(filter_and_lift tl)
333 | _::tl -> filter_and_lift tl
336 debug_print (lazy ("---- SKIPPO " ^ UriManager.string_of_uri uri)) ;
337 if List.for_all (function (uri',_) -> not (UriManager.eq uri uri'))
338 exp_named_subst' then debug_print (lazy "---- OK1") ;
339 debug_print (lazy ("++++ uri " ^ UriManager.string_of_uri uri ^ " not in " ^ String.concat " ; " (List.map UriManager.string_of_uri params))) ;
340 if List.mem uri params then debug_print (lazy "---- OK2") ;
344 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst' @
345 (filter_and_lift exp_named_subst)
347 if exp_named_subst = [] then t
351 (* subst_meta [t_1 ; ... ; t_n] t *)
352 (* returns the term [t] where [Rel i] is substituted with [t_i] *)
353 (* [t_i] is lifted as usual when it crosses an abstraction *)
355 let module C = Cic in
356 if l = [] then t else
357 let rec aux k = function
359 if n <= k then t else
361 match List.nth l (n-k-1) with
362 None -> raise RelToHiddenHypothesis
365 (Failure _) -> assert false
367 | C.Var (uri,exp_named_subst) ->
368 let exp_named_subst' =
369 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
371 C.Var (uri,exp_named_subst')
381 RelToHiddenHypothesis -> None
386 | C.Implicit _ as t -> t
387 | C.Cast (te,ty) -> C.Cast (aux k te, aux k ty) (*CSC ??? *)
388 | C.Prod (n,s,t) -> C.Prod (n, aux k s, aux (k + 1) t)
389 | C.Lambda (n,s,t) -> C.Lambda (n, aux k s, aux (k + 1) t)
390 | C.LetIn (n,s,t) -> C.LetIn (n, aux k s, aux (k + 1) t)
391 | C.Appl l -> C.Appl (List.map (aux k) l)
392 | C.Const (uri,exp_named_subst) ->
393 let exp_named_subst' =
394 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
396 C.Const (uri,exp_named_subst')
397 | C.MutInd (uri,typeno,exp_named_subst) ->
398 let exp_named_subst' =
399 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
401 C.MutInd (uri,typeno,exp_named_subst')
402 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
403 let exp_named_subst' =
404 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
406 C.MutConstruct (uri,typeno,consno,exp_named_subst')
407 | C.MutCase (sp,i,outt,t,pl) ->
408 C.MutCase (sp,i,aux k outt, aux k t, List.map (aux k) pl)
410 let len = List.length fl in
413 (fun (name,i,ty,bo) -> (name, i, aux k ty, aux (k+len) bo))
416 C.Fix (i, substitutedfl)
418 let len = List.length fl in
421 (fun (name,ty,bo) -> (name, aux k ty, aux (k+len) bo))
424 C.CoFix (i, substitutedfl)