\documentclass[11pt]{article} \input{exer-defs} %\renewcommand{\thepage}{} \begin{document} \begin{center}\large\bf CIS 705 --- Programming Languages --- Spring 2009 \end{center} \begin{center}\Large\bf Assignment 4 \end{center} \begin{center}\large\bf Due by 2:30 p.m.\ on Thursday, April 2 \end{center} \begin{center} The context for this assignment is Chapters~5--7 of \emph{TAPL} and Chapter~2 of \emph{DAWOC}. \end{center} \section*{Exercise 1 (60 Points)} The goal of this exercise is to prove the results that we called Propositions~D--G in class, when discussing Chapter~5 of \emph{TAPL}. You should prove these propositions in sequence, and you may use the results about $\fun$, $\fun^n$ and $\fun^*$ that were previously established. \begin{description} \item[Proposition D (15 Points)] For all closed terms $t_1$ and $t_2$, if $t_1\,t_2$ converges, then $t_1$, $t_2$ and $\overline{t_1}\;\overline{t_2}$ converge and $\overline{t_1t_2} = \overline{\overline{t_1}\;\overline{t_2}}$. \item[Proposition E (15 Points)] For all closed terms $t_1$ and $t_2$, if $t_1$, $t_2$ and $\overline{t_1}\;\overline{t_2}$ converge, then $t_1\,t_2$ converges and $\overline{t_1t_2} = \overline{\overline{t_1}\;\overline{t_2}}$. \item[Proposition F (15 Points)] For all $n\geq 2$ and closed terms $t_1$, \ldots, $t_n$, if $t_1\,\cdots\,t_n$ converges, then $t_1$, \ldots, $t_n$ and $\overline{t_1}\;\cdots\;\overline{t_n}$ converge and $\overline{t_1\,\cdots\,t_n} = \overline{\overline{t_1}\;\cdots\;\overline{t_n}}$. \item[Proposition G (15 Points)] For all $n\geq 2$ and closed terms $t_1$, \ldots, $t_n$, if $t_1$, \ldots, $t_n$ and $\overline{t_1}\;\cdots\;\overline{t_n}$ converge, then $t_1\,\cdots\,t_n$ converges and $\overline{t_1\,\cdots\,t_n} = \overline{\overline{t_1}\;\cdots\;\overline{t_n}}$. \end{description} \subsection*{Submission} Submit your solution to this exercise on paper, not electronically. \section*{Exercise 2 (40 Points)} The goal of this assignment is to translate the OCaml expression \begin{verbatim} let rec gcd n m = if m = 0 then n else gcd m (n mod m) in gcd \end{verbatim} into the untyped lambda calculus. This expression has type \texttt{int~->~int~->~int} and is the well-known greatest common divisor function. Although its type involves integers, it is only intended to be called with natural numbers. Of course, the untyped lambda calculus term that you translate it into won't be typed. You should write a closed lambda calculus term that, when applied to closed values representing natural numbers $n$ and $m$, converges to a closed value representing the greatest common divisor of $n$ and $m$. Your answer should reside in a file named \texttt{gcd-untyped.txt}. To test that your term works correctly on natural numbers $n$ and $m$, copy your term to a new file, parenthesize it, and follow it with \begin{mverbatim} $c_n$ $c_m$ (lambda x. lambda y. y x) (lambda x. x); \end{mverbatim} Then give the untyped lambda calculus interpreter the new file as its input. The output should look like \begin{mverbatim} (lambda y. y (lambda y'. y' (lambda y''. y'' $...$ (lambda x.x) $...$))) \end{mverbatim} where the number of occurrences of ``\texttt{lambda~y}''---with some number of primes on \texttt{y}---is the greatest common divisor of $n$ and $m$. The Linux/Mac OS X shell script \texttt{gcd-test} automates the process of creating the input to the untyped lambda calculus interpreter. Running \vspace{-.34cm} \begin{mverbatim} gcd-test gcd-untyped.txt $n$ $m$ \end{mverbatim} puts the text described above in the file \texttt{gcd-test-output}. For example, running \begin{verbatim} gcd-test gcd-untyped.txt 4 6 \end{verbatim} will put the text \begin{mverbatim} ( /* beginning of gcd code */ $...$ /* end of gcd code */ ) (lambda s. lambda z. s(s(s(s z)))) /* 4 */ (lambda s. lambda z. s(s(s(s(s(s z)))))) /* 6 */ (lambda x. lambda y. y x) (lambda x. x); \end{mverbatim} in \texttt{gcd-test-output}. Giving this file to the untyped lambda calculus interpreter should produce the output (formatted slightly differently) \begin{verbatim} (lambda y. y (lambda y'. y' (lambda x.x))) \end{verbatim} because the greatest common divisor of $4$ and $6$ is $2$. Try to make your lambda calculus term as easy to understand as possible. Use comments, which begin with ``\texttt{/*}'' and end with ``\texttt{*/}'', to explain what you are doing. Format your term carefully. Although your term should run to completion on small inputs, you don't have to be concerned with its efficiency. \subsection*{Submission} Your untyped lambda calculus term should begin with a comment containing your name. It should reside in an ordinary ASCII file, \texttt{gcd-untyped.txt}. Submit your program by emailing it to me. I will acknowledge receiving it. Make sure that you retain an electronic copy of your program. \end{document}