src/HOL/NatDef.ML
author paulson
Mon Dec 07 18:26:25 1998 +0100 (1998-12-07)
changeset 6019 0e55c2fb2ebb
parent 5983 79e301a6a51b
child 6075 c148037f53c6
permissions -rw-r--r--
tidying
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
wenzelm@5069
     7
Goal "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*)
wenzelm@5069
    14
Goal "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
paulson@5316
    19
Goal "i: Nat ==> Suc_Rep(i) : Nat";
nipkow@2608
    20
by (stac Nat_unfold 1);
nipkow@2608
    21
by (rtac (imageI RS UnI2) 1);
paulson@5316
    22
by (assume_tac 1);
nipkow@2608
    23
qed "Suc_RepI";
nipkow@2608
    24
nipkow@2608
    25
(*** Induction ***)
nipkow@2608
    26
paulson@5316
    27
val major::prems = Goal
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);
wenzelm@4089
    31
by (blast_tac (claset() addIs prems) 1);
nipkow@2608
    32
qed "Nat_induct";
nipkow@2608
    33
paulson@5316
    34
val prems = Goalw [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. *)
berghofe@5187
    44
fun nat_ind_tac a i = 
berghofe@5187
    45
  res_inst_tac [("n",a)] nat_induct i  THEN  rename_last_tac a [""] (i+1);
nipkow@3040
    46
nipkow@2608
    47
(*A special form of induction for reasoning about m<n and m-n*)
paulson@5316
    48
val prems = Goal
nipkow@2608
    49
    "[| !!x. P x 0;  \
nipkow@2608
    50
\       !!y. P 0 (Suc y);  \
nipkow@2608
    51
\       !!x y. [| P x y |] ==> P (Suc x) (Suc y)  \
nipkow@2608
    52
\    |] ==> P m n";
nipkow@2608
    53
by (res_inst_tac [("x","m")] spec 1);
nipkow@2608
    54
by (nat_ind_tac "n" 1);
nipkow@2608
    55
by (rtac allI 2);
nipkow@2608
    56
by (nat_ind_tac "x" 2);
nipkow@2608
    57
by (REPEAT (ares_tac (prems@[allI]) 1 ORELSE etac spec 1));
nipkow@2608
    58
qed "diff_induct";
nipkow@2608
    59
nipkow@2608
    60
(*** Isomorphisms: Abs_Nat and Rep_Nat ***)
nipkow@2608
    61
nipkow@2608
    62
(*We can't take these properties as axioms, or take Abs_Nat==Inv(Rep_Nat),
nipkow@2608
    63
  since we assume the isomorphism equations will one day be given by Isabelle*)
nipkow@2608
    64
wenzelm@5069
    65
Goal "inj(Rep_Nat)";
nipkow@2608
    66
by (rtac inj_inverseI 1);
nipkow@2608
    67
by (rtac Rep_Nat_inverse 1);
nipkow@2608
    68
qed "inj_Rep_Nat";
nipkow@2608
    69
wenzelm@5069
    70
Goal "inj_on Abs_Nat Nat";
nipkow@4830
    71
by (rtac inj_on_inverseI 1);
nipkow@2608
    72
by (etac Abs_Nat_inverse 1);
nipkow@4830
    73
qed "inj_on_Abs_Nat";
nipkow@2608
    74
nipkow@2608
    75
(*** Distinctness of constructors ***)
nipkow@2608
    76
wenzelm@5069
    77
Goalw [Zero_def,Suc_def] "Suc(m) ~= 0";
nipkow@4830
    78
by (rtac (inj_on_Abs_Nat RS inj_on_contraD) 1);
nipkow@2608
    79
by (rtac Suc_Rep_not_Zero_Rep 1);
nipkow@2608
    80
by (REPEAT (resolve_tac [Rep_Nat, Suc_RepI, Zero_RepI] 1));
nipkow@2608
    81
qed "Suc_not_Zero";
nipkow@2608
    82
nipkow@2608
    83
bind_thm ("Zero_not_Suc", Suc_not_Zero RS not_sym);
nipkow@2608
    84
nipkow@2608
    85
AddIffs [Suc_not_Zero,Zero_not_Suc];
nipkow@2608
    86
nipkow@2608
    87
bind_thm ("Suc_neq_Zero", (Suc_not_Zero RS notE));
nipkow@2608
    88
val Zero_neq_Suc = sym RS Suc_neq_Zero;
nipkow@2608
    89
nipkow@2608
    90
(** Injectiveness of Suc **)
nipkow@2608
    91
