src/HOL/Arith.ML
author paulson
Tue May 20 11:37:57 1997 +0200 (1997-05-20)
changeset 3234 503f4c8c29eb
parent 2922 580647a879cf
child 3293 c05f73cf3227
permissions -rw-r--r--
New theorems from Hoare/Arith2.ML
     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 open Arith;
    11 
    12 (*** Basic rewrite rules for the arithmetic operators ***)
    13 
    14 goalw Arith.thy [pred_def] "pred 0 = 0";
    15 by(Simp_tac 1);
    16 qed "pred_0";
    17 
    18 goalw Arith.thy [pred_def] "pred(Suc n) = n";
    19 by(Simp_tac 1);
    20 qed "pred_Suc";
    21 
    22 Addsimps [pred_0,pred_Suc];
    23 
    24 (** pred **)
    25 
    26 val prems = goal Arith.thy "n ~= 0 ==> Suc(pred n) = n";
    27 by (res_inst_tac [("n","n")] natE 1);
    28 by (cut_facts_tac prems 1);
    29 by (ALLGOALS Asm_full_simp_tac);
    30 qed "Suc_pred";
    31 Addsimps [Suc_pred];
    32 
    33 (** Difference **)
    34 
    35 qed_goalw "diff_0_eq_0" Arith.thy [pred_def]
    36     "0 - n = 0"
    37  (fn _ => [nat_ind_tac "n" 1,  ALLGOALS Asm_simp_tac]);
    38 
    39 (*Must simplify BEFORE the induction!!  (Else we get a critical pair)
    40   Suc(m) - Suc(n)   rewrites to   pred(Suc(m) - n)  *)
    41 qed_goalw "diff_Suc_Suc" Arith.thy [pred_def]
    42     "Suc(m) - Suc(n) = m - n"
    43  (fn _ =>
    44   [Simp_tac 1, nat_ind_tac "n" 1, ALLGOALS Asm_simp_tac]);
    45 
    46 Addsimps [diff_0_eq_0, diff_Suc_Suc];
    47 
    48 
    49 goal Arith.thy "!!k. 0<k ==> EX j. k = Suc(j)";
    50 by (etac rev_mp 1);
    51 by (nat_ind_tac "k" 1);
    52 by (Simp_tac 1);
    53 by (Blast_tac 1);
    54 val lemma = result();
    55 
    56 (* [| 0 < k; !!j. [| j: nat; k = succ(j) |] ==> Q |] ==> Q *)
    57 bind_thm ("zero_less_natE", lemma RS exE);
    58 
    59 
    60 
    61 (**** Inductive properties of the operators ****)
    62 
    63 (*** Addition ***)
    64 
    65 qed_goal "add_0_right" Arith.thy "m + 0 = m"
    66  (fn _ => [nat_ind_tac "m" 1, ALLGOALS Asm_simp_tac]);
    67 
    68 qed_goal "add_Suc_right" Arith.thy "m + Suc(n) = Suc(m+n)"
    69  (fn _ => [nat_ind_tac "m" 1, ALLGOALS Asm_simp_tac]);
    70 
    71 Addsimps [add_0_right,add_Suc_right];
    72 
    73 (*Associative law for addition*)
    74 qed_goal "add_assoc" Arith.thy "(m + n) + k = m + ((n + k)::nat)"
    75  (fn _ => [nat_ind_tac "m" 1, ALLGOALS Asm_simp_tac]);
    76 
    77 (*Commutative law for addition*)  
    78 qed_goal "add_commute" Arith.thy "m + n = n + (m::nat)"
    79  (fn _ =>  [nat_ind_tac "m" 1, ALLGOALS Asm_simp_tac]);
    80 
    81 qed_goal "add_left_commute" Arith.thy "x+(y+z)=y+((x+z)::nat)"
    82  (fn _ => [rtac (add_commute RS trans) 1, rtac (add_assoc RS trans) 1,
    83            rtac (add_commute RS arg_cong) 1]);
    84 
    85 (*Addition is an AC-operator*)
    86 val add_ac = [add_assoc, add_commute, add_left_commute];
    87 
    88 goal Arith.thy "!!k::nat. (k + m = k + n) = (m=n)";
    89 by (nat_ind_tac "k" 1);
    90 by (Simp_tac 1);
    91 by (Asm_simp_tac 1);
    92 qed "add_left_cancel";
    93 
    94 goal Arith.thy "!!k::nat. (m + k = n + k) = (m=n)";
    95 by (nat_ind_tac "k" 1);
    96 by (Simp_tac 1);
    97 by (Asm_simp_tac 1);
    98 qed "add_right_cancel";
    99 
   100 goal Arith.thy "!!k::nat. (k + m <= k + n) = (m<=n)";
   101 by (nat_ind_tac "k" 1);
   102 by (Simp_tac 1);
   103 by (Asm_simp_tac 1);
   104 qed "add_left_cancel_le";
   105 
   106 goal Arith.thy "!!k::nat. (k + m < k + n) = (m<n)";
   107 by (nat_ind_tac "k" 1);
   108 by (Simp_tac 1);
   109 by (Asm_simp_tac 1);
   110 qed "add_left_cancel_less";
   111 
   112 Addsimps [add_left_cancel, add_right_cancel,
   113           add_left_cancel_le, add_left_cancel_less];
   114 
   115 goal Arith.thy "(m+n = 0) = (m=0 & n=0)";
   116 by (nat_ind_tac "m" 1);
   117 by (ALLGOALS Asm_simp_tac);
   118 qed "add_is_0";
   119 Addsimps [add_is_0];
   120 
   121 goal Arith.thy "!!n. n ~= 0 ==> m + pred n = pred(m+n)";
   122 by (nat_ind_tac "m" 1);
   123 by (ALLGOALS Asm_simp_tac);
   124 qed "add_pred";
   125 Addsimps [add_pred];
   126 
   127 
   128 (**** Additional theorems about "less than" ****)
   129 
   130 goal Arith.thy "? k::nat. n = n+k";
   131 by (res_inst_tac [("x","0")] exI 1);
   132 by (Simp_tac 1);
   133 val lemma = result();
   134 
   135 goal Arith.thy "!!m. m<n --> (? k. n=Suc(m+k))";
   136 by (nat_ind_tac "n" 1);
   137 by (ALLGOALS (simp_tac (!simpset addsimps [less_Suc_eq])));
   138 by (step_tac (!claset addSIs [lemma]) 1);
   139 by (res_inst_tac [("x","Suc(k)")] exI 1);
   140 by (Simp_tac 1);
   141 qed_spec_mp "less_eq_Suc_add";
   142 
   143 goal Arith.thy "n <= ((m + n)::nat)";
   144 by (nat_ind_tac "m" 1);
   145 by (ALLGOALS Simp_tac);
   146 by (etac le_trans 1);
   147 by (rtac (lessI RS less_imp_le) 1);
   148 qed "le_add2";
   149 
   150 goal Arith.thy "n <= ((n + m)::nat)";
   151 by (simp_tac (!simpset addsimps add_ac) 1);
   152 by (rtac le_add2 1);
   153 qed "le_add1";
   154 
   155 bind_thm ("less_add_Suc1", (lessI RS (le_add1 RS le_less_trans)));
   156 bind_thm ("less_add_Suc2", (lessI RS (le_add2 RS le_less_trans)));
   157 
   158 (*"i <= j ==> i <= j+m"*)
   159 bind_thm ("trans_le_add1", le_add1 RSN (2,le_trans));
   160 
   161 (*"i <= j ==> i <= m+j"*)
   162 bind_thm ("trans_le_add2", le_add2 RSN (2,le_trans));
   163 
   164 (*"i < j ==> i < j+m"*)
   165 bind_thm ("trans_less_add1", le_add1 RSN (2,less_le_trans));
   166 
   167 (*"i < j ==> i < m+j"*)
   168 bind_thm ("trans_less_add2", le_add2 RSN (2,less_le_trans));
   169 
   170 goal Arith.thy "!!i. i+j < (k::nat) ==> i<k";
   171 by (etac rev_mp 1);
   172 by (nat_ind_tac "j" 1);
   173 by (ALLGOALS Asm_simp_tac);
   174 by (blast_tac (!claset addDs [Suc_lessD]) 1);
   175 qed "add_lessD1";
   176 
   177 goal Arith.thy "!!i::nat. ~ (i+j < i)";
   178 br notI 1;
   179 be (add_lessD1 RS less_irrefl) 1;
   180 qed "not_add_less1";
   181 
   182 goal Arith.thy "!!i::nat. ~ (j+i < i)";
   183 by (simp_tac (!simpset addsimps [add_commute, not_add_less1]) 1);
   184 qed "not_add_less2";
   185 AddIffs [not_add_less1, not_add_less2];
   186 
   187 goal Arith.thy "!!k::nat. m <= n ==> m <= n+k";
   188 by (etac le_trans 1);
   189 by (rtac le_add1 1);
   190 qed "le_imp_add_le";
   191 
   192 goal Arith.thy "!!k::nat. m < n ==> m < n+k";
   193 by (etac less_le_trans 1);
   194 by (rtac le_add1 1);
   195 qed "less_imp_add_less";
   196 
   197 goal Arith.thy "m+k<=n --> m<=(n::nat)";
   198 by (nat_ind_tac "k" 1);
   199 by (ALLGOALS Asm_simp_tac);
   200 by (blast_tac (!claset addDs [Suc_leD]) 1);
   201 qed_spec_mp "add_leD1";
   202 
   203 goal Arith.thy "!!n::nat. m+k<=n ==> k<=n";
   204 by (full_simp_tac (!simpset addsimps [add_commute]) 1);
   205 by (etac add_leD1 1);
   206 qed_spec_mp "add_leD2";
   207 
   208 goal Arith.thy "!!n::nat. m+k<=n ==> m<=n & k<=n";
   209 by (blast_tac (!claset addDs [add_leD1, add_leD2]) 1);
   210 bind_thm ("add_leE", result() RS conjE);
   211 
   212 goal Arith.thy "!!k l::nat. [| k<l; m+l = k+n |] ==> m<n";
   213 by (safe_tac (!claset addSDs [less_eq_Suc_add]));
   214 by (asm_full_simp_tac
   215     (!simpset delsimps [add_Suc_right]
   216                 addsimps ([add_Suc_right RS sym, add_left_cancel] @add_ac)) 1);
   217 by (etac subst 1);
   218 by (simp_tac (!simpset addsimps [less_add_Suc1]) 1);
   219 qed "less_add_eq_less";
   220 
   221 
   222 (*** Monotonicity of Addition ***)
   223 
   224 (*strict, in 1st argument*)
   225 goal Arith.thy "!!i j k::nat. i < j ==> i + k < j + k";
   226 by (nat_ind_tac "k" 1);
   227 by (ALLGOALS Asm_simp_tac);
   228 qed "add_less_mono1";
   229 
   230 (*strict, in both arguments*)
   231 goal Arith.thy "!!i j k::nat. [|i < j; k < l|] ==> i + k < j + l";
   232 by (rtac (add_less_mono1 RS less_trans) 1);
   233 by (REPEAT (assume_tac 1));
   234 by (nat_ind_tac "j" 1);
   235 by (ALLGOALS Asm_simp_tac);
   236 qed "add_less_mono";
   237 
   238 (*A [clumsy] way of lifting < monotonicity to <= monotonicity *)
   239 val [lt_mono,le] = goal Arith.thy
   240      "[| !!i j::nat. i<j ==> f(i) < f(j);       \
   241 \        i <= j                                 \
   242 \     |] ==> f(i) <= (f(j)::nat)";
   243 by (cut_facts_tac [le] 1);
   244 by (asm_full_simp_tac (!simpset addsimps [le_eq_less_or_eq]) 1);
   245 by (blast_tac (!claset addSIs [lt_mono]) 1);
   246 qed "less_mono_imp_le_mono";
   247 
   248 (*non-strict, in 1st argument*)
   249 goal Arith.thy "!!i j k::nat. i<=j ==> i + k <= j + k";
   250 by (res_inst_tac [("f", "%j.j+k")] less_mono_imp_le_mono 1);
   251 by (etac add_less_mono1 1);
   252 by (assume_tac 1);
   253 qed "add_le_mono1";
   254 
   255 (*non-strict, in both arguments*)
   256 goal Arith.thy "!!k l::nat. [|i<=j;  k<=l |] ==> i + k <= j + l";
   257 by (etac (add_le_mono1 RS le_trans) 1);
   258 by (simp_tac (!simpset addsimps [add_commute]) 1);
   259 (*j moves to the end because it is free while k, l are bound*)
   260 by (etac add_le_mono1 1);
   261 qed "add_le_mono";
   262 
   263 
   264 (*** Multiplication ***)
   265 
   266 (*right annihilation in product*)
   267 qed_goal "mult_0_right" Arith.thy "m * 0 = 0"
   268  (fn _ => [nat_ind_tac "m" 1, ALLGOALS Asm_simp_tac]);
   269 
   270 (*right Sucessor law for multiplication*)
   271 qed_goal "mult_Suc_right" Arith.thy  "m * Suc(n) = m + (m * n)"
   272  (fn _ => [nat_ind_tac "m" 1,
   273            ALLGOALS(asm_simp_tac (!simpset addsimps add_ac))]);
   274 
   275 Addsimps [mult_0_right,mult_Suc_right];
   276 
   277 goal Arith.thy "1 * n = n";
   278 by (Asm_simp_tac 1);
   279 qed "mult_1";
   280 
   281 goal Arith.thy "n * 1 = n";
   282 by (Asm_simp_tac 1);
   283 qed "mult_1_right";
   284 
   285 (*Commutative law for multiplication*)
   286 qed_goal "mult_commute" Arith.thy "m * n = n * (m::nat)"
   287  (fn _ => [nat_ind_tac "m" 1, ALLGOALS Asm_simp_tac]);
   288 
   289 (*addition distributes over multiplication*)
   290 qed_goal "add_mult_distrib" Arith.thy "(m + n)*k = (m*k) + ((n*k)::nat)"
   291  (fn _ => [nat_ind_tac "m" 1,
   292            ALLGOALS(asm_simp_tac (!simpset addsimps add_ac))]);
   293 
   294 qed_goal "add_mult_distrib2" Arith.thy "k*(m + n) = (k*m) + ((k*n)::nat)"
   295  (fn _ => [nat_ind_tac "m" 1,
   296            ALLGOALS(asm_simp_tac (!simpset addsimps add_ac))]);
   297 
   298 (*Associative law for multiplication*)
   299 qed_goal "mult_assoc" Arith.thy "(m * n) * k = m * ((n * k)::nat)"
   300   (fn _ => [nat_ind_tac "m" 1, 
   301             ALLGOALS (asm_simp_tac (!simpset addsimps [add_mult_distrib]))]);
   302 
   303 qed_goal "mult_left_commute" Arith.thy "x*(y*z) = y*((x*z)::nat)"
   304  (fn _ => [rtac trans 1, rtac mult_commute 1, rtac trans 1,
   305            rtac mult_assoc 1, rtac (mult_commute RS arg_cong) 1]);
   306 
   307 val mult_ac = [mult_assoc,mult_commute,mult_left_commute];
   308 
   309 
   310 (*** Difference ***)
   311 
   312 qed_goal "pred_Suc_diff" Arith.thy "pred(Suc m - n) = m - n"
   313  (fn _ => [nat_ind_tac "n" 1, ALLGOALS Asm_simp_tac]);
   314 Addsimps [pred_Suc_diff];
   315 
   316 qed_goal "diff_self_eq_0" Arith.thy "m - m = 0"
   317  (fn _ => [nat_ind_tac "m" 1, ALLGOALS Asm_simp_tac]);
   318 Addsimps [diff_self_eq_0];
   319 
   320 (*Addition is the inverse of subtraction: if n<=m then n+(m-n) = m. *)
   321 val [prem] = goal Arith.thy "[| ~ m<n |] ==> n+(m-n) = (m::nat)";
   322 by (rtac (prem RS rev_mp) 1);
   323 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   324 by (ALLGOALS (Asm_simp_tac));
   325 qed "add_diff_inverse";
   326 
   327 Delsimps  [diff_Suc];
   328 
   329 
   330 (*** More results about difference ***)
   331 
   332 goal Arith.thy "m - n < Suc(m)";
   333 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   334 by (etac less_SucE 3);
   335 by (ALLGOALS (asm_simp_tac (!simpset addsimps [less_Suc_eq])));
   336 qed "diff_less_Suc";
   337 
   338 goal Arith.thy "!!m::nat. m - n <= m";
   339 by (res_inst_tac [("m","m"), ("n","n")] diff_induct 1);
   340 by (ALLGOALS Asm_simp_tac);
   341 qed "diff_le_self";
   342 
   343 goal Arith.thy "!!n::nat. (n+m) - n = m";
   344 by (nat_ind_tac "n" 1);
   345 by (ALLGOALS Asm_simp_tac);
   346 qed "diff_add_inverse";
   347 Addsimps [diff_add_inverse];
   348 
   349 goal Arith.thy "!!n::nat.(m+n) - n = m";
   350 by (res_inst_tac [("m1","m")] (add_commute RS ssubst) 1);
   351 by (REPEAT (ares_tac [diff_add_inverse] 1));
   352 qed "diff_add_inverse2";
   353 Addsimps [diff_add_inverse2];
   354 
   355 val [prem] = goal Arith.thy "m < Suc(n) ==> m-n = 0";
   356 by (rtac (prem RS rev_mp) 1);
   357 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   358 by (asm_simp_tac (!simpset addsimps [less_Suc_eq]) 1);
   359 by (ALLGOALS (Asm_simp_tac));
   360 qed "less_imp_diff_is_0";
   361 
   362 val prems = goal Arith.thy "m-n = 0  -->  n-m = 0  -->  m=n";
   363 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   364 by (REPEAT(Simp_tac 1 THEN TRY(atac 1)));
   365 qed_spec_mp "diffs0_imp_equal";
   366 
   367 val [prem] = goal Arith.thy "m<n ==> 0<n-m";
   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 "less_imp_diff_positive";
   372 
   373 val [prem] = goal Arith.thy "n < Suc(m) ==> Suc(m)-n = Suc(m-n)";
   374 by (rtac (prem RS rev_mp) 1);
   375 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   376 by (ALLGOALS (Asm_simp_tac));
   377 qed "Suc_diff_n";
   378 
   379 goal Arith.thy "Suc(m)-n = (if m<n then 0 else Suc(m-n))";
   380 by (simp_tac (!simpset addsimps [less_imp_diff_is_0, not_less_eq, Suc_diff_n]
   381                     setloop (split_tac [expand_if])) 1);
   382 qed "if_Suc_diff_n";
   383 
   384 goal Arith.thy "P(k) --> (!n. P(Suc(n))--> P(n)) --> P(k-i)";
   385 by (res_inst_tac [("m","k"),("n","i")] diff_induct 1);
   386 by (ALLGOALS (strip_tac THEN' Simp_tac THEN' TRY o Blast_tac));
   387 qed "zero_induct_lemma";
   388 
   389 val prems = goal Arith.thy "[| P(k);  !!n. P(Suc(n)) ==> P(n) |] ==> P(0)";
   390 by (rtac (diff_self_eq_0 RS subst) 1);
   391 by (rtac (zero_induct_lemma RS mp RS mp) 1);
   392 by (REPEAT (ares_tac ([impI,allI]@prems) 1));
   393 qed "zero_induct";
   394 
   395 goal Arith.thy "!!k::nat. (k+m) - (k+n) = m - n";
   396 by (nat_ind_tac "k" 1);
   397 by (ALLGOALS Asm_simp_tac);
   398 qed "diff_cancel";
   399 Addsimps [diff_cancel];
   400 
   401 goal Arith.thy "!!m::nat. (m+k) - (n+k) = m - n";
   402 val add_commute_k = read_instantiate [("n","k")] add_commute;
   403 by (asm_simp_tac (!simpset addsimps ([add_commute_k])) 1);
   404 qed "diff_cancel2";
   405 Addsimps [diff_cancel2];
   406 
   407 (*From Clemens Ballarin*)
   408 goal Arith.thy "!!n::nat. [| k<=n; n<=m |] ==> (m-k) - (n-k) = m-n";
   409 by (subgoal_tac "k<=n --> n<=m --> (m-k) - (n-k) = m-n" 1);
   410 by (Asm_full_simp_tac 1);
   411 by (nat_ind_tac "k" 1);
   412 by (Simp_tac 1);
   413 (* Induction step *)
   414 by (subgoal_tac "Suc na <= m --> n <= m --> Suc na <= n --> \
   415 \                Suc (m - Suc na) - Suc (n - Suc na) = m-n" 1);
   416 by (Asm_full_simp_tac 1);
   417 by (blast_tac (!claset addIs [le_trans]) 1);
   418 by (auto_tac (!claset addIs [Suc_leD], !simpset delsimps [diff_Suc_Suc]));
   419 by (asm_full_simp_tac (!simpset delsimps [Suc_less_eq] 
   420 		       addsimps [Suc_diff_n RS sym, le_eq_less_Suc]) 1);
   421 qed "diff_right_cancel";
   422 
   423 goal Arith.thy "!!n::nat. n - (n+m) = 0";
   424 by (nat_ind_tac "n" 1);
   425 by (ALLGOALS Asm_simp_tac);
   426 qed "diff_add_0";
   427 Addsimps [diff_add_0];
   428 
   429 (** Difference distributes over multiplication **)
   430 
   431 goal Arith.thy "!!m::nat. (m - n) * k = (m * k) - (n * k)";
   432 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   433 by (ALLGOALS Asm_simp_tac);
   434 qed "diff_mult_distrib" ;
   435 
   436 goal Arith.thy "!!m::nat. k * (m - n) = (k * m) - (k * n)";
   437 val mult_commute_k = read_instantiate [("m","k")] mult_commute;
   438 by (simp_tac (!simpset addsimps [diff_mult_distrib, mult_commute_k]) 1);
   439 qed "diff_mult_distrib2" ;
   440 (*NOT added as rewrites, since sometimes they are used from right-to-left*)
   441 
   442 
   443 (** Less-then properties **)
   444 
   445 (*In ordinary notation: if 0<n and n<=m then m-n < m *)
   446 goal Arith.thy "!!m. [| 0<n; ~ m<n |] ==> m - n < m";
   447 by (subgoal_tac "0<n --> ~ m<n --> m - n < m" 1);
   448 by (Blast_tac 1);
   449 by (res_inst_tac [("m","m"),("n","n")] diff_induct 1);
   450 by (ALLGOALS(asm_simp_tac(!simpset addsimps [diff_less_Suc])));
   451 qed "diff_less";
   452 
   453 val wf_less_trans = [eq_reflection, wf_pred_nat RS wf_trancl] MRS 
   454                     def_wfrec RS trans;
   455 
   456 goalw Nat.thy [less_def] "(m,n) : pred_nat^+ = (m<n)";
   457 by (rtac refl 1);
   458 qed "less_eq";
   459 
   460 (*** Remainder ***)
   461 
   462 goal Arith.thy "(%m. m mod n) = wfrec (trancl pred_nat) \
   463              \                      (%f j. if j<n then j else f (j-n))";
   464 by (simp_tac (!simpset addsimps [mod_def]) 1);
   465 qed "mod_eq";
   466 
   467 goal Arith.thy "!!m. m<n ==> m mod n = m";
   468 by (rtac (mod_eq RS wf_less_trans) 1);
   469 by (Asm_simp_tac 1);
   470 qed "mod_less";
   471 
   472 goal Arith.thy "!!m. [| 0<n;  ~m<n |] ==> m mod n = (m-n) mod n";
   473 by (rtac (mod_eq RS wf_less_trans) 1);
   474 by (asm_simp_tac (!simpset addsimps [diff_less, cut_apply, less_eq]) 1);
   475 qed "mod_geq";
   476 
   477 goal thy "!!n. 0<n ==> n mod n = 0";
   478 by (rtac (mod_eq RS wf_less_trans) 1);
   479 by (asm_simp_tac (!simpset addsimps [mod_less, diff_self_eq_0,
   480 				     cut_def, less_eq]) 1);
   481 qed "mod_nn_is_0";
   482 
   483 goal thy "!!n. 0<n ==> (m+n) mod n = m mod n";
   484 by (subgoal_tac "(n + m) mod n = (n+m-n) mod n" 1);
   485 by (stac (mod_geq RS sym) 2);
   486 by (ALLGOALS (asm_full_simp_tac (!simpset addsimps [add_commute])));
   487 qed "mod_eq_add";
   488 
   489 goal thy "!!n. 0<n ==> m*n mod n = 0";
   490 by (nat_ind_tac "m" 1);
   491 by (asm_simp_tac (!simpset addsimps [mod_less]) 1);
   492 by (dres_inst_tac [("m","m*n")] mod_eq_add 1);
   493 by (asm_full_simp_tac (!simpset addsimps [add_commute]) 1);
   494 qed "mod_prod_nn_is_0";
   495 
   496 
   497 (*** Quotient ***)
   498 
   499 goal Arith.thy "(%m. m div n) = wfrec (trancl pred_nat) \
   500                         \            (%f j. if j<n then 0 else Suc (f (j-n)))";
   501 by (simp_tac (!simpset addsimps [div_def]) 1);
   502 qed "div_eq";
   503 
   504 goal Arith.thy "!!m. m<n ==> m div n = 0";
   505 by (rtac (div_eq RS wf_less_trans) 1);
   506 by (Asm_simp_tac 1);
   507 qed "div_less";
   508 
   509 goal Arith.thy "!!M. [| 0<n;  ~m<n |] ==> m div n = Suc((m-n) div n)";
   510 by (rtac (div_eq RS wf_less_trans) 1);
   511 by (asm_simp_tac (!simpset addsimps [diff_less, cut_apply, less_eq]) 1);
   512 qed "div_geq";
   513 
   514 (*Main Result about quotient and remainder.*)
   515 goal Arith.thy "!!m. 0<n ==> (m div n)*n + m mod n = m";
   516 by (res_inst_tac [("n","m")] less_induct 1);
   517 by (rename_tac "k" 1);    (*Variable name used in line below*)
   518 by (case_tac "k<n" 1);
   519 by (ALLGOALS (asm_simp_tac(!simpset addsimps ([add_assoc] @
   520                        [mod_less, mod_geq, div_less, div_geq,
   521                         add_diff_inverse, diff_less]))));
   522 qed "mod_div_equality";
   523 
   524 
   525 (*** Further facts about mod (mainly for mutilated checkerboard ***)
   526 
   527 goal Arith.thy
   528     "!!m n. 0<n ==> \
   529 \           Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))";
   530 by (res_inst_tac [("n","m")] less_induct 1);
   531 by (excluded_middle_tac "Suc(na)<n" 1);
   532 (* case Suc(na) < n *)
   533 by (forward_tac [lessI RS less_trans] 2);
   534 by (asm_simp_tac (!simpset addsimps [mod_less, less_not_refl2 RS not_sym]) 2);
   535 (* case n <= Suc(na) *)
   536 by (asm_full_simp_tac (!simpset addsimps [not_less_iff_le, mod_geq]) 1);
   537 by (etac (le_imp_less_or_eq RS disjE) 1);
   538 by (asm_simp_tac (!simpset addsimps [Suc_diff_n]) 1);
   539 by (asm_full_simp_tac (!simpset addsimps [not_less_eq RS sym, 
   540                                           diff_less, mod_geq]) 1);
   541 by (asm_simp_tac (!simpset addsimps [mod_less]) 1);
   542 qed "mod_Suc";
   543 
   544 goal Arith.thy "!!m n. 0<n ==> m mod n < n";
   545 by (res_inst_tac [("n","m")] less_induct 1);
   546 by (excluded_middle_tac "na<n" 1);
   547 (*case na<n*)
   548 by (asm_simp_tac (!simpset addsimps [mod_less]) 2);
   549 (*case n le na*)
   550 by (asm_full_simp_tac (!simpset addsimps [mod_geq, diff_less]) 1);
   551 qed "mod_less_divisor";
   552 
   553 
   554 (** Evens and Odds **)
   555 
   556 (*With less_zeroE, causes case analysis on b<2*)
   557 AddSEs [less_SucE];
   558 
   559 goal thy "!!k b. b<2 ==> k mod 2 = b | k mod 2 = (if b=1 then 0 else 1)";
   560 by (subgoal_tac "k mod 2 < 2" 1);
   561 by (asm_simp_tac (!simpset addsimps [mod_less_divisor]) 2);
   562 by (asm_simp_tac (!simpset setloop split_tac [expand_if]) 1);
   563 by (Blast_tac 1);
   564 qed "mod2_cases";
   565 
   566 goal thy "Suc(Suc(m)) mod 2 = m mod 2";
   567 by (subgoal_tac "m mod 2 < 2" 1);
   568 by (asm_simp_tac (!simpset addsimps [mod_less_divisor]) 2);
   569 by (Step_tac 1);
   570 by (ALLGOALS (asm_simp_tac (!simpset addsimps [mod_Suc])));
   571 qed "mod2_Suc_Suc";
   572 Addsimps [mod2_Suc_Suc];
   573 
   574 goal thy "(m+m) mod 2 = 0";
   575 by (nat_ind_tac "m" 1);
   576 by (simp_tac (!simpset addsimps [mod_less]) 1);
   577 by (asm_simp_tac (!simpset addsimps [mod2_Suc_Suc, add_Suc_right]) 1);
   578 qed "mod2_add_self";
   579 Addsimps [mod2_add_self];
   580 
   581 Delrules [less_SucE];
   582 
   583 
   584 (*** Monotonicity of Multiplication ***)
   585 
   586 goal Arith.thy "!!i::nat. i<=j ==> i*k<=j*k";
   587 by (nat_ind_tac "k" 1);
   588 by (ALLGOALS (asm_simp_tac (!simpset addsimps [add_le_mono])));
   589 qed "mult_le_mono1";
   590 
   591 (*<=monotonicity, BOTH arguments*)
   592 goal Arith.thy "!!i::nat. [| i<=j; k<=l |] ==> i*k<=j*l";
   593 by (etac (mult_le_mono1 RS le_trans) 1);
   594 by (rtac le_trans 1);
   595 by (stac mult_commute 2);
   596 by (etac mult_le_mono1 2);
   597 by (simp_tac (!simpset addsimps [mult_commute]) 1);
   598 qed "mult_le_mono";
   599 
   600 (*strict, in 1st argument; proof is by induction on k>0*)
   601 goal Arith.thy "!!i::nat. [| i<j; 0<k |] ==> k*i < k*j";
   602 by (etac zero_less_natE 1);
   603 by (Asm_simp_tac 1);
   604 by (nat_ind_tac "x" 1);
   605 by (ALLGOALS (asm_simp_tac (!simpset addsimps [add_less_mono])));
   606 qed "mult_less_mono2";
   607 
   608 goal Arith.thy "!!i::nat. [| i<j; 0<k |] ==> i*k < j*k";
   609 bd mult_less_mono2 1;
   610 by (ALLGOALS (asm_full_simp_tac (!simpset addsimps [mult_commute])));
   611 qed "mult_less_mono1";
   612 
   613 goal Arith.thy "(0 < m*n) = (0<m & 0<n)";
   614 by (nat_ind_tac "m" 1);
   615 by (nat_ind_tac "n" 2);
   616 by (ALLGOALS Asm_simp_tac);
   617 qed "zero_less_mult_iff";
   618 
   619 goal Arith.thy "(m*n = 1) = (m=1 & n=1)";
   620 by (nat_ind_tac "m" 1);
   621 by (Simp_tac 1);
   622 by (nat_ind_tac "n" 1);
   623 by (Simp_tac 1);
   624 by (fast_tac (!claset addss !simpset) 1);
   625 qed "mult_eq_1_iff";
   626 
   627 goal Arith.thy "!!k. 0<k ==> (m*k < n*k) = (m<n)";
   628 by (safe_tac (!claset addSIs [mult_less_mono1]));
   629 by (cut_facts_tac [less_linear] 1);
   630 by (blast_tac (!claset addDs [mult_less_mono1] addEs [less_asym]) 1);
   631 qed "mult_less_cancel2";
   632 
   633 goal Arith.thy "!!k. 0<k ==> (k*m < k*n) = (m<n)";
   634 bd mult_less_cancel2 1;
   635 by (asm_full_simp_tac (!simpset addsimps [mult_commute]) 1);
   636 qed "mult_less_cancel1";
   637 Addsimps [mult_less_cancel1, mult_less_cancel2];
   638 
   639 goal Arith.thy "!!k. 0<k ==> (m*k = n*k) = (m=n)";
   640 by (cut_facts_tac [less_linear] 1);
   641 by(Step_tac 1);
   642 ba 2;
   643 by (ALLGOALS (dtac mult_less_mono1 THEN' assume_tac));
   644 by (ALLGOALS Asm_full_simp_tac);
   645 qed "mult_cancel2";
   646 
   647 goal Arith.thy "!!k. 0<k ==> (k*m = k*n) = (m=n)";
   648 bd mult_cancel2 1;
   649 by (asm_full_simp_tac (!simpset addsimps [mult_commute]) 1);
   650 qed "mult_cancel1";
   651 Addsimps [mult_cancel1, mult_cancel2];
   652 
   653 
   654 (*** More division laws ***)
   655 
   656 goal thy "!!n. 0<n ==> m*n div n = m";
   657 by (cut_inst_tac [("m", "m*n")] mod_div_equality 1);
   658 ba 1;
   659 by (asm_full_simp_tac (!simpset addsimps [mod_prod_nn_is_0]) 1);
   660 qed "div_prod_nn_is_m";
   661 Addsimps [div_prod_nn_is_m];
   662 
   663 (*Cancellation law for division*)
   664 goal Arith.thy "!!k. [| 0<n; 0<k |] ==> (k*m) div (k*n) = m div n";
   665 by (res_inst_tac [("n","m")] less_induct 1);
   666 by (case_tac "na<n" 1);
   667 by (asm_simp_tac (!simpset addsimps [div_less, zero_less_mult_iff, 
   668                                      mult_less_mono2]) 1);
   669 by (subgoal_tac "~ k*na < k*n" 1);
   670 by (asm_simp_tac
   671      (!simpset addsimps [zero_less_mult_iff, div_geq,
   672                          diff_mult_distrib2 RS sym, diff_less]) 1);
   673 by (asm_full_simp_tac (!simpset addsimps [not_less_iff_le, 
   674                                           le_refl RS mult_le_mono]) 1);
   675 qed "div_cancel";
   676 Addsimps [div_cancel];
   677 
   678 goal Arith.thy "!!k. [| 0<n; 0<k |] ==> (k*m) mod (k*n) = k * (m mod n)";
   679 by (res_inst_tac [("n","m")] less_induct 1);
   680 by (case_tac "na<n" 1);
   681 by (asm_simp_tac (!simpset addsimps [mod_less, zero_less_mult_iff, 
   682                                      mult_less_mono2]) 1);
   683 by (subgoal_tac "~ k*na < k*n" 1);
   684 by (asm_simp_tac
   685      (!simpset addsimps [zero_less_mult_iff, mod_geq,
   686                          diff_mult_distrib2 RS sym, diff_less]) 1);
   687 by (asm_full_simp_tac (!simpset addsimps [not_less_iff_le, 
   688                                           le_refl RS mult_le_mono]) 1);
   689 qed "mult_mod_distrib";
   690 
   691 
   692 (** Lemma for gcd **)
   693 
   694 goal Arith.thy "!!m n. m = m*n ==> n=1 | m=0";
   695 by (dtac sym 1);
   696 by (rtac disjCI 1);
   697 by (rtac nat_less_cases 1 THEN assume_tac 2);
   698 by (fast_tac (!claset addSEs [less_SucE] addss !simpset) 1);
   699 by (best_tac (!claset addDs [mult_less_mono2] 
   700                       addss (!simpset addsimps [zero_less_eq RS sym])) 1);
   701 qed "mult_eq_self_implies_10";
   702 
   703 
   704 (*** Subtraction laws -- from Clemens Ballarin ***)
   705 
   706 goal Arith.thy "!! a b c::nat. [| a < b; c <= a |] ==> a-c < b-c";
   707 by (subgoal_tac "c+(a-c) < c+(b-c)" 1);
   708 by (Asm_full_simp_tac 1);
   709 by (subgoal_tac "c <= b" 1);
   710 by (blast_tac (!claset addIs [less_imp_le, le_trans]) 2);
   711 by (asm_simp_tac (!simpset addsimps [leD RS add_diff_inverse]) 1);
   712 qed "diff_less_mono";
   713 
   714 goal Arith.thy "!! a b c::nat. a+b < c ==> a < c-b";
   715 bd diff_less_mono 1;
   716 br le_add2 1;
   717 by (Asm_full_simp_tac 1);
   718 qed "add_less_imp_less_diff";
   719 
   720 goal Arith.thy "!! n. n <= m ==> Suc m - n = Suc (m - n)";
   721 br Suc_diff_n 1;
   722 by (asm_full_simp_tac (!simpset addsimps [le_eq_less_Suc]) 1);
   723 qed "Suc_diff_le";
   724 
   725 goal Arith.thy "!! n. Suc i <= n ==> Suc (n - Suc i) = n - i";
   726 by (asm_full_simp_tac
   727     (!simpset addsimps [Suc_diff_n RS sym, le_eq_less_Suc]) 1);
   728 qed "Suc_diff_Suc";
   729 
   730 goal Arith.thy "!! i::nat. i <= n ==> n - (n - i) = i";
   731 by (subgoal_tac "(n-i) + (n - (n-i)) = (n-i) + i" 1);
   732 by (Asm_full_simp_tac 1);
   733 by (asm_simp_tac (!simpset addsimps [leD RS add_diff_inverse, diff_le_self, 
   734 				     add_commute]) 1);
   735 qed "diff_diff_cancel";
   736 
   737 goal Arith.thy "!!k::nat. k <= n ==> m <= n + m - k";
   738 be rev_mp 1;
   739 by (res_inst_tac [("m", "k"), ("n", "n")] diff_induct 1);
   740 by (Simp_tac 1);
   741 by (simp_tac (!simpset addsimps [less_add_Suc2, less_imp_le]) 1);
   742 by (Simp_tac 1);
   743 qed "le_add_diff";
   744 
   745