src/HOL/Divides.thy
author haftmann
Thu Oct 29 22:13:09 2009 +0100 (2009-10-29)
changeset 33340 a165b97f3658
parent 33318 ddd97d9dfbfb
child 33361 1f18de40b43f
permissions -rw-r--r--
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
paulson@3366
     1
(*  Title:      HOL/Divides.thy
paulson@3366
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@6865
     3
    Copyright   1999  University of Cambridge
huffman@18154
     4
*)
paulson@3366
     5
haftmann@27651
     6
header {* The division operators div and mod *}
paulson@3366
     7
nipkow@15131
     8
theory Divides
haftmann@33318
     9
imports Nat_Numeral Nat_Transfer
haftmann@33340
    10
uses "~~/src/Provers/Arith/cancel_div_mod.ML"
nipkow@15131
    11
begin
paulson@3366
    12
haftmann@25942
    13
subsection {* Syntactic division operations *}
haftmann@25942
    14
haftmann@27651
    15
class div = dvd +
haftmann@27540
    16
  fixes div :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "div" 70)
haftmann@27651
    17
    and mod :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "mod" 70)
haftmann@27540
    18
haftmann@27540
    19
haftmann@27651
    20
subsection {* Abstract division in commutative semirings. *}
haftmann@25942
    21
haftmann@30930
    22
class semiring_div = comm_semiring_1_cancel + no_zero_divisors + div +
haftmann@25942
    23
  assumes mod_div_equality: "a div b * b + a mod b = a"
haftmann@27651
    24
    and div_by_0 [simp]: "a div 0 = 0"
haftmann@27651
    25
    and div_0 [simp]: "0 div a = 0"
haftmann@27651
    26
    and div_mult_self1 [simp]: "b \<noteq> 0 \<Longrightarrow> (a + c * b) div b = c + a div b"
haftmann@30930
    27
    and div_mult_mult1 [simp]: "c \<noteq> 0 \<Longrightarrow> (c * a) div (c * b) = 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@30934
    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@30934
    44
  by (simp add: mod_div_equality2)
haftmann@26062
    45
haftmann@27651
    46
lemma mod_by_0 [simp]: "a mod 0 = a"
haftmann@30934
    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@30934
    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"
nipkow@29667
    66
      by (simp add: algebra_simps)
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@30934
    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
haftmann@31998
   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
nipkow@29925
   175
lemma dvd_imp_mod_0: "a dvd b \<Longrightarrow> b mod a = 0"
nipkow@29948
   176
by (rule dvd_eq_mod_eq_0[THEN iffD1])
nipkow@29925
   177
nipkow@29925
   178
lemma dvd_div_mult_self: "a dvd b \<Longrightarrow> (b div a) * a = b"
nipkow@29925
   179
by (subst (2) mod_div_equality [of b a, symmetric]) (simp add:dvd_imp_mod_0)
nipkow@29925
   180
haftmann@33274
   181
lemma dvd_mult_div_cancel: "a dvd b \<Longrightarrow> a * (b div a) = b"
haftmann@33274
   182
by (drule dvd_div_mult_self) (simp add: mult_commute)
haftmann@33274
   183
nipkow@30052
   184
lemma dvd_div_mult: "a dvd b \<Longrightarrow> (b div a) * c = b * c div a"
nipkow@30052
   185
apply (cases "a = 0")
nipkow@30052
   186
 apply simp
nipkow@30052
   187
apply (auto simp: dvd_def mult_assoc)
nipkow@30052
   188
done
nipkow@30052
   189
nipkow@29925
   190
lemma div_dvd_div[simp]:
nipkow@29925
   191
  "a dvd b \<Longrightarrow> a dvd c \<Longrightarrow> (b div a dvd c div a) = (b dvd c)"
nipkow@29925
   192
apply (cases "a = 0")
nipkow@29925
   193
 apply simp
nipkow@29925
   194
apply (unfold dvd_def)
nipkow@29925
   195
apply auto
nipkow@29925
   196
 apply(blast intro:mult_assoc[symmetric])
nipkow@29925
   197
apply(fastsimp simp add: mult_assoc)
nipkow@29925
   198
done
nipkow@29925
   199
huffman@30078
   200
lemma dvd_mod_imp_dvd: "[| k dvd m mod n;  k dvd n |] ==> k dvd m"
huffman@30078
   201
  apply (subgoal_tac "k dvd (m div n) *n + m mod n")
huffman@30078
   202
   apply (simp add: mod_div_equality)
huffman@30078
   203
  apply (simp only: dvd_add dvd_mult)
huffman@30078
   204
  done
huffman@30078
   205
huffman@29403
   206
text {* Addition respects modular equivalence. *}
huffman@29403
   207
huffman@29403
   208
lemma mod_add_left_eq: "(a + b) mod c = (a mod c + b) mod c"
huffman@29403
   209
proof -
huffman@29403
   210
  have "(a + b) mod c = (a div c * c + a mod c + b) mod c"
huffman@29403
   211
    by (simp only: mod_div_equality)
huffman@29403
   212
  also have "\<dots> = (a mod c + b + a div c * c) mod c"
huffman@29403
   213
    by (simp only: add_ac)
huffman@29403
   214
  also have "\<dots> = (a mod c + b) mod c"
huffman@29403
   215
    by (rule mod_mult_self1)
huffman@29403
   216
  finally show ?thesis .
huffman@29403
   217
qed
huffman@29403
   218
huffman@29403
   219
lemma mod_add_right_eq: "(a + b) mod c = (a + b mod c) mod c"
huffman@29403
   220
proof -
huffman@29403
   221
  have "(a + b) mod c = (a + (b div c * c + b mod c)) mod c"
huffman@29403
   222
    by (simp only: mod_div_equality)
huffman@29403
   223
  also have "\<dots> = (a + b mod c + b div c * c) mod c"
huffman@29403
   224
    by (simp only: add_ac)
huffman@29403
   225
  also have "\<dots> = (a + b mod c) mod c"
huffman@29403
   226
    by (rule mod_mult_self1)
huffman@29403
   227
  finally show ?thesis .
huffman@29403
   228
qed
huffman@29403
   229
huffman@29403
   230
lemma mod_add_eq: "(a + b) mod c = (a mod c + b mod c) mod c"
huffman@29403
   231
by (rule trans [OF mod_add_left_eq mod_add_right_eq])
huffman@29403
   232
huffman@29403
   233
lemma mod_add_cong:
huffman@29403
   234
  assumes "a mod c = a' mod c"
huffman@29403
   235
  assumes "b mod c = b' mod c"
huffman@29403
   236
  shows "(a + b) mod c = (a' + b') mod c"
huffman@29403
   237
proof -
huffman@29403
   238
  have "(a mod c + b mod c) mod c = (a' mod c + b' mod c) mod c"
huffman@29403
   239
    unfolding assms ..
huffman@29403
   240
  thus ?thesis
huffman@29403
   241
    by (simp only: mod_add_eq [symmetric])
huffman@29403
   242
qed
huffman@29403
   243
haftmann@30923
   244
lemma div_add [simp]: "z dvd x \<Longrightarrow> z dvd y
nipkow@30837
   245
  \<Longrightarrow> (x + y) div z = x div z + y div z"
haftmann@30923
   246
by (cases "z = 0", simp, unfold dvd_def, auto simp add: algebra_simps)
nipkow@30837
   247
huffman@29403
   248
text {* Multiplication respects modular equivalence. *}
huffman@29403
   249
huffman@29403
   250
lemma mod_mult_left_eq: "(a * b) mod c = ((a mod c) * b) mod c"
huffman@29403
   251
proof -
huffman@29403
   252
  have "(a * b) mod c = ((a div c * c + a mod c) * b) mod c"
huffman@29403
   253
    by (simp only: mod_div_equality)
huffman@29403
   254
  also have "\<dots> = (a mod c * b + a div c * b * c) mod c"
nipkow@29667
   255
    by (simp only: algebra_simps)
huffman@29403
   256
  also have "\<dots> = (a mod c * b) mod c"
huffman@29403
   257
    by (rule mod_mult_self1)
huffman@29403
   258
  finally show ?thesis .
huffman@29403
   259
qed
huffman@29403
   260
huffman@29403
   261
