src/HOL/Divides.thy
author haftmann
Fri Jun 12 08:53:23 2015 +0200 (2015-06-12)
changeset 60429 d3d1e185cd63
parent 60353 838025c6e278
child 60516 0826b7025d07
permissions -rw-r--r--
uniform _ div _ as infix syntax for ring division
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
wenzelm@58889
     6
section {* The division operators div and mod *}
paulson@3366
     7
nipkow@15131
     8
theory Divides
haftmann@58778
     9
imports Parity
nipkow@15131
    10
begin
paulson@3366
    11
haftmann@60429
    12
subsection {* Abstract division in commutative semirings. *}
haftmann@25942
    13
haftmann@60352
    14
class div = dvd + divide +
haftmann@60352
    15
  fixes mod :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixl "mod" 70)
haftmann@25942
    16
haftmann@59833
    17
class semiring_div = semidom + div +
haftmann@25942
    18
  assumes mod_div_equality: "a div b * b + a mod b = a"
haftmann@27651
    19
    and div_by_0 [simp]: "a div 0 = 0"
haftmann@27651
    20
    and div_0 [simp]: "0 div a = 0"
haftmann@27651
    21
    and div_mult_self1 [simp]: "b \<noteq> 0 \<Longrightarrow> (a + c * b) div b = c + a div b"
haftmann@30930
    22
    and div_mult_mult1 [simp]: "c \<noteq> 0 \<Longrightarrow> (c * a) div (c * b) = a div b"
haftmann@25942
    23
begin
haftmann@25942
    24
haftmann@60353
    25
subclass semidom_divide
haftmann@60353
    26
proof
haftmann@60353
    27
  fix b a
haftmann@60353
    28
  assume "b \<noteq> 0"
haftmann@60353
    29
  then show "a * b div b = a"
haftmann@60353
    30
    using div_mult_self1 [of b 0 a] by (simp add: ac_simps)
haftmann@60353
    31
qed simp
haftmann@58953
    32
haftmann@59009
    33
lemma power_not_zero: -- \<open>FIXME cf. @{text field_power_not_zero}\<close>
haftmann@59009
    34
  "a \<noteq> 0 \<Longrightarrow> a ^ n \<noteq> 0"
haftmann@59009
    35
  by (induct n) (simp_all add: no_zero_divisors)
haftmann@59009
    36
haftmann@59009
    37
lemma semiring_div_power_eq_0_iff: -- \<open>FIXME cf. @{text power_eq_0_iff}, @{text power_eq_0_nat_iff}\<close>
haftmann@59009
    38
  "n \<noteq> 0 \<Longrightarrow> a ^ n = 0 \<longleftrightarrow> a = 0"
haftmann@59009
    39
  using power_not_zero [of a n] by (auto simp add: zero_power)
haftmann@59009
    40
haftmann@60429
    41
text {* @{const divide} and @{const mod} *}
haftmann@26100
    42
haftmann@26062
    43
lemma mod_div_equality2: "b * (a div b) + a mod b = a"
haftmann@57512
    44
  unfolding mult.commute [of b]
haftmann@26062
    45
  by (rule mod_div_equality)
haftmann@26062
    46
huffman@29403
    47
lemma mod_div_equality': "a mod b + a div b * b = a"
huffman@29403
    48
  using mod_div_equality [of a b]
haftmann@57514
    49
  by (simp only: ac_simps)
huffman@29403
    50
haftmann@26062
    51
lemma div_mod_equality: "((a div b) * b + a mod b) + c = a + c"
haftmann@30934
    52
  by (simp add: mod_div_equality)
haftmann@26062
    53
haftmann@26062
    54
lemma div_mod_equality2: "(b * (a div b) + a mod b) + c = a + c"
haftmann@30934
    55
  by (simp add: mod_div_equality2)
haftmann@26062
    56
haftmann@27651
    57
lemma mod_by_0 [simp]: "a mod 0 = a"
haftmann@30934
    58
  using mod_div_equality [of a zero] by simp
haftmann@27651
    59
haftmann@27651
    60
lemma mod_0 [simp]: "0 mod a = 0"
haftmann@30934
    61
  using mod_div_equality [of zero a] div_0 by simp
haftmann@27651
    62
haftmann@27651
    63
lemma div_mult_self2 [simp]:
haftmann@27651
    64
  assumes "b \<noteq> 0"
haftmann@27651
    65
  shows "(a + b * c) div b = c + a div b"
haftmann@57512
    66
  using assms div_mult_self1 [of b a c] by (simp add: mult.commute)
haftmann@26100
    67
haftmann@54221
    68
lemma div_mult_self3 [simp]:
haftmann@54221
    69
  assumes "b \<noteq> 0"
haftmann@54221
    70
  shows "(c * b + a) div b = c + a div b"
haftmann@54221
    71
  using assms by (simp add: add.commute)
haftmann@54221
    72
haftmann@54221
    73
lemma div_mult_self4 [simp]:
haftmann@54221
    74
  assumes "b \<noteq> 0"
haftmann@54221
    75
  shows "(b * c + a) div b = c + a div b"
haftmann@54221
    76
  using assms by (simp add: add.commute)
haftmann@54221
    77
haftmann@27651
    78
lemma mod_mult_self1 [simp]: "(a + c * b) mod b = a mod b"
haftmann@27651
    79
proof (cases "b = 0")
haftmann@27651
    80
  case True then show ?thesis by simp
haftmann@27651
    81
next
haftmann@27651
    82
  case False
haftmann@27651
    83
  have "a + c * b = (a + c * b) div b * b + (a + c * b) mod b"
haftmann@27651
    84
    by (simp add: mod_div_equality)
haftmann@27651
    85
  also from False div_mult_self1 [of b a c] have
haftmann@27651
    86
    "\<dots> = (c + a div b) * b + (a + c * b) mod b"
nipkow@29667
    87
      by (simp add: algebra_simps)
haftmann@27651
    88
  finally have "a = a div b * b + (a + c * b) mod b"
haftmann@57512
    89
    by (simp add: add.commute [of a] add.assoc distrib_right)
haftmann@27651
    90
  then have "a div b * b + (a + c * b) mod b = a div b * b + a mod b"
haftmann@27651
    91
    by (simp add: mod_div_equality)
haftmann@27651
    92
  then show ?thesis by simp
haftmann@27651
    93
qed
haftmann@27651
    94
haftmann@54221
    95
lemma mod_mult_self2 [simp]: 
haftmann@54221
    96
  "(a + b * c) mod b = a mod b"
haftmann@57512
    97
  by (simp add: mult.commute [of b])
haftmann@27651
    98
haftmann@54221
    99
lemma mod_mult_self3 [simp]:
haftmann@54221
   100
  "(c * b + a) mod b = a mod b"
haftmann@54221
   101
  by (simp add: add.commute)
haftmann@54221
   102
haftmann@54221
   103
lemma mod_mult_self4 [simp]:
haftmann@54221
   104
  "(b * c + a) mod b = a mod b"
haftmann@54221
   105
  by (simp add: add.commute)
haftmann@54221
   106
haftmann@60353
   107
lemma div_mult_self1_is_id:
haftmann@60353
   108
  "b \<noteq> 0 \<Longrightarrow> b * a div b = a"
haftmann@60353
   109
  by (fact nonzero_mult_divide_cancel_left)
haftmann@60353
   110
haftmann@60353
   111
lemma div_mult_self2_is_id:
haftmann@60353
   112
  "b \<noteq> 0 \<Longrightarrow> a * b div b = a"
haftmann@60353
   113
  by (fact nonzero_mult_divide_cancel_right)
haftmann@27651
   114
haftmann@27651
   115
lemma mod_mult_self1_is_0 [simp]: "b * a mod b = 0"
haftmann@27651
   116
  using mod_mult_self2 [of 0 b a] by simp
haftmann@27651
   117
haftmann@27651
   118
lemma mod_mult_self2_is_0 [simp]: "a * b mod b = 0"
haftmann@27651
   119
  using mod_mult_self1 [of 0 a b] by simp
haftmann@26062
   120
haftmann@27651
   121
lemma div_by_1 [simp]: "a div 1 = a"
haftmann@27651
   122
  using div_mult_self2_is_id [of 1 a] zero_neq_one by simp
haftmann@27651
   123
haftmann@27651
   124
lemma mod_by_1 [simp]: "a mod 1 = 0"
haftmann@27651
   125
proof -
haftmann@27651
   126
  from mod_div_equality [of a one] div_by_1 have "a + a mod 1 = a" by simp
haftmann@27651
   127
  then have "a + a mod 1 = a + 0" by simp
haftmann@27651
   128
  then show ?thesis by (rule add_left_imp_eq)
haftmann@27651
   129
qed
haftmann@27651
   130
haftmann@27651
   131
lemma mod_self [simp]: "a mod a = 0"
haftmann@27651
   132
  using mod_mult_self2_is_0 [of 1] by simp
haftmann@27651
   133
haftmann@27651
   134
lemma div_self [simp]: "a \<noteq> 0 \<Longrightarrow> a div a = 1"
haftmann@27651
   135
  using div_mult_self2_is_id [of _ 1] by simp
haftmann@27651
   136
haftmann@27676
   137
lemma div_add_self1 [simp]:
haftmann@27651
   138
  assumes "b \<noteq> 0"
haftmann@27651
   139
  shows "(b + a) div b = a div b + 1"
haftmann@57512
   140
  using assms div_mult_self1 [of b a 1] by (simp add: add.commute)
haftmann@26062
   141
haftmann@27676
   142
lemma div_add_self2 [simp]:
haftmann@27651
   143
  assumes "b \<noteq> 0"
haftmann@27651
   144
  shows "(a + b) div b = a div b + 1"
haftmann@57512
   145
  using assms div_add_self1 [of b a] by (simp add: add.commute)
haftmann@27651
   146
haftmann@27676
   147
lemma mod_add_self1 [simp]:
haftmann@27651
   148
  "(b + a) mod b = a mod b"
haftmann@57512
   149
  using mod_mult_self1 [of a 1 b] by (simp add: add.commute)
haftmann@27651
   150
haftmann@27676
   151
lemma mod_add_self2 [simp]:
haftmann@27651
   152
  "(a + b) mod b = a mod b"
haftmann@27651
   153
  using mod_mult_self1 [of a 1 b] by simp
haftmann@27651
   154
haftmann@27651
   155
lemma mod_div_decomp:
haftmann@27651
   156
  fixes a b
haftmann@27651
   157
  obtains q r where "q = a div b" and "r = a mod b"
haftmann@27651
   158
    and "a = q * b + r"
haftmann@27651
   159
proof -
haftmann@27651
   160
  from mod_div_equality have "a = a div b * b + a mod b" by simp
haftmann@27651
   161
  moreover have "a div b = a div b" ..
haftmann@27651
   162
  moreover have "a mod b = a mod b" ..
haftmann@27651
   163
  note that ultimately show thesis by blast
haftmann@27651
   164
qed
haftmann@27651
   165
haftmann@58834
   166
lemma dvd_imp_mod_0 [simp]:
haftmann@58834
   167
  assumes "a dvd b"
haftmann@58834
   168
  shows "b mod a = 0"
haftmann@58834
   169
proof -
haftmann@58834
   170
  from assms obtain c where "b = a * c" ..
haftmann@58834
   171
  then have "b mod a = a * c mod a" by simp
haftmann@58834
   172
  then show "b mod a = 0" by simp
haftmann@58834
   173
qed
haftmann@58911
   174
haftmann@58911
   175
lemma mod_eq_0_iff_dvd:
haftmann@58911
   176
  "a mod b = 0 \<longleftrightarrow> b dvd a"
haftmann@58911
   177
proof
haftmann@58911
   178
  assume "b dvd a"
haftmann@58911
   179
  then show "a mod b = 0" by simp
haftmann@58911
   180
next
haftmann@58911
   181
  assume "a mod b = 0"
haftmann@58911
   182
  with mod_div_equality [of a b] have "a div b * b = a" by simp
haftmann@58911
   183
  then have "a = b * (a div b)" by (simp add: ac_simps)
haftmann@58911
   184
  then show "b dvd a" ..
haftmann@58911
   185
qed
haftmann@58911
   186
haftmann@58834
   187
lemma dvd_eq_mod_eq_0 [code]:
haftmann@58834
   188
  "a dvd b \<longleftrightarrow> b mod a = 0"
haftmann@58911
   189
  by (simp add: mod_eq_0_iff_dvd)
haftmann@58911
   190
haftmann@58911
   191
lemma mod_div_trivial [simp]:
haftmann@58911
   192
  "a mod b div b = 0"
huffman@29403
   193
proof (cases "b = 0")
huffman@29403
   194
  assume "b = 0"
huffman@29403
   195
  thus ?thesis by simp
huffman@29403
   196
next
huffman@29403
   197
  assume "b \<noteq> 0"
huffman@29403
   198
  hence "a div b + a mod b div b = (a mod b + a div b * b) div b"
huffman@29403
   199
    by (rule div_mult_self1 [symmetric])
huffman@29403
   200
  also have "\<dots> = a div b"