wenzelm@5069
    92
Goalw [Suc_def] "inj(Suc)";
nipkow@2608
    93
by (rtac injI 1);
nipkow@4830
    94
by (dtac (inj_on_Abs_Nat RS inj_onD) 1);
nipkow@2608
    95
by (REPEAT (resolve_tac [Rep_Nat, Suc_RepI] 1));
nipkow@2608
    96
by (dtac (inj_Suc_Rep RS injD) 1);
nipkow@2608
    97
by (etac (inj_Rep_Nat RS injD) 1);
nipkow@2608
    98
qed "inj_Suc";
nipkow@2608
    99
nipkow@2608
   100
val Suc_inject = inj_Suc RS injD;
nipkow@2608
   101
wenzelm@5069
   102
Goal "(Suc(m)=Suc(n)) = (m=n)";
nipkow@2608
   103
by (EVERY1 [rtac iffI, etac Suc_inject, etac arg_cong]); 
nipkow@2608
   104
qed "Suc_Suc_eq";
nipkow@2608
   105
nipkow@2608
   106
AddIffs [Suc_Suc_eq];
nipkow@2608
   107
wenzelm@5069
   108
Goal "n ~= Suc(n)";
nipkow@2608
   109
by (nat_ind_tac "n" 1);
nipkow@2608
   110
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   111
qed "n_not_Suc_n";
nipkow@2608
   112
nipkow@2608
   113
bind_thm ("Suc_n_not_n", n_not_Suc_n RS not_sym);
nipkow@2608
   114
berghofe@5187
   115
(*** Basic properties of "less than" ***)
nipkow@2608
   116
wenzelm@5069
   117
Goalw [wf_def, pred_nat_def] "wf(pred_nat)";
paulson@3718
   118
by (Clarify_tac 1);
nipkow@2608
   119
by (nat_ind_tac "x" 1);
paulson@3236
   120
by (ALLGOALS Blast_tac);
nipkow@2608
   121
qed "wf_pred_nat";
nipkow@2608
   122
paulson@3378
   123
(*Used in TFL/post.sml*)
wenzelm@5069
   124
Goalw [less_def] "(m,n) : pred_nat^+ = (m<n)";
paulson@3378
   125
by (rtac refl 1);
paulson@3378
   126
qed "less_eq";
paulson@3378
   127
nipkow@2608
   128
(** Introduction properties **)
nipkow@2608
   129
paulson@5316
   130
Goalw [less_def] "[| i<j;  j<k |] ==> i<(k::nat)";
nipkow@2608
   131
by (rtac (trans_trancl RS transD) 1);
paulson@5316
   132
by (assume_tac 1);
paulson@5316
   133
by (assume_tac 1);
nipkow@2608
   134
qed "less_trans";
nipkow@2608
   135
wenzelm@5069
   136
Goalw [less_def, pred_nat_def] "n < Suc(n)";
wenzelm@4089
   137
by (simp_tac (simpset() addsimps [r_into_trancl]) 1);
nipkow@2608
   138
qed "lessI";
nipkow@2608
   139
AddIffs [lessI];
nipkow@2608
   140
nipkow@2608
   141
(* i<j ==> i<Suc(j) *)
nipkow@2608
   142
bind_thm("less_SucI", lessI RSN (2, less_trans));
nipkow@2608
   143
Addsimps [less_SucI];
nipkow@2608
   144
wenzelm@5069
   145
Goal "0 < Suc(n)";
nipkow@2608
   146
by (nat_ind_tac "n" 1);
nipkow@2608
   147
by (rtac lessI 1);
nipkow@2608
   148
by (etac less_trans 1);
nipkow@2608
   149
by (rtac lessI 1);
nipkow@2608
   150
qed "zero_less_Suc";
nipkow@2608
   151
AddIffs [zero_less_Suc];
nipkow@2608
   152
nipkow@2608
   153
(** Elimination properties **)
nipkow@2608
   154
paulson@5316
   155
Goalw [less_def] "n<m ==> ~ m<(n::nat)";
paulson@5316
   156
by (blast_tac (claset() addIs [wf_pred_nat, wf_trancl RS wf_asym])1);
nipkow@2608
   157
qed "less_not_sym";
nipkow@2608
   158
paulson@5474
   159
(* [| n<m; ~P ==> m<n |] ==> P *)
paulson@5474
   160
bind_thm ("less_asym", less_not_sym RS swap);
nipkow@2608
   161
wenzelm@5069
   162
