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