lang::typecheck::subst

lang/src/typecheck/subst.mo

De Bruijn shift/substitution for the canonical Term type — the two primitives the macro-expansion phase's TERM-producing macro application (defmacro name params := <term>) needs.

Standard, well-known PL theory (Pierce, Types and Programming Languages, §6/§7) — no macro-specific hygiene/gensym-renaming needed here, unlike the Rust reference's own subst_macro (rust/core/src/eval/macro_expand.rs), which is NAME-based (walks a pre-de-Bruijn-resolution Term matching NameRefs) and needs alpha_rename_body's gensym counter specifically to stop two DIFFERENT macro expansions' own introduced binders from colliding on the same name. Self-hosted's canonical Term is de-Bruijn- indexed from the parser itself — there is no "same name" for two binders to collide on, so this whole class of bug is inherently impossible here by construction. See plans/bootstrapping/self-hosted-compiler.md's macro-expansion-phase section for the full reasoning.

NOTE: this module is intentionally NOT used for the DECL-generating macro form (defmacro name params := decls { ... }) — a decl template's own top-level decl_list have no enclosing lambda binder, so a macro param referenced inside one is an ordinary FREE/qualified Term.var sentinel (DebugName.named X) reference, not a bound de Bruijn index. That form needs a separate, NAME-based substitution (mirroring the reference's own subst_macro/subst_decl_var much more directly) — a different module, not this one.

defterm_shift_above#
pub def term_shift_above (cutoff : I64) (d : I64) (t : Term) : Term

term_shift restricted to variables at or above cutoff: the OUTER references of a term being placed under new binders, leaving that term's references to the binders just introduced (indices below cutoff) alone. A constructor's field types reference its EARLIER fields at indices 0..i-1 and the enclosing context at i and above, so placing field i under i new binders -- what the Elim minor's Pi chain and lambda chain both do -- is exactly term_shift_above i i.