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