lemma mod_mult_right_eq: "(a * b) mod c = (a * (b mod c)) mod c"
huffman@29403
   262
proof -
huffman@29403
   263
  have "(a * b) mod c = (a * (b div c * c + b mod c)) mod c"
huffman@29403
   264
    by (simp only: mod_div_equality)
huffman@29403
   265
  also have "\<dots> = (a * (b mod c) + a * (b div c) * c) mod c"
nipkow@29667
   266
    by (simp only: algebra_simps)
huffman@29403
   267
  also have "\<dots> = (a * (b mod c)) mod c"
huffman@29403
   268
    by (rule mod_mult_self1)
huffman@29403
   269
  finally show ?thesis .
huffman@29403
   270
qed
huffman@29403
   271
huffman@29403
   272
lemma mod_mult_eq: "(a * b) mod c = ((a mod c) * (b mod c)) mod c"
huffman@29403
   273
by (rule trans [OF mod_mult_left_eq mod_mult_right_eq])
huffman@29403
   274
huffman@29403
   275
lemma mod_mult_cong:
huffman@29403
   276
  assumes "a mod c = a' mod c"
huffman@29403
   277
  assumes "b mod c = b' mod c"
huffman@29403
   278
  shows "(a * b) mod c = (a' * b') mod c"
huffman@29403
   279
proof -
huffman@29403
   280
  have "(a mod c * (b mod c)) mod c = (a' mod c * (b' mod c)) mod c"
huffman@29403
   281
    unfolding assms ..
huffman@29403
   282
  thus ?thesis
huffman@29403
   283
    by (simp only: mod_mult_eq [symmetric])
huffman@29403
   284
qed
huffman@29403
   285
huffman@29404
   286
lemma mod_mod_cancel:
huffman@29404
   287
  assumes "c dvd b"
huffman@29404
   288
  shows "a mod b mod c = a mod c"
huffman@29404
   289
proof -
huffman@29404
   290
  from `c dvd b` obtain k where "b = c * k"
huffman@29404
   291
    by (rule dvdE)
huffman@29404
   292
  have "a mod b mod c = a mod (c * k) mod c"
huffman@29404
   293
    by (simp only: `b = c * k`)
huffman@29404
   294
  also have "\<dots> = (a mod (c * k) + a div (c * k) * k * c) mod c"
huffman@29404
   295
    by (simp only: mod_mult_self1)
huffman@29404
   296
  also have "\<dots> = (a div (c * k) * (c * k) + a mod (c * k)) mod c"
huffman@29404
   297
    by (simp only: add_ac mult_ac)
huffman@29404
   298
  also have "\<dots> = a mod c"
huffman@29404
   299
    by (simp only: mod_div_equality)
huffman@29404
   300
  finally show ?thesis .
huffman@29404
   301
qed
huffman@29404
   302
haftmann@30930
   303
lemma div_mult_div_if_dvd:
haftmann@30930
   304
  "y dvd x \<Longrightarrow> z dvd w \<Longrightarrow> (x div y) * (w div z) = (x * w) div (y * z)"
haftmann@30930
   305
  apply (cases "y = 0", simp)
haftmann@30930
   306
  apply (cases "z = 0", simp)
haftmann@30930
   307
  apply (auto elim!: dvdE simp add: algebra_simps)
nipkow@30476
   308
  apply (subst mult_assoc [symmetric])
nipkow@30476
   309
  apply (simp add: no_zero_divisors)
haftmann@30930
   310
  done
haftmann@30930
   311
haftmann@30930
   312
lemma div_mult_mult2 [simp]:
haftmann@30930
   313
  "c \<noteq> 0 \<Longrightarrow> (a * c) div (b * c) = a div b"
haftmann@30930
   314
  by (drule div_mult_mult1) (simp add: mult_commute)
haftmann@30930
   315
haftmann@30930
   316
lemma div_mult_mult1_if [simp]:
haftmann@30930
   317
  "(c * a) div (c * b) = (if c = 0 then 0 else a div b)"
haftmann@30930
   318
  by simp_all
nipkow@30476
   319
haftmann@30930
   320
lemma mod_mult_mult1:
haftmann@30930
   321
  "(c * a) mod (c * b) = c * (a mod b)"
haftmann@30930
   322
proof (cases "c = 0")
haftmann@30930
   323
  case True then show ?thesis by simp
haftmann@30930
   324
next
haftmann@30930
   325
  case False
haftmann@30930
   326
  from mod_div_equality
haftmann@30930
   327
  have "((c * a) div (c * b)) * (c * b) + (c * a) mod (c * b) = c * a" .
haftmann@30930
   328
  with False have "c * ((a div b) * b + a mod b) + (c * a) mod (c * b)
haftmann@30930
   329
    = c * a + c * (a mod b)" by (simp add: algebra_simps)
haftmann@30930
   330
  with mod_div_equality show ?thesis by simp 
haftmann@30930
   331
qed
haftmann@30930
   332
  
haftmann@30930
   333
lemma mod_mult_mult2:
haftmann@30930
   334
  "(a * c) mod (b * c) = (a mod b) * c"
haftmann@30930
   335
  using mod_mult_mult1 [of c a b] by (simp add: mult_commute)
haftmann@30930
   336
huffman@31662
   337
lemma dvd_mod: "k dvd m \<Longrightarrow> k dvd n \<Longrightarrow> k dvd (m mod n)"
huffman@31662
   338
  unfolding dvd_def by (auto simp add: mod_mult_mult1)
huffman@31662
   339
huffman@31662
   340
lemma dvd_mod_iff: "k dvd n \<Longrightarrow> k dvd (m mod n) \<longleftrightarrow> k dvd m"
huffman@31662
   341
by (blast intro: dvd_mod_imp_dvd dvd_mod)
huffman@31662
   342
haftmann@31009
   343
lemma div_power:
huffman@31661
   344
  "y dvd x \<Longrightarrow> (x div y) ^ n = x ^ n div y ^ n"
nipkow@30476
   345
apply (induct n)
nipkow@30476
   346
 apply simp
nipkow@30476
   347
apply(simp add: div_mult_div_if_dvd dvd_power_same)
nipkow@30476
   348
done
nipkow@30476
   349
huffman@31661
   350
end
huffman@31661
   351
huffman@31661
   352
class ring_div = semiring_div + idom
huffman@29405
   353
begin
huffman@29405
   354
huffman@29405
   355
text {* Negation respects modular equivalence. *}
huffman@29405
   356
huffman@29405
   357
lemma mod_minus_eq: "(- a) mod b = (- (a mod b)) mod b"
huffman@29405
   358
proof -
huffman@29405
   359
  have "(- a) mod b = (- (a div b * b + a mod b)) mod b"
huffman@29405
   360
    by (simp only: mod_div_equality)
huffman@29405
   361
  also have "\<dots> = (- (a mod b) + - (a div b) * b) mod b"
huffman@29405
   362
    by (simp only: minus_add_distrib minus_mult_left add_ac)
huffman@29405
   363
  also have "\<dots> = (- (a mod b)) mod b"
huffman@29405
   364
    by (rule mod_mult_self1)
huffman@29405
   365
  finally show ?thesis .
huffman@29405
   366
qed
huffman@29405
   367
huffman@29405
   368
lemma mod_minus_cong:
huffman@29405
   369
  assumes "a mod b = a' mod b"
huffman@29405
   370
  shows "(- a) mod b = (- a') mod b"
huffman@29405
   371
proof -
huffman@29405
   372
  have "(- (a mod b)) mod b = (- (a' mod b)) mod b"
huffman@29405
   373
    unfolding assms ..
huffman@29405
   374
  thus ?thesis
huffman@29405
   375
    by (simp only: mod_minus_eq [symmetric])
huffman@29405
   376
qed
huffman@29405
   377
huffman@29405
   378
text {* Subtraction respects modular equivalence. *}
huffman@29405
   379
huffman@29405
   380
lemma mod_diff_left_eq: "(a - b) mod c = (a mod c - b) mod c"
huffman@29405
   381
  unfolding diff_minus
huffman@29405
   382
  by (intro mod_add_cong mod_minus_cong) simp_all
huffman@29405
   383
huffman@29405
   384
lemma mod_diff_right_eq: "(a - b) mod c = (a - b mod c) mod c"
huffman@29405
   385
  unfolding diff_minus
