src/HOL/Arith.ML
author paulson
Mon May 26 12:36:16 1997 +0200 (1997-05-26)
changeset 3339 cfa72a70f2b5
parent 3293 c05f73cf3227
child 3352 04502e5431fb
permissions -rw-r--r--
Tidying and a couple of useful lemmas
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@3234
   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@3234
   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@3234
   339
goal Arith.thy "m - n < Suc(m)";
paulson@3234
   340
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3234
   341
by (etac less_SucE 3);
paulson@3234
   342
by (ALLGOALS (asm_simp_tac (!simpset addsimps [less_Suc_eq])));
paulson@3234
   343
qed "diff_less_Suc";
paulson@3234
   344
paulson@3234
   345
goal Arith.thy "!!m::nat. m - n <= m";
paulson@3234
   346
by (res_inst_tac [("m","m"), ("n","n")] diff_induct 1);
paulson@3234
   347
by (ALLGOALS Asm_simp_tac);
paulson@3234
   348
qed "diff_le_self";
paulson@3234
   349
paulson@3234
   350
goal Arith.thy "!!n::nat. (n+m) - n = m";
paulson@3339
   351
by (induct_tac "n" 1);
paulson@3234
   352
by (ALLGOALS Asm_simp_tac);
paulson@3234
   353
qed "diff_add_inverse";
paulson@3234
   354
Addsimps [diff_add_inverse];
paulson@3234
   355
paulson@3234
   356
goal Arith.thy "!!n::nat.(m+n) - n = m";
paulson@3234
   357
by (res_inst_tac [("m1","m")] (add_commute RS ssubst) 1);
paulson@3234
   358
by (REPEAT (ares_tac [diff_add_inverse] 1));
paulson@3234
   359
qed "diff_add_inverse2";
paulson@3234
   360
Addsimps [diff_add_inverse2];
paulson@3234
   361
paulson@3234
   362
val [prem] = goal Arith.thy "m < Suc(n) ==> m-n = 0";
paulson@3234
   363
by (rtac (prem RS rev_mp) 1);
paulson@3234
   364
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3234
   365
by (asm_simp_tac (!simpset addsimps [less_Suc_eq]) 1);
paulson@3234
   366
by (ALLGOALS (Asm_simp_tac));
paulson@3234
   367
qed "less_imp_diff_is_0";
paulson@3234
   368
paulson@3234
   369
val prems = goal Arith.thy "m-n = 0  -->  n-m = 0  -->  m=n";
paulson@3234
   370
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3234
   371
by (REPEAT(Simp_tac 1 THEN TRY(atac 1)));
paulson@3234
   372
qed_spec_mp "diffs0_imp_equal";
paulson@3234
   373
paulson@3234
   374
val [prem] = goal Arith.thy "m<n ==> 0<n-m";
paulson@3234
   375
by (rtac (prem RS rev_mp) 1);
paulson@3234
   376
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3234
   377
by (ALLGOALS (Asm_simp_tac));
paulson@3234
   378
qed "less_imp_diff_positive";
paulson@3234
   379
paulson@3234
   380
val [prem] = goal Arith.thy "n < Suc(m) ==> Suc(m)-n = Suc(m-n)";
paulson@3234
   381
by (rtac (prem RS rev_mp) 1);
paulson@3234
   382
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3234
   383
by (ALLGOALS (Asm_simp_tac));
paulson@3234
   384
qed "Suc_diff_n";
paulson@3234
   385
paulson@3234
   386
goal Arith.thy "Suc(m)-n = (if m<n then 0 else Suc(m-n))";
paulson@3234
   387
by (simp_tac (!simpset addsimps [less_imp_diff_is_0, not_less_eq, Suc_diff_n]
paulson@3234
   388
                    setloop (split_tac [expand_if])) 1);
paulson@3234
   389
qed "if_Suc_diff_n";
paulson@3234
   390
paulson@3234
   391
goal Arith.thy "P(k) --> (!n. P(Suc(n))--> P(n)) --> P(k-i)";
paulson@3234
   392
by (res_inst_tac [("m","k"),("n","i")] diff_induct 1);
paulson@3234
   393
by (ALLGOALS (strip_tac THEN' Simp_tac THEN' TRY o Blast_tac));
paulson@3234
   394
qed "zero_induct_lemma";
paulson@3234
   395
paulson@3234
   396
