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