src/HOL/Arith.ML
author paulson
Tue May 27 13:03:41 1997 +0200 (1997-05-27)
changeset 3352 04502e5431fb
parent 3339 cfa72a70f2b5
child 3366 2402c6ab1561
permissions -rw-r--r--
New theorems suggested by Florian Kammueller
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.
paulson@3234
     7
Some from the Hoare example from Norbert Galm
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
nipkow@2099
    14
goalw Arith.thy [pred_def] "pred 0 = 0";
nipkow@2099
    15
by(Simp_tac 1);
nipkow@2099
    16
qed "pred_0";
nipkow@2099
    17
nipkow@2099
    18
goalw Arith.thy [pred_def] "pred(Suc n) = n";
nipkow@2099
    19
by(Simp_tac 1);
nipkow@2099
    20
qed "pred_Suc";
nipkow@2099
    21
pusch@2682
    22
Addsimps [pred_0,pred_Suc];
nipkow@1301
    23
nipkow@1301
    24
(** pred **)
nipkow@1301
    25
nipkow@1301
    26
val prems = goal Arith.thy "n ~= 0 ==> Suc(pred n) = n";
paulson@1552
    27
by (res_inst_tac [("n","n")] natE 1);
paulson@1552
    28
by (cut_facts_tac prems 1);
paulson@1552
    29
by (ALLGOALS Asm_full_simp_tac);
nipkow@1301
    30
qed "Suc_pred";
nipkow@1301
    31
Addsimps [Suc_pred];
clasohm@923
    32
clasohm@923
    33
(** Difference **)
clasohm@923
    34
pusch@2682
    35
qed_goalw "diff_0_eq_0" Arith.thy [pred_def]
clasohm@923
    36
    "0 - n = 0"
paulson@3339
    37
 (fn _ => [induct_tac "n" 1,  ALLGOALS Asm_simp_tac]);
clasohm@923
    38
clasohm@923
    39
(*Must simplify BEFORE the induction!!  (Else we get a critical pair)
clasohm@923
    40
  Suc(m) - Suc(n)   rewrites to   pred(Suc(m) - n)  *)
pusch@2682
    41
qed_goalw "diff_Suc_Suc" Arith.thy [pred_def]
clasohm@923
    42
    "Suc(m) - Suc(n) = m - n"
clasohm@923
    43
 (fn _ =>
paulson@3339
    44
  [Simp_tac 1, induct_tac "n" 1, ALLGOALS Asm_simp_tac]);
clasohm@923
    45
pusch@2682
    46
Addsimps [diff_0_eq_0, diff_Suc_Suc];
clasohm@923
    47
clasohm@923
    48
clasohm@923
    49
(**** Inductive properties of the operators ****)
clasohm@923
    50
clasohm@923
    51
(*** Addition ***)
clasohm@923
    52
clasohm@923
    53
