src/HOL/NatDef.ML
author paulson
Thu Sep 10 18:06:39 1998 +0200 (1998-09-10)
changeset 5474 a2109bb8ce2b
parent 5354 da63d9b35caf
child 5478 33fcf0e60547
permissions -rw-r--r--
well-formed asym rules; also adds less_irrefl, le_refl since order_refl
and order_less_irrefl are no longer included by AddIffs
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
paulson@4737
     5
paulson@4737
     6
Blast_tac proofs here can get PROOF FAILED of Ord theorems like order_refl
paulson@4737
     7
and order_less_irrefl.  We do not add the "nat" versions to the basic claset
paulson@4737
     8
because the type will be promoted to type class "order".
nipkow@2608
     9
*)
nipkow@2608
    10
wenzelm@5069
    11
Goal "mono(%X. {Zero_Rep} Un (Suc_Rep``X))";
nipkow@2608
    12
by (REPEAT (ares_tac [monoI, subset_refl, image_mono, Un_mono] 1));
nipkow@2608
    13
qed "Nat_fun_mono";
nipkow@2608
    14
nipkow@2608
    15
val Nat_unfold = Nat_fun_mono RS (Nat_def RS def_lfp_Tarski);
nipkow@2608
    16
nipkow@2608
    17
(* Zero is a natural number -- this also justifies the type definition*)
wenzelm@5069
    18
Goal "Zero_Rep: Nat";
nipkow@2608
    19
by (stac Nat_unfold 1);
nipkow@2608
    20
by (rtac (singletonI RS UnI1) 1);
nipkow@2608
    21
qed "Zero_RepI";
nipkow@2608
    22
paulson@5316
    23
Goal "i: Nat ==> Suc_Rep(i) : Nat";
nipkow@2608
    24
by (stac Nat_unfold 1);
nipkow@2608
    25
by (rtac (imageI RS UnI2) 1);
paulson@5316
    26
by (assume_tac 1);
nipkow@2608
    27
qed "Suc_RepI";
nipkow@2608
    28
nipkow@2608
    29
(*** Induction ***)
nipkow@2608
    30
paulson@5316
    31
val major::prems = Goal
nipkow@2608
    32
    "[| i: Nat;  P(Zero_Rep);   \
nipkow@2608
    33
\       !!j. [| j: Nat; P(j) |] ==> P(Suc_Rep(j)) |]  ==> P(i)";
nipkow@2608
    34
by (rtac ([Nat_def, Nat_fun_mono, major] MRS def_induct) 1);
wenzelm@4089
    35
by (blast_tac (claset() addIs prems) 1);
nipkow@2608
    36
qed "Nat_induct";
nipkow@2608
    37
paulson@5316
    38
val prems = Goalw [Zero_def,Suc_def]
nipkow@2608
    39
    "[| P(0);   \
nipkow@3040
    40
\       !!n. P(n) ==> P(Suc(n)) |]  ==> P(n)";
nipkow@2608
    41
by (rtac (Rep_Nat_inverse RS subst) 1);   (*types force good instantiation*)
nipkow@2608
    42
by (rtac (Rep_Nat RS Nat_induct) 1);
nipkow@2608
    43
by (REPEAT (ares_tac prems 1
nipkow@2608
    44
     ORELSE eresolve_tac [Abs_Nat_inverse RS subst] 1));
nipkow@2608
    45
qed "nat_induct";
nipkow@2608
    46
nipkow@2608
    47
(*Perform induction on n. *)
berghofe@5187
    48
fun nat_ind_tac a i = 
berghofe@5187
    49
  res_inst_tac [("n",a)] nat_induct i  THEN  rename_last_tac a [""] (i+1);
nipkow@3040
    50
nipkow@2608
    51
(*A special form of induction for reasoning about m<n and m-n*)
paulson@5316
    52
val prems = Goal
nipkow@2608
    53
    "[| !!x. P x 0;  \
nipkow@2608
    54
\       !!y. P 0 (Suc y);  \
nipkow@2608
    55
\       !!x y. [| P x y |] ==> P (Suc x) (Suc y)  \
nipkow@2608
    56
\    |] ==> P m n";
nipkow@2608
    57
by (res_inst_tac [("x","m")] spec 1);
nipkow@2608
    58
by (nat_ind_tac "n" 1);
nipkow@2608
    59
by (rtac allI 2);
nipkow@2608
    60
by (nat_ind_tac "x" 2);
nipkow@2608
    61
by (REPEAT (ares_tac (prems@[allI]) 1 ORELSE etac spec 1));
nipkow@2608
    62
qed "diff_induct";
nipkow@2608
    63
nipkow@2608
    64
(*** Isomorphisms: Abs_Nat and Rep_Nat ***)
nipkow@2608
    65
nipkow@2608
    66
(*We can't take these properties as axioms, or take Abs_Nat==Inv(Rep_Nat),
nipkow@2608
    67
  since we assume the isomorphism equations will one day be given by Isabelle*)
nipkow@2608
    68
wenzelm@5069
    69
Goal "inj(Rep_Nat)";
nipkow@2608
    70
by (rtac inj_inverseI 1);
nipkow@2608
    71
by (rtac Rep_Nat_inverse 1);
nipkow@2608
    72
qed "inj_Rep_Nat";
nipkow@2608
    73
wenzelm@5069
    74
Goal "inj_on Abs_Nat Nat";
nipkow@4830
    75
