src/HOL/Arith.ML
author paulson
Thu Sep 12 10:32:43 1996 +0200 (1996-09-12)
changeset 1979 91c74763c5a3
parent 1909 f535276171d1
child 2007 968f78b52540
permissions -rw-r--r--
Change to best_tac required to prevent looping
clasohm@1465
     1
(*  Title:      HOL/Arith.ML
clasohm@923
     2
    ID:         $Id$
clasohm@1465
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@923
     4
    Copyright   1993  University of Cambridge
clasohm@923
     5
clasohm@923
     6
Proofs about elementary arithmetic: addition, multiplication, etc.
clasohm@923
     7
Tests definitions and simplifier.
clasohm@923
     8
*)
clasohm@923
     9
clasohm@923
    10
open Arith;
clasohm@923
    11
clasohm@923
    12
(*** Basic rewrite rules for the arithmetic operators ***)
clasohm@923
    13
clasohm@923
    14
val [pred_0, pred_Suc] = nat_recs pred_def;
clasohm@923
    15
val [add_0,add_Suc] = nat_recs add_def; 
berghofe@1767
    16
val [mult_0,mult_Suc] = nat_recs mult_def;
berghofe@1767
    17
store_thm("pred_0",pred_0);
berghofe@1767
    18
store_thm("pred_Suc",pred_Suc);
berghofe@1767
    19
store_thm("add_0",add_0);
berghofe@1767
    20
store_thm("add_Suc",add_Suc);
berghofe@1767
    21
store_thm("mult_0",mult_0);
berghofe@1767
    22
store_thm("mult_Suc",mult_Suc);
nipkow@1301
    23
Addsimps [pred_0,pred_Suc,add_0,add_Suc,mult_0,mult_Suc];
nipkow@1301
    24
nipkow@1301
    25
(** pred **)
nipkow@1301
    26
nipkow@1301
    27
val prems = goal Arith.thy "n ~= 0 ==> Suc(pred n) = n";
paulson@1552
    28
by (res_inst_tac [("n","n")] natE 1);
paulson@1552
    29
by (cut_facts_tac prems 1);
paulson@1552
    30
by (ALLGOALS Asm_full_simp_tac);
nipkow@1301
    31
qed "Suc_pred";
nipkow@1301
    32
Addsimps [Suc_pred];
clasohm@923
    33
clasohm@923
    34
(** Difference **)
clasohm@923
    35
nipkow@1655
    36
bind_thm("diff_0", diff_def RS def_nat_rec_0);
clasohm@923
    37
clasohm@923
    38
qed_goalw "diff_0_eq_0" Arith.thy [diff_def, pred_def]
clasohm@923
    39
    "0 - n = 0"
clasohm@1264
    40
 (fn _ => [nat_ind_tac "n" 1,  ALLGOALS Asm_simp_tac]);
clasohm@923
    41
clasohm@923
    42
(*Must simplify BEFORE the induction!!  (Else we get a critical pair)
clasohm@923
    43
  Suc(m) - Suc(n)   rewrites to   pred(Suc(m) - n)  *)
clasohm@923
    44
qed_goalw "diff_Suc_Suc" Arith.thy [diff_def, pred_def]
clasohm@923
    45
    "Suc(m) - Suc(n) = m - n"
clasohm@923
    46
 (fn _ =>
clasohm@1264
    47
  [Simp_tac 1, nat_ind_tac "n" 1, ALLGOALS Asm_simp_tac]);
clasohm@923
    48
nipkow@1301
    49
Addsimps [diff_0, diff_0_eq_0, diff_Suc_Suc];
clasohm@923
    50
clasohm@923
    51
paulson@1713
    52
goal Arith.thy "!!k. 0<k ==> EX j. k = Suc(j)";
paulson@1713
    53
by (etac rev_mp 1);
paulson@1713
    54
by (nat_ind_tac "k" 1);
paulson@1713
    55
by (Simp_tac 1);
berghofe@1760
    56
by (Fast_tac 1);
paulson@1713
    57
val lemma = result();
paulson@1713
    58
paulson@1713
    59
(* [| 0 < k; !!j. [| j: nat; k = succ(j) |] ==> Q |] ==> Q *)
paulson@1713
    60
bind_thm ("zero_less_natE", lemma RS exE);
paulson@1713
    61
paulson@1713
    62
paulson@1713
    63
clasohm@923
    64
(**** Inductive properties of the operators ****)
clasohm@923
    65
clasohm@923
    66
(*** Addition ***)
clasohm@923
    67
clasohm@923
    68
qed_goal "add_0_right" Arith.thy "m + 0 = m"
clasohm@1264
    69
 (fn _ => [nat_ind_tac "m" 1, ALLGOALS Asm_simp_tac]);
clasohm@923
    70
clasohm@923
    71
qed_goal "add_Suc_right" Arith.thy "m + Suc(n) = Suc(m+n)"
clasohm@1264
    72
 (fn _ => [nat_ind_tac "m" 1, ALLGOALS Asm_simp_tac]);
clasohm@923
    73
clasohm@1264
    74
Addsimps [add_0_right,add_Suc_right];
clasohm@923
    75
clasohm@923
    76
(*Associative law for addition*)
clasohm@923
    77
