src/HOL/Arith.ML
author paulson
Mon Jun 02 12:12:27 1997 +0200 (1997-06-02)
changeset 3381 2bac33ec2b0d
parent 3366 2402c6ab1561
child 3396 aa74c71c3982
permissions -rw-r--r--
New theorems le_add_diff_inverse, le_add_diff_inverse2
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@3381
   328
goal Arith.thy "~ m<n --> n+(m-n) = (m::nat)";
paulson@3234
   329
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3352
   330
by (ALLGOALS Asm_simp_tac);
paulson@3381
   331
qed_spec_mp "add_diff_inverse";
paulson@3381
   332
paulson@3381
   333
goal Arith.thy "!!m. n<=m ==> n+(m-n) = (m::nat)";
paulson@3381
   334
by (asm_simp_tac (!simpset addsimps [add_diff_inverse, not_less_iff_le]) 1);
paulson@3381
   335
qed "le_add_diff_inverse";
paulson@3234
   336
paulson@3381
   337
goal Arith.thy "!!m. n<=m ==> (m-n)+n = (m::nat)";
paulson@3381
   338
by (asm_simp_tac (!simpset addsimps [le_add_diff_inverse, add_commute]) 1);
paulson@3381
   339
qed "le_add_diff_inverse2";
paulson@3381
   340
paulson@3381
   341
Addsimps  [le_add_diff_inverse, le_add_diff_inverse2];
paulson@3234
   342
Delsimps  [diff_Suc];
paulson@3234
   343
paulson@3234
   344
paulson@3234
   345
(*** More results about difference ***)
paulson@3234
   346
paulson@3352
   347
val [prem] = goal Arith.thy "n < Suc(m) ==> Suc(m)-n = Suc(m-n)";
paulson@3352
   348
by (rtac (prem RS rev_mp) 1);
paulson@3352
   349
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3352
   350
by (ALLGOALS Asm_simp_tac);
paulson@3352
   351
qed "Suc_diff_n";
paulson@3352
   352
paulson@3234
   353
goal Arith.thy "m - n < Suc(m)";
paulson@3234
   354
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3234
   355
by (etac less_SucE 3);
paulson@3234
   356
by (ALLGOALS (asm_simp_tac (!simpset addsimps [less_Suc_eq])));
paulson@3234
   357
qed "diff_less_Suc";
paulson@3234
   358
paulson@3234
   359
goal Arith.thy "!!m::nat. m - n <= m";
paulson@3234
   360
by (res_inst_tac [("m","m"), ("n","n")] diff_induct 1);
paulson@3234
   361
by (ALLGOALS Asm_simp_tac);
paulson@3234
   362
qed "diff_le_self";
paulson@3234
   363
paulson@3352
   364
goal Arith.thy "!!i::nat. i-j-k = i - (j+k)";
paulson@3352
   365
by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
paulson@3352
   366
by (ALLGOALS Asm_simp_tac);
paulson@3352
   367
qed "diff_diff_left";
paulson@3352
   368
paulson@3352
   369
(*This and the next few suggested by Florian Kammüller*)
paulson@3352
   370
goal Arith.thy "!!i::nat. i-j-k = i-k-j";
paulson@3352
   371
by (simp_tac (!simpset addsimps [diff_diff_left, add_commute]) 1);
paulson@3352
   372
qed "diff_commute";
paulson@3352
   373
paulson@3352
   374
goal Arith.thy "!!i j k:: nat. k<=j --> j<=i --> i - (j - k) = i - j + k";
paulson@3352
   375
by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
paulson@3352
   376
by (ALLGOALS Asm_simp_tac);
paulson@3352
   377
by (asm_simp_tac
paulson@3352
   378
    (!simpset addsimps [Suc_diff_n, le_imp_less_Suc, le_Suc_eq]) 1);
paulson@3352
   379
qed_spec_mp "diff_diff_right";
paulson@3352
   380
paulson@3352
   381
goal Arith.thy "!!i j k:: nat. k<=j --> (i + j) - k = i + (j - k)";
paulson@3352
   382
