src/HOL/NatDef.ML
author wenzelm
Fri Oct 10 19:02:28 1997 +0200 (1997-10-10)
changeset 3842 b55686a7b22c
parent 3768 67f4ac759100
child 4000 0614fdf0db20
permissions -rw-r--r--
fixed dots;
nipkow@2608
     1
(*  Title:      HOL/NatDef.ML
nipkow@2608
     2
    ID:         $Id$
nipkow@2608
     3
    Author:     Tobias Nipkow, Cambridge University Computer Laboratory
nipkow@2608
     4
    Copyright   1991  University of Cambridge
nipkow@2608
     5
*)
nipkow@2608
     6
nipkow@2608
     7
goal thy "mono(%X. {Zero_Rep} Un (Suc_Rep``X))";
nipkow@2608
     8
by (REPEAT (ares_tac [monoI, subset_refl, image_mono, Un_mono] 1));
nipkow@2608
     9
qed "Nat_fun_mono";
nipkow@2608
    10
nipkow@2608
    11
val Nat_unfold = Nat_fun_mono RS (Nat_def RS def_lfp_Tarski);
nipkow@2608
    12
nipkow@2608
    13
(* Zero is a natural number -- this also justifies the type definition*)
nipkow@2608
    14
goal thy "Zero_Rep: Nat";
nipkow@2608
    15
by (stac Nat_unfold 1);
nipkow@2608
    16
by (rtac (singletonI RS UnI1) 1);
nipkow@2608
    17
qed "Zero_RepI";
nipkow@2608
    18
nipkow@2608
    19
val prems = goal thy "i: Nat ==> Suc_Rep(i) : Nat";
nipkow@2608
    20
by (stac Nat_unfold 1);
nipkow@2608
    21
by (rtac (imageI RS UnI2) 1);
nipkow@2608
    22
by (resolve_tac prems 1);
nipkow@2608
    23
qed "Suc_RepI";
nipkow@2608
    24
nipkow@2608
    25
(*** Induction ***)
nipkow@2608
    26
nipkow@2608
    27
val major::prems = goal thy
nipkow@2608
    28
    "[| i: Nat;  P(Zero_Rep);   \
nipkow@2608
    29
\       !!j. [| j: Nat; P(j) |] ==> P(Suc_Rep(j)) |]  ==> P(i)";
nipkow@2608
    30
by (rtac ([Nat_def, Nat_fun_mono, major] MRS def_induct) 1);
paulson@2891
    31
by (blast_tac (!claset addIs prems) 1);
nipkow@2608
    32
qed "Nat_induct";
nipkow@2608
    33
nipkow@2608
    34
val prems = goalw thy [Zero_def,Suc_def]
nipkow@2608
    35
    "[| P(0);   \
nipkow@3040
    36
\       !!n. P(n) ==> P(Suc(n)) |]  ==> P(n)";
nipkow@2608
    37
by (rtac (Rep_Nat_inverse RS subst) 1);   (*types force good instantiation*)
nipkow@2608
    38
by (rtac (Rep_Nat RS Nat_induct) 1);
nipkow@2608
    39
by (REPEAT (ares_tac prems 1
nipkow@2608
    40
     ORELSE eresolve_tac [Abs_Nat_inverse RS subst] 1));
nipkow@2608
    41
qed "nat_induct";
nipkow@2608
    42
nipkow@2608
    43
(*Perform induction on n. *)
paulson@3563
    44
local fun raw_nat_ind_tac a i = 
paulson@3563
    45
    res_inst_tac [("n",a)] nat_induct i  THEN  rename_last_tac a [""] (i+1)
paulson@3563
    46
in
paulson@3563
    47
val nat_ind_tac = Datatype.occs_in_prems raw_nat_ind_tac
paulson@3563
    48
end;
nipkow@3040
    49
nipkow@2608
    50
(*A special form of induction for reasoning about m<n and m-n*)
nipkow@2608
    51
val prems = goal thy
nipkow@2608
    52
    "[| !!x. P x 0;  \
nipkow@2608
    53
\       !!y. P 0 (Suc y);  \
nipkow@2608
    54
\       !!x y. [| P x y |] ==> P (Suc x) (Suc y)  \
nipkow@2608
    55
\    |] ==> P m n";
nipkow@2608
    56
by (res_inst_tac [("x","m")] spec 1);
nipkow@2608
    57
by (nat_ind_tac "n" 1);
nipkow@2608
    58
by (rtac allI 2);
nipkow@2608
    59
by (nat_ind_tac "x" 2);
nipkow@2608
    60
by (REPEAT (ares_tac (prems@[allI]) 1 ORELSE etac spec 1));
nipkow@2608
    61
qed "diff_induct";
nipkow@2608
    62
nipkow@2608
    63
(*Case analysis on the natural numbers*)
nipkow@2608
    64
val prems = goal thy 
nipkow@2608
    65
    "[| n=0 ==> P;  !!x. n = Suc(x) ==> P |] ==> P";
nipkow@2608
    66
by (subgoal_tac "n=0 | (EX x. n = Suc(x))" 1);
nipkow@2608
    67
by (fast_tac (!claset addSEs prems) 1);
nipkow@2608
    68
by (nat_ind_tac "n" 1);
nipkow@2608
    69
by (rtac (refl RS disjI1) 1);
paulson@2891
    70
by (Blast_tac 1);
nipkow@2608
    71
qed "natE";
nipkow@2608
    72
nipkow@3282
    73
(*Install 'automatic' induction tactic, pretending nat is a datatype *)
nipkow@3292
    74
(*except for induct_tac and exhaust_tac, everything is dummy*)
nipkow@3282
    75
datatypes := [("nat",{case_const = Bound 0, case_rewrites = [],
nipkow@3282
    76
  constructors = [], induct_tac = nat_ind_tac,
nipkow@3292
    77
  exhaustion = natE,
nipkow@3292
    78
  exhaust_tac = fn v => ALLNEWSUBGOALS (res_inst_tac [("n",v)] natE)
nipkow@3292
    79
                                       (rotate_tac ~1),
nipkow@3282
    80
  nchotomy = flexpair_def, case_cong = flexpair_def})];
nipkow@3282
    81
nipkow@3282
    82
nipkow@2608
    83
(*** Isomorphisms: Abs_Nat and Rep_Nat ***)
nipkow@2608
    84
