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