by (res_inst_tac [("m","j"),("n","k")] diff_induct 1);
paulson@3352
   383
by (ALLGOALS Asm_simp_tac);
paulson@3352
   384
qed_spec_mp "diff_add_assoc";
paulson@3352
   385
paulson@3234
   386
goal Arith.thy "!!n::nat. (n+m) - n = m";
paulson@3339
   387
by (induct_tac "n" 1);
paulson@3234
   388
by (ALLGOALS Asm_simp_tac);
paulson@3234
   389
qed "diff_add_inverse";
paulson@3234
   390
Addsimps [diff_add_inverse];
paulson@3234
   391
paulson@3234
   392
goal Arith.thy "!!n::nat.(m+n) - n = m";
paulson@3352
   393
by (simp_tac (!simpset addsimps [diff_add_assoc]) 1);
paulson@3234
   394
qed "diff_add_inverse2";
paulson@3234
   395
Addsimps [diff_add_inverse2];
paulson@3234
   396
paulson@3366
   397
goal Arith.thy "!!i j k::nat. i<=j ==> (j-i=k) = (j=k+i)";
paulson@3366
   398
by (Step_tac 1);
paulson@3381
   399
by (ALLGOALS Asm_simp_tac);
paulson@3366
   400
qed "le_imp_diff_is_add";
paulson@3366
   401
paulson@3234
   402
val [prem] = goal Arith.thy "m < Suc(n) ==> m-n = 0";
paulson@3234
   403
by (rtac (prem RS rev_mp) 1);
paulson@3234
   404
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3234
   405
by (asm_simp_tac (!simpset addsimps [less_Suc_eq]) 1);
paulson@3352
   406
by (ALLGOALS Asm_simp_tac);
paulson@3234
   407
qed "less_imp_diff_is_0";
paulson@3234
   408
paulson@3234
   409
val prems = goal Arith.thy "m-n = 0  -->  n-m = 0  -->  m=n";
paulson@3234
   410
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3234
   411
by (REPEAT(Simp_tac 1 THEN TRY(atac 1)));
paulson@3234
   412
qed_spec_mp "diffs0_imp_equal";
paulson@3234
   413
paulson@3234
   414
val [prem] = goal Arith.thy "m<n ==> 0<n-m";
paulson@3234
   415
by (rtac (prem RS rev_mp) 1);
paulson@3234
   416
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3352
   417
by (ALLGOALS Asm_simp_tac);
paulson@3234
   418
qed "less_imp_diff_positive";
paulson@3234
   419
paulson@3234
   420
goal Arith.thy "Suc(m)-n = (if m<n then 0 else Suc(m-n))";
paulson@3234
   421
by (simp_tac (!simpset addsimps [less_imp_diff_is_0, not_less_eq, Suc_diff_n]
paulson@3234
   422
                    setloop (split_tac [expand_if])) 1);
paulson@3234
   423
qed "if_Suc_diff_n";
paulson@3234
   424
paulson@3234
   425
goal Arith.thy "P(k) --> (!n. P(Suc(n))--> P(n)) --> P(k-i)";
paulson@3234
   426
by (res_inst_tac [("m","k"),("n","i")] diff_induct 1);
paulson@3234
   427
by (ALLGOALS (strip_tac THEN' Simp_tac THEN' TRY o Blast_tac));
paulson@3234
   428
qed "zero_induct_lemma";
paulson@3234
   429
paulson@3234
   430
val prems = goal Arith.thy "[| P(k);  !!n. P(Suc(n)) ==> P(n) |] ==> P(0)";
paulson@3234
   431
by (rtac (diff_self_eq_0 RS subst) 1);
paulson@3234
   432
by (rtac (zero_induct_lemma RS mp RS mp) 1);
paulson@3234
   433
by (REPEAT (ares_tac ([impI,allI]@prems) 1));
paulson@3234
   434
qed "zero_induct";
paulson@3234
   435
paulson@3234
   436
goal Arith.thy "!!k::nat. (k+m) - (k+n) = m - n";
paulson@3339
   437
by (induct_tac "k" 1);
paulson@3234
   438