nipkow@2608
    85
(*We can't take these properties as axioms, or take Abs_Nat==Inv(Rep_Nat),
nipkow@2608
    86
  since we assume the isomorphism equations will one day be given by Isabelle*)
nipkow@2608
    87
nipkow@2608
    88
goal thy "inj(Rep_Nat)";
nipkow@2608
    89
by (rtac inj_inverseI 1);
nipkow@2608
    90
by (rtac Rep_Nat_inverse 1);
nipkow@2608
    91
qed "inj_Rep_Nat";
nipkow@2608
    92
nipkow@2608
    93
goal thy "inj_onto Abs_Nat Nat";
nipkow@2608
    94
by (rtac inj_onto_inverseI 1);
nipkow@2608
    95
by (etac Abs_Nat_inverse 1);
nipkow@2608
    96
qed "inj_onto_Abs_Nat";
nipkow@2608
    97
nipkow@2608
    98
(*** Distinctness of constructors ***)
nipkow@2608
    99
nipkow@2608
   100
goalw thy [Zero_def,Suc_def] "Suc(m) ~= 0";
nipkow@2608
   101
by (rtac (inj_onto_Abs_Nat RS inj_onto_contraD) 1);
nipkow@2608
   102
by (rtac Suc_Rep_not_Zero_Rep 1);
nipkow@2608
   103
by (REPEAT (resolve_tac [Rep_Nat, Suc_RepI, Zero_RepI] 1));
nipkow@2608
   104
qed "Suc_not_Zero";
nipkow@2608
   105
nipkow@2608
   106
bind_thm ("Zero_not_Suc", Suc_not_Zero RS not_sym);
nipkow@2608
   107
nipkow@2608
   108
AddIffs [Suc_not_Zero,Zero_not_Suc];
nipkow@2608
   109
nipkow@2608
   110
bind_thm ("Suc_neq_Zero", (Suc_not_Zero RS notE));
nipkow@2608
   111
val Zero_neq_Suc = sym RS Suc_neq_Zero;
nipkow@2608
   112
nipkow@2608
   113
(** Injectiveness of Suc **)
nipkow@2608
   114
nipkow@2608
   115
goalw thy [Suc_def] "inj(Suc)";
nipkow@2608
   116
by (rtac injI 1);
nipkow@2608
   117
by (dtac (inj_onto_Abs_Nat RS inj_ontoD) 1);
nipkow@2608
   118
by (REPEAT (resolve_tac [Rep_Nat, Suc_RepI] 1));
nipkow@2608
   119
by (dtac (inj_Suc_Rep RS injD) 1);
nipkow@2608
   120
by (etac (inj_Rep_Nat RS injD) 1);
nipkow@2608
   121
qed "inj_Suc";
nipkow@2608
   122
nipkow@2608
   123
val Suc_inject = inj_Suc RS injD;
nipkow@2608
   124
nipkow@2608
   125
goal thy "(Suc(m)=Suc(n)) = (m=n)";
nipkow@2608
   126
by (EVERY1 [rtac iffI, etac Suc_inject, etac arg_cong]); 
nipkow@2608
   127
qed "Suc_Suc_eq";
nipkow@2608
   128
nipkow@2608
   129
AddIffs [Suc_Suc_eq];
nipkow@2608
   130
nipkow@2608
   131
goal thy "n ~= Suc(n)";
nipkow@2608
   132
by (nat_ind_tac "n" 1);
nipkow@2608
   133
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   134
qed "n_not_Suc_n";
nipkow@2608
   135
nipkow@2608
   136
bind_thm ("Suc_n_not_n", n_not_Suc_n RS not_sym);
nipkow@2608
   137
paulson@3236
   138
goal thy "!!n. n ~= 0 ==> EX m. n = Suc m";
paulson@3457
   139
by (rtac natE 1);
paulson@3236
   140
by (REPEAT (Blast_tac 1));
paulson@3236
   141
qed "not0_implies_Suc";
paulson@3236
   142
paulson@3236
   143
nipkow@2608
   144
(*** nat_case -- the selection operator for nat ***)
nipkow@2608
   145
nipkow@2608
   146
goalw thy [nat_case_def] "nat_case a f 0 = a";
paulson@2891
   147
by (blast_tac (!claset addIs [select_equality]) 1);
nipkow@2608
   148
qed "nat_case_0";
nipkow@2608
   149
nipkow@2608
   150
goalw thy [nat_case_def] "nat_case a f (Suc k) = f(k)";
paulson@2891
   151
by (blast_tac (!claset addIs [select_equality]) 1);
nipkow@2608
   152
qed "nat_case_Suc";
nipkow@2608
   153
paulson@3236
   154
goalw thy [wf_def, pred_nat_def] "wf(pred_nat)";
paulson@3718
   155
by (Clarify_tac 1);
nipkow@2608
   156
by (nat_ind_tac "x" 1);
paulson@3236
   157
by (ALLGOALS Blast_tac);
nipkow@2608
   158
qed "wf_pred_nat";
nipkow@2608
   159
nipkow@2608
   160
nipkow@2608
   161
(*** nat_rec -- by wf recursion on pred_nat ***)
nipkow@2608
   162
nipkow@2608
   163
(* The unrolling rule for nat_rec *)
nipkow@2608
   164
goal thy
nipkow@2608
   165
   "(%n. nat_rec c d n) = wfrec pred_nat (%f. nat_case ?c (%m. ?d m (f m)))";
nipkow@2608
   166
  by (simp_tac (HOL_ss addsimps [nat_rec_def]) 1);
nipkow@2608
   167
bind_thm("nat_rec_unfold", wf_pred_nat RS 
nipkow@2608
   168
                            ((result() RS eq_reflection) RS def_wfrec));
nipkow@2608
   169
nipkow@2608
   170
(*---------------------------------------------------------------------------
nipkow@2608
   171
 * Old:
nipkow@2608
   172
 * bind_thm ("nat_rec_unfold", (wf_pred_nat RS (nat_rec_def RS def_wfrec))); 
nipkow@2608
   173
 *---------------------------------------------------------------------------*)
nipkow@2608
   174
nipkow@2608
   175
(** conversion rules **)
nipkow@2608
   176
nipkow@2608
   177
goal thy "nat_rec c h 0 = c";
nipkow@2608
   178