Goalw [less_def] "~ n<(n::nat)";
nipkow@2608
   163
by (rtac notI 1);
nipkow@2608
   164
by (etac (wf_pred_nat RS wf_trancl RS wf_irrefl) 1);
nipkow@2608
   165
qed "less_not_refl";
nipkow@2608
   166
nipkow@2608
   167
(* n<n ==> R *)
nipkow@2608
   168
bind_thm ("less_irrefl", (less_not_refl RS notE));
paulson@5474
   169
AddSEs [less_irrefl];
nipkow@2608
   170
paulson@5143
   171
Goal "n<m ==> m ~= (n::nat)";
paulson@5474
   172
by (Blast_tac 1);
nipkow@2608
   173
qed "less_not_refl2";
nipkow@2608
   174
paulson@5354
   175
(* s < t ==> s ~= t *)
paulson@5354
   176
bind_thm ("less_not_refl3", less_not_refl2 RS not_sym);
paulson@5354
   177
nipkow@2608
   178
paulson@5316
   179
val major::prems = Goalw [less_def, pred_nat_def]
nipkow@2608
   180
    "[| i<k;  k=Suc(i) ==> P;  !!j. [| i<j;  k=Suc(j) |] ==> P \
nipkow@2608
   181
\    |] ==> P";
nipkow@2608
   182
by (rtac (major RS tranclE) 1);
paulson@3236
   183
by (ALLGOALS Full_simp_tac); 
nipkow@2608
   184
by (REPEAT_FIRST (bound_hyp_subst_tac ORELSE'
paulson@3236
   185
                  eresolve_tac (prems@[asm_rl, Pair_inject])));
nipkow@2608
   186
qed "lessE";
nipkow@2608
   187
wenzelm@5069
   188
Goal "~ n<0";
nipkow@2608
   189
by (rtac notI 1);
nipkow@2608
   190
by (etac lessE 1);
nipkow@2608
   191
by (etac Zero_neq_Suc 1);
nipkow@2608
   192
by (etac Zero_neq_Suc 1);
nipkow@2608
   193
qed "not_less0";
nipkow@2608
   194
nipkow@2608
   195
AddIffs [not_less0];
nipkow@2608
   196
nipkow@2608
   197
(* n<0 ==> R *)
nipkow@2608
   198
bind_thm ("less_zeroE", not_less0 RS notE);
nipkow@2608
   199
paulson@5316
   200
val [major,less,eq] = Goal
nipkow@2608
   201
    "[| m < Suc(n);  m<n ==> P;  m=n ==> P |] ==> P";
nipkow@2608
   202
by (rtac (major RS lessE) 1);
nipkow@2608
   203
by (rtac eq 1);
paulson@2891
   204
by (Blast_tac 1);
nipkow@2608
   205
by (rtac less 1);
paulson@2891
   206
by (Blast_tac 1);
nipkow@2608
   207
qed "less_SucE";
nipkow@2608
   208
wenzelm@5069
   209
Goal "(m < Suc(n)) = (m < n | m = n)";
wenzelm@4089
   210
by (blast_tac (claset() addSEs [less_SucE] addIs [less_trans]) 1);
nipkow@2608
   211
qed "less_Suc_eq";
nipkow@2608
   212
wenzelm@5069
   213
Goal "(n<1) = (n=0)";
wenzelm@4089
   214
by (simp_tac (simpset() addsimps [less_Suc_eq]) 1);
nipkow@3484
   215
qed "less_one";
nipkow@3484
   216
AddIffs [less_one];
nipkow@3484
   217
paulson@5143
   218
Goal "m<n ==> Suc(m) < Suc(n)";
nipkow@2608
   219
by (etac rev_mp 1);
nipkow@2608
   220
by (nat_ind_tac "n" 1);
paulson@5474
   221
by (ALLGOALS (fast_tac (claset() addEs [less_trans, lessE])));
nipkow@2608
   222
qed "Suc_mono";
nipkow@2608
   223
nipkow@2608
   224
(*"Less than" is a linear ordering*)
wenzelm@5069
   225
Goal "m<n | m=n | n<(m::nat)";
nipkow@2608
   226
by (nat_ind_tac "m" 1);
nipkow@2608
   227
by (nat_ind_tac "n" 1);
nipkow@2608
   228
by (rtac (refl RS disjI1 RS disjI2) 1);
nipkow@2608
   229
by (rtac (zero_less_Suc RS disjI1) 1);
wenzelm@4089
   230
by (blast_tac (claset() addIs [Suc_mono, less_SucI] addEs [lessE]) 1);
nipkow@2608
   231
