src/HOL/ex/Primrec.thy
author wenzelm
Fri Nov 17 02:20:03 2006 +0100 (2006-11-17)
changeset 21404 eb85850d3eb7
parent 19736 d8d0f8f51d69
child 22283 26140713540b
permissions -rw-r--r--
more robust syntax for definition/abbreviation/notation;
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
wenzelm@11024
    69
consts PRIMREC :: "(nat list => nat) set"
wenzelm@11024
    70
inductive PRIMREC
wenzelm@11024
    71
  intros
wenzelm@11024
    72
    SC: "SC \<in> PRIMREC"
wenzelm@19676
    73
    CONSTANT: "CONSTANT k \<in> PRIMREC"
wenzelm@11024
    74
    PROJ: "PROJ i \<in> PRIMREC"
wenzelm@11024
    75
    COMP: "g \<in> PRIMREC ==> fs \<in> lists PRIMREC ==> COMP g fs \<in> PRIMREC"
wenzelm@11024
    76
    PREC: "f \<in> PRIMREC ==> g \<in> PRIMREC ==> PREC f g \<in> PRIMREC"
wenzelm@11024
    77
wenzelm@11024
    78
wenzelm@11024
    79
text {* Useful special cases of evaluation *}
wenzelm@11024
    80
wenzelm@11024
    81
lemma SC [simp]: "SC (x # l) = Suc x"
wenzelm@11024
    82
  apply (simp add: SC_def)
wenzelm@11024
    83
  done
wenzelm@11024
    84
wenzelm@19676
    85
lemma CONSTANT [simp]: "CONSTANT k l = k"
wenzelm@19676
    86
  apply (simp add: CONSTANT_def)
wenzelm@11024
    87
  done
wenzelm@11024
    88
wenzelm@11024
    89
lemma PROJ_0 [simp]: "PROJ 0 (x # l) = x"
wenzelm@11024
    90
  apply (simp add: PROJ_def)
wenzelm@11024
    91
  done
wenzelm@11024
    92
wenzelm@11024
    93
lemma COMP_1 [simp]: "COMP g [f] l = g [f l]"
wenzelm@11024
    94
  apply (simp add: COMP_def)
wenzelm@11024
    95
  done
paulson@3335
    96
wenzelm@11024
    97
lemma PREC_0 [simp]: "PREC f g (0 # l) = f l"
wenzelm@11024
    98
  apply (simp add: PREC_def)
wenzelm@11024
    99
  done
wenzelm@11024
   100
wenzelm@11024
   101
lemma PREC_Suc [simp]: "PREC f g (Suc x # l) = g (PREC f g (x # l) # x # l)"
wenzelm@11024
   102
  apply (simp add: PREC_def)
wenzelm@11024
   103
  done
wenzelm@11024
   104
wenzelm@11024
   105
wenzelm@11024
   106
text {* PROPERTY A 4 *}
wenzelm@11024
   107
wenzelm@11024
   108
lemma less_ack2 [iff]: "j < ack (i, j)"
wenzelm@11024
   109
  apply (induct i j rule: ack.induct)
wenzelm@11024
   110
    apply simp_all
wenzelm@11024
   111
  done
wenzelm@11024
   112
wenzelm@11024
   113
wenzelm@11024
   114
text {* PROPERTY A 5-, the single-step lemma *}
wenzelm@11024
   115
wenzelm@11024
   116
lemma ack_less_ack_Suc2 [iff]: "ack(i, j) < ack (i, Suc j)"
wenzelm@11024
   117
  apply (induct i j rule: ack.induct)
wenzelm@11024
   118
    apply simp_all
wenzelm@11024
   119
  done
wenzelm@11024
   120
wenzelm@11024
   121
wenzelm@11024
   122
text {* PROPERTY A 5, monotonicity for @{text "<"} *}
wenzelm@11024
   123
wenzelm@11024
   124
