src/HOL/Arith.ML
author paulson
Wed Sep 23 10:12:01 1998 +0200 (1998-09-23)
changeset 5537 c2bd39a2c0ee
parent 5497 497215d66441
child 5598 6b8dee1a6ebb
permissions -rw-r--r--
deleted needless parentheses
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
paulson@5429
    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
paulson@5429
    66
Goal "(k + m = k + n) = (m=(n::nat))";
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
paulson@5429
    72
Goal "(m + k = n + k) = (m=(n::nat))";
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
paulson@5429
    78
Goal "(k + m <= k + n) = (m<=(n::nat))";
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
paulson@5429
    84
Goal "(k + m < k + n) = (m<(n::nat))";
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
paulson@5429
   113
(* Could be generalized, eg to "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@5429
   121
Goal "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);
paulson@5497
   133
by (ALLGOALS (simp_tac (simpset() addsimps [le_eq_less_or_eq])));
wenzelm@4089
   134
by (blast_tac (claset() addSEs [less_SucE] 
paulson@5497
   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
paulson@5429
   153
Goal "(m<n) = (? k. n=Suc(m+k))";
paulson@5429
   154
by (blast_tac (claset() addSIs [less_add_Suc1, less_eq_Suc_add]) 1);
paulson@5429
   155
qed "less_iff_Suc_add";
paulson@5429
   156
paulson@5429
   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
paulson@5143
   170
Goal "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);
wenzelm@4089
   174
by (blast_tac (claset() addDs [Suc_lessD]) 1);
nipkow@1152
   175
qed "add_lessD1";
nipkow@1152
   176
paulson@5429
   177
Goal "~ (i+j < (i::nat))";
paulson@3457
   178
by (rtac notI 1);
paulson@3457
   179
by (etac (add_lessD1 RS less_irrefl) 1);
paulson@3234
   180
qed "not_add_less1";
paulson@3234
   181
paulson@5429
   182
Goal "~ (j+i < (i::nat))";
wenzelm@4089
   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
wenzelm@5069
   187
Goal "m+k<=n --> m<=(n::nat)";
paulson@3339
   188
by (induct_tac "k" 1);
paulson@5497
   189
by (ALLGOALS (asm_simp_tac (simpset() addsimps le_simps)));
nipkow@1485
   190
qed_spec_mp "add_leD1";
clasohm@923
   191
paulson@5429
   192
Goal "m+k<=n ==> k<=(n::nat)";
wenzelm@4089
   193
by (full_simp_tac (simpset() addsimps [add_commute]) 1);
paulson@2498
   194
by (etac add_leD1 1);
paulson@2498
   195
qed_spec_mp "add_leD2";
paulson@2498
   196
paulson@5429
   197
Goal "m+k<=n ==> m<=n & k<=(n::nat)";
wenzelm@4089
   198
by (blast_tac (claset() addDs [add_leD1, add_leD2]) 1);
paulson@2498
   199
bind_thm ("add_leE", result() RS conjE);
paulson@2498
   200
paulson@5429
   201
(*needs !!k for add_ac to work*)
paulson@5429
   202
Goal "!!k:: nat. [| k<l;  m+l = k+n |] ==> m<n";
paulson@5497
   203
by (auto_tac (claset(),
paulson@5497
   204
	      simpset() delsimps [add_Suc_right]
paulson@5537
   205
	                addsimps [less_iff_Suc_add,
paulson@5537
   206
				  add_Suc_right RS sym] @ add_ac));
clasohm@923
   207
qed "less_add_eq_less";
clasohm@923
   208
clasohm@923
   209
paulson@1713
   210
(*** Monotonicity of Addition ***)
clasohm@923
   211
clasohm@923
   212
(*strict, in 1st argument*)
paulson@5429
   213
Goal "i < j ==> i + k < j + (k::nat)";
paulson@3339
   214
by (induct_tac "k" 1);
clasohm@1264
   215
by (ALLGOALS Asm_simp_tac);
clasohm@923
   216
qed "add_less_mono1";
clasohm@923
   217
clasohm@923
   218
(*strict, in both arguments*)
paulson@5429
   219
Goal "[|i < j; k < l|] ==> i + k < j + (l::nat)";
clasohm@923
   220
by (rtac (add_less_mono1 RS less_trans) 1);
lcp@1198
   221
by (REPEAT (assume_tac 1));
paulson@3339
   222
by (induct_tac "j" 1);
clasohm@1264
   223
by (ALLGOALS Asm_simp_tac);
clasohm@923
   224
qed "add_less_mono";
clasohm@923
   225
clasohm@923
   226
(*A [clumsy] way of lifting < monotonicity to <= monotonicity *)
paulson@5316
   227
val [lt_mono,le] = Goal
clasohm@1465
   228
     "[| !!i j::nat. i<j ==> f(i) < f(j);       \
clasohm@1465
   229
\        i <= j                                 \
clasohm@923
   230
\     |] ==> f(i) <= (f(j)::nat)";
clasohm@923
   231
by (cut_facts_tac [le] 1);
wenzelm@4089
   232
by (asm_full_simp_tac (simpset() addsimps [le_eq_less_or_eq]) 1);
wenzelm@4089
   233
by (blast_tac (claset() addSIs [lt_mono]) 1);
clasohm@923
   234
qed "less_mono_imp_le_mono";
clasohm@923
   235
clasohm@923
   236
(*non-strict, in 1st argument*)
paulson@5429
   237
Goal "i<=j ==> i + k <= j + (k::nat)";
wenzelm@3842
   238
by (res_inst_tac [("f", "%j. j+k")] less_mono_imp_le_mono 1);
paulson@1552
   239
by (etac add_less_mono1 1);
clasohm@923
   240
by (assume_tac 1);
clasohm@923
   241
qed "add_le_mono1";
clasohm@923
   242
clasohm@923
   243
(*non-strict, in both arguments*)
paulson@5429
   244
Goal "[|i<=j;  k<=l |] ==> i + k <= j + (l::nat)";
clasohm@923
   245
by (etac (add_le_mono1 RS le_trans) 1);
wenzelm@4089
   246
by (simp_tac (simpset() addsimps [add_commute]) 1);
clasohm@923
   247
qed "add_le_mono";
paulson@1713
   248
paulson@3234
   249
paulson@3234
   250
(*** Multiplication ***)
paulson@3234
   251
paulson@3234
   252
(*right annihilation in product*)
paulson@4732
   253
qed_goal "mult_0_right" thy "m * 0 = 0"
paulson@3339
   254
 (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
paulson@3234
   255
paulson@3293
   256
(*right successor law for multiplication*)
paulson@4732
   257
qed_goal "mult_Suc_right" thy  "m * Suc(n) = m + (m * n)"
paulson@3339
   258
 (fn _ => [induct_tac "m" 1,
wenzelm@4089
   259
           ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))]);
paulson@3234
   260
paulson@3293
   261
Addsimps [mult_0_right, mult_Suc_right];
paulson@3234
   262
wenzelm@5069
   263
Goal "1 * n = n";
paulson@3234
   264
by (Asm_simp_tac 1);
paulson@3234
   265
qed "mult_1";
paulson@3234
   266
wenzelm@5069
   267
Goal "n * 1 = n";
paulson@3234
   268
by (Asm_simp_tac 1);
paulson@3234
   269
qed "mult_1_right";
paulson@3234
   270
paulson@3234
   271
(*Commutative law for multiplication*)
paulson@4732
   272
qed_goal "mult_commute" thy "m * n = n * (m::nat)"
paulson@3339
   273
 (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
paulson@3234
   274
paulson@3234
   275
(*addition distributes over multiplication*)
paulson@4732
   276
qed_goal "add_mult_distrib" thy "(m + n)*k = (m*k) + ((n*k)::nat)"
paulson@3339
   277
 (fn _ => [induct_tac "m" 1,
wenzelm@4089
   278
           ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))]);
