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