src/HOL/Divides.thy
author haftmann
Wed Apr 15 15:30:37 2009 +0200 (2009-04-15)
changeset 30923 2697a1d1d34a
parent 30840 98809b3f5e3c
child 30930 11010e5f18f0
permissions -rw-r--r--
more coherent developement in Divides.thy and IntDiv.thy
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@27651
     6
header {* The division operators div and mod *}
paulson@3366
     7
nipkow@15131
     8
theory Divides
krauss@26748
     9
imports Nat Power Product_Type
haftmann@22993
    10
uses "~~/src/Provers/Arith/cancel_div_mod.ML"
nipkow@15131
    11
begin
paulson@3366
    12
haftmann@25942
    13
subsection {* Syntactic division operations *}
haftmann@25942
    14
haftmann@27651
    15
class div = dvd +
haftmann@27540
    16
  fixes div :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "div" 70)
haftmann@27651
    17
    and mod :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "mod" 70)
haftmann@27540
    18
haftmann@27540
    19
haftmann@27651
    20
subsection {* Abstract division in commutative semirings. *}
haftmann@25942
    21
haftmann@29509
    22
class semiring_div = comm_semiring_1_cancel + div +
haftmann@25942
    23
  assumes mod_div_equality: "a div b * b + a mod b = a"
haftmann@27651
    24
    and div_by_0 [simp]: "a div 0 = 0"
haftmann@27651
    25
    and div_0 [simp]: "0 div a = 0"
haftmann@27651
    26
    and div_mult_self1 [simp]: "b \<noteq> 0 \<Longrightarrow> (a + c * b) div b = c + a div b"
haftmann@25942
    27
begin
haftmann@25942
    28
haftmann@26100
    29
text {* @{const div} and @{const mod} *}
haftmann@26100
    30
haftmann@26062
    31
lemma mod_div_equality2: "b * (a div b) + a mod b = a"
haftmann@26062
    32
  unfolding mult_commute [of b]
haftmann@26062
    33
  by (rule mod_div_equality)
haftmann@26062
    34
huffman@29403
    35
lemma mod_div_equality': "a mod b + a div b * b = a"
huffman@29403
    36
  using mod_div_equality [of a b]
huffman@29403
    37
  by (simp only: add_ac)
huffman@29403
    38
haftmann@26062
    39
lemma div_mod_equality: "((a div b) * b + a mod b) + c = a + c"
nipkow@29667
    40
by (simp add: mod_div_equality)
haftmann@26062
    41
haftmann@26062
    42
lemma div_mod_equality2: "(b * (a div b) + a mod b) + c = a + c"
nipkow@29667
    43
by (simp add: mod_div_equality2)
haftmann@26062
    44
haftmann@27651
    45
lemma mod_by_0 [simp]: "a mod 0 = a"
nipkow@30180
    46
using mod_div_equality [of a zero] by simp
haftmann@27651
    47
haftmann@27651
    48
lemma mod_0 [simp]: "0 mod a = 0"
nipkow@30180
    49
using mod_div_equality [of zero a] div_0 by simp
haftmann@27651
    50
haftmann@27651
    51
lemma div_mult_self2 [simp]:
haftmann@27651
    52
  assumes "b \<noteq> 0"
haftmann@27651
    53
  shows "(a + b * c) div b = c + a div b"
haftmann@27651
    54
  using assms div_mult_self1 [of b a c] by (simp add: mult_commute)
haftmann@26100
    55
haftmann@27651
    56
lemma mod_mult_self1 [simp]: "(a + c * b) mod b = a mod b"
haftmann@27651
    57
proof (cases "b = 0")
haftmann@27651
    58
  case True then show ?thesis by simp
haftmann@27651
    59
next
haftmann@27651
    60
  case False
haftmann@27651
    61
  have "a + c * b = (a + c * b) div b * b + (a + c * b) mod b"
haftmann@27651
    62
    by (simp add: mod_div_equality)
haftmann@27651
    63
  also from False div_mult_self1 [of b a c] have
haftmann@27651
    64
    "\<dots> = (c + a div b) * b + (a + c * b) mod b"
nipkow@29667
    65
      by (simp add: algebra_simps)
haftmann@27651
    66
  finally have "a = a div b * b + (a + c * b) mod b"
haftmann@27651
    67
    by (simp add: add_commute [of a] add_assoc left_distrib)
haftmann@27651
    68
  then have "a div b * b + (a + c * b) mod b = a div b * b + a mod b"
haftmann@27651
    69
    by (simp add: mod_div_equality)
haftmann@27651
    70
  then show ?thesis by simp
haftmann@27651
    71
qed
haftmann@27651
    72
haftmann@27651
    73
lemma mod_mult_self2 [simp]: "(a + b * c) mod b = a mod b"
nipkow@29667
    74
by (simp add: mult_commute [of b])
haftmann@27651
    75
haftmann@27651
    76
lemma div_mult_self1_is_id [simp]: "b \<noteq> 0 \<Longrightarrow> b * a div b = a"
haftmann@27651
    77
  using div_mult_self2 [of b 0 a] by simp
haftmann@27651
    78
haftmann@27651
    79
lemma div_mult_self2_is_id [simp]: "b \<noteq> 0 \<Longrightarrow> a * b div b = a"
haftmann@27651
    80
  using div_mult_self1 [of b 0 a] by simp
haftmann@27651
    81
haftmann@27651
    82
lemma mod_mult_self1_is_0 [simp]: "b * a mod b = 0"
haftmann@27651
    83
  using mod_mult_self2 [of 0 b a] by simp
haftmann@27651
    84
haftmann@27651
    85
lemma mod_mult_self2_is_0 [simp]: "a * b mod b = 0"
haftmann@27651
    86
  using mod_mult_self1 [of 0 a b] by simp
haftmann@26062
    87
haftmann@27651
    88
lemma div_by_1 [simp]: "a div 1 = a"
haftmann@27651
    89
  using div_mult_self2_is_id [of 1 a] zero_neq_one by simp
haftmann@27651
    90
haftmann@27651
    91
lemma mod_by_1 [simp]: "a mod 1 = 0"
haftmann@27651
    92
proof -
haftmann@27651
    93
  from mod_div_equality [of a one] div_by_1 have "a + a mod 1 = a" by simp
haftmann@27651
    94
  then have "a + a mod 1 = a + 0" by simp
haftmann@27651
    95
  then show ?thesis by (rule add_left_imp_eq)
haftmann@27651
    96
qed
haftmann@27651
    97
haftmann@27651
    98
lemma mod_self [simp]: "a mod a = 0"
haftmann@27651
    99
  using mod_mult_self2_is_0 [of 1] by simp
haftmann@27651
   100
haftmann@27651
   101
lemma div_self [simp]: "a \<noteq> 0 \<Longrightarrow> a div a = 1"
haftmann@27651
   102
  using div_mult_self2_is_id [of _ 1] by simp
haftmann@27651
   103
haftmann@27676
   104
lemma div_add_self1 [simp]:
haftmann@27651
   105
  assumes "b \<noteq> 0"
haftmann@27651
   106
  shows "(b + a) div b = a div b + 1"
haftmann@27651
   107
  using assms div_mult_self1 [of b a 1] by (simp add: add_commute)
haftmann@26062
   108
haftmann@27676
   109
lemma div_add_self2 [simp]:
haftmann@27651
   110
  assumes "b \<noteq> 0"
haftmann@27651
   111
  shows "(a + b) div b = a div b + 1"
haftmann@27651
   112
  using assms div_add_self1 [of b a] by (simp add: add_commute)
haftmann@27651
   113
haftmann@27676
   114
lemma mod_add_self1 [simp]:
haftmann@27651
   115
  "(b + a) mod b = a mod b"
haftmann@27651
   116
  using mod_mult_self1 [of a 1 b] by (simp add: add_commute)
haftmann@27651
   117
haftmann@27676
   118
lemma mod_add_self2 [simp]:
haftmann@27651
   119
  "(a + b) mod b = a mod b"
haftmann@27651
   120
  using mod_mult_self1 [of a 1 b] by simp
haftmann@27651
   121
haftmann@27651
   122
lemma mod_div_decomp:
haftmann@27651
   123
  fixes a b
haftmann@27651
   124
  obtains q r where "q = a div b" and "r = a mod b"
haftmann@27651
   125
    and "a = q * b + r"
haftmann@27651
   126
proof -
haftmann@27651
   127
  from mod_div_equality have "a = a div b * b + a mod b" by simp
haftmann@27651
   128
  moreover have "a div b = a div b" ..
haftmann@27651
   129
  moreover have "a mod b = a mod b" ..
haftmann@27651
   130
  note that ultimately show thesis by blast
haftmann@27651
   131
qed
haftmann@27651
   132
nipkow@29108
   133
lemma dvd_eq_mod_eq_0 [code unfold]: "a dvd b \<longleftrightarrow> b mod a = 0"
haftmann@25942
   134
proof
haftmann@25942
   135
  assume "b mod a = 0"
haftmann@25942
   136
  with mod_div_equality [of b a] have "b div a * a = b" by simp
haftmann@25942
   137
  then have "b = a * (b div a)" unfolding mult_commute ..
haftmann@25942
   138
  then have "\<exists>c. b = a * c" ..
haftmann@25942
   139
  then show "a dvd b" unfolding dvd_def .
haftmann@25942
   140