by (rtac (nat_rec_unfold RS trans) 1);
nipkow@2608
   179
by (simp_tac (!simpset addsimps [nat_case_0]) 1);
nipkow@2608
   180
qed "nat_rec_0";
nipkow@2608
   181
nipkow@2608
   182
goal thy "nat_rec c h (Suc n) = h n (nat_rec c h n)";
nipkow@2608
   183
by (rtac (nat_rec_unfold RS trans) 1);
paulson@3236
   184
by (simp_tac (!simpset addsimps [nat_case_Suc, pred_nat_def, cut_apply]) 1);
nipkow@2608
   185
qed "nat_rec_Suc";
nipkow@2608
   186
nipkow@2608
   187
(*These 2 rules ease the use of primitive recursion.  NOTE USE OF == *)
nipkow@2608
   188
val [rew] = goal thy
nipkow@2608
   189
    "[| !!n. f(n) == nat_rec c h n |] ==> f(0) = c";
nipkow@2608
   190
by (rewtac rew);
nipkow@2608
   191
by (rtac nat_rec_0 1);
nipkow@2608
   192
qed "def_nat_rec_0";
nipkow@2608
   193
nipkow@2608
   194
val [rew] = goal thy
nipkow@2608
   195
    "[| !!n. f(n) == nat_rec c h n |] ==> f(Suc(n)) = h n (f n)";
nipkow@2608
   196
by (rewtac rew);
nipkow@2608
   197
by (rtac nat_rec_Suc 1);
nipkow@2608
   198
qed "def_nat_rec_Suc";
nipkow@2608
   199
nipkow@2608
   200
fun nat_recs def =
nipkow@2608
   201
      [standard (def RS def_nat_rec_0),
nipkow@2608
   202
       standard (def RS def_nat_rec_Suc)];
nipkow@2608
   203
nipkow@2608
   204
nipkow@2608
   205
(*** Basic properties of "less than" ***)
nipkow@2608
   206
paulson@3378
   207
(*Used in TFL/post.sml*)
paulson@3378
   208
goalw thy [less_def] "(m,n) : pred_nat^+ = (m<n)";
paulson@3378
   209
by (rtac refl 1);
paulson@3378
   210
qed "less_eq";
paulson@3378
   211
nipkow@2608
   212
(** Introduction properties **)
nipkow@2608
   213
nipkow@2608
   214
val prems = goalw thy [less_def] "[| i<j;  j<k |] ==> i<(k::nat)";
nipkow@2608
   215
by (rtac (trans_trancl RS transD) 1);
nipkow@2608
   216
by (resolve_tac prems 1);
nipkow@2608
   217
by (resolve_tac prems 1);
nipkow@2608
   218
qed "less_trans";
nipkow@2608
   219
paulson@3236
   220
goalw thy [less_def, pred_nat_def] "n < Suc(n)";
paulson@3236
   221
by (simp_tac (!simpset addsimps [r_into_trancl]) 1);
nipkow@2608
   222
qed "lessI";
nipkow@2608
   223
AddIffs [lessI];
nipkow@2608
   224
nipkow@2608
   225
(* i<j ==> i<Suc(j) *)
nipkow@2608
   226
bind_thm("less_SucI", lessI RSN (2, less_trans));
nipkow@2608
   227
Addsimps [less_SucI];
nipkow@2608
   228
nipkow@2608
   229
goal thy "0 < Suc(n)";
nipkow@2608
   230
by (nat_ind_tac "n" 1);
nipkow@2608
   231
by (rtac lessI 1);
nipkow@2608
   232
by (etac less_trans 1);
nipkow@2608
   233
by (rtac lessI 1);
nipkow@2608
   234
qed "zero_less_Suc";
nipkow@2608
   235
AddIffs [zero_less_Suc];
nipkow@2608
   236
nipkow@2608
   237
(** Elimination properties **)
nipkow@2608
   238
nipkow@2608
   239
val prems = goalw thy [less_def] "n<m ==> ~ m<(n::nat)";
paulson@2891
   240
by (blast_tac (!claset addIs ([wf_pred_nat, wf_trancl RS wf_asym]@prems))1);
nipkow@2608
   241
qed "less_not_sym";
nipkow@2608
   242
nipkow@2608
   243
(* [| n<m; m<n |] ==> R *)
nipkow@2608
   244
bind_thm ("less_asym", (less_not_sym RS notE));
nipkow@2608
   245
nipkow@2608
   246
goalw thy [less_def] "~ n<(n::nat)";
nipkow@2608
   247
by (rtac notI 1);
nipkow@2608
   248
by (etac (wf_pred_nat RS wf_trancl RS wf_irrefl) 1);
nipkow@2608
   249
qed "less_not_refl";
nipkow@2608
   250
nipkow@2608
   251
(* n<n ==> R *)
nipkow@2608
   252
bind_thm ("less_irrefl", (less_not_refl RS notE));
nipkow@2608
   253
nipkow@2608
   254
goal thy "!!m. n<m ==> m ~= (n::nat)";
paulson@3085
   255
by (blast_tac (!claset addSEs [less_irrefl]) 1);
nipkow@2608
   256
qed "less_not_refl2";
nipkow@2608
   257
nipkow@2608
   258
paulson@3236
   259
val major::prems = goalw thy [less_def, pred_nat_def]
nipkow@2608
   260
    "[| i<k;  k=Suc(i) ==> P;  !!j. [| i<j;  k=Suc(j) |] ==> P \
nipkow@2608
   261
\    |] ==> P";
nipkow@2608
   262
by (rtac (major RS tranclE) 1);
paulson@3236
   263
by (ALLGOALS Full_simp_tac); 
nipkow@2608
   264
by (REPEAT_FIRST (bound_hyp_subst_tac ORELSE'
paulson@3236
   265
                  eresolve_tac (prems@[asm_rl, Pair_inject])));
nipkow@2608
   266
qed "lessE";
nipkow@2608
   267
nipkow@2608
   268
goal thy "~ n<0";
nipkow@2608
   269
by (rtac notI 1);
nipkow@2608
   270
by (etac lessE 1);
nipkow@2608
   271
by (etac Zero_neq_Suc 1);
nipkow@2608
   272
by (etac Zero_neq_Suc 1);
nipkow@2608
   273
qed "not_less0";
nipkow@2608
   274
nipkow@2608
   275