qed_goal "add_assoc" Arith.thy "(m + n) + k = m + ((n + k)::nat)"
clasohm@1264
    78
 (fn _ => [nat_ind_tac "m" 1, ALLGOALS Asm_simp_tac]);
clasohm@923
    79
clasohm@923
    80
(*Commutative law for addition*)  
clasohm@923
    81
qed_goal "add_commute" Arith.thy "m + n = n + (m::nat)"
clasohm@1264
    82
 (fn _ =>  [nat_ind_tac "m" 1, ALLGOALS Asm_simp_tac]);
clasohm@923
    83
clasohm@923
    84
qed_goal "add_left_commute" Arith.thy "x+(y+z)=y+((x+z)::nat)"
clasohm@923
    85
 (fn _ => [rtac (add_commute RS trans) 1, rtac (add_assoc RS trans) 1,
clasohm@923
    86
           rtac (add_commute RS arg_cong) 1]);
clasohm@923
    87
clasohm@923
    88
(*Addition is an AC-operator*)
clasohm@923
    89
val add_ac = [add_assoc, add_commute, add_left_commute];
clasohm@923
    90
clasohm@923
    91
goal Arith.thy "!!k::nat. (k + m = k + n) = (m=n)";
clasohm@923
    92
by (nat_ind_tac "k" 1);
clasohm@1264
    93
by (Simp_tac 1);
clasohm@1264
    94
by (Asm_simp_tac 1);
clasohm@923
    95
qed "add_left_cancel";
clasohm@923
    96
clasohm@923
    97
goal Arith.thy "!!k::nat. (m + k = n + k) = (m=n)";
clasohm@923
    98
by (nat_ind_tac "k" 1);
clasohm@1264
    99
by (Simp_tac 1);
clasohm@1264
   100
by (Asm_simp_tac 1);
clasohm@923
   101
qed "add_right_cancel";
clasohm@923
   102
clasohm@923
   103
goal Arith.thy "!!k::nat. (k + m <= k + n) = (m<=n)";
clasohm@923
   104
by (nat_ind_tac "k" 1);
clasohm@1264
   105
by (Simp_tac 1);
clasohm@1264
   106
by (Asm_simp_tac 1);
clasohm@923
   107
qed "add_left_cancel_le";
clasohm@923
   108
clasohm@923
   109
goal Arith.thy "!!k::nat. (k + m < k + n) = (m<n)";
clasohm@923
   110
by (nat_ind_tac "k" 1);
clasohm@1264
   111
by (Simp_tac 1);
clasohm@1264
   112
by (Asm_simp_tac 1);
clasohm@923
   113
qed "add_left_cancel_less";
clasohm@923
   114
nipkow@1327
   115
Addsimps [add_left_cancel, add_right_cancel,
nipkow@1327
   116
          add_left_cancel_le, add_left_cancel_less];
nipkow@1327
   117
nipkow@1327
   118
goal Arith.thy "(m+n = 0) = (m=0 & n=0)";
nipkow@1327
   119
by (nat_ind_tac "m" 1);
nipkow@1327
   120
by (ALLGOALS Asm_simp_tac);
nipkow@1327
   121
qed "add_is_0";
nipkow@1327
   122
Addsimps [add_is_0];
nipkow@1327
   123
nipkow@1327
   124
goal Arith.thy "!!n. n ~= 0 ==> m + pred n = pred(m+n)";
nipkow@1327
   125
by (nat_ind_tac "m" 1);
nipkow@1327
   126
by (ALLGOALS Asm_simp_tac);
nipkow@1327
   127
qed "add_pred";
nipkow@1327
   128
Addsimps [add_pred];
nipkow@1327
   129
clasohm@923
   130
(*** Multiplication ***)
clasohm@923
   131
clasohm@923
   132
(*right annihilation in product*)
clasohm@923
   133
qed_goal "mult_0_right" Arith.thy "m * 0 = 0"
clasohm@1264
   134
 (fn _ => [nat_ind_tac "m" 1, ALLGOALS Asm_simp_tac]);
clasohm@923
   135
clasohm@923
   136
(*right Sucessor law for multiplication*)
clasohm@923
   137
qed_goal "mult_Suc_right" Arith.thy  "m * Suc(n) = m + (m * n)"
clasohm@923
   138
 (fn _ => [nat_ind_tac "m" 1,
clasohm@1264
   139
           ALLGOALS(asm_simp_tac (!simpset addsimps add_ac))]);
clasohm@923
   140
clasohm@1264
   141
Addsimps [mult_0_right,mult_Suc_right];
clasohm@923
   142
paulson@1795
   143
goal Arith.thy "1 * n = n";
paulson@1795
   144
by (Asm_simp_tac 1);
paulson@1795
   145
qed "mult_1";
paulson@1795
   146
paulson@1795
   147
goal Arith.thy "n * 1 = n";
paulson@1795
   148
by (Asm_simp_tac 1);
paulson@1795
   149
qed "mult_1_right";
paulson@1795
   150
clasohm@923
   151
(*Commutative law for multiplication*)
clasohm@923
   152
qed_goal "mult_commute" Arith.thy "m * n = n * (m::nat)"
clasohm@1264
   153
 (fn _ => [nat_ind_tac "m" 1, ALLGOALS Asm_simp_tac]);
clasohm@923
   154
clasohm@923
   155
