| 9722 |      1 | %
 | 
|  |      2 | \begin{isabellebody}%
 | 
| 9924 |      3 | \def\isabellecontext{CodeGen}%
 | 
| 11866 |      4 | \isamarkupfalse%
 | 
| 8746 |      5 | %
 | 
| 10878 |      6 | \isamarkupsection{Case Study: Compiling Expressions%
 | 
| 10395 |      7 | }
 | 
| 11866 |      8 | \isamarkuptrue%
 | 
| 9844 |      9 | %
 | 
| 8746 |     10 | \begin{isamarkuptext}%
 | 
| 9844 |     11 | \label{sec:ExprCompiler}
 | 
| 11458 |     12 | \index{compiling expressions example|(}%
 | 
| 8746 |     13 | The task is to develop a compiler from a generic type of expressions (built
 | 
| 10795 |     14 | from variables, constants and binary operations) to a stack machine.  This
 | 
| 8746 |     15 | generic type of expressions is a generalization of the boolean expressions in
 | 
|  |     16 | \S\ref{sec:boolex}.  This time we do not commit ourselves to a particular
 | 
|  |     17 | type of variables or values but make them type parameters.  Neither is there
 | 
|  |     18 | a fixed set of binary operations: instead the expression contains the
 | 
|  |     19 | appropriate function itself.%
 | 
|  |     20 | \end{isamarkuptext}%
 | 
| 11866 |     21 | \isamarkuptrue%
 | 
| 9673 |     22 | \isacommand{types}\ {\isacharprime}v\ binop\ {\isacharequal}\ {\isachardoublequote}{\isacharprime}v\ {\isasymRightarrow}\ {\isacharprime}v\ {\isasymRightarrow}\ {\isacharprime}v{\isachardoublequote}\isanewline
 | 
| 11866 |     23 | \isamarkupfalse%
 | 
| 9673 |     24 | \isacommand{datatype}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}expr\ {\isacharequal}\ Cex\ {\isacharprime}v\isanewline
 | 
|  |     25 | \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharbar}\ Vex\ {\isacharprime}a\isanewline
 | 
| 11866 |     26 | \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharbar}\ Bex\ {\isachardoublequote}{\isacharprime}v\ binop{\isachardoublequote}\ \ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}expr{\isachardoublequote}\ \ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}expr{\isachardoublequote}\isamarkupfalse%
 | 
|  |     27 | %
 | 
| 8746 |     28 | \begin{isamarkuptext}%
 | 
|  |     29 | \noindent
 | 
| 8771 |     30 | The three constructors represent constants, variables and the application of
 | 
|  |     31 | a binary operation to two subexpressions.
 | 
| 8746 |     32 | 
 | 
| 10795 |     33 | The value of an expression with respect to an environment that maps variables to
 | 
| 8746 |     34 | values is easily defined:%
 | 
|  |     35 | \end{isamarkuptext}%
 | 
| 11866 |     36 | \isamarkuptrue%
 | 
| 9673 |     37 | \isacommand{consts}\ value\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}expr\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}v{\isacharparenright}\ {\isasymRightarrow}\ {\isacharprime}v{\isachardoublequote}\isanewline
 | 
| 11866 |     38 | \isamarkupfalse%
 | 
| 8746 |     39 | \isacommand{primrec}\isanewline
 | 
| 9673 |     40 | {\isachardoublequote}value\ {\isacharparenleft}Cex\ v{\isacharparenright}\ env\ {\isacharequal}\ v{\isachardoublequote}\isanewline
 | 
|  |     41 | {\isachardoublequote}value\ {\isacharparenleft}Vex\ a{\isacharparenright}\ env\ {\isacharequal}\ env\ a{\isachardoublequote}\isanewline
 | 
