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