src/HOL/Arith.ML
author paulson
Fri Dec 12 10:31:25 1997 +0100 (1997-12-12)
changeset 4389 1865cb8df116
parent 4378 e52f864c5b88
child 4423 a129b817b58a
permissions -rw-r--r--
Faster proof of mult_less_cancel2
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
(*** Basic rewrite rules for the arithmetic operators ***)
clasohm@923
    11
nipkow@3896
    12
clasohm@923
    13
(** Difference **)
clasohm@923
    14
nipkow@4360
    15
qed_goal "diff_0_eq_0" Arith.thy
clasohm@923
    16
    "0 - n = 0"
paulson@3339
    17
 (fn _ => [induct_tac "n" 1,  ALLGOALS Asm_simp_tac]);
clasohm@923
    18
clasohm@923
    19
(*Must simplify BEFORE the induction!!  (Else we get a critical pair)
clasohm@923
    20
  Suc(m) - Suc(n)   rewrites to   pred(Suc(m) - n)  *)
nipkow@4360
    21
qed_goal "diff_Suc_Suc" Arith.thy
clasohm@923
    22
    "Suc(m) - Suc(n) = m - n"
clasohm@923
    23
 (fn _ =>
paulson@3339
    24
  [Simp_tac 1, induct_tac "n" 1, ALLGOALS Asm_simp_tac]);
clasohm@923
    25
pusch@2682
    26
Addsimps [diff_0_eq_0, diff_Suc_Suc];
clasohm@923
    27
nipkow@4360
    28
(* Could be (and is, below) generalized in various ways;
nipkow@4360
    29
   However, none of the generalizations are currently in the simpset,
nipkow@4360
    30
   and I dread to think what happens if I put them in *)
nipkow@4360
    31
goal Arith.thy "!!n. 0 < n ==> Suc(n-1) = n";
nipkow@4360
    32
by(asm_simp_tac (simpset() addsplits [expand_nat_case]) 1);
nipkow@4360
    33
qed "Suc_pred";
nipkow@4360
    34
Addsimps [Suc_pred];
nipkow@4360
    35
nipkow@4360
    36
(* Generalize? *)
nipkow@4360
    37
goal Arith.thy "!!n. 0<n ==> n-1 < n";
nipkow@4360
    38
by(asm_simp_tac (simpset() addsplits [expand_nat_case]) 1);
nipkow@4360
    39
qed "pred_less";
nipkow@4360
    40
Addsimps [pred_less];
nipkow@4360
    41
nipkow@4360
    42
Delsimps [diff_Suc];
nipkow@4360
    43
clasohm@923
    44
clasohm@923
    45
(**** Inductive properties of the operators ****)
clasohm@923
    46
clasohm@923
    47
(*** Addition ***)
clasohm@923
    48
clasohm@923
    49