huffman@29403
   201
    by (simp only: mod_div_equality')
huffman@29403
   202
  also have "\<dots> = a div b + 0"
huffman@29403
   203
    by simp
huffman@29403
   204
  finally show ?thesis
huffman@29403
   205
    by (rule add_left_imp_eq)
huffman@29403
   206
qed
huffman@29403
   207
haftmann@58911
   208
lemma mod_mod_trivial [simp]:
haftmann@58911
   209
  "a mod b mod b = a mod b"
huffman@29403
   210
proof -
huffman@29403
   211
  have "a mod b mod b = (a mod b + a div b * b) mod b"
huffman@29403
   212
    by (simp only: mod_mult_self1)
huffman@29403
   213
  also have "\<dots> = a mod b"
huffman@29403
   214
    by (simp only: mod_div_equality')
huffman@29403
   215
  finally show ?thesis .
huffman@29403
   216
qed
huffman@29403
   217
haftmann@58834
   218
lemma dvd_div_mult_self [simp]:
haftmann@58834
   219
  "a dvd b \<Longrightarrow> (b div a) * a = b"
haftmann@58834
   220
  using mod_div_equality [of b a, symmetric] by simp
haftmann@58834
   221
haftmann@58834
   222
lemma dvd_mult_div_cancel [simp]:
haftmann@58834
   223
  "a dvd b \<Longrightarrow> a * (b div a) = b"
haftmann@58834
   224
  using dvd_div_mult_self by (simp add: ac_simps)
haftmann@58834
   225
haftmann@58834
   226
lemma dvd_div_mult:
haftmann@58834
   227
  "a dvd b \<Longrightarrow> (b div a) * c = (b * c) div a"
haftmann@58834
   228
  by (cases "a = 0") (auto elim!: dvdE simp add: mult.assoc)
haftmann@58834
   229
haftmann@58834
   230
lemma div_dvd_div [simp]:
haftmann@58834
   231
  assumes "a dvd b" and "a dvd c"
haftmann@58834
   232
  shows "b div a dvd c div a \<longleftrightarrow> b dvd c"
haftmann@58834
   233
using assms apply (cases "a = 0")
haftmann@58834
   234
apply auto
nipkow@29925
   235
apply (unfold dvd_def)
nipkow@29925
   236
apply auto
haftmann@57512
   237
 apply(blast intro:mult.assoc[symmetric])
haftmann@57512
   238
apply(fastforce simp add: mult.assoc)
nipkow@29925
   239
done
nipkow@29925
   240
haftmann@58834
   241
lemma dvd_mod_imp_dvd:
haftmann@58834
   242
  assumes "k dvd m mod n" and "k dvd n"
haftmann@58834
   243
  shows "k dvd m"
haftmann@58834
   244
proof -
haftmann@58834
   245
  from assms have "k dvd (m div n) * n + m mod n"
haftmann@58834
   246
    by (simp only: dvd_add dvd_mult)
haftmann@58834
   247
  then show ?thesis by (simp add: mod_div_equality)
haftmann@58834
   248
qed
huffman@30078
   249
huffman@29403
   250
text {* Addition respects modular equivalence. *}
huffman@29403
   251
huffman@29403
   252
lemma mod_add_left_eq: "(a + b) mod c = (a mod c + b) mod c"
huffman@29403
   253
proof -
huffman@29403
   254
  have "(a + b) mod c = (a div c * c + a mod c + b) mod c"
huffman@29403
   255
    by (simp only: mod_div_equality)
huffman@29403
   256
  also have "\<dots> = (a mod c + b + a div c * c) mod c"
haftmann@57514
   257
    by (simp only: ac_simps)
huffman@29403
   258
  also have "\<dots> = (a mod c + b) mod c"
huffman@29403
   259
    by (rule mod_mult_self1)
huffman@29403
   260
  finally show ?thesis .
huffman@29403
   261
qed
huffman@29403
   262
huffman@29403
   263
lemma mod_add_right_eq: "(a + b) mod c = (a + b mod c) mod c"
huffman@29403
   264
proof -
huffman@29403
   265
  have "(a + b) mod c = (a + (b div c * c + b mod c)) mod c"
huffman@29403
   266
    by (simp only: mod_div_equality)
huffman@29403
   267
  also have "\<dots> = (a + b mod c + b div c * c) mod c"
haftmann@57514
   268
    by (simp only: ac_simps)
huffman@29403
   269
  also have "\<dots> = (a + b mod c) mod c"
huffman@29403
   270
    by (rule mod_mult_self1)
huffman@29403
   271
  finally show ?thesis .
huffman@29403
   272
qed
huffman@29403
   273
huffman@29403
   274
lemma mod_add_eq: "(a + b) mod c = (a mod c + b mod c) mod c"
huffman@29403
   275
by (rule trans [OF mod_add_left_eq mod_add_right_eq])
huffman@29403
   276
huffman@29403
   277
lemma mod_add_cong:
huffman@29403
   278
  assumes "a mod c = a' mod c"
huffman@29403
   279
  assumes "b mod c = b' mod c"
huffman@29403
   280
  shows "(a + b) mod c = (a' + b') mod c"
huffman@29403
   281
proof -
huffman@29403
   282
  have "(a mod c + b mod c) mod c = (a' mod c + b' mod c) mod c"
huffman@29403
   283
    unfolding assms ..
huffman@29403
   284
  thus ?thesis
huffman@29403
   285
    by (simp only: mod_add_eq [symmetric])
huffman@29403
   286
qed
huffman@29403
   287
haftmann@30923
   288
lemma div_add [simp]: "z dvd x \<Longrightarrow> z dvd y
nipkow@30837
   289
  \<Longrightarrow> (x + y) div z = x div z + y div z"
haftmann@30923
   290
by (cases "z = 0", simp, unfold dvd_def, auto simp add: algebra_simps)
nipkow@30837
   291
huffman@29403
   292
text {* Multiplication respects modular equivalence. *}
huffman@29403
   293
huffman@29403
   294
lemma mod_mult_left_eq: "(a * b) mod c = ((a mod c) * b) mod c"
huffman@29403
   295
proof -
huffman@29403
   296
  have "(a * b) mod c = ((a div c * c + a mod c) * b) mod c"
huffman@29403
   297
    by (simp only: mod_div_equality)
huffman@29403
   298
  also have "\<dots> = (a mod c * b + a div c * b * c) mod c"
nipkow@29667
   299
    by (simp only: algebra_simps)
huffman@29403
   300
  also have "\<dots> = (a mod c * b) mod c"
huffman@29403
   301
    by (rule mod_mult_self1)
huffman@29403
   302
  finally show ?thesis .
huffman@29403
   303
qed
huffman@29403
   304
huffman@29403
   305
lemma mod_mult_right_eq: "(a * b) mod c = (a * (b mod c)) mod c"
huffman@29403
   306
proof -
huffman@29403
   307
  have "(a * b) mod c = (a * (b div c * c + b mod c)) mod c"
huffman@29403
   308
    by (simp only: mod_div_equality)
huffman@29403
   309
  also have "\<dots> = (a * (b mod c) + a * (b div c) * c) mod c"
nipkow@29667
   310
    by (simp only: algebra_simps)
huffman@29403
   311
  also have "\<dots> = (a * (b mod c)) mod c"
huffman@29403
   312
    by (rule mod_mult_self1)
huffman@29403
   313
  finally show ?thesis .
huffman@29403
   314
qed
huffman@29403
   315
huffman@29403
   316
lemma mod_mult_eq: "(a * b) mod c = ((a mod c) * (b mod c)) mod c"
huffman@29403
   317
by (rule trans [OF mod_mult_left_eq mod_mult_right_eq])
huffman@29403
   318
huffman@29403
   319
lemma mod_mult_cong:
huffman@29403
   320
  assumes "a mod c = a' mod c"
huffman@29403
   321
  assumes "b mod c = b' mod c"
huffman@29403
   322
  shows "(a * b) mod c = (a' * b') mod c"
huffman@29403
   323
proof -
huffman@29403
   324
  have "(a mod c * (b mod c)) mod c = (a' mod c * (b' mod c)) mod c"
huffman@29403
   325
    unfolding assms ..
huffman@29403
   326
  thus ?thesis
huffman@29403
   327
    by (simp only: mod_mult_eq [symmetric])
huffman@29403
   328
qed
huffman@29403
   329
huffman@47164
   330
text {* Exponentiation respects modular equivalence. *}
huffman@47164
   331
huffman@47164
   332
lemma power_mod: "(a mod b)^n mod b = a^n mod b"
huffman@47164
   333
apply (induct n, simp_all)
huffman@47164
   334
apply (rule mod_mult_right_eq [THEN trans])
huffman@47164
   335
apply (simp (no_asm_simp))
huffman@47164
   336
apply (rule mod_mult_eq [symmetric])
huffman@47164
   337
done
huffman@47164
   338
huffman@29404
   339
lemma mod_mod_cancel:
huffman@29404
   340
  assumes "c dvd b"
huffman@29404
   341
  shows "a mod b mod c = a mod c"
huffman@29404
   342
proof -
huffman@29404
   343
  from `c dvd b` obtain k where "b = c * k"
huffman@29404
   344
    by (rule dvdE)
huffman@29404
   345
  have "a mod b mod c = a mod (c * k) mod c"
huffman@29404
   346
    by (simp only: `b = c * k`)
huffman@29404
   347
  also have "\<dots> = (a mod (c * k) + a div (c * k) * k * c) mod c"
huffman@29404
   348
    by (simp only: mod_mult_self1)
huffman@29404
   349
  also have "\<dots> = (a div (c * k) * (c * k) + a mod (c * k)) mod c"
haftmann@58786
   350
    by (simp only: ac_simps)
huffman@29404
   351
  also have "\<dots> = a mod c"
huffman@29404
   352
    by (simp only: mod_div_equality)
huffman@29404
   353
  finally show ?thesis .
huffman@29404
   354
qed
huffman@29404
   355
haftmann@30930
   356
lemma div_mult_div_if_dvd:
haftmann@30930
   357
  "y dvd x \<Longrightarrow> z dvd w \<Longrightarrow> (x div y) * (w div z) = (x * w) div (y * z)"
haftmann@30930
   358
  apply (cases "y = 0", simp)
haftmann@30930
   359
  apply (cases "z = 0", simp)
haftmann@30930
   360
  apply (auto elim!: dvdE simp add: algebra_simps)
haftmann@57512
   361
  apply (subst mult.assoc [symmetric])
nipkow@30476
   362
  apply (simp add: no_zero_divisors)
haftmann@30930
   363
  done
haftmann@30930
   364
haftmann@35367
   365
lemma div_mult_swap:
haftmann@35367
   366
  assumes "c dvd b"
haftmann@35367
   367
  shows "a * (b div c) = (a * b) div c"
haftmann@35367
   368
proof -
haftmann@35367
   369
  from assms have "b div c * (a div 1) = b * a div (c * 1)"
haftmann@35367
   370
    by (simp only: div_mult_div_if_dvd one_dvd)
haftmann@57512
   371
  then show ?thesis by (simp add: mult.commute)
haftmann@35367
   372
qed
haftmann@35367
   373
   
haftmann@30930
   374
lemma div_mult_mult2 [simp]:
haftmann@30930
   375
  "c \<noteq> 0 \<Longrightarrow> (a * c) div (b * c) = a div b"
haftmann@57512
   376
  by (drule div_mult_mult1) (simp add: mult.commute)
haftmann@30930
   377
haftmann@30930
   378
lemma div_mult_mult1_if [simp]:
haftmann@30930
   379
  "(c * a) div (c * b) = (if c = 0 then 0 else a div b)"
haftmann@30930
   380
  by simp_all
nipkow@30476
   381
haftmann@30930
   382
lemma mod_mult_mult1:
haftmann@30930
   383
  "(c * a) mod (c * b) = c * (a mod b)"
haftmann@30930
   384
proof (cases "c = 0")
haftmann@30930
   385
  case True then show ?thesis by simp
haftmann@30930
   386
next
haftmann@30930
   387
  case False
haftmann@30930
   388
  from mod_div_equality
haftmann@30930
   389
  have "((c * a) div (c * b)) * (c * b) + (c * a) mod (c * b) = c * a" .
haftmann@30930
   390
  with False have "c * ((a div b) * b + a mod b) + (c * a) mod (c * b)
haftmann@30930
   391
    = c * a + c * (a mod b)" by (simp add: algebra_simps)
haftmann@30930
   392
  with mod_div_equality show ?thesis by simp 
haftmann@30930
   393
qed
haftmann@30930
   394
  
haftmann@30930
   395
lemma mod_mult_mult2:
haftmann@30930
   396
  "(a * c) mod (b * c) = (a mod b) * c"
haftmann@57512
   397
  using mod_mult_mult1 [of c a b] by (simp add: mult.commute)
haftmann@30930
   398
huffman@47159
   399
lemma mult_mod_left: "(a mod b) * c = (a * c) mod (b * c)"
huffman@47159
   400
  by (fact mod_mult_mult2 [symmetric])
huffman@47159
   401
huffman@47159
   402
lemma mult_mod_right: "c * (a mod b) = (c * a) mod (c * b)"
huffman@47159
   403
  by (fact mod_mult_mult1 [symmetric])
huffman@47159
   404
haftmann@59009
   405
lemma dvd_times_left_cancel_iff [simp]: -- \<open>FIXME generalize\<close>
haftmann@59009
   406
  assumes "c \<noteq> 0"
haftmann@59009
   407
  shows "c * a dvd c * b \<longleftrightarrow> a dvd b"
haftmann@59009
   408
proof -
haftmann@59009
   409
  have "(c * b) mod (c * a) = 0 \<longleftrightarrow> b mod a = 0" (is "?P \<longleftrightarrow> ?Q")
haftmann@59009
   410
    using assms by (simp add: mod_mult_mult1)
haftmann@59009
   411
  then show ?thesis by (simp add: mod_eq_0_iff_dvd)
haftmann@59009
   412
qed
haftmann@59009
   413
haftmann@59009
   414
lemma dvd_times_right_cancel_iff [simp]: -- \<open>FIXME generalize\<close>
haftmann@59009
   415
  assumes "c \<noteq> 0"
haftmann@59009
   416
  shows "a * c dvd b * c \<longleftrightarrow> a dvd b"
haftmann@59009
   417
  using assms dvd_times_left_cancel_iff [of c a b] by (simp add: ac_simps)
haftmann@59009
   418
huffman@31662
   419
lemma dvd_mod: "k dvd m \<Longrightarrow> k dvd n \<Longrightarrow> k dvd (m mod n)"
huffman@31662
   420
  unfolding dvd_def by (auto simp add: mod_mult_mult1)
huffman@31662
   421
huffman@31662
   422
lemma dvd_mod_iff: "k dvd n \<Longrightarrow> k dvd (m mod n) \<longleftrightarrow> k dvd m"
huffman@31662
   423
by (blast intro: dvd_mod_imp_dvd dvd_mod)
huffman@31662
   424
haftmann@31009
   425
lemma div_power:
huffman@31661
   426
  "y dvd x \<Longrightarrow> (x div y) ^ n = x ^ n div y ^ n"
nipkow@30476
   427
apply (induct n)
nipkow@30476
   428
 apply simp
nipkow@30476
   429
apply(simp add: div_mult_div_if_dvd dvd_power_same)
nipkow@30476
   430
done
nipkow@30476
   431
haftmann@35367
   432
lemma dvd_div_eq_mult:
haftmann@35367
   433
  assumes "a \<noteq> 0" and "a dvd b"  
haftmann@35367
   434
  shows "b div a = c \<longleftrightarrow> b = c * a"
haftmann@35367
   435
proof
haftmann@35367
   436
  assume "b = c * a"
haftmann@35367
   437
  then show "b div a = c" by (simp add: assms)
haftmann@35367
   438
next
haftmann@35367
   439
  assume "b div a = c"
haftmann@35367
   440
  then have "b div a * a = c * a" by simp
haftmann@60353
   441
  moreover from `a dvd b` have "b div a * a = b" by simp
haftmann@35367
   442
  ultimately show "b = c * a" by simp
haftmann@35367
   443
qed
haftmann@35367
   444
   
haftmann@35367
   445
lemma dvd_div_div_eq_mult:
haftmann@35367
   446
  assumes "a \<noteq> 0" "c \<noteq> 0" and "a dvd b" "c dvd d"
haftmann@35367
   447
  shows "b div a = d div c \<longleftrightarrow> b * c = a * d"
haftmann@60353
   448
  using assms by (auto simp add: mult.commute [of _ a] dvd_div_eq_mult div_mult_swap intro: sym)
haftmann@35367
   449
huffman@31661
   450
end
huffman@31661
   451
haftmann@59833
   452
class ring_div = comm_ring_1 + semiring_div
huffman@29405
   453
begin
huffman@29405
   454
haftmann@60353
   455
subclass idom_divide ..
haftmann@36634
   456
huffman@29405
   457
text {* Negation respects modular equivalence. *}
huffman@29405
   458
huffman@29405
   459
lemma mod_minus_eq: "(- a) mod b = (- (a mod b)) mod b"
huffman@29405
   460
proof -
huffman@29405
   461
  have "(- a) mod b = (- (a div b * b + a mod b)) mod b"
huffman@29405
   462
    by (simp only: mod_div_equality)
huffman@29405
   463
  also have "\<dots> = (- (a mod b) + - (a div b) * b) mod b"
haftmann@57514
   464
    by (simp add: ac_simps)
huffman@29405
   465
  also have "\<dots> = (- (a mod b)) mod b"
huffman@29405
   466
    by (rule mod_mult_self1)
huffman@29405
   467
  finally show ?thesis .
huffman@29405
   468
qed
huffman@29405
   469
huffman@29405
   470
lemma mod_minus_cong:
huffman@29405
   471
  assumes "a mod b = a' mod b"
huffman@29405
   472
  shows "(- a) mod b = (- a') mod b"
huffman@29405
   473
proof -
huffman@29405
   474
  have "(- (a mod b)) mod b = (- (a' mod b)) mod b"
huffman@29405
   475
    unfolding assms ..
huffman@29405
   476
  thus ?thesis
huffman@29405
   477
    by (simp only: mod_minus_eq [symmetric])
huffman@29405
   478
qed
huffman@29405
   479
huffman@29405
   480
text {* Subtraction respects modular equivalence. *}
huffman@29405
   481
haftmann@54230
   482
lemma mod_diff_left_eq:
haftmann@54230
   483
  "(a - b) mod c = (a mod c - b) mod c"
haftmann@54230
   484
  using mod_add_cong [of a c "a mod c" "- b" "- b"] by simp
haftmann@54230
   485
haftmann@54230
   486
lemma mod_diff_right_eq:
haftmann@54230
   487
  "(a - b) mod c = (a - b mod c) mod c"
haftmann@54230
   488
  using mod_add_cong [of a c a "- b" "- (b mod c)"] mod_minus_cong [of "b mod c" c b] by simp
haftmann@54230
   489
haftmann@54230
   490
lemma mod_diff_eq:
haftmann@54230
   491
  "(a - b) mod c = (a mod c - b mod c) mod c"
haftmann@54230
   492
  using mod_add_cong [of a c "a mod c" "- b" "- (b mod c)"] mod_minus_cong [of "b mod c" c b] by simp
huffman@29405
   493
huffman@29405
   494
lemma mod_diff_cong:
huffman@29405
   495
  assumes "a mod c = a' mod c"
huffman@29405
   496
  assumes "b mod c = b' mod c"
huffman@29405
   497
  shows "(a - b) mod c = (a' - b') mod c"
haftmann@54230
   498
  using assms mod_add_cong [of a c a' "- b" "- b'"] mod_minus_cong [of b c "b'"] by simp
huffman@29405
   499
nipkow@30180
   500
lemma dvd_neg_div: "y dvd x \<Longrightarrow> -x div y = - (x div y)"
nipkow@30180
   501
apply (case_tac "y = 0") apply simp
nipkow@30180
   502
apply (auto simp add: dvd_def)
nipkow@30180
   503
apply (subgoal_tac "-(y * k) = y * - k")
thomas@57492
   504
 apply (simp only:)
nipkow@30180
   505
 apply (erule div_mult_self1_is_id)
nipkow@30180
   506
apply simp
nipkow@30180
   507
done
nipkow@30180
   508
nipkow@30180
   509
lemma dvd_div_neg: "y dvd x \<Longrightarrow> x div -y = - (x div y)"
nipkow@30180
   510
apply (case_tac "y = 0") apply simp
nipkow@30180
   511
apply (auto simp add: dvd_def)
nipkow@30180
   512
apply (subgoal_tac "y * k = -y * -k")
thomas@57492
   513
 apply (erule ssubst, rule div_mult_self1_is_id)
nipkow@30180
   514
 apply simp
nipkow@30180
   515
apply simp
nipkow@30180
   516
done
nipkow@30180
   517
nipkow@59473
   518
lemma div_diff[simp]:
nipkow@59380
   519
  "\<lbrakk> z dvd x; z dvd y\<rbrakk> \<Longrightarrow> (x - y) div z = x div z - y div z"
nipkow@59380
   520
using div_add[where y = "- z" for z]
nipkow@59380
   521
by (simp add: dvd_neg_div)
nipkow@59380
   522
huffman@47159
   523
lemma div_minus_minus [simp]: "(-a) div (-b) = a div b"
huffman@47159
   524
  using div_mult_mult1 [of "- 1" a b]
huffman@47159
   525
  unfolding neg_equal_0_iff_equal by simp
huffman@47159
   526
huffman@47159
   527
lemma mod_minus_minus [simp]: "(-a) mod (-b) = - (a mod b)"
huffman@47159
   528
  using mod_mult_mult1 [of "- 1" a b] by simp
huffman@47159
   529
huffman@47159
   530
lemma div_minus_right: "a div (-b) = (-a) div b"
huffman@47159
   531
  using div_minus_minus [of "-a" b] by simp
huffman@47159
   532
huffman@47159
   533
lemma mod_minus_right: "a mod (-b) = - ((-a) mod b)"
huffman@47159
   534
  using mod_minus_minus [of "-a" b] by simp
huffman@47159
   535
huffman@47160
   536
lemma div_minus1_right [simp]: "a div (-1) = -a"
huffman@47160
   537
  using div_minus_right [of a 1] by simp
huffman@47160
   538
huffman@47160
   539
lemma mod_minus1_right [simp]: "a mod (-1) = 0"
huffman@47160
   540
  using mod_minus_right [of a 1] by simp
huffman@47160
   541
haftmann@54221
   542
lemma minus_mod_self2 [simp]: 
haftmann@54221
   543
  "(a - b) mod b = a mod b"
haftmann@54221
   544
  by (simp add: mod_diff_right_eq)
haftmann@54221
   545
haftmann@54221
   546
lemma minus_mod_self1 [simp]: 
haftmann@54221
   547
  "(b - a) mod b = - a mod b"
haftmann@54230
   548
  using mod_add_self2 [of "- a" b] by simp
haftmann@54221
   549
huffman@29405
   550
end
huffman@29405
   551
haftmann@58778
   552
haftmann@58778
   553
subsubsection {* Parity and division *}
haftmann@58778
   554
haftmann@59833
   555
class semiring_div_parity = semiring_div + comm_semiring_1_diff_distrib + numeral + 
haftmann@54226
   556
  assumes parity: "a mod 2 = 0 \<or> a mod 2 = 1"
haftmann@58786
   557
  assumes one_mod_two_eq_one [simp]: "1 mod 2 = 1"
haftmann@58710
   558
  assumes zero_not_eq_two: "0 \<noteq> 2"
haftmann@54226
   559
begin
haftmann@54226
   560
haftmann@54226
   561
lemma parity_cases [case_names even odd]:
haftmann@54226
   562
  assumes "a mod 2 = 0 \<Longrightarrow> P"
haftmann@54226
   563
  assumes "a mod 2 = 1 \<Longrightarrow> P"
haftmann@54226
   564
  shows P
haftmann@54226
   565
  using assms parity by blast
haftmann@54226
   566
haftmann@58786
   567
lemma one_div_two_eq_zero [simp]:
haftmann@58778
   568
  "1 div 2 = 0"
haftmann@58778
   569
proof (cases "2 = 0")
haftmann@58778
   570
  case True then show ?thesis by simp
haftmann@58778
   571
next
haftmann@58778
   572
  case False
haftmann@58778
   573
  from mod_div_equality have "1 div 2 * 2 + 1 mod 2 = 1" .
haftmann@58778
   574
  with one_mod_two_eq_one have "1 div 2 * 2 + 1 = 1" by simp
haftmann@58953
   575
  then have "1 div 2 * 2 = 0" by (simp add: ac_simps add_left_imp_eq del: mult_eq_0_iff)
haftmann@58953
   576
  then have "1 div 2 = 0 \<or> 2 = 0" by simp
haftmann@58778
   577
  with False show ?thesis by auto
haftmann@58778
   578
qed
haftmann@58778
   579
haftmann@58786
   580
lemma not_mod_2_eq_0_eq_1 [simp]:
haftmann@58786
   581
  "a mod 2 \<noteq> 0 \<longleftrightarrow> a mod 2 = 1"
haftmann@58786
   582
  by (cases a rule: parity_cases) simp_all
haftmann@58786
   583
haftmann@58786
   584
lemma not_mod_2_eq_1_eq_0 [simp]:
haftmann@58786
   585
  "a mod 2 \<noteq> 1 \<longleftrightarrow> a mod 2 = 0"
haftmann@58786
   586
  by (cases a rule: parity_cases) simp_all
haftmann@58786
   587
haftmann@58778
   588
subclass semiring_parity
haftmann@58778
   589
proof (unfold_locales, unfold dvd_eq_mod_eq_0 not_mod_2_eq_0_eq_1)
haftmann@58778
   590
  show "1 mod 2 = 1"
haftmann@58778
   591
    by (fact one_mod_two_eq_one)
haftmann@58778
   592
next
haftmann@58778
   593
  fix a b
haftmann@58778
   594
  assume "a mod 2 = 1"
haftmann@58778
   595
  moreover assume "b mod 2 = 1"
haftmann@58778
   596
  ultimately show "(a + b) mod 2 = 0"
haftmann@58778
   597
    using mod_add_eq [of a b 2] by simp
haftmann@58778
   598
next
haftmann@58778
   599
  fix a b
haftmann@58778
   600
  assume "(a * b) mod 2 = 0"
haftmann@58778
   601
  then have "(a mod 2) * (b mod 2) = 0"
haftmann@58778
   602
    by (cases "a mod 2 = 0") (simp_all add: mod_mult_eq [of a b 2])
haftmann@58778
   603
  then show "a mod 2 = 0 \<or> b mod 2 = 0"
haftmann@58778
   604
    by (rule divisors_zero)
haftmann@58778
   605
next
haftmann@58778
   606
  fix a
haftmann@58778
   607
  assume "a mod 2 = 1"
haftmann@58778
   608
  then have "a = a div 2 * 2 + 1" using mod_div_equality [of a 2] by simp
haftmann@58778
   609
  then show "\<exists>b. a = b + 1" ..
haftmann@58778
   610
qed
haftmann@58778
   611
haftmann@58778
   612
lemma even_iff_mod_2_eq_zero:
haftmann@58778
   613
  "even a \<longleftrightarrow> a mod 2 = 0"
haftmann@58778
   614
  by (fact dvd_eq_mod_eq_0)
haftmann@58778
   615
haftmann@58778
   616
lemma even_succ_div_two [simp]:
haftmann@58778
   617
  "even a \<Longrightarrow> (a + 1) div 2 = a div 2"
haftmann@58778
   618
  by (cases "a = 0") (auto elim!: evenE dest: mult_not_zero)
haftmann@58778
   619
haftmann@58778
   620
lemma odd_succ_div_two [simp]:
haftmann@58778
   621
  "odd a \<Longrightarrow> (a + 1) div 2 = a div 2 + 1"
haftmann@58778
   622
  by (auto elim!: oddE simp add: zero_not_eq_two [symmetric] add.assoc)
haftmann@58778
   623
haftmann@58778
   624
lemma even_two_times_div_two:
haftmann@58778
   625
  "even a \<Longrightarrow> 2 * (a div 2) = a"
haftmann@58778
   626
  by (fact dvd_mult_div_cancel)
haftmann@58778
   627
haftmann@58834
   628
lemma odd_two_times_div_two_succ [simp]:
haftmann@58778
   629
  "odd a \<Longrightarrow> 2 * (a div 2) + 1 = a"
haftmann@58778
   630
  using mod_div_equality2 [of 2 a] by (simp add: even_iff_mod_2_eq_zero)
haftmann@58778
   631
haftmann@54226
   632
end
haftmann@54226
   633
haftmann@25942
   634
haftmann@53067
   635
subsection {* Generic numeral division with a pragmatic type class *}
haftmann@53067
   636
haftmann@53067
   637
text {*
haftmann@53067
   638
  The following type class contains everything necessary to formulate
haftmann@53067
   639
  a division algorithm in ring structures with numerals, restricted
haftmann@53067
   640
  to its positive segments.  This is its primary motiviation, and it
haftmann@53067
   641
  could surely be formulated using a more fine-grained, more algebraic
haftmann@53067
   642
  and less technical class hierarchy.
haftmann@53067
   643
*}
haftmann@53067
   644
haftmann@59833
   645
class semiring_numeral_div = semiring_div + comm_semiring_1_diff_distrib + linordered_semidom +
haftmann@59816
   646
  assumes le_add_diff_inverse2: "b \<le> a \<Longrightarrow> a - b + b = a"
haftmann@59816
   647
  assumes div_less: "0 \<le> a \<Longrightarrow> a < b \<Longrightarrow> a div b = 0"
haftmann@53067
   648
    and mod_less: " 0 \<le> a \<Longrightarrow> a < b \<Longrightarrow> a mod b = a"
haftmann@53067
   649
    and div_positive: "0 < b \<Longrightarrow> b \<le> a \<Longrightarrow> a div b > 0"
haftmann@53067
   650
    and mod_less_eq_dividend: "0 \<le> a \<Longrightarrow> a mod b \<le> a"
haftmann@53067
   651
    and pos_mod_bound: "0 < b \<Longrightarrow> a mod b < b"
haftmann@53067
   652
    and pos_mod_sign: "0 < b \<Longrightarrow> 0 \<le> a mod b"
haftmann@53067
   653
    and mod_mult2_eq: "0 \<le> c \<Longrightarrow> a mod (b * c) = b * (a div b mod c) + a mod b"
haftmann@53067
   654
    and div_mult2_eq: "0 \<le> c \<Longrightarrow> a div (b * c) = a div b div c"
haftmann@53067
   655
  assumes discrete: "a < b \<longleftrightarrow> a + 1 \<le> b"
haftmann@53067
   656
begin
haftmann@53067
   657
haftmann@59816
   658
lemma mult_div_cancel:
haftmann@59816
   659
  "b * (a div b) = a - a mod b"
haftmann@59816
   660
proof -
haftmann@59816
   661
  have "b * (a div b) + a mod b = a"
haftmann@59816
   662
    using mod_div_equality [of a b] by (simp add: ac_simps)
haftmann@59816
   663
  then have "b * (a div b) + a mod b - a mod b = a - a mod b"
haftmann@59816
   664
    by simp
haftmann@59816
   665
  then show ?thesis
haftmann@59816
   666
    by simp
haftmann@59816
   667
qed
haftmann@53067
   668
haftmann@54226
   669
subclass semiring_div_parity
haftmann@54226
   670
proof
haftmann@54226
   671
  fix a
haftmann@54226
   672
  show "a mod 2 = 0 \<or> a mod 2 = 1"
haftmann@54226
   673
  proof (rule ccontr)
haftmann@54226
   674
    assume "\<not> (a mod 2 = 0 \<or> a mod 2 = 1)"
haftmann@54226
   675
    then have "a mod 2 \<noteq> 0" and "a mod 2 \<noteq> 1" by simp_all
haftmann@54226
   676
    have "0 < 2" by simp
haftmann@54226
   677
    with pos_mod_bound pos_mod_sign have "0 \<le> a mod 2" "a mod 2 < 2" by simp_all
haftmann@54226
   678
    with `a mod 2 \<noteq> 0` have "0 < a mod 2" by simp
haftmann@54226
   679
    with discrete have "1 \<le> a mod 2" by simp
haftmann@54226
   680
    with `a mod 2 \<noteq> 1` have "1 < a mod 2" by simp
haftmann@54226
   681
    with discrete have "2 \<le> a mod 2" by simp
haftmann@54226
   682
    with `a mod 2 < 2` show False by simp
haftmann@54226
   683
  qed
haftmann@58646
   684
next
haftmann@58646
   685
  show "1 mod 2 = 1"
haftmann@58646
   686
    by (rule mod_less) simp_all
haftmann@58710
   687
next
haftmann@58710
   688
  show "0 \<noteq> 2"
haftmann@58710
   689
    by simp
haftmann@53067
   690
qed
haftmann@53067
   691
haftmann@53067
   692
lemma divmod_digit_1:
haftmann@53067
   693
  assumes "0 \<le> a" "0 < b" and "b \<le> a mod (2 * b)"
haftmann@53067
   694
  shows "2 * (a div (2 * b)) + 1 = a div b" (is "?P")
haftmann@53067
   695
    and "a mod (2 * b) - b = a mod b" (is "?Q")
haftmann@53067
   696
proof -
haftmann@53067
   697
  from assms mod_less_eq_dividend [of a "2 * b"] have "b \<le> a"
haftmann@53067
   698
    by (auto intro: trans)
haftmann@53067
   699
  with `0 < b` have "0 < a div b" by (auto intro: div_positive)
haftmann@53067
   700
  then have [simp]: "1 \<le> a div b" by (simp add: discrete)
haftmann@53067
   701
  with `0 < b` have mod_less: "a mod b < b" by (simp add: pos_mod_bound)
haftmann@53067
   702
  def w \<equiv> "a div b mod 2" with parity have w_exhaust: "w = 0 \<or> w = 1" by auto
haftmann@53067
   703
  have mod_w: "a mod (2 * b) = a mod b + b * w"
haftmann@53067
   704
    by (simp add: w_def mod_mult2_eq ac_simps)
haftmann@53067
   705
  from assms w_exhaust have "w = 1"
haftmann@53067
   706
    by (auto simp add: mod_w) (insert mod_less, auto)
haftmann@53067
   707
  with mod_w have mod: "a mod (2 * b) = a mod b + b" by simp
haftmann@53067
   708
  have "2 * (a div (2 * b)) = a div b - w"
haftmann@53067
   709
    by (simp add: w_def div_mult2_eq mult_div_cancel ac_simps)
haftmann@53067
   710
  with `w = 1` have div: "2 * (a div (2 * b)) = a div b - 1" by simp
haftmann@53067
   711
  then show ?P and ?Q
haftmann@59816
   712
    by (simp_all add: div mod add_implies_diff [symmetric] le_add_diff_inverse2)
haftmann@53067
   713
qed
haftmann@53067
   714
haftmann@53067
   715
lemma divmod_digit_0:
haftmann@53067
   716
  assumes "0 < b" and "a mod (2 * b) < b"
haftmann@53067
   717
  shows "2 * (a div (2 * b)) = a div b" (is "?P")
haftmann@53067
   718
    and "a mod (2 * b) = a mod b" (is "?Q")
haftmann@53067
   719
proof -
haftmann@53067
   720
  def w \<equiv> "a div b mod 2" with parity have w_exhaust: "w = 0 \<or> w = 1" by auto
haftmann@53067
   721
  have mod_w: "a mod (2 * b) = a mod b + b * w"
haftmann@53067
   722
    by (simp add: w_def mod_mult2_eq ac_simps)
haftmann@53067
   723
  moreover have "b \<le> a mod b + b"
haftmann@53067
   724
  proof -
haftmann@53067
   725
    from `0 < b` pos_mod_sign have "0 \<le> a mod b" by blast
haftmann@53067
   726
    then have "0 + b \<le> a mod b + b" by (rule add_right_mono)
haftmann@53067
   727
    then show ?thesis by simp
haftmann@53067
   728
  qed
haftmann@53067
   729
  moreover note assms w_exhaust
haftmann@53067
   730
  ultimately have "w = 0" by auto
haftmann@53067
   731
  with mod_w have mod: "a mod (2 * b) = a mod b" by simp
haftmann@53067
   732
  have "2 * (a div (2 * b)) = a div b - w"
haftmann@53067
   733
    by (simp add: w_def div_mult2_eq mult_div_cancel ac_simps)
haftmann@53067
   734
  with `w = 0` have div: "2 * (a div (2 * b)) = a div b" by simp
haftmann@53067
   735
  then show ?P and ?Q
haftmann@53067
   736
    by (simp_all add: div mod)
haftmann@53067
   737
qed
haftmann@53067
   738
haftmann@53067
   739
definition divmod :: "num \<Rightarrow> num \<Rightarrow> 'a \<times> 'a"
haftmann@53067
   740
where
haftmann@53067
   741
  "divmod m n = (numeral m div numeral n, numeral m mod numeral n)"
haftmann@53067
   742
haftmann@53067
   743
lemma fst_divmod [simp]:
haftmann@53067
   744
  "fst (divmod m n) = numeral m div numeral n"
haftmann@53067
   745
  by (simp add: divmod_def)
haftmann@53067
   746
haftmann@53067
   747
lemma snd_divmod [simp]:
haftmann@53067
   748
  "snd (divmod m n) = numeral m mod numeral n"
haftmann@53067
   749
  by (simp add: divmod_def)
haftmann@53067
   750
haftmann@53067
   751
definition divmod_step :: "num \<Rightarrow> 'a \<times> 'a \<Rightarrow> 'a \<times> 'a"
haftmann@53067
   752
where
haftmann@53067
   753
  "divmod_step l qr = (let (q, r) = qr
haftmann@53067
   754
    in if r \<ge> numeral l then (2 * q + 1, r - numeral l)
haftmann@53067
   755
    else (2 * q, r))"
haftmann@53067
   756
haftmann@53067
   757
text {*
haftmann@53067
   758
  This is a formulation of one step (referring to one digit position)
haftmann@53067
   759
  in school-method division: compare the dividend at the current
haftmann@53070
   760
  digit position with the remainder from previous division steps
haftmann@53067
   761
  and evaluate accordingly.
haftmann@53067
   762
*}
haftmann@53067
   763
haftmann@53067
   764
lemma divmod_step_eq [code]:
haftmann@53067
   765
  "divmod_step l (q, r) = (if numeral l \<le> r
haftmann@53067
   766
    then (2 * q + 1, r - numeral l) else (2 * q, r))"
haftmann@53067
   767
  by (simp add: divmod_step_def)
haftmann@53067
   768
haftmann@53067
   769
lemma divmod_step_simps [simp]:
haftmann@53067
   770
  "r < numeral l \<Longrightarrow> divmod_step l (q, r) = (2 * q, r)"
haftmann@53067
   771
  "numeral l \<le> r \<Longrightarrow> divmod_step l (q, r) = (2 * q + 1, r - numeral l)"
haftmann@53067
   772
  by (auto simp add: divmod_step_eq not_le)
haftmann@53067
   773
haftmann@53067
   774
text {*
haftmann@53067
   775
  This is a formulation of school-method division.
haftmann@53067
   776
  If the divisor is smaller than the dividend, terminate.
haftmann@53067
   777
  If not, shift the dividend to the right until termination
haftmann@53067
   778
  occurs and then reiterate single division steps in the
haftmann@53067
   779
  opposite direction.
haftmann@53067
   780
*}
haftmann@53067
   781
haftmann@53067
   782
lemma divmod_divmod_step [code]:
haftmann@53067
   783
  "divmod m n = (if m < n then (0, numeral m)
haftmann@53067
   784
    else divmod_step n (divmod m (Num.Bit0 n)))"