AddIffs [not_less0];
nipkow@2608
   276
nipkow@2608
   277
(* n<0 ==> R *)
nipkow@2608
   278
bind_thm ("less_zeroE", not_less0 RS notE);
nipkow@2608
   279
nipkow@2608
   280
val [major,less,eq] = goal thy
nipkow@2608
   281
    "[| m < Suc(n);  m<n ==> P;  m=n ==> P |] ==> P";
nipkow@2608
   282
by (rtac (major RS lessE) 1);
nipkow@2608
   283
by (rtac eq 1);
paulson@2891
   284
by (Blast_tac 1);
nipkow@2608
   285
by (rtac less 1);
paulson@2891
   286
by (Blast_tac 1);
nipkow@2608
   287
qed "less_SucE";
nipkow@2608
   288
nipkow@2608
   289
goal thy "(m < Suc(n)) = (m < n | m = n)";
paulson@2935
   290
by (blast_tac (!claset addSEs [less_SucE] addIs [less_trans]) 1);
nipkow@2608
   291
qed "less_Suc_eq";
nipkow@2608
   292
nipkow@3484
   293
goal thy "(n<1) = (n=0)";
nipkow@3484
   294
by (simp_tac (!simpset addsimps [less_Suc_eq]) 1);
nipkow@3484
   295
qed "less_one";
nipkow@3484
   296
AddIffs [less_one];
nipkow@3484
   297
nipkow@2608
   298
val prems = goal thy "m<n ==> n ~= 0";
nipkow@2608
   299
by (res_inst_tac [("n","n")] natE 1);
nipkow@2608
   300
by (cut_facts_tac prems 1);
nipkow@2608
   301
by (ALLGOALS Asm_full_simp_tac);
nipkow@2608
   302
qed "gr_implies_not0";
nipkow@2608
   303
Addsimps [gr_implies_not0];
nipkow@2608
   304
nipkow@2608
   305
qed_goal "zero_less_eq" thy "0 < n = (n ~= 0)" (fn _ => [
nipkow@2608
   306
        rtac iffI 1,
nipkow@2608
   307
        etac gr_implies_not0 1,
nipkow@2608
   308
        rtac natE 1,
nipkow@2608
   309
        contr_tac 1,
nipkow@2608
   310
        etac ssubst 1,
nipkow@2608
   311
        rtac zero_less_Suc 1]);
nipkow@2608
   312
nipkow@2608
   313
(** Inductive (?) properties **)
nipkow@2608
   314
nipkow@2608
   315
val [prem] = goal thy "Suc(m) < n ==> m<n";
nipkow@2608
   316
by (rtac (prem RS rev_mp) 1);
nipkow@2608
   317
by (nat_ind_tac "n" 1);
nipkow@2608
   318
by (ALLGOALS (fast_tac (!claset addSIs [lessI RS less_SucI]
nipkow@2608
   319
                                addEs  [less_trans, lessE])));
nipkow@2608
   320
qed "Suc_lessD";
nipkow@2608
   321
nipkow@2608
   322
val [major,minor] = goal thy 
nipkow@2608
   323
    "[| Suc(i)<k;  !!j. [| i<j;  k=Suc(j) |] ==> P \
nipkow@2608
   324
\    |] ==> P";
nipkow@2608
   325
by (rtac (major RS lessE) 1);
nipkow@2608
   326
by (etac (lessI RS minor) 1);
nipkow@2608
   327
by (etac (Suc_lessD RS minor) 1);
nipkow@2608
   328
by (assume_tac 1);
nipkow@2608
   329
qed "Suc_lessE";
nipkow@2608
   330
nipkow@2608
   331
goal thy "!!m n. Suc(m) < Suc(n) ==> m<n";
paulson@2891
   332
by (blast_tac (!claset addEs [lessE, make_elim Suc_lessD]) 1);
nipkow@2608
   333
qed "Suc_less_SucD";
nipkow@2608
   334
nipkow@2608
   335
goal thy "!!m n. m<n ==> Suc(m) < Suc(n)";
nipkow@2608
   336
by (etac rev_mp 1);
nipkow@2608
   337
by (nat_ind_tac "n" 1);
paulson@2891
   338
by (ALLGOALS (fast_tac (!claset addEs  [less_trans, lessE])));
nipkow@2608
   339
qed "Suc_mono";
nipkow@2608
   340
nipkow@2608
   341
nipkow@2608
   342
goal thy "(Suc(m) < Suc(n)) = (m<n)";
nipkow@2608
   343
by (EVERY1 [rtac iffI, etac Suc_less_SucD, etac Suc_mono]);
nipkow@2608
   344
qed "Suc_less_eq";
nipkow@2608
   345
Addsimps [Suc_less_eq];
nipkow@2608
   346
nipkow@2608
   347
goal thy "~(Suc(n) < n)";
paulson@2891
   348
by (blast_tac (!claset addEs [Suc_lessD RS less_irrefl]) 1);
nipkow@2608
   349
qed "not_Suc_n_less_n";
nipkow@2608
   350
Addsimps [not_Suc_n_less_n];
nipkow@2608
   351
nipkow@2608
   352
goal thy "!!i. i<j ==> j<k --> Suc i < k";
nipkow@2608
   353
by (nat_ind_tac "k" 1);
nipkow@2608
   354
by (ALLGOALS (asm_simp_tac (!simpset)));
nipkow@2608
   355
by (asm_simp_tac (!simpset addsimps [less_Suc_eq]) 1);
paulson@2891
   356
by (blast_tac (!claset addDs [Suc_lessD]) 1);
nipkow@2608
   357
qed_spec_mp "less_trans_Suc";
nipkow@2608
   358
nipkow@2608
   359
(*"Less than" is a linear ordering*)
nipkow@2608
   360
goal thy "m<n | m=n | n<(m::nat)";
nipkow@2608
   361
by (nat_ind_tac "m" 1);
nipkow@2608
   362
by (nat_ind_tac "n" 1);
nipkow@2608
   363
by (rtac (refl RS disjI1 RS disjI2) 1);
nipkow@2608
   364
by (rtac (zero_less_Suc RS disjI1) 1);
paulson@2935
   365
by (blast_tac (!claset addIs [Suc_mono, less_SucI] addEs [lessE]) 1);
nipkow@2608
   366