qed_goal "add_0_right" Arith.thy "m + 0 = m"
paulson@3339
    50
 (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
clasohm@923
    51
clasohm@923
    52
qed_goal "add_Suc_right" Arith.thy "m + Suc(n) = Suc(m+n)"
paulson@3339
    53
 (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
clasohm@923
    54
clasohm@1264
    55
Addsimps [add_0_right,add_Suc_right];
clasohm@923
    56
clasohm@923
    57
(*Associative law for addition*)
clasohm@923
    58
qed_goal "add_assoc" Arith.thy "(m + n) + k = m + ((n + k)::nat)"
paulson@3339
    59
 (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
clasohm@923
    60
clasohm@923
    61
(*Commutative law for addition*)  
clasohm@923
    62
qed_goal "add_commute" Arith.thy "m + n = n + (m::nat)"
paulson@3339
    63
 (fn _ =>  [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
clasohm@923
    64
clasohm@923
    65
qed_goal "add_left_commute" Arith.thy "x+(y+z)=y+((x+z)::nat)"
clasohm@923
    66
 (fn _ => [rtac (add_commute RS trans) 1, rtac (add_assoc RS trans) 1,
clasohm@923
    67
           rtac (add_commute RS arg_cong) 1]);
clasohm@923
    68
clasohm@923
    69
(*Addition is an AC-operator*)
clasohm@923
    70
val add_ac = [add_assoc, add_commute, add_left_commute];
clasohm@923
    71
clasohm@923
    72
goal Arith.thy "!!k::nat. (k + m = k + n) = (m=n)";
paulson@3339
    73
by (induct_tac "k" 1);
clasohm@1264
    74
by (Simp_tac 1);
clasohm@1264
    75
by (Asm_simp_tac 1);
clasohm@923
    76
qed "add_left_cancel";
clasohm@923
    77
clasohm@923
    78
goal Arith.thy "!!k::nat. (m + k = n + k) = (m=n)";
paulson@3339
    79
by (induct_tac "k" 1);
clasohm@1264
    80
by (Simp_tac 1);
clasohm@1264
    81
by (Asm_simp_tac 1);
clasohm@923
    82
qed "add_right_cancel";
clasohm@923
    83
clasohm@923
    84
goal Arith.thy "!!k::nat. (k + m <= k + n) = (m<=n)";
paulson@3339
    85
by (induct_tac "k" 1);
clasohm@1264
    86
by (Simp_tac 1);
clasohm@1264
    87
by (Asm_simp_tac 1);
clasohm@923
    88
qed "add_left_cancel_le";
clasohm@923
    89
clasohm@923
    90
goal Arith.thy "!!k::nat. (k + m < k + n) = (m<n)";
paulson@3339
    91
by (induct_tac "k" 1);
clasohm@1264
    92
by (Simp_tac 1);
clasohm@1264
    93
by (Asm_simp_tac 1);
clasohm@923
    94
qed "add_left_cancel_less";
clasohm@923
    95
nipkow@1327
    96
Addsimps [add_left_cancel, add_right_cancel,
nipkow@1327
    97
          add_left_cancel_le, add_left_cancel_less];
nipkow@1327
    98
paulson@3339
    99
(** Reasoning about m+0=0, etc. **)
paulson@3339
   100
nipkow@1327
   101
goal Arith.thy "(m+n = 0) = (m=0 & n=0)";
paulson@3339
   102
by (induct_tac "m" 1);
nipkow@1327
   103
by (ALLGOALS Asm_simp_tac);
nipkow@1327
   104
qed "add_is_0";
nipkow@4360
   105
AddIffs [add_is_0];
nipkow@1327
   106
nipkow@4360
   107
goal Arith.thy "(0<m+n) = (0<m | 0<n)";
nipkow@4360
   108
by(simp_tac (simpset() delsimps [neq0_conv] addsimps [neq0_conv RS sym]) 1);
nipkow@4360
   109
qed "add_gr_0";
nipkow@4360
   110
AddIffs [add_gr_0];
nipkow@4360
   111
nipkow@4360
   112
(* FIXME: really needed?? *)
nipkow@4360
   113
goal Arith.thy "((m+n)-1 = 0) = (m=0 & n-1 = 0 | m-1 = 0 & n=0)";
nipkow@4360
   114
by (exhaust_tac "m" 1);
wenzelm@4089
   115
by (ALLGOALS (fast_tac (claset() addss (simpset()))));
paulson@3293
   116
qed "pred_add_is_0";
paulson@3293
   117
Addsimps [pred_add_is_0];
paulson@3293
   118
nipkow@4360
   119
(* Could be generalized, eg to "!!n. k<n ==> m+(n-(Suc k)) = (m+n)-(Suc k)" *)
nipkow@4378
   120
goal Arith.thy "!!n. 0<n ==> m + (n-1) = (m+n)-1";
nipkow@4360
   121
by (exhaust_tac "m" 1);
nipkow@4360
   122
by (ALLGOALS (asm_simp_tac (simpset() addsimps [diff_Suc]
nipkow@4360
   123
                                      addsplits [expand_nat_case])));
nipkow@1327
   124
qed "add_pred";
nipkow@1327
   125
Addsimps [add_pred];
nipkow@1327
   126
paulson@1626
   127
clasohm@923
   128
(**** Additional theorems about "less than" ****)
clasohm@923
   129
paulson@3339
   130
goal Arith.thy "i<j --> (EX k. j = Suc(i+k))";
paulson@3339
   131
by (induct_tac "j" 1);
paulson@1909
   132
by (Simp_tac 1);
wenzelm@4089
   133
by (blast_tac (claset() addSEs [less_SucE] 
paulson@3339
   134
                       addSIs [add_0_right RS sym, add_Suc_right RS sym]) 1);
paulson@1909
   135
val lemma = result();
paulson@1909
   136
paulson@3339
   137
(* [| i<j;  !!x. j = Suc(i+x) ==> Q |] ==> Q *)
paulson@3339
   138
bind_thm ("less_natE", lemma RS mp RS exE);
paulson@3339
   139
clasohm@923
   140
goal Arith.thy "!!m. m<n --> (? k. n=Suc(m+k))";
paulson@3339
   141
by (induct_tac "n" 1);
wenzelm@4089
   142
by (ALLGOALS (simp_tac (simpset() addsimps [less_Suc_eq])));
wenzelm@4089
   143
by (blast_tac (claset() addSEs [less_SucE] 
paulson@3339
   144
                       addSIs [add_0_right RS sym, add_Suc_right RS sym]) 1);
nipkow@1485
   145
qed_spec_mp "less_eq_Suc_add";
clasohm@923
   146
clasohm@923
   147
goal Arith.thy "n <= ((m + n)::nat)";
paulson@3339
   148
by (induct_tac "m" 1);
clasohm@1264
   149
by (ALLGOALS Simp_tac);
clasohm@923
   150
by (etac le_trans 1);
clasohm@923
   151
by (rtac (lessI RS less_imp_le) 1);
clasohm@923
   152
qed "le_add2";
clasohm@923
   153
clasohm@923
   154
goal Arith.thy "n <= ((n + m)::nat)";
wenzelm@4089
   155
by (simp_tac (simpset() addsimps add_ac) 1);
clasohm@923
   156
by (rtac le_add2 1);
clasohm@923
   157
qed "le_add1";
clasohm@923
   158
clasohm@923
   159
bind_thm ("less_add_Suc1", (lessI RS (le_add1 RS le_less_trans)));
clasohm@923
   160
bind_thm ("less_add_Suc2", (lessI RS (le_add2 RS le_less_trans)));
clasohm@923
   161
clasohm@923
   162
(*"i <= j ==> i <= j+m"*)
clasohm@923
   163
bind_thm ("trans_le_add1", le_add1 RSN (2,le_trans));
clasohm@923
   164
clasohm@923
   165
(*"i <= j ==> i <= m+j"*)
clasohm@923
   166
bind_thm ("trans_le_add2", le_add2 RSN (2,le_trans));
clasohm@923
   167
clasohm@923
   168
(*"i < j ==> i < j+m"*)
clasohm@923
   169
bind_thm ("trans_less_add1", le_add1 RSN (2,less_le_trans));
clasohm@923
   170
clasohm@923
   171
(*"i < j ==> i < m+j"*)
clasohm@923
   172
bind_thm ("trans_less_add2", le_add2 RSN (2,less_le_trans));
clasohm@923
   173
nipkow@1152
   174
goal Arith.thy "!!i. i+j < (k::nat) ==> i<k";
paulson@1552
   175
by (etac rev_mp 1);
paulson@3339
   176
by (induct_tac "j" 1);
clasohm@1264
   177
by (ALLGOALS Asm_simp_tac);
wenzelm@4089
   178
by (blast_tac (claset() addDs [Suc_lessD]) 1);
nipkow@1152
   179
qed "add_lessD1";
nipkow@1152
   180
paulson@3234
   181
goal Arith.thy "!!i::nat. ~ (i+j < i)";
paulson@3457
   182
by (rtac notI 1);
paulson@3457
   183
by (etac (add_lessD1 RS less_irrefl) 1);
paulson@3234
   184
qed "not_add_less1";
paulson@3234
   185
paulson@3234
   186
goal Arith.thy "!!i::nat. ~ (j+i < i)";
wenzelm@4089
   187
by (simp_tac (simpset() addsimps [add_commute, not_add_less1]) 1);
paulson@3234
   188
qed "not_add_less2";
paulson@3234
   189
AddIffs [not_add_less1, not_add_less2];
paulson@3234
   190
clasohm@923
   191
goal Arith.thy "!!k::nat. m <= n ==> m <= n+k";
paulson@1552
   192
by (etac le_trans 1);
paulson@1552
   193
by (rtac le_add1 1);
clasohm@923
   194
qed "le_imp_add_le";
clasohm@923
   195
clasohm@923
   196
goal Arith.thy "!!k::nat. m < n ==> m < n+k";
paulson@1552
   197
by (etac less_le_trans 1);
paulson@1552
   198
by (rtac le_add1 1);
clasohm@923
   199
qed "less_imp_add_less";
clasohm@923
   200
clasohm@923
   201
goal Arith.thy "m+k<=n --> m<=(n::nat)";
paulson@3339
   202
by (induct_tac "k" 1);
clasohm@1264
   203
by (ALLGOALS Asm_simp_tac);
wenzelm@4089
   204
by (blast_tac (claset() addDs [Suc_leD]) 1);
nipkow@1485
   205
qed_spec_mp "add_leD1";
clasohm@923
   206
paulson@2498
   207
goal Arith.thy "!!n::nat. m+k<=n ==> k<=n";
wenzelm@4089
   208
by (full_simp_tac (simpset() addsimps [add_commute]) 1);
paulson@2498
   209
by (etac add_leD1 1);
paulson@2498
   210
qed_spec_mp "add_leD2";
paulson@2498
   211
paulson@2498
   212
goal Arith.thy "!!n::nat. m+k<=n ==> m<=n & k<=n";
wenzelm@4089
   213
by (blast_tac (claset() addDs [add_leD1, add_leD2]) 1);
paulson@2498
   214
bind_thm ("add_leE", result() RS conjE);
paulson@2498
   215
clasohm@923
   216
goal Arith.thy "!!k l::nat. [| k<l; m+l = k+n |] ==> m<n";
wenzelm@4089
   217
by (safe_tac (claset() addSDs [less_eq_Suc_add]));
clasohm@923
   218
by (asm_full_simp_tac
wenzelm@4089
   219
    (simpset() delsimps [add_Suc_right]
clasohm@1264
   220
                addsimps ([add_Suc_right RS sym, add_left_cancel] @add_ac)) 1);
paulson@1552
   221
by (etac subst 1);
wenzelm@4089
   222
by (simp_tac (simpset() addsimps [less_add_Suc1]) 1);
clasohm@923
   223
qed "less_add_eq_less";
clasohm@923
   224
clasohm@923
   225
paulson@1713
   226
(*** Monotonicity of Addition ***)
clasohm@923
   227
clasohm@923
   228
(*strict, in 1st argument*)
clasohm@923
   229
goal Arith.thy "!!i j k::nat. i < j ==> i + k < j + k";
paulson@3339
   230
by (induct_tac "k" 1);
clasohm@1264
   231
by (ALLGOALS Asm_simp_tac);
clasohm@923
   232
qed "add_less_mono1";
clasohm@923
   233
clasohm@923
   234
(*strict, in both arguments*)
clasohm@923
   235
goal Arith.thy "!!i j k::nat. [|i < j; k < l|] ==> i + k < j + l";
clasohm@923
   236
by (rtac (add_less_mono1 RS less_trans) 1);
lcp@1198
   237
by (REPEAT (assume_tac 1));
paulson@3339
   238
by (induct_tac "j" 1);
clasohm@1264
   239
by (ALLGOALS Asm_simp_tac);
clasohm@923
   240
qed "add_less_mono";
clasohm@923
   241
clasohm@923
   242
(*A [clumsy] way of lifting < monotonicity to <= monotonicity *)
clasohm@923
   243
val [lt_mono,le] = goal Arith.thy
clasohm@1465
   244
     "[| !!i j::nat. i<j ==> f(i) < f(j);       \
clasohm@1465
   245
\        i <= j                                 \
clasohm@923
   246
\     |] ==> f(i) <= (f(j)::nat)";
clasohm@923
   247
by (cut_facts_tac [le] 1);
wenzelm@4089
   248
by (asm_full_simp_tac (simpset() addsimps [le_eq_less_or_eq]) 1);
wenzelm@4089
   249
by (blast_tac (claset() addSIs [lt_mono]) 1);
clasohm@923
   250
qed "less_mono_imp_le_mono";
clasohm@923
   251
clasohm@923
   252
(*non-strict, in 1st argument*)
clasohm@923
   253
goal Arith.thy "!!i j k::nat. i<=j ==> i + k <= j + k";
wenzelm@3842
   254
by (res_inst_tac [("f", "%j. j+k")] less_mono_imp_le_mono 1);
paulson@1552
   255
by (etac add_less_mono1 1);
clasohm@923
   256
by (assume_tac 1);
clasohm@923
   257
qed "add_le_mono1";
clasohm@923
   258
clasohm@923
   259
(*non-strict, in both arguments*)
clasohm@923
   260
goal Arith.thy "!!k l::nat. [|i<=j;  k<=l |] ==> i + k <= j + l";
clasohm@923
   261
by (etac (add_le_mono1 RS le_trans) 1);
wenzelm@4089
   262
by (simp_tac (simpset() addsimps [add_commute]) 1);
clasohm@923
   263
(*j moves to the end because it is free while k, l are bound*)
paulson@1552
   264
by (etac add_le_mono1 1);
clasohm@923
   265
qed "add_le_mono";
paulson@1713
   266
paulson@3234
   267
paulson@3234
   268
(*** Multiplication ***)
paulson@3234
   269
paulson@3234
   270
(*right annihilation in product*)
paulson@3234
   271
qed_goal "mult_0_right" Arith.thy "m * 0 = 0"
paulson@3339
   272
 (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
paulson@3234
   273
paulson@3293
   274
(*right successor law for multiplication*)
paulson@3234
   275
qed_goal "mult_Suc_right" Arith.thy  "m * Suc(n) = m + (m * n)"
paulson@3339
   276
 (fn _ => [induct_tac "m" 1,
wenzelm@4089
   277
           ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))]);
paulson@3234
   278
paulson@3293
   279
Addsimps [mult_0_right, mult_Suc_right];
paulson@3234
   280
paulson@3234
   281
goal Arith.thy "1 * n = n";
paulson@3234
   282
by (Asm_simp_tac 1);
paulson@3234
   283
qed "mult_1";
paulson@3234
   284
paulson@3234
   285
goal Arith.thy "n * 1 = n";
paulson@3234
   286
by (Asm_simp_tac 1);
paulson@3234
   287
qed "mult_1_right";
paulson@3234
   288
paulson@3234
   289
(*Commutative law for multiplication*)
paulson@3234
   290
qed_goal "mult_commute" Arith.thy "m * n = n * (m::nat)"
paulson@3339
   291
 (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
paulson@3234
   292
paulson@3234
   293
(*addition distributes over multiplication*)
paulson@3234
   294
qed_goal "add_mult_distrib" Arith.thy "(m + n)*k = (m*k) + ((n*k)::nat)"
paulson@3339
   295
 (fn _ => [induct_tac "m" 1,
wenzelm@4089
   296
           ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))]);
paulson@3234
   297
paulson@3234
   298
qed_goal "add_mult_distrib2" Arith.thy "k*(m + n) = (k*m) + ((k*n)::nat)"
paulson@3339
   299
 (fn _ => [induct_tac "m" 1,
wenzelm@4089
   300
           ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))]);