by (rtac inj_on_inverseI 1);
nipkow@2608
    76
by (etac Abs_Nat_inverse 1);
nipkow@4830
    77
qed "inj_on_Abs_Nat";
nipkow@2608
    78
nipkow@2608
    79
(*** Distinctness of constructors ***)
nipkow@2608
    80
wenzelm@5069
    81
Goalw [Zero_def,Suc_def] "Suc(m) ~= 0";
nipkow@4830
    82
by (rtac (inj_on_Abs_Nat RS inj_on_contraD) 1);
nipkow@2608
    83
by (rtac Suc_Rep_not_Zero_Rep 1);
nipkow@2608
    84
by (REPEAT (resolve_tac [Rep_Nat, Suc_RepI, Zero_RepI] 1));
nipkow@2608
    85
qed "Suc_not_Zero";
nipkow@2608
    86
nipkow@2608
    87
bind_thm ("Zero_not_Suc", Suc_not_Zero RS not_sym);
nipkow@2608
    88
nipkow@2608
    89
AddIffs [Suc_not_Zero,Zero_not_Suc];
nipkow@2608
    90
nipkow@2608
    91
bind_thm ("Suc_neq_Zero", (Suc_not_Zero RS notE));
nipkow@2608
    92
val Zero_neq_Suc = sym RS Suc_neq_Zero;
nipkow@2608
    93
nipkow@2608
    94
(** Injectiveness of Suc **)
nipkow@2608
    95
wenzelm@5069
    96
Goalw [Suc_def] "inj(Suc)";
nipkow@2608
    97
by (rtac injI 1);
nipkow@4830
    98
by (dtac (inj_on_Abs_Nat RS inj_onD) 1);
nipkow@2608
    99
by (REPEAT (resolve_tac [Rep_Nat, Suc_RepI] 1));
nipkow@2608
   100
by (dtac (inj_Suc_Rep RS injD) 1);
nipkow@2608
   101
by (etac (inj_Rep_Nat RS injD) 1);
nipkow@2608
   102
qed "inj_Suc";
nipkow@2608
   103
nipkow@2608
   104
val Suc_inject = inj_Suc RS injD;
nipkow@2608
   105
wenzelm@5069
   106
Goal "(Suc(m)=Suc(n)) = (m=n)";
nipkow@2608
   107
by (EVERY1 [rtac iffI, etac Suc_inject, etac arg_cong]); 
nipkow@2608
   108
qed "Suc_Suc_eq";
nipkow@2608
   109
nipkow@2608
   110
AddIffs [Suc_Suc_eq];
nipkow@2608
   111
wenzelm@5069
   112
Goal "n ~= Suc(n)";
nipkow@2608
   113
by (nat_ind_tac "n" 1);
nipkow@2608
   114
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   115
qed "n_not_Suc_n";
nipkow@2608
   116
nipkow@2608
   117
bind_thm ("Suc_n_not_n", n_not_Suc_n RS not_sym);
nipkow@2608
   118
berghofe@5187
   119
(*** Basic properties of "less than" ***)
nipkow@2608
   120
wenzelm@5069
   121
Goalw [wf_def, pred_nat_def] "wf(pred_nat)";
paulson@3718
   122
by (Clarify_tac 1);
nipkow@2608
   123
by (nat_ind_tac "x" 1);
paulson@3236
   124
by (ALLGOALS Blast_tac);
nipkow@2608
   125
qed "wf_pred_nat";
nipkow@2608
   126
paulson@3378
   127
(*Used in TFL/post.sml*)
wenzelm@5069
   128
Goalw [less_def] "(m,n) : pred_nat^+ = (m<n)";
paulson@3378
   129
by (rtac refl 1);
paulson@3378
   130
qed "less_eq";
paulson@3378
   131
nipkow@2608
   132
(** Introduction properties **)
nipkow@2608
   133
paulson@5316
   134
Goalw [less_def] "[| i<j;  j<k |] ==> i<(k::nat)";
nipkow@2608
   135
by (rtac (trans_trancl RS transD) 1);
paulson@5316
   136
by (assume_tac 1);
paulson@5316
   137
by (assume_tac 1);
nipkow@2608
   138
qed "less_trans";
nipkow@2608
   139
wenzelm@5069
   140
Goalw [less_def, pred_nat_def] "n < Suc(n)";
wenzelm@4089
   141
by (simp_tac (simpset() addsimps [r_into_trancl]) 1);
nipkow@2608
   142
qed "lessI";
nipkow@2608
   143
AddIffs [lessI];
nipkow@2608
   144
nipkow@2608
   145
(* i<j ==> i<Suc(j) *)
nipkow@2608
   146
bind_thm("less_SucI", lessI RSN (2, less_trans));
nipkow@2608
   147
Addsimps [less_SucI];
nipkow@2608
   148
wenzelm@5069
   149
Goal "0 < Suc(n)";
nipkow@2608
   150
by (nat_ind_tac "n" 1);
nipkow@2608
   151
by (rtac lessI 1);
nipkow@2608
   152
by (etac less_trans 1);
nipkow@2608
   153
by (rtac lessI 1);
nipkow@2608
   154
qed "zero_less_Suc";
nipkow@2608
   155
AddIffs [zero_less_Suc];
nipkow@2608
   156
nipkow@2608
   157
(** Elimination properties **)
nipkow@2608
   158
paulson@5316
   159