huffman@29405
   386
  by (intro mod_add_cong mod_minus_cong) simp_all
huffman@29405
   387
huffman@29405
   388
lemma mod_diff_eq: "(a - b) mod c = (a mod c - b mod c) mod c"
huffman@29405
   389
  unfolding diff_minus
huffman@29405
   390
  by (intro mod_add_cong mod_minus_cong) simp_all
huffman@29405
   391
huffman@29405
   392
lemma mod_diff_cong:
huffman@29405
   393
  assumes "a mod c = a' mod c"
huffman@29405
   394
  assumes "b mod c = b' mod c"
huffman@29405
   395
  shows "(a - b) mod c = (a' - b') mod c"
huffman@29405
   396
  unfolding diff_minus using assms
huffman@29405
   397
  by (intro mod_add_cong mod_minus_cong)
huffman@29405
   398
nipkow@30180
   399
lemma dvd_neg_div: "y dvd x \<Longrightarrow> -x div y = - (x div y)"
nipkow@30180
   400
apply (case_tac "y = 0") apply simp
nipkow@30180
   401
apply (auto simp add: dvd_def)
nipkow@30180
   402
apply (subgoal_tac "-(y * k) = y * - k")
nipkow@30180
   403
 apply (erule ssubst)
nipkow@30180
   404
 apply (erule div_mult_self1_is_id)
nipkow@30180
   405
apply simp
nipkow@30180
   406
done
nipkow@30180
   407
nipkow@30180
   408
lemma dvd_div_neg: "y dvd x \<Longrightarrow> x div -y = - (x div y)"
nipkow@30180
   409
apply (case_tac "y = 0") apply simp
nipkow@30180
   410
apply (auto simp add: dvd_def)
nipkow@30180
   411
apply (subgoal_tac "y * k = -y * -k")
nipkow@30180
   412
 apply (erule ssubst)
nipkow@30180
   413
 apply (rule div_mult_self1_is_id)
nipkow@30180
   414
 apply simp
nipkow@30180
   415
apply simp
nipkow@30180
   416
done
nipkow@30180
   417
huffman@29405
   418
end
huffman@29405
   419
haftmann@25942
   420
haftmann@26100
   421
subsection {* Division on @{typ nat} *}
haftmann@26100
   422
haftmann@26100
   423
text {*
haftmann@26100
   424
  We define @{const div} and @{const mod} on @{typ nat} by means
haftmann@26100
   425
  of a characteristic relation with two input arguments
haftmann@26100
   426
  @{term "m\<Colon>nat"}, @{term "n\<Colon>nat"} and two output arguments
haftmann@26100
   427
  @{term "q\<Colon>nat"}(uotient) and @{term "r\<Colon>nat"}(emainder).
haftmann@26100
   428
*}
haftmann@26100
   429
haftmann@33340
   430
definition divmod_nat_rel :: "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat \<Rightarrow> bool" where
haftmann@33340
   431
  "divmod_nat_rel m n qr \<longleftrightarrow>
haftmann@30923
   432
    m = fst qr * n + snd qr \<and>
haftmann@30923
   433
      (if n = 0 then fst qr = 0 else if n > 0 then 0 \<le> snd qr \<and> snd qr < n else n < snd qr \<and> snd qr \<le> 0)"
haftmann@26100
   434
haftmann@33340
   435
text {* @{const divmod_nat_rel} is total: *}
haftmann@26100
   436
haftmann@33340
   437
lemma divmod_nat_rel_ex:
haftmann@33340
   438
  obtains q r where "divmod_nat_rel m n (q, r)"
haftmann@26100
   439
proof (cases "n = 0")
haftmann@30923
   440
  case True  with that show thesis
haftmann@33340
   441
    by (auto simp add: divmod_nat_rel_def)
haftmann@26100
   442
next
haftmann@26100
   443
  case False
haftmann@26100
   444
  have "\<exists>q r. m = q * n + r \<and> r < n"
haftmann@26100
   445
  proof (induct m)
haftmann@26100
   446
    case 0 with `n \<noteq> 0`
haftmann@26100
   447
    have "(0\<Colon>nat) = 0 * n + 0 \<and> 0 < n" by simp
haftmann@26100
   448
    then show ?case by blast
haftmann@26100
   449
  next
haftmann@26100
   450
    case (Suc m) then obtain q' r'
haftmann@26100
   451
      where m: "m = q' * n + r'" and n: "r' < n" by auto
haftmann@26100
   452
    then show ?case proof (cases "Suc r' < n")
haftmann@26100
   453
      case True
haftmann@26100
   454
      from m n have "Suc m = q' * n + Suc r'" by simp
haftmann@26100
   455
      with True show ?thesis by blast
haftmann@26100
   456
    next
haftmann@26100
   457
      case False then have "n \<le> Suc r'" by auto
haftmann@26100
   458
      moreover from n have "Suc r' \<le> n" by auto
haftmann@26100
   459
      ultimately have "n = Suc r'" by auto
haftmann@26100
   460
      with m have "Suc m = Suc q' * n + 0" by simp
haftmann@26100
   461
      with `n \<noteq> 0` show ?thesis by blast
haftmann@26100
   462
    qed
haftmann@26100
   463
  qed
haftmann@26100
   464
  with that show thesis
haftmann@33340
   465
    using `n \<noteq> 0` by (auto simp add: divmod_nat_rel_def)
haftmann@26100
   466
qed
haftmann@26100
   467
haftmann@33340
   468
text {* @{const divmod_nat_rel} is injective: *}
haftmann@26100
   469
haftmann@33340
   470
lemma divmod_nat_rel_unique:
haftmann@33340
   471
  assumes "divmod_nat_rel m n qr"
haftmann@33340
   472
    and "divmod_nat_rel m n qr'"
haftmann@30923
   473
  shows "qr = qr'"
haftmann@26100
   474
proof (cases "n = 0")
haftmann@26100
   475
  case True with assms show ?thesis
