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