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