paulson@3234
   301
paulson@3234
   302
(*Associative law for multiplication*)
paulson@3234
   303
qed_goal "mult_assoc" Arith.thy "(m * n) * k = m * ((n * k)::nat)"
paulson@3339
   304
  (fn _ => [induct_tac "m" 1, 
wenzelm@4089
   305
            ALLGOALS (asm_simp_tac (simpset() addsimps [add_mult_distrib]))]);
paulson@3234
   306
paulson@3234
   307
qed_goal "mult_left_commute" Arith.thy "x*(y*z) = y*((x*z)::nat)"
paulson@3234
   308
 (fn _ => [rtac trans 1, rtac mult_commute 1, rtac trans 1,
paulson@3234
   309
           rtac mult_assoc 1, rtac (mult_commute RS arg_cong) 1]);
paulson@3234
   310
paulson@3234
   311
val mult_ac = [mult_assoc,mult_commute,mult_left_commute];
paulson@3234
   312
paulson@3293
   313
goal Arith.thy "(m*n = 0) = (m=0 | n=0)";
paulson@3339
   314
by (induct_tac "m" 1);
paulson@3339
   315
by (induct_tac "n" 2);
paulson@3293
   316
by (ALLGOALS Asm_simp_tac);
paulson@3293
   317
qed "mult_is_0";
paulson@3293
   318