qed "less_linear";
nipkow@2608
   232
wenzelm@5069
   233
Goal "!!m::nat. (m ~= n) = (m<n | n<m)";
paulson@4737
   234
by (cut_facts_tac [less_linear] 1);
paulson@5500
   235
by (Blast_tac 1);
paulson@4737
   236
qed "nat_neq_iff";
paulson@4737
   237
nipkow@2608
   238
qed_goal "nat_less_cases" thy 
nipkow@2608
   239
   "[| (m::nat)<n ==> P n m; m=n ==> P n m; n<m ==> P n m |] ==> P n m"
paulson@2935
   240
( fn [major,eqCase,lessCase] =>
nipkow@2608
   241
        [
paulson@2935
   242
        (rtac (less_linear RS disjE) 1),
nipkow@2608
   243
        (etac disjE 2),
paulson@2935
   244
        (etac lessCase 1),
paulson@2935
   245
        (etac (sym RS eqCase) 1),
paulson@2935
   246
        (etac major 1)
nipkow@2608
   247
        ]);
nipkow@2608
   248
paulson@4745
   249
paulson@4745
   250
(** Inductive (?) properties **)
paulson@4745
   251
paulson@5143
   252
Goal "[| m<n; Suc m ~= n |] ==> Suc(m) < n";
paulson@4745
   253
by (full_simp_tac (simpset() addsimps [nat_neq_iff]) 1);
paulson@4745
   254
by (blast_tac (claset() addSEs [less_irrefl, less_SucE] addEs [less_asym]) 1);
paulson@4745
   255
qed "Suc_lessI";
paulson@4745
   256
paulson@5316
   257
Goal "Suc(m) < n ==> m<n";
paulson@5316
   258
by (etac rev_mp 1);
paulson@4745
   259
by (nat_ind_tac "n" 1);
paulson@4745
   260
by (ALLGOALS (fast_tac (claset() addSIs [lessI RS less_SucI]
paulson@4745
   261
                                 addEs  [less_trans, lessE])));
paulson@4745
   262
qed "Suc_lessD";
paulson@4745
   263
paulson@5316
   264
val [major,minor] = Goal 
paulson@4745
   265
    "[| Suc(i)<k;  !!j. [| i<j;  k=Suc(j) |] ==> P \
paulson@4745
   266
\    |] ==> P";
paulson@4745
   267
by (rtac (major RS lessE) 1);
paulson@4745
   268
by (etac (lessI RS minor) 1);
paulson@4745
   269
by (etac (Suc_lessD RS minor) 1);
paulson@4745
   270
by (assume_tac 1);
paulson@4745
   271
qed "Suc_lessE";
paulson@4745
   272
paulson@5143
   273
Goal "Suc(m) < Suc(n) ==> m<n";
paulson@4745
   274
by (blast_tac (claset() addEs [lessE, make_elim Suc_lessD]) 1);
paulson@4745
   275
qed "Suc_less_SucD";
paulson@4745
   276
paulson@4745
   277
wenzelm@5069
   278
Goal "(Suc(m) < Suc(n)) = (m<n)";
paulson@4745
   279
by (EVERY1 [rtac iffI, etac Suc_less_SucD, etac Suc_mono]);
paulson@4745
   280
qed "Suc_less_eq";
paulson@4745
   281
Addsimps [Suc_less_eq];
paulson@4745
   282
wenzelm@5069
   283
Goal "~(Suc(n) < n)";
paulson@4745
   284
by (blast_tac (claset() addEs [Suc_lessD RS less_irrefl]) 1);
paulson@4745
   285
qed "not_Suc_n_less_n";
paulson@4745
   286
Addsimps [not_Suc_n_less_n];
paulson@4745
   287
paulson@5143
   288
Goal "i<j ==> j<k --> Suc i < k";
paulson@4745
   289
by (nat_ind_tac "k" 1);
paulson@4745
   290
by (ALLGOALS (asm_simp_tac (simpset())));
paulson@4745
   291
by (asm_simp_tac (simpset() addsimps [less_Suc_eq]) 1);
paulson@4745
   292
by (blast_tac (claset() addDs [Suc_lessD]) 1);
paulson@4745
   293
qed_spec_mp "less_trans_Suc";
paulson@4745
   294
nipkow@2608
   295
(*Can be used with less_Suc_eq to get n=m | n<m *)
wenzelm@5069
   296
Goal "(~ m < n) = (n < Suc(m))";
nipkow@2608
   297
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
nipkow@2608
   298
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   299
qed "not_less_eq";
nipkow@2608
   300
