\documentclass{article} \input{exer-defs} \newcommand{\nats}{\mathbb{N}} \newcommand{\dom}{\mathsf{dom}} \newcommand{\ran}{\mathsf{ran}} \newcommand{\goesto}{\mathrel{::=}} \newcommand{\typrel}{\mathrel{:}} \newcommand{\Unit}{\mathsf{Unit}} \newcommand{\unit}{\mathsf{unit}} \newcommand{\FV}{\mathsf{FV}} %\renewcommand{\thepage}{} \begin{document} \begin{center}\large\bf CIS 705 --- Programming Languages --- Spring 2009 \end{center} \begin{center}\Large\bf Assignment 5 \end{center} \begin{center}\large\bf Model Answers \end{center} \section*{Exercise 1} \renewcommand{\thesection}{1} \theoremnumber{1} For all closed terms $s$ and $s'$, if $s\fun s'$, then one of the following conditions holds: \begin{description} \item[\quad(E-App1)] there are closed terms $t_1$, $t'_1$ and $t_2$ such that $s=t_1\,t_2$, $s'=t'_1\,t_2$ and $t_1\fun t'_1$; \item[\quad(E-App2)] there are a closed value $v_1$ and closed terms $t_2$ and $t'_2$ such that $s=v_1\,t_2$, $s'=v_1\,t'_2$ and $t_2\fun t'_2$; or \item[\quad(E-AppAbs)] there are a variable $x$, a term $t$ such that $\FV(t)\sub\{x\}$, and a closed value $v$ such that $s=(\lambda x.\,t)v$ and $s'=[x\mapsto v]t$. \end{description} \section*{Exercise 2} \renewcommand{\thesection}{2} \theoremnumber{1} Suppose $P$ is a binary relation on closed terms. We sometimes write ``$P(t,t')$'' for ``$(t,t')\in P$''. The \emph{principle of induction on the evaluation relation} says that, \begin{center} for all closed terms $t$ and $t'$, if $t\fun t'$, then $P(t,t')$, \end{center} follows from showing \begin{description} \item[\quad(E-App1)] for all closed terms $t_1$, $t'_1$ and $t_2$, if $t_1\fun t'_1$ and $P(t_1,t'_1)$, then $P(t_1\,t_2, t'_1\,t_2)$; \item[\quad(E-App2)] for all closed values $v_1$ and closed terms $t_2$ and $t'_2$, if $t_2\fun t'_2$ and $P(t_2,t'_2)$, then $P(v_1\,t_2, v_1\,t'_2)$; \item[\quad(E-AppAbs)] for all variables $x$, terms $t$ such that $\FV(t)\sub\{x\}$, and closed values $v$, $P((\lambda x.\,t)v, [x\mapsto v]t)$. \end{description} \section*{Exercise 3} \renewcommand{\thesection}{3} \theoremnumber{1} For all contexts $\Gamma$, terms $s$ and types $T$, if $\Gamma\vdash s\typrel T$, then one of the following conditions holds: \begin{description} \item[\quad(T-Unit)] $s=\unit$ and $T=\Unit$; \item[\quad(T-Var)] there is a variable $x$ such that $s=x$ and $(x, T)\in\Gamma$; \item[\quad(T-Abs)] there are a variable $x$, a term $t$ and types $T_1$ and $T_2$ such that $s=\lambda x.\,t$, $T=T_1\fun T_2$ and $\Gamma[x\mapsto T_1]\vdash t\typrel T_2$; or \item[\quad(T-App)] there are terms $t_1$ and $t_2$ and types $T_1$ and $T_2$ such that $s=t_1\,t_2$, $T=T_2$, $\Gamma\vdash t_1\typrel T_1\fun T_2$ and $\Gamma\vdash t_2\typrel T_1$. \end{description} \section*{Exercise 4} \renewcommand{\thesection}{4} \theoremnumber{1} Suppose $P$ is a ternary relation between contexts, terms and types. We sometimes write ``$P(\Gamma,t,T)$'' for ``$(\Gamma,t,T)\in P$''. The \emph{principle of induction on the typing relation} says that, \begin{center} for all contexts $\Gamma$, terms $t$ and types $T$, if $\Gamma\vdash t\typrel T$, then $P(\Gamma,t,T)$ \end{center} follows from showing \begin{description} \item[\quad(T-Unit)] for all contexts $\Gamma$, $P(\Gamma,\unit,\Unit)$; \item[\quad(T-Var)] for all contexts $\Gamma$, variables $x$ and types $T$, if $(x,T)\in\Gamma$, then $P(\Gamma,x,T)$; \item[\quad(T-Abs)] for all contexts $\Gamma$, variables $x$, terms $t$, and types $T_1$ and $T_2$, if $\Gamma[x\mapsto T_1]\vdash t\typrel T_2$ and $P(\Gamma[x\mapsto T_1], t, T_2)$, then $P(\Gamma,\lambda x.\,t,T_1\fun T_2)$; \item[\quad(T-App)] for all contexts $\Gamma$, terms $t_1$ and $t_2$, and types $T_1$ and $T_2$, if $\Gamma\vdash t_1\typrel T_1\fun T_2$, $\Gamma\vdash t_2\typrel T_1$, $P(\Gamma,t_1,T_1\fun T_2)$ and $P(\Gamma,t_2,T_1)$, then $P(\Gamma, t_1\,t_2, T_2)$. \end{description} \section*{Exercise 5} \renewcommand{\thesection}{5} \theoremnumber{1} Let $P = \setof{(\Gamma,t,T)}{\Gamma\eqtxtl{is a context}, t\eqtxtl{is a term}, T \eqtxt{is a type and} \FV(t)\sub\dom(\Gamma)}$. We use induction on the typing relation to prove that, for all contexts $\Gamma$, terms $t$ and types $T$, if $\Gamma\vdash t\typrel T$, then $P(\Gamma,t,T)$. \begin{description} \item[\quad(T-Unit)] Suppose $\Gamma$ is a context. We must show that $P(\Gamma,\unit,\Unit)$, and this follows since $\FV(\unit) = \emptyset \sub \dom(\Gamma)$. \item[\quad(T-Var)] Suppose $\Gamma$ is a context, $x$ is a variable, $T$ is a type and $(x,T)\in\Gamma$. We must show that $P(\Gamma,x,T)$, and this follows since $\FV(x)=\{x\}\sub\dom(\Gamma)$. \item[\quad(T-Abs)] Suppose $\Gamma$ is a context, $x$ is a variable, $t$ is a term, $T_1$ and $T_2$ are types, and $\Gamma[x\mapsto T_1]\vdash t\typrel T_2$, and assume the inductive hypothesis, $P(\Gamma[x\mapsto T_1], t, T_2)$. We must show that $P(\Gamma,\lambda x.\,t,T_1\fun T_2)$. By the inductive hypothesis, we have that $\FV(t)\sub \dom(\Gamma[x\mapsto T_1]) = \dom(\Gamma)\cup\{x\}$. Suppose $y\in\FV(\lambda x.\,t)$. Since $\FV(\lambda x.\,t) = \FV(t)\setminus \{x\}$, it follows that $y\in\FV(t)$ and $y\neq x$. Thus $y\in\FV(t)\sub\dom(\Gamma)\cup\{x\}$. But $y\neq x$, and thus $y\in\dom(\Gamma)$, as required. \item[\quad(T-App)] Suppose $\Gamma$ is a context, $t_1$ and $t_2$ are terms, $T_1$ and $T_2$ are types, $\Gamma\vdash t_1\typrel T_1\fun T_2$ and $\Gamma\vdash t_2\typrel T_1$, and assume the inductive hypothesis, $P(\Gamma,t_1,T_1\fun T_2)$ and $P(\Gamma,t_2,T_1)$. We must show that $P(\Gamma, t_1\,t_2, T_2)$. By the inductive hypothesis, we have that $\FV(t_1)\sub\dom(\Gamma)$ and $\FV(t_2)\sub\dom(\Gamma)$. Thus $\FV(t_1\,t_2)=\FV(t_1)\cup\FV(t_2)\sub\dom(\Gamma)\cup \dom(\Gamma) = \dom(\Gamma)$. \end{description} \section*{Exercise 6} \renewcommand{\thesection}{6} \theoremnumber{1} Suppose $v$ is a closed value. \begin{itemize} \item Suppose $\emptyset\vdash v\typrel\Unit$. By inversion on the typing relation, there are four cases to consider. \begin{description} \item[\quad(T-Unit)] Suppose $v=\unit$ and $\Unit=\Unit$. Then $v=\unit$. \item[\quad(T-Var)] Suppose there is a variable $x$ such that $v=x$ and $(x, \Unit)\in\Gamma$. But this contradicts the fact that $v$ is closed. Thus $v=\unit$. \item[\quad(T-Abs)] Suppose there are a variable $x$, a term $t$ and types $T_1$ and $T_2$ such that $v=\lambda x.\,t$, $\Unit=T_1\fun T_2$ and $\Gamma[x\mapsto T_1]\vdash t\typrel T_2$. But $\Unit=T_1\fun T_2$ is a contradiction, and thus $v=\unit$. \item[\quad(T-App)] Suppose there are terms $t_1$ and $t_2$ and types $T_1$ and $T_2$ such that $v=t_1\,t_2$, $\Unit=T_2$, $\Gamma\vdash t_1\typrel T_1\fun T_2$ and $\Gamma\vdash t_2\typrel T_1$. Thus $t_1\,t_2=v$ is a value---contradiction. Hence $v=\unit$. \end{description} \item Suppose $\emptyset\vdash v\typrel S_1\fun S_2$ for some types $S_1$ and $S_2$. By inversion on the typing relation, there are four cases to consider. \begin{description} \item[\quad(T-Unit)] Suppose $v=\unit$ and $S_1\fun S_2=\Unit$. But $S_1\fun S_2=\Unit$ is a contradiction, and thus $v$ is an abstraction. \item[\quad(T-Var)] Suppose there is a variable $x$ such that $v=x$ and $(x, S_1\fun S_2)\in\Gamma$. But this contradicts the fact that $v$ is closed. Thus $v$ is an abstraction. \item[\quad(T-Abs)] Suppose there are a variable $x$, a term $t$ and types $T_1$ and $T_2$ such that $v=\lambda x.\,t$, $S_1\fun S_2=T_1\fun T_2$ and $\Gamma[x\mapsto T_1]\vdash t\typrel T_2$. Thus $v$ is an abstraction. \item[\quad(T-App)] Suppose there are terms $t_1$ and $t_2$ and types $T_1$ and $T_2$ such that $v=t_1\,t_2$, $S_1\fun S_2=T_2$, $\Gamma\vdash t_1\typrel T_1\fun T_2$ and $\Gamma\vdash t_2\typrel T_1$. Thus $t_1\,t_2=v$ is a value---contradiction. Hence $v$ is an abstraction. \end{description} \end{itemize} \section*{Exercise 7} \renewcommand{\thesection}{7} \theoremnumber{1} We prove the proposition. Let $P=\setof{(t,t')}{t\eqtxt{and}t'\eqtxt{are closed terms, and, for all closed terms}t'', \eqtxt{if} t\fun t'', \eqtxt{then} t'=t''}$. We use induction on the evaluation relation to show that, for all closed terms $t$ and $t'$, if $t\fun t'$, then $P(t,t')$. (Then, suppose $t$, $t'$ and $t''$ are closed terms, $t\fun t'$ and $t\fun t''$. Thus $P(t,t')$, so that, if $t\fun t''$, then $t'=t''$. But $t\fun t''$, and thus $t'=t''$.) \begin{description} \item[\quad(E-App1)] Suppose $t_1$, $t'_1$ and $t_2$ are closed terms, and $t_1\fun t'_1$, and assume the inductive hypothesis, $P(t_1,t'_1)$. We must show that $P(t_1\,t_2, t'_1\,t_2)$. Suppose $u$ is a closed term and $t_1\,t_2\fun u$. Because $t_1\fun t'_1$, we have that $t_1$ is not a normal form, and thus that $t_1$ is not a value. Thus, by inversion of the evaluation relation, only (E-App1) applies, and we have that there is a closed term $t''_1$ such that $u=t''_1\,t_2$ and $t_1\fun t''_1$. By the inductive hypothesis, it follows that $t'_1=t''_1$. Thus $t'_1\,t_2 = t''_1\,t_2 = u$. \item[\quad(E-App2)] Suppose $v_1$ is a closed value, $t_2$ and $t'_2$ are closed terms, and $t_2\fun t'_2$, and assume the inductive hypothesis, $P(t_2,t'_2)$. We must show that $P(v_1\,t_2, v_1\,t'_2)$. Suppose $u$ is a closed term and $v_1\,t_2\fun u$. Because $t_2\fun t'_2$, we have that $t_2$ is not a normal form, and thus that $t_2$ is not a value. Furthermore, $v_1$ is a value, and thus is a normal form. Hence, by inversion of the evaluation relation, only (E-App2) applies, and we have that there is a closed term $t''_2$ such that $u=v_1\,t''_2$ and $t_2\fun t''_2$. By the inductive hypothesis, it follows that $t'_2=t''_2$. Thus $v_1\,t'_2 = v_1t''_2 = u$. \item[\quad(E-AppAbs)] Suppose $x$ is a variable, $t$ is a term such that $\FV(t)\sub\{x\}$, and $v$ is a value. We must show that $P((\lambda x.\,t)v, [x\mapsto v]t)$. Suppose $u$ is a closed term such that $(\lambda x.\,t)v\fun u$. Because $\lambda x.\,t$ is a value, it is a normal form. And, because $v$ is a value, it is a normal form. Thus, by inversion of the evaluation relation, only (E-AppAbs) applies, and we have that $u=[x\mapsto v]t$, so that $[x\mapsto v]t = u$. \end{description} \section*{Exercise 8} \renewcommand{\thesection}{8} \theoremnumber{1} We disprove the proposition. Let $x$ be a variable. We will show that $\emptyset\vdash\lambda x.\,x\typrel\Unit\fun\Unit$ and $\emptyset\vdash \lambda x.\,x\typrel(\Unit\fun\Unit)\fun(\Unit\fun\Unit)$. \begin{itemize} \item Because $(x,\Unit)\in\{(x,\Unit)\}$, (T-Var) tells us that $\{(x,\Unit)\}\vdash x\typrel\Unit$. But $\{(x,\Unit)\} = \emptyset[x\mapsto\Unit]$, and thus $\emptyset[x\mapsto\Unit]\vdash x\typrel\Unit$. Thus $\emptyset\vdash\lambda x.\,x\typrel\Unit\fun\Unit$, by (T-Abs). \item Because $(x,\Unit\fun\Unit)\in\{(x,\Unit\fun\Unit)\}$, (T-Var) tells us that $\{(x,\Unit\fun\Unit)\}\vdash x\typrel\Unit\fun\Unit$. But $\{(x,\Unit\fun\Unit)\} = \emptyset[x\mapsto\Unit\fun\Unit]$, and thus $\emptyset[x\mapsto\Unit\fun\Unit]\vdash x\typrel\Unit\fun\Unit$. Thus $\emptyset\vdash\lambda x.\,x\typrel(\Unit\fun\Unit)\fun(\Unit\fun\Unit)$, by (T-Abs). \end{itemize} \section*{Exercise 9} \renewcommand{\thesection}{9} \theoremnumber{1} We prove the theorem. Let $P = \setof{(\Gamma,t,T)}{\Gamma\eqtxtl{is a context}, t\eqtxtl{is a term}, T \eqtxt{is a type and, if} \Gamma=\emptyset, \eqtxt{then} t \eqtxtl{is a closed term that is not stuck}}$. We use induction on the typing relation to prove that, for all contexts $\Gamma$, terms $t$ and types $T$, if $\Gamma\vdash t\typrel T$, then $P(\Gamma,t,T)$. (Then, suppose $t$ is a closed term, and $t$ is well-typed. Thus $\emptyset\vdash t\typrel T$ for some type $T$. By the result of the induction, we have that $P(\emptyset,t,T)$. Thus, if $\emptyset = \emptyset$, then $t$ is a closed term that is not stuck. But $\emptyset=\emptyset$, and thus $t$ is not stuck.) \begin{description} \item[\quad(T-Unit)] Suppose $\Gamma$ is a context. We must show that $P(\Gamma,\unit,\Unit)$, and this follows because $\unit$ is a closed value, and so is a closed term that is not stuck. \item[\quad(T-Var)] Suppose $\Gamma$ is a context, $x$ is a variable, $T$ is a type and $(x,T)\in\Gamma$. We must show that $P(\Gamma,x,T)$. Suppose $\Gamma=\emptyset$. We must show that $x$ is a closed term that is not stuck. But $(x,T)\in\Gamma=\emptyset$---contradiction. Thus $x$ is a closed term that is not stuck. \item[\quad(T-Abs)] Suppose $\Gamma$ is a context, $x$ is a variable, $t$ is a term, $T_1$ and $T_2$ are types, and $\Gamma[x\mapsto T_1]\vdash t\typrel T_2$, and assume the inductive hypothesis, $P(\Gamma[x\mapsto T_1], t, T_2)$. We must show that $P(\Gamma,\lambda x.\,t,T_1\fun T_2)$. Suppose $\Gamma=\emptyset$. We must show that $\lambda x.\,t$ is a closed term that is not stuck. Because $\emptyset[x\mapsto T_1]\vdash t\typrel T_2$, the Free Variables Lemma (Exercise~5) tells us that $\FV(t)\sub\dom(\emptyset[x\mapsto T_1]) = \dom(\emptyset)\cup \{x\} = \emptyset\cup\{x\}=\{x\}$. Thus $\FV(\lambda x.\,t) = \FV(t)\setminus \{x\} \sub \{x\}\setminus\{x\} = \emptyset$, showing that $\lambda x.\,t$ is closed. And $\lambda x.\,t$ is a value, showing that $\lambda x.\,t$ is not stuck. Thus $\lambda x.\,t$ is a closed term that is not stuck. \item[\quad(T-App)] Suppose $\Gamma$ is a context, $t_1$ and $t_2$ are terms, $T_1$ and $T_2$ are types, $\Gamma\vdash t_1\typrel T_1\fun T_2$ and $\Gamma\vdash t_2\typrel T_1$, and assume the inductive hypothesis, $P(\Gamma,t_1,T_1\fun T_2)$ and $P(\Gamma,t_2,T_1)$. We must show that $P(\Gamma, t_1\,t_2, T_2)$. Suppose $\Gamma=\emptyset$. We must show that $t_1\,t_2$ is a closed term that is not stuck. By $P(\Gamma,t_1,T_1\fun T_2)$, we have that $t_1$ is a closed term that is not stuck. And, by $P(\Gamma,t_2,T_1)$, $t_2$ is a closed term that is not stuck. Thus $t_1\,t_2$ is closed. Because $t_1$ is a closed term that is not stuck, there are two cases to consider. \begin{itemize} \item Suppose $t_1$ is not a normal form. Thus there is a closed term $t'_1$ such that $t_1\fun t'_1$. Hence $t_1\,t_2\fun t'_1\,t_2$, by (E-App1), showing that $t_1\,t_2$ is not a normal form, and so is not stuck. Thus $t_1\,t_2$ is a closed term that is not stuck. \item Suppose $t_1$ is a closed value. Because $t_2$ is a closed term that is not stuck, there are two subcases to consider. \begin{itemize} \item Suppose $t_2$ is not a normal form. Thus there is a closed term $t'_2$ such that $t_2\fun t'_2$. Hence $t_1\,t_2\fun t_1\,t'_2$, by (E-App2), because $t_1$ is a value, showing that $t_1\,t_2$ is not a normal form, and so is not stuck. Thus $t_1\,t_2$ is a closed term that is not stuck. \item Suppose $t_2$ is closed value. Because $t_1$ is a closed value and $\emptyset\vdash t_1\typrel T_1\fun T_2$, the Canonical Forms Lemma (Exercise~6) tells us that $t_1$ is an abstraction. Thus $t_1=\lambda x.\,u$ for a variable $x$ and a term $u$. Because $t_2$ is a value, we have that $t_1\,t_2=(\lambda x.\,u)t_2\fun [x\mapsto t_2]u$, by (E-AppAbs), showing that $t_1\,t_2$ is not a normal form, and so is not stuck. Thus $t_1\,t_2$ is a closed term that is not stuck. \end{itemize} \end{itemize} \end{description} \section*{Exercise 10} \renewcommand{\thesection}{10} \theoremnumber{1} We prove the theorem. First, we prove two supporting lemmas. \begin{lemma}[Weakening] \label{Weakening} For all contexts $\Gamma$ and $\Gamma'$, terms $t$ and types $T$, if $\Gamma\vdash t\typrel T$ and $\Gamma\sub\Gamma'$, then $\Gamma'\vdash t\typrel T$. \end{lemma} \begin{proof} Let $P = \setof{(\Gamma,t,T)}{\Gamma\eqtxtl{is a context}, t\eqtxtl{is a term}, T \eqtxt{is a type and, for all contexts}\Gamma', \eqtxt{if} \Gamma\sub\Gamma', \eqtxt{then} \Gamma'\vdash t\typrel T}$. We use induction on the typing relation to prove that, for all contexts $\Gamma$, terms $t$ and types $T$, if $\Gamma\vdash t\typrel T$, then $P(\Gamma,t,T)$. (Then, suppose $\Gamma$ and $\Gamma'$ are contexts, $t$ is a term, $T$ is a type, $\Gamma\vdash t\typrel T$ and $\Gamma\sub\Gamma'$. By the result of the induction, $P(\Gamma,t,T)$. Thus, since $\Gamma\sub\Gamma'$, we have that $\Gamma'\vdash t\typrel T$.) \begin{description} \item[\quad(T-Unit)] Suppose $\Gamma$ is a context. We must show that $P(\Gamma,\unit,\Unit)$. Suppose $\Gamma'$ is a context and $\Gamma\sub\Gamma'$. We must show that $\Gamma'\vdash\unit\typrel\Unit$, and this follows by (T-Unit). \item[\quad(T-Var)] Suppose $\Gamma$ is a context, $x$ is a variable, $T$ is a type and $(x,T)\in\Gamma$. We must show that $P(\Gamma,x,T)$. Suppose $\Gamma'$ is a context and $\Gamma\sub \Gamma'$. We must show that $\Gamma'\vdash x\typrel T$, and this follows by (T-Var), since $(x,T)\in\Gamma\sub\Gamma'$. \item[\quad(T-Abs)] Suppose $\Gamma$ is a context, $x$ is a variable, $t$ is a term, $T_1$ and $T_2$ are types, and $\Gamma[x\mapsto T_1]\vdash t\typrel T_2$, and assume the inductive hypothesis, $P(\Gamma[x\mapsto T_1], t, T_2)$. We must show that $P(\Gamma,\lambda x.\,t,T_1\fun T_2)$. Suppose $\Gamma'$ is a context and $\Gamma\sub\Gamma'$. We must show that $\Gamma'\vdash\lambda x.\,t\typrel T_1\fun T_2$. Because of (T-Abs), it will suffice to show that $\Gamma'[x\mapsto T_1]\vdash t\typrel T_2$. Thus, by the inductive hypothesis, it will suffice to show that $\Gamma[x\mapsto T_1]\sub\Gamma'[x\mapsto T_1]$. Suppose $(y,T)\in\Gamma[x\mapsto T_1]$. We must show that $(y,T)\in\Gamma'[x\mapsto T_1]$. There are two cases to consider. \begin{itemize} \item Suppose $y=x$. Because $(y,T)\in\Gamma[x\mapsto T_1]$, we have that $\Gamma[x\mapsto T_1](y) = T$. But $y=x$, and thus $\Gamma[x\mapsto T_1](y) = T_1$, so that $T=T_1$. Thus $(y,T)=(x,T_1)\in\Gamma'[x\mapsto T_1]$. \item Suppose $y\neq x$. Since $\Gamma[x\mapsto T_1](y)=T$ and $y\neq x$, it follows that $T=\Gamma[x\mapsto T_1](y) = \Gamma(y)$, so that $(y,T)\in\Gamma\sub\Gamma'$. Thus $\Gamma'[x\mapsto T_1](y) = \Gamma'(y)=T$, showing that $(y,T)\in\Gamma'[x\mapsto T_1]$. \end{itemize} \item[\quad(T-App)] Suppose $\Gamma$ is a context, $t_1$ and $t_2$ are terms, $T_1$ and $T_2$ are types, $\Gamma\vdash t_1\typrel T_1\fun T_2$ and $\Gamma\vdash t_2\typrel T_1$, and assume the inductive hypothesis, $P(\Gamma,t_1,T_1\fun T_2)$ and $P(\Gamma,t_2,T_1)$. We must show that $P(\Gamma, t_1\,t_2, T_2)$. Suppose $\Gamma'$ is a context and $\Gamma\sub\Gamma'$. We must show that $\Gamma'\vdash t_1\,t_2\typrel T_2$. By the inductive hypothesis, we have that $\Gamma'\vdash t_1\typrel T_1\fun T_2$ and $\Gamma'\vdash t_2\typrel T_1$. Thus, by (T-App), it follows that $\Gamma'\vdash t_1\,t_2\typrel T_2$. \end{description} \end{proof} \begin{lemma}[Typing of Substitutions] \label{Substitution} For all contexts $\Gamma$, variables $y$, terms $t$, values $v$ and types $T,T'$, if $\Gamma[y\mapsto T']\vdash t\typrel T$ and $\emptyset\vdash v\typrel T'$, then $\Gamma\vdash[y\mapsto v]t\typrel T$. \end{lemma} \begin{proof} It suffices to show that, for all variables $y$, values $v$ and types $T'$, if $\emptyset\vdash v\typrel T'$, then, for all contexts $\Gamma$, terms $t$ and types $T$, if $\Gamma[y\mapsto T']\vdash t\typrel T$, then $\Gamma\vdash[y\mapsto v]t\typrel T$. Suppose $y$ is a variable, $v$ is a value, $T'$ is a type, and $\emptyset\vdash v\typrel T'$. We must show that, for all contexts $\Gamma$, terms $t$ and types $T$, if $\Gamma[y\mapsto T']\vdash t\typrel T$, then $\Gamma\vdash[y\mapsto v]t\typrel T$. Let $P = \setof{(\Gamma,t,T)}{\Gamma\eqtxtl{is a context}, t\eqtxtl{is a term}, T \eqtxt{is a type and, for all contexts}\Gamma', \eqtxt{if} \Gamma=\Gamma'[y\mapsto T'], \eqtxt{then} \Gamma'\vdash [y\mapsto v]t\typrel T}$. We use induction on the typing relation to prove that, for all contexts $\Gamma$, terms $t$ and types $T$, if $\Gamma\vdash t\typrel T$, then $P(\Gamma,t,T)$. (Then, suppose $\Gamma$ is a context, $t$ is a term, $T$ is a type and $\Gamma[y\mapsto T']\vdash t\typrel T$. We must show that $\Gamma\vdash[y\mapsto v]t\typrel T$. By the result of the induction, we have that $P(\Gamma[y\mapsto T'],t,T)$, so that for all contexts $\Gamma'$, if $\Gamma[y\mapsto T']=\Gamma'[y\mapsto T']$, then $\Gamma'\vdash [y\mapsto v]t\typrel T$. Thus $\Gamma\vdash[y\mapsto v]t\typrel T$.) \begin{description} \item[\quad(T-Unit)] Suppose $\Gamma$ is a context. We must show that $P(\Gamma,\unit,\Unit)$. Suppose $\Gamma'$ is a context and $\Gamma=\Gamma'[y\mapsto T']$. We must show that $\Gamma'\vdash[y\mapsto v]\unit\typrel\Unit$. Since $[y\mapsto v]\unit = \unit$, it will suffice to show $\Gamma'\vdash \unit\typrel\Unit$, which follows by (T-Unit). \item[\quad(T-Var)] Suppose $\Gamma$ is a context, $x$ is a variable, $T$ is a type and $(x,T)\in\Gamma$. We must show that $P(\Gamma,x,T)$. Suppose $\Gamma'$ is a context and $\Gamma=\Gamma'[y\mapsto T']$. We must show that $\Gamma'\vdash[y\mapsto v]x\typrel T$. There are two cases to consider. \begin{itemize} \item Suppose $y=x$. Thus $[y\mapsto v]x = v$, so we must show that $\Gamma'\vdash v\typrel T$. Because $\emptyset\vdash v\typrel T'$, the Weakening Lemma (Lemma~\ref{Weakening}), tells us that $\Gamma'\vdash v\typrel T'$. Because $(x,T)\in\Gamma= \Gamma'[y\mapsto T']$ and $x=y$, we have that $T = \Gamma'[y\mapsto T'](x) = T'$. Thus $\Gamma'\vdash v\typrel T$, as required. \item Suppose $y\neq x$. Thus $[y\mapsto v]x = x$, so we must show that $\Gamma'\vdash x\typrel T$. Because $(x,T)\in\Gamma = \Gamma'[y\mapsto T']$ and $x\neq y$, we have that $T=\Gamma'[y\mapsto T'](x)= \Gamma'(x)$. Thus $(x,T)\in\Gamma'$, so that $\Gamma'\vdash x\typrel T$ follows by (T-Var). \end{itemize} \item[\quad(T-Abs)] Suppose $\Gamma$ is a context, $x$ is a variable, $t$ is a term, $T_1$ and $T_2$ are types, and $\Gamma[x\mapsto T_1]\vdash t\typrel T_2$, and assume the inductive hypothesis, $P(\Gamma[x\mapsto T_1], t, T_2)$. We must show that $P(\Gamma,\lambda x.\,t,T_1\fun T_2)$. Suppose $\Gamma'$ is a context and $\Gamma=\Gamma'[y\mapsto T']$. We must show that $\Gamma'\vdash[y\mapsto v]\lambda x.\,t\typrel T_1\fun T_2$. There are two cases to consider. \begin{itemize} \item Suppose $x=y$. Then $[y\mapsto v]\lambda x.\,t = \lambda x.\,t$, so it will suffice to show that $\Gamma'\vdash\lambda x.\,t\typrel T_1\fun T_2$. By (T-Abs), it will suffice to show that $\Gamma'[x\mapsto T_1]\vdash t\typrel T_2$. Because $\Gamma[x\mapsto T_1]\vdash t\typrel T_2$ and $\Gamma=\Gamma'[y\mapsto T']$, we have that $\Gamma'[y\mapsto T'][x\mapsto T_1]\vdash t\typrel T_2$. Thus it will suffice to show that $\Gamma'[x\mapsto T_1] = \Gamma'[y\mapsto T'][x\mapsto T_1]$. Because $x=y$, both of these contexts have domain $\dom(\Gamma')\cup\{x\}$, both yield $T_1$ when called with $x$, and both yield $\Gamma'(z)$ when called with some $z\in\dom(\Gamma')\setminus\{x\}$. Thus, they are equal. \item Suppose $x\neq y$. Then $[y\mapsto v]\lambda x.\,t = \lambda x.\,[y\mapsto v] t$, so it will suffice to show that $\Gamma'\vdash\lambda x.\,[y\mapsto v]t \typrel T_1\fun T_2$. By (T-Abs), it will suffice to show that $\Gamma'[x\mapsto T_1]\vdash [y\mapsto v]t\typrel T_2$. By the inductive hypothesis, we have that, for all contexts $\Gamma'$, if $\Gamma[x\mapsto T_1]=\Gamma'[y\mapsto T']$, then $\Gamma'\vdash [y\mapsto v]t\typrel T_2$. Thus, if $\Gamma[x\mapsto T_1]=\Gamma'[x\mapsto T_1][y\mapsto T']$, then $\Gamma'[x\mapsto T_1]\vdash[y\mapsto v]t\typrel T_2$. Thus, it will suffice to show that $\Gamma[x\mapsto T_1]=\Gamma'[x\mapsto T_1][y\mapsto T']$. Because $\Gamma=\Gamma'[y\mapsto T']$, it will suffice to show that $\Gamma'[y\mapsto T'][x\mapsto T_1]=\Gamma'[x\mapsto T_1][y\mapsto T']$. The contexts both have domain $\dom(\Gamma') \cup \{x\} \cup \{y\}$, both yield $T_1$ when called with $x$, both yield $T'$ when called with $y$, and both yield $\Gamma'(z)$ when called with a $z\in\dom(\Gamma')\setminus\{x,y\}$. Thus, they are equal. \end{itemize} \item[\quad(T-App)] Suppose $\Gamma$ is a context, $t_1$ and $t_2$ are terms, $T_1$ and $T_2$ are types, $\Gamma\vdash t_1\typrel T_1\fun T_2$ and $\Gamma\vdash t_2\typrel T_1$, and assume the inductive hypothesis, $P(\Gamma,t_1,T_1\fun T_2)$ and $P(\Gamma,t_2,T_1)$. We must show that $P(\Gamma, t_1\,t_2, T_2)$. Suppose $\Gamma'$ is a context and $\Gamma=\Gamma'[y\mapsto T']$. We must show that $\Gamma'\vdash[y\mapsto v](t_1\,t_2)\typrel T_2$. Since $[y\mapsto v](t_1\,t_2) = [y\mapsto v]t_1\,[y\mapsto v]t_2$, we must show that $\Gamma'\vdash[y\mapsto v]t_1\,[y\mapsto v]t_2\typrel T_2$. By $P(\Gamma,t_1,T_1\fun T_2)$, we have that $\Gamma'\vdash[y\mapsto v]t_1\typrel T_1\fun T_2$. And, by $P(\Gamma,t_2,T_1)$, we have that $\Gamma'\vdash[y\mapsto v]t_2\typrel T_1$. Thus, by T-App, we have that $\Gamma'\vdash [y\mapsto v]t_1\,[y\mapsto v]t_2\typrel T_2$. \end{description} \end{proof} Now, we prove the Preservation Theorem. Let $P=\setof{(t,t')}{t\eqtxt{and}t'\eqtxtl{are closed terms, and, for all} \eqtxtr{types} T,\eqtxt{if}\emptyset\vdash t\typrel T,\eqtxt{then} \emptyset\vdash t'\typrel T}$. We use induction on the evaluation relation to show that, for all closed terms $t$ and $t'$, if $t\fun t'$, then $P(t,t')$. (Then, suppose $t$ and $t'$ are closed terms, $T$ is a type, $\emptyset\vdash t\typrel T$ and $t\fun t'$. By the result of the induction, we have that $P(t,t')$. But $\emptyset\vdash t\typrel T$, and thus $\emptyset\vdash t'\typrel T$.) \begin{description} \item[\quad(E-App1)] Suppose $t_1$, $t'_1$ and $t_2$ are closed terms, and $t_1\fun t'_1$, and assume the inductive hypothesis, $P(t_1,t'_1)$. We must show that $P(t_1\,t_2, t'_1\,t_2)$. Suppose $T$ is a type and $\emptyset\vdash t_1\,t_2\typrel T$. We must show that $\emptyset\vdash t'_1\,t_2\typrel T$. By inversion of the typing relation, we have that there is a type $T'$ such that $\emptyset\vdash t_1\typrel T'\fun T$ and $\emptyset\vdash t_2\typrel T'$. By the inductive hypothesis, it follows that $\emptyset\vdash t'_1\typrel T'\fun T$. Thus by (T-App), we have that $\emptyset\vdash t'_1\,t_2\typrel T$. \item[\quad(E-App2)] Suppose $v_1$ is a closed value, $t_2$ and $t'_2$ are closed terms, and $t_2\fun t'_2$, and assume the inductive hypothesis, $P(t_2,t'_2)$. We must show that $P(v_1\,t_2, v_1\,t'_2)$. Suppose $T$ is a type and $\emptyset\vdash v_1\,t_2\typrel T$. We must show that $\emptyset\vdash v_1\,t'_2\typrel T$. By inversion of the typing relation, we have that there is a type $T'$ such that $\emptyset\vdash v_1\typrel T'\fun T$ and $\emptyset\vdash t_2\typrel T'$. By the inductive hypothesis, it follows that $\emptyset\vdash t'_2\typrel T'$. Thus by (T-App), we have that $\emptyset\vdash v_1\,t'_2\typrel T$. \item[\quad(E-AppAbs)] Suppose $x$ is a variable, $t$ is a term such that $\FV(t)\sub\{x\}$, and $v$ is a value. We must show that $P((\lambda x.\,t)v, [x\mapsto v]t)$. Suppose $T$ is a type and $\emptyset\vdash(\lambda x.\,t)v\typrel T$. We must show that $\emptyset\vdash[x\mapsto v]t\typrel T$. By the inversion of the typing relation, we have that there is a type $T'$ such that $\emptyset\vdash\lambda x.\,t\typrel T'\fun T$ and $\emptyset\vdash v\typrel T'$. Because $\emptyset\vdash\lambda x.\,t\typrel T'\fun T$, by another inversion of the typing relation, we have that $\emptyset[x\mapsto T']\vdash t\typrel T$. Since $\emptyset[x\mapsto T']\vdash t\typrel T$ and $\emptyset\vdash v\typrel T'$, Lemma~\ref{Substitution} tells us that $\emptyset\vdash[x\mapsto v]t\typrel T$. \end{description} \end{document} %%% Local Variables: %%% mode: latex %%% TeX-master: t %%% End: