author | wenzelm |
Fri, 17 Nov 2006 02:20:03 +0100 | |
changeset 21404 | eb85850d3eb7 |
parent 19736 | d8d0f8f51d69 |
child 22283 | 26140713540b |
permissions | -rw-r--r-- |
11024 | 1 |
(* Title: HOL/ex/Primrec.thy |
3335 | 2 |
ID: $Id$ |
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
4 |
Copyright 1997 University of Cambridge |
|
5 |
||
11024 | 6 |
Primitive Recursive Functions. Demonstrates recursive definitions, |
7 |
the TFL package. |
|
3335 | 8 |
*) |
9 |
||
11024 | 10 |
header {* Primitive Recursive Functions *} |
11 |
||
16417 | 12 |
theory Primrec imports Main begin |
11024 | 13 |
|
14 |
text {* |
|
15 |
Proof adopted from |
|
16 |
||
17 |
Nora Szasz, A Machine Checked Proof that Ackermann's Function is not |
|
18 |
Primitive Recursive, In: Huet \& Plotkin, eds., Logical Environments |
|
19 |
(CUP, 1993), 317-338. |
|
20 |
||
21 |
See also E. Mendelson, Introduction to Mathematical Logic. (Van |
|
22 |
Nostrand, 1964), page 250, exercise 11. |
|
23 |
\medskip |
|
24 |
*} |
|
25 |
||
26 |
consts ack :: "nat * nat => nat" |
|
27 |
recdef ack "less_than <*lex*> less_than" |
|
28 |
"ack (0, n) = Suc n" |
|
29 |
"ack (Suc m, 0) = ack (m, 1)" |
|
30 |
"ack (Suc m, Suc n) = ack (m, ack (Suc m, n))" |
|
31 |
||
32 |
consts list_add :: "nat list => nat" |
|
33 |
primrec |
|
34 |
"list_add [] = 0" |
|
35 |
"list_add (m # ms) = m + list_add ms" |
|
36 |
||
37 |
consts zeroHd :: "nat list => nat" |
|
38 |
primrec |
|
39 |
"zeroHd [] = 0" |
|
40 |
"zeroHd (m # ms) = m" |
|
41 |
||
42 |
||
43 |
text {* The set of primitive recursive functions of type @{typ "nat list => nat"}. *} |
|
44 |
||
19736 | 45 |
definition |
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
19736
diff
changeset
|
46 |
SC :: "nat list => nat" where |
19736 | 47 |
"SC l = Suc (zeroHd l)" |
3335 | 48 |
|
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
19736
diff
changeset
|
49 |
definition |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
19736
diff
changeset
|
50 |
CONSTANT :: "nat => nat list => nat" where |
19736 | 51 |
"CONSTANT k l = k" |
11024 | 52 |
|
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
19736
diff
changeset
|
53 |
definition |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
19736
diff
changeset
|
54 |
PROJ :: "nat => nat list => nat" where |
19736 | 55 |
"PROJ i l = zeroHd (drop i l)" |
11024 | 56 |
|
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
19736
diff
changeset
|
57 |
definition |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
19736
diff
changeset
|
58 |
COMP :: "(nat list => nat) => (nat list => nat) list => nat list => nat" where |
19736 | 59 |
"COMP g fs l = g (map (\<lambda>f. f l) fs)" |
11024 | 60 |
|
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
19736
diff
changeset
|
61 |
definition |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
19736
diff
changeset
|
62 |
PREC :: "(nat list => nat) => (nat list => nat) => nat list => nat" where |
19736 | 63 |
"PREC f g l = |
64 |
(case l of |
|
11024 | 65 |
[] => 0 |
19736 | 66 |
| x # l' => nat_rec (f l') (\<lambda>y r. g (r # y # l')) x)" |
11024 | 67 |
-- {* Note that @{term g} is applied first to @{term "PREC f g y"} and then to @{term y}! *} |
68 |
||
69 |
consts PRIMREC :: "(nat list => nat) set" |
|
70 |
inductive PRIMREC |
|
71 |
intros |
|
72 |
SC: "SC \<in> PRIMREC" |
|
19676 | 73 |
CONSTANT: "CONSTANT k \<in> PRIMREC" |
11024 | 74 |
PROJ: "PROJ i \<in> PRIMREC" |
75 |
COMP: "g \<in> PRIMREC ==> fs \<in> lists PRIMREC ==> COMP g fs \<in> PRIMREC" |
|
76 |
PREC: "f \<in> PRIMREC ==> g \<in> PRIMREC ==> PREC f g \<in> PRIMREC" |
|
77 |
||
78 |
||
79 |
text {* Useful special cases of evaluation *} |
|
80 |
||
81 |
lemma SC [simp]: "SC (x # l) = Suc x" |
|
82 |
apply (simp add: SC_def) |
|
83 |
done |
|
84 |
||
19676 | 85 |
lemma CONSTANT [simp]: "CONSTANT k l = k" |
86 |
apply (simp add: CONSTANT_def) |
|
11024 | 87 |
done |
88 |
||
89 |
lemma PROJ_0 [simp]: "PROJ 0 (x # l) = x" |
|
90 |
apply (simp add: PROJ_def) |
|
91 |
done |
|
92 |
||
93 |
lemma COMP_1 [simp]: "COMP g [f] l = g [f l]" |
|
94 |
apply (simp add: COMP_def) |
|
95 |
done |
|
3335 | 96 |
|
11024 | 97 |
lemma PREC_0 [simp]: "PREC f g (0 # l) = f l" |
98 |
apply (simp add: PREC_def) |
|
99 |
done |
|
100 |
||
101 |
lemma PREC_Suc [simp]: "PREC f g (Suc x # l) = g (PREC f g (x # l) # x # l)" |
|
102 |
apply (simp add: PREC_def) |
|
103 |
done |
|
104 |
||
105 |
||
106 |
text {* PROPERTY A 4 *} |
|
107 |
||
108 |
lemma less_ack2 [iff]: "j < ack (i, j)" |
|
109 |
apply (induct i j rule: ack.induct) |
|
110 |
apply simp_all |
|
111 |
done |
|
112 |
||
113 |
||
114 |
text {* PROPERTY A 5-, the single-step lemma *} |
|
115 |
||
116 |
lemma ack_less_ack_Suc2 [iff]: "ack(i, j) < ack (i, Suc j)" |
|
117 |
apply (induct i j rule: ack.induct) |
|
118 |
apply simp_all |
|
119 |
done |
|
120 |
||
121 |
||
122 |
text {* PROPERTY A 5, monotonicity for @{text "<"} *} |
|
123 |
||
124 |
lemma ack_less_mono2: "j < k ==> ack (i, j) < ack (i, k)" |
|
125 |
apply (induct i k rule: ack.induct) |
|
126 |
apply simp_all |
|
127 |
apply (blast elim!: less_SucE intro: less_trans) |
|
128 |
done |
|
129 |
||
130 |
||
131 |
text {* PROPERTY A 5', monotonicity for @{text \<le>} *} |
|
132 |
||
133 |
lemma ack_le_mono2: "j \<le> k ==> ack (i, j) \<le> ack (i, k)" |
|
134 |
apply (simp add: order_le_less) |
|
135 |
apply (blast intro: ack_less_mono2) |
|
136 |
done |
|
3335 | 137 |
|
11024 | 138 |
|
139 |
text {* PROPERTY A 6 *} |
|
140 |
||
141 |
lemma ack2_le_ack1 [iff]: "ack (i, Suc j) \<le> ack (Suc i, j)" |
|
142 |
apply (induct j) |
|
143 |
apply simp_all |
|
144 |
apply (blast intro: ack_le_mono2 less_ack2 [THEN Suc_leI] le_trans) |
|
145 |
done |
|
146 |
||
147 |
||
148 |
text {* PROPERTY A 7-, the single-step lemma *} |
|
149 |
||
150 |
lemma ack_less_ack_Suc1 [iff]: "ack (i, j) < ack (Suc i, j)" |
|
151 |
apply (blast intro: ack_less_mono2 less_le_trans) |
|
152 |
done |
|
153 |
||
154 |
||
19676 | 155 |
text {* PROPERTY A 4'? Extra lemma needed for @{term CONSTANT} case, constant functions *} |
11024 | 156 |
|
157 |
lemma less_ack1 [iff]: "i < ack (i, j)" |
|
158 |
apply (induct i) |
|
159 |
apply simp_all |
|
160 |
apply (blast intro: Suc_leI le_less_trans) |
|
161 |
done |
|
162 |
||
163 |
||
164 |
text {* PROPERTY A 8 *} |
|
165 |
||
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
166 |
lemma ack_1 [simp]: "ack (Suc 0, j) = j + 2" |
11024 | 167 |
apply (induct j) |
168 |
apply simp_all |
|
169 |
done |
|
170 |
||
171 |
||
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11464
diff
changeset
|
172 |
text {* PROPERTY A 9. The unary @{text 1} and @{text 2} in @{term |
11024 | 173 |
ack} is essential for the rewriting. *} |
174 |
||
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
175 |
lemma ack_2 [simp]: "ack (Suc (Suc 0), j) = 2 * j + 3" |
11024 | 176 |
apply (induct j) |
177 |
apply simp_all |
|
178 |
done |
|
3335 | 179 |
|
180 |
||
11024 | 181 |
text {* PROPERTY A 7, monotonicity for @{text "<"} [not clear why |
182 |
@{thm [source] ack_1} is now needed first!] *} |
|
183 |
||
184 |
lemma ack_less_mono1_aux: "ack (i, k) < ack (Suc (i +i'), k)" |
|
185 |
apply (induct i k rule: ack.induct) |
|
186 |
apply simp_all |
|
187 |
prefer 2 |
|
188 |
apply (blast intro: less_trans ack_less_mono2) |
|
189 |
apply (induct_tac i' n rule: ack.induct) |
|
190 |
apply simp_all |
|
191 |
apply (blast intro: Suc_leI [THEN le_less_trans] ack_less_mono2) |
|
192 |
done |
|
193 |
||
194 |
lemma ack_less_mono1: "i < j ==> ack (i, k) < ack (j, k)" |
|
195 |
apply (drule less_imp_Suc_add) |
|
196 |
apply (blast intro!: ack_less_mono1_aux) |
|
197 |
done |
|
198 |
||
199 |
||
200 |
text {* PROPERTY A 7', monotonicity for @{text "\<le>"} *} |
|
201 |
||
202 |
lemma ack_le_mono1: "i \<le> j ==> ack (i, k) \<le> ack (j, k)" |
|
203 |
apply (simp add: order_le_less) |
|
204 |
apply (blast intro: ack_less_mono1) |
|
205 |
done |
|
206 |
||
207 |
||
208 |
text {* PROPERTY A 10 *} |
|
209 |
||
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
210 |
lemma ack_nest_bound: "ack(i1, ack (i2, j)) < ack (2 + (i1 + i2), j)" |
11024 | 211 |
apply (simp add: numerals) |
212 |
apply (rule ack2_le_ack1 [THEN [2] less_le_trans]) |
|
213 |
apply simp |
|
214 |
apply (rule le_add1 [THEN ack_le_mono1, THEN le_less_trans]) |
|
215 |
apply (rule ack_less_mono1 [THEN ack_less_mono2]) |
|
216 |
apply (simp add: le_imp_less_Suc le_add2) |
|
217 |
done |
|
218 |
||
3335 | 219 |
|
11024 | 220 |
text {* PROPERTY A 11 *} |
3335 | 221 |
|
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
222 |
lemma ack_add_bound: "ack (i1, j) + ack (i2, j) < ack (4 + (i1 + i2), j)" |
11701
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
wenzelm
parents:
11464
diff
changeset
|
223 |
apply (rule_tac j = "ack (Suc (Suc 0), ack (i1 + i2, j))" in less_trans) |
11024 | 224 |
prefer 2 |
225 |
apply (rule ack_nest_bound [THEN less_le_trans]) |
|
226 |
apply (simp add: Suc3_eq_add_3) |
|
227 |
apply simp |
|
228 |
apply (cut_tac i = i1 and m1 = i2 and k = j in le_add1 [THEN ack_le_mono1]) |
|
229 |
apply (cut_tac i = "i2" and m1 = i1 and k = j in le_add2 [THEN ack_le_mono1]) |
|
230 |
apply auto |
|
231 |
done |
|
232 |
||
233 |
||
234 |
text {* PROPERTY A 12. Article uses existential quantifier but the ALF proof |
|
235 |
used @{text "k + 4"}. Quantified version must be nested @{text |
|
236 |
"\<exists>k'. \<forall>i j. ..."} *} |
|
3335 | 237 |
|
11704
3c50a2cd6f00
* sane numerals (stage 2): plain "num" syntax (removed "#");
wenzelm
parents:
11701
diff
changeset
|
238 |
lemma ack_add_bound2: "i < ack (k, j) ==> i + j < ack (4 + k, j)" |
11024 | 239 |
apply (rule_tac j = "ack (k, j) + ack (0, j)" in less_trans) |
240 |
prefer 2 |
|
241 |
apply (rule ack_add_bound [THEN less_le_trans]) |
|
242 |
apply simp |
|
243 |
apply (rule add_less_mono less_ack2 | assumption)+ |
|
244 |
done |
|
245 |
||
246 |
||
247 |
||
248 |
text {* Inductive definition of the @{term PR} functions *} |
|
3335 | 249 |
|
11024 | 250 |
text {* MAIN RESULT *} |
251 |
||
252 |
lemma SC_case: "SC l < ack (1, list_add l)" |
|
253 |
apply (unfold SC_def) |
|
254 |
apply (induct l) |
|
255 |
apply (simp_all add: le_add1 le_imp_less_Suc) |
|
256 |
done |
|
257 |
||
19676 | 258 |
lemma CONSTANT_case: "CONSTANT k l < ack (k, list_add l)" |
11024 | 259 |
apply simp |
260 |
done |
|
3335 | 261 |
|
11024 | 262 |
lemma PROJ_case [rule_format]: "\<forall>i. PROJ i l < ack (0, list_add l)" |
263 |
apply (simp add: PROJ_def) |
|
264 |
apply (induct l) |
|
265 |
apply simp_all |
|
266 |
apply (rule allI) |
|
267 |
apply (case_tac i) |
|
268 |
apply (simp (no_asm_simp) add: le_add1 le_imp_less_Suc) |
|
269 |
apply (simp (no_asm_simp)) |
|
270 |
apply (blast intro: less_le_trans intro!: le_add2) |
|
271 |
done |
|
272 |
||
273 |
||
274 |
text {* @{term COMP} case *} |
|
3335 | 275 |
|
11024 | 276 |
lemma COMP_map_aux: "fs \<in> lists (PRIMREC \<inter> {f. \<exists>kf. \<forall>l. f l < ack (kf, list_add l)}) |
277 |
==> \<exists>k. \<forall>l. list_add (map (\<lambda>f. f l) fs) < ack (k, list_add l)" |
|
278 |
apply (erule lists.induct) |
|
279 |
apply (rule_tac x = 0 in exI) |
|
280 |
apply simp |
|
281 |
apply safe |
|
282 |
apply simp |
|
283 |
apply (rule exI) |
|
284 |
apply (blast intro: add_less_mono ack_add_bound less_trans) |
|
285 |
done |
|
286 |
||
287 |
lemma COMP_case: |
|
288 |
"\<forall>l. g l < ack (kg, list_add l) ==> |
|
289 |
fs \<in> lists(PRIMREC Int {f. \<exists>kf. \<forall>l. f l < ack(kf, list_add l)}) |
|
290 |
==> \<exists>k. \<forall>l. COMP g fs l < ack(k, list_add l)" |
|
291 |
apply (unfold COMP_def) |
|
292 |
apply (frule Int_lower1 [THEN lists_mono, THEN subsetD]) |
|
16588 | 293 |
--{*Now, if meson tolerated map, we could finish with |
16731 | 294 |
@{text "(drule COMP_map_aux, meson ack_less_mono2 ack_nest_bound less_trans)"} *} |
16588 | 295 |
apply (erule COMP_map_aux [THEN exE]) |
296 |
apply (rule exI) |
|
297 |
apply (rule allI) |
|
298 |
apply (drule spec)+ |
|
299 |
apply (erule less_trans) |
|
300 |
apply (blast intro: ack_less_mono2 ack_nest_bound less_trans) |
|
11024 | 301 |
done |
302 |
||
303 |
||
304 |
text {* @{term PREC} case *} |
|
3335 | 305 |
|
11024 | 306 |
lemma PREC_case_aux: |
307 |
"\<forall>l. f l + list_add l < ack (kf, list_add l) ==> |
|
308 |
\<forall>l. g l + list_add l < ack (kg, list_add l) ==> |
|
309 |
PREC f g l + list_add l < ack (Suc (kf + kg), list_add l)" |
|
310 |
apply (unfold PREC_def) |
|
311 |
apply (case_tac l) |
|
312 |
apply simp_all |
|
313 |
apply (blast intro: less_trans) |
|
314 |
apply (erule ssubst) -- {* get rid of the needless assumption *} |
|
315 |
apply (induct_tac a) |
|
316 |
apply simp_all |
|
317 |
txt {* base case *} |
|
318 |
apply (blast intro: le_add1 [THEN le_imp_less_Suc, THEN ack_less_mono1] less_trans) |
|
319 |
txt {* induction step *} |
|
320 |
apply (rule Suc_leI [THEN le_less_trans]) |
|
321 |
apply (rule le_refl [THEN add_le_mono, THEN le_less_trans]) |
|
322 |
prefer 2 |
|
323 |
apply (erule spec) |
|
324 |
apply (simp add: le_add2) |
|
325 |
txt {* final part of the simplification *} |
|
326 |
apply simp |
|
327 |
apply (rule le_add2 [THEN ack_le_mono1, THEN le_less_trans]) |
|
328 |
apply (erule ack_less_mono2) |
|
329 |
done |
|
330 |
||
331 |
lemma PREC_case: |
|
332 |
"\<forall>l. f l < ack (kf, list_add l) ==> |
|
333 |
\<forall>l. g l < ack (kg, list_add l) ==> |
|
334 |
\<exists>k. \<forall>l. PREC f g l < ack (k, list_add l)" |
|
335 |
apply (rule exI) |
|
336 |
apply (rule allI) |
|
337 |
apply (rule le_less_trans [OF le_add1 PREC_case_aux]) |
|
338 |
apply (blast intro: ack_add_bound2)+ |
|
339 |
done |
|
340 |
||
341 |
lemma ack_bounds_PRIMREC: "f \<in> PRIMREC ==> \<exists>k. \<forall>l. f l < ack (k, list_add l)" |
|
342 |
apply (erule PRIMREC.induct) |
|
19676 | 343 |
apply (blast intro: SC_case CONSTANT_case PROJ_case COMP_case PREC_case)+ |
11024 | 344 |
done |
345 |
||
346 |
lemma ack_not_PRIMREC: "(\<lambda>l. case l of [] => 0 | x # l' => ack (x, x)) \<notin> PRIMREC" |
|
347 |
apply (rule notI) |
|
348 |
apply (erule ack_bounds_PRIMREC [THEN exE]) |
|
349 |
apply (rule less_irrefl) |
|
350 |
apply (drule_tac x = "[x]" in spec) |
|
351 |
apply simp |
|
352 |
done |
|
3335 | 353 |
|
354 |
end |