nipkow@2608
   301
(*Complete induction, aka course-of-values induction*)
paulson@5316
   302
val prems = Goalw [less_def]
nipkow@2608
   303
    "[| !!n. [| ! m::nat. m<n --> P(m) |] ==> P(n) |]  ==>  P(n)";
nipkow@2608
   304
by (wf_ind_tac "n" [wf_pred_nat RS wf_trancl] 1);
nipkow@2608
   305
by (eresolve_tac prems 1);
nipkow@2608
   306
qed "less_induct";
nipkow@2608
   307
nipkow@2608
   308
(*** Properties of <= ***)
nipkow@2608
   309
paulson@5500
   310
(*Was le_eq_less_Suc, but this orientation is more useful*)
paulson@5500
   311
Goalw [le_def] "(m < Suc n) = (m <= n)";
paulson@5500
   312
by (rtac (not_less_eq RS sym) 1);
paulson@5500
   313
qed "less_Suc_eq_le";
nipkow@2608
   314
paulson@3343
   315
(*  m<=n ==> m < Suc n  *)
paulson@5500
   316
bind_thm ("le_imp_less_Suc", less_Suc_eq_le RS iffD2);
paulson@3343
   317
wenzelm@5069
   318
Goalw [le_def] "0 <= n";
nipkow@2608
   319
by (rtac not_less0 1);
nipkow@2608
   320
qed "le0";
nipkow@2608
   321
wenzelm@5069
   322
Goalw [le_def] "~ Suc n <= n";
nipkow@2608
   323
by (Simp_tac 1);
nipkow@2608
   324
qed "Suc_n_not_le_n";
nipkow@2608
   325
wenzelm@5069
   326
Goalw [le_def] "(i <= 0) = (i = 0)";
nipkow@2608
   327
by (nat_ind_tac "i" 1);
nipkow@2608
   328
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   329
qed "le_0_eq";
paulson@4614
   330
AddIffs [le_0_eq];
nipkow@2608
   331
nipkow@2608
   332
Addsimps [(*less_Suc_eq, makes simpset non-confluent*) le0, le_0_eq,
nipkow@2608
   333
          Suc_n_not_le_n,
berghofe@5187
   334
          n_not_Suc_n, Suc_n_not_n];
nipkow@2608
   335
paulson@5143
   336
Goal "(m <= Suc(n)) = (m<=n | m = Suc n)";
paulson@5500
   337
by (simp_tac (simpset() delsimps [less_Suc_eq_le]
paulson@5500
   338
			addsimps [less_Suc_eq_le RS sym, less_Suc_eq]) 1);
paulson@3355
   339
qed "le_Suc_eq";
paulson@3355
   340
paulson@4614
   341
(* [| m <= Suc n;  m <= n ==> R;  m = Suc n ==> R |] ==> R *)
paulson@4614
   342
bind_thm ("le_SucE", le_Suc_eq RS iffD1 RS disjE);
paulson@4614
   343
paulson@5316
   344
Goalw [le_def] "~n<m ==> m<=(n::nat)";
paulson@5316
   345
by (assume_tac 1);
nipkow@2608
   346
qed "leI";
nipkow@2608
   347
paulson@5316
   348
Goalw [le_def] "m<=n ==> ~ n < (m::nat)";
paulson@5316
   349
by (assume_tac 1);
nipkow@2608
   350
qed "leD";
nipkow@2608
   351
nipkow@2608
   352
val leE = make_elim leD;
nipkow@2608
   353
wenzelm@5069
   354
Goal "(~n<m) = (m<=(n::nat))";
wenzelm@4089
   355
by (blast_tac (claset() addIs [leI] addEs [leE]) 1);
nipkow@2608
   356
qed "not_less_iff_le";
nipkow@2608
   357
paulson@5143
   358
Goalw [le_def] "~ m <= n ==> n<(m::nat)";
paulson@2891
   359
by (Blast_tac 1);
nipkow@2608
   360
qed "not_leE";
nipkow@2608
   361
wenzelm@5069
   362
Goalw [le_def] "(~n<=m) = (m<(n::nat))";
paulson@4599
   363
by (Simp_tac 1);
paulson@4599
   364
qed "not_le_iff_less";
paulson@4599
   365
paulson@5143
   366
Goalw [le_def] "m < n ==> Suc(m) <= n";
wenzelm@4089
   367
by (simp_tac (simpset() addsimps [less_Suc_eq]) 1);
wenzelm@4089
   368
