13405
|
1 |
(* Title: HOL/Extraction/Higman.thy
|
|
2 |
ID: $Id$
|
|
3 |
Author: Stefan Berghofer, TU Muenchen
|
|
4 |
Monika Seisenberger, LMU Muenchen
|
|
5 |
*)
|
|
6 |
|
|
7 |
header {* Higman's lemma *}
|
|
8 |
|
|
9 |
theory Higman = Main:
|
|
10 |
|
|
11 |
text {*
|
|
12 |
Formalization by Stefan Berghofer and Monika Seisenberger,
|
|
13 |
based on Coquand and Fridlender \cite{Coquand93}.
|
|
14 |
*}
|
|
15 |
|
|
16 |
datatype letter = A | B
|
|
17 |
|
|
18 |
consts
|
|
19 |
emb :: "(letter list \<times> letter list) set"
|
|
20 |
|
|
21 |
inductive emb
|
|
22 |
intros
|
|
23 |
emb0 [CPure.intro]: "([], bs) \<in> emb"
|
|
24 |
emb1 [CPure.intro]: "(as, bs) \<in> emb \<Longrightarrow> (as, b # bs) \<in> emb"
|
|
25 |
emb2 [CPure.intro]: "(as, bs) \<in> emb \<Longrightarrow> (a # as, a # bs) \<in> emb"
|
|
26 |
|
|
27 |
consts
|
|
28 |
L :: "letter list \<Rightarrow> letter list list set"
|
|
29 |
|
|
30 |
inductive "L y"
|
|
31 |
intros
|
|
32 |
L0 [CPure.intro]: "(w, y) \<in> emb \<Longrightarrow> w # ws \<in> L y"
|
|
33 |
L1 [CPure.intro]: "ws \<in> L y \<Longrightarrow> w # ws \<in> L y"
|
|
34 |
|
|
35 |
consts
|
|
36 |
good :: "letter list list set"
|
|
37 |
|
|
38 |
inductive good
|
|
39 |
intros
|
|
40 |
good0 [CPure.intro]: "ws \<in> L w \<Longrightarrow> w # ws \<in> good"
|
|
41 |
good1 [CPure.intro]: "ws \<in> good \<Longrightarrow> w # ws \<in> good"
|
|
42 |
|
|
43 |
consts
|
|
44 |
R :: "letter \<Rightarrow> (letter list list \<times> letter list list) set"
|
|
45 |
|
|
46 |
inductive "R a"
|
|
47 |
intros
|
|
48 |
R0 [CPure.intro]: "([], []) \<in> R a"
|
|
49 |
R1 [CPure.intro]: "(vs, ws) \<in> R a \<Longrightarrow> (w # vs, (a # w) # ws) \<in> R a"
|
|
50 |
|
|
51 |
consts
|
|
52 |
T :: "letter \<Rightarrow> (letter list list \<times> letter list list) set"
|
|
53 |
|
|
54 |
inductive "T a"
|
|
55 |
intros
|
|
56 |
T0 [CPure.intro]: "a \<noteq> b \<Longrightarrow> (ws, zs) \<in> R b \<Longrightarrow> (w # zs, (a # w) # zs) \<in> T a"
|
|
57 |
T1 [CPure.intro]: "(ws, zs) \<in> T a \<Longrightarrow> (w # ws, (a # w) # zs) \<in> T a"
|
|
58 |
T2 [CPure.intro]: "a \<noteq> b \<Longrightarrow> (ws, zs) \<in> T a \<Longrightarrow> (ws, (b # w) # zs) \<in> T a"
|
|
59 |
|
|
60 |
consts
|
|
61 |
bar :: "letter list list set"
|
|
62 |
|
|
63 |
inductive bar
|
|
64 |
intros
|
|
65 |
bar1 [CPure.intro]: "ws \<in> good \<Longrightarrow> ws \<in> bar"
|
|
66 |
bar2 [CPure.intro]: "(\<forall>w. w # ws \<in> bar) \<Longrightarrow> ws \<in> bar"
|
|
67 |
|
|
68 |
theorem prop1: "([] # ws) \<in> bar" by rules
|
|
69 |
|
|
70 |
theorem lemma1: "ws \<in> L as \<Longrightarrow> ws \<in> L (a # as)"
|
|
71 |
by (erule L.induct, rules+)
|
|
72 |
|
|
73 |
theorem lemma2' [rule_format]: "(vs, ws) \<in> R a \<Longrightarrow> vs \<in> L as \<longrightarrow> ws \<in> L (a # as)"
|
|
74 |
apply (erule R.induct)
|
|
75 |
apply (rule impI)
|
|
76 |
apply (erule L.elims)
|
|
77 |
apply simp+
|
|
78 |
apply (rule impI)
|
|
79 |
apply (erule L.elims)
|
|
80 |
apply simp_all
|
|
81 |
apply (rule L0)
|
|
82 |
apply (erule emb2)
|
|
83 |
apply (erule L1)
|
|
84 |
done
|
|
85 |
|
|
86 |
theorem lemma2 [rule_format]: "(vs, ws) \<in> R a \<Longrightarrow> vs \<in> good \<longrightarrow> ws \<in> good"
|
|
87 |
apply (erule R.induct)
|
|
88 |
apply rules
|
|
89 |
apply (rule impI)
|
|
90 |
apply (erule good.elims)
|
|
91 |
apply simp_all
|
|
92 |
apply (rule good0)
|
|
93 |
apply (erule lemma2')
|
|
94 |
apply assumption
|
|
95 |
apply (erule good1)
|
|
96 |
done
|
|
97 |
|
|
98 |
theorem lemma3' [rule_format]:
|
|
99 |
"(vs, ws) \<in> T a \<Longrightarrow> vs \<in> L as \<longrightarrow> ws \<in> L (a # as)"
|
|
100 |
apply (erule T.induct)
|
|
101 |
apply (rule impI)
|
|
102 |
apply (erule L.elims)
|
|
103 |
apply simp_all
|
|
104 |
apply (rule L0)
|
|
105 |
apply (erule emb2)
|
|
106 |
apply (rule L1)
|
|
107 |
apply (erule lemma1)
|
|
108 |
apply (rule impI)
|
|
109 |
apply (erule L.elims)
|
|
110 |
apply simp_all
|
|
111 |
apply rules+
|
|
112 |
done
|
|
113 |
|
|
114 |
theorem lemma3 [rule_format]: "(ws, zs) \<in> T a \<Longrightarrow> ws \<in> good \<longrightarrow> zs \<in> good"
|
|
115 |
apply (erule T.induct)
|
|
116 |
apply (rule impI)
|
|
117 |
apply (erule good.elims)
|
|
118 |
apply simp_all
|
|
119 |
apply (rule good0)
|
|
120 |
apply (erule lemma1)
|
|
121 |
apply (erule good1)
|
|
122 |
apply (rule impI)
|
|
123 |
apply (erule good.elims)
|
|
124 |
apply simp_all
|
|
125 |
apply (rule good0)
|
|
126 |
apply (erule lemma3')
|
|
127 |
apply rules+
|
|
128 |
done
|
|
129 |
|
|
130 |
theorem letter_cases:
|
|
131 |
"(a::letter) \<noteq> b \<Longrightarrow> (x = a \<Longrightarrow> P) \<Longrightarrow> (x = b \<Longrightarrow> P) \<Longrightarrow> P"
|
|
132 |
apply (case_tac a)
|
|
133 |
apply (case_tac b)
|
|
134 |
apply (case_tac x, simp, simp)
|
|
135 |
apply (case_tac x, simp, simp)
|
|
136 |
apply (case_tac b)
|
|
137 |
apply (case_tac x, simp, simp)
|
|
138 |
apply (case_tac x, simp, simp)
|
|
139 |
done
|
|
140 |
|
|
141 |
theorem prop2:
|
|
142 |
"xs \<in> bar \<Longrightarrow> \<forall>ys. ys \<in> bar \<longrightarrow>
|
|
143 |
(\<forall>a b zs. a \<noteq> b \<longrightarrow> (xs, zs) \<in> T a \<longrightarrow> (ys, zs) \<in> T b \<longrightarrow> zs \<in> bar)"
|
|
144 |
apply (erule bar.induct)
|
|
145 |
apply (rule allI impI)+
|
|
146 |
apply (rule bar1)
|
|
147 |
apply (rule lemma3)
|
|
148 |
apply assumption+
|
|
149 |
apply (rule allI, rule impI)
|
|
150 |
apply (erule bar.induct)
|
|
151 |
apply (rule allI impI)+
|
|
152 |
apply (rule bar1)
|
|
153 |
apply (rule lemma3, assumption, assumption)
|
|
154 |
apply (rule allI impI)+
|
|
155 |
apply (rule bar2)
|
|
156 |
apply (rule allI)
|
|
157 |
apply (induct_tac w)
|
|
158 |
apply (rule prop1)
|
|
159 |
apply (rule_tac x=aa in letter_cases, assumption)
|
|
160 |
apply hypsubst
|
|
161 |
apply (erule_tac x=list in allE)
|
|
162 |
apply (erule conjE)
|
|
163 |
apply (erule_tac x=wsa in allE, erule impE)
|
|
164 |
apply (rule bar2)
|
|
165 |
apply rules
|
|
166 |
apply (erule allE, erule allE, erule_tac x="(a # list) # zs" in allE,
|
|
167 |
erule impE, assumption)
|
|
168 |
apply (erule impE)
|
|
169 |
apply (rule T1)
|
|
170 |
apply assumption
|
|
171 |
apply (erule mp)
|
|
172 |
apply (rule T2)
|
|
173 |
apply (erule not_sym)
|
|
174 |
apply assumption
|
|
175 |
apply hypsubst
|
|
176 |
apply (rotate_tac 1)
|
|
177 |
apply (erule_tac x=list in allE)
|
|
178 |
apply (erule conjE)
|
|
179 |
apply (erule allE, erule allE, erule_tac x="(b # list) # zs" in allE,
|
|
180 |
erule impE)
|
|
181 |
apply assumption
|
|
182 |
apply (erule impE)
|
|
183 |
apply (rule T2)
|
|
184 |
apply assumption
|
|
185 |
apply assumption
|
|
186 |
apply (erule mp)
|
|
187 |
apply (rule T1)
|
|
188 |
apply assumption
|
|
189 |
done
|
|
190 |
|
|
191 |
theorem lemma4 [rule_format]: "(ws, zs) \<in> R a \<Longrightarrow> ws \<noteq> [] \<longrightarrow> (ws, zs) \<in> T a"
|
|
192 |
apply (erule R.induct)
|
|
193 |
apply rules
|
|
194 |
apply (rule impI)
|
|
195 |
apply (case_tac vs)
|
|
196 |
apply (erule R.elims)
|
|
197 |
apply simp
|
|
198 |
apply (case_tac a)
|
|
199 |
apply (rule_tac b=B in T0)
|
|
200 |
apply simp
|
|
201 |
apply (rule R0)
|
|
202 |
apply (rule_tac b=A in T0)
|
|
203 |
apply simp
|
|
204 |
apply (rule R0)
|
|
205 |
apply simp
|
|
206 |
apply (rule T1)
|
|
207 |
apply (erule mp)
|
|
208 |
apply simp
|
|
209 |
done
|
|
210 |
|
|
211 |
theorem R_nil: "([], zs) \<in> R a \<Longrightarrow> zs = []"
|
|
212 |
by (erule R.elims, simp+)
|
|
213 |
|
|
214 |
theorem letter_eq_dec: "(a::letter) = b \<or> a \<noteq> b"
|
|
215 |
apply (case_tac a)
|
|
216 |
apply (case_tac b)
|
|
217 |
apply simp
|
|
218 |
apply simp
|
|
219 |
apply (case_tac b)
|
|
220 |
apply simp
|
|
221 |
apply simp
|
|
222 |
done
|
|
223 |
|
|
224 |
theorem prop3: "xs \<in> bar \<Longrightarrow> \<forall>zs. (xs, zs) \<in> R a \<longrightarrow> zs \<in> bar"
|
|
225 |
apply (erule bar.induct)
|
|
226 |
apply (rule allI impI)+
|
|
227 |
apply (rule bar1)
|
|
228 |
apply (rule lemma2)
|
|
229 |
apply assumption+
|
|
230 |
apply (rule allI impI)+
|
|
231 |
apply (case_tac ws)
|
|
232 |
apply simp
|
|
233 |
apply (drule R_nil)
|
|
234 |
apply simp_all
|
|
235 |
apply rules
|
|
236 |
apply (rule bar2)
|
|
237 |
apply (rule allI)
|
|
238 |
apply (induct_tac w)
|
|
239 |
apply (rule prop1)
|
|
240 |
apply (rule_tac a1=aaa and b1=a in disjE [OF letter_eq_dec])
|
|
241 |
apply rules
|
|
242 |
apply (rule_tac xs="aa # list" and ys="lista # zs" and zs="(aaa # lista) # zs"
|
|
243 |
and b=aaa in prop2 [rule_format])
|
|
244 |
apply (rule bar2)
|
|
245 |
apply rules
|
|
246 |
apply assumption
|
|
247 |
apply (erule not_sym)
|
|
248 |
apply (rule T2)
|
|
249 |
apply (erule not_sym)
|
|
250 |
apply (erule lemma4)
|
|
251 |
apply simp
|
|
252 |
apply (rule T0)
|
|
253 |
apply assumption+
|
|
254 |
done
|
|
255 |
|
|
256 |
theorem prop5: "[w] \<in> bar"
|
|
257 |
apply (induct_tac w)
|
|
258 |
apply (rule prop1)
|
|
259 |
apply (rule prop3 [rule_format])
|
|
260 |
apply rules+
|
|
261 |
done
|
|
262 |
|
|
263 |
theorem higman: "[] \<in> bar"
|
|
264 |
apply (rule bar2)
|
|
265 |
apply (rule allI)
|
|
266 |
apply (rule prop5)
|
|
267 |
done
|
|
268 |
|
|
269 |
consts
|
|
270 |
is_prefix :: "'a list \<Rightarrow> (nat \<Rightarrow> 'a) \<Rightarrow> bool"
|
|
271 |
|
|
272 |
primrec
|
|
273 |
"is_prefix [] f = True"
|
|
274 |
"is_prefix (x # xs) f = (x = f (length xs) \<and> is_prefix xs f)"
|
|
275 |
|
|
276 |
theorem good_prefix_lemma:
|
|
277 |
"ws \<in> bar \<Longrightarrow> is_prefix ws f \<longrightarrow> (\<exists>vs. is_prefix vs f \<and> vs \<in> good)"
|
|
278 |
apply (erule bar.induct)
|
|
279 |
apply rules
|
|
280 |
apply (rule impI)
|
|
281 |
apply (erule_tac x="f (length ws)" in allE)
|
|
282 |
apply (erule conjE)
|
|
283 |
apply (erule impE)
|
|
284 |
apply simp
|
|
285 |
apply assumption
|
|
286 |
done
|
|
287 |
|
|
288 |
theorem good_prefix: "\<exists>vs. is_prefix vs f \<and> vs \<in> good"
|
|
289 |
apply (insert higman)
|
|
290 |
apply (drule good_prefix_lemma)
|
|
291 |
apply simp
|
|
292 |
done
|
|
293 |
|
|
294 |
|
|
295 |
subsection {* Realizers *}
|
|
296 |
|
|
297 |
subsubsection {* Bar induction *}
|
|
298 |
|
|
299 |
datatype Bar =
|
|
300 |
Good "letter list list"
|
|
301 |
| Bar "letter list list" "letter list \<Rightarrow> Bar"
|
|
302 |
|
|
303 |
consts
|
|
304 |
bar_realizes :: "Bar \<Rightarrow> letter list list \<Rightarrow> bool"
|
|
305 |
|
|
306 |
primrec
|
|
307 |
"bar_realizes (Good ws') ws = (ws = ws' \<and> ws' \<in> good)"
|
|
308 |
"bar_realizes (Bar ws' f) ws = (ws = ws' \<and> (\<forall>w. bar_realizes (f w) (w # ws')))"
|
|
309 |
|
|
310 |
theorem Good_realizer: "ws \<in> good \<Longrightarrow> bar_realizes (Good ws) ws"
|
|
311 |
by simp
|
|
312 |
|
|
313 |
theorem Bar_realizer:
|
|
314 |
"\<forall>w. bar_realizes (f w) (w # ws) \<Longrightarrow> bar_realizes (Bar ws f) ws"
|
|
315 |
by simp
|
|
316 |
|
|
317 |
consts
|
|
318 |
bar_ind :: "Bar \<Rightarrow> (letter list list \<Rightarrow> 'a) \<Rightarrow>
|
|
319 |
(letter list list \<Rightarrow> (letter list \<Rightarrow> Bar \<times> 'a) \<Rightarrow> 'a) \<Rightarrow> 'a"
|
|
320 |
|
|
321 |
primrec
|
|
322 |
"bar_ind (Good ws) f g = f ws"
|
|
323 |
"bar_ind (Bar ws f') f g = g ws (\<lambda>w. (f' w, bar_ind (f' w) f g))"
|
|
324 |
|
|
325 |
theorem Bar_ind_realizer:
|
|
326 |
assumes bar: "bar_realizes r x"
|
|
327 |
and f: "\<And>ws. ws \<in> good \<Longrightarrow> P (f ws) ws"
|
|
328 |
and g: "\<And>ws f. (\<forall>w. bar_realizes (fst (f w)) (w # ws) \<and> P (snd (f w)) (w # ws)) \<Longrightarrow>
|
|
329 |
P (g ws f) ws"
|
|
330 |
shows "P (bar_ind r f g) x"
|
|
331 |
proof -
|
|
332 |
have "\<And>x. bar_realizes r x \<Longrightarrow> P (bar_ind r f g) x"
|
|
333 |
apply (induct r)
|
|
334 |
apply simp
|
|
335 |
apply (rules intro: f)
|
|
336 |
apply simp
|
|
337 |
apply (rule g)
|
|
338 |
apply simp
|
|
339 |
done
|
|
340 |
thus ?thesis .
|
|
341 |
qed
|
|
342 |
|
|
343 |
extract_type
|
|
344 |
"typeof bar \<equiv> Type (TYPE(Bar))"
|
|
345 |
|
|
346 |
realizability
|
|
347 |
"realizes r (w : bar) \<equiv> bar_realizes r w"
|
|
348 |
|
|
349 |
realizers
|
|
350 |
bar1: "Good" "Good_realizer"
|
|
351 |
|
|
352 |
bar2: "Bar" "\<Lambda>ws f. Bar_realizer \<cdot> _ \<cdot> _"
|
|
353 |
|
|
354 |
bar.induct (P): "\<lambda>x P. bar_ind"
|
|
355 |
"\<Lambda>x P r (h1: _) f (h2: _) g.
|
|
356 |
Bar_ind_realizer \<cdot> _ \<cdot> _ \<cdot> (\<lambda>r x. realizes r (P x)) \<cdot> _ \<cdot> _ \<bullet> h1 \<bullet> h2"
|
|
357 |
|
|
358 |
subsubsection {* Lists *}
|
|
359 |
|
|
360 |
theorem list_ind_realizer:
|
|
361 |
assumes f: "P f []"
|
|
362 |
and g: "\<And>a as r. P r as \<Longrightarrow> P (g a as r) (a # as)"
|
|
363 |
shows "P (list_rec f g xs) xs"
|
|
364 |
apply (induct xs)
|
|
365 |
apply simp
|
|
366 |
apply (rule f)
|
|
367 |
apply simp
|
|
368 |
apply (rule g)
|
|
369 |
apply assumption
|
|
370 |
done
|
|
371 |
|
|
372 |
realizers
|
|
373 |
list.induct (P): "\<lambda>P xs f g. list_rec f g xs"
|
|
374 |
"\<Lambda>P xs f (h: _) g. list_ind_realizer \<cdot> _ \<cdot> _ \<cdot> _ \<cdot> _ \<bullet> h"
|
|
375 |
|
|
376 |
subsubsection {* Letters *}
|
|
377 |
|
|
378 |
theorem letter_exhaust_realizer:
|
|
379 |
"(y = A \<Longrightarrow> P r) \<Longrightarrow> (y = B \<Longrightarrow> P s) \<Longrightarrow> P (case y of A \<Rightarrow> r | B \<Rightarrow> s)"
|
|
380 |
by (case_tac y, simp+)
|
|
381 |
|
|
382 |
realizers
|
|
383 |
letter.exhaust (P): "\<lambda>y P r s. case y of A \<Rightarrow> r | B \<Rightarrow> s"
|
|
384 |
"\<Lambda>y P r (h: _) s. letter_exhaust_realizer \<cdot> _ \<cdot> (\<lambda>x. realizes x P) \<cdot> _ \<cdot> _ \<bullet> h"
|
|
385 |
|
|
386 |
|
|
387 |
subsection {* Extracting the program *}
|
|
388 |
|
|
389 |
extract good_prefix
|
|
390 |
|
|
391 |
text {*
|
|
392 |
Program extracted from the proof of @{text good_prefix}:
|
|
393 |
@{thm [display] good_prefix_def [no_vars]}
|
|
394 |
Corresponding correctness theorem:
|
|
395 |
@{thm [display] good_prefix_correctness [no_vars]}
|
|
396 |
Program extracted from the proof of @{text good_prefix_lemma}:
|
|
397 |
@{thm [display] good_prefix_lemma_def [no_vars]}
|
|
398 |
Program extracted from the proof of @{text higman}:
|
|
399 |
@{thm [display] higman_def [no_vars]}
|
|
400 |
Program extracted from the proof of @{text prop5}:
|
|
401 |
@{thm [display] prop5_def [no_vars]}
|
|
402 |
Program extracted from the proof of @{text prop1}:
|
|
403 |
@{thm [display] prop1_def [no_vars]}
|
|
404 |
Program extracted from the proof of @{text prop2}:
|
|
405 |
@{thm [display] prop2_def [no_vars]}
|
|
406 |
Program extracted from the proof of @{text prop3}:
|
|
407 |
@{thm [display] prop3_def [no_vars]}
|
|
408 |
*}
|
|
409 |
|
|
410 |
generate_code
|
|
411 |
test = good_prefix
|
|
412 |
|
|
413 |
ML {*
|
|
414 |
val a = 16807.0;
|
|
415 |
val m = 2147483647.0;
|
|
416 |
|
|
417 |
fun nextRand seed =
|
|
418 |
let val t = a*seed
|
|
419 |
in t - m * real (Real.floor(t/m)) end;
|
|
420 |
|
|
421 |
fun mk_word seed l =
|
|
422 |
let
|
|
423 |
val r = nextRand seed;
|
|
424 |
val i = Real.round (r / m * 10.0);
|
|
425 |
in if i > 7 andalso l > 2 then (r, []) else
|
|
426 |
apsnd (cons (if i mod 2 = 0 then A else B)) (mk_word r (l+1))
|
|
427 |
end;
|
|
428 |
|
|
429 |
fun f s id0 = mk_word s 0
|
|
430 |
| f s (Suc n) = f (fst (mk_word s 0)) n;
|
|
431 |
|
|
432 |
val g1 = snd o (f 20000.0);
|
|
433 |
|
|
434 |
val g2 = snd o (f 50000.0);
|
|
435 |
|
|
436 |
fun f1 id0 = [A,A]
|
|
437 |
| f1 (Suc id0) = [B]
|
|
438 |
| f1 (Suc (Suc id0)) = [A,B]
|
|
439 |
| f1 _ = [];
|
|
440 |
|
|
441 |
fun f2 id0 = [A,A]
|
|
442 |
| f2 (Suc id0) = [B]
|
|
443 |
| f2 (Suc (Suc id0)) = [B,A]
|
|
444 |
| f2 _ = [];
|
|
445 |
|
|
446 |
val xs1 = test g1;
|
|
447 |
val xs2 = test g2;
|
|
448 |
val xs3 = test f1;
|
|
449 |
val xs4 = test f2;
|
|
450 |
*}
|
|
451 |
|
|
452 |
end
|