[step:Prove substitution preserves typing]
[claim:Structural admissibility]
Weakening and exchange are admissible for typing contexts. More precisely, if $\Delta \vdash m:M$ and $\Delta'$ is obtained from $\Delta$ by adding fresh variable declarations and permuting declarations while preserving the declared types of all free variables of $m$, then $\Delta' \vdash m:M$.
[/claim]
[proof]
We prove this by induction on the typing derivation of $\Delta \vdash m:M$. In the variable case, the required declaration is still present in $\Delta'$ with the same type. Each term-forming rule is then rebuilt from the induction hypotheses applied to its premises: abstraction extends both contexts by the same bound-variable declaration after alpha-renaming the bound variable fresh for $\Delta'$, application rebuilds the application rule, pairing rebuilds the pairing rule, projections rebuild the corresponding projection rule, injections rebuild the corresponding injection rule, and case analysis rebuilds the case rule after alpha-renaming the branch variables fresh for $\Delta'$. Hence weakening and exchange are admissible.
[/proof]
[claim:Substitution lemma]
Let $\Gamma$ be a context, let $x$ be a variable not already declared in $\Gamma$, and let $B,A$ be types. If $\Gamma,x:B \vdash r:A$ and $\Gamma \vdash a:B$, then $\Gamma \vdash r[a/x]:A$.
[/claim]
[proof]
We prove the claim by induction on the typing derivation of $\Gamma,x:B \vdash r:A$.
If the final rule is the variable rule, then $r$ is a variable $z$. There are two cases. If $z=x$, then the typing derivation gives $A=B$, and $r[a/x]=a$, so $\Gamma \vdash r[a/x]:A$ follows from the hypothesis $\Gamma \vdash a:B$. If $z \neq x$, then $z:A$ must occur in $\Gamma$, and $r[a/x]=z$, so the variable rule gives $\Gamma \vdash z:A$.
If the final rule is abstraction, then $r=\lambda y:C.\,r_0$ and the derivation has premise
\begin{align*}
\Gamma,x:B,y:C \vdash r_0:D
\end{align*}
with $A=C \to D$. Renaming bound variables if necessary, assume $y \neq x$ and $y$ is not free in $a$. By exchange, view the premise as $\Gamma,y:C,x:B \vdash r_0:D$. By weakening applied to $\Gamma \vdash a:B$, we have $\Gamma,y:C \vdash a:B$. The induction hypothesis applied in the context $\Gamma,y:C$ gives
\begin{align*}
\Gamma,y:C \vdash r_0[a/x]:D.
\end{align*}
The abstraction typing rule gives
\begin{align*}
\Gamma \vdash \lambda y:C.\,r_0[a/x] : C \to D.
\end{align*}
Since $(\lambda y:C.\,r_0)[a/x]=\lambda y:C.\,r_0[a/x]$, this is the desired judgment.
If the final rule is application, then $r=r_1\,r_2$ and there is a type $C$ such that
\begin{align*}
\Gamma,x:B \vdash r_1:C \to A,
\qquad
\Gamma,x:B \vdash r_2:C.
\end{align*}
The induction hypothesis gives
\begin{align*}
\Gamma \vdash r_1[a/x]:C \to A,
\qquad
\Gamma \vdash r_2[a/x]:C.
\end{align*}
The application rule gives $\Gamma \vdash r_1[a/x]\,r_2[a/x]:A$, which is exactly $\Gamma \vdash (r_1\,r_2)[a/x]:A$.
If the final rule is pairing, then $r=\langle r_1,r_2\rangle$ and $A=C \times D$ for types $C,D$, with premises
\begin{align*}
\Gamma,x:B \vdash r_1:C,
\qquad
\Gamma,x:B \vdash r_2:D.
\end{align*}
Applying the induction hypothesis to both premises and then the pairing rule gives
\begin{align*}
\Gamma \vdash \langle r_1[a/x],r_2[a/x]\rangle:C \times D.
\end{align*}
If the final rule is a projection rule, the result follows by applying the induction hypothesis to the premise typing the projected term and then reapplying the same projection rule.
If the final rule is an injection rule, the result follows by applying the induction hypothesis to the premise typing the injected term and then reapplying the same injection rule.
If the final rule is case analysis, then
\begin{align*}
r=\operatorname{case}(r_0,y.p,z.q),
\end{align*}
and there are types $C,D$ such that
\begin{align*}
\Gamma,x:B \vdash r_0:C+D,
\qquad
\Gamma,x:B,y:C \vdash p:A,
\qquad
\Gamma,x:B,z:D \vdash q:A.
\end{align*}
After renaming bound variables, assume $y,z$ are distinct from $x$ and not free in $a$. By weakening, $\Gamma,y:C \vdash a:B$ and $\Gamma,z:D \vdash a:B$. By exchange, view the branch premises as $\Gamma,y:C,x:B \vdash p:A$ and $\Gamma,z:D,x:B \vdash q:A$. The induction hypothesis gives
\begin{align*}
\Gamma \vdash r_0[a/x]:C+D,
\qquad
\Gamma,y:C \vdash p[a/x]:A,
\qquad
\Gamma,z:D \vdash q[a/x]:A.
\end{align*}
The case typing rule gives
\begin{align*}
\Gamma \vdash \operatorname{case}(r_0[a/x],y.p[a/x],z.q[a/x]):A.
\end{align*}
This term is exactly $r[a/x]$. Hence $\Gamma \vdash r[a/x]:A$.
All typing rules have been covered, so the substitution lemma follows.
[/proof]
[/step]