Goalw [less_def] "n<m ==> ~ m<(n::nat)";
paulson@5316
   160
by (blast_tac (claset() addIs [wf_pred_nat, wf_trancl RS wf_asym])1);
nipkow@2608
   161
qed "less_not_sym";
nipkow@2608
   162
paulson@5474
   163
(* [| n<m; ~P ==> m<n |] ==> P *)
paulson@5474
   164
bind_thm ("less_asym", less_not_sym RS swap);
nipkow@2608
   165
wenzelm@5069
   166
Goalw [less_def] "~ n<(n::nat)";
nipkow@2608
   167
by (rtac notI 1);
nipkow@2608
   168
by (etac (wf_pred_nat RS wf_trancl RS wf_irrefl) 1);
nipkow@2608
   169
qed "less_not_refl";
nipkow@2608
   170
nipkow@2608
   171
(* n<n ==> R *)
nipkow@2608
   172
bind_thm ("less_irrefl", (less_not_refl RS notE));
paulson@5474
   173
AddSEs [less_irrefl];
nipkow@2608
   174
paulson@5143
   175
Goal "n<m ==> m ~= (n::nat)";
paulson@5474
   176
by (Blast_tac 1);
nipkow@2608
   177
qed "less_not_refl2";
nipkow@2608
   178
paulson@5354
   179
(* s < t ==> s ~= t *)
paulson@5354
   180
bind_thm ("less_not_refl3", less_not_refl2 RS not_sym);
paulson@5354
   181
nipkow@2608
   182
paulson@5316
   183
val major::prems = Goalw [less_def, pred_nat_def]
nipkow@2608
   184
    "[| i<k;  k=Suc(i) ==> P;  !!j. [| i<j;  k=Suc(j) |] ==> P \
nipkow@2608
   185
\    |] ==> P";
nipkow@2608
   186
by (rtac (major RS tranclE) 1);
paulson@3236
   187
by (ALLGOALS Full_simp_tac); 
nipkow@2608
   188
by (REPEAT_FIRST (bound_hyp_subst_tac ORELSE'
paulson@3236
   189
                  eresolve_tac (prems@[asm_rl, Pair_inject])));
nipkow@2608
   190
qed "lessE";
nipkow@2608
   191
wenzelm@5069
   192
Goal "~ n<0";
nipkow@2608
   193
by (rtac notI 1);
nipkow@2608
   194
by (etac lessE 1);
nipkow@2608
   195
by (etac Zero_neq_Suc 1);
nipkow@2608
   196
by (etac Zero_neq_Suc 1);
nipkow@2608
   197
qed "not_less0";
nipkow@2608
   198
nipkow@2608
   199
AddIffs [not_less0];
nipkow@2608
   200
nipkow@2608
   201
(* n<0 ==> R *)
nipkow@2608
   202
bind_thm ("less_zeroE", not_less0 RS notE);
nipkow@2608
   203
paulson@5316
   204
val [major,less,eq] = Goal
nipkow@2608
   205
    "[| m < Suc(n);  m<n ==> P;  m=n ==> P |] ==> P";
nipkow@2608
   206
by (rtac (major RS lessE) 1);
nipkow@2608
   207
by (rtac eq 1);
paulson@2891
   208
by (Blast_tac 1);
nipkow@2608
   209
by (rtac less 1);
paulson@2891
   210
by (Blast_tac 1);
nipkow@2608
   211
qed "less_SucE";
nipkow@2608
   212
wenzelm@5069
   213
Goal "(m < Suc(n)) = (m < n | m = n)";
wenzelm@4089
   214
by (blast_tac (claset() addSEs [less_SucE] addIs [less_trans]) 1);
nipkow@2608
   215
qed "less_Suc_eq";
nipkow@2608
   216
wenzelm@5069
   217
Goal "(n<1) = (n=0)";
wenzelm@4089
   218
by (simp_tac (simpset() addsimps [less_Suc_eq]) 1);
nipkow@3484
   219
qed "less_one";
nipkow@3484
   220
AddIffs [less_one];
nipkow@3484
   221
paulson@5143
   222
Goal "m<n ==> Suc(m) < Suc(n)";
nipkow@2608
   223
by (etac rev_mp 1);
nipkow@2608
   224
by (nat_ind_tac "n" 1);
paulson@5474
   225
by (ALLGOALS (fast_tac (claset() addEs [less_trans, lessE])));
nipkow@2608
   226
qed "Suc_mono";
nipkow@2608
   227
nipkow@2608
   228
(*"Less than" is a linear ordering*)
wenzelm@5069
   229
Goal "m<n | m=n | n<(m::nat)";
nipkow@2608
   230
by (nat_ind_tac "m" 1);
nipkow@2608
   231
by (nat_ind_tac "n" 1);
nipkow@2608
   232
by (rtac (refl RS disjI1 RS disjI2) 1);
nipkow@2608
   233
by (rtac (zero_less_Suc RS disjI1) 1);
wenzelm@4089
   234
by (blast_tac (claset() addIs [Suc_mono, less_SucI] addEs [lessE]) 1);
nipkow@2608
   235
qed "less_linear";
nipkow@2608
   236
wenzelm@5069
   237
Goal "!!m::nat. (m ~= n) = (m<n | n<m)";
paulson@4737
   238
by (cut_facts_tac [less_linear] 1);
paulson@4737
   239
by (blast_tac (claset() addSEs [less_irrefl]) 1);
paulson@4737
   240
