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