Addsimps [mult_is_0];
paulson@3293
   319
paulson@4158
   320
goal Arith.thy "!!m::nat. m <= m*m";
paulson@4158
   321
by (induct_tac "m" 1);
paulson@4158
   322
by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_assoc RS sym])));
paulson@4158
   323
by (etac (le_add2 RSN (2,le_trans)) 1);
paulson@4158
   324
qed "le_square";
paulson@4158
   325
paulson@3234
   326
paulson@3234
   327
(*** Difference ***)
paulson@3234
   328
paulson@3234
   329
paulson@3234
   330
qed_goal "diff_self_eq_0" Arith.thy "m - m = 0"
paulson@3339
   331
 (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
paulson@3234
   332
Addsimps [diff_self_eq_0];
paulson@3234
   333
paulson@3234
   334
(*Addition is the inverse of subtraction: if n<=m then n+(m-n) = m. *)
paulson@3381
   335
goal Arith.thy "~ m<n --> n+(m-n) = (m::nat)";
paulson@3234
   336
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3352
   337
by (ALLGOALS Asm_simp_tac);
paulson@3381
   338
qed_spec_mp "add_diff_inverse";
paulson@3381
   339
paulson@3381
   340
goal Arith.thy "!!m. n<=m ==> n+(m-n) = (m::nat)";
wenzelm@4089
   341
by (asm_simp_tac (simpset() addsimps [add_diff_inverse, not_less_iff_le]) 1);
paulson@3381
   342
qed "le_add_diff_inverse";
paulson@3234
   343
paulson@3381
   344
goal Arith.thy "!!m. n<=m ==> (m-n)+n = (m::nat)";
wenzelm@4089
   345
by (asm_simp_tac (simpset() addsimps [le_add_diff_inverse, add_commute]) 1);
paulson@3381
   346
qed "le_add_diff_inverse2";
paulson@3381
   347
paulson@3381
   348
Addsimps  [le_add_diff_inverse, le_add_diff_inverse2];
paulson@3234
   349
paulson@3234
   350
paulson@3234
   351
(*** More results about difference ***)
paulson@3234
   352
paulson@3352
   353
val [prem] = goal Arith.thy "n < Suc(m) ==> Suc(m)-n = Suc(m-n)";
paulson@3352
   354
by (rtac (prem RS rev_mp) 1);
paulson@3352
   355
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3352
   356
by (ALLGOALS Asm_simp_tac);
paulson@3352
   357
qed "Suc_diff_n";
paulson@3352
   358
paulson@3234
   359
goal Arith.thy "m - n < Suc(m)";
paulson@3234
   360
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3234
   361
by (etac less_SucE 3);
wenzelm@4089
   362
by (ALLGOALS (asm_simp_tac (simpset() addsimps [less_Suc_eq])));
paulson@3234
   363
qed "diff_less_Suc";
paulson@3234
   364
paulson@3234
   365
goal Arith.thy "!!m::nat. m - n <= m";
paulson@3234
   366
by (res_inst_tac [("m","m"), ("n","n")] diff_induct 1);
paulson@3234
   367
by (ALLGOALS Asm_simp_tac);
paulson@3234
   368
qed "diff_le_self";
paulson@3903
   369
Addsimps [diff_le_self];
paulson@3234
   370
paulson@3352
   371
goal Arith.thy "!!i::nat. i-j-k = i - (j+k)";
paulson@3352
   372
by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
paulson@3352
   373
by (ALLGOALS Asm_simp_tac);
paulson@3352
   374
qed "diff_diff_left";
paulson@3352
   375
nipkow@4360
   376
(* This is a trivial consequence of diff_diff_left;
nipkow@4360
   377
   could be got rid of if diff_diff_left were in the simpset...
nipkow@4360
   378
*)
nipkow@4360
   379
goal Arith.thy "(Suc m - n)-1 = m - n";
nipkow@4360
   380
by(simp_tac (simpset() addsimps [diff_diff_left]) 1);
nipkow@4360
   381
qed "pred_Suc_diff";
nipkow@4360
   382
Addsimps [pred_Suc_diff];
nipkow@4360
   383
wenzelm@3396
   384
(*This and the next few suggested by Florian Kammueller*)
paulson@3352
   385
goal Arith.thy "!!i::nat. i-j-k = i-k-j";
wenzelm@4089
   386
by (simp_tac (simpset() addsimps [diff_diff_left, add_commute]) 1);
paulson@3352
   387
qed "diff_commute";
paulson@3352
   388
paulson@3352
   389
goal Arith.thy "!!i j k:: nat. k<=j --> j<=i --> i - (j - k) = i - j + k";
paulson@3352
   390
by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
paulson@3352
   391
by (ALLGOALS Asm_simp_tac);
paulson@3352
   392
by (asm_simp_tac
wenzelm@4089
   393
    (simpset() addsimps [Suc_diff_n, le_imp_less_Suc, le_Suc_eq]) 1);
paulson@3352
   394
qed_spec_mp "diff_diff_right";
paulson@3352
   395
paulson@3352
   396
goal Arith.thy "!!i j k:: nat. k<=j --> (i + j) - k = i + (j - k)";
paulson@3352
   397
by (res_inst_tac [("m","j"),("n","k")] diff_induct 1);
paulson@3352
   398
by (ALLGOALS Asm_simp_tac);
paulson@3352
   399
qed_spec_mp "diff_add_assoc";
paulson@3352
   400
paulson@3234
   401
goal Arith.thy "!!n::nat. (n+m) - n = m";
paulson@3339
   402
by (induct_tac "n" 1);
paulson@3234
   403
by (ALLGOALS Asm_simp_tac);
paulson@3234
   404
qed "diff_add_inverse";
paulson@3234
   405
Addsimps [diff_add_inverse];
paulson@3234
   406
paulson@3234
   407
goal Arith.thy "!!n::nat.(m+n) - n = m";
wenzelm@4089
   408
by (simp_tac (simpset() addsimps [diff_add_assoc]) 1);
paulson@3234
   409
qed "diff_add_inverse2";
paulson@3234
   410
Addsimps [diff_add_inverse2];
paulson@3234
   411
paulson@3366
   412
goal Arith.thy "!!i j k::nat. i<=j ==> (j-i=k) = (j=k+i)";
paulson@3724
   413
by Safe_tac;
paulson@3381
   414
by (ALLGOALS Asm_simp_tac);
paulson@3366
   415
qed "le_imp_diff_is_add";
paulson@3366
   416
paulson@3234
   417
val [prem] = goal Arith.thy "m < Suc(n) ==> m-n = 0";
paulson@3234
   418
by (rtac (prem RS rev_mp) 1);
paulson@3234
   419
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
wenzelm@4089
   420
by (asm_simp_tac (simpset() addsimps [less_Suc_eq]) 1);
paulson@3352
   421
by (ALLGOALS Asm_simp_tac);
paulson@3234
   422
qed "less_imp_diff_is_0";
paulson@3234
   423
paulson@3234
   424
val prems = goal Arith.thy "m-n = 0  -->  n-m = 0  -->  m=n";
paulson@3234
   425
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3234
   426
by (REPEAT(Simp_tac 1 THEN TRY(atac 1)));
paulson@3234
   427
qed_spec_mp "diffs0_imp_equal";
paulson@3234
   428
paulson@3234
   429
val [prem] = goal Arith.thy "m<n ==> 0<n-m";
paulson@3234
   430
by (rtac (prem RS rev_mp) 1);
paulson@3234
   431
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3352
   432
by (ALLGOALS Asm_simp_tac);
paulson@3234
   433
qed "less_imp_diff_positive";
paulson@3234
   434
paulson@3234
   435
goal Arith.thy "Suc(m)-n = (if m<n then 0 else Suc(m-n))";
wenzelm@4089
   436
by (simp_tac (simpset() addsimps [less_imp_diff_is_0, not_less_eq, Suc_diff_n]
nipkow@3919
   437
                       addsplits [expand_if]) 1);
paulson@3234
   438
qed "if_Suc_diff_n";
paulson@3234
   439
paulson@3234
   440
goal Arith.thy "P(k) --> (!n. P(Suc(n))--> P(n)) --> P(k-i)";
paulson@3234
   441
by (res_inst_tac [("m","k"),("n","i")] diff_induct 1);
paulson@3718
   442
by (ALLGOALS (Clarify_tac THEN' Simp_tac THEN' TRY o Blast_tac));
paulson@3234
   443
qed "zero_induct_lemma";
paulson@3234
   444
paulson@3234
   445
val prems = goal Arith.thy "[| P(k);  !!n. P(Suc(n)) ==> P(n) |] ==> P(0)";
paulson@3234
   446
by (rtac (diff_self_eq_0 RS subst) 1);
paulson@3234
   447
by (rtac (zero_induct_lemma RS mp RS mp) 1);
paulson@3234
   448
by (REPEAT (ares_tac ([impI,allI]@prems) 1));
paulson@3234
   449
qed "zero_induct";
paulson@3234
   450
paulson@3234
   451
goal Arith.thy "!!k::nat. (k+m) - (k+n) = m - n";
paulson@3339
   452
by (induct_tac "k" 1);
paulson@3234
   453
by (ALLGOALS Asm_simp_tac);
paulson@3234
   454
qed "diff_cancel";
paulson@3234
   455
Addsimps [diff_cancel];
paulson@3234
   456
paulson@3234
   457
goal Arith.thy "!!m::nat. (m+k) - (n+k) = m - n";
paulson@3234
   458
val add_commute_k = read_instantiate [("n","k")] add_commute;
wenzelm@4089
   459
by (asm_simp_tac (simpset() addsimps ([add_commute_k])) 1);
paulson@3234
   460
qed "diff_cancel2";
paulson@3234
   461
Addsimps [diff_cancel2];
paulson@3234
   462
paulson@3234
   463
(*From Clemens Ballarin*)
paulson@3234
   464
goal Arith.thy "!!n::nat. [| k<=n; n<=m |] ==> (m-k) - (n-k) = m-n";
paulson@3234
   465
by (subgoal_tac "k<=n --> n<=m --> (m-k) - (n-k) = m-n" 1);
paulson@3234
   466
by (Asm_full_simp_tac 1);
paulson@3339
   467
by (induct_tac "k" 1);
paulson@3234
   468
by (Simp_tac 1);
paulson@3234
   469
(* Induction step *)
paulson@3234
   470
by (subgoal_tac "Suc na <= m --> n <= m --> Suc na <= n --> \
paulson@3234
   471
\                Suc (m - Suc na) - Suc (n - Suc na) = m-n" 1);
paulson@3234
   472
by (Asm_full_simp_tac 1);
wenzelm@4089
   473
by (blast_tac (claset() addIs [le_trans]) 1);
wenzelm@4089
   474
by (auto_tac (claset() addIs [Suc_leD], simpset() delsimps [diff_Suc_Suc]));
wenzelm@4089
   475
by (asm_full_simp_tac (simpset() delsimps [Suc_less_eq] 
paulson@3234
   476
		       addsimps [Suc_diff_n RS sym, le_eq_less_Suc]) 1);
paulson@3234
   477
qed "diff_right_cancel";
paulson@3234
   478
paulson@3234
   479
goal Arith.thy "!!n::nat. n - (n+m) = 0";
paulson@3339
   480
by (induct_tac "n" 1);
paulson@3234
   481
by (ALLGOALS Asm_simp_tac);
paulson@3234
   482
qed "diff_add_0";
paulson@3234
   483
Addsimps [diff_add_0];
paulson@3234
   484
paulson@3234
   485
(** Difference distributes over multiplication **)
paulson@3234
   486
paulson@3234
   487
goal Arith.thy "!!m::nat. (m - n) * k = (m * k) - (n * k)";
paulson@3234
   488
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3234
   489
by (ALLGOALS Asm_simp_tac);
paulson@3234
   490
qed "diff_mult_distrib" ;
paulson@3234
   491
paulson@3234
   492
goal Arith.thy "!!m::nat. k * (m - n) = (k * m) - (k * n)";
paulson@3234
   493
val mult_commute_k = read_instantiate [("m","k")] mult_commute;
wenzelm@4089
   494
by (simp_tac (simpset() addsimps [diff_mult_distrib, mult_commute_k]) 1);
paulson@3234
   495
qed "diff_mult_distrib2" ;
paulson@3234
   496
(*NOT added as rewrites, since sometimes they are used from right-to-left*)
paulson@3234
   497
paulson@3234
   498
paulson@1713
   499
(*** Monotonicity of Multiplication ***)
paulson@1713
   500
paulson@1713
   501
goal Arith.thy "!!i::nat. i<=j ==> i*k<=j*k";
paulson@3339
   502
by (induct_tac "k" 1);
wenzelm@4089
   503
by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_le_mono])));
paulson@1713
   504