haftmann@53067
   785
proof (cases "m < n")
haftmann@53067
   786
  case True then have "numeral m < numeral n" by simp
haftmann@53067
   787
  then show ?thesis
haftmann@53067
   788
    by (simp add: prod_eq_iff div_less mod_less)
haftmann@53067
   789
next
haftmann@53067
   790
  case False
haftmann@53067
   791
  have "divmod m n =
haftmann@53067
   792
    divmod_step n (numeral m div (2 * numeral n),
haftmann@53067
   793
      numeral m mod (2 * numeral n))"
haftmann@53067
   794
  proof (cases "numeral n \<le> numeral m mod (2 * numeral n)")
haftmann@53067
   795
    case True
haftmann@53067
   796
    with divmod_step_simps
haftmann@53067
   797
      have "divmod_step n (numeral m div (2 * numeral n), numeral m mod (2 * numeral n)) =
haftmann@53067
   798
        (2 * (numeral m div (2 * numeral n)) + 1, numeral m mod (2 * numeral n) - numeral n)"
haftmann@53067
   799
        by blast
haftmann@53067
   800
    moreover from True divmod_digit_1 [of "numeral m" "numeral n"]
haftmann@53067
   801
      have "2 * (numeral m div (2 * numeral n)) + 1 = numeral m div numeral n"
haftmann@53067
   802
      and "numeral m mod (2 * numeral n) - numeral n = numeral m mod numeral n"
haftmann@53067
   803
      by simp_all
haftmann@53067
   804
    ultimately show ?thesis by (simp only: divmod_def)
haftmann@53067
   805
  next
haftmann@53067
   806
    case False then have *: "numeral m mod (2 * numeral n) < numeral n"
haftmann@53067
   807
      by (simp add: not_le)
haftmann@53067
   808
    with divmod_step_simps
haftmann@53067
   809
      have "divmod_step n (numeral m div (2 * numeral n), numeral m mod (2 * numeral n)) =
haftmann@53067
   810
        (2 * (numeral m div (2 * numeral n)), numeral m mod (2 * numeral n))"
haftmann@53067
   811
        by blast
haftmann@53067
   812
    moreover from * divmod_digit_0 [of "numeral n" "numeral m"]
haftmann@53067
   813
      have "2 * (numeral m div (2 * numeral n)) = numeral m div numeral n"
haftmann@53067
   814
      and "numeral m mod (2 * numeral n) = numeral m mod numeral n"
haftmann@53067
   815
      by (simp_all only: zero_less_numeral)
haftmann@53067
   816
    ultimately show ?thesis by (simp only: divmod_def)
haftmann@53067
   817
  qed
haftmann@53067
   818
  then have "divmod m n =
haftmann@53067
   819
    divmod_step n (numeral m div numeral (Num.Bit0 n),
haftmann@53067
   820
      numeral m mod numeral (Num.Bit0 n))"
haftmann@53067
   821
    by (simp only: numeral.simps distrib mult_1) 
haftmann@53067
   822
  then have "divmod m n = divmod_step n (divmod m (Num.Bit0 n))"
haftmann@53067
   823
    by (simp add: divmod_def)
haftmann@53067
   824
  with False show ?thesis by simp
haftmann@53067
   825
qed
haftmann@53067
   826
haftmann@58953
   827
lemma divmod_eq [simp]:
haftmann@58953
   828
  "m < n \<Longrightarrow> divmod m n = (0, numeral m)"
haftmann@58953
   829
  "n \<le> m \<Longrightarrow> divmod m n = divmod_step n (divmod m (Num.Bit0 n))"
haftmann@58953
   830
  by (auto simp add: divmod_divmod_step [of m n])
haftmann@58953
   831
haftmann@58953
   832
lemma divmod_cancel [simp, code]:
haftmann@53069
   833
  "divmod (Num.Bit0 m) (Num.Bit0 n) = (case divmod m n of (q, r) \<Rightarrow> (q, 2 * r))" (is ?P)
haftmann@53069
   834
  "divmod (Num.Bit1 m) (Num.Bit0 n) = (case divmod m n of (q, r) \<Rightarrow> (q, 2 * r + 1))" (is ?Q)
haftmann@53069
   835
proof -
haftmann@53069
   836
  have *: "\<And>q. numeral (Num.Bit0 q) = 2 * numeral q"
haftmann@53069
   837
    "\<And>q. numeral (Num.Bit1 q) = 2 * numeral q + 1"
haftmann@53069
   838
    by (simp_all only: numeral_mult numeral.simps distrib) simp_all
haftmann@53069
   839
  have "1 div 2 = 0" "1 mod 2 = 1" by (auto intro: div_less mod_less)
haftmann@53069
   840
  then show ?P and ?Q
haftmann@53069
   841
    by (simp_all add: prod_eq_iff split_def * [of m] * [of n] mod_mult_mult1
haftmann@53069
   842
      div_mult2_eq [of _ _ 2] mod_mult2_eq [of _ _ 2] add.commute del: numeral_times_numeral)
haftmann@58953
   843
qed
haftmann@58953
   844
haftmann@58953
   845
text {* Special case: divisibility *}
haftmann@58953
   846
haftmann@58953
   847
definition divides_aux :: "'a \<times> 'a \<Rightarrow> bool"
haftmann@58953
   848
where
haftmann@58953
   849
  "divides_aux qr \<longleftrightarrow> snd qr = 0"
haftmann@58953
   850
haftmann@58953
   851
lemma divides_aux_eq [simp]:
haftmann@58953
   852
  "divides_aux (q, r) \<longleftrightarrow> r = 0"
haftmann@58953
   853
  by (simp add: divides_aux_def)
haftmann@58953
   854
haftmann@58953
   855
lemma dvd_numeral_simp [simp]:
haftmann@58953
   856
  "numeral m dvd numeral n \<longleftrightarrow> divides_aux (divmod n m)"
haftmann@58953
   857
  by (simp add: divmod_def mod_eq_0_iff_dvd)
haftmann@53069
   858
haftmann@53067
   859
end
haftmann@53067
   860
haftmann@59816
   861
hide_fact (open) le_add_diff_inverse2
haftmann@53067
   862
  -- {* restore simple accesses for more general variants of theorems *}
haftmann@53067
   863
haftmann@53067
   864
  
haftmann@26100
   865
subsection {* Division on @{typ nat} *}
haftmann@26100
   866
haftmann@26100
   867
text {*
haftmann@60429
   868
  We define @{const divide} and @{const mod} on @{typ nat} by means
haftmann@26100
   869
  of a characteristic relation with two input arguments
haftmann@26100
   870
  @{term "m\<Colon>nat"}, @{term "n\<Colon>nat"} and two output arguments
haftmann@26100
   871
  @{term "q\<Colon>nat"}(uotient) and @{term "r\<Colon>nat"}(emainder).
haftmann@26100
   872
*}
haftmann@26100
   873
haftmann@33340
   874
definition divmod_nat_rel :: "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat \<Rightarrow> bool" where
haftmann@33340
   875
  "divmod_nat_rel m n qr \<longleftrightarrow>
haftmann@30923
   876
    m = fst qr * n + snd qr \<and>