qed "nat_neq_iff";
paulson@4737
   241
nipkow@2608
   242
qed_goal "nat_less_cases" thy 
nipkow@2608
   243
   "[| (m::nat)<n ==> P n m; m=n ==> P n m; n<m ==> P n m |] ==> P n m"
paulson@2935
   244
( fn [major,eqCase,lessCase] =>
nipkow@2608
   245
        [
paulson@2935
   246
        (rtac (less_linear RS disjE) 1),
nipkow@2608
   247
        (etac disjE 2),
paulson@2935
   248
        (etac lessCase 1),
paulson@2935
   249
        (etac (sym RS eqCase) 1),
paulson@2935
   250
        (etac major 1)
nipkow@2608
   251
        ]);
nipkow@2608
   252
paulson@4745
   253
paulson@4745
   254
(** Inductive (?) properties **)
paulson@4745
   255
paulson@5143
   256
Goal "[| m<n; Suc m ~= n |] ==> Suc(m) < n";
paulson@4745
   257
by (full_simp_tac (simpset() addsimps [nat_neq_iff]) 1);
paulson@4745
   258
by (blast_tac (claset() addSEs [less_irrefl, less_SucE] addEs [less_asym]) 1);
paulson@4745
   259
qed "Suc_lessI";
paulson@4745
   260
paulson@5316
   261
Goal "Suc(m) < n ==> m<n";
paulson@5316
   262
by (etac rev_mp 1);
paulson@4745
   263
by (nat_ind_tac "n" 1);
paulson@4745
   264
by (ALLGOALS (fast_tac (claset() addSIs [lessI RS less_SucI]
paulson@4745
   265
                                 addEs  [less_trans, lessE])));
paulson@4745
   266
qed "Suc_lessD";
paulson@4745
   267
paulson@5316
   268
val [major,minor] = Goal 
paulson@4745
   269
    "[| Suc(i)<k;  !!j. [| i<j;  k=Suc(j) |] ==> P \
paulson@4745
   270
\    |] ==> P";
paulson@4745
   271
by (rtac (major RS lessE) 1);
paulson@4745
   272
by (etac (lessI RS minor) 1);
paulson@4745
   273
by (etac (Suc_lessD RS minor) 1);
paulson@4745
   274
by (assume_tac 1);
paulson@4745
   275
qed "Suc_lessE";
paulson@4745
   276
paulson@5143
   277
Goal "Suc(m) < Suc(n) ==> m<n";
paulson@4745
   278
by (blast_tac (claset() addEs [lessE, make_elim Suc_lessD]) 1);
paulson@4745
   279
qed "Suc_less_SucD";
paulson@4745
   280
paulson@4745
   281
wenzelm@5069
   282
Goal "(Suc(m) < Suc(n)) = (m<n)";
paulson@4745
   283
by (EVERY1 [rtac iffI, etac Suc_less_SucD, etac Suc_mono]);
paulson@4745
   284
qed "Suc_less_eq";
paulson@4745
   285
Addsimps [Suc_less_eq];
paulson@4745
   286
wenzelm@5069
   287
Goal "~(Suc(n) < n)";
paulson@4745
   288
by (blast_tac (claset() addEs [Suc_lessD RS less_irrefl]) 1);
paulson@4745
   289
qed "not_Suc_n_less_n";
paulson@4745
   290
Addsimps [not_Suc_n_less_n];
paulson@4745
   291
paulson@5143
   292
Goal "i<j ==> j<k --> Suc i < k";
paulson@4745
   293
by (nat_ind_tac "k" 1);
paulson@4745
   294
by (ALLGOALS (asm_simp_tac (simpset())));
paulson@4745
   295
by (asm_simp_tac (simpset() addsimps [less_Suc_eq]) 1);
paulson@4745
   296
by (blast_tac (claset() addDs [Suc_lessD]) 1);
paulson@4745
   297
qed_spec_mp "less_trans_Suc";
paulson@4745
   298
nipkow@2608
   299
(*Can be used with less_Suc_eq to get n=m | n<m *)
wenzelm@5069
   300
Goal "(~ m < n) = (n < Suc(m))";
nipkow@2608
   301
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
nipkow@2608
   302
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   303
qed "not_less_eq";
nipkow@2608
   304
nipkow@2608
   305
(*Complete induction, aka course-of-values induction*)
paulson@5316
   306
val prems = Goalw [less_def]
nipkow@2608
   307
    "[| !!n. [| ! m::nat. m<n --> P(m) |] ==> P(n) |]  ==>  P(n)";
nipkow@2608
   308
by (wf_ind_tac "n" [wf_pred_nat RS wf_trancl] 1);
nipkow@2608
   309
by (eresolve_tac prems 1);
nipkow@2608
   310
qed "less_induct";
nipkow@2608
   311
nipkow@2608
   312
(*** Properties of <= ***)
nipkow@2608
   313
wenzelm@5069
   314
Goalw [le_def] "(m <= n) = (m < Suc n)";
nipkow@2608
   315
by (rtac not_less_eq 1);
nipkow@2608
   316
qed "le_eq_less_Suc";
nipkow@2608
   317
paulson@3343
   318
(*  m<=n ==> m < Suc n  *)
paulson@3343
   319
bind_thm ("le_imp_less_Suc", le_eq_less_Suc RS iffD1);
paulson@3343
   320
wenzelm@5069
   321
