src/HOL/Arith.ML
author nipkow
Wed Jul 02 11:59:10 1997 +0200 (1997-07-02)
changeset 3484 1e93eb09ebb9
parent 3457 a8ab7c64817c
child 3718 d78cf498a88c
permissions -rw-r--r--
Added the following lemmas tp Divides and a few others to Arith and NatDef:

div_le_mono, div_le_mono2, div_le_dividend, div_less_dividend

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