by (blast_tac (claset() addSEs [less_irrefl,less_asym]) 1);
paulson@3343
   369
qed "Suc_leI";  (*formerly called lessD*)
nipkow@2608
   370
paulson@5143
   371
Goalw [le_def] "Suc(m) <= n ==> m <= n";
wenzelm@4089
   372
by (asm_full_simp_tac (simpset() addsimps [less_Suc_eq]) 1);
nipkow@2608
   373
qed "Suc_leD";
nipkow@2608
   374
nipkow@2608
   375
(* stronger version of Suc_leD *)
paulson@5148
   376
Goalw [le_def] "Suc m <= n ==> m < n";
wenzelm@4089
   377
by (asm_full_simp_tac (simpset() addsimps [less_Suc_eq]) 1);
nipkow@2608
   378
by (cut_facts_tac [less_linear] 1);
paulson@2891
   379
by (Blast_tac 1);
nipkow@2608
   380
qed "Suc_le_lessD";
nipkow@2608
   381
wenzelm@5069
   382
Goal "(Suc m <= n) = (m < n)";
wenzelm@4089
   383
by (blast_tac (claset() addIs [Suc_leI, Suc_le_lessD]) 1);
nipkow@2608
   384
qed "Suc_le_eq";
nipkow@2608
   385
paulson@5143
   386
Goalw [le_def] "m <= n ==> m <= Suc n";
wenzelm@4089
   387
by (blast_tac (claset() addDs [Suc_lessD]) 1);
nipkow@2608
   388
qed "le_SucI";
nipkow@2608
   389
Addsimps[le_SucI];
nipkow@2608
   390
nipkow@2608
   391
bind_thm ("le_Suc", not_Suc_n_less_n RS leI);
nipkow@2608
   392
paulson@5143
   393
Goalw [le_def] "m < n ==> m <= (n::nat)";
wenzelm@4089
   394
by (blast_tac (claset() addEs [less_asym]) 1);
nipkow@2608
   395
qed "less_imp_le";
nipkow@2608
   396
paulson@5591
   397
(*For instance, (Suc m < Suc n)  =   (Suc m <= n)  =  (m<n) *)
paulson@5591
   398
val le_simps = [less_imp_le, less_Suc_eq_le, Suc_le_eq];
paulson@5591
   399
paulson@5354
   400
paulson@3343
   401
(** Equivalence of m<=n and  m<n | m=n **)
paulson@3343
   402
paulson@5143
   403
Goalw [le_def] "m <= n ==> m < n | m=(n::nat)";
nipkow@2608
   404
by (cut_facts_tac [less_linear] 1);
wenzelm@4089
   405
by (blast_tac (claset() addEs [less_irrefl,less_asym]) 1);
nipkow@2608
   406
qed "le_imp_less_or_eq";
nipkow@2608
   407
paulson@5143
   408
Goalw [le_def] "m<n | m=n ==> m <=(n::nat)";
nipkow@2608
   409
by (cut_facts_tac [less_linear] 1);
wenzelm@4089
   410
by (blast_tac (claset() addSEs [less_irrefl] addEs [less_asym]) 1);
nipkow@2608
   411
qed "less_or_eq_imp_le";
nipkow@2608
   412
wenzelm@5069
   413
Goal "(m <= (n::nat)) = (m < n | m=n)";
nipkow@2608
   414
by (REPEAT(ares_tac [iffI,less_or_eq_imp_le,le_imp_less_or_eq] 1));
nipkow@2608
   415
qed "le_eq_less_or_eq";
nipkow@2608
   416
paulson@4635
   417
(*Useful with Blast_tac.   m=n ==> m<=n *)
paulson@4635
   418
bind_thm ("eq_imp_le", disjI2 RS less_or_eq_imp_le);
paulson@4635
   419
wenzelm@5069
   420
Goal "n <= (n::nat)";
wenzelm@4089
   421
by (simp_tac (simpset() addsimps [le_eq_less_or_eq]) 1);
nipkow@2608
   422
qed "le_refl";
nipkow@2608
   423
paulson@5591
   424
AddIffs [le_refl];
paulson@5354
   425
paulson@5354
   426
paulson@5143
   427
Goal "[| i <= j; j < k |] ==> i < (k::nat)";
paulson@4468
   428
by (blast_tac (claset() addSDs [le_imp_less_or_eq]
paulson@4468
   429
	                addIs [less_trans]) 1);
nipkow@2608
   430
qed "le_less_trans";
nipkow@2608
   431
