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