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