Permalink
Browse files

Added translation of concrete syntax and corresponding Scala code

  • Loading branch information...
Meredith Gregory
Meredith Gregory committed Mar 2, 2010
1 parent 726a59d commit 09f0a8dec5d7bc84b613370a9728432679788681
@@ -1,6 +1,6 @@
\relax
\@setckpt{bibliography/monadic}{
-\setcounter{page}{60}
+\setcounter{page}{64}
\setcounter{equation}{0}
\setcounter{enumi}{0}
\setcounter{enumii}{0}
@@ -19,7 +19,7 @@
\setcounter{table}{0}
\setcounter{parentequation}{0}
\setcounter{Item}{0}
-\setcounter{Hfootnote}{5}
+\setcounter{Hfootnote}{6}
\setcounter{lstnumber}{264}
\setcounter{theorem}{0}
\setcounter{section@level}{2}
@@ -1,12 +1,12 @@
\relax
-\@writefile{toc}{\contentsline {chapter}{\numberline {8}Domain model, storage and state}{55}{chapter.8}}
+\@writefile{toc}{\contentsline {chapter}{\numberline {8}Domain model, storage and state}{59}{chapter.8}}
\@writefile{lof}{\addvspace {10\p@ }}
\@writefile{lot}{\addvspace {10\p@ }}
-\@writefile{toc}{\contentsline {section}{\numberline {8.1}Mapping our domain model to storage}{55}{section.8.1}}
-\@writefile{toc}{\contentsline {section}{\numberline {8.2}Storage and language-integrated query}{55}{section.8.2}}
-\@writefile{toc}{\contentsline {section}{\numberline {8.3}Continuations revisited}{55}{section.8.3}}
+\@writefile{toc}{\contentsline {section}{\numberline {8.1}Mapping our domain model to storage}{59}{section.8.1}}
+\@writefile{toc}{\contentsline {section}{\numberline {8.2}Storage and language-integrated query}{59}{section.8.2}}
+\@writefile{toc}{\contentsline {section}{\numberline {8.3}Continuations revisited}{59}{section.8.3}}
\@setckpt{chapters/eight/ch}{
-\setcounter{page}{56}
+\setcounter{page}{60}
\setcounter{equation}{0}
\setcounter{enumi}{0}
\setcounter{enumii}{0}
@@ -25,7 +25,7 @@
\setcounter{table}{0}
\setcounter{parentequation}{0}
\setcounter{Item}{0}
-\setcounter{Hfootnote}{5}
+\setcounter{Hfootnote}{6}
\setcounter{lstnumber}{264}
\setcounter{theorem}{0}
\setcounter{section@level}{1}
@@ -1,12 +1,12 @@
\relax
-\@writefile{toc}{\contentsline {chapter}{\numberline {5}The domain model as abstract syntax}{49}{chapter.5}}
+\@writefile{toc}{\contentsline {chapter}{\numberline {5}The domain model as abstract syntax}{53}{chapter.5}}
\@writefile{lof}{\addvspace {10\p@ }}
\@writefile{lot}{\addvspace {10\p@ }}
-\@writefile{toc}{\contentsline {section}{\numberline {5.1}Our abstract syntax}{49}{section.5.1}}
-\@writefile{toc}{\contentsline {section}{\numberline {5.2}Our application domain model}{49}{section.5.2}}
-\@writefile{toc}{\contentsline {section}{\numberline {5.3}A transform pipeline}{49}{section.5.3}}
+\@writefile{toc}{\contentsline {section}{\numberline {5.1}Our abstract syntax}{53}{section.5.1}}
+\@writefile{toc}{\contentsline {section}{\numberline {5.2}Our application domain model}{53}{section.5.2}}
+\@writefile{toc}{\contentsline {section}{\numberline {5.3}A transform pipeline}{53}{section.5.3}}
\@setckpt{chapters/five/ch}{
-\setcounter{page}{50}
+\setcounter{page}{54}
\setcounter{equation}{0}
\setcounter{enumi}{0}
\setcounter{enumii}{0}
@@ -25,7 +25,7 @@
\setcounter{table}{0}
\setcounter{parentequation}{0}
\setcounter{Item}{0}
-\setcounter{Hfootnote}{5}
+\setcounter{Hfootnote}{6}
\setcounter{lstnumber}{264}
\setcounter{theorem}{0}
\setcounter{section@level}{1}
@@ -1,12 +1,12 @@
\relax
-\@writefile{toc}{\contentsline {chapter}{\numberline {4}Parsing requests, monadically}{47}{chapter.4}}
+\@writefile{toc}{\contentsline {chapter}{\numberline {4}Parsing requests, monadically}{51}{chapter.4}}
\@writefile{lof}{\addvspace {10\p@ }}
\@writefile{lot}{\addvspace {10\p@ }}
-\@writefile{toc}{\contentsline {section}{\numberline {4.1}Obligatory parsing monad}{47}{section.4.1}}
-\@writefile{toc}{\contentsline {section}{\numberline {4.2}Your parser combinators are showing}{47}{section.4.2}}
-\@writefile{toc}{\contentsline {section}{\numberline {4.3}EBNF and why higher levels of abstraction are better}{47}{section.4.3}}
+\@writefile{toc}{\contentsline {section}{\numberline {4.1}Obligatory parsing monad}{51}{section.4.1}}
+\@writefile{toc}{\contentsline {section}{\numberline {4.2}Your parser combinators are showing}{51}{section.4.2}}
+\@writefile{toc}{\contentsline {section}{\numberline {4.3}EBNF and why higher levels of abstraction are better}{51}{section.4.3}}
\@setckpt{chapters/four/ch}{
-\setcounter{page}{48}
+\setcounter{page}{52}
\setcounter{equation}{0}
\setcounter{enumi}{0}
\setcounter{enumii}{0}
@@ -25,7 +25,7 @@
\setcounter{table}{0}
\setcounter{parentequation}{0}
\setcounter{Item}{0}
-\setcounter{Hfootnote}{5}
+\setcounter{Hfootnote}{6}
\setcounter{lstnumber}{264}
\setcounter{theorem}{0}
\setcounter{section@level}{1}
@@ -1,13 +1,13 @@
\relax
-\@writefile{toc}{\contentsline {chapter}{\numberline {9}Putting it all together}{57}{chapter.9}}
+\@writefile{toc}{\contentsline {chapter}{\numberline {9}Putting it all together}{61}{chapter.9}}
\@writefile{lof}{\addvspace {10\p@ }}
\@writefile{lot}{\addvspace {10\p@ }}
-\@writefile{toc}{\contentsline {section}{\numberline {9.1}Our web application end-to-end}{57}{section.9.1}}
-\@writefile{toc}{\contentsline {section}{\numberline {9.2}Deploying our application}{57}{section.9.2}}
-\@writefile{toc}{\contentsline {subsection}{\numberline {9.2.1}Why we are not deploying on GAE}{57}{subsection.9.2.1}}
-\@writefile{toc}{\contentsline {section}{\numberline {9.3}From one web application to web framework}{57}{section.9.3}}
+\@writefile{toc}{\contentsline {section}{\numberline {9.1}Our web application end-to-end}{61}{section.9.1}}
+\@writefile{toc}{\contentsline {section}{\numberline {9.2}Deploying our application}{61}{section.9.2}}
+\@writefile{toc}{\contentsline {subsection}{\numberline {9.2.1}Why we are not deploying on GAE}{61}{subsection.9.2.1}}
+\@writefile{toc}{\contentsline {section}{\numberline {9.3}From one web application to web framework}{61}{section.9.3}}
\@setckpt{chapters/nine/ch}{
-\setcounter{page}{58}
+\setcounter{page}{62}
\setcounter{equation}{0}
\setcounter{enumi}{0}
\setcounter{enumii}{0}
@@ -26,7 +26,7 @@
\setcounter{table}{0}
\setcounter{parentequation}{0}
\setcounter{Item}{0}
-\setcounter{Hfootnote}{5}
+\setcounter{Hfootnote}{6}
\setcounter{lstnumber}{264}
\setcounter{theorem}{0}
\setcounter{section@level}{1}
@@ -2,7 +2,7 @@
\@writefile{toc}{\contentsline {chapter}{\numberline {1}Motivation and Background}{3}{chapter.1}}
\@writefile{lof}{\addvspace {10\p@ }}
\@writefile{lot}{\addvspace {10\p@ }}
-\@writefile{toc}{\contentsline {paragraph}{With apologies to Italo Calvino}{3}{section*.6}}
+\@writefile{toc}{\contentsline {paragraph}{If on a winter's night a programmer (with apologies to Italo Calvino)}{3}{section*.6}}
\@writefile{toc}{\contentsline {section}{\numberline {1.1}Where are we}{4}{section.1.1}}
\@writefile{toc}{\contentsline {subsection}{\numberline {1.1.1}The concurrency squeeze: from the hardware up, from the web down}{4}{subsection.1.1.1}}
\@writefile{toc}{\contentsline {subsection}{\numberline {1.1.2}Ubiquity of robust, high-performance virtual machines}{5}{subsection.1.1.2}}
@@ -20,25 +20,25 @@
\@writefile{toc}{\contentsline {paragraph}{A simple-minded representation}{17}{section*.9}}
\@writefile{toc}{\contentsline {paragraph}{Type parametrization and quotation}{18}{section*.10}}
\@writefile{toc}{\contentsline {paragraph}{Some syntactic sugar}{19}{section*.11}}
-\@writefile{toc}{\contentsline {paragraph}{Digression: complexity management principle}{19}{section*.12}}
+\@writefile{toc}{\contentsline {paragraph}{Digression: complexity management principle}{20}{section*.12}}
\@writefile{toc}{\contentsline {paragraph}{Concrete syntax}{20}{section*.13}}
\@writefile{toc}{\contentsline {paragraph}{Translating concrete syntax to abstract syntax}{20}{section*.14}}
-\@writefile{toc}{\contentsline {paragraph}{Structural equivalence and Relations or What makes abstract syntax abstract}{20}{section*.15}}
-\@writefile{toc}{\contentsline {paragraph}{Digression: the internal structure of the type of variables}{25}{section*.16}}
-\@writefile{toc}{\contentsline {paragraph}{Evaluation -- aka operational semantics}{25}{section*.17}}
-\@writefile{toc}{\contentsline {subsubsection}{What goes into a language definition}{27}{section*.18}}
-\@writefile{toc}{\contentsline {paragraph}{Syntax}{28}{section*.19}}
-\@writefile{toc}{\contentsline {paragraph}{Structural equivalence}{28}{section*.20}}
-\@writefile{toc}{\contentsline {paragraph}{Operational semantics}{28}{section*.21}}
-\@writefile{toc}{\contentsline {paragraph}{Discussion}{28}{section*.22}}
+\@writefile{toc}{\contentsline {paragraph}{Structural equivalence and Relations or What makes abstract syntax abstract}{23}{section*.15}}
+\@writefile{toc}{\contentsline {paragraph}{Digression: the internal structure of the type of variables}{27}{section*.16}}
+\@writefile{toc}{\contentsline {paragraph}{Evaluation -- aka operational semantics}{27}{section*.17}}
+\@writefile{toc}{\contentsline {subsubsection}{What goes into a language definition}{29}{section*.18}}
+\@writefile{toc}{\contentsline {paragraph}{Syntax}{30}{section*.19}}
+\@writefile{toc}{\contentsline {paragraph}{Structural equivalence}{30}{section*.20}}
+\@writefile{toc}{\contentsline {paragraph}{Operational semantics}{30}{section*.21}}
+\@writefile{toc}{\contentsline {paragraph}{Discussion}{30}{section*.22}}
\@setckpt{chapters/one/ch}{
-\setcounter{page}{29}
+\setcounter{page}{31}
\setcounter{equation}{0}
\setcounter{enumi}{0}
\setcounter{enumii}{0}
\setcounter{enumiii}{0}
\setcounter{enumiv}{0}
-\setcounter{footnote}{5}
+\setcounter{footnote}{6}
\setcounter{mpfootnote}{0}
\setcounter{part}{0}
\setcounter{chapter}{1}
@@ -51,7 +51,7 @@
\setcounter{table}{0}
\setcounter{parentequation}{0}
\setcounter{Item}{0}
-\setcounter{Hfootnote}{5}
+\setcounter{Hfootnote}{6}
\setcounter{lstnumber}{73}
\setcounter{theorem}{0}
\setcounter{section@level}{4}
@@ -100,19 +100,23 @@ \subsubsection{Our toy language}
\texttt{Scala} code.
\break
-\begin{lstlisting}[language=Scala]
+\begin{lstlisting}[language=Scala,mathescape=true]
trait Expressions {
type Nominal
+ // $M,N ::=$
abstract class Expression
+ // $x$
case class Mention( reference : Nominal )
extends Expression
+ // $\lambda \; x_1,...,x_n.M$
case class Abstraction(
formals : List[Nominal],
body : Expression
) extends Expression
+ // $M N_1 ... N_n$
case class Application(
operation : Expression,
actuals : List[Expression]
@@ -267,10 +271,116 @@ \subsubsection{Our toy language}
\paragraph{Translating concrete syntax to abstract syntax}
The translation from the concrete syntax to the abstract syntax is
-compactly expressed as follows. Perhaps the best way to understand
-this presentation is in terms of a \texttt{Scala} implementation.
+compactly expressed as follows. Even if the form of the translation is
+unfamiliar, it should still leave you with the impression that some
+core of \texttt{Scala} is really the $\lambda$-calculus.
+
+% \begin{mathpar}
+% \inferrule* {} {\meaningof{\lstinline[language=Scala,mathescape=true]!x!} = x}
+% \and
+% \inferrule* {} {\meaningof{\lstinline[language=Scala,mathescape=true]!(x) $\Rightarrow$! expr } = \lambda \; x . \meaningof{expr} }
+% \inferrule* {} {\meaningof{\lstinline[language=Scala,mathescape=true]!val x!} = let \; x = \meaningof{expr_1} in \meaningof{expr_2} }
+% \end{mathpar}
+
+\begin{lstlisting}[language=Scala,mathescape=true]
+ $\ldb$ x $\rdb$ $=$ $x$
+ $\ldb$ (x) => expr $\rdb$ $=$ $\lambda \; x . \ldb$ expr $\rdb$
+ $\ldb$ expr( expr$_1$, ..., expr$_n$ ) $\rdb$ $=$ $\ldb$ expr $\rdb$ $\ldb$ expr$_1$ $\rdb$ ... $\ldb$ expr$n$ $\rdb$
+ $\ldb$ val x = expr$_1$ ; expr$_2$ $\rdb$ $=$ $let$ $\ldb$ x $\rdb$ $=$ $\ldb$ expr$_1$ $\rdb$ $in$ $\ldb$ expr$_2$ $\rdb$
+ $\ldb$ expr$_1$ ; expr$_2$ $\rdb$ $=$ $x$ $\ldb$ expr$_1$ $\rdb$ $;$ $\ldb$ expr$_2$ $\rdb$
+ $\ldb$ { expr } $\rdb$ $=$ ( $\ldb$ expr $\rdb$ )
+\end{lstlisting}
+Further, the value of the explicit representation of sugar in terms of
+structuring the translation should be clear. Of course, in a book
+entitled \emph{Pro Scala} the best way to unpack this presentation
+is in terms of a \texttt{Scala} implementation.
+
+\begin{lstlisting}[language=Scala,mathescape=true]
+ trait Compiler extends Expressions with Nominals {
+ // Abstract away interning variables
+ type Internist =
+ {def intern( varExpr : Absyn.VariableExpr ) : Nominal}
+ def internist() : Internist
+
+ def intern( varExpr : Absyn.VariableExpr )
+ : Nominal = { internist().intern( varExpr ) }
+ def compileExpr( numericExpr : Absyn.Numeric )
+ : Expression = {
+ new IntegerExpression(
+ numericExpr.integer_.asInstanceOf[Int]
+ )
+ }
+
+ // $\ldb$ x $\rdb$ $=$ $x$
+ def compileExpr( mentionExpr : Absyn.Mention )
+ : Expression = {
+ new Mention( intern( mentionExpr.variableexpr_ ) )
+ }
+ // $\ldb$ (x) => expr $\rdb$ $=$ $\lambda \; x . \ldb$ expr $\rdb$
+ def compileExpr( abstractionExpr : Absyn.Abstraction )
+ : Expression = {
+ val fmls : List[Nominal] =
+ abstractionExpr.listvariableexpr_.map(
+ { ( vExpr : Absyn.VariableExpr ) => intern( vExpr ) }
+ ).toList
+ new Abstraction( fmls, compile( abstractionExpr.expression_ ) )
+ }
+ // $\ldb$ expr( expr$_1$, ..., expr$_n$ ) $\rdb$
+ // $=$ $\ldb$ expr $\rdb$ $\ldb$ expr$_1$ $\rdb$ ... $\ldb$ expr$n$ $\rdb$
+ def compileExpr( applicationExpr : Absyn.Application )
+ : Expression = {
+ new Application(
+ compile( applicationExpr.expression_1 ),
+ List( compile( applicationExpr.expression_2 ) )
+ )
+ }
+
+ def compile( expr : Absyn.Expression )
+ : Expression = {
+ expr match {
+ case value : Absyn.Value => {
+ value.valueexpr_ match {
+ case numericExpr : Absyn.Numeric =>
+ compileExpr( numericExpr )
+ }
+ }
+ case numericExpr : Absyn.Numeric => {
+ compileExpr( numericExpr )
+ }
+ case mentionExpr : Absyn.Mention => {
+ compileExpr( mentionExpr )
+ }
+ case abstractionExpr : Absyn.Abstraction => {
+ compileExpr( abstractionExpr )
+ }
+ case applicationExpr : Absyn.Application => {
+ compileExpr( applicationExpr )
+ }
+ }
+ }
+
+ def parse( str : String ) : Absyn.Expression = {
+ (new parser(
+ new Yylex( new StringReader( str ) )
+ ) ).pExpression()
+ }
+
+ def compile( str : String ) : Expression = {
+ try {
+ compile( parse( str ) )
+ }
+ catch {
+ case e => { // log error
+ throw e
+ }
+ }
+ }
+ }
+\end{lstlisting}
+The first thing to notice about this translation is how faithfully it
+follows the equational specification.
\paragraph{Structural equivalence and Relations or What makes abstract syntax abstract}
@@ -449,7 +559,7 @@ \subsubsection{Our toy language}
\lstinline[language=Scala]!String!s, but what about
\lstinline[language=Scala]!Quotations!?
-The question reveals something quite startling about the types of
+The question reveals something quite startling about the types\footnote{Note that here we mean the type of the entity in the model that represents variables -- not a typing for variables in the language we're modeling.} of
variables. Clearly, the type has to include a definition of
equality. Now, if we want to have an inexhaustible supply of
variables, then the definition of equality of variables must make use
Oops, something went wrong.

0 comments on commit 09f0a8d

Please sign in to comment.