9722
|
1 |
%
|
|
2 |
\begin{isabellebody}%
|
9924
|
3 |
\def\isabellecontext{CodeGen}%
|
17181
|
4 |
\isamarkupfalse%
|
17056
|
5 |
%
|
|
6 |
\isadelimtheory
|
|
7 |
%
|
|
8 |
\endisadelimtheory
|
|
9 |
%
|
|
10 |
\isatagtheory
|
|
11 |
%
|
|
12 |
\endisatagtheory
|
|
13 |
{\isafoldtheory}%
|
|
14 |
%
|
|
15 |
\isadelimtheory
|
|
16 |
%
|
|
17 |
\endisadelimtheory
|
8746
|
18 |
%
|
10878
|
19 |
\isamarkupsection{Case Study: Compiling Expressions%
|
10395
|
20 |
}
|
11866
|
21 |
\isamarkuptrue%
|
9844
|
22 |
%
|
8746
|
23 |
\begin{isamarkuptext}%
|
9844
|
24 |
\label{sec:ExprCompiler}
|
11458
|
25 |
\index{compiling expressions example|(}%
|
8746
|
26 |
The task is to develop a compiler from a generic type of expressions (built
|
10795
|
27 |
from variables, constants and binary operations) to a stack machine. This
|
8746
|
28 |
generic type of expressions is a generalization of the boolean expressions in
|
|
29 |
\S\ref{sec:boolex}. This time we do not commit ourselves to a particular
|
|
30 |
type of variables or values but make them type parameters. Neither is there
|
|
31 |
a fixed set of binary operations: instead the expression contains the
|
|
32 |
appropriate function itself.%
|
|
33 |
\end{isamarkuptext}%
|
17175
|
34 |
\isamarkuptrue%
|
|
35 |
\isacommand{types}\isamarkupfalse%
|
|
36 |
\ {\isacharprime}v\ binop\ {\isacharequal}\ {\isachardoublequoteopen}{\isacharprime}v\ {\isasymRightarrow}\ {\isacharprime}v\ {\isasymRightarrow}\ {\isacharprime}v{\isachardoublequoteclose}\isanewline
|
|
37 |
\isacommand{datatype}\isamarkupfalse%
|
|
38 |
\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}expr\ {\isacharequal}\ Cex\ {\isacharprime}v\isanewline
|
9673
|
39 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharbar}\ Vex\ {\isacharprime}a\isanewline
|
17175
|
40 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharbar}\ Bex\ {\isachardoublequoteopen}{\isacharprime}v\ binop{\isachardoublequoteclose}\ \ {\isachardoublequoteopen}{\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}expr{\isachardoublequoteclose}\ \ {\isachardoublequoteopen}{\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}expr{\isachardoublequoteclose}%
|
8746
|
41 |
\begin{isamarkuptext}%
|
|
42 |
\noindent
|
8771
|
43 |
The three constructors represent constants, variables and the application of
|
|
44 |
a binary operation to two subexpressions.
|
8746
|
45 |
|
10795
|
46 |
The value of an expression with respect to an environment that maps variables to
|
8746
|
47 |
values is easily defined:%
|
|
48 |
\end{isamarkuptext}%
|
17175
|
49 |
\isamarkuptrue%
|
|
50 |
\isacommand{consts}\isamarkupfalse%
|
|
51 |
\ value\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}expr\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}v{\isacharparenright}\ {\isasymRightarrow}\ {\isacharprime}v{\isachardoublequoteclose}\isanewline
|
|
52 |
\isacommand{primrec}\isamarkupfalse%
|
|
53 |
\isanewline
|
|
54 |
{\isachardoublequoteopen}value\ {\isacharparenleft}Cex\ v{\isacharparenright}\ env\ {\isacharequal}\ v{\isachardoublequoteclose}\isanewline
|
|
55 |
{\isachardoublequoteopen}value\ {\isacharparenleft}Vex\ a{\isacharparenright}\ env\ {\isacharequal}\ env\ a{\isachardoublequoteclose}\isanewline
|
|
56 |
{\isachardoublequoteopen}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}{\isachardoublequoteclose}%
|
8746
|
57 |
\begin{isamarkuptext}%
|
|
58 |
The stack machine has three instructions: load a constant value onto the
|
10795
|
59 |
stack, load the contents of an address onto the stack, and apply a
|
8746
|
60 |
binary operation to the two topmost elements of the stack, replacing them by
|
|
61 |
the result. As for \isa{expr}, addresses and values are type parameters:%
|
|
62 |
\end{isamarkuptext}%
|
17175
|
63 |
\isamarkuptrue%
|
|
64 |
\isacommand{datatype}\isamarkupfalse%
|
|
65 |
\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}\ instr\ {\isacharequal}\ Const\ {\isacharprime}v\isanewline
|
9673
|
66 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharbar}\ Load\ {\isacharprime}a\isanewline
|
17175
|
67 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {\isacharbar}\ Apply\ {\isachardoublequoteopen}{\isacharprime}v\ binop{\isachardoublequoteclose}%
|
8746
|
68 |
\begin{isamarkuptext}%
|
8771
|
69 |
The execution of the stack machine is modelled by a function
|
|
70 |
\isa{exec} that takes a list of instructions, a store (modelled as a
|
|
71 |
function from addresses to values, just like the environment for
|
|
72 |
evaluating expressions), and a stack (modelled as a list) of values,
|
10971
|
73 |
and returns the stack at the end of the execution --- the store remains
|
8771
|
74 |
unchanged:%
|
8746
|
75 |
\end{isamarkuptext}%
|
17175
|
76 |
\isamarkuptrue%
|
|
77 |
\isacommand{consts}\isamarkupfalse%
|
|
78 |
\ exec\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\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{\isachardoublequoteclose}\isanewline
|
|
79 |
\isacommand{primrec}\isamarkupfalse%
|
|
80 |
\isanewline
|
|
81 |
{\isachardoublequoteopen}exec\ {\isacharbrackleft}{\isacharbrackright}\ s\ vs\ {\isacharequal}\ vs{\isachardoublequoteclose}\isanewline
|
|
82 |
{\isachardoublequoteopen}exec\ {\isacharparenleft}i{\isacharhash}is{\isacharparenright}\ s\ vs\ {\isacharequal}\ {\isacharparenleft}case\ i\ of\isanewline
|
9673
|
83 |
\ \ \ \ Const\ v\ \ {\isasymRightarrow}\ exec\ is\ s\ {\isacharparenleft}v{\isacharhash}vs{\isacharparenright}\isanewline
|
|
84 |
\ \ {\isacharbar}\ Load\ a\ \ \ {\isasymRightarrow}\ exec\ is\ s\ {\isacharparenleft}{\isacharparenleft}s\ a{\isacharparenright}{\isacharhash}vs{\isacharparenright}\isanewline
|
17175
|
85 |
\ \ {\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}{\isachardoublequoteclose}%
|
8746
|
86 |
\begin{isamarkuptext}%
|
|
87 |
\noindent
|
|
88 |
Recall that \isa{hd} and \isa{tl}
|
|
89 |
return the first element and the remainder of a list.
|
11458
|
90 |
Because all functions are total, \cdx{hd} is defined even for the empty
|
8746
|
91 |
list, although we do not know what the result is. Thus our model of the
|
10795
|
92 |
machine always terminates properly, although the definition above does not
|
8746
|
93 |
tell us much about the result in situations where \isa{Apply} was executed
|
|
94 |
with fewer than two elements on the stack.
|
|
95 |
|
|
96 |
The compiler is a function from expressions to a list of instructions. Its
|
10795
|
97 |
definition is obvious:%
|
8746
|
98 |
\end{isamarkuptext}%
|
17175
|
99 |
\isamarkuptrue%
|
|
100 |
\isacommand{consts}\isamarkupfalse%
|
|
101 |
\ comp\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}expr\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}{\isacharprime}v{\isacharparenright}instr\ list{\isachardoublequoteclose}\isanewline
|
|
102 |
\isacommand{primrec}\isamarkupfalse%
|
|
103 |
\isanewline
|
|
104 |
{\isachardoublequoteopen}comp\ {\isacharparenleft}Cex\ v{\isacharparenright}\ \ \ \ \ \ \ {\isacharequal}\ {\isacharbrackleft}Const\ v{\isacharbrackright}{\isachardoublequoteclose}\isanewline
|
|
105 |
{\isachardoublequoteopen}comp\ {\isacharparenleft}Vex\ a{\isacharparenright}\ \ \ \ \ \ \ {\isacharequal}\ {\isacharbrackleft}Load\ a{\isacharbrackright}{\isachardoublequoteclose}\isanewline
|
|
106 |
{\isachardoublequoteopen}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}{\isachardoublequoteclose}%
|
8746
|
107 |
\begin{isamarkuptext}%
|
|
108 |
Now we have to prove the correctness of the compiler, i.e.\ that the
|
|
109 |
execution of a compiled expression results in the value of the expression:%
|
|
110 |
\end{isamarkuptext}%
|
17175
|
111 |
\isamarkuptrue%
|
|
112 |
\isacommand{theorem}\isamarkupfalse%
|
|
113 |
\ {\isachardoublequoteopen}exec\ {\isacharparenleft}comp\ e{\isacharparenright}\ s\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ {\isacharbrackleft}value\ e\ s{\isacharbrackright}{\isachardoublequoteclose}%
|
17056
|
114 |
\isadelimproof
|
|
115 |
%
|
|
116 |
\endisadelimproof
|
|
117 |
%
|
|
118 |
\isatagproof
|
|
119 |
%
|
|
120 |
\endisatagproof
|
|
121 |
{\isafoldproof}%
|
|
122 |
%
|
|
123 |
\isadelimproof
|
|
124 |
%
|
|
125 |
\endisadelimproof
|
11866
|
126 |
%
|
8746
|
127 |
\begin{isamarkuptext}%
|
|
128 |
\noindent
|
11458
|
129 |
This theorem needs to be generalized:%
|
8746
|
130 |
\end{isamarkuptext}%
|
17175
|
131 |
\isamarkuptrue%
|
|
132 |
\isacommand{theorem}\isamarkupfalse%
|
|
133 |
\ {\isachardoublequoteopen}{\isasymforall}vs{\isachardot}\ exec\ {\isacharparenleft}comp\ e{\isacharparenright}\ s\ vs\ {\isacharequal}\ {\isacharparenleft}value\ e\ s{\isacharparenright}\ {\isacharhash}\ vs{\isachardoublequoteclose}%
|
17056
|
134 |
\isadelimproof
|
|
135 |
%
|
|
136 |
\endisadelimproof
|
|
137 |
%
|
|
138 |
\isatagproof
|
16069
|
139 |
%
|
|
140 |
\begin{isamarkuptxt}%
|
|
141 |
\noindent
|
|
142 |
It will be proved by induction on \isa{e} followed by simplification.
|
|
143 |
First, we must prove a lemma about executing the concatenation of two
|
|
144 |
instruction sequences:%
|
|
145 |
\end{isamarkuptxt}%
|
17175
|
146 |
\isamarkuptrue%
|
17056
|
147 |
%
|
|
148 |
\endisatagproof
|
|
149 |
{\isafoldproof}%
|
|
150 |
%
|
|
151 |
\isadelimproof
|
|
152 |
%
|
|
153 |
\endisadelimproof
|
17175
|
154 |
\isacommand{lemma}\isamarkupfalse%
|
|
155 |
\ exec{\isacharunderscore}app{\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\isanewline
|
|
156 |
\ \ {\isachardoublequoteopen}{\isasymforall}vs{\isachardot}\ exec\ {\isacharparenleft}xs{\isacharat}ys{\isacharparenright}\ s\ vs\ {\isacharequal}\ exec\ ys\ s\ {\isacharparenleft}exec\ xs\ s\ vs{\isacharparenright}{\isachardoublequoteclose}%
|
17056
|
157 |
\isadelimproof
|
|
158 |
%
|
|
159 |
\endisadelimproof
|
|
160 |
%
|
|
161 |
\isatagproof
|
16069
|
162 |
%
|
|
163 |
\begin{isamarkuptxt}%
|
|
164 |
\noindent
|
|
165 |
This requires induction on \isa{xs} and ordinary simplification for the
|
|
166 |
base cases. In the induction step, simplification leaves us with a formula
|
|
167 |
that contains two \isa{case}-expressions over instructions. Thus we add
|
|
168 |
automatic case splitting, which finishes the proof:%
|
|
169 |
\end{isamarkuptxt}%
|
17175
|
170 |
\isamarkuptrue%
|
|
171 |
\isacommand{apply}\isamarkupfalse%
|
17181
|
172 |
{\isacharparenleft}induct{\isacharunderscore}tac\ xs{\isacharcomma}\ simp{\isacharcomma}\ simp\ split{\isacharcolon}\ instr{\isachardot}split{\isacharparenright}%
|
17056
|
173 |
\endisatagproof
|
|
174 |
{\isafoldproof}%
|
|
175 |
%
|
|
176 |
\isadelimproof
|
|
177 |
%
|
|
178 |
\endisadelimproof
|
11866
|
179 |
%
|
8746
|
180 |
\begin{isamarkuptext}%
|
|
181 |
\noindent
|
11428
|
182 |
Note that because both \methdx{simp_all} and \methdx{auto} perform simplification, they can
|
|
183 |
be modified in the same way as \isa{simp}. Thus the proof can be
|
8746
|
184 |
rewritten as%
|
|
185 |
\end{isamarkuptext}%
|
17175
|
186 |
\isamarkuptrue%
|
17056
|
187 |
%
|
|
188 |
\isadelimproof
|
|
189 |
%
|
|
190 |
\endisadelimproof
|
|
191 |
%
|
|
192 |
\isatagproof
|
17175
|
193 |
\isacommand{apply}\isamarkupfalse%
|
17181
|
194 |
{\isacharparenleft}induct{\isacharunderscore}tac\ xs{\isacharcomma}\ simp{\isacharunderscore}all\ split{\isacharcolon}\ instr{\isachardot}split{\isacharparenright}%
|
17056
|
195 |
\endisatagproof
|
|
196 |
{\isafoldproof}%
|
|
197 |
%
|
|
198 |
\isadelimproof
|
|
199 |
%
|
|
200 |
\endisadelimproof
|
11866
|
201 |
%
|
8746
|
202 |
\begin{isamarkuptext}%
|
|
203 |
\noindent
|
|
204 |
Although this is more compact, it is less clear for the reader of the proof.
|
|
205 |
|
8771
|
206 |
We could now go back and prove \isa{exec (comp e) s [] = [value e s]}
|
8746
|
207 |
merely by simplification with the generalized version we just proved.
|
|
208 |
However, this is unnecessary because the generalized version fully subsumes
|
|
209 |
its instance.%
|
11458
|
210 |
\index{compiling expressions example|)}%
|
8746
|
211 |
\end{isamarkuptext}%
|
17175
|
212 |
\isamarkuptrue%
|
17056
|
213 |
%
|
|
214 |
\isadelimproof
|
|
215 |
%
|
|
216 |
\endisadelimproof
|
|
217 |
%
|
|
218 |
\isatagproof
|
|
219 |
%
|
|
220 |
\endisatagproof
|
|
221 |
{\isafoldproof}%
|
|
222 |
%
|
|
223 |
\isadelimproof
|
|
224 |
%
|
|
225 |
\endisadelimproof
|
|
226 |
%
|
|
227 |
\isadelimtheory
|
|
228 |
%
|
|
229 |
\endisadelimtheory
|
|
230 |
%
|
|
231 |
\isatagtheory
|
|
232 |
%
|
|
233 |
\endisatagtheory
|
|
234 |
{\isafoldtheory}%
|
|
235 |
%
|
|
236 |
\isadelimtheory
|
|
237 |
%
|
|
238 |
\endisadelimtheory
|
9722
|
239 |
\end{isabellebody}%
|
9145
|
240 |
%%% Local Variables:
|
|
241 |
%%% mode: latex
|
|
242 |
%%% TeX-master: "root"
|
|
243 |
%%% End:
|