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