author | wenzelm |
Thu, 21 Jun 2007 17:28:53 +0200 | |
changeset 23463 | 9953ff53cc64 |
parent 23373 | ead82c82da9e |
child 23747 | b07cff284683 |
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 |
||
16417 | 9 |
theory Higman imports Main begin |
13405 | 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 |
||
22266 | 18 |
inductive2 emb :: "letter list \<Rightarrow> letter list \<Rightarrow> bool" |
19 |
where |
|
20 |
emb0 [Pure.intro]: "emb [] bs" |
|
21 |
| emb1 [Pure.intro]: "emb as bs \<Longrightarrow> emb as (b # bs)" |
|
22 |
| emb2 [Pure.intro]: "emb as bs \<Longrightarrow> emb (a # as) (a # bs)" |
|
13405 | 23 |
|
22266 | 24 |
inductive2 L :: "letter list \<Rightarrow> letter list list \<Rightarrow> bool" |
25 |
for v :: "letter list" |
|
26 |
where |
|
27 |
L0 [Pure.intro]: "emb w v \<Longrightarrow> L v (w # ws)" |
|
28 |
| L1 [Pure.intro]: "L v ws \<Longrightarrow> L v (w # ws)" |
|
13405 | 29 |
|
22266 | 30 |
inductive2 good :: "letter list list \<Rightarrow> bool" |
31 |
where |
|
32 |
good0 [Pure.intro]: "L w ws \<Longrightarrow> good (w # ws)" |
|
33 |
| good1 [Pure.intro]: "good ws \<Longrightarrow> good (w # ws)" |
|
13405 | 34 |
|
22266 | 35 |
inductive2 R :: "letter \<Rightarrow> letter list list \<Rightarrow> letter list list \<Rightarrow> bool" |
36 |
for a :: letter |
|
37 |
where |
|
38 |
R0 [Pure.intro]: "R a [] []" |
|
39 |
| R1 [Pure.intro]: "R a vs ws \<Longrightarrow> R a (w # vs) ((a # w) # ws)" |
|
13405 | 40 |
|
22266 | 41 |
inductive2 T :: "letter \<Rightarrow> letter list list \<Rightarrow> letter list list \<Rightarrow> bool" |
42 |
for a :: letter |
|
43 |
where |
|
44 |
T0 [Pure.intro]: "a \<noteq> b \<Longrightarrow> R b ws zs \<Longrightarrow> T a (w # zs) ((a # w) # zs)" |
|
45 |
| T1 [Pure.intro]: "T a ws zs \<Longrightarrow> T a (w # ws) ((a # w) # zs)" |
|
46 |
| T2 [Pure.intro]: "a \<noteq> b \<Longrightarrow> T a ws zs \<Longrightarrow> T a ws ((b # w) # zs)" |
|
13405 | 47 |
|
22266 | 48 |
inductive2 bar :: "letter list list \<Rightarrow> bool" |
49 |
where |
|
50 |
bar1 [Pure.intro]: "good ws \<Longrightarrow> bar ws" |
|
51 |
| bar2 [Pure.intro]: "(\<And>w. bar (w # ws)) \<Longrightarrow> bar ws" |
|
13405 | 52 |
|
22266 | 53 |
theorem prop1: "bar ([] # ws)" by iprover |
13405 | 54 |
|
22266 | 55 |
theorem lemma1: "L as ws \<Longrightarrow> L (a # as) ws" |
17604 | 56 |
by (erule L.induct, iprover+) |
13405 | 57 |
|
22266 | 58 |
lemma lemma2': "R a vs ws \<Longrightarrow> L as vs \<Longrightarrow> L (a # as) ws" |
13969 | 59 |
apply (induct set: R) |
22266 | 60 |
apply (erule L.cases) |
13405 | 61 |
apply simp+ |
22266 | 62 |
apply (erule L.cases) |
13405 | 63 |
apply simp_all |
64 |
apply (rule L0) |
|
65 |
apply (erule emb2) |
|
66 |
apply (erule L1) |
|
67 |
done |
|
13969 | 68 |
|
22266 | 69 |
lemma lemma2: "R a vs ws \<Longrightarrow> good vs \<Longrightarrow> good ws" |
13969 | 70 |
apply (induct set: R) |
17604 | 71 |
apply iprover |
22266 | 72 |
apply (erule good.cases) |
13405 | 73 |
apply simp_all |
74 |
apply (rule good0) |
|
75 |
apply (erule lemma2') |
|
76 |
apply assumption |
|
77 |
apply (erule good1) |
|
78 |
done |
|
79 |
||
22266 | 80 |
lemma lemma3': "T a vs ws \<Longrightarrow> L as vs \<Longrightarrow> L (a # as) ws" |
13969 | 81 |
apply (induct set: T) |
22266 | 82 |
apply (erule L.cases) |
13405 | 83 |
apply simp_all |
84 |
apply (rule L0) |
|
85 |
apply (erule emb2) |
|
86 |
apply (rule L1) |
|
87 |
apply (erule lemma1) |
|
22266 | 88 |
apply (erule L.cases) |
13405 | 89 |
apply simp_all |
17604 | 90 |
apply iprover+ |
13405 | 91 |
done |
92 |
||
22266 | 93 |
lemma lemma3: "T a ws zs \<Longrightarrow> good ws \<Longrightarrow> good zs" |
13969 | 94 |
apply (induct set: T) |
22266 | 95 |
apply (erule good.cases) |
13405 | 96 |
apply simp_all |
97 |
apply (rule good0) |
|
98 |
apply (erule lemma1) |
|
99 |
apply (erule good1) |
|
22266 | 100 |
apply (erule good.cases) |
13405 | 101 |
apply simp_all |
102 |
apply (rule good0) |
|
103 |
apply (erule lemma3') |
|
17604 | 104 |
apply iprover+ |
13405 | 105 |
done |
106 |
||
22266 | 107 |
lemma lemma4: "R a ws zs \<Longrightarrow> ws \<noteq> [] \<Longrightarrow> T a ws zs" |
13969 | 108 |
apply (induct set: R) |
17604 | 109 |
apply iprover |
13405 | 110 |
apply (case_tac vs) |
22266 | 111 |
apply (erule R.cases) |
13405 | 112 |
apply simp |
113 |
apply (case_tac a) |
|
114 |
apply (rule_tac b=B in T0) |
|
115 |
apply simp |
|
116 |
apply (rule R0) |
|
117 |
apply (rule_tac b=A in T0) |
|
118 |
apply simp |
|
119 |
apply (rule R0) |
|
120 |
apply simp |
|
121 |
apply (rule T1) |
|
122 |
apply simp |
|
123 |
done |
|
124 |
||
13930 | 125 |
lemma letter_neq: "(a::letter) \<noteq> b \<Longrightarrow> c \<noteq> a \<Longrightarrow> c = b" |
126 |
apply (case_tac a) |
|
127 |
apply (case_tac b) |
|
128 |
apply (case_tac c, simp, simp) |
|
129 |
apply (case_tac c, simp, simp) |
|
130 |
apply (case_tac b) |
|
131 |
apply (case_tac c, simp, simp) |
|
132 |
apply (case_tac c, simp, simp) |
|
133 |
done |
|
13405 | 134 |
|
13930 | 135 |
lemma letter_eq_dec: "(a::letter) = b \<or> a \<noteq> b" |
13405 | 136 |
apply (case_tac a) |
137 |
apply (case_tac b) |
|
138 |
apply simp |
|
139 |
apply simp |
|
140 |
apply (case_tac b) |
|
141 |
apply simp |
|
142 |
apply simp |
|
143 |
done |
|
144 |
||
13930 | 145 |
theorem prop2: |
22266 | 146 |
assumes ab: "a \<noteq> b" and bar: "bar xs" |
147 |
shows "\<And>ys zs. bar ys \<Longrightarrow> T a xs zs \<Longrightarrow> T b ys zs \<Longrightarrow> bar zs" using bar |
|
13930 | 148 |
proof induct |
23373 | 149 |
fix xs zs assume "T a xs zs" and "good xs" |
150 |
hence "good zs" by (rule lemma3) |
|
151 |
then show "bar zs" by (rule bar1) |
|
13930 | 152 |
next |
153 |
fix xs ys |
|
22266 | 154 |
assume I: "\<And>w ys zs. bar ys \<Longrightarrow> T a (w # xs) zs \<Longrightarrow> T b ys zs \<Longrightarrow> bar zs" |
155 |
assume "bar ys" |
|
156 |
thus "\<And>zs. T a xs zs \<Longrightarrow> T b ys zs \<Longrightarrow> bar zs" |
|
13930 | 157 |
proof induct |
23373 | 158 |
fix ys zs assume "T b ys zs" and "good ys" |
159 |
then have "good zs" by (rule lemma3) |
|
160 |
then show "bar zs" by (rule bar1) |
|
13930 | 161 |
next |
22266 | 162 |
fix ys zs assume I': "\<And>w zs. T a xs zs \<Longrightarrow> T b (w # ys) zs \<Longrightarrow> bar zs" |
163 |
and ys: "\<And>w. bar (w # ys)" and Ta: "T a xs zs" and Tb: "T b ys zs" |
|
164 |
show "bar zs" |
|
13930 | 165 |
proof (rule bar2) |
166 |
fix w |
|
22266 | 167 |
show "bar (w # zs)" |
13930 | 168 |
proof (cases w) |
169 |
case Nil |
|
170 |
thus ?thesis by simp (rule prop1) |
|
171 |
next |
|
172 |
case (Cons c cs) |
|
173 |
from letter_eq_dec show ?thesis |
|
174 |
proof |
|
175 |
assume ca: "c = a" |
|
22266 | 176 |
from ab have "bar ((a # cs) # zs)" by (iprover intro: I ys Ta Tb) |
13930 | 177 |
thus ?thesis by (simp add: Cons ca) |
178 |
next |
|
179 |
assume "c \<noteq> a" |
|
180 |
with ab have cb: "c = b" by (rule letter_neq) |
|
22266 | 181 |
from ab have "bar ((b # cs) # zs)" by (iprover intro: I' Ta Tb) |
13930 | 182 |
thus ?thesis by (simp add: Cons cb) |
183 |
qed |
|
184 |
qed |
|
185 |
qed |
|
186 |
qed |
|
187 |
qed |
|
13405 | 188 |
|
13930 | 189 |
theorem prop3: |
22266 | 190 |
assumes bar: "bar xs" |
191 |
shows "\<And>zs. xs \<noteq> [] \<Longrightarrow> R a xs zs \<Longrightarrow> bar zs" using bar |
|
13930 | 192 |
proof induct |
193 |
fix xs zs |
|
23373 | 194 |
assume "R a xs zs" and "good xs" |
195 |
then have "good zs" by (rule lemma2) |
|
196 |
then show "bar zs" by (rule bar1) |
|
13930 | 197 |
next |
198 |
fix xs zs |
|
22266 | 199 |
assume I: "\<And>w zs. w # xs \<noteq> [] \<Longrightarrow> R a (w # xs) zs \<Longrightarrow> bar zs" |
200 |
and xsb: "\<And>w. bar (w # xs)" and xsn: "xs \<noteq> []" and R: "R a xs zs" |
|
201 |
show "bar zs" |
|
13930 | 202 |
proof (rule bar2) |
203 |
fix w |
|
22266 | 204 |
show "bar (w # zs)" |
13930 | 205 |
proof (induct w) |
206 |
case Nil |
|
207 |
show ?case by (rule prop1) |
|
208 |
next |
|
209 |
case (Cons c cs) |
|
210 |
from letter_eq_dec show ?case |
|
211 |
proof |
|
212 |
assume "c = a" |
|
17604 | 213 |
thus ?thesis by (iprover intro: I [simplified] R) |
13930 | 214 |
next |
22266 | 215 |
from R xsn have T: "T a xs zs" by (rule lemma4) |
13930 | 216 |
assume "c \<noteq> a" |
17604 | 217 |
thus ?thesis by (iprover intro: prop2 Cons xsb xsn R T) |
13930 | 218 |
qed |
219 |
qed |
|
220 |
qed |
|
221 |
qed |
|
13405 | 222 |
|
22266 | 223 |
theorem higman: "bar []" |
13930 | 224 |
proof (rule bar2) |
225 |
fix w |
|
22266 | 226 |
show "bar [w]" |
13930 | 227 |
proof (induct w) |
22266 | 228 |
show "bar [[]]" by (rule prop1) |
13930 | 229 |
next |
22266 | 230 |
fix c cs assume "bar [cs]" |
231 |
thus "bar [c # cs]" by (rule prop3) (simp, iprover) |
|
13930 | 232 |
qed |
233 |
qed |
|
13405 | 234 |
|
235 |
consts |
|
236 |
is_prefix :: "'a list \<Rightarrow> (nat \<Rightarrow> 'a) \<Rightarrow> bool" |
|
237 |
||
238 |
primrec |
|
239 |
"is_prefix [] f = True" |
|
240 |
"is_prefix (x # xs) f = (x = f (length xs) \<and> is_prefix xs f)" |
|
241 |
||
22266 | 242 |
theorem L_idx: |
243 |
assumes L: "L w ws" |
|
244 |
shows "is_prefix ws f \<Longrightarrow> \<exists>i. emb (f i) w \<and> i < length ws" using L |
|
245 |
proof induct |
|
246 |
case (L0 v ws) |
|
247 |
hence "emb (f (length ws)) w" by simp |
|
248 |
moreover have "length ws < length (v # ws)" by simp |
|
249 |
ultimately show ?case by iprover |
|
250 |
next |
|
251 |
case (L1 ws v) |
|
252 |
then obtain i where emb: "emb (f i) w" and "i < length ws" |
|
253 |
by simp iprover |
|
254 |
hence "i < length (v # ws)" by simp |
|
255 |
with emb show ?case by iprover |
|
256 |
qed |
|
257 |
||
258 |
theorem good_idx: |
|
259 |
assumes good: "good ws" |
|
260 |
shows "is_prefix ws f \<Longrightarrow> \<exists>i j. emb (f i) (f j) \<and> i < j" using good |
|
261 |
proof induct |
|
262 |
case (good0 w ws) |
|
263 |
hence "w = f (length ws)" and "is_prefix ws f" by simp_all |
|
264 |
with good0 show ?case by (iprover dest: L_idx) |
|
265 |
next |
|
266 |
case (good1 ws w) |
|
267 |
thus ?case by simp |
|
268 |
qed |
|
269 |
||
270 |
theorem bar_idx: |
|
271 |
assumes bar: "bar ws" |
|
272 |
shows "is_prefix ws f \<Longrightarrow> \<exists>i j. emb (f i) (f j) \<and> i < j" using bar |
|
273 |
proof induct |
|
274 |
case (bar1 ws) |
|
275 |
thus ?case by (rule good_idx) |
|
276 |
next |
|
277 |
case (bar2 ws) |
|
278 |
hence "is_prefix (f (length ws) # ws) f" by simp |
|
279 |
thus ?case by (rule bar2) |
|
280 |
qed |
|
281 |
||
282 |
text {* |
|
283 |
Strong version: yields indices of words that can be embedded into each other. |
|
284 |
*} |
|
285 |
||
286 |
theorem higman_idx: "\<exists>(i::nat) j. emb (f i) (f j) \<and> i < j" |
|
287 |
proof (rule bar_idx) |
|
288 |
show "bar []" by (rule higman) |
|
289 |
show "is_prefix [] f" by simp |
|
290 |
qed |
|
291 |
||
292 |
text {* |
|
293 |
Weak version: only yield sequence containing words |
|
294 |
that can be embedded into each other. |
|
295 |
*} |
|
296 |
||
13405 | 297 |
theorem good_prefix_lemma: |
22266 | 298 |
assumes bar: "bar ws" |
299 |
shows "is_prefix ws f \<Longrightarrow> \<exists>vs. is_prefix vs f \<and> good vs" using bar |
|
13930 | 300 |
proof induct |
301 |
case bar1 |
|
17604 | 302 |
thus ?case by iprover |
13930 | 303 |
next |
304 |
case (bar2 ws) |
|
23373 | 305 |
from bar2.prems have "is_prefix (f (length ws) # ws) f" by simp |
17604 | 306 |
thus ?case by (iprover intro: bar2) |
13930 | 307 |
qed |
13405 | 308 |
|
22266 | 309 |
theorem good_prefix: "\<exists>vs. is_prefix vs f \<and> good vs" |
13930 | 310 |
using higman |
311 |
by (rule good_prefix_lemma) simp+ |
|
13405 | 312 |
|
13711
5ace1cccb612
Removed (now unneeded) declarations of realizers for bar induction.
berghofe
parents:
13470
diff
changeset
|
313 |
subsection {* Extracting the program *} |
13405 | 314 |
|
22266 | 315 |
declare R.induct [ind_realizer] |
316 |
declare T.induct [ind_realizer] |
|
317 |
declare L.induct [ind_realizer] |
|
318 |
declare good.induct [ind_realizer] |
|
13711
5ace1cccb612
Removed (now unneeded) declarations of realizers for bar induction.
berghofe
parents:
13470
diff
changeset
|
319 |
declare bar.induct [ind_realizer] |
13405 | 320 |
|
22266 | 321 |
extract higman_idx |
13405 | 322 |
|
323 |
text {* |
|
22266 | 324 |
Program extracted from the proof of @{text higman_idx}: |
325 |
@{thm [display] higman_idx_def [no_vars]} |
|
13405 | 326 |
Corresponding correctness theorem: |
22266 | 327 |
@{thm [display] higman_idx_correctness [no_vars]} |
13405 | 328 |
Program extracted from the proof of @{text higman}: |
329 |
@{thm [display] higman_def [no_vars]} |
|
330 |
Program extracted from the proof of @{text prop1}: |
|
331 |
@{thm [display] prop1_def [no_vars]} |
|
332 |
Program extracted from the proof of @{text prop2}: |
|
333 |
@{thm [display] prop2_def [no_vars]} |
|
334 |
Program extracted from the proof of @{text prop3}: |
|
335 |
@{thm [display] prop3_def [no_vars]} |
|
336 |
*} |
|
337 |
||
22266 | 338 |
consts_code |
22921
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents:
22845
diff
changeset
|
339 |
"arbitrary :: LT" ("({* L0 [] [] *})") |
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents:
22845
diff
changeset
|
340 |
"arbitrary :: TT" ("({* T0 A [] [] [] R0 *})") |
22266 | 341 |
|
17145 | 342 |
code_module Higman |
343 |
contains |
|
22266 | 344 |
test = higman_idx |
13405 | 345 |
|
346 |
ML {* |
|
17145 | 347 |
local open Higman in |
348 |
||
13405 | 349 |
val a = 16807.0; |
350 |
val m = 2147483647.0; |
|
351 |
||
352 |
fun nextRand seed = |
|
353 |
let val t = a*seed |
|
354 |
in t - m * real (Real.floor(t/m)) end; |
|
355 |
||
356 |
fun mk_word seed l = |
|
357 |
let |
|
358 |
val r = nextRand seed; |
|
359 |
val i = Real.round (r / m * 10.0); |
|
360 |
in if i > 7 andalso l > 2 then (r, []) else |
|
361 |
apsnd (cons (if i mod 2 = 0 then A else B)) (mk_word r (l+1)) |
|
362 |
end; |
|
363 |
||
22266 | 364 |
fun f s zero = mk_word s 0 |
13405 | 365 |
| f s (Suc n) = f (fst (mk_word s 0)) n; |
366 |
||
367 |
val g1 = snd o (f 20000.0); |
|
368 |
||
369 |
val g2 = snd o (f 50000.0); |
|
370 |
||
22266 | 371 |
fun f1 zero = [A,A] |
372 |
| f1 (Suc zero) = [B] |
|
373 |
| f1 (Suc (Suc zero)) = [A,B] |
|
13405 | 374 |
| f1 _ = []; |
375 |
||
22266 | 376 |
fun f2 zero = [A,A] |
377 |
| f2 (Suc zero) = [B] |
|
378 |
| f2 (Suc (Suc zero)) = [B,A] |
|
13405 | 379 |
| f2 _ = []; |
380 |
||
22266 | 381 |
val (i1, j1) = test g1; |
382 |
val (v1, w1) = (g1 i1, g1 j1); |
|
383 |
val (i2, j2) = test g2; |
|
384 |
val (v2, w2) = (g2 i2, g2 j2); |
|
385 |
val (i3, j3) = test f1; |
|
386 |
val (v3, w3) = (f1 i3, f1 j3); |
|
387 |
val (i4, j4) = test f2; |
|
388 |
val (v4, w4) = (f2 i4, f2 j4); |
|
17145 | 389 |
|
390 |
end; |
|
13405 | 391 |
*} |
392 |
||
22266 | 393 |
definition |
394 |
arbitrary_LT :: "LT" where |
|
395 |
[symmetric, code inline]: "arbitrary_LT = arbitrary" |
|
396 |
||
397 |
definition |
|
398 |
arbitrary_TT :: "TT" where |
|
399 |
[symmetric, code inline]: "arbitrary_TT = arbitrary" |
|
400 |
||
401 |
||
402 |
definition |
|
403 |
"arbitrary_LT' = L0 [] []" |
|
404 |
||
405 |
definition |
|
406 |
"arbitrary_TT' = T0 A [] [] [] R0" |
|
407 |
||
408 |
code_axioms |
|
409 |
arbitrary_LT \<equiv> arbitrary_LT' |
|
410 |
arbitrary_TT \<equiv> arbitrary_TT' |
|
411 |
||
22845 | 412 |
code_gen higman_idx in SML |
20837 | 413 |
|
414 |
ML {* |
|
415 |
local |
|
416 |
open ROOT.Higman |
|
21196 | 417 |
open ROOT.Nat |
20837 | 418 |
in |
419 |
||
420 |
val a = 16807.0; |
|
421 |
val m = 2147483647.0; |
|
422 |
||
423 |
fun nextRand seed = |
|
424 |
let val t = a*seed |
|
425 |
in t - m * real (Real.floor(t/m)) end; |
|
426 |
||
427 |
fun mk_word seed l = |
|
428 |
let |
|
429 |
val r = nextRand seed; |
|
430 |
val i = Real.round (r / m * 10.0); |
|
431 |
in if i > 7 andalso l > 2 then (r, []) else |
|
432 |
apsnd (cons (if i mod 2 = 0 then A else B)) (mk_word r (l+1)) |
|
433 |
end; |
|
434 |
||
22266 | 435 |
fun f s Zero_nat = mk_word s 0 |
21196 | 436 |
| f s (Suc n) = f (fst (mk_word s 0)) n; |
20837 | 437 |
|
438 |
val g1 = snd o (f 20000.0); |
|
439 |
||
440 |
val g2 = snd o (f 50000.0); |
|
441 |
||
22266 | 442 |
fun f1 Zero_nat = [A,A] |
443 |
| f1 (Suc Zero_nat) = [B] |
|
444 |
| f1 (Suc (Suc Zero_nat)) = [A,B] |
|
20837 | 445 |
| f1 _ = []; |
446 |
||
22266 | 447 |
fun f2 Zero_nat = [A,A] |
448 |
| f2 (Suc Zero_nat) = [B] |
|
449 |
| f2 (Suc (Suc Zero_nat)) = [B,A] |
|
20837 | 450 |
| f2 _ = []; |
451 |
||
22266 | 452 |
val (i1, j1) = higman_idx g1; |
453 |
val (v1, w1) = (g1 i1, g1 j1); |
|
454 |
val (i2, j2) = higman_idx g2; |
|
455 |
val (v2, w2) = (g2 i2, g2 j2); |
|
456 |
val (i3, j3) = higman_idx f1; |
|
457 |
val (v3, w3) = (f1 i3, f1 j3); |
|
458 |
val (i4, j4) = higman_idx f2; |
|
459 |
val (v4, w4) = (f2 i4, f2 j4); |
|
20837 | 460 |
|
461 |
end; |
|
21152 | 462 |
*} |
20837 | 463 |
|
13405 | 464 |
end |