lemma ack_less_mono2: "j < k ==> ack (i, j) < ack (i, k)"
wenzelm@11024
   125
  apply (induct i k rule: ack.induct)
wenzelm@11024
   126
    apply simp_all
wenzelm@11024
   127
  apply (blast elim!: less_SucE intro: less_trans)
wenzelm@11024
   128
  done
wenzelm@11024
   129
wenzelm@11024
   130
wenzelm@11024
   131
text {* PROPERTY A 5', monotonicity for @{text \<le>} *}
wenzelm@11024
   132
wenzelm@11024
   133
lemma ack_le_mono2: "j \<le> k ==> ack (i, j) \<le> ack (i, k)"
wenzelm@11024
   134
  apply (simp add: order_le_less)
wenzelm@11024
   135
  apply (blast intro: ack_less_mono2)
wenzelm@11024
   136
  done
paulson@3335
   137
wenzelm@11024
   138
wenzelm@11024
   139
text {* PROPERTY A 6 *}
wenzelm@11024
   140
wenzelm@11024
   141
lemma ack2_le_ack1 [iff]: "ack (i, Suc j) \<le> ack (Suc i, j)"
wenzelm@11024
   142
  apply (induct j)
wenzelm@11024
   143
   apply simp_all
wenzelm@11024
   144
  apply (blast intro: ack_le_mono2 less_ack2 [THEN Suc_leI] le_trans)
wenzelm@11024
   145
  done
wenzelm@11024
   146
wenzelm@11024
   147
wenzelm@11024
   148
text {* PROPERTY A 7-, the single-step lemma *}
wenzelm@11024
   149
wenzelm@11024
   150
lemma ack_less_ack_Suc1 [iff]: "ack (i, j) < ack (Suc i, j)"
wenzelm@11024
   151
  apply (blast intro: ack_less_mono2 less_le_trans)
wenzelm@11024
   152
  done
wenzelm@11024
   153
wenzelm@11024
   154
wenzelm@19676
   155
text {* PROPERTY A 4'? Extra lemma needed for @{term CONSTANT} case, constant functions *}
wenzelm@11024
   156
wenzelm@11024
   157
lemma less_ack1 [iff]: "i < ack (i, j)"
wenzelm@11024
   158
  apply (induct i)
wenzelm@11024
   159
   apply simp_all
wenzelm@11024
   160
  apply (blast intro: Suc_leI le_less_trans)
wenzelm@11024
   161
  done
wenzelm@11024
   162
wenzelm@11024
   163
wenzelm@11024
   164
text {* PROPERTY A 8 *}
wenzelm@11024
   165
wenzelm@11704
   166
lemma ack_1 [simp]: "ack (Suc 0, j) = j + 2"
wenzelm@11024
   167
  apply (induct j)
wenzelm@11024
   168
   apply simp_all
wenzelm@11024
   169
  done
wenzelm@11024
   170
wenzelm@11024
   171
wenzelm@11701
   172
text {* PROPERTY A 9.  The unary @{text 1} and @{text 2} in @{term
wenzelm@11024
   173
  ack} is essential for the rewriting. *}
wenzelm@11024
   174
wenzelm@11704
   175
lemma ack_2 [simp]: "ack (Suc (Suc 0), j) = 2 * j + 3"
wenzelm@11024
   176
  apply (induct j)
wenzelm@11024
   177
   apply simp_all
wenzelm@11024
   178
  done
paulson@3335
   179
paulson@3335
   180
wenzelm@11024
   181
text {* PROPERTY A 7, monotonicity for @{text "<"} [not clear why
wenzelm@11024
   182
  @{thm [source] ack_1} is now needed first!] *}
wenzelm@11024
   183
wenzelm@11024
   184
lemma ack_less_mono1_aux: "ack (i, k) < ack (Suc (i +i'), k)"
wenzelm@11024
   185
  apply (induct i k rule: ack.induct)
wenzelm@11024
   186
    apply simp_all
wenzelm@11024
   187
   prefer 2
wenzelm@11024
   188
   apply (blast intro: less_trans ack_less_mono2)
wenzelm@11024
   189
  apply (induct_tac i' n rule: ack.induct)
wenzelm@11024
   190
    apply simp_all
wenzelm@11024
   191
  apply (blast intro: Suc_leI [THEN le_less_trans] ack_less_mono2)
wenzelm@11024
   192
  done
wenzelm@11024
   193
wenzelm@11024
   194
lemma ack_less_mono1: "i < j ==> ack (i, k) < ack (j, k)"
wenzelm@11024
   195
  apply (drule less_imp_Suc_add)
wenzelm@11024
   196
  apply (blast intro!: ack_less_mono1_aux)
wenzelm@11024
   197
  done
wenzelm@11024
   198
wenzelm@11024
   199
wenzelm@11024
   200
text {* PROPERTY A 7', monotonicity for @{text "\<le>"} *}
wenzelm@11024
   201
wenzelm@11024
   202
lemma ack_le_mono1: "i \<le> j ==> ack (i, k) \<le> ack (j, k)"
wenzelm@11024
   203
  apply (simp add: order_le_less)
wenzelm@11024
   204
  apply (blast intro: ack_less_mono1)
wenzelm@11024
   205
  done
wenzelm@11024
   206
wenzelm@11024
   207
wenzelm@11024
   208
text {* PROPERTY A 10 *}
wenzelm@11024
   209
wenzelm@11704
   210
lemma ack_nest_bound: "ack(i1, ack (i2, j)) < ack (2 + (i1 + i2), j)"
wenzelm@11024
   211
  apply (simp add: numerals)
wenzelm@11024
   212
  apply (rule ack2_le_ack1 [THEN [2] less_le_trans])
wenzelm@11024
   213
  apply simp
wenzelm@11024
   214
  apply (rule le_add1 [THEN ack_le_mono1, THEN le_less_trans])
wenzelm@11024
   215
  apply (rule ack_less_mono1 [THEN ack_less_mono2])
wenzelm@11024
   216
  apply (simp add: le_imp_less_Suc le_add2)
wenzelm@11024
   217
  done
wenzelm@11024
   218
paulson@3335
   219
wenzelm@11024
   220
text {* PROPERTY A 11 *}
paulson@3335
   221
wenzelm@11704
   222
lemma ack_add_bound: "ack (i1, j) + ack (i2, j) < ack (4 + (i1 + i2), j)"
wenzelm@11701
   223
  apply (rule_tac j = "ack (Suc (Suc 0), ack (i1 + i2, j))" in less_trans)
wenzelm@11024
   224
   prefer 2
wenzelm@11024
   225
   apply (rule ack_nest_bound [THEN less_le_trans])
wenzelm@11024
   226
   apply (simp add: Suc3_eq_add_3)
wenzelm@11024
   227
  apply simp
wenzelm@11024
   228
  apply (cut_tac i = i1 and m1 = i2 and k = j in le_add1 [THEN ack_le_mono1])
wenzelm@11024
   229
  apply (cut_tac i = "i2" and m1 = i1 and k = j in le_add2 [THEN ack_le_mono1])
wenzelm@11024
   230
  apply auto
wenzelm@11024
   231
  done
wenzelm@11024
   232
wenzelm@11024
   233
wenzelm@11024
   234
text {* PROPERTY A 12.  Article uses existential quantifier but the ALF proof
wenzelm@11024
   235
  used @{text "k + 4"}.  Quantified version must be nested @{text
wenzelm@11024
   236
  "\<exists>k'. \<forall>i j. ..."} *}
paulson@3335
   237
wenzelm@11704
   238
lemma ack_add_bound2: "i < ack (k, j) ==> i + j < ack (4 + k, j)"
wenzelm@11024
   239
  apply (rule_tac j = "ack (k, j) + ack (0, j)" in less_trans)
wenzelm@11024
   240
   prefer 2
wenzelm@11024
   241
   apply (rule ack_add_bound [THEN less_le_trans])
wenzelm@11024
   242
   apply simp
wenzelm@11024
   243
  apply (rule add_less_mono less_ack2 | assumption)+
wenzelm@11024
   244
  done
wenzelm@11024
   245
wenzelm@11024
   246
wenzelm@11024
   247
wenzelm@11024
   248
text {* Inductive definition of the @{term PR} functions *}
paulson@3335
   249
wenzelm@11024
   250
text {* MAIN RESULT *}
wenzelm@11024
   251
wenzelm@11024
   252
lemma SC_case: "SC l < ack (1, list_add l)"
wenzelm@11024
   253
  apply (unfold SC_def)
wenzelm@11024
   254
  apply (induct l)
wenzelm@11024
   255
  apply (simp_all add: le_add1 le_imp_less_Suc)
wenzelm@11024
   256
  done
wenzelm@11024
   257
wenzelm@19676
   258
lemma CONSTANT_case: "CONSTANT k l < ack (k, list_add l)"
wenzelm@11024
   259
  apply simp
wenzelm@11024
   260
  done
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)
wenzelm@11024
   265
   apply simp_all
wenzelm@11024
   266
  apply (rule allI)
wenzelm@11024
   267
  apply (case_tac i)
wenzelm@11024
   268
  apply (simp (no_asm_simp) add: le_add1 le_imp_less_Suc)
wenzelm@11024
   269
  apply (simp (no_asm_simp))
wenzelm@11024
   270
  apply (blast intro: less_le_trans intro!: le_add2)
wenzelm@11024
   271
  done
wenzelm@11024
   272
wenzelm@11024
   273
wenzelm@11024
   274
text {* @{term COMP} case *}
paulson@3335
   275
wenzelm@11024
   276
lemma COMP_map_aux: "fs \<in> lists (PRIMREC \<inter> {f. \<exists>kf. \<forall>l. f l < ack (kf, list_add l)})
wenzelm@11024
   277
  ==> \<exists>k. \<forall>l. list_add (map (\<lambda>f. f l) fs) < ack (k, list_add l)"
wenzelm@11024
   278
  apply (erule lists.induct)
wenzelm@11024
   279
  apply (rule_tac x = 0 in exI)
wenzelm@11024
   280
   apply simp
wenzelm@11024
   281
  apply safe
wenzelm@11024
   282
  apply simp
wenzelm@11024
   283
  apply (rule exI)
wenzelm@11024
   284
  apply (blast intro: add_less_mono ack_add_bound less_trans)
wenzelm@11024
   285
  done
wenzelm@11024
   286
wenzelm@11024
   287
lemma COMP_case:
wenzelm@11024
   288
  "\<forall>l. g l < ack (kg, list_add l) ==>
wenzelm@11024
   289
  fs \<in> lists(PRIMREC Int {f. \<exists>kf. \<forall>l. f l < ack(kf, list_add l)})
wenzelm@11024
   290
  ==> \<exists>k. \<forall>l. COMP g fs  l < ack(k, list_add l)"
wenzelm@11024
   291
  apply (unfold COMP_def)
wenzelm@11024
   292
  apply (frule Int_lower1 [THEN lists_mono, THEN subsetD])
paulson@16588
   293
    --{*Now, if meson tolerated map, we could finish with
wenzelm@16731
   294
  @{text "(drule COMP_map_aux, meson ack_less_mono2 ack_nest_bound less_trans)"} *}
paulson@16588
   295
  apply (erule COMP_map_aux [THEN exE])
paulson@16588
   296
  apply (rule exI)
paulson@16588
   297
  apply (rule allI)
paulson@16588
   298
  apply (drule spec)+
paulson@16588
   299
  apply (erule less_trans)
paulson@16588
   300
  apply (blast intro: ack_less_mono2 ack_nest_bound less_trans)
wenzelm@11024
   301
  done
wenzelm@11024
   302
wenzelm@11024
   303
wenzelm@11024
   304
text {* @{term PREC} case *}
paulson@3335
   305
wenzelm@11024
   306
lemma PREC_case_aux:
wenzelm@11024
   307
  "\<forall>l. f l + list_add l < ack (kf, list_add l) ==>
wenzelm@11024
   308
    \<forall>l. g l + list_add l < ack (kg, list_add l) ==>
wenzelm@11024
   309
    PREC f g l + list_add l < ack (Suc (kf + kg), list_add l)"
wenzelm@11024
   310
  apply (unfold PREC_def)
wenzelm@11024
   311
  apply (case_tac l)
wenzelm@11024
   312
   apply simp_all
wenzelm@11024
   313
   apply (blast intro: less_trans)
wenzelm@11024
   314
  apply (erule ssubst) -- {* get rid of the needless assumption *}
wenzelm@11024
   315
  apply (induct_tac a)
wenzelm@11024
   316
   apply simp_all
wenzelm@11024
   317
   txt {* base case *}
wenzelm@11024
   318
   apply (blast intro: le_add1 [THEN le_imp_less_Suc, THEN ack_less_mono1] less_trans)
wenzelm@11024
   319
  txt {* induction step *}
wenzelm@11024
   320
  apply (rule Suc_leI [THEN le_less_trans])
wenzelm@11024
   321
   apply (rule le_refl [THEN add_le_mono, THEN le_less_trans])
wenzelm@11024
   322
    prefer 2
wenzelm@11024
   323
    apply (erule spec)
wenzelm@11024
   324
   apply (simp add: le_add2)
wenzelm@11024
   325
  txt {* final part of the simplification *}
wenzelm@11024
   326
  apply simp
wenzelm@11024
   327
  apply (rule le_add2 [THEN ack_le_mono1, THEN le_less_trans])
wenzelm@11024
   328
  apply (erule ack_less_mono2)
wenzelm@11024
   329
  done
wenzelm@11024
   330
wenzelm@11024
   331
lemma PREC_case:
wenzelm@11024
   332
  "\<forall>l. f l < ack (kf, list_add l) ==>
wenzelm@11024
   333
    \<forall>l. g l < ack (kg, list_add l) ==>
wenzelm@11024
   334
    \<exists>k. \<forall>l. PREC f g l < ack (k, list_add l)"
wenzelm@11024
   335
  apply (rule exI)
wenzelm@11024
   336
  apply (rule allI)
wenzelm@11024
   337
  apply (rule le_less_trans [OF le_add1 PREC_case_aux])
wenzelm@11024
   338
   apply (blast intro: ack_add_bound2)+
wenzelm@11024
   339
  done
wenzelm@11024
   340
wenzelm@11024
   341
lemma ack_bounds_PRIMREC: "f \<in> PRIMREC ==> \<exists>k. \<forall>l. f l < ack (k, list_add l)"
wenzelm@11024
   342
  apply (erule PRIMREC.induct)
wenzelm@19676
   343
      apply (blast intro: SC_case CONSTANT_case PROJ_case COMP_case PREC_case)+
wenzelm@11024
   344
  done
wenzelm@11024
   345
wenzelm@11024
   346
lemma ack_not_PRIMREC: "(\<lambda>l. case l of [] => 0 | x # l' => ack (x, x)) \<notin> PRIMREC"
wenzelm@11024
   347
  apply (rule notI)
wenzelm@11024
   348
  apply (erule ack_bounds_PRIMREC [THEN exE])
wenzelm@11024
   349
  apply (rule less_irrefl)
wenzelm@11024
   350
  apply (drule_tac x = "[x]" in spec)
wenzelm@11024
   351
  apply simp
wenzelm@11024
   352
  done
paulson@3335
   353
paulson@3335
   354
end