src/HOL/Arith.ML
author paulson
Fri May 30 15:15:57 1997 +0200 (1997-05-30)
changeset 3366 2402c6ab1561
parent 3352 04502e5431fb
child 3381 2bac33ec2b0d
permissions -rw-r--r--
Moving div and mod from Arith to Divides
Moving dvd from ex/Primes to Divides
     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 br notI 1;
   179 be (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 val [prem] = goal Arith.thy "~ m<n ==> n+(m-n) = (m::nat)";
   329 by (rtac (prem RS rev_mp) 1);
   330 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   331 by (ALLGOALS Asm_simp_tac);
   332 qed "add_diff_inverse";
   333 
   334 Delsimps  [diff_Suc];
   335 
   336 
   337 (*** More results about difference ***)
   338 
   339 val [prem] = goal Arith.thy "n < Suc(m) ==> Suc(m)-n = Suc(m-n)";
   340 by (rtac (prem RS rev_mp) 1);
   341 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   342 by (ALLGOALS Asm_simp_tac);
   343 qed "Suc_diff_n";
   344 
   345 goal Arith.thy "m - n < Suc(m)";
   346 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   347 by (etac less_SucE 3);
   348 by (ALLGOALS (asm_simp_tac (!simpset addsimps [less_Suc_eq])));
   349 qed "diff_less_Suc";
   350 
   351 goal Arith.thy "!!m::nat. m - n <= m";
   352 by (res_inst_tac [("m","m"), ("n","n")] diff_induct 1);
   353 by (ALLGOALS Asm_simp_tac);
   354 qed "diff_le_self";
   355 
   356 goal Arith.thy "!!i::nat. i-j-k = i - (j+k)";
   357 by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
   358 by (ALLGOALS Asm_simp_tac);
   359 qed "diff_diff_left";
   360 
   361 (*This and the next few suggested by Florian Kammüller*)
   362 goal Arith.thy "!!i::nat. i-j-k = i-k-j";
   363 by (simp_tac (!simpset addsimps [diff_diff_left, add_commute]) 1);
   364 qed "diff_commute";
   365 
   366 goal Arith.thy "!!i j k:: nat. k<=j --> j<=i --> i - (j - k) = i - j + k";
   367 by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
   368 by (ALLGOALS Asm_simp_tac);
   369 by (asm_simp_tac
   370     (!simpset addsimps [Suc_diff_n, le_imp_less_Suc, le_Suc_eq]) 1);
   371 by (simp_tac
   372     (!simpset addsimps [add_diff_inverse, not_less_iff_le, add_commute]) 1);
   373 qed_spec_mp "diff_diff_right";
   374 
   375 goal Arith.thy "!!i j k:: nat. k<=j --> (i + j) - k = i + (j - k)";
   376 by (res_inst_tac [("m","j"),("n","k")] diff_induct 1);
   377 by (ALLGOALS Asm_simp_tac);
   378 qed_spec_mp "diff_add_assoc";
   379 
   380 goal Arith.thy "!!n::nat. (n+m) - n = m";
   381 by (induct_tac "n" 1);
   382 by (ALLGOALS Asm_simp_tac);
   383 qed "diff_add_inverse";
   384 Addsimps [diff_add_inverse];
   385 
   386 goal Arith.thy "!!n::nat.(m+n) - n = m";
   387 by (simp_tac (!simpset addsimps [diff_add_assoc]) 1);
   388 qed "diff_add_inverse2";
   389 Addsimps [diff_add_inverse2];
   390 
   391 goal Arith.thy "!!i j k::nat. i<=j ==> (j-i=k) = (j=k+i)";
   392 by (Step_tac 1);
   393 by (ALLGOALS 
   394     (asm_simp_tac
   395      (!simpset addsimps [add_diff_inverse, not_less_iff_le, add_commute])));
   396 qed "le_imp_diff_is_add";
   397 
   398 val [prem] = goal Arith.thy "m < Suc(n) ==> m-n = 0";
   399 by (rtac (prem RS rev_mp) 1);
   400 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   401 by (asm_simp_tac (!simpset addsimps [less_Suc_eq]) 1);
   402 by (ALLGOALS Asm_simp_tac);
   403 qed "less_imp_diff_is_0";
   404 
   405 val prems = goal Arith.thy "m-n = 0  -->  n-m = 0  -->  m=n";
   406 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   407 by (REPEAT(Simp_tac 1 THEN TRY(atac 1)));
   408 qed_spec_mp "diffs0_imp_equal";
   409 
   410 val [prem] = goal Arith.thy "m<n ==> 0<n-m";
   411 by (rtac (prem RS rev_mp) 1);
   412 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   413 by (ALLGOALS Asm_simp_tac);
   414 qed "less_imp_diff_positive";
   415 
   416 goal Arith.thy "Suc(m)-n = (if m<n then 0 else Suc(m-n))";
   417 by (simp_tac (!simpset addsimps [less_imp_diff_is_0, not_less_eq, Suc_diff_n]
   418                     setloop (split_tac [expand_if])) 1);
   419 qed "if_Suc_diff_n";
   420 
   421 goal Arith.thy "P(k) --> (!n. P(Suc(n))--> P(n)) --> P(k-i)";
   422 by (res_inst_tac [("m","k"),("n","i")] diff_induct 1);
   423 by (ALLGOALS (strip_tac THEN' Simp_tac THEN' TRY o Blast_tac));
   424 qed "zero_induct_lemma";
   425 
   426 val prems = goal Arith.thy "[| P(k);  !!n. P(Suc(n)) ==> P(n) |] ==> P(0)";
   427 by (rtac (diff_self_eq_0 RS subst) 1);
   428 by (rtac (zero_induct_lemma RS mp RS mp) 1);
   429 by (REPEAT (ares_tac ([impI,allI]@prems) 1));
   430 qed "zero_induct";
   431 
   432 goal Arith.thy "!!k::nat. (k+m) - (k+n) = m - n";
   433 by (induct_tac "k" 1);
   434 by (ALLGOALS Asm_simp_tac);
   435 qed "diff_cancel";
   436 Addsimps [diff_cancel];
   437 
   438 goal Arith.thy "!!m::nat. (m+k) - (n+k) = m - n";
   439 val add_commute_k = read_instantiate [("n","k")] add_commute;
   440 by (asm_simp_tac (!simpset addsimps ([add_commute_k])) 1);
   441 qed "diff_cancel2";
   442 Addsimps [diff_cancel2];
   443 
   444 (*From Clemens Ballarin*)
   445 goal Arith.thy "!!n::nat. [| k<=n; n<=m |] ==> (m-k) - (n-k) = m-n";
   446 by (subgoal_tac "k<=n --> n<=m --> (m-k) - (n-k) = m-n" 1);
   447 by (Asm_full_simp_tac 1);
   448 by (induct_tac "k" 1);
   449 by (Simp_tac 1);
   450 (* Induction step *)
   451 by (subgoal_tac "Suc na <= m --> n <= m --> Suc na <= n --> \
   452 \                Suc (m - Suc na) - Suc (n - Suc na) = m-n" 1);
   453 by (Asm_full_simp_tac 1);
   454 by (blast_tac (!claset addIs [le_trans]) 1);
   455 by (auto_tac (!claset addIs [Suc_leD], !simpset delsimps [diff_Suc_Suc]));
   456 by (asm_full_simp_tac (!simpset delsimps [Suc_less_eq] 
   457 		       addsimps [Suc_diff_n RS sym, le_eq_less_Suc]) 1);
   458 qed "diff_right_cancel";
   459 
   460 goal Arith.thy "!!n::nat. n - (n+m) = 0";
   461 by (induct_tac "n" 1);
   462 by (ALLGOALS Asm_simp_tac);
   463 qed "diff_add_0";
   464 Addsimps [diff_add_0];
   465 
   466 (** Difference distributes over multiplication **)
   467 
   468 goal Arith.thy "!!m::nat. (m - n) * k = (m * k) - (n * k)";
   469 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   470 by (ALLGOALS Asm_simp_tac);
   471 qed "diff_mult_distrib" ;
   472 
   473 goal Arith.thy "!!m::nat. k * (m - n) = (k * m) - (k * n)";
   474 val mult_commute_k = read_instantiate [("m","k")] mult_commute;
   475 by (simp_tac (!simpset addsimps [diff_mult_distrib, mult_commute_k]) 1);
   476 qed "diff_mult_distrib2" ;
   477 (*NOT added as rewrites, since sometimes they are used from right-to-left*)
   478 
   479 
   480 (*** Monotonicity of Multiplication ***)
   481 
   482 goal Arith.thy "!!i::nat. i<=j ==> i*k<=j*k";
   483 by (induct_tac "k" 1);
   484 by (ALLGOALS (asm_simp_tac (!simpset addsimps [add_le_mono])));
   485 qed "mult_le_mono1";
   486 
   487 (*<=monotonicity, BOTH arguments*)
   488 goal Arith.thy "!!i::nat. [| i<=j; k<=l |] ==> i*k<=j*l";
   489 by (etac (mult_le_mono1 RS le_trans) 1);
   490 by (rtac le_trans 1);
   491 by (stac mult_commute 2);
   492 by (etac mult_le_mono1 2);
   493 by (simp_tac (!simpset addsimps [mult_commute]) 1);
   494 qed "mult_le_mono";
   495 
   496 (*strict, in 1st argument; proof is by induction on k>0*)
   497 goal Arith.thy "!!i::nat. [| i<j; 0<k |] ==> k*i < k*j";
   498 by (eres_inst_tac [("i","0")] less_natE 1);
   499 by (Asm_simp_tac 1);
   500 by (induct_tac "x" 1);
   501 by (ALLGOALS (asm_simp_tac (!simpset addsimps [add_less_mono])));
   502 qed "mult_less_mono2";
   503 
   504 goal Arith.thy "!!i::nat. [| i<j; 0<k |] ==> i*k < j*k";
   505 bd mult_less_mono2 1;
   506 by (ALLGOALS (asm_full_simp_tac (!simpset addsimps [mult_commute])));
   507 qed "mult_less_mono1";
   508 
   509 goal Arith.thy "(0 < m*n) = (0<m & 0<n)";
   510 by (induct_tac "m" 1);
   511 by (induct_tac "n" 2);
   512 by (ALLGOALS Asm_simp_tac);
   513 qed "zero_less_mult_iff";
   514 
   515 goal Arith.thy "(m*n = 1) = (m=1 & n=1)";
   516 by (induct_tac "m" 1);
   517 by (Simp_tac 1);
   518 by (induct_tac "n" 1);
   519 by (Simp_tac 1);
   520 by (fast_tac (!claset addss !simpset) 1);
   521 qed "mult_eq_1_iff";
   522 
   523 goal Arith.thy "!!k. 0<k ==> (m*k < n*k) = (m<n)";
   524 by (safe_tac (!claset addSIs [mult_less_mono1]));
   525 by (cut_facts_tac [less_linear] 1);
   526 by (blast_tac (!claset addDs [mult_less_mono1] addEs [less_asym]) 1);
   527 qed "mult_less_cancel2";
   528 
   529 goal Arith.thy "!!k. 0<k ==> (k*m < k*n) = (m<n)";
   530 bd mult_less_cancel2 1;
   531 by (asm_full_simp_tac (!simpset addsimps [mult_commute]) 1);
   532 qed "mult_less_cancel1";
   533 Addsimps [mult_less_cancel1, mult_less_cancel2];
   534 
   535 goal Arith.thy "!!k. 0<k ==> (m*k = n*k) = (m=n)";
   536 by (cut_facts_tac [less_linear] 1);
   537 by(Step_tac 1);
   538 ba 2;
   539 by (ALLGOALS (dtac mult_less_mono1 THEN' assume_tac));
   540 by (ALLGOALS Asm_full_simp_tac);
   541 qed "mult_cancel2";
   542 
   543 goal Arith.thy "!!k. 0<k ==> (k*m = k*n) = (m=n)";
   544 bd mult_cancel2 1;
   545 by (asm_full_simp_tac (!simpset addsimps [mult_commute]) 1);
   546 qed "mult_cancel1";
   547 Addsimps [mult_cancel1, mult_cancel2];
   548 
   549 
   550 (** Lemma for gcd **)
   551 
   552 goal Arith.thy "!!m n. m = m*n ==> n=1 | m=0";
   553 by (dtac sym 1);
   554 by (rtac disjCI 1);
   555 by (rtac nat_less_cases 1 THEN assume_tac 2);
   556 by (fast_tac (!claset addSEs [less_SucE] addss !simpset) 1);
   557 by (best_tac (!claset addDs [mult_less_mono2] 
   558                       addss (!simpset addsimps [zero_less_eq RS sym])) 1);
   559 qed "mult_eq_self_implies_10";
   560 
   561 
   562 (*** Subtraction laws -- from Clemens Ballarin ***)
   563 
   564 goal Arith.thy "!! a b c::nat. [| a < b; c <= a |] ==> a-c < b-c";
   565 by (subgoal_tac "c+(a-c) < c+(b-c)" 1);
   566 by (Asm_full_simp_tac 1);
   567 by (subgoal_tac "c <= b" 1);
   568 by (blast_tac (!claset addIs [less_imp_le, le_trans]) 2);
   569 by (asm_simp_tac (!simpset addsimps [leD RS add_diff_inverse]) 1);
   570 qed "diff_less_mono";
   571 
   572 goal Arith.thy "!! a b c::nat. a+b < c ==> a < c-b";
   573 bd diff_less_mono 1;
   574 br le_add2 1;
   575 by (Asm_full_simp_tac 1);
   576 qed "add_less_imp_less_diff";
   577 
   578 goal Arith.thy "!! n. n <= m ==> Suc m - n = Suc (m - n)";
   579 br Suc_diff_n 1;
   580 by (asm_full_simp_tac (!simpset addsimps [le_eq_less_Suc]) 1);
   581 qed "Suc_diff_le";
   582 
   583 goal Arith.thy "!! n. Suc i <= n ==> Suc (n - Suc i) = n - i";
   584 by (asm_full_simp_tac
   585     (!simpset addsimps [Suc_diff_n RS sym, le_eq_less_Suc]) 1);
   586 qed "Suc_diff_Suc";
   587 
   588 goal Arith.thy "!! i::nat. i <= n ==> n - (n - i) = i";
   589 by (subgoal_tac "(n-i) + (n - (n-i)) = (n-i) + i" 1);
   590 by (Asm_full_simp_tac 1);
   591 by (asm_simp_tac (!simpset addsimps [leD RS add_diff_inverse, diff_le_self, 
   592 				     add_commute]) 1);
   593 qed "diff_diff_cancel";
   594 
   595 goal Arith.thy "!!k::nat. k <= n ==> m <= n + m - k";
   596 be rev_mp 1;
   597 by (res_inst_tac [("m", "k"), ("n", "n")] diff_induct 1);
   598 by (Simp_tac 1);
   599 by (simp_tac (!simpset addsimps [less_add_Suc2, less_imp_le]) 1);
   600 by (Simp_tac 1);
   601 qed "le_add_diff";
   602 
   603