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