Goalw [le_def] "0 <= n";
nipkow@2608
   322
by (rtac not_less0 1);
nipkow@2608
   323
qed "le0";
nipkow@2608
   324
wenzelm@5069
   325
Goalw [le_def] "~ Suc n <= n";
nipkow@2608
   326
by (Simp_tac 1);
nipkow@2608
   327
qed "Suc_n_not_le_n";
nipkow@2608
   328
wenzelm@5069
   329
Goalw [le_def] "(i <= 0) = (i = 0)";
nipkow@2608
   330
by (nat_ind_tac "i" 1);
nipkow@2608
   331
by (ALLGOALS Asm_simp_tac);
nipkow@2608
   332
qed "le_0_eq";
paulson@4614
   333
AddIffs [le_0_eq];
nipkow@2608
   334
nipkow@2608
   335
Addsimps [(*less_Suc_eq, makes simpset non-confluent*) le0, le_0_eq,
nipkow@2608
   336
          Suc_n_not_le_n,
berghofe@5187
   337
          n_not_Suc_n, Suc_n_not_n];
nipkow@2608
   338
paulson@5143
   339
Goal "(m <= Suc(n)) = (m<=n | m = Suc n)";
wenzelm@4089
   340
by (simp_tac (simpset() addsimps [le_eq_less_Suc]) 1);
wenzelm@4089
   341
by (blast_tac (claset() addSEs [less_SucE] addIs [less_SucI]) 1);
paulson@3355
   342
qed "le_Suc_eq";
paulson@3355
   343
paulson@4614
   344
(* [| m <= Suc n;  m <= n ==> R;  m = Suc n ==> R |] ==> R *)
paulson@4614
   345
bind_thm ("le_SucE", le_Suc_eq RS iffD1 RS disjE);
paulson@4614
   346
nipkow@2608
   347
(*
wenzelm@5069
   348
Goal "(Suc m < n | Suc m = n) = (m < n)";
nipkow@2608
   349
by (stac (less_Suc_eq RS sym) 1);
nipkow@2608
   350
by (rtac Suc_less_eq 1);
nipkow@2608
   351
qed "Suc_le_eq";
nipkow@2608
   352
nipkow@2608
   353
this could make the simpset (with less_Suc_eq added again) more confluent,
nipkow@2608
   354
but less_Suc_eq makes additional problems with terms of the form 0 < Suc (...)
nipkow@2608
   355
*)
nipkow@2608
   356
paulson@5316
   357
Goalw [le_def] "~n<m ==> m<=(n::nat)";
paulson@5316
   358
by (assume_tac 1);
nipkow@2608
   359
qed "leI";
nipkow@2608
   360
paulson@5316
   361
Goalw [le_def] "m<=n ==> ~ n < (m::nat)";
paulson@5316
   362
by (assume_tac 1);
nipkow@2608
   363
qed "leD";
nipkow@2608
   364
nipkow@2608
   365
val leE = make_elim leD;
nipkow@2608
   366
wenzelm@5069
   367
Goal "(~n<m) = (m<=(n::nat))";
wenzelm@4089
   368
by (blast_tac (claset() addIs [leI] addEs [leE]) 1);
nipkow@2608
   369
qed "not_less_iff_le";
nipkow@2608
   370
paulson@5143
   371
Goalw [le_def] "~ m <= n ==> n<(m::nat)";
paulson@2891
   372
by (Blast_tac 1);
nipkow@2608
   373
qed "not_leE";
nipkow@2608
   374
wenzelm@5069
   375
Goalw [le_def] "(~n<=m) = (m<(n::nat))";
paulson@4599
   376
by (Simp_tac 1);
paulson@4599
   377
qed "not_le_iff_less";
paulson@4599
   378
paulson@5143
   379
Goalw [le_def] "m < n ==> Suc(m) <= n";
wenzelm@4089
   380
by (simp_tac (simpset() addsimps [less_Suc_eq]) 1);
wenzelm@4089
   381
by (blast_tac (claset() addSEs [less_irrefl,less_asym]) 1);
paulson@3343
   382
qed "Suc_leI";  (*formerly called lessD*)
nipkow@2608
   383
paulson@5143
   384
Goalw [le_def] "Suc(m) <= n ==> m <= n";
wenzelm@4089
   385
by (asm_full_simp_tac (simpset() addsimps [less_Suc_eq]) 1);
nipkow@2608
   386
qed "Suc_leD";
nipkow@2608
   387
nipkow@2608
   388
(* stronger version of Suc_leD *)
paulson@5148
   389
Goalw [le_def] "Suc m <= n ==> m < n";
wenzelm@4089
   390
by (asm_full_simp_tac (simpset() addsimps [less_Suc_eq]) 1);
nipkow@2608
   391
by (cut_facts_tac [less_linear] 1);
paulson@2891
   392
by (Blast_tac 1);
nipkow@2608
   393
qed "Suc_le_lessD";
nipkow@2608
   394
wenzelm@5069
   395
Goal "(Suc m <= n) = (m < n)";
wenzelm@4089
   396
by (blast_tac (claset() addIs [Suc_leI, Suc_le_lessD]) 1);
nipkow@2608
   397
qed "Suc_le_eq";
nipkow@2608
   398
paulson@5143
   399
Goalw [le_def] "m <= n ==> m <= Suc n";
wenzelm@4089
   400
by (blast_tac (claset() addDs [Suc_lessD]) 1);
nipkow@2608
   401