| 11866 |     42 | {\isachardoublequote}value\ {\isacharparenleft}Bex\ f\ e{\isadigit{1}}\ e{\isadigit{2}}{\isacharparenright}\ env\ {\isacharequal}\ f\ {\isacharparenleft}value\ e{\isadigit{1}}\ env{\isacharparenright}\ {\isacharparenleft}value\ e{\isadigit{2}}\ env{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
 | 
|  |     43 | %
 | 
| 8746 |     44 | \begin{isamarkuptext}%
 | 
|  |     45 | The stack machine has three instructions: load a constant value onto the
 | 
| 10795 |     46 | stack, load the contents of an address onto the stack, and apply a
 | 
| 8746 |     47 | binary operation to the two topmost elements of the stack, replacing them by
 | 
|  |     48 | the result. As for \isa{expr}, addresses and values are type parameters:%
 | 
|  |     49 | \end{isamarkuptext}%
 | 
| 11866 |     50 | \isamarkuptrue%
 | 
| 9673 |     51 | \isacommand{datatype}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}\ instr\ {\isacharequal}\ Const\ {\isacharprime}v\isanewline
 | 
|  |     52 | \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharbar}\ Load\ {\isacharprime}a\isanewline
 | 
| 11866 |     53 | \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharbar}\ Apply\ {\isachardoublequote}{\isacharprime}v\ binop{\isachardoublequote}\isamarkupfalse%
 | 
|  |     54 | %
 | 
| 8746 |     55 | \begin{isamarkuptext}%
 | 
| 8771 |     56 | The execution of the stack machine is modelled by a function
 | 
|  |     57 | \isa{exec} that takes a list of instructions, a store (modelled as a
 | 
|  |     58 | function from addresses to values, just like the environment for
 | 
|  |     59 | evaluating expressions), and a stack (modelled as a list) of values,
 | 
| 10971 |     60 | and returns the stack at the end of the execution --- the store remains
 | 
| 8771 |     61 | unchanged:%
 | 
| 8746 |     62 | \end{isamarkuptext}%
 | 
| 11866 |     63 | \isamarkuptrue%
 | 
| 9673 |     64 | \isacommand{consts}\ exec\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}instr\ list\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a{\isasymRightarrow}{\isacharprime}v{\isacharparenright}\ {\isasymRightarrow}\ {\isacharprime}v\ list\ {\isasymRightarrow}\ {\isacharprime}v\ list{\isachardoublequote}\isanewline
 | 
| 11866 |     65 | \isamarkupfalse%
 | 
| 8746 |     66 | \isacommand{primrec}\isanewline
 | 
| 9673 |     67 | {\isachardoublequote}exec\ {\isacharbrackleft}{\isacharbrackright}\ s\ vs\ {\isacharequal}\ vs{\isachardoublequote}\isanewline
 | 
|  |     68 | {\isachardoublequote}exec\ {\isacharparenleft}i{\isacharhash}is{\isacharparenright}\ s\ vs\ {\isacharequal}\ {\isacharparenleft}case\ i\ of\isanewline
 | 
|  |     69 | \ \ \ \ Const\ v\ \ {\isasymRightarrow}\ exec\ is\ s\ {\isacharparenleft}v{\isacharhash}vs{\isacharparenright}\isanewline
 | 
|  |     70 | \ \ {\isacharbar}\ Load\ a\ \ \ {\isasymRightarrow}\ exec\ is\ s\ {\isacharparenleft}{\isacharparenleft}s\ a{\isacharparenright}{\isacharhash}vs{\isacharparenright}\isanewline
 | 