paulson@3234
   279
paulson@4732
   280
qed_goal "add_mult_distrib2" thy "k*(m + n) = (k*m) + ((k*n)::nat)"
paulson@3339
   281
 (fn _ => [induct_tac "m" 1,
wenzelm@4089
   282
           ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))]);
paulson@3234
   283
paulson@3234
   284
(*Associative law for multiplication*)
paulson@4732
   285
qed_goal "mult_assoc" thy "(m * n) * k = m * ((n * k)::nat)"
paulson@3339
   286
  (fn _ => [induct_tac "m" 1, 
wenzelm@4089
   287
            ALLGOALS (asm_simp_tac (simpset() addsimps [add_mult_distrib]))]);
paulson@3234
   288
paulson@4732
   289
qed_goal "mult_left_commute" thy "x*(y*z) = y*((x*z)::nat)"
paulson@3234
   290
 (fn _ => [rtac trans 1, rtac mult_commute 1, rtac trans 1,
paulson@3234
   291
           rtac mult_assoc 1, rtac (mult_commute RS arg_cong) 1]);
paulson@3234
   292
paulson@3234
   293
val mult_ac = [mult_assoc,mult_commute,mult_left_commute];
paulson@3234
   294
wenzelm@5069
   295
Goal "(m*n = 0) = (m=0 | n=0)";
paulson@3339
   296
by (induct_tac "m" 1);
paulson@3339
   297
by (induct_tac "n" 2);
paulson@3293
   298
by (ALLGOALS Asm_simp_tac);
paulson@3293
   299
qed "mult_is_0";
paulson@3293
   300
Addsimps [mult_is_0];
paulson@3293
   301
paulson@5429
   302
