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