author | haftmann |
Mon, 02 Oct 2006 23:00:53 +0200 | |
changeset 20837 | 099877d83d2b |
parent 20637 | d883e0fc1c51 |
child 21114 | 3c09ec7565ed |
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 |
||
18 |
consts |
|
19 |
emb :: "(letter list \<times> letter list) set" |
|
20 |
||
21 |
inductive emb |
|
22 |
intros |
|
15801 | 23 |
emb0 [Pure.intro]: "([], bs) \<in> emb" |
24 |
emb1 [Pure.intro]: "(as, bs) \<in> emb \<Longrightarrow> (as, b # bs) \<in> emb" |
|
25 |
emb2 [Pure.intro]: "(as, bs) \<in> emb \<Longrightarrow> (a # as, a # bs) \<in> emb" |
|
13405 | 26 |
|
27 |
consts |
|
28 |
L :: "letter list \<Rightarrow> letter list list set" |
|
29 |
||
13969 | 30 |
inductive "L v" |
13405 | 31 |
intros |
15801 | 32 |
L0 [Pure.intro]: "(w, v) \<in> emb \<Longrightarrow> w # ws \<in> L v" |
33 |
L1 [Pure.intro]: "ws \<in> L v \<Longrightarrow> w # ws \<in> L v" |
|
13405 | 34 |
|
35 |
consts |
|
36 |
good :: "letter list list set" |
|
37 |
||
38 |
inductive good |
|
39 |
intros |
|
15801 | 40 |
good0 [Pure.intro]: "ws \<in> L w \<Longrightarrow> w # ws \<in> good" |
41 |
good1 [Pure.intro]: "ws \<in> good \<Longrightarrow> w # ws \<in> good" |
|
13405 | 42 |
|
43 |
consts |
|
44 |
R :: "letter \<Rightarrow> (letter list list \<times> letter list list) set" |
|
45 |
||
46 |
inductive "R a" |
|
47 |
intros |
|
15801 | 48 |
R0 [Pure.intro]: "([], []) \<in> R a" |
49 |
R1 [Pure.intro]: "(vs, ws) \<in> R a \<Longrightarrow> (w # vs, (a # w) # ws) \<in> R a" |
|
13405 | 50 |
|
51 |
consts |
|
52 |
T :: "letter \<Rightarrow> (letter list list \<times> letter list list) set" |
|
53 |
||
54 |
inductive "T a" |
|
55 |
intros |
|
15801 | 56 |
T0 [Pure.intro]: "a \<noteq> b \<Longrightarrow> (ws, zs) \<in> R b \<Longrightarrow> (w # zs, (a # w) # zs) \<in> T a" |
57 |
T1 [Pure.intro]: "(ws, zs) \<in> T a \<Longrightarrow> (w # ws, (a # w) # zs) \<in> T a" |
|
58 |
T2 [Pure.intro]: "a \<noteq> b \<Longrightarrow> (ws, zs) \<in> T a \<Longrightarrow> (ws, (b # w) # zs) \<in> T a" |
|
13405 | 59 |
|
60 |
consts |
|
61 |
bar :: "letter list list set" |
|
62 |
||
63 |
inductive bar |
|
64 |
intros |
|
15801 | 65 |
bar1 [Pure.intro]: "ws \<in> good \<Longrightarrow> ws \<in> bar" |
66 |
bar2 [Pure.intro]: "(\<And>w. w # ws \<in> bar) \<Longrightarrow> ws \<in> bar" |
|
13405 | 67 |
|
17604 | 68 |
theorem prop1: "([] # ws) \<in> bar" by iprover |
13405 | 69 |
|
70 |
theorem lemma1: "ws \<in> L as \<Longrightarrow> ws \<in> L (a # as)" |
|
17604 | 71 |
by (erule L.induct, iprover+) |
13405 | 72 |
|
13969 | 73 |
lemma lemma2': "(vs, ws) \<in> R a \<Longrightarrow> vs \<in> L as \<Longrightarrow> ws \<in> L (a # as)" |
74 |
apply (induct set: R) |
|
13405 | 75 |
apply (erule L.elims) |
76 |
apply simp+ |
|
77 |
apply (erule L.elims) |
|
78 |
apply simp_all |
|
79 |
apply (rule L0) |
|
80 |
apply (erule emb2) |
|
81 |
apply (erule L1) |
|
82 |
done |
|
13969 | 83 |
|
84 |
lemma lemma2: "(vs, ws) \<in> R a \<Longrightarrow> vs \<in> good \<Longrightarrow> ws \<in> good" |
|
85 |
apply (induct set: R) |
|
17604 | 86 |
apply iprover |
13405 | 87 |
apply (erule good.elims) |
88 |
apply simp_all |
|
89 |
apply (rule good0) |
|
90 |
apply (erule lemma2') |
|
91 |
apply assumption |
|
92 |
apply (erule good1) |
|
93 |
done |
|
94 |
||
13969 | 95 |
lemma lemma3': "(vs, ws) \<in> T a \<Longrightarrow> vs \<in> L as \<Longrightarrow> ws \<in> L (a # as)" |
96 |
apply (induct set: T) |
|
13405 | 97 |
apply (erule L.elims) |
98 |
apply simp_all |
|
99 |
apply (rule L0) |
|
100 |
apply (erule emb2) |
|
101 |
apply (rule L1) |
|
102 |
apply (erule lemma1) |
|
103 |
apply (erule L.elims) |
|
104 |
apply simp_all |
|
17604 | 105 |
apply iprover+ |
13405 | 106 |
done |
107 |
||
13969 | 108 |
lemma lemma3: "(ws, zs) \<in> T a \<Longrightarrow> ws \<in> good \<Longrightarrow> zs \<in> good" |
109 |
apply (induct set: T) |
|
13405 | 110 |
apply (erule good.elims) |
111 |
apply simp_all |
|
112 |
apply (rule good0) |
|
113 |
apply (erule lemma1) |
|
114 |
apply (erule good1) |
|
115 |
apply (erule good.elims) |
|
116 |
apply simp_all |
|
117 |
apply (rule good0) |
|
118 |
apply (erule lemma3') |
|
17604 | 119 |
apply iprover+ |
13405 | 120 |
done |
121 |
||
13969 | 122 |
lemma lemma4: "(ws, zs) \<in> R a \<Longrightarrow> ws \<noteq> [] \<Longrightarrow> (ws, zs) \<in> T a" |
123 |
apply (induct set: R) |
|
17604 | 124 |
apply iprover |
13405 | 125 |
apply (case_tac vs) |
126 |
apply (erule R.elims) |
|
127 |
apply simp |
|
128 |
apply (case_tac a) |
|
129 |
apply (rule_tac b=B in T0) |
|
130 |
apply simp |
|
131 |
apply (rule R0) |
|
132 |
apply (rule_tac b=A in T0) |
|
133 |
apply simp |
|
134 |
apply (rule R0) |
|
135 |
apply simp |
|
136 |
apply (rule T1) |
|
137 |
apply simp |
|
138 |
done |
|
139 |
||
13930 | 140 |
lemma letter_neq: "(a::letter) \<noteq> b \<Longrightarrow> c \<noteq> a \<Longrightarrow> c = b" |
141 |
apply (case_tac a) |
|
142 |
apply (case_tac b) |
|
143 |
apply (case_tac c, simp, simp) |
|
144 |
apply (case_tac c, simp, simp) |
|
145 |
apply (case_tac b) |
|
146 |
apply (case_tac c, simp, simp) |
|
147 |
apply (case_tac c, simp, simp) |
|
148 |
done |
|
13405 | 149 |
|
13930 | 150 |
lemma letter_eq_dec: "(a::letter) = b \<or> a \<noteq> b" |
13405 | 151 |
apply (case_tac a) |
152 |
apply (case_tac b) |
|
153 |
apply simp |
|
154 |
apply simp |
|
155 |
apply (case_tac b) |
|
156 |
apply simp |
|
157 |
apply simp |
|
158 |
done |
|
159 |
||
13930 | 160 |
theorem prop2: |
161 |
assumes ab: "a \<noteq> b" and bar: "xs \<in> bar" |
|
162 |
shows "\<And>ys zs. ys \<in> bar \<Longrightarrow> (xs, zs) \<in> T a \<Longrightarrow> (ys, zs) \<in> T b \<Longrightarrow> zs \<in> bar" using bar |
|
163 |
proof induct |
|
164 |
fix xs zs assume "xs \<in> good" and "(xs, zs) \<in> T a" |
|
165 |
show "zs \<in> bar" by (rule bar1) (rule lemma3) |
|
166 |
next |
|
167 |
fix xs ys |
|
168 |
assume I: "\<And>w ys zs. ys \<in> bar \<Longrightarrow> (w # xs, zs) \<in> T a \<Longrightarrow> (ys, zs) \<in> T b \<Longrightarrow> zs \<in> bar" |
|
169 |
assume "ys \<in> bar" |
|
170 |
thus "\<And>zs. (xs, zs) \<in> T a \<Longrightarrow> (ys, zs) \<in> T b \<Longrightarrow> zs \<in> bar" |
|
171 |
proof induct |
|
172 |
fix ys zs assume "ys \<in> good" and "(ys, zs) \<in> T b" |
|
173 |
show "zs \<in> bar" by (rule bar1) (rule lemma3) |
|
174 |
next |
|
175 |
fix ys zs assume I': "\<And>w zs. (xs, zs) \<in> T a \<Longrightarrow> (w # ys, zs) \<in> T b \<Longrightarrow> zs \<in> bar" |
|
176 |
and ys: "\<And>w. w # ys \<in> bar" and Ta: "(xs, zs) \<in> T a" and Tb: "(ys, zs) \<in> T b" |
|
177 |
show "zs \<in> bar" |
|
178 |
proof (rule bar2) |
|
179 |
fix w |
|
180 |
show "w # zs \<in> bar" |
|
181 |
proof (cases w) |
|
182 |
case Nil |
|
183 |
thus ?thesis by simp (rule prop1) |
|
184 |
next |
|
185 |
case (Cons c cs) |
|
186 |
from letter_eq_dec show ?thesis |
|
187 |
proof |
|
188 |
assume ca: "c = a" |
|
17604 | 189 |
from ab have "(a # cs) # zs \<in> bar" by (iprover intro: I ys Ta Tb) |
13930 | 190 |
thus ?thesis by (simp add: Cons ca) |
191 |
next |
|
192 |
assume "c \<noteq> a" |
|
193 |
with ab have cb: "c = b" by (rule letter_neq) |
|
17604 | 194 |
from ab have "(b # cs) # zs \<in> bar" by (iprover intro: I' Ta Tb) |
13930 | 195 |
thus ?thesis by (simp add: Cons cb) |
196 |
qed |
|
197 |
qed |
|
198 |
qed |
|
199 |
qed |
|
200 |
qed |
|
13405 | 201 |
|
13930 | 202 |
theorem prop3: |
203 |
assumes bar: "xs \<in> bar" |
|
204 |
shows "\<And>zs. xs \<noteq> [] \<Longrightarrow> (xs, zs) \<in> R a \<Longrightarrow> zs \<in> bar" using bar |
|
205 |
proof induct |
|
206 |
fix xs zs |
|
207 |
assume "xs \<in> good" and "(xs, zs) \<in> R a" |
|
208 |
show "zs \<in> bar" by (rule bar1) (rule lemma2) |
|
209 |
next |
|
210 |
fix xs zs |
|
211 |
assume I: "\<And>w zs. w # xs \<noteq> [] \<Longrightarrow> (w # xs, zs) \<in> R a \<Longrightarrow> zs \<in> bar" |
|
212 |
and xsb: "\<And>w. w # xs \<in> bar" and xsn: "xs \<noteq> []" and R: "(xs, zs) \<in> R a" |
|
213 |
show "zs \<in> bar" |
|
214 |
proof (rule bar2) |
|
215 |
fix w |
|
216 |
show "w # zs \<in> bar" |
|
217 |
proof (induct w) |
|
218 |
case Nil |
|
219 |
show ?case by (rule prop1) |
|
220 |
next |
|
221 |
case (Cons c cs) |
|
222 |
from letter_eq_dec show ?case |
|
223 |
proof |
|
224 |
assume "c = a" |
|
17604 | 225 |
thus ?thesis by (iprover intro: I [simplified] R) |
13930 | 226 |
next |
227 |
from R xsn have T: "(xs, zs) \<in> T a" by (rule lemma4) |
|
228 |
assume "c \<noteq> a" |
|
17604 | 229 |
thus ?thesis by (iprover intro: prop2 Cons xsb xsn R T) |
13930 | 230 |
qed |
231 |
qed |
|
232 |
qed |
|
233 |
qed |
|
13405 | 234 |
|
235 |
theorem higman: "[] \<in> bar" |
|
13930 | 236 |
proof (rule bar2) |
237 |
fix w |
|
238 |
show "[w] \<in> bar" |
|
239 |
proof (induct w) |
|
240 |
show "[[]] \<in> bar" by (rule prop1) |
|
241 |
next |
|
242 |
fix c cs assume "[cs] \<in> bar" |
|
17604 | 243 |
thus "[c # cs] \<in> bar" by (rule prop3) (simp, iprover) |
13930 | 244 |
qed |
245 |
qed |
|
13405 | 246 |
|
247 |
consts |
|
248 |
is_prefix :: "'a list \<Rightarrow> (nat \<Rightarrow> 'a) \<Rightarrow> bool" |
|
249 |
||
250 |
primrec |
|
251 |
"is_prefix [] f = True" |
|
252 |
"is_prefix (x # xs) f = (x = f (length xs) \<and> is_prefix xs f)" |
|
253 |
||
254 |
theorem good_prefix_lemma: |
|
13930 | 255 |
assumes bar: "ws \<in> bar" |
256 |
shows "is_prefix ws f \<Longrightarrow> \<exists>vs. is_prefix vs f \<and> vs \<in> good" using bar |
|
257 |
proof induct |
|
258 |
case bar1 |
|
17604 | 259 |
thus ?case by iprover |
13930 | 260 |
next |
261 |
case (bar2 ws) |
|
262 |
have "is_prefix (f (length ws) # ws) f" by simp |
|
17604 | 263 |
thus ?case by (iprover intro: bar2) |
13930 | 264 |
qed |
13405 | 265 |
|
266 |
theorem good_prefix: "\<exists>vs. is_prefix vs f \<and> vs \<in> good" |
|
13930 | 267 |
using higman |
268 |
by (rule good_prefix_lemma) simp+ |
|
13405 | 269 |
|
13711
5ace1cccb612
Removed (now unneeded) declarations of realizers for bar induction.
berghofe
parents:
13470
diff
changeset
|
270 |
subsection {* Extracting the program *} |
13405 | 271 |
|
13711
5ace1cccb612
Removed (now unneeded) declarations of realizers for bar induction.
berghofe
parents:
13470
diff
changeset
|
272 |
declare bar.induct [ind_realizer] |
13405 | 273 |
|
274 |
extract good_prefix |
|
275 |
||
276 |
text {* |
|
277 |
Program extracted from the proof of @{text good_prefix}: |
|
278 |
@{thm [display] good_prefix_def [no_vars]} |
|
279 |
Corresponding correctness theorem: |
|
280 |
@{thm [display] good_prefix_correctness [no_vars]} |
|
281 |
Program extracted from the proof of @{text good_prefix_lemma}: |
|
282 |
@{thm [display] good_prefix_lemma_def [no_vars]} |
|
283 |
Program extracted from the proof of @{text higman}: |
|
284 |
@{thm [display] higman_def [no_vars]} |
|
285 |
Program extracted from the proof of @{text prop1}: |
|
286 |
@{thm [display] prop1_def [no_vars]} |
|
287 |
Program extracted from the proof of @{text prop2}: |
|
288 |
@{thm [display] prop2_def [no_vars]} |
|
289 |
Program extracted from the proof of @{text prop3}: |
|
290 |
@{thm [display] prop3_def [no_vars]} |
|
291 |
*} |
|
292 |
||
17145 | 293 |
code_module Higman |
294 |
contains |
|
13405 | 295 |
test = good_prefix |
296 |
||
297 |
ML {* |
|
17145 | 298 |
local open Higman in |
299 |
||
13405 | 300 |
val a = 16807.0; |
301 |
val m = 2147483647.0; |
|
302 |
||
303 |
fun nextRand seed = |
|
304 |
let val t = a*seed |
|
305 |
in t - m * real (Real.floor(t/m)) end; |
|
306 |
||
307 |
fun mk_word seed l = |
|
308 |
let |
|
309 |
val r = nextRand seed; |
|
310 |
val i = Real.round (r / m * 10.0); |
|
311 |
in if i > 7 andalso l > 2 then (r, []) else |
|
312 |
apsnd (cons (if i mod 2 = 0 then A else B)) (mk_word r (l+1)) |
|
313 |
end; |
|
314 |
||
17145 | 315 |
fun f s id_0 = mk_word s 0 |
13405 | 316 |
| f s (Suc n) = f (fst (mk_word s 0)) n; |
317 |
||
318 |
val g1 = snd o (f 20000.0); |
|
319 |
||
320 |
val g2 = snd o (f 50000.0); |
|
321 |
||
17145 | 322 |
fun f1 id_0 = [A,A] |
323 |
| f1 (Suc id_0) = [B] |
|
324 |
| f1 (Suc (Suc id_0)) = [A,B] |
|
13405 | 325 |
| f1 _ = []; |
326 |
||
17145 | 327 |
fun f2 id_0 = [A,A] |
328 |
| f2 (Suc id_0) = [B] |
|
329 |
| f2 (Suc (Suc id_0)) = [B,A] |
|
13405 | 330 |
| f2 _ = []; |
331 |
||
332 |
val xs1 = test g1; |
|
333 |
val xs2 = test g2; |
|
334 |
val xs3 = test f1; |
|
335 |
val xs4 = test f2; |
|
17145 | 336 |
|
337 |
end; |
|
13405 | 338 |
*} |
339 |
||
20837 | 340 |
code_gen good_prefix (SML -) |
341 |
||
342 |
ML {* |
|
343 |
local |
|
344 |
open ROOT.Higman |
|
345 |
open ROOT.IntDef |
|
346 |
in |
|
347 |
||
348 |
val a = 16807.0; |
|
349 |
val m = 2147483647.0; |
|
350 |
||
351 |
fun nextRand seed = |
|
352 |
let val t = a*seed |
|
353 |
in t - m * real (Real.floor(t/m)) end; |
|
354 |
||
355 |
fun mk_word seed l = |
|
356 |
let |
|
357 |
val r = nextRand seed; |
|
358 |
val i = Real.round (r / m * 10.0); |
|
359 |
in if i > 7 andalso l > 2 then (r, []) else |
|
360 |
apsnd (cons (if i mod 2 = 0 then A else B)) (mk_word r (l+1)) |
|
361 |
end; |
|
362 |
||
363 |
fun f s id_0 = mk_word s 0 |
|
364 |
| f s (Succ_nat n) = f (fst (mk_word s 0)) n; |
|
365 |
||
366 |
val g1 = snd o (f 20000.0); |
|
367 |
||
368 |
val g2 = snd o (f 50000.0); |
|
369 |
||
370 |
fun f1 id_0 = [A,A] |
|
371 |
| f1 (Succ_nat id_0) = [B] |
|
372 |
| f1 (Succ_nat (Succ_nat id_0)) = [A,B] |
|
373 |
| f1 _ = []; |
|
374 |
||
375 |
fun f2 id_0 = [A,A] |
|
376 |
| f2 (Succ_nat id_0) = [B] |
|
377 |
| f2 (Succ_nat (Succ_nat id_0)) = [B,A] |
|
378 |
| f2 _ = []; |
|
379 |
||
380 |
val xs1 = good_prefix g1; |
|
381 |
val xs2 = good_prefix g2; |
|
382 |
val xs3 = good_prefix f1; |
|
383 |
val xs4 = good_prefix f2; |
|
384 |
||
385 |
end; |
|
386 |
*} |
|
387 |
||
13405 | 388 |
end |