src/HOL/Arith.ML
author nipkow
Wed Dec 03 17:25:43 1997 +0100 (1997-12-03)
changeset 4356 0dfd34f0d33d
parent 4297 5defc2105cc8
child 4360 40e5c97e988d
permissions -rw-r--r--
Replaced n ~= 0 by 0 < n
     1 (*  Title:      HOL/Arith.ML
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1993  University of Cambridge
     5 
     6 Proofs about elementary arithmetic: addition, multiplication, etc.
     7 Some from the Hoare example from Norbert Galm
     8 *)
     9 
    10 (*** Basic rewrite rules for the arithmetic operators ***)
    11 
    12 goalw Arith.thy [pred_def] "pred 0 = 0";
    13 by (Simp_tac 1);
    14 qed "pred_0";
    15 
    16 goalw Arith.thy [pred_def] "pred(Suc n) = n";
    17 by (Simp_tac 1);
    18 qed "pred_Suc";
    19 
    20 Addsimps [pred_0,pred_Suc];
    21 
    22 (** pred **)
    23 
    24 val prems = goal Arith.thy "n ~= 0 ==> Suc(pred n) = n";
    25 by (res_inst_tac [("n","n")] natE 1);
    26 by (cut_facts_tac prems 1);
    27 by (ALLGOALS Asm_full_simp_tac);
    28 qed "Suc_pred";
    29 Addsimps [Suc_pred];
    30 
    31 goal Arith.thy "pred(n) <= (n::nat)";
    32 by (res_inst_tac [("n","n")] natE 1);
    33 by (ALLGOALS Asm_simp_tac);
    34 qed "pred_le";
    35 AddIffs [pred_le];
    36 
    37 goalw Arith.thy [pred_def] "m<=n --> pred(m) <= pred(n)";
    38 by (simp_tac (simpset() addsplits [expand_nat_case]) 1);
    39 qed_spec_mp "pred_le_mono";
    40 
    41 goal Arith.thy "!!n. n ~= 0 ==> pred n < n";
    42 by (exhaust_tac "n" 1);
    43 by (ALLGOALS Asm_full_simp_tac);
    44 qed "pred_less";
    45 Addsimps [pred_less];
    46 
    47 (** Difference **)
    48 
    49 qed_goalw "diff_0_eq_0" Arith.thy [pred_def]
    50     "0 - n = 0"
    51  (fn _ => [induct_tac "n" 1,  ALLGOALS Asm_simp_tac]);
    52 
    53 (*Must simplify BEFORE the induction!!  (Else we get a critical pair)
    54   Suc(m) - Suc(n)   rewrites to   pred(Suc(m) - n)  *)
    55 qed_goalw "diff_Suc_Suc" Arith.thy [pred_def]
    56     "Suc(m) - Suc(n) = m - n"
    57  (fn _ =>
    58   [Simp_tac 1, induct_tac "n" 1, ALLGOALS Asm_simp_tac]);
    59 
    60 Addsimps [diff_0_eq_0, diff_Suc_Suc];
    61 
    62 
    63 (**** Inductive properties of the operators ****)
    64 
    65 (*** Addition ***)
    66 
    67 qed_goal "add_0_right" Arith.thy "m + 0 = m"
    68  (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
    69 
    70 qed_goal "add_Suc_right" Arith.thy "m + Suc(n) = Suc(m+n)"
    71  (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
    72 
    73 Addsimps [add_0_right,add_Suc_right];
    74 
    75 (*Associative law for addition*)
    76 qed_goal "add_assoc" Arith.thy "(m + n) + k = m + ((n + k)::nat)"
    77  (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
    78 
    79 (*Commutative law for addition*)  
    80 qed_goal "add_commute" Arith.thy "m + n = n + (m::nat)"
    81  (fn _ =>  [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
    82 
    83 qed_goal "add_left_commute" Arith.thy "x+(y+z)=y+((x+z)::nat)"
    84  (fn _ => [rtac (add_commute RS trans) 1, rtac (add_assoc RS trans) 1,
    85            rtac (add_commute RS arg_cong) 1]);
    86 
    87 (*Addition is an AC-operator*)
    88 val add_ac = [add_assoc, add_commute, add_left_commute];
    89 
    90 goal Arith.thy "!!k::nat. (k + m = k + n) = (m=n)";
    91 by (induct_tac "k" 1);
    92 by (Simp_tac 1);
    93 by (Asm_simp_tac 1);
    94 qed "add_left_cancel";
    95 
    96 goal Arith.thy "!!k::nat. (m + k = n + k) = (m=n)";
    97 by (induct_tac "k" 1);
    98 by (Simp_tac 1);
    99 by (Asm_simp_tac 1);
   100 qed "add_right_cancel";
   101 
   102 goal Arith.thy "!!k::nat. (k + m <= k + n) = (m<=n)";
   103 by (induct_tac "k" 1);
   104 by (Simp_tac 1);
   105 by (Asm_simp_tac 1);
   106 qed "add_left_cancel_le";
   107 
   108 goal Arith.thy "!!k::nat. (k + m < k + n) = (m<n)";
   109 by (induct_tac "k" 1);
   110 by (Simp_tac 1);
   111 by (Asm_simp_tac 1);
   112 qed "add_left_cancel_less";
   113 
   114 Addsimps [add_left_cancel, add_right_cancel,
   115           add_left_cancel_le, add_left_cancel_less];
   116 
   117 (** Reasoning about m+0=0, etc. **)
   118 
   119 goal Arith.thy "(m+n = 0) = (m=0 & n=0)";
   120 by (induct_tac "m" 1);
   121 by (ALLGOALS Asm_simp_tac);
   122 qed "add_is_0";
   123 Addsimps [add_is_0];
   124 
   125 goal Arith.thy "(pred (m+n) = 0) = (m=0 & pred n = 0 | pred m = 0 & n=0)";
   126 by (induct_tac "m" 1);
   127 by (ALLGOALS (fast_tac (claset() addss (simpset()))));
   128 qed "pred_add_is_0";
   129 Addsimps [pred_add_is_0];
   130 
   131 goal Arith.thy "!!n. n ~= 0 ==> m + pred n = pred(m+n)";
   132 by (induct_tac "m" 1);
   133 by (ALLGOALS Asm_simp_tac);
   134 qed "add_pred";
   135 Addsimps [add_pred];
   136 
   137 
   138 (**** Additional theorems about "less than" ****)
   139 
   140 goal Arith.thy "i<j --> (EX k. j = Suc(i+k))";
   141 by (induct_tac "j" 1);
   142 by (Simp_tac 1);
   143 by (blast_tac (claset() addSEs [less_SucE] 
   144                        addSIs [add_0_right RS sym, add_Suc_right RS sym]) 1);
   145 val lemma = result();
   146 
   147 (* [| i<j;  !!x. j = Suc(i+x) ==> Q |] ==> Q *)
   148 bind_thm ("less_natE", lemma RS mp RS exE);
   149 
   150 goal Arith.thy "!!m. m<n --> (? k. n=Suc(m+k))";
   151 by (induct_tac "n" 1);
   152 by (ALLGOALS (simp_tac (simpset() addsimps [less_Suc_eq])));
   153 by (blast_tac (claset() addSEs [less_SucE] 
   154                        addSIs [add_0_right RS sym, add_Suc_right RS sym]) 1);
   155 qed_spec_mp "less_eq_Suc_add";
   156 
   157 goal Arith.thy "n <= ((m + n)::nat)";
   158 by (induct_tac "m" 1);
   159 by (ALLGOALS Simp_tac);
   160 by (etac le_trans 1);
   161 by (rtac (lessI RS less_imp_le) 1);
   162 qed "le_add2";
   163 
   164 goal Arith.thy "n <= ((n + m)::nat)";
   165 by (simp_tac (simpset() addsimps add_ac) 1);
   166 by (rtac le_add2 1);
   167 qed "le_add1";
   168 
   169 bind_thm ("less_add_Suc1", (lessI RS (le_add1 RS le_less_trans)));
   170 bind_thm ("less_add_Suc2", (lessI RS (le_add2 RS le_less_trans)));
   171 
   172 (*"i <= j ==> i <= j+m"*)
   173 bind_thm ("trans_le_add1", le_add1 RSN (2,le_trans));
   174 
   175 (*"i <= j ==> i <= m+j"*)
   176 bind_thm ("trans_le_add2", le_add2 RSN (2,le_trans));
   177 
   178 (*"i < j ==> i < j+m"*)
   179 bind_thm ("trans_less_add1", le_add1 RSN (2,less_le_trans));
   180 
   181 (*"i < j ==> i < m+j"*)
   182 bind_thm ("trans_less_add2", le_add2 RSN (2,less_le_trans));
   183 
   184 goal Arith.thy "!!i. i+j < (k::nat) ==> i<k";
   185 by (etac rev_mp 1);
   186 by (induct_tac "j" 1);
   187 by (ALLGOALS Asm_simp_tac);
   188 by (blast_tac (claset() addDs [Suc_lessD]) 1);
   189 qed "add_lessD1";
   190 
   191 goal Arith.thy "!!i::nat. ~ (i+j < i)";
   192 by (rtac notI 1);
   193 by (etac (add_lessD1 RS less_irrefl) 1);
   194 qed "not_add_less1";
   195 
   196 goal Arith.thy "!!i::nat. ~ (j+i < i)";
   197 by (simp_tac (simpset() addsimps [add_commute, not_add_less1]) 1);
   198 qed "not_add_less2";
   199 AddIffs [not_add_less1, not_add_less2];
   200 
   201 goal Arith.thy "!!k::nat. m <= n ==> m <= n+k";
   202 by (etac le_trans 1);
   203 by (rtac le_add1 1);
   204 qed "le_imp_add_le";
   205 
   206 goal Arith.thy "!!k::nat. m < n ==> m < n+k";
   207 by (etac less_le_trans 1);
   208 by (rtac le_add1 1);
   209 qed "less_imp_add_less";
   210 
   211 goal Arith.thy "m+k<=n --> m<=(n::nat)";
   212 by (induct_tac "k" 1);
   213 by (ALLGOALS Asm_simp_tac);
   214 by (blast_tac (claset() addDs [Suc_leD]) 1);
   215 qed_spec_mp "add_leD1";
   216 
   217 goal Arith.thy "!!n::nat. m+k<=n ==> k<=n";
   218 by (full_simp_tac (simpset() addsimps [add_commute]) 1);
   219 by (etac add_leD1 1);
   220 qed_spec_mp "add_leD2";
   221 
   222 goal Arith.thy "!!n::nat. m+k<=n ==> m<=n & k<=n";
   223 by (blast_tac (claset() addDs [add_leD1, add_leD2]) 1);
   224 bind_thm ("add_leE", result() RS conjE);
   225 
   226 goal Arith.thy "!!k l::nat. [| k<l; m+l = k+n |] ==> m<n";
   227 by (safe_tac (claset() addSDs [less_eq_Suc_add]));
   228 by (asm_full_simp_tac
   229     (simpset() delsimps [add_Suc_right]
   230                 addsimps ([add_Suc_right RS sym, add_left_cancel] @add_ac)) 1);
   231 by (etac subst 1);
   232 by (simp_tac (simpset() addsimps [less_add_Suc1]) 1);
   233 qed "less_add_eq_less";
   234 
   235 
   236 (*** Monotonicity of Addition ***)
   237 
   238 (*strict, in 1st argument*)
   239 goal Arith.thy "!!i j k::nat. i < j ==> i + k < j + k";
   240 by (induct_tac "k" 1);
   241 by (ALLGOALS Asm_simp_tac);
   242 qed "add_less_mono1";
   243 
   244 (*strict, in both arguments*)
   245 goal Arith.thy "!!i j k::nat. [|i < j; k < l|] ==> i + k < j + l";
   246 by (rtac (add_less_mono1 RS less_trans) 1);
   247 by (REPEAT (assume_tac 1));
   248 by (induct_tac "j" 1);
   249 by (ALLGOALS Asm_simp_tac);
   250 qed "add_less_mono";
   251 
   252 (*A [clumsy] way of lifting < monotonicity to <= monotonicity *)
   253 val [lt_mono,le] = goal Arith.thy
   254      "[| !!i j::nat. i<j ==> f(i) < f(j);       \
   255 \        i <= j                                 \
   256 \     |] ==> f(i) <= (f(j)::nat)";
   257 by (cut_facts_tac [le] 1);
   258 by (asm_full_simp_tac (simpset() addsimps [le_eq_less_or_eq]) 1);
   259 by (blast_tac (claset() addSIs [lt_mono]) 1);
   260 qed "less_mono_imp_le_mono";
   261 
   262 (*non-strict, in 1st argument*)
   263 goal Arith.thy "!!i j k::nat. i<=j ==> i + k <= j + k";
   264 by (res_inst_tac [("f", "%j. j+k")] less_mono_imp_le_mono 1);
   265 by (etac add_less_mono1 1);
   266 by (assume_tac 1);
   267 qed "add_le_mono1";
   268 
   269 (*non-strict, in both arguments*)
   270 goal Arith.thy "!!k l::nat. [|i<=j;  k<=l |] ==> i + k <= j + l";
   271 by (etac (add_le_mono1 RS le_trans) 1);
   272 by (simp_tac (simpset() addsimps [add_commute]) 1);
   273 (*j moves to the end because it is free while k, l are bound*)
   274 by (etac add_le_mono1 1);
   275 qed "add_le_mono";
   276 
   277 
   278 (*** Multiplication ***)
   279 
   280 (*right annihilation in product*)
   281 qed_goal "mult_0_right" Arith.thy "m * 0 = 0"
   282  (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
   283 
   284 (*right successor law for multiplication*)
   285 qed_goal "mult_Suc_right" Arith.thy  "m * Suc(n) = m + (m * n)"
   286  (fn _ => [induct_tac "m" 1,
   287            ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))]);
   288 
   289 Addsimps [mult_0_right, mult_Suc_right];
   290 
   291 goal Arith.thy "1 * n = n";
   292 by (Asm_simp_tac 1);
   293 qed "mult_1";
   294 
   295 goal Arith.thy "n * 1 = n";
   296 by (Asm_simp_tac 1);
   297 qed "mult_1_right";
   298 
   299 (*Commutative law for multiplication*)
   300 qed_goal "mult_commute" Arith.thy "m * n = n * (m::nat)"
   301  (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
   302 
   303 (*addition distributes over multiplication*)
   304 qed_goal "add_mult_distrib" Arith.thy "(m + n)*k = (m*k) + ((n*k)::nat)"
   305  (fn _ => [induct_tac "m" 1,
   306            ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))]);
   307 
   308 qed_goal "add_mult_distrib2" Arith.thy "k*(m + n) = (k*m) + ((k*n)::nat)"
   309  (fn _ => [induct_tac "m" 1,
   310            ALLGOALS(asm_simp_tac (simpset() addsimps add_ac))]);
   311 
   312 (*Associative law for multiplication*)
   313 qed_goal "mult_assoc" Arith.thy "(m * n) * k = m * ((n * k)::nat)"
   314   (fn _ => [induct_tac "m" 1, 
   315             ALLGOALS (asm_simp_tac (simpset() addsimps [add_mult_distrib]))]);
   316 
   317 qed_goal "mult_left_commute" Arith.thy "x*(y*z) = y*((x*z)::nat)"
   318  (fn _ => [rtac trans 1, rtac mult_commute 1, rtac trans 1,
   319            rtac mult_assoc 1, rtac (mult_commute RS arg_cong) 1]);
   320 
   321 val mult_ac = [mult_assoc,mult_commute,mult_left_commute];
   322 
   323 goal Arith.thy "(m*n = 0) = (m=0 | n=0)";
   324 by (induct_tac "m" 1);
   325 by (induct_tac "n" 2);
   326 by (ALLGOALS Asm_simp_tac);
   327 qed "mult_is_0";
   328 Addsimps [mult_is_0];
   329 
   330 goal Arith.thy "!!m::nat. m <= m*m";
   331 by (induct_tac "m" 1);
   332 by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_assoc RS sym])));
   333 by (etac (le_add2 RSN (2,le_trans)) 1);
   334 qed "le_square";
   335 
   336 
   337 (*** Difference ***)
   338 
   339 qed_goal "pred_Suc_diff" Arith.thy "pred(Suc m - n) = m - n"
   340  (fn _ => [induct_tac "n" 1, ALLGOALS Asm_simp_tac]);
   341 Addsimps [pred_Suc_diff];
   342 
   343 qed_goal "diff_self_eq_0" Arith.thy "m - m = 0"
   344  (fn _ => [induct_tac "m" 1, ALLGOALS Asm_simp_tac]);
   345 Addsimps [diff_self_eq_0];
   346 
   347 (*Addition is the inverse of subtraction: if n<=m then n+(m-n) = m. *)
   348 goal Arith.thy "~ m<n --> n+(m-n) = (m::nat)";
   349 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   350 by (ALLGOALS Asm_simp_tac);
   351 qed_spec_mp "add_diff_inverse";
   352 
   353 goal Arith.thy "!!m. n<=m ==> n+(m-n) = (m::nat)";
   354 by (asm_simp_tac (simpset() addsimps [add_diff_inverse, not_less_iff_le]) 1);
   355 qed "le_add_diff_inverse";
   356 
   357 goal Arith.thy "!!m. n<=m ==> (m-n)+n = (m::nat)";
   358 by (asm_simp_tac (simpset() addsimps [le_add_diff_inverse, add_commute]) 1);
   359 qed "le_add_diff_inverse2";
   360 
   361 Addsimps  [le_add_diff_inverse, le_add_diff_inverse2];
   362 Delsimps  [diff_Suc];
   363 
   364 
   365 (*** More results about difference ***)
   366 
   367 val [prem] = goal Arith.thy "n < Suc(m) ==> Suc(m)-n = Suc(m-n)";
   368 by (rtac (prem RS rev_mp) 1);
   369 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   370 by (ALLGOALS Asm_simp_tac);
   371 qed "Suc_diff_n";
   372 
   373 goal Arith.thy "m - n < Suc(m)";
   374 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   375 by (etac less_SucE 3);
   376 by (ALLGOALS (asm_simp_tac (simpset() addsimps [less_Suc_eq])));
   377 qed "diff_less_Suc";
   378 
   379 goal Arith.thy "!!m::nat. m - n <= m";
   380 by (res_inst_tac [("m","m"), ("n","n")] diff_induct 1);
   381 by (ALLGOALS Asm_simp_tac);
   382 qed "diff_le_self";
   383 Addsimps [diff_le_self];
   384 
   385 goal Arith.thy "!!i::nat. i-j-k = i - (j+k)";
   386 by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
   387 by (ALLGOALS Asm_simp_tac);
   388 qed "diff_diff_left";
   389 
   390 (*This and the next few suggested by Florian Kammueller*)
   391 goal Arith.thy "!!i::nat. i-j-k = i-k-j";
   392 by (simp_tac (simpset() addsimps [diff_diff_left, add_commute]) 1);
   393 qed "diff_commute";
   394 
   395 goal Arith.thy "!!i j k:: nat. k<=j --> j<=i --> i - (j - k) = i - j + k";
   396 by (res_inst_tac [("m","i"),("n","j")] diff_induct 1);
   397 by (ALLGOALS Asm_simp_tac);
   398 by (asm_simp_tac
   399     (simpset() addsimps [Suc_diff_n, le_imp_less_Suc, le_Suc_eq]) 1);
   400 qed_spec_mp "diff_diff_right";
   401 
   402 goal Arith.thy "!!i j k:: nat. k<=j --> (i + j) - k = i + (j - k)";
   403 by (res_inst_tac [("m","j"),("n","k")] diff_induct 1);
   404 by (ALLGOALS Asm_simp_tac);
   405 qed_spec_mp "diff_add_assoc";
   406 
   407 goal Arith.thy "!!n::nat. (n+m) - n = m";
   408 by (induct_tac "n" 1);
   409 by (ALLGOALS Asm_simp_tac);
   410 qed "diff_add_inverse";
   411 Addsimps [diff_add_inverse];
   412 
   413 goal Arith.thy "!!n::nat.(m+n) - n = m";
   414 by (simp_tac (simpset() addsimps [diff_add_assoc]) 1);
   415 qed "diff_add_inverse2";
   416 Addsimps [diff_add_inverse2];
   417 
   418 goal Arith.thy "!!i j k::nat. i<=j ==> (j-i=k) = (j=k+i)";
   419 by Safe_tac;
   420 by (ALLGOALS Asm_simp_tac);
   421 qed "le_imp_diff_is_add";
   422 
   423 val [prem] = goal Arith.thy "m < Suc(n) ==> m-n = 0";
   424 by (rtac (prem RS rev_mp) 1);
   425 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   426 by (asm_simp_tac (simpset() addsimps [less_Suc_eq]) 1);
   427 by (ALLGOALS Asm_simp_tac);
   428 qed "less_imp_diff_is_0";
   429 
   430 val prems = goal Arith.thy "m-n = 0  -->  n-m = 0  -->  m=n";
   431 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   432 by (REPEAT(Simp_tac 1 THEN TRY(atac 1)));
   433 qed_spec_mp "diffs0_imp_equal";
   434 
   435 val [prem] = goal Arith.thy "m<n ==> 0<n-m";
   436 by (rtac (prem RS rev_mp) 1);
   437 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   438 by (ALLGOALS Asm_simp_tac);
   439 qed "less_imp_diff_positive";
   440 
   441 goal Arith.thy "Suc(m)-n = (if m<n then 0 else Suc(m-n))";
   442 by (simp_tac (simpset() addsimps [less_imp_diff_is_0, not_less_eq, Suc_diff_n]
   443                        addsplits [expand_if]) 1);
   444 qed "if_Suc_diff_n";
   445 
   446 goal Arith.thy "P(k) --> (!n. P(Suc(n))--> P(n)) --> P(k-i)";
   447 by (res_inst_tac [("m","k"),("n","i")] diff_induct 1);
   448 by (ALLGOALS (Clarify_tac THEN' Simp_tac THEN' TRY o Blast_tac));
   449 qed "zero_induct_lemma";
   450 
   451 val prems = goal Arith.thy "[| P(k);  !!n. P(Suc(n)) ==> P(n) |] ==> P(0)";
   452 by (rtac (diff_self_eq_0 RS subst) 1);
   453 by (rtac (zero_induct_lemma RS mp RS mp) 1);
   454 by (REPEAT (ares_tac ([impI,allI]@prems) 1));
   455 qed "zero_induct";
   456 
   457 goal Arith.thy "!!k::nat. (k+m) - (k+n) = m - n";
   458 by (induct_tac "k" 1);
   459 by (ALLGOALS Asm_simp_tac);
   460 qed "diff_cancel";
   461 Addsimps [diff_cancel];
   462 
   463 goal Arith.thy "!!m::nat. (m+k) - (n+k) = m - n";
   464 val add_commute_k = read_instantiate [("n","k")] add_commute;
   465 by (asm_simp_tac (simpset() addsimps ([add_commute_k])) 1);
   466 qed "diff_cancel2";
   467 Addsimps [diff_cancel2];
   468 
   469 (*From Clemens Ballarin*)
   470 goal Arith.thy "!!n::nat. [| k<=n; n<=m |] ==> (m-k) - (n-k) = m-n";
   471 by (subgoal_tac "k<=n --> n<=m --> (m-k) - (n-k) = m-n" 1);
   472 by (Asm_full_simp_tac 1);
   473 by (induct_tac "k" 1);
   474 by (Simp_tac 1);
   475 (* Induction step *)
   476 by (subgoal_tac "Suc na <= m --> n <= m --> Suc na <= n --> \
   477 \                Suc (m - Suc na) - Suc (n - Suc na) = m-n" 1);
   478 by (Asm_full_simp_tac 1);
   479 by (blast_tac (claset() addIs [le_trans]) 1);
   480 by (auto_tac (claset() addIs [Suc_leD], simpset() delsimps [diff_Suc_Suc]));
   481 by (asm_full_simp_tac (simpset() delsimps [Suc_less_eq] 
   482 		       addsimps [Suc_diff_n RS sym, le_eq_less_Suc]) 1);
   483 qed "diff_right_cancel";
   484 
   485 goal Arith.thy "!!n::nat. n - (n+m) = 0";
   486 by (induct_tac "n" 1);
   487 by (ALLGOALS Asm_simp_tac);
   488 qed "diff_add_0";
   489 Addsimps [diff_add_0];
   490 
   491 (** Difference distributes over multiplication **)
   492 
   493 goal Arith.thy "!!m::nat. (m - n) * k = (m * k) - (n * k)";
   494 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   495 by (ALLGOALS Asm_simp_tac);
   496 qed "diff_mult_distrib" ;
   497 
   498 goal Arith.thy "!!m::nat. k * (m - n) = (k * m) - (k * n)";
   499 val mult_commute_k = read_instantiate [("m","k")] mult_commute;
   500 by (simp_tac (simpset() addsimps [diff_mult_distrib, mult_commute_k]) 1);
   501 qed "diff_mult_distrib2" ;
   502 (*NOT added as rewrites, since sometimes they are used from right-to-left*)
   503 
   504 
   505 (*** Monotonicity of Multiplication ***)
   506 
   507 goal Arith.thy "!!i::nat. i<=j ==> i*k<=j*k";
   508 by (induct_tac "k" 1);
   509 by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_le_mono])));
   510 qed "mult_le_mono1";
   511 
   512 (*<=monotonicity, BOTH arguments*)
   513 goal Arith.thy "!!i::nat. [| i<=j; k<=l |] ==> i*k<=j*l";
   514 by (etac (mult_le_mono1 RS le_trans) 1);
   515 by (rtac le_trans 1);
   516 by (stac mult_commute 2);
   517 by (etac mult_le_mono1 2);
   518 by (simp_tac (simpset() addsimps [mult_commute]) 1);
   519 qed "mult_le_mono";
   520 
   521 (*strict, in 1st argument; proof is by induction on k>0*)
   522 goal Arith.thy "!!i::nat. [| i<j; 0<k |] ==> k*i < k*j";
   523 by (eres_inst_tac [("i","0")] less_natE 1);
   524 by (Asm_simp_tac 1);
   525 by (induct_tac "x" 1);
   526 by (ALLGOALS (asm_simp_tac (simpset() addsimps [add_less_mono])));
   527 qed "mult_less_mono2";
   528 
   529 goal Arith.thy "!!i::nat. [| i<j; 0<k |] ==> i*k < j*k";
   530 by (dtac mult_less_mono2 1);
   531 by (ALLGOALS (asm_full_simp_tac (simpset() addsimps [mult_commute])));
   532 qed "mult_less_mono1";
   533 
   534 goal Arith.thy "(0 < m*n) = (0<m & 0<n)";
   535 by (induct_tac "m" 1);
   536 by (induct_tac "n" 2);
   537 by (ALLGOALS Asm_simp_tac);
   538 qed "zero_less_mult_iff";
   539 Addsimps [zero_less_mult_iff];
   540 
   541 goal Arith.thy "(m*n = 1) = (m=1 & n=1)";
   542 by (induct_tac "m" 1);
   543 by (Simp_tac 1);
   544 by (induct_tac "n" 1);
   545 by (Simp_tac 1);
   546 by (fast_tac (claset() addss simpset()) 1);
   547 qed "mult_eq_1_iff";
   548 Addsimps [mult_eq_1_iff];
   549 
   550 goal Arith.thy "!!k. 0<k ==> (m*k < n*k) = (m<n)";
   551 by (safe_tac (claset() addSIs [mult_less_mono1]));
   552 by (cut_facts_tac [less_linear] 1);
   553 by (blast_tac (claset() addDs [mult_less_mono1] addEs [less_asym]) 1);
   554 qed "mult_less_cancel2";
   555 
   556 goal Arith.thy "!!k. 0<k ==> (k*m < k*n) = (m<n)";
   557 by (dtac mult_less_cancel2 1);
   558 by (asm_full_simp_tac (simpset() addsimps [mult_commute]) 1);
   559 qed "mult_less_cancel1";
   560 Addsimps [mult_less_cancel1, mult_less_cancel2];
   561 
   562 goal Arith.thy "(Suc k * m < Suc k * n) = (m < n)";
   563 br mult_less_cancel1 1;
   564 by (Simp_tac 1);
   565 qed "Suc_mult_less_cancel1";
   566 
   567 goalw Arith.thy [le_def] "(Suc k * m <= Suc k * n) = (m <= n)";
   568 by (simp_tac (simpset_of HOL.thy) 1);
   569 br Suc_mult_less_cancel1 1;
   570 qed "Suc_mult_le_cancel1";
   571 
   572 goal Arith.thy "!!k. 0<k ==> (m*k = n*k) = (m=n)";
   573 by (cut_facts_tac [less_linear] 1);
   574 by Safe_tac;
   575 by (assume_tac 2);
   576 by (ALLGOALS (dtac mult_less_mono1 THEN' assume_tac));
   577 by (ALLGOALS Asm_full_simp_tac);
   578 qed "mult_cancel2";
   579 
   580 goal Arith.thy "!!k. 0<k ==> (k*m = k*n) = (m=n)";
   581 by (dtac mult_cancel2 1);
   582 by (asm_full_simp_tac (simpset() addsimps [mult_commute]) 1);
   583 qed "mult_cancel1";
   584 Addsimps [mult_cancel1, mult_cancel2];
   585 
   586 goal Arith.thy "(Suc k * m = Suc k * n) = (m = n)";
   587 br mult_cancel1 1;
   588 by (Simp_tac 1);
   589 qed "Suc_mult_cancel1";
   590 
   591 
   592 (** Lemma for gcd **)
   593 
   594 goal Arith.thy "!!m n. m = m*n ==> n=1 | m=0";
   595 by (dtac sym 1);
   596 by (rtac disjCI 1);
   597 by (rtac nat_less_cases 1 THEN assume_tac 2);
   598 by (fast_tac (claset() addSEs [less_SucE] addss simpset()) 1);
   599 by (best_tac (claset() addDs [mult_less_mono2] addss simpset()) 1);
   600 qed "mult_eq_self_implies_10";
   601 
   602 
   603 (*** Subtraction laws -- from Clemens Ballarin ***)
   604 
   605 goal Arith.thy "!! a b c::nat. [| a < b; c <= a |] ==> a-c < b-c";
   606 by (subgoal_tac "c+(a-c) < c+(b-c)" 1);
   607 by (Full_simp_tac 1);
   608 by (subgoal_tac "c <= b" 1);
   609 by (blast_tac (claset() addIs [less_imp_le, le_trans]) 2);
   610 by (Asm_simp_tac 1);
   611 qed "diff_less_mono";
   612 
   613 goal Arith.thy "!! a b c::nat. a+b < c ==> a < c-b";
   614 by (dtac diff_less_mono 1);
   615 by (rtac le_add2 1);
   616 by (Asm_full_simp_tac 1);
   617 qed "add_less_imp_less_diff";
   618 
   619 goal Arith.thy "!! n. n <= m ==> Suc m - n = Suc (m - n)";
   620 by (rtac Suc_diff_n 1);
   621 by (asm_full_simp_tac (simpset() addsimps [le_eq_less_Suc]) 1);
   622 qed "Suc_diff_le";
   623 
   624 goal Arith.thy "!! n. Suc i <= n ==> Suc (n - Suc i) = n - i";
   625 by (asm_full_simp_tac
   626     (simpset() addsimps [Suc_diff_n RS sym, le_eq_less_Suc]) 1);
   627 qed "Suc_diff_Suc";
   628 
   629 goal Arith.thy "!! i::nat. i <= n ==> n - (n - i) = i";
   630 by (etac rev_mp 1);
   631 by (res_inst_tac [("m","n"),("n","i")] diff_induct 1);
   632 by (ALLGOALS (asm_simp_tac  (simpset() addsimps [Suc_diff_le])));
   633 qed "diff_diff_cancel";
   634 Addsimps [diff_diff_cancel];
   635 
   636 goal Arith.thy "!!k::nat. k <= n ==> m <= n + m - k";
   637 by (etac rev_mp 1);
   638 by (res_inst_tac [("m", "k"), ("n", "n")] diff_induct 1);
   639 by (Simp_tac 1);
   640 by (simp_tac (simpset() addsimps [less_add_Suc2, less_imp_le]) 1);
   641 by (Simp_tac 1);
   642 qed "le_add_diff";
   643 
   644 
   645 (** (Anti)Monotonicity of subtraction -- by Stefan Merz **)
   646 
   647 (* Monotonicity of subtraction in first argument *)
   648 goal Arith.thy "!!n::nat. m<=n --> (m-l) <= (n-l)";
   649 by (induct_tac "n" 1);
   650 by (Simp_tac 1);
   651 by (simp_tac (simpset() addsimps [le_Suc_eq]) 1);
   652 by (rtac impI 1);
   653 by (etac impE 1);
   654 by (atac 1);
   655 by (etac le_trans 1);
   656 by (res_inst_tac [("m1","n")] (pred_Suc_diff RS subst) 1);
   657 by (rtac pred_le 1);
   658 qed_spec_mp "diff_le_mono";
   659 
   660 goal Arith.thy "!!n::nat. m<=n ==> (l-n) <= (l-m)";
   661 by (induct_tac "l" 1);
   662 by (Simp_tac 1);
   663 by (case_tac "n <= l" 1);
   664 by (subgoal_tac "m <= l" 1);
   665 by (asm_simp_tac (simpset() addsimps [Suc_diff_le]) 1);
   666 by (fast_tac (claset() addEs [le_trans]) 1);
   667 by (dtac not_leE 1);
   668 by (asm_simp_tac (simpset() addsimps [if_Suc_diff_n]) 1);
   669 qed_spec_mp "diff_le_mono2";