(*addition distributes over multiplication*)
clasohm@923
   156
qed_goal "add_mult_distrib" Arith.thy "(m + n)*k = (m*k) + ((n*k)::nat)"
clasohm@923
   157
 (fn _ => [nat_ind_tac "m" 1,
clasohm@1264
   158
           ALLGOALS(asm_simp_tac (!simpset addsimps add_ac))]);
clasohm@923
   159
clasohm@923
   160
qed_goal "add_mult_distrib2" Arith.thy "k*(m + n) = (k*m) + ((k*n)::nat)"
clasohm@923
   161
 (fn _ => [nat_ind_tac "m" 1,
clasohm@1264
   162
           ALLGOALS(asm_simp_tac (!simpset addsimps add_ac))]);
clasohm@923
   163
clasohm@923
   164
(*Associative law for multiplication*)
clasohm@923
   165
qed_goal "mult_assoc" Arith.thy "(m * n) * k = m * ((n * k)::nat)"
paulson@1795
   166
  (fn _ => [nat_ind_tac "m" 1, 
paulson@1795
   167
	    ALLGOALS (asm_simp_tac (!simpset addsimps [add_mult_distrib]))]);
clasohm@923
   168
clasohm@923
   169
qed_goal "mult_left_commute" Arith.thy "x*(y*z) = y*((x*z)::nat)"
clasohm@923
   170
 (fn _ => [rtac trans 1, rtac mult_commute 1, rtac trans 1,
clasohm@923
   171
           rtac mult_assoc 1, rtac (mult_commute RS arg_cong) 1]);
clasohm@923
   172
clasohm@923
   173
val mult_ac = [mult_assoc,mult_commute,mult_left_commute];
clasohm@923
   174
clasohm@923
   175
(*** Difference ***)
clasohm@923
   176
clasohm@923
   177
qed_goal "diff_self_eq_0" Arith.thy "m - m = 0"
clasohm@1264
   178
 (fn _ => [nat_ind_tac "m" 1, ALLGOALS Asm_simp_tac]);
nipkow@1496
   179
Addsimps [diff_self_eq_0];
clasohm@923
   180
clasohm@923
   181
(*Addition is the inverse of subtraction: if n<=m then n+(m-n) = m. *)
clasohm@923
   182
val [prem] = goal Arith.thy "[| ~ m<n |] ==> n+(m-n) = (m::nat)";
clasohm@923
   183
by (rtac (prem RS rev_mp) 1);
clasohm@923
   184
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
oheimb@1660
   185
by (ALLGOALS (Asm_simp_tac));
clasohm@923
   186
qed "add_diff_inverse";
clasohm@923
   187
clasohm@923
   188
clasohm@923
   189
(*** Remainder ***)
clasohm@923
   190
clasohm@923
   191
goal Arith.thy "m - n < Suc(m)";
clasohm@923
   192
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
clasohm@923
   193
by (etac less_SucE 3);
oheimb@1660
   194
by (ALLGOALS (asm_simp_tac (!simpset addsimps [less_Suc_eq])));
clasohm@923
   195
qed "diff_less_Suc";
clasohm@923
   196
clasohm@923
   197
goal Arith.thy "!!m::nat. m - n <= m";
clasohm@923
   198
by (res_inst_tac [("m","m"), ("n","n")] diff_induct 1);
clasohm@1264
   199
by (ALLGOALS Asm_simp_tac);
clasohm@923
   200
qed "diff_le_self";
clasohm@923
   201
clasohm@923
   202
goal Arith.thy "!!n::nat. (n+m) - n = m";
clasohm@923
   203
by (nat_ind_tac "n" 1);
clasohm@1264
   204
by (ALLGOALS Asm_simp_tac);
clasohm@923
   205
qed "diff_add_inverse";
clasohm@923
   206
paulson@1713
   207
goal Arith.thy "!!n::nat.(m+n) - n = m";
paulson@1713
   208
by (res_inst_tac [("m1","m")] (add_commute RS ssubst) 1);
paulson@1713
   209
by (REPEAT (ares_tac [diff_add_inverse] 1));
paulson@1713
   210
qed "diff_add_inverse2";
paulson@1713
   211
paulson@1713
   212
goal Arith.thy "!!k::nat. (k+m) - (k+n) = m - n";
paulson@1713
   213
by (nat_ind_tac "k" 1);
paulson@1713
   214
by (ALLGOALS Asm_simp_tac);
paulson@1713
   215
qed "diff_cancel";
paulson@1713
   216
Addsimps [diff_cancel];
paulson@1713
   217
paulson@1713
   218
goal Arith.thy "!!m::nat. (m+k) - (n+k) = m - n";
paulson@1713
   219
val add_commute_k = read_instantiate [("n","k")] add_commute;
paulson@1713
   220
by (asm_simp_tac (!simpset addsimps ([add_commute_k])) 1);
paulson@1713
   221
qed "diff_cancel2";
paulson@1713
   222
Addsimps [diff_cancel2];
paulson@1713
   223
clasohm@923
   224
goal Arith.thy "!!n::nat. n - (n+m) = 0";
clasohm@923
   225
by (nat_ind_tac "n" 1);
clasohm@1264
   226
by (ALLGOALS Asm_simp_tac);
clasohm@923
   227
qed "diff_add_0";
paulson@1713
   228