haftmann@30923
   877
      (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
   878
haftmann@33340
   879
text {* @{const divmod_nat_rel} is total: *}
haftmann@26100
   880
haftmann@33340
   881
lemma divmod_nat_rel_ex:
haftmann@33340
   882
  obtains q r where "divmod_nat_rel m n (q, r)"
haftmann@26100
   883
proof (cases "n = 0")
haftmann@30923
   884
  case True  with that show thesis
haftmann@33340
   885
    by (auto simp add: divmod_nat_rel_def)
haftmann@26100
   886
next
haftmann@26100
   887
  case False
haftmann@26100
   888
  have "\<exists>q r. m = q * n + r \<and> r < n"
haftmann@26100
   889
  proof (induct m)
haftmann@26100
   890
    case 0 with `n \<noteq> 0`
haftmann@26100
   891
    have "(0\<Colon>nat) = 0 * n + 0 \<and> 0 < n" by simp
haftmann@26100
   892
    then show ?case by blast
haftmann@26100
   893
  next
haftmann@26100
   894
    case (Suc m) then obtain q' r'
haftmann@26100
   895
      where m: "m = q' * n + r'" and n: "r' < n" by auto
haftmann@26100
   896
    then show ?case proof (cases "Suc r' < n")
haftmann@26100
   897
      case True
haftmann@26100
   898
      from m n have "Suc m = q' * n + Suc r'" by simp
haftmann@26100
   899
      with True show ?thesis by blast
haftmann@26100
   900
    next
haftmann@26100
   901
      case False then have "n \<le> Suc r'" by auto
haftmann@26100
   902
      moreover from n have "Suc r' \<le> n" by auto
haftmann@26100
   903
      ultimately have "n = Suc r'" by auto
haftmann@26100
   904
      with m have "Suc m = Suc q' * n + 0" by simp
haftmann@26100
   905
      with `n \<noteq> 0` show ?thesis by blast
haftmann@26100
   906
    qed
haftmann@26100
   907
  qed
haftmann@26100
   908
  with that show thesis
haftmann@33340
   909
    using `n \<noteq> 0` by (auto simp add: divmod_nat_rel_def)
haftmann@26100
   910
qed
haftmann@26100
   911
haftmann@33340
   912
text {* @{const divmod_nat_rel} is injective: *}
haftmann@26100
   913
haftmann@33340
   914
lemma divmod_nat_rel_unique:
haftmann@33340
   915
  assumes "divmod_nat_rel m n qr"
haftmann@33340
   916
    and "divmod_nat_rel m n qr'"
haftmann@30923
   917
  shows "qr = qr'"
haftmann@26100
   918
proof (cases "n = 0")
haftmann@26100
   919
  case True with assms show ?thesis
haftmann@30923
   920
    by (cases qr, cases qr')
haftmann@33340
   921
      (simp add: divmod_nat_rel_def)
haftmann@26100
   922
next
haftmann@26100
   923
  case False
haftmann@26100
   924
  have aux: "\<And>q r q' r'. q' * n + r' = q * n + r \<Longrightarrow> r < n \<Longrightarrow> q' \<le> (q\<Colon>nat)"
haftmann@26100
   925
  apply (rule leI)
haftmann@26100
   926
  apply (subst less_iff_Suc_add)
haftmann@26100
   927
  apply (auto simp add: add_mult_distrib)
haftmann@26100
   928
  done
wenzelm@53374
   929
  from `n \<noteq> 0` assms have *: "fst qr = fst qr'"
haftmann@33340
   930
    by (auto simp add: divmod_nat_rel_def intro: order_antisym dest: aux sym)
wenzelm@53374
   931
  with assms have "snd qr = snd qr'"
haftmann@33340
   932
    by (simp add: divmod_nat_rel_def)
wenzelm@53374
   933
  with * show ?thesis by (cases qr, cases qr') simp
haftmann@26100
   934
qed
haftmann@26100
   935
haftmann@26100
   936
text {*
haftmann@26100
   937
  We instantiate divisibility on the natural numbers by
haftmann@33340
   938
  means of @{const divmod_nat_rel}:
haftmann@26100
   939
*}
haftmann@25942
   940
haftmann@33340
   941
definition divmod_nat :: "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat" where
haftmann@37767
   942
  "divmod_nat m n = (THE qr. divmod_nat_rel m n qr)"
haftmann@30923
   943
haftmann@33340
   944
lemma divmod_nat_rel_divmod_nat:
haftmann@33340
   945
  "divmod_nat_rel m n (divmod_nat m n)"
haftmann@30923
   946
proof -
haftmann@33340
   947
  from divmod_nat_rel_ex
haftmann@33340
   948
    obtain qr where rel: "divmod_nat_rel m n qr" .
haftmann@30923
   949
  then show ?thesis
haftmann@33340
   950
  by (auto simp add: divmod_nat_def intro: theI elim: divmod_nat_rel_unique)
haftmann@30923
   951
qed
haftmann@30923
   952
huffman@47135
   953
lemma divmod_nat_unique:
haftmann@33340
   954
  assumes "divmod_nat_rel m n qr" 
haftmann@33340
   955
  shows "divmod_nat m n = qr"
haftmann@33340
   956
  using assms by (auto intro: divmod_nat_rel_unique divmod_nat_rel_divmod_nat)
haftmann@26100
   957
haftmann@60429
   958
instantiation nat :: semiring_div
haftmann@60352
   959
begin
haftmann@60352
   960
haftmann@60352
   961
definition divide_nat where
haftmann@60429
   962
  div_nat_def: "m div n = fst (divmod_nat m n)"
haftmann@60352
   963
haftmann@60352
   964
definition mod_nat where
haftmann@60352
   965
  "m mod n = snd (divmod_nat m n)"
huffman@46551
   966
huffman@46551
   967
lemma fst_divmod_nat [simp]:
huffman@46551
   968
  "fst (divmod_nat m n) = m div n"
huffman@46551
   969
  by (simp add: div_nat_def)
huffman@46551
   970
huffman@46551
   971
lemma snd_divmod_nat [simp]:
huffman@46551
   972
  "snd (divmod_nat m n) = m mod n"
huffman@46551
   973
  by (simp add: mod_nat_def)
huffman@46551
   974
haftmann@33340
   975
lemma divmod_nat_div_mod:
haftmann@33340
   976
  "divmod_nat m n = (m div n, m mod n)"
huffman@46551
   977
  by (simp add: prod_eq_iff)
haftmann@26100
   978
huffman@47135
   979
lemma div_nat_unique:
haftmann@33340
   980
  assumes "divmod_nat_rel m n (q, r)" 
haftmann@26100
   981
  shows "m div n = q"
huffman@47135
   982
  using assms by (auto dest!: divmod_nat_unique simp add: prod_eq_iff)
huffman@47135
   983
huffman@47135
   984
lemma mod_nat_unique:
haftmann@33340
   985
  assumes "divmod_nat_rel m n (q, r)" 
haftmann@26100
   986
  shows "m mod n = r"
huffman@47135
   987
  using assms by (auto dest!: divmod_nat_unique simp add: prod_eq_iff)
haftmann@25571
   988
haftmann@33340
   989
lemma divmod_nat_rel: "divmod_nat_rel m n (m div n, m mod n)"
huffman@46551
   990
  using divmod_nat_rel_divmod_nat by (simp add: divmod_nat_div_mod)
paulson@14267
   991
huffman@47136
   992
lemma divmod_nat_zero: "divmod_nat m 0 = (0, m)"
huffman@47136
   993
  by (simp add: divmod_nat_unique divmod_nat_rel_def)
huffman@47136
   994
huffman@47136
   995
lemma divmod_nat_zero_left: "divmod_nat 0 n = (0, 0)"
huffman@47136
   996
  by (simp add: divmod_nat_unique divmod_nat_rel_def)
haftmann@25942
   997
huffman@47137
   998
lemma divmod_nat_base: "m < n \<Longrightarrow> divmod_nat m n = (0, m)"
huffman@47137
   999
  by (simp add: divmod_nat_unique divmod_nat_rel_def)
haftmann@25942
  1000
haftmann@33340
  1001
lemma divmod_nat_step:
haftmann@26100
  1002
  assumes "0 < n" and "n \<le> m"
haftmann@33340
  1003
  shows "divmod_nat m n = (Suc ((m - n) div n), (m - n) mod n)"
huffman@47135
  1004
proof (rule divmod_nat_unique)
huffman@47134
  1005
  have "divmod_nat_rel (m - n) n ((m - n) div n, (m - n) mod n)"
huffman@47134
  1006
    by (rule divmod_nat_rel)
huffman@47134
  1007
  thus "divmod_nat_rel m n (Suc ((m - n) div n), (m - n) mod n)"
huffman@47134
  1008
    unfolding divmod_nat_rel_def using assms by auto
haftmann@26100
  1009
qed
haftmann@25942
  1010
haftmann@60429
  1011
text {* The ''recursion'' equations for @{const divide} and @{const mod} *}
haftmann@26100
  1012
haftmann@26100
  1013
lemma div_less [simp]:
haftmann@26100
  1014
  fixes m n :: nat
haftmann@26100
  1015
  assumes "m < n"
haftmann@26100
  1016
  shows "m div n = 0"
huffman@46551
  1017
  using assms divmod_nat_base by (simp add: prod_eq_iff)
haftmann@25942
  1018
haftmann@26100
  1019
lemma le_div_geq:
haftmann@26100
  1020
  fixes m n :: nat
haftmann@26100
  1021
  assumes "0 < n" and "n \<le> m"
haftmann@26100
  1022
  shows "m div n = Suc ((m - n) div n)"
huffman@46551
  1023
  using assms divmod_nat_step by (simp add: prod_eq_iff)
paulson@14267
  1024
haftmann@26100
  1025
lemma mod_less [simp]:
haftmann@26100
  1026
  fixes m n :: nat
haftmann@26100
  1027
  assumes "m < n"
haftmann@26100
  1028
  shows "m mod n = m"
huffman@46551
  1029
  using assms divmod_nat_base by (simp add: prod_eq_iff)
haftmann@26100
  1030
haftmann@26100
  1031
lemma le_mod_geq:
haftmann@26100
  1032
  fixes m n :: nat
haftmann@26100
  1033
  assumes "n \<le> m"
haftmann@26100
  1034
  shows "m mod n = (m - n) mod n"
huffman@46551
  1035
  using assms divmod_nat_step by (cases "n = 0") (simp_all add: prod_eq_iff)
paulson@14267
  1036
huffman@47136
  1037
instance proof
huffman@47136
  1038
  fix m n :: nat
huffman@47136
  1039
  show "m div n * n + m mod n = m"
huffman@47136
  1040
    using divmod_nat_rel [of m n] by (simp add: divmod_nat_rel_def)
huffman@47136
  1041
next
huffman@47136
  1042
  fix m n q :: nat
huffman@47136
  1043
  assume "n \<noteq> 0"
huffman@47136
  1044
  then show "(q + m * n) div n = m + q div n"
huffman@47136
  1045
    by (induct m) (simp_all add: le_div_geq)
huffman@47136
  1046
next
huffman@47136
  1047
  fix m n q :: nat
huffman@47136
  1048
  assume "m \<noteq> 0"
huffman@47136
  1049
  hence "\<And>a b. divmod_nat_rel n q (a, b) \<Longrightarrow> divmod_nat_rel (m * n) (m * q) (a, m * b)"
huffman@47136
  1050
    unfolding divmod_nat_rel_def
huffman@47136
  1051
    by (auto split: split_if_asm, simp_all add: algebra_simps)
huffman@47136
  1052
  moreover from divmod_nat_rel have "divmod_nat_rel n q (n div q, n mod q)" .
huffman@47136
  1053
  ultimately have "divmod_nat_rel (m * n) (m * q) (n div q, m * (n mod q))" .
huffman@47136
  1054
  thus "(m * n) div (m * q) = n div q" by (rule div_nat_unique)
huffman@47136
  1055
next
huffman@47136
  1056
  fix n :: nat show "n div 0 = 0"
haftmann@33340
  1057
    by (simp add: div_nat_def divmod_nat_zero)
huffman@47136
  1058
next
huffman@47136
  1059
  fix n :: nat show "0 div n = 0"
huffman@47136
  1060
    by (simp add: div_nat_def divmod_nat_zero_left)
haftmann@25942
  1061
qed
haftmann@26100
  1062
haftmann@25942
  1063
end
paulson@14267
  1064
haftmann@33361
  1065
lemma divmod_nat_if [code]: "divmod_nat m n = (if n = 0 \<or> m < n then (0, m) else
haftmann@33361
  1066
  let (q, r) = divmod_nat (m - n) n in (Suc q, r))"
blanchet@55414
  1067
  by (simp add: prod_eq_iff case_prod_beta not_less le_div_geq le_mod_geq)
haftmann@33361
  1068
haftmann@60429
  1069
text {* Simproc for cancelling @{const divide} and @{const mod} *}
haftmann@25942
  1070
wenzelm@51299
  1071
ML_file "~~/src/Provers/Arith/cancel_div_mod.ML"
wenzelm@51299
  1072
haftmann@30934
  1073
ML {*
wenzelm@43594
  1074
structure Cancel_Div_Mod_Nat = Cancel_Div_Mod
wenzelm@41550
  1075
(
haftmann@60352
  1076
  val div_name = @{const_name divide};
haftmann@30934
  1077
  val mod_name = @{const_name mod};
haftmann@30934
  1078
  val mk_binop = HOLogic.mk_binop;
huffman@48561
  1079
  val mk_plus = HOLogic.mk_binop @{const_name Groups.plus};
huffman@48561
  1080
  val dest_plus = HOLogic.dest_bin @{const_name Groups.plus} HOLogic.natT;
huffman@48561
  1081
  fun mk_sum [] = HOLogic.zero
huffman@48561
  1082
    | mk_sum [t] = t
huffman@48561
  1083
    | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
huffman@48561
  1084
  fun dest_sum tm =
huffman@48561
  1085
    if HOLogic.is_zero tm then []
huffman@48561
  1086
    else
huffman@48561
  1087
      (case try HOLogic.dest_Suc tm of
huffman@48561
  1088
        SOME t => HOLogic.Suc_zero :: dest_sum t
huffman@48561
  1089
      | NONE =>
huffman@48561
  1090
          (case try dest_plus tm of
huffman@48561
  1091
            SOME (t, u) => dest_sum t @ dest_sum u
huffman@48561
  1092
          | NONE => [tm]));
haftmann@25942
  1093
haftmann@30934
  1094
  val div_mod_eqs = map mk_meta_eq [@{thm div_mod_equality}, @{thm div_mod_equality2}];
paulson@14267
  1095
haftmann@30934
  1096
  val prove_eq_sums = Arith_Data.prove_conv2 all_tac (Arith_Data.simp_all_tac
haftmann@57514
  1097
    (@{thm add_0_left} :: @{thm add_0_right} :: @{thms ac_simps}))
wenzelm@41550
  1098
)
haftmann@25942
  1099
*}
haftmann@25942
  1100
wenzelm@43594
  1101
simproc_setup cancel_div_mod_nat ("(m::nat) + n") = {* K Cancel_Div_Mod_Nat.proc *}
wenzelm@43594
  1102
haftmann@26100
  1103
haftmann@26100
  1104
subsubsection {* Quotient *}
haftmann@26100
  1105
haftmann@26100
  1106
lemma div_geq: "0 < n \<Longrightarrow>  \<not> m < n \<Longrightarrow> m div n = Suc ((m - n) div n)"
nipkow@29667
  1107
by (simp add: le_div_geq linorder_not_less)
haftmann@26100
  1108
haftmann@26100
  1109
lemma div_if: "0 < n \<Longrightarrow> m div n = (if m < n then 0 else Suc ((m - n) div n))"
nipkow@29667
  1110
by (simp add: div_geq)
haftmann@26100
  1111
haftmann@26100
  1112
lemma div_mult_self_is_m [simp]: "0<n ==> (m*n) div n = (m::nat)"
nipkow@29667
  1113
by simp
haftmann@26100
  1114
haftmann@26100
  1115
lemma div_mult_self1_is_m [simp]: "0<n ==> (n*m) div n = (m::nat)"
nipkow@29667
  1116
by simp
haftmann@26100
  1117
haftmann@53066
  1118
lemma div_positive:
haftmann@53066
  1119
  fixes m n :: nat
haftmann@53066
  1120
  assumes "n > 0"
haftmann@53066
  1121
  assumes "m \<ge> n"
haftmann@53066
  1122
  shows "m div n > 0"
haftmann@53066
  1123
proof -
haftmann@53066
  1124
  from `m \<ge> n` obtain q where "m = n + q"
haftmann@53066
  1125
    by (auto simp add: le_iff_add)
haftmann@53066
  1126
  with `n > 0` show ?thesis by simp
haftmann@53066
  1127
qed
haftmann@53066
  1128
hoelzl@59000
  1129
lemma div_eq_0_iff: "(a div b::nat) = 0 \<longleftrightarrow> a < b \<or> b = 0"
hoelzl@59000
  1130
  by (metis div_less div_positive div_by_0 gr0I less_numeral_extra(3) not_less)
haftmann@25942
  1131
haftmann@25942
  1132
subsubsection {* Remainder *}
haftmann@25942
  1133
haftmann@26100
  1134
lemma mod_less_divisor [simp]:
haftmann@26100
  1135
  fixes m n :: nat
haftmann@26100
  1136
  assumes "n > 0"
haftmann@26100
  1137
  shows "m mod n < (n::nat)"
haftmann@33340
  1138
  using assms divmod_nat_rel [of m n] unfolding divmod_nat_rel_def by auto
paulson@14267
  1139
haftmann@51173
  1140
lemma mod_Suc_le_divisor [simp]:
haftmann@51173
  1141
  "m mod Suc n \<le> n"
haftmann@51173
  1142
  using mod_less_divisor [of "Suc n" m] by arith
haftmann@51173
  1143
haftmann@26100
  1144
lemma mod_less_eq_dividend [simp]:
haftmann@26100
  1145
  fixes m n :: nat
haftmann@26100
  1146
  shows "m mod n \<le> m"
haftmann@26100
  1147
proof (rule add_leD2)
haftmann@26100
  1148
  from mod_div_equality have "m div n * n + m mod n = m" .
haftmann@26100
  1149
  then show "m div n * n + m mod n \<le> m" by auto
haftmann@26100
  1150
qed
haftmann@26100
  1151
haftmann@26100
  1152
lemma mod_geq: "\<not> m < (n\<Colon>nat) \<Longrightarrow> m mod n = (m - n) mod n"
nipkow@29667
  1153
by (simp add: le_mod_geq linorder_not_less)
paulson@14267
  1154
haftmann@26100
  1155
lemma mod_if: "m mod (n\<Colon>nat) = (if m < n then m else (m - n) mod n)"
nipkow@29667
  1156
by (simp add: le_mod_geq)
haftmann@26100
  1157
paulson@14267
  1158
lemma mod_1 [simp]: "m mod Suc 0 = 0"
nipkow@29667
  1159
by (induct m) (simp_all add: mod_geq)
paulson@14267
  1160
paulson@14267
  1161
(* a simple rearrangement of mod_div_equality: *)
paulson@14267
  1162
lemma mult_div_cancel: "(n::nat) * (m div n) = m - (m mod n)"
huffman@47138
  1163
  using mod_div_equality2 [of n m] by arith
paulson@14267
  1164
nipkow@15439
  1165
lemma mod_le_divisor[simp]: "0 < n \<Longrightarrow> m mod n \<le> (n::nat)"
wenzelm@22718
  1166
  apply (drule mod_less_divisor [where m = m])
wenzelm@22718
  1167
  apply simp
wenzelm@22718
  1168
  done
paulson@14267
  1169
haftmann@26100
  1170
subsubsection {* Quotient and Remainder *}
paulson@14267
  1171
haftmann@33340
  1172
lemma divmod_nat_rel_mult1_eq:
bulwahn@46552
  1173
  "divmod_nat_rel b c (q, r)
haftmann@33340
  1174
   \<Longrightarrow> divmod_nat_rel (a * b) c (a * q + a * r div c, a * r mod c)"
haftmann@33340
  1175
by (auto simp add: split_ifs divmod_nat_rel_def algebra_simps)
paulson@14267
  1176
haftmann@30923
  1177
lemma div_mult1_eq:
haftmann@30923
  1178
  "(a * b) div c = a * (b div c) + a * (b mod c) div (c::nat)"
huffman@47135
  1179
by (blast intro: divmod_nat_rel_mult1_eq [THEN div_nat_unique] divmod_nat_rel)
paulson@14267
  1180
haftmann@33340
  1181
lemma divmod_nat_rel_add1_eq:
bulwahn@46552
  1182
  "divmod_nat_rel a c (aq, ar) \<Longrightarrow> divmod_nat_rel b c (bq, br)
haftmann@33340
  1183
   \<Longrightarrow> divmod_nat_rel (a + b) c (aq + bq + (ar + br) div c, (ar + br) mod c)"
haftmann@33340
  1184
by (auto simp add: split_ifs divmod_nat_rel_def algebra_simps)
paulson@14267
  1185
paulson@14267
  1186
(*NOT suitable for rewriting: the RHS has an instance of the LHS*)
paulson@14267
  1187
lemma div_add1_eq:
nipkow@25134
  1188
  "(a+b) div (c::nat) = a div c + b div c + ((a mod c + b mod c) div c)"
huffman@47135
  1189
by (blast intro: divmod_nat_rel_add1_eq [THEN div_nat_unique] divmod_nat_rel)
paulson@14267
  1190
haftmann@33340
  1191
lemma divmod_nat_rel_mult2_eq:
haftmann@60352
  1192
  assumes "divmod_nat_rel a b (q, r)"
haftmann@60352
  1193
  shows "divmod_nat_rel a (b * c) (q div c, b *(q mod c) + r)"
haftmann@60352
  1194
proof -
haftmann@60352
  1195
  { assume "r < b" and "0 < c" 
haftmann@60352
  1196
    then have "b * (q mod c) + r < b * c"
haftmann@60352
  1197
      apply (cut_tac m = q and n = c in mod_less_divisor)
haftmann@60352
  1198
      apply (drule_tac [2] m = "q mod c" in less_imp_Suc_add, auto)
haftmann@60352
  1199
      apply (erule_tac P = "%x. lhs < rhs x" for lhs rhs in ssubst)
haftmann@60352
  1200
      apply (simp add: add_mult_distrib2)
haftmann@60352
  1201
      done
haftmann@60352
  1202
    then have "r + b * (q mod c) < b * c"
haftmann@60352
  1203
      by (simp add: ac_simps)
haftmann@60352
  1204
  } with assms show ?thesis
haftmann@60352
  1205
    by (auto simp add: divmod_nat_rel_def algebra_simps add_mult_distrib2 [symmetric])
haftmann@60352
  1206
qed
haftmann@60352
  1207
    
blanchet@55085
  1208
lemma div_mult2_eq: "a div (b * c) = (a div b) div (c::nat)"
huffman@47135
  1209
by (force simp add: divmod_nat_rel [THEN divmod_nat_rel_mult2_eq, THEN div_nat_unique])
paulson@14267
  1210
blanchet@55085
  1211
lemma mod_mult2_eq: "a mod (b * c) = b * (a div b mod c) + a mod (b::nat)"
haftmann@57512
  1212
by (auto simp add: mult.commute divmod_nat_rel [THEN divmod_nat_rel_mult2_eq, THEN mod_nat_unique])
paulson@14267
  1213
haftmann@58786
  1214
instance nat :: semiring_numeral_div
haftmann@58786
  1215
  by intro_classes (auto intro: div_positive simp add: mult_div_cancel mod_mult2_eq div_mult2_eq)
haftmann@58786
  1216
paulson@14267
  1217
huffman@46551
  1218
subsubsection {* Further Facts about Quotient and Remainder *}
paulson@14267
  1219
haftmann@58786
  1220
lemma div_1 [simp]:
haftmann@58786
  1221
  "m div Suc 0 = m"
haftmann@58786
  1222
  using div_by_1 [of m] by simp
paulson@14267
  1223
paulson@14267
  1224
(* Monotonicity of div in first argument *)
haftmann@30923
  1225
lemma div_le_mono [rule_format (no_asm)]:
wenzelm@22718
  1226
    "\<forall>m::nat. m \<le> n --> (m div k) \<le> (n div k)"
paulson@14267
  1227
apply (case_tac "k=0", simp)
paulson@15251
  1228
apply (induct "n" rule: nat_less_induct, clarify)
paulson@14267
  1229
apply (case_tac "n<k")
paulson@14267
  1230
(* 1  case n<k *)
paulson@14267
  1231
apply simp
paulson@14267
  1232
(* 2  case n >= k *)
paulson@14267
  1233
apply (case_tac "m<k")
paulson@14267
  1234
(* 2.1  case m<k *)
paulson@14267
  1235
apply simp
paulson@14267
  1236
(* 2.2  case m>=k *)
nipkow@15439
  1237
apply (simp add: div_geq diff_le_mono)
paulson@14267
  1238
done
paulson@14267
  1239
paulson@14267
  1240
(* Antimonotonicity of div in second argument *)
paulson@14267
  1241
lemma div_le_mono2: "!!m::nat. [| 0<m; m\<le>n |] ==> (k div n) \<le> (k div m)"
paulson@14267
  1242
apply (subgoal_tac "0<n")
wenzelm@22718
  1243
 prefer 2 apply simp
paulson@15251
  1244
apply (induct_tac k rule: nat_less_induct)
paulson@14267
  1245
apply (rename_tac "k")
paulson@14267
  1246
apply (case_tac "k<n", simp)
paulson@14267
  1247
apply (subgoal_tac "~ (k<m) ")
wenzelm@22718
  1248
 prefer 2 apply simp
paulson@14267
  1249
apply (simp add: div_geq)
paulson@15251
  1250
apply (subgoal_tac "(k-n) div n \<le> (k-m) div n")
paulson@14267
  1251
 prefer 2
paulson@14267
  1252
 apply (blast intro: div_le_mono diff_le_mono2)
paulson@14267
  1253
apply (rule le_trans, simp)
nipkow@15439
  1254
apply (simp)
paulson@14267
  1255
done
paulson@14267
  1256
paulson@14267
  1257
lemma div_le_dividend [simp]: "m div n \<le> (m::nat)"
paulson@14267
  1258
apply (case_tac "n=0", simp)
paulson@14267
  1259
apply (subgoal_tac "m div n \<le> m div 1", simp)
paulson@14267
  1260
apply (rule div_le_mono2)
paulson@14267
  1261
apply (simp_all (no_asm_simp))
paulson@14267
  1262
done
paulson@14267
  1263
wenzelm@22718
  1264
(* Similar for "less than" *)
huffman@47138
  1265
lemma div_less_dividend [simp]:
huffman@47138
  1266
  "\<lbrakk>(1::nat) < n; 0 < m\<rbrakk> \<Longrightarrow> m div n < m"
huffman@47138
  1267
apply (induct m rule: nat_less_induct)
paulson@14267
  1268
apply (rename_tac "m")
paulson@14267
  1269
apply (case_tac "m<n", simp)
paulson@14267
  1270
apply (subgoal_tac "0<n")
wenzelm@22718
  1271
 prefer 2 apply simp
paulson@14267
  1272
apply (simp add: div_geq)
paulson@14267
  1273
apply (case_tac "n<m")
paulson@15251
  1274
 apply (subgoal_tac "(m-n) div n < (m-n) ")
paulson@14267
  1275
  apply (rule impI less_trans_Suc)+
paulson@14267
  1276
apply assumption
nipkow@15439
  1277
  apply (simp_all)
paulson@14267
  1278
done
paulson@14267
  1279
paulson@14267
  1280
text{*A fact for the mutilated chess board*}
paulson@14267
  1281
lemma mod_Suc: "Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))"
paulson@14267
  1282
apply (case_tac "n=0", simp)
paulson@15251
  1283
apply (induct "m" rule: nat_less_induct)
paulson@14267
  1284
apply (case_tac "Suc (na) <n")
paulson@14267
  1285
(* case Suc(na) < n *)
paulson@14267
  1286
apply (frule lessI [THEN less_trans], simp add: less_not_refl3)
paulson@14267
  1287
(* case n \<le> Suc(na) *)
paulson@16796
  1288
apply (simp add: linorder_not_less le_Suc_eq mod_geq)
nipkow@15439
  1289
apply (auto simp add: Suc_diff_le le_mod_geq)
paulson@14267
  1290
done
paulson@14267
  1291
paulson@14267
  1292
lemma mod_eq_0_iff: "(m mod d = 0) = (\<exists>q::nat. m = d*q)"
nipkow@29667
  1293
by (auto simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)
paulson@17084
  1294
wenzelm@22718
  1295
lemmas mod_eq_0D [dest!] = mod_eq_0_iff [THEN iffD1]
paulson@14267
  1296
paulson@14267
  1297
(*Loses information, namely we also have r<d provided d is nonzero*)
haftmann@57514
  1298
lemma mod_eqD:
haftmann@57514
  1299
  fixes m d r q :: nat
haftmann@57514
  1300
  assumes "m mod d = r"
haftmann@57514
  1301
  shows "\<exists>q. m = r + q * d"
haftmann@57514
  1302
proof -
haftmann@57514
  1303
  from mod_div_equality obtain q where "q * d + m mod d = m" by blast
haftmann@57514
  1304
  with assms have "m = r + q * d" by simp
haftmann@57514
  1305
  then show ?thesis ..
haftmann@57514
  1306
qed
paulson@14267
  1307
nipkow@13152
  1308
lemma split_div:
nipkow@13189
  1309
 "P(n div k :: nat) =
nipkow@13189
  1310
 ((k = 0 \<longrightarrow> P 0) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P i)))"