qed "less_linear";
nipkow@2608
   367
nipkow@2608
   368
qed_goal "nat_less_cases" thy 
nipkow@2608
   369
   "[| (m::nat)<n ==> P n m; m=n ==> P n m; n<m ==> P n m |] ==> P n m"
paulson@2935
   370
( fn [major,eqCase,lessCase] =>
nipkow@2608
   371
        [
paulson@2935
   372
        (rtac (less_linear RS disjE) 1),
nipkow@2608
   373
        (etac disjE 2),
paulson@2935
   374
        (etac lessCase 1),
paulson@2935
   375
        (etac (sym RS eqCase) 1),
paulson@2935
   376
        (etac major 1)
nipkow@2608
   377
        ]);
nipkow@2608
   378
nipkow@2608
   379
(*Can be used with less_Suc_eq to get n=m | n<m *)
nipkow@2608
   380
goal thy "(~ m < n) = (n < Suc(m))";
nipkow@2608
   381
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
nipkow@2608
   382
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   383
qed "not_less_eq";
nipkow@2608
   384
nipkow@2608
   385
(*Complete induction, aka course-of-values induction*)
nipkow@2608
   386
val prems = goalw thy [less_def]
nipkow@2608
   387
    "[| !!n. [| ! m::nat. m<n --> P(m) |] ==> P(n) |]  ==>  P(n)";
nipkow@2608
   388
by (wf_ind_tac "n" [wf_pred_nat RS wf_trancl] 1);
nipkow@2608
   389
by (eresolve_tac prems 1);
nipkow@2608
   390
qed "less_induct";
nipkow@2608
   391
nipkow@2608
   392
qed_goal "nat_induct2" thy 
nipkow@2608
   393
"[| P 0; P 1; !!k. P k ==> P (Suc (Suc k)) |] ==> P n" (fn prems => [
paulson@3023
   394
        cut_facts_tac prems 1,
paulson@3023
   395
        rtac less_induct 1,
paulson@3023
   396
        res_inst_tac [("n","n")] natE 1,
paulson@3023
   397
         hyp_subst_tac 1,
paulson@3023
   398
         atac 1,
paulson@3023
   399
        hyp_subst_tac 1,
paulson@3023
   400
        res_inst_tac [("n","x")] natE 1,
paulson@3023
   401
         hyp_subst_tac 1,
paulson@3023
   402
         atac 1,
paulson@3023
   403
        hyp_subst_tac 1,
paulson@3023
   404
        resolve_tac prems 1,
paulson@3023
   405
        dtac spec 1,
paulson@3023
   406
        etac mp 1,
paulson@3023
   407
        rtac (lessI RS less_trans) 1,
paulson@3023
   408
        rtac (lessI RS Suc_mono) 1]);
nipkow@2608
   409
nipkow@2608
   410
(*** Properties of <= ***)
nipkow@2608
   411
nipkow@2608
   412
goalw thy [le_def] "(m <= n) = (m < Suc n)";
nipkow@2608
   413
by (rtac not_less_eq 1);
nipkow@2608
   414
qed "le_eq_less_Suc";
nipkow@2608
   415
paulson@3343
   416
(*  m<=n ==> m < Suc n  *)
paulson@3343
   417
bind_thm ("le_imp_less_Suc", le_eq_less_Suc RS iffD1);
paulson@3343
   418
nipkow@2608
   419
goalw thy [le_def] "0 <= n";
nipkow@2608
   420
by (rtac not_less0 1);
nipkow@2608
   421
qed "le0";
nipkow@2608
   422
nipkow@2608
   423
goalw thy [le_def] "~ Suc n <= n";
nipkow@2608
   424
by (Simp_tac 1);
nipkow@2608
   425
qed "Suc_n_not_le_n";
nipkow@2608
   426
nipkow@2608
   427
goalw thy [le_def] "(i <= 0) = (i = 0)";
nipkow@2608
   428
by (nat_ind_tac "i" 1);
nipkow@2608
   429
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   430
qed "le_0_eq";
nipkow@2608
   431
nipkow@2608
   432
Addsimps [(*less_Suc_eq, makes simpset non-confluent*) le0, le_0_eq,
nipkow@2608
   433
          Suc_n_not_le_n,
nipkow@2608
   434
          n_not_Suc_n, Suc_n_not_n,
nipkow@2608
   435
          nat_case_0, nat_case_Suc, nat_rec_0, nat_rec_Suc];
nipkow@2608
   436
paulson@3355
   437
goal thy "!!m. (m <= Suc(n)) = (m<=n | m = Suc n)";
paulson@3355
   438
by (simp_tac (!simpset addsimps [le_eq_less_Suc]) 1);
paulson@3355
   439
by (blast_tac (!claset addSEs [less_SucE] addIs [less_SucI]) 1);
paulson@3355
   440
qed "le_Suc_eq";
paulson@3355
   441
nipkow@2608
   442
(*
nipkow@2608
   443
goal thy "(Suc m < n | Suc m = n) = (m < n)";
nipkow@2608
   444
by (stac (less_Suc_eq RS sym) 1);
nipkow@2608
   445
by (rtac Suc_less_eq 1);
nipkow@2608
   446
qed "Suc_le_eq";
nipkow@2608
   447
nipkow@2608
   448
this could make the simpset (with less_Suc_eq added again) more confluent,
nipkow@2608
   449
but less_Suc_eq makes additional problems with terms of the form 0 < Suc (...)
nipkow@2608
   450
*)
nipkow@2608
   451
nipkow@2608
   452
(*Prevents simplification of f and g: much faster*)
nipkow@2608
   453
qed_goal "nat_case_weak_cong" thy
nipkow@2608
   454
  "m=n ==> nat_case a f m = nat_case a f n"
nipkow@2608
   455
  (fn [prem] => [rtac (prem RS arg_cong) 1]);
nipkow@2608
   456
nipkow@2608
   457
qed_goal "nat_rec_weak_cong" thy
nipkow@2608
   458
  "m=n ==> nat_rec a f m = nat_rec a f n"
nipkow@2608
   459
  (fn [prem] => [rtac (prem RS arg_cong) 1]);
nipkow@2608
   460
nipkow@2608
   461
qed_goal "expand_nat_case" thy
nipkow@2608
   462
  "P(nat_case z s n) = ((n=0 --> P z) & (!m. n = Suc m --> P(s m)))"
