\documentclass[10pt]{article}

\usepackage{times,mathptm}
\pagestyle{myheadings}



\parindent 0in
\setlength{\parindent}{0in}
\setlength{\parskip}{1ex}

\topmargin -0.1in \headheight\baselineskip
\textheight 8.5in         % 1in top and bottom margin
\textwidth 6.5in        % 1in left and right margin
\oddsidemargin 0in
\evensidemargin 0in




\usepackage{amsmath}
\usepackage{amssymb}


% \setlength{\itemsep}{0pt}

\markright{\footnotesize CS 451 Programming Paradigms, Spring 2001}

\begin{document}

\section*{Project 1: Programming in ML --- due Wednesday 28 March}

Total number of points available on this project is 140. Full credit
is equivalent to 100 points.


We've learned
 that $\beta$-reduction of terms of the $\lambda$-calculus by hand is
tedious and error-prone.

The goal of this  project is to develop an interpreter for 
the $\lambda$-calculus that will automate the reductions.
This program will follow literally the rules for $\beta$ and $\eta$
conversion, and the rules for substitution. The internal representation of
$\lambda$-terms
is essentially the same as the textual representation, though the
data type makes the bracketting structure apparent, and pattern-matching
easier. We call this kind of system ``string rewriting''. 


We must first specify the internal representation for $\lambda$-expressions.
The following type is suggested (but you are free to use a different type):

\begin{quote}
$\textbf{datatype} \ \textit{Expr} = \textit{Var} \ \textbf{of} \ \textit{string} \mid
\textit{Abstraction} \ \textbf{of} \ \textit{string} \ast  \textit{Expr} \mid
\textit{Application} \ \textbf{of} \  \textit{Expr}  \ast  \textit{Expr}$
\end{quote}





Tasks:
\begin{enumerate}

\item (5 pts.)
Write a function \textit{lexpPrint} to convert an \textit{Expr} into a 
character string
in the fully-bracketted syntax for $\lambda$-expressions.
Use the character \# for $\lambda$.

\item (5 pts.)
Write a function \textit{lexpPrettyPrint} to convert an \textit{Expr}
into a 
character string
in the loose syntax for $\lambda$-expressions. 

\item (10 pts.)     
Write a function \textit{lexpParse} to 
parse a character string containing the text of a $\lambda$-expression
(which may be in the loose syntax) and convert it into an \textit{Expr}. 

\item (10 pts.)     
Implement an environment mapping 
identifiers to $\lambda$-expressions, with type 
$\textit{string} \rightarrow \textit{Expr}$.
There should be a mechanism to build new
environments out of old ones by introducing new definitions
for an identifier.

\item (10 pts.)
Implement a top-level environment and appropriate input/output handling,
so that it is possible interactivelly to add new definitions of
named $\lambda$-expressions, and ask for expressions to be reduced
and printed. It should be possible to print all intermediate steps
of the reduction as well.

\item (40 pts.)     
Implement
a generic reduction framework, in which several notions of
conversion can be specified, and different reduction orders can
be specified.
For instance, 
one should be able to specify that either $\beta$ and $\eta$ conversions,
or both,
should be tried, and that applicative-order or normal-order
reduction should be used.

Implement $\beta$ and $\eta$ conversions,
as well as a conversion that we'll call $\zeta$ that performs the
``macro-expansion'' of a name defined in the top-level environment.

\item (10 pts.)     
Test your program by reducing the several expressions from homework assignments:
$\textsf{\textbf{S}} \textsf{\textbf{K}} \textsf{\textbf{K}}$;
$\textsf{\textbf{K}} (\textsf{\textbf{S}} \textsf{\textbf{I}} \textsf{\textbf{I}})$;
$\textsf{\textbf{S}} (\textsf{\textbf{S}} (\textsf{\textbf{K}}\textsf{\textbf{S}})(\textsf{\textbf{K}}\textsf{\textbf{I}})) (\textsf{\textbf{K}}\textsf{\textbf{I}})$;
$\textsf{\textbf{S}}\textsf{\textbf{S}}\textsf{\textbf{S}}\textsf{\textbf{S}}\textsf{\textbf{S}}\textsf{\textbf{S}}\textsf{\textbf{S}}$.

\item (10 pts.)
Structure your ML code well.
Document your design and your program well.
Submit program listing and transcript of a sample session 
including at least the reductions
above.

\item (20 pts. extra credit)
Do what is necessary so that strictness annotations can be added
to the $\lambda$-bindings: even if normal-order reduction is otherwise
used, $\beta$-conversion of a redex $(\underline{\lambda} v. B) E$ will cause
$E$ to be reduced first, before substitution into the body $B$.

\item (20 pts. extra credit)
Write a function \textit{lexpInterpretivePrettyPrint} 
to convert an \textit{Expr}
into a 
character string
in the loose syntax for $\lambda$-expressions, but in a form that is
easier for humans to read, as follows. Recognize subexpressions that
are exactly as the Church numerals and the truth values and print them
as 0, 1, etc. Also recognize \texttt{cons} and \texttt{nil} so that lists
can be printed as \texttt{(cons 2 (cons 1 nil))}, for instance.

\end{enumerate}

\end{document}