nipkow@13189
  1311
 (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
nipkow@13189
  1312
proof
nipkow@13189
  1313
  assume P: ?P
nipkow@13189
  1314
  show ?Q
nipkow@13189
  1315
  proof (cases)
nipkow@13189
  1316
    assume "k = 0"
haftmann@27651
  1317
    with P show ?Q by simp
nipkow@13189
  1318
  next
nipkow@13189
  1319
    assume not0: "k \<noteq> 0"
nipkow@13189
  1320
    thus ?Q
nipkow@13189
  1321
    proof (simp, intro allI impI)
nipkow@13189
  1322
      fix i j
nipkow@13189
  1323
      assume n: "n = k*i + j" and j: "j < k"
nipkow@13189
  1324
      show "P i"
nipkow@13189
  1325
      proof (cases)
wenzelm@22718
  1326
        assume "i = 0"
wenzelm@22718
  1327
        with n j P show "P i" by simp
nipkow@13189
  1328
      next
wenzelm@22718
  1329
        assume "i \<noteq> 0"
haftmann@57514
  1330
        with not0 n j P show "P i" by(simp add:ac_simps)
nipkow@13189
  1331
      qed
nipkow@13189
  1332
    qed
nipkow@13189
  1333
  qed
nipkow@13189
  1334
next
nipkow@13189
  1335
  assume Q: ?Q
nipkow@13189
  1336
  show ?P
nipkow@13189
  1337
  proof (cases)
nipkow@13189
  1338
    assume "k = 0"
haftmann@27651
  1339
    with Q show ?P by simp
nipkow@13189
  1340
  next
nipkow@13189
  1341
    assume not0: "k \<noteq> 0"
nipkow@13189
  1342
    with Q have R: ?R by simp
nipkow@13189
  1343
    from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
nipkow@13517
  1344
    show ?P by simp
nipkow@13189
  1345
  qed
nipkow@13189
  1346
qed
nipkow@13189
  1347
berghofe@13882
  1348
lemma split_div_lemma:
haftmann@26100
  1349
  assumes "0 < n"
haftmann@26100
  1350
  shows "n * q \<le> m \<and> m < n * Suc q \<longleftrightarrow> q = ((m\<Colon>nat) div n)" (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@26100
  1351
proof
haftmann@26100
  1352
  assume ?rhs
haftmann@26100
  1353
  with mult_div_cancel have nq: "n * q = m - (m mod n)" by simp
haftmann@26100
  1354
  then have A: "n * q \<le> m" by simp
haftmann@26100
  1355
  have "n - (m mod n) > 0" using mod_less_divisor assms by auto
haftmann@26100
  1356
  then have "m < m + (n - (m mod n))" by simp
haftmann@26100
  1357
  then have "m < n + (m - (m mod n))" by simp
haftmann@26100
  1358
  with nq have "m < n + n * q" by simp
haftmann@26100
  1359
  then have B: "m < n * Suc q" by simp
haftmann@26100
  1360
  from A B show ?lhs ..
haftmann@26100
  1361
next
haftmann@26100
  1362
  assume P: ?lhs
haftmann@33340
  1363
  then have "divmod_nat_rel m n (q, m - n * q)"
haftmann@57514
  1364
    unfolding divmod_nat_rel_def by (auto simp add: ac_simps)
haftmann@33340
  1365
  with divmod_nat_rel_unique divmod_nat_rel [of m n]
haftmann@30923
  1366
  have "(q, m - n * q) = (m div n, m mod n)" by auto
haftmann@30923
  1367
  then show ?rhs by simp
haftmann@26100
  1368
qed
berghofe@13882
  1369
berghofe@13882
  1370
theorem split_div':
berghofe@13882
  1371
  "P ((m::nat) div n) = ((n = 0 \<and> P 0) \<or>
paulson@14267
  1372
   (\<exists>q. (n * q \<le> m \<and> m < n * (Suc q)) \<and> P q))"
berghofe@13882
  1373
  apply (case_tac "0 < n")
berghofe@13882
  1374
  apply (simp only: add: split_div_lemma)
haftmann@27651
  1375
  apply simp_all
berghofe@13882
  1376
  done
berghofe@13882
  1377
nipkow@13189
  1378
lemma split_mod:
nipkow@13189
  1379
 "P(n mod k :: nat) =
nipkow@13189
  1380
 ((k = 0 \<longrightarrow> P n) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P j)))"
nipkow@13189
  1381
 (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
nipkow@13189
  1382
proof
nipkow@13189
  1383
  assume P: ?P
nipkow@13189
  1384
  show ?Q
nipkow@13189
  1385
  proof (cases)
nipkow@13189
  1386
    assume "k = 0"
haftmann@27651
  1387
    with P show ?Q by simp
nipkow@13189
  1388
  next
nipkow@13189
  1389
    assume not0: "k \<noteq> 0"
nipkow@13189
  1390
    thus ?Q
nipkow@13189
  1391
    proof (simp, intro allI impI)
nipkow@13189
  1392
      fix i j
nipkow@13189
  1393
      assume "n = k*i + j" "j < k"
haftmann@58786
  1394
      thus "P j" using not0 P by (simp add: ac_simps)
nipkow@13189
  1395
    qed
nipkow@13189
  1396
  qed
nipkow@13189
  1397
next
nipkow@13189
  1398
  assume Q: ?Q
nipkow@13189
  1399
  show ?P
nipkow@13189
  1400
  proof (cases)
nipkow@13189
  1401
    assume "k = 0"
haftmann@27651
  1402
    with Q show ?P by simp
nipkow@13189
  1403
  next
nipkow@13189
  1404
    assume not0: "k \<noteq> 0"
nipkow@13189
  1405
    with Q have R: ?R by simp
nipkow@13189
  1406
    from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
nipkow@13517
  1407
    show ?P by simp
nipkow@13189
  1408
  qed
nipkow@13189
  1409
qed
nipkow@13189
  1410
berghofe@13882
  1411
theorem mod_div_equality': "(m::nat) mod n = m - (m div n) * n"
huffman@47138
  1412
  using mod_div_equality [of m n] by arith
huffman@47138
  1413
huffman@47138
  1414
lemma div_mod_equality': "(m::nat) div n * n = m - m mod n"
huffman@47138
  1415
  using mod_div_equality [of m n] by arith
huffman@47138
  1416
(* FIXME: very similar to mult_div_cancel *)
haftmann@22800
  1417
noschinl@52398
  1418
lemma div_eq_dividend_iff: "a \<noteq> 0 \<Longrightarrow> (a :: nat) div b = a \<longleftrightarrow> b = 1"
noschinl@52398
  1419
  apply rule
noschinl@52398
  1420
  apply (cases "b = 0")
noschinl@52398
  1421
  apply simp_all
noschinl@52398
  1422
  apply (metis (full_types) One_nat_def Suc_lessI div_less_dividend less_not_refl3)
noschinl@52398
  1423
  done
noschinl@52398
  1424
haftmann@22800
  1425
huffman@46551
  1426
subsubsection {* An ``induction'' law for modulus arithmetic. *}
paulson@14640
  1427
paulson@14640
  1428
lemma mod_induct_0:
paulson@14640
  1429
  assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
paulson@14640
  1430
  and base: "P i" and i: "i<p"
paulson@14640
  1431
  shows "P 0"
paulson@14640
  1432
proof (rule ccontr)
paulson@14640
  1433
  assume contra: "\<not>(P 0)"
paulson@14640
  1434
  from i have p: "0<p" by simp
paulson@14640
  1435
  have "\<forall>k. 0<k \<longrightarrow> \<not> P (p-k)" (is "\<forall>k. ?A k")
paulson@14640
  1436
  proof
paulson@14640
  1437
    fix k
paulson@14640
  1438
    show "?A k"
paulson@14640
  1439
    proof (induct k)
paulson@14640
  1440
      show "?A 0" by simp  -- "by contradiction"
paulson@14640
  1441
    next
paulson@14640
  1442
      fix n
paulson@14640
  1443
      assume ih: "?A n"
paulson@14640
  1444
      show "?A (Suc n)"
paulson@14640
  1445
      proof (clarsimp)
wenzelm@22718
  1446
        assume y: "P (p - Suc n)"
wenzelm@22718
  1447
        have n: "Suc n < p"
wenzelm@22718
  1448
        proof (rule ccontr)
wenzelm@22718
  1449
          assume "\<not>(Suc n < p)"
wenzelm@22718
  1450
          hence "p - Suc n = 0"
wenzelm@22718
  1451
            by simp
wenzelm@22718
  1452
          with y contra show "False"
wenzelm@22718
  1453
            by simp
wenzelm@22718
  1454
        qed
wenzelm@22718
  1455
        hence n2: "Suc (p - Suc n) = p-n" by arith
wenzelm@22718
  1456
        from p have "p - Suc n < p" by arith
wenzelm@22718
  1457
        with y step have z: "P ((Suc (p - Suc n)) mod p)"
wenzelm@22718
  1458
          by blast
wenzelm@22718
  1459
        show "False"
wenzelm@22718
  1460
        proof (cases "n=0")
wenzelm@22718
  1461
          case True
wenzelm@22718
  1462
          with z n2 contra show ?thesis by simp
wenzelm@22718
  1463
        next
wenzelm@22718
  1464
          case False
wenzelm@22718
  1465
          with p have "p-n < p" by arith
wenzelm@22718
  1466
          with z n2 False ih show ?thesis by simp
wenzelm@22718
  1467
        qed
paulson@14640
  1468
      qed
paulson@14640
  1469
    qed
paulson@14640
  1470
  qed
paulson@14640
  1471
  moreover
paulson@14640
  1472
  from i obtain k where "0<k \<and> i+k=p"
paulson@14640
  1473
    by (blast dest: less_imp_add_positive)
paulson@14640
  1474
  hence "0<k \<and> i=p-k" by auto
paulson@14640
  1475
  moreover
paulson@14640
  1476
  note base
paulson@14640
  1477
  ultimately
paulson@14640
  1478
  show "False" by blast
paulson@14640
  1479
qed
paulson@14640
  1480
paulson@14640
  1481
lemma mod_induct:
paulson@14640
  1482
  assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
paulson@14640
  1483
  and base: "P i" and i: "i<p" and j: "j<p"
paulson@14640
  1484
  shows "P j"
paulson@14640
  1485
proof -
paulson@14640
  1486
  have "\<forall>j<p. P j"
paulson@14640
  1487
  proof
paulson@14640
  1488
    fix j
paulson@14640
  1489
    show "j<p \<longrightarrow> P j" (is "?A j")
paulson@14640
  1490
    proof (induct j)
paulson@14640
  1491
      from step base i show "?A 0"
wenzelm@22718
  1492
        by (auto elim: mod_induct_0)
paulson@14640
  1493
    next
paulson@14640
  1494
      fix k
paulson@14640
  1495
      assume ih: "?A k"
paulson@14640
  1496
      show "?A (Suc k)"
paulson@14640
  1497
      proof
wenzelm@22718
  1498
        assume suc: "Suc k < p"
wenzelm@22718
  1499
        hence k: "k<p" by simp
wenzelm@22718
  1500
        with ih have "P k" ..
wenzelm@22718
  1501
        with step k have "P (Suc k mod p)"
wenzelm@22718
  1502
          by blast
wenzelm@22718
  1503
        moreover
wenzelm@22718
  1504
        from suc have "Suc k mod p = Suc k"
wenzelm@22718
  1505
          by simp
wenzelm@22718
  1506
        ultimately
wenzelm@22718
  1507
        show "P (Suc k)" by simp
paulson@14640
  1508
      qed
paulson@14640
  1509
    qed
paulson@14640
  1510
  qed
paulson@14640
  1511
  with j show ?thesis by blast
paulson@14640
  1512
qed
paulson@14640
  1513
haftmann@33296
  1514
lemma div2_Suc_Suc [simp]: "Suc (Suc m) div 2 = Suc (m div 2)"
huffman@47138
  1515
  by (simp add: numeral_2_eq_2 le_div_geq)
huffman@47138
  1516
huffman@47138
  1517
lemma mod2_Suc_Suc [simp]: "Suc (Suc m) mod 2 = m mod 2"
huffman@47138
  1518
  by (simp add: numeral_2_eq_2 le_mod_geq)
haftmann@33296
  1519
haftmann@33296
  1520
lemma add_self_div_2 [simp]: "(m + m) div 2 = (m::nat)"
huffman@47217
  1521
by (simp add: mult_2 [symmetric])
haftmann@33296
  1522
haftmann@33296
  1523
lemma mod2_gr_0 [simp]: "0 < (m\<Colon>nat) mod 2 \<longleftrightarrow> m mod 2 = 1"
haftmann@33296
  1524
proof -
boehmes@35815
  1525
  { fix n :: nat have  "(n::nat) < 2 \<Longrightarrow> n = 0 \<or> n = 1" by (cases n) simp_all }
haftmann@33296
  1526
  moreover have "m mod 2 < 2" by simp
haftmann@33296
  1527
  ultimately have "m mod 2 = 0 \<or> m mod 2 = 1" .
haftmann@33296
  1528
  then show ?thesis by auto
haftmann@33296
  1529
qed
haftmann@33296
  1530
haftmann@33296
  1531
text{*These lemmas collapse some needless occurrences of Suc:
haftmann@33296
  1532
    at least three Sucs, since two and fewer are rewritten back to Suc again!
haftmann@33296
  1533
    We already have some rules to simplify operands smaller than 3.*}
haftmann@33296
  1534
haftmann@33296
  1535
lemma div_Suc_eq_div_add3 [simp]: "m div (Suc (Suc (Suc n))) = m div (3+n)"
haftmann@33296
  1536
by (simp add: Suc3_eq_add_3)
haftmann@33296
  1537
haftmann@33296
  1538
lemma mod_Suc_eq_mod_add3 [simp]: "m mod (Suc (Suc (Suc n))) = m mod (3+n)"
haftmann@33296
  1539
by (simp add: Suc3_eq_add_3)
haftmann@33296
  1540
haftmann@33296
  1541
lemma Suc_div_eq_add3_div: "(Suc (Suc (Suc m))) div n = (3+m) div n"
haftmann@33296
  1542
by (simp add: Suc3_eq_add_3)
haftmann@33296
  1543
haftmann@33296
  1544
lemma Suc_mod_eq_add3_mod: "(Suc (Suc (Suc m))) mod n = (3+m) mod n"
haftmann@33296
  1545
by (simp add: Suc3_eq_add_3)
haftmann@33296
  1546
huffman@47108
  1547
lemmas Suc_div_eq_add3_div_numeral [simp] = Suc_div_eq_add3_div [of _ "numeral v"] for v
huffman@47108
  1548
lemmas Suc_mod_eq_add3_mod_numeral [simp] = Suc_mod_eq_add3_mod [of _ "numeral v"] for v
haftmann@33296
  1549
haftmann@33361
  1550
lemma Suc_times_mod_eq: "1<k ==> Suc (k * m) mod k = 1" 
haftmann@33361
  1551
apply (induct "m")
haftmann@33361
  1552
apply (simp_all add: mod_Suc)
haftmann@33361
  1553
done
haftmann@33361
  1554
huffman@47108
  1555
declare Suc_times_mod_eq [of "numeral w", simp] for w
haftmann@33361
  1556
huffman@47138
  1557
lemma Suc_div_le_mono [simp]: "n div k \<le> (Suc n) div k"
huffman@47138
  1558
by (simp add: div_le_mono)
haftmann@33361
  1559
haftmann@33361
  1560
lemma Suc_n_div_2_gt_zero [simp]: "(0::nat) < n ==> 0 < (n + 1) div 2"
haftmann@33361
  1561
by (cases n) simp_all
haftmann@33361
  1562
boehmes@35815
  1563
lemma div_2_gt_zero [simp]: assumes A: "(1::nat) < n" shows "0 < n div 2"
boehmes@35815
  1564
proof -
boehmes@35815
  1565
  from A have B: "0 < n - 1" and C: "n - 1 + 1 = n" by simp_all
boehmes@35815
  1566
  from Suc_n_div_2_gt_zero [OF B] C show ?thesis by simp 
boehmes@35815
  1567
qed
haftmann@33361
  1568
haftmann@33361
  1569
lemma mod_mult_self4 [simp]: "Suc (k*n + m) mod n = Suc m mod n"
haftmann@33361
  1570
proof -
haftmann@33361
  1571
  have "Suc (k * n + m) mod n = (k * n + Suc m) mod n" by simp
haftmann@33361
  1572
  also have "... = Suc m mod n" by (rule mod_mult_self3) 
haftmann@33361
  1573
  finally show ?thesis .
haftmann@33361
  1574
qed
haftmann@33361
  1575
haftmann@33361
  1576
lemma mod_Suc_eq_Suc_mod: "Suc m mod n = Suc (m mod n) mod n"
haftmann@33361
  1577
apply (subst mod_Suc [of m]) 
haftmann@33361
  1578
apply (subst mod_Suc [of "m mod n"], simp) 
haftmann@33361
  1579
done
haftmann@33361
  1580
huffman@47108
  1581
lemma mod_2_not_eq_zero_eq_one_nat:
huffman@47108
  1582
  fixes n :: nat
huffman@47108
  1583
  shows "n mod 2 \<noteq> 0 \<longleftrightarrow> n mod 2 = 1"
haftmann@58786
  1584
  by (fact not_mod_2_eq_0_eq_1)
haftmann@58786
  1585
  
haftmann@58778
  1586
lemma even_Suc_div_two [simp]:
haftmann@58778
  1587
  "even n \<Longrightarrow> Suc n div 2 = n div 2"
haftmann@58778
  1588
  using even_succ_div_two [of n] by simp
haftmann@58778
  1589
  
haftmann@58778
  1590
lemma odd_Suc_div_two [simp]:
haftmann@58778
  1591
  "odd n \<Longrightarrow> Suc n div 2 = Suc (n div 2)"
haftmann@58778
  1592
  using odd_succ_div_two [of n] by simp
haftmann@58778
  1593
haftmann@58834
  1594
lemma odd_two_times_div_two_nat [simp]:
haftmann@60352
  1595
  assumes "odd n"
haftmann@60352
  1596
  shows "2 * (n div 2) = n - (1 :: nat)"
haftmann@60352
  1597
proof -
haftmann@60352
  1598
  from assms have "2 * (n div 2) + 1 = n"
haftmann@60352
  1599
    by (rule odd_two_times_div_two_succ)
haftmann@60352
  1600
  then have "Suc (2 * (n div 2)) - 1 = n - 1"
haftmann@60352
  1601
    by simp
haftmann@60352
  1602
  then show ?thesis
haftmann@60352
  1603
    by simp
haftmann@60352
  1604
qed
haftmann@58778
  1605
haftmann@58834
  1606
lemma odd_Suc_minus_one [simp]:
haftmann@58834
  1607
  "odd n \<Longrightarrow> Suc (n - Suc 0) = n"
haftmann@58834
  1608
  by (auto elim: oddE)
haftmann@58834
  1609
haftmann@58778
  1610
lemma parity_induct [case_names zero even odd]:
haftmann@58778
  1611
  assumes zero: "P 0"
haftmann@58778
  1612
  assumes even: "\<And>n. P n \<Longrightarrow> P (2 * n)"
haftmann@58778
  1613
  assumes odd: "\<And>n. P n \<Longrightarrow> P (Suc (2 * n))"
haftmann@58778
  1614
  shows "P n"
haftmann@58778
  1615
proof (induct n rule: less_induct)
haftmann@58778
  1616
  case (less n)
haftmann@58778
  1617
  show "P n"
haftmann@58778
  1618
  proof (cases "n = 0")
haftmann@58778
  1619
    case True with zero show ?thesis by simp
haftmann@58778
  1620
  next
haftmann@58778
  1621
    case False
haftmann@58778
  1622
    with less have hyp: "P (n div 2)" by simp
haftmann@58778
  1623
    show ?thesis
haftmann@58778
  1624
    proof (cases "even n")
haftmann@58778
  1625
      case True
haftmann@58778
  1626
      with hyp even [of "n div 2"] show ?thesis
haftmann@58834
  1627
        by simp
haftmann@58778
  1628
    next
haftmann@58778
  1629
      case False
haftmann@58778
  1630
      with hyp odd [of "n div 2"] show ?thesis 
haftmann@58834
  1631
        by simp
haftmann@58778
  1632
    qed
haftmann@58778
  1633
  qed
haftmann@58778
  1634
qed
haftmann@58778
  1635
haftmann@33361
  1636
haftmann@33361
  1637
subsection {* Division on @{typ int} *}
haftmann@33361
  1638
haftmann@33361
  1639
definition divmod_int_rel :: "int \<Rightarrow> int \<Rightarrow> int \<times> int \<Rightarrow> bool" where
haftmann@33361
  1640
    --{*definition of quotient and remainder*}
huffman@47139
  1641
  "divmod_int_rel a b = (\<lambda>(q, r). a = b * q + r \<and>
huffman@47139
  1642
    (if 0 < b then 0 \<le> r \<and> r < b else if b < 0 then b < r \<and> r \<le> 0 else q = 0))"