qed "mult_le_mono1";
paulson@1713
   505
paulson@1713
   506
(*<=monotonicity, BOTH arguments*)
paulson@1713
   507
goal Arith.thy "!!i::nat. [| i<=j; k<=l |] ==> i*k<=j*l";
paulson@2007
   508
by (etac (mult_le_mono1 RS le_trans) 1);
paulson@1713
   509
by (rtac le_trans 1);
paulson@2007
   510
by (stac mult_commute 2);
paulson@2007
   511
by (etac mult_le_mono1 2);
wenzelm@4089
   512
by (simp_tac (simpset() addsimps [mult_commute]) 1);
paulson@1713
   513
qed "mult_le_mono";
paulson@1713
   514
paulson@1713
   515
(*strict, in 1st argument; proof is by induction on k>0*)
paulson@1713
   516
goal Arith.thy "!!i::nat. [| i<j; 0<k |] ==> k*i < k*j";
paulson@3339
   517
by (eres_inst_tac [("i","0")] less_natE 1);
paulson@1713
   518
by (Asm_simp_tac 1);
paulson@3339
   519
by (induct_tac "x" 1);
wenzelm@4089
   520
by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_less_mono])));
paulson@1713
   521
qed "mult_less_mono2";
paulson@1713
   522
paulson@3234
   523