val prems = goal Arith.thy "[| P(k);  !!n. P(Suc(n)) ==> P(n) |] ==> P(0)";
paulson@3234
   397
by (rtac (diff_self_eq_0 RS subst) 1);
paulson@3234
   398
by (rtac (zero_induct_lemma RS mp RS mp) 1);
paulson@3234
   399
by (REPEAT (ares_tac ([impI,allI]@prems) 1));
paulson@3234
   400
qed "zero_induct";
paulson@3234
   401
paulson@3234
   402
goal Arith.thy "!!k::nat. (k+m) - (k+n) = m - n";
paulson@3339
   403
by (induct_tac "k" 1);
paulson@3234
   404
by (ALLGOALS Asm_simp_tac);
paulson@3234
   405
qed "diff_cancel";
paulson@3234
   406
Addsimps [diff_cancel];
paulson@3234
   407
paulson@3234
   408
goal Arith.thy "!!m::nat. (m+k) - (n+k) = m - n";
paulson@3234
   409
val add_commute_k = read_instantiate [("n","k")] add_commute;
paulson@3234
   410
by (asm_simp_tac (!simpset addsimps ([add_commute_k])) 1);
paulson@3234
   411
qed "diff_cancel2";
paulson@3234
   412
Addsimps [diff_cancel2];
paulson@3234
   413
paulson@3234
   414
(*From Clemens Ballarin*)
paulson@3234
   415
goal Arith.thy "!!n::nat. [| k<=n; n<=m |] ==> (m-k) - (n-k) = m-n";
paulson@3234
   416
by (subgoal_tac "k<=n --> n<=m --> (m-k) - (n-k) = m-n" 1);
paulson@3234
   417
by (Asm_full_simp_tac 1);
paulson@3339
   418
by (induct_tac "k" 1);
paulson@3234
   419
by (Simp_tac 1);
paulson@3234
   420
(* Induction step *)
paulson@3234
   421