next
haftmann@25942
   141
  assume "a dvd b"
haftmann@25942
   142
  then have "\<exists>c. b = a * c" unfolding dvd_def .
haftmann@25942
   143
  then obtain c where "b = a * c" ..
haftmann@25942
   144
  then have "b mod a = a * c mod a" by simp
haftmann@25942
   145
  then have "b mod a = c * a mod a" by (simp add: mult_commute)
haftmann@27651
   146
  then show "b mod a = 0" by simp
haftmann@25942
   147
qed
haftmann@25942
   148
huffman@29403
   149
lemma mod_div_trivial [simp]: "a mod b div b = 0"
huffman@29403
   150
proof (cases "b = 0")
huffman@29403
   151
  assume "b = 0"
huffman@29403
   152
  thus ?thesis by simp
huffman@29403
   153
next
huffman@29403
   154
  assume "b \<noteq> 0"
huffman@29403
   155
  hence "a div b + a mod b div b = (a mod b + a div b * b) div b"
huffman@29403
   156
    by (rule div_mult_self1 [symmetric])
huffman@29403
   157
  also have "\<dots> = a div b"
huffman@29403
   158
    by (simp only: mod_div_equality')
huffman@29403
   159
  also have "\<dots> = a div b + 0"
huffman@29403
   160
    by simp
huffman@29403
   161
  finally show ?thesis
huffman@29403
   162
    by (rule add_left_imp_eq)
huffman@29403
   163
qed
huffman@29403
   164
huffman@29403
   165
lemma mod_mod_trivial [simp]: "a mod b mod b = a mod b"
huffman@29403
   166
proof -
huffman@29403
   167
  have "a mod b mod b = (a mod b + a div b * b) mod b"
huffman@29403
   168
    by (simp only: mod_mult_self1)
huffman@29403
   169
  also have "\<dots> = a mod b"
huffman@29403
   170
    by (simp only: mod_div_equality')
huffman@29403
   171
  finally show ?thesis .
huffman@29403
   172
qed
huffman@29403
   173
nipkow@29925
   174
lemma dvd_imp_mod_0: "a dvd b \<Longrightarrow> b mod a = 0"
nipkow@29948
   175
by (rule dvd_eq_mod_eq_0[THEN iffD1])
nipkow@29925
   176
nipkow@29925
   177
lemma dvd_div_mult_self: "a dvd b \<Longrightarrow> (b div a) * a = b"
nipkow@29925
   178
by (subst (2) mod_div_equality [of b a, symmetric]) (simp add:dvd_imp_mod_0)
nipkow@29925
   179
nipkow@30052
   180
lemma dvd_div_mult: "a dvd b \<Longrightarrow> (b div a) * c = b * c div a"
nipkow@30052
   181
apply (cases "a = 0")
nipkow@30052
   182
 apply simp
nipkow@30052
   183
apply (auto simp: dvd_def mult_assoc)
nipkow@30052
   184
done
nipkow@30052
   185
nipkow@29925
   186
lemma div_dvd_div[simp]:
nipkow@29925
   187
  "a dvd b \<Longrightarrow> a dvd c \<Longrightarrow> (b div a dvd c div a) = (b dvd c)"
nipkow@29925
   188
apply (cases "a = 0")
nipkow@29925
   189
 apply simp
nipkow@29925
   190
apply (unfold dvd_def)
nipkow@29925
   191
apply auto
nipkow@29925
   192
 apply(blast intro:mult_assoc[symmetric])
nipkow@29925
   193
apply(fastsimp simp add: mult_assoc)
nipkow@29925
   194
done
nipkow@29925
   195
huffman@30078
   196
lemma dvd_mod_imp_dvd: "[| k dvd m mod n;  k dvd n |] ==> k dvd m"
huffman@30078
   197
  apply (subgoal_tac "k dvd (m div n) *n + m mod n")
huffman@30078
   198
   apply (simp add: mod_div_equality)
huffman@30078
   199
  apply (simp only: dvd_add dvd_mult)
huffman@30078
   200
  done
huffman@30078
   201
huffman@29403
   202
text {* Addition respects modular equivalence. *}
huffman@29403
   203
huffman@29403
   204
lemma mod_add_left_eq: "(a + b) mod c = (a mod c + b) mod c"
huffman@29403
   205
proof -
huffman@29403
   206
  have "(a + b) mod c = (a div c * c + a mod c + b) mod c"
huffman@29403
   207
    by (simp only: mod_div_equality)
huffman@29403
   208
  also have "\<dots> = (a mod c + b + a div c * c) mod c"
huffman@29403
   209
    by (simp only: add_ac)
huffman@29403
   210
  also have "\<dots> = (a mod c + b) mod c"
huffman@29403
   211
    by (rule mod_mult_self1)
huffman@29403
   212
  finally show ?thesis .
huffman@29403
   213
qed
huffman@29403
   214
huffman@29403
   215
lemma mod_add_right_eq: "(a + b) mod c = (a + b mod c) mod c"
huffman@29403
   216
proof -
huffman@29403
   217
  have "(a + b) mod c = (a + (b div c * c + b mod c)) mod c"
huffman@29403
   218
    by (simp only: mod_div_equality)
huffman@29403
   219
  also have "\<dots> = (a + b mod c + b div c * c) mod c"
huffman@29403
   220
    by (simp only: add_ac)
huffman@29403
   221
  also have "\<dots> = (a + b mod c) mod c"
huffman@29403
   222
    by (rule mod_mult_self1)
huffman@29403
   223
  finally show ?thesis .
huffman@29403
   224
qed
huffman@29403
   225
huffman@29403
   226
lemma mod_add_eq: "(a + b) mod c = (a mod c + b mod c) mod c"
huffman@29403
   227
by (rule trans [OF mod_add_left_eq mod_add_right_eq])
huffman@29403
   228
huffman@29403
   229
lemma mod_add_cong:
huffman@29403
   230
  assumes "a mod c = a' mod c"
huffman@29403
   231
  assumes "b mod c = b' mod c"
huffman@29403
   232
  shows "(a + b) mod c = (a' + b') mod c"
huffman@29403
   233
proof -
huffman@29403
   234
  have "(a mod c + b mod c) mod c = (a' mod c + b' mod c) mod c"
huffman@29403
   235
    unfolding assms ..
huffman@29403
   236
  thus ?thesis
huffman@29403
   237
    by (simp only: mod_add_eq [symmetric])
huffman@29403
   238
qed
huffman@29403
   239
haftmann@30923
   240
lemma div_add [simp]: "z dvd x \<Longrightarrow> z dvd y
nipkow@30837
   241
  \<Longrightarrow> (x + y) div z = x div z + y div z"
haftmann@30923
   242
by (cases "z = 0", simp, unfold dvd_def, auto simp add: algebra_simps)
nipkow@30837
   243
huffman@29403
   244
text {* Multiplication respects modular equivalence. *}
huffman@29403
   245
huffman@29403
   246
lemma mod_mult_left_eq: "(a * b) mod c = ((a mod c) * b) mod c"
huffman@29403
   247
proof -
huffman@29403
   248
  have "(a * b) mod c = ((a div c * c + a mod c) * b) mod c"
huffman@29403
   249
    by (simp only: mod_div_equality)
huffman@29403
   250
  also have "\<dots> = (a mod c * b + a div c * b * c) mod c"
nipkow@29667
   251
    by (simp only: algebra_simps)
huffman@29403
   252
  also have "\<dots> = (a mod c * b) mod c"
huffman@29403
   253
    by (rule mod_mult_self1)
huffman@29403
   254
  finally show ?thesis .
huffman@29403
   255
qed
huffman@29403
   256
huffman@29403
   257
lemma mod_mult_right_eq: "(a * b) mod c = (a * (b mod c)) mod c"
huffman@29403
   258
proof -
huffman@29403
   259
  have "(a * b) mod c = (a * (b div c * c + b mod c)) mod c"
huffman@29403
   260
    by (simp only: mod_div_equality)
huffman@29403
   261
  also have "\<dots> = (a * (b mod c) + a * (b div c) * c) mod c"
nipkow@29667
   262
    by (simp only: algebra_simps)
huffman@29403
   263
  also have "\<dots> = (a * (b mod c)) mod c"
huffman@29403
   264
    by (rule mod_mult_self1)
huffman@29403
   265
  finally show ?thesis .
huffman@29403
   266
qed
huffman@29403
   267
huffman@29403
   268
lemma mod_mult_eq: "(a * b) mod c = ((a mod c) * (b mod c)) mod c"
huffman@29403
   269
by (rule trans [OF mod_mult_left_eq mod_mult_right_eq])
huffman@29403
   270
huffman@29403
   271
lemma mod_mult_cong:
huffman@29403
   272
  assumes "a mod c = a' mod c"
huffman@29403
   273
  assumes "b mod c = b' mod c"
huffman@29403
   274
  shows "(a * b) mod c = (a' * b') mod c"
huffman@29403
   275
proof -
huffman@29403
   276
  have "(a mod c * (b mod c)) mod c = (a' mod c * (b' mod c)) mod c"
huffman@29403
   277
    unfolding assms ..
huffman@29403
   278
  thus ?thesis
huffman@29403
   279
    by (simp only: mod_mult_eq [symmetric])
huffman@29403
   280
qed
huffman@29403
   281
huffman@29404
   282
lemma mod_mod_cancel:
huffman@29404
   283
  assumes "c dvd b"
huffman@29404
   284
  shows "a mod b mod c = a mod c"
huffman@29404
   285
proof -
huffman@29404
   286
  from `c dvd b` obtain k where "b = c * k"
huffman@29404
   287
    by (rule dvdE)
huffman@29404
   288
  have "a mod b mod c = a mod (c * k) mod c"
huffman@29404
   289
    by (simp only: `b = c * k`)
huffman@29404
   290
  also have "\<dots> = (a mod (c * k) + a div (c * k) * k * c) mod c"
huffman@29404
   291
    by (simp only: mod_mult_self1)
huffman@29404
   292
  also have "\<dots> = (a div (c * k) * (c * k) + a mod (c * k)) mod c"
huffman@29404
   293
    by (simp only: add_ac mult_ac)
huffman@29404
   294
  also have "\<dots> = a mod c"
huffman@29404
   295
    by (simp only: mod_div_equality)
huffman@29404
   296
  finally show ?thesis .
huffman@29404
   297
qed
huffman@29404
   298
haftmann@25942
   299
end
haftmann@25942
   300
nipkow@30476
   301
lemma div_mult_div_if_dvd: "(y::'a::{semiring_div,no_zero_divisors}) dvd x \<Longrightarrow> 
nipkow@30476
   302
  z dvd w \<Longrightarrow> (x div y) * (w div z) = (x * w) div (y * z)"
nipkow@30476
   303
unfolding dvd_def
nipkow@30476
   304
  apply clarify
nipkow@30476
   305
  apply (case_tac "y = 0")
nipkow@30476
   306
  apply simp
nipkow@30476
   307
  apply (case_tac "z = 0")
nipkow@30476
   308
  apply simp
nipkow@30476
   309
  apply (simp add: algebra_simps)
nipkow@30476
   310
  apply (subst mult_assoc [symmetric])
nipkow@30476
   311
  apply (simp add: no_zero_divisors)
nipkow@30476
   312
done
nipkow@30476
   313
nipkow@30476
   314
nipkow@30476
   315
lemma div_power: "(y::'a::{semiring_div,no_zero_divisors,recpower}) dvd x \<Longrightarrow>
nipkow@30476
   316
    (x div y)^n = x^n div y^n"
nipkow@30476
   317
apply (induct n)
nipkow@30476
   318
 apply simp
nipkow@30476
   319
apply(simp add: div_mult_div_if_dvd dvd_power_same)
nipkow@30476
   320
done
nipkow@30476
   321
huffman@29405
   322
class ring_div = semiring_div + comm_ring_1
huffman@29405
   323
begin
huffman@29405
   324
huffman@29405
   325
text {* Negation respects modular equivalence. *}
huffman@29405
   326
huffman@29405
   327
lemma mod_minus_eq: "(- a) mod b = (- (a mod b)) mod b"
huffman@29405
   328
proof -
huffman@29405
   329
  have "(- a) mod b = (- (a div b * b + a mod b)) mod b"
huffman@29405
   330
    by (simp only: mod_div_equality)
huffman@29405
   331
  also have "\<dots> = (- (a mod b) + - (a div b) * b) mod b"
huffman@29405
   332
    by (simp only: minus_add_distrib minus_mult_left add_ac)
huffman@29405
   333
  also have "\<dots> = (- (a mod b)) mod b"
huffman@29405
   334
    by (rule mod_mult_self1)
huffman@29405
   335
  finally show ?thesis .
huffman@29405
   336
qed
huffman@29405
   337
huffman@29405
   338
lemma mod_minus_cong:
huffman@29405
   339
  assumes "a mod b = a' mod b"
huffman@29405
   340
  shows "(- a) mod b = (- a') mod b"
huffman@29405
   341
proof -
huffman@29405
   342
  have "(- (a mod b)) mod b = (- (a' mod b)) mod b"
huffman@29405
   343
    unfolding assms ..
huffman@29405
   344
  thus ?thesis
huffman@29405
   345
    by (simp only: mod_minus_eq [symmetric])
huffman@29405
   346
qed
huffman@29405
   347
huffman@29405
   348
text {* Subtraction respects modular equivalence. *}
huffman@29405
   349
huffman@29405
   350
lemma mod_diff_left_eq: "(a - b) mod c = (a mod c - b) mod c"
huffman@29405
   351
  unfolding diff_minus
huffman@29405
   352
  by (intro mod_add_cong mod_minus_cong) simp_all
huffman@29405
   353
huffman@29405
   354
lemma mod_diff_right_eq: "(a - b) mod c = (a - b mod c) mod c"
huffman@29405
   355
  unfolding diff_minus
huffman@29405
   356
  by (intro mod_add_cong mod_minus_cong) simp_all
huffman@29405
   357
huffman@29405
   358
lemma mod_diff_eq: "(a - b) mod c = (a mod c - b mod c) mod c"
huffman@29405
   359
  unfolding diff_minus
huffman@29405
   360
  by (intro mod_add_cong mod_minus_cong) simp_all
huffman@29405
   361
huffman@29405
   362
lemma mod_diff_cong:
huffman@29405
   363
  assumes "a mod c = a' mod c"
huffman@29405
   364
  assumes "b mod c = b' mod c"
huffman@29405
   365
  shows "(a - b) mod c = (a' - b') mod c"
huffman@29405
   366
  unfolding diff_minus using assms
huffman@29405
   367
  by (intro mod_add_cong mod_minus_cong)
huffman@29405
   368
nipkow@30180
   369
lemma dvd_neg_div: "y dvd x \<Longrightarrow> -x div y = - (x div y)"
nipkow@30180
   370
apply (case_tac "y = 0") apply simp
nipkow@30180
   371
apply (auto simp add: dvd_def)
nipkow@30180
   372
apply (subgoal_tac "-(y * k) = y * - k")
nipkow@30180
   373
 apply (erule ssubst)
nipkow@30180
   374
 apply (erule div_mult_self1_is_id)
nipkow@30180
   375
apply simp
nipkow@30180
   376
done
nipkow@30180
   377
nipkow@30180
   378
lemma dvd_div_neg: "y dvd x \<Longrightarrow> x div -y = - (x div y)"
nipkow@30180
   379
apply (case_tac "y = 0") apply simp
nipkow@30180
   380
apply (auto simp add: dvd_def)
nipkow@30180
   381
apply (subgoal_tac "y * k = -y * -k")
nipkow@30180
   382
 apply (erule ssubst)
nipkow@30180
   383
 apply (rule div_mult_self1_is_id)
nipkow@30180
   384
 apply simp
nipkow@30180
   385
apply simp
nipkow@30180
   386
done
nipkow@30180
   387
huffman@29405
   388
end
huffman@29405
   389
haftmann@25942
   390
haftmann@26100
   391
subsection {* Division on @{typ nat} *}
haftmann@26100
   392
haftmann@26100
   393
text {*
haftmann@26100
   394
  We define @{const div} and @{const mod} on @{typ nat} by means
haftmann@26100
   395
  of a characteristic relation with two input arguments
haftmann@26100
   396
  @{term "m\<Colon>nat"}, @{term "n\<Colon>nat"} and two output arguments
haftmann@26100
   397
  @{term "q\<Colon>nat"}(uotient) and @{term "r\<Colon>nat"}(emainder).
haftmann@26100
   398
*}
haftmann@26100
   399
haftmann@30923
   400
definition divmod_rel :: "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat \<Rightarrow> bool" where
haftmann@30923
   401
  "divmod_rel m n qr \<longleftrightarrow>
haftmann@30923
   402
    m = fst qr * n + snd qr \<and>
haftmann@30923
   403
      (if n = 0 then fst qr = 0 else if n > 0 then 0 \<le> snd qr \<and> snd qr < n else n < snd qr \<and> snd qr \<le> 0)"
haftmann@26100
   404
haftmann@26100
   405
text {* @{const divmod_rel} is total: *}
haftmann@26100
   406
haftmann@26100
   407
lemma divmod_rel_ex:
haftmann@30923
   408
  obtains q r where "divmod_rel m n (q, r)"
haftmann@26100
   409
proof (cases "n = 0")
haftmann@30923
   410
  case True  with that show thesis
haftmann@26100
   411
    by (auto simp add: divmod_rel_def)
haftmann@26100
   412
next
haftmann@26100
   413
  case False
haftmann@26100
   414
  have "\<exists>q r. m = q * n + r \<and> r < n"
haftmann@26100
   415
  proof (induct m)
haftmann@26100
   416
    case 0 with `n \<noteq> 0`
haftmann@26100
   417
    have "(0\<Colon>nat) = 0 * n + 0 \<and> 0 < n" by simp
haftmann@26100
   418
    then show ?case by blast
haftmann@26100
   419
  next
haftmann@26100
   420
    case (Suc m) then obtain q' r'
haftmann@26100
   421
      where m: "m = q' * n + r'" and n: "r' < n" by auto
haftmann@26100
   422
    then show ?case proof (cases "Suc r' < n")
haftmann@26100
   423
      case True
haftmann@26100
   424
      from m n have "Suc m = q' * n + Suc r'" by simp
haftmann@26100
   425
      with True show ?thesis by blast
haftmann@26100
   426
    next
haftmann@26100
   427
      case False then have "n \<le> Suc r'" by auto
haftmann@26100
   428
      moreover from n have "Suc r' \<le> n" by auto
haftmann@26100
   429
      ultimately have "n = Suc r'" by auto
haftmann@26100
   430
      with m have "Suc m = Suc q' * n + 0" by simp
haftmann@26100
   431
      with `n \<noteq> 0` show ?thesis by blast
haftmann@26100
   432
    qed
haftmann@26100
   433
  qed
haftmann@26100
   434
  with that show thesis
haftmann@26100
   435
    using `n \<noteq> 0` by (auto simp add: divmod_rel_def)
haftmann@26100
   436
qed
haftmann@26100
   437
haftmann@26100
   438
text {* @{const divmod_rel} is injective: *}
haftmann@26100
   439
haftmann@30923
   440
lemma divmod_rel_unique:
haftmann@30923
   441
  assumes "divmod_rel m n qr"
haftmann@30923
   442
    and "divmod_rel m n qr'"
haftmann@30923
   443
  shows "qr = qr'"
haftmann@26100
   444
proof (cases "n = 0")
haftmann@26100
   445
  case True with assms show ?thesis
haftmann@30923
   446
    by (cases qr, cases qr')
haftmann@30923
   447
      (simp add: divmod_rel_def)
haftmann@26100
   448
next
haftmann@26100
   449
  case False
haftmann@26100
   450
  have aux: "\<And>q r q' r'. q' * n + r' = q * n + r \<Longrightarrow> r < n \<Longrightarrow> q' \<le> (q\<Colon>nat)"
haftmann@26100
   451
  apply (rule leI)
haftmann@26100
   452
  apply (subst less_iff_Suc_add)
haftmann@26100
   453
  apply (auto simp add: add_mult_distrib)
haftmann@26100
   454
  done
haftmann@30923
   455
  from `n \<noteq> 0` assms have "fst qr = fst qr'"
haftmann@30923
   456
    by (auto simp add: divmod_rel_def intro: order_antisym dest: aux sym)
haftmann@30923
   457
  moreover from this assms have "snd qr = snd qr'"
haftmann@30923
   458
    by (simp add: divmod_rel_def)
haftmann@30923
   459
  ultimately show ?thesis by (cases qr, cases qr') simp
haftmann@26100
   460
qed
haftmann@26100
   461
haftmann@26100
   462
text {*
haftmann@26100
   463
  We instantiate divisibility on the natural numbers by
haftmann@26100
   464
  means of @{const divmod_rel}:
haftmann@26100
   465
*}
haftmann@25942
   466
haftmann@25942
   467
instantiation nat :: semiring_div
haftmann@25571
   468
begin
haftmann@25571
   469
haftmann@26100
   470
definition divmod :: "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat" where
haftmann@30923
   471
  [code del]: "divmod m n = (THE qr. divmod_rel m n qr)"
haftmann@30923
   472
haftmann@30923
   473
lemma divmod_rel_divmod:
haftmann@30923
   474
  "divmod_rel m n (divmod m n)"
haftmann@30923
   475
proof -
haftmann@30923
   476
  from divmod_rel_ex
haftmann@30923
   477
    obtain qr where rel: "divmod_rel m n qr" .
haftmann@30923
   478
  then show ?thesis
haftmann@30923
   479
  by (auto simp add: divmod_def intro: theI elim: divmod_rel_unique)
haftmann@30923
   480
qed
haftmann@30923
   481
haftmann@30923
   482
lemma divmod_eq:
haftmann@30923
   483
  assumes "divmod_rel m n qr" 
haftmann@30923
   484
  shows "divmod m n = qr"
haftmann@30923
   485
  using assms by (auto intro: divmod_rel_unique divmod_rel_divmod)
haftmann@26100
   486
haftmann@26100
   487
definition div_nat where
haftmann@26100
   488
  "m div n = fst (divmod m n)"
haftmann@26100
   489
haftmann@26100
   490
definition mod_nat where
haftmann@26100
   491
  "m mod n = snd (divmod m n)"
haftmann@25571
   492
haftmann@26100
   493
lemma divmod_div_mod:
haftmann@26100
   494
  "divmod m n = (m div n, m mod n)"
haftmann@26100
   495
  unfolding div_nat_def mod_nat_def by simp
haftmann@26100
   496
haftmann@26100
   497
lemma div_eq:
haftmann@30923
   498
  assumes "divmod_rel m n (q, r)" 
haftmann@26100
   499
  shows "m div n = q"
haftmann@30923
   500
  using assms by (auto dest: divmod_eq simp add: divmod_div_mod)
haftmann@26100
   501
haftmann@26100
   502
lemma mod_eq:
haftmann@30923
   503
  assumes "divmod_rel m n (q, r)" 
haftmann@26100
   504
  shows "m mod n = r"
haftmann@30923
   505
  using assms by (auto dest: divmod_eq simp add: divmod_div_mod)
haftmann@25571
   506
haftmann@30923
   507
lemma divmod_rel: "divmod_rel m n (m div n, m mod n)"
haftmann@30923
   508
  by (simp add: div_nat_def mod_nat_def divmod_rel_divmod)
paulson@14267
   509
haftmann@26100
   510
lemma divmod_zero:
haftmann@26100
   511
  "divmod m 0 = (0, m)"
haftmann@26100
   512
proof -
haftmann@26100
   513
  from divmod_rel [of m 0] show ?thesis
haftmann@26100
   514
    unfolding divmod_div_mod divmod_rel_def by simp
haftmann@26100
   515
qed
haftmann@25942
   516
haftmann@26100
   517
lemma divmod_base:
haftmann@26100
   518
  assumes "m < n"
haftmann@26100
   519
  shows "divmod m n = (0, m)"
haftmann@26100
   520
proof -
haftmann@26100
   521
  from divmod_rel [of m n] show ?thesis
haftmann@26100
   522
    unfolding divmod_div_mod divmod_rel_def
haftmann@26100
   523
    using assms by (cases "m div n = 0")
haftmann@26100
   524
      (auto simp add: gr0_conv_Suc [of "m div n"])
haftmann@26100
   525
qed
haftmann@25942
   526
haftmann@26100
   527
lemma divmod_step:
haftmann@26100
   528
  assumes "0 < n" and "n \<le> m"
haftmann@26100
   529
  shows "divmod m n = (Suc ((m - n) div n), (m - n) mod n)"
haftmann@26100
   530
proof -
haftmann@30923
   531
  from divmod_rel have divmod_m_n: "divmod_rel m n (m div n, m mod n)" .
haftmann@26100
   532
  with assms have m_div_n: "m div n \<ge> 1"
haftmann@26100
   533
    by (cases "m div n") (auto simp add: divmod_rel_def)
haftmann@30923
   534
  from assms divmod_m_n have "divmod_rel (m - n) n (m div n - Suc 0, m mod n)"
haftmann@26100
   535
    by (cases "m div n") (auto simp add: divmod_rel_def)
huffman@30079
   536
  with divmod_eq have "divmod (m - n) n = (m div n - Suc 0, m mod n)" by simp
haftmann@26100
   537
  moreover from divmod_div_mod have "divmod (m - n) n = ((m - n) div n, (m - n) mod n)" .
haftmann@26100
   538
  ultimately have "m div n = Suc ((m - n) div n)"
haftmann@26100
   539
    and "m mod n = (m - n) mod n" using m_div_n by simp_all
haftmann@26100
   540
  then show ?thesis using divmod_div_mod by simp
haftmann@26100
   541
qed
haftmann@25942
   542
wenzelm@26300
   543
text {* The ''recursion'' equations for @{const div} and @{const mod} *}
haftmann@26100
   544
haftmann@26100
   545
lemma div_less [simp]:
haftmann@26100
   546
  fixes m n :: nat
haftmann@26100
   547
  assumes "m < n"
haftmann@26100
   548
  shows "m div n = 0"
haftmann@26100
   549
  using assms divmod_base divmod_div_mod by simp
haftmann@25942
   550
haftmann@26100
   551
lemma le_div_geq:
haftmann@26100
   552
  fixes m n :: nat
haftmann@26100
   553
  assumes "0 < n" and "n \<le> m"
haftmann@26100
   554
  shows "m div n = Suc ((m - n) div n)"
haftmann@26100
   555
  using assms divmod_step divmod_div_mod by simp
paulson@14267
   556
haftmann@26100
   557
lemma mod_less [simp]:
haftmann@26100
   558
  fixes m n :: nat
haftmann@26100
   559
  assumes "m < n"
haftmann@26100
   560
  shows "m mod n = m"
haftmann@26100
   561
  using assms divmod_base divmod_div_mod by simp
haftmann@26100
   562
haftmann@26100
   563
lemma le_mod_geq:
haftmann@26100
   564
  fixes m n :: nat
haftmann@26100
   565
  assumes "n \<le> m"
haftmann@26100
   566
  shows "m mod n = (m - n) mod n"
haftmann@26100
   567
  using assms divmod_step divmod_div_mod by (cases "n = 0") simp_all
paulson@14267
   568
haftmann@25942
   569
instance proof
haftmann@26100
   570
  fix m n :: nat show "m div n * n + m mod n = m"
haftmann@26100
   571
    using divmod_rel [of m n] by (simp add: divmod_rel_def)
haftmann@25942
   572
next
haftmann@26100
   573
  fix n :: nat show "n div 0 = 0"
haftmann@26100
   574
    using divmod_zero divmod_div_mod [of n 0] by simp
haftmann@25942
   575
next
haftmann@27651
   576
  fix n :: nat show "0 div n = 0"
haftmann@27651
   577
    using divmod_rel [of 0 n] by (cases n) (simp_all add: divmod_rel_def)
haftmann@27651
   578
next
haftmann@27651
   579
  fix m n q :: nat assume "n \<noteq> 0" then show "(q + m * n) div n = m + q div n"
haftmann@25942
   580
    by (induct m) (simp_all add: le_div_geq)
haftmann@25942
   581
qed
haftmann@26100
   582
haftmann@25942
   583
end
paulson@14267
   584
haftmann@26100
   585
text {* Simproc for cancelling @{const div} and @{const mod} *}
haftmann@25942
   586
haftmann@27651
   587
(*lemmas mod_div_equality_nat = semiring_div_class.times_div_mod_plus_zero_one.mod_div_equality [of "m\<Colon>nat" n, standard]
haftmann@27651
   588
lemmas mod_div_equality2_nat = mod_div_equality2 [of "n\<Colon>nat" m, standard*)
haftmann@25942
   589
haftmann@25942
   590
ML {*
haftmann@25942
   591
structure CancelDivModData =
haftmann@25942
   592
struct
haftmann@25942
   593
haftmann@26100
   594
val div_name = @{const_name div};
haftmann@26100
   595
val mod_name = @{const_name mod};
haftmann@25942
   596
val mk_binop = HOLogic.mk_binop;
haftmann@30496
   597
val mk_sum = Nat_Arith.mk_sum;
haftmann@30496
   598
val dest_sum = Nat_Arith.dest_sum;
haftmann@25942
   599
haftmann@25942
   600
(*logic*)
paulson@14267
   601
haftmann@25942
   602
val div_mod_eqs = map mk_meta_eq [@{thm div_mod_equality}, @{thm div_mod_equality2}]
haftmann@25942
   603
haftmann@25942
   604
val trans = trans
haftmann@25942
   605
haftmann@25942
   606
val prove_eq_sums =
haftmann@25942
   607
  let val simps = @{thm add_0} :: @{thm add_0_right} :: @{thms add_ac}
haftmann@30496
   608
  in Arith_Data.prove_conv2 all_tac (Arith_Data.simp_all_tac simps) end;
haftmann@25942
   609
haftmann@25942
   610
end;
haftmann@25942
   611
haftmann@25942
   612
structure CancelDivMod = CancelDivModFun(CancelDivModData);
haftmann@25942
   613
wenzelm@28262
   614
val cancel_div_mod_proc = Simplifier.simproc (the_context ())
haftmann@26100
   615
  "cancel_div_mod" ["(m::nat) + n"] (K CancelDivMod.proc);
haftmann@25942
   616
haftmann@25942
   617
Addsimprocs[cancel_div_mod_proc];
haftmann@25942
   618
*}
haftmann@25942
   619
haftmann@26100
   620
text {* code generator setup *}
haftmann@26100
   621
haftmann@26100
   622
lemma divmod_if [code]: "divmod m n = (if n = 0 \<or> m < n then (0, m) else
haftmann@26100
   623
  let (q, r) = divmod (m - n) n in (Suc q, r))"
nipkow@29667
   624
by (simp add: divmod_zero divmod_base divmod_step)
haftmann@26100
   625
    (simp add: divmod_div_mod)
haftmann@26100
   626
haftmann@26100
   627
code_modulename SML
haftmann@26100
   628
  Divides Nat
haftmann@26100
   629
haftmann@26100
   630
code_modulename OCaml
haftmann@26100
   631
  Divides Nat
haftmann@26100
   632
haftmann@26100
   633
code_modulename Haskell
haftmann@26100
   634
  Divides Nat
haftmann@26100
   635
haftmann@26100
   636
haftmann@26100
   637
subsubsection {* Quotient *}
haftmann@26100
   638
haftmann@26100
   639
lemma div_geq: "0 < n \<Longrightarrow>  \<not> m < n \<Longrightarrow> m div n = Suc ((m - n) div n)"
nipkow@29667
   640
by (simp add: le_div_geq linorder_not_less)
haftmann@26100
   641
haftmann@26100
   642
lemma div_if: "0 < n \<Longrightarrow> m div n = (if m < n then 0 else Suc ((m - n) div n))"
nipkow@29667
   643
by (simp add: div_geq)
haftmann@26100
   644
haftmann@26100
   645
lemma div_mult_self_is_m [simp]: "0<n ==> (m*n) div n = (m::nat)"
nipkow@29667
   646
by simp
haftmann@26100
   647
haftmann@26100
   648
lemma div_mult_self1_is_m [simp]: "0<n ==> (n*m) div n = (m::nat)"
nipkow@29667
   649
by simp
haftmann@26100
   650
haftmann@25942
   651
haftmann@25942
   652
subsubsection {* Remainder *}
haftmann@25942
   653
haftmann@26100
   654
lemma mod_less_divisor [simp]:
haftmann@26100
   655
  fixes m n :: nat
haftmann@26100
   656
  assumes "n > 0"
haftmann@26100
   657
  shows "m mod n < (n::nat)"
haftmann@30923
   658
  using assms divmod_rel [of m n] unfolding divmod_rel_def by auto
paulson@14267
   659
haftmann@26100
   660
lemma mod_less_eq_dividend [simp]:
haftmann@26100
   661
  fixes m n :: nat
haftmann@26100
   662
  shows "m mod n \<le> m"
haftmann@26100
   663
proof (rule add_leD2)
haftmann@26100
   664
  from mod_div_equality have "m div n * n + m mod n = m" .
haftmann@26100
   665
  then show "m div n * n + m mod n \<le> m" by auto
haftmann@26100
   666
qed
haftmann@26100
   667
haftmann@26100
   668
lemma mod_geq: "\<not> m < (n\<Colon>nat) \<Longrightarrow> m mod n = (m - n) mod n"
nipkow@29667
   669
by (simp add: le_mod_geq linorder_not_less)
paulson@14267
   670
haftmann@26100
   671
lemma mod_if: "m mod (n\<Colon>nat) = (if m < n then m else (m - n) mod n)"
nipkow@29667
   672
by (simp add: le_mod_geq)
haftmann@26100
   673
paulson@14267
   674
lemma mod_1 [simp]: "m mod Suc 0 = 0"
nipkow@29667
   675
by (induct m) (simp_all add: mod_geq)
paulson@14267
   676
haftmann@26100
   677
lemma mod_mult_distrib: "(m mod n) * (k\<Colon>nat) = (m * k) mod (n * k)"
wenzelm@22718
   678
  apply (cases "n = 0", simp)
wenzelm@22718
   679
  apply (cases "k = 0", simp)
wenzelm@22718
   680
  apply (induct m rule: nat_less_induct)
wenzelm@22718
   681
  apply (subst mod_if, simp)
wenzelm@22718
   682
  apply (simp add: mod_geq diff_mult_distrib)
wenzelm@22718
   683
  done
paulson@14267
   684
paulson@14267
   685
lemma mod_mult_distrib2: "(k::nat) * (m mod n) = (k*m) mod (k*n)"
nipkow@29667
   686
by (simp add: mult_commute [of k] mod_mult_distrib)
paulson@14267
   687
paulson@14267
   688
(* a simple rearrangement of mod_div_equality: *)
paulson@14267
   689
lemma mult_div_cancel: "(n::nat) * (m div n) = m - (m mod n)"
nipkow@29667
   690
by (cut_tac a = m and b = n in mod_div_equality2, arith)
paulson@14267
   691
nipkow@15439
   692
lemma mod_le_divisor[simp]: "0 < n \<Longrightarrow> m mod n \<le> (n::nat)"
wenzelm@22718
   693
  apply (drule mod_less_divisor [where m = m])
wenzelm@22718
   694
  apply simp
wenzelm@22718
   695
  done
paulson@14267
   696
haftmann@26100
   697
subsubsection {* Quotient and Remainder *}
paulson@14267
   698
haftmann@26100
   699
lemma divmod_rel_mult1_eq:
haftmann@30923
   700
  "divmod_rel b c (q, r) \<Longrightarrow> c > 0
haftmann@30923
   701
   \<Longrightarrow> divmod_rel (a * b) c (a * q + a * r div c, a * r mod c)"
nipkow@29667
   702
by (auto simp add: split_ifs divmod_rel_def algebra_simps)
paulson@14267
   703
haftmann@30923
   704
lemma div_mult1_eq:
haftmann@30923
   705
  "(a * b) div c = a * (b div c) + a * (b mod c) div (c::nat)"
nipkow@25134
   706
apply (cases "c = 0", simp)
haftmann@26100
   707
apply (blast intro: divmod_rel [THEN divmod_rel_mult1_eq, THEN div_eq])
nipkow@25134
   708
done
paulson@14267
   709
haftmann@26100
   710
lemma divmod_rel_add1_eq:
haftmann@30923
   711
  "divmod_rel a c (aq, ar) \<Longrightarrow> divmod_rel b c (bq, br) \<Longrightarrow>  c > 0
haftmann@30923
   712
   \<Longrightarrow> divmod_rel (a + b) c (aq + bq + (ar + br) div c, (ar + br) mod c)"
nipkow@29667
   713
by (auto simp add: split_ifs divmod_rel_def algebra_simps)
paulson@14267
   714
paulson@14267
   715
(*NOT suitable for rewriting: the RHS has an instance of the LHS*)
paulson@14267
   716
lemma div_add1_eq:
nipkow@25134
   717
  "(a+b) div (c::nat) = a div c + b div c + ((a mod c + b mod c) div c)"
nipkow@25134
   718
apply (cases "c = 0", simp)
haftmann@26100
   719
apply (blast intro: divmod_rel_add1_eq [THEN div_eq] divmod_rel)
nipkow@25134
   720
done
paulson@14267
   721
paulson@14267
   722
lemma mod_lemma: "[| (0::nat) < c; r < b |] ==> b * (q mod c) + r < b * c"
wenzelm@22718
   723
  apply (cut_tac m = q and n = c in mod_less_divisor)
wenzelm@22718
   724
  apply (drule_tac [2] m = "q mod c" in less_imp_Suc_add, auto)
wenzelm@22718
   725
  apply (erule_tac P = "%x. ?lhs < ?rhs x" in ssubst)
wenzelm@22718
   726
  apply (simp add: add_mult_distrib2)
wenzelm@22718
   727
  done
paulson@10559
   728
haftmann@30923
   729
lemma divmod_rel_mult2_eq:
haftmann@30923
   730
  "divmod_rel a b (q, r) \<Longrightarrow> 0 < b \<Longrightarrow> 0 < c
haftmann@30923
   731
   \<Longrightarrow> divmod_rel a (b * c) (q div c, b *(q mod c) + r)"
nipkow@29667
   732
by (auto simp add: mult_ac divmod_rel_def add_mult_distrib2 [symmetric] mod_lemma)
paulson@14267
   733
paulson@14267
   734
lemma div_mult2_eq: "a div (b*c) = (a div b) div (c::nat)"
wenzelm@22718
   735
  apply (cases "b = 0", simp)
wenzelm@22718
   736
  apply (cases "c = 0", simp)
haftmann@26100
   737
  apply (force simp add: divmod_rel [THEN divmod_rel_mult2_eq, THEN div_eq])
wenzelm@22718
   738
  done
paulson@14267
   739
paulson@14267
   740
lemma mod_mult2_eq: "a mod (b*c) = b*(a div b mod c) + a mod (b::nat)"
wenzelm@22718
   741
  apply (cases "b = 0", simp)
wenzelm@22718
   742
  apply (cases "c = 0", simp)
haftmann@26100
   743
  apply (auto simp add: mult_commute divmod_rel [THEN divmod_rel_mult2_eq, THEN mod_eq])
wenzelm@22718
   744
  done
paulson@14267
   745
paulson@14267
   746
haftmann@25942
   747
subsubsection{*Cancellation of Common Factors in Division*}
paulson@14267
   748
paulson@14267
   749
lemma div_mult_mult_lemma:
wenzelm@22718
   750
    "[| (0::nat) < b;  0 < c |] ==> (c*a) div (c*b) = a div b"
nipkow@29667
   751
by (auto simp add: div_mult2_eq)
paulson@14267
   752
paulson@14267
   753
lemma div_mult_mult1 [simp]: "(0::nat) < c ==> (c*a) div (c*b) = a div b"
wenzelm@22718
   754
  apply (cases "b = 0")
wenzelm@22718
   755
  apply (auto simp add: linorder_neq_iff [of b] div_mult_mult_lemma)
wenzelm@22718
   756
  done
paulson@14267
   757
paulson@14267
   758
lemma div_mult_mult2 [simp]: "(0::nat) < c ==> (a*c) div (b*c) = a div b"
wenzelm@22718
   759
  apply (drule div_mult_mult1)
wenzelm@22718
   760
  apply (auto simp add: mult_commute)
wenzelm@22718
   761
  done
paulson@14267
   762
paulson@14267
   763
haftmann@25942
   764
subsubsection{*Further Facts about Quotient and Remainder*}
paulson@14267
   765
paulson@14267
   766
lemma div_1 [simp]: "m div Suc 0 = m"
nipkow@29667
   767
by (induct m) (simp_all add: div_geq)
paulson@14267
   768
paulson@14267
   769
paulson@14267
   770
(* Monotonicity of div in first argument *)
haftmann@30923
   771
lemma div_le_mono [rule_format (no_asm)]:
wenzelm@22718
   772
    "\<forall>m::nat. m \<le> n --> (m div k) \<le> (n div k)"
paulson@14267
   773
apply (case_tac "k=0", simp)
paulson@15251
   774
apply (induct "n" rule: nat_less_induct, clarify)
paulson@14267
   775
apply (case_tac "n<k")
paulson@14267
   776
(* 1  case n<k *)
paulson@14267
   777
apply simp
paulson@14267
   778
(* 2  case n >= k *)
paulson@14267
   779
apply (case_tac "m<k")
paulson@14267
   780
(* 2.1  case m<k *)
paulson@14267
   781
apply simp
paulson@14267
   782
(* 2.2  case m>=k *)
nipkow@15439
   783
apply (simp add: div_geq diff_le_mono)
paulson@14267
   784
done
paulson@14267
   785
paulson@14267
   786
(* Antimonotonicity of div in second argument *)
paulson@14267
   787
lemma div_le_mono2: "!!m::nat. [| 0<m; m\<le>n |] ==> (k div n) \<le> (k div m)"
paulson@14267
   788
apply (subgoal_tac "0<n")
wenzelm@22718
   789
 prefer 2 apply simp
paulson@15251
   790
apply (induct_tac k rule: nat_less_induct)
paulson@14267
   791
apply (rename_tac "k")
paulson@14267
   792
apply (case_tac "k<n", simp)
paulson@14267
   793
apply (subgoal_tac "~ (k<m) ")
wenzelm@22718
   794
 prefer 2 apply simp
paulson@14267
   795
apply (simp add: div_geq)
paulson@15251
   796
apply (subgoal_tac "(k-n) div n \<le> (k-m) div n")
paulson@14267
   797
 prefer 2
paulson@14267
   798
 apply (blast intro: div_le_mono diff_le_mono2)
paulson@14267
   799
apply (rule le_trans, simp)
nipkow@15439
   800
apply (simp)
paulson@14267
   801
done
paulson@14267
   802
paulson@14267
   803
lemma div_le_dividend [simp]: "m div n \<le> (m::nat)"
paulson@14267
   804
apply (case_tac "n=0", simp)
paulson@14267
   805
apply (subgoal_tac "m div n \<le> m div 1", simp)
paulson@14267
   806
apply (rule div_le_mono2)
paulson@14267
   807
apply (simp_all (no_asm_simp))
paulson@14267
   808
done
paulson@14267
   809
wenzelm@22718
   810
(* Similar for "less than" *)
paulson@17085
   811
lemma div_less_dividend [rule_format]:
paulson@14267
   812
     "!!n::nat. 1<n ==> 0 < m --> m div n < m"
paulson@15251
   813
apply (induct_tac m rule: nat_less_induct)
paulson@14267
   814
apply (rename_tac "m")
paulson@14267
   815
apply (case_tac "m<n", simp)
paulson@14267
   816
apply (subgoal_tac "0<n")
wenzelm@22718
   817
 prefer 2 apply simp
paulson@14267
   818
apply (simp add: div_geq)
paulson@14267
   819
apply (case_tac "n<m")
paulson@15251
   820
 apply (subgoal_tac "(m-n) div n < (m-n) ")
paulson@14267
   821
  apply (rule impI less_trans_Suc)+
paulson@14267
   822
apply assumption
nipkow@15439
   823
  apply (simp_all)
paulson@14267
   824
done
paulson@14267
   825
paulson@17085
   826
declare div_less_dividend [simp]
paulson@17085
   827
paulson@14267
   828
text{*A fact for the mutilated chess board*}
paulson@14267
   829
lemma mod_Suc: "Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))"
paulson@14267
   830
apply (case_tac "n=0", simp)
paulson@15251
   831
apply (induct "m" rule: nat_less_induct)
paulson@14267
   832
apply (case_tac "Suc (na) <n")
paulson@14267
   833
(* case Suc(na) < n *)
paulson@14267
   834
apply (frule lessI [THEN less_trans], simp add: less_not_refl3)
paulson@14267
   835
(* case n \<le> Suc(na) *)
paulson@16796
   836
apply (simp add: linorder_not_less le_Suc_eq mod_geq)
nipkow@15439
   837
apply (auto simp add: Suc_diff_le le_mod_geq)
paulson@14267
   838
done
paulson@14267
   839
paulson@14267
   840
haftmann@27651
   841
subsubsection {* The Divides Relation *}
paulson@24286
   842
paulson@14267
   843
lemma dvd_1_left [iff]: "Suc 0 dvd k"
wenzelm@22718
   844
  unfolding dvd_def by simp
paulson@14267
   845
paulson@14267
   846
lemma dvd_1_iff_1 [simp]: "(m dvd Suc 0) = (m = Suc 0)"
nipkow@29667
   847
by (simp add: dvd_def)
paulson@14267
   848
huffman@30079
   849
lemma nat_dvd_1_iff_1 [simp]: "m dvd (1::nat) \<longleftrightarrow> m = 1"
huffman@30079
   850
by (simp add: dvd_def)
huffman@30079
   851
paulson@14267
   852
lemma dvd_anti_sym: "[| m dvd n; n dvd m |] ==> m = (n::nat)"
wenzelm@22718
   853
  unfolding dvd_def
wenzelm@22718
   854
  by (force dest: mult_eq_self_implies_10 simp add: mult_assoc mult_eq_1_iff)
paulson@14267
   855
haftmann@23684
   856
text {* @{term "op dvd"} is a partial order *}
haftmann@23684
   857
wenzelm@30729
   858
interpretation dvd: order "op dvd" "\<lambda>n m \<Colon> nat. n dvd m \<and> \<not> m dvd n"
haftmann@28823
   859
  proof qed (auto intro: dvd_refl dvd_trans dvd_anti_sym)
paulson@14267
   860
nipkow@30042
   861
lemma nat_dvd_diff[simp]: "[| k dvd m; k dvd n |] ==> k dvd (m-n :: nat)"
nipkow@30042
   862
unfolding dvd_def
nipkow@30042
   863
by (blast intro: diff_mult_distrib2 [symmetric])
paulson@14267
   864
paulson@14267
   865
lemma dvd_diffD: "[| k dvd m-n; k dvd n; n\<le>m |] ==> k dvd (m::nat)"
wenzelm@22718
   866
  apply (erule linorder_not_less [THEN iffD2, THEN add_diff_inverse, THEN subst])
wenzelm@22718
   867
  apply (blast intro: dvd_add)
wenzelm@22718
   868
  done
paulson@14267
   869
paulson@14267
   870
lemma dvd_diffD1: "[| k dvd m-n; k dvd m; n\<le>m |] ==> k dvd (n::nat)"
nipkow@30042
   871
by (drule_tac m = m in nat_dvd_diff, auto)
paulson@14267
   872
paulson@14267
   873
lemma dvd_reduce: "(k dvd n + k) = (k dvd (n::nat))"
wenzelm@22718
   874
  apply (rule iffI)
wenzelm@22718
   875
   apply (erule_tac [2] dvd_add)
wenzelm@22718
   876
   apply (rule_tac [2] dvd_refl)
wenzelm@22718
   877
  apply (subgoal_tac "n = (n+k) -k")
wenzelm@22718
   878
   prefer 2 apply simp
wenzelm@22718
   879
  apply (erule ssubst)
nipkow@30042
   880
  apply (erule nat_dvd_diff)
wenzelm@22718
   881
  apply (rule dvd_refl)
wenzelm@22718
   882
  done
paulson@14267
   883
paulson@14267
   884
lemma dvd_mod: "!!n::nat. [| f dvd m; f dvd n |] ==> f dvd m mod n"
wenzelm@22718
   885
  unfolding dvd_def
wenzelm@22718
   886
  apply (case_tac "n = 0", auto)
wenzelm@22718
   887
  apply (blast intro: mod_mult_distrib2 [symmetric])
wenzelm@22718
   888
  done
paulson@14267
   889
paulson@14267
   890
lemma dvd_mod_iff: "k dvd n ==> ((k::nat) dvd m mod n) = (k dvd m)"
nipkow@29667
   891
by (blast intro: dvd_mod_imp_dvd dvd_mod)
paulson@14267
   892
paulson@14267
   893
lemma dvd_mult_cancel: "!!k::nat. [| k*m dvd k*n; 0<k |] ==> m dvd n"
wenzelm@22718
   894
  unfolding dvd_def
wenzelm@22718
   895
  apply (erule exE)
wenzelm@22718
   896
  apply (simp add: mult_ac)
wenzelm@22718
   897
  done
paulson@14267
   898
paulson@14267
   899
lemma dvd_mult_cancel1: "0<m ==> (m*n dvd m) = (n = (1::nat))"
wenzelm@22718
   900
  apply auto
wenzelm@22718
   901
   apply (subgoal_tac "m*n dvd m*1")
wenzelm@22718
   902
   apply (drule dvd_mult_cancel, auto)
wenzelm@22718
   903
  done
paulson@14267
   904
paulson@14267
   905
lemma dvd_mult_cancel2: "0<m ==> (n*m dvd m) = (n = (1::nat))"
wenzelm@22718
   906
  apply (subst mult_commute)
wenzelm@22718
   907
  apply (erule dvd_mult_cancel1)
wenzelm@22718
   908
  done
paulson@14267
   909
paulson@14267
   910
lemma dvd_imp_le: "[| k dvd n; 0 < n |] ==> k \<le> (n::nat)"
haftmann@30923
   911
  by (auto elim!: dvdE) (auto simp add: gr0_conv_Suc)
paulson@14267
   912
paulson@14267
   913
lemma dvd_mult_div_cancel: "n dvd m ==> n * (m div n) = (m::nat)"
haftmann@30923
   914
  by (simp add: dvd_eq_mod_eq_0 mult_div_cancel)
paulson@14267
   915
nipkow@25162
   916
lemma nat_zero_less_power_iff [simp]: "(x^n > 0) = (x > (0::nat) | n=0)"
wenzelm@22718
   917
  by (induct n) auto
haftmann@21408
   918
haftmann@21408
   919
lemma power_dvd_imp_le: "[|i^m dvd i^n;  (1::nat) < i|] ==> m \<le> n"
wenzelm@22718
   920
  apply (rule power_le_imp_le_exp, assumption)
wenzelm@22718
   921
  apply (erule dvd_imp_le, simp)
wenzelm@22718
   922
  done
haftmann@21408
   923
paulson@14267
   924
lemma mod_eq_0_iff: "(m mod d = 0) = (\<exists>q::nat. m = d*q)"
nipkow@29667
   925
by (auto simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)
paulson@17084
   926
wenzelm@22718
   927
lemmas mod_eq_0D [dest!] = mod_eq_0_iff [THEN iffD1]
paulson@14267
   928
paulson@14267
   929
(*Loses information, namely we also have r<d provided d is nonzero*)
paulson@14267
   930
lemma mod_eqD: "(m mod d = r) ==> \<exists>q::nat. m = r + q*d"
haftmann@27651
   931
  apply (cut_tac a = m in mod_div_equality)
wenzelm@22718
   932
  apply (simp only: add_ac)
wenzelm@22718
   933
  apply (blast intro: sym)
wenzelm@22718
   934
  done
paulson@14267
   935
nipkow@13152
   936
lemma split_div:
nipkow@13189
   937
 "P(n div k :: nat) =
nipkow@13189
   938
 ((k = 0 \<longrightarrow> P 0) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P i)))"
nipkow@13189
   939
 (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
nipkow@13189
   940
proof
nipkow@13189
   941
  assume P: ?P
nipkow@13189
   942
  show ?Q
nipkow@13189
   943
  proof (cases)
nipkow@13189
   944
    assume "k = 0"
haftmann@27651
   945
    with P show ?Q by simp
nipkow@13189
   946
  next
nipkow@13189
   947
    assume not0: "k \<noteq> 0"
nipkow@13189
   948
    thus ?Q
nipkow@13189
   949
    proof (simp, intro allI impI)
nipkow@13189
   950
      fix i j
nipkow@13189
   951
      assume n: "n = k*i + j" and j: "j < k"
nipkow@13189
   952
      show "P i"
nipkow@13189
   953
      proof (cases)
wenzelm@22718
   954
        assume "i = 0"
wenzelm@22718
   955
        with n j P show "P i" by simp
nipkow@13189
   956
      next
wenzelm@22718
   957
        assume "i \<noteq> 0"
wenzelm@22718
   958
        with not0 n j P show "P i" by(simp add:add_ac)
nipkow@13189
   959
      qed
nipkow@13189
   960
    qed
nipkow@13189
   961
  qed
nipkow@13189
   962
next
nipkow@13189
   963
  assume Q: ?Q
nipkow@13189
   964
  show ?P
nipkow@13189
   965
  proof (cases)
nipkow@13189
   966
    assume "k = 0"
haftmann@27651
   967
    with Q show ?P by simp
nipkow@13189
   968
  next
nipkow@13189
   969
    assume not0: "k \<noteq> 0"
nipkow@13189
   970
    with Q have R: ?R by simp
nipkow@13189
   971
    from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
nipkow@13517
   972
    show ?P by simp
nipkow@13189
   973
  qed
nipkow@13189
   974
qed
nipkow@13189
   975
berghofe@13882
   976
lemma split_div_lemma:
haftmann@26100
   977
  assumes "0 < n"
haftmann@26100
   978
  shows "n * q \<le> m \<and> m < n * Suc q \<longleftrightarrow> q = ((m\<Colon>nat) div n)" (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@26100
   979
proof
haftmann@26100
   980
  assume ?rhs
haftmann@26100
   981
  with mult_div_cancel have nq: "n * q = m - (m mod n)" by simp
haftmann@26100
   982
  then have A: "n * q \<le> m" by simp
haftmann@26100
   983
  have "n - (m mod n) > 0" using mod_less_divisor assms by auto
haftmann@26100
   984
  then have "m < m + (n - (m mod n))" by simp
haftmann@26100
   985
  then have "m < n + (m - (m mod n))" by simp
haftmann@26100
   986
  with nq have "m < n + n * q" by simp
haftmann@26100
   987
  then have B: "m < n * Suc q" by simp
haftmann@26100
   988
  from A B show ?lhs ..
haftmann@26100
   989
next
haftmann@26100
   990
  assume P: ?lhs
haftmann@30923
   991
  then have "divmod_rel m n (q, m - n * q)"
haftmann@26100
   992
    unfolding divmod_rel_def by (auto simp add: mult_ac)
haftmann@30923
   993
  with divmod_rel_unique divmod_rel [of m n]
haftmann@30923
   994
  have "(q, m - n * q) = (m div n, m mod n)" by auto
haftmann@30923
   995
  then show ?rhs by simp
haftmann@26100
   996
qed
berghofe@13882
   997
berghofe@13882
   998
theorem split_div':
berghofe@13882
   999
  "P ((m::nat) div n) = ((n = 0 \<and> P 0) \<or>
paulson@14267
  1000
   (\<exists>q. (n * q \<le> m \<and> m < n * (Suc q)) \<and> P q))"
berghofe@13882
  1001
  apply (case_tac "0 < n")
berghofe@13882
  1002
  apply (simp only: add: split_div_lemma)
haftmann@27651
  1003
  apply simp_all
berghofe@13882
  1004
  done
berghofe@13882
  1005
nipkow@13189
  1006
lemma split_mod:
nipkow@13189
  1007
 "P(n mod k :: nat) =
nipkow@13189
  1008
 ((k = 0 \<longrightarrow> P n) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P j)))"
nipkow@13189
  1009
 (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
nipkow@13189
  1010
proof
nipkow@13189
  1011
  assume P: ?P
nipkow@13189
  1012
  show ?Q
nipkow@13189
  1013
  proof (cases)
nipkow@13189
  1014
    assume "k = 0"
haftmann@27651
  1015
    with P show ?Q by simp
nipkow@13189
  1016
  next
nipkow@13189
  1017
    assume not0: "k \<noteq> 0"
nipkow@13189
  1018
    thus ?Q
nipkow@13189
  1019
    proof (simp, intro allI impI)
nipkow@13189
  1020
      fix i j
nipkow@13189
  1021
      assume "n = k*i + j" "j < k"
nipkow@13189
  1022
      thus "P j" using not0 P by(simp add:add_ac mult_ac)
nipkow@13189
  1023
    qed
nipkow@13189
  1024
  qed
nipkow@13189
  1025
next
nipkow@13189
  1026
  assume Q: ?Q
nipkow@13189
  1027
  show ?P
nipkow@13189
  1028
  proof (cases)
nipkow@13189
  1029
    assume "k = 0"
haftmann@27651
  1030
    with Q show ?P by simp
nipkow@13189
  1031
  next
nipkow@13189
  1032
    assume not0: "k \<noteq> 0"
nipkow@13189
  1033
    with Q have R: ?R by simp
nipkow@13189
  1034
    from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
nipkow@13517
  1035
    show ?P by simp
nipkow@13189
  1036
  qed
nipkow@13189
  1037
qed
nipkow@13189
  1038
berghofe@13882
  1039
theorem mod_div_equality': "(m::nat) mod n = m - (m div n) * n"
berghofe@13882
  1040
  apply (rule_tac P="%x. m mod n = x - (m div n) * n" in
berghofe@13882
  1041
    subst [OF mod_div_equality [of _ n]])
berghofe@13882
  1042
  apply arith
berghofe@13882
  1043
  done
berghofe@13882
  1044
haftmann@22800
  1045
lemma div_mod_equality':
haftmann@22800
  1046
  fixes m n :: nat
haftmann@22800
  1047
  shows "m div n * n = m - m mod n"
haftmann@22800
  1048
proof -
haftmann@22800
  1049
  have "m mod n \<le> m mod n" ..
haftmann@22800
  1050
  from div_mod_equality have 
haftmann@22800
  1051
    "m div n * n + m mod n - m mod n = m - m mod n" by simp
haftmann@22800
  1052
  with diff_add_assoc [OF `m mod n \<le> m mod n`, of "m div n * n"] have
haftmann@22800
  1053
    "m div n * n + (m mod n - m mod n) = m - m mod n"
haftmann@22800
  1054
    by simp
haftmann@22800
  1055
  then show ?thesis by simp
haftmann@22800
  1056
qed
haftmann@22800
  1057
haftmann@22800
  1058
haftmann@25942
  1059
subsubsection {*An ``induction'' law for modulus arithmetic.*}
paulson@14640
  1060
paulson@14640
  1061
lemma mod_induct_0:
paulson@14640
  1062
  assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
paulson@14640
  1063
  and base: "P i" and i: "i<p"
paulson@14640
  1064
  shows "P 0"
paulson@14640
  1065
proof (rule ccontr)
paulson@14640
  1066
  assume contra: "\<not>(P 0)"
paulson@14640
  1067
  from i have p: "0<p" by simp
paulson@14640
  1068
  have "\<forall>k. 0<k \<longrightarrow> \<not> P (p-k)" (is "\<forall>k. ?A k")
paulson@14640
  1069
  proof
paulson@14640
  1070
    fix k
paulson@14640
  1071
    show "?A k"
paulson@14640
  1072
    proof (induct k)
paulson@14640
  1073
      show "?A 0" by simp  -- "by contradiction"
paulson@14640
  1074
    next
paulson@14640
  1075
      fix n
paulson@14640
  1076
      assume ih: "?A n"
paulson@14640
  1077
      show "?A (Suc n)"
paulson@14640
  1078
      proof (clarsimp)
wenzelm@22718
  1079
        assume y: "P (p - Suc n)"
wenzelm@22718
  1080
        have n: "Suc n < p"
wenzelm@22718
  1081
        proof (rule ccontr)
wenzelm@22718
  1082
          assume "\<not>(Suc n < p)"
wenzelm@22718
  1083
          hence "p - Suc n = 0"
wenzelm@22718
  1084
            by simp
wenzelm@22718
  1085
          with y contra show "False"
wenzelm@22718
  1086
            by simp
wenzelm@22718
  1087
        qed
wenzelm@22718
  1088
        hence n2: "Suc (p - Suc n) = p-n" by arith
wenzelm@22718
  1089
        from p have "p - Suc n < p" by arith
wenzelm@22718
  1090
        with y step have z: "P ((Suc (p - Suc n)) mod p)"
wenzelm@22718
  1091
          by blast
wenzelm@22718
  1092
        show "False"
wenzelm@22718
  1093
        proof (cases "n=0")
wenzelm@22718
  1094
          case True
wenzelm@22718
  1095
          with z n2 contra show ?thesis by simp
wenzelm@22718
  1096
        next
wenzelm@22718
  1097
          case False
wenzelm@22718
  1098
          with p have "p-n < p" by arith
wenzelm@22718
  1099
          with z n2 False ih show ?thesis by simp
wenzelm@22718
  1100
        qed
paulson@14640
  1101
      qed
paulson@14640
  1102
    qed
paulson@14640
  1103
  qed
paulson@14640
  1104
  moreover
paulson@14640
  1105
  from i obtain k where "0<k \<and> i+k=p"
paulson@14640
  1106
    by (blast dest: less_imp_add_positive)
paulson@14640
  1107
  hence "0<k \<and> i=p-k" by auto
paulson@14640
  1108
  moreover
paulson@14640
  1109
  note base
paulson@14640
  1110
  ultimately
paulson@14640
  1111
  show "False" by blast
paulson@14640
  1112
qed
paulson@14640
  1113
paulson@14640
  1114
lemma mod_induct:
paulson@14640
  1115
  assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
paulson@14640
  1116
  and base: "P i" and i: "i<p" and j: "j<p"
paulson@14640
  1117
  shows "P j"
paulson@14640
  1118
proof -
paulson@14640
  1119
  have "\<forall>j<p. P j"
paulson@14640
  1120
  proof
paulson@14640
  1121
    fix j
paulson@14640
  1122
    show "j<p \<longrightarrow> P j" (is "?A j")
paulson@14640
  1123
    proof (induct j)
paulson@14640
  1124
      from step base i show "?A 0"
wenzelm@22718
  1125
        by (auto elim: mod_induct_0)
paulson@14640
  1126
    next
paulson@14640
  1127
      fix k
paulson@14640
  1128
      assume ih: "?A k"
paulson@14640
  1129
      show "?A (Suc k)"
paulson@14640
  1130
      proof
wenzelm@22718
  1131
        assume suc: "Suc k < p"
wenzelm@22718
  1132
        hence k: "k<p" by simp
wenzelm@22718
  1133
        with ih have "P k" ..
wenzelm@22718
  1134
        with step k have "P (Suc k mod p)"
wenzelm@22718
  1135
          by blast
wenzelm@22718
  1136
        moreover
wenzelm@22718
  1137
        from suc have "Suc k mod p = Suc k"
wenzelm@22718
  1138
          by simp
wenzelm@22718
  1139
        ultimately
wenzelm@22718
  1140
        show "P (Suc k)" by simp
paulson@14640
  1141
      qed
paulson@14640
  1142
    qed
paulson@14640
  1143
  qed
paulson@14640
  1144
  with j show ?thesis by blast
paulson@14640
  1145
qed
paulson@14640
  1146
haftmann@30923
  1147
lemma nat_dvd_not_less:
haftmann@30923
  1148
  fixes m n :: nat
haftmann@30923
  1149
  shows "0 < m \<Longrightarrow> m < n \<Longrightarrow> \<not> n dvd m"
haftmann@30923
  1150
by (auto elim!: dvdE) (auto simp add: gr0_conv_Suc)
haftmann@30923
  1151
paulson@3366
  1152
end