doc-src/TutorialI/CodeGen/document/CodeGen.tex
author nipkow
Tue, 29 Aug 2000 15:43:29 +0200
changeset 9722 a5f86aed785b
parent 9721 7e51c9f3d5a0
child 9844 8016321c7de1
permissions -rw-r--r--
*** empty log message ***
nipkow@9722
     1
%
nipkow@9722
     2
\begin{isabellebody}%
nipkow@8746
     3
%
nipkow@8746
     4
\begin{isamarkuptext}%
nipkow@8746
     5
\noindent
nipkow@8746
     6
The task is to develop a compiler from a generic type of expressions (built
nipkow@8746
     7
up from variables, constants and binary operations) to a stack machine.  This
nipkow@8746
     8
generic type of expressions is a generalization of the boolean expressions in
nipkow@8746
     9
\S\ref{sec:boolex}.  This time we do not commit ourselves to a particular
nipkow@8746
    10
type of variables or values but make them type parameters.  Neither is there
nipkow@8746
    11
a fixed set of binary operations: instead the expression contains the
nipkow@8746
    12
appropriate function itself.%
nipkow@8746
    13
\end{isamarkuptext}%
wenzelm@9673
    14
\isacommand{types}\ {\isacharprime}v\ binop\ {\isacharequal}\ {\isachardoublequote}{\isacharprime}v\ {\isasymRightarrow}\ {\isacharprime}v\ {\isasymRightarrow}\ {\isacharprime}v{\isachardoublequote}\isanewline
wenzelm@9673
    15
\isacommand{datatype}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}expr\ {\isacharequal}\ Cex\ {\isacharprime}v\isanewline
wenzelm@9673
    16
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharbar}\ Vex\ {\isacharprime}a\isanewline
wenzelm@9673
    17
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\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}%
nipkow@8746
    18
\begin{isamarkuptext}%
nipkow@8746
    19
\noindent
nipkow@8771
    20
The three constructors represent constants, variables and the application of
nipkow@8771
    21
a binary operation to two subexpressions.
nipkow@8746
    22
nipkow@8746
    23
The value of an expression w.r.t.\ an environment that maps variables to
nipkow@8746
    24
values is easily defined:%
nipkow@8746
    25
\end{isamarkuptext}%
wenzelm@9673
    26
\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
nipkow@8746
    27
\isacommand{primrec}\isanewline
wenzelm@9673
    28
{\isachardoublequote}value\ {\isacharparenleft}Cex\ v{\isacharparenright}\ env\ {\isacharequal}\ v{\isachardoublequote}\isanewline
wenzelm@9673
    29
{\isachardoublequote}value\ {\isacharparenleft}Vex\ a{\isacharparenright}\ env\ {\isacharequal}\ env\ a{\isachardoublequote}\isanewline
wenzelm@9673
    30
{\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}%
nipkow@8746
    31
\begin{isamarkuptext}%
nipkow@8746
    32
The stack machine has three instructions: load a constant value onto the
nipkow@8746
    33
stack, load the contents of a certain address onto the stack, and apply a
nipkow@8746
    34
binary operation to the two topmost elements of the stack, replacing them by
nipkow@8746
    35
the result. As for \isa{expr}, addresses and values are type parameters:%
nipkow@8746
    36
\end{isamarkuptext}%
wenzelm@9673
    37
\isacommand{datatype}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}\ instr\ {\isacharequal}\ Const\ {\isacharprime}v\isanewline
wenzelm@9673
    38
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharbar}\ Load\ {\isacharprime}a\isanewline
wenzelm@9673
    39
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharbar}\ Apply\ {\isachardoublequote}{\isacharprime}v\ binop{\isachardoublequote}%
nipkow@8746
    40
\begin{isamarkuptext}%
nipkow@8771
    41
The execution of the stack machine is modelled by a function
nipkow@8771
    42
\isa{exec} that takes a list of instructions, a store (modelled as a
nipkow@8771
    43
function from addresses to values, just like the environment for
nipkow@8771
    44
evaluating expressions), and a stack (modelled as a list) of values,
nipkow@8771
    45
and returns the stack at the end of the execution---the store remains
nipkow@8771
    46
unchanged:%
nipkow@8746
    47
\end{isamarkuptext}%
wenzelm@9673
    48
\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
nipkow@8746
    49
\isacommand{primrec}\isanewline
wenzelm@9673
    50
{\isachardoublequote}exec\ {\isacharbrackleft}{\isacharbrackright}\ s\ vs\ {\isacharequal}\ vs{\isachardoublequote}\isanewline
wenzelm@9673
    51
{\isachardoublequote}exec\ {\isacharparenleft}i{\isacharhash}is{\isacharparenright}\ s\ vs\ {\isacharequal}\ {\isacharparenleft}case\ i\ of\isanewline
wenzelm@9673
    52
\ \ \ \ Const\ v\ \ {\isasymRightarrow}\ exec\ is\ s\ {\isacharparenleft}v{\isacharhash}vs{\isacharparenright}\isanewline
wenzelm@9673
    53
\ \ {\isacharbar}\ Load\ a\ \ \ {\isasymRightarrow}\ exec\ is\ s\ {\isacharparenleft}{\isacharparenleft}s\ a{\isacharparenright}{\isacharhash}vs{\isacharparenright}\isanewline
wenzelm@9673
    54
\ \ {\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}%
nipkow@8746
    55
\begin{isamarkuptext}%
nipkow@8746
    56
\noindent
nipkow@8746
    57
Recall that \isa{hd} and \isa{tl}
nipkow@8746
    58
return the first element and the remainder of a list.
nipkow@8746
    59