Addsimps [diff_add_0];
paulson@1713
   229
paulson@1713
   230
(** Difference distributes over multiplication **)
paulson@1713
   231
paulson@1713
   232
goal Arith.thy "!!m::nat. (m - n) * k = (m * k) - (n * k)";
paulson@1713
   233
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@1713
   234
by (ALLGOALS Asm_simp_tac);
paulson@1713
   235
qed "diff_mult_distrib" ;
paulson@1713
   236
paulson@1713
   237
goal Arith.thy "!!m::nat. k * (m - n) = (k * m) - (k * n)";
paulson@1713
   238
val mult_commute_k = read_instantiate [("m","k")] mult_commute;
paulson@1713
   239
by (simp_tac (!simpset addsimps [diff_mult_distrib, mult_commute_k]) 1);
paulson@1713
   240
qed "diff_mult_distrib2" ;
paulson@1713
   241
(*NOT added as rewrites, since sometimes they are used from right-to-left*)
paulson@1713
   242
paulson@1713
   243
paulson@1713
   244
(** Less-then properties **)
clasohm@923
   245
clasohm@923
   246
(*In ordinary notation: if 0<n and n<=m then m-n < m *)
clasohm@923
   247
goal Arith.thy "!!m. [| 0<n; ~ m<n |] ==> m - n < m";
clasohm@923
   248
by (subgoal_tac "0<n --> ~ m<n --> m - n < m" 1);
berghofe@1760
   249
by (Fast_tac 1);
clasohm@923
   250
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
clasohm@1264
   251
by (ALLGOALS(asm_simp_tac(!simpset addsimps [diff_less_Suc])));
nipkow@1398
   252
qed "diff_less";
clasohm@923
   253
clasohm@923
   254
val wf_less_trans = wf_pred_nat RS wf_trancl RSN (2, def_wfrec RS trans);
clasohm@923
   255
clasohm@972
   256
goalw Nat.thy [less_def] "(m,n) : pred_nat^+ = (m<n)";
clasohm@923
   257
by (rtac refl 1);
clasohm@923
   258
qed "less_eq";
clasohm@923
   259
clasohm@1475
   260
goal Arith.thy "(%m. m mod n) = wfrec (trancl pred_nat) \
clasohm@1475
   261
             \                      (%f j. if j<n then j else f (j-n))";
clasohm@1475
   262
by (simp_tac (HOL_ss addsimps [mod_def]) 1);
clasohm@1475
   263
val mod_def1 = result() RS eq_reflection;
clasohm@1475
   264
clasohm@923
   265
goal Arith.thy "!!m. m<n ==> m mod n = m";
clasohm@1475
   266
by (rtac (mod_def1 RS wf_less_trans) 1);
paulson@1552
   267
by (Asm_simp_tac 1);
clasohm@923
   268
qed "mod_less";
clasohm@923
   269
clasohm@923
   270
goal Arith.thy "!!m. [| 0<n;  ~m<n |] ==> m mod n = (m-n) mod n";
clasohm@1475
   271
by (rtac (mod_def1 RS wf_less_trans) 1);
paulson@1552
   272
by (asm_simp_tac (!simpset addsimps [diff_less, cut_apply, less_eq]) 1);
clasohm@923
   273
qed "mod_geq";
clasohm@923
   274
clasohm@923
   275
clasohm@923
   276
(*** Quotient ***)
clasohm@923
   277
clasohm@1475
   278
goal Arith.thy "(%m. m div n) = wfrec (trancl pred_nat) \
clasohm@1475
   279
                        \            (%f j. if j<n then 0 else Suc (f (j-n)))";
clasohm@1475
   280
by (simp_tac (HOL_ss addsimps [div_def]) 1);
clasohm@1475
   281
val div_def1 = result() RS eq_reflection;
clasohm@1475
   282
clasohm@923
   283
goal Arith.thy "!!m. m<n ==> m div n = 0";
clasohm@1475
   284
by (rtac (div_def1 RS wf_less_trans) 1);
paulson@1552
   285
by (Asm_simp_tac 1);
clasohm@923
   286
qed "div_less";
clasohm@923
   287
clasohm@923
   288
goal Arith.thy "!!M. [| 0<n;  ~m<n |] ==> m div n = Suc((m-n) div n)";
clasohm@1475
   289
by (rtac (div_def1 RS wf_less_trans) 1);
paulson@1552
   290
by (asm_simp_tac (!simpset addsimps [diff_less, cut_apply, less_eq]) 1);
clasohm@923
   291
qed "div_geq";
clasohm@923
   292
clasohm@923
   293
(*Main Result about quotient and remainder.*)
clasohm@923
   294
goal Arith.thy "!!m. 0<n ==> (m div n)*n + m mod n = m";
clasohm@923
   295
by (res_inst_tac [("n","m")] less_induct 1);
clasohm@923
   296
by (rename_tac "k" 1);    (*Variable name used in line below*)
clasohm@923
   297
by (case_tac "k<n" 1);
oheimb@1660
   298
by (ALLGOALS (asm_simp_tac(!simpset addsimps ([add_assoc] @
clasohm@923
   299
                       [mod_less, mod_geq, div_less, div_geq,
clasohm@1465
   300
                        add_diff_inverse, diff_less]))));
clasohm@923
   301
