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