| 11866 |     71 | \ \ {\isacharbar}\ Apply\ f\ \ {\isasymRightarrow}\ exec\ is\ s\ {\isacharparenleft}{\isacharparenleft}f\ {\isacharparenleft}hd\ vs{\isacharparenright}\ {\isacharparenleft}hd{\isacharparenleft}tl\ vs{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isacharhash}{\isacharparenleft}tl{\isacharparenleft}tl\ vs{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
 | 
|  |     72 | %
 | 
| 8746 |     73 | \begin{isamarkuptext}%
 | 
|  |     74 | \noindent
 | 
|  |     75 | Recall that \isa{hd} and \isa{tl}
 | 
|  |     76 | return the first element and the remainder of a list.
 | 
| 11458 |     77 | Because all functions are total, \cdx{hd} is defined even for the empty
 | 
| 8746 |     78 | list, although we do not know what the result is. Thus our model of the
 | 
| 10795 |     79 | machine always terminates properly, although the definition above does not
 | 
| 8746 |     80 | tell us much about the result in situations where \isa{Apply} was executed
 | 
|  |     81 | with fewer than two elements on the stack.
 | 
|  |     82 | 
 | 
|  |     83 | The compiler is a function from expressions to a list of instructions. Its
 | 
| 10795 |     84 | definition is obvious:%
 | 
| 8746 |     85 | \end{isamarkuptext}%
 | 
| 11866 |     86 | \isamarkuptrue%
 | 
| 9673 |     87 | \isacommand{consts}\ comp\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}expr\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}instr\ list{\isachardoublequote}\isanewline
 | 
| 11866 |     88 | \isamarkupfalse%
 | 
| 8746 |     89 | \isacommand{primrec}\isanewline
 | 
| 9673 |     90 | {\isachardoublequote}comp\ {\isacharparenleft}Cex\ v{\isacharparenright}\ \ \ \ \ \ \ {\isacharequal}\ {\isacharbrackleft}Const\ v{\isacharbrackright}{\isachardoublequote}\isanewline
 | 
|  |     91 | {\isachardoublequote}comp\ {\isacharparenleft}Vex\ a{\isacharparenright}\ \ \ \ \ \ \ {\isacharequal}\ {\isacharbrackleft}Load\ a{\isacharbrackright}{\isachardoublequote}\isanewline
 | 
| 11866 |     92 | {\isachardoublequote}comp\ {\isacharparenleft}Bex\ f\ e{\isadigit{1}}\ e{\isadigit{2}}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}comp\ e{\isadigit{2}}{\isacharparenright}\ {\isacharat}\ {\isacharparenleft}comp\ e{\isadigit{1}}{\isacharparenright}\ {\isacharat}\ {\isacharbrackleft}Apply\ f{\isacharbrackright}{\isachardoublequote}\isamarkupfalse%
 | 
|  |     93 | %
 | 
| 8746 |     94 | \begin{isamarkuptext}%
 | 
|  |     95 | Now we have to prove the correctness of the compiler, i.e.\ that the
 | 
|  |     96 | execution of a compiled expression results in the value of the expression:%
 | 
|  |     97 | \end{isamarkuptext}%
 | 
| 11866 |     98 | \isamarkuptrue%
 | 
| 13791 |     99 | \isacommand{theorem}\ {\isachardoublequote}exec\ {\isacharparenleft}comp\ e{\isacharparenright}\ s\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ {\isacharbrackleft}value\ e\ s{\isacharbrackright}{\isachardoublequote}\isamarkupfalse%
 | 
| 11866 |    100 | \isamarkupfalse%
 | 
|  |    101 | %
 | 
| 8746 |    102 | \begin{isamarkuptext}%
 | 
|  |    103 | \noindent
 | 
| 11458 |    104 | This theorem needs to be generalized:%
 | 
| 8746 |    105 | \end{isamarkuptext}%
 | 
| 11866 |    106 | \isamarkuptrue%
 | 
|  |    107 | \isacommand{theorem}\ {\isachardoublequote}{\isasymforall}vs{\isachardot}\ exec\ {\isacharparenleft}comp\ e{\isacharparenright}\ s\ vs\ {\isacharequal}\ {\isacharparenleft}value\ e\ s{\isacharparenright}\ {\isacharhash}\ vs{\isachardoublequote}\isamarkupfalse%
 | 
|  |    108 | %
 | 
| 8746 |    109 | \begin{isamarkuptxt}%
 | 
|  |    110 | \noindent
 | 
| 11458 |    111 | It will be proved by induction on \isa{e} followed by simplification.  
 | 
