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