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