goal Arith.thy "!!i::nat. [| i<j; 0<k |] ==> i*k < j*k";
paulson@3457
   524
by (dtac mult_less_mono2 1);
wenzelm@4089
   525
by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [mult_commute])));
paulson@3234
   526
qed "mult_less_mono1";
paulson@3234
   527
paulson@1713
   528
goal Arith.thy "(0 < m*n) = (0<m & 0<n)";
paulson@3339
   529
by (induct_tac "m" 1);
paulson@3339
   530
by (induct_tac "n" 2);
paulson@1713
   531
by (ALLGOALS Asm_simp_tac);
paulson@1713
   532
qed "zero_less_mult_iff";
nipkow@4356
   533
Addsimps [zero_less_mult_iff];
paulson@1713
   534
paulson@1795
   535
goal Arith.thy "(m*n = 1) = (m=1 & n=1)";
paulson@3339
   536
by (induct_tac "m" 1);
paulson@1795
   537
by (Simp_tac 1);
paulson@3339
   538
by (induct_tac "n" 1);
paulson@1795
   539
by (Simp_tac 1);
wenzelm@4089
   540
by (fast_tac (claset() addss simpset()) 1);
paulson@1795
   541
qed "mult_eq_1_iff";
nipkow@4356
   542
Addsimps [mult_eq_1_iff];
paulson@1795
   543
