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