|
@@ -25,7 +25,7 @@
|
|
|
|
|
|
\def\racketEd{0}
|
|
|
\def\pythonEd{1}
|
|
|
-\def\edition{0}
|
|
|
+\def\edition{1}
|
|
|
|
|
|
% material that is specific to the Racket edition of the book
|
|
|
\newcommand{\racket}[1]{{\if\edition\racketEd{#1}\fi}}
|
|
@@ -14405,10 +14405,9 @@ mainconclusion:
|
|
|
\index{subject}{lambda}
|
|
|
\index{subject}{lexical scoping}
|
|
|
|
|
|
-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.
|
|
|
+This chapter studies lexically scoped functions. Lexical scoping means
|
|
|
+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} written in
|
|
|
\LangLam{}, which extends \LangFun{} with anonymous functions using the
|
|
@@ -14455,7 +14454,6 @@ functions because they use different values for \code{x}. Applying
|
|
|
\label{fig:lexical-scoping}
|
|
|
\end{figure}
|
|
|
|
|
|
-
|
|
|
The approach that we take for implementing lexically scoped functions
|
|
|
is to compile them into top-level function definitions, translating
|
|
|
from \LangLam{} into \LangFun{}. However, the compiler must give
|
|
@@ -14471,7 +14469,9 @@ inside $e$ but does not have an enclosing binding that is also in
|
|
|
$e$.\index{subject}{free variable}
|
|
|
\end{definition}
|
|
|
|
|
|
-For example, in the expression \code{(+ x (+ y z))} the variables
|
|
|
+For example, in the expression
|
|
|
+\racket{\code{(+ x (+ y z))}}\python{\code{x + y + z}}
|
|
|
+the variables
|
|
|
\code{x}, \code{y}, and \code{z} are all free. On the other hand,
|
|
|
only \code{x} and \code{y} are free in the following expression
|
|
|
because \code{z} is bound by the \code{lambda}.
|
|
@@ -14568,6 +14568,17 @@ syntax for function application.
|
|
|
\end{array}
|
|
|
}
|
|
|
|
|
|
+\newcommand{\LlambdaGrammarPython}{
|
|
|
+ \begin{array}{lcl}
|
|
|
+ \Exp &::=& \CLAMBDA{\Var \code{,} \ldots}{\Exp}
|
|
|
+ \end{array}
|
|
|
+}
|
|
|
+\newcommand{\LlambdaASTPython}{
|
|
|
+ \begin{array}{lcl}
|
|
|
+ \Exp &::=& \LAMBDA{\LS\Var\code{, }\ldots\RS}{\Exp}
|
|
|
+ \end{array}
|
|
|
+}
|
|
|
+
|
|
|
% include AnnAssign in ASTPython
|
|
|
|
|
|
\begin{figure}[tp]
|
|
@@ -14575,6 +14586,7 @@ syntax for function application.
|
|
|
\fbox{
|
|
|
\begin{minipage}{0.96\textwidth}
|
|
|
\small
|
|
|
+{\if\edition\racketEd
|
|
|
\[
|
|
|
\begin{array}{l}
|
|
|
\gray{\LintGrammarRacket{}} \\ \hline
|
|
@@ -14589,6 +14601,23 @@ syntax for function application.
|
|
|
\end{array}
|
|
|
\end{array}
|
|
|
\]
|
|
|
+\fi}
|
|
|
+{\if\edition\pythonEd
|
|
|
+\[
|
|
|
+\begin{array}{l}
|
|
|
+ \gray{\LintGrammarPython{}} \\ \hline
|
|
|
+ \gray{\LvarGrammarPython{}} \\ \hline
|
|
|
+ \gray{\LifGrammarPython{}} \\ \hline
|
|
|
+ \gray{\LwhileGrammarPython} \\ \hline
|
|
|
+ \gray{\LtupGrammarPython} \\ \hline
|
|
|
+ \gray{\LfunGrammarPython} \\ \hline
|
|
|
+ \LlambdaGrammarPython \\
|
|
|
+ \begin{array}{lcl}
|
|
|
+ \LangLamM{} &::=& \Def^{*} \Stmt^{*}
|
|
|
+ \end{array}
|
|
|
+\end{array}
|
|
|
+\]
|
|
|
+\fi}
|
|
|
\end{minipage}
|
|
|
}
|
|
|
\caption{The concrete syntax of \LangLam{}, extending \LangFun{} (Figure~\ref{fig:Rfun-concrete-syntax})
|
|
@@ -14601,6 +14630,7 @@ syntax for function application.
|
|
|
\fbox{
|
|
|
\begin{minipage}{0.96\textwidth}
|
|
|
\small
|
|
|
+{\if\edition\racketEd
|
|
|
\[
|
|
|
\begin{array}{l}
|
|
|
\gray{\LintOpAST} \\ \hline
|
|
@@ -14611,10 +14641,27 @@ syntax for function application.
|
|
|
\gray{\LfunASTRacket} \\ \hline
|
|
|
\LlambdaASTRacket \\
|
|
|
\begin{array}{lcl}
|
|
|
- \LangLamM{} &::=& \gray{ \PROGRAMDEFSEXP{\code{'()}}{\LP\Def\ldots\RP}{\Exp} }
|
|
|
+ \LangLamM{} &::=& \PROGRAMDEFSEXP{\code{'()}}{\LP\Def\ldots\RP}{\Exp}
|
|
|
\end{array}
|
|
|
\end{array}
|
|
|
\]
|
|
|
+\fi}
|
|
|
+{\if\edition\pythonEd
|
|
|
+\[
|
|
|
+\begin{array}{l}
|
|
|
+ \gray{\LintASTPython} \\ \hline
|
|
|
+ \gray{\LvarASTPython{}} \\ \hline
|
|
|
+ \gray{\LifASTPython{}} \\ \hline
|
|
|
+ \gray{\LwhileASTPython{}} \\ \hline
|
|
|
+ \gray{\LtupASTPython{}} \\ \hline
|
|
|
+ \gray{\LfunASTPython} \\ \hline
|
|
|
+ \LlambdaASTPython \\
|
|
|
+ \begin{array}{lcl}
|
|
|
+ \LangLamM{} &::=& \PROGRAM{}{\LS \Def \ldots \Stmt \ldots \RS}
|
|
|
+ \end{array}
|
|
|
+\end{array}
|
|
|
+\]
|
|
|
+\fi}
|
|
|
\end{minipage}
|
|
|
}
|
|
|
\caption{The abstract syntax of \LangLam{}, extending \LangFun{} (Figure~\ref{fig:Rfun-syntax}).}
|