Goal "m <= m*(m::nat)";
paulson@4158
   303
by (induct_tac "m" 1);
paulson@4158
   304
by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_assoc RS sym])));
paulson@4158
   305
by (etac (le_add2 RSN (2,le_trans)) 1);
paulson@4158
   306
qed "le_square";
paulson@4158
   307
paulson@3234
   308
paulson@3234
   309
(*** Difference ***)
paulson@3234
   310
paulson@3234
   311
paulson@4732
   312
qed_goal "diff_self_eq_0" thy "m - m = 0"
paulson@3339
   313
 (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
paulson@3234
   314
Addsimps [diff_self_eq_0];
paulson@3234
   315
paulson@3234
   316
(*Addition is the inverse of subtraction: if n<=m then n+(m-n) = m. *)
wenzelm@5069
   317
Goal "~ m<n --> n+(m-n) = (m::nat)";
paulson@3234
   318
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3352
   319
by (ALLGOALS Asm_simp_tac);
paulson@3381
   320
qed_spec_mp "add_diff_inverse";
paulson@3381
   321
paulson@5143
   322
Goal "n<=m ==> n+(m-n) = (m::nat)";
wenzelm@4089
   323
by (asm_simp_tac (simpset() addsimps [add_diff_inverse, not_less_iff_le]) 1);
paulson@3381
   324
qed "le_add_diff_inverse";
paulson@3234
   325
paulson@5143
   326
Goal "n<=m ==> (m-n)+n = (m::nat)";
wenzelm@4089
   327
by (asm_simp_tac (simpset() addsimps [le_add_diff_inverse, add_commute]) 1);
paulson@3381
   328
qed "le_add_diff_inverse2";
paulson@3381
   329
paulson@3381
   330
Addsimps  [le_add_diff_inverse, le_add_diff_inverse2];
paulson@3234
   331
paulson@3234
   332
paulson@3234
   333
(*** More results about difference ***)
paulson@3234
   334
paulson@5414
   335
Goal "n <= m ==> Suc(m)-n = Suc(m-n)";
paulson@5316
   336
by (etac rev_mp 1);
paulson@3352
   337
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3352
   338
by (ALLGOALS Asm_simp_tac);
paulson@5414
   339
qed "Suc_diff_le";
paulson@3352
   340
paulson@5429
   341
Goal "n<=(l::nat) --> Suc l - n + m = Suc (l - n + m)";
paulson@5429
   342
by (res_inst_tac [("m","n"),("n","l")] diff_induct 1);
paulson@5429
   343
by (ALLGOALS Asm_simp_tac);
paulson@5429
   344
qed_spec_mp "Suc_diff_add_le";
paulson@5429
   345
wenzelm@5069
   346
Goal "m - n < Suc(m)";
paulson@3234
   347
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3234
   348
by (etac less_SucE 3);
wenzelm@4089
   349
by (ALLGOALS (asm_simp_tac (simpset() addsimps [less_Suc_eq])));
paulson@3234
   350
qed "diff_less_Suc";
paulson@3234
   351
paulson@5429
   352
Goal "m - n <= (m::nat)";
paulson@3234
   353
by (res_inst_tac [("m","m"), ("n","n")] diff_induct 1);
paulson@3234
   354
by (ALLGOALS Asm_simp_tac);
paulson@3234
   355
qed "diff_le_self";
paulson@3903
   356
Addsimps [diff_le_self];
paulson@3234
   357
paulson@4732
   358
(* j<k ==> j-n < k *)
paulson@4732
   359
bind_thm ("less_imp_diff_less", diff_le_self RS le_less_trans);
paulson@4732
   360
wenzelm@5069
   361
Goal "!!i::nat. i-j-k = i - (j+k)";
paulson@3352
   362
by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
paulson@3352
   363
by (ALLGOALS Asm_simp_tac);
paulson@3352
   364
qed "diff_diff_left";
paulson@3352
   365
wenzelm@5069
   366
Goal "(Suc m - n) - Suc k = m - n - k";
wenzelm@4423
   367
by (simp_tac (simpset() addsimps [diff_diff_left]) 1);
paulson@4736
   368
qed "Suc_diff_diff";
paulson@4736
   369
Addsimps [Suc_diff_diff];
nipkow@4360
   370
paulson@5143
   371
Goal "0<n ==> n - Suc i < n";
berghofe@5183
   372
by (exhaust_tac "n" 1);
paulson@4732
   373
by Safe_tac;
paulson@5497
   374
by (asm_simp_tac (simpset() addsimps le_simps) 1);
paulson@4732
   375
qed "diff_Suc_less";
paulson@4732
   376
Addsimps [diff_Suc_less];
paulson@4732
   377
paulson@5329
   378
Goal "i<n ==> n - Suc i < n - i";
paulson@5329
   379
by (exhaust_tac "n" 1);
paulson@5497
   380
by (auto_tac (claset(),
paulson@5537
   381
	      simpset() addsimps [Suc_diff_le]@le_simps));
paulson@5329
   382
qed "diff_Suc_less_diff";
paulson@5329
   383
paulson@5333
   384
Goal "m - n <= Suc m - n";
paulson@4732
   385
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@4732
   386
by (ALLGOALS Asm_simp_tac);
paulson@4732
   387
qed "diff_le_Suc_diff";
paulson@4732
   388
wenzelm@3396
   389
(*This and the next few suggested by Florian Kammueller*)
wenzelm@5069
   390
Goal "!!i::nat. i-j-k = i-k-j";
wenzelm@4089
   391
by (simp_tac (simpset() addsimps [diff_diff_left, add_commute]) 1);
paulson@3352
   392
qed "diff_commute";
paulson@3352
   393
paulson@5429
   394
Goal "k<=j --> j<=i --> i - (j - k) = i - j + (k::nat)";
paulson@3352
   395
by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
paulson@3352
   396
by (ALLGOALS Asm_simp_tac);
paulson@5414
   397
by (asm_simp_tac (simpset() addsimps [Suc_diff_le, le_Suc_eq]) 1);
paulson@3352
   398
qed_spec_mp "diff_diff_right";
paulson@3352
   399
paulson@5429
   400
Goal "k <= (j::nat) --> (i + j) - k = i + (j - k)";
paulson@3352
   401
by (res_inst_tac [("m","j"),("n","k")] diff_induct 1);
paulson@3352
   402
by (ALLGOALS Asm_simp_tac);
paulson@3352
   403
qed_spec_mp "diff_add_assoc";
paulson@3352
   404
paulson@5429
   405
Goal "k <= (j::nat) --> (j + i) - k = i + (j - k)";
paulson@4732
   406
by (asm_simp_tac (simpset() addsimps [add_commute, diff_add_assoc]) 1);
paulson@4732
   407
qed_spec_mp "diff_add_assoc2";
paulson@4732
   408
paulson@5429
   409
Goal "(n+m) - n = (m::nat)";
paulson@3339
   410
by (induct_tac "n" 1);
paulson@3234
   411
by (ALLGOALS Asm_simp_tac);
paulson@3234
   412
qed "diff_add_inverse";
paulson@3234
   413
Addsimps [diff_add_inverse];
paulson@3234
   414
paulson@5429
   415
Goal "(m+n) - n = (m::nat)";
wenzelm@4089
   416
by (simp_tac (simpset() addsimps [diff_add_assoc]) 1);
paulson@3234
   417
qed "diff_add_inverse2";
paulson@3234
   418
Addsimps [diff_add_inverse2];
paulson@3234
   419
paulson@5429
   420
Goal "i <= (j::nat) ==> (j-i=k) = (j=k+i)";
paulson@3724
   421
by Safe_tac;
paulson@3381
   422
by (ALLGOALS Asm_simp_tac);
paulson@3366
   423
qed "le_imp_diff_is_add";
paulson@3366
   424
paulson@5356
   425
Goal "(m-n = 0) = (m <= n)";
paulson@3234
   426
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@5497
   427
by (ALLGOALS Asm_simp_tac);
paulson@5356
   428
qed "diff_is_0_eq";
paulson@5356
   429
Addsimps [diff_is_0_eq RS iffD2];
paulson@3234
   430
paulson@5316
   431
Goal "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@5333
   436
Goal "(0<n-m) = (m<n)";
paulson@3234
   437
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3352
   438
by (ALLGOALS Asm_simp_tac);
paulson@5333
   439
qed "zero_less_diff";
paulson@5333
   440
Addsimps [zero_less_diff];
paulson@3234
   441
paulson@5333
   442
Goal "i < j  ==> ? k. 0<k & i+k = j";
paulson@5078
   443
by (res_inst_tac [("x","j - i")] exI 1);
paulson@5333
   444
by (asm_simp_tac (simpset() addsimps [add_diff_inverse, less_not_sym]) 1);
paulson@5078
   445
qed "less_imp_add_positive";
paulson@5078
   446
wenzelm@5069
   447
Goal "Suc(m)-n = (if m<n then 0 else Suc(m-n))";
paulson@5414
   448
by (simp_tac (simpset() addsimps [leI, Suc_le_eq, Suc_diff_le]) 1);
paulson@5414
   449
qed "if_Suc_diff_le";
paulson@3234
   450
wenzelm@5069
   451
Goal "Suc(m)-n <= Suc(m-n)";
paulson@5414
   452
by (simp_tac (simpset() addsimps [if_Suc_diff_le]) 1);
paulson@4672
   453
qed "diff_Suc_le_Suc_diff";
paulson@4672
   454
wenzelm@5069
   455
Goal "P(k) --> (!n. P(Suc(n))--> P(n)) --> P(k-i)";
paulson@3234
   456
by (res_inst_tac [("m","k"),("n","i")] diff_induct 1);
paulson@3718
   457
by (ALLGOALS (Clarify_tac THEN' Simp_tac THEN' TRY o Blast_tac));
paulson@3234
   458
qed "zero_induct_lemma";
paulson@3234
   459
paulson@5316
   460
val prems = Goal "[| P(k);  !!n. P(Suc(n)) ==> P(n) |] ==> P(0)";
paulson@3234
   461
by (rtac (diff_self_eq_0 RS subst) 1);
paulson@3234
   462
by (rtac (zero_induct_lemma RS mp RS mp) 1);
paulson@3234
   463
by (REPEAT (ares_tac ([impI,allI]@prems) 1));
paulson@3234
   464
qed "zero_induct";
paulson@3234
   465
paulson@5429
   466
Goal "(k+m) - (k+n) = m - (n::nat)";
paulson@3339
   467
by (induct_tac "k" 1);
paulson@3234
   468
by (ALLGOALS Asm_simp_tac);
paulson@3234
   469
qed "diff_cancel";
paulson@3234
   470
Addsimps [diff_cancel];
paulson@3234
   471
paulson@5429
   472
Goal "(m+k) - (n+k) = m - (n::nat)";
paulson@3234
   473
val add_commute_k = read_instantiate [("n","k")] add_commute;
paulson@5537
   474
by (asm_simp_tac (simpset() addsimps [add_commute_k]) 1);
paulson@3234
   475
qed "diff_cancel2";
paulson@3234
   476
Addsimps [diff_cancel2];
paulson@3234
   477
paulson@5414
   478
(*From Clemens Ballarin, proof by lcp*)
paulson@5429
   479
Goal "[| k<=n; n<=m |] ==> (m-k) - (n-k) = m-(n::nat)";
paulson@5414
   480
by (REPEAT (etac rev_mp 1));
paulson@5414
   481
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@5414
   482
by (ALLGOALS Asm_simp_tac);
paulson@5414
   483
(*a confluence problem*)
paulson@5414
   484
by (asm_simp_tac (simpset() addsimps [Suc_diff_le, le_Suc_eq]) 1);
paulson@3234
   485
qed "diff_right_cancel";
paulson@3234
   486
paulson@5429
   487
Goal "n - (n+m) = 0";
paulson@3339
   488
by (induct_tac "n" 1);
paulson@3234
   489
by (ALLGOALS Asm_simp_tac);
paulson@3234
   490
qed "diff_add_0";
paulson@3234
   491
Addsimps [diff_add_0];
paulson@3234
   492
paulson@5409
   493
paulson@3234
   494
(** Difference distributes over multiplication **)
paulson@3234
   495
wenzelm@5069
   496
Goal "!!m::nat. (m - n) * k = (m * k) - (n * k)";
paulson@3234
   497
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@3234
   498
by (ALLGOALS Asm_simp_tac);
paulson@3234
   499
qed "diff_mult_distrib" ;
paulson@3234
   500
wenzelm@5069
   501
Goal "!!m::nat. k * (m - n) = (k * m) - (k * n)";
paulson@3234
   502
val mult_commute_k = read_instantiate [("m","k")] mult_commute;
wenzelm@4089
   503
by (simp_tac (simpset() addsimps [diff_mult_distrib, mult_commute_k]) 1);
paulson@3234
   504
qed "diff_mult_distrib2" ;
paulson@3234
   505
(*NOT added as rewrites, since sometimes they are used from right-to-left*)
paulson@3234
   506
paulson@3234
   507
paulson@1713
   508
(*** Monotonicity of Multiplication ***)
paulson@1713
   509
paulson@5429
   510
Goal "i <= (j::nat) ==> i*k<=j*k";
paulson@3339
   511
by (induct_tac "k" 1);
wenzelm@4089
   512
by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_le_mono])));
paulson@1713
   513
