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