| author | berghofe |
| Wed, 13 Nov 2002 15:34:01 +0100 | |
| changeset 13711 | 5ace1cccb612 |
| parent 13470 | d2cbbad84ad3 |
| child 13930 | 562fd03b266d |
| permissions | -rw-r--r-- |
| 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 |
||
|
13711
5ace1cccb612
Removed (now unneeded) declarations of realizers for bar induction.
berghofe
parents:
13470
diff
changeset
|
294 |
subsection {* Extracting the program *}
|
| 13405 | 295 |
|
|
13711
5ace1cccb612
Removed (now unneeded) declarations of realizers for bar induction.
berghofe
parents:
13470
diff
changeset
|
296 |
declare bar.induct [ind_realizer] |
| 13405 | 297 |
|
298 |
extract good_prefix |
|
299 |
||
300 |
text {*
|
|
301 |
Program extracted from the proof of @{text good_prefix}:
|
|
302 |
@{thm [display] good_prefix_def [no_vars]}
|
|
303 |
Corresponding correctness theorem: |
|
304 |
@{thm [display] good_prefix_correctness [no_vars]}
|
|
305 |
Program extracted from the proof of @{text good_prefix_lemma}:
|
|
306 |
@{thm [display] good_prefix_lemma_def [no_vars]}
|
|
307 |
Program extracted from the proof of @{text higman}:
|
|
308 |
@{thm [display] higman_def [no_vars]}
|
|
309 |
Program extracted from the proof of @{text prop5}:
|
|
310 |
@{thm [display] prop5_def [no_vars]}
|
|
311 |
Program extracted from the proof of @{text prop1}:
|
|
312 |
@{thm [display] prop1_def [no_vars]}
|
|
313 |
Program extracted from the proof of @{text prop2}:
|
|
314 |
@{thm [display] prop2_def [no_vars]}
|
|
315 |
Program extracted from the proof of @{text prop3}:
|
|
316 |
@{thm [display] prop3_def [no_vars]}
|
|
317 |
*} |
|
318 |
||
319 |
generate_code |
|
320 |
test = good_prefix |
|
321 |
||
322 |
ML {*
|
|
323 |
val a = 16807.0; |
|
324 |
val m = 2147483647.0; |
|
325 |
||
326 |
fun nextRand seed = |
|
327 |
let val t = a*seed |
|
328 |
in t - m * real (Real.floor(t/m)) end; |
|
329 |
||
330 |
fun mk_word seed l = |
|
331 |
let |
|
332 |
val r = nextRand seed; |
|
333 |
val i = Real.round (r / m * 10.0); |
|
334 |
in if i > 7 andalso l > 2 then (r, []) else |
|
335 |
apsnd (cons (if i mod 2 = 0 then A else B)) (mk_word r (l+1)) |
|
336 |
end; |
|
337 |
||
338 |
fun f s id0 = mk_word s 0 |
|
339 |
| f s (Suc n) = f (fst (mk_word s 0)) n; |
|
340 |
||
341 |
val g1 = snd o (f 20000.0); |
|
342 |
||
343 |
val g2 = snd o (f 50000.0); |
|
344 |
||
345 |
fun f1 id0 = [A,A] |
|
346 |
| f1 (Suc id0) = [B] |
|
347 |
| f1 (Suc (Suc id0)) = [A,B] |
|
348 |
| f1 _ = []; |
|
349 |
||
350 |
fun f2 id0 = [A,A] |
|
351 |
| f2 (Suc id0) = [B] |
|
352 |
| f2 (Suc (Suc id0)) = [B,A] |
|
353 |
| f2 _ = []; |
|
354 |
||
355 |
val xs1 = test g1; |
|
356 |
val xs2 = test g2; |
|
357 |
val xs3 = test f1; |
|
358 |
val xs4 = test f2; |
|
359 |
*} |
|
360 |
||
361 |
end |