|
@@ -25,7 +25,7 @@
|
|
|
|
|
|
\def\racketEd{0}
|
|
\def\racketEd{0}
|
|
\def\pythonEd{1}
|
|
\def\pythonEd{1}
|
|
-\def\edition{0}
|
|
|
|
|
|
+\def\edition{1}
|
|
|
|
|
|
% material that is specific to the Racket edition of the book
|
|
% material that is specific to the Racket edition of the book
|
|
\newcommand{\racket}[1]{{\if\edition\racketEd{#1}\fi}}
|
|
\newcommand{\racket}[1]{{\if\edition\racketEd{#1}\fi}}
|
|
@@ -16036,20 +16036,26 @@ and used in Chez Scheme version 1~\citep{Dybvig:2006aa}.
|
|
\label{ch:Ldyn}
|
|
\label{ch:Ldyn}
|
|
\index{subject}{dynamic typing}
|
|
\index{subject}{dynamic typing}
|
|
|
|
|
|
-\if\edition\racketEd
|
|
|
|
-
|
|
|
|
-In this chapter we discuss the compilation of \LangDyn{}, a dynamically
|
|
|
|
-typed language that is a subset of Racket. This is in contrast to the
|
|
|
|
-previous chapters, which have studied the compilation of Typed
|
|
|
|
-Racket. In dynamically typed languages such as \LangDyn{}, a given
|
|
|
|
-expression may produce a value of a different type each time it is
|
|
|
|
-executed. Consider the following example with a conditional \code{if}
|
|
|
|
-expression that may return a Boolean or an integer depending on the
|
|
|
|
-input to the program.
|
|
|
|
|
|
+In this chapter we discuss the compilation of \LangDyn{}, a
|
|
|
|
+dynamically typed language. This is in contrast to the previous
|
|
|
|
+chapters, which have studied the compilation of statically typed
|
|
|
|
+languages. In dynamically typed languages such as \LangDyn{}, a
|
|
|
|
+particular expression may produce a value of a different type each
|
|
|
|
+time it is executed. Consider the following example with a conditional
|
|
|
|
+\code{if} expression that may return a Boolean or an integer depending
|
|
|
|
+on the input to the program.
|
|
% part of dynamic_test_25.rkt
|
|
% part of dynamic_test_25.rkt
|
|
|
|
+{\if\edition\racketEd
|
|
|
|
+\begin{lstlisting}
|
|
|
|
+(not (if (eq? (read) 1) #f 0))
|
|
|
|
+\end{lstlisting}
|
|
|
|
+\fi}
|
|
|
|
+{\if\edition\pythonEd
|
|
\begin{lstlisting}
|
|
\begin{lstlisting}
|
|
- (not (if (eq? (read) 1) #f 0))
|
|
|
|
|
|
+not (False if input_int() == 1 else 0)
|
|
\end{lstlisting}
|
|
\end{lstlisting}
|
|
|
|
+\fi}
|
|
|
|
+
|
|
Languages that allow expressions to produce different kinds of values
|
|
Languages that allow expressions to produce different kinds of values
|
|
are called \emph{polymorphic}, a word composed of the Greek roots
|
|
are called \emph{polymorphic}, a word composed of the Greek roots
|
|
``poly'', meaning ``many'', and ``morph'', meaning ``shape''. There
|
|
``poly'', meaning ``many'', and ``morph'', meaning ``shape''. There
|
|
@@ -16061,17 +16067,28 @@ that arises in dynamically typed languages.
|
|
|
|
|
|
Another characteristic of dynamically typed languages is that
|
|
Another characteristic of dynamically typed languages is that
|
|
primitive operations, such as \code{not}, are often defined to operate
|
|
primitive operations, such as \code{not}, are often defined to operate
|
|
-on many different types of values. In fact, in Racket, the \code{not}
|
|
|
|
-operator produces a result for any kind of value: given \code{\#f} it
|
|
|
|
-returns \code{\#t} and given anything else it returns \code{\#f}.
|
|
|
|
|
|
+on many different types of values. In fact, in
|
|
|
|
+\racket{Racket}\python{Python}, the \code{not} operator produces a
|
|
|
|
+result for any kind of value: given \FALSE{} it returns \TRUE{} and
|
|
|
|
+given anything else it returns \FALSE{}.
|
|
|
|
+
|
|
Furthermore, even when primitive operations restrict their inputs to
|
|
Furthermore, even when primitive operations restrict their inputs to
|
|
values of a certain type, this restriction is enforced at runtime
|
|
values of a certain type, this restriction is enforced at runtime
|
|
-instead of during compilation. For example, the following vector
|
|
|
|
-reference results in a run-time contract violation because the index
|
|
|
|
-must be in integer, not a Boolean such as \code{\#t}.
|
|
|
|
|
|
+instead of during compilation. For example, the following tuple read
|
|
|
|
+operation results in a run-time error because it requires a tuple, not
|
|
|
|
+a Boolean such as \TRUE{}.
|
|
|
|
+%
|
|
|
|
+{\if\edition\racketEd
|
|
\begin{lstlisting}
|
|
\begin{lstlisting}
|
|
- (vector-ref (vector 42) #t)
|
|
|
|
|
|
+(vector-ref #t 0)
|
|
\end{lstlisting}
|
|
\end{lstlisting}
|
|
|
|
+\fi}
|
|
|
|
+%
|
|
|
|
+{\if\edition\pythonEd
|
|
|
|
+\begin{lstlisting}
|
|
|
|
+True[0]
|
|
|
|
+\end{lstlisting}
|
|
|
|
+\fi}
|
|
|
|
|
|
\begin{figure}[tp]
|
|
\begin{figure}[tp]
|
|
\centering
|
|
\centering
|
|
@@ -16101,7 +16118,7 @@ must be in integer, not a Boolean such as \code{\#t}.
|
|
\]
|
|
\]
|
|
\end{minipage}
|
|
\end{minipage}
|
|
}
|
|
}
|
|
-\caption{Syntax of \LangDyn{}, an untyped language (a subset of Racket).}
|
|
|
|
|
|
+\caption{Syntax of \LangDyn{}, an untyped language (a subset of \racket{Racket}\python{Python}).}
|
|
\label{fig:r7-concrete-syntax}
|
|
\label{fig:r7-concrete-syntax}
|
|
\end{figure}
|
|
\end{figure}
|
|
|
|
|
|
@@ -16111,6 +16128,7 @@ must be in integer, not a Boolean such as \code{\#t}.
|
|
\fbox{
|
|
\fbox{
|
|
\begin{minipage}{0.96\textwidth}
|
|
\begin{minipage}{0.96\textwidth}
|
|
\small
|
|
\small
|
|
|
|
+{\if\edition\racketEd
|
|
\[
|
|
\[
|
|
\begin{array}{lcl}
|
|
\begin{array}{lcl}
|
|
\Exp &::=& \INT{\Int} \MID \VAR{\Var} \MID \LET{\Var}{\Exp}{\Exp} \\
|
|
\Exp &::=& \INT{\Int} \MID \VAR{\Var} \MID \LET{\Var}{\Exp}{\Exp} \\
|
|
@@ -16123,6 +16141,10 @@ must be in integer, not a Boolean such as \code{\#t}.
|
|
\LangDynM{} &::=& \PROGRAMDEFSEXP{\code{'()}}{\LP\Def\ldots\RP}{\Exp}
|
|
\LangDynM{} &::=& \PROGRAMDEFSEXP{\code{'()}}{\LP\Def\ldots\RP}{\Exp}
|
|
\end{array}
|
|
\end{array}
|
|
\]
|
|
\]
|
|
|
|
+\fi}
|
|
|
|
+{\if\edition\pythonEd
|
|
|
|
+UNDER CONSTRUCTION
|
|
|
|
+\fi}
|
|
\end{minipage}
|
|
\end{minipage}
|
|
}
|
|
}
|
|
\caption{The abstract syntax of \LangDyn{}.}
|
|
\caption{The abstract syntax of \LangDyn{}.}
|
|
@@ -16130,17 +16152,19 @@ must be in integer, not a Boolean such as \code{\#t}.
|
|
\end{figure}
|
|
\end{figure}
|
|
|
|
|
|
|
|
|
|
-The concrete and abstract syntax of \LangDyn{}, our subset of Racket, is
|
|
|
|
-defined in Figures~\ref{fig:r7-concrete-syntax} and
|
|
|
|
-\ref{fig:r7-syntax}.
|
|
|
|
|
|
+The concrete and abstract syntax of \LangDyn{}, our subset of
|
|
|
|
+\racket{Racket}\python{Python}, is defined in
|
|
|
|
+Figures~\ref{fig:r7-concrete-syntax} and \ref{fig:r7-syntax}.
|
|
%
|
|
%
|
|
There is no type checker for \LangDyn{} because it is not a statically
|
|
There is no type checker for \LangDyn{} because it is not a statically
|
|
typed language (it's dynamically typed!).
|
|
typed language (it's dynamically typed!).
|
|
|
|
|
|
|
|
+UNDER CONSTRUCTION
|
|
|
|
+
|
|
The definitional interpreter for \LangDyn{} is presented in
|
|
The definitional interpreter for \LangDyn{} is presented in
|
|
-Figure~\ref{fig:interp-Rdyn} and its auxiliary functions are defined i
|
|
|
|
|
|
+Figure~\ref{fig:interp-Rdyn} and its auxiliary functions are defined in
|
|
Figure~\ref{fig:interp-Rdyn-aux}. Consider the match case for
|
|
Figure~\ref{fig:interp-Rdyn-aux}. Consider the match case for
|
|
-\code{(Int n)}. Instead of simply returning the integer \code{n} (as
|
|
|
|
|
|
+\INT{n}. Instead of simply returning the integer \code{n} (as
|
|
in the interpreter for \LangVar{} in Figure~\ref{fig:interp-Lvar}), the
|
|
in the interpreter for \LangVar{} in Figure~\ref{fig:interp-Lvar}), the
|
|
interpreter for \LangDyn{} creates a \emph{tagged value}\index{subject}{tagged
|
|
interpreter for \LangDyn{} creates a \emph{tagged value}\index{subject}{tagged
|
|
value} that combines an underlying value with a tag that identifies
|
|
value} that combines an underlying value with a tag that identifies
|
|
@@ -16337,6 +16361,7 @@ in greater detail.
|
|
\fbox{
|
|
\fbox{
|
|
\begin{minipage}{0.96\textwidth}
|
|
\begin{minipage}{0.96\textwidth}
|
|
\small
|
|
\small
|
|
|
|
+{\if\edition\racketEd
|
|
\[
|
|
\[
|
|
\begin{array}{l}
|
|
\begin{array}{l}
|
|
\gray{\LintOpAST} \\ \hline
|
|
\gray{\LintOpAST} \\ \hline
|
|
@@ -16361,6 +16386,10 @@ in greater detail.
|
|
\end{array}
|
|
\end{array}
|
|
\end{array}
|
|
\end{array}
|
|
\]
|
|
\]
|
|
|
|
+\fi}
|
|
|
|
+{\if\edition\pythonEd
|
|
|
|
+UNDER CONSTRUCTION
|
|
|
|
+\fi}
|
|
\end{minipage}
|
|
\end{minipage}
|
|
}
|
|
}
|
|
\caption{The abstract syntax of \LangAny{}, extending \LangLam{} (Figure~\ref{fig:Rlam-syntax}).}
|
|
\caption{The abstract syntax of \LangAny{}, extending \LangLam{} (Figure~\ref{fig:Rlam-syntax}).}
|
|
@@ -16863,6 +16892,7 @@ of an integer, as in \LangCVec{} (Figure~\ref{fig:c2-syntax}).
|
|
\fbox{
|
|
\fbox{
|
|
\begin{minipage}{0.96\textwidth}
|
|
\begin{minipage}{0.96\textwidth}
|
|
\small
|
|
\small
|
|
|
|
+{\if\edition\racketEd
|
|
\[
|
|
\[
|
|
\begin{array}{lcl}
|
|
\begin{array}{lcl}
|
|
\Exp &::= & \ldots
|
|
\Exp &::= & \ldots
|
|
@@ -16880,6 +16910,10 @@ of an integer, as in \LangCVec{} (Figure~\ref{fig:c2-syntax}).
|
|
\LangCAnyM{} & ::= & \gray{ \PROGRAMDEFS{\itm{info}}{\LP\Def\ldots\RP} }
|
|
\LangCAnyM{} & ::= & \gray{ \PROGRAMDEFS{\itm{info}}{\LP\Def\ldots\RP} }
|
|
\end{array}
|
|
\end{array}
|
|
\]
|
|
\]
|
|
|
|
+\fi}
|
|
|
|
+{\if\edition\pythonEd
|
|
|
|
+UNDER CONSTRUCTION
|
|
|
|
+\fi}
|
|
\end{minipage}
|
|
\end{minipage}
|
|
}
|
|
}
|
|
\caption{The abstract syntax of \LangCAny{}, extending \LangCLam{} (Figure~\ref{fig:c4-syntax}).}
|
|
\caption{The abstract syntax of \LangCAny{}, extending \LangCLam{} (Figure~\ref{fig:c4-syntax}).}
|
|
@@ -17125,7 +17159,7 @@ for the compilation of \LangDyn{}.
|
|
|
|
|
|
% Further Reading
|
|
% Further Reading
|
|
|
|
|
|
-\fi % racketEd
|
|
|
|
|
|
+
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
{\if\edition\pythonEd
|
|
{\if\edition\pythonEd
|