qed "mult_le_mono1";
paulson@1713
   514
paulson@1713
   515
(*<=monotonicity, BOTH arguments*)
paulson@5429
   516
Goal "[| i <= (j::nat); k <= l |] ==> i*k <= j*l";
paulson@2007
   517
by (etac (mult_le_mono1 RS le_trans) 1);
paulson@1713
   518
by (rtac le_trans 1);
paulson@2007
   519
by (stac mult_commute 2);
paulson@2007
   520
by (etac mult_le_mono1 2);
wenzelm@4089
   521
by (simp_tac (simpset() addsimps [mult_commute]) 1);
paulson@1713
   522
qed "mult_le_mono";
paulson@1713
   523
paulson@1713
   524
(*strict, in 1st argument; proof is by induction on k>0*)
paulson@5429
   525
Goal "[| i<j; 0<k |] ==> k*i < k*j";
paulson@5078
   526
by (eres_inst_tac [("m1","0")] (less_eq_Suc_add RS exE) 1);
paulson@1713
   527
by (Asm_simp_tac 1);
paulson@3339
   528
by (induct_tac "x" 1);
wenzelm@4089
   529
by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_less_mono])));
paulson@1713
   530
qed "mult_less_mono2";
paulson@1713
   531
paulson@5429
   532
Goal "[| i<j; 0<k |] ==> i*k < j*k";
paulson@3457
   533