nipkow@2608
   463
  (fn _ => [nat_ind_tac "n" 1, ALLGOALS Asm_simp_tac]);
nipkow@2608
   464
nipkow@2608
   465
val prems = goalw thy [le_def] "~n<m ==> m<=(n::nat)";
nipkow@2608
   466
by (resolve_tac prems 1);
nipkow@2608
   467
qed "leI";
nipkow@2608
   468
nipkow@2608
   469
val prems = goalw thy [le_def] "m<=n ==> ~ n < (m::nat)";
nipkow@2608
   470
by (resolve_tac prems 1);
nipkow@2608
   471
qed "leD";
nipkow@2608
   472
nipkow@2608
   473
val leE = make_elim leD;
nipkow@2608
   474
nipkow@2608
   475
goal thy "(~n<m) = (m<=(n::nat))";
paulson@2891
   476
by (blast_tac (!claset addIs [leI] addEs [leE]) 1);
nipkow@2608
   477
qed "not_less_iff_le";
nipkow@2608
   478
nipkow@2608
   479
goalw thy [le_def] "!!m. ~ m <= n ==> n<(m::nat)";
paulson@2891
   480
by (Blast_tac 1);
nipkow@2608
   481
qed "not_leE";
nipkow@2608
   482
nipkow@2608
   483
goalw thy [le_def] "!!m. m < n ==> Suc(m) <= n";
nipkow@2608
   484
by (simp_tac (!simpset addsimps [less_Suc_eq]) 1);
paulson@3085
   485
by (blast_tac (!claset addSEs [less_irrefl,less_asym]) 1);
paulson@3343
   486
qed "Suc_leI";  (*formerly called lessD*)
nipkow@2608
   487
nipkow@2608
   488
goalw thy [le_def] "!!m. Suc(m) <= n ==> m <= n";
nipkow@2608
   489
by (asm_full_simp_tac (!simpset addsimps [less_Suc_eq]) 1);
nipkow@2608
   490
qed "Suc_leD";
nipkow@2608
   491
nipkow@2608
   492
(* stronger version of Suc_leD *)
nipkow@2608
   493
goalw thy [le_def] 
nipkow@2608
   494
        "!!m. Suc m <= n ==> m < n";
nipkow@2608
   495
by (asm_full_simp_tac (!simpset addsimps [less_Suc_eq]) 1);
nipkow@2608
   496
by (cut_facts_tac [less_linear] 1);
paulson@2891
   497
by (Blast_tac 1);
nipkow@2608
   498
qed "Suc_le_lessD";
nipkow@2608
   499
nipkow@2608
   500
goal thy "(Suc m <= n) = (m < n)";
paulson@3343
   501
by (blast_tac (!claset addIs [Suc_leI, Suc_le_lessD]) 1);
nipkow@2608
   502
qed "Suc_le_eq";
nipkow@2608
   503
nipkow@2608
   504
goalw thy [le_def] "!!m. m <= n ==> m <= Suc n";
paulson@2891
   505
by (blast_tac (!claset addDs [Suc_lessD]) 1);
nipkow@2608
   506
qed "le_SucI";
nipkow@2608
   507
Addsimps[le_SucI];
nipkow@2608
   508
nipkow@2608
   509
bind_thm ("le_Suc", not_Suc_n_less_n RS leI);
nipkow@2608
   510
nipkow@2608
   511
goalw thy [le_def] "!!m. m < n ==> m <= (n::nat)";
paulson@2891
   512
by (blast_tac (!claset addEs [less_asym]) 1);
nipkow@2608
   513
qed "less_imp_le";
nipkow@2608
   514
paulson@3343
   515
(** Equivalence of m<=n and  m<n | m=n **)
paulson@3343
   516
nipkow@2608
   517
goalw thy [le_def] "!!m. m <= n ==> m < n | m=(n::nat)";
nipkow@2608
   518
by (cut_facts_tac [less_linear] 1);
paulson@2891
   519
by (blast_tac (!claset addEs [less_irrefl,less_asym]) 1);
nipkow@2608
   520
qed "le_imp_less_or_eq";
nipkow@2608
   521
nipkow@2608
   522
goalw thy [le_def] "!!m. m<n | m=n ==> m <=(n::nat)";
nipkow@2608
   523
by (cut_facts_tac [less_linear] 1);
paulson@3085
   524
by (blast_tac (!claset addSEs [less_irrefl] addEs [less_asym]) 1);
nipkow@2608
   525
qed "less_or_eq_imp_le";
nipkow@2608
   526
paulson@3343
   527
goal thy "(m <= (n::nat)) = (m < n | m=n)";
nipkow@2608
   528
by (REPEAT(ares_tac [iffI,less_or_eq_imp_le,le_imp_less_or_eq] 1));
nipkow@2608
   529
qed "le_eq_less_or_eq";
nipkow@2608
   530
nipkow@2608
   531
goal thy "n <= (n::nat)";
nipkow@2608
   532
by (simp_tac (!simpset addsimps [le_eq_less_or_eq]) 1);
nipkow@2608
   533
qed "le_refl";
nipkow@2608
   534
nipkow@2608
   535
val prems = goal thy "!!i. [| i <= j; j < k |] ==> i < (k::nat)";
nipkow@2608
   536
by (dtac le_imp_less_or_eq 1);
paulson@2935
   537
by (blast_tac (!claset addIs [less_trans]) 1);
nipkow@2608
   538
qed "le_less_trans";
nipkow@2608
   539
nipkow@2608
   540
goal thy "!!i. [| i < j; j <= k |] ==> i < (k::nat)";
nipkow@2608
   541
by (dtac le_imp_less_or_eq 1);
paulson@2935
   542
by (blast_tac (!claset addIs [less_trans]) 1);
nipkow@2608
   543
qed "less_le_trans";
nipkow@2608
   544
nipkow@2608
   545
goal thy "!!i. [| i <= j; j <= k |] ==> i <= (k::nat)";
paulson@2891
   546
by (EVERY1[dtac le_imp_less_or_eq, 
paulson@3023
   547
           dtac le_imp_less_or_eq,
paulson@3023
   548
           rtac less_or_eq_imp_le, 
paulson@3023
   549
           blast_tac (!claset addIs [less_trans])]);
nipkow@2608
   550
qed "le_trans";
nipkow@2608
   551