haftmann@33361
  1643
haftmann@53067
  1644
text {*
haftmann@53067
  1645
  The following algorithmic devlopment actually echos what has already
haftmann@53067
  1646
  been developed in class @{class semiring_numeral_div}.  In the long
haftmann@53067
  1647
  run it seems better to derive division on @{typ int} just from
haftmann@53067
  1648
  division on @{typ nat} and instantiate @{class semiring_numeral_div}
haftmann@53067
  1649
  accordingly.
haftmann@53067
  1650
*}
haftmann@53067
  1651
haftmann@33361
  1652
definition adjust :: "int \<Rightarrow> int \<times> int \<Rightarrow> int \<times> int" where
haftmann@33361
  1653
    --{*for the division algorithm*}
huffman@47108
  1654
    "adjust b = (\<lambda>(q, r). if 0 \<le> r - b then (2 * q + 1, r - b)
haftmann@33361
  1655
                         else (2 * q, r))"
haftmann@33361
  1656
haftmann@33361
  1657
text{*algorithm for the case @{text "a\<ge>0, b>0"}*}
haftmann@33361
  1658
function posDivAlg :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
haftmann@33361
  1659
  "posDivAlg a b = (if a < b \<or>  b \<le> 0 then (0, a)
haftmann@33361
  1660
     else adjust b (posDivAlg a (2 * b)))"
haftmann@33361
  1661
by auto
haftmann@33361
  1662
termination by (relation "measure (\<lambda>(a, b). nat (a - b + 1))")
haftmann@33361
  1663
  (auto simp add: mult_2)
haftmann@33361
  1664
haftmann@33361
  1665
text{*algorithm for the case @{text "a<0, b>0"}*}
haftmann@33361
  1666
function negDivAlg :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
haftmann@33361
  1667
  "negDivAlg a b = (if 0 \<le>a + b \<or> b \<le> 0  then (-1, a + b)
haftmann@33361
  1668
     else adjust b (negDivAlg a (2 * b)))"
haftmann@33361
  1669
by auto
haftmann@33361
  1670
termination by (relation "measure (\<lambda>(a, b). nat (- a - b))")
haftmann@33361
  1671
  (auto simp add: mult_2)
haftmann@33361
  1672
haftmann@33361
  1673
text{*algorithm for the general case @{term "b\<noteq>0"}*}
haftmann@33361
  1674
haftmann@33361
  1675
definition divmod_int :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
haftmann@33361
  1676
    --{*The full division algorithm considers all possible signs for a, b
haftmann@33361
  1677
       including the special case @{text "a=0, b<0"} because 
haftmann@33361
  1678
       @{term negDivAlg} requires @{term "a<0"}.*}
haftmann@33361
  1679
  "divmod_int a b = (if 0 \<le> a then if 0 \<le> b then posDivAlg a b
haftmann@33361
  1680
                  else if a = 0 then (0, 0)
huffman@46560
  1681
                       else apsnd uminus (negDivAlg (-a) (-b))
haftmann@33361
  1682
               else 
haftmann@33361
  1683
                  if 0 < b then negDivAlg a b
huffman@46560
  1684
                  else apsnd uminus (posDivAlg (-a) (-b)))"
haftmann@33361
  1685
haftmann@60429
  1686
instantiation int :: ring_div
haftmann@33361
  1687
begin
haftmann@33361
  1688
haftmann@60352
  1689
definition divide_int where
haftmann@60429
  1690
  div_int_def: "a div b = fst (divmod_int a b)"
haftmann@60352
  1691
haftmann@60352
  1692
definition mod_int where
haftmann@60352
  1693
  "a mod b = snd (divmod_int a b)"
haftmann@60352
  1694
huffman@46551
  1695
lemma fst_divmod_int [simp]:
huffman@46551
  1696
  "fst (divmod_int a b) = a div b"
huffman@46551
  1697
  by (simp add: div_int_def)
huffman@46551
  1698
huffman@46551
  1699
lemma snd_divmod_int [simp]:
huffman@46551
  1700
  "snd (divmod_int a b) = a mod b"
huffman@46551
  1701
  by (simp add: mod_int_def)
huffman@46551
  1702
haftmann@33361
  1703
lemma divmod_int_mod_div:
haftmann@33361
  1704
  "divmod_int p q = (p div q, p mod q)"
huffman@46551
  1705
  by (simp add: prod_eq_iff)
haftmann@33361
  1706
haftmann@33361
  1707
text{*
haftmann@33361
  1708
Here is the division algorithm in ML:
haftmann@33361
  1709
haftmann@33361
  1710
\begin{verbatim}
haftmann@33361
  1711
    fun posDivAlg (a,b) =
haftmann@33361
  1712
      if a<b then (0,a)
haftmann@33361
  1713
      else let val (q,r) = posDivAlg(a, 2*b)
haftmann@33361
  1714
               in  if 0\<le>r-b then (2*q+1, r-b) else (2*q, r)
haftmann@33361
  1715
           end
haftmann@33361
  1716
haftmann@33361
  1717
    fun negDivAlg (a,b) =
haftmann@33361
  1718
      if 0\<le>a+b then (~1,a+b)
haftmann@33361
  1719
      else let val (q,r) = negDivAlg(a, 2*b)
haftmann@33361
  1720
               in  if 0\<le>r-b then (2*q+1, r-b) else (2*q, r)
haftmann@33361
  1721
           end;
haftmann@33361
  1722
haftmann@33361
  1723
    fun negateSnd (q,r:int) = (q,~r);
haftmann@33361
  1724
haftmann@33361
  1725
    fun divmod (a,b) = if 0\<le>a then 
haftmann@33361
  1726
                          if b>0 then posDivAlg (a,b) 
haftmann@33361
  1727
                           else if a=0 then (0,0)
haftmann@33361
  1728
                                else negateSnd (negDivAlg (~a,~b))
haftmann@33361
  1729
                       else 
haftmann@33361
  1730
                          if 0<b then negDivAlg (a,b)
haftmann@33361
  1731
                          else        negateSnd (posDivAlg (~a,~b));
haftmann@33361
  1732
\end{verbatim}
haftmann@33361
  1733
*}
haftmann@33361
  1734
haftmann@33361
  1735
huffman@46551
  1736
subsubsection {* Uniqueness and Monotonicity of Quotients and Remainders *}
haftmann@33361
  1737
haftmann@33361
  1738
lemma unique_quotient_lemma:
haftmann@33361
  1739
     "[| b*q' + r'  \<le> b*q + r;  0 \<le> r';  r' < b;  r < b |]  
haftmann@33361
  1740
      ==> q' \<le> (q::int)"
haftmann@33361
  1741
apply (subgoal_tac "r' + b * (q'-q) \<le> r")
haftmann@33361
  1742
 prefer 2 apply (simp add: right_diff_distrib)
haftmann@33361
  1743
apply (subgoal_tac "0 < b * (1 + q - q') ")
haftmann@33361
  1744
apply (erule_tac [2] order_le_less_trans)
webertj@49962
  1745
 prefer 2 apply (simp add: right_diff_distrib distrib_left)
haftmann@33361
  1746
apply (subgoal_tac "b * q' < b * (1 + q) ")
webertj@49962
  1747
 prefer 2 apply (simp add: right_diff_distrib distrib_left)
haftmann@33361
  1748
apply (simp add: mult_less_cancel_left)
haftmann@33361
  1749
done
haftmann@33361
  1750
haftmann@33361
  1751
lemma unique_quotient_lemma_neg:
haftmann@33361
  1752
     "[| b*q' + r' \<le> b*q + r;  r \<le> 0;  b < r;  b < r' |]  
