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