\documentclass[11pt]{article} \input{exer-defs} \newcommand{\dom}{\mathsf{dom}} \newcommand{\ran}{\mathsf{ran}} \newcommand{\nats}{\mathbb{N}} \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}} \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 Due by 2:30 p.m.\ on Thursday, May 7 \end{center} \begin{center} The context for this assignment is Chapters 11--12 of \emph{TAPL}. \end{center} \section*{Reformulating the Simply Typed Lambda Calculus with Unit and Products} If $f$ is a function and $x,y$ are elements of our universe, we define the function $f[x\mapsto y]$ from $\dom(f)\cup\{x\}$ to $\ran(f)\cup\{y\}$ by, for all $z\in\dom(f)\cup\{x\}$, \begin{displaymath} f[x\mapsto y](z) = \left\{ \begin{array}{ll} y, & \eqtxtr{if} z = x, \\ f(z), & \eqtxtr{if} z\neq x . \end{array} \right. \end{displaymath} If $f$ is a function and $X$ is a set, then $f/X$ is the function from $\dom(f)\setminus X$ to $\ran(f)$ such that, for all $z\in\dom(f)\setminus X$, $(f/X)(z) = f(z)$. We reformulate the syntax of the simply typed lambda calculus with unit and products as follows, where variables $x$ are as usual. Our \emph{types} are defined by: \begin{alignat*}{2} T &\goesto &\qquad \eqtxt{types:}\\ &\quad\Unit &\qquad \eqtxt{unit type}\\ &\quad T\times T &\qquad \eqtxt{product type} \\ &\quad T\fun T &\qquad \eqtxt{function type} \end{alignat*} As usual, $\fun$ associates to the right, and $\times$ has higher precedence than $\fun$. Our \emph{terms} are defined by: \begin{alignat*}{2} t &\goesto &\qquad \eqtxt{terms:}\\ &\quad \unit &\qquad \eqtxt{unit constant}\\ &\quad (t,t) &\qquad \eqtxt{pair} \\ &\quad \fst\,t &\qquad \eqtxt{first projection} \\ &\quad \snd\,t &\qquad \eqtxt{second projection} \\ &\quad x &\qquad \eqtxt{variable}\\ &\quad \lambda x\typrel T.\,t &\qquad \eqtxt{abstraction}\\ &\quad t\,t &\qquad \eqtxt{application} \end{alignat*} And our \emph{values} are defined by: \begin{alignat*}{2} v &\goesto &\qquad \eqtxt{values:}\\ &\quad\unit &\qquad \eqtxt{unit constant}\\ &\quad(v,v) &\quad \eqtxt{pair value} \\ &\quad \lambda x\typrel T.\,t &\qquad \eqtxt{abstraction value} \end{alignat*} As usual, application associates to the left and abstractions extend as far as possible. And $\fst$ and $\snd$ have higher precedence than application. In contrast to TAPL's approach, we do \emph{not} identify abstractions up to the renaming of bound variables, so that, e.g., $\lambda x\typrel\Unit.\,x = \lambda y\typrel\Unit.\,y$ iff $x=y$. The \emph{free variables} of a term $t$ ($\FV(t)$) is defined recursively as follows: \begin{align*} \FV(\unit) &= \emptyset , \\ \FV((t_1,t_2)) &= \FV(t_1)\cup\FV(t_2) , \\ \FV(\fst\,t) &= \FV(t) , \\ \FV(\snd\,t) &= \FV(t) , \\ \FV(x) &= \{x\} , \\ \FV(\lambda x\typrel T.\, t) &= \FV(t)\setminus \{x\} , \\ \FV(t_1\,t_2) &= \FV(t_1)\cup\FV(t_2) . \end{align*} A term is \emph{closed} iff it has no free variables; otherwise it is \emph{open}. A \emph{simultaneous substitution} (or just \emph{substitution}) $\sigma$ is a function such that $\dom(\sigma)$ is a finite subset of the variables, and $\ran(\sigma)$ is a subset of the closed values. The result of \emph{applying} a substitution $\sigma$ \emph{to} a term $t$ ($t\,\sigma$) is defined recursively by: \begin{align*} \unit\,\sigma &= \unit , \\ (t_1,t_2)\,\sigma &= (t_1\,\sigma, t_2\,\sigma) , \\ (\fst\,t)\,\sigma &= \fst(t\,\sigma) , \\ (\snd\,t)\,\sigma &= \snd(t\,\sigma) , \\ y\,\sigma &= \left\{ \begin{array}{ll} \sigma(y), & \eqtxtr{if} y\in\dom(\sigma), \\ y, & \eqtxtr{if} y\not\in\dom(\sigma), \end{array} \right. \\ (\lambda x\typrel T.\,t)\,\sigma &= \lambda x\typrel T.\,t\,(\sigma/\{x\}) , \\ (t_1\,t_2)\,\sigma &= (t_1\,\sigma)(t_2\,\sigma) . \end{align*} The \emph{evaluation relation} \fbox{$t\fun t'$} between \emph{closed} terms is defined inductively by: \begin{alignat*}{2} &\frac{t_1\fun t'_1}% {(t_1, t_2) \fun (t'_1,t_2)} &\quad\eqtxt{(E-Pair1)} \\[.2cm] &\frac{t_2\fun t'_2}% {(v_1, t_2) \fun (v_1,t'_2)} &\quad\eqtxt{(E-Pair2)} \\[.2cm] &\frac{t\fun t'}% {\fst\,t \fun \fst\,t'} &\quad\eqtxt{(E-Fst)} \\[.2cm] &\fst(v_1,v_2)\fun v_1 &\quad\eqtxt{(E-FstVal)} \\[.2cm] &\frac{t\fun t'}% {\snd\,t \fun \snd\,t'} &\quad\eqtxt{(E-Snd)} \\[.2cm] &\snd(v_1,v_2)\fun v_2 &\quad\eqtxt{(E-SndVal)} \\[.2cm] &\frac{t_1\fun t'_1}% {t_1\,t_2 \fun t'_1\,t_2} &\quad\eqtxt{(E-App1)} \\[.2cm] &\frac{t_2\fun t'_2}% {v_1\,t_2 \fun v_1\,t'_2} &\quad\eqtxt{(E-App2)} \\[.2cm] &(\lambda x\typrel T.\,t)v\fun t\,\{(x,v)\} &\quad\eqtxt{(E-AppAbs)} \end{alignat*} So, in all but the last rule, $t$, $t'$, $t_1$, $t'_1$, $t_2$, $t'_2$, $v$, $v_1$ and $v_2$ are \emph{closed}, whereas, in (E-AppAbs), $v$ is closed but $\FV(t)\sub\{x\}$. A closed term $t$ is a \emph{normal form} iff there is no closed term $t'$ such that $t\fun t'$. A closed term $t$ is \emph{stuck} iff $t$ is a normal form but $t$ is not a value. A closed term $t$ \emph{converges} iff there is a closed value $v$ such that $t\fun^*v$; otherwise, it \emph{diverges}. An easy induction on the evaluation relation suffices to show that \textbf{evaluation is deterministic}: for all closed terms $t$, $t'$ and $t''$, if $t\fun t'$ and $t\fun t''$, then $t'=t''$. A \emph{typing context} (or just \emph{context}) $\Gamma$ is a function such that $\dom(\Gamma)$ is a finite subset of the variables, and $\ran(\Gamma)$ is a subset of the types. The \emph{typing relation} \fbox{$\Gamma\vdash t\typrel T$} between typing contexts, terms and types is defined inductively by: \begin{alignat*}{2} &\Gamma\vdash\unit:\Unit & \quad\eqtxt{(T-Unit)} \\[.2cm] &\frac{\Gamma\vdash t_1\typrel T_1\qquad \Gamma\vdash t_2\typrel T_2}% {\Gamma\vdash (t_1,t_2) \typrel T_1\times T_2} & \quad\eqtxt{(T-Pair)} \\[.2cm] &\frac{\Gamma\vdash t\typrel T_1\times T_2} {\Gamma\vdash\fst\,t\typrel T_1} & \quad\eqtxt{(T-Fst)} \\[.2cm] &\frac{\Gamma\vdash t\typrel T_1\times T_2} {\Gamma\vdash\snd\,t\typrel T_2} & \quad\eqtxt{(T-Snd)} \\[.2cm] &\frac{(x, T)\in\Gamma}% {\Gamma\vdash x \typrel T} & \quad\eqtxt{(T-Var)} \\[.2cm] &\frac{\Gamma[x\mapsto T_1]\vdash t\typrel T_2} {\Gamma\vdash\lambda x\typrel T_1.\,t\typrel T_1\fun T_2} & \quad\eqtxt{(T-Abs)} \\[.2cm] &\frac{\Gamma\vdash t_1\typrel T_1\fun T_2\qquad \Gamma\vdash t_2\typrel T_1}% {\Gamma\vdash t_1\,t_2 \typrel T_2} & \quad\eqtxt{(T-App)} \end{alignat*} We say that a closed term $t$ is \emph{well-typed} iff $\emptyset\vdash t\typrel T$ for some type $T$. An easy induction on the typing relation suffices to show that, for all contexts $\Gamma$, terms $t$ and types $T$, if $\Gamma\vdash t\typrel T$, then $\FV(t)\sub\dom(\Gamma)$. Another induction on the typing relation shows the \textbf{weakening lemma}: 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$. And another induction on the typing relation suffices to show the \textbf{uniqueness of typing}: for all contexts $\Gamma$, terms $t$ and types $T$ and $T'$, if $\Gamma\vdash t\typrel T$ and $\Gamma\vdash t\typrel T'$, then $T=T'$. The \textbf{canonical forms lemma} holds: \begin{itemize} \item If $v$ is a value and $\emptyset\vdash v\typrel\Unit$, then $v=\unit$. \item For all types $T_1$ and $T_2$, if $v$ is a value and $\emptyset\vdash v\typrel T_1\times T_2$, then $v=(v_1,v_2)$, where $\emptyset\vdash v_1\typrel T_1$ and $\emptyset\vdash v_2\typrel T_2$. \item For all types $T_1$ and $T_2$, if $v$ is a value and $\emptyset\vdash v\typrel T_1\fun T_2$, then $v=\lambda x\typrel T_1.\,t$ for a variable $x$ and term $t$ such that $\{(x,T_1)\}\vdash t\typrel T_2$. \end{itemize} The \textbf{progress theorem} holds: for all closed terms $t$, if $t$ is well-typed, then $t$ is not stuck. And the \textbf{preservation theorem} holds: for all closed terms $t$ and $t'$ and types $T$, if $\emptyset\vdash t\typrel T$ and $t\fun t'$, then $\emptyset\vdash t'\typrel T$. Define a function $R$ from types to sets of terms, by recursion (where we write $R(T)$ as $R_T$): \begin{itemize} \item $R_\Unit = \setof{t}{\emptyset\vdash t\typrel\Unit\eqtxt{and} t \eqtxtl{converges}}$; \item $R_{T_1\times T_2} = \setof{t}{\emptyset\vdash t\typrel T_1\times T_2\eqtxt{and} t\eqtxt{converges and} \fst\,t\in R_{T_1} \eqtxt{and} \snd\,t\in R_{T_2}}$; \item $R_{T_1\fun T_2} = \setof{t}{\emptyset\vdash t\typrel T_1\fun T_2\eqtxt{and} t\eqtxt{converges and, for all terms} s, \eqtxt{if} s\in R_{T_1} , \eqtxt{then} t\,s\in R_{T_2}}$. \end{itemize} Note that, for all types $T$, $R_T\sub\setof{t}{\emptyset\vdash t\typrel T \eqtxt{and} t \eqtxtl{converges}}$. We often write $R_T(t)$ instead of $t\in R_T$. \section*{Exercise 1 (10 Points)} Prove that, for all terms $t$ and substitutions $\sigma$, if $\FV(t)\cap\dom(\sigma)=\emptyset$, then $t\,\sigma = t$. \section*{Exercise 2 (15 Points)} Prove that, for all terms $t$ and substitutions $\sigma$, $\FV(t\,\sigma) = \FV(t) \setminus \dom(\sigma)$. (This result is needed to see that the rule (E-AppAbs) of the definition of $\fun$ is valid.) \section*{Exercise 3 (15 Points)} Prove that, for all contexts $\Gamma$, terms $t$, types $T$ and substitutions $\sigma$, if $\Gamma\vdash t\typrel T$ and, for all $x\in\dom(\Gamma)\cap\dom(\sigma)$, $\emptyset\vdash\sigma(x)\typrel\Gamma(x)$, then $\Gamma/\dom(\sigma)\vdash t\,\sigma\typrel T$. \section*{Exercise 4 (25 Points)} Prove that, for all types $T$, for all closed terms $t$ and $t'$, if $\emptyset\vdash t\typrel T$ and $t\fun t'$, then $R_T(t)$ iff $R_T(t')$. \section*{Exercise 5 (30 Points)} Prove that, for all contexts $\Gamma$, terms $t$, types $T$ and substitutions $\sigma$, if $\Gamma\vdash t\typrel T$, $\dom(\Gamma)\sub \dom(\sigma)$, and for all $x\in\dom(\Gamma)$, $R_{\Gamma(x)}(\sigma(x))$, then $R_T(t\,\sigma)$. \section*{Exercise 6 (5 Points)} Prove that all closed, well-typed terms converge. \end{document}