|
@@ -6476,17 +6476,19 @@ point where the \code{lambda} is applied. Referring again to
|
|
|
Figure~\ref{fig:lexical-scoping}, the binding of \code{x} to \code{5}
|
|
|
needs to be used in the application of \code{g} to \code{11}, but the
|
|
|
binding of \code{x} to \code{3} needs to be used in the application of
|
|
|
-\code{h} to \code{15}. The solution is to bundle the values of the
|
|
|
+\code{h} to \code{15}. An efficient solution to the problem, due to
|
|
|
+\citet{Cardelli:1983aa}, is to bundle into a vector the values of the
|
|
|
free variables together with the function pointer for the lambda's
|
|
|
-code into a data structure called a \emph{closure}. Fortunately, we
|
|
|
-already have the appropriate ingredients to make closures,
|
|
|
-Chapter~\ref{ch:tuples} gave us tuples and Chapter~\ref{ch:functions}
|
|
|
-gave us function pointers. The function pointer shall reside at index
|
|
|
-$0$ and the values for free variables will fill in the rest of the
|
|
|
-tuple. Figure~\ref{fig:closures} depicts the two closures created by
|
|
|
-the two calls to \code{f} in Figure~\ref{fig:lexical-scoping}.
|
|
|
-Because the two closures came from the same \key{lambda}, they share
|
|
|
-the same code but differ in the values for free variable \code{x}.
|
|
|
+code, an arrangement called a \emph{flat closure} (which we shorten to
|
|
|
+just ``closure'') . Fortunately, we have all the ingredients to make
|
|
|
+closures, Chapter~\ref{ch:tuples} gave us vectors and
|
|
|
+Chapter~\ref{ch:functions} gave us function pointers. The function
|
|
|
+pointer shall reside at index $0$ and the values for free variables
|
|
|
+will fill in the rest of the vector. Figure~\ref{fig:closures} depicts
|
|
|
+the two closures created by the two calls to \code{f} in
|
|
|
+Figure~\ref{fig:lexical-scoping}. Because the two closures came from
|
|
|
+the same \key{lambda}, they share the same function pointer but differ
|
|
|
+in the values for the free variable \code{x}.
|
|
|
|
|
|
\begin{figure}[tbp]
|
|
|
\centering \includegraphics[width=0.6\textwidth]{figs/closures}
|