qed "mod_div_equality";
clasohm@923
   302
clasohm@923
   303
clasohm@923
   304
(*** More results about difference ***)
clasohm@923
   305
clasohm@923
   306
val [prem] = goal Arith.thy "m < Suc(n) ==> m-n = 0";
clasohm@923
   307
by (rtac (prem RS rev_mp) 1);
clasohm@923
   308
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
oheimb@1660
   309
by (asm_simp_tac (!simpset addsimps [less_Suc_eq]) 1);
oheimb@1660
   310
by (ALLGOALS (Asm_simp_tac));
clasohm@923
   311
qed "less_imp_diff_is_0";
clasohm@923
   312
clasohm@923
   313
val prems = goal Arith.thy "m-n = 0  -->  n-m = 0  -->  m=n";
clasohm@923
   314
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
clasohm@1264
   315
by (REPEAT(Simp_tac 1 THEN TRY(atac 1)));
nipkow@1485
   316
qed_spec_mp "diffs0_imp_equal";
clasohm@923
   317
clasohm@923
   318
val [prem] = goal Arith.thy "m<n ==> 0<n-m";
clasohm@923
   319
by (rtac (prem RS rev_mp) 1);
clasohm@923
   320
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
oheimb@1660
   321
by (ALLGOALS (Asm_simp_tac));
clasohm@923
   322
qed "less_imp_diff_positive";
clasohm@923
   323
clasohm@923
   324
val [prem] = goal Arith.thy "n < Suc(m) ==> Suc(m)-n = Suc(m-n)";
clasohm@923
   325
by (rtac (prem RS rev_mp) 1);
clasohm@923
   326
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
oheimb@1660
   327
by (ALLGOALS (Asm_simp_tac));
clasohm@923
   328
qed "Suc_diff_n";
clasohm@923
   329
nipkow@1398
   330
goal Arith.thy "Suc(m)-n = (if m<n then 0 else Suc(m-n))";
paulson@1552
   331
by (simp_tac (!simpset addsimps [less_imp_diff_is_0, not_less_eq, Suc_diff_n]
clasohm@923
   332
                    setloop (split_tac [expand_if])) 1);
clasohm@923
   333
qed "if_Suc_diff_n";
clasohm@923
   334
clasohm@923
   335
goal Arith.thy "P(k) --> (!n. P(Suc(n))--> P(n)) --> P(k-i)";
clasohm@923
   336
by (res_inst_tac [("m","k"),("n","i")] diff_induct 1);
berghofe@1760
   337
by (ALLGOALS (strip_tac THEN' Simp_tac THEN' TRY o Fast_tac));
clasohm@923
   338
qed "zero_induct_lemma";
clasohm@923
   339
clasohm@923
   340
val prems = goal Arith.thy "[| P(k);  !!n. P(Suc(n)) ==> P(n) |] ==> P(0)";
clasohm@923
   341
by (rtac (diff_self_eq_0 RS subst) 1);
clasohm@923
   342
by (rtac (zero_induct_lemma RS mp RS mp) 1);
clasohm@923
   343
by (REPEAT (ares_tac ([impI,allI]@prems) 1));
clasohm@923
   344
qed "zero_induct";
clasohm@923
   345
clasohm@923
   346
(*13 July 1992: loaded in 105.7s*)
clasohm@923
   347
paulson@1618
   348
paulson@1618
   349
(*** Further facts about mod (mainly for mutilated checkerboard ***)
paulson@1618
   350
paulson@1618
   351
goal Arith.thy
paulson@1618
   352
    "!!m n. 0<n ==> \
paulson@1618
   353
\           Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))";
paulson@1618
   354
by (res_inst_tac [("n","m")] less_induct 1);
paulson@1618
   355
by (excluded_middle_tac "Suc(na)<n" 1);
paulson@1618
   356
(* case Suc(na) < n *)
paulson@1618
   357
by (forward_tac [lessI RS less_trans] 2);
paulson@1618
   358
by (asm_simp_tac (!simpset addsimps [mod_less, less_not_refl2 RS not_sym]) 2);
paulson@1618
   359
(* case n <= Suc(na) *)
paulson@1618
   360
by (asm_full_simp_tac (!simpset addsimps [not_less_iff_le, mod_geq]) 1);
paulson@1618
   361
by (etac (le_imp_less_or_eq RS disjE) 1);
paulson@1618
   362
by (asm_simp_tac (!simpset addsimps [Suc_diff_n]) 1);
paulson@1618
   363
by (asm_full_simp_tac (!simpset addsimps [not_less_eq RS sym, 
paulson@1618
   364
                                          diff_less, mod_geq]) 1);
paulson@1618
   365
by (asm_simp_tac (!simpset addsimps [mod_less]) 1);
paulson@1618
   366
qed "mod_Suc";
paulson@1618
   367
paulson@1618
   368
goal Arith.thy "!!m n. 0<n ==> m mod n < n";
paulson@1618
   369
by (res_inst_tac [("n","m")] less_induct 1);
paulson@1618
   370
by (excluded_middle_tac "na<n" 1);
paulson@1618
   371
(*case na<n*)
paulson@1618
   372
by (asm_simp_tac (!simpset addsimps [mod_less]) 2);
paulson@1618
   373
(*case n le na*)
paulson@1618
   374
