- | NCic.Rel n as t ->
- (match n with
- | n when n >= (k+nargs) -> if delift then NCic.Rel (n - nargs) else t
- | n when n < k -> t
- | n (* k <= n < k+nargs *) ->
- (try lift (k+lift_args) (map_arg (List.nth args (n-k)))
- with Failure _ -> assert false))
- | NCic.Meta (i,(m,l)) as t when m >= k + nargs - 1 ->
- if delift then NCic.Meta (i,(m-nargs,l)) else t
- | NCic.Meta (i,(m,(NCic.Irl l as irl))) as t when k > l + m ->
- if delift then NCic.Meta (i,(m-nargs,irl)) else t
- | NCic.Meta (i,(m,l)) ->
- let lctx = NCicUtils.expand_local_context l in
- (* 1-nargs < k-m, when <= 0 is still reasonable because we will
- * substitute args[ k-m ... k-m+nargs-1 > 0 ] *)
- NCic.Meta (i,(m, NCic.Ctx (List.map (substaux (k-m)) lctx)))
- | NCic.Implicit _ -> assert false (* was identity *)
- | NCic.Appl (he::tl) ->
- (* Invariant: no Appl applied to another Appl *)
- let rec avoid he = function
- | [] -> he
- | arg::tl as args->
- (match he with
- | NCic.Appl l -> NCic.Appl (l@args)
- | NCic.Lambda (_,_,bo) when avoid_beta_redexes ->
- (* map_arg is here \x.x, Obj magic is needed because
- * we don't have polymorphic recursion w/o records *)
- avoid (psubst
- ~avoid_beta_redexes true 0 Obj.magic [Obj.magic arg] bo) tl
- | _ as he -> NCic.Appl (he::args))
- in
- let tl = List.map (substaux k) tl in
- avoid (substaux k he) tl
- | NCic.Appl _ -> assert false
- | t -> NCicUtils.map substaux ((+) 1) k t
- (*
- | NCic.Sort _
- | NCic.Const _ as t -> t
- | NCic.Rel n as t ->
- (match n with
- | n when n >= (k+nargs) -> if delift then NCic.Rel (n - nargs) else t
- | n when n < k -> t
- | n (* k <= n < k+nargs *) ->
- (try lift (k+lift_args) (map_arg (List.nth args (n-k)))
- with Failure _ -> assert false))
- | NCic.Meta (i,(m,l)) as t when m >= k + nargs - 1 ->
- if delift then NCic.Meta (i,(m-nargs,l)) else t
- | NCic.Meta (i,(m,(NCic.Irl l as irl))) as t when k > l + m ->
- if delift then NCic.Meta (i,(m-nargs,irl)) else t
- | NCic.Meta (i,(m,l)) ->
- let lctx = NCicUtils.expand_local_context l in
- (* 1-nargs < k-m, when <= 0 is still reasonable because we will
- * substitute args[ k-m ... k-m+nargs-1 > 0 ] *)
- NCic.Meta (i,(m, NCic.Ctx (List.map (substaux (k-m)) lctx)))
- | NCic.Implicit _ -> assert false (* was identity *)
- | NCic.Prod (n,s,t) -> NCic.Prod (n, substaux k s, substaux (k + 1) t)
- | NCic.Lambda (n,s,t) -> NCic.Lambda (n, substaux k s, substaux (k + 1) t)
- | NCic.LetIn (n,ty,te,t) ->
- NCic.LetIn (n, substaux k ty, substaux k te, substaux (k + 1) t)
- | NCic.Appl (he::tl) ->
- (* Invariant: no Appl applied to another Appl *)
- let rec avoid he = function
- | [] -> he
- | arg::tl as args->
- (match he with
- | NCic.Appl l -> NCic.Appl (l@args)
- | NCic.Lambda (_,_,bo) when avoid_beta_redexes ->
- (* map_arg is here \x.x, Obj magic is needed because
- * we don't have polymorphic recursion w/o records *)
- avoid (psubst
- ~avoid_beta_redexes true 0 Obj.magic [Obj.magic arg] bo) tl
- | _ as he -> NCic.Appl (he::args))
- in
- let tl = List.map (substaux k) tl in
- avoid (substaux k he) tl
- | NCic.Appl _ -> assert false
- | NCic.Match (r,outt,t,pl) ->
- NCic.Match (r,substaux k outt, substaux k t, List.map (substaux k) pl)
- *)
+ | NCic.Rel n as t ->
+ (match n with
+ | n when n >= (k+nargs) ->
+ if delift && nargs <> 0 then NCic.Rel (n - nargs) else t
+ | n when n < k -> t
+ | n (* k <= n < k+nargs *) ->
+ (try lift (k+lift_args) (map_arg (List.nth args (n-k)))
+ with Failure _ -> assert false))
+ | NCic.Meta (i,(m,l)) as t when m >= k + nargs - 1 ->
+ if delift && nargs <> 0 then NCic.Meta (i,(m-nargs,l)) else t
+ | NCic.Meta (i,(m,(NCic.Irl l as irl))) as t when k > l + m ->
+ if delift && nargs <> 0 then NCic.Meta (i,(m-nargs,irl)) else t
+ | NCic.Meta (i,(m,l)) ->
+ let lctx = NCicUtils.expand_local_context l in
+ (* 1-nargs < k-m, when <= 0 is still reasonable because we will
+ * substitute args[ k-m ... k-m+nargs-1 > 0 ] *)
+ NCic.Meta (i,(m, NCic.Ctx (NCicUtils.sharing_map (substaux (k-m)) lctx)))
+ | NCic.Implicit _ -> assert false (* was identity *)
+ | NCic.Appl (he::tl) as t ->
+ (* Invariant: no Appl applied to another Appl *)
+ let rec avoid he' = function
+ | [] -> he'
+ | arg::tl' as args->
+ (match he' with
+ | NCic.Appl l -> NCic.Appl (l@args)
+ | NCic.Lambda (_,_,bo) when avoid_beta_redexes ->
+ (* map_arg is here \x.x, Obj magic is needed because
+ * we don't have polymorphic recursion w/o records *)
+ avoid (psubst
+ ~avoid_beta_redexes true 0 Obj.magic [Obj.magic arg] bo) tl'
+ | _ -> if he == he' && args == tl then t else NCic.Appl (he'::args))
+ in
+ let tl = NCicUtils.sharing_map (substaux k) tl in
+ avoid (substaux k he) tl
+ | t -> NCicUtils.map (fun _ k -> k + 1) k substaux t