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