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