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