|
@@ -5202,9 +5202,23 @@ programs.
|
|
|
This chapter studies lexically scoped functions as they appear in
|
|
|
functional languages such as Racket. By lexical scoping we mean that a
|
|
|
function's body may refer to variables whose binding site is outside
|
|
|
-of the function, in an enclosing scope. Consider the example in
|
|
|
-Figure~\ref{fig:lexical-scoping} featuring an anonymous function
|
|
|
-defined using the \key{lambda} form.
|
|
|
+of the function, in an enclosing scope.
|
|
|
+%
|
|
|
+Consider the example in Figure~\ref{fig:lexical-scoping} featuring an
|
|
|
+anonymous function defined using the \key{lambda} form. The body of
|
|
|
+the \key{lambda}, refers to three variables: \code{x}, \code{y}, and
|
|
|
+\code{z}. The binding sites for \code{x} and \code{y} are outside of
|
|
|
+the \key{lambda}. Variable \code{y} is bound by the enclosing
|
|
|
+\key{let} and \code{x} is a parameter of \code{f}. The \key{lambda} is
|
|
|
+returned from the function \code{f}. Below the definition of \code{f},
|
|
|
+we have two calls to \code{f} with different arguments for \code{x},
|
|
|
+first \code{5} then \code{3}. The functions returned from \code{f} are
|
|
|
+bound to variables \code{g} and \code{h}. Even though these two
|
|
|
+functions were created by the same \code{lambda}, they are really
|
|
|
+different functions because they use different values for
|
|
|
+\code{x}. Finally, we apply \code{g} to \code{11} (producing
|
|
|
+\code{20}) and apply \code{h} to \code{15} (producing \code{22}) so
|
|
|
+the result of this program is \code{42}.
|
|
|
|
|
|
\begin{figure}[btp]
|
|
|
\begin{lstlisting}
|
|
@@ -5221,27 +5235,16 @@ defined using the \key{lambda} form.
|
|
|
\label{fig:lexical-scoping}
|
|
|
\end{figure}
|
|
|
|
|
|
-The body of the \key{lambda}, \code{(+ x (+ y z))}, refers to three
|
|
|
-variables. The binding sites for \code{x} and \code{y} are outside of
|
|
|
-the \key{lambda}: \code{y} is bound by the enclosing \key{let} and
|
|
|
-\code{x} is a parameter of \code{f}. The \key{lambda} is returned from
|
|
|
-the function \code{f}. Below the definition of \code{f}, we have two
|
|
|
-calls to \code{f} with different arguments for \code{x}, first
|
|
|
-\code{5} then \code{3}. The functions returned from \code{f} are bound
|
|
|
-to \code{g} and \code{h}. Even though these two functions were created
|
|
|
-by the same \code{lambda}, they are really different functions because
|
|
|
-they use different values for \code{x}. Finally, we apply \code{g} to
|
|
|
-\code{11} (producing \code{20}) and apply \code{h} to \code{15}
|
|
|
-(producing \code{22}) so the result of this program is \code{42}.
|
|
|
|
|
|
\section{The $R_5$ Language}
|
|
|
|
|
|
-The syntax for this language with lexical scoping, $R_5$, is defined
|
|
|
-in Figure~\ref{fig:r5-syntax}. It adds the \key{lambda} form to the
|
|
|
-grammar for $R_4$, which already has syntax for function application.
|
|
|
-In this chapter we shall descibe how to compile $R_5$ back into $R_4$,
|
|
|
-compiling lexically-scoped functions into a combination of functions
|
|
|
-(as in $R_4$) and tuples (as in $R_3$).
|
|
|
+The syntax for this language with anonymous functions and lexical
|
|
|
+scoping, $R_5$, is defined in Figure~\ref{fig:r5-syntax}. It adds the
|
|
|
+\key{lambda} form to the grammar for $R_4$, which already has syntax
|
|
|
+for function application. In this chapter we shall descibe how to
|
|
|
+compile $R_5$ back into $R_4$, compiling lexically-scoped functions
|
|
|
+into a combination of functions (as in $R_4$) and tuples (as in
|
|
|
+$R_3$).
|
|
|
|
|
|
\begin{figure}[tp]
|
|
|
\centering
|
|
@@ -5274,8 +5277,10 @@ compiling lexically-scoped functions into a combination of functions
|
|
|
\label{fig:r5-syntax}
|
|
|
\end{figure}
|
|
|
|
|
|
-Our compiler must provide special treatment to variable occurences
|
|
|
-such as \code{x} and \code{y} in the body of the \code{lambda} of
|
|
|
+We shall describe how to compile $R_5$ to $R_4$, replacing anonymous
|
|
|
+functions with top-level function definitions. However, our compiler
|
|
|
+must provide special treatment to variable occurences such as \code{x}
|
|
|
+and \code{y} in the body of the \code{lambda} of
|
|
|
Figure~\ref{fig:lexical-scoping}, for the functions of $R_4$ may not
|
|
|
refer to variables defined outside the function. To identify such
|
|
|
variable occurences, we review the standard notion of free variable.
|