by (dtac mult_less_mono2 1);
wenzelm@4089
   534
by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [mult_commute])));
paulson@3234
   535
qed "mult_less_mono1";
paulson@3234
   536
wenzelm@5069
   537
Goal "(0 < m*n) = (0<m & 0<n)";
paulson@3339
   538
by (induct_tac "m" 1);
paulson@3339
   539
by (induct_tac "n" 2);
paulson@1713
   540
by (ALLGOALS Asm_simp_tac);
paulson@1713
   541
qed "zero_less_mult_iff";
nipkow@4356
   542
Addsimps [zero_less_mult_iff];
paulson@1713
   543
wenzelm@5069
   544
Goal "(m*n = 1) = (m=1 & n=1)";
paulson@3339
   545
by (induct_tac "m" 1);
paulson@1795
   546
by (Simp_tac 1);
paulson@3339
   547
by (induct_tac "n" 1);
paulson@1795
   548
by (Simp_tac 1);
wenzelm@4089
   549
by (fast_tac (claset() addss simpset()) 1);
paulson@1795
   550
qed "mult_eq_1_iff";
nipkow@4356
   551
Addsimps [mult_eq_1_iff];
paulson@1795
   552
paulson@5143
   553
Goal "0<k ==> (m*k < n*k) = (m<n)";
wenzelm@4089
   554
