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