by (asm_full_simp_tac (!simpset addsimps [mod_geq, diff_less]) 1);
paulson@1618
   375
qed "mod_less_divisor";
paulson@1618
   376
paulson@1618
   377
paulson@1626
   378
(** Evens and Odds **)
paulson@1626
   379
paulson@1909
   380
(*With less_zeroE, causes case analysis on b<2*)
paulson@1909
   381
AddSEs [less_SucE];
berghofe@1760
   382
paulson@1626
   383
goal thy "!!k b. b<2 ==> k mod 2 = b | k mod 2 = (if b=1 then 0 else 1)";
paulson@1626
   384
by (subgoal_tac "k mod 2 < 2" 1);
paulson@1626
   385
by (asm_simp_tac (!simpset addsimps [mod_less_divisor]) 2);
paulson@1626
   386
by (asm_simp_tac (!simpset setloop split_tac [expand_if]) 1);
berghofe@1760
   387
by (Fast_tac 1);
paulson@1626
   388
qed "mod2_cases";
paulson@1626
   389
paulson@1626
   390
goal thy "Suc(Suc(m)) mod 2 = m mod 2";
paulson@1626
   391
by (subgoal_tac "m mod 2 < 2" 1);
paulson@1626
   392
by (asm_simp_tac (!simpset addsimps [mod_less_divisor]) 2);
paulson@1909
   393
by (Step_tac 1);
paulson@1626
   394
by (ALLGOALS (asm_simp_tac (!simpset addsimps [mod_Suc])));
paulson@1626
   395
qed "mod2_Suc_Suc";
paulson@1626
   396
Addsimps [mod2_Suc_Suc];
paulson@1626
   397
paulson@1626
   398
goal thy "(m+m) mod 2 = 0";
paulson@1626
   399
by (nat_ind_tac "m" 1);
paulson@1626
   400
by (simp_tac (!simpset addsimps [mod_less]) 1);
paulson@1626
   401
by (asm_simp_tac (!simpset addsimps [mod2_Suc_Suc, add_Suc_right]) 1);
paulson@1626
   402
qed "mod2_add_self";
paulson@1626
   403
Addsimps [mod2_add_self];
paulson@1626
   404
paulson@1909
   405
Delrules [less_SucE];
paulson@1909
   406
paulson@1626
   407
clasohm@923
   408
(**** Additional theorems about "less than" ****)
clasohm@923
   409
paulson@1909
   410
goal Arith.thy "? k::nat. n = n+k";
paulson@1909
   411
by (res_inst_tac [("x","0")] exI 1);
paulson@1909
   412
by (Simp_tac 1);
paulson@1909
   413
val lemma = result();
paulson@1909
   414
clasohm@923
   415
goal Arith.thy "!!m. m<n --> (? k. n=Suc(m+k))";
clasohm@923
   416
by (nat_ind_tac "n" 1);
paulson@1909
   417
by (ALLGOALS (simp_tac (!simpset addsimps [less_Suc_eq])));
paulson@1909
   418
by (step_tac (!claset addSIs [lemma]) 1);
clasohm@923
   419
by (res_inst_tac [("x","Suc(k)")] exI 1);
clasohm@1264
   420
by (Simp_tac 1);
nipkow@1485
   421
qed_spec_mp "less_eq_Suc_add";
clasohm@923
   422
clasohm@923
   423
goal Arith.thy "n <= ((m + n)::nat)";
clasohm@923
   424
by (nat_ind_tac "m" 1);
clasohm@1264
   425
by (ALLGOALS Simp_tac);
clasohm@923
   426
by (etac le_trans 1);
clasohm@923
   427
by (rtac (lessI RS less_imp_le) 1);
clasohm@923
   428
qed "le_add2";
clasohm@923
   429
clasohm@923
   430
goal Arith.thy "n <= ((n + m)::nat)";
clasohm@1264
   431
by (simp_tac (!simpset addsimps add_ac) 1);
clasohm@923
   432
by (rtac le_add2 1);
clasohm@923
   433
qed "le_add1";
clasohm@923
   434
clasohm@923
   435
bind_thm ("less_add_Suc1", (lessI RS (le_add1 RS le_less_trans)));
clasohm@923
   436
bind_thm ("less_add_Suc2", (lessI RS (le_add2 RS le_less_trans)));
clasohm@923
   437
clasohm@923
   438
(*"i <= j ==> i <= j+m"*)
clasohm@923
   439
bind_thm ("trans_le_add1", le_add1 RSN (2,le_trans));
clasohm@923
   440
clasohm@923
   441
(*"i <= j ==> i <= m+j"*)
clasohm@923
   442
bind_thm ("trans_le_add2", le_add2 RSN (2,le_trans));
clasohm@923
   443
clasohm@923
   444
(*"i < j ==> i < j+m"*)
clasohm@923
   445
bind_thm ("trans_less_add1", le_add1 RSN (2,less_le_trans));
clasohm@923
   446
clasohm@923
   447
(*"i < j ==> i < m+j"*)
clasohm@923
   448
bind_thm ("trans_less_add2", le_add2 RSN (2,less_le_trans));
clasohm@923
   449
nipkow@1152
   450
goal Arith.thy "!!i. i+j < (k::nat) ==> i<k";
paulson@1552
   451
by (etac rev_mp 1);
paulson@1552
   452