by (safe_tac (claset() addSIs [mult_less_mono1]));
paulson@3234
   555
by (cut_facts_tac [less_linear] 1);
paulson@4389
   556
by (blast_tac (claset() addIs [mult_less_mono1] addEs [less_asym]) 1);
paulson@3234
   557
qed "mult_less_cancel2";
paulson@3234
   558
paulson@5143
   559
Goal "0<k ==> (k*m < k*n) = (m<n)";
paulson@3457
   560
by (dtac mult_less_cancel2 1);
wenzelm@4089
   561
by (asm_full_simp_tac (simpset() addsimps [mult_commute]) 1);
paulson@3234
   562
qed "mult_less_cancel1";
paulson@3234
   563
Addsimps [mult_less_cancel1, mult_less_cancel2];
paulson@3234
   564
wenzelm@5069
   565
Goal "(Suc k * m < Suc k * n) = (m < n)";
wenzelm@4423
   566
by (rtac mult_less_cancel1 1);
wenzelm@4297
   567
by (Simp_tac 1);
wenzelm@4297
   568
qed "Suc_mult_less_cancel1";
wenzelm@4297
   569
wenzelm@5069
   570
Goalw [le_def] "(Suc k * m <= Suc k * n) = (m <= n)";
wenzelm@4297
   571
by (simp_tac (simpset_of HOL.thy) 1);
wenzelm@4423
   572
by (rtac Suc_mult_less_cancel1 1);
wenzelm@4297
   573
qed "Suc_mult_le_cancel1";
wenzelm@4297
   574
paulson@5143
   575
Goal "0<k ==> (m*k = n*k) = (m=n)";
paulson@3234
   576
by (cut_facts_tac [less_linear] 1);
paulson@3724
   577
by Safe_tac;
paulson@3457
   578
by (assume_tac 2);
paulson@3234
   579