by (subgoal_tac "Suc na <= m --> n <= m --> Suc na <= n --> \
paulson@3234
   422
\                Suc (m - Suc na) - Suc (n - Suc na) = m-n" 1);
paulson@3234
   423
by (Asm_full_simp_tac 1);
paulson@3234
   424
by (blast_tac (!claset addIs [le_trans]) 1);
paulson@3234
   425
by (auto_tac (!claset addIs [Suc_leD], !simpset delsimps [diff_Suc_Suc]));
paulson@3234
   426
by (asm_full_simp_tac (!simpset delsimps [Suc_less_eq] 
paulson@3234
   427
		       addsimps [Suc_diff_n RS sym, le_eq_less_Suc]) 1);
paulson@3234
   428
qed "diff_right_cancel";
paulson@3234
   429
paulson@3234
   430
goal Arith.thy "!!n::nat. n - (n+m) = 0";
paulson@3339
   431
by (induct_tac "n" 1);
paulson@3234
   432
by (ALLGOALS Asm_simp_tac);
paulson@3234
   433
qed "diff_add_0";
paulson@3234
   434
Addsimps [diff_add_0];
paulson@3234
   435
paulson@3234
   436
(** Difference distributes over multiplication **)
paulson@3234
   437
paulson@3234
   438
goal Arith.thy "!!m::nat. (m - n) * k = (m * k) - (n * k)";
paulson@3234
   439
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3234
   440
by (ALLGOALS Asm_simp_tac);
paulson@3234
   441
qed "diff_mult_distrib" ;
paulson@3234
   442
paulson@3234
   443
goal Arith.thy "!!m::nat. k * (m - n) = (k * m) - (k * n)";
paulson@3234
   444
val mult_commute_k = read_instantiate [("m","k")] mult_commute;
paulson@3234
   445
by (simp_tac (!simpset addsimps [diff_mult_distrib, mult_commute_k]) 1);
paulson@3234
   446
qed "diff_mult_distrib2" ;
paulson@3234
   447
(*NOT added as rewrites, since sometimes they are used from right-to-left*)
paulson@3234
   448
paulson@3234
   449
paulson@3234
   450
(** Less-then properties **)
paulson@3234
   451
paulson@3234
   452
(*In ordinary notation: if 0<n and n<=m then m-n < m *)
paulson@3234
   453
goal Arith.thy "!!m. [| 0<n; ~ m<n |] ==> m - n < m";
paulson@3234
   454
by (subgoal_tac "0<n --> ~ m<n --> m - n < m" 1);
paulson@3234
   455
by (Blast_tac 1);
paulson@3234
   456
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3234
   457
by (ALLGOALS(asm_simp_tac(!simpset addsimps [diff_less_Suc])));
paulson@3234
   458
qed "diff_less";
paulson@3234
   459
paulson@3234
   460
val wf_less_trans = [eq_reflection, wf_pred_nat RS wf_trancl] MRS 
paulson@3234
   461
                    def_wfrec RS trans;
paulson@3234
   462
paulson@3234
   463
goalw Nat.thy [less_def] "(m,n) : pred_nat^+ = (m<n)";
paulson@3234
   464
by (rtac refl 1);
paulson@3234
   465
qed "less_eq";
paulson@3234
   466
paulson@3234
   467
(*** Remainder ***)
paulson@3234
   468
paulson@3234
   469
goal Arith.thy "(%m. m mod n) = wfrec (trancl pred_nat) \
paulson@3234
   470
             \                      (%f j. if j<n then j else f (j-n))";
paulson@3234
   471
by (simp_tac (!simpset addsimps [mod_def]) 1);
paulson@3234
   472
qed "mod_eq";
paulson@3234
   473
paulson@3234
   474
goal Arith.thy "!!m. m<n ==> m mod n = m";
paulson@3234
   475
by (rtac (mod_eq RS wf_less_trans) 1);
paulson@3234
   476
by (Asm_simp_tac 1);
paulson@3234
   477
qed "mod_less";
paulson@3234
   478
paulson@3234
   479
goal Arith.thy "!!m. [| 0<n;  ~m<n |] ==> m mod n = (m-n) mod n";
paulson@3234
   480
by (rtac (mod_eq RS wf_less_trans) 1);
paulson@3234
   481
by (asm_simp_tac (!simpset addsimps [diff_less, cut_apply, less_eq]) 1);
paulson@3234
   482
qed "mod_geq";
paulson@3234
   483
paulson@3234
   484
goal thy "!!n. 0<n ==> n mod n = 0";
paulson@3234
   485
by (rtac (mod_eq RS wf_less_trans) 1);
paulson@3234
   486
by (asm_simp_tac (!simpset addsimps [mod_less, diff_self_eq_0,
paulson@3234
   487
				     cut_def, less_eq]) 1);
paulson@3234
   488
qed "mod_nn_is_0";
paulson@3234
   489
paulson@3234
   490
goal thy "!!n. 0<n ==> (m+n) mod n = m mod n";
paulson@3234
   491
by (subgoal_tac "(n + m) mod n = (n+m-n) mod n" 1);
paulson@3234
   492
by (stac (mod_geq RS sym) 2);
paulson@3234
   493
by (ALLGOALS (asm_full_simp_tac (!simpset addsimps [add_commute])));
paulson@3234
   494
qed "mod_eq_add";
paulson@3234
   495
paulson@3234
   496
goal thy "!!n. 0<n ==> m*n mod n = 0";
paulson@3339
   497
by (induct_tac "m" 1);
paulson@3234
   498
by (asm_simp_tac (!simpset addsimps [mod_less]) 1);
paulson@3234
   499
by (dres_inst_tac [("m","m*n")] mod_eq_add 1);
paulson@3234
   500
by (asm_full_simp_tac (!simpset addsimps [add_commute]) 1);
paulson@3234
   501
qed "mod_prod_nn_is_0";
paulson@3234
   502
paulson@3234
   503
paulson@3234
   504
(*** Quotient ***)
paulson@3234
   505
paulson@3234
   506
goal Arith.thy "(%m. m div n) = wfrec (trancl pred_nat) \
paulson@3234
   507
                        \            (%f j. if j<n then 0 else Suc (f (j-n)))";
paulson@3234
   508
by (simp_tac (!simpset addsimps [div_def]) 1);
paulson@3234
   509
qed "div_eq";
paulson@3234
   510
paulson@3234
   511
goal Arith.thy "!!m. m<n ==> m div n = 0";
paulson@3234
   512
by (rtac (div_eq RS wf_less_trans) 1);
paulson@3234
   513
by (Asm_simp_tac 1);
paulson@3234
   514
qed "div_less";
paulson@3234
   515
paulson@3234
   516
goal Arith.thy "!!M. [| 0<n;  ~m<n |] ==> m div n = Suc((m-n) div n)";
paulson@3234
   517
by (rtac (div_eq RS wf_less_trans) 1);
paulson@3234
   518
by (asm_simp_tac (!simpset addsimps [diff_less, cut_apply, less_eq]) 1);
paulson@3234
   519
qed "div_geq";
paulson@3234
   520
paulson@3234
   521
(*Main Result about quotient and remainder.*)
paulson@3234
   522
goal Arith.thy "!!m. 0<n ==> (m div n)*n + m mod n = m";
paulson@3234
   523
by (res_inst_tac [("n","m")] less_induct 1);
paulson@3234
   524
by (rename_tac "k" 1);    (*Variable name used in line below*)
paulson@3234
   525
by (case_tac "k<n" 1);
paulson@3234
   526
by (ALLGOALS (asm_simp_tac(!simpset addsimps ([add_assoc] @
paulson@3234
   527
                       [mod_less, mod_geq, div_less, div_geq,
paulson@3234
   528
                        add_diff_inverse, diff_less]))));
paulson@3234
   529
qed "mod_div_equality";
paulson@3234
   530
paulson@3234
   531
paulson@3293
   532
(*** Further facts about mod (mainly for the mutilated chess board ***)
paulson@3234
   533
paulson@3234
   534
goal Arith.thy
paulson@3234
   535
    "!!m n. 0<n ==> \
paulson@3234
   536
\           Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))";
paulson@3234
   537