haftmann@30923
   476
    by (cases qr, cases qr')
haftmann@33340
   477
      (simp add: divmod_nat_rel_def)
haftmann@26100
   478
next
haftmann@26100
   479
  case False
haftmann@26100
   480
  have aux: "\<And>q r q' r'. q' * n + r' = q * n + r \<Longrightarrow> r < n \<Longrightarrow> q' \<le> (q\<Colon>nat)"
haftmann@26100
   481
  apply (rule leI)
haftmann@26100
   482
  apply (subst less_iff_Suc_add)
haftmann@26100
   483
  apply (auto simp add: add_mult_distrib)
haftmann@26100
   484
  done
haftmann@30923
   485
  from `n \<noteq> 0` assms have "fst qr = fst qr'"
haftmann@33340
   486
    by (auto simp add: divmod_nat_rel_def intro: order_antisym dest: aux sym)
haftmann@30923
   487
  moreover from this assms have "snd qr = snd qr'"
haftmann@33340
   488
    by (simp add: divmod_nat_rel_def)
haftmann@30923
   489
  ultimately show ?thesis by (cases qr, cases qr') simp
haftmann@26100
   490
qed
haftmann@26100
   491
haftmann@26100
   492
text {*
haftmann@26100
   493
  We instantiate divisibility on the natural numbers by
haftmann@33340
   494
  means of @{const divmod_nat_rel}:
haftmann@26100
   495
*}
haftmann@25942
   496
haftmann@25942
   497
instantiation nat :: semiring_div
haftmann@25571
   498
begin
haftmann@25571
   499
haftmann@33340
   500
definition divmod_nat :: "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat" where
haftmann@33340
   501
  [code del]: "divmod_nat m n = (THE qr. divmod_nat_rel m n qr)"
haftmann@30923
   502
haftmann@33340
   503
lemma divmod_nat_rel_divmod_nat:
haftmann@33340
   504
  "divmod_nat_rel m n (divmod_nat m n)"
haftmann@30923
   505
proof -
haftmann@33340
   506
  from divmod_nat_rel_ex
haftmann@33340
   507
    obtain qr where rel: "divmod_nat_rel m n qr" .
haftmann@30923
   508
  then show ?thesis
haftmann@33340
   509
  by (auto simp add: divmod_nat_def intro: theI elim: divmod_nat_rel_unique)
haftmann@30923
   510
qed
haftmann@30923
   511
haftmann@33340
   512
lemma divmod_nat_eq:
haftmann@33340
   513
  assumes "divmod_nat_rel m n qr" 
haftmann@33340
   514
  shows "divmod_nat m n = qr"
haftmann@33340
   515
  using assms by (auto intro: divmod_nat_rel_unique divmod_nat_rel_divmod_nat)
haftmann@26100
   516
haftmann@26100
   517
definition div_nat where
haftmann@33340
   518
  "m div n = fst (divmod_nat m n)"
haftmann@26100
   519
haftmann@26100
   520
definition mod_nat where
haftmann@33340
   521
  "m mod n = snd (divmod_nat m n)"
haftmann@25571
   522
haftmann@33340
   523
lemma divmod_nat_div_mod:
haftmann@33340
   524
  "divmod_nat m n = (m div n, m mod n)"
haftmann@26100
   525
  unfolding div_nat_def mod_nat_def by simp
haftmann@26100
   526
haftmann@26100
   527
lemma div_eq:
haftmann@33340
   528
  assumes "divmod_nat_rel m n (q, r)" 
haftmann@26100
   529
  shows "m div n = q"
haftmann@33340
   530
  using assms by (auto dest: divmod_nat_eq simp add: divmod_nat_div_mod)
haftmann@26100
   531
haftmann@26100
   532
lemma mod_eq:
haftmann@33340
   533
  assumes "divmod_nat_rel m n (q, r)" 
haftmann@26100
   534
  shows "m mod n = r"
haftmann@33340
   535
  using assms by (auto dest: divmod_nat_eq simp add: divmod_nat_div_mod)
haftmann@25571
   536
haftmann@33340
   537
lemma divmod_nat_rel: "divmod_nat_rel m n (m div n, m mod n)"
haftmann@33340
   538
  by (simp add: div_nat_def mod_nat_def divmod_nat_rel_divmod_nat)
paulson@14267
   539
haftmann@33340
   540
lemma divmod_nat_zero:
haftmann@33340
   541
  "divmod_nat m 0 = (0, m)"
haftmann@26100
   542
proof -
haftmann@33340
   543
  from divmod_nat_rel [of m 0] show ?thesis
haftmann@33340
   544
    unfolding divmod_nat_div_mod divmod_nat_rel_def by simp
haftmann@26100
   545
qed
haftmann@25942
   546
haftmann@33340
   547
lemma divmod_nat_base:
haftmann@26100
   548
  assumes "m < n"
haftmann@33340
   549
  shows "divmod_nat m n = (0, m)"
haftmann@26100
   550
proof -
haftmann@33340
   551
  from divmod_nat_rel [of m n] show ?thesis
haftmann@33340
   552
    unfolding divmod_nat_div_mod divmod_nat_rel_def
haftmann@26100
   553
    using assms by (cases "m div n = 0")
haftmann@26100
   554
      (auto simp add: gr0_conv_Suc [of "m div n"])
haftmann@26100
   555
qed
haftmann@25942
   556
haftmann@33340
   557
lemma divmod_nat_step:
haftmann@26100
   558
  assumes "0 < n" and "n \<le> m"
haftmann@33340
   559
  shows "divmod_nat m n = (Suc ((m - n) div n), (m - n) mod n)"
haftmann@26100
   560
proof -
haftmann@33340
   561
  from divmod_nat_rel have divmod_nat_m_n: "divmod_nat_rel m n (m div n, m mod n)" .
haftmann@26100
   562
  with assms have m_div_n: "m div n \<ge> 1"
haftmann@33340
   563
    by (cases "m div n") (auto simp add: divmod_nat_rel_def)
haftmann@33340
   564
  from assms divmod_nat_m_n have "divmod_nat_rel (m - n) n (m div n - Suc 0, m mod n)"
haftmann@33340
   565
    by (cases "m div n") (auto simp add: divmod_nat_rel_def)
haftmann@33340
   566
  with divmod_nat_eq have "divmod_nat (m - n) n = (m div n - Suc 0, m mod n)" by simp
haftmann@33340
   567
  moreover from divmod_nat_div_mod have "divmod_nat (m - n) n = ((m - n) div n, (m - n) mod n)" .
haftmann@26100
   568
  ultimately have "m div n = Suc ((m - n) div n)"
haftmann@26100
   569
    and "m mod n = (m - n) mod n" using m_div_n by simp_all
haftmann@33340
   570
  then show ?thesis using divmod_nat_div_mod by simp
haftmann@26100
   571
qed
haftmann@25942
   572
wenzelm@26300
   573
text {* The ''recursion'' equations for @{const div} and @{const mod} *}
haftmann@26100
   574
haftmann@26100
   575
lemma div_less [simp]:
haftmann@26100
   576
  fixes m n :: nat
haftmann@26100
   577
  assumes "m < n"
haftmann@26100
   578
  shows "m div n = 0"
haftmann@33340
   579
  using assms divmod_nat_base divmod_nat_div_mod by simp
haftmann@25942
   580
haftmann@26100
   581
lemma le_div_geq:
haftmann@26100
   582
  fixes m n :: nat
haftmann@26100
   583
  assumes "0 < n" and "n \<le> m"
haftmann@26100
   584
  shows "m div n = Suc ((m - n) div n)"
haftmann@33340
   585
  using assms divmod_nat_step divmod_nat_div_mod by simp
paulson@14267
   586
haftmann@26100
   587
lemma mod_less [simp]:
haftmann@26100
   588
  fixes m n :: nat
haftmann@26100
   589
  assumes "m < n"
haftmann@26100
   590
  shows "m mod n = m"
haftmann@33340
   591
  using assms divmod_nat_base divmod_nat_div_mod by simp
haftmann@26100
   592
haftmann@26100
   593
lemma le_mod_geq:
haftmann@26100
   594
  fixes m n :: nat
haftmann@26100
   595
  assumes "n \<le> m"
haftmann@26100
   596
  shows "m mod n = (m - n) mod n"
haftmann@33340
   597
  using assms divmod_nat_step divmod_nat_div_mod by (cases "n = 0") simp_all
paulson@14267
   598
haftmann@30930
   599
instance proof -
haftmann@30930
   600
  have [simp]: "\<And>n::nat. n div 0 = 0"
haftmann@33340
   601
    by (simp add: div_nat_def divmod_nat_zero)
haftmann@30930
   602
  have [simp]: "\<And>n::nat. 0 div n = 0"
haftmann@30930
   603
  proof -
haftmann@30930
   604
    fix n :: nat
haftmann@30930
   605
    show "0 div n = 0"
haftmann@30930
   606
      by (cases "n = 0") simp_all
haftmann@30930
   607
  qed
haftmann@30930
   608
  show "OFCLASS(nat, semiring_div_class)" proof
haftmann@30930
   609
    fix m n :: nat
haftmann@30930
   610
    show "m div n * n + m mod n = m"
haftmann@33340
   611
      using divmod_nat_rel [of m n] by (simp add: divmod_nat_rel_def)
haftmann@30930
   612
  next
haftmann@30930
   613
    fix m n q :: nat
haftmann@30930
   614
    assume "n \<noteq> 0"
haftmann@30930
   615
    then show "(q + m * n) div n = m + q div n"
haftmann@30930
   616
      by (induct m) (simp_all add: le_div_geq)
haftmann@30930
   617
  next
haftmann@30930
   618
    fix m n q :: nat
haftmann@30930
   619
    assume "m \<noteq> 0"
haftmann@30930
   620
    then show "(m * n) div (m * q) = n div q"
haftmann@30930
   621
    proof (cases "n \<noteq> 0 \<and> q \<noteq> 0")
haftmann@30930
   622
      case False then show ?thesis by auto
haftmann@30930
   623
    next
haftmann@30930
   624
      case True with `m \<noteq> 0`
haftmann@30930
   625
        have "m > 0" and "n > 0" and "q > 0" by auto
haftmann@33340
   626
      then have "\<And>a b. divmod_nat_rel n q (a, b) \<Longrightarrow> divmod_nat_rel (m * n) (m * q) (a, m * b)"
haftmann@33340
   627
        by (auto simp add: divmod_nat_rel_def) (simp_all add: algebra_simps)
haftmann@33340
   628
      moreover from divmod_nat_rel have "divmod_nat_rel n q (n div q, n mod q)" .
haftmann@33340
   629
      ultimately have "divmod_nat_rel (m * n) (m * q) (n div q, m * (n mod q))" .
haftmann@30930
   630
      then show ?thesis by (simp add: div_eq)
haftmann@30930
   631
    qed
haftmann@30930
   632
  qed simp_all
haftmann@25942
   633
qed
haftmann@26100
   634
haftmann@25942
   635
end
paulson@14267
   636
haftmann@26100
   637
text {* Simproc for cancelling @{const div} and @{const mod} *}
haftmann@25942
   638
haftmann@30934
   639
ML {*
haftmann@30934
   640
local
haftmann@30934
   641
haftmann@30934
   642
structure CancelDivMod = CancelDivModFun(struct
haftmann@25942
   643
haftmann@30934
   644
  val div_name = @{const_name div};
haftmann@30934
   645
  val mod_name = @{const_name mod};
haftmann@30934
   646
  val mk_binop = HOLogic.mk_binop;
haftmann@30934
   647
  val mk_sum = Nat_Arith.mk_sum;
haftmann@30934
   648
  val dest_sum = Nat_Arith.dest_sum;
haftmann@25942
   649
haftmann@30934
   650
  val div_mod_eqs = map mk_meta_eq [@{thm div_mod_equality}, @{thm div_mod_equality2}];
paulson@14267
   651
haftmann@30934
   652
  val trans = trans;
haftmann@25942
   653
haftmann@30934
   654
  val prove_eq_sums = Arith_Data.prove_conv2 all_tac (Arith_Data.simp_all_tac
haftmann@30934
   655
    (@{thm monoid_add_class.add_0_left} :: @{thm monoid_add_class.add_0_right} :: @{thms add_ac}))
haftmann@25942
   656
haftmann@30934
   657
end)
haftmann@25942
   658
haftmann@30934
   659
in
haftmann@25942
   660
wenzelm@32010
   661
val cancel_div_mod_nat_proc = Simplifier.simproc @{theory}
haftmann@26100
   662
  "cancel_div_mod" ["(m::nat) + n"] (K CancelDivMod.proc);
haftmann@25942
   663
haftmann@30934
   664
val _ = Addsimprocs [cancel_div_mod_nat_proc];
haftmann@30934
   665
haftmann@30934
   666
end
haftmann@25942
   667
*}
haftmann@25942
   668
haftmann@26100
   669
text {* code generator setup *}
haftmann@26100
   670
haftmann@33340
   671
lemma divmod_nat_if [code]: "divmod_nat m n = (if n = 0 \<or> m < n then (0, m) else
haftmann@33340
   672
  let (q, r) = divmod_nat (m - n) n in (Suc q, r))"
haftmann@33340
   673
by (simp add: divmod_nat_zero divmod_nat_base divmod_nat_step)
haftmann@33340
   674
    (simp add: divmod_nat_div_mod)
haftmann@26100
   675
haftmann@26100
   676
code_modulename SML
haftmann@26100
   677
  Divides Nat
haftmann@26100
   678
haftmann@26100
   679
code_modulename OCaml
haftmann@26100
   680
  Divides Nat
haftmann@26100
   681
haftmann@26100
   682
code_modulename Haskell
haftmann@26100
   683
  Divides Nat
haftmann@26100
   684
haftmann@26100
   685
haftmann@26100
   686
subsubsection {* Quotient *}
haftmann@26100
   687
haftmann@26100
   688
lemma div_geq: "0 < n \<Longrightarrow>  \<not> m < n \<Longrightarrow> m div n = Suc ((m - n) div n)"
nipkow@29667
   689
by (simp add: le_div_geq linorder_not_less)
haftmann@26100
   690
haftmann@26100
   691
lemma div_if: "0 < n \<Longrightarrow> m div n = (if m < n then 0 else Suc ((m - n) div n))"
nipkow@29667
   692
by (simp add: div_geq)
haftmann@26100
   693
haftmann@26100
   694
lemma div_mult_self_is_m [simp]: "0<n ==> (m*n) div n = (m::nat)"
nipkow@29667
   695
by simp
haftmann@26100
   696
haftmann@26100
   697
lemma div_mult_self1_is_m [simp]: "0<n ==> (n*m) div n = (m::nat)"
nipkow@29667
   698
by simp
haftmann@26100
   699
haftmann@25942
   700
haftmann@25942
   701
subsubsection {* Remainder *}
haftmann@25942
   702
haftmann@26100
   703
lemma mod_less_divisor [simp]:
haftmann@26100
   704
  fixes m n :: nat
haftmann@26100
   705
  assumes "n > 0"
haftmann@26100
   706
  shows "m mod n < (n::nat)"
haftmann@33340
   707
  using assms divmod_nat_rel [of m n] unfolding divmod_nat_rel_def by auto
paulson@14267
   708
haftmann@26100
   709
lemma mod_less_eq_dividend [simp]:
haftmann@26100
   710
  fixes m n :: nat
haftmann@26100
   711
  shows "m mod n \<le> m"
haftmann@26100
   712
proof (rule add_leD2)
haftmann@26100
   713
  from mod_div_equality have "m div n * n + m mod n = m" .
haftmann@26100
   714
  then show "m div n * n + m mod n \<le> m" by auto
haftmann@26100
   715
qed
haftmann@26100
   716
haftmann@26100
   717
lemma mod_geq: "\<not> m < (n\<Colon>nat) \<Longrightarrow> m mod n = (m - n) mod n"
nipkow@29667
   718
by (simp add: le_mod_geq linorder_not_less)
paulson@14267
   719
haftmann@26100
   720
lemma mod_if: "m mod (n\<Colon>nat) = (if m < n then m else (m - n) mod n)"
nipkow@29667
   721
by (simp add: le_mod_geq)
haftmann@26100
   722
paulson@14267
   723
lemma mod_1 [simp]: "m mod Suc 0 = 0"
nipkow@29667
   724
by (induct m) (simp_all add: mod_geq)
paulson@14267
   725
haftmann@26100
   726
lemma mod_mult_distrib: "(m mod n) * (k\<Colon>nat) = (m * k) mod (n * k)"
wenzelm@22718
   727
  apply (cases "n = 0", simp)
wenzelm@22718
   728
  apply (cases "k = 0", simp)
wenzelm@22718
   729
  apply (induct m rule: nat_less_induct)
wenzelm@22718
   730
  apply (subst mod_if, simp)
wenzelm@22718
   731
  apply (simp add: mod_geq diff_mult_distrib)
wenzelm@22718
   732
  done
paulson@14267
   733
paulson@14267
   734
lemma mod_mult_distrib2: "(k::nat) * (m mod n) = (k*m) mod (k*n)"
nipkow@29667
   735
by (simp add: mult_commute [of k] mod_mult_distrib)
paulson@14267
   736
paulson@14267
   737
(* a simple rearrangement of mod_div_equality: *)
paulson@14267
   738
lemma mult_div_cancel: "(n::nat) * (m div n) = m - (m mod n)"
nipkow@29667
   739
by (cut_tac a = m and b = n in mod_div_equality2, arith)
paulson@14267
   740
nipkow@15439
   741
lemma mod_le_divisor[simp]: "0 < n \<Longrightarrow> m mod n \<le> (n::nat)"
wenzelm@22718
   742
  apply (drule mod_less_divisor [where m = m])
wenzelm@22718
   743
  apply simp
wenzelm@22718
   744
  done
paulson@14267
   745
haftmann@26100
   746
subsubsection {* Quotient and Remainder *}
paulson@14267
   747
haftmann@33340
   748
lemma divmod_nat_rel_mult1_eq:
haftmann@33340
   749
  "divmod_nat_rel b c (q, r) \<Longrightarrow> c > 0
haftmann@33340
   750
   \<Longrightarrow> divmod_nat_rel (a * b) c (a * q + a * r div c, a * r mod c)"
haftmann@33340
   751
by (auto simp add: split_ifs divmod_nat_rel_def algebra_simps)
paulson@14267
   752
haftmann@30923
   753
lemma div_mult1_eq:
haftmann@30923
   754
  "(a * b) div c = a * (b div c) + a * (b mod c) div (c::nat)"
nipkow@25134
   755
apply (cases "c = 0", simp)
haftmann@33340
   756
apply (blast intro: divmod_nat_rel [THEN divmod_nat_rel_mult1_eq, THEN div_eq])
nipkow@25134
   757
done
paulson@14267
   758
haftmann@33340
   759
lemma divmod_nat_rel_add1_eq:
haftmann@33340
   760
  "divmod_nat_rel a c (aq, ar) \<Longrightarrow> divmod_nat_rel b c (bq, br) \<Longrightarrow>  c > 0
haftmann@33340
   761
   \<Longrightarrow> divmod_nat_rel (a + b) c (aq + bq + (ar + br) div c, (ar + br) mod c)"
haftmann@33340
   762
by (auto simp add: split_ifs divmod_nat_rel_def algebra_simps)
paulson@14267
   763
paulson@14267
   764
(*NOT suitable for rewriting: the RHS has an instance of the LHS*)
paulson@14267
   765
lemma div_add1_eq:
nipkow@25134
   766
  "(a+b) div (c::nat) = a div c + b div c + ((a mod c + b mod c) div c)"
nipkow@25134
   767
apply (cases "c = 0", simp)
haftmann@33340
   768
apply (blast intro: divmod_nat_rel_add1_eq [THEN div_eq] divmod_nat_rel)
nipkow@25134
   769
done
paulson@14267
   770
paulson@14267
   771
lemma mod_lemma: "[| (0::nat) < c; r < b |] ==> b * (q mod c) + r < b * c"
wenzelm@22718
   772
  apply (cut_tac m = q and n = c in mod_less_divisor)
wenzelm@22718
   773
  apply (drule_tac [2] m = "q mod c" in less_imp_Suc_add, auto)
wenzelm@22718
   774
  apply (erule_tac P = "%x. ?lhs < ?rhs x" in ssubst)
wenzelm@22718
   775
  apply (simp add: add_mult_distrib2)
wenzelm@22718
   776
  done
paulson@10559
   777
haftmann@33340
   778
lemma divmod_nat_rel_mult2_eq:
haftmann@33340
   779
  "divmod_nat_rel a b (q, r) \<Longrightarrow> 0 < b \<Longrightarrow> 0 < c
haftmann@33340
   780
   \<Longrightarrow> divmod_nat_rel a (b * c) (q div c, b *(q mod c) + r)"
haftmann@33340
   781
by (auto simp add: mult_ac divmod_nat_rel_def add_mult_distrib2 [symmetric] mod_lemma)
paulson@14267
   782
paulson@14267
   783
lemma div_mult2_eq: "a div (b*c) = (a div b) div (c::nat)"
wenzelm@22718
   784
  apply (cases "b = 0", simp)
wenzelm@22718
   785
  apply (cases "c = 0", simp)
haftmann@33340
   786
  apply (force simp add: divmod_nat_rel [THEN divmod_nat_rel_mult2_eq, THEN div_eq])
wenzelm@22718
   787
  done
paulson@14267
   788
paulson@14267
   789
lemma mod_mult2_eq: "a mod (b*c) = b*(a div b mod c) + a mod (b::nat)"
wenzelm@22718
   790
  apply (cases "b = 0", simp)
wenzelm@22718
   791
  apply (cases "c = 0", simp)
haftmann@33340
   792
  apply (auto simp add: mult_commute divmod_nat_rel [THEN divmod_nat_rel_mult2_eq, THEN mod_eq])
wenzelm@22718
   793
  done
paulson@14267
   794
paulson@14267
   795
haftmann@25942
   796
subsubsection{*Further Facts about Quotient and Remainder*}
paulson@14267
   797
paulson@14267
   798
lemma div_1 [simp]: "m div Suc 0 = m"
nipkow@29667
   799
by (induct m) (simp_all add: div_geq)
paulson@14267
   800
paulson@14267
   801
paulson@14267
   802
(* Monotonicity of div in first argument *)
haftmann@30923
   803
lemma div_le_mono [rule_format (no_asm)]:
wenzelm@22718
   804
    "\<forall>m::nat. m \<le> n --> (m div k) \<le> (n div k)"
paulson@14267
   805
apply (case_tac "k=0", simp)
paulson@15251
   806
apply (induct "n" rule: nat_less_induct, clarify)
paulson@14267
   807
apply (case_tac "n<k")
paulson@14267
   808
(* 1  case n<k *)
paulson@14267
   809
apply simp
paulson@14267
   810
(* 2  case n >= k *)
paulson@14267
   811
apply (case_tac "m<k")
paulson@14267
   812
(* 2.1  case m<k *)
paulson@14267
   813
apply simp
paulson@14267
   814
(* 2.2  case m>=k *)
nipkow@15439
   815
apply (simp add: div_geq diff_le_mono)
paulson@14267
   816
done
paulson@14267
   817
paulson@14267
   818
(* Antimonotonicity of div in second argument *)
paulson@14267
   819
lemma div_le_mono2: "!!m::nat. [| 0<m; m\<le>n |] ==> (k div n) \<le> (k div m)"
paulson@14267
   820
apply (subgoal_tac "0<n")
wenzelm@22718
   821
 prefer 2 apply simp
paulson@15251
   822
apply (induct_tac k rule: nat_less_induct)
paulson@14267
   823
apply (rename_tac "k")
paulson@14267
   824
apply (case_tac "k<n", simp)
paulson@14267
   825
apply (subgoal_tac "~ (k<m) ")
wenzelm@22718
   826
 prefer 2 apply simp
paulson@14267
   827
apply (simp add: div_geq)
paulson@15251
   828
apply (subgoal_tac "(k-n) div n \<le> (k-m) div n")
paulson@14267
   829
 prefer 2
paulson@14267
   830
 apply (blast intro: div_le_mono diff_le_mono2)
paulson@14267
   831
apply (rule le_trans, simp)
nipkow@15439
   832
apply (simp)
paulson@14267
   833
done
paulson@14267
   834
paulson@14267
   835
lemma div_le_dividend [simp]: "m div n \<le> (m::nat)"
paulson@14267
   836
apply (case_tac "n=0", simp)
paulson@14267
   837
apply (subgoal_tac "m div n \<le> m div 1", simp)
paulson@14267
   838
apply (rule div_le_mono2)
paulson@14267
   839
apply (simp_all (no_asm_simp))
paulson@14267
   840
done
paulson@14267
   841
wenzelm@22718
   842
(* Similar for "less than" *)
paulson@17085
   843
lemma div_less_dividend [rule_format]:
paulson@14267
   844
     "!!n::nat. 1<n ==> 0 < m --> m div n < m"
paulson@15251
   845
apply (induct_tac m rule: nat_less_induct)
paulson@14267
   846
apply (rename_tac "m")
paulson@14267
   847
apply (case_tac "m<n", simp)
paulson@14267
   848
apply (subgoal_tac "0<n")
wenzelm@22718
   849
 prefer 2 apply simp
paulson@14267
   850
apply (simp add: div_geq)
paulson@14267
   851
apply (case_tac "n<m")
paulson@15251
   852
 apply (subgoal_tac "(m-n) div n < (m-n) ")
paulson@14267
   853
  apply (rule impI less_trans_Suc)+
paulson@14267
   854
apply assumption
nipkow@15439
   855
  apply (simp_all)
paulson@14267
   856
done
paulson@14267
   857
paulson@17085
   858
declare div_less_dividend [simp]
paulson@17085
   859
paulson@14267
   860
text{*A fact for the mutilated chess board*}
paulson@14267
   861
lemma mod_Suc: "Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))"
paulson@14267
   862
apply (case_tac "n=0", simp)
paulson@15251
   863
apply (induct "m" rule: nat_less_induct)
paulson@14267
   864
apply (case_tac "Suc (na) <n")
paulson@14267
   865
(* case Suc(na) < n *)
paulson@14267
   866
apply (frule lessI [THEN less_trans], simp add: less_not_refl3)
paulson@14267
   867
(* case n \<le> Suc(na) *)
paulson@16796
   868
apply (simp add: linorder_not_less le_Suc_eq mod_geq)
nipkow@15439
   869
apply (auto simp add: Suc_diff_le le_mod_geq)
paulson@14267
   870
done
paulson@14267
   871
paulson@14267
   872
lemma mod_eq_0_iff: "(m mod d = 0) = (\<exists>q::nat. m = d*q)"
nipkow@29667
   873
by (auto simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)
paulson@17084
   874
wenzelm@22718
   875
lemmas mod_eq_0D [dest!] = mod_eq_0_iff [THEN iffD1]
paulson@14267
   876
paulson@14267
   877
(*Loses information, namely we also have r<d provided d is nonzero*)
paulson@14267
   878
lemma mod_eqD: "(m mod d = r) ==> \<exists>q::nat. m = r + q*d"
haftmann@27651
   879
  apply (cut_tac a = m in mod_div_equality)
wenzelm@22718
   880
  apply (simp only: add_ac)
wenzelm@22718
   881
  apply (blast intro: sym)
wenzelm@22718
   882
  done
paulson@14267
   883
nipkow@13152
   884
lemma split_div:
nipkow@13189
   885
 "P(n div k :: nat) =
nipkow@13189
   886
 ((k = 0 \<longrightarrow> P 0) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P i)))"