Because all functions are total, \isa{hd} is defined even for the empty
nipkow@8746
    60
list, although we do not know what the result is. Thus our model of the
nipkow@8746
    61
machine always terminates properly, although the above definition does not
nipkow@8746
    62
tell us much about the result in situations where \isa{Apply} was executed
nipkow@8746
    63
with fewer than two elements on the stack.
nipkow@8746
    64
nipkow@8746
    65
The compiler is a function from expressions to a list of instructions. Its
nipkow@8746
    66
definition is pretty much obvious:%
nipkow@8746
    67
\end{isamarkuptext}%
wenzelm@9673
    68
\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
nipkow@8746
    69
\isacommand{primrec}\isanewline
wenzelm@9673
    70
{\isachardoublequote}comp\ {\isacharparenleft}Cex\ v{\isacharparenright}\ \ \ \ \ \ \ {\isacharequal}\ {\isacharbrackleft}Const\ v{\isacharbrackright}{\isachardoublequote}\isanewline
wenzelm@9673
    71
{\isachardoublequote}comp\ {\isacharparenleft}Vex\ a{\isacharparenright}\ \ \ \ \ \ \ {\isacharequal}\ {\isacharbrackleft}Load\ a{\isacharbrackright}{\isachardoublequote}\isanewline
wenzelm@9673
    72
{\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}%
nipkow@8746
    73
\begin{isamarkuptext}%
nipkow@8746
    74
Now we have to prove the correctness of the compiler, i.e.\ that the
nipkow@8746
    75
execution of a compiled expression results in the value of the expression:%
nipkow@8746
    76
\end{isamarkuptext}%
wenzelm@9673
    77
\isacommand{theorem}\ {\isachardoublequote}exec\ {\isacharparenleft}comp\ e{\isacharparenright}\ s\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ {\isacharbrackleft}value\ e\ s{\isacharbrackright}{\isachardoublequote}%
nipkow@8746
    78
\begin{isamarkuptext}%
nipkow@8746
    79
\noindent
nipkow@8746
    80
This theorem needs to be generalized to%
nipkow@8746
    81
\end{isamarkuptext}%
wenzelm@9673
    82
\isacommand{theorem}\ {\isachardoublequote}{\isasymforall}vs{\isachardot}\ exec\ {\isacharparenleft}comp\ e{\isacharparenright}\ s\ vs\ {\isacharequal}\ {\isacharparenleft}value\ e\ s{\isacharparenright}\ {\isacharhash}\ vs{\isachardoublequote}%
nipkow@8746
    83
\begin{isamarkuptxt}%
nipkow@8746
    84
\noindent
nipkow@8746
    85
which is proved by induction on \isa{e} followed by simplification, once
nipkow@8746
    86
we have the following lemma about executing the concatenation of two
nipkow@8746
    87
instruction sequences:%
nipkow@8746
    88
\end{isamarkuptxt}%
wenzelm@9673
    89
\isacommand{lemma}\ exec{\isacharunderscore}app{\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\isanewline
wenzelm@9673
    90
\ \ {\isachardoublequote}{\isasymforall}vs{\isachardot}\ exec\ {\isacharparenleft}xs{\isacharat}ys{\isacharparenright}\ s\ vs\ {\isacharequal}\ exec\ ys\ s\ {\isacharparenleft}exec\ xs\ s\ vs{\isacharparenright}{\isachardoublequote}%
nipkow@8746
    91
\begin{isamarkuptxt}%
nipkow@8746
    92
\noindent
nipkow@8746
    93
This requires induction on \isa{xs} and ordinary simplification for the
nipkow@8746
    94
base cases. In the induction step, simplification leaves us with a formula
nipkow@8746
    95
that contains two \isa{case}-expressions over instructions. Thus we add
nipkow@8746
    96
automatic case splitting as well, which finishes the proof:%
nipkow@8746
    97
\end{isamarkuptxt}%
wenzelm@9673
    98
\isacommand{by}{\isacharparenleft}induct{\isacharunderscore}tac\ xs{\isacharcomma}\ simp{\isacharcomma}\ simp\ split{\isacharcolon}\ instr{\isachardot}split{\isacharparenright}%
nipkow@8746
    99
\begin{isamarkuptext}%
nipkow@8746
   100
\noindent
nipkow@8746
   101
Note that because \isaindex{auto} performs simplification, it can
nipkow@8746
   102
also be modified in the same way \isa{simp} can. Thus the proof can be
nipkow@8746
   103
rewritten as%
nipkow@8746
   104
\end{isamarkuptext}%
wenzelm@9673
   105
\isacommand{by}{\isacharparenleft}induct{\isacharunderscore}tac\ xs{\isacharcomma}\ auto\ split{\isacharcolon}\ instr{\isachardot}split{\isacharparenright}%
nipkow@8746
   106
\begin{isamarkuptext}%
nipkow@8746
   107
\noindent
nipkow@8746
   108
Although this is more compact, it is less clear for the reader of the proof.
nipkow@8746
   109
nipkow@8771
   110
We could now go back and prove \isa{exec (comp e) s [] = [value e s]}
nipkow@8746
   111
merely by simplification with the generalized version we just proved.
nipkow@8746
   112
However, this is unnecessary because the generalized version fully subsumes
nipkow@8746
   113
its instance.%
nipkow@8746
   114
\end{isamarkuptext}%
nipkow@9722
   115
\end{isabellebody}%
wenzelm@9145
   116
%%% Local Variables:
wenzelm@9145
   117
%%% mode: latex
wenzelm@9145
   118
%%% TeX-master: "root"
wenzelm@9145
   119
%%% End: