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 let debug_print = fun _ -> ();;
31 let rec liftaux k = function
33 | NCic.Sort _ as t -> t
35 if m < k then NCic.Rel m
37 | NCic.Meta (i,(m,l)) when k <= m -> NCic.Meta (i,(m+n,l))
38 | NCic.Meta (_,(m,NCic.Irl l)) as t when k > l + m -> t
39 | NCic.Meta (i,(m,l)) ->
40 let lctx = NCicUtils.expand_local_context l in
41 NCic.Meta (i, (m, NCic.Ctx (List.map (liftaux (k-m)) lctx)))
42 | NCic.Implicit _ -> (* was the identity *) assert false
43 | NCic.Prod (n,s,t) -> NCic.Prod (n, liftaux k s, liftaux (k+1) t)
44 | NCic.Lambda (n,s,t) -> NCic.Lambda (n, liftaux k s, liftaux (k+1) t)
45 | NCic.LetIn (n,ty,te,t) ->
46 NCic.LetIn (n, liftaux k ty, liftaux k te, liftaux (k+1) t)
47 | NCic.Appl l -> NCic.Appl (List.map (liftaux k) l)
48 | NCic.Match (r,outty,t,pl) ->
49 NCic.Match (r,liftaux k outty,liftaux k t, List.map (liftaux k) pl)
54 let lift ?(from=1) n t =
56 else lift_from from n t
60 (* substitutes [t1] for [Rel 1] in [t2] *)
61 (* if avoid_beta_redexes is true (default: false) no new beta redexes *)
62 (* are generated. WARNING: the substitution can diverge when t2 is not *)
63 (* well typed and avoid_beta_redexes is true. *)
64 (* map_arg is ReductionStrategy.from_env_for_unwind when psubst is *)
65 (* used to implement nCicReduction.unwind' *)
66 let rec psubst ?(avoid_beta_redexes=false) delift lift_args map_arg args =
67 let nargs = List.length args in
68 let rec substaux k = function
70 | NCic.Const _ as t -> t
73 | n when n >= (k+nargs) -> if delift then NCic.Rel (n - nargs) else t
75 | n (* k <= n < k+nargs *) ->
76 (try lift (k+lift_args) (map_arg (List.nth args (n-k)))
77 with Failure _ -> assert false))
78 | NCic.Meta (i,(m,l)) as t when m >= k + nargs - 1 ->
79 if delift then NCic.Meta (i,(m-nargs,l)) else t
80 | NCic.Meta (i,(m,(NCic.Irl l as irl))) as t when k > l + m ->
81 if delift then NCic.Meta (i,(m-nargs,irl)) else t
82 | NCic.Meta (i,(m,l)) ->
83 let lctx = NCicUtils.expand_local_context l in
84 (* 1-nargs < k-m, when <= 0 is still reasonable because we will
85 * substitute args[ k-m ... k-m+nargs-1 > 0 ] *)
86 NCic.Meta (i,(m, NCic.Ctx (List.map (substaux (k-m)) lctx)))
87 | NCic.Implicit _ -> assert false (* was identity *)
88 | NCic.Prod (n,s,t) -> NCic.Prod (n, substaux k s, substaux (k + 1) t)
89 | NCic.Lambda (n,s,t) -> NCic.Lambda (n, substaux k s, substaux (k + 1) t)
90 | NCic.LetIn (n,ty,te,t) ->
91 NCic.LetIn (n, substaux k ty, substaux k te, substaux (k + 1) t)
92 | NCic.Appl (he::tl) ->
93 (* Invariant: no Appl applied to another Appl *)
94 let rec avoid he = function
98 | NCic.Appl l -> NCic.Appl (l@args)
99 | NCic.Lambda (_,_,bo) when avoid_beta_redexes ->
100 (* map_arg is here \x.x, Obj magic is needed because
101 * we don't have polymorphic recursion w/o records *)
103 ~avoid_beta_redexes true 0 Obj.magic [Obj.magic arg] bo) tl
104 | _ as he -> NCic.Appl (he::args))
106 let tl = List.map (substaux k) tl in
107 avoid (substaux k he) tl
108 | NCic.Appl _ -> assert false
109 | NCic.Match (r,outt,t,pl) ->
110 NCic.Match (r,substaux k outt, substaux k t, List.map (substaux k) pl)
115 let subst ?avoid_beta_redexes arg =
116 psubst ?avoid_beta_redexes true 0 (fun x -> x)[arg];;
118 (* subst_meta (n, Some [t_1 ; ... ; t_n]) t *)
119 (* returns the term [t] where [Rel i] is substituted with [t_i] lifted by n *)
120 (* [t_i] is lifted as usual when it crosses an abstraction *)
121 (* subst_meta (n, Non) t -> lift n t *)
122 let subst_meta = function
124 | m, NCic.Ctx [] -> lift m
125 | m, NCic.Ctx l -> psubst false m (fun x -> x) l