\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}} \newcommand{\fst}{\mathsf{fst}} \newcommand{\snd}{\mathsf{snd}} \newcommand{\down}{\mathbin{\downarrow}} %\renewcommand{\thepage}{} \allowdisplaybreaks \begin{document} \begin{center}\large\bf CIS 705 --- Programming Languages --- Spring 2009 \end{center} \begin{center}\Large\bf Assignment 6 \end{center} \begin{center}\large\bf Model Answers \end{center} \section*{Exercise 1} \renewcommand{\thesection}{1} \theoremnumber{1} Let $P=\setof{t}{t\eqtxt{is a term and, for all substitutions} \sigma, \eqtxt{if} \FV(t)\cap\dom(\sigma)=\emptyset, \eqtxt{then} t\,\sigma = t}$. We use structural induction to show that, for all terms $t$, $P(t)$. \begin{description} \item[\quad(Unit Constant)] We must show $P(\unit)$. Suppose $\sigma$ is a substitution and $\FV(\unit)\cap\dom(\sigma)=\emptyset$. We have that $\unit\,\sigma = \unit$. \item[\quad(Pairing)] Suppose $t_1$ and $t_2$ are terms, and assume the inductive hypothesis: $P(t_1)$ and $P(t_2)$. We must show that $P((t_1,t_2))$. Suppose $\sigma$ is a substitution and $\FV((t_1,t_2))\cap\dom(\sigma)=\emptyset$. Because $\FV((t_1,t_2)) = \FV(t_1)\cup\FV(t_2)$, it follows that $\FV(t_1)\cap\dom(\sigma)=\emptyset$ and $\FV(t_2)\cap\dom(\sigma)=\emptyset$. Thus, by the inductive hypothesis, we have that $t_1\,\sigma = t_1$ and $t_2\,\sigma = t_2$, so that $(t_1,t_2)\,\sigma = (t_1\,\sigma, t_2\,\sigma) = (t_1,t_2)$. \item[\quad(First Projection)] Similar to pairing, but slightly simpler. \item[\quad(Second Projection)] Similar to pairing, but slightly simpler. \item[\quad(Variable)] Suppose $x$ is a variable. We must show $P(x)$. Suppose $\sigma$ is a substitution and $\FV(x)\cap\dom(\sigma)=\emptyset$. Since $\FV(x) = \{x\}$, this means that $x\not\in\dom(\sigma)$. Thus $x\,\sigma = x$. \item[\quad(Abstraction)] Suppose $x$ is a variable, $T$ is a type, and $t$ is a term, and assume the inductive hypothesis: $P(t)$. We must show $P(\lambda x\typrel T.\,t)$. Suppose $\sigma$ is a substitution and $\FV(\lambda x\typrel T.\,t)\cap\dom(\sigma)=\emptyset$. We must show that $(\lambda x\typrel T.\,t)\,\sigma = \lambda x\typrel T.\,t$. Because $(\lambda x\typrel T.\,t)\,\sigma = \lambda x\typrel T.\,t\,(\sigma/\{x\})$, it will suffice to show that $t\,(\sigma/\{x\}) = t$. Thus, by the inductive hypothesis, it will suffice to show that $\FV(t)\cap\dom(\sigma/\{x\})=\emptyset$. Since $\dom(\sigma/\{x\}) = \dom(\sigma)\setminus\{x\}$, we must show that $\FV(t)\cap(\dom(\sigma)\setminus\{x\}) = \emptyset$. Suppose, toward a contradiction, that $z\in\FV(t)$ and $z\in\dom(\sigma)\setminus\{x\}$. Thus $z\in\dom(\sigma)$ and $z\neq x$. Since $z\in\FV(t)$ and $z\neq x$, we have that $z\in \FV(t)\setminus\{x\} = \FV(\lambda x\typrel T.\,t)$. Hence $z\in\FV(\lambda x\typrel T.\,t)$ and $z\in\dom(\sigma)$, contradicting our assumption that $\FV(\lambda x\typrel T.\,t)\cap\dom(\sigma)=\emptyset$. Thus $\FV(t)\cap(\dom(\sigma)\setminus\{x\}) = \emptyset$, as required. \item[\quad(Application)] Similar to pairing. \end{description} \section*{Exercise 2} \renewcommand{\thesection}{2} \theoremnumber{1} \begin{lemma} \label{MinusLem1} For all sets $A$, $B$ and $C$, $(A\setminus C)\cup(B\setminus C) = (A\cup B)\setminus C$. \end{lemma} \begin{proof} Suppose $A$, $B$ and $C$ are sets. We must show that $(A\setminus C)\cup(B\setminus C) \sub (A\cup B)\setminus C \sub (A\setminus C)\cup(B\setminus C)$. \begin{itemize} \item ($(A\setminus C)\cup(B\setminus C) \sub (A\cup B)\setminus C$)\quad Suppose $x\in(A\setminus C)\cup(B\setminus C)$. There are two cases to consider. \begin{itemize} \item Suppose $x\in A\setminus C$, so that $x\in A$ and $x\not\in C$. Thus $x\in A\cup B$ and $x\not\in C$, so that $x\in(A\cup B)\setminus C$. \item Suppose $x\in B\setminus C$, so that $x\in B$ and $x\not\in C$. Thus $x\in A\cup B$ and $x\not\in C$, so that $x\in(A\cup B)\setminus C$. \end{itemize} \item ($(A\cup B)\setminus C\sub(A\setminus C)\cup(B\setminus C)$)\quad Suppose $x\in(A\cup B)\setminus C$. Thus $x\in A\cup B$ and $x\not\in C$. Thus there are two cases to consider. \begin{itemize} \item Suppose $x\in A$. Thus $x\in A$ and $x\not\in C$, so that $x\in(A\setminus C)\sub(A\setminus C)\cup(B\setminus C)$. \item Suppose $x\in B$. Thus $x\in B$ and $x\not\in C$, so that $x\in(B\setminus C)\sub(A\setminus C)\cup(B\setminus C)$. \end{itemize} \end{itemize} \end{proof} Let $P=\setof{t}{t\eqtxt{is a term and, for all substitutions} \sigma, \FV(t\,\sigma) = FV(t)\setminus\dom(\sigma)}$. We use structural induction to show that, for all terms $t$, $P(t)$. \begin{description} \item[\quad(Unit Constant)] We must show $P(\unit)$. Suppose $\sigma$ is a substitution. We have that $\FV(\unit\,\sigma) = \FV(\unit) = \emptyset = \emptyset\setminus\dom(\sigma) = \FV(\unit)\setminus\dom(\sigma)$. \item[\quad(Pairing)] Suppose $t_1$ and $t_2$ are terms, and assume the inductive hypothesis: $P(t_1)$ and $P(t_2)$. We must show that $P((t_1,t_2))$. Suppose $\sigma$ is a substitution. We must show that $\FV((t_1,t_2)\,\sigma) = \FV((t_1,t_2))\setminus\dom(\sigma)$. By the inductive hypothesis and Lemma~\ref{MinusLem1}, we have that \begin{align*} \FV((t_1,t_2)\,\sigma) &= \FV((t_1\,\sigma, t_2\,\sigma)) = \FV(t_1\,\sigma)\cup\FV(t_2\,\sigma) \\ &= (\FV(t_1)\setminus\dom(\sigma)) \cup (\FV(t_2)\setminus\dom(\sigma)) \\ &= (\FV(t_1)\cup\FV(t_2))\setminus\dom(\sigma) \\ &= \FV((t_1,t_2))\setminus\dom(\sigma). \end{align*} \item[\quad(First Projection)] Similar to pairing, but simpler. \item[\quad(Second Projection)] Similar to pairing, but simpler. \item[\quad(Variable)] Suppose $x$ is a variable. We must show that $P(x)$. Suppose $\sigma$ is a substitution. We must show that $\FV(x\,\sigma) = \FV(x)\setminus\dom(\sigma)$. There are two cases to consider. \begin{itemize} \item Suppose $x\in\dom(\sigma)$. Then $\FV(x\,\sigma) = \FV(\sigma(x)) = \emptyset = \{x\}\setminus\dom(\sigma) = \FV(x)\setminus\dom(\sigma)$. \item Suppose $x\not\in\dom(\sigma)$. Then $\FV(x\,\sigma) = \FV(x) = \{x\} = \{x\}\setminus\dom(\sigma) = \FV(x)\setminus\dom(\sigma)$. \end{itemize} \item[\quad(Abstraction)] Suppose $x$ is a variable, $T$ is a type, and $t$ is a term, and assume the inductive hypothesis: $P(t)$. We must show that $P(\lambda x\typrel T.\,t)$. Suppose $\sigma$ is a substitution. We must show that $\FV((\lambda x\typrel T.\,t)\,\sigma) = \FV(\lambda x\typrel T.\,t)\setminus\dom(\sigma)$. By the inductive hypothesis, we have that \begin{align*} \FV((\lambda x\typrel T.\,t)\,\sigma) &= \FV(\lambda x\typrel T.\,t\,(\sigma/\{x\})) \\ &= \FV(t\,(\sigma/\{x\})) \setminus \{x\} \\ &= (\FV(t)\setminus\dom(\sigma/\{x\})) \setminus \{x\} \\ &= (\FV(t)\setminus(\dom(\sigma)\setminus\{x\})) \setminus \{x\}. \end{align*} And $\FV(\lambda x\typrel T.\,t)\setminus\dom(\sigma) = (\FV(t)\setminus\{x\})\setminus\dom(\sigma)$, so it will suffice to show that \begin{displaymath} (\FV(t)\setminus(\dom(\sigma)\setminus\{x\})) \setminus \{x\} = (\FV(t)\setminus\{x\})\setminus\dom(\sigma) . \end{displaymath} There are two inclusions to show. \begin{itemize} \item Suppose $w\in(\FV(t)\setminus(\dom(\sigma)\setminus\{x\})) \setminus \{x\}$. Thus $w\in \FV(t)\setminus(\dom(\sigma)\setminus\{x\})$ and $w\neq x$, so that $w\in\FV(t)$ and $w\not\in\dom(\sigma)\setminus\{x\}$. Hence $w\not\in\dom(\sigma)$. Because $w\in\FV(t)$ and $w\neq x$, we have that $w\in\FV(t)\setminus\{x\}$. Thus $w\in(\FV(t)\setminus\{x\})\setminus\dom(\sigma)$. \item Suppose $w\in(\FV(t)\setminus\{x\})\setminus\dom(\sigma)$. Thus $w\in\FV(t)\setminus\{x\}$ and $w\not\in\dom(\sigma)$, so that $w\in\FV(t)$ and $w\neq x$. Because $w\not\in\dom(\sigma)$, we have that $w\not\in\dom(\sigma)\setminus\{x\}$. Since $w\in\FV(t)$ and $w\not\in\dom(\sigma)\setminus\{x\}$, it follows that $w\in\FV(t)\setminus(\dom(\sigma)\setminus\{x\})$. And $w\neq x$, and thus $w\in(\FV(t)\setminus(\dom(\sigma)\setminus\{x\})) \setminus \{x\}$. \end{itemize} \item[\quad(Application)] Similar to pairing. \end{description} \section*{Exercise 3} \renewcommand{\thesection}{3} \theoremnumber{1} \begin{lemma} \label{MinusLem2} For all sets $A$, $B$ and $C$, $A\setminus(B\setminus C) = (A\setminus B)\cup(A\cap C)$. \end{lemma} \begin{proof} Suppose $A$, $B$ and $C$ are sets. It will suffice to show that $A\setminus(B\setminus C) \sub (A\setminus B)\cup(A\cap C) \sub A\setminus(B\setminus C)$. \begin{itemize} \item ($A\setminus(B\setminus C) \sub (A\setminus B)\cup(A\cap C)$)\quad Suppose $x\in A\setminus(B\setminus C)$, so that $x\in A$ and $x\not\in B\setminus C$. There are two subcases to consider. \begin{itemize} \item Suppose $x\in C$. Thus $x\in A$ and $x\in C$, so that $x\in A\cap C\sub (A\setminus B)\cup(A\cap C)$. \item Suppose $x\not\in C$. Because $x\not\in B\setminus C$, it follows that $x\not\in B$. Thus $x\in A$ and $x\not\in B$, so that $x\in A\setminus B\sub(A\setminus B)\cup(A\cap C)$. \end{itemize} \item ($(A\setminus B)\cup(A\cap C)\sub A\setminus(B\setminus C)$)\quad Suppose $x\in (A\setminus B)\cup(A\cap C)$. There are two cases to consider. \begin{itemize} \item Suppose $x\in A\setminus B$. Thus $x\in A$ and $x\not\in B$, so that $x\not\in B\setminus C$. Hence $x\in A\setminus(B\setminus C)$. \item Suppose $x\in A\cap C$. Thus $x\in A$ and $x\in C$, so that $x\not\in B\setminus C$. Hence $x\in A\setminus(B\setminus C)$. \end{itemize} \end{itemize} \end{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 substitutions} \sigma, \eqtxt{if, for all} x\in\dom(\Gamma)\cap\dom(\sigma),\, \emptyset\vdash\sigma(x)\typrel\Gamma(x), \eqtxt{then} \Gamma/\dom(\sigma) \vdash t\,\sigma \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)$. \begin{description} \item[\quad(T-Unit)] Suppose $\Gamma$ is a context. We must show that $P(\Gamma,\unit,\Unit)$. Suppose $\sigma$ is a substitution, and for all $x\in\dom(\Gamma)\cap\dom(\sigma)$, $\emptyset\vdash\sigma(x)\typrel\Gamma(x)$. We must show that $\Gamma/\dom(\sigma)\vdash \unit\,\sigma\typrel \Unit$. And this follows by (T-Unit), since $\unit\,\sigma=\unit$. \item[\quad(T-Pair)] 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$ and $\Gamma\vdash t_2\typrel T_2$, and assume the inductive hypothesis, $P(\Gamma,t_1,T_1)$ and $P(\Gamma,t_2,T_2)$. We must show that $P(\Gamma, (t_1,t_2), T_1\times T_2)$. Suppose $\sigma$ is a substitution, and for all $x\in\dom(\Gamma)\cap\dom(\sigma)$, $\emptyset\vdash\sigma(x)\typrel\Gamma(x)$. We must show that $\Gamma/\dom(\sigma)\vdash(t_1,t_2)\,\sigma\typrel T_1\times T_2$. Because of $P(\Gamma,t_1,T_1)$, we have that $\Gamma/\dom(\sigma)\vdash t_1\,\sigma\typrel T_1$, and because of $P(\Gamma,t_2,T_2)$, we have that $\Gamma/\dom(\sigma)\vdash t_2\,\sigma\typrel T_2$. Thus $\Gamma/\dom(\sigma)\vdash (t_1\,\sigma, t_2\,\sigma)\typrel T_1\times T_2$, by (T-Pair). But $(t_1,t_2)\,\sigma = (t_1\,\sigma, t_2\,\sigma)$, and so $\Gamma/\dom(\sigma)\vdash(t_1,t_2)\,\sigma\typrel T_1\times T_2$. \item[\quad(T-Fst)] Similar to (T-Pair), but slightly simpler. \item[\quad(T-Snd)] Similar to (T-Pair), but slightly simpler. \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 $\sigma$ is a substitution, and for all $x\in\dom(\Gamma)\cap\dom(\sigma)$, $\emptyset\vdash\sigma(x)\typrel\Gamma(x)$. We must show that $\Gamma/\dom(\sigma)\vdash x\,\sigma\typrel T$. There are two cases to consider. \begin{itemize} \item Suppose $x\in\dom(\sigma)$. Since $(x,T)\in\Gamma$, we have that $x\in\dom(\Gamma)\cap\dom(\sigma)$. Thus by our assumption we have that $\emptyset\vdash\sigma(x)\typrel\Gamma(x)$. But $\sigma(x) = x\,\sigma$ and $\Gamma(x) = T$, so that $\emptyset\vdash x\,\sigma\typrel T$. Thus the weakening lemma tells us that $\Gamma/\dom(\sigma)\vdash x\,\sigma\typrel T$. \item Suppose $x\not\in\dom(\sigma)$. Then $x\,\sigma = x$, so we must show that $\Gamma/\dom(\sigma)\vdash x\typrel T$. By (T-Var), it will suffice to show that $(x,T)\in\Gamma/\dom(\sigma)$. Since $\dom(\Gamma/\dom(\sigma)) = \dom(\Gamma)\setminus\dom(\sigma)$, $x\in\dom(\Gamma)$ and $x\not\in\dom(\sigma)$, we have that $x\in \dom(\Gamma/\dom(\sigma))$. Thus $(\Gamma/\dom(\sigma))(x) = \Gamma(x) = T$, since $(x,T)\in\Gamma$, showing that $(x,T)\in\Gamma/\dom(\sigma)$. \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\typrel T_1.\,t,T_1\fun T_2)$. Suppose $\sigma$ is a substitution, and, (\dag) for all $x\in\dom(\Gamma)\cap\dom(\sigma)$, $\emptyset\vdash\sigma(x)\typrel\Gamma(x)$. We must show that $\Gamma/\dom(\sigma)\vdash (\lambda x\typrel T_1.\,t)\,\sigma\typrel T_1\fun T_2$. Since $(\lambda x\typrel T_1.\,t)\,\sigma = \lambda x\typrel T_1.\,t\,(\sigma/\{x\})$, we must show that $\Gamma/\dom(\sigma)\vdash \lambda x\typrel T_1.\,t\,(\sigma/\{x\})\typrel T_1\fun T_2$. And, by T-Abs, it will suffice to show that $(\Gamma/\dom(\sigma))[x\mapsto T_1]\vdash t\,(\sigma/\{x\})\typrel T_2$. Next, we show that, (\ddag) for all $y\in\dom(\Gamma[x\mapsto T_1])\cap \dom(\sigma/\{x\})$, $\emptyset\vdash(\sigma/\{x\})(y)\typrel \Gamma[x\mapsto T_1](y)$. Suppose $y\in\dom(\Gamma[x\mapsto T_1])\cap \dom(\sigma/\{x\})$. Thus $y\in\dom(\Gamma[x\mapsto T_1]) = \dom(\Gamma)\cup\{x\}$ and $y\in\dom(\sigma/\{x\}) = \dom(\sigma)\setminus\{x\}$, so that $y\in\dom(\sigma)$ and $y\neq x$. Thus $y\in\dom(\Gamma)\cap\dom(\sigma)$, so that (\dag) tells us that $\emptyset\vdash\sigma(y)\typrel\Gamma(y)$. We have that $\sigma(y) = (\sigma/\{x\})(y)$ and (because ($y\neq x$) $\Gamma(y) = \Gamma[x\mapsto T_1](y)$. Thus $\emptyset\vdash(\sigma/\{x\})(y)\typrel\Gamma[x\mapsto T_1](y)$. This completes the proof of (\ddag). Because of (\ddag) and the inductive hypothesis, $P(\Gamma[x\mapsto T_1], t, T_2)$, we have that \begin{displaymath} \Gamma[x\mapsto T_1]/\dom(\sigma/\{x\})\vdash t\,(\sigma/\{x\})\typrel T_2. \end{displaymath} Thus it will suffice to show that $(\Gamma/\dom(\sigma))[x\mapsto T_1] = \Gamma[x\mapsto T_1]/\dom(\sigma/\{x\})$. By Lemma~\ref{MinusLem2}, we have that \begin{align*} \dom((\Gamma/\dom(\sigma))[x\mapsto T_1]) &= \dom(\Gamma/\dom(\sigma)) \cup \{x\} \\ &= (\dom(\Gamma)\setminus\dom(\sigma)) \cup \{x\} \\ &= ((\dom(\Gamma)\cup\{x\})\setminus\dom(\sigma)) \cup \{x\} \\ &= ((\dom(\Gamma)\cup\{x\})\setminus\dom(\sigma)) \cup ((\dom(\Gamma)\cup\{x\})\cap\{x\}) \\ &= (\dom(\Gamma)\cup\{x\})\setminus(\dom(\sigma)\setminus\{x\}) \\ &= \dom(\Gamma[x\mapsto T_1])\setminus\dom(\sigma/\{x\}) \\ &= \dom(\Gamma[x\mapsto T_1]/(\sigma/\{x\})). \end{align*} Finally, suppose $y\in\dom((\Gamma/\dom(\sigma))[x\mapsto T_1]) = \dom(\Gamma[x\mapsto T_1]/(\sigma/\{x\}))$. We must show that $(\Gamma/\dom(\sigma))[x\mapsto T_1](y) = (\Gamma[x\mapsto T_1]/(\sigma/\{x\}))(y)$. There are two cases to consider. \begin{itemize} \item Suppose $y=x$. Then $(\Gamma/\dom(\sigma))[x\mapsto T_1](y) = T_1 = \Gamma[x\mapsto T_1](y) = (\Gamma[x\mapsto T_1]/(\sigma/\{x\}))(y)$. \item Suppose $y\neq x$. Then $(\Gamma/\dom(\sigma))[x\mapsto T_1](y) = (\Gamma/\dom(\sigma))(y) = \Gamma(y) = \Gamma[x\mapsto T_1](y) = (\Gamma[x\mapsto T_1]/(\sigma/\{x\}))(y)$. \end{itemize} \item[\quad(T-App)] Similar to (T-Pair). \end{description} \section*{Exercise 4} \renewcommand{\thesection}{4} \theoremnumber{1} Let $P=\setof{T}{T\eqtxt{is a type and, for all closed terms} t\eqtxt{and}t', \eqtxt{if} \emptyset\vdash t\typrel T \eqtxt{and} t\fun t', \eqtxt{then} R_T(t) \eqtxt{iff} R_T(t')}$. We use structural induction to show that, for all types $T$, $P(T)$. \begin{description} \item[\quad(Unit Type)] We must show $P(\Unit)$. Suppose $t$ and $t'$ are closed terms, $\emptyset\vdash t\typrel\Unit$ and $t\fun t'$. We must show $R_\Unit(t)$ iff $R_\Unit(t')$. By preservation, we know that $\emptyset\vdash t'\typrel\Unit$. There are two directions to show. \begin{itemize} \item Suppose $R_\Unit(t)$. Thus $t$ converges, so that $t\fun^*v$ for some closed value $v$. By the determinacy of the evaluation relation, it follows that $t'\fun^*v$, showing the $t'$ converges. And $\emptyset\vdash t'\typrel\Unit$, so that $R_\Unit(t')$. \item Suppose $R_\Unit(t')$. Thus $t'$ converges, so that $t'\fun^*v$ for some closed value $v$. Thus $t\fun^*v$, showing the $t$ converges. And $\emptyset\vdash t\typrel\Unit$, so that $R_\Unit(t)$. \end{itemize} \item[\quad(Product Type)] Suppose $T_1$ and $T_2$ are types, and assume the inductive hypothesis, $P(T_1)$ and $P(T_2)$. We must show that $P(T_1\times T_2)$. Suppose $t$ and $t'$ are closed terms, $\emptyset\vdash t\typrel T_1\times T_2$ and $t\fun t'$. We must show $R_{T_1\times T_2}(t)$ iff $R_{T_1\times T_2}(t')$. By preservation, we know that $\emptyset\vdash t'\typrel T_1\times T_2$. There are two directions to show. \begin{itemize} \item Suppose $R_{T_1\times T_2}(t)$. Thus $t$ converges, $R_{T_1}(\fst\,t)$ and $R_{T_2}(\snd\,t)$, so that $t\fun^*v$ for some closed value $v$, $\emptyset\vdash\fst\,t\typrel T_1$ and $\emptyset\vdash\snd\,t\typrel T_2$. By the determinacy of the evaluation relation, it follows that $t'\fun^*v$, showing the $t'$ converges. Because $t\fun t'$, we have that $\fst\,t\fun\fst\,t'$. And $R_{T_1}(\fst\,t)$, so that $P(T_1)$ tells us that $R_{T_1}(\fst\,t')$. Because $t\fun t'$, we have that $\snd\,t\fun\snd\,t'$. And $R_{T_2}(\snd\,t)$, so that $P(T_2)$ tells us that $R_{T_2}(\snd\,t')$. Since $\emptyset\vdash t'\typrel T_1\times T_2$, $t'$ converges, $R_{T_1}(\fst\,t')$ and $R_{T_2}(\snd\,t')$, we can conclude that $R_{T_1\times T_2}(t')$. \item Suppose $R_{T_1\times T_2}(t')$. Thus $t'$ converges, $R_{T_1}(\fst\,t')$ and $R_{T_2}(\snd\,t')$, so that $t'\fun^*v$ for some closed value $v$. Thus $t\fun^*v$, showing the $t$ converges. Since $\emptyset\vdash t\typrel T_1\times T_2$, we have that $\emptyset\vdash\fst\,t\typrel T_1$ and $\emptyset\vdash\snd\,t\typrel T_2$. Because $t\fun t'$, we have that $\fst\,t\fun\fst\, t'$. And $R_{T_1}(\fst\,t')$, so that $P(T_1)$ tells us that $R_{T_1}(\fst\,t)$. Because $t\fun t'$, we have that $\snd\,t\fun\snd\,t'$. And $R_{T_2}(\snd\,t')$, so that $P(T_2)$ tells us that $R_{T_2}(\snd\,t)$. Since $\emptyset\vdash t\typrel T_1\times T_2$, $t$ converges, $R_{T_1}(\fst\,t)$ and $R_{T_2}(\snd\,t)$, we can conclude that $R_{T_1\times T_2}(t)$. \end{itemize} \item[\quad(Function Type)] Suppose $T_1$ and $T_2$ are types, and assume the inductive hypothesis, $P(T_1)$ and $P(T_2)$. We must show that $P(T_1\fun T_2)$. Suppose $t$ and $t'$ are closed terms, $\emptyset\vdash t\typrel T_1\fun T_2$ and $t\fun t'$. We must show $R_{T_1\fun T_2}(t)$ iff $R_{T_1\fun T_2}(t')$. By preservation, we know that $\emptyset\vdash t'\typrel T_1\fun T_2$. There are two directions to show. \begin{itemize} \item Suppose $R_{T_1\fun T_2}(t)$. Thus $t$ converges, and, (\dag) for all terms $s$, if $R_{T_1}(s)$, then $R_{T_2}(t\,s)$. Because $t$ converges, we have that $t\fun^*v$ for some closed value $v$. By the determinacy of the evaluation relation, it follows that $t'\fun^*v$, showing the $t'$ converges. Because $\emptyset\vdash t'\typrel T_1\fun T_2$ and $t'$ converges, to show that $R_{T_1\fun T_2}(t')$, it remains to show that, for all terms $s$, if $R_{T_1}(s)$, then $R_{T_2}(t'\,s)$. Suppose $s$ is a term and $R_{T_1}(s)$. We must show that $R_{T_2}(t'\,s)$. Since $R_{T_1}(s)$, we know that $\emptyset\vdash s\typrel T_1$, so that $\emptyset\vdash t\,s\typrel T_2$ and $s$ is closed. And, from (\dag) and $R_{T_1}(s)$, we can conclude $R_{T_2}(t\,s)$. Since $t\fun t'$, it follows that $t\,s\fun t'\,s$. Thus $P(T_2)$ tells us that $R_{T_2}(t'\,s)$, as required. \item Suppose $R_{T_1\fun T_2}(t')$. Thus $t'$ converges, and, (\dag) for all terms $s$, if $R_{T_1}(s)$, then $R_{T_2}(t'\,s)$. Because $t'$ converges, we have that $t'\fun^*v$ for some closed value $v$. Thus $t\fun^*v$, showing the $t$ converges. Because $\emptyset\vdash t\typrel T_1\fun T_2$ and $t$ converges, to show that $R_{T_1\fun T_2}(t)$, it remains to show that, for all terms $s$, if $R_{T_1}(s)$, then $R_{T_2}(t\,s)$. Suppose $s$ is a term and $R_{T_1}(s)$. We must show that $R_{T_2}(t\,s)$. Since $R_{T_1}(s)$, we know that $\emptyset\vdash s\typrel T_1$, so that $\emptyset\vdash t\,s\typrel T_2$ and $s$ is closed. And, from (\dag) and $R_{T_1}(s)$, we can conclude $R_{T_2}(t'\,s)$. Since $t\fun t'$, it follows that $t\,s\fun t'\,s$. Thus $P(T_2)$ tells us that $R_{T_2}(t\,s)$, as required. \end{itemize} \end{description} \section*{Exercise 5} \renewcommand{\thesection}{5} \theoremnumber{1} \begin{lemma} \label{SlashLem1} For all functions $f$ and sets $A$, $(f/A)/A = f/A$. \end{lemma} \begin{proof} Suppose $f$ is a function and $A$ is a set. Since $\dom((f/A)/A) = \dom(f/A) \setminus A = (\dom(f)\setminus A) \setminus A = \dom(f) \setminus A = \dom(f/A)$, we have that $(f/A)/A$ and $f/A$ have the same domain, $\dom(f)\setminus A$. And, for all $x$ in that common domain, $((f/A)/A)(x) = (f/A)(x) = f(x) = (f/A)(x)$, completing the proof that $(f/A)/A = f/A$. \end{proof} \begin{lemma} \label{SlashLem2} For all functions $f$ and sets $A$ and $B$, $(f/A)/B = (f/B)/A$. \end{lemma} \begin{proof} Suppose $f$ is a function and $A$ and $B$ are sets. Since $\dom((f/A)/B) = \dom(f/A) \setminus B = (\dom(f)\setminus A)\setminus B = (\dom(f)\setminus B)\setminus A = \dom(f/B)\setminus A = \dom((f/B)/A)$, we have that $(f/A)/B$ and $(f/B)/A$ have the same domain $(\dom(f)\setminus A)\setminus B = (\dom(f)\setminus B)\setminus A$. And, for all $x$ in that common domain, we have that $((f/A)/B)(x) = (f/A)(x) = f(x) = (f/B)(x) = ((f/B)/A)(x)$, completing the proof that $(f/A)/B = (f/B)/A$. \end{proof} \begin{lemma} \label{SlashLem3} For all functions $f$ and elements $x$ and $y$ of our universe, $f[x\mapsto y] / \{x\} = f / \{x\}$. \end{lemma} \begin{proof} Suppose $f$ is a function and $x$ are $y$ are elements of our universe. Since $\dom(f[x\mapsto y] / \{x\}) = \dom(f[x\mapsto y]) \setminus \{x\} = (\dom(f)\cup\{x\}) \setminus \{x\} = \dom(f)\setminus \{x\} = \dom(f / \{x\})$, we have that $f[x\mapsto y] / \{x\}$ and $f / \{x\}$ have the same domain, $\dom(f)\setminus\{x\}$. And, for all $z\in\dom(f)\setminus\{x\}$, $(f[x\mapsto y] / \{x\})(z) = f[x\mapsto y](z) = f(z) = (f/\{x\})(z)$, completing the proof that $f[x\mapsto y] / \{x\} = f / \{x\}$. \end{proof} \begin{lemma} \label{SlashLem4} For all functions $f$ and elements $x$, $y$ and $z$ of our universe, if $x\neq y$, then $(f/\{y\})[x\mapsto z] = f[x\mapsto z]/\{y\}$. \end{lemma} \begin{proof} Suppose $f$ is a function, $x$, $y$ and $z$ are elements of our universe, and $x\neq y$. We have that $\dom((f/\{y\})[x\mapsto z]) = \dom(f/\{y\})\cup\{x\} = (\dom(f)\setminus\{y\})\cup\{x\} = (\dom(f)\cup\{x\})\setminus\{y\} = \dom(f[x\mapsto z])\setminus\{y\} = \dom(f[x\mapsto z]/\{y\})$. Suppose $w$ is in the common domain of $(f/\{y\})[x\mapsto z]$ and $f[x\mapsto z]/\{y\}$. We must show that $((f/\{y\})[x\mapsto z])(w) = (f[x\mapsto z]/\{y\})(w)$. There are two cases to consider. \begin{itemize} \item Suppose $w=x$. Then $((f/\{y\})[x\mapsto z])(w) = z = f[x\mapsto z](w) = (f[x\mapsto z]/\{y\})(w)$. \item Suppose $w\neq x$. Then $((f/\{y\})[x\mapsto z])(w) = (f/\{y\})(w) = f(w) = f[x\mapsto z](w) = (f[x\mapsto z]/\{y\})(w)$. \end{itemize} \end{proof} \begin{lemma} \label{Ex5Lem} For all terms $t$, closed values $v$, variables $x$ and substitutions $\sigma$, \begin{displaymath} (t\,(\sigma/\{x\}))\,\{(x,v)\} = t\,\sigma[x\mapsto v] . \end{displaymath} \end{lemma} \begin{proof} Suppose $v$ is a closed value and $x$ is a variable. Let $P=\setof{t}{t\eqtxtl{is a term, and, for all}\eqtxtr{substitutions} \sigma, (t\,(\sigma/\{x\}))\,\{(x,v)\} = t\,\sigma[x\mapsto v]}$. We use structural induction to prove that, for all terms $t$, $P(t)$. \begin{description} \item[\quad(Unit Constant)] We must show $P(\unit)$. Suppose $\sigma$ is a substitution. We have that $(\unit\,(\sigma/\{x\}))\,\{(x,v)\} = \unit\,\{(x,v)\} = \unit = \unit\,\sigma[x\mapsto v]$. \item[\quad(Pairing)] Suppose $t_1$ and $t_2$ are terms, and assume the inductive hypothesis: $P(t_1)$ and $P(t_2)$. We must show that $P((t_1,t_2))$. Suppose $\sigma$ is a substitution. By the inductive hypothesis, we have that \begin{align*} ((t_1,t_2)\,(\sigma/\{x\}))\,\{(x,v)\} &= (t_1\,(\sigma/\{x\}), t_2\,(\sigma/\{x\}))\,\{(x,v)\} \\ &= ((t_1\,(\sigma/\{x\}))\,\{(x,v)\}, (t_2\,(\sigma/\{x\}))\,\{(x,v)\}) \\ &= (t_1\,\sigma[x\mapsto v], t_2\,\sigma[x\mapsto v]) \\ &= (t_1,t_2)\,\sigma[x\mapsto v] . \end{align*} \item[\quad(First Projection)] Similar to pairing. \item[\quad(Second Projection)] Similar to pairing. \item[\quad(Variable)] Suppose $y$ is a variable. We must show $P(y)$. Suppose $\sigma$ is a substitution. There are two cases to consider. \begin{itemize} \item Suppose $y=x$. Then $y\not\in\dom(\sigma)\setminus\{x\} = \dom(\sigma/\{x\})$, $y\in\dom\{(x,v)\}$ and $y\in\dom(\sigma)\cup\{x\} = \dom(\sigma[x\mapsto v])$, so that $(y\,(\sigma/\{x\}))\,\{(x,v)\} = y\,\{(x,v)\} = \{(x,v)\}(y) = v = \sigma[x\mapsto v](y) = y\,\sigma[x\mapsto v]$. \item Suppose $y\neq x$. There are two sub-cases to consider \begin{itemize} \item Suppose $y\in\dom(\sigma)$. Since $y\in\dom(\sigma)\setminus\{x\} = \dom(\sigma/\{x\})$, we have that $(y\,(\sigma/\{x\}))\,\{(x,v)\} = ((\sigma/\{x\})(y))\,\{(x,v)\} = (\sigma(y))\,\{(x,v)\}$. But $\sigma(y)$ is closed, so that $\FV(\sigma(y))\cap\dom(\{(x,v)\}) = \emptyset$, and thus Exercise~1 tells us that $(\sigma(y))\,\{(x,v)\} = \sigma(y)$. Furthermore, because $y\in\dom(\sigma)\cup\{x\} = \dom(\sigma[x\mapsto v])$, we have that $\sigma(y) = \sigma[x\mapsto v](y) = y\,\sigma[x\mapsto v]$. Hence $(y\,(\sigma/\{x\}))\,\{(x,v)\} = (\sigma(y))\,\{(x,v)\} = \sigma(y) = y\,\sigma[x\mapsto v]$. \item Suppose $y\not\in\dom(\sigma)$. Because $y\not\in\dom(\sigma)\setminus\{x\}=\dom(\sigma/\{x\})$, $y\not\in\dom\{(x,v)\}$ and $y\not\in\dom(\sigma)\cup\{x\} = \dom(\sigma[x\mapsto v])$, we have that $(y\,(\sigma/\{x\}))\,\{(x,v)\} = y\,\{(x,v)\} = y = y\,\sigma[x\mapsto v]$. \end{itemize} \end{itemize} \item[\quad(Abstraction)] Suppose $y$ is a variable, $T$ is a type, and $t$ is a term, and assume the inductive hypothesis: $P(t)$. We must show $P(\lambda y\typrel T.\,t)$. Suppose $\sigma$ is a substitution. There are two cases to consider. \begin{itemize} \item Suppose $y=x$. By Lemma~\ref{SlashLem1} and since $\{(y,v)\}/\{y\} = \emptyset$ ($\dom(\{(y,v)\}/\{y\}) = \dom(\{(y,v)\}\setminus\{y\} = \{y\}\setminus\{y\} = \emptyset$), $(t\,(\sigma/\{y\}))\,\emptyset = t\,(\sigma/\{y\})$ (by Exercise~1, since $\FV(t\,(\sigma/\{y\}))\cap\dom(\emptyset) = \FV(t\,(\sigma/\{y\}))\cap\emptyset = \emptyset$) and $\sigma/\{y\} = \sigma/\{x\} = \sigma[x\mapsto v]/\{x\} = \sigma[x\mapsto v]/\{y\}$ (by Lemma~\ref{SlashLem3}), we have that \begin{align*} ((\lambda y\typrel T.\,t)(\sigma/\{x\}))\,\{(x,v)\} &= (\lambda y\typrel T.\,t\,((\sigma/\{x\})/\{y\}))\,\{(x,v)\} \\ &= (\lambda y\typrel T.\,t\,((\sigma/\{y\})/\{y\}))\,\{(x,v)\} \\ &= (\lambda y\typrel T.\,t\,(\sigma/\{y\}))\,\{(x,v)\} \\ &= \lambda y\typrel T.\,(t\,(\sigma/\{y\}))\,(\{(x,v)\}/\{y\}) \\ &= \lambda y\typrel T.\,(t\,(\sigma/\{y\}))\,(\{(y,v)\}/\{y\}) \\ &= \lambda y\typrel T.\,(t\,(\sigma/\{y\}))\,\emptyset \\ &= \lambda y\typrel T.\,t\,(\sigma/\{y\}) \\ &= \lambda y\typrel T.\,t\,(\sigma[x\mapsto v]/\{y\}) \\ &= (\lambda y\typrel T.t)\,\sigma[x\mapsto v]. \end{align*} \item Suppose $y\neq x$. By Lemmas~\ref{SlashLem2} and \ref{SlashLem4}, the inductive hypothesis, and since $(\{(x,v)\}/{y}) = \{(x,v)\}$, we have that \begin{align*} ((\lambda y\typrel T.\,t)(\sigma/\{x\}))\,\{(x,v)\} &= (\lambda y\typrel T.\,t\,((\sigma/\{x\})/\{y\}))\,\{(x,v)\} \\ &= \lambda y\typrel T.\,(t\,((\sigma/\{x\})/\{y\}))\,(\{(x,v)\}/{y}) \\ &= \lambda y\typrel T.\,(t\,((\sigma/\{x\})/\{y\}))\,\{(x,v)\} \\ &= \lambda y\typrel T.\,(t\,((\sigma/\{y\})/\{x\}))\,\{(x,v)\} \\ &= \lambda y\typrel T.\,t\,((\sigma/\{y\})[x\mapsto v]) \\ &= \lambda y\typrel T.\,t\,(\sigma[x\mapsto v]/\{y\}) \\ &= (\lambda y\typrel T.t)\,\sigma[x\mapsto v]. \end{align*} \end{itemize} \item[\quad(Application)] Similar to pairing. \end{description} \end{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 substitutions} \sigma, \eqtxt{if} \dom(\Gamma)\sub\dom(\sigma) \eqtxt{and, for all} x\in\dom(\Gamma), R_{\Gamma(x)}(\sigma(x)), \eqtxt{then} R_T(t\,\sigma)}$. 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)$. Suppose $\sigma$ is a substitution, $\dom(\Gamma)\sub \dom(\sigma)$, and for all $x\in\dom(\Gamma)$, $R_{\Gamma(x)}(\sigma(x))$. We must show that $R_\Unit(\unit\,\sigma)$. Since $\unit\,\sigma = \unit$, it will suffice to show that $R_\Unit(\unit)$, and this follows since $\emptyset\vdash\unit\typrel\Unit$ (by (T-Unit)) and $\unit$ is a value and thus converges. \item[\quad(T-Pair)] 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$ and $\Gamma\vdash t_2\typrel T_2$, and assume the inductive hypothesis, $P(\Gamma,t_1,T_1)$ and $P(\Gamma,t_2,T_2)$. We must show that $P(\Gamma, (t_1,t_2), T_1\times T_2)$. Suppose $\sigma$ is a substitution, $\dom(\Gamma)\sub \dom(\sigma)$, and for all $x\in\dom(\Gamma)$, $R_{\Gamma(x)}(\sigma(x))$. We must show that $R_{T_1\times T_2}((t_1,t_2)\,\sigma)$. Since $(t_1,t_2)\,\sigma = (t_1\,\sigma, t_2\,\sigma)$, it will suffice to show $R_{T_1\times T_2}((t_1\,\sigma, t_2\,\sigma))$. By the inductive hypothesis, we have that $R_{T_1}(t_1\,\sigma)$ and $R_{T_2}(t_2\,\sigma)$. Since $R_{T_1}(t_1\,\sigma)$, we have that $\emptyset\vdash t_1\,\sigma\typrel T_1$ and $t_1\,\sigma$ converges, so that $t_1\,\sigma\fun^*v_1$, for some closed value $v_1$. By Exercise~4, it follows that $R_{T_1}(v_1)$. And, since $R_{T_2}(t_2\,\sigma)$, we have that $\emptyset\vdash t_2\,\sigma\typrel T_2$ and $t_2\,\sigma$ converges, so that $t_2\,\sigma\fun^*v_2$, for some closed value $v_2$. By Exercise~4, it follows that $R_{T_2}(v_2)$. Since $\emptyset\vdash t_1\,\sigma\typrel T_1$ and $\emptyset\vdash t_2\,\sigma\typrel T_2$, (T-Pair) gives us $\emptyset\vdash(t_1\,\sigma, t_2\,\sigma)\typrel T_1\times T_2$. Since $t_1\,\sigma\fun^*v_1$ and $t_2\,\sigma\fun^*v_2$, we have that $(t_1\,\sigma, t_2\,\sigma)\fun^*(v_1, t_2\,\sigma)\fun^* (v_1,v_2)$. Thus, by Exercise~4, it will suffice to show that $R_{T_1\times T_2}((v_1,v_2))$. By the preservation theorem, we have that $\emptyset\vdash(v_1,v_2)\typrel T_1\times T_2$. Since $(v_1,v_2)$ is a value, it converges. It remains to show that $R_{T_1}(\fst\,(v_1,v_2))$ and $R_{T_2}(\snd\,(v_1,v_2))$. Since $\emptyset\vdash(v_1,v_2)\typrel T_1\times T_2$, we have that $\emptyset\vdash\fst\,(v_1,v_2)\typrel T_1$ and $\emptyset\vdash\snd\,(v_1,v_2)\typrel T_2$. Furthermore, $\fst\,(v_1,v_2)\fun v_1$ and $\snd\,(v_1,v_2)\fun v_2$. Thus, since $R_{T_1}(v_1)$ and $R_{T_2}(v_2)$, Exercise~4 tells us that $R_{T_1}(\fst\,(v_1,v_2))$ and $R_{T_2}(\snd\,(v_1,v_2))$. \item[\quad(T-Fst)] Suppose $\Gamma$ is a context, $t$ is a term, $T_1$ and $T_2$ are types, $\Gamma\vdash t\typrel T_1\times T_2$, and assume the inductive hypothesis, $P(\Gamma,t,T_1\times T_2)$. We must show that $P(\Gamma, \fst\,t, T_1)$. Suppose $\sigma$ is a substitution, $\dom(\Gamma)\sub \dom(\sigma)$, and for all $x\in\dom(\Gamma)$, $R_{\Gamma(x)}(\sigma(x))$. We must show that $R_{T_1}((\fst\,t)\,\sigma)$. Since $(\fst\,t)\,\sigma = \fst(t\,\sigma)$, it will suffice to show that $R_{T_1}(\fst(t\,\sigma))$. By the inductive hypothesis, we have that $R_{T_1\times T_2}(t\,\sigma)$. Thus $\emptyset\vdash t\,\sigma \typrel T_1\times T_2$ and $t\,\sigma$ converges, so that $\emptyset\vdash\fst\,(t\,\sigma)\typrel T_1$ and $t\,\sigma\fun^*v$ for some closed value $v$. By Exercise~4, we have that $R_{T_1\times T_2}(v)$, so that, by the definition of $R_{T_1\times T_2}$, $R_{T_1}(\fst\,v)$. Since $t\,\sigma\fun^*v$, we have that $\fst\,(t\,\sigma)\fun^* \fst\,v$. Thus $R_{T_1}(\fst(t\,\sigma))$, by Exercise~4. \item[\quad(T-Snd)] Similar to (T-Fst). \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 $\sigma$ is a substitution, $\dom(\Gamma)\sub \dom(\sigma)$, and for all $x\in\dom(\Gamma)$, $R_{\Gamma(x)}(\sigma(x))$. We must show that $R_T(x\,\sigma)$. Since $x\in\dom(\Gamma)$, we have that $R_{\Gamma(x)}(\sigma(x))$. Since $(x,T)\in\Gamma$, we have that $\Gamma(x)=T$. And, because $x\in\dom(\Gamma)$, we have that $x\in\dom(\sigma)$, so that $\sigma(x) = x\,\sigma$. Thus $R_T(x\,\sigma))$. \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\typrel T_1.\,t,T_1\fun T_2)$. Suppose $\sigma$ is a substitution, $\dom(\Gamma)\sub \dom(\sigma)$, and for all $x\in\dom(\Gamma)$, $R_{\Gamma(x)}(\sigma(x))$. We must show that $R_{T_1\fun T_2}((\lambda x\typrel T_1.\,t)\,\sigma)$. Since $(\lambda x\typrel T_1.\,t)\,\sigma = \lambda x\typrel T_1.\, t\,(\sigma/\{x\})$, it will suffice to show $R_{T_1\fun T_2}(\lambda x\typrel T_1.\, t\,(\sigma/\{x\}))$. Because $\Gamma[x\mapsto T_1]\vdash t\typrel T_2$, (T-Abs) gives us $\Gamma\vdash\lambda x\typrel T_1.\,t\typrel T_1\fun T_2$. Since $\dom(\Gamma)\sub \dom(\sigma)$, and for all $x\in\dom(\Gamma)$, $R_{\Gamma(x)}(\sigma(x))$, we have that, for all $x\in\dom(\Gamma)\cap\dom(\sigma)$, $\emptyset\vdash\sigma(x)\typrel\Gamma(x)$. Thus Exercise~3 tells us that $\Gamma/\dom(\sigma)\vdash (\lambda x\typrel T_1.\,t)\,\sigma \typrel T_1\fun T_2$. But $\Gamma/\dom(\sigma) = \emptyset$ and $(\lambda x\typrel T_1.\,t)\,\sigma = \lambda x\typrel T_1.\, t\,(\sigma/\{x\})$, and thus $\emptyset\vdash\lambda x\typrel T_1.\, t\,(\sigma/\{x\}) \typrel T_1\fun T_2$. Furthermore, $\lambda x\typrel T_1.\, t\,(\sigma/\{x\})$ is a value, and thus converges. So to show $R_{T_1\fun T_2}(\lambda x\typrel T_1.\, t\,(\sigma/\{x\}))$, it remains to show that, for all terms $s$, if $R_{T_1}(s)$, then $R_{T_2}((\lambda x\typrel T_1.\, t\,(\sigma/\{x\}))\,s)$. Suppose $s$ is a term and $R_{T_1}(s)$. We must show $R_{T_2}((\lambda x\typrel T_1.\, t\,(\sigma/\{x\}))\,s)$. Since $R_{T_1}(s)$, we have that $\emptyset\vdash s\typrel T_1$ and $s$ converges, so that $s\fun^*v$ for some closed value $v$. By Exercise~4, it follows that $R_{T_1}(v)$. Since $\emptyset\vdash\lambda x\typrel T_1.\, t\,(\sigma/\{x\}) \typrel T_1\fun T_2$ and $\emptyset\vdash s\typrel T_1$, we have that $\emptyset\vdash(\lambda x\typrel T_1.\, t\,(\sigma/\{x\}))\,s \typrel T_2$. By Lemma~\ref{Ex5Lem}, we have that \begin{align*} (\lambda x\typrel T_1.\, t\,(\sigma/\{x\}))\,s &\fun^* (\lambda x\typrel T_1.\, t\,(\sigma/\{x\}))\,v \\ &\fun (t\,(\sigma/\{x\}))\,\{(x,v)\} \\ &= t\,\sigma[x\mapsto v] . \end{align*} Thus, by Exercise~4, it will suffice to show that $R_{T_2}(t\,\sigma[x\mapsto v])$. By the inductive hypothesis, $P(\Gamma[x\mapsto T_1], t, T_2)$, it will suffice to show that $\dom(\Gamma[x\mapsto T_1])\sub \dom(\sigma[x\mapsto v])$ and, for all $y\in\dom(\Gamma[x\mapsto T_1])$, $R_{\Gamma[x\mapsto T_1](y)}(\sigma[x\mapsto v](y))$. Since $\dom(\Gamma)\sub\dom(\sigma)$, we have that $\dom(\Gamma[x\mapsto T_1]) = \dom(\Gamma)\cup\{x\} \sub \dom(\sigma)\cup\{x\} = \dom(\sigma[x\mapsto v])$. Suppose $y\in \dom(\Gamma[x\mapsto T_1]) = \dom(\Gamma)\cup\{x\}$. We must show that $R_{\Gamma[x\mapsto T_1](y)}(\sigma[x\mapsto v](y))$. There are two cases to consider. \begin{itemize} \item Suppose $y=x$. Then $R_{T_1}(v)$, $T_1 = \Gamma[x\mapsto T_1](y)$ and $v = \sigma[x\mapsto v](y)$, so that $R_{\Gamma[x\mapsto T_1](y)}(\sigma[x\mapsto v](y))$. \item Suppose $y\neq x$, so that $y\in\dom(\Gamma)$. Thus $R_{\Gamma(y)}(\sigma(y))$, $\Gamma(y) = \Gamma[x\mapsto T_1](y)$ and $\sigma(y) = \sigma[x\mapsto v](y)$, so that $R_{\Gamma[x\mapsto T_1](y)}(\sigma[x\mapsto v](y))$. \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 $\sigma$ is a substitution, $\dom(\Gamma)\sub \dom(\sigma)$, and for all $x\in\dom(\Gamma)$, $R_{\Gamma(x)}(\sigma(x))$. We must show that $R_{T_2}((t_1\,t_2)\,\sigma)$. Since $(t_1\,t_2)\,\sigma = (t_1\,\sigma)(t_2\,\sigma)$, it will suffice to show $R_{T_2}((t_1\,\sigma)(t_2\,\sigma))$. By the inductive hypothesis, we have that $R_{T_1\fun T_2}(t_1\,\sigma)$ and $R_{T_1}(t_2\,\sigma)$. Thus, by the definition of $R_{T_1\fun T_2}(t_1\,\sigma)$, we have $R_{T_2}((t_1\,\sigma)(t_2\,\sigma))$. \end{description} \section*{Exercise 6} \renewcommand{\thesection}{6} \theoremnumber{1} Suppose $t$ is a closed, well-typed term. Thus $\emptyset\vdash t\typrel T$ for some type $T$. Since $\emptyset\vdash t\typrel T$, $\dom(\emptyset)\sub\dom(\emptyset)$, and, for all $x\in\dom(\emptyset)$, $R_{\emptyset(x)}(\emptyset(x))$, Exercise~5 tells us that $R_T(t\,\emptyset)$. Since $\FV(t)\cap\dom(\emptyset) = \emptyset\cap\emptyset = \emptyset$, Exercise~1 tells us that $t\,\emptyset = t$. Thus $R_T(t)$, so that $t$ converges. \end{document} %%% Local Variables: %%% mode: latex %%% TeX-master: t %%% End: