% -*- Mode: Tex -*-

%\input /home/welg/6001mac.tex
%\input /homes/welg/6001/6001mac.tex
%\input /zu/scheme/6001ARCHIVE/fall89/6001mac.tex
\input ./6001mac.tex

{\obeyspaces\gdef {\ }}


\def\fbox#1{%
  \vtop{\vbox{\hrule%
              \hbox{\vrule\kern3pt%
                    \vtop{\vbox{\kern3pt#1}\kern3pt}%
                    \kern3pt\vrule}}%
        \hrule}}

\def\emptybox#1#2{\fbox{\vbox to #2{\hbox to #1 {\null}}}}

\def\pscenter#1{\hbox to\hsize{\hss\psadobenomag{#1}\hss}}

\setlength{\textheight}{9.2in}

\begin{document}

\psetheader{Spring Semester, 1998}{Recitation -- Wednesday, April 15}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

{\Large\bf 1. Student Presentation}

Review the discussion of internal definitions in section 4.1.6 of the
text. Discuss Exercise 4.19 (You need not give an implementation).

\vskip .08in

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

{\Large\bf 2. Analyzing an Expression before Evaluation}

We briefly saw this idea of using {\tt analyze} to cut down on the
amount of work we do at run time.  What does {\tt analyze} do?
First, recall what {\tt eval} and {\tt apply} do.

\centerline{\psfig{figure=r19a.ps,width=4.8in}}

Consider the form of {\tt analyze}:

\centerline{\psfig{figure=r19d.ps,width=2in}}

Ok, so what's an {\tt execution procedure}?  Compare it to what we
know about regular procedures:

\begin{tabular}{p{2.6in}p{4.1in}}
\centerline{\psfig{figure=r19b.ps,width=2.4in}} &
\begin{itemize}
\vspace{-0.75in}
\item Procedures pack up computation.
\vspace{-0.1in}
\item Can call multiple times w/ different arguments.
\vspace{-0.1in}
\item Don't have to be re-write the procedure each time.
\end{itemize}
\\ 
\centerline{\psfig{figure=r19c.ps,width=2.4in}} &
\begin{itemize}
\vspace{-0.75in}
\item Execution Procedures pack up computation.
\vspace{-0.1in}
\item Can call multiple times w/ different environments.
\vspace{-0.1in}
\item Don't have to re-analyze the expression each time.
\end{itemize}
\\
\end{tabular}


\vspace{-0.1in}

Using this method, we {\tt analyze} an expression only once but can
{\tt evaluate} it many times, with respect to different environments.

{\Large\bf 3. Code for Analyze}

\beginlisp
(define (eval exp env) ((analyze exp) env))
\null
(define (analyze exp)
  (cond ((self-evaluating? exp) (analyze-self-evaluating exp))
        ((variable? exp) (analyze-variable exp))
        ((definition? exp) (analyze-definition exp))
        ((lambda? exp) (analyze-lambda exp))
        ((cond? exp) (analyze (cond->if exp)))
        ((application? exp) (analyze-application exp))))
\null
(define (analyze-self-evaluating exp) (lambda (env) exp))
\null
(define (analyze-variable exp)
  (lambda (env) (lookup-variable-value exp env)))
\null
(define (analyze-lambda exp)
  (let ((bproc (analyze (lambda-body exp))))
    (lambda (env) (make-procedure (lambda-parameters exp) bproc env))))
\endlisp

\newpage
{\Large\bf 4. An Example}

Consider evaluating the following expressions with and without
using {\tt analyze}.  

\beginlisp
(define foo (lambda () (+ 1 2)))
\null
\null
\null
\null
\null
\null
\null
(foo)
\null
\null
\null
\null
\null
\null
\null
(foo)
\null
\null
\null
\null
\null
\null
\null
\endlisp

{\Large\bf 5. Adding {\tt and} to the Evaluator}

Recall that {\tt and} is a special form that takes an arbirary number
of arguments.  {\tt And} evalutes each argument in turn until one of
its arguments is false, in which case it returns false.  For example,

\beginlisp
(and)                   ==>  \#t
(and \#t)                ==>  \#t
(and \#t \#f (/ 1 0))     ==>  \#f
\endlisp

Consider adding {\tt and} to the Evaluator.  Write a version of {\tt
eval-and} that does not do any desugaring.

\beginlisp
(define (eval-and exp env)
  (define (iter clauses)
    (cond ((null? clauses) \#t)
          ((true? (mc-eval (car clauses) env))
           (iter (cdr clauses)))
          (else \#f)))
  (iter (cdr exp)))
\endlisp

Write a version of {\tt eval-and} that desugars into an if-statement.

\beginlisp
(define (eval-and exp env)
  (cond ((null? (cdr exp)) true)
        (else 
         (mc-eval 
          (list 'if 
                (cadr exp) 
                (cons 'and (cddr exp))
                false)
          env))))
\endlisp


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}