by (res_inst_tac [("n","m")] less_induct 1);
paulson@3234
   538
by (excluded_middle_tac "Suc(na)<n" 1);
paulson@3234
   539
(* case Suc(na) < n *)
paulson@3234
   540
by (forward_tac [lessI RS less_trans] 2);
paulson@3234
   541
by (asm_simp_tac (!simpset addsimps [mod_less, less_not_refl2 RS not_sym]) 2);
paulson@3234
   542
(* case n <= Suc(na) *)
paulson@3234
   543
by (asm_full_simp_tac (!simpset addsimps [not_less_iff_le, mod_geq]) 1);
paulson@3234
   544
by (etac (le_imp_less_or_eq RS disjE) 1);
paulson@3234
   545
by (asm_simp_tac (!simpset addsimps [Suc_diff_n]) 1);
paulson@3234
   546
by (asm_full_simp_tac (!simpset addsimps [not_less_eq RS sym, 
paulson@3234
   547
                                          diff_less, mod_geq]) 1);
paulson@3234
   548
by (asm_simp_tac (!simpset addsimps [mod_less]) 1);
paulson@3234
   549
qed "mod_Suc";
paulson@3234
   550
paulson@3234
   551
goal Arith.thy "!!m n. 0<n ==> m mod n < n";
paulson@3234
   552
by (res_inst_tac [("n","m")] less_induct 1);
paulson@3234
   553
by (excluded_middle_tac "na<n" 1);
paulson@3234
   554
(*case na<n*)
paulson@3234
   555
by (asm_simp_tac (!simpset addsimps [mod_less]) 2);
paulson@3234
   556
(*case n le na*)
paulson@3234
   557
by (asm_full_simp_tac (!simpset addsimps [mod_geq, diff_less]) 1);
paulson@3234
   558
qed "mod_less_divisor";
paulson@3234
   559
paulson@3234
   560
paulson@3234
   561
(** Evens and Odds **)
paulson@3234
   562
paulson@3234
   563
(*With less_zeroE, causes case analysis on b<2*)
paulson@3234
   564
AddSEs [less_SucE];
paulson@3234
   565
paulson@3234
   566
goal thy "!!k b. b<2 ==> k mod 2 = b | k mod 2 = (if b=1 then 0 else 1)";
paulson@3234
   567
by (subgoal_tac "k mod 2 < 2" 1);
paulson@3234
   568
by (asm_simp_tac (!simpset addsimps [mod_less_divisor]) 2);
paulson@3234
   569
by (asm_simp_tac (!simpset setloop split_tac [expand_if]) 1);
paulson@3234
   570
by (Blast_tac 1);
paulson@3234
   571
qed "mod2_cases";
paulson@3234
   572
paulson@3234
   573
goal thy "Suc(Suc(m)) mod 2 = m mod 2";
paulson@3234
   574
by (subgoal_tac "m mod 2 < 2" 1);
paulson@3234
   575
by (asm_simp_tac (!simpset addsimps [mod_less_divisor]) 2);
paulson@3234
   576
by (Step_tac 1);
paulson@3234
   577