nipkow@13189
   887
 (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
nipkow@13189
   888
proof
nipkow@13189
   889
  assume P: ?P
nipkow@13189
   890
  show ?Q
nipkow@13189
   891
  proof (cases)
nipkow@13189
   892
    assume "k = 0"
haftmann@27651
   893
    with P show ?Q by simp
nipkow@13189
   894
  next
nipkow@13189
   895
    assume not0: "k \<noteq> 0"
nipkow@13189
   896
    thus ?Q
nipkow@13189
   897
    proof (simp, intro allI impI)
nipkow@13189
   898
      fix i j
nipkow@13189
   899
      assume n: "n = k*i + j" and j: "j < k"
nipkow@13189
   900
      show "P i"
nipkow@13189
   901
      proof (cases)
wenzelm@22718
   902
        assume "i = 0"
wenzelm@22718
   903
        with n j P show "P i" by simp
nipkow@13189
   904
      next
wenzelm@22718
   905
        assume "i \<noteq> 0"
wenzelm@22718
   906
        with not0 n j P show "P i" by(simp add:add_ac)
nipkow@13189
   907
      qed
nipkow@13189
   908
    qed
nipkow@13189
   909
  qed
nipkow@13189
   910
next
nipkow@13189
   911
  assume Q: ?Q
nipkow@13189
   912
  show ?P
nipkow@13189
   913
  proof (cases)
nipkow@13189
   914
    assume "k = 0"
haftmann@27651
   915
    with Q show ?P by simp
nipkow@13189
   916
  next
nipkow@13189
   917
    assume not0: "k \<noteq> 0"
nipkow@13189
   918
    with Q have R: ?R by simp
nipkow@13189
   919
    from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
nipkow@13517
   920
    show ?P by simp
nipkow@13189
   921
  qed
nipkow@13189
   922
qed
nipkow@13189
   923
berghofe@13882
   924
lemma split_div_lemma:
haftmann@26100
   925
  assumes "0 < n"
haftmann@26100
   926
  shows "n * q \<le> m \<and> m < n * Suc q \<longleftrightarrow> q = ((m\<Colon>nat) div n)" (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@26100
   927
proof
haftmann@26100
   928
  assume ?rhs
haftmann@26100
   929
  with mult_div_cancel have nq: "n * q = m - (m mod n)" by simp
haftmann@26100
   930
  then have A: "n * q \<le> m" by simp
haftmann@26100
   931
  have "n - (m mod n) > 0" using mod_less_divisor assms by auto
haftmann@26100
   932
  then have "m < m + (n - (m mod n))" by simp
haftmann@26100
   933
  then have "m < n + (m - (m mod n))" by simp
haftmann@26100
   934
  with nq have "m < n + n * q" by simp
haftmann@26100
   935
  then have B: "m < n * Suc q" by simp
haftmann@26100
   936
  from A B show ?lhs ..
haftmann@26100
   937
next
haftmann@26100
   938
  assume P: ?lhs
haftmann@33340
   939
  then have "divmod_nat_rel m n (q, m - n * q)"
haftmann@33340
   940
    unfolding divmod_nat_rel_def by (auto simp add: mult_ac)
haftmann@33340
   941
  with divmod_nat_rel_unique divmod_nat_rel [of m n]
haftmann@30923
   942
  have "(q, m - n * q) = (m div n, m mod n)" by auto
haftmann@30923
   943
  then show ?rhs by simp
haftmann@26100
   944
qed
berghofe@13882
   945
berghofe@13882
   946
theorem split_div':
berghofe@13882
   947
  "P ((m::nat) div n) = ((n = 0 \<and> P 0) \<or>
paulson@14267
   948
   (\<exists>q. (n * q \<le> m \<and> m < n * (Suc q)) \<and> P q))"
berghofe@13882
   949
  apply (case_tac "0 < n")
berghofe@13882
   950
  apply (simp only: add: split_div_lemma)
haftmann@27651
   951
  apply simp_all
berghofe@13882
   952
  done
berghofe@13882
   953
nipkow@13189
   954
lemma split_mod:
nipkow@13189
   955
 "P(n mod k :: nat) =
nipkow@13189
   956
 ((k = 0 \<longrightarrow> P n) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P j)))"
