-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter4.thy
354 lines (281 loc) · 11.7 KB
/
Chapter4.thy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
theory Chapter4
imports "HOL-IMP.ASM"
begin
inductive star :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool" for r where
refl: "star r x x" |
step: "r x y \<Longrightarrow> star r y z \<Longrightarrow> star r x z"
text \<open>
\section*{Chapter 4}
\exercise
Start from the data type of binary trees defined earlier:
\<close>
datatype 'a tree = Tip | Node "'a tree" 'a "'a tree"
text \<open>
An @{typ "int tree"} is ordered if for every @{term "Node l i r"} in the tree,
@{text l} and @{text r} are ordered
and all values in @{text l} are @{text "< i"}
and all values in @{text r} are @{text "> i"}.
Define a function that returns the elements in a tree and one
the tests if a tree is ordered:
\<close>
fun set :: "'a tree \<Rightarrow> 'a set" where
"set Tip = {}" |
"set (Node l a r) = \<Union> {set l, {a}, set r}"
fun ord :: "int tree \<Rightarrow> bool" where
"ord Tip = True" |
"ord (Node l a r) \<longleftrightarrow> ord l \<and> ord r \<and> (\<forall> x \<in> set l. x < a) \<and> (\<forall> x \<in> set r. a < x)"
text \<open> Hint: use quantifiers.
Define a function @{text ins} that inserts an element into an ordered @{typ "int tree"}
while maintaining the order of the tree. If the element is already in the tree, the
same tree should be returned.
\<close>
fun ins :: "int \<Rightarrow> int tree \<Rightarrow> int tree" where
"ins x Tip = Node Tip x Tip" |
"ins x (Node l a r) =
(if x < a
then Node (ins x l) a r
else if a < x
then Node l a (ins x r)
else Node l a r)"
text \<open> Prove correctness of @{const ins}: \<close>
lemma set_ins: "set (ins x t) = {x} \<union> set t"
apply (induction t)
apply auto
done
theorem ord_ins: "ord t \<Longrightarrow> ord (ins i t)"
apply (induction t)
apply (auto simp add: set_ins)
done
text \<open>
\endexercise
\exercise
Formalize the following definition of palindromes
\begin{itemize}
\item The empty list and a singleton list are palindromes.
\item If @{text xs} is a palindrome, so is @{term "a # xs @ [a]"}.
\end{itemize}
as an inductive predicate
\<close>
inductive palindrome :: "'a list \<Rightarrow> bool" where
palindrome_Nil: "palindrome []" |
palindrome_singleton: "palindrome [a]" |
palindrome_circumfix: "palindrome as \<Longrightarrow> palindrome (a # as @ [a])"
text \<open> and prove \<close>
lemma "palindrome xs \<Longrightarrow> rev xs = xs"
apply (induction rule: palindrome.induct)
apply auto
done
text \<open>
\endexercise
\exercise
We could also have defined @{const star} as follows:
\<close>
inductive star' :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool" for r where
refl': "star' r x x" |
step': "star' r x y \<Longrightarrow> r y z \<Longrightarrow> star' r x z"
text \<open>
The single @{text r} step is performed after rather than before the @{text star'}
steps. Prove
\<close>
lemma star_trans: "star r x y \<Longrightarrow> star r y z \<Longrightarrow> star r x z"
apply (induction rule: star.induct)
apply assumption
by (rule step) (* alternatively, apply (metis step) done *)
lemma "star' r x y \<Longrightarrow> star r x y"
apply (induction rule: star'.induct)
by (auto simp add: refl step intro: star_trans)
(* limitation: no way to change order of premises, induction works
only on first premise *)
lemma star'_trans: "star' r y z \<Longrightarrow> star' r x y \<Longrightarrow> star' r x z"
apply (induction rule: star'.induct)
apply assumption
by (rule step')
lemma "star r x y \<Longrightarrow> star' r x y"
apply (induction rule: star.induct)
apply (auto intro: refl' step')
by (blast intro: refl' step' star'_trans)
text \<open>
You may need lemmas. Note that rule induction fails
if the assumption about the inductive predicate
is not the first assumption.
\endexercise
\exercise\label{exe:iter}
Analogous to @{const star}, give an inductive definition of the @{text n}-fold iteration
of a relation @{text r}: @{term "iter r n x y"} should hold if there are @{text x\<^sub>0}, \dots, @{text x\<^sub>n}
such that @{prop"x = x\<^sub>0"}, @{prop"x\<^sub>n = y"} and @{text"r x\<^bsub>i\<^esub> x\<^bsub>i+1\<^esub>"} for
all @{prop"i < n"}:
\<close>
inductive iter :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> bool" for r where
iter0: "iter r 0 x x" |
iterS: "\<lbrakk>r x y; iter r n y z \<rbrakk> \<Longrightarrow> iter r (Suc n) x z"
text \<open>
Correct and prove the following claim:
\<close>
lemma "star r x y \<Longrightarrow> \<exists> n. iter r n x y"
apply (induction rule: star.induct)
apply (blast intro: iter0)
by (blast intro: iterS)
text \<open>
\endexercise
\exercise\label{exe:cfg}
A context-free grammar can be seen as an inductive definition where each
nonterminal $A$ is an inductively defined predicate on lists of terminal
symbols: $A(w)$ mans that $w$ is in the language generated by $A$.
For example, the production $S \to aSb$ can be viewed as the implication
@{prop"S w \<Longrightarrow> S (a # w @ [b])"} where @{text a} and @{text b} are terminal symbols,
i.e., elements of some alphabet. The alphabet can be defined as a datatype:
\<close>
datatype alpha = alphaa | alphab
text \<open>
If you think of @{const alphaa} and @{const alphab} as ``@{text "("}'' and ``@{text ")"}'',
the following two grammars both generate strings of balanced parentheses
(where $\varepsilon$ is the empty word):
\[
\begin{array}{r@ {\quad}c@ {\quad}l}
S &\to& \varepsilon \quad\mid\quad aSb \quad\mid\quad SS \\
T &\to& \varepsilon \quad\mid\quad TaTb
\end{array}
\]
Define them as inductive predicates and prove their equivalence:
\<close>
inductive S :: "alpha list \<Rightarrow> bool" where
S_\<epsilon>: "S []" |
S_aSb: "S w \<Longrightarrow> S (alphaa # w @ [alphab])" |
S_SS: "\<lbrakk> S v; S w \<rbrakk> \<Longrightarrow> S (v @ w)"
inductive T :: "alpha list \<Rightarrow> bool" where
T_\<epsilon>: "T []" |
T_TaTb: "\<lbrakk> T v; T w \<rbrakk> \<Longrightarrow> T (v @ [alphaa] @ w @ [alphab])"
lemma TS: "T w \<Longrightarrow> S w"
apply (induction rule: T.induct)
by (auto intro: S_\<epsilon> S_SS S_aSb)
lemma ST_aux_1: "[] @ [alphaa] @ w @ [alphab] = alphaa # w @ [alphab]"
by simp
lemma ST_aux_2: "T v \<Longrightarrow> T w \<Longrightarrow> T (w @ v)"
apply (induction rule: T.induct)
apply simp
by (subst append_assoc [THEN sym], blast intro: T_TaTb)
lemma ST: "S w \<Longrightarrow> T w"
apply (induction rule: S.induct)
apply (rule T_\<epsilon>)
apply (subst ST_aux_1 [THEN sym], rule T_TaTb, rule T_\<epsilon>, assumption)
by (rule ST_aux_2)
corollary SeqT: "S w \<longleftrightarrow> T w"
by (auto intro: TS ST)
text \<open>
\endexercise
\<close>
text \<open>
\exercise
In Chapter 3 we defined a recursive evaluation function
@{text "aval ::"} @{typ "aexp \<Rightarrow> state \<Rightarrow> val"}.
Define an inductive evaluation predicate and prove that it agrees with
the recursive function:
\<close>
inductive aval_rel :: "aexp \<Rightarrow> state \<Rightarrow> val \<Rightarrow> bool" where
aval_N: "aval_rel (N i) s i" |
aval_V: "aval_rel (V x) s (s x)" |
aval_Plus: "\<lbrakk> aval_rel e\<^sub>1 s v\<^sub>1; aval_rel e\<^sub>2 s v\<^sub>2 \<rbrakk> \<Longrightarrow> aval_rel (Plus e\<^sub>1 e\<^sub>2) s (v\<^sub>1 + v\<^sub>2)"
lemma aval_rel_aval: "aval_rel e s v \<Longrightarrow> aval e s = v"
apply (induction rule: aval_rel.induct)
by auto
lemma aval_aval_rel: "aval e s = v \<Longrightarrow> aval_rel e s v"
apply (induction e arbitrary: v)
by (auto simp add: aval_N aval_V aval_Plus)
corollary "aval_rel e s v \<longleftrightarrow> aval e s = v"
by (blast intro: aval_rel_aval aval_aval_rel)
text \<open>
\endexercise
\exercise
Consider the stack machine from Chapter~3
and recall the concept of \concept{stack underflow}
from Exercise~\ref{exe:stack-underflow}.
Define an inductive predicate
\<close>
inductive ok :: "nat \<Rightarrow> instr list \<Rightarrow> nat \<Rightarrow> bool" where
ok_Nil: "ok 0 [] 0" |
ok_Pad: "ok n is n' \<Longrightarrow> ok (Suc n) is (Suc n')" |
ok_LOADI: "ok (Suc n) is n' \<Longrightarrow> ok n (LOADI i # is) n'" |
ok_LOAD: "ok (Suc n) is n' \<Longrightarrow> ok n (LOAD x # is) n'" |
ok_ADD: "ok (Suc n) is n' \<Longrightarrow> ok (Suc (Suc n)) (ADD # is) n'"
text \<open>
such that @{text "ok n is n'"} means that with any initial stack of length
@{text n} the instructions @{text "is"} can be executed
without stack underflow and that the final stack has length @{text n'}.
Using the introduction rules for @{const ok},
prove the following special cases: \<close>
lemma "ok 0 [LOAD x] (Suc 0)"
by (auto intro: ok_Nil ok_Pad ok_LOAD)
lemma "ok 0 [LOAD x, LOADI v, ADD] (Suc 0)"
apply (rule ok_LOAD)
apply (rule ok_LOADI)
apply (rule ok_ADD)
apply (rule ok_Pad)
by (rule ok_Nil)
lemma "ok (Suc (Suc 0)) [LOAD x, ADD, ADD, LOAD y] (Suc (Suc 0))"
apply (rule ok_LOAD)
apply (rule ok_ADD)
apply (rule ok_ADD)
apply (rule ok_LOAD)
apply (rule ok_Pad)
apply (rule ok_Pad)
by (rule ok_Nil)
text \<open> Prove that @{text ok} correctly computes the final stack size: \<close>
(* near impossible (probably impossible) without _tac and without using Isar *)
(* because of ok_Pad, inversion rules need to induct over that case *)
lemma ok_Nil_inv_aux: "\<lbrakk>ok n j n'; j = []\<rbrakk> \<Longrightarrow> n = n'"
apply (induction rule: ok.induct)
by auto
lemma ok_Nil_inv: "ok n [] n' \<Longrightarrow> n = n'"
by (simp add: ok_Nil_inv_aux)
lemma ok_LOADI_inv_aux: "\<lbrakk>ok n j n'; j = LOADI i # iss\<rbrakk> \<Longrightarrow> ok (Suc n) iss n'"
apply (induction rule: ok.induct)
by (auto simp add: ok.ok_Pad)
lemma ok_LOADI_inv: "ok n (LOADI i # iss) n' \<Longrightarrow> ok (Suc n) iss n'"
by (simp add: ok_LOADI_inv_aux)
lemma ok_LOAD_inv_aux: "\<lbrakk>ok n j n'; j = LOAD x # iss\<rbrakk> \<Longrightarrow> ok (Suc n) iss n'"
apply (induction rule: ok.induct)
by (auto simp add: ok.ok_Pad)
lemma ok_LOAD_inv: "ok n (LOAD x # iss) n' \<Longrightarrow> ok (Suc n) iss n'"
by (simp add: ok_LOAD_inv_aux)
lemma ok_ADD_inv_aux: "\<lbrakk>ok n j n'; j = ADD # iss\<rbrakk> \<Longrightarrow> \<exists> k. ok (Suc k) iss n' \<and> n = (Suc (Suc k))"
apply (induction rule: ok.induct)
by (auto simp add: ok.ok_Pad)
lemma ok_ADD_inv: "ok n (ADD # iss) n' \<Longrightarrow> \<exists> k. ok (Suc k) iss n' \<and> n = (Suc (Suc k))"
by (auto simp add: ok_ADD_inv_aux)
lemma "\<lbrakk>ok n inss n'; length stk = n\<rbrakk> \<Longrightarrow> length (exec inss s stk) = n'"
proof (induct inss arbitrary: n n' stk)
case Nil
thus ?case by (simp add: ok_Nil_inv)
next
case (Cons a inss)
note H = this
thus ?case
proof (cases a)
case (LOADI i)
thus ?thesis using Cons.hyps H(2) H(3) ok_LOADI_inv by fastforce
next
case (LOAD x)
note Hx = this
thus ?thesis using Cons.hyps H(2) H(3) ok_LOAD_inv by fastforce
next
case ADD
note Ha = this
from H(2) obtain k where H2': "ok (Suc k) inss n'" and H2eq: "n = (Suc (Suc k))" using Ha ok_ADD_inv by blast
from H(3) H2eq have H3': "length stk = Suc (Suc k)" by simp
then obtain x y stkr where Hstk: "stk = x # y # stkr" by (metis Suc_length_conv)
hence H3'': "length ((y + x) # stkr) = Suc k" using H3' by simp
from Ha Hstk have "length (exec (a # inss) s stk) = length (exec (ADD # inss) s (x # y # stkr))" by simp
also have "\<dots> = length (exec inss s ((y + x) # stkr))" by simp
also
from H(1) H2' H3'' have "\<dots> = n'" by simp
finally show ?thesis .
qed
qed
text \<open>
Lemma @{thm [source] length_Suc_conv} may come in handy.
Prove that instruction sequences generated by @{text comp}
cannot cause stack underflow: \ @{text "ok n (comp a) ?"} \ for
some suitable value of @{text "?"}.
\endexercise
\<close>
end