by (ALLGOALS (asm_simp_tac (!simpset addsimps [mod_Suc])));
paulson@3234
   578
qed "mod2_Suc_Suc";
paulson@3234
   579
Addsimps [mod2_Suc_Suc];
paulson@3234
   580
paulson@3293
   581
goal Arith.thy "!!m. m mod 2 ~= 0 ==> m mod 2 = 1";
paulson@3293
   582
by (subgoal_tac "m mod 2 < 2" 1);
paulson@3293
   583
by (asm_simp_tac (!simpset addsimps [mod_less_divisor]) 2);
paulson@3293
   584
by (safe_tac (!claset addSEs [lessE]));
paulson@3293
   585
by (ALLGOALS (blast_tac (!claset addIs [sym])));
paulson@3293
   586
qed "mod2_neq_0";
paulson@3293
   587
paulson@3234
   588
goal thy "(m+m) mod 2 = 0";
paulson@3339
   589
by (induct_tac "m" 1);
paulson@3234
   590
by (simp_tac (!simpset addsimps [mod_less]) 1);
paulson@3234
   591
by (asm_simp_tac (!simpset addsimps [mod2_Suc_Suc, add_Suc_right]) 1);
paulson@3234
   592
qed "mod2_add_self";
paulson@3234
   593
Addsimps [mod2_add_self];
paulson@3234
   594
paulson@3234
   595
Delrules [less_SucE];
paulson@3234
   596
paulson@3234
   597
paulson@1713
   598
(*** Monotonicity of Multiplication ***)
paulson@1713
   599
paulson@1713
   600
goal Arith.thy "!!i::nat. i<=j ==> i*k<=j*k";
paulson@3339
   601
by (induct_tac "k" 1);
paulson@1713
   602
by (ALLGOALS (asm_simp_tac (!simpset addsimps [add_le_mono])));
paulson@1713
   603
qed "mult_le_mono1";
paulson@1713
   604
paulson@1713
   605
(*<=monotonicity, BOTH arguments*)
paulson@1713
   606
goal Arith.thy "!!i::nat. [| i<=j; k<=l |] ==> i*k<=j*l";
paulson@2007
   607
by (etac (mult_le_mono1 RS le_trans) 1);
paulson@1713
   608
by (rtac le_trans 1);
paulson@2007
   609
by (stac mult_commute 2);
paulson@2007
   610
by (etac mult_le_mono1 2);
paulson@2007
   611
by (simp_tac (!simpset addsimps [mult_commute]) 1);
paulson@1713
   612
qed "mult_le_mono";
paulson@1713
   613
paulson@1713
   614
(*strict, in 1st argument; proof is by induction on k>0*)
paulson@1713
   615
goal Arith.thy "!!i::nat. [| i<j; 0<k |] ==> k*i < k*j";
paulson@3339
   616
by (eres_inst_tac [("i","0")] less_natE 1);
paulson@1713
   617
by (Asm_simp_tac 1);
paulson@3339
   618
by (induct_tac "x" 1);
paulson@1713
   619
by (ALLGOALS (asm_simp_tac (!simpset addsimps [add_less_mono])));
paulson@1713
   620
qed "mult_less_mono2";
paulson@1713
   621
paulson@3234
   622
goal Arith.thy "!!i::nat. [| i<j; 0<k |] ==> i*k < j*k";
paulson@3234
   623
bd mult_less_mono2 1;
paulson@3234
   624
by (ALLGOALS (asm_full_simp_tac (!simpset addsimps [mult_commute])));
paulson@3234
   625
qed "mult_less_mono1";
paulson@3234
   626
paulson@1713
   627
goal Arith.thy "(0 < m*n) = (0<m & 0<n)";
paulson@3339
   628
by (induct_tac "m" 1);
paulson@3339
   629
by (induct_tac "n" 2);
paulson@1713
   630
by (ALLGOALS Asm_simp_tac);
paulson@1713
   631
qed "zero_less_mult_iff";
paulson@1713
   632
paulson@1795
   633
goal Arith.thy "(m*n = 1) = (m=1 & n=1)";
paulson@3339
   634
by (induct_tac "m" 1);
paulson@1795
   635
by (Simp_tac 1);
paulson@3339
   636
by (induct_tac "n" 1);
paulson@1795
   637
by (Simp_tac 1);
paulson@1795
   638
by (fast_tac (!claset addss !simpset) 1);
paulson@1795
   639
qed "mult_eq_1_iff";
paulson@1795
   640