nipkow@13189
   957
 (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
nipkow@13189
   958
proof
nipkow@13189
   959
  assume P: ?P
nipkow@13189
   960
  show ?Q
nipkow@13189
   961
  proof (cases)
nipkow@13189
   962
    assume "k = 0"
haftmann@27651
   963
    with P show ?Q by simp
nipkow@13189
   964
  next
nipkow@13189
   965
    assume not0: "k \<noteq> 0"
nipkow@13189
   966
    thus ?Q
nipkow@13189
   967
    proof (simp, intro allI impI)
nipkow@13189
   968
      fix i j
nipkow@13189
   969
      assume "n = k*i + j" "j < k"
nipkow@13189
   970
      thus "P j" using not0 P by(simp add:add_ac mult_ac)
nipkow@13189
   971
    qed
nipkow@13189
   972
  qed
nipkow@13189
   973
next
nipkow@13189
   974
  assume Q: ?Q
nipkow@13189
   975
  show ?P
nipkow@13189
   976
  proof (cases)
nipkow@13189
   977
    assume "k = 0"
haftmann@27651
   978
    with Q show ?P by simp
nipkow@13189
   979
  next
nipkow@13189
   980
    assume not0: "k \<noteq> 0"
nipkow@13189
   981
    with Q have R: ?R by simp
nipkow@13189
   982
    from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
nipkow@13517
   983
    show ?P by simp
nipkow@13189
   984
  qed
nipkow@13189
   985
qed
nipkow@13189
   986
berghofe@13882
   987
theorem mod_div_equality': "(m::nat) mod n = m - (m div n) * n"
berghofe@13882
   988
  apply (rule_tac P="%x. m mod n = x - (m div n) * n" in
berghofe@13882
   989
    subst [OF mod_div_equality [of _ n]])
berghofe@13882
   990
  apply arith
berghofe@13882
   991
  done
berghofe@13882
   992
haftmann@22800
   993
lemma div_mod_equality':
haftmann@22800
   994
  fixes m n :: nat
haftmann@22800
   995
  shows "m div n * n = m - m mod n"
haftmann@22800
   996
proof -
haftmann@22800
   997
  have "m mod n \<le> m mod n" ..
haftmann@22800
   998
  from div_mod_equality have 
haftmann@22800
   999
    "m div n * n + m mod n - m mod n = m - m mod n" by simp
haftmann@22800
  1000
  with diff_add_assoc [OF `m mod n \<le> m mod n`, of "m div n * n"] have
haftmann@22800
  1001
    "m div n * n + (m mod n - m mod n) = m - m mod n"
haftmann@22800
  1002
    by simp
haftmann@22800
  1003
  then show ?thesis by simp
haftmann@22800
  1004
qed
haftmann@22800
  1005
haftmann@22800
  1006
haftmann@25942
  1007
subsubsection {*An ``induction'' law for modulus arithmetic.*}
paulson@14640
  1008
paulson@14640
  1009
lemma mod_induct_0:
paulson@14640
  1010
  assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
paulson@14640
  1011
  and base: "P i" and i: "i<p"
paulson@14640
  1012
  shows "P 0"
paulson@14640
  1013
proof (rule ccontr)
paulson@14640
  1014
  assume contra: "\<not>(P 0)"
paulson@14640
  1015
  from i have p: "0<p" by simp
paulson@14640
  1016
  have "\<forall>k. 0<k \<longrightarrow> \<not> P (p-k)" (is "\<forall>k. ?A k")
paulson@14640
  1017
  proof
paulson@14640
  1018
    fix k
paulson@14640
  1019
    show "?A k"
paulson@14640
  1020
    proof (induct k)
paulson@14640
  1021
      show "?A 0" by simp  -- "by contradiction"
paulson@14640
  1022
    next
paulson@14640
  1023
      fix n
paulson@14640
  1024
      assume ih: "?A n"
paulson@14640
  1025
      show "?A (Suc n)"
paulson@14640
  1026
      proof (clarsimp)
wenzelm@22718
  1027
        assume y: "P (p - Suc n)"
wenzelm@22718
  1028
        have n: "Suc n < p"
wenzelm@22718
  1029
        proof (rule ccontr)
wenzelm@22718
  1030
          assume "\<not>(Suc n < p)"
wenzelm@22718
  1031
          hence "p - Suc n = 0"
wenzelm@22718
  1032
            by simp
wenzelm@22718
  1033
          with y contra show "False"
wenzelm@22718
  1034
            by simp
wenzelm@22718
  1035
        qed
wenzelm@22718
  1036
        hence n2: "Suc (p - Suc n) = p-n" by arith
wenzelm@22718
  1037
        from p have "p - Suc n < p" by arith
wenzelm@22718
  1038
        with y step have z: "P ((Suc (p - Suc n)) mod p)"
wenzelm@22718
  1039
          by blast
wenzelm@22718
  1040
        show "False"
wenzelm@22718
  1041
        proof (cases "n=0")
wenzelm@22718
  1042
          case True
wenzelm@22718
  1043
          with z n2 contra show ?thesis by simp
wenzelm@22718
  1044
        next
wenzelm@22718
  1045
          case False
wenzelm@22718
  1046
          with p have "p-n < p" by arith
wenzelm@22718
  1047
          with z n2 False ih show ?thesis by simp
wenzelm@22718
  1048
        qed
paulson@14640
  1049
      qed
paulson@14640
  1050
    qed
paulson@14640
  1051
  qed
paulson@14640
  1052
  moreover
paulson@14640
  1053
  from i obtain k where "0<k \<and> i+k=p"
paulson@14640
  1054
    by (blast dest: less_imp_add_positive)
paulson@14640
  1055
  hence "0<k \<and> i=p-k" by auto
paulson@14640
  1056
  moreover
paulson@14640
  1057
  note base
paulson@14640
  1058
  ultimately
paulson@14640
  1059
  show "False" by blast
paulson@14640
  1060
qed
paulson@14640
  1061
paulson@14640
  1062
lemma mod_induct:
paulson@14640
  1063
  assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
paulson@14640
  1064
  and base: "P i" and i: "i<p" and j: "j<p"
paulson@14640
  1065
  shows "P j"
paulson@14640
  1066
proof -
paulson@14640
  1067
  have "\<forall>j<p. P j"
paulson@14640
  1068
  proof
paulson@14640
  1069
    fix j
paulson@14640
  1070
    show "j<p \<longrightarrow> P j" (is "?A j")
paulson@14640
  1071
    proof (induct j)
paulson@14640
  1072
      from step base i show "?A 0"
wenzelm@22718
  1073
        by (auto elim: mod_induct_0)
paulson@14640
  1074
    next
paulson@14640
  1075
      fix k
paulson@14640
  1076
      assume ih: "?A k"
paulson@14640
  1077
      show "?A (Suc k)"
paulson@14640
  1078
      proof
wenzelm@22718
  1079
        assume suc: "Suc k < p"
wenzelm@22718
  1080
        hence k: "k<p" by simp
wenzelm@22718
  1081
        with ih have "P k" ..
wenzelm@22718
  1082
        with step k have "P (Suc k mod p)"
wenzelm@22718
  1083
          by blast
wenzelm@22718
  1084
        moreover
wenzelm@22718
  1085
        from suc have "Suc k mod p = Suc k"
wenzelm@22718
  1086
          by simp
wenzelm@22718
  1087
        ultimately
wenzelm@22718
  1088
        show "P (Suc k)" by simp
paulson@14640
  1089
      qed
paulson@14640
  1090
    qed
paulson@14640
  1091
  qed
paulson@14640
  1092
  with j show ?thesis by blast
paulson@14640
  1093
qed
paulson@14640
  1094
haftmann@33296
  1095
lemma div2_Suc_Suc [simp]: "Suc (Suc m) div 2 = Suc (m div 2)"
haftmann@33296
  1096
by (auto simp add: numeral_2_eq_2 le_div_geq)
haftmann@33296
  1097
haftmann@33296
  1098
lemma add_self_div_2 [simp]: "(m + m) div 2 = (m::nat)"
haftmann@33296
  1099
by (simp add: nat_mult_2 [symmetric])
haftmann@33296
  1100
haftmann@33296
  1101
lemma mod2_Suc_Suc [simp]: "Suc(Suc(m)) mod 2 = m mod 2"
haftmann@33296
  1102
apply (subgoal_tac "m mod 2 < 2")
haftmann@33296
  1103
apply (erule less_2_cases [THEN disjE])
haftmann@33296
  1104
apply (simp_all (no_asm_simp) add: Let_def mod_Suc nat_1)
haftmann@33296
  1105
done
haftmann@33296
  1106
haftmann@33296
  1107
lemma mod2_gr_0 [simp]: "0 < (m\<Colon>nat) mod 2 \<longleftrightarrow> m mod 2 = 1"
haftmann@33296
  1108
proof -
haftmann@33296
  1109
  { fix n :: nat have  "(n::nat) < 2 \<Longrightarrow> n = 0 \<or> n = 1" by (induct n) simp_all }
haftmann@33296
  1110
  moreover have "m mod 2 < 2" by simp
haftmann@33296
  1111
  ultimately have "m mod 2 = 0 \<or> m mod 2 = 1" .
haftmann@33296
  1112
  then show ?thesis by auto
haftmann@33296
  1113
qed
haftmann@33296
  1114
haftmann@33296
  1115
text{*These lemmas collapse some needless occurrences of Suc:
haftmann@33296
  1116
    at least three Sucs, since two and fewer are rewritten back to Suc again!
haftmann@33296
  1117
    We already have some rules to simplify operands smaller than 3.*}
haftmann@33296
  1118
haftmann@33296
  1119
lemma div_Suc_eq_div_add3 [simp]: "m div (Suc (Suc (Suc n))) = m div (3+n)"
haftmann@33296
  1120
by (simp add: Suc3_eq_add_3)
haftmann@33296
  1121
haftmann@33296
  1122
lemma mod_Suc_eq_mod_add3 [simp]: "m mod (Suc (Suc (Suc n))) = m mod (3+n)"
haftmann@33296
  1123
by (simp add: Suc3_eq_add_3)
haftmann@33296
  1124
haftmann@33296
  1125
lemma Suc_div_eq_add3_div: "(Suc (Suc (Suc m))) div n = (3+m) div n"
haftmann@33296
  1126
by (simp add: Suc3_eq_add_3)
haftmann@33296
  1127
haftmann@33296
  1128
lemma Suc_mod_eq_add3_mod: "(Suc (Suc (Suc m))) mod n = (3+m) mod n"
haftmann@33296
  1129
by (simp add: Suc3_eq_add_3)
haftmann@33296
  1130
haftmann@33296
  1131
lemmas Suc_div_eq_add3_div_number_of =
haftmann@33296
  1132
    Suc_div_eq_add3_div [of _ "number_of v", standard]
haftmann@33296
  1133
declare Suc_div_eq_add3_div_number_of [simp]
haftmann@33296
  1134
haftmann@33296
  1135
lemmas Suc_mod_eq_add3_mod_number_of =
haftmann@33296
  1136
    Suc_mod_eq_add3_mod [of _ "number_of v", standard]
haftmann@33296
  1137
declare Suc_mod_eq_add3_mod_number_of [simp]
haftmann@33296
  1138
paulson@3366
  1139
end