| author | krauss | 
| Sat, 15 May 2010 00:45:42 +0200 | |
| changeset 36934 | ae0809cff6f0 | 
| parent 34055 | fdf294ee08b2 | 
| child 41460 | ea56b98aee83 | 
| 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 | ||
| 27626 | 6 | Ackermann's Function and the | 
| 7 | Primitive Recursive Functions. | |
| 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 | ||
| 27626 | 27 | subsection{* Ackermann's Function *}
 | 
| 11024 | 28 | |
| 27626 | 29 | fun ack :: "nat => nat => nat" where | 
| 30 | "ack 0 n = Suc n" | | |
| 31 | "ack (Suc m) 0 = ack m 1" | | |
| 32 | "ack (Suc m) (Suc n) = ack m (ack (Suc m) n)" | |
| 11024 | 33 | |
| 34 | ||
| 35 | text {* PROPERTY A 4 *}
 | |
| 36 | ||
| 27626 | 37 | lemma less_ack2 [iff]: "j < ack i j" | 
| 38 | by (induct i j rule: ack.induct) simp_all | |
| 11024 | 39 | |
| 40 | ||
| 41 | text {* PROPERTY A 5-, the single-step lemma *}
 | |
| 42 | ||
| 27626 | 43 | lemma ack_less_ack_Suc2 [iff]: "ack i j < ack i (Suc j)" | 
| 44 | by (induct i j rule: ack.induct) simp_all | |
| 11024 | 45 | |
| 46 | ||
| 47 | text {* PROPERTY A 5, monotonicity for @{text "<"} *}
 | |
| 48 | ||
| 27626 | 49 | lemma ack_less_mono2: "j < k ==> ack i j < ack i k" | 
| 50 | using lift_Suc_mono_less[where f = "ack i"] | |
| 51 | by (metis ack_less_ack_Suc2) | |
| 11024 | 52 | |
| 53 | ||
| 54 | text {* PROPERTY A 5', monotonicity for @{text \<le>} *}
 | |
| 55 | ||
| 27626 | 56 | lemma ack_le_mono2: "j \<le> k ==> ack i j \<le> ack i k" | 
| 57 | apply (simp add: order_le_less) | |
| 58 | apply (blast intro: ack_less_mono2) | |
| 59 | done | |
| 3335 | 60 | |
| 11024 | 61 | |
| 62 | text {* PROPERTY A 6 *}
 | |
| 63 | ||
| 27626 | 64 | lemma ack2_le_ack1 [iff]: "ack i (Suc j) \<le> ack (Suc i) j" | 
| 26072 
f65a7fa2da6c
<= and < on nat no longer depend on wellfounded relations
 haftmann parents: 
25157diff
changeset | 65 | proof (induct j) | 
| 
f65a7fa2da6c
<= and < on nat no longer depend on wellfounded relations
 haftmann parents: 
25157diff
changeset | 66 | case 0 show ?case by simp | 
| 
f65a7fa2da6c
<= and < on nat no longer depend on wellfounded relations
 haftmann parents: 
25157diff
changeset | 67 | next | 
| 
f65a7fa2da6c
<= and < on nat no longer depend on wellfounded relations
 haftmann parents: 
25157diff
changeset | 68 | case (Suc j) show ?case | 
| 
f65a7fa2da6c
<= and < on nat no longer depend on wellfounded relations
 haftmann parents: 
25157diff
changeset | 69 | by (auto intro!: ack_le_mono2) | 
| 
f65a7fa2da6c
<= and < on nat no longer depend on wellfounded relations
 haftmann parents: 
25157diff
changeset | 70 | (metis Suc Suc_leI Suc_lessI less_ack2 linorder_not_less) | 
| 
f65a7fa2da6c
<= and < on nat no longer depend on wellfounded relations
 haftmann parents: 
25157diff
changeset | 71 | qed | 
| 11024 | 72 | |
| 73 | ||
| 74 | text {* PROPERTY A 7-, the single-step lemma *}
 | |
| 75 | ||
| 27626 | 76 | lemma ack_less_ack_Suc1 [iff]: "ack i j < ack (Suc i) j" | 
| 77 | by (blast intro: ack_less_mono2 less_le_trans) | |
| 11024 | 78 | |
| 79 | ||
| 19676 | 80 | text {* PROPERTY A 4'? Extra lemma needed for @{term CONSTANT} case, constant functions *}
 | 
| 11024 | 81 | |
| 27626 | 82 | lemma less_ack1 [iff]: "i < ack i j" | 
| 83 | apply (induct i) | |
| 84 | apply simp_all | |
| 85 | apply (blast intro: Suc_leI le_less_trans) | |
| 86 | done | |
| 11024 | 87 | |
| 88 | ||
| 89 | text {* PROPERTY A 8 *}
 | |
| 90 | ||
| 27626 | 91 | lemma ack_1 [simp]: "ack (Suc 0) j = j + 2" | 
| 92 | by (induct j) simp_all | |
| 11024 | 93 | |
| 94 | ||
| 11701 
3d51fbf81c17
sane numerals (stage 1): added generic 1, removed 1' and 2 on nat,
 wenzelm parents: 
11464diff
changeset | 95 | text {* PROPERTY A 9.  The unary @{text 1} and @{text 2} in @{term
 | 
| 11024 | 96 | ack} is essential for the rewriting. *} | 
| 97 | ||
| 27626 | 98 | lemma ack_2 [simp]: "ack (Suc (Suc 0)) j = 2 * j + 3" | 
| 99 | by (induct j) simp_all | |
| 3335 | 100 | |
| 101 | ||
| 11024 | 102 | text {* PROPERTY A 7, monotonicity for @{text "<"} [not clear why
 | 
| 103 |   @{thm [source] ack_1} is now needed first!] *}
 | |
| 104 | ||
| 27626 | 105 | lemma ack_less_mono1_aux: "ack i k < ack (Suc (i +i')) k" | 
| 34055 | 106 | proof (induct i k rule: ack.induct) | 
| 107 | case (1 n) show ?case | |
| 108 | by (simp, metis ack_less_ack_Suc1 less_ack2 less_trans_Suc) | |
| 109 | next | |
| 110 | case (2 m) thus ?case by simp | |
| 111 | next | |
| 112 | case (3 m n) thus ?case | |
| 113 | by (simp, blast intro: less_trans ack_less_mono2) | |
| 114 | qed | |
| 11024 | 115 | |
| 27626 | 116 | lemma ack_less_mono1: "i < j ==> ack i k < ack j k" | 
| 117 | apply (drule less_imp_Suc_add) | |
| 118 | apply (blast intro!: ack_less_mono1_aux) | |
| 119 | done | |
| 11024 | 120 | |
| 121 | ||
| 122 | text {* PROPERTY A 7', monotonicity for @{text "\<le>"} *}
 | |
| 123 | ||
| 27626 | 124 | lemma ack_le_mono1: "i \<le> j ==> ack i k \<le> ack j k" | 
| 125 | apply (simp add: order_le_less) | |
| 126 | apply (blast intro: ack_less_mono1) | |
| 127 | done | |
| 11024 | 128 | |
| 129 | ||
| 130 | text {* PROPERTY A 10 *}
 | |
| 131 | ||
| 27626 | 132 | lemma ack_nest_bound: "ack i1 (ack i2 j) < ack (2 + (i1 + i2)) j" | 
| 133 | apply (simp add: numerals) | |
| 134 | apply (rule ack2_le_ack1 [THEN [2] less_le_trans]) | |
| 135 | apply simp | |
| 136 | apply (rule le_add1 [THEN ack_le_mono1, THEN le_less_trans]) | |
| 137 | apply (rule ack_less_mono1 [THEN ack_less_mono2]) | |
| 138 | apply (simp add: le_imp_less_Suc le_add2) | |
| 139 | done | |
| 11024 | 140 | |
| 3335 | 141 | |
| 11024 | 142 | text {* PROPERTY A 11 *}
 | 
| 3335 | 143 | |
| 27626 | 144 | lemma ack_add_bound: "ack i1 j + ack i2 j < ack (4 + (i1 + i2)) j" | 
| 145 | apply (rule less_trans [of _ "ack (Suc (Suc 0)) (ack (i1 + i2) j)"]) | |
| 146 | prefer 2 | |
| 147 | apply (rule ack_nest_bound [THEN less_le_trans]) | |
| 148 | apply (simp add: Suc3_eq_add_3) | |
| 149 | apply simp | |
| 150 | apply (cut_tac i = i1 and m1 = i2 and k = j in le_add1 [THEN ack_le_mono1]) | |
| 151 | apply (cut_tac i = "i2" and m1 = i1 and k = j in le_add2 [THEN ack_le_mono1]) | |
| 152 | apply auto | |
| 153 | done | |
| 11024 | 154 | |
| 155 | ||
| 156 | text {* PROPERTY A 12.  Article uses existential quantifier but the ALF proof
 | |
| 157 |   used @{text "k + 4"}.  Quantified version must be nested @{text
 | |
| 158 | "\<exists>k'. \<forall>i j. ..."} *} | |
| 3335 | 159 | |
| 27626 | 160 | lemma ack_add_bound2: "i < ack k j ==> i + j < ack (4 + k) j" | 
| 161 | apply (rule less_trans [of _ "ack k j + ack 0 j"]) | |
| 162 | apply (blast intro: add_less_mono less_ack2) | |
| 163 | apply (rule ack_add_bound [THEN less_le_trans]) | |
| 164 | apply simp | |
| 165 | done | |
| 166 | ||
| 167 | ||
| 168 | subsection{*Primitive Recursive Functions*}
 | |
| 169 | ||
| 170 | primrec hd0 :: "nat list => nat" where | |
| 171 | "hd0 [] = 0" | | |
| 172 | "hd0 (m # ms) = m" | |
| 11024 | 173 | |
| 174 | ||
| 27626 | 175 | text {* Inductive definition of the set of primitive recursive functions of type @{typ "nat list => nat"}. *}
 | 
| 11024 | 176 | |
| 27626 | 177 | definition SC :: "nat list => nat" where | 
| 178 | "SC l = Suc (hd0 l)" | |
| 179 | ||
| 180 | definition CONSTANT :: "nat => nat list => nat" where | |
| 181 | "CONSTANT k l = k" | |
| 182 | ||
| 183 | definition PROJ :: "nat => nat list => nat" where | |
| 184 | "PROJ i l = hd0 (drop i l)" | |
| 185 | ||
| 186 | definition | |
| 187 | COMP :: "(nat list => nat) => (nat list => nat) list => nat list => nat" | |
| 188 | where "COMP g fs l = g (map (\<lambda>f. f l) fs)" | |
| 189 | ||
| 190 | definition PREC :: "(nat list => nat) => (nat list => nat) => nat list => nat" | |
| 191 | where | |
| 192 | "PREC f g l = | |
| 193 | (case l of | |
| 194 | [] => 0 | |
| 195 | | x # l' => nat_rec (f l') (\<lambda>y r. g (r # y # l')) x)" | |
| 196 |   -- {* Note that @{term g} is applied first to @{term "PREC f g y"} and then to @{term y}! *}
 | |
| 197 | ||
| 198 | inductive PRIMREC :: "(nat list => nat) => bool" where | |
| 199 | SC: "PRIMREC SC" | | |
| 200 | CONSTANT: "PRIMREC (CONSTANT k)" | | |
| 201 | PROJ: "PRIMREC (PROJ i)" | | |
| 202 | COMP: "PRIMREC g ==> \<forall>f \<in> set fs. PRIMREC f ==> PRIMREC (COMP g fs)" | | |
| 203 | PREC: "PRIMREC f ==> PRIMREC g ==> PRIMREC (PREC f g)" | |
| 204 | ||
| 205 | ||
| 206 | text {* Useful special cases of evaluation *}
 | |
| 207 | ||
| 208 | lemma SC [simp]: "SC (x # l) = Suc x" | |
| 209 | by (simp add: SC_def) | |
| 210 | ||
| 211 | lemma CONSTANT [simp]: "CONSTANT k l = k" | |
| 212 | by (simp add: CONSTANT_def) | |
| 213 | ||
| 214 | lemma PROJ_0 [simp]: "PROJ 0 (x # l) = x" | |
| 215 | by (simp add: PROJ_def) | |
| 216 | ||
| 217 | lemma COMP_1 [simp]: "COMP g [f] l = g [f l]" | |
| 218 | by (simp add: COMP_def) | |
| 219 | ||
| 220 | lemma PREC_0 [simp]: "PREC f g (0 # l) = f l" | |
| 221 | by (simp add: PREC_def) | |
| 222 | ||
| 223 | lemma PREC_Suc [simp]: "PREC f g (Suc x # l) = g (PREC f g (x # l) # x # l)" | |
| 224 | by (simp add: PREC_def) | |
| 225 | ||
| 3335 | 226 | |
| 11024 | 227 | text {* MAIN RESULT *}
 | 
| 228 | ||
| 27626 | 229 | lemma SC_case: "SC l < ack 1 (listsum l)" | 
| 230 | apply (unfold SC_def) | |
| 231 | apply (induct l) | |
| 232 | apply (simp_all add: le_add1 le_imp_less_Suc) | |
| 233 | done | |
| 11024 | 234 | |
| 27626 | 235 | lemma CONSTANT_case: "CONSTANT k l < ack k (listsum l)" | 
| 236 | by simp | |
| 3335 | 237 | |
| 27626 | 238 | lemma PROJ_case: "PROJ i l < ack 0 (listsum l)" | 
| 239 | apply (simp add: PROJ_def) | |
| 240 | apply (induct l arbitrary:i) | |
| 241 | apply (auto simp add: drop_Cons split: nat.split) | |
| 242 | apply (blast intro: less_le_trans le_add2) | |
| 243 | done | |
| 11024 | 244 | |
| 245 | ||
| 246 | text {* @{term COMP} case *}
 | |
| 3335 | 247 | |
| 27626 | 248 | lemma COMP_map_aux: "\<forall>f \<in> set fs. PRIMREC f \<and> (\<exists>kf. \<forall>l. f l < ack kf (listsum l)) | 
| 249 | ==> \<exists>k. \<forall>l. listsum (map (\<lambda>f. f l) fs) < ack k (listsum l)" | |
| 250 | apply (induct fs) | |
| 251 | apply (rule_tac x = 0 in exI) | |
| 252 | apply simp | |
| 253 | apply simp | |
| 254 | apply (blast intro: add_less_mono ack_add_bound less_trans) | |
| 255 | done | |
| 11024 | 256 | |
| 257 | lemma COMP_case: | |
| 27626 | 258 | "\<forall>l. g l < ack kg (listsum l) ==> | 
| 259 | \<forall>f \<in> set fs. PRIMREC f \<and> (\<exists>kf. \<forall>l. f l < ack kf (listsum l)) | |
| 260 | ==> \<exists>k. \<forall>l. COMP g fs l < ack k (listsum l)" | |
| 261 | apply (unfold COMP_def) | |
| 34055 | 262 | apply (drule COMP_map_aux) | 
| 263 | apply (meson ack_less_mono2 ack_nest_bound less_trans) | |
| 27626 | 264 | done | 
| 11024 | 265 | |
| 266 | ||
| 267 | text {* @{term PREC} case *}
 | |
| 3335 | 268 | |
| 11024 | 269 | lemma PREC_case_aux: | 
| 27626 | 270 | "\<forall>l. f l + listsum l < ack kf (listsum l) ==> | 
| 271 | \<forall>l. g l + listsum l < ack kg (listsum l) ==> | |
| 272 | PREC f g l + listsum l < ack (Suc (kf + kg)) (listsum l)" | |
| 273 | apply (unfold PREC_def) | |
| 274 | apply (case_tac l) | |
| 275 | apply simp_all | |
| 276 | apply (blast intro: less_trans) | |
| 277 | apply (erule ssubst) -- {* get rid of the needless assumption *}
 | |
| 278 | apply (induct_tac a) | |
| 279 | apply simp_all | |
| 280 |  txt {* base case *}
 | |
| 281 | apply (blast intro: le_add1 [THEN le_imp_less_Suc, THEN ack_less_mono1] less_trans) | |
| 282 | txt {* induction step *}
 | |
| 283 | apply (rule Suc_leI [THEN le_less_trans]) | |
| 284 | apply (rule le_refl [THEN add_le_mono, THEN le_less_trans]) | |
| 285 | prefer 2 | |
| 286 | apply (erule spec) | |
| 287 | apply (simp add: le_add2) | |
| 288 | txt {* final part of the simplification *}
 | |
| 289 | apply simp | |
| 290 | apply (rule le_add2 [THEN ack_le_mono1, THEN le_less_trans]) | |
| 291 | apply (erule ack_less_mono2) | |
| 292 | done | |
| 11024 | 293 | |
| 294 | lemma PREC_case: | |
| 27626 | 295 | "\<forall>l. f l < ack kf (listsum l) ==> | 
| 296 | \<forall>l. g l < ack kg (listsum l) ==> | |
| 297 | \<exists>k. \<forall>l. PREC f g l < ack k (listsum l)" | |
| 298 | by (metis le_less_trans [OF le_add1 PREC_case_aux] ack_add_bound2) | |
| 11024 | 299 | |
| 27626 | 300 | lemma ack_bounds_PRIMREC: "PRIMREC f ==> \<exists>k. \<forall>l. f l < ack k (listsum l)" | 
| 301 | apply (erule PRIMREC.induct) | |
| 302 | apply (blast intro: SC_case CONSTANT_case PROJ_case COMP_case PREC_case)+ | |
| 303 | done | |
| 11024 | 304 | |
| 27626 | 305 | theorem ack_not_PRIMREC: | 
| 306 | "\<not> PRIMREC (\<lambda>l. case l of [] => 0 | x # l' => ack x x)" | |
| 307 | apply (rule notI) | |
| 308 | apply (erule ack_bounds_PRIMREC [THEN exE]) | |
| 309 | apply (rule less_irrefl [THEN notE]) | |
| 310 | apply (drule_tac x = "[x]" in spec) | |
| 311 | apply simp | |
| 312 | done | |
| 3335 | 313 | |
| 314 | end |