src/HOL/ex/Primrec.thy
 changeset 11024 23bf8d787b04 parent 8703 816d8f6513be child 11464 ddea204de5bc
```     1.1 --- a/src/HOL/ex/Primrec.thy	Thu Feb 01 20:48:58 2001 +0100
1.2 +++ b/src/HOL/ex/Primrec.thy	Thu Feb 01 20:51:13 2001 +0100
1.3 @@ -1,72 +1,348 @@
1.4 -(*  Title:      HOL/ex/Primrec
1.5 +(*  Title:      HOL/ex/Primrec.thy
1.6      ID:         \$Id\$
1.7      Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
1.8      Copyright   1997  University of Cambridge
1.9
1.10 -Primitive Recursive Functions
1.11 -
1.12 -Proof adopted from
1.13 -Nora Szasz,
1.14 -A Machine Checked Proof that Ackermann's Function is not Primitive Recursive,
1.15 -In: Huet & Plotkin, eds., Logical Environments (CUP, 1993), 317-338.
1.16 -
1.17 -See also E. Mendelson, Introduction to Mathematical Logic.
1.18 -(Van Nostrand, 1964), page 250, exercise 11.
1.19 -
1.20 -Demonstrates recursive definitions, the TFL package
1.21 +Primitive Recursive Functions.  Demonstrates recursive definitions,
1.22 +the TFL package.
1.23  *)
1.24
1.25 -Primrec = Main +
1.26 +header {* Primitive Recursive Functions *}
1.27 +
1.28 +theory Primrec = Main:
1.29 +
1.30 +text {*
1.31 +  Proof adopted from
1.32 +
1.33 +  Nora Szasz, A Machine Checked Proof that Ackermann's Function is not
1.34 +  Primitive Recursive, In: Huet \& Plotkin, eds., Logical Environments
1.35 +  (CUP, 1993), 317-338.
1.36 +
1.37 +  See also E. Mendelson, Introduction to Mathematical Logic.  (Van
1.38 +  Nostrand, 1964), page 250, exercise 11.
1.39 +  \medskip
1.40 +*}
1.41 +
1.42 +consts ack :: "nat * nat => nat"
1.43 +recdef ack  "less_than <*lex*> less_than"
1.44 +  "ack (0, n) =  Suc n"
1.45 +  "ack (Suc m, 0) = ack (m, 1)"
1.46 +  "ack (Suc m, Suc n) = ack (m, ack (Suc m, n))"
1.47 +
1.48 +consts list_add :: "nat list => nat"
1.49 +primrec
1.50 +  "list_add [] = 0"
1.51 +  "list_add (m # ms) = m + list_add ms"
1.52 +
1.53 +consts zeroHd :: "nat list => nat"
1.54 +primrec
1.55 +  "zeroHd [] = 0"
1.56 +  "zeroHd (m # ms) = m"
1.57 +
1.58 +
1.59 +text {* The set of primitive recursive functions of type @{typ "nat list => nat"}. *}
1.60 +
1.61 +constdefs
1.62 +  SC :: "nat list => nat"
1.63 +  "SC l == Suc (zeroHd l)"
1.64
1.65 -consts ack  :: "nat * nat => nat"
1.66 -recdef ack "less_than <*lex*> less_than"
1.67 -    "ack (0,n) =  Suc n"
1.68 -    "ack (Suc m,0) = (ack (m, 1))"
1.69 -    "ack (Suc m, Suc n) = ack (m, ack (Suc m, n))"
1.70 +  CONST :: "nat => nat list => nat"
1.71 +  "CONST k l == k"
1.72 +
1.73 +  PROJ :: "nat => nat list => nat"
1.74 +  "PROJ i l == zeroHd (drop i l)"
1.75 +
1.76 +  COMP :: "(nat list => nat) => (nat list => nat) list => nat list => nat"
1.77 +  "COMP g fs l == g (map (\<lambda>f. f l) fs)"
1.78 +
1.79 +  PREC :: "(nat list => nat) => (nat list => nat) => nat list => nat"
1.80 +  "PREC f g l ==
1.81 +    case l of
1.82 +      [] => 0
1.83 +    | x # l' => nat_rec (f l') (\<lambda>y r. g (r # y # l')) x"
1.84 +  -- {* Note that @{term g} is applied first to @{term "PREC f g y"} and then to @{term y}! *}
1.85 +
1.86 +consts PRIMREC :: "(nat list => nat) set"
1.87 +inductive PRIMREC
1.88 +  intros
1.89 +    SC: "SC \<in> PRIMREC"
1.90 +    CONST: "CONST k \<in> PRIMREC"
1.91 +    PROJ: "PROJ i \<in> PRIMREC"
1.92 +    COMP: "g \<in> PRIMREC ==> fs \<in> lists PRIMREC ==> COMP g fs \<in> PRIMREC"
1.93 +    PREC: "f \<in> PRIMREC ==> g \<in> PRIMREC ==> PREC f g \<in> PRIMREC"
1.94 +
1.95 +
1.96 +text {* Useful special cases of evaluation *}
1.97 +
1.98 +lemma SC [simp]: "SC (x # l) = Suc x"
1.99 +  apply (simp add: SC_def)
1.100 +  done
1.101 +
1.102 +lemma CONST [simp]: "CONST k l = k"
1.103 +  apply (simp add: CONST_def)
1.104 +  done
1.105 +
1.106 +lemma PROJ_0 [simp]: "PROJ 0 (x # l) = x"
1.107 +  apply (simp add: PROJ_def)
1.108 +  done
1.109 +
1.110 +lemma COMP_1 [simp]: "COMP g [f] l = g [f l]"
1.111 +  apply (simp add: COMP_def)
1.112 +  done
1.113
1.114 -consts  list_add :: nat list => nat
1.115 -primrec
1.116 -  "list_add []     = 0"
1.117 -  "list_add (m#ms) = m + list_add ms"
1.118 +lemma PREC_0 [simp]: "PREC f g (0 # l) = f l"
1.119 +  apply (simp add: PREC_def)
1.120 +  done
1.121 +
1.122 +lemma PREC_Suc [simp]: "PREC f g (Suc x # l) = g (PREC f g (x # l) # x # l)"
1.123 +  apply (simp add: PREC_def)
1.124 +  done
1.125 +
1.126 +
1.127 +text {* PROPERTY A 4 *}
1.128 +
1.129 +lemma less_ack2 [iff]: "j < ack (i, j)"
1.130 +  apply (induct i j rule: ack.induct)
1.131 +    apply simp_all
1.132 +  done
1.133 +
1.134 +
1.135 +text {* PROPERTY A 5-, the single-step lemma *}
1.136 +
1.137 +lemma ack_less_ack_Suc2 [iff]: "ack(i, j) < ack (i, Suc j)"
1.138 +  apply (induct i j rule: ack.induct)
1.139 +    apply simp_all
1.140 +  done
1.141 +
1.142 +
1.143 +text {* PROPERTY A 5, monotonicity for @{text "<"} *}
1.144 +
1.145 +lemma ack_less_mono2: "j < k ==> ack (i, j) < ack (i, k)"
1.146 +  apply (induct i k rule: ack.induct)
1.147 +    apply simp_all
1.148 +  apply (blast elim!: less_SucE intro: less_trans)
1.149 +  done
1.150 +
1.151 +
1.152 +text {* PROPERTY A 5', monotonicity for @{text \<le>} *}
1.153 +
1.154 +lemma ack_le_mono2: "j \<le> k ==> ack (i, j) \<le> ack (i, k)"
1.155 +  apply (simp add: order_le_less)
1.156 +  apply (blast intro: ack_less_mono2)
1.157 +  done
1.158
1.159 -consts  zeroHd  :: nat list => nat
1.160 -primrec
1.161 -  "zeroHd []     = 0"
1.162 -  "zeroHd (m#ms) = m"
1.163 +
1.164 +text {* PROPERTY A 6 *}
1.165 +
1.166 +lemma ack2_le_ack1 [iff]: "ack (i, Suc j) \<le> ack (Suc i, j)"
1.167 +  apply (induct j)
1.168 +   apply simp_all
1.169 +  apply (blast intro: ack_le_mono2 less_ack2 [THEN Suc_leI] le_trans)
1.170 +  done
1.171 +
1.172 +
1.173 +text {* PROPERTY A 7-, the single-step lemma *}
1.174 +
1.175 +lemma ack_less_ack_Suc1 [iff]: "ack (i, j) < ack (Suc i, j)"
1.176 +  apply (blast intro: ack_less_mono2 less_le_trans)
1.177 +  done
1.178 +
1.179 +
1.180 +text {* PROPERTY A 4'? Extra lemma needed for @{term CONST} case, constant functions *}
1.181 +
1.182 +lemma less_ack1 [iff]: "i < ack (i, j)"
1.183 +  apply (induct i)
1.184 +   apply simp_all
1.185 +  apply (blast intro: Suc_leI le_less_trans)
1.186 +  done
1.187 +
1.188 +
1.189 +text {* PROPERTY A 8 *}
1.190 +
1.191 +lemma ack_1 [simp]: "ack (1, j) = j + #2"
1.192 +  apply (induct j)
1.193 +   apply simp_all
1.194 +  done
1.195 +
1.196 +
1.197 +text {* PROPERTY A 9.  The unary @{term 1} and @{term 2} in @{term
1.198 +  ack} is essential for the rewriting. *}
1.199 +
1.200 +lemma ack_2 [simp]: "ack (2, j) = #2 * j + #3"
1.201 +  apply (induct j)
1.202 +   apply simp_all
1.203 +  done
1.204
1.205
1.206 -(** The set of primitive recursive functions of type  nat list => nat **)
1.207 -consts
1.208 -    PRIMREC :: (nat list => nat) set
1.209 -    SC      :: nat list => nat
1.210 -    CONST   :: [nat, nat list] => nat
1.211 -    PROJ    :: [nat, nat list] => nat
1.212 -    COMP    :: [nat list => nat, (nat list => nat)list, nat list] => nat
1.213 -    PREC    :: [nat list => nat, nat list => nat, nat list] => nat
1.214 +text {* PROPERTY A 7, monotonicity for @{text "<"} [not clear why
1.215 +  @{thm [source] ack_1} is now needed first!] *}
1.216 +
1.217 +lemma ack_less_mono1_aux: "ack (i, k) < ack (Suc (i +i'), k)"
1.218 +  apply (induct i k rule: ack.induct)
1.219 +    apply simp_all
1.220 +   prefer 2
1.221 +   apply (blast intro: less_trans ack_less_mono2)
1.222 +  apply (induct_tac i' n rule: ack.induct)
1.223 +    apply simp_all
1.224 +  apply (blast intro: Suc_leI [THEN le_less_trans] ack_less_mono2)
1.225 +  done
1.226 +
1.227 +lemma ack_less_mono1: "i < j ==> ack (i, k) < ack (j, k)"
1.228 +  apply (drule less_imp_Suc_add)
1.229 +  apply (blast intro!: ack_less_mono1_aux)
1.230 +  done
1.231 +
1.232 +
1.233 +text {* PROPERTY A 7', monotonicity for @{text "\<le>"} *}
1.234 +
1.235 +lemma ack_le_mono1: "i \<le> j ==> ack (i, k) \<le> ack (j, k)"
1.236 +  apply (simp add: order_le_less)
1.237 +  apply (blast intro: ack_less_mono1)
1.238 +  done
1.239 +
1.240 +
1.241 +text {* PROPERTY A 10 *}
1.242 +
1.243 +lemma ack_nest_bound: "ack(i1, ack (i2, j)) < ack (#2 + (i1 + i2), j)"
1.244 +  apply (simp add: numerals)
1.245 +  apply (rule ack2_le_ack1 [THEN [2] less_le_trans])
1.246 +  apply simp
1.247 +  apply (rule le_add1 [THEN ack_le_mono1, THEN le_less_trans])
1.248 +  apply (rule ack_less_mono1 [THEN ack_less_mono2])
1.249 +  apply (simp add: le_imp_less_Suc le_add2)
1.250 +  done
1.251 +
1.252
1.253 -defs
1.254 +text {* PROPERTY A 11 *}
1.255
1.256 -  SC_def    "SC l        == Suc (zeroHd l)"
1.257 +lemma ack_add_bound: "ack (i1, j) + ack (i2, j) < ack (#4 + (i1 + i2), j)"
1.258 +  apply (rule_tac j = "ack (2, ack (i1 + i2, j))" in less_trans)
1.259 +   prefer 2
1.260 +   apply (rule ack_nest_bound [THEN less_le_trans])
1.261 +   apply (simp add: Suc3_eq_add_3)
1.262 +  apply simp
1.263 +  apply (cut_tac i = i1 and m1 = i2 and k = j in le_add1 [THEN ack_le_mono1])
1.264 +  apply (cut_tac i = "i2" and m1 = i1 and k = j in le_add2 [THEN ack_le_mono1])
1.265 +  apply auto
1.266 +  done
1.267 +
1.268 +
1.269 +text {* PROPERTY A 12.  Article uses existential quantifier but the ALF proof
1.270 +  used @{text "k + 4"}.  Quantified version must be nested @{text
1.271 +  "\<exists>k'. \<forall>i j. ..."} *}
1.272
1.273 -  CONST_def "CONST k l   == k"
1.274 +lemma ack_add_bound2: "i < ack (k, j) ==> i + j < ack (#4 + k, j)"
1.275 +  apply (rule_tac j = "ack (k, j) + ack (0, j)" in less_trans)
1.276 +   prefer 2
1.277 +   apply (rule ack_add_bound [THEN less_le_trans])
1.278 +   apply simp
1.279 +  apply (rule add_less_mono less_ack2 | assumption)+
1.280 +  done
1.281 +
1.282 +
1.283 +
1.284 +text {* Inductive definition of the @{term PR} functions *}
1.285
1.286 -  PROJ_def  "PROJ i l    == zeroHd (drop i l)"
1.287 +text {* MAIN RESULT *}
1.288 +
1.289 +lemma SC_case: "SC l < ack (1, list_add l)"
1.290 +  apply (unfold SC_def)
1.291 +  apply (induct l)
1.292 +  apply (simp_all add: le_add1 le_imp_less_Suc)
1.293 +  done
1.294 +
1.295 +lemma CONST_case: "CONST k l < ack (k, list_add l)"
1.296 +  apply simp
1.297 +  done
1.298
1.299 -  COMP_def  "COMP g fs l == g (map (%f. f l) fs)"
1.300 +lemma PROJ_case [rule_format]: "\<forall>i. PROJ i l < ack (0, list_add l)"
1.301 +  apply (simp add: PROJ_def)
1.302 +  apply (induct l)
1.303 +   apply simp_all
1.304 +  apply (rule allI)
1.305 +  apply (case_tac i)
1.306 +  apply (simp (no_asm_simp) add: le_add1 le_imp_less_Suc)
1.307 +  apply (simp (no_asm_simp))
1.308 +  apply (blast intro: less_le_trans intro!: le_add2)
1.309 +  done
1.310 +
1.311 +
1.312 +text {* @{term COMP} case *}
1.313
1.314 -  (*Note that g is applied first to PREC f g y and then to y!*)
1.315 -  PREC_def  "PREC f g l == case l of
1.316 -                             []   => 0
1.317 -                           | x#l' => nat_rec (f l') (%y r. g (r#y#l')) x"
1.318 +lemma COMP_map_aux: "fs \<in> lists (PRIMREC \<inter> {f. \<exists>kf. \<forall>l. f l < ack (kf, list_add l)})
1.319 +  ==> \<exists>k. \<forall>l. list_add (map (\<lambda>f. f l) fs) < ack (k, list_add l)"
1.320 +  apply (erule lists.induct)
1.321 +  apply (rule_tac x = 0 in exI)
1.322 +   apply simp
1.323 +  apply safe
1.324 +  apply simp
1.325 +  apply (rule exI)
1.326 +  apply (blast intro: add_less_mono ack_add_bound less_trans)
1.327 +  done
1.328 +
1.329 +lemma COMP_case:
1.330 +  "\<forall>l. g l < ack (kg, list_add l) ==>
1.331 +  fs \<in> lists(PRIMREC Int {f. \<exists>kf. \<forall>l. f l < ack(kf, list_add l)})
1.332 +  ==> \<exists>k. \<forall>l. COMP g fs  l < ack(k, list_add l)"
1.333 +  apply (unfold COMP_def)
1.334 +  apply (frule Int_lower1 [THEN lists_mono, THEN subsetD])
1.335 +  apply (erule COMP_map_aux [THEN exE])
1.336 +  apply (rule exI)
1.337 +  apply (rule allI)
1.338 +  apply (drule spec)+
1.339 +  apply (erule less_trans)
1.340 +  apply (blast intro: ack_less_mono2 ack_nest_bound less_trans)
1.341 +  done
1.342 +
1.343 +
1.344 +text {* @{term PREC} case *}
1.345
1.346 -
1.347 -inductive PRIMREC
1.348 -  intrs
1.349 -    SC       "SC : PRIMREC"
1.350 -    CONST    "CONST k : PRIMREC"
1.351 -    PROJ     "PROJ i : PRIMREC"
1.352 -    COMP     "[| g: PRIMREC; fs: lists PRIMREC |] ==> COMP g fs : PRIMREC"
1.353 -    PREC     "[| f: PRIMREC; g: PRIMREC |] ==> PREC f g: PRIMREC"
1.354 -  monos      lists_mono
1.355 +lemma PREC_case_aux:
1.356 +  "\<forall>l. f l + list_add l < ack (kf, list_add l) ==>
1.357 +    \<forall>l. g l + list_add l < ack (kg, list_add l) ==>
1.358 +    PREC f g l + list_add l < ack (Suc (kf + kg), list_add l)"
1.359 +  apply (unfold PREC_def)
1.360 +  apply (case_tac l)
1.361 +   apply simp_all
1.362 +   apply (blast intro: less_trans)
1.363 +  apply (erule ssubst) -- {* get rid of the needless assumption *}
1.364 +  apply (induct_tac a)
1.365 +   apply simp_all
1.366 +   txt {* base case *}
1.367 +   apply (blast intro: le_add1 [THEN le_imp_less_Suc, THEN ack_less_mono1] less_trans)
1.368 +  txt {* induction step *}
1.369 +  apply (rule Suc_leI [THEN le_less_trans])
1.370 +   apply (rule le_refl [THEN add_le_mono, THEN le_less_trans])
1.371 +    prefer 2
1.372 +    apply (erule spec)
1.373 +   apply (simp add: le_add2)
1.374 +  txt {* final part of the simplification *}
1.375 +  apply simp
1.376 +  apply (rule le_add2 [THEN ack_le_mono1, THEN le_less_trans])
1.377 +  apply (erule ack_less_mono2)
1.378 +  done
1.379 +
1.380 +lemma PREC_case:
1.381 +  "\<forall>l. f l < ack (kf, list_add l) ==>
1.382 +    \<forall>l. g l < ack (kg, list_add l) ==>
1.383 +    \<exists>k. \<forall>l. PREC f g l < ack (k, list_add l)"
1.384 +  apply (rule exI)
1.385 +  apply (rule allI)
1.386 +  apply (rule le_less_trans [OF le_add1 PREC_case_aux])
1.387 +   apply (blast intro: ack_add_bound2)+
1.388 +  done
1.389 +
1.390 +lemma ack_bounds_PRIMREC: "f \<in> PRIMREC ==> \<exists>k. \<forall>l. f l < ack (k, list_add l)"
1.391 +  apply (erule PRIMREC.induct)
1.392 +      apply (blast intro: SC_case CONST_case PROJ_case COMP_case PREC_case)+
1.393 +  done
1.394 +
1.395 +lemma ack_not_PRIMREC: "(\<lambda>l. case l of [] => 0 | x # l' => ack (x, x)) \<notin> PRIMREC"
1.396 +  apply (rule notI)
1.397 +  apply (erule ack_bounds_PRIMREC [THEN exE])
1.398 +  apply (rule less_irrefl)
1.399 +  apply (drule_tac x = "[x]" in spec)
1.400 +  apply simp
1.401 +  done
1.402
1.403  end
```