paulson@3234
   544
goal Arith.thy "!!k. 0<k ==> (m*k < n*k) = (m<n)";
wenzelm@4089
   545
by (safe_tac (claset() addSIs [mult_less_mono1]));
paulson@3234
   546
by (cut_facts_tac [less_linear] 1);
paulson@4389
   547
by (blast_tac (claset() addIs [mult_less_mono1] addEs [less_asym]) 1);
paulson@3234
   548
qed "mult_less_cancel2";
paulson@3234
   549
paulson@3234
   550
goal Arith.thy "!!k. 0<k ==> (k*m < k*n) = (m<n)";
paulson@3457
   551
by (dtac mult_less_cancel2 1);
wenzelm@4089
   552
by (asm_full_simp_tac (simpset() addsimps [mult_commute]) 1);
paulson@3234
   553
qed "mult_less_cancel1";
paulson@3234
   554
Addsimps [mult_less_cancel1, mult_less_cancel2];
paulson@3234
   555
wenzelm@4297
   556
goal Arith.thy "(Suc k * m < Suc k * n) = (m < n)";
wenzelm@4297
   557
br mult_less_cancel1 1;
wenzelm@4297
   558
by (Simp_tac 1);
wenzelm@4297
   559
qed "Suc_mult_less_cancel1";
wenzelm@4297
   560
wenzelm@4297
   561
goalw Arith.thy [le_def] "(Suc k * m <= Suc k * n) = (m <= n)";
wenzelm@4297
   562
by (simp_tac (simpset_of HOL.thy) 1);
wenzelm@4297
   563
br Suc_mult_less_cancel1 1;
wenzelm@4297
   564
qed "Suc_mult_le_cancel1";
wenzelm@4297
   565
paulson@3234
   566
goal Arith.thy "!!k. 0<k ==> (m*k = n*k) = (m=n)";
paulson@3234
   567
by (cut_facts_tac [less_linear] 1);
paulson@3724
   568
by Safe_tac;
paulson@3457
   569
by (assume_tac 2);
paulson@3234
   570
