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