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;;
33 let debug_print = fun _ -> ()
44 | C.Var (uri,exp_named_subst) ->
45 let exp_named_subst' =
46 List.map (function (uri,t) -> (uri,liftaux k t)) exp_named_subst
48 C.Var (uri,exp_named_subst')
54 | Some t -> Some (liftaux k t)
59 | C.Implicit _ as t -> t
60 | C.Cast (te,ty) -> C.Cast (liftaux k te, liftaux k ty)
61 | C.Prod (n,s,t) -> C.Prod (n, liftaux k s, liftaux (k+1) t)
62 | C.Lambda (n,s,t) -> C.Lambda (n, liftaux k s, liftaux (k+1) t)
63 | C.LetIn (n,s,t) -> C.LetIn (n, liftaux k s, liftaux (k+1) t)
64 | C.Appl l -> C.Appl (List.map (liftaux k) l)
65 | C.Const (uri,exp_named_subst) ->
66 let exp_named_subst' =
67 List.map (function (uri,t) -> (uri,liftaux k t)) exp_named_subst
69 C.Const (uri,exp_named_subst')
70 | C.MutInd (uri,tyno,exp_named_subst) ->
71 let exp_named_subst' =
72 List.map (function (uri,t) -> (uri,liftaux k t)) exp_named_subst
74 C.MutInd (uri,tyno,exp_named_subst')
75 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
76 let exp_named_subst' =
77 List.map (function (uri,t) -> (uri,liftaux k t)) exp_named_subst
79 C.MutConstruct (uri,tyno,consno,exp_named_subst')
80 | C.MutCase (sp,i,outty,t,pl) ->
81 C.MutCase (sp, i, liftaux k outty, liftaux k t,
82 List.map (liftaux k) pl)
84 let len = List.length fl in
87 (fun (name, i, ty, bo) -> (name, i, liftaux k ty, liftaux (k+len) bo))
92 let len = List.length fl in
95 (fun (name, ty, bo) -> (name, liftaux k ty, liftaux (k+len) bo))
109 (* delifts a term t of n levels strating from k, that is changes (Rel m)
110 * to (Rel (m - n)) when m > (k + n). if k <= m < k + n delift fails
112 let delift_from k n =
114 let module C = Cic in
119 else if m < k + n then
120 (failwith "delifting this term whould capture free variables")
123 | C.Var (uri,exp_named_subst) ->
124 let exp_named_subst' =
125 List.map (function (uri,t) -> (uri,liftaux k t)) exp_named_subst
127 C.Var (uri,exp_named_subst')
133 | Some t -> Some (liftaux k t)
138 | C.Implicit _ as t -> t
139 | C.Cast (te,ty) -> C.Cast (liftaux k te, liftaux k ty)
140 | C.Prod (n,s,t) -> C.Prod (n, liftaux k s, liftaux (k+1) t)
141 | C.Lambda (n,s,t) -> C.Lambda (n, liftaux k s, liftaux (k+1) t)
142 | C.LetIn (n,s,t) -> C.LetIn (n, liftaux k s, liftaux (k+1) t)
143 | C.Appl l -> C.Appl (List.map (liftaux k) l)
144 | C.Const (uri,exp_named_subst) ->
145 let exp_named_subst' =
146 List.map (function (uri,t) -> (uri,liftaux k t)) exp_named_subst
148 C.Const (uri,exp_named_subst')
149 | C.MutInd (uri,tyno,exp_named_subst) ->
150 let exp_named_subst' =
151 List.map (function (uri,t) -> (uri,liftaux k t)) exp_named_subst
153 C.MutInd (uri,tyno,exp_named_subst')
154 | C.MutConstruct (uri,tyno,consno,exp_named_subst) ->
155 let exp_named_subst' =
156 List.map (function (uri,t) -> (uri,liftaux k t)) exp_named_subst
158 C.MutConstruct (uri,tyno,consno,exp_named_subst')
159 | C.MutCase (sp,i,outty,t,pl) ->
160 C.MutCase (sp, i, liftaux k outty, liftaux k t,
161 List.map (liftaux k) pl)
163 let len = List.length fl in
166 (fun (name, i, ty, bo) -> (name, i, liftaux k ty, liftaux (k+len) bo))
171 let len = List.length fl in
174 (fun (name, ty, bo) -> (name, liftaux k ty, liftaux (k+len) bo))
177 C.CoFix (i, liftedfl)
186 let module C = Cic in
190 n when n = k -> lift (k - 1) arg
194 | C.Var (uri,exp_named_subst) ->
195 let exp_named_subst' =
196 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst
198 C.Var (uri,exp_named_subst')
199 | C.Meta (i, l) as t ->
204 | Some t -> Some (substaux k t)
209 | C.Implicit _ as t -> t
210 | C.Cast (te,ty) -> C.Cast (substaux k te, substaux k ty)
211 | C.Prod (n,s,t) -> C.Prod (n, substaux k s, substaux (k + 1) t)
212 | C.Lambda (n,s,t) -> C.Lambda (n, substaux k s, substaux (k + 1) t)
213 | C.LetIn (n,s,t) -> C.LetIn (n, substaux k s, substaux (k + 1) t)
215 (* Invariant: no Appl applied to another Appl *)
216 let tl' = List.map (substaux k) tl in
218 match substaux k he with
219 C.Appl l -> C.Appl (l@tl')
220 | _ as he' -> C.Appl (he'::tl')
222 | C.Appl _ -> assert false
223 | C.Const (uri,exp_named_subst) ->
224 let exp_named_subst' =
225 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst
227 C.Const (uri,exp_named_subst')
228 | C.MutInd (uri,typeno,exp_named_subst) ->
229 let exp_named_subst' =
230 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst
232 C.MutInd (uri,typeno,exp_named_subst')
233 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
234 let exp_named_subst' =
235 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst
237 C.MutConstruct (uri,typeno,consno,exp_named_subst')
238 | C.MutCase (sp,i,outt,t,pl) ->
239 C.MutCase (sp,i,substaux k outt, substaux k t,
240 List.map (substaux k) pl)
242 let len = List.length fl in
245 (fun (name,i,ty,bo) -> (name, i, substaux k ty, substaux (k+len) bo))
248 C.Fix (i, substitutedfl)
250 let len = List.length fl in
253 (fun (name,ty,bo) -> (name, substaux k ty, substaux (k+len) bo))
256 C.CoFix (i, substitutedfl)
261 (*CSC: i controlli di tipo debbono essere svolti da destra a *)
262 (*CSC: sinistra: i{B/A;b/a} ==> a{B/A;b/a} ==> a{b/a{B/A}} ==> b *)
263 (*CSC: la sostituzione ora e' implementata in maniera simultanea, ma *)
264 (*CSC: dovrebbe diventare da sinistra verso destra: *)
265 (*CSC: t{a=a/A;b/a} ==> \H:a=a.H{b/a} ==> \H:b=b.H *)
266 (*CSC: per la roba che proviene da Coq questo non serve! *)
267 let subst_vars exp_named_subst =
269 debug_print ("@@@POSSIBLE BUG: SUBSTITUTION IS NOT SIMULTANEOUS") ;
272 let module C = Cic in
275 | C.Var (uri,exp_named_subst') ->
279 (function (varuri,_) -> UriManager.eq uri varuri) exp_named_subst
285 let obj,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
287 C.Constant _ -> raise ReferenceToConstant
288 | C.Variable (_,_,_,params,_) -> params
289 | C.CurrentProof _ -> raise ReferenceToCurrentProof
290 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
294 debug_print "\n\n---- BEGIN " ;
295 debug_print ("----params: " ^ String.concat " ; " (List.map UriManager.string_of_uri params)) ;
296 debug_print ("----S(" ^ UriManager.string_of_uri uri ^ "): " ^ String.concat " ; " (List.map (function (uri,_) -> UriManager.string_of_uri uri) exp_named_subst)) ;
297 debug_print ("----P: " ^ String.concat " ; " (List.map (function (uri,_) -> UriManager.string_of_uri uri) exp_named_subst')) ;
299 let exp_named_subst'' =
300 substaux_in_exp_named_subst uri k exp_named_subst' params
303 debug_print ("----D: " ^ String.concat " ; " (List.map (function (uri,_) -> UriManager.string_of_uri uri) exp_named_subst'')) ;
304 debug_print "---- END\n\n " ;
306 C.Var (uri,exp_named_subst'')
308 | C.Meta (i, l) as t ->
313 | Some t -> Some (substaux k t)
318 | C.Implicit _ as t -> t
319 | C.Cast (te,ty) -> C.Cast (substaux k te, substaux k ty)
320 | C.Prod (n,s,t) -> C.Prod (n, substaux k s, substaux (k + 1) t)
321 | C.Lambda (n,s,t) -> C.Lambda (n, substaux k s, substaux (k + 1) t)
322 | C.LetIn (n,s,t) -> C.LetIn (n, substaux k s, substaux (k + 1) t)
324 (* Invariant: no Appl applied to another Appl *)
325 let tl' = List.map (substaux k) tl in
327 match substaux k he with
328 C.Appl l -> C.Appl (l@tl')
329 | _ as he' -> C.Appl (he'::tl')
331 | C.Appl _ -> assert false
332 | C.Const (uri,exp_named_subst') ->
334 let obj,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
336 C.Constant (_,_,_,params,_) -> params
337 | C.Variable _ -> raise ReferenceToVariable
338 | C.CurrentProof (_,_,_,_,params,_) -> params
339 | C.InductiveDefinition _ -> raise ReferenceToInductiveDefinition
342 let exp_named_subst'' =
343 substaux_in_exp_named_subst uri k exp_named_subst' params
345 C.Const (uri,exp_named_subst'')
346 | C.MutInd (uri,typeno,exp_named_subst') ->
348 let obj,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
350 C.Constant _ -> raise ReferenceToConstant
351 | C.Variable _ -> raise ReferenceToVariable
352 | C.CurrentProof _ -> raise ReferenceToCurrentProof
353 | C.InductiveDefinition (_,params,_,_) -> params
356 let exp_named_subst'' =
357 substaux_in_exp_named_subst uri k exp_named_subst' params
359 C.MutInd (uri,typeno,exp_named_subst'')
360 | C.MutConstruct (uri,typeno,consno,exp_named_subst') ->
362 let obj,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
364 C.Constant _ -> raise ReferenceToConstant
365 | C.Variable _ -> raise ReferenceToVariable
366 | C.CurrentProof _ -> raise ReferenceToCurrentProof
367 | C.InductiveDefinition (_,params,_,_) -> params
370 let exp_named_subst'' =
371 substaux_in_exp_named_subst uri k exp_named_subst' params
373 C.MutConstruct (uri,typeno,consno,exp_named_subst'')
374 | C.MutCase (sp,i,outt,t,pl) ->
375 C.MutCase (sp,i,substaux k outt, substaux k t,
376 List.map (substaux k) pl)
378 let len = List.length fl in
381 (fun (name,i,ty,bo) -> (name, i, substaux k ty, substaux (k+len) bo))
384 C.Fix (i, substitutedfl)
386 let len = List.length fl in
389 (fun (name,ty,bo) -> (name, substaux k ty, substaux (k+len) bo))
392 C.CoFix (i, substitutedfl)
393 and substaux_in_exp_named_subst uri k exp_named_subst' params =
394 (*CSC: invece di concatenare sarebbe meglio rispettare l'ordine dei params *)
395 (*CSC: e' vero???? una veloce prova non sembra confermare la teoria *)
396 let rec filter_and_lift =
401 (function (uri',_) -> not (UriManager.eq uri uri')) exp_named_subst'
405 (uri,lift (k-1) t)::(filter_and_lift tl)
406 | _::tl -> filter_and_lift tl
409 debug_print ("---- SKIPPO " ^ UriManager.string_of_uri uri) ;
410 if List.for_all (function (uri',_) -> not (UriManager.eq uri uri'))
411 exp_named_subst' then debug_print "---- OK1" ;
412 debug_print ("++++ uri " ^ UriManager.string_of_uri uri ^ " not in " ^ String.concat " ; " (List.map UriManager.string_of_uri params)) ;
413 if List.mem uri params then debug_print "---- OK2" ;
417 List.map (function (uri,t) -> (uri,substaux k t)) exp_named_subst' @
418 (filter_and_lift exp_named_subst)
423 (* subst_meta [t_1 ; ... ; t_n] t *)
424 (* returns the term [t] where [Rel i] is substituted with [t_i] *)
425 (* [t_i] is lifted as usual when it crosses an abstraction *)
427 let module C = Cic in
428 if l = [] then t else
429 let rec aux k = function
431 if n <= k then t else
433 match List.nth l (n-k-1) with
434 None -> raise RelToHiddenHypothesis
437 (Failure _) -> assert false
439 | C.Var (uri,exp_named_subst) ->
440 let exp_named_subst' =
441 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
443 C.Var (uri,exp_named_subst')
453 RelToHiddenHypothesis -> None
458 | C.Implicit _ as t -> t
459 | C.Cast (te,ty) -> C.Cast (aux k te, aux k ty) (*CSC ??? *)
460 | C.Prod (n,s,t) -> C.Prod (n, aux k s, aux (k + 1) t)
461 | C.Lambda (n,s,t) -> C.Lambda (n, aux k s, aux (k + 1) t)
462 | C.LetIn (n,s,t) -> C.LetIn (n, aux k s, aux (k + 1) t)
463 | C.Appl l -> C.Appl (List.map (aux k) l)
464 | C.Const (uri,exp_named_subst) ->
465 let exp_named_subst' =
466 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
468 C.Const (uri,exp_named_subst')
469 | C.MutInd (uri,typeno,exp_named_subst) ->
470 let exp_named_subst' =
471 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
473 C.MutInd (uri,typeno,exp_named_subst')
474 | C.MutConstruct (uri,typeno,consno,exp_named_subst) ->
475 let exp_named_subst' =
476 List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
478 C.MutConstruct (uri,typeno,consno,exp_named_subst')
479 | C.MutCase (sp,i,outt,t,pl) ->
480 C.MutCase (sp,i,aux k outt, aux k t, List.map (aux k) pl)
482 let len = List.length fl in
485 (fun (name,i,ty,bo) -> (name, i, aux k ty, aux (k+len) bo))
488 C.Fix (i, substitutedfl)
490 let len = List.length fl in
493 (fun (name,ty,bo) -> (name, aux k ty, aux (k+len) bo))
496 C.CoFix (i, substitutedfl)