src/HOL/Divides.thy
author haftmann
Thu Oct 29 11:41:36 2009 +0100 (2009-10-29)
changeset 33318 ddd97d9dfbfb
parent 33296 a3924d1069e5
child 33340 a165b97f3658
permissions -rw-r--r--
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
     1 (*  Title:      HOL/Divides.thy
     2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     3     Copyright   1999  University of Cambridge
     4 *)
     5 
     6 header {* The division operators div and mod *}
     7 
     8 theory Divides
     9 imports Nat_Numeral Nat_Transfer
    10 uses
    11   "~~/src/Provers/Arith/assoc_fold.ML"
    12   "~~/src/Provers/Arith/cancel_numerals.ML"
    13   "~~/src/Provers/Arith/combine_numerals.ML"
    14   "~~/src/Provers/Arith/cancel_numeral_factor.ML"
    15   "~~/src/Provers/Arith/extract_common_term.ML"
    16   ("Tools/numeral_simprocs.ML")
    17   ("Tools/nat_numeral_simprocs.ML")
    18   "~~/src/Provers/Arith/cancel_div_mod.ML"
    19 begin
    20 
    21 subsection {* Syntactic division operations *}
    22 
    23 class div = dvd +
    24   fixes div :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "div" 70)
    25     and mod :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "mod" 70)
    26 
    27 
    28 subsection {* Abstract division in commutative semirings. *}
    29 
    30 class semiring_div = comm_semiring_1_cancel + no_zero_divisors + div +
    31   assumes mod_div_equality: "a div b * b + a mod b = a"
    32     and div_by_0 [simp]: "a div 0 = 0"
    33     and div_0 [simp]: "0 div a = 0"
    34     and div_mult_self1 [simp]: "b \<noteq> 0 \<Longrightarrow> (a + c * b) div b = c + a div b"
    35     and div_mult_mult1 [simp]: "c \<noteq> 0 \<Longrightarrow> (c * a) div (c * b) = a div b"
    36 begin
    37 
    38 text {* @{const div} and @{const mod} *}
    39 
    40 lemma mod_div_equality2: "b * (a div b) + a mod b = a"
    41   unfolding mult_commute [of b]
    42   by (rule mod_div_equality)
    43 
    44 lemma mod_div_equality': "a mod b + a div b * b = a"
    45   using mod_div_equality [of a b]
    46   by (simp only: add_ac)
    47 
    48 lemma div_mod_equality: "((a div b) * b + a mod b) + c = a + c"
    49   by (simp add: mod_div_equality)
    50 
    51 lemma div_mod_equality2: "(b * (a div b) + a mod b) + c = a + c"
    52   by (simp add: mod_div_equality2)
    53 
    54 lemma mod_by_0 [simp]: "a mod 0 = a"
    55   using mod_div_equality [of a zero] by simp
    56 
    57 lemma mod_0 [simp]: "0 mod a = 0"
    58   using mod_div_equality [of zero a] div_0 by simp
    59 
    60 lemma div_mult_self2 [simp]:
    61   assumes "b \<noteq> 0"
    62   shows "(a + b * c) div b = c + a div b"
    63   using assms div_mult_self1 [of b a c] by (simp add: mult_commute)
    64 
    65 lemma mod_mult_self1 [simp]: "(a + c * b) mod b = a mod b"
    66 proof (cases "b = 0")
    67   case True then show ?thesis by simp
    68 next
    69   case False
    70   have "a + c * b = (a + c * b) div b * b + (a + c * b) mod b"
    71     by (simp add: mod_div_equality)
    72   also from False div_mult_self1 [of b a c] have
    73     "\<dots> = (c + a div b) * b + (a + c * b) mod b"
    74       by (simp add: algebra_simps)
    75   finally have "a = a div b * b + (a + c * b) mod b"
    76     by (simp add: add_commute [of a] add_assoc left_distrib)
    77   then have "a div b * b + (a + c * b) mod b = a div b * b + a mod b"
    78     by (simp add: mod_div_equality)
    79   then show ?thesis by simp
    80 qed
    81 
    82 lemma mod_mult_self2 [simp]: "(a + b * c) mod b = a mod b"
    83   by (simp add: mult_commute [of b])
    84 
    85 lemma div_mult_self1_is_id [simp]: "b \<noteq> 0 \<Longrightarrow> b * a div b = a"
    86   using div_mult_self2 [of b 0 a] by simp
    87 
    88 lemma div_mult_self2_is_id [simp]: "b \<noteq> 0 \<Longrightarrow> a * b div b = a"
    89   using div_mult_self1 [of b 0 a] by simp
    90 
    91 lemma mod_mult_self1_is_0 [simp]: "b * a mod b = 0"
    92   using mod_mult_self2 [of 0 b a] by simp
    93 
    94 lemma mod_mult_self2_is_0 [simp]: "a * b mod b = 0"
    95   using mod_mult_self1 [of 0 a b] by simp
    96 
    97 lemma div_by_1 [simp]: "a div 1 = a"
    98   using div_mult_self2_is_id [of 1 a] zero_neq_one by simp
    99 
   100 lemma mod_by_1 [simp]: "a mod 1 = 0"
   101 proof -
   102   from mod_div_equality [of a one] div_by_1 have "a + a mod 1 = a" by simp
   103   then have "a + a mod 1 = a + 0" by simp
   104   then show ?thesis by (rule add_left_imp_eq)
   105 qed
   106 
   107 lemma mod_self [simp]: "a mod a = 0"
   108   using mod_mult_self2_is_0 [of 1] by simp
   109 
   110 lemma div_self [simp]: "a \<noteq> 0 \<Longrightarrow> a div a = 1"
   111   using div_mult_self2_is_id [of _ 1] by simp
   112 
   113 lemma div_add_self1 [simp]:
   114   assumes "b \<noteq> 0"
   115   shows "(b + a) div b = a div b + 1"
   116   using assms div_mult_self1 [of b a 1] by (simp add: add_commute)
   117 
   118 lemma div_add_self2 [simp]:
   119   assumes "b \<noteq> 0"
   120   shows "(a + b) div b = a div b + 1"
   121   using assms div_add_self1 [of b a] by (simp add: add_commute)
   122 
   123 lemma mod_add_self1 [simp]:
   124   "(b + a) mod b = a mod b"
   125   using mod_mult_self1 [of a 1 b] by (simp add: add_commute)
   126 
   127 lemma mod_add_self2 [simp]:
   128   "(a + b) mod b = a mod b"
   129   using mod_mult_self1 [of a 1 b] by simp
   130 
   131 lemma mod_div_decomp:
   132   fixes a b
   133   obtains q r where "q = a div b" and "r = a mod b"
   134     and "a = q * b + r"
   135 proof -
   136   from mod_div_equality have "a = a div b * b + a mod b" by simp
   137   moreover have "a div b = a div b" ..
   138   moreover have "a mod b = a mod b" ..
   139   note that ultimately show thesis by blast
   140 qed
   141 
   142 lemma dvd_eq_mod_eq_0 [code_unfold]: "a dvd b \<longleftrightarrow> b mod a = 0"
   143 proof
   144   assume "b mod a = 0"
   145   with mod_div_equality [of b a] have "b div a * a = b" by simp
   146   then have "b = a * (b div a)" unfolding mult_commute ..
   147   then have "\<exists>c. b = a * c" ..
   148   then show "a dvd b" unfolding dvd_def .
   149 next
   150   assume "a dvd b"
   151   then have "\<exists>c. b = a * c" unfolding dvd_def .
   152   then obtain c where "b = a * c" ..
   153   then have "b mod a = a * c mod a" by simp
   154   then have "b mod a = c * a mod a" by (simp add: mult_commute)
   155   then show "b mod a = 0" by simp
   156 qed
   157 
   158 lemma mod_div_trivial [simp]: "a mod b div b = 0"
   159 proof (cases "b = 0")
   160   assume "b = 0"
   161   thus ?thesis by simp
   162 next
   163   assume "b \<noteq> 0"
   164   hence "a div b + a mod b div b = (a mod b + a div b * b) div b"
   165     by (rule div_mult_self1 [symmetric])
   166   also have "\<dots> = a div b"
   167     by (simp only: mod_div_equality')
   168   also have "\<dots> = a div b + 0"
   169     by simp
   170   finally show ?thesis
   171     by (rule add_left_imp_eq)
   172 qed
   173 
   174 lemma mod_mod_trivial [simp]: "a mod b mod b = a mod b"
   175 proof -
   176   have "a mod b mod b = (a mod b + a div b * b) mod b"
   177     by (simp only: mod_mult_self1)
   178   also have "\<dots> = a mod b"
   179     by (simp only: mod_div_equality')
   180   finally show ?thesis .
   181 qed
   182 
   183 lemma dvd_imp_mod_0: "a dvd b \<Longrightarrow> b mod a = 0"
   184 by (rule dvd_eq_mod_eq_0[THEN iffD1])
   185 
   186 lemma dvd_div_mult_self: "a dvd b \<Longrightarrow> (b div a) * a = b"
   187 by (subst (2) mod_div_equality [of b a, symmetric]) (simp add:dvd_imp_mod_0)
   188 
   189 lemma dvd_mult_div_cancel: "a dvd b \<Longrightarrow> a * (b div a) = b"
   190 by (drule dvd_div_mult_self) (simp add: mult_commute)
   191 
   192 lemma dvd_div_mult: "a dvd b \<Longrightarrow> (b div a) * c = b * c div a"
   193 apply (cases "a = 0")
   194  apply simp
   195 apply (auto simp: dvd_def mult_assoc)
   196 done
   197 
   198 lemma div_dvd_div[simp]:
   199   "a dvd b \<Longrightarrow> a dvd c \<Longrightarrow> (b div a dvd c div a) = (b dvd c)"
   200 apply (cases "a = 0")
   201  apply simp
   202 apply (unfold dvd_def)
   203 apply auto
   204  apply(blast intro:mult_assoc[symmetric])
   205 apply(fastsimp simp add: mult_assoc)
   206 done
   207 
   208 lemma dvd_mod_imp_dvd: "[| k dvd m mod n;  k dvd n |] ==> k dvd m"
   209   apply (subgoal_tac "k dvd (m div n) *n + m mod n")
   210    apply (simp add: mod_div_equality)
   211   apply (simp only: dvd_add dvd_mult)
   212   done
   213 
   214 text {* Addition respects modular equivalence. *}
   215 
   216 lemma mod_add_left_eq: "(a + b) mod c = (a mod c + b) mod c"
   217 proof -
   218   have "(a + b) mod c = (a div c * c + a mod c + b) mod c"
   219     by (simp only: mod_div_equality)
   220   also have "\<dots> = (a mod c + b + a div c * c) mod c"
   221     by (simp only: add_ac)
   222   also have "\<dots> = (a mod c + b) mod c"
   223     by (rule mod_mult_self1)
   224   finally show ?thesis .
   225 qed
   226 
   227 lemma mod_add_right_eq: "(a + b) mod c = (a + b mod c) mod c"
   228 proof -
   229   have "(a + b) mod c = (a + (b div c * c + b mod c)) mod c"
   230     by (simp only: mod_div_equality)
   231   also have "\<dots> = (a + b mod c + b div c * c) mod c"
   232     by (simp only: add_ac)
   233   also have "\<dots> = (a + b mod c) mod c"
   234     by (rule mod_mult_self1)
   235   finally show ?thesis .
   236 qed
   237 
   238 lemma mod_add_eq: "(a + b) mod c = (a mod c + b mod c) mod c"
   239 by (rule trans [OF mod_add_left_eq mod_add_right_eq])
   240 
   241 lemma mod_add_cong:
   242   assumes "a mod c = a' mod c"
   243   assumes "b mod c = b' mod c"
   244   shows "(a + b) mod c = (a' + b') mod c"
   245 proof -
   246   have "(a mod c + b mod c) mod c = (a' mod c + b' mod c) mod c"
   247     unfolding assms ..
   248   thus ?thesis
   249     by (simp only: mod_add_eq [symmetric])
   250 qed
   251 
   252 lemma div_add [simp]: "z dvd x \<Longrightarrow> z dvd y
   253   \<Longrightarrow> (x + y) div z = x div z + y div z"
   254 by (cases "z = 0", simp, unfold dvd_def, auto simp add: algebra_simps)
   255 
   256 text {* Multiplication respects modular equivalence. *}
   257 
   258 lemma mod_mult_left_eq: "(a * b) mod c = ((a mod c) * b) mod c"
   259 proof -
   260   have "(a * b) mod c = ((a div c * c + a mod c) * b) mod c"
   261     by (simp only: mod_div_equality)
   262   also have "\<dots> = (a mod c * b + a div c * b * c) mod c"
   263     by (simp only: algebra_simps)
   264   also have "\<dots> = (a mod c * b) mod c"
   265     by (rule mod_mult_self1)
   266   finally show ?thesis .
   267 qed
   268 
   269 lemma mod_mult_right_eq: "(a * b) mod c = (a * (b mod c)) mod c"
   270 proof -
   271   have "(a * b) mod c = (a * (b div c * c + b mod c)) mod c"
   272     by (simp only: mod_div_equality)
   273   also have "\<dots> = (a * (b mod c) + a * (b div c) * c) mod c"
   274     by (simp only: algebra_simps)
   275   also have "\<dots> = (a * (b mod c)) mod c"
   276     by (rule mod_mult_self1)
   277   finally show ?thesis .
   278 qed
   279 
   280 lemma mod_mult_eq: "(a * b) mod c = ((a mod c) * (b mod c)) mod c"
   281 by (rule trans [OF mod_mult_left_eq mod_mult_right_eq])
   282 
   283 lemma mod_mult_cong:
   284   assumes "a mod c = a' mod c"
   285   assumes "b mod c = b' mod c"
   286   shows "(a * b) mod c = (a' * b') mod c"
   287 proof -
   288   have "(a mod c * (b mod c)) mod c = (a' mod c * (b' mod c)) mod c"
   289     unfolding assms ..
   290   thus ?thesis
   291     by (simp only: mod_mult_eq [symmetric])
   292 qed
   293 
   294 lemma mod_mod_cancel:
   295   assumes "c dvd b"
   296   shows "a mod b mod c = a mod c"
   297 proof -
   298   from `c dvd b` obtain k where "b = c * k"
   299     by (rule dvdE)
   300   have "a mod b mod c = a mod (c * k) mod c"
   301     by (simp only: `b = c * k`)
   302   also have "\<dots> = (a mod (c * k) + a div (c * k) * k * c) mod c"
   303     by (simp only: mod_mult_self1)
   304   also have "\<dots> = (a div (c * k) * (c * k) + a mod (c * k)) mod c"
   305     by (simp only: add_ac mult_ac)
   306   also have "\<dots> = a mod c"
   307     by (simp only: mod_div_equality)
   308   finally show ?thesis .
   309 qed
   310 
   311 lemma div_mult_div_if_dvd:
   312   "y dvd x \<Longrightarrow> z dvd w \<Longrightarrow> (x div y) * (w div z) = (x * w) div (y * z)"
   313   apply (cases "y = 0", simp)
   314   apply (cases "z = 0", simp)
   315   apply (auto elim!: dvdE simp add: algebra_simps)
   316   apply (subst mult_assoc [symmetric])
   317   apply (simp add: no_zero_divisors)
   318   done
   319 
   320 lemma div_mult_mult2 [simp]:
   321   "c \<noteq> 0 \<Longrightarrow> (a * c) div (b * c) = a div b"
   322   by (drule div_mult_mult1) (simp add: mult_commute)
   323 
   324 lemma div_mult_mult1_if [simp]:
   325   "(c * a) div (c * b) = (if c = 0 then 0 else a div b)"
   326   by simp_all
   327 
   328 lemma mod_mult_mult1:
   329   "(c * a) mod (c * b) = c * (a mod b)"
   330 proof (cases "c = 0")
   331   case True then show ?thesis by simp
   332 next
   333   case False
   334   from mod_div_equality
   335   have "((c * a) div (c * b)) * (c * b) + (c * a) mod (c * b) = c * a" .
   336   with False have "c * ((a div b) * b + a mod b) + (c * a) mod (c * b)
   337     = c * a + c * (a mod b)" by (simp add: algebra_simps)
   338   with mod_div_equality show ?thesis by simp 
   339 qed
   340   
   341 lemma mod_mult_mult2:
   342   "(a * c) mod (b * c) = (a mod b) * c"
   343   using mod_mult_mult1 [of c a b] by (simp add: mult_commute)
   344 
   345 lemma dvd_mod: "k dvd m \<Longrightarrow> k dvd n \<Longrightarrow> k dvd (m mod n)"
   346   unfolding dvd_def by (auto simp add: mod_mult_mult1)
   347 
   348 lemma dvd_mod_iff: "k dvd n \<Longrightarrow> k dvd (m mod n) \<longleftrightarrow> k dvd m"
   349 by (blast intro: dvd_mod_imp_dvd dvd_mod)
   350 
   351 lemma div_power:
   352   "y dvd x \<Longrightarrow> (x div y) ^ n = x ^ n div y ^ n"
   353 apply (induct n)
   354  apply simp
   355 apply(simp add: div_mult_div_if_dvd dvd_power_same)
   356 done
   357 
   358 end
   359 
   360 class ring_div = semiring_div + idom
   361 begin
   362 
   363 text {* Negation respects modular equivalence. *}
   364 
   365 lemma mod_minus_eq: "(- a) mod b = (- (a mod b)) mod b"
   366 proof -
   367   have "(- a) mod b = (- (a div b * b + a mod b)) mod b"
   368     by (simp only: mod_div_equality)
   369   also have "\<dots> = (- (a mod b) + - (a div b) * b) mod b"
   370     by (simp only: minus_add_distrib minus_mult_left add_ac)
   371   also have "\<dots> = (- (a mod b)) mod b"
   372     by (rule mod_mult_self1)
   373   finally show ?thesis .
   374 qed
   375 
   376 lemma mod_minus_cong:
   377   assumes "a mod b = a' mod b"
   378   shows "(- a) mod b = (- a') mod b"
   379 proof -
   380   have "(- (a mod b)) mod b = (- (a' mod b)) mod b"
   381     unfolding assms ..
   382   thus ?thesis
   383     by (simp only: mod_minus_eq [symmetric])
   384 qed
   385 
   386 text {* Subtraction respects modular equivalence. *}
   387 
   388 lemma mod_diff_left_eq: "(a - b) mod c = (a mod c - b) mod c"
   389   unfolding diff_minus
   390   by (intro mod_add_cong mod_minus_cong) simp_all
   391 
   392 lemma mod_diff_right_eq: "(a - b) mod c = (a - b mod c) mod c"
   393   unfolding diff_minus
   394   by (intro mod_add_cong mod_minus_cong) simp_all
   395 
   396 lemma mod_diff_eq: "(a - b) mod c = (a mod c - b mod c) mod c"
   397   unfolding diff_minus
   398   by (intro mod_add_cong mod_minus_cong) simp_all
   399 
   400 lemma mod_diff_cong:
   401   assumes "a mod c = a' mod c"
   402   assumes "b mod c = b' mod c"
   403   shows "(a - b) mod c = (a' - b') mod c"
   404   unfolding diff_minus using assms
   405   by (intro mod_add_cong mod_minus_cong)
   406 
   407 lemma dvd_neg_div: "y dvd x \<Longrightarrow> -x div y = - (x div y)"
   408 apply (case_tac "y = 0") apply simp
   409 apply (auto simp add: dvd_def)
   410 apply (subgoal_tac "-(y * k) = y * - k")
   411  apply (erule ssubst)
   412  apply (erule div_mult_self1_is_id)
   413 apply simp
   414 done
   415 
   416 lemma dvd_div_neg: "y dvd x \<Longrightarrow> x div -y = - (x div y)"
   417 apply (case_tac "y = 0") apply simp
   418 apply (auto simp add: dvd_def)
   419 apply (subgoal_tac "y * k = -y * -k")
   420  apply (erule ssubst)
   421  apply (rule div_mult_self1_is_id)
   422  apply simp
   423 apply simp
   424 done
   425 
   426 end
   427 
   428 
   429 subsection {* Division on @{typ nat} *}
   430 
   431 text {*
   432   We define @{const div} and @{const mod} on @{typ nat} by means
   433   of a characteristic relation with two input arguments
   434   @{term "m\<Colon>nat"}, @{term "n\<Colon>nat"} and two output arguments
   435   @{term "q\<Colon>nat"}(uotient) and @{term "r\<Colon>nat"}(emainder).
   436 *}
   437 
   438 definition divmod_rel :: "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat \<Rightarrow> bool" where
   439   "divmod_rel m n qr \<longleftrightarrow>
   440     m = fst qr * n + snd qr \<and>
   441       (if n = 0 then fst qr = 0 else if n > 0 then 0 \<le> snd qr \<and> snd qr < n else n < snd qr \<and> snd qr \<le> 0)"
   442 
   443 text {* @{const divmod_rel} is total: *}
   444 
   445 lemma divmod_rel_ex:
   446   obtains q r where "divmod_rel m n (q, r)"
   447 proof (cases "n = 0")
   448   case True  with that show thesis
   449     by (auto simp add: divmod_rel_def)
   450 next
   451   case False
   452   have "\<exists>q r. m = q * n + r \<and> r < n"
   453   proof (induct m)
   454     case 0 with `n \<noteq> 0`
   455     have "(0\<Colon>nat) = 0 * n + 0 \<and> 0 < n" by simp
   456     then show ?case by blast
   457   next
   458     case (Suc m) then obtain q' r'
   459       where m: "m = q' * n + r'" and n: "r' < n" by auto
   460     then show ?case proof (cases "Suc r' < n")
   461       case True
   462       from m n have "Suc m = q' * n + Suc r'" by simp
   463       with True show ?thesis by blast
   464     next
   465       case False then have "n \<le> Suc r'" by auto
   466       moreover from n have "Suc r' \<le> n" by auto
   467       ultimately have "n = Suc r'" by auto
   468       with m have "Suc m = Suc q' * n + 0" by simp
   469       with `n \<noteq> 0` show ?thesis by blast
   470     qed
   471   qed
   472   with that show thesis
   473     using `n \<noteq> 0` by (auto simp add: divmod_rel_def)
   474 qed
   475 
   476 text {* @{const divmod_rel} is injective: *}
   477 
   478 lemma divmod_rel_unique:
   479   assumes "divmod_rel m n qr"
   480     and "divmod_rel m n qr'"
   481   shows "qr = qr'"
   482 proof (cases "n = 0")
   483   case True with assms show ?thesis
   484     by (cases qr, cases qr')
   485       (simp add: divmod_rel_def)
   486 next
   487   case False
   488   have aux: "\<And>q r q' r'. q' * n + r' = q * n + r \<Longrightarrow> r < n \<Longrightarrow> q' \<le> (q\<Colon>nat)"
   489   apply (rule leI)
   490   apply (subst less_iff_Suc_add)
   491   apply (auto simp add: add_mult_distrib)
   492   done
   493   from `n \<noteq> 0` assms have "fst qr = fst qr'"
   494     by (auto simp add: divmod_rel_def intro: order_antisym dest: aux sym)
   495   moreover from this assms have "snd qr = snd qr'"
   496     by (simp add: divmod_rel_def)
   497   ultimately show ?thesis by (cases qr, cases qr') simp
   498 qed
   499 
   500 text {*
   501   We instantiate divisibility on the natural numbers by
   502   means of @{const divmod_rel}:
   503 *}
   504 
   505 instantiation nat :: semiring_div
   506 begin
   507 
   508 definition divmod :: "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat" where
   509   [code del]: "divmod m n = (THE qr. divmod_rel m n qr)"
   510 
   511 lemma divmod_rel_divmod:
   512   "divmod_rel m n (divmod m n)"
   513 proof -
   514   from divmod_rel_ex
   515     obtain qr where rel: "divmod_rel m n qr" .
   516   then show ?thesis
   517   by (auto simp add: divmod_def intro: theI elim: divmod_rel_unique)
   518 qed
   519 
   520 lemma divmod_eq:
   521   assumes "divmod_rel m n qr" 
   522   shows "divmod m n = qr"
   523   using assms by (auto intro: divmod_rel_unique divmod_rel_divmod)
   524 
   525 definition div_nat where
   526   "m div n = fst (divmod m n)"
   527 
   528 definition mod_nat where
   529   "m mod n = snd (divmod m n)"
   530 
   531 lemma divmod_div_mod:
   532   "divmod m n = (m div n, m mod n)"
   533   unfolding div_nat_def mod_nat_def by simp
   534 
   535 lemma div_eq:
   536   assumes "divmod_rel m n (q, r)" 
   537   shows "m div n = q"
   538   using assms by (auto dest: divmod_eq simp add: divmod_div_mod)
   539 
   540 lemma mod_eq:
   541   assumes "divmod_rel m n (q, r)" 
   542   shows "m mod n = r"
   543   using assms by (auto dest: divmod_eq simp add: divmod_div_mod)
   544 
   545 lemma divmod_rel: "divmod_rel m n (m div n, m mod n)"
   546   by (simp add: div_nat_def mod_nat_def divmod_rel_divmod)
   547 
   548 lemma divmod_zero:
   549   "divmod m 0 = (0, m)"
   550 proof -
   551   from divmod_rel [of m 0] show ?thesis
   552     unfolding divmod_div_mod divmod_rel_def by simp
   553 qed
   554 
   555 lemma divmod_base:
   556   assumes "m < n"
   557   shows "divmod m n = (0, m)"
   558 proof -
   559   from divmod_rel [of m n] show ?thesis
   560     unfolding divmod_div_mod divmod_rel_def
   561     using assms by (cases "m div n = 0")
   562       (auto simp add: gr0_conv_Suc [of "m div n"])
   563 qed
   564 
   565 lemma divmod_step:
   566   assumes "0 < n" and "n \<le> m"
   567   shows "divmod m n = (Suc ((m - n) div n), (m - n) mod n)"
   568 proof -
   569   from divmod_rel have divmod_m_n: "divmod_rel m n (m div n, m mod n)" .
   570   with assms have m_div_n: "m div n \<ge> 1"
   571     by (cases "m div n") (auto simp add: divmod_rel_def)
   572   from assms divmod_m_n have "divmod_rel (m - n) n (m div n - Suc 0, m mod n)"
   573     by (cases "m div n") (auto simp add: divmod_rel_def)
   574   with divmod_eq have "divmod (m - n) n = (m div n - Suc 0, m mod n)" by simp
   575   moreover from divmod_div_mod have "divmod (m - n) n = ((m - n) div n, (m - n) mod n)" .
   576   ultimately have "m div n = Suc ((m - n) div n)"
   577     and "m mod n = (m - n) mod n" using m_div_n by simp_all
   578   then show ?thesis using divmod_div_mod by simp
   579 qed
   580 
   581 text {* The ''recursion'' equations for @{const div} and @{const mod} *}
   582 
   583 lemma div_less [simp]:
   584   fixes m n :: nat
   585   assumes "m < n"
   586   shows "m div n = 0"
   587   using assms divmod_base divmod_div_mod by simp
   588 
   589 lemma le_div_geq:
   590   fixes m n :: nat
   591   assumes "0 < n" and "n \<le> m"
   592   shows "m div n = Suc ((m - n) div n)"
   593   using assms divmod_step divmod_div_mod by simp
   594 
   595 lemma mod_less [simp]:
   596   fixes m n :: nat
   597   assumes "m < n"
   598   shows "m mod n = m"
   599   using assms divmod_base divmod_div_mod by simp
   600 
   601 lemma le_mod_geq:
   602   fixes m n :: nat
   603   assumes "n \<le> m"
   604   shows "m mod n = (m - n) mod n"
   605   using assms divmod_step divmod_div_mod by (cases "n = 0") simp_all
   606 
   607 instance proof -
   608   have [simp]: "\<And>n::nat. n div 0 = 0"
   609     by (simp add: div_nat_def divmod_zero)
   610   have [simp]: "\<And>n::nat. 0 div n = 0"
   611   proof -
   612     fix n :: nat
   613     show "0 div n = 0"
   614       by (cases "n = 0") simp_all
   615   qed
   616   show "OFCLASS(nat, semiring_div_class)" proof
   617     fix m n :: nat
   618     show "m div n * n + m mod n = m"
   619       using divmod_rel [of m n] by (simp add: divmod_rel_def)
   620   next
   621     fix m n q :: nat
   622     assume "n \<noteq> 0"
   623     then show "(q + m * n) div n = m + q div n"
   624       by (induct m) (simp_all add: le_div_geq)
   625   next
   626     fix m n q :: nat
   627     assume "m \<noteq> 0"
   628     then show "(m * n) div (m * q) = n div q"
   629     proof (cases "n \<noteq> 0 \<and> q \<noteq> 0")
   630       case False then show ?thesis by auto
   631     next
   632       case True with `m \<noteq> 0`
   633         have "m > 0" and "n > 0" and "q > 0" by auto
   634       then have "\<And>a b. divmod_rel n q (a, b) \<Longrightarrow> divmod_rel (m * n) (m * q) (a, m * b)"
   635         by (auto simp add: divmod_rel_def) (simp_all add: algebra_simps)
   636       moreover from divmod_rel have "divmod_rel n q (n div q, n mod q)" .
   637       ultimately have "divmod_rel (m * n) (m * q) (n div q, m * (n mod q))" .
   638       then show ?thesis by (simp add: div_eq)
   639     qed
   640   qed simp_all
   641 qed
   642 
   643 end
   644 
   645 text {* Simproc for cancelling @{const div} and @{const mod} *}
   646 
   647 ML {*
   648 local
   649 
   650 structure CancelDivMod = CancelDivModFun(struct
   651 
   652   val div_name = @{const_name div};
   653   val mod_name = @{const_name mod};
   654   val mk_binop = HOLogic.mk_binop;
   655   val mk_sum = Nat_Arith.mk_sum;
   656   val dest_sum = Nat_Arith.dest_sum;
   657 
   658   val div_mod_eqs = map mk_meta_eq [@{thm div_mod_equality}, @{thm div_mod_equality2}];
   659 
   660   val trans = trans;
   661 
   662   val prove_eq_sums = Arith_Data.prove_conv2 all_tac (Arith_Data.simp_all_tac
   663     (@{thm monoid_add_class.add_0_left} :: @{thm monoid_add_class.add_0_right} :: @{thms add_ac}))
   664 
   665 end)
   666 
   667 in
   668 
   669 val cancel_div_mod_nat_proc = Simplifier.simproc @{theory}
   670   "cancel_div_mod" ["(m::nat) + n"] (K CancelDivMod.proc);
   671 
   672 val _ = Addsimprocs [cancel_div_mod_nat_proc];
   673 
   674 end
   675 *}
   676 
   677 text {* code generator setup *}
   678 
   679 lemma divmod_if [code]: "divmod m n = (if n = 0 \<or> m < n then (0, m) else
   680   let (q, r) = divmod (m - n) n in (Suc q, r))"
   681 by (simp add: divmod_zero divmod_base divmod_step)
   682     (simp add: divmod_div_mod)
   683 
   684 code_modulename SML
   685   Divides Nat
   686 
   687 code_modulename OCaml
   688   Divides Nat
   689 
   690 code_modulename Haskell
   691   Divides Nat
   692 
   693 
   694 subsubsection {* Quotient *}
   695 
   696 lemma div_geq: "0 < n \<Longrightarrow>  \<not> m < n \<Longrightarrow> m div n = Suc ((m - n) div n)"
   697 by (simp add: le_div_geq linorder_not_less)
   698 
   699 lemma div_if: "0 < n \<Longrightarrow> m div n = (if m < n then 0 else Suc ((m - n) div n))"
   700 by (simp add: div_geq)
   701 
   702 lemma div_mult_self_is_m [simp]: "0<n ==> (m*n) div n = (m::nat)"
   703 by simp
   704 
   705 lemma div_mult_self1_is_m [simp]: "0<n ==> (n*m) div n = (m::nat)"
   706 by simp
   707 
   708 
   709 subsubsection {* Remainder *}
   710 
   711 lemma mod_less_divisor [simp]:
   712   fixes m n :: nat
   713   assumes "n > 0"
   714   shows "m mod n < (n::nat)"
   715   using assms divmod_rel [of m n] unfolding divmod_rel_def by auto
   716 
   717 lemma mod_less_eq_dividend [simp]:
   718   fixes m n :: nat
   719   shows "m mod n \<le> m"
   720 proof (rule add_leD2)
   721   from mod_div_equality have "m div n * n + m mod n = m" .
   722   then show "m div n * n + m mod n \<le> m" by auto
   723 qed
   724 
   725 lemma mod_geq: "\<not> m < (n\<Colon>nat) \<Longrightarrow> m mod n = (m - n) mod n"
   726 by (simp add: le_mod_geq linorder_not_less)
   727 
   728 lemma mod_if: "m mod (n\<Colon>nat) = (if m < n then m else (m - n) mod n)"
   729 by (simp add: le_mod_geq)
   730 
   731 lemma mod_1 [simp]: "m mod Suc 0 = 0"
   732 by (induct m) (simp_all add: mod_geq)
   733 
   734 lemma mod_mult_distrib: "(m mod n) * (k\<Colon>nat) = (m * k) mod (n * k)"
   735   apply (cases "n = 0", simp)
   736   apply (cases "k = 0", simp)
   737   apply (induct m rule: nat_less_induct)
   738   apply (subst mod_if, simp)
   739   apply (simp add: mod_geq diff_mult_distrib)
   740   done
   741 
   742 lemma mod_mult_distrib2: "(k::nat) * (m mod n) = (k*m) mod (k*n)"
   743 by (simp add: mult_commute [of k] mod_mult_distrib)
   744 
   745 (* a simple rearrangement of mod_div_equality: *)
   746 lemma mult_div_cancel: "(n::nat) * (m div n) = m - (m mod n)"
   747 by (cut_tac a = m and b = n in mod_div_equality2, arith)
   748 
   749 lemma mod_le_divisor[simp]: "0 < n \<Longrightarrow> m mod n \<le> (n::nat)"
   750   apply (drule mod_less_divisor [where m = m])
   751   apply simp
   752   done
   753 
   754 subsubsection {* Quotient and Remainder *}
   755 
   756 lemma divmod_rel_mult1_eq:
   757   "divmod_rel b c (q, r) \<Longrightarrow> c > 0
   758    \<Longrightarrow> divmod_rel (a * b) c (a * q + a * r div c, a * r mod c)"
   759 by (auto simp add: split_ifs divmod_rel_def algebra_simps)
   760 
   761 lemma div_mult1_eq:
   762   "(a * b) div c = a * (b div c) + a * (b mod c) div (c::nat)"
   763 apply (cases "c = 0", simp)
   764 apply (blast intro: divmod_rel [THEN divmod_rel_mult1_eq, THEN div_eq])
   765 done
   766 
   767 lemma divmod_rel_add1_eq:
   768   "divmod_rel a c (aq, ar) \<Longrightarrow> divmod_rel b c (bq, br) \<Longrightarrow>  c > 0
   769    \<Longrightarrow> divmod_rel (a + b) c (aq + bq + (ar + br) div c, (ar + br) mod c)"
   770 by (auto simp add: split_ifs divmod_rel_def algebra_simps)
   771 
   772 (*NOT suitable for rewriting: the RHS has an instance of the LHS*)
   773 lemma div_add1_eq:
   774   "(a+b) div (c::nat) = a div c + b div c + ((a mod c + b mod c) div c)"
   775 apply (cases "c = 0", simp)
   776 apply (blast intro: divmod_rel_add1_eq [THEN div_eq] divmod_rel)
   777 done
   778 
   779 lemma mod_lemma: "[| (0::nat) < c; r < b |] ==> b * (q mod c) + r < b * c"
   780   apply (cut_tac m = q and n = c in mod_less_divisor)
   781   apply (drule_tac [2] m = "q mod c" in less_imp_Suc_add, auto)
   782   apply (erule_tac P = "%x. ?lhs < ?rhs x" in ssubst)
   783   apply (simp add: add_mult_distrib2)
   784   done
   785 
   786 lemma divmod_rel_mult2_eq:
   787   "divmod_rel a b (q, r) \<Longrightarrow> 0 < b \<Longrightarrow> 0 < c
   788    \<Longrightarrow> divmod_rel a (b * c) (q div c, b *(q mod c) + r)"
   789 by (auto simp add: mult_ac divmod_rel_def add_mult_distrib2 [symmetric] mod_lemma)
   790 
   791 lemma div_mult2_eq: "a div (b*c) = (a div b) div (c::nat)"
   792   apply (cases "b = 0", simp)
   793   apply (cases "c = 0", simp)
   794   apply (force simp add: divmod_rel [THEN divmod_rel_mult2_eq, THEN div_eq])
   795   done
   796 
   797 lemma mod_mult2_eq: "a mod (b*c) = b*(a div b mod c) + a mod (b::nat)"
   798   apply (cases "b = 0", simp)
   799   apply (cases "c = 0", simp)
   800   apply (auto simp add: mult_commute divmod_rel [THEN divmod_rel_mult2_eq, THEN mod_eq])
   801   done
   802 
   803 
   804 subsubsection{*Further Facts about Quotient and Remainder*}
   805 
   806 lemma div_1 [simp]: "m div Suc 0 = m"
   807 by (induct m) (simp_all add: div_geq)
   808 
   809 
   810 (* Monotonicity of div in first argument *)
   811 lemma div_le_mono [rule_format (no_asm)]:
   812     "\<forall>m::nat. m \<le> n --> (m div k) \<le> (n div k)"
   813 apply (case_tac "k=0", simp)
   814 apply (induct "n" rule: nat_less_induct, clarify)
   815 apply (case_tac "n<k")
   816 (* 1  case n<k *)
   817 apply simp
   818 (* 2  case n >= k *)
   819 apply (case_tac "m<k")
   820 (* 2.1  case m<k *)
   821 apply simp
   822 (* 2.2  case m>=k *)
   823 apply (simp add: div_geq diff_le_mono)
   824 done
   825 
   826 (* Antimonotonicity of div in second argument *)
   827 lemma div_le_mono2: "!!m::nat. [| 0<m; m\<le>n |] ==> (k div n) \<le> (k div m)"
   828 apply (subgoal_tac "0<n")
   829  prefer 2 apply simp
   830 apply (induct_tac k rule: nat_less_induct)
   831 apply (rename_tac "k")
   832 apply (case_tac "k<n", simp)
   833 apply (subgoal_tac "~ (k<m) ")
   834  prefer 2 apply simp
   835 apply (simp add: div_geq)
   836 apply (subgoal_tac "(k-n) div n \<le> (k-m) div n")
   837  prefer 2
   838  apply (blast intro: div_le_mono diff_le_mono2)
   839 apply (rule le_trans, simp)
   840 apply (simp)
   841 done
   842 
   843 lemma div_le_dividend [simp]: "m div n \<le> (m::nat)"
   844 apply (case_tac "n=0", simp)
   845 apply (subgoal_tac "m div n \<le> m div 1", simp)
   846 apply (rule div_le_mono2)
   847 apply (simp_all (no_asm_simp))
   848 done
   849 
   850 (* Similar for "less than" *)
   851 lemma div_less_dividend [rule_format]:
   852      "!!n::nat. 1<n ==> 0 < m --> m div n < m"
   853 apply (induct_tac m rule: nat_less_induct)
   854 apply (rename_tac "m")
   855 apply (case_tac "m<n", simp)
   856 apply (subgoal_tac "0<n")
   857  prefer 2 apply simp
   858 apply (simp add: div_geq)
   859 apply (case_tac "n<m")
   860  apply (subgoal_tac "(m-n) div n < (m-n) ")
   861   apply (rule impI less_trans_Suc)+
   862 apply assumption
   863   apply (simp_all)
   864 done
   865 
   866 declare div_less_dividend [simp]
   867 
   868 text{*A fact for the mutilated chess board*}
   869 lemma mod_Suc: "Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))"
   870 apply (case_tac "n=0", simp)
   871 apply (induct "m" rule: nat_less_induct)
   872 apply (case_tac "Suc (na) <n")
   873 (* case Suc(na) < n *)
   874 apply (frule lessI [THEN less_trans], simp add: less_not_refl3)
   875 (* case n \<le> Suc(na) *)
   876 apply (simp add: linorder_not_less le_Suc_eq mod_geq)
   877 apply (auto simp add: Suc_diff_le le_mod_geq)
   878 done
   879 
   880 lemma mod_eq_0_iff: "(m mod d = 0) = (\<exists>q::nat. m = d*q)"
   881 by (auto simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)
   882 
   883 lemmas mod_eq_0D [dest!] = mod_eq_0_iff [THEN iffD1]
   884 
   885 (*Loses information, namely we also have r<d provided d is nonzero*)
   886 lemma mod_eqD: "(m mod d = r) ==> \<exists>q::nat. m = r + q*d"
   887   apply (cut_tac a = m in mod_div_equality)
   888   apply (simp only: add_ac)
   889   apply (blast intro: sym)
   890   done
   891 
   892 lemma split_div:
   893  "P(n div k :: nat) =
   894  ((k = 0 \<longrightarrow> P 0) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P i)))"
   895  (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
   896 proof
   897   assume P: ?P
   898   show ?Q
   899   proof (cases)
   900     assume "k = 0"
   901     with P show ?Q by simp
   902   next
   903     assume not0: "k \<noteq> 0"
   904     thus ?Q
   905     proof (simp, intro allI impI)
   906       fix i j
   907       assume n: "n = k*i + j" and j: "j < k"
   908       show "P i"
   909       proof (cases)
   910         assume "i = 0"
   911         with n j P show "P i" by simp
   912       next
   913         assume "i \<noteq> 0"
   914         with not0 n j P show "P i" by(simp add:add_ac)
   915       qed
   916     qed
   917   qed
   918 next
   919   assume Q: ?Q
   920   show ?P
   921   proof (cases)
   922     assume "k = 0"
   923     with Q show ?P by simp
   924   next
   925     assume not0: "k \<noteq> 0"
   926     with Q have R: ?R by simp
   927     from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
   928     show ?P by simp
   929   qed
   930 qed
   931 
   932 lemma split_div_lemma:
   933   assumes "0 < n"
   934   shows "n * q \<le> m \<and> m < n * Suc q \<longleftrightarrow> q = ((m\<Colon>nat) div n)" (is "?lhs \<longleftrightarrow> ?rhs")
   935 proof
   936   assume ?rhs
   937   with mult_div_cancel have nq: "n * q = m - (m mod n)" by simp
   938   then have A: "n * q \<le> m" by simp
   939   have "n - (m mod n) > 0" using mod_less_divisor assms by auto
   940   then have "m < m + (n - (m mod n))" by simp
   941   then have "m < n + (m - (m mod n))" by simp
   942   with nq have "m < n + n * q" by simp
   943   then have B: "m < n * Suc q" by simp
   944   from A B show ?lhs ..
   945 next
   946   assume P: ?lhs
   947   then have "divmod_rel m n (q, m - n * q)"
   948     unfolding divmod_rel_def by (auto simp add: mult_ac)
   949   with divmod_rel_unique divmod_rel [of m n]
   950   have "(q, m - n * q) = (m div n, m mod n)" by auto
   951   then show ?rhs by simp
   952 qed
   953 
   954 theorem split_div':
   955   "P ((m::nat) div n) = ((n = 0 \<and> P 0) \<or>
   956    (\<exists>q. (n * q \<le> m \<and> m < n * (Suc q)) \<and> P q))"
   957   apply (case_tac "0 < n")
   958   apply (simp only: add: split_div_lemma)
   959   apply simp_all
   960   done
   961 
   962 lemma split_mod:
   963  "P(n mod k :: nat) =
   964  ((k = 0 \<longrightarrow> P n) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P j)))"
   965  (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
   966 proof
   967   assume P: ?P
   968   show ?Q
   969   proof (cases)
   970     assume "k = 0"
   971     with P show ?Q by simp
   972   next
   973     assume not0: "k \<noteq> 0"
   974     thus ?Q
   975     proof (simp, intro allI impI)
   976       fix i j
   977       assume "n = k*i + j" "j < k"
   978       thus "P j" using not0 P by(simp add:add_ac mult_ac)
   979     qed
   980   qed
   981 next
   982   assume Q: ?Q
   983   show ?P
   984   proof (cases)
   985     assume "k = 0"
   986     with Q show ?P by simp
   987   next
   988     assume not0: "k \<noteq> 0"
   989     with Q have R: ?R by simp
   990     from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
   991     show ?P by simp
   992   qed
   993 qed
   994 
   995 theorem mod_div_equality': "(m::nat) mod n = m - (m div n) * n"
   996   apply (rule_tac P="%x. m mod n = x - (m div n) * n" in
   997     subst [OF mod_div_equality [of _ n]])
   998   apply arith
   999   done
  1000 
  1001 lemma div_mod_equality':
  1002   fixes m n :: nat
  1003   shows "m div n * n = m - m mod n"
  1004 proof -
  1005   have "m mod n \<le> m mod n" ..
  1006   from div_mod_equality have 
  1007     "m div n * n + m mod n - m mod n = m - m mod n" by simp
  1008   with diff_add_assoc [OF `m mod n \<le> m mod n`, of "m div n * n"] have
  1009     "m div n * n + (m mod n - m mod n) = m - m mod n"
  1010     by simp
  1011   then show ?thesis by simp
  1012 qed
  1013 
  1014 
  1015 subsubsection {*An ``induction'' law for modulus arithmetic.*}
  1016 
  1017 lemma mod_induct_0:
  1018   assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
  1019   and base: "P i" and i: "i<p"
  1020   shows "P 0"
  1021 proof (rule ccontr)
  1022   assume contra: "\<not>(P 0)"
  1023   from i have p: "0<p" by simp
  1024   have "\<forall>k. 0<k \<longrightarrow> \<not> P (p-k)" (is "\<forall>k. ?A k")
  1025   proof
  1026     fix k
  1027     show "?A k"
  1028     proof (induct k)
  1029       show "?A 0" by simp  -- "by contradiction"
  1030     next
  1031       fix n
  1032       assume ih: "?A n"
  1033       show "?A (Suc n)"
  1034       proof (clarsimp)
  1035         assume y: "P (p - Suc n)"
  1036         have n: "Suc n < p"
  1037         proof (rule ccontr)
  1038           assume "\<not>(Suc n < p)"
  1039           hence "p - Suc n = 0"
  1040             by simp
  1041           with y contra show "False"
  1042             by simp
  1043         qed
  1044         hence n2: "Suc (p - Suc n) = p-n" by arith
  1045         from p have "p - Suc n < p" by arith
  1046         with y step have z: "P ((Suc (p - Suc n)) mod p)"
  1047           by blast
  1048         show "False"
  1049         proof (cases "n=0")
  1050           case True
  1051           with z n2 contra show ?thesis by simp
  1052         next
  1053           case False
  1054           with p have "p-n < p" by arith
  1055           with z n2 False ih show ?thesis by simp
  1056         qed
  1057       qed
  1058     qed
  1059   qed
  1060   moreover
  1061   from i obtain k where "0<k \<and> i+k=p"
  1062     by (blast dest: less_imp_add_positive)
  1063   hence "0<k \<and> i=p-k" by auto
  1064   moreover
  1065   note base
  1066   ultimately
  1067   show "False" by blast
  1068 qed
  1069 
  1070 lemma mod_induct:
  1071   assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
  1072   and base: "P i" and i: "i<p" and j: "j<p"
  1073   shows "P j"
  1074 proof -
  1075   have "\<forall>j<p. P j"
  1076   proof
  1077     fix j
  1078     show "j<p \<longrightarrow> P j" (is "?A j")
  1079     proof (induct j)
  1080       from step base i show "?A 0"
  1081         by (auto elim: mod_induct_0)
  1082     next
  1083       fix k
  1084       assume ih: "?A k"
  1085       show "?A (Suc k)"
  1086       proof
  1087         assume suc: "Suc k < p"
  1088         hence k: "k<p" by simp
  1089         with ih have "P k" ..
  1090         with step k have "P (Suc k mod p)"
  1091           by blast
  1092         moreover
  1093         from suc have "Suc k mod p = Suc k"
  1094           by simp
  1095         ultimately
  1096         show "P (Suc k)" by simp
  1097       qed
  1098     qed
  1099   qed
  1100   with j show ?thesis by blast
  1101 qed
  1102 
  1103 lemma div2_Suc_Suc [simp]: "Suc (Suc m) div 2 = Suc (m div 2)"
  1104 by (auto simp add: numeral_2_eq_2 le_div_geq)
  1105 
  1106 lemma add_self_div_2 [simp]: "(m + m) div 2 = (m::nat)"
  1107 by (simp add: nat_mult_2 [symmetric])
  1108 
  1109 lemma mod2_Suc_Suc [simp]: "Suc(Suc(m)) mod 2 = m mod 2"
  1110 apply (subgoal_tac "m mod 2 < 2")
  1111 apply (erule less_2_cases [THEN disjE])
  1112 apply (simp_all (no_asm_simp) add: Let_def mod_Suc nat_1)
  1113 done
  1114 
  1115 lemma mod2_gr_0 [simp]: "0 < (m\<Colon>nat) mod 2 \<longleftrightarrow> m mod 2 = 1"
  1116 proof -
  1117   { fix n :: nat have  "(n::nat) < 2 \<Longrightarrow> n = 0 \<or> n = 1" by (induct n) simp_all }
  1118   moreover have "m mod 2 < 2" by simp
  1119   ultimately have "m mod 2 = 0 \<or> m mod 2 = 1" .
  1120   then show ?thesis by auto
  1121 qed
  1122 
  1123 text{*These lemmas collapse some needless occurrences of Suc:
  1124     at least three Sucs, since two and fewer are rewritten back to Suc again!
  1125     We already have some rules to simplify operands smaller than 3.*}
  1126 
  1127 lemma div_Suc_eq_div_add3 [simp]: "m div (Suc (Suc (Suc n))) = m div (3+n)"
  1128 by (simp add: Suc3_eq_add_3)
  1129 
  1130 lemma mod_Suc_eq_mod_add3 [simp]: "m mod (Suc (Suc (Suc n))) = m mod (3+n)"
  1131 by (simp add: Suc3_eq_add_3)
  1132 
  1133 lemma Suc_div_eq_add3_div: "(Suc (Suc (Suc m))) div n = (3+m) div n"
  1134 by (simp add: Suc3_eq_add_3)
  1135 
  1136 lemma Suc_mod_eq_add3_mod: "(Suc (Suc (Suc m))) mod n = (3+m) mod n"
  1137 by (simp add: Suc3_eq_add_3)
  1138 
  1139 lemmas Suc_div_eq_add3_div_number_of =
  1140     Suc_div_eq_add3_div [of _ "number_of v", standard]
  1141 declare Suc_div_eq_add3_div_number_of [simp]
  1142 
  1143 lemmas Suc_mod_eq_add3_mod_number_of =
  1144     Suc_mod_eq_add3_mod [of _ "number_of v", standard]
  1145 declare Suc_mod_eq_add3_mod_number_of [simp]
  1146 
  1147 
  1148 subsection {* Proof Tools setup; Combination and Cancellation Simprocs *}
  1149 
  1150 declare split_div[of _ _ "number_of k", standard, arith_split]
  1151 declare split_mod[of _ _ "number_of k", standard, arith_split]
  1152 
  1153 
  1154 subsubsection{*For @{text combine_numerals}*}
  1155 
  1156 lemma left_add_mult_distrib: "i*u + (j*u + k) = (i+j)*u + (k::nat)"
  1157 by (simp add: add_mult_distrib)
  1158 
  1159 
  1160 subsubsection{*For @{text cancel_numerals}*}
  1161 
  1162 lemma nat_diff_add_eq1:
  1163      "j <= (i::nat) ==> ((i*u + m) - (j*u + n)) = (((i-j)*u + m) - n)"
  1164 by (simp split add: nat_diff_split add: add_mult_distrib)
  1165 
  1166 lemma nat_diff_add_eq2:
  1167      "i <= (j::nat) ==> ((i*u + m) - (j*u + n)) = (m - ((j-i)*u + n))"
  1168 by (simp split add: nat_diff_split add: add_mult_distrib)
  1169 
  1170 lemma nat_eq_add_iff1:
  1171      "j <= (i::nat) ==> (i*u + m = j*u + n) = ((i-j)*u + m = n)"
  1172 by (auto split add: nat_diff_split simp add: add_mult_distrib)
  1173 
  1174 lemma nat_eq_add_iff2:
  1175      "i <= (j::nat) ==> (i*u + m = j*u + n) = (m = (j-i)*u + n)"
  1176 by (auto split add: nat_diff_split simp add: add_mult_distrib)
  1177 
  1178 lemma nat_less_add_iff1:
  1179      "j <= (i::nat) ==> (i*u + m < j*u + n) = ((i-j)*u + m < n)"
  1180 by (auto split add: nat_diff_split simp add: add_mult_distrib)
  1181 
  1182 lemma nat_less_add_iff2:
  1183      "i <= (j::nat) ==> (i*u + m < j*u + n) = (m < (j-i)*u + n)"
  1184 by (auto split add: nat_diff_split simp add: add_mult_distrib)
  1185 
  1186 lemma nat_le_add_iff1:
  1187      "j <= (i::nat) ==> (i*u + m <= j*u + n) = ((i-j)*u + m <= n)"
  1188 by (auto split add: nat_diff_split simp add: add_mult_distrib)
  1189 
  1190 lemma nat_le_add_iff2:
  1191      "i <= (j::nat) ==> (i*u + m <= j*u + n) = (m <= (j-i)*u + n)"
  1192 by (auto split add: nat_diff_split simp add: add_mult_distrib)
  1193 
  1194 
  1195 subsubsection{*For @{text cancel_numeral_factors} *}
  1196 
  1197 lemma nat_mult_le_cancel1: "(0::nat) < k ==> (k*m <= k*n) = (m<=n)"
  1198 by auto
  1199 
  1200 lemma nat_mult_less_cancel1: "(0::nat) < k ==> (k*m < k*n) = (m<n)"
  1201 by auto
  1202 
  1203 lemma nat_mult_eq_cancel1: "(0::nat) < k ==> (k*m = k*n) = (m=n)"
  1204 by auto
  1205 
  1206 lemma nat_mult_div_cancel1: "(0::nat) < k ==> (k*m) div (k*n) = (m div n)"
  1207 by auto
  1208 
  1209 lemma nat_mult_dvd_cancel_disj[simp]:
  1210   "(k*m) dvd (k*n) = (k=0 | m dvd (n::nat))"
  1211 by(auto simp: dvd_eq_mod_eq_0 mod_mult_distrib2[symmetric])
  1212 
  1213 lemma nat_mult_dvd_cancel1: "0 < k \<Longrightarrow> (k*m) dvd (k*n::nat) = (m dvd n)"
  1214 by(auto)
  1215 
  1216 
  1217 subsubsection{*For @{text cancel_factor} *}
  1218 
  1219 lemma nat_mult_le_cancel_disj: "(k*m <= k*n) = ((0::nat) < k --> m<=n)"
  1220 by auto
  1221 
  1222 lemma nat_mult_less_cancel_disj: "(k*m < k*n) = ((0::nat) < k & m<n)"
  1223 by auto
  1224 
  1225 lemma nat_mult_eq_cancel_disj: "(k*m = k*n) = (k = (0::nat) | m=n)"
  1226 by auto
  1227 
  1228 lemma nat_mult_div_cancel_disj[simp]:
  1229      "(k*m) div (k*n) = (if k = (0::nat) then 0 else m div n)"
  1230 by (simp add: nat_mult_div_cancel1)
  1231 
  1232 
  1233 use "Tools/numeral_simprocs.ML"
  1234 
  1235 use "Tools/nat_numeral_simprocs.ML"
  1236 
  1237 declaration {* 
  1238   K (Lin_Arith.add_simps (@{thms neg_simps} @ [@{thm Suc_nat_number_of}, @{thm int_nat_number_of}])
  1239   #> Lin_Arith.add_simps (@{thms ring_distribs} @ [@{thm Let_number_of}, @{thm Let_0}, @{thm Let_1},
  1240      @{thm nat_0}, @{thm nat_1},
  1241      @{thm add_nat_number_of}, @{thm diff_nat_number_of}, @{thm mult_nat_number_of},
  1242      @{thm eq_nat_number_of}, @{thm less_nat_number_of}, @{thm le_number_of_eq_not_less},
  1243      @{thm le_Suc_number_of}, @{thm le_number_of_Suc},
  1244      @{thm less_Suc_number_of}, @{thm less_number_of_Suc},
  1245      @{thm Suc_eq_number_of}, @{thm eq_number_of_Suc},
  1246      @{thm mult_Suc}, @{thm mult_Suc_right},
  1247      @{thm add_Suc}, @{thm add_Suc_right},
  1248      @{thm eq_number_of_0}, @{thm eq_0_number_of}, @{thm less_0_number_of},
  1249      @{thm of_int_number_of_eq}, @{thm of_nat_number_of_eq}, @{thm nat_number_of},
  1250      @{thm if_True}, @{thm if_False}])
  1251   #> Lin_Arith.add_simprocs (Numeral_Simprocs.assoc_fold_simproc
  1252       :: Numeral_Simprocs.combine_numerals
  1253       :: Numeral_Simprocs.cancel_numerals)
  1254   #> Lin_Arith.add_simprocs (Nat_Numeral_Simprocs.combine_numerals :: Nat_Numeral_Simprocs.cancel_numerals))
  1255 *}
  1256 
  1257 end