src/HOL/Arith.ML
author paulson
Fri Oct 02 10:41:35 1998 +0200 (1998-10-02)
changeset 5604 cd17004d09e1
parent 5598 6b8dee1a6ebb
child 5654 8b872d546b9e
permissions -rw-r--r--
tidying
     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 [order_le_less])));
   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 [order_le_less]) 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 (*This and the next few suggested by Florian Kammueller*)
   401 Goal "!!i::nat. i-j-k = i-k-j";
   402 by (simp_tac (simpset() addsimps [diff_diff_left, add_commute]) 1);
   403 qed "diff_commute";
   404 
   405 Goal "k<=j --> j<=i --> i - (j - k) = i - j + (k::nat)";
   406 by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
   407 by (ALLGOALS Asm_simp_tac);
   408 by (asm_simp_tac (simpset() addsimps [Suc_diff_le, le_Suc_eq]) 1);
   409 qed_spec_mp "diff_diff_right";
   410 
   411 Goal "k <= (j::nat) --> (i + j) - k = i + (j - k)";
   412 by (res_inst_tac [("m","j"),("n","k")] diff_induct 1);
   413 by (ALLGOALS Asm_simp_tac);
   414 qed_spec_mp "diff_add_assoc";
   415 
   416 Goal "k <= (j::nat) --> (j + i) - k = i + (j - k)";
   417 by (asm_simp_tac (simpset() addsimps [add_commute, diff_add_assoc]) 1);
   418 qed_spec_mp "diff_add_assoc2";
   419 
   420 Goal "(n+m) - n = (m::nat)";
   421 by (induct_tac "n" 1);
   422 by (ALLGOALS Asm_simp_tac);
   423 qed "diff_add_inverse";
   424 Addsimps [diff_add_inverse];
   425 
   426 Goal "(m+n) - n = (m::nat)";
   427 by (simp_tac (simpset() addsimps [diff_add_assoc]) 1);
   428 qed "diff_add_inverse2";
   429 Addsimps [diff_add_inverse2];
   430 
   431 Goal "i <= (j::nat) ==> (j-i=k) = (j=k+i)";
   432 by Safe_tac;
   433 by (ALLGOALS Asm_simp_tac);
   434 qed "le_imp_diff_is_add";
   435 
   436 Goal "(m-n = 0) = (m <= n)";
   437 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   438 by (ALLGOALS Asm_simp_tac);
   439 qed "diff_is_0_eq";
   440 Addsimps [diff_is_0_eq RS iffD2];
   441 
   442 Goal "m-n = 0  -->  n-m = 0  -->  m=n";
   443 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   444 by (REPEAT(Simp_tac 1 THEN TRY(atac 1)));
   445 qed_spec_mp "diffs0_imp_equal";
   446 
   447 Goal "(0<n-m) = (m<n)";
   448 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   449 by (ALLGOALS Asm_simp_tac);
   450 qed "zero_less_diff";
   451 Addsimps [zero_less_diff];
   452 
   453 Goal "i < j  ==> ? k. 0<k & i+k = j";
   454 by (res_inst_tac [("x","j - i")] exI 1);
   455 by (asm_simp_tac (simpset() addsimps [add_diff_inverse, less_not_sym]) 1);
   456 qed "less_imp_add_positive";
   457 
   458 Goal "Suc(m)-n = (if m<n then 0 else Suc(m-n))";
   459 by (simp_tac (simpset() addsimps [leI, Suc_le_eq, Suc_diff_le]) 1);
   460 qed "if_Suc_diff_le";
   461 
   462 Goal "Suc(m)-n <= Suc(m-n)";
   463 by (simp_tac (simpset() addsimps [if_Suc_diff_le]) 1);
   464 qed "diff_Suc_le_Suc_diff";
   465 
   466 Goal "P(k) --> (!n. P(Suc(n))--> P(n)) --> P(k-i)";
   467 by (res_inst_tac [("m","k"),("n","i")] diff_induct 1);
   468 by (ALLGOALS (Clarify_tac THEN' Simp_tac THEN' TRY o Blast_tac));
   469 qed "zero_induct_lemma";
   470 
   471 val prems = Goal "[| P(k);  !!n. P(Suc(n)) ==> P(n) |] ==> P(0)";
   472 by (rtac (diff_self_eq_0 RS subst) 1);
   473 by (rtac (zero_induct_lemma RS mp RS mp) 1);
   474 by (REPEAT (ares_tac ([impI,allI]@prems) 1));
   475 qed "zero_induct";
   476 
   477 Goal "(k+m) - (k+n) = m - (n::nat)";
   478 by (induct_tac "k" 1);
   479 by (ALLGOALS Asm_simp_tac);
   480 qed "diff_cancel";
   481 Addsimps [diff_cancel];
   482 
   483 Goal "(m+k) - (n+k) = m - (n::nat)";
   484 val add_commute_k = read_instantiate [("n","k")] add_commute;
   485 by (asm_simp_tac (simpset() addsimps [add_commute_k]) 1);
   486 qed "diff_cancel2";
   487 Addsimps [diff_cancel2];
   488 
   489 (*From Clemens Ballarin, proof by lcp*)
   490 Goal "[| k<=n; n<=m |] ==> (m-k) - (n-k) = m-(n::nat)";
   491 by (REPEAT (etac rev_mp 1));
   492 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   493 by (ALLGOALS Asm_simp_tac);
   494 (*a confluence problem*)
   495 by (asm_simp_tac (simpset() addsimps [Suc_diff_le, le_Suc_eq]) 1);
   496 qed "diff_right_cancel";
   497 
   498 Goal "n - (n+m) = 0";
   499 by (induct_tac "n" 1);
   500 by (ALLGOALS Asm_simp_tac);
   501 qed "diff_add_0";
   502 Addsimps [diff_add_0];
   503 
   504 
   505 (** Difference distributes over multiplication **)
   506 
   507 Goal "!!m::nat. (m - n) * k = (m * k) - (n * k)";
   508 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   509 by (ALLGOALS Asm_simp_tac);
   510 qed "diff_mult_distrib" ;
   511 
   512 Goal "!!m::nat. k * (m - n) = (k * m) - (k * n)";
   513 val mult_commute_k = read_instantiate [("m","k")] mult_commute;
   514 by (simp_tac (simpset() addsimps [diff_mult_distrib, mult_commute_k]) 1);
   515 qed "diff_mult_distrib2" ;
   516 (*NOT added as rewrites, since sometimes they are used from right-to-left*)
   517 
   518 
   519 (*** Monotonicity of Multiplication ***)
   520 
   521 Goal "i <= (j::nat) ==> i*k<=j*k";
   522 by (induct_tac "k" 1);
   523 by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_le_mono])));
   524 qed "mult_le_mono1";
   525 
   526 (*<=monotonicity, BOTH arguments*)
   527 Goal "[| i <= (j::nat); k <= l |] ==> i*k <= j*l";
   528 by (etac (mult_le_mono1 RS le_trans) 1);
   529 by (rtac le_trans 1);
   530 by (stac mult_commute 2);
   531 by (etac mult_le_mono1 2);
   532 by (simp_tac (simpset() addsimps [mult_commute]) 1);
   533 qed "mult_le_mono";
   534 
   535 (*strict, in 1st argument; proof is by induction on k>0*)
   536 Goal "[| i<j; 0<k |] ==> k*i < k*j";
   537 by (eres_inst_tac [("m1","0")] (less_eq_Suc_add RS exE) 1);
   538 by (Asm_simp_tac 1);
   539 by (induct_tac "x" 1);
   540 by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_less_mono])));
   541 qed "mult_less_mono2";
   542 
   543 Goal "[| i<j; 0<k |] ==> i*k < j*k";
   544 by (dtac mult_less_mono2 1);
   545 by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [mult_commute])));
   546 qed "mult_less_mono1";
   547 
   548 Goal "(0 < m*n) = (0<m & 0<n)";
   549 by (induct_tac "m" 1);
   550 by (induct_tac "n" 2);
   551 by (ALLGOALS Asm_simp_tac);
   552 qed "zero_less_mult_iff";
   553 Addsimps [zero_less_mult_iff];
   554 
   555 Goal "(m*n = 1) = (m=1 & n=1)";
   556 by (induct_tac "m" 1);
   557 by (Simp_tac 1);
   558 by (induct_tac "n" 1);
   559 by (Simp_tac 1);
   560 by (fast_tac (claset() addss simpset()) 1);
   561 qed "mult_eq_1_iff";
   562 Addsimps [mult_eq_1_iff];
   563 
   564 Goal "0<k ==> (m*k < n*k) = (m<n)";
   565 by (safe_tac (claset() addSIs [mult_less_mono1]));
   566 by (cut_facts_tac [less_linear] 1);
   567 by (blast_tac (claset() addIs [mult_less_mono1] addEs [less_asym]) 1);
   568 qed "mult_less_cancel2";
   569 
   570 Goal "0<k ==> (k*m < k*n) = (m<n)";
   571 by (dtac mult_less_cancel2 1);
   572 by (asm_full_simp_tac (simpset() addsimps [mult_commute]) 1);
   573 qed "mult_less_cancel1";
   574 Addsimps [mult_less_cancel1, mult_less_cancel2];
   575 
   576 Goal "(Suc k * m < Suc k * n) = (m < n)";
   577 by (rtac mult_less_cancel1 1);
   578 by (Simp_tac 1);
   579 qed "Suc_mult_less_cancel1";
   580 
   581 Goalw [le_def] "(Suc k * m <= Suc k * n) = (m <= n)";
   582 by (simp_tac (simpset_of HOL.thy) 1);
   583 by (rtac Suc_mult_less_cancel1 1);
   584 qed "Suc_mult_le_cancel1";
   585 
   586 Goal "0<k ==> (m*k = n*k) = (m=n)";
   587 by (cut_facts_tac [less_linear] 1);
   588 by Safe_tac;
   589 by (assume_tac 2);
   590 by (ALLGOALS (dtac mult_less_mono1 THEN' assume_tac));
   591 by (ALLGOALS Asm_full_simp_tac);
   592 qed "mult_cancel2";
   593 
   594 Goal "0<k ==> (k*m = k*n) = (m=n)";
   595 by (dtac mult_cancel2 1);
   596 by (asm_full_simp_tac (simpset() addsimps [mult_commute]) 1);
   597 qed "mult_cancel1";
   598 Addsimps [mult_cancel1, mult_cancel2];
   599 
   600 Goal "(Suc k * m = Suc k * n) = (m = n)";
   601 by (rtac mult_cancel1 1);
   602 by (Simp_tac 1);
   603 qed "Suc_mult_cancel1";
   604 
   605 
   606 (** Lemma for gcd **)
   607 
   608 Goal "m = m*n ==> n=1 | m=0";
   609 by (dtac sym 1);
   610 by (rtac disjCI 1);
   611 by (rtac nat_less_cases 1 THEN assume_tac 2);
   612 by (fast_tac (claset() addSEs [less_SucE] addss simpset()) 1);
   613 by (best_tac (claset() addDs [mult_less_mono2] addss simpset()) 1);
   614 qed "mult_eq_self_implies_10";
   615 
   616 
   617 (*** Subtraction laws -- mostly from Clemens Ballarin ***)
   618 
   619 Goal "[| a < (b::nat); c <= a |] ==> a-c < b-c";
   620 by (subgoal_tac "c+(a-c) < c+(b-c)" 1);
   621 by (Full_simp_tac 1);
   622 by (subgoal_tac "c <= b" 1);
   623 by (blast_tac (claset() addIs [less_imp_le, le_trans]) 2);
   624 by (Asm_simp_tac 1);
   625 qed "diff_less_mono";
   626 
   627 Goal "a+b < (c::nat) ==> a < c-b";
   628 by (dtac diff_less_mono 1);
   629 by (rtac le_add2 1);
   630 by (Asm_full_simp_tac 1);
   631 qed "add_less_imp_less_diff";
   632 
   633 Goal "(i < j-k) = (i+k < (j::nat))";
   634 by (rtac iffI 1);
   635  by (case_tac "k <= j" 1);
   636   by (dtac le_add_diff_inverse2 1);
   637   by (dres_inst_tac [("k","k")] add_less_mono1 1);
   638   by (Asm_full_simp_tac 1);
   639  by (rotate_tac 1 1);
   640  by (asm_full_simp_tac (simpset() addSolver cut_trans_tac) 1);
   641 by (etac add_less_imp_less_diff 1);
   642 qed "less_diff_conv";
   643 
   644 Goal "(j-k <= (i::nat)) = (j <= i+k)";
   645 by (simp_tac (simpset() addsimps [less_diff_conv, le_def]) 1);
   646 qed "le_diff_conv";
   647 
   648 Goal "k <= j ==> (i <= j-k) = (i+k <= (j::nat))";
   649 by (asm_full_simp_tac
   650     (simpset() delsimps [less_Suc_eq_le]
   651                addsimps [less_Suc_eq_le RS sym, less_diff_conv,
   652 			 Suc_diff_le RS sym]) 1);
   653 qed "le_diff_conv2";
   654 
   655 Goal "Suc i <= n ==> Suc (n - Suc i) = n - i";
   656 by (asm_full_simp_tac (simpset() addsimps [Suc_diff_le RS sym]) 1);
   657 qed "Suc_diff_Suc";
   658 
   659 Goal "i <= (n::nat) ==> n - (n - i) = i";
   660 by (etac rev_mp 1);
   661 by (res_inst_tac [("m","n"),("n","i")] diff_induct 1);
   662 by (ALLGOALS (asm_simp_tac  (simpset() addsimps [Suc_diff_le])));
   663 qed "diff_diff_cancel";
   664 Addsimps [diff_diff_cancel];
   665 
   666 Goal "k <= (n::nat) ==> m <= n + m - k";
   667 by (etac rev_mp 1);
   668 by (res_inst_tac [("m", "k"), ("n", "n")] diff_induct 1);
   669 by (Simp_tac 1);
   670 by (simp_tac (simpset() addsimps [le_add2, less_imp_le]) 1);
   671 by (Simp_tac 1);
   672 qed "le_add_diff";
   673 
   674 Goal "0<k ==> j<i --> j+k-i < k";
   675 by (res_inst_tac [("m","j"),("n","i")] diff_induct 1);
   676 by (ALLGOALS Asm_simp_tac);
   677 qed_spec_mp "add_diff_less";
   678 
   679 
   680 Goal "m-1 < n ==> m <= n";
   681 by (exhaust_tac "m" 1);
   682 by (auto_tac (claset(), simpset() addsimps [Suc_le_eq]));
   683 qed "pred_less_imp_le";
   684 
   685 Goal "j<=i ==> i - j < Suc i - j";
   686 by (REPEAT (etac rev_mp 1));
   687 by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
   688 by Auto_tac;
   689 qed "diff_less_Suc_diff";
   690 
   691 Goal "i - j <= Suc i - j";
   692 by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
   693 by Auto_tac;
   694 qed "diff_le_Suc_diff";
   695 AddIffs [diff_le_Suc_diff];
   696 
   697 Goal "n - Suc i <= n - i";
   698 by (case_tac "i<n" 1);
   699 by (dtac diff_Suc_less_diff 1);
   700 by (auto_tac (claset(), simpset() addsimps [less_imp_le, leI]));
   701 qed "diff_Suc_le_diff";
   702 AddIffs [diff_Suc_le_diff];
   703 
   704 Goal "0 < n ==> (m <= n-1) = (m<n)";
   705 by (exhaust_tac "n" 1);
   706 by (auto_tac (claset(), simpset() addsimps le_simps));
   707 qed "le_pred_eq";
   708 
   709 Goal "0 < n ==> (m-1 < n) = (m<=n)";
   710 by (exhaust_tac "m" 1);
   711 by (auto_tac (claset(), simpset() addsimps [Suc_le_eq]));
   712 qed "less_pred_eq";
   713 
   714 (*In ordinary notation: if 0<n and n<=m then m-n < m *)
   715 Goal "[| 0<n; ~ m<n |] ==> m - n < m";
   716 by (subgoal_tac "0<n --> ~ m<n --> m - n < m" 1);
   717 by (Blast_tac 1);
   718 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   719 by (ALLGOALS(asm_simp_tac(simpset() addsimps [diff_less_Suc])));
   720 qed "diff_less";
   721 
   722 Goal "[| 0<n; n<=m |] ==> m - n < m";
   723 by (asm_simp_tac (simpset() addsimps [diff_less, not_less_iff_le]) 1);
   724 qed "le_diff_less";
   725 
   726 
   727 
   728 (** (Anti)Monotonicity of subtraction -- by Stefan Merz **)
   729 
   730 (* Monotonicity of subtraction in first argument *)
   731 Goal "m <= (n::nat) --> (m-l) <= (n-l)";
   732 by (induct_tac "n" 1);
   733 by (Simp_tac 1);
   734 by (simp_tac (simpset() addsimps [le_Suc_eq]) 1);
   735 by (blast_tac (claset() addIs [diff_le_Suc_diff, le_trans]) 1);
   736 qed_spec_mp "diff_le_mono";
   737 
   738 Goal "m <= (n::nat) ==> (l-n) <= (l-m)";
   739 by (induct_tac "l" 1);
   740 by (Simp_tac 1);
   741 by (case_tac "n <= na" 1);
   742 by (subgoal_tac "m <= na" 1);
   743 by (asm_simp_tac (simpset() addsimps [Suc_diff_le]) 1);
   744 by (fast_tac (claset() addEs [le_trans]) 1);
   745 by (dtac not_leE 1);
   746 by (asm_simp_tac (simpset() addsimps [if_Suc_diff_le]) 1);
   747 qed_spec_mp "diff_le_mono2";