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