qed "le_SucI";
nipkow@2608
   402
Addsimps[le_SucI];
nipkow@2608
   403
nipkow@2608
   404
bind_thm ("le_Suc", not_Suc_n_less_n RS leI);
nipkow@2608
   405
paulson@5143
   406
Goalw [le_def] "m < n ==> m <= (n::nat)";
wenzelm@4089
   407
by (blast_tac (claset() addEs [less_asym]) 1);
nipkow@2608
   408
qed "less_imp_le";
nipkow@2608
   409
paulson@5354
   410
paulson@3343
   411
(** Equivalence of m<=n and  m<n | m=n **)
paulson@3343
   412
paulson@5143
   413
Goalw [le_def] "m <= n ==> m < n | m=(n::nat)";
nipkow@2608
   414
by (cut_facts_tac [less_linear] 1);
wenzelm@4089
   415
by (blast_tac (claset() addEs [less_irrefl,less_asym]) 1);
nipkow@2608
   416
qed "le_imp_less_or_eq";
nipkow@2608
   417
paulson@5143
   418
Goalw [le_def] "m<n | m=n ==> m <=(n::nat)";
nipkow@2608
   419
by (cut_facts_tac [less_linear] 1);
wenzelm@4089
   420
by (blast_tac (claset() addSEs [less_irrefl] addEs [less_asym]) 1);
nipkow@2608
   421
qed "less_or_eq_imp_le";
nipkow@2608
   422
wenzelm@5069
   423
Goal "(m <= (n::nat)) = (m < n | m=n)";
nipkow@2608
   424
by (REPEAT(ares_tac [iffI,less_or_eq_imp_le,le_imp_less_or_eq] 1));
nipkow@2608
   425
qed "le_eq_less_or_eq";
nipkow@2608
   426
paulson@4635
   427
(*Useful with Blast_tac.   m=n ==> m<=n *)
paulson@4635
   428
bind_thm ("eq_imp_le", disjI2 RS less_or_eq_imp_le);
paulson@4635
   429
wenzelm@5069
   430
Goal "n <= (n::nat)";
wenzelm@4089
   431
by (simp_tac (simpset() addsimps [le_eq_less_or_eq]) 1);
nipkow@2608
   432
qed "le_refl";
paulson@5474
   433
AddSIs [le_refl];
nipkow@2608
   434
paulson@5354
   435
(*Obvious ways of proving m<=n; 
paulson@5354
   436
  adding these rules to claset might be risky*)
paulson@5354
   437
Addsimps [le_refl, less_imp_le];
paulson@5354
   438
paulson@5354
   439
paulson@5143
   440
Goal "[| i <= j; j < k |] ==> i < (k::nat)";
paulson@4468
   441
by (blast_tac (claset() addSDs [le_imp_less_or_eq]
paulson@4468
   442
	                addIs [less_trans]) 1);
nipkow@2608
   443
qed "le_less_trans";
nipkow@2608
   444
paulson@5143
   445
Goal "[| i < j; j <= k |] ==> i < (k::nat)";
paulson@4468
   446
by (blast_tac (claset() addSDs [le_imp_less_or_eq]
paulson@4468
   447
	                addIs [less_trans]) 1);
nipkow@2608
   448
qed "less_le_trans";
nipkow@2608
   449
paulson@5143
   450
Goal "[| i <= j; j <= k |] ==> i <= (k::nat)";
paulson@4468
   451
by (blast_tac (claset() addSDs [le_imp_less_or_eq]
paulson@4468
   452
	                addIs [less_or_eq_imp_le, less_trans]) 1);
nipkow@2608
   453
qed "le_trans";
nipkow@2608
   454
paulson@5143
   455
Goal "[| m <= n; n <= m |] ==> m = (n::nat)";
paulson@4468
   456
(*order_less_irrefl could make this proof fail*)
paulson@4468
   457
by (blast_tac (claset() addSDs [le_imp_less_or_eq]
paulson@4468
   458
	                addSEs [less_irrefl] addEs [less_asym]) 1);
nipkow@2608
   459
qed "le_anti_sym";
nipkow@2608
   460
wenzelm@5069
   461
Goal "(Suc(n) <= Suc(m)) = (n <= m)";
wenzelm@4089
   462
by (simp_tac (simpset() addsimps [le_eq_less_or_eq]) 1);
nipkow@2608
   463
qed "Suc_le_mono";
nipkow@2608
   464
nipkow@2608
   465
AddIffs [Suc_le_mono];
nipkow@2608
   466
nipkow@2608
   467
(* Axiom 'order_le_less' of class 'order': *)
wenzelm@5069
   468
Goal "(m::nat) < n = (m <= n & m ~= n)";
paulson@4737
   469
by (simp_tac (simpset() addsimps [le_def, nat_neq_iff]) 1);
paulson@4737
   470
by (blast_tac (claset() addSEs [less_asym]) 1);
nipkow@2608
   471
qed "nat_less_le";
nipkow@2608
   472
paulson@5354
   473
(* [| m <= n; m ~= n |] ==> m < n *)
paulson@5354
   474
bind_thm ("le_neq_implies_less", [nat_less_le, conjI] MRS iffD2);
paulson@5354
   475
nipkow@4640
   476
(* Axiom 'linorder_linear' of class 'linorder': *)
wenzelm@5069
   477
Goal "(m::nat) <= n | n <= m";
nipkow@4640
   478
