src/HOL/Arith.ML
author paulson
Wed Mar 11 11:03:43 1998 +0100 (1998-03-11)
changeset 4732 10af4886b33f
parent 4686 74a12e86b20b
child 4736 f7d3b9aec7a1
permissions -rw-r--r--
Arith.thy -> thy; proved a few new theorems
     1 (*  Title:      HOL/Arith.ML
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1993  University of Cambridge
     5 
     6 Proofs about elementary arithmetic: addition, multiplication, etc.
     7 Some from the Hoare example from Norbert Galm
     8 *)
     9 
    10 (*** Basic rewrite rules for the arithmetic operators ***)
    11 
    12 
    13 (** Difference **)
    14 
    15 qed_goal "diff_0_eq_0" 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 [expand_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 [expand_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 (* This is a trivial consequence of diff_diff_left;
   374    could be got rid of if diff_diff_left were in the simpset...
   375 *)
   376 goal thy "(Suc m - n)-1 = m - n";
   377 by (simp_tac (simpset() addsimps [diff_diff_left]) 1);
   378 qed "pred_Suc_diff";
   379 Addsimps [pred_Suc_diff];
   380 
   381 goal thy "!!n. 0<n ==> n - Suc i < n";
   382 by (res_inst_tac [("n","n")] natE 1);
   383 by Safe_tac;
   384 by (asm_simp_tac (simpset() addsimps [le_eq_less_Suc RS sym]) 1);
   385 qed "diff_Suc_less";
   386 Addsimps [diff_Suc_less];
   387 
   388 goal thy "!!n::nat. m - n <= Suc m - n";
   389 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   390 by (ALLGOALS Asm_simp_tac);
   391 qed "diff_le_Suc_diff";
   392 
   393 (*This and the next few suggested by Florian Kammueller*)
   394 goal thy "!!i::nat. i-j-k = i-k-j";
   395 by (simp_tac (simpset() addsimps [diff_diff_left, add_commute]) 1);
   396 qed "diff_commute";
   397 
   398 goal thy "!!i j k:: nat. k<=j --> j<=i --> i - (j - k) = i - j + k";
   399 by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
   400 by (ALLGOALS Asm_simp_tac);
   401 by (asm_simp_tac
   402     (simpset() addsimps [Suc_diff_n, le_imp_less_Suc, le_Suc_eq]) 1);
   403 qed_spec_mp "diff_diff_right";
   404 
   405 goal thy "!!i j k:: nat. k<=j --> (i + j) - k = i + (j - k)";
   406 by (res_inst_tac [("m","j"),("n","k")] diff_induct 1);
   407 by (ALLGOALS Asm_simp_tac);
   408 qed_spec_mp "diff_add_assoc";
   409 
   410 goal thy "!!i j k:: nat. k<=j --> (j + i) - k = i + (j - k)";
   411 by (asm_simp_tac (simpset() addsimps [add_commute, diff_add_assoc]) 1);
   412 qed_spec_mp "diff_add_assoc2";
   413 
   414 goal thy "!!n::nat. (n+m) - n = m";
   415 by (induct_tac "n" 1);
   416 by (ALLGOALS Asm_simp_tac);
   417 qed "diff_add_inverse";
   418 Addsimps [diff_add_inverse];
   419 
   420 goal thy "!!n::nat.(m+n) - n = m";
   421 by (simp_tac (simpset() addsimps [diff_add_assoc]) 1);
   422 qed "diff_add_inverse2";
   423 Addsimps [diff_add_inverse2];
   424 
   425 goal thy "!!i j k::nat. i<=j ==> (j-i=k) = (j=k+i)";
   426 by Safe_tac;
   427 by (ALLGOALS Asm_simp_tac);
   428 qed "le_imp_diff_is_add";
   429 
   430 val [prem] = goal thy "m < Suc(n) ==> m-n = 0";
   431 by (rtac (prem RS rev_mp) 1);
   432 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   433 by (asm_simp_tac (simpset() addsimps [less_Suc_eq]) 1);
   434 by (ALLGOALS Asm_simp_tac);
   435 qed "less_imp_diff_is_0";
   436 
   437 val prems = goal thy "m-n = 0  -->  n-m = 0  -->  m=n";
   438 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   439 by (REPEAT(Simp_tac 1 THEN TRY(atac 1)));
   440 qed_spec_mp "diffs0_imp_equal";
   441 
   442 val [prem] = goal thy "m<n ==> 0<n-m";
   443 by (rtac (prem RS rev_mp) 1);
   444 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   445 by (ALLGOALS Asm_simp_tac);
   446 qed "less_imp_diff_positive";
   447 
   448 goal thy "Suc(m)-n = (if m<n then 0 else Suc(m-n))";
   449 by (simp_tac (simpset() addsimps [less_imp_diff_is_0, not_less_eq, Suc_diff_n]) 1);
   450 qed "if_Suc_diff_n";
   451 
   452 goal thy "Suc(m)-n <= Suc(m-n)";
   453 by (simp_tac (simpset() addsimps [if_Suc_diff_n]) 1);
   454 qed "diff_Suc_le_Suc_diff";
   455 
   456 goal thy "P(k) --> (!n. P(Suc(n))--> P(n)) --> P(k-i)";
   457 by (res_inst_tac [("m","k"),("n","i")] diff_induct 1);
   458 by (ALLGOALS (Clarify_tac THEN' Simp_tac THEN' TRY o Blast_tac));
   459 qed "zero_induct_lemma";
   460 
   461 val prems = goal thy "[| P(k);  !!n. P(Suc(n)) ==> P(n) |] ==> P(0)";
   462 by (rtac (diff_self_eq_0 RS subst) 1);
   463 by (rtac (zero_induct_lemma RS mp RS mp) 1);
   464 by (REPEAT (ares_tac ([impI,allI]@prems) 1));
   465 qed "zero_induct";
   466 
   467 goal thy "!!k::nat. (k+m) - (k+n) = m - n";
   468 by (induct_tac "k" 1);
   469 by (ALLGOALS Asm_simp_tac);
   470 qed "diff_cancel";
   471 Addsimps [diff_cancel];
   472 
   473 goal thy "!!m::nat. (m+k) - (n+k) = m - n";
   474 val add_commute_k = read_instantiate [("n","k")] add_commute;
   475 by (asm_simp_tac (simpset() addsimps ([add_commute_k])) 1);
   476 qed "diff_cancel2";
   477 Addsimps [diff_cancel2];
   478 
   479 (*From Clemens Ballarin*)
   480 goal thy "!!n::nat. [| k<=n; n<=m |] ==> (m-k) - (n-k) = m-n";
   481 by (subgoal_tac "k<=n --> n<=m --> (m-k) - (n-k) = m-n" 1);
   482 by (Asm_full_simp_tac 1);
   483 by (induct_tac "k" 1);
   484 by (Simp_tac 1);
   485 (* Induction step *)
   486 by (subgoal_tac "Suc na <= m --> n <= m --> Suc na <= n --> \
   487 \                Suc (m - Suc na) - Suc (n - Suc na) = m-n" 1);
   488 by (Asm_full_simp_tac 1);
   489 by (blast_tac (claset() addIs [le_trans]) 1);
   490 by (auto_tac (claset() addIs [Suc_leD], simpset() delsimps [diff_Suc_Suc]));
   491 by (asm_full_simp_tac (simpset() delsimps [Suc_less_eq] 
   492 		       addsimps [Suc_diff_n RS sym, le_eq_less_Suc]) 1);
   493 qed "diff_right_cancel";
   494 
   495 goal thy "!!n::nat. n - (n+m) = 0";
   496 by (induct_tac "n" 1);
   497 by (ALLGOALS Asm_simp_tac);
   498 qed "diff_add_0";
   499 Addsimps [diff_add_0];
   500 
   501 (** Difference distributes over multiplication **)
   502 
   503 goal thy "!!m::nat. (m - n) * k = (m * k) - (n * k)";
   504 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   505 by (ALLGOALS Asm_simp_tac);
   506 qed "diff_mult_distrib" ;
   507 
   508 goal thy "!!m::nat. k * (m - n) = (k * m) - (k * n)";
   509 val mult_commute_k = read_instantiate [("m","k")] mult_commute;
   510 by (simp_tac (simpset() addsimps [diff_mult_distrib, mult_commute_k]) 1);
   511 qed "diff_mult_distrib2" ;
   512 (*NOT added as rewrites, since sometimes they are used from right-to-left*)
   513 
   514 
   515 (*** Monotonicity of Multiplication ***)
   516 
   517 goal thy "!!i::nat. i<=j ==> i*k<=j*k";
   518 by (induct_tac "k" 1);
   519 by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_le_mono])));
   520 qed "mult_le_mono1";
   521 
   522 (*<=monotonicity, BOTH arguments*)
   523 goal thy "!!i::nat. [| i<=j; k<=l |] ==> i*k<=j*l";
   524 by (etac (mult_le_mono1 RS le_trans) 1);
   525 by (rtac le_trans 1);
   526 by (stac mult_commute 2);
   527 by (etac mult_le_mono1 2);
   528 by (simp_tac (simpset() addsimps [mult_commute]) 1);
   529 qed "mult_le_mono";
   530 
   531 (*strict, in 1st argument; proof is by induction on k>0*)
   532 goal thy "!!i::nat. [| i<j; 0<k |] ==> k*i < k*j";
   533 by (eres_inst_tac [("i","0")] less_natE 1);
   534 by (Asm_simp_tac 1);
   535 by (induct_tac "x" 1);
   536 by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_less_mono])));
   537 qed "mult_less_mono2";
   538 
   539 goal thy "!!i::nat. [| i<j; 0<k |] ==> i*k < j*k";
   540 by (dtac mult_less_mono2 1);
   541 by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [mult_commute])));
   542 qed "mult_less_mono1";
   543 
   544 goal thy "(0 < m*n) = (0<m & 0<n)";
   545 by (induct_tac "m" 1);
   546 by (induct_tac "n" 2);
   547 by (ALLGOALS Asm_simp_tac);
   548 qed "zero_less_mult_iff";
   549 Addsimps [zero_less_mult_iff];
   550 
   551 goal thy "(m*n = 1) = (m=1 & n=1)";
   552 by (induct_tac "m" 1);
   553 by (Simp_tac 1);
   554 by (induct_tac "n" 1);
   555 by (Simp_tac 1);
   556 by (fast_tac (claset() addss simpset()) 1);
   557 qed "mult_eq_1_iff";
   558 Addsimps [mult_eq_1_iff];
   559 
   560 goal thy "!!k. 0<k ==> (m*k < n*k) = (m<n)";
   561 by (safe_tac (claset() addSIs [mult_less_mono1]));
   562 by (cut_facts_tac [less_linear] 1);
   563 by (blast_tac (claset() addIs [mult_less_mono1] addEs [less_asym]) 1);
   564 qed "mult_less_cancel2";
   565 
   566 goal thy "!!k. 0<k ==> (k*m < k*n) = (m<n)";
   567 by (dtac mult_less_cancel2 1);
   568 by (asm_full_simp_tac (simpset() addsimps [mult_commute]) 1);
   569 qed "mult_less_cancel1";
   570 Addsimps [mult_less_cancel1, mult_less_cancel2];
   571 
   572 goal thy "(Suc k * m < Suc k * n) = (m < n)";
   573 by (rtac mult_less_cancel1 1);
   574 by (Simp_tac 1);
   575 qed "Suc_mult_less_cancel1";
   576 
   577 goalw thy [le_def] "(Suc k * m <= Suc k * n) = (m <= n)";
   578 by (simp_tac (simpset_of HOL.thy) 1);
   579 by (rtac Suc_mult_less_cancel1 1);
   580 qed "Suc_mult_le_cancel1";
   581 
   582 goal thy "!!k. 0<k ==> (m*k = n*k) = (m=n)";
   583 by (cut_facts_tac [less_linear] 1);
   584 by Safe_tac;
   585 by (assume_tac 2);
   586 by (ALLGOALS (dtac mult_less_mono1 THEN' assume_tac));
   587 by (ALLGOALS Asm_full_simp_tac);
   588 qed "mult_cancel2";
   589 
   590 goal thy "!!k. 0<k ==> (k*m = k*n) = (m=n)";
   591 by (dtac mult_cancel2 1);
   592 by (asm_full_simp_tac (simpset() addsimps [mult_commute]) 1);
   593 qed "mult_cancel1";
   594 Addsimps [mult_cancel1, mult_cancel2];
   595 
   596 goal thy "(Suc k * m = Suc k * n) = (m = n)";
   597 by (rtac mult_cancel1 1);
   598 by (Simp_tac 1);
   599 qed "Suc_mult_cancel1";
   600 
   601 
   602 (** Lemma for gcd **)
   603 
   604 goal thy "!!m n. m = m*n ==> n=1 | m=0";
   605 by (dtac sym 1);
   606 by (rtac disjCI 1);
   607 by (rtac nat_less_cases 1 THEN assume_tac 2);
   608 by (fast_tac (claset() addSEs [less_SucE] addss simpset()) 1);
   609 by (best_tac (claset() addDs [mult_less_mono2] addss simpset()) 1);
   610 qed "mult_eq_self_implies_10";
   611 
   612 
   613 (*** Subtraction laws -- from Clemens Ballarin ***)
   614 
   615 goal thy "!! a b c::nat. [| a < b; c <= a |] ==> a-c < b-c";
   616 by (subgoal_tac "c+(a-c) < c+(b-c)" 1);
   617 by (Full_simp_tac 1);
   618 by (subgoal_tac "c <= b" 1);
   619 by (blast_tac (claset() addIs [less_imp_le, le_trans]) 2);
   620 by (Asm_simp_tac 1);
   621 qed "diff_less_mono";
   622 
   623 goal thy "!! a b c::nat. a+b < c ==> a < c-b";
   624 by (dtac diff_less_mono 1);
   625 by (rtac le_add2 1);
   626 by (Asm_full_simp_tac 1);
   627 qed "add_less_imp_less_diff";
   628 
   629 goal thy "!! n. n <= m ==> Suc m - n = Suc (m - n)";
   630 by (asm_full_simp_tac (simpset() addsimps [Suc_diff_n, le_eq_less_Suc]) 1);
   631 qed "Suc_diff_le";
   632 
   633 goal thy "!! n. Suc i <= n ==> Suc (n - Suc i) = n - i";
   634 by (asm_full_simp_tac
   635     (simpset() addsimps [Suc_diff_n RS sym, le_eq_less_Suc]) 1);
   636 qed "Suc_diff_Suc";
   637 
   638 goal thy "!! i::nat. i <= n ==> n - (n - i) = i";
   639 by (etac rev_mp 1);
   640 by (res_inst_tac [("m","n"),("n","i")] diff_induct 1);
   641 by (ALLGOALS (asm_simp_tac  (simpset() addsimps [Suc_diff_le])));
   642 qed "diff_diff_cancel";
   643 Addsimps [diff_diff_cancel];
   644 
   645 goal thy "!!k::nat. k <= n ==> m <= n + m - k";
   646 by (etac rev_mp 1);
   647 by (res_inst_tac [("m", "k"), ("n", "n")] diff_induct 1);
   648 by (Simp_tac 1);
   649 by (simp_tac (simpset() addsimps [less_add_Suc2, less_imp_le]) 1);
   650 by (Simp_tac 1);
   651 qed "le_add_diff";
   652 
   653 
   654 
   655 (** (Anti)Monotonicity of subtraction -- by Stefan Merz **)
   656 
   657 (* Monotonicity of subtraction in first argument *)
   658 goal thy "!!n::nat. m<=n --> (m-l) <= (n-l)";
   659 by (induct_tac "n" 1);
   660 by (Simp_tac 1);
   661 by (simp_tac (simpset() addsimps [le_Suc_eq]) 1);
   662 by (blast_tac (claset() addIs [diff_le_Suc_diff, le_trans]) 1);
   663 qed_spec_mp "diff_le_mono";
   664 
   665 goal thy "!!n::nat. m<=n ==> (l-n) <= (l-m)";
   666 by (induct_tac "l" 1);
   667 by (Simp_tac 1);
   668 by (case_tac "n <= l" 1);
   669 by (subgoal_tac "m <= l" 1);
   670 by (asm_simp_tac (simpset() addsimps [Suc_diff_le]) 1);
   671 by (fast_tac (claset() addEs [le_trans]) 1);
   672 by (dtac not_leE 1);
   673 by (asm_simp_tac (simpset() addsimps [if_Suc_diff_n]) 1);
   674 qed_spec_mp "diff_le_mono2";