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