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