|  |    112 | First, we must prove a lemma about executing the concatenation of two
 | 
| 8746 |    113 | instruction sequences:%
 | 
|  |    114 | \end{isamarkuptxt}%
 | 
| 11866 |    115 | \isamarkuptrue%
 | 
|  |    116 | \isamarkupfalse%
 | 
| 9673 |    117 | \isacommand{lemma}\ exec{\isacharunderscore}app{\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\isanewline
 | 
| 11866 |    118 | \ \ {\isachardoublequote}{\isasymforall}vs{\isachardot}\ exec\ {\isacharparenleft}xs{\isacharat}ys{\isacharparenright}\ s\ vs\ {\isacharequal}\ exec\ ys\ s\ {\isacharparenleft}exec\ xs\ s\ vs{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
 | 
|  |    119 | %
 | 
| 8746 |    120 | \begin{isamarkuptxt}%
 | 
|  |    121 | \noindent
 | 
|  |    122 | This requires induction on \isa{xs} and ordinary simplification for the
 | 
|  |    123 | base cases. In the induction step, simplification leaves us with a formula
 | 
|  |    124 | that contains two \isa{case}-expressions over instructions. Thus we add
 | 
| 11458 |    125 | automatic case splitting, which finishes the proof:%
 | 
| 8746 |    126 | \end{isamarkuptxt}%
 | 
| 11866 |    127 | \isamarkuptrue%
 | 
| 13791 |    128 | \isacommand{apply}{\isacharparenleft}induct{\isacharunderscore}tac\ xs{\isacharcomma}\ simp{\isacharcomma}\ simp\ split{\isacharcolon}\ instr{\isachardot}split{\isacharparenright}\isamarkupfalse%
 | 
| 11866 |    129 | \isamarkupfalse%
 | 
|  |    130 | %
 | 
| 8746 |    131 | \begin{isamarkuptext}%
 | 
|  |    132 | \noindent
 | 
| 11428 |    133 | Note that because both \methdx{simp_all} and \methdx{auto} perform simplification, they can
 | 
|  |    134 | be modified in the same way as \isa{simp}.  Thus the proof can be
 | 
| 8746 |    135 | rewritten as%
 | 
|  |    136 | \end{isamarkuptext}%
 | 
| 11866 |    137 | \isamarkuptrue%
 | 
|  |    138 | \isamarkupfalse%
 | 
|  |    139 | \isamarkupfalse%
 | 
| 13791 |    140 | \isacommand{apply}{\isacharparenleft}induct{\isacharunderscore}tac\ xs{\isacharcomma}\ simp{\isacharunderscore}all\ split{\isacharcolon}\ instr{\isachardot}split{\isacharparenright}\isamarkupfalse%
 | 
| 11866 |    141 | \isamarkupfalse%
 | 
|  |    142 | %
 | 
| 8746 |    143 | \begin{isamarkuptext}%
 | 
|  |    144 | \noindent
 | 
|  |    145 | Although this is more compact, it is less clear for the reader of the proof.
 | 
|  |    146 | 
 | 
| 8771 |    147 | We could now go back and prove \isa{exec (comp e) s [] = [value e s]}
 | 
| 8746 |    148 | merely by simplification with the generalized version we just proved.
 | 
|  |    149 | However, this is unnecessary because the generalized version fully subsumes
 | 
|  |    150 | its instance.%
 | 
| 11458 |    151 | \index{compiling expressions example|)}%
 | 
| 8746 |    152 | \end{isamarkuptext}%
 | 
| 11866 |    153 | \isamarkuptrue%
 | 
|  |    154 | \isamarkupfalse%
 | 
|  |    155 | \isamarkupfalse%
 | 
|  |    156 | \isamarkupfalse%
 | 
| 9722 |    157 | \end{isabellebody}%
 | 
| 9145 |    158 | %%% Local Variables:
 | 
|  |    159 | %%% mode: latex
 | 
|  |    160 | %%% TeX-master: "root"
 | 
|  |    161 | %%% End:
 |