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