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