qed_goal "add_0_right" Arith.thy "m + 0 = m"
paulson@3339
    54
 (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
clasohm@923
    55
clasohm@923
    56
qed_goal "add_Suc_right" Arith.thy "m + Suc(n) = Suc(m+n)"
paulson@3339
    57
 (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
clasohm@923
    58
clasohm@1264
    59
Addsimps [add_0_right,add_Suc_right];
clasohm@923
    60
clasohm@923
    61
(*Associative law for addition*)
clasohm@923
    62
qed_goal "add_assoc" Arith.thy "(m + n) + k = m + ((n + k)::nat)"
paulson@3339
    63
 (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
clasohm@923
    64
clasohm@923
    65
(*Commutative law for addition*)  
clasohm@923
    66
qed_goal "add_commute" Arith.thy "m + n = n + (m::nat)"
paulson@3339
    67
 (fn _ =>  [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
clasohm@923
    68
clasohm@923
    69
qed_goal "add_left_commute" Arith.thy "x+(y+z)=y+((x+z)::nat)"
clasohm@923
    70
 (fn _ => [rtac (add_commute RS trans) 1, rtac (add_assoc RS trans) 1,
clasohm@923
    71
           rtac (add_commute RS arg_cong) 1]);
clasohm@923
    72
clasohm@923
    73
(*Addition is an AC-operator*)
clasohm@923
    74
val add_ac = [add_assoc, add_commute, add_left_commute];
clasohm@923
    75
clasohm@923
    76
goal Arith.thy "!!k::nat. (k + m = k + n) = (m=n)";
paulson@3339
    77
by (induct_tac "k" 1);
clasohm@1264
    78
by (Simp_tac 1);
clasohm@1264
    79
by (Asm_simp_tac 1);
clasohm@923
    80
qed "add_left_cancel";
clasohm@923
    81
clasohm@923
    82
goal Arith.thy "!!k::nat. (m + k = n + k) = (m=n)";
paulson@3339
    83
by (induct_tac "k" 1);
clasohm@1264
    84
by (Simp_tac 1);
clasohm@1264
    85
by (Asm_simp_tac 1);
clasohm@923
    86
qed "add_right_cancel";
clasohm@923
    87
clasohm@923
    88
goal Arith.thy "!!k::nat. (k + m <= k + n) = (m<=n)";
paulson@3339
    89
by (induct_tac "k" 1);
clasohm@1264
    90
by (Simp_tac 1);
clasohm@1264
    91
by (Asm_simp_tac 1);
clasohm@923
    92
qed "add_left_cancel_le";
clasohm@923
    93
clasohm@923
    94
goal Arith.thy "!!k::nat. (k + m < k + n) = (m<n)";
paulson@3339
    95
by (induct_tac "k" 1);
clasohm@1264
    96
by (Simp_tac 1);
clasohm@1264
    97
by (Asm_simp_tac 1);
clasohm@923
    98
qed "add_left_cancel_less";
clasohm@923
    99
nipkow@1327
   100
Addsimps [add_left_cancel, add_right_cancel,
nipkow@1327
   101
          add_left_cancel_le, add_left_cancel_less];
nipkow@1327
   102
paulson@3339
   103
(** Reasoning about m+0=0, etc. **)
paulson@3339
   104
nipkow@1327
   105
goal Arith.thy "(m+n = 0) = (m=0 & n=0)";
paulson@3339
   106
by (induct_tac "m" 1);
nipkow@1327
   107
by (ALLGOALS Asm_simp_tac);
nipkow@1327
   108
qed "add_is_0";
nipkow@1327
   109
Addsimps [add_is_0];
nipkow@1327
   110
paulson@3293
   111
goal Arith.thy "(pred (m+n) = 0) = (m=0 & pred n = 0 | pred m = 0 & n=0)";
paulson@3339
   112
by (induct_tac "m" 1);
paulson@3293
   113
by (ALLGOALS (fast_tac (!claset addss (!simpset))));
paulson@3293
   114
qed "pred_add_is_0";
paulson@3293
   115
Addsimps [pred_add_is_0];
paulson@3293
   116
nipkow@1327
   117
goal Arith.thy "!!n. n ~= 0 ==> m + pred n = pred(m+n)";
paulson@3339
   118
by (induct_tac "m" 1);
nipkow@1327
   119
by (ALLGOALS Asm_simp_tac);
nipkow@1327
   120
qed "add_pred";
nipkow@1327
   121
Addsimps [add_pred];
nipkow@1327
   122
paulson@1626
   123
clasohm@923
   124
(**** Additional theorems about "less than" ****)
clasohm@923
   125
paulson@3339
   126
goal Arith.thy "i<j --> (EX k. j = Suc(i+k))";
paulson@3339
   127
by (induct_tac "j" 1);
paulson@1909
   128
by (Simp_tac 1);
paulson@3339
   129
by (blast_tac (!claset addSEs [less_SucE] 
paulson@3339
   130
                       addSIs [add_0_right RS sym, add_Suc_right RS sym]) 1);
paulson@1909
   131
val lemma = result();
paulson@1909
   132
paulson@3339
   133
(* [| i<j;  !!x. j = Suc(i+x) ==> Q |] ==> Q *)
paulson@3339
   134
bind_thm ("less_natE", lemma RS mp RS exE);
paulson@3339
   135
clasohm@923
   136
goal Arith.thy "!!m. m<n --> (? k. n=Suc(m+k))";
paulson@3339
   137
by (induct_tac "n" 1);
paulson@1909
   138
by (ALLGOALS (simp_tac (!simpset addsimps [less_Suc_eq])));
paulson@3339
   139
by (blast_tac (!claset addSEs [less_SucE] 
paulson@3339
   140
                       addSIs [add_0_right RS sym, add_Suc_right RS sym]) 1);
nipkow@1485
   141
qed_spec_mp "less_eq_Suc_add";
clasohm@923
   142
clasohm@923
   143
goal Arith.thy "n <= ((m + n)::nat)";
paulson@3339
   144
by (induct_tac "m" 1);
clasohm@1264
   145
by (ALLGOALS Simp_tac);
clasohm@923
   146
by (etac le_trans 1);
clasohm@923
   147
by (rtac (lessI RS less_imp_le) 1);
clasohm@923
   148
qed "le_add2";
clasohm@923
   149
clasohm@923
   150
goal Arith.thy "n <= ((n + m)::nat)";
clasohm@1264
   151
by (simp_tac (!simpset addsimps add_ac) 1);
clasohm@923
   152
by (rtac le_add2 1);
clasohm@923
   153
qed "le_add1";
clasohm@923
   154
clasohm@923
   155
bind_thm ("less_add_Suc1", (lessI RS (le_add1 RS le_less_trans)));
clasohm@923
   156
bind_thm ("less_add_Suc2", (lessI RS (le_add2 RS le_less_trans)));
clasohm@923
   157
clasohm@923
   158
(*"i <= j ==> i <= j+m"*)
clasohm@923
   159
bind_thm ("trans_le_add1", le_add1 RSN (2,le_trans));
clasohm@923
   160
clasohm@923
   161
(*"i <= j ==> i <= m+j"*)
clasohm@923
   162
bind_thm ("trans_le_add2", le_add2 RSN (2,le_trans));
clasohm@923
   163
clasohm@923
   164
(*"i < j ==> i < j+m"*)
clasohm@923
   165
bind_thm ("trans_less_add1", le_add1 RSN (2,less_le_trans));
clasohm@923
   166
clasohm@923
   167
(*"i < j ==> i < m+j"*)
clasohm@923
   168
bind_thm ("trans_less_add2", le_add2 RSN (2,less_le_trans));
clasohm@923
   169
nipkow@1152
   170
goal Arith.thy "!!i. i+j < (k::nat) ==> i<k";
paulson@1552
   171
by (etac rev_mp 1);
paulson@3339
   172
by (induct_tac "j" 1);
clasohm@1264
   173
by (ALLGOALS Asm_simp_tac);
paulson@2922
   174
by (blast_tac (!claset addDs [Suc_lessD]) 1);
nipkow@1152
   175
qed "add_lessD1";
nipkow@1152
   176
paulson@3234
   177
goal Arith.thy "!!i::nat. ~ (i+j < i)";
paulson@3234
   178
br notI 1;
paulson@3234
   179
be (add_lessD1 RS less_irrefl) 1;
paulson@3234
   180
qed "not_add_less1";
paulson@3234
   181
paulson@3234
   182
goal Arith.thy "!!i::nat. ~ (j+i < i)";
paulson@3234
   183
by (simp_tac (!simpset addsimps [add_commute, not_add_less1]) 1);
paulson@3234
   184
qed "not_add_less2";
paulson@3234
   185
AddIffs [not_add_less1, not_add_less2];
paulson@3234
   186
clasohm@923
   187
goal Arith.thy "!!k::nat. m <= n ==> m <= n+k";
paulson@1552
   188
by (etac le_trans 1);
paulson@1552
   189
by (rtac le_add1 1);
clasohm@923
   190
qed "le_imp_add_le";
clasohm@923
   191
clasohm@923
   192
goal Arith.thy "!!k::nat. m < n ==> m < n+k";
paulson@1552
   193
by (etac less_le_trans 1);
paulson@1552
   194
by (rtac le_add1 1);
clasohm@923
   195
qed "less_imp_add_less";
clasohm@923
   196
clasohm@923
   197
goal Arith.thy "m+k<=n --> m<=(n::nat)";
paulson@3339
   198
by (induct_tac "k" 1);
clasohm@1264
   199
by (ALLGOALS Asm_simp_tac);
paulson@2922
   200
by (blast_tac (!claset addDs [Suc_leD]) 1);
nipkow@1485
   201
qed_spec_mp "add_leD1";
clasohm@923
   202
paulson@2498
   203
goal Arith.thy "!!n::nat. m+k<=n ==> k<=n";
paulson@2498
   204
by (full_simp_tac (!simpset addsimps [add_commute]) 1);
paulson@2498
   205
by (etac add_leD1 1);
paulson@2498
   206
qed_spec_mp "add_leD2";
paulson@2498
   207
paulson@2498
   208
goal Arith.thy "!!n::nat. m+k<=n ==> m<=n & k<=n";
paulson@2922
   209
by (blast_tac (!claset addDs [add_leD1, add_leD2]) 1);
paulson@2498
   210
bind_thm ("add_leE", result() RS conjE);
paulson@2498
   211
clasohm@923
   212
goal Arith.thy "!!k l::nat. [| k<l; m+l = k+n |] ==> m<n";
berghofe@1786
   213
by (safe_tac (!claset addSDs [less_eq_Suc_add]));
clasohm@923
   214
by (asm_full_simp_tac
clasohm@1264
   215
    (!simpset delsimps [add_Suc_right]
clasohm@1264
   216
                addsimps ([add_Suc_right RS sym, add_left_cancel] @add_ac)) 1);
paulson@1552
   217
by (etac subst 1);
clasohm@1264
   218
by (simp_tac (!simpset addsimps [less_add_Suc1]) 1);
clasohm@923
   219
qed "less_add_eq_less";
clasohm@923
   220
clasohm@923
   221
paulson@1713
   222
(*** Monotonicity of Addition ***)
clasohm@923
   223
clasohm@923
   224
(*strict, in 1st argument*)
clasohm@923
   225
goal Arith.thy "!!i j k::nat. i < j ==> i + k < j + k";
paulson@3339
   226
by (induct_tac "k" 1);
clasohm@1264
   227
by (ALLGOALS Asm_simp_tac);
clasohm@923
   228
qed "add_less_mono1";
clasohm@923
   229
clasohm@923
   230
(*strict, in both arguments*)
clasohm@923
   231
goal Arith.thy "!!i j k::nat. [|i < j; k < l|] ==> i + k < j + l";
clasohm@923
   232
by (rtac (add_less_mono1 RS less_trans) 1);
lcp@1198
   233
by (REPEAT (assume_tac 1));
paulson@3339
   234
by (induct_tac "j" 1);
clasohm@1264
   235
by (ALLGOALS Asm_simp_tac);
clasohm@923
   236
qed "add_less_mono";
clasohm@923
   237
clasohm@923
   238
(*A [clumsy] way of lifting < monotonicity to <= monotonicity *)
clasohm@923
   239
val [lt_mono,le] = goal Arith.thy
clasohm@1465
   240
     "[| !!i j::nat. i<j ==> f(i) < f(j);       \
clasohm@1465
   241
\        i <= j                                 \
clasohm@923
   242
\     |] ==> f(i) <= (f(j)::nat)";
clasohm@923
   243
by (cut_facts_tac [le] 1);
clasohm@1264
   244
by (asm_full_simp_tac (!simpset addsimps [le_eq_less_or_eq]) 1);
paulson@2922
   245
by (blast_tac (!claset addSIs [lt_mono]) 1);
clasohm@923
   246
qed "less_mono_imp_le_mono";
clasohm@923
   247
clasohm@923
   248
(*non-strict, in 1st argument*)
clasohm@923
   249
goal Arith.thy "!!i j k::nat. i<=j ==> i + k <= j + k";
clasohm@923
   250
by (res_inst_tac [("f", "%j.j+k")] less_mono_imp_le_mono 1);
paulson@1552
   251
by (etac add_less_mono1 1);
clasohm@923
   252
by (assume_tac 1);
clasohm@923
   253
qed "add_le_mono1";
clasohm@923
   254
clasohm@923
   255
(*non-strict, in both arguments*)
clasohm@923
   256
goal Arith.thy "!!k l::nat. [|i<=j;  k<=l |] ==> i + k <= j + l";
clasohm@923
   257
by (etac (add_le_mono1 RS le_trans) 1);
clasohm@1264
   258
by (simp_tac (!simpset addsimps [add_commute]) 1);
clasohm@923
   259
(*j moves to the end because it is free while k, l are bound*)
paulson@1552
   260
by (etac add_le_mono1 1);
clasohm@923
   261
qed "add_le_mono";
paulson@1713
   262
paulson@3234
   263
paulson@3234
   264
(*** Multiplication ***)
paulson@3234
   265
paulson@3234
   266
(*right annihilation in product*)
paulson@3234
   267
qed_goal "mult_0_right" Arith.thy "m * 0 = 0"
paulson@3339
   268
 (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
paulson@3234
   269
paulson@3293
   270
(*right successor law for multiplication*)
paulson@3234
   271
qed_goal "mult_Suc_right" Arith.thy  "m * Suc(n) = m + (m * n)"
paulson@3339
   272
 (fn _ => [induct_tac "m" 1,
paulson@3234
   273
           ALLGOALS(asm_simp_tac (!simpset addsimps add_ac))]);
paulson@3234
   274
paulson@3293
   275
Addsimps [mult_0_right, mult_Suc_right];
paulson@3234
   276
paulson@3234
   277
goal Arith.thy "1 * n = n";
paulson@3234
   278
by (Asm_simp_tac 1);
paulson@3234
   279
qed "mult_1";
paulson@3234
   280
paulson@3234
   281
goal Arith.thy "n * 1 = n";
paulson@3234
   282
by (Asm_simp_tac 1);
paulson@3234
   283
qed "mult_1_right";
paulson@3234
   284
paulson@3234
   285
(*Commutative law for multiplication*)
paulson@3234
   286
qed_goal "mult_commute" Arith.thy "m * n = n * (m::nat)"
paulson@3339
   287
 (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
paulson@3234
   288
paulson@3234
   289
(*addition distributes over multiplication*)
paulson@3234
   290
qed_goal "add_mult_distrib" Arith.thy "(m + n)*k = (m*k) + ((n*k)::nat)"
paulson@3339
   291
 (fn _ => [induct_tac "m" 1,
paulson@3234
   292
           ALLGOALS(asm_simp_tac (!simpset addsimps add_ac))]);
paulson@3234
   293
paulson@3234
   294
qed_goal "add_mult_distrib2" Arith.thy "k*(m + n) = (k*m) + ((k*n)::nat)"
paulson@3339
   295
 (fn _ => [induct_tac "m" 1,
paulson@3234
   296
           ALLGOALS(asm_simp_tac (!simpset addsimps add_ac))]);
paulson@3234
   297
paulson@3234
   298
(*Associative law for multiplication*)
paulson@3234
   299
qed_goal "mult_assoc" Arith.thy "(m * n) * k = m * ((n * k)::nat)"
paulson@3339
   300
  (fn _ => [induct_tac "m" 1, 
paulson@3234
   301
            ALLGOALS (asm_simp_tac (!simpset addsimps [add_mult_distrib]))]);
paulson@3234
   302
paulson@3234
   303
qed_goal "mult_left_commute" Arith.thy "x*(y*z) = y*((x*z)::nat)"
paulson@3234
   304
 (fn _ => [rtac trans 1, rtac mult_commute 1, rtac trans 1,
paulson@3234
   305
           rtac mult_assoc 1, rtac (mult_commute RS arg_cong) 1]);
paulson@3234
   306
paulson@3234
   307
val mult_ac = [mult_assoc,mult_commute,mult_left_commute];
paulson@3234
   308
paulson@3293
   309
goal Arith.thy "(m*n = 0) = (m=0 | n=0)";
paulson@3339
   310
by (induct_tac "m" 1);
paulson@3339
   311
by (induct_tac "n" 2);
paulson@3293
   312
by (ALLGOALS Asm_simp_tac);
paulson@3293
   313
qed "mult_is_0";
paulson@3293
   314
Addsimps [mult_is_0];
paulson@3293
   315
paulson@3234
   316
paulson@3234
   317
(*** Difference ***)
paulson@3234
   318
paulson@3234
   319
qed_goal "pred_Suc_diff" Arith.thy "pred(Suc m - n) = m - n"
paulson@3339
   320
 (fn _ => [induct_tac "n" 1, ALLGOALS Asm_simp_tac]);
paulson@3234
   321
Addsimps [pred_Suc_diff];
paulson@3234
   322
paulson@3234
   323
qed_goal "diff_self_eq_0" Arith.thy "m - m = 0"
paulson@3339
   324
 (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
paulson@3234
   325
Addsimps [diff_self_eq_0];
paulson@3234
   326
paulson@3234
   327
(*Addition is the inverse of subtraction: if n<=m then n+(m-n) = m. *)
paulson@3352
   328
val [prem] = goal Arith.thy "~ m<n ==> n+(m-n) = (m::nat)";
paulson@3234
   329
by (rtac (prem RS rev_mp) 1);
paulson@3234
   330
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3352
   331
by (ALLGOALS Asm_simp_tac);
paulson@3234
   332
qed "add_diff_inverse";
paulson@3234
   333
paulson@3234
   334
Delsimps  [diff_Suc];
paulson@3234
   335
paulson@3234
   336
paulson@3234
   337
(*** More results about difference ***)
paulson@3234
   338
paulson@3352
   339
val [prem] = goal Arith.thy "n < Suc(m) ==> Suc(m)-n = Suc(m-n)";
paulson@3352
   340
by (rtac (prem RS rev_mp) 1);
paulson@3352
   341
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3352
   342
by (ALLGOALS Asm_simp_tac);
paulson@3352
   343
qed "Suc_diff_n";
paulson@3352
   344
paulson@3234
   345
goal Arith.thy "m - n < Suc(m)";
paulson@3234
   346
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3234
   347
by (etac less_SucE 3);
paulson@3234
   348
by (ALLGOALS (asm_simp_tac (!simpset addsimps [less_Suc_eq])));
paulson@3234
   349
qed "diff_less_Suc";
paulson@3234
   350
paulson@3234
   351
goal Arith.thy "!!m::nat. m - n <= m";
paulson@3234
   352
by (res_inst_tac [("m","m"), ("n","n")] diff_induct 1);
paulson@3234
   353
by (ALLGOALS Asm_simp_tac);
paulson@3234
   354
qed "diff_le_self";
paulson@3234
   355
paulson@3352
   356
goal Arith.thy "!!i::nat. i-j-k = i - (j+k)";
paulson@3352
   357
by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
paulson@3352
   358
by (ALLGOALS Asm_simp_tac);
paulson@3352
   359
qed "diff_diff_left";
paulson@3352
   360
paulson@3352
   361
(*This and the next few suggested by Florian Kammüller*)
paulson@3352
   362
goal Arith.thy "!!i::nat. i-j-k = i-k-j";
paulson@3352
   363
by (simp_tac (!simpset addsimps [diff_diff_left, add_commute]) 1);
paulson@3352
   364
qed "diff_commute";
paulson@3352
   365
paulson@3352
   366
goal Arith.thy "!!i j k:: nat. k<=j --> j<=i --> i - (j - k) = i - j + k";
paulson@3352
   367
by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
paulson@3352
   368
by (ALLGOALS Asm_simp_tac);
paulson@3352
   369
by (asm_simp_tac
paulson@3352
   370
    (!simpset addsimps [Suc_diff_n, le_imp_less_Suc, le_Suc_eq]) 1);
paulson@3352
   371
by (simp_tac
paulson@3352
   372
    (!simpset addsimps [add_diff_inverse, not_less_iff_le, add_commute]) 1);
paulson@3352
   373
qed_spec_mp "diff_diff_right";
paulson@3352
   374
paulson@3352
   375
goal Arith.thy "!!i j k:: nat. k<=j --> (i + j) - k = i + (j - k)";
paulson@3352
   376
by (res_inst_tac [("m","j"),("n","k")] diff_induct 1);
paulson@3352
   377
by (ALLGOALS Asm_simp_tac);
paulson@3352
   378
qed_spec_mp "diff_add_assoc";
paulson@3352
   379
paulson@3234
   380
goal Arith.thy "!!n::nat. (n+m) - n = m";
paulson@3339
   381
by (induct_tac "n" 1);
paulson@3234
   382
by (ALLGOALS Asm_simp_tac);
paulson@3234
   383
qed "diff_add_inverse";
paulson@3234
   384
Addsimps [diff_add_inverse];
paulson@3234
   385
paulson@3234
   386
goal Arith.thy "!!n::nat.(m+n) - n = m";
paulson@3352
   387
by (simp_tac (!simpset addsimps [diff_add_assoc]) 1);
paulson@3234
   388
qed "diff_add_inverse2";
paulson@3234
   389
Addsimps [diff_add_inverse2];
paulson@3234
   390
paulson@3234
   391
val [prem] = goal Arith.thy "m < Suc(n) ==> m-n = 0";
paulson@3234
   392
by (rtac (prem RS rev_mp) 1);
paulson@3234
   393
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3234
   394
by (asm_simp_tac (!simpset addsimps [less_Suc_eq]) 1);
paulson@3352
   395
by (ALLGOALS Asm_simp_tac);
paulson@3234
   396
qed "less_imp_diff_is_0";
paulson@3234
   397
paulson@3234
   398
val prems = goal Arith.thy "m-n = 0  -->  n-m = 0  -->  m=n";
paulson@3234
   399
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3234
   400
by (REPEAT(Simp_tac 1 THEN TRY(atac 1)));
paulson@3234
   401
qed_spec_mp "diffs0_imp_equal";
paulson@3234
   402
paulson@3234
   403
val [prem] = goal Arith.thy "m<n ==> 0<n-m";
paulson@3234
   404
by (rtac (prem RS rev_mp) 1);
paulson@3234
   405
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3352
   406
by (ALLGOALS Asm_simp_tac);
paulson@3234
   407
qed "less_imp_diff_positive";
paulson@3234
   408
paulson@3234
   409
goal Arith.thy "Suc(m)-n = (if m<n then 0 else Suc(m-n))";
paulson@3234
   410
by (simp_tac (!simpset addsimps [less_imp_diff_is_0, not_less_eq, Suc_diff_n]
paulson@3234
   411
                    setloop (split_tac [expand_if])) 1);
paulson@3234
   412
qed "if_Suc_diff_n";
paulson@3234
   413
paulson@3234
   414
goal Arith.thy "P(k) --> (!n. P(Suc(n))--> P(n)) --> P(k-i)";
paulson@3234
   415
by (res_inst_tac [("m","k"),("n","i")] diff_induct 1);
paulson@3234
   416
by (ALLGOALS (strip_tac THEN' Simp_tac THEN' TRY o Blast_tac));
paulson@3234
   417
qed "zero_induct_lemma";
paulson@3234
   418
paulson@3234
   419
val prems = goal Arith.thy "[| P(k);  !!n. P(Suc(n)) ==> P(n) |] ==> P(0)";
paulson@3234
   420
by (rtac (diff_self_eq_0 RS subst) 1);
paulson@3234
   421
by (rtac (zero_induct_lemma RS mp RS mp) 1);
paulson@3234
   422
by (REPEAT (ares_tac ([impI,allI]@prems) 1));
paulson@3234
   423
qed "zero_induct";
paulson@3234
   424
paulson@3234
   425
goal Arith.thy "!!k::nat. (k+m) - (k+n) = m - n";
paulson@3339
   426
by (induct_tac "k" 1);
paulson@3234
   427
by (ALLGOALS Asm_simp_tac);
paulson@3234
   428
qed "diff_cancel";
paulson@3234
   429
Addsimps [diff_cancel];
paulson@3234
   430
paulson@3234
   431
goal Arith.thy "!!m::nat. (m+k) - (n+k) = m - n";
paulson@3234
   432
val add_commute_k = read_instantiate [("n","k")] add_commute;
paulson@3234
   433
by (asm_simp_tac (!simpset addsimps ([add_commute_k])) 1);
paulson@3234
   434
qed "diff_cancel2";
paulson@3234
   435
Addsimps [diff_cancel2];
paulson@3234
   436
paulson@3234
   437
(*From Clemens Ballarin*)
paulson@3234
   438
goal Arith.thy "!!n::nat. [| k<=n; n<=m |] ==> (m-k) - (n-k) = m-n";
paulson@3234
   439
by (subgoal_tac "k<=n --> n<=m --> (m-k) - (n-k) = m-n" 1);
paulson@3234
   440
by (Asm_full_simp_tac 1);
paulson@3339
   441
by (induct_tac "k" 1);
paulson@3234
   442
by (Simp_tac 1);
paulson@3234
   443
(* Induction step *)
paulson@3234
   444
by (subgoal_tac "Suc na <= m --> n <= m --> Suc na <= n --> \
paulson@3234
   445
\                Suc (m - Suc na) - Suc (n - Suc na) = m-n" 1);
paulson@3234
   446
by (Asm_full_simp_tac 1);
paulson@3234
   447
by (blast_tac (!claset addIs [le_trans]) 1);
paulson@3234
   448
by (auto_tac (!claset addIs [Suc_leD], !simpset delsimps [diff_Suc_Suc]));
paulson@3234
   449
by (asm_full_simp_tac (!simpset delsimps [Suc_less_eq] 
paulson@3234
   450
		       addsimps [Suc_diff_n RS sym, le_eq_less_Suc]) 1);
paulson@3234
   451
qed "diff_right_cancel";
paulson@3234
   452
paulson@3234
   453
goal Arith.thy "!!n::nat. n - (n+m) = 0";
paulson@3339
   454
by (induct_tac "n" 1);
paulson@3234
   455
by (ALLGOALS Asm_simp_tac);
paulson@3234
   456
qed "diff_add_0";
paulson@3234
   457
Addsimps [diff_add_0];
paulson@3234
   458
paulson@3234
   459
(** Difference distributes over multiplication **)
paulson@3234
   460
paulson@3234
   461
goal Arith.thy "!!m::nat. (m - n) * k = (m * k) - (n * k)";
paulson@3234
   462
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3234
   463
by (ALLGOALS Asm_simp_tac);
paulson@3234
   464
qed "diff_mult_distrib" ;
paulson@3234
   465
paulson@3234
   466
goal Arith.thy "!!m::nat. k * (m - n) = (k * m) - (k * n)";
paulson@3234
   467
val mult_commute_k = read_instantiate [("m","k")] mult_commute;
paulson@3234
   468
by (simp_tac (!simpset addsimps [diff_mult_distrib, mult_commute_k]) 1);
paulson@3234
   469
qed "diff_mult_distrib2" ;
paulson@3234
   470
(*NOT added as rewrites, since sometimes they are used from right-to-left*)
paulson@3234
   471
paulson@3234
   472
paulson@3234
   473
(** Less-then properties **)
paulson@3234
   474
paulson@3234
   475
(*In ordinary notation: if 0<n and n<=m then m-n < m *)
paulson@3234
   476
goal Arith.thy "!!m. [| 0<n; ~ m<n |] ==> m - n < m";
paulson@3234
   477
by (subgoal_tac "0<n --> ~ m<n --> m - n < m" 1);
paulson@3234
   478
by (Blast_tac 1);
paulson@3234
   479
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3234
   480
by (ALLGOALS(asm_simp_tac(!simpset addsimps [diff_less_Suc])));
paulson@3234
   481
qed "diff_less";
paulson@3234
   482
paulson@3234
   483
val wf_less_trans = [eq_reflection, wf_pred_nat RS wf_trancl] MRS 
paulson@3234
   484
                    def_wfrec RS trans;
paulson@3234
   485
paulson@3234
   486
goalw Nat.thy [less_def] "(m,n) : pred_nat^+ = (m<n)";
paulson@3234
   487
by (rtac refl 1);
paulson@3234
   488
qed "less_eq";
paulson@3234
   489
paulson@3234
   490
(*** Remainder ***)
paulson@3234
   491
paulson@3234
   492
goal Arith.thy "(%m. m mod n) = wfrec (trancl pred_nat) \
paulson@3234
   493
             \                      (%f j. if j<n then j else f (j-n))";
paulson@3234
   494
by (simp_tac (!simpset addsimps [mod_def]) 1);
paulson@3234
   495
qed "mod_eq";
paulson@3234
   496
paulson@3234
   497
goal Arith.thy "!!m. m<n ==> m mod n = m";
paulson@3234
   498
by (rtac (mod_eq RS wf_less_trans) 1);
paulson@3234
   499
by (Asm_simp_tac 1);
paulson@3234
   500
qed "mod_less";
paulson@3234
   501
paulson@3234
   502
goal Arith.thy "!!m. [| 0<n;  ~m<n |] ==> m mod n = (m-n) mod n";
paulson@3234
   503
by (rtac (mod_eq RS wf_less_trans) 1);
paulson@3234
   504
by (asm_simp_tac (!simpset addsimps [diff_less, cut_apply, less_eq]) 1);
paulson@3234
   505
qed "mod_geq";
paulson@3234
   506
paulson@3234
   507
goal thy "!!n. 0<n ==> n mod n = 0";
paulson@3234
   508
by (rtac (mod_eq RS wf_less_trans) 1);
paulson@3234
   509
by (asm_simp_tac (!simpset addsimps [mod_less, diff_self_eq_0,
paulson@3234
   510
				     cut_def, less_eq]) 1);
paulson@3234
   511
qed "mod_nn_is_0";
paulson@3234
   512
paulson@3234
   513
goal thy "!!n. 0<n ==> (m+n) mod n = m mod n";
paulson@3234
   514
by (subgoal_tac "(n + m) mod n = (n+m-n) mod n" 1);
paulson@3234
   515
by (stac (mod_geq RS sym) 2);
paulson@3234
   516
by (ALLGOALS (asm_full_simp_tac (!simpset addsimps [add_commute])));
paulson@3234
   517
qed "mod_eq_add";
paulson@3234
   518
paulson@3234
   519
goal thy "!!n. 0<n ==> m*n mod n = 0";
paulson@3339
   520
by (induct_tac "m" 1);
paulson@3234
   521
by (asm_simp_tac (!simpset addsimps [mod_less]) 1);
paulson@3234
   522
by (dres_inst_tac [("m","m*n")] mod_eq_add 1);
paulson@3234
   523
by (asm_full_simp_tac (!simpset addsimps [add_commute]) 1);
paulson@3234
   524
qed "mod_prod_nn_is_0";
paulson@3234
   525
paulson@3234
   526
paulson@3234
   527
(*** Quotient ***)
paulson@3234
   528
paulson@3234
   529
goal Arith.thy "(%m. m div n) = wfrec (trancl pred_nat) \
paulson@3234
   530
                        \            (%f j. if j<n then 0 else Suc (f (j-n)))";
paulson@3234
   531
by (simp_tac (!simpset addsimps [div_def]) 1);
paulson@3234
   532
qed "div_eq";
paulson@3234
   533
paulson@3234
   534
goal Arith.thy "!!m. m<n ==> m div n = 0";
paulson@3234
   535
by (rtac (div_eq RS wf_less_trans) 1);
paulson@3234
   536
by (Asm_simp_tac 1);
paulson@3234
   537
qed "div_less";
paulson@3234
   538
paulson@3234
   539
goal Arith.thy "!!M. [| 0<n;  ~m<n |] ==> m div n = Suc((m-n) div n)";
paulson@3234
   540
by (rtac (div_eq RS wf_less_trans) 1);
paulson@3234
   541
by (asm_simp_tac (!simpset addsimps [diff_less, cut_apply, less_eq]) 1);
paulson@3234
   542
qed "div_geq";
paulson@3234
   543
paulson@3234
   544
(*Main Result about quotient and remainder.*)
paulson@3234
   545
goal Arith.thy "!!m. 0<n ==> (m div n)*n + m mod n = m";
paulson@3234
   546
by (res_inst_tac [("n","m")] less_induct 1);
paulson@3234
   547
by (rename_tac "k" 1);    (*Variable name used in line below*)
paulson@3234
   548
by (case_tac "k<n" 1);
paulson@3234
   549
by (ALLGOALS (asm_simp_tac(!simpset addsimps ([add_assoc] @
paulson@3234
   550
                       [mod_less, mod_geq, div_less, div_geq,
paulson@3234
   551
                        add_diff_inverse, diff_less]))));
paulson@3234
   552
qed "mod_div_equality";
paulson@3234
   553
paulson@3234
   554
paulson@3293
   555
(*** Further facts about mod (mainly for the mutilated chess board ***)
paulson@3234
   556
paulson@3234
   557
goal Arith.thy
paulson@3234
   558
    "!!m n. 0<n ==> \
paulson@3234
   559
\           Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))";
paulson@3234
   560
by (res_inst_tac [("n","m")] less_induct 1);
paulson@3234
   561
by (excluded_middle_tac "Suc(na)<n" 1);
paulson@3234
   562
(* case Suc(na) < n *)
paulson@3234
   563
by (forward_tac [lessI RS less_trans] 2);
paulson@3234
   564
by (asm_simp_tac (!simpset addsimps [mod_less, less_not_refl2 RS not_sym]) 2);
paulson@3234
   565
(* case n <= Suc(na) *)
paulson@3234
   566
by (asm_full_simp_tac (!simpset addsimps [not_less_iff_le, mod_geq]) 1);
paulson@3234
   567
by (etac (le_imp_less_or_eq RS disjE) 1);
paulson@3234
   568
by (asm_simp_tac (!simpset addsimps [Suc_diff_n]) 1);
paulson@3234
   569
by (asm_full_simp_tac (!simpset addsimps [not_less_eq RS sym, 
paulson@3234
   570
                                          diff_less, mod_geq]) 1);
paulson@3234
   571
by (asm_simp_tac (!simpset addsimps [mod_less]) 1);
paulson@3234
   572
qed "mod_Suc";
paulson@3234
   573
paulson@3234
   574
goal Arith.thy "!!m n. 0<n ==> m mod n < n";
paulson@3234
   575
by (res_inst_tac [("n","m")] less_induct 1);
paulson@3234
   576
by (excluded_middle_tac "na<n" 1);
paulson@3234
   577
(*case na<n*)
paulson@3234
   578
by (asm_simp_tac (!simpset addsimps [mod_less]) 2);
paulson@3234
   579
(*case n le na*)
paulson@3234
   580
by (asm_full_simp_tac (!simpset addsimps [mod_geq, diff_less]) 1);
paulson@3234
   581
qed "mod_less_divisor";
paulson@3234
   582
paulson@3234
   583
paulson@3234
   584
(** Evens and Odds **)
paulson@3234
   585
paulson@3234
   586
(*With less_zeroE, causes case analysis on b<2*)
paulson@3234
   587
AddSEs [less_SucE];
paulson@3234
   588
paulson@3234
   589
goal thy "!!k b. b<2 ==> k mod 2 = b | k mod 2 = (if b=1 then 0 else 1)";
paulson@3234
   590
by (subgoal_tac "k mod 2 < 2" 1);
paulson@3234
   591
by (asm_simp_tac (!simpset addsimps [mod_less_divisor]) 2);
paulson@3234
   592
by (asm_simp_tac (!simpset setloop split_tac [expand_if]) 1);
paulson@3234
   593
by (Blast_tac 1);
paulson@3234
   594
qed "mod2_cases";
paulson@3234
   595
paulson@3234
   596
goal thy "Suc(Suc(m)) mod 2 = m mod 2";
paulson@3234
   597
by (subgoal_tac "m mod 2 < 2" 1);
paulson@3234
   598
by (asm_simp_tac (!simpset addsimps [mod_less_divisor]) 2);
paulson@3234
   599
by (Step_tac 1);
paulson@3234
   600
by (ALLGOALS (asm_simp_tac (!simpset addsimps [mod_Suc])));
paulson@3234
   601
qed "mod2_Suc_Suc";
paulson@3234
   602
Addsimps [mod2_Suc_Suc];
paulson@3234
   603
paulson@3293
   604
goal Arith.thy "!!m. m mod 2 ~= 0 ==> m mod 2 = 1";
paulson@3293
   605
by (subgoal_tac "m mod 2 < 2" 1);
paulson@3293
   606
by (asm_simp_tac (!simpset addsimps [mod_less_divisor]) 2);
paulson@3293
   607
by (safe_tac (!claset addSEs [lessE]));
paulson@3293
   608
by (ALLGOALS (blast_tac (!claset addIs [sym])));
paulson@3293
   609
qed "mod2_neq_0";
paulson@3293
   610
paulson@3234
   611
goal thy "(m+m) mod 2 = 0";
paulson@3339
   612
by (induct_tac "m" 1);
paulson@3234
   613
by (simp_tac (!simpset addsimps [mod_less]) 1);
paulson@3234
   614
by (asm_simp_tac (!simpset addsimps [mod2_Suc_Suc, add_Suc_right]) 1);
paulson@3234
   615
qed "mod2_add_self";
paulson@3234
   616
Addsimps [mod2_add_self];
paulson@3234
   617
paulson@3234
   618
Delrules [less_SucE];
paulson@3234
   619
paulson@3234
   620
paulson@1713
   621
(*** Monotonicity of Multiplication ***)
paulson@1713
   622
paulson@1713
   623
goal Arith.thy "!!i::nat. i<=j ==> i*k<=j*k";
paulson@3339
   624
by (induct_tac "k" 1);
paulson@1713
   625
by (ALLGOALS (asm_simp_tac (!simpset addsimps [add_le_mono])));
paulson@1713
   626
qed "mult_le_mono1";
paulson@1713
   627
paulson@1713
   628
(*<=monotonicity, BOTH arguments*)
paulson@1713
   629
goal Arith.thy "!!i::nat. [| i<=j; k<=l |] ==> i*k<=j*l";
paulson@2007
   630
by (etac (mult_le_mono1 RS le_trans) 1);
paulson@1713
   631
by (rtac le_trans 1);
paulson@2007
   632
by (stac mult_commute 2);
paulson@2007
   633
by (etac mult_le_mono1 2);
paulson@2007
   634
by (simp_tac (!simpset addsimps [mult_commute]) 1);
paulson@1713
   635
qed "mult_le_mono";
paulson@1713
   636
paulson@1713
   637
(*strict, in 1st argument; proof is by induction on k>0*)
paulson@1713
   638
goal Arith.thy "!!i::nat. [| i<j; 0<k |] ==> k*i < k*j";
paulson@3339
   639
by (eres_inst_tac [("i","0")] less_natE 1);
paulson@1713
   640
by (Asm_simp_tac 1);
paulson@3339
   641
by (induct_tac "x" 1);
paulson@1713
   642
by (ALLGOALS (asm_simp_tac (!simpset addsimps [add_less_mono])));
paulson@1713
   643
qed "mult_less_mono2";
paulson@1713
   644
paulson@3234
   645
goal Arith.thy "!!i::nat. [| i<j; 0<k |] ==> i*k < j*k";
paulson@3234
   646
bd mult_less_mono2 1;
paulson@3234
   647
by (ALLGOALS (asm_full_simp_tac (!simpset addsimps [mult_commute])));
paulson@3234
   648
qed "mult_less_mono1";
paulson@3234
   649
paulson@1713
   650
goal Arith.thy "(0 < m*n) = (0<m & 0<n)";
paulson@3339
   651
by (induct_tac "m" 1);
paulson@3339
   652
by (induct_tac "n" 2);
paulson@1713
   653
by (ALLGOALS Asm_simp_tac);
paulson@1713
   654
qed "zero_less_mult_iff";
paulson@1713
   655
paulson@1795
   656
goal Arith.thy "(m*n = 1) = (m=1 & n=1)";
paulson@3339
   657
by (induct_tac "m" 1);
paulson@1795
   658
by (Simp_tac 1);
paulson@3339
   659
by (induct_tac "n" 1);
paulson@1795
   660
by (Simp_tac 1);
paulson@1795
   661
by (fast_tac (!claset addss !simpset) 1);
paulson@1795
   662
qed "mult_eq_1_iff";
paulson@1795
   663
paulson@3234
   664
goal Arith.thy "!!k. 0<k ==> (m*k < n*k) = (m<n)";
paulson@3234
   665
by (safe_tac (!claset addSIs [mult_less_mono1]));
paulson@3234
   666
by (cut_facts_tac [less_linear] 1);
paulson@3234
   667
by (blast_tac (!claset addDs [mult_less_mono1] addEs [less_asym]) 1);
paulson@3234
   668
qed "mult_less_cancel2";
paulson@3234
   669
paulson@3234
   670
goal Arith.thy "!!k. 0<k ==> (k*m < k*n) = (m<n)";
paulson@3234
   671
bd mult_less_cancel2 1;
paulson@3234
   672
by (asm_full_simp_tac (!simpset addsimps [mult_commute]) 1);
paulson@3234
   673
qed "mult_less_cancel1";
paulson@3234
   674
Addsimps [mult_less_cancel1, mult_less_cancel2];
paulson@3234
   675
paulson@3234
   676
goal Arith.thy "!!k. 0<k ==> (m*k = n*k) = (m=n)";
paulson@3234
   677
by (cut_facts_tac [less_linear] 1);
paulson@3234
   678
by(Step_tac 1);
paulson@3234
   679
ba 2;
paulson@3234
   680
by (ALLGOALS (dtac mult_less_mono1 THEN' assume_tac));
paulson@3234
   681
by (ALLGOALS Asm_full_simp_tac);
paulson@3234
   682
qed "mult_cancel2";
paulson@3234
   683
paulson@3234
   684
goal Arith.thy "!!k. 0<k ==> (k*m = k*n) = (m=n)";
paulson@3234
   685
bd mult_cancel2 1;
paulson@3234
   686
by (asm_full_simp_tac (!simpset addsimps [mult_commute]) 1);
paulson@3234
   687
qed "mult_cancel1";
paulson@3234
   688
Addsimps [mult_cancel1, mult_cancel2];
paulson@3234
   689
paulson@3234
   690
paulson@3234
   691
(*** More division laws ***)
paulson@3234
   692
paulson@3234
   693
goal thy "!!n. 0<n ==> m*n div n = m";
paulson@3234
   694
by (cut_inst_tac [("m", "m*n")] mod_div_equality 1);
paulson@3234
   695
ba 1;
paulson@3234
   696
by (asm_full_simp_tac (!simpset addsimps [mod_prod_nn_is_0]) 1);
paulson@3234
   697
qed "div_prod_nn_is_m";
paulson@3234
   698
Addsimps [div_prod_nn_is_m];
paulson@3234
   699
paulson@1713
   700
(*Cancellation law for division*)
paulson@1713
   701
goal Arith.thy "!!k. [| 0<n; 0<k |] ==> (k*m) div (k*n) = m div n";
paulson@1713
   702
by (res_inst_tac [("n","m")] less_induct 1);
paulson@1713
   703
by (case_tac "na<n" 1);
paulson@1713
   704
by (asm_simp_tac (!simpset addsimps [div_less, zero_less_mult_iff, 
paulson@2031
   705
                                     mult_less_mono2]) 1);
paulson@1713
   706
by (subgoal_tac "~ k*na < k*n" 1);
paulson@1713
   707
by (asm_simp_tac
paulson@1713
   708
     (!simpset addsimps [zero_less_mult_iff, div_geq,
paulson@2031
   709
                         diff_mult_distrib2 RS sym, diff_less]) 1);
paulson@1713
   710
by (asm_full_simp_tac (!simpset addsimps [not_less_iff_le, 
paulson@2031
   711
                                          le_refl RS mult_le_mono]) 1);
paulson@1713
   712
qed "div_cancel";
paulson@3234
   713
Addsimps [div_cancel];
paulson@1713
   714
paulson@1713
   715
goal Arith.thy "!!k. [| 0<n; 0<k |] ==> (k*m) mod (k*n) = k * (m mod n)";
paulson@1713
   716
by (res_inst_tac [("n","m")] less_induct 1);
paulson@1713
   717
by (case_tac "na<n" 1);
paulson@1713
   718
by (asm_simp_tac (!simpset addsimps [mod_less, zero_less_mult_iff, 
paulson@2031
   719
                                     mult_less_mono2]) 1);
paulson@1713
   720
by (subgoal_tac "~ k*na < k*n" 1);
paulson@1713
   721
by (asm_simp_tac
paulson@1713
   722
     (!simpset addsimps [zero_less_mult_iff, mod_geq,
paulson@2031
   723
                         diff_mult_distrib2 RS sym, diff_less]) 1);
paulson@1713
   724
by (asm_full_simp_tac (!simpset addsimps [not_less_iff_le, 
paulson@2031
   725
                                          le_refl RS mult_le_mono]) 1);
paulson@1713
   726
qed "mult_mod_distrib";
paulson@1713
   727
paulson@1713
   728
paulson@1795
   729
(** Lemma for gcd **)
paulson@1795
   730
paulson@1795
   731
goal Arith.thy "!!m n. m = m*n ==> n=1 | m=0";
paulson@1795
   732
by (dtac sym 1);
paulson@1795
   733
by (rtac disjCI 1);
paulson@1795
   734
by (rtac nat_less_cases 1 THEN assume_tac 2);
paulson@1909
   735
by (fast_tac (!claset addSEs [less_SucE] addss !simpset) 1);
paulson@1979
   736
by (best_tac (!claset addDs [mult_less_mono2] 
paulson@1795
   737
                      addss (!simpset addsimps [zero_less_eq RS sym])) 1);
paulson@1795
   738
qed "mult_eq_self_implies_10";
paulson@1795
   739
paulson@1795
   740
paulson@3234
   741
(*** Subtraction laws -- from Clemens Ballarin ***)
paulson@3234
   742
paulson@3234
   743
goal Arith.thy "!! a b c::nat. [| a < b; c <= a |] ==> a-c < b-c";
paulson@3234
   744
by (subgoal_tac "c+(a-c) < c+(b-c)" 1);
paulson@3234
   745
by (Asm_full_simp_tac 1);
paulson@3234
   746
by (subgoal_tac "c <= b" 1);
paulson@3234
   747
by (blast_tac (!claset addIs [less_imp_le, le_trans]) 2);
paulson@3234
   748
by (asm_simp_tac (!simpset addsimps [leD RS add_diff_inverse]) 1);
paulson@3234
   749
qed "diff_less_mono";
paulson@3234
   750
paulson@3234
   751
goal Arith.thy "!! a b c::nat. a+b < c ==> a < c-b";
paulson@3234
   752
bd diff_less_mono 1;
paulson@3234
   753
br le_add2 1;
paulson@3234
   754
by (Asm_full_simp_tac 1);
paulson@3234
   755
qed "add_less_imp_less_diff";
paulson@3234
   756
paulson@3234
   757
goal Arith.thy "!! n. n <= m ==> Suc m - n = Suc (m - n)";
paulson@3234
   758
br Suc_diff_n 1;
paulson@3234
   759
by (asm_full_simp_tac (!simpset addsimps [le_eq_less_Suc]) 1);
paulson@3234
   760
qed "Suc_diff_le";
paulson@3234
   761
paulson@3234
   762
goal Arith.thy "!! n. Suc i <= n ==> Suc (n - Suc i) = n - i";
paulson@3234
   763
by (asm_full_simp_tac
paulson@3234
   764
    (!simpset addsimps [Suc_diff_n RS sym, le_eq_less_Suc]) 1);
paulson@3234
   765
qed "Suc_diff_Suc";
paulson@3234
   766
paulson@3234
   767
goal Arith.thy "!! i::nat. i <= n ==> n - (n - i) = i";
paulson@3234
   768
by (subgoal_tac "(n-i) + (n - (n-i)) = (n-i) + i" 1);
paulson@3234
   769
by (Asm_full_simp_tac 1);
paulson@3234
   770
by (asm_simp_tac (!simpset addsimps [leD RS add_diff_inverse, diff_le_self, 
paulson@3234
   771
				     add_commute]) 1);
paulson@3234
   772
qed "diff_diff_cancel";
paulson@3234
   773
paulson@3234
   774
goal Arith.thy "!!k::nat. k <= n ==> m <= n + m - k";
paulson@3234
   775
be rev_mp 1;
paulson@3234
   776
by (res_inst_tac [("m", "k"), ("n", "n")] diff_induct 1);
paulson@3234
   777
by (Simp_tac 1);
paulson@3234
   778
by (simp_tac (!simpset addsimps [less_add_Suc2, less_imp_le]) 1);
paulson@3234
   779
by (Simp_tac 1);
paulson@3234
   780
qed "le_add_diff";
paulson@3234
   781
paulson@3234
   782