paulson@2891
   552
goal thy "!!m. [| m <= n; n <= m |] ==> m = (n::nat)";
paulson@2891
   553
by (EVERY1[dtac le_imp_less_or_eq, 
paulson@3023
   554
           dtac le_imp_less_or_eq,
paulson@3023
   555
           blast_tac (!claset addEs [less_irrefl,less_asym])]);
nipkow@2608
   556
qed "le_anti_sym";
nipkow@2608
   557
nipkow@2608
   558
goal thy "(Suc(n) <= Suc(m)) = (n <= m)";
nipkow@2608
   559
by (simp_tac (!simpset addsimps [le_eq_less_or_eq]) 1);
nipkow@2608
   560
qed "Suc_le_mono";
nipkow@2608
   561
nipkow@2608
   562
AddIffs [Suc_le_mono];
nipkow@2608
   563
nipkow@2608
   564
(* Axiom 'order_le_less' of class 'order': *)
nipkow@2608
   565
goal thy "(m::nat) < n = (m <= n & m ~= n)";
paulson@3023
   566
by (rtac iffI 1);
paulson@3023
   567
 by (rtac conjI 1);
paulson@3023
   568
  by (etac less_imp_le 1);
paulson@3023
   569
 by (etac (less_not_refl2 RS not_sym) 1);
paulson@3023
   570
by (blast_tac (!claset addSDs [le_imp_less_or_eq]) 1);
nipkow@2608
   571
qed "nat_less_le";
nipkow@2608
   572
nipkow@2608
   573
(** LEAST -- the least number operator **)
nipkow@2608
   574
nipkow@3143
   575
goal thy "(! m::nat. P m --> n <= m) = (! m. m < n --> ~ P m)";
paulson@3457
   576
by (blast_tac (!claset addIs [leI] addEs [leE]) 1);
nipkow@3143
   577
val lemma = result();
nipkow@3143
   578
nipkow@3143
   579
(* This is an old def of Least for nat, which is derived for compatibility *)
nipkow@3143
   580
goalw thy [Least_def]
nipkow@3143
   581
  "(LEAST n::nat. P n) == (@n. P(n) & (ALL m. m < n --> ~P(m)))";
paulson@3457
   582
by (simp_tac (!simpset addsimps [lemma]) 1);
paulson@3457
   583
by (rtac eq_reflection 1);
paulson@3457
   584
by (rtac refl 1);
nipkow@3143
   585
qed "Least_nat_def";
nipkow@3143
   586
nipkow@3143
   587
val [prem1,prem2] = goalw thy [Least_nat_def]
wenzelm@3842
   588
    "[| P(k::nat);  !!x. x<k ==> ~P(x) |] ==> (LEAST x. P(x)) = k";
nipkow@2608
   589
by (rtac select_equality 1);
paulson@2891
   590
by (blast_tac (!claset addSIs [prem1,prem2]) 1);
nipkow@2608
   591
by (cut_facts_tac [less_linear] 1);
paulson@2891
   592
by (blast_tac (!claset addSIs [prem1] addSDs [prem2]) 1);
nipkow@2608
   593
qed "Least_equality";
nipkow@2608
   594
wenzelm@3842
   595
val [prem] = goal thy "P(k::nat) ==> P(LEAST x. P(x))";
nipkow@2608
   596
by (rtac (prem RS rev_mp) 1);
nipkow@2608
   597
by (res_inst_tac [("n","k")] less_induct 1);
nipkow@2608
   598
by (rtac impI 1);
nipkow@2608
   599
by (rtac classical 1);
nipkow@2608
   600
by (res_inst_tac [("s","n")] (Least_equality RS ssubst) 1);
nipkow@2608
   601
by (assume_tac 1);
nipkow@2608
   602
by (assume_tac 2);
paulson@2891
   603
by (Blast_tac 1);
nipkow@2608
   604
qed "LeastI";
nipkow@2608
   605
nipkow@2608
   606
(*Proof is almost identical to the one above!*)
wenzelm@3842
   607
val [prem] = goal thy "P(k::nat) ==> (LEAST x. P(x)) <= k";
nipkow@2608
   608
by (rtac (prem RS rev_mp) 1);
nipkow@2608
   609
by (res_inst_tac [("n","k")] less_induct 1);
nipkow@2608
   610
by (rtac impI 1);
nipkow@2608
   611
by (rtac classical 1);
nipkow@2608
   612
by (res_inst_tac [("s","n")] (Least_equality RS ssubst) 1);
nipkow@2608
   613
by (assume_tac 1);
nipkow@2608
   614
by (rtac le_refl 2);
paulson@2891
   615
by (blast_tac (!claset addIs [less_imp_le,le_trans]) 1);
nipkow@2608
   616
qed "Least_le";
nipkow@2608
   617
wenzelm@3842
   618
val [prem] = goal thy "k < (LEAST x. P(x)) ==> ~P(k::nat)";
nipkow@2608
   619
by (rtac notI 1);
nipkow@2608
   620
by (etac (rewrite_rule [le_def] Least_le RS notE) 1);
nipkow@2608
   621
by (rtac prem 1);
nipkow@2608
   622
qed "not_less_Least";
nipkow@2608
   623
nipkow@3143
   624
qed_goalw "Least_Suc" thy [Least_nat_def]
nipkow@2608
   625
 "!!P. [| ? n. P(Suc n); ~ P 0 |] ==> (LEAST n. P n) = Suc (LEAST m. P(Suc m))"
nipkow@2608
   626
 (fn _ => [
nipkow@2608
   627
        rtac select_equality 1,
nipkow@3143
   628
        fold_goals_tac [Least_nat_def],
nipkow@2608
   629
        safe_tac (!claset addSEs [LeastI]),
nipkow@2608
   630
        rename_tac "j" 1,
nipkow@2608
   631
        res_inst_tac [("n","j")] natE 1,
paulson@2891
   632
        Blast_tac 1,
paulson@2891
   633
        blast_tac (!claset addDs [Suc_less_SucD, not_less_Least]) 1,
nipkow@2608
   634
        rename_tac "k n" 1,
nipkow@2608
   635
        res_inst_tac [("n","k")] natE 1,
paulson@2891
   636
        Blast_tac 1,
nipkow@2608
   637
        hyp_subst_tac 1,
nipkow@3143
   638
        rewtac Least_nat_def,
nipkow@2608
   639
        rtac (select_equality RS arg_cong RS sym) 1,
nipkow@2608
   640
        safe_tac (!claset),
nipkow@2608
   641
        dtac Suc_mono 1,
paulson@2891
   642
        Blast_tac 1,
nipkow@2608
   643
        cut_facts_tac [less_linear] 1,
nipkow@2608
   644
        safe_tac (!claset),
nipkow@2608
   645
        atac 2,
paulson@2891
   646
        Blast_tac 2,
nipkow@2608
   647
        dtac Suc_mono 1,
paulson@2891
   648
        Blast_tac 1]);