by (simp_tac (simpset() addsimps [le_eq_less_or_eq]) 1);
nipkow@4640
   479
by (cut_facts_tac [less_linear] 1);
wenzelm@5132
   480
by (Blast_tac 1);
nipkow@4640
   481
qed "nat_le_linear";
nipkow@4640
   482
paulson@5354
   483
Goal "~ n < m ==> (n < Suc m) = (n = m)";
paulson@5354
   484
by (blast_tac (claset() addSEs [less_SucE]) 1);
paulson@5354
   485
qed "not_less_less_Suc_eq";
paulson@5354
   486
paulson@5354
   487
paulson@5354
   488
(*Rewrite (n < Suc m) to (n=m) if  ~ n<m or m<=n hold.
paulson@5354
   489
  Not suitable as default simprules because they often lead to looping*)
paulson@5354
   490
val not_less_simps = [not_less_less_Suc_eq, leD RS not_less_less_Suc_eq];
nipkow@4640
   491
nipkow@4640
   492
(** max
paulson@4599
   493
wenzelm@5069
   494
Goalw [max_def] "!!z::nat. (z <= max 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_max_iff_disj";
paulson@4599
   498
wenzelm@5069
   499
Goalw [max_def] "!!z::nat. (max x y <= z) = (x <= z & y <= z)";
nipkow@4686
   500
by (simp_tac (simpset() addsimps [not_le_iff_less]) 1);
paulson@4599
   501
by (blast_tac (claset() addIs [less_imp_le, le_trans]) 1);
paulson@4599
   502
qed "max_le_iff_conj";
paulson@4599
   503
paulson@4599
   504
paulson@4599
   505
(** min **)
paulson@4599
   506
wenzelm@5069
   507
Goalw [min_def] "!!z::nat. (z <= min x y) = (z <= x & z <= y)";
nipkow@4686
   508
by (simp_tac (simpset() addsimps [not_le_iff_less]) 1);
paulson@4599
   509
by (blast_tac (claset() addIs [less_imp_le, le_trans]) 1);
paulson@4599
   510
qed "le_min_iff_conj";
paulson@4599
   511
wenzelm@5069
   512
Goalw [min_def] "!!z::nat. (min x y <= z) = (x <= z | y <= z)";
nipkow@4686
   513
by (simp_tac (simpset() addsimps [not_le_iff_less] addsplits) 1);
paulson@4599
   514
by (blast_tac (claset() addIs [less_imp_le, le_trans]) 1);
paulson@4599
   515
qed "min_le_iff_disj";
nipkow@4640
   516
 **)
paulson@4599
   517
nipkow@2608
   518
(** LEAST -- the least number operator **)
nipkow@2608
   519
wenzelm@5069
   520
Goal "(! m::nat. P m --> n <= m) = (! m. m < n --> ~ P m)";
wenzelm@4089
   521
by (blast_tac (claset() addIs [leI] addEs [leE]) 1);
nipkow@3143
   522
val lemma = result();
nipkow@3143
   523
nipkow@3143
   524
(* This is an old def of Least for nat, which is derived for compatibility *)
wenzelm@5069
   525
Goalw [Least_def]
nipkow@3143
   526
  "(LEAST n::nat. P n) == (@n. P(n) & (ALL m. m < n --> ~P(m)))";
wenzelm@4089
   527
by (simp_tac (simpset() addsimps [lemma]) 1);
nipkow@3143
   528
qed "Least_nat_def";
nipkow@3143
   529
paulson@5316
   530
val [prem1,prem2] = Goalw [Least_nat_def]
wenzelm@3842
   531
    "[| P(k::nat);  !!x. x<k ==> ~P(x) |] ==> (LEAST x. P(x)) = k";
nipkow@2608
   532
by (rtac select_equality 1);
wenzelm@4089
   533
by (blast_tac (claset() addSIs [prem1,prem2]) 1);
nipkow@2608
   534
by (cut_facts_tac [less_linear] 1);
wenzelm@4089
   535
by (blast_tac (claset() addSIs [prem1] addSDs [prem2]) 1);
nipkow@2608
   536
qed "Least_equality";
nipkow@2608
   537
paulson@5316
   538
Goal "P(k::nat) ==> P(LEAST x. P(x))";
paulson@5316
   539
by (etac rev_mp 1);
nipkow@2608
   540
by (res_inst_tac [("n","k")] less_induct 1);
nipkow@2608
   541
by (rtac impI 1);
nipkow@2608
   542
by (rtac classical 1);
nipkow@2608
   543
by (res_inst_tac [("s","n")] (Least_equality RS ssubst) 1);
nipkow@2608
   544
by (assume_tac 1);
nipkow@2608
   545
by (assume_tac 2);
paulson@2891
   546
by (Blast_tac 1);
nipkow@2608
   547
qed "LeastI";
nipkow@2608
   548
nipkow@2608
   549
(*Proof is almost identical to the one above!*)
paulson@5316
   550
Goal "P(k::nat) ==> (LEAST x. P(x)) <= k";
paulson@5316
   551
by (etac rev_mp 1);
nipkow@2608
   552
by (res_inst_tac [("n","k")] less_induct 1);
nipkow@2608
   553
by (rtac impI 1);
nipkow@2608
   554
by (rtac classical 1);
nipkow@2608
   555
by (res_inst_tac [("s","n")] (Least_equality RS ssubst) 1);
nipkow@2608
   556