by (ALLGOALS Asm_simp_tac);
paulson@3234
   439
qed "diff_cancel";
paulson@3234
   440
Addsimps [diff_cancel];
paulson@3234
   441
paulson@3234
   442
goal Arith.thy "!!m::nat. (m+k) - (n+k) = m - n";
paulson@3234
   443
val add_commute_k = read_instantiate [("n","k")] add_commute;
paulson@3234
   444
by (asm_simp_tac (!simpset addsimps ([add_commute_k])) 1);
paulson@3234
   445
qed "diff_cancel2";
paulson@3234
   446
Addsimps [diff_cancel2];
paulson@3234
   447
paulson@3234
   448
(*From Clemens Ballarin*)
paulson@3234
   449
goal Arith.thy "!!n::nat. [| k<=n; n<=m |] ==> (m-k) - (n-k) = m-n";
paulson@3234
   450
by (subgoal_tac "k<=n --> n<=m --> (m-k) - (n-k) = m-n" 1);
paulson@3234
   451
by (Asm_full_simp_tac 1);
paulson@3339
   452
by (induct_tac "k" 1);
paulson@3234
   453
by (Simp_tac 1);
paulson@3234
   454
(* Induction step *)
paulson@3234
   455
by (subgoal_tac "Suc na <= m --> n <= m --> Suc na <= n --> \
paulson@3234
   456
\                Suc (m - Suc na) - Suc (n - Suc na) = m-n" 1);
paulson@3234
   457
by (Asm_full_simp_tac 1);
paulson@3234
   458
by (blast_tac (!claset addIs [le_trans]) 1);
paulson@3234
   459
by (auto_tac (!claset addIs [Suc_leD], !simpset delsimps [diff_Suc_Suc]));
paulson@3234
   460
by (asm_full_simp_tac (!simpset delsimps [Suc_less_eq] 
paulson@3234
   461
		       addsimps [Suc_diff_n RS sym, le_eq_less_Suc]) 1);
paulson@3234
   462
qed "diff_right_cancel";
paulson@3234
   463
paulson@3234
   464
goal Arith.thy "!!n::nat. n - (n+m) = 0";
paulson@3339
   465
by (induct_tac "n" 1);
paulson@3234
   466
by (ALLGOALS Asm_simp_tac);
paulson@3234
   467
qed "diff_add_0";
paulson@3234
   468
Addsimps [diff_add_0];
paulson@3234
   469
paulson@3234
   470
(** Difference distributes over multiplication **)
paulson@3234
   471
paulson@3234
   472
goal Arith.thy "!!m::nat. (m - n) * k = (m * k) - (n * k)";
paulson@3234
   473
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3234
   474
by (ALLGOALS Asm_simp_tac);
paulson@3234
   475
qed "diff_mult_distrib" ;
paulson@3234
   476
paulson@3234
   477
goal Arith.thy "!!m::nat. k * (m - n) = (k * m) - (k * n)";
paulson@3234
   478
val mult_commute_k = read_instantiate [("m","k")] mult_commute;
paulson@3234
   479
by (simp_tac (!simpset addsimps [diff_mult_distrib, mult_commute_k]) 1);
paulson@3234
   480
qed "diff_mult_distrib2" ;
paulson@3234
   481
(*NOT added as rewrites, since sometimes they are used from right-to-left*)
paulson@3234
   482
paulson@3234
   483
paulson@1713
   484
(*** Monotonicity of Multiplication ***)
paulson@1713
   485
paulson@1713
   486
goal Arith.thy "!!i::nat. i<=j ==> i*k<=j*k";
paulson@3339
   487
by (induct_tac "k" 1);
paulson@1713
   488
by (ALLGOALS (asm_simp_tac (!simpset addsimps [add_le_mono])));
paulson@1713
   489
qed "mult_le_mono1";
paulson@1713
   490
paulson@1713
   491
(*<=monotonicity, BOTH arguments*)
paulson@1713
   492
goal Arith.thy "!!i::nat. [| i<=j; k<=l |] ==> i*k<=j*l";
paulson@2007
   493
by (etac (mult_le_mono1 RS le_trans) 1);
paulson@1713
   494
by (rtac le_trans 1);
paulson@2007
   495
by (stac mult_commute 2);
paulson@2007
   496
by (etac mult_le_mono1 2);
paulson@2007
   497
by (simp_tac (!simpset addsimps [mult_commute]) 1);
paulson@1713
   498
qed "mult_le_mono";
paulson@1713
   499
paulson@1713
   500
(*strict, in 1st argument; proof is by induction on k>0*)
paulson@1713
   501
goal Arith.thy "!!i::nat. [| i<j; 0<k |] ==> k*i < k*j";
paulson@3339
   502
by (eres_inst_tac [("i","0")] less_natE 1);
paulson@1713
   503
by (Asm_simp_tac 1);
paulson@3339
   504
by (induct_tac "x" 1);
paulson@1713
   505
by (ALLGOALS (asm_simp_tac (!simpset addsimps [add_less_mono])));
paulson@1713
   506
qed "mult_less_mono2";
paulson@1713
   507
paulson@3234
   508
goal Arith.thy "!!i::nat. [| i<j; 0<k |] ==> i*k < j*k";
paulson@3234
   509
bd mult_less_mono2 1;
paulson@3234
   510
by (ALLGOALS (asm_full_simp_tac (!simpset addsimps [mult_commute])));
paulson@3234
   511
qed "mult_less_mono1";
paulson@3234
   512
paulson@1713
   513
goal Arith.thy "(0 < m*n) = (0<m & 0<n)";
paulson@3339
   514
by (induct_tac "m" 1);
paulson@3339
   515
by (induct_tac "n" 2);
paulson@1713
   516
by (ALLGOALS Asm_simp_tac);
paulson@1713
   517
qed "zero_less_mult_iff";
paulson@1713
   518
paulson@1795
   519
goal Arith.thy "(m*n = 1) = (m=1 & n=1)";
paulson@3339
   520
by (induct_tac "m" 1);
paulson@1795
   521
by (Simp_tac 1);
paulson@3339
   522
by (induct_tac "n" 1);
paulson@1795
   523
by (Simp_tac 1);
paulson@1795
   524
by (fast_tac (!claset addss !simpset) 1);
paulson@1795
   525
qed "mult_eq_1_iff";
paulson@1795
   526
paulson@3234
   527
goal Arith.thy "!!k. 0<k ==> (m*k < n*k) = (m<n)";
paulson@3234
   528
by (safe_tac (!claset addSIs [mult_less_mono1]));
paulson@3234
   529
by (cut_facts_tac [less_linear] 1);
paulson@3234
   530
by (blast_tac (!claset addDs [mult_less_mono1] addEs [less_asym]) 1);
paulson@3234
   531
qed "mult_less_cancel2";
paulson@3234
   532
paulson@3234
   533
goal Arith.thy "!!k. 0<k ==> (k*m < k*n) = (m<n)";
paulson@3234
   534
bd mult_less_cancel2 1;
paulson@3234
   535
by (asm_full_simp_tac (!simpset addsimps [mult_commute]) 1);
paulson@3234
   536
qed "mult_less_cancel1";
paulson@3234
   537
Addsimps [mult_less_cancel1, mult_less_cancel2];
paulson@3234
   538
paulson@3234
   539
goal Arith.thy "!!k. 0<k ==> (m*k = n*k) = (m=n)";
paulson@3234
   540
by (cut_facts_tac [less_linear] 1);
paulson@3234
   541
by(Step_tac 1);
paulson@3234
   542
ba 2;
paulson@3234
   543
by (ALLGOALS (dtac mult_less_mono1 THEN' assume_tac));
paulson@3234
   544
by (ALLGOALS Asm_full_simp_tac);
paulson@3234
   545
qed "mult_cancel2";
paulson@3234
   546
paulson@3234
   547
goal Arith.thy "!!k. 0<k ==> (k*m = k*n) = (m=n)";
paulson@3234
   548
bd mult_cancel2 1;
paulson@3234
   549
by (asm_full_simp_tac (!simpset addsimps [mult_commute]) 1);
paulson@3234
   550
qed "mult_cancel1";
paulson@3234
   551
Addsimps [mult_cancel1, mult_cancel2];
paulson@3234
   552
paulson@3234
   553
paulson@1795
   554
(** Lemma for gcd **)
paulson@1795
   555
paulson@1795
   556
goal Arith.thy "!!m n. m = m*n ==> n=1 | m=0";
paulson@1795
   557
by (dtac sym 1);
paulson@1795
   558
by (rtac disjCI 1);
paulson@1795
   559
by (rtac nat_less_cases 1 THEN assume_tac 2);
paulson@1909
   560
by (fast_tac (!claset addSEs [less_SucE] addss !simpset) 1);
paulson@1979
   561
by (best_tac (!claset addDs [mult_less_mono2] 
paulson@1795
   562
                      addss (!simpset addsimps [zero_less_eq RS sym])) 1);
paulson@1795
   563
qed "mult_eq_self_implies_10";
paulson@1795
   564
paulson@1795
   565
paulson@3234
   566
(*** Subtraction laws -- from Clemens Ballarin ***)
paulson@3234
   567
paulson@3234
   568
goal Arith.thy "!! a b c::nat. [| a < b; c <= a |] ==> a-c < b-c";
paulson@3234
   569
by (subgoal_tac "c+(a-c) < c+(b-c)" 1);
paulson@3381
   570
by (Full_simp_tac 1);
paulson@3234
   571
by (subgoal_tac "c <= b" 1);
paulson@3234
   572
by (blast_tac (!claset addIs [less_imp_le, le_trans]) 2);
paulson@3381
   573
by (Asm_simp_tac 1);
paulson@3234
   574
qed "diff_less_mono";
paulson@3234
   575
paulson@3234
   576
goal Arith.thy "!! a b c::nat. a+b < c ==> a < c-b";
paulson@3234
   577
bd diff_less_mono 1;
paulson@3234
   578
br le_add2 1;
paulson@3234
   579
by (Asm_full_simp_tac 1);
paulson@3234
   580
qed "add_less_imp_less_diff";
paulson@3234
   581
paulson@3234
   582
goal Arith.thy "!! n. n <= m ==> Suc m - n = Suc (m - n)";
paulson@3234
   583
br Suc_diff_n 1;
paulson@3234
   584
by (asm_full_simp_tac (!simpset addsimps [le_eq_less_Suc]) 1);
paulson@3234
   585
qed "Suc_diff_le";
paulson@3234
   586
paulson@3234
   587
goal Arith.thy "!! n. Suc i <= n ==> Suc (n - Suc i) = n - i";
paulson@3234
   588
by (asm_full_simp_tac
paulson@3234
   589
    (!simpset addsimps [Suc_diff_n RS sym, le_eq_less_Suc]) 1);
paulson@3234
   590
qed "Suc_diff_Suc";
paulson@3234
   591
paulson@3234
   592
goal Arith.thy "!! i::nat. i <= n ==> n - (n - i) = i";
paulson@3234
   593
by (subgoal_tac "(n-i) + (n - (n-i)) = (n-i) + i" 1);
paulson@3381
   594
by (Full_simp_tac 1);
paulson@3381
   595
by (asm_simp_tac (!simpset addsimps [diff_le_self, add_commute]) 1);
paulson@3234
   596
qed "diff_diff_cancel";
paulson@3381
   597
Addsimps [diff_diff_cancel];
paulson@3234
   598
paulson@3234
   599
goal Arith.thy "!!k::nat. k <= n ==> m <= n + m - k";
paulson@3234
   600
be rev_mp 1;
paulson@3234
   601
by (res_inst_tac [("m", "k"), ("n", "n")] diff_induct 1);
paulson@3234
   602
by (Simp_tac 1);
paulson@3234
   603
by (simp_tac (!simpset addsimps [less_add_Suc2, less_imp_le]) 1);
paulson@3234
   604
by (Simp_tac 1);
paulson@3234
   605
qed "le_add_diff";
paulson@3234
   606
paulson@3234
   607