by (ALLGOALS (dtac mult_less_mono1 THEN' assume_tac));
paulson@3234
   571
by (ALLGOALS Asm_full_simp_tac);
paulson@3234
   572
qed "mult_cancel2";
paulson@3234
   573
paulson@3234
   574
goal Arith.thy "!!k. 0<k ==> (k*m = k*n) = (m=n)";
paulson@3457
   575
by (dtac mult_cancel2 1);
wenzelm@4089
   576
by (asm_full_simp_tac (simpset() addsimps [mult_commute]) 1);
paulson@3234
   577
qed "mult_cancel1";
paulson@3234
   578
Addsimps [mult_cancel1, mult_cancel2];
paulson@3234
   579
wenzelm@4297
   580
goal Arith.thy "(Suc k * m = Suc k * n) = (m = n)";
wenzelm@4297
   581
br mult_cancel1 1;
wenzelm@4297
   582
by (Simp_tac 1);
wenzelm@4297
   583
qed "Suc_mult_cancel1";
wenzelm@4297
   584
paulson@3234
   585
paulson@1795
   586
(** Lemma for gcd **)
paulson@1795
   587
paulson@1795
   588
goal Arith.thy "!!m n. m = m*n ==> n=1 | m=0";
paulson@1795
   589
by (dtac sym 1);
paulson@1795
   590
by (rtac disjCI 1);
paulson@1795
   591
by (rtac nat_less_cases 1 THEN assume_tac 2);
wenzelm@4089
   592
by (fast_tac (claset() addSEs [less_SucE] addss simpset()) 1);
nipkow@4356
   593
by (best_tac (claset() addDs [mult_less_mono2] addss simpset()) 1);
paulson@1795
   594
qed "mult_eq_self_implies_10";
paulson@1795
   595
paulson@1795
   596
paulson@3234
   597
(*** Subtraction laws -- from Clemens Ballarin ***)
paulson@3234
   598
paulson@3234
   599
goal Arith.thy "!! a b c::nat. [| a < b; c <= a |] ==> a-c < b-c";
paulson@3234
   600
by (subgoal_tac "c+(a-c) < c+(b-c)" 1);
paulson@3381
   601
by (Full_simp_tac 1);
paulson@3234
   602
by (subgoal_tac "c <= b" 1);
wenzelm@4089
   603
by (blast_tac (claset() addIs [less_imp_le, le_trans]) 2);
paulson@3381
   604
by (Asm_simp_tac 1);
paulson@3234
   605
qed "diff_less_mono";
paulson@3234
   606
paulson@3234
   607
goal Arith.thy "!! a b c::nat. a+b < c ==> a < c-b";
paulson@3457
   608
by (dtac diff_less_mono 1);
paulson@3457
   609
by (rtac le_add2 1);
paulson@3234
   610
by (Asm_full_simp_tac 1);
paulson@3234
   611
qed "add_less_imp_less_diff";
paulson@3234
   612
paulson@3234
   613
goal Arith.thy "!! n. n <= m ==> Suc m - n = Suc (m - n)";
paulson@3457
   614
by (rtac Suc_diff_n 1);
wenzelm@4089
   615
by (asm_full_simp_tac (simpset() addsimps [le_eq_less_Suc]) 1);
paulson@3234
   616
qed "Suc_diff_le";
paulson@3234
   617
paulson@3234
   618
goal Arith.thy "!! n. Suc i <= n ==> Suc (n - Suc i) = n - i";
paulson@3234
   619
by (asm_full_simp_tac
wenzelm@4089
   620
    (simpset() addsimps [Suc_diff_n RS sym, le_eq_less_Suc]) 1);
paulson@3234
   621
qed "Suc_diff_Suc";
paulson@3234
   622
paulson@3234
   623
goal Arith.thy "!! i::nat. i <= n ==> n - (n - i) = i";
paulson@3903
   624
by (etac rev_mp 1);
paulson@3903
   625
by (res_inst_tac [("m","n"),("n","i")] diff_induct 1);
wenzelm@4089
   626
by (ALLGOALS (asm_simp_tac  (simpset() addsimps [Suc_diff_le])));
paulson@3234
   627
qed "diff_diff_cancel";
paulson@3381
   628
Addsimps [diff_diff_cancel];
paulson@3234
   629
paulson@3234
   630
goal Arith.thy "!!k::nat. k <= n ==> m <= n + m - k";
paulson@3457
   631
by (etac rev_mp 1);
paulson@3234
   632
by (res_inst_tac [("m", "k"), ("n", "n")] diff_induct 1);
paulson@3234
   633
by (Simp_tac 1);
wenzelm@4089
   634
by (simp_tac (simpset() addsimps [less_add_Suc2, less_imp_le]) 1);
paulson@3234
   635
by (Simp_tac 1);
paulson@3234
   636
qed "le_add_diff";
paulson@3234
   637
paulson@3234
   638
nipkow@3484
   639
(** (Anti)Monotonicity of subtraction -- by Stefan Merz **)
nipkow@3484
   640
nipkow@3484
   641
(* Monotonicity of subtraction in first argument *)
nipkow@3484
   642
goal Arith.thy "!!n::nat. m<=n --> (m-l) <= (n-l)";
nipkow@3484
   643
by (induct_tac "n" 1);
nipkow@3484
   644
by (Simp_tac 1);
wenzelm@4089
   645
by (simp_tac (simpset() addsimps [le_Suc_eq]) 1);
nipkow@3484
   646
by (rtac impI 1);
nipkow@3484
   647
by (etac impE 1);
nipkow@3484
   648
by (atac 1);
nipkow@3484
   649
by (etac le_trans 1);
nipkow@3484
   650
by (res_inst_tac [("m1","n")] (pred_Suc_diff RS subst) 1);
nipkow@4360
   651
by (simp_tac (simpset() addsimps [diff_Suc] addsplits [expand_nat_case]) 1);
nipkow@3484
   652
qed_spec_mp "diff_le_mono";
nipkow@3484
   653
nipkow@3484
   654
goal Arith.thy "!!n::nat. m<=n ==> (l-n) <= (l-m)";
nipkow@3484
   655
by (induct_tac "l" 1);
nipkow@3484
   656
by (Simp_tac 1);
nipkow@3484
   657
by (case_tac "n <= l" 1);
nipkow@3484
   658
by (subgoal_tac "m <= l" 1);
wenzelm@4089
   659
by (asm_simp_tac (simpset() addsimps [Suc_diff_le]) 1);
wenzelm@4089
   660
by (fast_tac (claset() addEs [le_trans]) 1);
nipkow@3484
   661
by (dtac not_leE 1);
wenzelm@4089
   662
by (asm_simp_tac (simpset() addsimps [if_Suc_diff_n]) 1);
nipkow@3484
   663
qed_spec_mp "diff_le_mono2";