by (assume_tac 1);
nipkow@2608
   557
by (rtac le_refl 2);
wenzelm@4089
   558
by (blast_tac (claset() addIs [less_imp_le,le_trans]) 1);
nipkow@2608
   559
qed "Least_le";
nipkow@2608
   560
paulson@5316
   561
Goal "k < (LEAST x. P(x)) ==> ~P(k::nat)";
nipkow@2608
   562
by (rtac notI 1);
paulson@5316
   563
by (etac (rewrite_rule [le_def] Least_le RS notE) 1 THEN assume_tac 1);
nipkow@2608
   564
qed "not_less_Least";
nipkow@2608
   565
nipkow@2608
   566
(*** Instantiation of transitivity prover ***)
nipkow@2608
   567
nipkow@2608
   568
structure Less_Arith =
nipkow@2608
   569
struct
nipkow@2608
   570
val nat_leI = leI;
nipkow@2608
   571
val nat_leD = leD;
nipkow@2608
   572
val lessI = lessI;
nipkow@2608
   573
val zero_less_Suc = zero_less_Suc;
nipkow@2608
   574
val less_reflE = less_irrefl;
nipkow@2608
   575
val less_zeroE = less_zeroE;
nipkow@2608
   576
val less_incr = Suc_mono;
nipkow@2608
   577
val less_decr = Suc_less_SucD;
nipkow@2608
   578
val less_incr_rhs = Suc_mono RS Suc_lessD;
nipkow@2608
   579
val less_decr_lhs = Suc_lessD;
nipkow@2608
   580
val less_trans_Suc = less_trans_Suc;
paulson@3343
   581
val leI = Suc_leI RS (Suc_le_mono RS iffD1);
nipkow@2608
   582
val not_lessI = leI RS leD
nipkow@2608
   583
val not_leI = prove_goal thy "!!m::nat. n < m ==> ~ m <= n"
nipkow@2608
   584
  (fn _ => [etac swap2 1, etac leD 1]);
nipkow@2608
   585
val eqI = prove_goal thy "!!m. [| m < Suc n; n < Suc m |] ==> m=n"
nipkow@2608
   586
  (fn _ => [etac less_SucE 1,
wenzelm@4089
   587
            blast_tac (claset() addSDs [Suc_less_SucD] addSEs [less_irrefl]
paulson@2891
   588
                              addDs [less_trans_Suc]) 1,
paulson@2935
   589
            assume_tac 1]);
nipkow@2608
   590
val leD = le_eq_less_Suc RS iffD1;
nipkow@2608
   591
val not_lessD = nat_leI RS leD;
nipkow@2608
   592
val not_leD = not_leE
nipkow@2608
   593
val eqD1 = prove_goal thy  "!!n. m = n ==> m < Suc n"
nipkow@2608
   594
 (fn _ => [etac subst 1, rtac lessI 1]);
nipkow@2608
   595
val eqD2 = sym RS eqD1;
nipkow@2608
   596
nipkow@2608
   597
fun is_zero(t) =  t = Const("0",Type("nat",[]));
nipkow@2608
   598
nipkow@2608
   599
fun nnb T = T = Type("fun",[Type("nat",[]),
nipkow@2608
   600
                            Type("fun",[Type("nat",[]),
nipkow@2608
   601
                                        Type("bool",[])])])
nipkow@2608
   602
nipkow@2608
   603
fun decomp_Suc(Const("Suc",_)$t) = let val (a,i) = decomp_Suc t in (a,i+1) end
nipkow@2608
   604
  | decomp_Suc t = (t,0);
nipkow@2608
   605
nipkow@2608
   606
fun decomp2(rel,T,lhs,rhs) =
nipkow@2608
   607
  if not(nnb T) then None else
nipkow@2608
   608
  let val (x,i) = decomp_Suc lhs
nipkow@2608
   609
      val (y,j) = decomp_Suc rhs
nipkow@2608
   610
  in case rel of
nipkow@2608
   611
       "op <"  => Some(x,i,"<",y,j)
nipkow@2608
   612
     | "op <=" => Some(x,i,"<=",y,j)
nipkow@2608
   613
     | "op ="  => Some(x,i,"=",y,j)
nipkow@2608
   614
     | _       => None
nipkow@2608
   615
  end;
nipkow@2608
   616
nipkow@2608
   617
fun negate(Some(x,i,rel,y,j)) = Some(x,i,"~"^rel,y,j)
nipkow@2608
   618
  | negate None = None;
nipkow@2608
   619
nipkow@2608
   620
fun decomp(_$(Const(rel,T)$lhs$rhs)) = decomp2(rel,T,lhs,rhs)
paulson@2718
   621
  | decomp(_$(Const("Not",_)$(Const(rel,T)$lhs$rhs))) =
nipkow@2608
   622
      negate(decomp2(rel,T,lhs,rhs))
nipkow@2608
   623
  | decomp _ = None
nipkow@2608
   624
nipkow@2608
   625
end;
nipkow@2608
   626
nipkow@2608
   627
structure Trans_Tac = Trans_Tac_Fun(Less_Arith);
nipkow@2608
   628
nipkow@2608
   629
open Trans_Tac;
nipkow@2608
   630
nipkow@2608
   631
(*** eliminates ~= in premises, which trans_tac cannot deal with ***)
paulson@4737
   632
bind_thm("nat_neqE", nat_neq_iff RS iffD1 RS disjE);