nipkow@2608
   649
nipkow@2608
   650
nipkow@2608
   651
(*** Instantiation of transitivity prover ***)
nipkow@2608
   652
nipkow@2608
   653
structure Less_Arith =
nipkow@2608
   654
struct
nipkow@2608
   655
val nat_leI = leI;
nipkow@2608
   656
val nat_leD = leD;
nipkow@2608
   657
val lessI = lessI;
nipkow@2608
   658
val zero_less_Suc = zero_less_Suc;
nipkow@2608
   659
val less_reflE = less_irrefl;
nipkow@2608
   660
val less_zeroE = less_zeroE;
nipkow@2608
   661
val less_incr = Suc_mono;
nipkow@2608
   662
val less_decr = Suc_less_SucD;
nipkow@2608
   663
val less_incr_rhs = Suc_mono RS Suc_lessD;
nipkow@2608
   664
val less_decr_lhs = Suc_lessD;
nipkow@2608
   665
val less_trans_Suc = less_trans_Suc;
paulson@3343
   666
val leI = Suc_leI RS (Suc_le_mono RS iffD1);
nipkow@2608
   667
val not_lessI = leI RS leD
nipkow@2608
   668
val not_leI = prove_goal thy "!!m::nat. n < m ==> ~ m <= n"
nipkow@2608
   669
  (fn _ => [etac swap2 1, etac leD 1]);
nipkow@2608
   670
val eqI = prove_goal thy "!!m. [| m < Suc n; n < Suc m |] ==> m=n"
nipkow@2608
   671
  (fn _ => [etac less_SucE 1,
paulson@2935
   672
            blast_tac (!claset addSDs [Suc_less_SucD] addSEs [less_irrefl]
paulson@2891
   673
                              addDs [less_trans_Suc]) 1,
paulson@2935
   674
            assume_tac 1]);
nipkow@2608
   675
val leD = le_eq_less_Suc RS iffD1;
nipkow@2608
   676
val not_lessD = nat_leI RS leD;
nipkow@2608
   677
val not_leD = not_leE
nipkow@2608
   678
val eqD1 = prove_goal thy  "!!n. m = n ==> m < Suc n"
nipkow@2608
   679
 (fn _ => [etac subst 1, rtac lessI 1]);
nipkow@2608
   680
val eqD2 = sym RS eqD1;
nipkow@2608
   681
nipkow@2608
   682
fun is_zero(t) =  t = Const("0",Type("nat",[]));
nipkow@2608
   683
nipkow@2608
   684
fun nnb T = T = Type("fun",[Type("nat",[]),
nipkow@2608
   685
                            Type("fun",[Type("nat",[]),
nipkow@2608
   686
                                        Type("bool",[])])])
nipkow@2608
   687
nipkow@2608
   688
fun decomp_Suc(Const("Suc",_)$t) = let val (a,i) = decomp_Suc t in (a,i+1) end
nipkow@2608
   689
  | decomp_Suc t = (t,0);
nipkow@2608
   690
nipkow@2608
   691
fun decomp2(rel,T,lhs,rhs) =
nipkow@2608
   692
  if not(nnb T) then None else
nipkow@2608
   693
  let val (x,i) = decomp_Suc lhs
nipkow@2608
   694
      val (y,j) = decomp_Suc rhs
nipkow@2608
   695
  in case rel of
nipkow@2608
   696
       "op <"  => Some(x,i,"<",y,j)
nipkow@2608
   697
     | "op <=" => Some(x,i,"<=",y,j)
nipkow@2608
   698
     | "op ="  => Some(x,i,"=",y,j)
nipkow@2608
   699
     | _       => None
nipkow@2608
   700
  end;
nipkow@2608
   701
nipkow@2608
   702
fun negate(Some(x,i,rel,y,j)) = Some(x,i,"~"^rel,y,j)
nipkow@2608
   703
  | negate None = None;
nipkow@2608
   704
nipkow@2608
   705
fun decomp(_$(Const(rel,T)$lhs$rhs)) = decomp2(rel,T,lhs,rhs)
paulson@2718
   706
  | decomp(_$(Const("Not",_)$(Const(rel,T)$lhs$rhs))) =
nipkow@2608
   707
      negate(decomp2(rel,T,lhs,rhs))
nipkow@2608
   708
  | decomp _ = None
nipkow@2608
   709
nipkow@2608
   710
end;
nipkow@2608
   711
nipkow@2608
   712
structure Trans_Tac = Trans_Tac_Fun(Less_Arith);
nipkow@2608
   713
nipkow@2608
   714
open Trans_Tac;
nipkow@2608
   715
nipkow@2608
   716
(*** eliminates ~= in premises, which trans_tac cannot deal with ***)
nipkow@2608
   717
qed_goal "nat_neqE" thy
nipkow@2608
   718
  "[| (m::nat) ~= n; m < n ==> P; n < m ==> P |] ==> P"
nipkow@2608
   719
  (fn major::prems => [cut_facts_tac [less_linear] 1,
nipkow@2608
   720
                       REPEAT(eresolve_tac ([disjE,major RS notE]@prems) 1)]);
pusch@2680
   721
pusch@2680
   722
pusch@2680
   723
pusch@2680
   724
(* add function nat_add_primrec *) 
nipkow@3308
   725
val (_, nat_add_primrec, _) = Datatype.add_datatype
nipkow@3308
   726
([], "nat", [("0", [], Mixfix ("0", [], max_pri)), ("Suc", [dtTyp ([],
wenzelm@3768
   727
"nat")], NoSyn)]) (Theory.add_name "Arith" HOL.thy);
wenzelm@3768
   728
(*pretend Arith is part of the basic theory to fool package*)