haftmann@33361
  1753
      ==> q \<le> (q'::int)"
haftmann@33361
  1754
by (rule_tac b = "-b" and r = "-r'" and r' = "-r" in unique_quotient_lemma, 
haftmann@33361
  1755
    auto)
haftmann@33361
  1756
haftmann@33361
  1757
lemma unique_quotient:
bulwahn@46552
  1758
     "[| divmod_int_rel a b (q, r); divmod_int_rel a b (q', r') |]  
haftmann@33361
  1759
      ==> q = q'"
haftmann@33361
  1760
apply (simp add: divmod_int_rel_def linorder_neq_iff split: split_if_asm)
haftmann@33361
  1761
apply (blast intro: order_antisym
haftmann@33361
  1762
             dest: order_eq_refl [THEN unique_quotient_lemma] 
haftmann@33361
  1763
             order_eq_refl [THEN unique_quotient_lemma_neg] sym)+
haftmann@33361
  1764
done
haftmann@33361
  1765
haftmann@33361
  1766
haftmann@33361
  1767
lemma unique_remainder:
bulwahn@46552
  1768
     "[| divmod_int_rel a b (q, r); divmod_int_rel a b (q', r') |]  
haftmann@33361
  1769
      ==> r = r'"
haftmann@33361
  1770
apply (subgoal_tac "q = q'")
haftmann@33361
  1771
 apply (simp add: divmod_int_rel_def)
haftmann@33361
  1772
apply (blast intro: unique_quotient)
haftmann@33361
  1773
done
haftmann@33361
  1774
haftmann@33361
  1775
huffman@46551
  1776
subsubsection {* Correctness of @{term posDivAlg}, the Algorithm for Non-Negative Dividends *}
haftmann@33361
  1777
haftmann@33361
  1778
text{*And positive divisors*}
haftmann@33361
  1779
haftmann@33361
  1780
lemma adjust_eq [simp]:
huffman@47108
  1781
     "adjust b (q, r) = 
huffman@47108
  1782
      (let diff = r - b in  
huffman@47108
  1783
        if 0 \<le> diff then (2 * q + 1, diff)   
haftmann@33361
  1784
                     else (2*q, r))"
huffman@47108
  1785
  by (simp add: Let_def adjust_def)
haftmann@33361
  1786
haftmann@33361
  1787
declare posDivAlg.simps [simp del]
haftmann@33361
  1788
haftmann@33361
  1789
text{*use with a simproc to avoid repeatedly proving the premise*}
haftmann@33361
  1790
lemma posDivAlg_eqn:
haftmann@33361
  1791
     "0 < b ==>  
haftmann@33361
  1792
      posDivAlg a b = (if a<b then (0,a) else adjust b (posDivAlg a (2*b)))"
haftmann@33361
  1793
by (rule posDivAlg.simps [THEN trans], simp)
haftmann@33361
  1794
haftmann@33361
  1795
text{*Correctness of @{term posDivAlg}: it computes quotients correctly*}
haftmann@33361
  1796
theorem posDivAlg_correct:
haftmann@33361
  1797
  assumes "0 \<le> a" and "0 < b"
haftmann@33361
  1798
  shows "divmod_int_rel a b (posDivAlg a b)"
wenzelm@41550
  1799
  using assms
wenzelm@41550
  1800
  apply (induct a b rule: posDivAlg.induct)
wenzelm@41550
  1801
  apply auto
wenzelm@41550
  1802
  apply (simp add: divmod_int_rel_def)
webertj@49962
  1803
  apply (subst posDivAlg_eqn, simp add: distrib_left)
wenzelm@41550
  1804
  apply (case_tac "a < b")
wenzelm@41550
  1805
  apply simp_all
wenzelm@41550
  1806
  apply (erule splitE)
haftmann@57514
  1807
  apply (auto simp add: distrib_left Let_def ac_simps mult_2_right)
wenzelm@41550
  1808
  done
haftmann@33361
  1809
haftmann@33361
  1810
huffman@46551
  1811
subsubsection {* Correctness of @{term negDivAlg}, the Algorithm for Negative Dividends *}
haftmann@33361
  1812
haftmann@33361
  1813
text{*And positive divisors*}
haftmann@33361
  1814
haftmann@33361
  1815
declare negDivAlg.simps [simp del]
haftmann@33361
  1816
haftmann@33361
  1817
text{*use with a simproc to avoid repeatedly proving the premise*}
haftmann@33361
  1818
lemma negDivAlg_eqn:
haftmann@33361
  1819
     "0 < b ==>  
haftmann@33361
  1820
      negDivAlg a b =       
haftmann@33361
  1821
       (if 0\<le>a+b then (-1,a+b) else adjust b (negDivAlg a (2*b)))"
haftmann@33361
  1822
by (rule negDivAlg.simps [THEN trans], simp)
haftmann@33361
  1823
haftmann@33361
  1824
(*Correctness of negDivAlg: it computes quotients correctly
haftmann@33361
  1825
  It doesn't work if a=0 because the 0/b equals 0, not -1*)
haftmann@33361
  1826
lemma negDivAlg_correct:
haftmann@33361
  1827
  assumes "a < 0" and "b > 0"
haftmann@33361
  1828
  shows "divmod_int_rel a b (negDivAlg a b)"
wenzelm@41550
  1829
  using assms
wenzelm@41550
  1830
  apply (induct a b rule: negDivAlg.induct)
wenzelm@41550
  1831
  apply (auto simp add: linorder_not_le)
wenzelm@41550
  1832
  apply (simp add: divmod_int_rel_def)
wenzelm@41550
  1833
  apply (subst negDivAlg_eqn, assumption)
wenzelm@41550
  1834
  apply (case_tac "a + b < (0\<Colon>int)")
wenzelm@41550
  1835
  apply simp_all
wenzelm@41550
  1836
  apply (erule splitE)
haftmann@57514
  1837
  apply (auto simp add: distrib_left Let_def ac_simps mult_2_right)
wenzelm@41550
  1838
  done
haftmann@33361
  1839
haftmann@33361
  1840
huffman@46551
  1841
subsubsection {* Existence Shown by Proving the Division Algorithm to be Correct *}
haftmann@33361
  1842
haftmann@33361
  1843
(*the case a=0*)
huffman@47139
  1844
lemma divmod_int_rel_0: "divmod_int_rel 0 b (0, 0)"
haftmann@33361
  1845
by (auto simp add: divmod_int_rel_def linorder_neq_iff)
haftmann@33361
  1846
haftmann@33361
  1847
lemma posDivAlg_0 [simp]: "posDivAlg 0 b = (0, 0)"
haftmann@33361
  1848
by (subst posDivAlg.simps, auto)
haftmann@33361
  1849
huffman@47139
  1850
lemma posDivAlg_0_right [simp]: "posDivAlg a 0 = (0, a)"
huffman@47139
  1851
by (subst posDivAlg.simps, auto)
huffman@47139
  1852
haftmann@58410
  1853
lemma negDivAlg_minus1 [simp]: "negDivAlg (- 1) b = (- 1, b - 1)"
haftmann@33361
  1854
by (subst negDivAlg.simps, auto)
haftmann@33361
  1855
huffman@46560
  1856
lemma divmod_int_rel_neg: "divmod_int_rel (-a) (-b) qr ==> divmod_int_rel a b (apsnd uminus qr)"
huffman@47139
  1857
by (auto simp add: divmod_int_rel_def)
huffman@47139
  1858
huffman@47139
  1859
lemma divmod_int_correct: "divmod_int_rel a b (divmod_int a b)"
huffman@47139
  1860
apply (cases "b = 0", simp add: divmod_int_def divmod_int_rel_def)
haftmann@33361
  1861
by (force simp add: linorder_neq_iff divmod_int_rel_0 divmod_int_def divmod_int_rel_neg
haftmann@33361
  1862
                    posDivAlg_correct negDivAlg_correct)
haftmann@33361
  1863
huffman@47141
  1864
lemma divmod_int_unique:
huffman@47141
  1865
  assumes "divmod_int_rel a b qr" 
huffman@47141
  1866
  shows "divmod_int a b = qr"
huffman@47141
  1867
  using assms divmod_int_correct [of a b]
huffman@47141
  1868
  using unique_quotient [of a b] unique_remainder [of a b]
huffman@47141
  1869
  by (metis pair_collapse)
huffman@47141
  1870
huffman@47141
  1871
lemma divmod_int_rel_div_mod: "divmod_int_rel a b (a div b, a mod b)"
huffman@47141
  1872
  using divmod_int_correct by (simp add: divmod_int_mod_div)
huffman@47141
  1873
huffman@47141
  1874
lemma div_int_unique: "divmod_int_rel a b (q, r) \<Longrightarrow> a div b = q"
huffman@47141
  1875
  by (simp add: divmod_int_rel_div_mod [THEN unique_quotient])
huffman@47141
  1876
huffman@47141
  1877
lemma mod_int_unique: "divmod_int_rel a b (q, r) \<Longrightarrow> a mod b = r"
huffman@47141
  1878
  by (simp add: divmod_int_rel_div_mod [THEN unique_remainder])
huffman@47141
  1879
haftmann@60429
  1880
instance
huffman@47141
  1881
proof
huffman@47141
  1882
  fix a b :: int
huffman@47141
  1883
  show "a div b * b + a mod b = a"
huffman@47141
  1884
    using divmod_int_rel_div_mod [of a b]
haftmann@57512
  1885
    unfolding divmod_int_rel_def by (simp add: mult.commute)
huffman@47141
  1886
next
huffman@47141
  1887
  fix a b c :: int
huffman@47141
  1888
  assume "b \<noteq> 0"
huffman@47141
  1889
  hence "divmod_int_rel (a + c * b) b (c + a div b, a mod b)"
huffman@47141
  1890
    using divmod_int_rel_div_mod [of a b]
huffman@47141
  1891
    unfolding divmod_int_rel_def by (auto simp: algebra_simps)
huffman@47141
  1892
  thus "(a + c * b) div b = c + a div b"
huffman@47141
  1893
    by (rule div_int_unique)
huffman@47141
  1894
next
huffman@47141
  1895
  fix a b c :: int
huffman@47141
  1896
  assume "c \<noteq> 0"
huffman@47141
  1897
  hence "\<And>q r. divmod_int_rel a b (q, r)
huffman@47141
  1898
    \<Longrightarrow> divmod_int_rel (c * a) (c * b) (q, c * r)"
huffman@47141
  1899
    unfolding divmod_int_rel_def
huffman@47141
  1900
    by - (rule linorder_cases [of 0 b], auto simp: algebra_simps
huffman@47141
  1901
      mult_less_0_iff zero_less_mult_iff mult_strict_right_mono
huffman@47141
  1902
      mult_strict_right_mono_neg zero_le_mult_iff mult_le_0_iff)
huffman@47141
  1903
  hence "divmod_int_rel (c * a) (c * b) (a div b, c * (a mod b))"
huffman@47141
  1904
    using divmod_int_rel_div_mod [of a b] .
huffman@47141
  1905
  thus "(c * a) div (c * b) = a div b"
huffman@47141
  1906
    by (rule div_int_unique)
huffman@47141
  1907
next
huffman@47141
  1908
  fix a :: int show "a div 0 = 0"
huffman@47141
  1909
    by (rule div_int_unique, simp add: divmod_int_rel_def)
huffman@47141
  1910
next
huffman@47141
  1911
  fix a :: int show "0 div a = 0"
huffman@47141
  1912
    by (rule div_int_unique, auto simp add: divmod_int_rel_def)
huffman@47141
  1913
qed
huffman@47141
  1914
haftmann@60429
  1915
end
haftmann@60429
  1916
haftmann@33361
  1917
text{*Basic laws about division and remainder*}
haftmann@33361
  1918
haftmann@33361
  1919
lemma zmod_zdiv_equality: "(a::int) = b * (a div b) + (a mod b)"
huffman@47141
  1920
  by (fact mod_div_equality2 [symmetric])
haftmann@33361
  1921
haftmann@33361
  1922
text {* Tool setup *}
haftmann@33361
  1923
haftmann@33361
  1924
ML {*
wenzelm@43594
  1925
structure Cancel_Div_Mod_Int = Cancel_Div_Mod
wenzelm@41550
  1926
(
haftmann@60352
  1927
  val div_name = @{const_name Rings.divide};
haftmann@33361
  1928
  val mod_name = @{const_name mod};
haftmann@33361
  1929
  val mk_binop = HOLogic.mk_binop;
haftmann@33361
  1930
  val mk_sum = Arith_Data.mk_sum HOLogic.intT;
haftmann@33361
  1931
  val dest_sum = Arith_Data.dest_sum;
haftmann@33361
  1932
huffman@47165
  1933
  val div_mod_eqs = map mk_meta_eq [@{thm div_mod_equality}, @{thm div_mod_equality2}];
haftmann@33361
  1934
haftmann@33361
  1935
  val prove_eq_sums = Arith_Data.prove_conv2 all_tac (Arith_Data.simp_all_tac 
haftmann@59556
  1936
    (@{thm diff_conv_add_uminus} :: @{thms add_0_left add_0_right} @ @{thms ac_simps}))
wenzelm@41550
  1937
)
haftmann@33361
  1938
*}
haftmann@33361
  1939
wenzelm@43594
  1940
simproc_setup cancel_div_mod_int ("(k::int) + l") = {* K Cancel_Div_Mod_Int.proc *}
wenzelm@43594
  1941
huffman@47141
  1942
lemma pos_mod_conj: "(0::int) < b \<Longrightarrow> 0 \<le> a mod b \<and> a mod b < b"
huffman@47141
  1943
  using divmod_int_correct [of a b]
huffman@47141
  1944
  by (auto simp add: divmod_int_rel_def prod_eq_iff)
haftmann@33361
  1945
wenzelm@45607
  1946
lemmas pos_mod_sign [simp] = pos_mod_conj [THEN conjunct1]
wenzelm@45607
  1947
   and pos_mod_bound [simp] = pos_mod_conj [THEN conjunct2]
haftmann@33361
  1948
huffman@47141
  1949
lemma neg_mod_conj: "b < (0::int) \<Longrightarrow> a mod b \<le> 0 \<and> b < a mod b"
huffman@47141
  1950
  using divmod_int_correct [of a b]
huffman@47141
  1951
  by (auto simp add: divmod_int_rel_def prod_eq_iff)
haftmann@33361
  1952
wenzelm@45607
  1953
lemmas neg_mod_sign [simp] = neg_mod_conj [THEN conjunct1]
wenzelm@45607
  1954
   and neg_mod_bound [simp] = neg_mod_conj [THEN conjunct2]
haftmann@33361
  1955
haftmann@33361
  1956
huffman@46551
  1957
subsubsection {* General Properties of div and mod *}
haftmann@33361
  1958
haftmann@33361
  1959
lemma div_pos_pos_trivial: "[| (0::int) \<le> a;  a < b |] ==> a div b = 0"
huffman@47140
  1960
apply (rule div_int_unique)
haftmann@33361
  1961
apply (auto simp add: divmod_int_rel_def)
haftmann@33361
  1962
done
haftmann@33361
  1963
haftmann@33361
  1964
lemma div_neg_neg_trivial: "[| a \<le> (0::int);  b < a |] ==> a div b = 0"
huffman@47140
  1965
apply (rule div_int_unique)
haftmann@33361
  1966
apply (auto simp add: divmod_int_rel_def)
haftmann@33361
  1967
done
haftmann@33361
  1968
haftmann@33361
  1969
lemma div_pos_neg_trivial: "[| (0::int) < a;  a+b \<le> 0 |] ==> a div b = -1"
huffman@47140
  1970
apply (rule div_int_unique)
haftmann@33361
  1971
apply (auto simp add: divmod_int_rel_def)
haftmann@33361
  1972
done
haftmann@33361
  1973
haftmann@33361
  1974
(*There is no div_neg_pos_trivial because  0 div b = 0 would supersede it*)
haftmann@33361
  1975
haftmann@33361
  1976
lemma mod_pos_pos_trivial: "[| (0::int) \<le> a;  a < b |] ==> a mod b = a"
huffman@47140
  1977
apply (rule_tac q = 0 in mod_int_unique)
haftmann@33361
  1978
apply (auto simp add: divmod_int_rel_def)
haftmann@33361
  1979
done
haftmann@33361
  1980
haftmann@33361
  1981
lemma mod_neg_neg_trivial: "[| a \<le> (0::int);  b < a |] ==> a mod b = a"
huffman@47140
  1982
apply (rule_tac q = 0 in mod_int_unique)
haftmann@33361
  1983
apply (auto simp add: divmod_int_rel_def)
haftmann@33361
  1984
done
haftmann@33361
  1985
haftmann@33361
  1986
lemma mod_pos_neg_trivial: "[| (0::int) < a;  a+b \<le> 0 |] ==> a mod b = a+b"
huffman@47140
  1987
apply (rule_tac q = "-1" in mod_int_unique)
haftmann@33361
  1988
apply (auto simp add: divmod_int_rel_def)
haftmann@33361
  1989
done
haftmann@33361
  1990
haftmann@33361
  1991
text{*There is no @{text mod_neg_pos_trivial}.*}
haftmann@33361
  1992
haftmann@33361
  1993
huffman@46551
  1994
subsubsection {* Laws for div and mod with Unary Minus *}
haftmann@33361
  1995
haftmann@33361
  1996
lemma zminus1_lemma:
huffman@47139
  1997
     "divmod_int_rel a b (q, r) ==> b \<noteq> 0
haftmann@33361
  1998
      ==> divmod_int_rel (-a) b (if r=0 then -q else -q - 1,  
haftmann@33361
  1999
                          if r=0 then 0 else b-r)"
haftmann@33361
  2000
by (force simp add: split_ifs divmod_int_rel_def linorder_neq_iff right_diff_distrib)
haftmann@33361
  2001
haftmann@33361
  2002
haftmann@33361
  2003
lemma zdiv_zminus1_eq_if:
haftmann@33361
  2004
     "b \<noteq> (0::int)  
haftmann@33361
  2005
      ==> (-a) div b =  
haftmann@33361
  2006
          (if a mod b = 0 then - (a div b) else  - (a div b) - 1)"
huffman@47140
  2007
by (blast intro: divmod_int_rel_div_mod [THEN zminus1_lemma, THEN div_int_unique])
haftmann@33361
  2008
haftmann@33361
  2009
lemma zmod_zminus1_eq_if:
haftmann@33361
  2010
     "(-a::int) mod b = (if a mod b = 0 then 0 else  b - (a mod b))"
haftmann@33361
  2011
apply (case_tac "b = 0", simp)
huffman@47140
  2012
apply (blast intro: divmod_int_rel_div_mod [THEN zminus1_lemma, THEN mod_int_unique])
haftmann@33361
  2013
done
haftmann@33361
  2014
haftmann@33361
  2015
lemma zmod_zminus1_not_zero:
haftmann@33361
  2016
  fixes k l :: int
haftmann@33361
  2017
  shows "- k mod l \<noteq> 0 \<Longrightarrow> k mod l \<noteq> 0"
haftmann@33361
  2018
  unfolding zmod_zminus1_eq_if by auto
haftmann@33361
  2019
haftmann@33361
  2020
lemma zdiv_zminus2_eq_if:
haftmann@33361
  2021
     "b \<noteq> (0::int)  
haftmann@33361
  2022
      ==> a div (-b) =  
haftmann@33361
  2023
          (if a mod b = 0 then - (a div b) else  - (a div b) - 1)"
huffman@47159
  2024
by (simp add: zdiv_zminus1_eq_if div_minus_right)
haftmann@33361
  2025
haftmann@33361
  2026
lemma zmod_zminus2_eq_if:
haftmann@33361
  2027
     "a mod (-b::int) = (if a mod b = 0 then 0 else  (a mod b) - b)"
huffman@47159
  2028
by (simp add: zmod_zminus1_eq_if mod_minus_right)
haftmann@33361
  2029
haftmann@33361
  2030
lemma zmod_zminus2_not_zero:
haftmann@33361
  2031
  fixes k l :: int
haftmann@33361
  2032
  shows "k mod - l \<noteq> 0 \<Longrightarrow> k mod l \<noteq> 0"
haftmann@33361
  2033
  unfolding zmod_zminus2_eq_if by auto 
haftmann@33361
  2034
haftmann@33361
  2035
huffman@46551
  2036
subsubsection {* Computation of Division and Remainder *}
haftmann@33361
  2037
haftmann@33361
  2038
lemma div_eq_minus1: "(0::int) < b ==> -1 div b = -1"
haftmann@33361
  2039
by (simp add: div_int_def divmod_int_def)
haftmann@33361
  2040
haftmann@33361
  2041
lemma zmod_minus1: "(0::int) < b ==> -1 mod b = b - 1"
haftmann@33361
  2042
by (simp add: mod_int_def divmod_int_def)
haftmann@33361
  2043
haftmann@33361
  2044
text{*a positive, b positive *}
haftmann@33361
  2045
haftmann@33361
  2046
lemma div_pos_pos: "[| 0 < a;  0 \<le> b |] ==> a div b = fst (posDivAlg a b)"
haftmann@33361
  2047
by (simp add: div_int_def divmod_int_def)
haftmann@33361
  2048
haftmann@33361
  2049
lemma mod_pos_pos: "[| 0 < a;  0 \<le> b |] ==> a mod b = snd (posDivAlg a b)"
haftmann@33361
  2050
by (simp add: mod_int_def divmod_int_def)
haftmann@33361
  2051
haftmann@33361
  2052
text{*a negative, b positive *}
haftmann@33361
  2053
haftmann@33361
  2054
lemma div_neg_pos: "[| a < 0;  0 < b |] ==> a div b = fst (negDivAlg a b)"
haftmann@33361
  2055
by (simp add: div_int_def divmod_int_def)
haftmann@33361
  2056
haftmann@33361
  2057
lemma mod_neg_pos: "[| a < 0;  0 < b |] ==> a mod b = snd (negDivAlg a b)"
haftmann@33361
  2058
by (simp add: mod_int_def divmod_int_def)
haftmann@33361
  2059
haftmann@33361
  2060
text{*a positive, b negative *}
haftmann@33361
  2061
haftmann@33361
  2062
lemma div_pos_neg:
huffman@46560
  2063
     "[| 0 < a;  b < 0 |] ==> a div b = fst (apsnd uminus (negDivAlg (-a) (-b)))"
haftmann@33361
  2064
by (simp add: div_int_def divmod_int_def)
haftmann@33361
  2065
haftmann@33361
  2066
lemma mod_pos_neg:
huffman@46560
  2067
     "[| 0 < a;  b < 0 |] ==> a mod b = snd (apsnd uminus (negDivAlg (-a) (-b)))"
haftmann@33361
  2068
by (simp add: mod_int_def divmod_int_def)
haftmann@33361
  2069
haftmann@33361
  2070
text{*a negative, b negative *}
haftmann@33361
  2071
haftmann@33361
  2072
lemma div_neg_neg:
huffman@46560
  2073
     "[| a < 0;  b \<le> 0 |] ==> a div b = fst (apsnd uminus (posDivAlg (-a) (-b)))"
haftmann@33361
  2074
by (simp add: div_int_def divmod_int_def)
haftmann@33361
  2075
haftmann@33361
  2076
lemma mod_neg_neg:
huffman@46560
  2077
     "[| a < 0;  b \<le> 0 |] ==> a mod b = snd (apsnd uminus (posDivAlg (-a) (-b)))"
haftmann@33361
  2078
by (simp add: mod_int_def divmod_int_def)
haftmann@33361
  2079
haftmann@33361
  2080
text {*Simplify expresions in which div and mod combine numerical constants*}
haftmann@33361
  2081
huffman@45530
  2082
lemma int_div_pos_eq: "\<lbrakk>(a::int) = b * q + r; 0 \<le> r; r < b\<rbrakk> \<Longrightarrow> a div b = q"
huffman@47140
  2083
  by (rule div_int_unique [of a b q r]) (simp add: divmod_int_rel_def)
huffman@45530
  2084
huffman@45530
  2085
lemma int_div_neg_eq: "\<lbrakk>(a::int) = b * q + r; r \<le> 0; b < r\<rbrakk> \<Longrightarrow> a div b = q"
huffman@47140
  2086
  by (rule div_int_unique [of a b q r],
bulwahn@46552
  2087
    simp add: divmod_int_rel_def)
huffman@45530
  2088
huffman@45530
  2089
lemma int_mod_pos_eq: "\<lbrakk>(a::int) = b * q + r; 0 \<le> r; r < b\<rbrakk> \<Longrightarrow> a mod b = r"
huffman@47140
  2090
  by (rule mod_int_unique [of a b q r],
bulwahn@46552
  2091
    simp add: divmod_int_rel_def)
huffman@45530
  2092
huffman@45530
  2093
lemma int_mod_neg_eq: "\<lbrakk>(a::int) = b * q + r; r \<le> 0; b < r\<rbrakk> \<Longrightarrow> a mod b = r"
huffman@47140
  2094
  by (rule mod_int_unique [of a b q r],
bulwahn@46552
  2095
    simp add: divmod_int_rel_def)
huffman@45530
  2096
haftmann@53069
  2097
text {*
haftmann@53069
  2098
  numeral simprocs -- high chance that these can be replaced
haftmann@53069
  2099
  by divmod algorithm from @{class semiring_numeral_div}
haftmann@53069
  2100
*}
haftmann@53069
  2101
haftmann@33361
  2102
ML {*
haftmann@33361
  2103
local
huffman@45530
  2104
  val mk_number = HOLogic.mk_number HOLogic.intT
huffman@45530
  2105
  val plus = @{term "plus :: int \<Rightarrow> int \<Rightarrow> int"}
huffman@45530
  2106
  val times = @{term "times :: int \<Rightarrow> int \<Rightarrow> int"}
huffman@45530
  2107
  val zero = @{term "0 :: int"}
huffman@45530
  2108
  val less = @{term "op < :: int \<Rightarrow> int \<Rightarrow> bool"}
huffman@45530
  2109
  val le = @{term "op \<le> :: int \<Rightarrow> int \<Rightarrow> bool"}
haftmann@54489
  2110
  val simps = @{thms arith_simps} @ @{thms rel_simps} @ [@{thm numeral_1_eq_1 [symmetric]}]
wenzelm@58847
  2111
  fun prove ctxt goal = Goal.prove ctxt [] [] (HOLogic.mk_Trueprop goal)
wenzelm@58847
  2112
    (K (ALLGOALS (full_simp_tac (put_simpset HOL_basic_ss ctxt addsimps simps))));
wenzelm@51717
  2113
  fun binary_proc proc ctxt ct =
haftmann@33361
  2114
    (case Thm.term_of ct of
haftmann@33361
  2115
      _ $ t $ u =>
wenzelm@59058
  2116
      (case try (apply2 (`(snd o HOLogic.dest_number))) (t, u) of
wenzelm@51717
  2117
        SOME args => proc ctxt args
haftmann@33361
  2118
      | NONE => NONE)
haftmann@33361
  2119
    | _ => NONE);
haftmann@33361
  2120
in
huffman@45530
  2121
  fun divmod_proc posrule negrule =
huffman@45530
  2122
    binary_proc (fn ctxt => fn ((a, t), (b, u)) =>
wenzelm@59058
  2123
      if b = 0 then NONE
wenzelm@59058
  2124
      else
wenzelm@59058
  2125
        let
wenzelm@59058
  2126
          val (q, r) = apply2 mk_number (Integer.div_mod a b)
wenzelm@59058
  2127
          val goal1 = HOLogic.mk_eq (t, plus $ (times $ u $ q) $ r)
wenzelm@59058
  2128
          val (goal2, goal3, rule) =
wenzelm@59058
  2129
            if b > 0
wenzelm@59058
  2130
            then (le $ zero $ r, less $ r $ u, posrule RS eq_reflection)
wenzelm@59058
  2131
            else (le $ r $ zero, less $ u $ r, negrule RS eq_reflection)
wenzelm@59058
  2132
        in SOME (rule OF map (prove ctxt) [goal1, goal2, goal3]) end)
haftmann@33361
  2133
end
haftmann@33361
  2134
*}
haftmann@33361
  2135
huffman@47108
  2136
simproc_setup binary_int_div
huffman@47108
  2137
  ("numeral m div numeral n :: int" |
haftmann@54489
  2138
   "numeral m div - numeral n :: int" |
haftmann@54489
  2139
   "- numeral m div numeral n :: int" |
haftmann@54489
  2140
   "- numeral m div - numeral n :: int") =
huffman@45530
  2141
  {* K (divmod_proc @{thm int_div_pos_eq} @{thm int_div_neg_eq}) *}
haftmann@33361
  2142
huffman@47108
  2143
simproc_setup binary_int_mod
huffman@47108
  2144
  ("numeral m mod numeral n :: int" |
haftmann@54489
  2145
   "numeral m mod - numeral n :: int" |
haftmann@54489
  2146
   "- numeral m mod numeral n :: int" |
haftmann@54489
  2147
   "- numeral m mod - numeral n :: int") =
huffman@45530
  2148
  {* K (divmod_proc @{thm int_mod_pos_eq} @{thm int_mod_neg_eq}) *}
haftmann@33361
  2149
huffman@47108
  2150
lemmas posDivAlg_eqn_numeral [simp] =
huffman@47108
  2151
    posDivAlg_eqn [of "numeral v" "numeral w", OF zero_less_numeral] for v w
huffman@47108
  2152
huffman@47108
  2153
lemmas negDivAlg_eqn_numeral [simp] =
haftmann@54489
  2154
    negDivAlg_eqn [of "numeral v" "- numeral w", OF zero_less_numeral] for v w
haftmann@33361
  2155
haftmann@33361
  2156
haftmann@55172
  2157
text {* Special-case simplification: @{text "\<plusminus>1 div z"} and @{text "\<plusminus>1 mod z"} *}
haftmann@55172
  2158
haftmann@55172
  2159
lemma [simp]:
haftmann@55172
  2160
  shows div_one_bit0: "1 div numeral (Num.Bit0 v) = (0 :: int)"
haftmann@55172
  2161
    and mod_one_bit0: "1 mod numeral (Num.Bit0 v) = (1 :: int)"
wenzelm@55439
  2162
    and div_one_bit1: "1 div numeral (Num.Bit1 v) = (0 :: int)"
wenzelm@55439
  2163
    and mod_one_bit1: "1 mod numeral (Num.Bit1 v) = (1 :: int)"
wenzelm@55439
  2164
    and div_one_neg_numeral: "1 div - numeral v = (- 1 :: int)"
wenzelm@55439
  2165
    and mod_one_neg_numeral: "1 mod - numeral v = (1 :: int) - numeral v"
haftmann@55172
  2166
  by (simp_all del: arith_special
haftmann@55172
  2167
    add: div_pos_pos mod_pos_pos div_pos_neg mod_pos_neg posDivAlg_eqn)
wenzelm@55439
  2168
haftmann@55172
  2169
lemma [simp]:
haftmann@55172
  2170
  shows div_neg_one_numeral: "- 1 div numeral v = (- 1 :: int)"
haftmann@55172
  2171
    and mod_neg_one_numeral: "- 1 mod numeral v = numeral v - (1 :: int)"
haftmann@55172
  2172
    and div_neg_one_neg_bit0: "- 1 div - numeral (Num.Bit0 v) = (0 :: int)"
haftmann@55172
  2173
    and mod_neg_one_neb_bit0: "- 1 mod - numeral (Num.Bit0 v) = (- 1 :: int)"
haftmann@55172
  2174
    and div_neg_one_neg_bit1: "- 1 div - numeral (Num.Bit1 v) = (0 :: int)"
haftmann@55172
  2175
    and mod_neg_one_neb_bit1: "- 1 mod - numeral (Num.Bit1 v) = (- 1 :: int)"
haftmann@55172
  2176
  by (simp_all add: div_eq_minus1 zmod_minus1)
haftmann@33361
  2177
haftmann@33361
  2178
huffman@46551
  2179
subsubsection {* Monotonicity in the First Argument (Dividend) *}
haftmann@33361
  2180
haftmann@33361
  2181
lemma zdiv_mono1: "[| a \<le> a';  0 < (b::int) |] ==> a div b \<le> a' div b"
haftmann@33361
  2182
apply (cut_tac a = a and b = b in zmod_zdiv_equality)
haftmann@33361
  2183
apply (cut_tac a = a' and b = b in zmod_zdiv_equality)
haftmann@33361
  2184
apply (rule unique_quotient_lemma)
haftmann@33361
  2185
apply (erule subst)
haftmann@33361
  2186
apply (erule subst, simp_all)
haftmann@33361
  2187
done
haftmann@33361
  2188
haftmann@33361
  2189
lemma zdiv_mono1_neg: "[| a \<le> a';  (b::int) < 0 |] ==> a' div b \<le> a div b"
haftmann@33361
  2190
apply (cut_tac a = a and b = b in zmod_zdiv_equality)
haftmann@33361
  2191
apply (cut_tac a = a' and b = b in zmod_zdiv_equality)
haftmann@33361
  2192
apply (rule unique_quotient_lemma_neg)
haftmann@33361
  2193
apply (erule subst)
haftmann@33361
  2194
apply (erule subst, simp_all)
haftmann@33361
  2195
done
haftmann@33361
  2196
haftmann@33361
  2197
huffman@46551
  2198
subsubsection {* Monotonicity in the Second Argument (Divisor) *}
haftmann@33361
  2199
haftmann@33361
  2200
lemma q_pos_lemma:
haftmann@33361
  2201
     "[| 0 \<le> b'*q' + r'; r' < b';  0 < b' |] ==> 0 \<le> (q'::int)"
haftmann@33361
  2202
apply (subgoal_tac "0 < b'* (q' + 1) ")
haftmann@33361
  2203
 apply (simp add: zero_less_mult_iff)
webertj@49962
  2204
apply (simp add: distrib_left)
haftmann@33361
  2205
done
haftmann@33361
  2206
haftmann@33361
  2207
lemma zdiv_mono2_lemma:
haftmann@33361
  2208
     "[| b*q + r = b'*q' + r';  0 \<le> b'*q' + r';   
haftmann@33361
  2209
         r' < b';  0 \<le> r;  0 < b';  b' \<le> b |]   
haftmann@33361
  2210
      ==> q \<le> (q'::int)"
haftmann@33361
  2211
apply (frule q_pos_lemma, assumption+) 
haftmann@33361
  2212
apply (subgoal_tac "b*q < b* (q' + 1) ")
haftmann@33361
  2213
 apply (simp add: mult_less_cancel_left)
haftmann@33361
  2214
apply (subgoal_tac "b*q = r' - r + b'*q'")
haftmann@33361
  2215
 prefer 2 apply simp
webertj@49962
  2216
apply (simp (no_asm_simp) add: distrib_left)
haftmann@57512
  2217
apply (subst add.commute, rule add_less_le_mono, arith)
haftmann@33361
  2218
apply (rule mult_right_mono, auto)
haftmann@33361
  2219
done
haftmann@33361
  2220
haftmann@33361
  2221
lemma zdiv_mono2:
haftmann@33361
  2222
     "[| (0::int) \<le> a;  0 < b';  b' \<le> b |] ==> a div b \<le> a div b'"
haftmann@33361
  2223
apply (subgoal_tac "b \<noteq> 0")
haftmann@33361
  2224
 prefer 2 apply arith
haftmann@33361
  2225
apply (cut_tac a = a and b = b in zmod_zdiv_equality)
haftmann@33361
  2226
apply (cut_tac a = a and b = b' in zmod_zdiv_equality)
haftmann@33361
  2227
apply (rule zdiv_mono2_lemma)
haftmann@33361
  2228
apply (erule subst)
haftmann@33361
  2229
apply (erule subst, simp_all)
haftmann@33361
  2230
done
haftmann@33361
  2231
haftmann@33361
  2232
lemma q_neg_lemma:
haftmann@33361
  2233
     "[| b'*q' + r' < 0;  0 \<le> r';  0 < b' |] ==> q' \<le> (0::int)"
haftmann@33361
  2234
apply (subgoal_tac "b'*q' < 0")
haftmann@33361
  2235
 apply (simp add: mult_less_0_iff, arith)
haftmann@33361
  2236
done
haftmann@33361
  2237
haftmann@33361
  2238
lemma zdiv_mono2_neg_lemma:
haftmann@33361
  2239
     "[| b*q + r = b'*q' + r';  b'*q' + r' < 0;   
haftmann@33361
  2240
         r < b;  0 \<le> r';  0 < b';  b' \<le> b |]   
haftmann@33361
  2241
      ==> q' \<le> (q::int)"
haftmann@33361
  2242
apply (frule q_neg_lemma, assumption+) 
haftmann@33361
  2243
apply (subgoal_tac "b*q' < b* (q + 1) ")
haftmann@33361
  2244
 apply (simp add: mult_less_cancel_left)
webertj@49962
  2245
apply (simp add: distrib_left)
haftmann@33361
  2246
apply (subgoal_tac "b*q' \<le> b'*q'")
haftmann@33361
  2247
 prefer 2 apply (simp add: mult_right_mono_neg, arith)
haftmann@33361
  2248
done
haftmann@33361
  2249
haftmann@33361
  2250
lemma zdiv_mono2_neg:
haftmann@33361
  2251
     "[| a < (0::int);  0 < b';  b' \<le> b |] ==> a div b' \<le> a div b"
haftmann@33361
  2252
apply (cut_tac a = a and b = b in zmod_zdiv_equality)
haftmann@33361
  2253
apply (cut_tac a = a and b = b' in zmod_zdiv_equality)
haftmann@33361
  2254
apply (rule zdiv_mono2_neg_lemma)
haftmann@33361
  2255
apply (erule subst)
haftmann@33361
  2256
apply (erule subst, simp_all)
haftmann@33361
  2257
done
haftmann@33361
  2258
haftmann@33361
  2259
huffman@46551
  2260
subsubsection {* More Algebraic Laws for div and mod *}
haftmann@33361
  2261
haftmann@33361
  2262
text{*proving (a*b) div c = a * (b div c) + a * (b mod c) *}
haftmann@33361
  2263
haftmann@33361
  2264
lemma zmult1_lemma:
bulwahn@46552
  2265
     "[| divmod_int_rel b c (q, r) |]  
haftmann@33361
  2266
      ==> divmod_int_rel (a * b) c (a*q + a*r div c, a*r mod c)"
haftmann@57514
  2267
by (auto simp add: split_ifs divmod_int_rel_def linorder_neq_iff distrib_left ac_simps)
haftmann@33361
  2268
haftmann@33361
  2269
lemma zdiv_zmult1_eq: "(a*b) div c = a*(b div c) + a*(b mod c) div (c::int)"
haftmann@33361
  2270
apply (case_tac "c = 0", simp)
huffman@47140
  2271
apply (blast intro: divmod_int_rel_div_mod [THEN zmult1_lemma, THEN div_int_unique])
haftmann@33361
  2272
done
haftmann@33361
  2273
haftmann@33361
  2274
text{*proving (a+b) div c = a div c + b div c + ((a mod c + b mod c) div c) *}
haftmann@33361
  2275
haftmann@33361
  2276
lemma zadd1_lemma:
bulwahn@46552
  2277
     "[| divmod_int_rel a c (aq, ar);  divmod_int_rel b c (bq, br) |]  
haftmann@33361
  2278
      ==> divmod_int_rel (a+b) c (aq + bq + (ar+br) div c, (ar+br) mod c)"
webertj@49962
  2279
by (force simp add: split_ifs divmod_int_rel_def linorder_neq_iff distrib_left)
haftmann@33361
  2280
haftmann@33361
  2281
(*NOT suitable for rewriting: the RHS has an instance of the LHS*)
haftmann@33361
  2282
lemma zdiv_zadd1_eq:
haftmann@33361
  2283
     "(a+b) div (c::int) = a div c + b div c + ((a mod c + b mod c) div c)"
haftmann@33361
  2284
apply (case_tac "c = 0", simp)
huffman@47140
  2285
apply (blast intro: zadd1_lemma [OF divmod_int_rel_div_mod divmod_int_rel_div_mod] div_int_unique)
haftmann@33361
  2286
done
haftmann@33361
  2287
haftmann@33361
  2288
lemma posDivAlg_div_mod:
haftmann@33361
  2289
  assumes "k \<ge> 0"
haftmann@33361
  2290
  and "l \<ge> 0"
haftmann@33361
  2291
  shows "posDivAlg k l = (k div l, k mod l)"
haftmann@33361
  2292
proof (cases "l = 0")
haftmann@33361
  2293
  case True then show ?thesis by (simp add: posDivAlg.simps)
haftmann@33361
  2294
next
haftmann@33361
  2295
  case False with assms posDivAlg_correct
haftmann@33361
  2296
    have "divmod_int_rel k l (fst (posDivAlg k l), snd (posDivAlg k l))"
haftmann@33361
  2297
    by simp
huffman@47140
  2298
  from div_int_unique [OF this] mod_int_unique [OF this]
haftmann@33361
  2299
  show ?thesis by simp
haftmann@33361
  2300
qed
haftmann@33361
  2301
haftmann@33361
  2302
lemma negDivAlg_div_mod:
haftmann@33361
  2303
  assumes "k < 0"
haftmann@33361
  2304
  and "l > 0"
haftmann@33361
  2305
  shows "negDivAlg k l = (k div l, k mod l)"
haftmann@33361
  2306
proof -
haftmann@33361
  2307
  from assms have "l \<noteq> 0" by simp
haftmann@33361
  2308
  from assms negDivAlg_correct
haftmann@33361
  2309
    have "divmod_int_rel k l (fst (negDivAlg k l), snd (negDivAlg k l))"
haftmann@33361
  2310
    by simp
huffman@47140
  2311
  from div_int_unique [OF this] mod_int_unique [OF this]
haftmann@33361
  2312
  show ?thesis by simp
haftmann@33361
  2313
qed
haftmann@33361
  2314
haftmann@33361
  2315
lemma zmod_eq_0_iff: "(m mod d = 0) = (EX q::int. m = d*q)"
haftmann@33361
  2316
by (simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)
haftmann@33361
  2317
haftmann@33361
  2318
(* REVISIT: should this be generalized to all semiring_div types? *)
haftmann@33361
  2319
lemmas zmod_eq_0D [dest!] = zmod_eq_0_iff [THEN iffD1]
haftmann@33361
  2320
huffman@47108
  2321
lemma zmod_zdiv_equality':
huffman@47108
  2322
  "(m\<Colon>int) mod n = m - (m div n) * n"
huffman@47141
  2323
  using mod_div_equality [of m n] by arith
huffman@47108
  2324
haftmann@33361
  2325
blanchet@55085
  2326
subsubsection {* Proving  @{term "a div (b * c) = (a div b) div c"} *}
haftmann@33361
  2327
haftmann@33361
  2328
(*The condition c>0 seems necessary.  Consider that 7 div ~6 = ~2 but
haftmann@33361
  2329
  7 div 2 div ~3 = 3 div ~3 = ~1.  The subcase (a div b) mod c = 0 seems
haftmann@33361
  2330
  to cause particular problems.*)
haftmann@33361
  2331
haftmann@33361
  2332
text{*first, four lemmas to bound the remainder for the cases b<0 and b>0 *}
haftmann@33361
  2333
blanchet@55085
  2334
lemma zmult2_lemma_aux1: "[| (0::int) < c;  b < r;  r \<le> 0 |] ==> b * c < b * (q mod c) + r"
haftmann@33361
  2335
apply (subgoal_tac "b * (c - q mod c) < r * 1")
haftmann@33361
  2336
 apply (simp add: algebra_simps)
haftmann@33361
  2337
apply (rule order_le_less_trans)
haftmann@33361
  2338
 apply (erule_tac [2] mult_strict_right_mono)
haftmann@33361
  2339
 apply (rule mult_left_mono_neg)
huffman@35216
  2340
  using add1_zle_eq[of "q mod c"]apply(simp add: algebra_simps)
haftmann@33361
  2341
 apply (simp)
haftmann@33361
  2342
apply (simp)
haftmann@33361
  2343
done
haftmann@33361
  2344
haftmann@33361
  2345
lemma zmult2_lemma_aux2:
haftmann@33361
  2346
     "[| (0::int) < c;   b < r;  r \<le> 0 |] ==> b * (q mod c) + r \<le> 0"
haftmann@33361
  2347
apply (subgoal_tac "b * (q mod c) \<le> 0")
haftmann@33361
  2348
 apply arith
haftmann@33361
  2349
apply (simp add: mult_le_0_iff)
haftmann@33361
  2350
done
haftmann@33361
  2351
haftmann@33361
  2352
lemma zmult2_lemma_aux3: "[| (0::int) < c;  0 \<le> r;  r < b |] ==> 0 \<le> b * (q mod c) + r"
haftmann@33361
  2353
apply (subgoal_tac "0 \<le> b * (q mod c) ")
haftmann@33361
  2354
apply arith
haftmann@33361
  2355
apply (simp add: zero_le_mult_iff)
haftmann@33361
  2356
done
haftmann@33361
  2357
haftmann@33361
  2358
lemma zmult2_lemma_aux4: "[| (0::int) < c; 0 \<le> r; r < b |] ==> b * (q mod c) + r < b * c"
haftmann@33361
  2359
apply (subgoal_tac "r * 1 < b * (c - q mod c) ")
haftmann@33361
  2360
 apply (simp add: right_diff_distrib)
haftmann@33361
  2361
apply (rule order_less_le_trans)
haftmann@33361
  2362
 apply (erule mult_strict_right_mono)
haftmann@33361
  2363
 apply (rule_tac [2] mult_left_mono)
haftmann@33361
  2364
  apply simp
huffman@35216
  2365
 using add1_zle_eq[of "q mod c"] apply (simp add: algebra_simps)
haftmann@33361
  2366
apply simp
haftmann@33361
  2367
done
haftmann@33361
  2368
bulwahn@46552
  2369
lemma zmult2_lemma: "[| divmod_int_rel a b (q, r); 0 < c |]  
haftmann@33361
  2370
      ==> divmod_int_rel a (b * c) (q div c, b*(q mod c) + r)"
haftmann@57514
  2371
by (auto simp add: mult.assoc divmod_int_rel_def linorder_neq_iff
webertj@49962
  2372
                   zero_less_mult_iff distrib_left [symmetric] 
huffman@47139
  2373
                   zmult2_lemma_aux1 zmult2_lemma_aux2 zmult2_lemma_aux3 zmult2_lemma_aux4 mult_less_0_iff split: split_if_asm)
haftmann@33361
  2374
haftmann@53068
  2375
lemma zdiv_zmult2_eq:
haftmann@53068
  2376
  fixes a b c :: int
haftmann@53068
  2377
  shows "0 \<le> c \<Longrightarrow> a div (b * c) = (a div b) div c"
haftmann@33361
  2378
apply (case_tac "b = 0", simp)
haftmann@53068
  2379
apply (force simp add: le_less divmod_int_rel_div_mod [THEN zmult2_lemma, THEN div_int_unique])
haftmann@33361
  2380
done
haftmann@33361
  2381
haftmann@33361
  2382
lemma zmod_zmult2_eq:
haftmann@53068
  2383
  fixes a b c :: int
haftmann@53068
  2384
  shows "0 \<le> c \<Longrightarrow> a mod (b * c) = b * (a div b mod c) + a mod b"
haftmann@33361
  2385
apply (case_tac "b = 0", simp)
haftmann@53068
  2386
apply (force simp add: le_less divmod_int_rel_div_mod [THEN zmult2_lemma, THEN mod_int_unique])
haftmann@33361
  2387
done
haftmann@33361
  2388
huffman@47108
  2389
lemma div_pos_geq:
huffman@47108
  2390
  fixes k l :: int
huffman@47108
  2391
  assumes "0 < l" and "l \<le> k"
huffman@47108
  2392
  shows "k div l = (k - l) div l + 1"
huffman@47108
  2393
proof -
huffman@47108
  2394
  have "k = (k - l) + l" by simp
huffman@47108
  2395
  then obtain j where k: "k = j + l" ..
huffman@47108
  2396
  with assms show ?thesis by simp
huffman@47108
  2397
qed
huffman@47108
  2398
huffman@47108
  2399
lemma mod_pos_geq:
huffman@47108
  2400
  fixes k l :: int
huffman@47108
  2401
  assumes "0 < l" and "l \<le> k"
huffman@47108
  2402
  shows "k mod l = (k - l) mod l"
huffman@47108
  2403
proof -
huffman@47108
  2404
  have "k = (k - l) + l" by simp
huffman@47108
  2405
  then obtain j where k: "k = j + l" ..
huffman@47108
  2406
  with assms show ?thesis by simp
huffman@47108
  2407
qed
huffman@47108
  2408
haftmann@33361
  2409
huffman@46551
  2410
subsubsection {* Splitting Rules for div and mod *}
haftmann@33361
  2411
haftmann@33361
  2412
text{*The proofs of the two lemmas below are essentially identical*}
haftmann@33361
  2413
haftmann@33361
  2414
lemma split_pos_lemma:
haftmann@33361
  2415
 "0<k ==> 
haftmann@33361
  2416
    P(n div k :: int)(n mod k) = (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P i j)"
haftmann@33361
  2417
apply (rule iffI, clarify)
wenzelm@59807
  2418
 apply (erule_tac P="P x y" for x y in rev_mp)  
haftmann@33361
  2419
 apply (subst mod_add_eq) 
haftmann@33361
  2420
 apply (subst zdiv_zadd1_eq) 
haftmann@33361
  2421
 apply (simp add: div_pos_pos_trivial mod_pos_pos_trivial)  
haftmann@33361
  2422
txt{*converse direction*}
haftmann@33361
  2423
apply (drule_tac x = "n div k" in spec) 
haftmann@33361
  2424
apply (drule_tac x = "n mod k" in spec, simp)
haftmann@33361
  2425
done
haftmann@33361
  2426
haftmann@33361
  2427
lemma split_neg_lemma:
haftmann@33361
  2428
 "k<0 ==>
haftmann@33361
  2429
    P(n div k :: int)(n mod k) = (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P i j)"
haftmann@33361
  2430
apply (rule iffI, clarify)
wenzelm@59807
  2431
 apply (erule_tac P="P x y" for x y in rev_mp)  
haftmann@33361
  2432
 apply (subst mod_add_eq) 
haftmann@33361
  2433
 apply (subst zdiv_zadd1_eq) 
haftmann@33361
  2434
 apply (simp add: div_neg_neg_trivial mod_neg_neg_trivial)  
haftmann@33361
  2435
txt{*converse direction*}
haftmann@33361
  2436
apply (drule_tac x = "n div k" in spec) 
haftmann@33361
  2437
apply (drule_tac x = "n mod k" in spec, simp)
haftmann@33361
  2438
done
haftmann@33361
  2439
haftmann@33361
  2440
lemma split_zdiv:
haftmann@33361
  2441
 "P(n div k :: int) =
haftmann@33361
  2442
  ((k = 0 --> P 0) & 
haftmann@33361
  2443
   (0<k --> (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P i)) & 
haftmann@33361
  2444
   (k<0 --> (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P i)))"
haftmann@33361
  2445
apply (case_tac "k=0", simp)
haftmann@33361
  2446
apply (simp only: linorder_neq_iff)
haftmann@33361
  2447
apply (erule disjE) 
haftmann@33361
  2448
 apply (simp_all add: split_pos_lemma [of concl: "%x y. P x"] 
haftmann@33361
  2449
                      split_neg_lemma [of concl: "%x y. P x"])
haftmann@33361
  2450
done
haftmann@33361
  2451
haftmann@33361
  2452
lemma split_zmod:
haftmann@33361
  2453
 "P(n mod k :: int) =
haftmann@33361
  2454
  ((k = 0 --> P n) & 
haftmann@33361
  2455
   (0<k --> (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P j)) & 
haftmann@33361
  2456
   (k<0 --> (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P j)))"
haftmann@33361
  2457
apply (case_tac "k=0", simp)
haftmann@33361
  2458
apply (simp only: linorder_neq_iff)
haftmann@33361
  2459
apply (erule disjE) 
haftmann@33361
  2460
 apply (simp_all add: split_pos_lemma [of concl: "%x y. P y"] 
haftmann@33361
  2461
                      split_neg_lemma [of concl: "%x y. P y"])
haftmann@33361
  2462
done
haftmann@33361
  2463
haftmann@60429
  2464
text {* Enable (lin)arith to deal with @{const divide} and @{const mod}
webertj@33730
  2465
  when these are applied to some constant that is of the form
huffman@47108
  2466
  @{term "numeral k"}: *}
huffman@47108
  2467
declare split_zdiv [of _ _ "numeral k", arith_split] for k
huffman@47108
  2468
declare split_zmod [of _ _ "numeral k", arith_split] for k
haftmann@33361
  2469
haftmann@33361
  2470
huffman@47166
  2471
subsubsection {* Computing @{text "div"} and @{text "mod"} with shifting *}
huffman@47166
  2472
huffman@47166
  2473
lemma pos_divmod_int_rel_mult_2:
huffman@47166
  2474
  assumes "0 \<le> b"
huffman@47166
  2475
  assumes "divmod_int_rel a b (q, r)"
huffman@47166
  2476
  shows "divmod_int_rel (1 + 2*a) (2*b) (q, 1 + 2*r)"
huffman@47166
  2477
  using assms unfolding divmod_int_rel_def by auto
huffman@47166
  2478
haftmann@54489
  2479
declaration {* K (Lin_Arith.add_simps @{thms uminus_numeral_One}) *}
haftmann@54489
  2480
huffman@47166
  2481
lemma neg_divmod_int_rel_mult_2:
huffman@47166
  2482
  assumes "b \<le> 0"
huffman@47166
  2483
  assumes "divmod_int_rel (a + 1) b (q, r)"
huffman@47166
  2484
  shows "divmod_int_rel (1 + 2*a) (2*b) (q, 2*r - 1)"
huffman@47166
  2485
  using assms unfolding divmod_int_rel_def by auto
haftmann@33361
  2486
haftmann@33361
  2487
text{*computing div by shifting *}
haftmann@33361
  2488
haftmann@33361
  2489
lemma pos_zdiv_mult_2: "(0::int) \<le> a ==> (1 + 2*b) div (2*a) = b div a"
huffman@47166
  2490
  using pos_divmod_int_rel_mult_2 [OF _ divmod_int_rel_div_mod]
huffman@47166
  2491
  by (rule div_int_unique)
haftmann@33361
  2492
boehmes@35815
  2493
lemma neg_zdiv_mult_2: 
boehmes@35815
  2494
  assumes A: "a \<le> (0::int)" shows "(1 + 2*b) div (2*a) = (b+1) div a"
huffman@47166
  2495
  using neg_divmod_int_rel_mult_2 [OF A divmod_int_rel_div_mod]
huffman@47166
  2496
  by (rule div_int_unique)
haftmann@33361
  2497
huffman@47108
  2498
(* FIXME: add rules for negative numerals *)
huffman@47108
  2499
lemma zdiv_numeral_Bit0 [simp]:
huffman@47108
  2500
  "numeral (Num.Bit0 v) div numeral (Num.Bit0 w) =
huffman@47108
  2501
    numeral v div (numeral w :: int)"
huffman@47108
  2502
  unfolding numeral.simps unfolding mult_2 [symmetric]
huffman@47108
  2503
  by (rule div_mult_mult1, simp)
huffman@47108
  2504
huffman@47108
  2505
lemma zdiv_numeral_Bit1 [simp]:
huffman@47108
  2506
  "numeral (Num.Bit1 v) div numeral (Num.Bit0 w) =  
huffman@47108
  2507
    (numeral v div (numeral w :: int))"
huffman@47108
  2508
  unfolding numeral.simps
haftmann@57512
  2509
  unfolding mult_2 [symmetric] add.commute [of _ 1]
huffman@47108
  2510
  by (rule pos_zdiv_mult_2, simp)
haftmann@33361
  2511
haftmann@33361
  2512
lemma pos_zmod_mult_2:
haftmann@33361
  2513
  fixes a b :: int
haftmann@33361
  2514
  assumes "0 \<le> a"
haftmann@33361
  2515
  shows "(1 + 2 * b) mod (2 * a) = 1 + 2 * (b mod a)"
huffman@47166
  2516
  using pos_divmod_int_rel_mult_2 [OF assms divmod_int_rel_div_mod]
huffman@47166
  2517
  by (rule mod_int_unique)
haftmann@33361
  2518
haftmann@33361
  2519
lemma neg_zmod_mult_2:
haftmann@33361
  2520
  fixes a b :: int
haftmann@33361
  2521
  assumes "a \<le> 0"
haftmann@33361
  2522
  shows "(1 + 2 * b) mod (2 * a) = 2 * ((b + 1) mod a) - 1"