by (nat_ind_tac "j" 1);
clasohm@1264
   453
by (ALLGOALS Asm_simp_tac);
berghofe@1760
   454
by (fast_tac (!claset addDs [Suc_lessD]) 1);
nipkow@1152
   455
qed "add_lessD1";
nipkow@1152
   456
clasohm@923
   457
goal Arith.thy "!!k::nat. m <= n ==> m <= n+k";
paulson@1552
   458
by (etac le_trans 1);
paulson@1552
   459
by (rtac le_add1 1);
clasohm@923
   460
qed "le_imp_add_le";
clasohm@923
   461
clasohm@923
   462
goal Arith.thy "!!k::nat. m < n ==> m < n+k";
paulson@1552
   463
by (etac less_le_trans 1);
paulson@1552
   464
by (rtac le_add1 1);
clasohm@923
   465
qed "less_imp_add_less";
clasohm@923
   466
clasohm@923
   467
goal Arith.thy "m+k<=n --> m<=(n::nat)";
clasohm@923
   468
by (nat_ind_tac "k" 1);
clasohm@1264
   469
by (ALLGOALS Asm_simp_tac);
berghofe@1760
   470
by (fast_tac (!claset addDs [Suc_leD]) 1);
nipkow@1485
   471
qed_spec_mp "add_leD1";
clasohm@923
   472
clasohm@923
   473
goal Arith.thy "!!k l::nat. [| k<l; m+l = k+n |] ==> m<n";
berghofe@1786
   474
by (safe_tac (!claset addSDs [less_eq_Suc_add]));
clasohm@923
   475
by (asm_full_simp_tac
clasohm@1264
   476
    (!simpset delsimps [add_Suc_right]
clasohm@1264
   477
                addsimps ([add_Suc_right RS sym, add_left_cancel] @add_ac)) 1);
paulson@1552
   478
by (etac subst 1);
clasohm@1264
   479
by (simp_tac (!simpset addsimps [less_add_Suc1]) 1);
clasohm@923
   480
qed "less_add_eq_less";
clasohm@923
   481
clasohm@923
   482
paulson@1713
   483
(*** Monotonicity of Addition ***)
clasohm@923
   484
clasohm@923
   485
(*strict, in 1st argument*)
clasohm@923
   486
goal Arith.thy "!!i j k::nat. i < j ==> i + k < j + k";
clasohm@923
   487
by (nat_ind_tac "k" 1);
clasohm@1264
   488
by (ALLGOALS Asm_simp_tac);
clasohm@923
   489
qed "add_less_mono1";
clasohm@923
   490
clasohm@923
   491
(*strict, in both arguments*)
clasohm@923
   492
goal Arith.thy "!!i j k::nat. [|i < j; k < l|] ==> i + k < j + l";
clasohm@923
   493
by (rtac (add_less_mono1 RS less_trans) 1);
lcp@1198
   494
by (REPEAT (assume_tac 1));
clasohm@923
   495
by (nat_ind_tac "j" 1);
clasohm@1264
   496
by (ALLGOALS Asm_simp_tac);
clasohm@923
   497
qed "add_less_mono";
clasohm@923
   498
clasohm@923
   499
(*A [clumsy] way of lifting < monotonicity to <= monotonicity *)
clasohm@923
   500
val [lt_mono,le] = goal Arith.thy
clasohm@1465
   501
     "[| !!i j::nat. i<j ==> f(i) < f(j);       \
clasohm@1465
   502
\        i <= j                                 \
clasohm@923
   503
\     |] ==> f(i) <= (f(j)::nat)";
clasohm@923
   504
by (cut_facts_tac [le] 1);
clasohm@1264
   505
by (asm_full_simp_tac (!simpset addsimps [le_eq_less_or_eq]) 1);
berghofe@1760
   506
by (fast_tac (!claset addSIs [lt_mono]) 1);
clasohm@923
   507
qed "less_mono_imp_le_mono";
clasohm@923
   508
clasohm@923
   509
(*non-strict, in 1st argument*)
clasohm@923
   510
goal Arith.thy "!!i j k::nat. i<=j ==> i + k <= j + k";
clasohm@923
   511
by (res_inst_tac [("f", "%j.j+k")] less_mono_imp_le_mono 1);
paulson@1552
   512
by (etac add_less_mono1 1);
clasohm@923
   513
by (assume_tac 1);
clasohm@923
   514
qed "add_le_mono1";
clasohm@923
   515
clasohm@923
   516
(*non-strict, in both arguments*)
clasohm@923
   517
goal Arith.thy "!!k l::nat. [|i<=j;  k<=l |] ==> i + k <= j + l";
clasohm@923
   518
by (etac (add_le_mono1 RS le_trans) 1);
clasohm@1264
   519
by (simp_tac (!simpset addsimps [add_commute]) 1);
clasohm@923
   520
(*j moves to the end because it is free while k, l are bound*)
paulson@1552
   521
by (etac add_le_mono1 1);
clasohm@923
   522
qed "add_le_mono";
paulson@1713
   523
paulson@1713
   524
(*** Monotonicity of Multiplication ***)
paulson@1713
   525
paulson@1713
   526
goal Arith.thy "!!i::nat. i<=j ==> i*k<=j*k";
paulson@1713
   527
by (nat_ind_tac "k" 1);
paulson@1713
   528
