\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}} \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 Due by 2:30 p.m.\ on Tuesday, April 21 \end{center} \begin{center} The context for this assignment is Chapters~9 and 11 of \emph{TAPL}. \end{center} \section*{Re-formulating the Simply Typed Lambda Calculus} 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} We reformulate the syntax of the simply typed lambda calculus with $\Unit$ 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\fun T &\qquad \eqtxt{type of functions} \end{alignat*} As usual, $\fun$ associates to the right. Our \emph{terms} are defined by: \begin{alignat*}{2} t &\goesto &\qquad \eqtxt{terms:}\\ &\quad \unit &\qquad \eqtxt{constant $\unit$}\\ &\quad x &\qquad \eqtxt{variables}\\ &\quad \lambda x.\,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{constant $\unit$}\\ &\quad \lambda x.\,t &\qquad \eqtxt{abstraction value} \end{alignat*} As usual, application associates to the left and abstractions extend as far as possible. 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.x = \lambda y.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(x) &= \{x\} , \\ \FV(\lambda x.\, 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}. The \emph{substitution of} a \emph{closed} term $s$ \emph{for the free occurrences of} a variable $x$ \emph{in} a term $t$ ($[x\mapsto s]t$) is defined recursively by: \begin{align*} [x\mapsto s]\unit &= \unit , \\ [x\mapsto s]y &= \left\{ \begin{array}{ll} s, & \eqtxtr{if} y = x, \\ y, & \eqtxtr{if} y\neq x , \end{array} \right. \\ [x\mapsto s](\lambda y.\,t) &= \left\{ \begin{array}{ll} \lambda y.\,t, & \eqtxtr{if} y = x, \\ \lambda y.\,[x\mapsto s]t, & \eqtxtr{if} y\neq x , \end{array} \right. \\ [x\mapsto s](t_1\,t_2) &= [x\mapsto s]t_1\,[x\mapsto s]t_2 . \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-App1)} \\[.2cm] &\frac{t_2\fun t'_2}% {v_1\,t_2 \fun v_1\,t'_2} & \quad\eqtxt{(E-App2)} \\[.2cm] &(\lambda x.\,t)v\fun [x\mapsto v]t & \quad\eqtxt{(E-AppAbs)} \end{alignat*} So, in the above, $t_1$, $t'_1$, $t_2$, $t'_2$, $v$ and $v_1$ range over \emph{closed} terms, whereas $\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 \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{(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.\,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$. \section*{Exercise 1 (5 Points)} State the Inversion Lemma for the Evaluation Relation. (You don't have to prove that it's correct.) A consequence of this lemma will be that closed values are normal forms, and you may use this fact without proof in what follows. \section*{Exercise 2 (5 Points)} State the Principle of Induction on the Evaluation Relation. (You don't have to prove that it's correct.) \section*{Exercise 3 (5 Points)} State the Inversion Lemma for the Typing Relation. (You don't have to prove that it's correct.) \section*{Exercise 4 (5 Points)} State the Principle of Induction on the Typing Relation. (You don't have to prove that it's correct.) \section*{Exercise 5 (10 Points)} Prove the Free Variables Lemma: \begin{quotation} \noindent For all contexts $\Gamma$, terms $t$ and types $T$, if $\Gamma\vdash t\typrel T$, then $\FV(t)\sub\dom(\Gamma)$. \end{quotation} \section*{Exercise 6 (5 Points)} Prove the Canonical Forms Lemma: \begin{quotation} \noindent For all closed values $v$: \begin{itemize} \item if $\emptyset\vdash v\typrel\Unit$, then $v=\unit$. \item if $\emptyset\vdash v\typrel T_1\fun T_2$, for some types $T_1$ and $T_2$, then $v$ is an abstraction. \end{itemize} \end{quotation} \section*{Exercise 7 (10 Points)} Either prove or disprove the Determinacy of Evaluation Proposition: \begin{quotation} \noindent For all closed terms $t$, $t'$ and $t''$, if $t\fun t'$ and $t\fun t''$, then $t'=t''$. \end{quotation} \section*{Exercise 8 (10 Points)} Either prove or disprove the Uniqueness of Typing Proposition: \begin{quotation} \noindent 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'$. \end{quotation} \section*{Exercise 9 (20 Points)} Either prove or disprove the Progress Theorem: \begin{quotation} \noindent For all closed terms $t$, if $t$ is well-typed, then $t$ is not stuck. \end{quotation} \section*{Exercise 10 (25 Points)} Either prove or disprove the Preservation Theorem: \begin{quotation} \noindent 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$. \end{quotation} \end{document}