by (ALLGOALS (dtac mult_less_mono1 THEN' assume_tac));
paulson@3234
   580
by (ALLGOALS Asm_full_simp_tac);
paulson@3234
   581
qed "mult_cancel2";
paulson@3234
   582
paulson@5143
   583
Goal "0<k ==> (k*m = k*n) = (m=n)";
paulson@3457
   584
by (dtac mult_cancel2 1);
wenzelm@4089
   585
by (asm_full_simp_tac (simpset() addsimps [mult_commute]) 1);
paulson@3234
   586
qed "mult_cancel1";
paulson@3234
   587
Addsimps [mult_cancel1, mult_cancel2];
paulson@3234
   588
wenzelm@5069
   589
Goal "(Suc k * m = Suc k * n) = (m = n)";
wenzelm@4423
   590
by (rtac mult_cancel1 1);
wenzelm@4297
   591
by (Simp_tac 1);
wenzelm@4297
   592
qed "Suc_mult_cancel1";
wenzelm@4297
   593
paulson@3234
   594
paulson@1795
   595
(** Lemma for gcd **)
paulson@1795
   596
paulson@5143
   597
Goal "m = m*n ==> n=1 | m=0";
paulson@1795
   598
by (dtac sym 1);
paulson@1795
   599
by (rtac disjCI 1);
paulson@1795
   600
by (rtac nat_less_cases 1 THEN assume_tac 2);
wenzelm@4089
   601
by (fast_tac (claset() addSEs [less_SucE] addss simpset()) 1);
nipkow@4356
   602
by (best_tac (claset() addDs [mult_less_mono2] addss simpset()) 1);
paulson@1795
   603
qed "mult_eq_self_implies_10";
paulson@1795
   604
paulson@1795
   605
paulson@4736
   606
(*** Subtraction laws -- mostly from Clemens Ballarin ***)
paulson@3234
   607
paulson@5429
   608
Goal "[| a < (b::nat); c <= a |] ==> a-c < b-c";
paulson@3234
   609
by (subgoal_tac "c+(a-c) < c+(b-c)" 1);
paulson@3381
   610
by (Full_simp_tac 1);
paulson@3234
   611
by (subgoal_tac "c <= b" 1);
wenzelm@4089
   612
by (blast_tac (claset() addIs [less_imp_le, le_trans]) 2);
paulson@3381
   613
by (Asm_simp_tac 1);
paulson@3234
   614
qed "diff_less_mono";
paulson@3234
   615
paulson@5429
   616
Goal "a+b < (c::nat) ==> a < c-b";
paulson@3457
   617
by (dtac diff_less_mono 1);
paulson@3457
   618
by (rtac le_add2 1);
paulson@3234
   619
by (Asm_full_simp_tac 1);
paulson@3234
   620
qed "add_less_imp_less_diff";
paulson@3234
   621
nipkow@5427
   622
Goal "(i < j-k) = (i+k < (j::nat))";
paulson@5497
   623
by (rtac iffI 1);
paulson@5497
   624
 by (case_tac "k <= j" 1);
paulson@5497
   625
  by (dtac le_add_diff_inverse2 1);
paulson@5497
   626
  by (dres_inst_tac [("k","k")] add_less_mono1 1);
paulson@5497
   627
  by (Asm_full_simp_tac 1);
paulson@5497
   628
 by (rotate_tac 1 1);
paulson@5497
   629
 by (asm_full_simp_tac (simpset() addSolver cut_trans_tac) 1);
paulson@5497
   630
by (etac add_less_imp_less_diff 1);
nipkow@5427
   631
qed "less_diff_conv";
nipkow@5427
   632
paulson@5497
   633
Goal "(j-k <= (i::nat)) = (j <= i+k)";
paulson@5497
   634
by (simp_tac (simpset() addsimps [less_diff_conv, le_def]) 1);
paulson@5485
   635
qed "le_diff_conv";
paulson@5485
   636
paulson@5497
   637
Goal "k <= j ==> (i <= j-k) = (i+k <= (j::nat))";
paulson@5497
   638
by (asm_full_simp_tac
paulson@5497
   639
    (simpset() delsimps [less_Suc_eq_le]
paulson@5497
   640
               addsimps [less_Suc_eq_le RS sym, less_diff_conv,
paulson@5497
   641
			 Suc_diff_le RS sym]) 1);
paulson@5497
   642
qed "le_diff_conv2";
paulson@5497
   643
paulson@5143
   644
Goal "Suc i <= n ==> Suc (n - Suc i) = n - i";
paulson@5497
   645
by (asm_full_simp_tac (simpset() addsimps [Suc_diff_le RS sym]) 1);
paulson@3234
   646
qed "Suc_diff_Suc";
paulson@3234
   647
paulson@5429
   648
Goal "i <= (n::nat) ==> n - (n - i) = i";
paulson@3903
   649
by (etac rev_mp 1);
paulson@3903
   650
by (res_inst_tac [("m","n"),("n","i")] diff_induct 1);
wenzelm@4089
   651
by (ALLGOALS (asm_simp_tac  (simpset() addsimps [Suc_diff_le])));
paulson@3234
   652
qed "diff_diff_cancel";
paulson@3381
   653
Addsimps [diff_diff_cancel];
paulson@3234
   654
paulson@5429
   655
Goal "k <= (n::nat) ==> m <= n + m - k";
paulson@3457
   656
by (etac rev_mp 1);
paulson@3234
   657
by (res_inst_tac [("m", "k"), ("n", "n")] diff_induct 1);
paulson@3234
   658
by (Simp_tac 1);
paulson@5497
   659
by (simp_tac (simpset() addsimps [le_add2, less_imp_le]) 1);
paulson@3234
   660
by (Simp_tac 1);
paulson@3234
   661
qed "le_add_diff";
paulson@3234
   662
paulson@5429
   663
Goal "0<k ==> j<i --> j+k-i < k";
paulson@4736
   664
by (res_inst_tac [("m","j"),("n","i")] diff_induct 1);
paulson@4736
   665
by (ALLGOALS Asm_simp_tac);
paulson@4736
   666
qed_spec_mp "add_diff_less";
paulson@4736
   667
paulson@3234
   668
paulson@5356
   669
Goal "m-1 < n ==> m <= n";
paulson@5356
   670
by (exhaust_tac "m" 1);
paulson@5356
   671
by (auto_tac (claset(), simpset() addsimps [Suc_le_eq]));
paulson@5356
   672
qed "pred_less_imp_le";
paulson@5356
   673
paulson@5356
   674
Goal "j<=i ==> i - j < Suc i - j";
paulson@5356
   675
by (REPEAT (etac rev_mp 1));
paulson@5356
   676
by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
paulson@5356
   677
by Auto_tac;
paulson@5356
   678
qed "diff_less_Suc_diff";
paulson@5356
   679
paulson@5356
   680
Goal "i - j <= Suc i - j";
paulson@5356
   681
by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
paulson@5356
   682
by Auto_tac;
paulson@5356
   683
qed "diff_le_Suc_diff";
paulson@5356
   684
AddIffs [diff_le_Suc_diff];
paulson@5356
   685
paulson@5356
   686
Goal "n - Suc i <= n - i";
paulson@5356
   687
by (case_tac "i<n" 1);
paulson@5497
   688
by (dtac diff_Suc_less_diff 1);
paulson@5497
   689
by (auto_tac (claset(), simpset() addsimps [leI]));
paulson@5356
   690
qed "diff_Suc_le_diff";
paulson@5356
   691
AddIffs [diff_Suc_le_diff];
paulson@5356
   692
paulson@5409
   693
Goal "0 < n ==> (m <= n-1) = (m<n)";
paulson@5409
   694
by (exhaust_tac "n" 1);
paulson@5497
   695
by (auto_tac (claset(), simpset() addsimps le_simps));
paulson@5409
   696
qed "le_pred_eq";
paulson@5409
   697
paulson@5409
   698
Goal "0 < n ==> (m-1 < n) = (m<=n)";
paulson@5409
   699
by (exhaust_tac "m" 1);
paulson@5409
   700
by (auto_tac (claset(), simpset() addsimps [Suc_le_eq]));
paulson@5409
   701
qed "less_pred_eq";
paulson@5409
   702
paulson@5414
   703
(*In ordinary notation: if 0<n and n<=m then m-n < m *)
paulson@5414
   704
Goal "[| 0<n; ~ m<n |] ==> m - n < m";
paulson@5414
   705
by (subgoal_tac "0<n --> ~ m<n --> m - n < m" 1);
paulson@5414
   706
by (Blast_tac 1);
paulson@5414
   707
by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
paulson@5414
   708
by (ALLGOALS(asm_simp_tac(simpset() addsimps [diff_less_Suc])));
paulson@5414
   709
qed "diff_less";
paulson@5414
   710
paulson@5414
   711
Goal "[| 0<n; n<=m |] ==> m - n < m";
paulson@5414
   712
by (asm_simp_tac (simpset() addsimps [diff_less, not_less_iff_le]) 1);
paulson@5414
   713
qed "le_diff_less";
paulson@5414
   714
paulson@5356
   715
paulson@4732
   716
nipkow@3484
   717
(** (Anti)Monotonicity of subtraction -- by Stefan Merz **)
nipkow@3484
   718
nipkow@3484
   719
(* Monotonicity of subtraction in first argument *)
paulson@5429
   720
Goal "m <= (n::nat) --> (m-l) <= (n-l)";
nipkow@3484
   721
by (induct_tac "n" 1);
nipkow@3484
   722
by (Simp_tac 1);
wenzelm@4089
   723
by (simp_tac (simpset() addsimps [le_Suc_eq]) 1);
paulson@4732
   724
by (blast_tac (claset() addIs [diff_le_Suc_diff, le_trans]) 1);
nipkow@3484
   725
qed_spec_mp "diff_le_mono";
nipkow@3484
   726
paulson@5429
   727
Goal "m <= (n::nat) ==> (l-n) <= (l-m)";
nipkow@3484
   728
by (induct_tac "l" 1);
nipkow@3484
   729
by (Simp_tac 1);
berghofe@5183
   730
by (case_tac "n <= na" 1);
berghofe@5183
   731
by (subgoal_tac "m <= na" 1);
wenzelm@4089
   732
by (asm_simp_tac (simpset() addsimps [Suc_diff_le]) 1);
wenzelm@4089
   733
by (fast_tac (claset() addEs [le_trans]) 1);
nipkow@3484
   734
by (dtac not_leE 1);
paulson@5414
   735
by (asm_simp_tac (simpset() addsimps [if_Suc_diff_le]) 1);
nipkow@3484
   736
qed_spec_mp "diff_le_mono2";