by (ALLGOALS (asm_simp_tac (!simpset addsimps [add_le_mono])));
paulson@1713
   529
qed "mult_le_mono1";
paulson@1713
   530
paulson@1713
   531
(*<=monotonicity, BOTH arguments*)
paulson@1713
   532
goal Arith.thy "!!i::nat. [| i<=j; k<=l |] ==> i*k<=j*l";
paulson@1713
   533
by (rtac le_trans 1);
paulson@1713
   534
by (ALLGOALS 
berghofe@1786
   535
    (deepen_tac (!claset addIs [mult_commute RS ssubst, mult_le_mono1]) 0));
paulson@1713
   536
qed "mult_le_mono";
paulson@1713
   537
paulson@1713
   538
(*strict, in 1st argument; proof is by induction on k>0*)
paulson@1713
   539
goal Arith.thy "!!i::nat. [| i<j; 0<k |] ==> k*i < k*j";
paulson@1713
   540
be zero_less_natE 1;
paulson@1713
   541
by (Asm_simp_tac 1);
paulson@1713
   542
by (nat_ind_tac "x" 1);
paulson@1713
   543
by (ALLGOALS (asm_simp_tac (!simpset addsimps [add_less_mono])));
paulson@1713
   544
qed "mult_less_mono2";
paulson@1713
   545
paulson@1713
   546
goal Arith.thy "(0 < m*n) = (0<m & 0<n)";
paulson@1713
   547
by (nat_ind_tac "m" 1);
paulson@1713
   548
by (nat_ind_tac "n" 2);
paulson@1713
   549
by (ALLGOALS Asm_simp_tac);
paulson@1713
   550
qed "zero_less_mult_iff";
paulson@1713
   551
paulson@1795
   552
goal Arith.thy "(m*n = 1) = (m=1 & n=1)";
paulson@1795
   553
by (nat_ind_tac "m" 1);
paulson@1795
   554
by (Simp_tac 1);
paulson@1795
   555
by (nat_ind_tac "n" 1);
paulson@1795
   556
by (Simp_tac 1);
paulson@1795
   557
by (fast_tac (!claset addss !simpset) 1);
paulson@1795
   558
qed "mult_eq_1_iff";
paulson@1795
   559
paulson@1713
   560
(*Cancellation law for division*)
paulson@1713
   561
goal Arith.thy "!!k. [| 0<n; 0<k |] ==> (k*m) div (k*n) = m div n";
paulson@1713
   562
by (res_inst_tac [("n","m")] less_induct 1);
paulson@1713
   563
by (case_tac "na<n" 1);
paulson@1713
   564
by (asm_simp_tac (!simpset addsimps [div_less, zero_less_mult_iff, 
paulson@1713
   565
				     mult_less_mono2]) 1);
paulson@1713
   566
by (subgoal_tac "~ k*na < k*n" 1);
paulson@1713
   567
by (asm_simp_tac
paulson@1713
   568
     (!simpset addsimps [zero_less_mult_iff, div_geq,
paulson@1713
   569
			 diff_mult_distrib2 RS sym, diff_less]) 1);
paulson@1713
   570
by (asm_full_simp_tac (!simpset addsimps [not_less_iff_le, 
paulson@1713
   571
					  le_refl RS mult_le_mono]) 1);
paulson@1713
   572
qed "div_cancel";
paulson@1713
   573
paulson@1713
   574
goal Arith.thy "!!k. [| 0<n; 0<k |] ==> (k*m) mod (k*n) = k * (m mod n)";
paulson@1713
   575
by (res_inst_tac [("n","m")] less_induct 1);
paulson@1713
   576
by (case_tac "na<n" 1);
paulson@1713
   577
by (asm_simp_tac (!simpset addsimps [mod_less, zero_less_mult_iff, 
paulson@1713
   578
				     mult_less_mono2]) 1);
paulson@1713
   579
by (subgoal_tac "~ k*na < k*n" 1);
paulson@1713
   580
by (asm_simp_tac
paulson@1713
   581
     (!simpset addsimps [zero_less_mult_iff, mod_geq,
paulson@1713
   582
			 diff_mult_distrib2 RS sym, diff_less]) 1);
paulson@1713
   583
by (asm_full_simp_tac (!simpset addsimps [not_less_iff_le, 
paulson@1713
   584
					  le_refl RS mult_le_mono]) 1);
paulson@1713
   585
qed "mult_mod_distrib";
paulson@1713
   586
paulson@1713
   587
paulson@1795
   588
(** Lemma for gcd **)
paulson@1795
   589
paulson@1795
   590
goal Arith.thy "!!m n. m = m*n ==> n=1 | m=0";
paulson@1795
   591
by (dtac sym 1);
paulson@1795
   592
by (rtac disjCI 1);
paulson@1795
   593
by (rtac nat_less_cases 1 THEN assume_tac 2);
paulson@1909
   594
by (fast_tac (!claset addSEs [less_SucE] addss !simpset) 1);
paulson@1979
   595
by (best_tac (!claset addDs [mult_less_mono2] 
paulson@1795
   596
                      addss (!simpset addsimps [zero_less_eq RS sym])) 1);
paulson@1795
   597
qed "mult_eq_self_implies_10";
paulson@1795
   598
paulson@1795
   599