src/HOL/Arith.ML
author nipkow
Thu Oct 01 18:29:25 1998 +0200 (1998-10-01)
changeset 5598 6b8dee1a6ebb
parent 5537 c2bd39a2c0ee
child 5604 cd17004d09e1
permissions -rw-r--r--
a few new lemmas.
     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 "0 < n ==> Suc(n-1) = n";
    32 by (asm_simp_tac (simpset() addsplits [nat.split]) 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 "(k + m = k + n) = (m=(n::nat))";
    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 "(m + k = n + k) = (m=(n::nat))";
    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 "(k + m <= k + n) = (m<=(n::nat))";
    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 "(k + m < k + n) = (m<(n::nat))";
    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 "(m+n = 0) = (m=0 & n=0)";
    96 by (exhaust_tac "m" 1);
    97 by (Auto_tac);
    98 qed "add_is_0";
    99 AddIffs [add_is_0];
   100 
   101 Goal "(0 = m+n) = (m=0 & n=0)";
   102 by (exhaust_tac "m" 1);
   103 by (Auto_tac);
   104 qed "zero_is_add";
   105 AddIffs [zero_is_add];
   106 
   107 Goal "(m+n=1) = (m=1 & n=0 | m=0 & n=1)";
   108 by(exhaust_tac "m" 1);
   109 by(Auto_tac);
   110 qed "add_is_1";
   111 
   112 Goal "(1=m+n) = (m=1 & n=0 | m=0 & n=1)";
   113 by(exhaust_tac "m" 1);
   114 by(Auto_tac);
   115 qed "one_is_add";
   116 
   117 Goal "(0<m+n) = (0<m | 0<n)";
   118 by (simp_tac (simpset() delsimps [neq0_conv] addsimps [neq0_conv RS sym]) 1);
   119 qed "add_gr_0";
   120 AddIffs [add_gr_0];
   121 
   122 (* FIXME: really needed?? *)
   123 Goal "((m+n)-1 = 0) = (m=0 & n-1 = 0 | m-1 = 0 & n=0)";
   124 by (exhaust_tac "m" 1);
   125 by (ALLGOALS (fast_tac (claset() addss (simpset()))));
   126 qed "pred_add_is_0";
   127 Addsimps [pred_add_is_0];
   128 
   129 (* Could be generalized, eg to "k<n ==> m+(n-(Suc k)) = (m+n)-(Suc k)" *)
   130 Goal "0<n ==> m + (n-1) = (m+n)-1";
   131 by (exhaust_tac "m" 1);
   132 by (ALLGOALS (asm_simp_tac (simpset() addsimps [diff_Suc]
   133                                       addsplits [nat.split])));
   134 qed "add_pred";
   135 Addsimps [add_pred];
   136 
   137 Goal "m + n = m ==> n = 0";
   138 by (dtac (add_0_right RS ssubst) 1);
   139 by (asm_full_simp_tac (simpset() addsimps [add_assoc]
   140                                  delsimps [add_0_right]) 1);
   141 qed "add_eq_self_zero";
   142 
   143 
   144 (**** Additional theorems about "less than" ****)
   145 
   146 (*Deleted less_natE; instead use less_eq_Suc_add RS exE*)
   147 Goal "m<n --> (? k. n=Suc(m+k))";
   148 by (induct_tac "n" 1);
   149 by (ALLGOALS (simp_tac (simpset() addsimps [le_eq_less_or_eq])));
   150 by (blast_tac (claset() addSEs [less_SucE] 
   151                         addSIs [add_0_right RS sym, add_Suc_right RS sym]) 1);
   152 qed_spec_mp "less_eq_Suc_add";
   153 
   154 Goal "n <= ((m + n)::nat)";
   155 by (induct_tac "m" 1);
   156 by (ALLGOALS Simp_tac);
   157 by (etac le_trans 1);
   158 by (rtac (lessI RS less_imp_le) 1);
   159 qed "le_add2";
   160 
   161 Goal "n <= ((n + m)::nat)";
   162 by (simp_tac (simpset() addsimps add_ac) 1);
   163 by (rtac le_add2 1);
   164 qed "le_add1";
   165 
   166 bind_thm ("less_add_Suc1", (lessI RS (le_add1 RS le_less_trans)));
   167 bind_thm ("less_add_Suc2", (lessI RS (le_add2 RS le_less_trans)));
   168 
   169 Goal "(m<n) = (? k. n=Suc(m+k))";
   170 by (blast_tac (claset() addSIs [less_add_Suc1, less_eq_Suc_add]) 1);
   171 qed "less_iff_Suc_add";
   172 
   173 
   174 (*"i <= j ==> i <= j+m"*)
   175 bind_thm ("trans_le_add1", le_add1 RSN (2,le_trans));
   176 
   177 (*"i <= j ==> i <= m+j"*)
   178 bind_thm ("trans_le_add2", le_add2 RSN (2,le_trans));
   179 
   180 (*"i < j ==> i < j+m"*)
   181 bind_thm ("trans_less_add1", le_add1 RSN (2,less_le_trans));
   182 
   183 (*"i < j ==> i < m+j"*)
   184 bind_thm ("trans_less_add2", le_add2 RSN (2,less_le_trans));
   185 
   186 Goal "i+j < (k::nat) ==> i<k";
   187 by (etac rev_mp 1);
   188 by (induct_tac "j" 1);
   189 by (ALLGOALS Asm_simp_tac);
   190 by (blast_tac (claset() addDs [Suc_lessD]) 1);
   191 qed "add_lessD1";
   192 
   193 Goal "~ (i+j < (i::nat))";
   194 by (rtac notI 1);
   195 by (etac (add_lessD1 RS less_irrefl) 1);
   196 qed "not_add_less1";
   197 
   198 Goal "~ (j+i < (i::nat))";
   199 by (simp_tac (simpset() addsimps [add_commute, not_add_less1]) 1);
   200 qed "not_add_less2";
   201 AddIffs [not_add_less1, not_add_less2];
   202 
   203 Goal "m+k<=n --> m<=(n::nat)";
   204 by (induct_tac "k" 1);
   205 by (ALLGOALS (asm_simp_tac (simpset() addsimps le_simps)));
   206 qed_spec_mp "add_leD1";
   207 
   208 Goal "m+k<=n ==> k<=(n::nat)";
   209 by (full_simp_tac (simpset() addsimps [add_commute]) 1);
   210 by (etac add_leD1 1);
   211 qed_spec_mp "add_leD2";
   212 
   213 Goal "m+k<=n ==> m<=n & k<=(n::nat)";
   214 by (blast_tac (claset() addDs [add_leD1, add_leD2]) 1);
   215 bind_thm ("add_leE", result() RS conjE);
   216 
   217 (*needs !!k for add_ac to work*)
   218 Goal "!!k:: nat. [| k<l;  m+l = k+n |] ==> m<n";
   219 by (auto_tac (claset(),
   220 	      simpset() delsimps [add_Suc_right]
   221 	                addsimps [less_iff_Suc_add,
   222 				  add_Suc_right RS sym] @ add_ac));
   223 qed "less_add_eq_less";
   224 
   225 
   226 (*** Monotonicity of Addition ***)
   227 
   228 (*strict, in 1st argument*)
   229 Goal "i < j ==> i + k < j + (k::nat)";
   230 by (induct_tac "k" 1);
   231 by (ALLGOALS Asm_simp_tac);
   232 qed "add_less_mono1";
   233 
   234 (*strict, in both arguments*)
   235 Goal "[|i < j; k < l|] ==> i + k < j + (l::nat)";
   236 by (rtac (add_less_mono1 RS less_trans) 1);
   237 by (REPEAT (assume_tac 1));
   238 by (induct_tac "j" 1);
   239 by (ALLGOALS Asm_simp_tac);
   240 qed "add_less_mono";
   241 
   242 (*A [clumsy] way of lifting < monotonicity to <= monotonicity *)
   243 val [lt_mono,le] = Goal
   244      "[| !!i j::nat. i<j ==> f(i) < f(j);       \
   245 \        i <= j                                 \
   246 \     |] ==> f(i) <= (f(j)::nat)";
   247 by (cut_facts_tac [le] 1);
   248 by (asm_full_simp_tac (simpset() addsimps [le_eq_less_or_eq]) 1);
   249 by (blast_tac (claset() addSIs [lt_mono]) 1);
   250 qed "less_mono_imp_le_mono";
   251 
   252 (*non-strict, in 1st argument*)
   253 Goal "i<=j ==> i + k <= j + (k::nat)";
   254 by (res_inst_tac [("f", "%j. j+k")] less_mono_imp_le_mono 1);
   255 by (etac add_less_mono1 1);
   256 by (assume_tac 1);
   257 qed "add_le_mono1";
   258 
   259 (*non-strict, in both arguments*)
   260 Goal "[|i<=j;  k<=l |] ==> i + k <= j + (l::nat)";
   261 by (etac (add_le_mono1 RS le_trans) 1);
   262 by (simp_tac (simpset() addsimps [add_commute]) 1);
   263 qed "add_le_mono";
   264 
   265 
   266 (*** Multiplication ***)
   267 
   268 (*right annihilation in product*)
   269 qed_goal "mult_0_right" thy "m * 0 = 0"
   270  (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
   271 
   272 (*right successor law for multiplication*)
   273 qed_goal "mult_Suc_right" thy  "m * Suc(n) = m + (m * n)"
   274  (fn _ => [induct_tac "m" 1,
   275            ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))]);
   276 
   277 Addsimps [mult_0_right, mult_Suc_right];
   278 
   279 Goal "1 * n = n";
   280 by (Asm_simp_tac 1);
   281 qed "mult_1";
   282 
   283 Goal "n * 1 = n";
   284 by (Asm_simp_tac 1);
   285 qed "mult_1_right";
   286 
   287 (*Commutative law for multiplication*)
   288 qed_goal "mult_commute" thy "m * n = n * (m::nat)"
   289  (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
   290 
   291 (*addition distributes over multiplication*)
   292 qed_goal "add_mult_distrib" thy "(m + n)*k = (m*k) + ((n*k)::nat)"
   293  (fn _ => [induct_tac "m" 1,
   294            ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))]);
   295 
   296 qed_goal "add_mult_distrib2" thy "k*(m + n) = (k*m) + ((k*n)::nat)"
   297  (fn _ => [induct_tac "m" 1,
   298            ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))]);
   299 
   300 (*Associative law for multiplication*)
   301 qed_goal "mult_assoc" thy "(m * n) * k = m * ((n * k)::nat)"
   302   (fn _ => [induct_tac "m" 1, 
   303             ALLGOALS (asm_simp_tac (simpset() addsimps [add_mult_distrib]))]);
   304 
   305 qed_goal "mult_left_commute" thy "x*(y*z) = y*((x*z)::nat)"
   306  (fn _ => [rtac trans 1, rtac mult_commute 1, rtac trans 1,
   307            rtac mult_assoc 1, rtac (mult_commute RS arg_cong) 1]);
   308 
   309 val mult_ac = [mult_assoc,mult_commute,mult_left_commute];
   310 
   311 Goal "(m*n = 0) = (m=0 | n=0)";
   312 by (induct_tac "m" 1);
   313 by (induct_tac "n" 2);
   314 by (ALLGOALS Asm_simp_tac);
   315 qed "mult_is_0";
   316 Addsimps [mult_is_0];
   317 
   318 Goal "m <= m*(m::nat)";
   319 by (induct_tac "m" 1);
   320 by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_assoc RS sym])));
   321 by (etac (le_add2 RSN (2,le_trans)) 1);
   322 qed "le_square";
   323 
   324 
   325 (*** Difference ***)
   326 
   327 
   328 qed_goal "diff_self_eq_0" thy "m - m = 0"
   329  (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
   330 Addsimps [diff_self_eq_0];
   331 
   332 (*Addition is the inverse of subtraction: if n<=m then n+(m-n) = m. *)
   333 Goal "~ m<n --> n+(m-n) = (m::nat)";
   334 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   335 by (ALLGOALS Asm_simp_tac);
   336 qed_spec_mp "add_diff_inverse";
   337 
   338 Goal "n<=m ==> n+(m-n) = (m::nat)";
   339 by (asm_simp_tac (simpset() addsimps [add_diff_inverse, not_less_iff_le]) 1);
   340 qed "le_add_diff_inverse";
   341 
   342 Goal "n<=m ==> (m-n)+n = (m::nat)";
   343 by (asm_simp_tac (simpset() addsimps [le_add_diff_inverse, add_commute]) 1);
   344 qed "le_add_diff_inverse2";
   345 
   346 Addsimps  [le_add_diff_inverse, le_add_diff_inverse2];
   347 
   348 
   349 (*** More results about difference ***)
   350 
   351 Goal "n <= m ==> Suc(m)-n = Suc(m-n)";
   352 by (etac rev_mp 1);
   353 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   354 by (ALLGOALS Asm_simp_tac);
   355 qed "Suc_diff_le";
   356 
   357 Goal "n<=(l::nat) --> Suc l - n + m = Suc (l - n + m)";
   358 by (res_inst_tac [("m","n"),("n","l")] diff_induct 1);
   359 by (ALLGOALS Asm_simp_tac);
   360 qed_spec_mp "Suc_diff_add_le";
   361 
   362 Goal "m - n < Suc(m)";
   363 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   364 by (etac less_SucE 3);
   365 by (ALLGOALS (asm_simp_tac (simpset() addsimps [less_Suc_eq])));
   366 qed "diff_less_Suc";
   367 
   368 Goal "m - n <= (m::nat)";
   369 by (res_inst_tac [("m","m"), ("n","n")] diff_induct 1);
   370 by (ALLGOALS Asm_simp_tac);
   371 qed "diff_le_self";
   372 Addsimps [diff_le_self];
   373 
   374 (* j<k ==> j-n < k *)
   375 bind_thm ("less_imp_diff_less", diff_le_self RS le_less_trans);
   376 
   377 Goal "!!i::nat. i-j-k = i - (j+k)";
   378 by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
   379 by (ALLGOALS Asm_simp_tac);
   380 qed "diff_diff_left";
   381 
   382 Goal "(Suc m - n) - Suc k = m - n - k";
   383 by (simp_tac (simpset() addsimps [diff_diff_left]) 1);
   384 qed "Suc_diff_diff";
   385 Addsimps [Suc_diff_diff];
   386 
   387 Goal "0<n ==> n - Suc i < n";
   388 by (exhaust_tac "n" 1);
   389 by Safe_tac;
   390 by (asm_simp_tac (simpset() addsimps le_simps) 1);
   391 qed "diff_Suc_less";
   392 Addsimps [diff_Suc_less];
   393 
   394 Goal "i<n ==> n - Suc i < n - i";
   395 by (exhaust_tac "n" 1);
   396 by (auto_tac (claset(),
   397 	      simpset() addsimps [Suc_diff_le]@le_simps));
   398 qed "diff_Suc_less_diff";
   399 
   400 Goal "m - n <= Suc m - n";
   401 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   402 by (ALLGOALS Asm_simp_tac);
   403 qed "diff_le_Suc_diff";
   404 
   405 (*This and the next few suggested by Florian Kammueller*)
   406 Goal "!!i::nat. i-j-k = i-k-j";
   407 by (simp_tac (simpset() addsimps [diff_diff_left, add_commute]) 1);
   408 qed "diff_commute";
   409 
   410 Goal "k<=j --> j<=i --> i - (j - k) = i - j + (k::nat)";
   411 by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
   412 by (ALLGOALS Asm_simp_tac);
   413 by (asm_simp_tac (simpset() addsimps [Suc_diff_le, le_Suc_eq]) 1);
   414 qed_spec_mp "diff_diff_right";
   415 
   416 Goal "k <= (j::nat) --> (i + j) - k = i + (j - k)";
   417 by (res_inst_tac [("m","j"),("n","k")] diff_induct 1);
   418 by (ALLGOALS Asm_simp_tac);
   419 qed_spec_mp "diff_add_assoc";
   420 
   421 Goal "k <= (j::nat) --> (j + i) - k = i + (j - k)";
   422 by (asm_simp_tac (simpset() addsimps [add_commute, diff_add_assoc]) 1);
   423 qed_spec_mp "diff_add_assoc2";
   424 
   425 Goal "(n+m) - n = (m::nat)";
   426 by (induct_tac "n" 1);
   427 by (ALLGOALS Asm_simp_tac);
   428 qed "diff_add_inverse";
   429 Addsimps [diff_add_inverse];
   430 
   431 Goal "(m+n) - n = (m::nat)";
   432 by (simp_tac (simpset() addsimps [diff_add_assoc]) 1);
   433 qed "diff_add_inverse2";
   434 Addsimps [diff_add_inverse2];
   435 
   436 Goal "i <= (j::nat) ==> (j-i=k) = (j=k+i)";
   437 by Safe_tac;
   438 by (ALLGOALS Asm_simp_tac);
   439 qed "le_imp_diff_is_add";
   440 
   441 Goal "(m-n = 0) = (m <= n)";
   442 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   443 by (ALLGOALS Asm_simp_tac);
   444 qed "diff_is_0_eq";
   445 Addsimps [diff_is_0_eq RS iffD2];
   446 
   447 Goal "m-n = 0  -->  n-m = 0  -->  m=n";
   448 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   449 by (REPEAT(Simp_tac 1 THEN TRY(atac 1)));
   450 qed_spec_mp "diffs0_imp_equal";
   451 
   452 Goal "(0<n-m) = (m<n)";
   453 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   454 by (ALLGOALS Asm_simp_tac);
   455 qed "zero_less_diff";
   456 Addsimps [zero_less_diff];
   457 
   458 Goal "i < j  ==> ? k. 0<k & i+k = j";
   459 by (res_inst_tac [("x","j - i")] exI 1);
   460 by (asm_simp_tac (simpset() addsimps [add_diff_inverse, less_not_sym]) 1);
   461 qed "less_imp_add_positive";
   462 
   463 Goal "Suc(m)-n = (if m<n then 0 else Suc(m-n))";
   464 by (simp_tac (simpset() addsimps [leI, Suc_le_eq, Suc_diff_le]) 1);
   465 qed "if_Suc_diff_le";
   466 
   467 Goal "Suc(m)-n <= Suc(m-n)";
   468 by (simp_tac (simpset() addsimps [if_Suc_diff_le]) 1);
   469 qed "diff_Suc_le_Suc_diff";
   470 
   471 Goal "P(k) --> (!n. P(Suc(n))--> P(n)) --> P(k-i)";
   472 by (res_inst_tac [("m","k"),("n","i")] diff_induct 1);
   473 by (ALLGOALS (Clarify_tac THEN' Simp_tac THEN' TRY o Blast_tac));
   474 qed "zero_induct_lemma";
   475 
   476 val prems = Goal "[| P(k);  !!n. P(Suc(n)) ==> P(n) |] ==> P(0)";
   477 by (rtac (diff_self_eq_0 RS subst) 1);
   478 by (rtac (zero_induct_lemma RS mp RS mp) 1);
   479 by (REPEAT (ares_tac ([impI,allI]@prems) 1));
   480 qed "zero_induct";
   481 
   482 Goal "(k+m) - (k+n) = m - (n::nat)";
   483 by (induct_tac "k" 1);
   484 by (ALLGOALS Asm_simp_tac);
   485 qed "diff_cancel";
   486 Addsimps [diff_cancel];
   487 
   488 Goal "(m+k) - (n+k) = m - (n::nat)";
   489 val add_commute_k = read_instantiate [("n","k")] add_commute;
   490 by (asm_simp_tac (simpset() addsimps [add_commute_k]) 1);
   491 qed "diff_cancel2";
   492 Addsimps [diff_cancel2];
   493 
   494 (*From Clemens Ballarin, proof by lcp*)
   495 Goal "[| k<=n; n<=m |] ==> (m-k) - (n-k) = m-(n::nat)";
   496 by (REPEAT (etac rev_mp 1));
   497 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   498 by (ALLGOALS Asm_simp_tac);
   499 (*a confluence problem*)
   500 by (asm_simp_tac (simpset() addsimps [Suc_diff_le, le_Suc_eq]) 1);
   501 qed "diff_right_cancel";
   502 
   503 Goal "n - (n+m) = 0";
   504 by (induct_tac "n" 1);
   505 by (ALLGOALS Asm_simp_tac);
   506 qed "diff_add_0";
   507 Addsimps [diff_add_0];
   508 
   509 
   510 (** Difference distributes over multiplication **)
   511 
   512 Goal "!!m::nat. (m - n) * k = (m * k) - (n * k)";
   513 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   514 by (ALLGOALS Asm_simp_tac);
   515 qed "diff_mult_distrib" ;
   516 
   517 Goal "!!m::nat. k * (m - n) = (k * m) - (k * n)";
   518 val mult_commute_k = read_instantiate [("m","k")] mult_commute;
   519 by (simp_tac (simpset() addsimps [diff_mult_distrib, mult_commute_k]) 1);
   520 qed "diff_mult_distrib2" ;
   521 (*NOT added as rewrites, since sometimes they are used from right-to-left*)
   522 
   523 
   524 (*** Monotonicity of Multiplication ***)
   525 
   526 Goal "i <= (j::nat) ==> i*k<=j*k";
   527 by (induct_tac "k" 1);
   528 by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_le_mono])));
   529 qed "mult_le_mono1";
   530 
   531 (*<=monotonicity, BOTH arguments*)
   532 Goal "[| i <= (j::nat); k <= l |] ==> i*k <= j*l";
   533 by (etac (mult_le_mono1 RS le_trans) 1);
   534 by (rtac le_trans 1);
   535 by (stac mult_commute 2);
   536 by (etac mult_le_mono1 2);
   537 by (simp_tac (simpset() addsimps [mult_commute]) 1);
   538 qed "mult_le_mono";
   539 
   540 (*strict, in 1st argument; proof is by induction on k>0*)
   541 Goal "[| i<j; 0<k |] ==> k*i < k*j";
   542 by (eres_inst_tac [("m1","0")] (less_eq_Suc_add RS exE) 1);
   543 by (Asm_simp_tac 1);
   544 by (induct_tac "x" 1);
   545 by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_less_mono])));
   546 qed "mult_less_mono2";
   547 
   548 Goal "[| i<j; 0<k |] ==> i*k < j*k";
   549 by (dtac mult_less_mono2 1);
   550 by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [mult_commute])));
   551 qed "mult_less_mono1";
   552 
   553 Goal "(0 < m*n) = (0<m & 0<n)";
   554 by (induct_tac "m" 1);
   555 by (induct_tac "n" 2);
   556 by (ALLGOALS Asm_simp_tac);
   557 qed "zero_less_mult_iff";
   558 Addsimps [zero_less_mult_iff];
   559 
   560 Goal "(m*n = 1) = (m=1 & n=1)";
   561 by (induct_tac "m" 1);
   562 by (Simp_tac 1);
   563 by (induct_tac "n" 1);
   564 by (Simp_tac 1);
   565 by (fast_tac (claset() addss simpset()) 1);
   566 qed "mult_eq_1_iff";
   567 Addsimps [mult_eq_1_iff];
   568 
   569 Goal "0<k ==> (m*k < n*k) = (m<n)";
   570 by (safe_tac (claset() addSIs [mult_less_mono1]));
   571 by (cut_facts_tac [less_linear] 1);
   572 by (blast_tac (claset() addIs [mult_less_mono1] addEs [less_asym]) 1);
   573 qed "mult_less_cancel2";
   574 
   575 Goal "0<k ==> (k*m < k*n) = (m<n)";
   576 by (dtac mult_less_cancel2 1);
   577 by (asm_full_simp_tac (simpset() addsimps [mult_commute]) 1);
   578 qed "mult_less_cancel1";
   579 Addsimps [mult_less_cancel1, mult_less_cancel2];
   580 
   581 Goal "(Suc k * m < Suc k * n) = (m < n)";
   582 by (rtac mult_less_cancel1 1);
   583 by (Simp_tac 1);
   584 qed "Suc_mult_less_cancel1";
   585 
   586 Goalw [le_def] "(Suc k * m <= Suc k * n) = (m <= n)";
   587 by (simp_tac (simpset_of HOL.thy) 1);
   588 by (rtac Suc_mult_less_cancel1 1);
   589 qed "Suc_mult_le_cancel1";
   590 
   591 Goal "0<k ==> (m*k = n*k) = (m=n)";
   592 by (cut_facts_tac [less_linear] 1);
   593 by Safe_tac;
   594 by (assume_tac 2);
   595 by (ALLGOALS (dtac mult_less_mono1 THEN' assume_tac));
   596 by (ALLGOALS Asm_full_simp_tac);
   597 qed "mult_cancel2";
   598 
   599 Goal "0<k ==> (k*m = k*n) = (m=n)";
   600 by (dtac mult_cancel2 1);
   601 by (asm_full_simp_tac (simpset() addsimps [mult_commute]) 1);
   602 qed "mult_cancel1";
   603 Addsimps [mult_cancel1, mult_cancel2];
   604 
   605 Goal "(Suc k * m = Suc k * n) = (m = n)";
   606 by (rtac mult_cancel1 1);
   607 by (Simp_tac 1);
   608 qed "Suc_mult_cancel1";
   609 
   610 
   611 (** Lemma for gcd **)
   612 
   613 Goal "m = m*n ==> n=1 | m=0";
   614 by (dtac sym 1);
   615 by (rtac disjCI 1);
   616 by (rtac nat_less_cases 1 THEN assume_tac 2);
   617 by (fast_tac (claset() addSEs [less_SucE] addss simpset()) 1);
   618 by (best_tac (claset() addDs [mult_less_mono2] addss simpset()) 1);
   619 qed "mult_eq_self_implies_10";
   620 
   621 
   622 (*** Subtraction laws -- mostly from Clemens Ballarin ***)
   623 
   624 Goal "[| a < (b::nat); c <= a |] ==> a-c < b-c";
   625 by (subgoal_tac "c+(a-c) < c+(b-c)" 1);
   626 by (Full_simp_tac 1);
   627 by (subgoal_tac "c <= b" 1);
   628 by (blast_tac (claset() addIs [less_imp_le, le_trans]) 2);
   629 by (Asm_simp_tac 1);
   630 qed "diff_less_mono";
   631 
   632 Goal "a+b < (c::nat) ==> a < c-b";
   633 by (dtac diff_less_mono 1);
   634 by (rtac le_add2 1);
   635 by (Asm_full_simp_tac 1);
   636 qed "add_less_imp_less_diff";
   637 
   638 Goal "(i < j-k) = (i+k < (j::nat))";
   639 by (rtac iffI 1);
   640  by (case_tac "k <= j" 1);
   641   by (dtac le_add_diff_inverse2 1);
   642   by (dres_inst_tac [("k","k")] add_less_mono1 1);
   643   by (Asm_full_simp_tac 1);
   644  by (rotate_tac 1 1);
   645  by (asm_full_simp_tac (simpset() addSolver cut_trans_tac) 1);
   646 by (etac add_less_imp_less_diff 1);
   647 qed "less_diff_conv";
   648 
   649 Goal "(j-k <= (i::nat)) = (j <= i+k)";
   650 by (simp_tac (simpset() addsimps [less_diff_conv, le_def]) 1);
   651 qed "le_diff_conv";
   652 
   653 Goal "k <= j ==> (i <= j-k) = (i+k <= (j::nat))";
   654 by (asm_full_simp_tac
   655     (simpset() delsimps [less_Suc_eq_le]
   656                addsimps [less_Suc_eq_le RS sym, less_diff_conv,
   657 			 Suc_diff_le RS sym]) 1);
   658 qed "le_diff_conv2";
   659 
   660 Goal "Suc i <= n ==> Suc (n - Suc i) = n - i";
   661 by (asm_full_simp_tac (simpset() addsimps [Suc_diff_le RS sym]) 1);
   662 qed "Suc_diff_Suc";
   663 
   664 Goal "i <= (n::nat) ==> n - (n - i) = i";
   665 by (etac rev_mp 1);
   666 by (res_inst_tac [("m","n"),("n","i")] diff_induct 1);
   667 by (ALLGOALS (asm_simp_tac  (simpset() addsimps [Suc_diff_le])));
   668 qed "diff_diff_cancel";
   669 Addsimps [diff_diff_cancel];
   670 
   671 Goal "k <= (n::nat) ==> m <= n + m - k";
   672 by (etac rev_mp 1);
   673 by (res_inst_tac [("m", "k"), ("n", "n")] diff_induct 1);
   674 by (Simp_tac 1);
   675 by (simp_tac (simpset() addsimps [le_add2, less_imp_le]) 1);
   676 by (Simp_tac 1);
   677 qed "le_add_diff";
   678 
   679 Goal "0<k ==> j<i --> j+k-i < k";
   680 by (res_inst_tac [("m","j"),("n","i")] diff_induct 1);
   681 by (ALLGOALS Asm_simp_tac);
   682 qed_spec_mp "add_diff_less";
   683 
   684 
   685 Goal "m-1 < n ==> m <= n";
   686 by (exhaust_tac "m" 1);
   687 by (auto_tac (claset(), simpset() addsimps [Suc_le_eq]));
   688 qed "pred_less_imp_le";
   689 
   690 Goal "j<=i ==> i - j < Suc i - j";
   691 by (REPEAT (etac rev_mp 1));
   692 by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
   693 by Auto_tac;
   694 qed "diff_less_Suc_diff";
   695 
   696 Goal "i - j <= Suc i - j";
   697 by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
   698 by Auto_tac;
   699 qed "diff_le_Suc_diff";
   700 AddIffs [diff_le_Suc_diff];
   701 
   702 Goal "n - Suc i <= n - i";
   703 by (case_tac "i<n" 1);
   704 by (dtac diff_Suc_less_diff 1);
   705 by (auto_tac (claset(), simpset() addsimps [leI]));
   706 qed "diff_Suc_le_diff";
   707 AddIffs [diff_Suc_le_diff];
   708 
   709 Goal "0 < n ==> (m <= n-1) = (m<n)";
   710 by (exhaust_tac "n" 1);
   711 by (auto_tac (claset(), simpset() addsimps le_simps));
   712 qed "le_pred_eq";
   713 
   714 Goal "0 < n ==> (m-1 < n) = (m<=n)";
   715 by (exhaust_tac "m" 1);
   716 by (auto_tac (claset(), simpset() addsimps [Suc_le_eq]));
   717 qed "less_pred_eq";
   718 
   719 (*In ordinary notation: if 0<n and n<=m then m-n < m *)
   720 Goal "[| 0<n; ~ m<n |] ==> m - n < m";
   721 by (subgoal_tac "0<n --> ~ m<n --> m - n < m" 1);
   722 by (Blast_tac 1);
   723 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   724 by (ALLGOALS(asm_simp_tac(simpset() addsimps [diff_less_Suc])));
   725 qed "diff_less";
   726 
   727 Goal "[| 0<n; n<=m |] ==> m - n < m";
   728 by (asm_simp_tac (simpset() addsimps [diff_less, not_less_iff_le]) 1);
   729 qed "le_diff_less";
   730 
   731 
   732 
   733 (** (Anti)Monotonicity of subtraction -- by Stefan Merz **)
   734 
   735 (* Monotonicity of subtraction in first argument *)
   736 Goal "m <= (n::nat) --> (m-l) <= (n-l)";
   737 by (induct_tac "n" 1);
   738 by (Simp_tac 1);
   739 by (simp_tac (simpset() addsimps [le_Suc_eq]) 1);
   740 by (blast_tac (claset() addIs [diff_le_Suc_diff, le_trans]) 1);
   741 qed_spec_mp "diff_le_mono";
   742 
   743 Goal "m <= (n::nat) ==> (l-n) <= (l-m)";
   744 by (induct_tac "l" 1);
   745 by (Simp_tac 1);
   746 by (case_tac "n <= na" 1);
   747 by (subgoal_tac "m <= na" 1);
   748 by (asm_simp_tac (simpset() addsimps [Suc_diff_le]) 1);
   749 by (fast_tac (claset() addEs [le_trans]) 1);
   750 by (dtac not_leE 1);
   751 by (asm_simp_tac (simpset() addsimps [if_Suc_diff_le]) 1);
   752 qed_spec_mp "diff_le_mono2";