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