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