paulson@3234
   641
goal Arith.thy "!!k. 0<k ==> (m*k < n*k) = (m<n)";
paulson@3234
   642
by (safe_tac (!claset addSIs [mult_less_mono1]));
paulson@3234
   643
by (cut_facts_tac [less_linear] 1);
paulson@3234
   644
by (blast_tac (!claset addDs [mult_less_mono1] addEs [less_asym]) 1);
paulson@3234
   645
qed "mult_less_cancel2";
paulson@3234
   646
paulson@3234
   647
goal Arith.thy "!!k. 0<k ==> (k*m < k*n) = (m<n)";
paulson@3234
   648
bd mult_less_cancel2 1;
paulson@3234
   649
by (asm_full_simp_tac (!simpset addsimps [mult_commute]) 1);
paulson@3234
   650
qed "mult_less_cancel1";
paulson@3234
   651
Addsimps [mult_less_cancel1, mult_less_cancel2];
paulson@3234
   652
paulson@3234
   653
goal Arith.thy "!!k. 0<k ==> (m*k = n*k) = (m=n)";
paulson@3234
   654
by (cut_facts_tac [less_linear] 1);
paulson@3234
   655
by(Step_tac 1);
paulson@3234
   656
ba 2;
paulson@3234
   657
by (ALLGOALS (dtac mult_less_mono1 THEN' assume_tac));
paulson@3234
   658
by (ALLGOALS Asm_full_simp_tac);
paulson@3234
   659
qed "mult_cancel2";
paulson@3234
   660
paulson@3234
   661
goal Arith.thy "!!k. 0<k ==> (k*m = k*n) = (m=n)";
paulson@3234
   662
bd mult_cancel2 1;
paulson@3234
   663
by (asm_full_simp_tac (!simpset addsimps [mult_commute]) 1);
paulson@3234
   664
qed "mult_cancel1";
paulson@3234
   665
Addsimps [mult_cancel1, mult_cancel2];
paulson@3234
   666
paulson@3234
   667
paulson@3234
   668
(*** More division laws ***)
paulson@3234
   669
paulson@3234
   670
goal thy "!!n. 0<n ==> m*n div n = m";
paulson@3234
   671
by (cut_inst_tac [("m", "m*n")] mod_div_equality 1);
paulson@3234
   672
ba 1;
paulson@3234
   673
by (asm_full_simp_tac (!simpset addsimps [mod_prod_nn_is_0]) 1);
paulson@3234
   674
qed "div_prod_nn_is_m";
paulson@3234
   675
Addsimps [div_prod_nn_is_m];
paulson@3234
   676
paulson@1713
   677
(*Cancellation law for division*)
paulson@1713
   678
goal Arith.thy "!!k. [| 0<n; 0<k |] ==> (k*m) div (k*n) = m div n";
paulson@1713
   679
by (res_inst_tac [("n","m")] less_induct 1);
paulson@1713
   680
by (case_tac "na<n" 1);
paulson@1713
   681
by (asm_simp_tac (!simpset addsimps [div_less, zero_less_mult_iff, 
paulson@2031
   682
                                     mult_less_mono2]) 1);
paulson@1713
   683
by (subgoal_tac "~ k*na < k*n" 1);
paulson@1713
   684
by (asm_simp_tac
paulson@1713
   685
     (!simpset addsimps [zero_less_mult_iff, div_geq,
paulson@2031
   686
                         diff_mult_distrib2 RS sym, diff_less]) 1);
paulson@1713
   687
by (asm_full_simp_tac (!simpset addsimps [not_less_iff_le, 
paulson@2031
   688
                                          le_refl RS mult_le_mono]) 1);
paulson@1713
   689
qed "div_cancel";
paulson@3234
   690
Addsimps [div_cancel];
paulson@1713
   691
paulson@1713
   692
goal Arith.thy "!!k. [| 0<n; 0<k |] ==> (k*m) mod (k*n) = k * (m mod n)";
paulson@1713
   693
by (res_inst_tac [("n","m")] less_induct 1);
paulson@1713
   694
by (case_tac "na<n" 1);
paulson@1713
   695
by (asm_simp_tac (!simpset addsimps [mod_less, zero_less_mult_iff, 
paulson@2031
   696
                                     mult_less_mono2]) 1);
paulson@1713
   697
by (subgoal_tac "~ k*na < k*n" 1);
paulson@1713
   698
by (asm_simp_tac
paulson@1713
   699
     (!simpset addsimps [zero_less_mult_iff, mod_geq,
paulson@2031
   700
                         diff_mult_distrib2 RS sym, diff_less]) 1);
paulson@1713
   701
by (asm_full_simp_tac (!simpset addsimps [not_less_iff_le, 
paulson@2031
   702
                                          le_refl RS mult_le_mono]) 1);
paulson@1713
   703
qed "mult_mod_distrib";
paulson@1713
   704
paulson@1713
   705
paulson@1795
   706
(** Lemma for gcd **)
paulson@1795
   707
paulson@1795
   708
goal Arith.thy "!!m n. m = m*n ==> n=1 | m=0";
paulson@1795
   709
by (dtac sym 1);
paulson@1795
   710
by (rtac disjCI 1);
paulson@1795
   711
by (rtac nat_less_cases 1 THEN assume_tac 2);
paulson@1909
   712
by (fast_tac (!claset addSEs [less_SucE] addss !simpset) 1);
paulson@1979
   713
by (best_tac (!claset addDs [mult_less_mono2] 
paulson@1795
   714
                      addss (!simpset addsimps [zero_less_eq RS sym])) 1);
paulson@1795
   715
qed "mult_eq_self_implies_10";
paulson@1795
   716
paulson@1795
   717
paulson@3234
   718
(*** Subtraction laws -- from Clemens Ballarin ***)
paulson@3234
   719
paulson@3234
   720
goal Arith.thy "!! a b c::nat. [| a < b; c <= a |] ==> a-c < b-c";
paulson@3234
   721
by (subgoal_tac "c+(a-c) < c+(b-c)" 1);
paulson@3234
   722
by (Asm_full_simp_tac 1);
paulson@3234
   723
by (subgoal_tac "c <= b" 1);
paulson@3234
   724
by (blast_tac (!claset addIs [less_imp_le, le_trans]) 2);
paulson@3234
   725
by (asm_simp_tac (!simpset addsimps [leD RS add_diff_inverse]) 1);
paulson@3234
   726
qed "diff_less_mono";
paulson@3234
   727
paulson@3234
   728
goal Arith.thy "!! a b c::nat. a+b < c ==> a < c-b";
paulson@3234
   729
bd diff_less_mono 1;
paulson@3234
   730
br le_add2 1;
paulson@3234
   731
by (Asm_full_simp_tac 1);
paulson@3234
   732
qed "add_less_imp_less_diff";
paulson@3234
   733
paulson@3234
   734
goal Arith.thy "!! n. n <= m ==> Suc m - n = Suc (m - n)";
paulson@3234
   735
br Suc_diff_n 1;
paulson@3234
   736
by (asm_full_simp_tac (!simpset addsimps [le_eq_less_Suc]) 1);
paulson@3234
   737
qed "Suc_diff_le";
paulson@3234
   738
paulson@3234
   739
goal Arith.thy "!! n. Suc i <= n ==> Suc (n - Suc i) = n - i";
paulson@3234
   740
by (asm_full_simp_tac
paulson@3234
   741
    (!simpset addsimps [Suc_diff_n RS sym, le_eq_less_Suc]) 1);
paulson@3234
   742
qed "Suc_diff_Suc";
paulson@3234
   743
paulson@3234
   744
goal Arith.thy "!! i::nat. i <= n ==> n - (n - i) = i";
paulson@3234
   745
by (subgoal_tac "(n-i) + (n - (n-i)) = (n-i) + i" 1);
paulson@3234
   746
by (Asm_full_simp_tac 1);
paulson@3234
   747
by (asm_simp_tac (!simpset addsimps [leD RS add_diff_inverse, diff_le_self, 
paulson@3234
   748
				     add_commute]) 1);
paulson@3234
   749
qed "diff_diff_cancel";
paulson@3234
   750
paulson@3234
   751
goal Arith.thy "!!k::nat. k <= n ==> m <= n + m - k";
paulson@3234
   752
be rev_mp 1;
paulson@3234
   753
by (res_inst_tac [("m", "k"), ("n", "n")] diff_induct 1);
paulson@3234
   754
by (Simp_tac 1);
paulson@3234
   755
by (simp_tac (!simpset addsimps [less_add_Suc2, less_imp_le]) 1);
paulson@3234
   756
by (Simp_tac 1);
paulson@3234
   757
qed "le_add_diff";
paulson@3234
   758
paulson@3234
   759