src/HOL/Nat.ML
author oheimb
Thu May 31 16:50:13 2001 +0200 (2001-05-31)
changeset 11337 9d6d6a8966b9
parent 11139 b092ad5cd510
child 11464 ddea204de5bc
permissions -rw-r--r--
added Least_Suc2
     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 1; !!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-1) = 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::nat. (m+n=1) = (m=1 & n=0 | m=0 & n=1)";
   242 by (case_tac "m" 1);
   243 by (Auto_tac);
   244 qed "add_is_1";
   245 
   246 Goal "!!m::nat. (0<m+n) = (0<m | 0<n)";
   247 by (simp_tac (simpset() delsimps [neq0_conv] addsimps [neq0_conv RS sym]) 1);
   248 qed "add_gr_0";
   249 AddIffs [add_gr_0];
   250 
   251 Goal "!!m::nat. m + n = m ==> n = 0";
   252 by (dtac (add_0_right RS ssubst) 1);
   253 by (asm_full_simp_tac (simpset() addsimps [add_assoc]
   254                                  delsimps [add_0_right]) 1);
   255 qed "add_eq_self_zero";
   256 
   257 
   258 (**** Additional theorems about "less than" ****)
   259 
   260 (*Deleted less_natE; instead use less_imp_Suc_add RS exE*)
   261 Goal "m<n --> (EX k. n=Suc(m+k))";
   262 by (induct_tac "n" 1);
   263 by (ALLGOALS (simp_tac (simpset() addsimps [order_le_less])));
   264 by (blast_tac (claset() addSEs [less_SucE]
   265                         addSIs [add_0_right RS sym, add_Suc_right RS sym]) 1);
   266 qed_spec_mp "less_imp_Suc_add";
   267 
   268 Goal "n <= ((m + n)::nat)";
   269 by (induct_tac "m" 1);
   270 by (ALLGOALS Simp_tac);
   271 by (etac le_SucI 1);
   272 qed "le_add2";
   273 
   274 Goal "n <= ((n + m)::nat)";
   275 by (simp_tac (simpset() addsimps add_ac) 1);
   276 by (rtac le_add2 1);
   277 qed "le_add1";
   278 
   279 bind_thm ("less_add_Suc1", (lessI RS (le_add1 RS le_less_trans)));
   280 bind_thm ("less_add_Suc2", (lessI RS (le_add2 RS le_less_trans)));
   281 
   282 Goal "(m<n) = (EX k. n=Suc(m+k))";
   283 by (blast_tac (claset() addSIs [less_add_Suc1, less_imp_Suc_add]) 1);
   284 qed "less_iff_Suc_add";
   285 
   286 
   287 (*"i <= j ==> i <= j+m"*)
   288 bind_thm ("trans_le_add1", le_add1 RSN (2,le_trans));
   289 
   290 (*"i <= j ==> i <= m+j"*)
   291 bind_thm ("trans_le_add2", le_add2 RSN (2,le_trans));
   292 
   293 (*"i < j ==> i < j+m"*)
   294 bind_thm ("trans_less_add1", le_add1 RSN (2,less_le_trans));
   295 
   296 (*"i < j ==> i < m+j"*)
   297 bind_thm ("trans_less_add2", le_add2 RSN (2,less_le_trans));
   298 
   299 Goal "i+j < (k::nat) --> i<k";
   300 by (induct_tac "j" 1);
   301 by (ALLGOALS Asm_simp_tac);
   302 by (blast_tac (claset() addDs [Suc_lessD]) 1);
   303 qed_spec_mp "add_lessD1";
   304 
   305 Goal "~ (i+j < (i::nat))";
   306 by (rtac notI 1);
   307 by (etac (add_lessD1 RS less_irrefl) 1);
   308 qed "not_add_less1";
   309 
   310 Goal "~ (j+i < (i::nat))";
   311 by (simp_tac (simpset() addsimps [add_commute, not_add_less1]) 1);
   312 qed "not_add_less2";
   313 AddIffs [not_add_less1, not_add_less2];
   314 
   315 Goal "m+k<=n --> m<=(n::nat)";
   316 by (induct_tac "k" 1);
   317 by (ALLGOALS (asm_simp_tac (simpset() addsimps le_simps)));
   318 qed_spec_mp "add_leD1";
   319 
   320 Goal "m+k<=n ==> k<=(n::nat)";
   321 by (full_simp_tac (simpset() addsimps [add_commute]) 1);
   322 by (etac add_leD1 1);
   323 qed_spec_mp "add_leD2";
   324 
   325 Goal "m+k<=n ==> m<=n & k<=(n::nat)";
   326 by (blast_tac (claset() addDs [add_leD1, add_leD2]) 1);
   327 bind_thm ("add_leE", result() RS conjE);
   328 
   329 (*needs !!k for add_ac to work*)
   330 Goal "!!k:: nat. [| k<l;  m+l = k+n |] ==> m<n";
   331 by (force_tac (claset(),
   332               simpset() delsimps [add_Suc_right]
   333                         addsimps [less_iff_Suc_add,
   334                                   add_Suc_right RS sym] @ add_ac) 1);
   335 qed "less_add_eq_less";
   336 
   337 
   338 (*** Monotonicity of Addition ***)
   339 
   340 (*strict, in 1st argument*)
   341 Goal "i < j ==> i + k < j + (k::nat)";
   342 by (induct_tac "k" 1);
   343 by (ALLGOALS Asm_simp_tac);
   344 qed "add_less_mono1";
   345 
   346 (*strict, in both arguments*)
   347 Goal "[|i < j; k < l|] ==> i + k < j + (l::nat)";
   348 by (rtac (add_less_mono1 RS less_trans) 1);
   349 by (REPEAT (assume_tac 1));
   350 by (induct_tac "j" 1);
   351 by (ALLGOALS Asm_simp_tac);
   352 qed "add_less_mono";
   353 
   354 (*A [clumsy] way of lifting < monotonicity to <= monotonicity *)
   355 val [lt_mono,le] = Goal
   356      "[| !!i j::nat. i<j ==> f(i) < f(j);       \
   357 \        i <= j                                 \
   358 \     |] ==> f(i) <= (f(j)::nat)";
   359 by (cut_facts_tac [le] 1);
   360 by (asm_full_simp_tac (simpset() addsimps [order_le_less]) 1);
   361 by (blast_tac (claset() addSIs [lt_mono]) 1);
   362 qed "less_mono_imp_le_mono";
   363 
   364 (*non-strict, in 1st argument*)
   365 Goal "i<=j ==> i + k <= j + (k::nat)";
   366 by (res_inst_tac [("f", "%j. j+k")] less_mono_imp_le_mono 1);
   367 by (etac add_less_mono1 1);
   368 by (assume_tac 1);
   369 qed "add_le_mono1";
   370 
   371 (*non-strict, in both arguments*)
   372 Goal "[|i<=j;  k<=l |] ==> i + k <= j + (l::nat)";
   373 by (etac (add_le_mono1 RS le_trans) 1);
   374 by (simp_tac (simpset() addsimps [add_commute]) 1);
   375 qed "add_le_mono";
   376 
   377 
   378 (*** Multiplication ***)
   379 
   380 (*right annihilation in product*)
   381 Goal "!!m::nat. m * 0 = 0";
   382 by (induct_tac "m" 1);
   383 by (ALLGOALS Asm_simp_tac);
   384 qed "mult_0_right";
   385 
   386 (*right successor law for multiplication*)
   387 Goal  "m * Suc(n) = m + (m * n)";
   388 by (induct_tac "m" 1);
   389 by (ALLGOALS(asm_simp_tac (simpset() addsimps add_ac)));
   390 qed "mult_Suc_right";
   391 
   392 Addsimps [mult_0_right, mult_Suc_right];
   393 
   394 Goal "1 * n = n";
   395 by (Asm_simp_tac 1);
   396 qed "mult_1";
   397 
   398 Goal "n * 1 = n";
   399 by (Asm_simp_tac 1);
   400 qed "mult_1_right";
   401 
   402 (*Commutative law for multiplication*)
   403 Goal "m * n = n * (m::nat)";
   404 by (induct_tac "m" 1);
   405 by (ALLGOALS Asm_simp_tac);
   406 qed "mult_commute";
   407 
   408 (*addition distributes over multiplication*)
   409 Goal "(m + n)*k = (m*k) + ((n*k)::nat)";
   410 by (induct_tac "m" 1);
   411 by (ALLGOALS(asm_simp_tac (simpset() addsimps add_ac)));
   412 qed "add_mult_distrib";
   413 
   414 Goal "k*(m + n) = (k*m) + ((k*n)::nat)";
   415 by (induct_tac "m" 1);
   416 by (ALLGOALS(asm_simp_tac (simpset() addsimps add_ac)));
   417 qed "add_mult_distrib2";
   418 
   419 (*Associative law for multiplication*)
   420 Goal "(m * n) * k = m * ((n * k)::nat)";
   421 by (induct_tac "m" 1);
   422 by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_mult_distrib])));
   423 qed "mult_assoc";
   424 
   425 Goal "x*(y*z) = y*((x*z)::nat)";
   426 by (rtac trans 1);
   427 by (rtac mult_commute 1);
   428 by (rtac trans 1);
   429 by (rtac mult_assoc 1);
   430 by (rtac (mult_commute RS arg_cong) 1);
   431 qed "mult_left_commute";
   432 
   433 bind_thms ("mult_ac", [mult_assoc,mult_commute,mult_left_commute]);
   434 
   435 Goal "!!m::nat. (m*n = 0) = (m=0 | n=0)";
   436 by (induct_tac "m" 1);
   437 by (induct_tac "n" 2);
   438 by (ALLGOALS Asm_simp_tac);
   439 qed "mult_is_0";
   440 Addsimps [mult_is_0];
   441 
   442 
   443 (*** Difference ***)
   444 
   445 Goal "!!m::nat. m - m = 0";
   446 by (induct_tac "m" 1);
   447 by (ALLGOALS Asm_simp_tac);
   448 qed "diff_self_eq_0";
   449 
   450 Addsimps [diff_self_eq_0];
   451 
   452 (*Addition is the inverse of subtraction: if n<=m then n+(m-n) = m. *)
   453 Goal "~ m<n --> n+(m-n) = (m::nat)";
   454 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   455 by (ALLGOALS Asm_simp_tac);
   456 qed_spec_mp "add_diff_inverse";
   457 
   458 Goal "n<=m ==> n+(m-n) = (m::nat)";
   459 by (asm_simp_tac (simpset() addsimps [add_diff_inverse, not_less_iff_le]) 1);
   460 qed "le_add_diff_inverse";
   461 
   462 Goal "n<=m ==> (m-n)+n = (m::nat)";
   463 by (asm_simp_tac (simpset() addsimps [le_add_diff_inverse, add_commute]) 1);
   464 qed "le_add_diff_inverse2";
   465 
   466 Addsimps  [le_add_diff_inverse, le_add_diff_inverse2];
   467 
   468 
   469 (*** More results about difference ***)
   470 
   471 Goal "n <= m ==> Suc(m)-n = Suc(m-n)";
   472 by (etac rev_mp 1);
   473 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   474 by (ALLGOALS Asm_simp_tac);
   475 qed "Suc_diff_le";
   476 
   477 Goal "m - n < Suc(m)";
   478 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   479 by (etac less_SucE 3);
   480 by (ALLGOALS (asm_simp_tac (simpset() addsimps [less_Suc_eq])));
   481 qed "diff_less_Suc";
   482 
   483 Goal "m - n <= (m::nat)";
   484 by (res_inst_tac [("m","m"), ("n","n")] diff_induct 1);
   485 by (ALLGOALS (asm_simp_tac (simpset() addsimps [le_SucI])));
   486 qed "diff_le_self";
   487 Addsimps [diff_le_self];
   488 
   489 (* j<k ==> j-n < k *)
   490 bind_thm ("less_imp_diff_less", diff_le_self RS le_less_trans);
   491 
   492 Goal "!!i::nat. i-j-k = i - (j+k)";
   493 by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
   494 by (ALLGOALS Asm_simp_tac);
   495 qed "diff_diff_left";
   496 
   497 Goal "(Suc m - n) - Suc k = m - n - k";
   498 by (simp_tac (simpset() addsimps [diff_diff_left]) 1);
   499 qed "Suc_diff_diff";
   500 Addsimps [Suc_diff_diff];
   501 
   502 Goal "0<n ==> n - Suc i < n";
   503 by (case_tac "n" 1);
   504 by Safe_tac;
   505 by (asm_simp_tac (simpset() addsimps le_simps) 1);
   506 qed "diff_Suc_less";
   507 Addsimps [diff_Suc_less];
   508 
   509 (*This and the next few suggested by Florian Kammueller*)
   510 Goal "!!i::nat. i-j-k = i-k-j";
   511 by (simp_tac (simpset() addsimps [diff_diff_left, add_commute]) 1);
   512 qed "diff_commute";
   513 
   514 Goal "k <= (j::nat) --> (i + j) - k = i + (j - k)";
   515 by (res_inst_tac [("m","j"),("n","k")] diff_induct 1);
   516 by (ALLGOALS Asm_simp_tac);
   517 qed_spec_mp "diff_add_assoc";
   518 
   519 Goal "k <= (j::nat) --> (j + i) - k = (j - k) + i";
   520 by (asm_simp_tac (simpset() addsimps [add_commute, diff_add_assoc]) 1);
   521 qed_spec_mp "diff_add_assoc2";
   522 
   523 Goal "(n+m) - n = (m::nat)";
   524 by (induct_tac "n" 1);
   525 by (ALLGOALS Asm_simp_tac);
   526 qed "diff_add_inverse";
   527 
   528 Goal "(m+n) - n = (m::nat)";
   529 by (simp_tac (simpset() addsimps [diff_add_assoc]) 1);
   530 qed "diff_add_inverse2";
   531 
   532 Goal "i <= (j::nat) ==> (j-i=k) = (j=k+i)";
   533 by Safe_tac;
   534 by (ALLGOALS (asm_simp_tac (simpset() addsimps [diff_add_inverse2])));
   535 qed "le_imp_diff_is_add";
   536 
   537 Goal "!!m::nat. (m-n = 0) = (m <= n)";
   538 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   539 by (ALLGOALS Asm_simp_tac);
   540 qed "diff_is_0_eq";
   541 Addsimps [diff_is_0_eq];
   542 
   543 Goal "!!m::nat. (0<n-m) = (m<n)";
   544 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   545 by (ALLGOALS Asm_simp_tac);
   546 qed "zero_less_diff";
   547 Addsimps [zero_less_diff];
   548 
   549 Goal "i < j  ==> EX k::nat. 0<k & i+k = j";
   550 by (res_inst_tac [("x","j - i")] exI 1);
   551 by (asm_simp_tac (simpset() addsimps [add_diff_inverse, less_not_sym]) 1);
   552 qed "less_imp_add_positive";
   553 
   554 Goal "P(k) --> (ALL n. P(Suc(n))--> P(n)) --> P(k-i)";
   555 by (res_inst_tac [("m","k"),("n","i")] diff_induct 1);
   556 by (ALLGOALS (Clarify_tac THEN' Simp_tac THEN' TRY o Blast_tac));
   557 qed "zero_induct_lemma";
   558 
   559 val prems = Goal "[| P(k);  !!n. P(Suc(n)) ==> P(n) |] ==> P(0)";
   560 by (rtac (diff_self_eq_0 RS subst) 1);
   561 by (rtac (zero_induct_lemma RS mp RS mp) 1);
   562 by (REPEAT (ares_tac ([impI,allI]@prems) 1));
   563 qed "zero_induct";
   564 
   565 Goal "(k+m) - (k+n) = m - (n::nat)";
   566 by (induct_tac "k" 1);
   567 by (ALLGOALS Asm_simp_tac);
   568 qed "diff_cancel";
   569 
   570 Goal "(m+k) - (n+k) = m - (n::nat)";
   571 by (asm_simp_tac
   572     (simpset() addsimps [diff_cancel, inst "n" "k" add_commute]) 1);
   573 qed "diff_cancel2";
   574 
   575 Goal "n - (n+m) = (0::nat)";
   576 by (induct_tac "n" 1);
   577 by (ALLGOALS Asm_simp_tac);
   578 qed "diff_add_0";
   579 
   580 
   581 (** Difference distributes over multiplication **)
   582 
   583 Goal "!!m::nat. (m - n) * k = (m * k) - (n * k)";
   584 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   585 by (ALLGOALS (asm_simp_tac (simpset() addsimps [diff_cancel])));
   586 qed "diff_mult_distrib" ;
   587 
   588 Goal "!!m::nat. k * (m - n) = (k * m) - (k * n)";
   589 val mult_commute_k = read_instantiate [("m","k")] mult_commute;
   590 by (simp_tac (simpset() addsimps [diff_mult_distrib, mult_commute_k]) 1);
   591 qed "diff_mult_distrib2" ;
   592 (*NOT added as rewrites, since sometimes they are used from right-to-left*)
   593 
   594 bind_thms ("nat_distrib",
   595   [add_mult_distrib, add_mult_distrib2, diff_mult_distrib, diff_mult_distrib2]);
   596 
   597 
   598 (*** Monotonicity of Multiplication ***)
   599 
   600 Goal "i <= (j::nat) ==> i*k<=j*k";
   601 by (induct_tac "k" 1);
   602 by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_le_mono])));
   603 qed "mult_le_mono1";
   604 
   605 Goal "i <= (j::nat) ==> k*i <= k*j";
   606 by (dtac mult_le_mono1 1);
   607 by (asm_simp_tac (simpset() addsimps [mult_commute]) 1);
   608 qed "mult_le_mono2";
   609 
   610 (* <= monotonicity, BOTH arguments*)
   611 Goal "[| i <= (j::nat); k <= l |] ==> i*k <= j*l";
   612 by (etac (mult_le_mono1 RS le_trans) 1);
   613 by (etac mult_le_mono2 1);
   614 qed "mult_le_mono";
   615 
   616 (*strict, in 1st argument; proof is by induction on k>0*)
   617 Goal "!!i::nat. [| i<j; 0<k |] ==> k*i < k*j";
   618 by (eres_inst_tac [("m1","0")] (less_imp_Suc_add RS exE) 1);
   619 by (Asm_simp_tac 1);
   620 by (induct_tac "x" 1);
   621 by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_less_mono])));
   622 qed "mult_less_mono2";
   623 
   624 Goal "!!i::nat. [| i<j; 0<k |] ==> i*k < j*k";
   625 by (dtac mult_less_mono2 1);
   626 by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [mult_commute])));
   627 qed "mult_less_mono1";
   628 
   629 Goal "!!m::nat. (0 < m*n) = (0<m & 0<n)";
   630 by (induct_tac "m" 1);
   631 by (case_tac "n" 2);
   632 by (ALLGOALS Asm_simp_tac);
   633 qed "zero_less_mult_iff";
   634 Addsimps [zero_less_mult_iff];
   635 
   636 Goal "(1 <= m*n) = (1<=m & 1<=n)";
   637 by (induct_tac "m" 1);
   638 by (case_tac "n" 2);
   639 by (ALLGOALS Asm_simp_tac);
   640 qed "one_le_mult_iff";
   641 Addsimps [one_le_mult_iff];
   642 
   643 Goal "(m*n = 1) = (m=1 & n=1)";
   644 by (induct_tac "m" 1);
   645 by (Simp_tac 1);
   646 by (induct_tac "n" 1);
   647 by (Simp_tac 1);
   648 by (fast_tac (claset() addss simpset()) 1);
   649 qed "mult_eq_1_iff";
   650 Addsimps [mult_eq_1_iff];
   651 
   652 Goal "!!m::nat. (m*k < n*k) = (0<k & m<n)";
   653 by (safe_tac (claset() addSIs [mult_less_mono1]));
   654 by (case_tac "k" 1);
   655 by Auto_tac;  
   656 by (full_simp_tac (simpset() delsimps [le_0_eq]
   657 			     addsimps [linorder_not_le RS sym]) 1);
   658 by (blast_tac (claset() addIs [mult_le_mono1]) 1); 
   659 qed "mult_less_cancel2";
   660 
   661 Goal "!!m::nat. (k*m < k*n) = (0<k & m<n)";
   662 by (simp_tac (simpset() addsimps [mult_less_cancel2, 
   663                                   inst "m" "k" mult_commute]) 1);
   664 qed "mult_less_cancel1";
   665 Addsimps [mult_less_cancel1, mult_less_cancel2];
   666 
   667 Goal "!!m::nat. (m*k <= n*k) = (0<k --> m<=n)";
   668 by (simp_tac (simpset() addsimps [linorder_not_less RS sym]) 1);
   669 by Auto_tac;  
   670 qed "mult_le_cancel2";
   671 
   672 Goal "!!m::nat. (k*m <= k*n) = (0<k --> m<=n)";
   673 by (simp_tac (simpset() addsimps [linorder_not_less RS sym]) 1);
   674 by Auto_tac;  
   675 qed "mult_le_cancel1";
   676 Addsimps [mult_le_cancel1, mult_le_cancel2];
   677 
   678 Goal "(m*k = n*k) = (m=n | (k = (0::nat)))";
   679 by (cut_facts_tac [less_linear] 1);
   680 by Safe_tac;
   681 by Auto_tac; 	
   682 by (ALLGOALS (dtac mult_less_mono1 THEN' assume_tac));
   683 by (ALLGOALS Asm_full_simp_tac);
   684 qed "mult_cancel2";
   685 
   686 Goal "(k*m = k*n) = (m=n | (k = (0::nat)))";
   687 by (simp_tac (simpset() addsimps [mult_cancel2, inst "m" "k" mult_commute]) 1);
   688 qed "mult_cancel1";
   689 Addsimps [mult_cancel1, mult_cancel2];
   690 
   691 Goal "(Suc k * m < Suc k * n) = (m < n)";
   692 by (stac mult_less_cancel1 1);
   693 by (Simp_tac 1);
   694 qed "Suc_mult_less_cancel1";
   695 
   696 Goal "(Suc k * m <= Suc k * n) = (m <= n)";
   697 by (stac mult_le_cancel1 1);
   698 by (Simp_tac 1);
   699 qed "Suc_mult_le_cancel1";
   700 
   701 Goal "(Suc k * m = Suc k * n) = (m = n)";
   702 by (stac mult_cancel1 1);
   703 by (Simp_tac 1);
   704 qed "Suc_mult_cancel1";
   705 
   706 
   707 (*Lemma for gcd*)
   708 Goal "!!m::nat. m = m*n ==> n=1 | m=0";
   709 by (dtac sym 1);
   710 by (rtac disjCI 1);
   711 by (rtac nat_less_cases 1 THEN assume_tac 2);
   712 by (fast_tac (claset() addSEs [less_SucE] addss simpset()) 1);
   713 by (best_tac (claset() addDs [mult_less_mono2] addss simpset()) 1);
   714 qed "mult_eq_self_implies_10";