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