src/HOL/Nat.ML
author paulson
Wed Dec 20 12:14:26 2000 +0100 (2000-12-20)
changeset 10710 0c8d58332658
parent 10558 09a91221ced1
child 10850 e1a793957a8f
permissions -rw-r--r--
tidying, removing obsolete lemmas about 0=... and 1=...
     1 (*  Title:      HOL/Nat.ML
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson and Tobias Nipkow
     4 
     5 Proofs about natural numbers and elementary arithmetic: addition,
     6 multiplication, etc.  Some from the Hoare example from Norbert Galm.
     7 *)
     8 
     9 (** conversion rules for nat_rec **)
    10 
    11 val [nat_rec_0, nat_rec_Suc] = nat.recs;
    12 bind_thm ("nat_rec_0", nat_rec_0);
    13 bind_thm ("nat_rec_Suc", nat_rec_Suc);
    14 
    15 (*These 2 rules ease the use of primitive recursion.  NOTE USE OF == *)
    16 val prems = Goal
    17     "[| !!n. f(n) == nat_rec c h n |] ==> f(0) = c";
    18 by (simp_tac (simpset() addsimps prems) 1);
    19 qed "def_nat_rec_0";
    20 
    21 val prems = Goal
    22     "[| !!n. f(n) == nat_rec c h n |] ==> f(Suc(n)) = h n (f n)";
    23 by (simp_tac (simpset() addsimps prems) 1);
    24 qed "def_nat_rec_Suc";
    25 
    26 val [nat_case_0, nat_case_Suc] = nat.cases;
    27 bind_thm ("nat_case_0", nat_case_0);
    28 bind_thm ("nat_case_Suc", nat_case_Suc);
    29 
    30 Goal "n ~= 0 ==> EX m. n = Suc m";
    31 by (case_tac "n" 1);
    32 by (REPEAT (Blast_tac 1));
    33 qed "not0_implies_Suc";
    34 
    35 Goal "!!n::nat. m<n ==> n ~= 0";
    36 by (case_tac "n" 1);
    37 by (ALLGOALS Asm_full_simp_tac);
    38 qed "gr_implies_not0";
    39 
    40 Goal "!!n::nat. (n ~= 0) = (0 < n)";
    41 by (case_tac "n" 1);
    42 by Auto_tac;
    43 qed "neq0_conv";
    44 AddIffs [neq0_conv];
    45 
    46 (*This theorem is useful with blast_tac: (n=0 ==> False) ==> 0<n *)
    47 bind_thm ("gr0I", [neq0_conv, notI] MRS iffD1);
    48 
    49 Goal "(0<n) = (EX m. n = Suc m)";
    50 by(fast_tac (claset() addIs [not0_implies_Suc]) 1);
    51 qed "gr0_conv_Suc";
    52 
    53 Goal "!!n::nat. (~(0 < n)) = (n=0)";
    54 by (rtac iffI 1);
    55  by (rtac ccontr 1);
    56  by (ALLGOALS Asm_full_simp_tac);
    57 qed "not_gr0";
    58 AddIffs [not_gr0];
    59 
    60 Goal "(Suc n <= m') --> (? m. m' = Suc m)";
    61 by (induct_tac "m'" 1);
    62 by  Auto_tac;
    63 qed_spec_mp "Suc_le_D";
    64 
    65 (*Useful in certain inductive arguments*)
    66 Goal "(m < Suc n) = (m=0 | (EX j. m = Suc j & j < n))";
    67 by (case_tac "m" 1);
    68 by Auto_tac;
    69 qed "less_Suc_eq_0_disj";
    70 
    71 Goalw [Least_nat_def]
    72  "[| ? n. P(Suc n); ~ P 0 |] ==> (LEAST n. P n) = Suc (LEAST m. P(Suc m))";
    73 by (rtac some_equality 1);
    74 by (fold_goals_tac [Least_nat_def]);
    75 by (safe_tac (claset() addSEs [LeastI]));
    76 by (rename_tac "j" 1);
    77 by (case_tac "j" 1);
    78 by (Blast_tac 1);
    79 by (blast_tac (claset() addDs [Suc_less_SucD, not_less_Least]) 1);
    80 by (rename_tac "k n" 1);
    81 by (case_tac "k" 1);
    82 by (Blast_tac 1);
    83 by (hyp_subst_tac 1);
    84 by (rewtac Least_nat_def);
    85 by (rtac (some_equality RS arg_cong RS sym) 1);
    86 by (blast_tac (claset() addDs [Suc_mono]) 1);
    87 by (cut_inst_tac [("m","m")] less_linear 1);
    88 by (blast_tac (claset() addIs [Suc_mono]) 1);
    89 qed "Least_Suc";
    90 
    91 val prems = Goal "[| P 0; P 1; !!k. P k ==> P (Suc (Suc k)) |] ==> P n";
    92 by (rtac nat_less_induct 1);
    93 by (case_tac "n" 1);
    94 by (case_tac "nat" 2);
    95 by (ALLGOALS (blast_tac (claset() addIs prems@[less_trans])));
    96 qed "nat_induct2";
    97 
    98 Goal "min 0 n = (0::nat)";
    99 by (rtac min_leastL 1);
   100 by (Simp_tac 1);
   101 qed "min_0L";
   102 
   103 Goal "min n 0 = (0::nat)";
   104 by (rtac min_leastR 1);
   105 by (Simp_tac 1);
   106 qed "min_0R";
   107 
   108 Goalw [min_def] "min (Suc m) (Suc n) = Suc(min m n)";
   109 by (Simp_tac 1);
   110 qed "min_Suc_Suc";
   111 
   112 Addsimps [min_0L,min_0R,min_Suc_Suc];
   113 
   114 Goalw [max_def] "max 0 n = (n::nat)";
   115 by (Simp_tac 1);
   116 qed "max_0L";
   117 
   118 Goalw [max_def] "max n 0 = (n::nat)";
   119 by (Simp_tac 1);
   120 qed "max_0R";
   121 
   122 Goalw [max_def] "max (Suc m) (Suc n) = Suc(max m n)";
   123 by (Simp_tac 1);
   124 qed "max_Suc_Suc";
   125 
   126 Addsimps [max_0L,max_0R,max_Suc_Suc];
   127 
   128 
   129 (*** Basic rewrite rules for the arithmetic operators ***)
   130 
   131 (** Difference **)
   132 
   133 Goal "0 - n = (0::nat)";
   134 by (induct_tac "n" 1);
   135 by (ALLGOALS Asm_simp_tac);
   136 qed "diff_0_eq_0";
   137 
   138 (*Must simplify BEFORE the induction!  (Else we get a critical pair)
   139   Suc(m) - Suc(n)   rewrites to   pred(Suc(m) - n)  *)
   140 Goal "Suc(m) - Suc(n) = m - n";
   141 by (Simp_tac 1);
   142 by (induct_tac "n" 1);
   143 by (ALLGOALS Asm_simp_tac);
   144 qed "diff_Suc_Suc";
   145 
   146 Addsimps [diff_0_eq_0, diff_Suc_Suc];
   147 
   148 (* Could be (and is, below) generalized in various ways;
   149    However, none of the generalizations are currently in the simpset,
   150    and I dread to think what happens if I put them in *)
   151 Goal "0 < n ==> Suc(n-1) = n";
   152 by (asm_simp_tac (simpset() addsplits [nat.split]) 1);
   153 qed "Suc_pred";
   154 Addsimps [Suc_pred];
   155 
   156 Delsimps [diff_Suc];
   157 
   158 
   159 (**** Inductive properties of the operators ****)
   160 
   161 (*** Addition ***)
   162 
   163 Goal "m + 0 = (m::nat)";
   164 by (induct_tac "m" 1);
   165 by (ALLGOALS Asm_simp_tac);
   166 qed "add_0_right";
   167 
   168 Goal "m + Suc(n) = Suc(m+n)";
   169 by (induct_tac "m" 1);
   170 by (ALLGOALS Asm_simp_tac);
   171 qed "add_Suc_right";
   172 
   173 Addsimps [add_0_right,add_Suc_right];
   174 
   175 
   176 (*Associative law for addition*)
   177 Goal "(m + n) + k = m + ((n + k)::nat)";
   178 by (induct_tac "m" 1);
   179 by (ALLGOALS Asm_simp_tac);
   180 qed "add_assoc";
   181 
   182 (*Commutative law for addition*)
   183 Goal "m + n = n + (m::nat)";
   184 by (induct_tac "m" 1);
   185 by (ALLGOALS Asm_simp_tac);
   186 qed "add_commute";
   187 
   188 Goal "x+(y+z)=y+((x+z)::nat)";
   189 by (rtac (add_commute RS trans) 1);
   190 by (rtac (add_assoc RS trans) 1);
   191 by (rtac (add_commute RS arg_cong) 1);
   192 qed "add_left_commute";
   193 
   194 (*Addition is an AC-operator*)
   195 bind_thms ("add_ac", [add_assoc, add_commute, add_left_commute]);
   196 
   197 Goal "(k + m = k + n) = (m=(n::nat))";
   198 by (induct_tac "k" 1);
   199 by (Simp_tac 1);
   200 by (Asm_simp_tac 1);
   201 qed "add_left_cancel";
   202 
   203 Goal "(m + k = n + k) = (m=(n::nat))";
   204 by (induct_tac "k" 1);
   205 by (Simp_tac 1);
   206 by (Asm_simp_tac 1);
   207 qed "add_right_cancel";
   208 
   209 Goal "(k + m <= k + n) = (m<=(n::nat))";
   210 by (induct_tac "k" 1);
   211 by (Simp_tac 1);
   212 by (Asm_simp_tac 1);
   213 qed "add_left_cancel_le";
   214 
   215 Goal "(k + m < k + n) = (m<(n::nat))";
   216 by (induct_tac "k" 1);
   217 by (Simp_tac 1);
   218 by (Asm_simp_tac 1);
   219 qed "add_left_cancel_less";
   220 
   221 Addsimps [add_left_cancel, add_right_cancel,
   222           add_left_cancel_le, add_left_cancel_less];
   223 
   224 (** Reasoning about m+0=0, etc. **)
   225 
   226 Goal "!!m::nat. (m+n = 0) = (m=0 & n=0)";
   227 by (case_tac "m" 1);
   228 by (Auto_tac);
   229 qed "add_is_0";
   230 AddIffs [add_is_0];
   231 
   232 Goal "!!m::nat. (m+n=1) = (m=1 & n=0 | m=0 & n=1)";
   233 by (case_tac "m" 1);
   234 by (Auto_tac);
   235 qed "add_is_1";
   236 
   237 Goal "!!m::nat. (0<m+n) = (0<m | 0<n)";
   238 by (simp_tac (simpset() delsimps [neq0_conv] addsimps [neq0_conv RS sym]) 1);
   239 qed "add_gr_0";
   240 AddIffs [add_gr_0];
   241 
   242 Goal "!!m::nat. m + n = m ==> n = 0";
   243 by (dtac (add_0_right RS ssubst) 1);
   244 by (asm_full_simp_tac (simpset() addsimps [add_assoc]
   245                                  delsimps [add_0_right]) 1);
   246 qed "add_eq_self_zero";
   247 
   248 
   249 (**** Additional theorems about "less than" ****)
   250 
   251 (*Deleted less_natE; instead use less_imp_Suc_add RS exE*)
   252 Goal "m<n --> (EX k. n=Suc(m+k))";
   253 by (induct_tac "n" 1);
   254 by (ALLGOALS (simp_tac (simpset() addsimps [order_le_less])));
   255 by (blast_tac (claset() addSEs [less_SucE]
   256                         addSIs [add_0_right RS sym, add_Suc_right RS sym]) 1);
   257 qed_spec_mp "less_imp_Suc_add";
   258 
   259 Goal "n <= ((m + n)::nat)";
   260 by (induct_tac "m" 1);
   261 by (ALLGOALS Simp_tac);
   262 by (etac le_SucI 1);
   263 qed "le_add2";
   264 
   265 Goal "n <= ((n + m)::nat)";
   266 by (simp_tac (simpset() addsimps add_ac) 1);
   267 by (rtac le_add2 1);
   268 qed "le_add1";
   269 
   270 bind_thm ("less_add_Suc1", (lessI RS (le_add1 RS le_less_trans)));
   271 bind_thm ("less_add_Suc2", (lessI RS (le_add2 RS le_less_trans)));
   272 
   273 Goal "(m<n) = (EX k. n=Suc(m+k))";
   274 by (blast_tac (claset() addSIs [less_add_Suc1, less_imp_Suc_add]) 1);
   275 qed "less_iff_Suc_add";
   276 
   277 
   278 (*"i <= j ==> i <= j+m"*)
   279 bind_thm ("trans_le_add1", le_add1 RSN (2,le_trans));
   280 
   281 (*"i <= j ==> i <= m+j"*)
   282 bind_thm ("trans_le_add2", le_add2 RSN (2,le_trans));
   283 
   284 (*"i < j ==> i < j+m"*)
   285 bind_thm ("trans_less_add1", le_add1 RSN (2,less_le_trans));
   286 
   287 (*"i < j ==> i < m+j"*)
   288 bind_thm ("trans_less_add2", le_add2 RSN (2,less_le_trans));
   289 
   290 Goal "i+j < (k::nat) --> i<k";
   291 by (induct_tac "j" 1);
   292 by (ALLGOALS Asm_simp_tac);
   293 by (blast_tac (claset() addDs [Suc_lessD]) 1);
   294 qed_spec_mp "add_lessD1";
   295 
   296 Goal "~ (i+j < (i::nat))";
   297 by (rtac notI 1);
   298 by (etac (add_lessD1 RS less_irrefl) 1);
   299 qed "not_add_less1";
   300 
   301 Goal "~ (j+i < (i::nat))";
   302 by (simp_tac (simpset() addsimps [add_commute, not_add_less1]) 1);
   303 qed "not_add_less2";
   304 AddIffs [not_add_less1, not_add_less2];
   305 
   306 Goal "m+k<=n --> m<=(n::nat)";
   307 by (induct_tac "k" 1);
   308 by (ALLGOALS (asm_simp_tac (simpset() addsimps le_simps)));
   309 qed_spec_mp "add_leD1";
   310 
   311 Goal "m+k<=n ==> k<=(n::nat)";
   312 by (full_simp_tac (simpset() addsimps [add_commute]) 1);
   313 by (etac add_leD1 1);
   314 qed_spec_mp "add_leD2";
   315 
   316 Goal "m+k<=n ==> m<=n & k<=(n::nat)";
   317 by (blast_tac (claset() addDs [add_leD1, add_leD2]) 1);
   318 bind_thm ("add_leE", result() RS conjE);
   319 
   320 (*needs !!k for add_ac to work*)
   321 Goal "!!k:: nat. [| k<l;  m+l = k+n |] ==> m<n";
   322 by (force_tac (claset(),
   323               simpset() delsimps [add_Suc_right]
   324                         addsimps [less_iff_Suc_add,
   325                                   add_Suc_right RS sym] @ add_ac) 1);
   326 qed "less_add_eq_less";
   327 
   328 
   329 (*** Monotonicity of Addition ***)
   330 
   331 (*strict, in 1st argument*)
   332 Goal "i < j ==> i + k < j + (k::nat)";
   333 by (induct_tac "k" 1);
   334 by (ALLGOALS Asm_simp_tac);
   335 qed "add_less_mono1";
   336 
   337 (*strict, in both arguments*)
   338 Goal "[|i < j; k < l|] ==> i + k < j + (l::nat)";
   339 by (rtac (add_less_mono1 RS less_trans) 1);
   340 by (REPEAT (assume_tac 1));
   341 by (induct_tac "j" 1);
   342 by (ALLGOALS Asm_simp_tac);
   343 qed "add_less_mono";
   344 
   345 (*A [clumsy] way of lifting < monotonicity to <= monotonicity *)
   346 val [lt_mono,le] = Goal
   347      "[| !!i j::nat. i<j ==> f(i) < f(j);       \
   348 \        i <= j                                 \
   349 \     |] ==> f(i) <= (f(j)::nat)";
   350 by (cut_facts_tac [le] 1);
   351 by (asm_full_simp_tac (simpset() addsimps [order_le_less]) 1);
   352 by (blast_tac (claset() addSIs [lt_mono]) 1);
   353 qed "less_mono_imp_le_mono";
   354 
   355 (*non-strict, in 1st argument*)
   356 Goal "i<=j ==> i + k <= j + (k::nat)";
   357 by (res_inst_tac [("f", "%j. j+k")] less_mono_imp_le_mono 1);
   358 by (etac add_less_mono1 1);
   359 by (assume_tac 1);
   360 qed "add_le_mono1";
   361 
   362 (*non-strict, in both arguments*)
   363 Goal "[|i<=j;  k<=l |] ==> i + k <= j + (l::nat)";
   364 by (etac (add_le_mono1 RS le_trans) 1);
   365 by (simp_tac (simpset() addsimps [add_commute]) 1);
   366 qed "add_le_mono";
   367 
   368 
   369 (*** Multiplication ***)
   370 
   371 (*right annihilation in product*)
   372 Goal "!!m::nat. m * 0 = 0";
   373 by (induct_tac "m" 1);
   374 by (ALLGOALS Asm_simp_tac);
   375 qed "mult_0_right";
   376 
   377 (*right successor law for multiplication*)
   378 Goal  "m * Suc(n) = m + (m * n)";
   379 by (induct_tac "m" 1);
   380 by (ALLGOALS(asm_simp_tac (simpset() addsimps add_ac)));
   381 qed "mult_Suc_right";
   382 
   383 Addsimps [mult_0_right, mult_Suc_right];
   384 
   385 Goal "1 * n = n";
   386 by (Asm_simp_tac 1);
   387 qed "mult_1";
   388 
   389 Goal "n * 1 = n";
   390 by (Asm_simp_tac 1);
   391 qed "mult_1_right";
   392 
   393 (*Commutative law for multiplication*)
   394 Goal "m * n = n * (m::nat)";
   395 by (induct_tac "m" 1);
   396 by (ALLGOALS Asm_simp_tac);
   397 qed "mult_commute";
   398 
   399 (*addition distributes over multiplication*)
   400 Goal "(m + n)*k = (m*k) + ((n*k)::nat)";
   401 by (induct_tac "m" 1);
   402 by (ALLGOALS(asm_simp_tac (simpset() addsimps add_ac)));
   403 qed "add_mult_distrib";
   404 
   405 Goal "k*(m + n) = (k*m) + ((k*n)::nat)";
   406 by (induct_tac "m" 1);
   407 by (ALLGOALS(asm_simp_tac (simpset() addsimps add_ac)));
   408 qed "add_mult_distrib2";
   409 
   410 (*Associative law for multiplication*)
   411 Goal "(m * n) * k = m * ((n * k)::nat)";
   412 by (induct_tac "m" 1);
   413 by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_mult_distrib])));
   414 qed "mult_assoc";
   415 
   416 Goal "x*(y*z) = y*((x*z)::nat)";
   417 by (rtac trans 1);
   418 by (rtac mult_commute 1);
   419 by (rtac trans 1);
   420 by (rtac mult_assoc 1);
   421 by (rtac (mult_commute RS arg_cong) 1);
   422 qed "mult_left_commute";
   423 
   424 bind_thms ("mult_ac", [mult_assoc,mult_commute,mult_left_commute]);
   425 
   426 Goal "!!m::nat. (m*n = 0) = (m=0 | n=0)";
   427 by (induct_tac "m" 1);
   428 by (induct_tac "n" 2);
   429 by (ALLGOALS Asm_simp_tac);
   430 qed "mult_is_0";
   431 Addsimps [mult_is_0];
   432 
   433 
   434 (*** Difference ***)
   435 
   436 Goal "!!m::nat. m - m = 0";
   437 by (induct_tac "m" 1);
   438 by (ALLGOALS Asm_simp_tac);
   439 qed "diff_self_eq_0";
   440 
   441 Addsimps [diff_self_eq_0];
   442 
   443 (*Addition is the inverse of subtraction: if n<=m then n+(m-n) = m. *)
   444 Goal "~ m<n --> n+(m-n) = (m::nat)";
   445 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   446 by (ALLGOALS Asm_simp_tac);
   447 qed_spec_mp "add_diff_inverse";
   448 
   449 Goal "n<=m ==> n+(m-n) = (m::nat)";
   450 by (asm_simp_tac (simpset() addsimps [add_diff_inverse, not_less_iff_le]) 1);
   451 qed "le_add_diff_inverse";
   452 
   453 Goal "n<=m ==> (m-n)+n = (m::nat)";
   454 by (asm_simp_tac (simpset() addsimps [le_add_diff_inverse, add_commute]) 1);
   455 qed "le_add_diff_inverse2";
   456 
   457 Addsimps  [le_add_diff_inverse, le_add_diff_inverse2];
   458 
   459 
   460 (*** More results about difference ***)
   461 
   462 Goal "n <= m ==> Suc(m)-n = Suc(m-n)";
   463 by (etac rev_mp 1);
   464 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   465 by (ALLGOALS Asm_simp_tac);
   466 qed "Suc_diff_le";
   467 
   468 Goal "m - n < Suc(m)";
   469 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   470 by (etac less_SucE 3);
   471 by (ALLGOALS (asm_simp_tac (simpset() addsimps [less_Suc_eq])));
   472 qed "diff_less_Suc";
   473 
   474 Goal "m - n <= (m::nat)";
   475 by (res_inst_tac [("m","m"), ("n","n")] diff_induct 1);
   476 by (ALLGOALS (asm_simp_tac (simpset() addsimps [le_SucI])));
   477 qed "diff_le_self";
   478 Addsimps [diff_le_self];
   479 
   480 (* j<k ==> j-n < k *)
   481 bind_thm ("less_imp_diff_less", diff_le_self RS le_less_trans);
   482 
   483 Goal "!!i::nat. i-j-k = i - (j+k)";
   484 by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
   485 by (ALLGOALS Asm_simp_tac);
   486 qed "diff_diff_left";
   487 
   488 Goal "(Suc m - n) - Suc k = m - n - k";
   489 by (simp_tac (simpset() addsimps [diff_diff_left]) 1);
   490 qed "Suc_diff_diff";
   491 Addsimps [Suc_diff_diff];
   492 
   493 Goal "0<n ==> n - Suc i < n";
   494 by (case_tac "n" 1);
   495 by Safe_tac;
   496 by (asm_simp_tac (simpset() addsimps le_simps) 1);
   497 qed "diff_Suc_less";
   498 Addsimps [diff_Suc_less];
   499 
   500 (*This and the next few suggested by Florian Kammueller*)
   501 Goal "!!i::nat. i-j-k = i-k-j";
   502 by (simp_tac (simpset() addsimps [diff_diff_left, add_commute]) 1);
   503 qed "diff_commute";
   504 
   505 Goal "k <= (j::nat) --> (i + j) - k = i + (j - k)";
   506 by (res_inst_tac [("m","j"),("n","k")] diff_induct 1);
   507 by (ALLGOALS Asm_simp_tac);
   508 qed_spec_mp "diff_add_assoc";
   509 
   510 Goal "k <= (j::nat) --> (j + i) - k = (j - k) + i";
   511 by (asm_simp_tac (simpset() addsimps [add_commute, diff_add_assoc]) 1);
   512 qed_spec_mp "diff_add_assoc2";
   513 
   514 Goal "(n+m) - n = (m::nat)";
   515 by (induct_tac "n" 1);
   516 by (ALLGOALS Asm_simp_tac);
   517 qed "diff_add_inverse";
   518 
   519 Goal "(m+n) - n = (m::nat)";
   520 by (simp_tac (simpset() addsimps [diff_add_assoc]) 1);
   521 qed "diff_add_inverse2";
   522 
   523 Goal "i <= (j::nat) ==> (j-i=k) = (j=k+i)";
   524 by Safe_tac;
   525 by (ALLGOALS (asm_simp_tac (simpset() addsimps [diff_add_inverse2])));
   526 qed "le_imp_diff_is_add";
   527 
   528 Goal "!!m::nat. (m-n = 0) = (m <= n)";
   529 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   530 by (ALLGOALS Asm_simp_tac);
   531 qed "diff_is_0_eq";
   532 Addsimps [diff_is_0_eq];
   533 
   534 Goal "!!m::nat. (0<n-m) = (m<n)";
   535 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   536 by (ALLGOALS Asm_simp_tac);
   537 qed "zero_less_diff";
   538 Addsimps [zero_less_diff];
   539 
   540 Goal "i < j  ==> EX k::nat. 0<k & i+k = j";
   541 by (res_inst_tac [("x","j - i")] exI 1);
   542 by (asm_simp_tac (simpset() addsimps [add_diff_inverse, less_not_sym]) 1);
   543 qed "less_imp_add_positive";
   544 
   545 Goal "P(k) --> (ALL n. P(Suc(n))--> P(n)) --> P(k-i)";
   546 by (res_inst_tac [("m","k"),("n","i")] diff_induct 1);
   547 by (ALLGOALS (Clarify_tac THEN' Simp_tac THEN' TRY o Blast_tac));
   548 qed "zero_induct_lemma";
   549 
   550 val prems = Goal "[| P(k);  !!n. P(Suc(n)) ==> P(n) |] ==> P(0)";
   551 by (rtac (diff_self_eq_0 RS subst) 1);
   552 by (rtac (zero_induct_lemma RS mp RS mp) 1);
   553 by (REPEAT (ares_tac ([impI,allI]@prems) 1));
   554 qed "zero_induct";
   555 
   556 Goal "(k+m) - (k+n) = m - (n::nat)";
   557 by (induct_tac "k" 1);
   558 by (ALLGOALS Asm_simp_tac);
   559 qed "diff_cancel";
   560 
   561 Goal "(m+k) - (n+k) = m - (n::nat)";
   562 by (asm_simp_tac
   563     (simpset() addsimps [diff_cancel, inst "n" "k" add_commute]) 1);
   564 qed "diff_cancel2";
   565 
   566 Goal "n - (n+m) = (0::nat)";
   567 by (induct_tac "n" 1);
   568 by (ALLGOALS Asm_simp_tac);
   569 qed "diff_add_0";
   570 
   571 
   572 (** Difference distributes over multiplication **)
   573 
   574 Goal "!!m::nat. (m - n) * k = (m * k) - (n * k)";
   575 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   576 by (ALLGOALS (asm_simp_tac (simpset() addsimps [diff_cancel])));
   577 qed "diff_mult_distrib" ;
   578 
   579 Goal "!!m::nat. k * (m - n) = (k * m) - (k * n)";
   580 val mult_commute_k = read_instantiate [("m","k")] mult_commute;
   581 by (simp_tac (simpset() addsimps [diff_mult_distrib, mult_commute_k]) 1);
   582 qed "diff_mult_distrib2" ;
   583 (*NOT added as rewrites, since sometimes they are used from right-to-left*)
   584 
   585 bind_thms ("nat_distrib",
   586   [add_mult_distrib, add_mult_distrib2, diff_mult_distrib, diff_mult_distrib2]);
   587 
   588 
   589 (*** Monotonicity of Multiplication ***)
   590 
   591 Goal "i <= (j::nat) ==> i*k<=j*k";
   592 by (induct_tac "k" 1);
   593 by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_le_mono])));
   594 qed "mult_le_mono1";
   595 
   596 Goal "i <= (j::nat) ==> k*i <= k*j";
   597 by (dtac mult_le_mono1 1);
   598 by (asm_simp_tac (simpset() addsimps [mult_commute]) 1);
   599 qed "mult_le_mono2";
   600 
   601 (* <= monotonicity, BOTH arguments*)
   602 Goal "[| i <= (j::nat); k <= l |] ==> i*k <= j*l";
   603 by (etac (mult_le_mono1 RS le_trans) 1);
   604 by (etac mult_le_mono2 1);
   605 qed "mult_le_mono";
   606 
   607 (*strict, in 1st argument; proof is by induction on k>0*)
   608 Goal "!!i::nat. [| i<j; 0<k |] ==> k*i < k*j";
   609 by (eres_inst_tac [("m1","0")] (less_imp_Suc_add RS exE) 1);
   610 by (Asm_simp_tac 1);
   611 by (induct_tac "x" 1);
   612 by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_less_mono])));
   613 qed "mult_less_mono2";
   614 
   615 Goal "!!i::nat. [| i<j; 0<k |] ==> i*k < j*k";
   616 by (dtac mult_less_mono2 1);
   617 by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [mult_commute])));
   618 qed "mult_less_mono1";
   619 
   620 Goal "!!m::nat. (0 < m*n) = (0<m & 0<n)";
   621 by (induct_tac "m" 1);
   622 by (case_tac "n" 2);
   623 by (ALLGOALS Asm_simp_tac);
   624 qed "zero_less_mult_iff";
   625 Addsimps [zero_less_mult_iff];
   626 
   627 Goal "(1 <= m*n) = (1<=m & 1<=n)";
   628 by (induct_tac "m" 1);
   629 by (case_tac "n" 2);
   630 by (ALLGOALS Asm_simp_tac);
   631 qed "one_le_mult_iff";
   632 Addsimps [one_le_mult_iff];
   633 
   634 Goal "(m*n = 1) = (m=1 & n=1)";
   635 by (induct_tac "m" 1);
   636 by (Simp_tac 1);
   637 by (induct_tac "n" 1);
   638 by (Simp_tac 1);
   639 by (fast_tac (claset() addss simpset()) 1);
   640 qed "mult_eq_1_iff";
   641 Addsimps [mult_eq_1_iff];
   642 
   643 Goal "!!m::nat. (m*k < n*k) = (0<k & m<n)";
   644 by (safe_tac (claset() addSIs [mult_less_mono1]));
   645 by (case_tac "k" 1);
   646 by Auto_tac;  
   647 by (full_simp_tac (simpset() delsimps [le_0_eq]
   648 			     addsimps [linorder_not_le RS sym]) 1);
   649 by (blast_tac (claset() addIs [mult_le_mono1]) 1); 
   650 qed "mult_less_cancel2";
   651 
   652 Goal "!!m::nat. (k*m < k*n) = (0<k & m<n)";
   653 by (simp_tac (simpset() addsimps [mult_less_cancel2, 
   654                                   inst "m" "k" mult_commute]) 1);
   655 qed "mult_less_cancel1";
   656 Addsimps [mult_less_cancel1, mult_less_cancel2];
   657 
   658 Goal "!!m::nat. (m*k <= n*k) = (0<k --> m<=n)";
   659 by (simp_tac (simpset() addsimps [linorder_not_less RS sym]) 1);
   660 by Auto_tac;  
   661 qed "mult_le_cancel2";
   662 
   663 Goal "!!m::nat. (k*m <= k*n) = (0<k --> m<=n)";
   664 by (simp_tac (simpset() addsimps [linorder_not_less RS sym]) 1);
   665 by Auto_tac;  
   666 qed "mult_le_cancel1";
   667 Addsimps [mult_le_cancel1, mult_le_cancel2];
   668 
   669 Goal "(m*k = n*k) = (m=n | (k = (0::nat)))";
   670 by (cut_facts_tac [less_linear] 1);
   671 by Safe_tac;
   672 by Auto_tac; 	
   673 by (ALLGOALS (dtac mult_less_mono1 THEN' assume_tac));
   674 by (ALLGOALS Asm_full_simp_tac);
   675 qed "mult_cancel2";
   676 
   677 Goal "(k*m = k*n) = (m=n | (k = (0::nat)))";
   678 by (simp_tac (simpset() addsimps [mult_cancel2, inst "m" "k" mult_commute]) 1);
   679 qed "mult_cancel1";
   680 Addsimps [mult_cancel1, mult_cancel2];
   681 
   682 Goal "(Suc k * m < Suc k * n) = (m < n)";
   683 by (stac mult_less_cancel1 1);
   684 by (Simp_tac 1);
   685 qed "Suc_mult_less_cancel1";
   686 
   687 Goal "(Suc k * m <= Suc k * n) = (m <= n)";
   688 by (stac mult_le_cancel1 1);
   689 by (Simp_tac 1);
   690 qed "Suc_mult_le_cancel1";
   691 
   692 Goal "(Suc k * m = Suc k * n) = (m = n)";
   693 by (stac mult_cancel1 1);
   694 by (Simp_tac 1);
   695 qed "Suc_mult_cancel1";
   696 
   697 
   698 (*Lemma for gcd*)
   699 Goal "!!m::nat. m = m*n ==> n=1 | m=0";
   700 by (dtac sym 1);
   701 by (rtac disjCI 1);
   702 by (rtac nat_less_cases 1 THEN assume_tac 2);
   703 by (fast_tac (claset() addSEs [less_SucE] addss simpset()) 1);
   704 by (best_tac (claset() addDs [mult_less_mono2] addss simpset()) 1);
   705 qed "mult_eq_self_implies_10";