paulson@5143
   432
Goal "[| i < j; j <= k |] ==> i < (k::nat)";
paulson@4468
   433
by (blast_tac (claset() addSDs [le_imp_less_or_eq]
paulson@4468
   434
	                addIs [less_trans]) 1);
nipkow@2608
   435
qed "less_le_trans";
nipkow@2608
   436
paulson@5143
   437
Goal "[| i <= j; j <= k |] ==> i <= (k::nat)";
paulson@4468
   438
by (blast_tac (claset() addSDs [le_imp_less_or_eq]
paulson@4468
   439
	                addIs [less_or_eq_imp_le, less_trans]) 1);
nipkow@2608
   440
qed "le_trans";
nipkow@2608
   441
paulson@5143
   442
Goal "[| m <= n; n <= m |] ==> m = (n::nat)";
paulson@4468
   443
(*order_less_irrefl could make this proof fail*)
paulson@4468
   444
by (blast_tac (claset() addSDs [le_imp_less_or_eq]
paulson@4468
   445
	                addSEs [less_irrefl] addEs [less_asym]) 1);
nipkow@2608
   446
qed "le_anti_sym";
nipkow@2608
   447
wenzelm@5069
   448
Goal "(Suc(n) <= Suc(m)) = (n <= m)";
paulson@5500
   449
by (simp_tac (simpset() addsimps le_simps) 1);
nipkow@2608
   450
qed "Suc_le_mono";
nipkow@2608
   451
nipkow@2608
   452
AddIffs [Suc_le_mono];
nipkow@2608
   453
paulson@5500
   454
(* Axiom 'order_less_le' of class 'order': *)
wenzelm@5069
   455
Goal "(m::nat) < n = (m <= n & m ~= n)";
paulson@4737
   456
by (simp_tac (simpset() addsimps [le_def, nat_neq_iff]) 1);
paulson@4737
   457
by (blast_tac (claset() addSEs [less_asym]) 1);
nipkow@2608
   458
qed "nat_less_le";
nipkow@2608
   459
paulson@5354
   460
(* [| m <= n; m ~= n |] ==> m < n *)
paulson@5354
   461
bind_thm ("le_neq_implies_less", [nat_less_le, conjI] MRS iffD2);
paulson@5354
   462
nipkow@4640
   463
(* Axiom 'linorder_linear' of class 'linorder': *)
wenzelm@5069
   464
Goal "(m::nat) <= n | n <= m";
nipkow@4640
   465
by (simp_tac (simpset() addsimps [le_eq_less_or_eq]) 1);
nipkow@4640
   466
by (cut_facts_tac [less_linear] 1);
wenzelm@5132
   467
by (Blast_tac 1);
nipkow@4640
   468
qed "nat_le_linear";
nipkow@4640
   469
paulson@5354
   470
Goal "~ n < m ==> (n < Suc m) = (n = m)";
paulson@5354
   471
by (blast_tac (claset() addSEs [less_SucE]) 1);
paulson@5354
   472
qed "not_less_less_Suc_eq";
paulson@5354
   473
paulson@5354
   474
paulson@5354
   475
(*Rewrite (n < Suc m) to (n=m) if  ~ n<m or m<=n hold.
paulson@5354
   476
  Not suitable as default simprules because they often lead to looping*)
paulson@5354
   477
val not_less_simps = [not_less_less_Suc_eq, leD RS not_less_less_Suc_eq];
nipkow@4640
   478
nipkow@4640
   479
(** max
paulson@4599
   480
wenzelm@5069
   481
Goalw [max_def] "!!z::nat. (z <= max x y) = (z <= x | z <= y)";
nipkow@4686
   482
by (simp_tac (simpset() addsimps [not_le_iff_less]) 1);
paulson@4599
   483
by (blast_tac (claset() addIs [less_imp_le, le_trans]) 1);
paulson@4599
   484
qed "le_max_iff_disj";
paulson@4599
   485
wenzelm@5069
   486
Goalw [max_def] "!!z::nat. (max x y <= z) = (x <= z & y <= z)";
nipkow@4686
   487
by (simp_tac (simpset() addsimps [not_le_iff_less]) 1);
paulson@4599
   488
by (blast_tac (claset() addIs [less_imp_le, le_trans]) 1);
paulson@4599
   489
qed "max_le_iff_conj";
paulson@4599
   490
paulson@4599
   491
paulson@4599
   492
(** min **)
paulson@4599
   493
wenzelm@5069
   494
Goalw [min_def] "!!z::nat. (z <= min x y) = (z <= x & z <= y)";
nipkow@4686
   495
by (simp_tac (simpset() addsimps [not_le_iff_less]) 1);
paulson@4599
   496
by (blast_tac (claset() addIs [less_imp_le, le_trans]) 1);
paulson@4599
   497
qed "le_min_iff_conj";
paulson@4599
   498
wenzelm@5069
   499
Goalw [min_def] "!!z::nat. (min x y <= z) = (x <= z | y <= z)";
nipkow@4686
   500
by (simp_tac (simpset() addsimps [not_le_iff_less] addsplits) 1);
paulson@4599
   501
by (blast_tac (claset() addIs [less_imp_le, le_trans]) 1);
paulson@4599
   502
qed "min_le_iff_disj";
nipkow@4640
   503
 **)
paulson@4599
   504
nipkow@2608
   505
(** LEAST -- the least number operator **)
nipkow@2608
   506
wenzelm@5069
   507
Goal "(! m::nat. P m --> n <= m) = (! m. m < n --> ~ P m)";
wenzelm@4089
   508
by (blast_tac (claset() addIs [leI] addEs [leE]) 1);
nipkow@3143
   509
val lemma = result();
nipkow@3143
   510
nipkow@3143
   511
(* This is an old def of Least for nat, which is derived for compatibility *)
wenzelm@5069
   512
Goalw [Least_def]
nipkow@3143
   513
  "(LEAST n::nat. P n) == (@n. P(n) & (ALL m. m < n --> ~P(m)))";
wenzelm@4089
   514
by (simp_tac (simpset() addsimps [lemma]) 1);
nipkow@3143
   515
qed "Least_nat_def";
nipkow@3143
   516
paulson@5316
   517
val [prem1,prem2] = Goalw [Least_nat_def]
wenzelm@3842
   518
    "[| P(k::nat);  !!x. x<k ==> ~P(x) |] ==> (LEAST x. P(x)) = k";
nipkow@2608
   519
by (rtac select_equality 1);
wenzelm@4089
   520
by (blast_tac (claset() addSIs [prem1,prem2]) 1);
nipkow@2608
   521
by (cut_facts_tac [less_linear] 1);
wenzelm@4089
   522
by (blast_tac (claset() addSIs [prem1] addSDs [prem2]) 1);
nipkow@2608
   523
qed "Least_equality";
nipkow@2608
   524
paulson@5316
   525
Goal "P(k::nat) ==> P(LEAST x. P(x))";
paulson@5316
   526
by (etac rev_mp 1);
nipkow@2608
   527
by (res_inst_tac [("n","k")] less_induct 1);
nipkow@2608
   528
by (rtac impI 1);
nipkow@2608
   529
by (rtac classical 1);
nipkow@2608
   530
by (res_inst_tac [("s","n")] (Least_equality RS ssubst) 1);
nipkow@2608
   531
by (assume_tac 1);
nipkow@2608
   532
by (assume_tac 2);
paulson@2891
   533
by (Blast_tac 1);
nipkow@2608
   534
qed "LeastI";
nipkow@2608
   535
nipkow@2608
   536
(*Proof is almost identical to the one above!*)
paulson@5316
   537
Goal "P(k::nat) ==> (LEAST x. P(x)) <= k";
paulson@5316
   538
by (etac rev_mp 1);
nipkow@2608
   539
by (res_inst_tac [("n","k")] less_induct 1);
nipkow@2608
   540
by (rtac impI 1);
nipkow@2608
   541
by (rtac classical 1);
nipkow@2608
   542
by (res_inst_tac [("s","n")] (Least_equality RS ssubst) 1);
nipkow@2608
   543
by (assume_tac 1);
nipkow@2608
   544
by (rtac le_refl 2);
wenzelm@4089
   545
by (blast_tac (claset() addIs [less_imp_le,le_trans]) 1);
nipkow@2608
   546
qed "Least_le";
nipkow@2608
   547
paulson@5316
   548
Goal "k < (LEAST x. P(x)) ==> ~P(k::nat)";
nipkow@2608
   549
by (rtac notI 1);
paulson@5316
   550
by (etac (rewrite_rule [le_def] Least_le RS notE) 1 THEN assume_tac 1);
nipkow@2608
   551
qed "not_less_Least";
nipkow@2608
   552
nipkow@5983
   553
(* [| m ~= n; m < n ==> P; n < m ==> P |] ==> P *)
paulson@4737
   554
bind_thm("nat_neqE", nat_neq_iff RS iffD1 RS disjE);