src/HOL/Divides.thy
author haftmann
Sun Jun 23 21:16:07 2013 +0200 (2013-06-23)
changeset 52435 6646bb548c6b
parent 52398 656e5e171f19
child 53066 1f61a923c2d6
permissions -rw-r--r--
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
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
huffman@47255
     9
imports Nat_Transfer
nipkow@15131
    10
begin
paulson@3366
    11
haftmann@25942
    12
subsection {* Syntactic division operations *}
haftmann@25942
    13
haftmann@27651
    14
class div = dvd +
haftmann@27540
    15
  fixes div :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "div" 70)
haftmann@27651
    16
    and mod :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "mod" 70)
haftmann@27540
    17
haftmann@27540
    18
haftmann@27651
    19
subsection {* Abstract division in commutative semirings. *}
haftmann@25942
    20
haftmann@30930
    21
class semiring_div = comm_semiring_1_cancel + no_zero_divisors + div +
haftmann@25942
    22
  assumes mod_div_equality: "a div b * b + a mod b = a"
haftmann@27651
    23
    and div_by_0 [simp]: "a div 0 = 0"
haftmann@27651
    24
    and div_0 [simp]: "0 div a = 0"
haftmann@27651
    25
    and div_mult_self1 [simp]: "b \<noteq> 0 \<Longrightarrow> (a + c * b) div b = c + a div b"
haftmann@30930
    26
    and div_mult_mult1 [simp]: "c \<noteq> 0 \<Longrightarrow> (c * a) div (c * b) = 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"
haftmann@30934
    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"
haftmann@30934
    43
  by (simp add: mod_div_equality2)
haftmann@26062
    44
haftmann@27651
    45
lemma mod_by_0 [simp]: "a mod 0 = a"
haftmann@30934
    46
  using mod_div_equality [of a zero] by simp
haftmann@27651
    47
haftmann@27651
    48
lemma mod_0 [simp]: "0 mod a = 0"
haftmann@30934
    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"
webertj@49962
    67
    by (simp add: add_commute [of a] add_assoc distrib_right)
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"
haftmann@30934
    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
bulwahn@45231
   133
lemma dvd_eq_mod_eq_0 [code]: "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
haftmann@33274
   180
lemma dvd_mult_div_cancel: "a dvd b \<Longrightarrow> a * (b div a) = b"
haftmann@33274
   181
by (drule dvd_div_mult_self) (simp add: mult_commute)
haftmann@33274
   182
nipkow@30052
   183
lemma dvd_div_mult: "a dvd b \<Longrightarrow> (b div a) * c = b * c div a"
nipkow@30052
   184
apply (cases "a = 0")
nipkow@30052
   185
 apply simp
nipkow@30052
   186
apply (auto simp: dvd_def mult_assoc)
nipkow@30052
   187
done
nipkow@30052
   188
nipkow@29925
   189
lemma div_dvd_div[simp]:
nipkow@29925
   190
  "a dvd b \<Longrightarrow> a dvd c \<Longrightarrow> (b div a dvd c div a) = (b dvd c)"
nipkow@29925
   191
apply (cases "a = 0")
nipkow@29925
   192
 apply simp
nipkow@29925
   193
apply (unfold dvd_def)
nipkow@29925
   194
apply auto
nipkow@29925
   195
 apply(blast intro:mult_assoc[symmetric])
nipkow@44890
   196
apply(fastforce simp add: mult_assoc)
nipkow@29925
   197
done
nipkow@29925
   198
huffman@30078
   199
lemma dvd_mod_imp_dvd: "[| k dvd m mod n;  k dvd n |] ==> k dvd m"
huffman@30078
   200
  apply (subgoal_tac "k dvd (m div n) *n + m mod n")
huffman@30078
   201
   apply (simp add: mod_div_equality)
huffman@30078
   202
  apply (simp only: dvd_add dvd_mult)
huffman@30078
   203
  done
huffman@30078
   204
huffman@29403
   205
text {* Addition respects modular equivalence. *}
huffman@29403
   206
huffman@29403
   207
lemma mod_add_left_eq: "(a + b) mod c = (a mod c + b) mod c"
huffman@29403
   208
proof -
huffman@29403
   209
  have "(a + b) mod c = (a div c * c + a mod c + b) mod c"
huffman@29403
   210
    by (simp only: mod_div_equality)
huffman@29403
   211
  also have "\<dots> = (a mod c + b + a div c * c) mod c"
huffman@29403
   212
    by (simp only: add_ac)
huffman@29403
   213
  also have "\<dots> = (a mod c + b) mod c"
huffman@29403
   214
    by (rule mod_mult_self1)
huffman@29403
   215
  finally show ?thesis .
huffman@29403
   216
qed
huffman@29403
   217
huffman@29403
   218
lemma mod_add_right_eq: "(a + b) mod c = (a + b mod c) mod c"
huffman@29403
   219
proof -
huffman@29403
   220
  have "(a + b) mod c = (a + (b div c * c + b mod c)) mod c"
huffman@29403
   221
    by (simp only: mod_div_equality)
huffman@29403
   222
  also have "\<dots> = (a + b mod c + b div c * c) mod c"
huffman@29403
   223
    by (simp only: add_ac)
huffman@29403
   224
  also have "\<dots> = (a + b mod c) mod c"
huffman@29403
   225
    by (rule mod_mult_self1)
huffman@29403
   226
  finally show ?thesis .
huffman@29403
   227
qed
huffman@29403
   228
huffman@29403
   229
lemma mod_add_eq: "(a + b) mod c = (a mod c + b mod c) mod c"
huffman@29403
   230
by (rule trans [OF mod_add_left_eq mod_add_right_eq])
huffman@29403
   231
huffman@29403
   232
lemma mod_add_cong:
huffman@29403
   233
  assumes "a mod c = a' mod c"
huffman@29403
   234
  assumes "b mod c = b' mod c"
huffman@29403
   235
  shows "(a + b) mod c = (a' + b') mod c"
huffman@29403
   236
proof -
huffman@29403
   237
  have "(a mod c + b mod c) mod c = (a' mod c + b' mod c) mod c"
huffman@29403
   238
    unfolding assms ..
huffman@29403
   239
  thus ?thesis
huffman@29403
   240
    by (simp only: mod_add_eq [symmetric])
huffman@29403
   241
qed
huffman@29403
   242
haftmann@30923
   243
lemma div_add [simp]: "z dvd x \<Longrightarrow> z dvd y
nipkow@30837
   244
  \<Longrightarrow> (x + y) div z = x div z + y div z"
haftmann@30923
   245
by (cases "z = 0", simp, unfold dvd_def, auto simp add: algebra_simps)
nipkow@30837
   246
huffman@29403
   247
text {* Multiplication respects modular equivalence. *}
huffman@29403
   248
huffman@29403
   249
lemma mod_mult_left_eq: "(a * b) mod c = ((a mod c) * b) mod c"
huffman@29403
   250
proof -
huffman@29403
   251
  have "(a * b) mod c = ((a div c * c + a mod c) * b) mod c"
huffman@29403
   252
    by (simp only: mod_div_equality)
huffman@29403
   253
  also have "\<dots> = (a mod c * b + a div c * b * c) mod c"
nipkow@29667
   254
    by (simp only: algebra_simps)
huffman@29403
   255
  also have "\<dots> = (a mod c * b) mod c"
huffman@29403
   256
    by (rule mod_mult_self1)
huffman@29403
   257
  finally show ?thesis .
huffman@29403
   258
qed
huffman@29403
   259
huffman@29403
   260
lemma mod_mult_right_eq: "(a * b) mod c = (a * (b mod c)) mod c"
huffman@29403
   261
proof -
huffman@29403
   262
  have "(a * b) mod c = (a * (b div c * c + b mod c)) mod c"
huffman@29403
   263
    by (simp only: mod_div_equality)
huffman@29403
   264
  also have "\<dots> = (a * (b mod c) + a * (b div c) * c) mod c"
nipkow@29667
   265
    by (simp only: algebra_simps)
huffman@29403
   266
  also have "\<dots> = (a * (b mod c)) mod c"
huffman@29403
   267
    by (rule mod_mult_self1)
huffman@29403
   268
  finally show ?thesis .
huffman@29403
   269
qed
huffman@29403
   270
huffman@29403
   271
lemma mod_mult_eq: "(a * b) mod c = ((a mod c) * (b mod c)) mod c"
huffman@29403
   272
by (rule trans [OF mod_mult_left_eq mod_mult_right_eq])
huffman@29403
   273
huffman@29403
   274
lemma mod_mult_cong:
huffman@29403
   275
  assumes "a mod c = a' mod c"
huffman@29403
   276
  assumes "b mod c = b' mod c"
huffman@29403
   277
  shows "(a * b) mod c = (a' * b') mod c"
huffman@29403
   278
proof -
huffman@29403
   279
  have "(a mod c * (b mod c)) mod c = (a' mod c * (b' mod c)) mod c"
huffman@29403
   280
    unfolding assms ..
huffman@29403
   281
  thus ?thesis
huffman@29403
   282
    by (simp only: mod_mult_eq [symmetric])
huffman@29403
   283
qed
huffman@29403
   284
huffman@47164
   285
text {* Exponentiation respects modular equivalence. *}
huffman@47164
   286
huffman@47164
   287
lemma power_mod: "(a mod b)^n mod b = a^n mod b"
huffman@47164
   288
apply (induct n, simp_all)
huffman@47164
   289
apply (rule mod_mult_right_eq [THEN trans])
huffman@47164
   290
apply (simp (no_asm_simp))
huffman@47164
   291
apply (rule mod_mult_eq [symmetric])
huffman@47164
   292
done
huffman@47164
   293
huffman@29404
   294
lemma mod_mod_cancel:
huffman@29404
   295
  assumes "c dvd b"
huffman@29404
   296
  shows "a mod b mod c = a mod c"
huffman@29404
   297
proof -
huffman@29404
   298
  from `c dvd b` obtain k where "b = c * k"
huffman@29404
   299
    by (rule dvdE)
huffman@29404
   300
  have "a mod b mod c = a mod (c * k) mod c"
huffman@29404
   301
    by (simp only: `b = c * k`)
huffman@29404
   302
  also have "\<dots> = (a mod (c * k) + a div (c * k) * k * c) mod c"
huffman@29404
   303
    by (simp only: mod_mult_self1)
huffman@29404
   304
  also have "\<dots> = (a div (c * k) * (c * k) + a mod (c * k)) mod c"
huffman@29404
   305
    by (simp only: add_ac mult_ac)
huffman@29404
   306
  also have "\<dots> = a mod c"
huffman@29404
   307
    by (simp only: mod_div_equality)
huffman@29404
   308
  finally show ?thesis .
huffman@29404
   309
qed
huffman@29404
   310
haftmann@30930
   311
lemma div_mult_div_if_dvd:
haftmann@30930
   312
  "y dvd x \<Longrightarrow> z dvd w \<Longrightarrow> (x div y) * (w div z) = (x * w) div (y * z)"
haftmann@30930
   313
  apply (cases "y = 0", simp)
haftmann@30930
   314
  apply (cases "z = 0", simp)
haftmann@30930
   315
  apply (auto elim!: dvdE simp add: algebra_simps)
nipkow@30476
   316
  apply (subst mult_assoc [symmetric])
nipkow@30476
   317
  apply (simp add: no_zero_divisors)
haftmann@30930
   318
  done
haftmann@30930
   319
haftmann@35367
   320
lemma div_mult_swap:
haftmann@35367
   321
  assumes "c dvd b"
haftmann@35367
   322
  shows "a * (b div c) = (a * b) div c"
haftmann@35367
   323
proof -
haftmann@35367
   324
  from assms have "b div c * (a div 1) = b * a div (c * 1)"
haftmann@35367
   325
    by (simp only: div_mult_div_if_dvd one_dvd)
haftmann@35367
   326
  then show ?thesis by (simp add: mult_commute)
haftmann@35367
   327
qed
haftmann@35367
   328
   
haftmann@30930
   329
lemma div_mult_mult2 [simp]:
haftmann@30930
   330
  "c \<noteq> 0 \<Longrightarrow> (a * c) div (b * c) = a div b"
haftmann@30930
   331
  by (drule div_mult_mult1) (simp add: mult_commute)
haftmann@30930
   332
haftmann@30930
   333
lemma div_mult_mult1_if [simp]:
haftmann@30930
   334
  "(c * a) div (c * b) = (if c = 0 then 0 else a div b)"
haftmann@30930
   335
  by simp_all
nipkow@30476
   336
haftmann@30930
   337
lemma mod_mult_mult1:
haftmann@30930
   338
  "(c * a) mod (c * b) = c * (a mod b)"
haftmann@30930
   339
proof (cases "c = 0")
haftmann@30930
   340
  case True then show ?thesis by simp
haftmann@30930
   341
next
haftmann@30930
   342
  case False
haftmann@30930
   343
  from mod_div_equality
haftmann@30930
   344
  have "((c * a) div (c * b)) * (c * b) + (c * a) mod (c * b) = c * a" .
haftmann@30930
   345
  with False have "c * ((a div b) * b + a mod b) + (c * a) mod (c * b)
haftmann@30930
   346
    = c * a + c * (a mod b)" by (simp add: algebra_simps)
haftmann@30930
   347
  with mod_div_equality show ?thesis by simp 
haftmann@30930
   348
qed
haftmann@30930
   349
  
haftmann@30930
   350
lemma mod_mult_mult2:
haftmann@30930
   351
  "(a * c) mod (b * c) = (a mod b) * c"
haftmann@30930
   352
  using mod_mult_mult1 [of c a b] by (simp add: mult_commute)
haftmann@30930
   353
huffman@47159
   354
lemma mult_mod_left: "(a mod b) * c = (a * c) mod (b * c)"
huffman@47159
   355
  by (fact mod_mult_mult2 [symmetric])
huffman@47159
   356
huffman@47159
   357
lemma mult_mod_right: "c * (a mod b) = (c * a) mod (c * b)"
huffman@47159
   358
  by (fact mod_mult_mult1 [symmetric])
huffman@47159
   359
huffman@31662
   360
lemma dvd_mod: "k dvd m \<Longrightarrow> k dvd n \<Longrightarrow> k dvd (m mod n)"
huffman@31662
   361
  unfolding dvd_def by (auto simp add: mod_mult_mult1)
huffman@31662
   362
huffman@31662
   363
lemma dvd_mod_iff: "k dvd n \<Longrightarrow> k dvd (m mod n) \<longleftrightarrow> k dvd m"
huffman@31662
   364
by (blast intro: dvd_mod_imp_dvd dvd_mod)
huffman@31662
   365
haftmann@31009
   366
lemma div_power:
huffman@31661
   367
  "y dvd x \<Longrightarrow> (x div y) ^ n = x ^ n div y ^ n"
nipkow@30476
   368
apply (induct n)
nipkow@30476
   369
 apply simp
nipkow@30476
   370
apply(simp add: div_mult_div_if_dvd dvd_power_same)
nipkow@30476
   371
done
nipkow@30476
   372
haftmann@35367
   373
lemma dvd_div_eq_mult:
haftmann@35367
   374
  assumes "a \<noteq> 0" and "a dvd b"  
haftmann@35367
   375
  shows "b div a = c \<longleftrightarrow> b = c * a"
haftmann@35367
   376
proof
haftmann@35367
   377
  assume "b = c * a"
haftmann@35367
   378
  then show "b div a = c" by (simp add: assms)
haftmann@35367
   379
next
haftmann@35367
   380
  assume "b div a = c"
haftmann@35367
   381
  then have "b div a * a = c * a" by simp
haftmann@35367
   382
  moreover from `a dvd b` have "b div a * a = b" by (simp add: dvd_div_mult_self)
haftmann@35367
   383
  ultimately show "b = c * a" by simp
haftmann@35367
   384
qed
haftmann@35367
   385
   
haftmann@35367
   386
lemma dvd_div_div_eq_mult:
haftmann@35367
   387
  assumes "a \<noteq> 0" "c \<noteq> 0" and "a dvd b" "c dvd d"
haftmann@35367
   388
  shows "b div a = d div c \<longleftrightarrow> b * c = a * d"
haftmann@35367
   389
  using assms by (auto simp add: mult_commute [of _ a] dvd_div_mult_self dvd_div_eq_mult div_mult_swap intro: sym)
haftmann@35367
   390
huffman@31661
   391
end
huffman@31661
   392
haftmann@35673
   393
class ring_div = semiring_div + comm_ring_1
huffman@29405
   394
begin
huffman@29405
   395
haftmann@36634
   396
subclass ring_1_no_zero_divisors ..
haftmann@36634
   397
huffman@29405
   398
text {* Negation respects modular equivalence. *}
huffman@29405
   399
huffman@29405
   400
lemma mod_minus_eq: "(- a) mod b = (- (a mod b)) mod b"
huffman@29405
   401
proof -
huffman@29405
   402
  have "(- a) mod b = (- (a div b * b + a mod b)) mod b"
huffman@29405
   403
    by (simp only: mod_div_equality)
huffman@29405
   404
  also have "\<dots> = (- (a mod b) + - (a div b) * b) mod b"
huffman@29405
   405
    by (simp only: minus_add_distrib minus_mult_left add_ac)
huffman@29405
   406
  also have "\<dots> = (- (a mod b)) mod b"
huffman@29405
   407
    by (rule mod_mult_self1)
huffman@29405
   408
  finally show ?thesis .
huffman@29405
   409
qed
huffman@29405
   410
huffman@29405
   411
lemma mod_minus_cong:
huffman@29405
   412
  assumes "a mod b = a' mod b"
huffman@29405
   413
  shows "(- a) mod b = (- a') mod b"
huffman@29405
   414
proof -
huffman@29405
   415
  have "(- (a mod b)) mod b = (- (a' mod b)) mod b"
huffman@29405
   416
    unfolding assms ..
huffman@29405
   417
  thus ?thesis
huffman@29405
   418
    by (simp only: mod_minus_eq [symmetric])
huffman@29405
   419
qed
huffman@29405
   420
huffman@29405
   421
text {* Subtraction respects modular equivalence. *}
huffman@29405
   422
huffman@29405
   423
lemma mod_diff_left_eq: "(a - b) mod c = (a mod c - b) mod c"
huffman@29405
   424
  unfolding diff_minus
huffman@29405
   425
  by (intro mod_add_cong mod_minus_cong) simp_all
huffman@29405
   426
huffman@29405
   427
lemma mod_diff_right_eq: "(a - b) mod c = (a - b mod c) mod c"
huffman@29405
   428
  unfolding diff_minus
huffman@29405
   429
  by (intro mod_add_cong mod_minus_cong) simp_all
huffman@29405
   430
huffman@29405
   431
lemma mod_diff_eq: "(a - b) mod c = (a mod c - b mod c) mod c"
huffman@29405
   432
  unfolding diff_minus
huffman@29405
   433
  by (intro mod_add_cong mod_minus_cong) simp_all
huffman@29405
   434
huffman@29405
   435
lemma mod_diff_cong:
huffman@29405
   436
  assumes "a mod c = a' mod c"
huffman@29405
   437
  assumes "b mod c = b' mod c"
huffman@29405
   438
  shows "(a - b) mod c = (a' - b') mod c"
huffman@29405
   439
  unfolding diff_minus using assms
huffman@29405
   440
  by (intro mod_add_cong mod_minus_cong)
huffman@29405
   441
nipkow@30180
   442
lemma dvd_neg_div: "y dvd x \<Longrightarrow> -x div y = - (x div y)"
nipkow@30180
   443
apply (case_tac "y = 0") apply simp
nipkow@30180
   444
apply (auto simp add: dvd_def)
nipkow@30180
   445
apply (subgoal_tac "-(y * k) = y * - k")
nipkow@30180
   446
 apply (erule ssubst)
nipkow@30180
   447
 apply (erule div_mult_self1_is_id)
nipkow@30180
   448
apply simp
nipkow@30180
   449
done
nipkow@30180
   450
nipkow@30180
   451
lemma dvd_div_neg: "y dvd x \<Longrightarrow> x div -y = - (x div y)"
nipkow@30180
   452
apply (case_tac "y = 0") apply simp
nipkow@30180
   453
apply (auto simp add: dvd_def)
nipkow@30180
   454
apply (subgoal_tac "y * k = -y * -k")
nipkow@30180
   455
 apply (erule ssubst)
nipkow@30180
   456
 apply (rule div_mult_self1_is_id)
nipkow@30180
   457
 apply simp
nipkow@30180
   458
apply simp
nipkow@30180
   459
done
nipkow@30180
   460
huffman@47159
   461
lemma div_minus_minus [simp]: "(-a) div (-b) = a div b"
huffman@47159
   462
  using div_mult_mult1 [of "- 1" a b]
huffman@47159
   463
  unfolding neg_equal_0_iff_equal by simp
huffman@47159
   464
huffman@47159
   465
lemma mod_minus_minus [simp]: "(-a) mod (-b) = - (a mod b)"
huffman@47159
   466
  using mod_mult_mult1 [of "- 1" a b] by simp
huffman@47159
   467
huffman@47159
   468
lemma div_minus_right: "a div (-b) = (-a) div b"
huffman@47159
   469
  using div_minus_minus [of "-a" b] by simp
huffman@47159
   470
huffman@47159
   471
lemma mod_minus_right: "a mod (-b) = - ((-a) mod b)"
huffman@47159
   472
  using mod_minus_minus [of "-a" b] by simp
huffman@47159
   473
huffman@47160
   474
lemma div_minus1_right [simp]: "a div (-1) = -a"
huffman@47160
   475
  using div_minus_right [of a 1] by simp
huffman@47160
   476
huffman@47160
   477
lemma mod_minus1_right [simp]: "a mod (-1) = 0"
huffman@47160
   478
  using mod_minus_right [of a 1] by simp
huffman@47160
   479
huffman@29405
   480
end
huffman@29405
   481
haftmann@25942
   482
haftmann@26100
   483
subsection {* Division on @{typ nat} *}
haftmann@26100
   484
haftmann@26100
   485
text {*
haftmann@26100
   486
  We define @{const div} and @{const mod} on @{typ nat} by means
haftmann@26100
   487
  of a characteristic relation with two input arguments
haftmann@26100
   488
  @{term "m\<Colon>nat"}, @{term "n\<Colon>nat"} and two output arguments
haftmann@26100
   489
  @{term "q\<Colon>nat"}(uotient) and @{term "r\<Colon>nat"}(emainder).
haftmann@26100
   490
*}
haftmann@26100
   491
haftmann@33340
   492
definition divmod_nat_rel :: "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat \<Rightarrow> bool" where
haftmann@33340
   493
  "divmod_nat_rel m n qr \<longleftrightarrow>
haftmann@30923
   494
    m = fst qr * n + snd qr \<and>
haftmann@30923
   495
      (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
   496
haftmann@33340
   497
text {* @{const divmod_nat_rel} is total: *}
haftmann@26100
   498
haftmann@33340
   499
lemma divmod_nat_rel_ex:
haftmann@33340
   500
  obtains q r where "divmod_nat_rel m n (q, r)"
haftmann@26100
   501
proof (cases "n = 0")
haftmann@30923
   502
  case True  with that show thesis
haftmann@33340
   503
    by (auto simp add: divmod_nat_rel_def)
haftmann@26100
   504
next
haftmann@26100
   505
  case False
haftmann@26100
   506
  have "\<exists>q r. m = q * n + r \<and> r < n"
haftmann@26100
   507
  proof (induct m)
haftmann@26100
   508
    case 0 with `n \<noteq> 0`
haftmann@26100
   509
    have "(0\<Colon>nat) = 0 * n + 0 \<and> 0 < n" by simp
haftmann@26100
   510
    then show ?case by blast
haftmann@26100
   511
  next
haftmann@26100
   512
    case (Suc m) then obtain q' r'
haftmann@26100
   513
      where m: "m = q' * n + r'" and n: "r' < n" by auto
haftmann@26100
   514
    then show ?case proof (cases "Suc r' < n")
haftmann@26100
   515
      case True
haftmann@26100
   516
      from m n have "Suc m = q' * n + Suc r'" by simp
haftmann@26100
   517
      with True show ?thesis by blast
haftmann@26100
   518
    next
haftmann@26100
   519
      case False then have "n \<le> Suc r'" by auto
haftmann@26100
   520
      moreover from n have "Suc r' \<le> n" by auto
haftmann@26100
   521
      ultimately have "n = Suc r'" by auto
haftmann@26100
   522
      with m have "Suc m = Suc q' * n + 0" by simp
haftmann@26100
   523
      with `n \<noteq> 0` show ?thesis by blast
haftmann@26100
   524
    qed
haftmann@26100
   525
  qed
haftmann@26100
   526
  with that show thesis
haftmann@33340
   527
    using `n \<noteq> 0` by (auto simp add: divmod_nat_rel_def)
haftmann@26100
   528
qed
haftmann@26100
   529
haftmann@33340
   530
text {* @{const divmod_nat_rel} is injective: *}
haftmann@26100
   531
haftmann@33340
   532
lemma divmod_nat_rel_unique:
haftmann@33340
   533
  assumes "divmod_nat_rel m n qr"
haftmann@33340
   534
    and "divmod_nat_rel m n qr'"
haftmann@30923
   535
  shows "qr = qr'"
haftmann@26100
   536
proof (cases "n = 0")
haftmann@26100
   537
  case True with assms show ?thesis
haftmann@30923
   538
    by (cases qr, cases qr')
haftmann@33340
   539
      (simp add: divmod_nat_rel_def)
haftmann@26100
   540
next
haftmann@26100
   541
  case False
haftmann@26100
   542
  have aux: "\<And>q r q' r'. q' * n + r' = q * n + r \<Longrightarrow> r < n \<Longrightarrow> q' \<le> (q\<Colon>nat)"
haftmann@26100
   543
  apply (rule leI)
haftmann@26100
   544
  apply (subst less_iff_Suc_add)
haftmann@26100
   545
  apply (auto simp add: add_mult_distrib)
haftmann@26100
   546
  done
haftmann@30923
   547
  from `n \<noteq> 0` assms have "fst qr = fst qr'"
haftmann@33340
   548
    by (auto simp add: divmod_nat_rel_def intro: order_antisym dest: aux sym)
haftmann@30923
   549
  moreover from this assms have "snd qr = snd qr'"
haftmann@33340
   550
    by (simp add: divmod_nat_rel_def)
haftmann@30923
   551
  ultimately show ?thesis by (cases qr, cases qr') simp
haftmann@26100
   552
qed
haftmann@26100
   553
haftmann@26100
   554
text {*
haftmann@26100
   555
  We instantiate divisibility on the natural numbers by
haftmann@33340
   556
  means of @{const divmod_nat_rel}:
haftmann@26100
   557
*}
haftmann@25942
   558
haftmann@33340
   559
definition divmod_nat :: "nat \<Rightarrow> nat \<Rightarrow> nat \<times> nat" where
haftmann@37767
   560
  "divmod_nat m n = (THE qr. divmod_nat_rel m n qr)"
haftmann@30923
   561
haftmann@33340
   562
lemma divmod_nat_rel_divmod_nat:
haftmann@33340
   563
  "divmod_nat_rel m n (divmod_nat m n)"
haftmann@30923
   564
proof -
haftmann@33340
   565
  from divmod_nat_rel_ex
haftmann@33340
   566
    obtain qr where rel: "divmod_nat_rel m n qr" .
haftmann@30923
   567
  then show ?thesis
haftmann@33340
   568
  by (auto simp add: divmod_nat_def intro: theI elim: divmod_nat_rel_unique)
haftmann@30923
   569
qed
haftmann@30923
   570
huffman@47135
   571
lemma divmod_nat_unique:
haftmann@33340
   572
  assumes "divmod_nat_rel m n qr" 
haftmann@33340
   573
  shows "divmod_nat m n = qr"
haftmann@33340
   574
  using assms by (auto intro: divmod_nat_rel_unique divmod_nat_rel_divmod_nat)
haftmann@26100
   575
huffman@46551
   576
instantiation nat :: semiring_div
huffman@46551
   577
begin
huffman@46551
   578
haftmann@26100
   579
definition div_nat where
haftmann@33340
   580
  "m div n = fst (divmod_nat m n)"
haftmann@26100
   581
huffman@46551
   582
lemma fst_divmod_nat [simp]:
huffman@46551
   583
  "fst (divmod_nat m n) = m div n"
huffman@46551
   584
  by (simp add: div_nat_def)
huffman@46551
   585
haftmann@26100
   586
definition mod_nat where
haftmann@33340
   587
  "m mod n = snd (divmod_nat m n)"
haftmann@25571
   588
huffman@46551
   589
lemma snd_divmod_nat [simp]:
huffman@46551
   590
  "snd (divmod_nat m n) = m mod n"
huffman@46551
   591
  by (simp add: mod_nat_def)
huffman@46551
   592
haftmann@33340
   593
lemma divmod_nat_div_mod:
haftmann@33340
   594
  "divmod_nat m n = (m div n, m mod n)"
huffman@46551
   595
  by (simp add: prod_eq_iff)
haftmann@26100
   596
huffman@47135
   597
lemma div_nat_unique:
haftmann@33340
   598
  assumes "divmod_nat_rel m n (q, r)" 
haftmann@26100
   599
  shows "m div n = q"
huffman@47135
   600
  using assms by (auto dest!: divmod_nat_unique simp add: prod_eq_iff)
huffman@47135
   601
huffman@47135
   602
lemma mod_nat_unique:
haftmann@33340
   603
  assumes "divmod_nat_rel m n (q, r)" 
haftmann@26100
   604
  shows "m mod n = r"
huffman@47135
   605
  using assms by (auto dest!: divmod_nat_unique simp add: prod_eq_iff)
haftmann@25571
   606
haftmann@33340
   607
lemma divmod_nat_rel: "divmod_nat_rel m n (m div n, m mod n)"
huffman@46551
   608
  using divmod_nat_rel_divmod_nat by (simp add: divmod_nat_div_mod)
paulson@14267
   609
huffman@47136
   610
lemma divmod_nat_zero: "divmod_nat m 0 = (0, m)"
huffman@47136
   611
  by (simp add: divmod_nat_unique divmod_nat_rel_def)
huffman@47136
   612
huffman@47136
   613
lemma divmod_nat_zero_left: "divmod_nat 0 n = (0, 0)"
huffman@47136
   614
  by (simp add: divmod_nat_unique divmod_nat_rel_def)
haftmann@25942
   615
huffman@47137
   616
lemma divmod_nat_base: "m < n \<Longrightarrow> divmod_nat m n = (0, m)"
huffman@47137
   617
  by (simp add: divmod_nat_unique divmod_nat_rel_def)
haftmann@25942
   618
haftmann@33340
   619
lemma divmod_nat_step:
haftmann@26100
   620
  assumes "0 < n" and "n \<le> m"
haftmann@33340
   621
  shows "divmod_nat m n = (Suc ((m - n) div n), (m - n) mod n)"
huffman@47135
   622
proof (rule divmod_nat_unique)
huffman@47134
   623
  have "divmod_nat_rel (m - n) n ((m - n) div n, (m - n) mod n)"
huffman@47134
   624
    by (rule divmod_nat_rel)
huffman@47134
   625
  thus "divmod_nat_rel m n (Suc ((m - n) div n), (m - n) mod n)"
huffman@47134
   626
    unfolding divmod_nat_rel_def using assms by auto
haftmann@26100
   627
qed
haftmann@25942
   628
wenzelm@26300
   629
text {* The ''recursion'' equations for @{const div} and @{const mod} *}
haftmann@26100
   630
haftmann@26100
   631
lemma div_less [simp]:
haftmann@26100
   632
  fixes m n :: nat
haftmann@26100
   633
  assumes "m < n"
haftmann@26100
   634
  shows "m div n = 0"
huffman@46551
   635
  using assms divmod_nat_base by (simp add: prod_eq_iff)
haftmann@25942
   636
haftmann@26100
   637
lemma le_div_geq:
haftmann@26100
   638
  fixes m n :: nat
haftmann@26100
   639
  assumes "0 < n" and "n \<le> m"
haftmann@26100
   640
  shows "m div n = Suc ((m - n) div n)"
huffman@46551
   641
  using assms divmod_nat_step by (simp add: prod_eq_iff)
paulson@14267
   642
haftmann@26100
   643
lemma mod_less [simp]:
haftmann@26100
   644
  fixes m n :: nat
haftmann@26100
   645
  assumes "m < n"
haftmann@26100
   646
  shows "m mod n = m"
huffman@46551
   647
  using assms divmod_nat_base by (simp add: prod_eq_iff)
haftmann@26100
   648
haftmann@26100
   649
lemma le_mod_geq:
haftmann@26100
   650
  fixes m n :: nat
haftmann@26100
   651
  assumes "n \<le> m"
haftmann@26100
   652
  shows "m mod n = (m - n) mod n"
huffman@46551
   653
  using assms divmod_nat_step by (cases "n = 0") (simp_all add: prod_eq_iff)
paulson@14267
   654
huffman@47136
   655
instance proof
huffman@47136
   656
  fix m n :: nat
huffman@47136
   657
  show "m div n * n + m mod n = m"
huffman@47136
   658
    using divmod_nat_rel [of m n] by (simp add: divmod_nat_rel_def)
huffman@47136
   659
next
huffman@47136
   660
  fix m n q :: nat
huffman@47136
   661
  assume "n \<noteq> 0"
huffman@47136
   662
  then show "(q + m * n) div n = m + q div n"
huffman@47136
   663
    by (induct m) (simp_all add: le_div_geq)
huffman@47136
   664
next
huffman@47136
   665
  fix m n q :: nat
huffman@47136
   666
  assume "m \<noteq> 0"
huffman@47136
   667
  hence "\<And>a b. divmod_nat_rel n q (a, b) \<Longrightarrow> divmod_nat_rel (m * n) (m * q) (a, m * b)"
huffman@47136
   668
    unfolding divmod_nat_rel_def
huffman@47136
   669
    by (auto split: split_if_asm, simp_all add: algebra_simps)
huffman@47136
   670
  moreover from divmod_nat_rel have "divmod_nat_rel n q (n div q, n mod q)" .
huffman@47136
   671
  ultimately have "divmod_nat_rel (m * n) (m * q) (n div q, m * (n mod q))" .
huffman@47136
   672
  thus "(m * n) div (m * q) = n div q" by (rule div_nat_unique)
huffman@47136
   673
next
huffman@47136
   674
  fix n :: nat show "n div 0 = 0"
haftmann@33340
   675
    by (simp add: div_nat_def divmod_nat_zero)
huffman@47136
   676
next
huffman@47136
   677
  fix n :: nat show "0 div n = 0"
huffman@47136
   678
    by (simp add: div_nat_def divmod_nat_zero_left)
haftmann@25942
   679
qed
haftmann@26100
   680
haftmann@25942
   681
end
paulson@14267
   682
haftmann@33361
   683
lemma divmod_nat_if [code]: "divmod_nat m n = (if n = 0 \<or> m < n then (0, m) else
haftmann@33361
   684
  let (q, r) = divmod_nat (m - n) n in (Suc q, r))"
huffman@46551
   685
  by (simp add: prod_eq_iff prod_case_beta not_less le_div_geq le_mod_geq)
haftmann@33361
   686
haftmann@26100
   687
text {* Simproc for cancelling @{const div} and @{const mod} *}
haftmann@25942
   688
wenzelm@51299
   689
ML_file "~~/src/Provers/Arith/cancel_div_mod.ML"
wenzelm@51299
   690
haftmann@30934
   691
ML {*
wenzelm@43594
   692
structure Cancel_Div_Mod_Nat = Cancel_Div_Mod
wenzelm@41550
   693
(
haftmann@30934
   694
  val div_name = @{const_name div};
haftmann@30934
   695
  val mod_name = @{const_name mod};
haftmann@30934
   696
  val mk_binop = HOLogic.mk_binop;
huffman@48561
   697
  val mk_plus = HOLogic.mk_binop @{const_name Groups.plus};
huffman@48561
   698
  val dest_plus = HOLogic.dest_bin @{const_name Groups.plus} HOLogic.natT;
huffman@48561
   699
  fun mk_sum [] = HOLogic.zero
huffman@48561
   700
    | mk_sum [t] = t
huffman@48561
   701
    | mk_sum (t :: ts) = mk_plus (t, mk_sum ts);
huffman@48561
   702
  fun dest_sum tm =
huffman@48561
   703
    if HOLogic.is_zero tm then []
huffman@48561
   704
    else
huffman@48561
   705
      (case try HOLogic.dest_Suc tm of
huffman@48561
   706
        SOME t => HOLogic.Suc_zero :: dest_sum t
huffman@48561
   707
      | NONE =>
huffman@48561
   708
          (case try dest_plus tm of
huffman@48561
   709
            SOME (t, u) => dest_sum t @ dest_sum u
huffman@48561
   710
          | NONE => [tm]));
haftmann@25942
   711
haftmann@30934
   712
  val div_mod_eqs = map mk_meta_eq [@{thm div_mod_equality}, @{thm div_mod_equality2}];
paulson@14267
   713
haftmann@30934
   714
  val prove_eq_sums = Arith_Data.prove_conv2 all_tac (Arith_Data.simp_all_tac
haftmann@35050
   715
    (@{thm add_0_left} :: @{thm add_0_right} :: @{thms add_ac}))
wenzelm@41550
   716
)
haftmann@25942
   717
*}
haftmann@25942
   718
wenzelm@43594
   719
simproc_setup cancel_div_mod_nat ("(m::nat) + n") = {* K Cancel_Div_Mod_Nat.proc *}
wenzelm@43594
   720
haftmann@26100
   721
haftmann@26100
   722
subsubsection {* Quotient *}
haftmann@26100
   723
haftmann@26100
   724
lemma div_geq: "0 < n \<Longrightarrow>  \<not> m < n \<Longrightarrow> m div n = Suc ((m - n) div n)"
nipkow@29667
   725
by (simp add: le_div_geq linorder_not_less)
haftmann@26100
   726
haftmann@26100
   727
lemma div_if: "0 < n \<Longrightarrow> m div n = (if m < n then 0 else Suc ((m - n) div n))"
nipkow@29667
   728
by (simp add: div_geq)
haftmann@26100
   729
haftmann@26100
   730
lemma div_mult_self_is_m [simp]: "0<n ==> (m*n) div n = (m::nat)"
nipkow@29667
   731
by simp
haftmann@26100
   732
haftmann@26100
   733
lemma div_mult_self1_is_m [simp]: "0<n ==> (n*m) div n = (m::nat)"
nipkow@29667
   734
by simp
haftmann@26100
   735
haftmann@25942
   736
haftmann@25942
   737
subsubsection {* Remainder *}
haftmann@25942
   738
haftmann@26100
   739
lemma mod_less_divisor [simp]:
haftmann@26100
   740
  fixes m n :: nat
haftmann@26100
   741
  assumes "n > 0"
haftmann@26100
   742
  shows "m mod n < (n::nat)"
haftmann@33340
   743
  using assms divmod_nat_rel [of m n] unfolding divmod_nat_rel_def by auto
paulson@14267
   744
haftmann@51173
   745
lemma mod_Suc_le_divisor [simp]:
haftmann@51173
   746
  "m mod Suc n \<le> n"
haftmann@51173
   747
  using mod_less_divisor [of "Suc n" m] by arith
haftmann@51173
   748
haftmann@26100
   749
lemma mod_less_eq_dividend [simp]:
haftmann@26100
   750
  fixes m n :: nat
haftmann@26100
   751
  shows "m mod n \<le> m"
haftmann@26100
   752
proof (rule add_leD2)
haftmann@26100
   753
  from mod_div_equality have "m div n * n + m mod n = m" .
haftmann@26100
   754
  then show "m div n * n + m mod n \<le> m" by auto
haftmann@26100
   755
qed
haftmann@26100
   756
haftmann@26100
   757
lemma mod_geq: "\<not> m < (n\<Colon>nat) \<Longrightarrow> m mod n = (m - n) mod n"
nipkow@29667
   758
by (simp add: le_mod_geq linorder_not_less)
paulson@14267
   759
haftmann@26100
   760
lemma mod_if: "m mod (n\<Colon>nat) = (if m < n then m else (m - n) mod n)"
nipkow@29667
   761
by (simp add: le_mod_geq)
haftmann@26100
   762
paulson@14267
   763
lemma mod_1 [simp]: "m mod Suc 0 = 0"
nipkow@29667
   764
by (induct m) (simp_all add: mod_geq)
paulson@14267
   765
paulson@14267
   766
(* a simple rearrangement of mod_div_equality: *)
paulson@14267
   767
lemma mult_div_cancel: "(n::nat) * (m div n) = m - (m mod n)"
huffman@47138
   768
  using mod_div_equality2 [of n m] by arith
paulson@14267
   769
nipkow@15439
   770
lemma mod_le_divisor[simp]: "0 < n \<Longrightarrow> m mod n \<le> (n::nat)"
wenzelm@22718
   771
  apply (drule mod_less_divisor [where m = m])
wenzelm@22718
   772
  apply simp
wenzelm@22718
   773
  done
paulson@14267
   774
haftmann@26100
   775
subsubsection {* Quotient and Remainder *}
paulson@14267
   776
haftmann@33340
   777
lemma divmod_nat_rel_mult1_eq:
bulwahn@46552
   778
  "divmod_nat_rel b c (q, r)
haftmann@33340
   779
   \<Longrightarrow> divmod_nat_rel (a * b) c (a * q + a * r div c, a * r mod c)"
haftmann@33340
   780
by (auto simp add: split_ifs divmod_nat_rel_def algebra_simps)
paulson@14267
   781
haftmann@30923
   782
lemma div_mult1_eq:
haftmann@30923
   783
  "(a * b) div c = a * (b div c) + a * (b mod c) div (c::nat)"
huffman@47135
   784
by (blast intro: divmod_nat_rel_mult1_eq [THEN div_nat_unique] divmod_nat_rel)
paulson@14267
   785
haftmann@33340
   786
lemma divmod_nat_rel_add1_eq:
bulwahn@46552
   787
  "divmod_nat_rel a c (aq, ar) \<Longrightarrow> divmod_nat_rel b c (bq, br)
haftmann@33340
   788
   \<Longrightarrow> divmod_nat_rel (a + b) c (aq + bq + (ar + br) div c, (ar + br) mod c)"
haftmann@33340
   789
by (auto simp add: split_ifs divmod_nat_rel_def algebra_simps)
paulson@14267
   790
paulson@14267
   791
(*NOT suitable for rewriting: the RHS has an instance of the LHS*)
paulson@14267
   792
lemma div_add1_eq:
nipkow@25134
   793
  "(a+b) div (c::nat) = a div c + b div c + ((a mod c + b mod c) div c)"
huffman@47135
   794
by (blast intro: divmod_nat_rel_add1_eq [THEN div_nat_unique] divmod_nat_rel)
paulson@14267
   795
paulson@14267
   796
lemma mod_lemma: "[| (0::nat) < c; r < b |] ==> b * (q mod c) + r < b * c"
wenzelm@22718
   797
  apply (cut_tac m = q and n = c in mod_less_divisor)
wenzelm@22718
   798
  apply (drule_tac [2] m = "q mod c" in less_imp_Suc_add, auto)
wenzelm@22718
   799
  apply (erule_tac P = "%x. ?lhs < ?rhs x" in ssubst)
wenzelm@22718
   800
  apply (simp add: add_mult_distrib2)
wenzelm@22718
   801
  done
paulson@10559
   802
haftmann@33340
   803
lemma divmod_nat_rel_mult2_eq:
bulwahn@46552
   804
  "divmod_nat_rel a b (q, r)
haftmann@33340
   805
   \<Longrightarrow> divmod_nat_rel a (b * c) (q div c, b *(q mod c) + r)"
haftmann@33340
   806
by (auto simp add: mult_ac divmod_nat_rel_def add_mult_distrib2 [symmetric] mod_lemma)
paulson@14267
   807
paulson@14267
   808
lemma div_mult2_eq: "a div (b*c) = (a div b) div (c::nat)"
huffman@47135
   809
by (force simp add: divmod_nat_rel [THEN divmod_nat_rel_mult2_eq, THEN div_nat_unique])
paulson@14267
   810
paulson@14267
   811
lemma mod_mult2_eq: "a mod (b*c) = b*(a div b mod c) + a mod (b::nat)"
huffman@47135
   812
by (auto simp add: mult_commute divmod_nat_rel [THEN divmod_nat_rel_mult2_eq, THEN mod_nat_unique])
paulson@14267
   813
paulson@14267
   814
huffman@46551
   815
subsubsection {* Further Facts about Quotient and Remainder *}
paulson@14267
   816
paulson@14267
   817
lemma div_1 [simp]: "m div Suc 0 = m"
nipkow@29667
   818
by (induct m) (simp_all add: div_geq)
paulson@14267
   819
paulson@14267
   820
(* Monotonicity of div in first argument *)
haftmann@30923
   821
lemma div_le_mono [rule_format (no_asm)]:
wenzelm@22718
   822
    "\<forall>m::nat. m \<le> n --> (m div k) \<le> (n div k)"
paulson@14267
   823
apply (case_tac "k=0", simp)
paulson@15251
   824
apply (induct "n" rule: nat_less_induct, clarify)
paulson@14267
   825
apply (case_tac "n<k")
paulson@14267
   826
(* 1  case n<k *)
paulson@14267
   827
apply simp
paulson@14267
   828
(* 2  case n >= k *)
paulson@14267
   829
apply (case_tac "m<k")
paulson@14267
   830
(* 2.1  case m<k *)
paulson@14267
   831
apply simp
paulson@14267
   832
(* 2.2  case m>=k *)
nipkow@15439
   833
apply (simp add: div_geq diff_le_mono)
paulson@14267
   834
done
paulson@14267
   835
paulson@14267
   836
(* Antimonotonicity of div in second argument *)
paulson@14267
   837
lemma div_le_mono2: "!!m::nat. [| 0<m; m\<le>n |] ==> (k div n) \<le> (k div m)"
paulson@14267
   838
apply (subgoal_tac "0<n")
wenzelm@22718
   839
 prefer 2 apply simp
paulson@15251
   840
apply (induct_tac k rule: nat_less_induct)
paulson@14267
   841
apply (rename_tac "k")
paulson@14267
   842
apply (case_tac "k<n", simp)
paulson@14267
   843
apply (subgoal_tac "~ (k<m) ")
wenzelm@22718
   844
 prefer 2 apply simp
paulson@14267
   845
apply (simp add: div_geq)
paulson@15251
   846
apply (subgoal_tac "(k-n) div n \<le> (k-m) div n")
paulson@14267
   847
 prefer 2
paulson@14267
   848
 apply (blast intro: div_le_mono diff_le_mono2)
paulson@14267
   849
apply (rule le_trans, simp)
nipkow@15439
   850
apply (simp)
paulson@14267
   851
done
paulson@14267
   852
paulson@14267
   853
lemma div_le_dividend [simp]: "m div n \<le> (m::nat)"
paulson@14267
   854
apply (case_tac "n=0", simp)
paulson@14267
   855
apply (subgoal_tac "m div n \<le> m div 1", simp)
paulson@14267
   856
apply (rule div_le_mono2)
paulson@14267
   857
apply (simp_all (no_asm_simp))
paulson@14267
   858
done
paulson@14267
   859
wenzelm@22718
   860
(* Similar for "less than" *)
huffman@47138
   861
lemma div_less_dividend [simp]:
huffman@47138
   862
  "\<lbrakk>(1::nat) < n; 0 < m\<rbrakk> \<Longrightarrow> m div n < m"
huffman@47138
   863
apply (induct m rule: nat_less_induct)
paulson@14267
   864
apply (rename_tac "m")
paulson@14267
   865
apply (case_tac "m<n", simp)
paulson@14267
   866
apply (subgoal_tac "0<n")
wenzelm@22718
   867
 prefer 2 apply simp
paulson@14267
   868
apply (simp add: div_geq)
paulson@14267
   869
apply (case_tac "n<m")
paulson@15251
   870
 apply (subgoal_tac "(m-n) div n < (m-n) ")
paulson@14267
   871
  apply (rule impI less_trans_Suc)+
paulson@14267
   872
apply assumption
nipkow@15439
   873
  apply (simp_all)
paulson@14267
   874
done
paulson@14267
   875
paulson@14267
   876
text{*A fact for the mutilated chess board*}
paulson@14267
   877
lemma mod_Suc: "Suc(m) mod n = (if Suc(m mod n) = n then 0 else Suc(m mod n))"
paulson@14267
   878
apply (case_tac "n=0", simp)
paulson@15251
   879
apply (induct "m" rule: nat_less_induct)
paulson@14267
   880
apply (case_tac "Suc (na) <n")
paulson@14267
   881
(* case Suc(na) < n *)
paulson@14267
   882
apply (frule lessI [THEN less_trans], simp add: less_not_refl3)
paulson@14267
   883
(* case n \<le> Suc(na) *)
paulson@16796
   884
apply (simp add: linorder_not_less le_Suc_eq mod_geq)
nipkow@15439
   885
apply (auto simp add: Suc_diff_le le_mod_geq)
paulson@14267
   886
done
paulson@14267
   887
paulson@14267
   888
lemma mod_eq_0_iff: "(m mod d = 0) = (\<exists>q::nat. m = d*q)"
nipkow@29667
   889
by (auto simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)
paulson@17084
   890
wenzelm@22718
   891
lemmas mod_eq_0D [dest!] = mod_eq_0_iff [THEN iffD1]
paulson@14267
   892
paulson@14267
   893
(*Loses information, namely we also have r<d provided d is nonzero*)
paulson@14267
   894
lemma mod_eqD: "(m mod d = r) ==> \<exists>q::nat. m = r + q*d"
haftmann@27651
   895
  apply (cut_tac a = m in mod_div_equality)
wenzelm@22718
   896
  apply (simp only: add_ac)
wenzelm@22718
   897
  apply (blast intro: sym)
wenzelm@22718
   898
  done
paulson@14267
   899
nipkow@13152
   900
lemma split_div:
nipkow@13189
   901
 "P(n div k :: nat) =
nipkow@13189
   902
 ((k = 0 \<longrightarrow> P 0) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P i)))"
nipkow@13189
   903
 (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
nipkow@13189
   904
proof
nipkow@13189
   905
  assume P: ?P
nipkow@13189
   906
  show ?Q
nipkow@13189
   907
  proof (cases)
nipkow@13189
   908
    assume "k = 0"
haftmann@27651
   909
    with P show ?Q by simp
nipkow@13189
   910
  next
nipkow@13189
   911
    assume not0: "k \<noteq> 0"
nipkow@13189
   912
    thus ?Q
nipkow@13189
   913
    proof (simp, intro allI impI)
nipkow@13189
   914
      fix i j
nipkow@13189
   915
      assume n: "n = k*i + j" and j: "j < k"
nipkow@13189
   916
      show "P i"
nipkow@13189
   917
      proof (cases)
wenzelm@22718
   918
        assume "i = 0"
wenzelm@22718
   919
        with n j P show "P i" by simp
nipkow@13189
   920
      next
wenzelm@22718
   921
        assume "i \<noteq> 0"
wenzelm@22718
   922
        with not0 n j P show "P i" by(simp add:add_ac)
nipkow@13189
   923
      qed
nipkow@13189
   924
    qed
nipkow@13189
   925
  qed
nipkow@13189
   926
next
nipkow@13189
   927
  assume Q: ?Q
nipkow@13189
   928
  show ?P
nipkow@13189
   929
  proof (cases)
nipkow@13189
   930
    assume "k = 0"
haftmann@27651
   931
    with Q show ?P by simp
nipkow@13189
   932
  next
nipkow@13189
   933
    assume not0: "k \<noteq> 0"
nipkow@13189
   934
    with Q have R: ?R by simp
nipkow@13189
   935
    from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
nipkow@13517
   936
    show ?P by simp
nipkow@13189
   937
  qed
nipkow@13189
   938
qed
nipkow@13189
   939
berghofe@13882
   940
lemma split_div_lemma:
haftmann@26100
   941
  assumes "0 < n"
haftmann@26100
   942
  shows "n * q \<le> m \<and> m < n * Suc q \<longleftrightarrow> q = ((m\<Colon>nat) div n)" (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@26100
   943
proof
haftmann@26100
   944
  assume ?rhs
haftmann@26100
   945
  with mult_div_cancel have nq: "n * q = m - (m mod n)" by simp
haftmann@26100
   946
  then have A: "n * q \<le> m" by simp
haftmann@26100
   947
  have "n - (m mod n) > 0" using mod_less_divisor assms by auto
haftmann@26100
   948
  then have "m < m + (n - (m mod n))" by simp
haftmann@26100
   949
  then have "m < n + (m - (m mod n))" by simp
haftmann@26100
   950
  with nq have "m < n + n * q" by simp
haftmann@26100
   951
  then have B: "m < n * Suc q" by simp
haftmann@26100
   952
  from A B show ?lhs ..
haftmann@26100
   953
next
haftmann@26100
   954
  assume P: ?lhs
haftmann@33340
   955
  then have "divmod_nat_rel m n (q, m - n * q)"
haftmann@33340
   956
    unfolding divmod_nat_rel_def by (auto simp add: mult_ac)
haftmann@33340
   957
  with divmod_nat_rel_unique divmod_nat_rel [of m n]
haftmann@30923
   958
  have "(q, m - n * q) = (m div n, m mod n)" by auto
haftmann@30923
   959
  then show ?rhs by simp
haftmann@26100
   960
qed
berghofe@13882
   961
berghofe@13882
   962
theorem split_div':
berghofe@13882
   963
  "P ((m::nat) div n) = ((n = 0 \<and> P 0) \<or>
paulson@14267
   964
   (\<exists>q. (n * q \<le> m \<and> m < n * (Suc q)) \<and> P q))"
berghofe@13882
   965
  apply (case_tac "0 < n")
berghofe@13882
   966
  apply (simp only: add: split_div_lemma)
haftmann@27651
   967
  apply simp_all
berghofe@13882
   968
  done
berghofe@13882
   969
nipkow@13189
   970
lemma split_mod:
nipkow@13189
   971
 "P(n mod k :: nat) =
nipkow@13189
   972
 ((k = 0 \<longrightarrow> P n) \<and> (k \<noteq> 0 \<longrightarrow> (!i. !j<k. n = k*i + j \<longrightarrow> P j)))"
nipkow@13189
   973
 (is "?P = ?Q" is "_ = (_ \<and> (_ \<longrightarrow> ?R))")
nipkow@13189
   974
proof
nipkow@13189
   975
  assume P: ?P
nipkow@13189
   976
  show ?Q
nipkow@13189
   977
  proof (cases)
nipkow@13189
   978
    assume "k = 0"
haftmann@27651
   979
    with P show ?Q by simp
nipkow@13189
   980
  next
nipkow@13189
   981
    assume not0: "k \<noteq> 0"
nipkow@13189
   982
    thus ?Q
nipkow@13189
   983
    proof (simp, intro allI impI)
nipkow@13189
   984
      fix i j
nipkow@13189
   985
      assume "n = k*i + j" "j < k"
nipkow@13189
   986
      thus "P j" using not0 P by(simp add:add_ac mult_ac)
nipkow@13189
   987
    qed
nipkow@13189
   988
  qed
nipkow@13189
   989
next
nipkow@13189
   990
  assume Q: ?Q
nipkow@13189
   991
  show ?P
nipkow@13189
   992
  proof (cases)
nipkow@13189
   993
    assume "k = 0"
haftmann@27651
   994
    with Q show ?P by simp
nipkow@13189
   995
  next
nipkow@13189
   996
    assume not0: "k \<noteq> 0"
nipkow@13189
   997
    with Q have R: ?R by simp
nipkow@13189
   998
    from not0 R[THEN spec,of "n div k",THEN spec, of "n mod k"]
nipkow@13517
   999
    show ?P by simp
nipkow@13189
  1000
  qed
nipkow@13189
  1001
qed
nipkow@13189
  1002
berghofe@13882
  1003
theorem mod_div_equality': "(m::nat) mod n = m - (m div n) * n"
huffman@47138
  1004
  using mod_div_equality [of m n] by arith
huffman@47138
  1005
huffman@47138
  1006
lemma div_mod_equality': "(m::nat) div n * n = m - m mod n"
huffman@47138
  1007
  using mod_div_equality [of m n] by arith
huffman@47138
  1008
(* FIXME: very similar to mult_div_cancel *)
haftmann@22800
  1009
noschinl@52398
  1010
lemma div_eq_dividend_iff: "a \<noteq> 0 \<Longrightarrow> (a :: nat) div b = a \<longleftrightarrow> b = 1"
noschinl@52398
  1011
  apply rule
noschinl@52398
  1012
  apply (cases "b = 0")
noschinl@52398
  1013
  apply simp_all
noschinl@52398
  1014
  apply (metis (full_types) One_nat_def Suc_lessI div_less_dividend less_not_refl3)
noschinl@52398
  1015
  done
noschinl@52398
  1016
haftmann@22800
  1017
huffman@46551
  1018
subsubsection {* An ``induction'' law for modulus arithmetic. *}
paulson@14640
  1019
paulson@14640
  1020
lemma mod_induct_0:
paulson@14640
  1021
  assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
paulson@14640
  1022
  and base: "P i" and i: "i<p"
paulson@14640
  1023
  shows "P 0"
paulson@14640
  1024
proof (rule ccontr)
paulson@14640
  1025
  assume contra: "\<not>(P 0)"
paulson@14640
  1026
  from i have p: "0<p" by simp
paulson@14640
  1027
  have "\<forall>k. 0<k \<longrightarrow> \<not> P (p-k)" (is "\<forall>k. ?A k")
paulson@14640
  1028
  proof
paulson@14640
  1029
    fix k
paulson@14640
  1030
    show "?A k"
paulson@14640
  1031
    proof (induct k)
paulson@14640
  1032
      show "?A 0" by simp  -- "by contradiction"
paulson@14640
  1033
    next
paulson@14640
  1034
      fix n
paulson@14640
  1035
      assume ih: "?A n"
paulson@14640
  1036
      show "?A (Suc n)"
paulson@14640
  1037
      proof (clarsimp)
wenzelm@22718
  1038
        assume y: "P (p - Suc n)"
wenzelm@22718
  1039
        have n: "Suc n < p"
wenzelm@22718
  1040
        proof (rule ccontr)
wenzelm@22718
  1041
          assume "\<not>(Suc n < p)"
wenzelm@22718
  1042
          hence "p - Suc n = 0"
wenzelm@22718
  1043
            by simp
wenzelm@22718
  1044
          with y contra show "False"
wenzelm@22718
  1045
            by simp
wenzelm@22718
  1046
        qed
wenzelm@22718
  1047
        hence n2: "Suc (p - Suc n) = p-n" by arith
wenzelm@22718
  1048
        from p have "p - Suc n < p" by arith
wenzelm@22718
  1049
        with y step have z: "P ((Suc (p - Suc n)) mod p)"
wenzelm@22718
  1050
          by blast
wenzelm@22718
  1051
        show "False"
wenzelm@22718
  1052
        proof (cases "n=0")
wenzelm@22718
  1053
          case True
wenzelm@22718
  1054
          with z n2 contra show ?thesis by simp
wenzelm@22718
  1055
        next
wenzelm@22718
  1056
          case False
wenzelm@22718
  1057
          with p have "p-n < p" by arith
wenzelm@22718
  1058
          with z n2 False ih show ?thesis by simp
wenzelm@22718
  1059
        qed
paulson@14640
  1060
      qed
paulson@14640
  1061
    qed
paulson@14640
  1062
  qed
paulson@14640
  1063
  moreover
paulson@14640
  1064
  from i obtain k where "0<k \<and> i+k=p"
paulson@14640
  1065
    by (blast dest: less_imp_add_positive)
paulson@14640
  1066
  hence "0<k \<and> i=p-k" by auto
paulson@14640
  1067
  moreover
paulson@14640
  1068
  note base
paulson@14640
  1069
  ultimately
paulson@14640
  1070
  show "False" by blast
paulson@14640
  1071
qed
paulson@14640
  1072
paulson@14640
  1073
lemma mod_induct:
paulson@14640
  1074
  assumes step: "\<forall>i<p. P i \<longrightarrow> P ((Suc i) mod p)"
paulson@14640
  1075
  and base: "P i" and i: "i<p" and j: "j<p"
paulson@14640
  1076
  shows "P j"
paulson@14640
  1077
proof -
paulson@14640
  1078
  have "\<forall>j<p. P j"
paulson@14640
  1079
  proof
paulson@14640
  1080
    fix j
paulson@14640
  1081
    show "j<p \<longrightarrow> P j" (is "?A j")
paulson@14640
  1082
    proof (induct j)
paulson@14640
  1083
      from step base i show "?A 0"
wenzelm@22718
  1084
        by (auto elim: mod_induct_0)
paulson@14640
  1085
    next
paulson@14640
  1086
      fix k
paulson@14640
  1087
      assume ih: "?A k"
paulson@14640
  1088
      show "?A (Suc k)"
paulson@14640
  1089
      proof
wenzelm@22718
  1090
        assume suc: "Suc k < p"
wenzelm@22718
  1091
        hence k: "k<p" by simp
wenzelm@22718
  1092
        with ih have "P k" ..
wenzelm@22718
  1093
        with step k have "P (Suc k mod p)"
wenzelm@22718
  1094
          by blast
wenzelm@22718
  1095
        moreover
wenzelm@22718
  1096
        from suc have "Suc k mod p = Suc k"
wenzelm@22718
  1097
          by simp
wenzelm@22718
  1098
        ultimately
wenzelm@22718
  1099
        show "P (Suc k)" by simp
paulson@14640
  1100
      qed
paulson@14640
  1101
    qed
paulson@14640
  1102
  qed
paulson@14640
  1103
  with j show ?thesis by blast
paulson@14640
  1104
qed
paulson@14640
  1105
haftmann@33296
  1106
lemma div2_Suc_Suc [simp]: "Suc (Suc m) div 2 = Suc (m div 2)"
huffman@47138
  1107
  by (simp add: numeral_2_eq_2 le_div_geq)
huffman@47138
  1108
huffman@47138
  1109
lemma mod2_Suc_Suc [simp]: "Suc (Suc m) mod 2 = m mod 2"
huffman@47138
  1110
  by (simp add: numeral_2_eq_2 le_mod_geq)
haftmann@33296
  1111
haftmann@33296
  1112
lemma add_self_div_2 [simp]: "(m + m) div 2 = (m::nat)"
huffman@47217
  1113
by (simp add: mult_2 [symmetric])
haftmann@33296
  1114
haftmann@33296
  1115
lemma mod2_gr_0 [simp]: "0 < (m\<Colon>nat) mod 2 \<longleftrightarrow> m mod 2 = 1"
haftmann@33296
  1116
proof -
boehmes@35815
  1117
  { fix n :: nat have  "(n::nat) < 2 \<Longrightarrow> n = 0 \<or> n = 1" by (cases n) simp_all }
haftmann@33296
  1118
  moreover have "m mod 2 < 2" by simp
haftmann@33296
  1119
  ultimately have "m mod 2 = 0 \<or> m mod 2 = 1" .
haftmann@33296
  1120
  then show ?thesis by auto
haftmann@33296
  1121
qed
haftmann@33296
  1122
haftmann@33296
  1123
text{*These lemmas collapse some needless occurrences of Suc:
haftmann@33296
  1124
    at least three Sucs, since two and fewer are rewritten back to Suc again!
haftmann@33296
  1125
    We already have some rules to simplify operands smaller than 3.*}
haftmann@33296
  1126
haftmann@33296
  1127
lemma div_Suc_eq_div_add3 [simp]: "m div (Suc (Suc (Suc n))) = m div (3+n)"
haftmann@33296
  1128
by (simp add: Suc3_eq_add_3)
haftmann@33296
  1129
haftmann@33296
  1130
lemma mod_Suc_eq_mod_add3 [simp]: "m mod (Suc (Suc (Suc n))) = m mod (3+n)"
haftmann@33296
  1131
by (simp add: Suc3_eq_add_3)
haftmann@33296
  1132
haftmann@33296
  1133
lemma Suc_div_eq_add3_div: "(Suc (Suc (Suc m))) div n = (3+m) div n"
haftmann@33296
  1134
by (simp add: Suc3_eq_add_3)
haftmann@33296
  1135
haftmann@33296
  1136
lemma Suc_mod_eq_add3_mod: "(Suc (Suc (Suc m))) mod n = (3+m) mod n"
haftmann@33296
  1137
by (simp add: Suc3_eq_add_3)
haftmann@33296
  1138
huffman@47108
  1139
lemmas Suc_div_eq_add3_div_numeral [simp] = Suc_div_eq_add3_div [of _ "numeral v"] for v
huffman@47108
  1140
lemmas Suc_mod_eq_add3_mod_numeral [simp] = Suc_mod_eq_add3_mod [of _ "numeral v"] for v
haftmann@33296
  1141
haftmann@33361
  1142
haftmann@33361
  1143
lemma Suc_times_mod_eq: "1<k ==> Suc (k * m) mod k = 1" 
haftmann@33361
  1144
apply (induct "m")
haftmann@33361
  1145
apply (simp_all add: mod_Suc)
haftmann@33361
  1146
done
haftmann@33361
  1147
huffman@47108
  1148
declare Suc_times_mod_eq [of "numeral w", simp] for w
haftmann@33361
  1149
huffman@47138
  1150
lemma Suc_div_le_mono [simp]: "n div k \<le> (Suc n) div k"
huffman@47138
  1151
by (simp add: div_le_mono)
haftmann@33361
  1152
haftmann@33361
  1153
lemma Suc_n_div_2_gt_zero [simp]: "(0::nat) < n ==> 0 < (n + 1) div 2"
haftmann@33361
  1154
by (cases n) simp_all
haftmann@33361
  1155
boehmes@35815
  1156
lemma div_2_gt_zero [simp]: assumes A: "(1::nat) < n" shows "0 < n div 2"
boehmes@35815
  1157
proof -
boehmes@35815
  1158
  from A have B: "0 < n - 1" and C: "n - 1 + 1 = n" by simp_all
boehmes@35815
  1159
  from Suc_n_div_2_gt_zero [OF B] C show ?thesis by simp 
boehmes@35815
  1160
qed
haftmann@33361
  1161
haftmann@33361
  1162
  (* Potential use of algebra : Equality modulo n*)
haftmann@33361
  1163
lemma mod_mult_self3 [simp]: "(k*n + m) mod n = m mod (n::nat)"
haftmann@33361
  1164
by (simp add: mult_ac add_ac)
haftmann@33361
  1165
haftmann@33361
  1166
lemma mod_mult_self4 [simp]: "Suc (k*n + m) mod n = Suc m mod n"
haftmann@33361
  1167
proof -
haftmann@33361
  1168
  have "Suc (k * n + m) mod n = (k * n + Suc m) mod n" by simp
haftmann@33361
  1169
  also have "... = Suc m mod n" by (rule mod_mult_self3) 
haftmann@33361
  1170
  finally show ?thesis .
haftmann@33361
  1171
qed
haftmann@33361
  1172
haftmann@33361
  1173
lemma mod_Suc_eq_Suc_mod: "Suc m mod n = Suc (m mod n) mod n"
haftmann@33361
  1174
apply (subst mod_Suc [of m]) 
haftmann@33361
  1175
apply (subst mod_Suc [of "m mod n"], simp) 
haftmann@33361
  1176
done
haftmann@33361
  1177
huffman@47108
  1178
lemma mod_2_not_eq_zero_eq_one_nat:
huffman@47108
  1179
  fixes n :: nat
huffman@47108
  1180
  shows "n mod 2 \<noteq> 0 \<longleftrightarrow> n mod 2 = 1"
huffman@47108
  1181
  by simp
huffman@47108
  1182
haftmann@33361
  1183
haftmann@33361
  1184
subsection {* Division on @{typ int} *}
haftmann@33361
  1185
haftmann@33361
  1186
definition divmod_int_rel :: "int \<Rightarrow> int \<Rightarrow> int \<times> int \<Rightarrow> bool" where
haftmann@33361
  1187
    --{*definition of quotient and remainder*}
huffman@47139
  1188
  "divmod_int_rel a b = (\<lambda>(q, r). a = b * q + r \<and>
huffman@47139
  1189
    (if 0 < b then 0 \<le> r \<and> r < b else if b < 0 then b < r \<and> r \<le> 0 else q = 0))"
haftmann@33361
  1190
haftmann@33361
  1191
definition adjust :: "int \<Rightarrow> int \<times> int \<Rightarrow> int \<times> int" where
haftmann@33361
  1192
    --{*for the division algorithm*}
huffman@47108
  1193
    "adjust b = (\<lambda>(q, r). if 0 \<le> r - b then (2 * q + 1, r - b)
haftmann@33361
  1194
                         else (2 * q, r))"
haftmann@33361
  1195
haftmann@33361
  1196
text{*algorithm for the case @{text "a\<ge>0, b>0"}*}
haftmann@33361
  1197
function posDivAlg :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
haftmann@33361
  1198
  "posDivAlg a b = (if a < b \<or>  b \<le> 0 then (0, a)
haftmann@33361
  1199
     else adjust b (posDivAlg a (2 * b)))"
haftmann@33361
  1200
by auto
haftmann@33361
  1201
termination by (relation "measure (\<lambda>(a, b). nat (a - b + 1))")
haftmann@33361
  1202
  (auto simp add: mult_2)
haftmann@33361
  1203
haftmann@33361
  1204
text{*algorithm for the case @{text "a<0, b>0"}*}
haftmann@33361
  1205
function negDivAlg :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
haftmann@33361
  1206
  "negDivAlg a b = (if 0 \<le>a + b \<or> b \<le> 0  then (-1, a + b)
haftmann@33361
  1207
     else adjust b (negDivAlg a (2 * b)))"
haftmann@33361
  1208
by auto
haftmann@33361
  1209
termination by (relation "measure (\<lambda>(a, b). nat (- a - b))")
haftmann@33361
  1210
  (auto simp add: mult_2)
haftmann@33361
  1211
haftmann@33361
  1212
text{*algorithm for the general case @{term "b\<noteq>0"}*}
haftmann@33361
  1213
haftmann@33361
  1214
definition divmod_int :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
haftmann@33361
  1215
    --{*The full division algorithm considers all possible signs for a, b
haftmann@33361
  1216
       including the special case @{text "a=0, b<0"} because 
haftmann@33361
  1217
       @{term negDivAlg} requires @{term "a<0"}.*}
haftmann@33361
  1218
  "divmod_int a b = (if 0 \<le> a then if 0 \<le> b then posDivAlg a b
haftmann@33361
  1219
                  else if a = 0 then (0, 0)
huffman@46560
  1220
                       else apsnd uminus (negDivAlg (-a) (-b))
haftmann@33361
  1221
               else 
haftmann@33361
  1222
                  if 0 < b then negDivAlg a b
huffman@46560
  1223
                  else apsnd uminus (posDivAlg (-a) (-b)))"
haftmann@33361
  1224
haftmann@33361
  1225
instantiation int :: Divides.div
haftmann@33361
  1226
begin
haftmann@33361
  1227
huffman@46551
  1228
definition div_int where
haftmann@33361
  1229
  "a div b = fst (divmod_int a b)"
haftmann@33361
  1230
huffman@46551
  1231
lemma fst_divmod_int [simp]:
huffman@46551
  1232
  "fst (divmod_int a b) = a div b"
huffman@46551
  1233
  by (simp add: div_int_def)
huffman@46551
  1234
huffman@46551
  1235
definition mod_int where
huffman@46560
  1236
  "a mod b = snd (divmod_int a b)"
haftmann@33361
  1237
huffman@46551
  1238
lemma snd_divmod_int [simp]:
huffman@46551
  1239
  "snd (divmod_int a b) = a mod b"
huffman@46551
  1240
  by (simp add: mod_int_def)
huffman@46551
  1241
haftmann@33361
  1242
instance ..
haftmann@33361
  1243
paulson@3366
  1244
end
haftmann@33361
  1245
haftmann@33361
  1246
lemma divmod_int_mod_div:
haftmann@33361
  1247
  "divmod_int p q = (p div q, p mod q)"
huffman@46551
  1248
  by (simp add: prod_eq_iff)
haftmann@33361
  1249
haftmann@33361
  1250
text{*
haftmann@33361
  1251
Here is the division algorithm in ML:
haftmann@33361
  1252
haftmann@33361
  1253
\begin{verbatim}
haftmann@33361
  1254
    fun posDivAlg (a,b) =
haftmann@33361
  1255
      if a<b then (0,a)
haftmann@33361
  1256
      else let val (q,r) = posDivAlg(a, 2*b)
haftmann@33361
  1257
               in  if 0\<le>r-b then (2*q+1, r-b) else (2*q, r)
haftmann@33361
  1258
           end
haftmann@33361
  1259
haftmann@33361
  1260
    fun negDivAlg (a,b) =
haftmann@33361
  1261
      if 0\<le>a+b then (~1,a+b)
haftmann@33361
  1262
      else let val (q,r) = negDivAlg(a, 2*b)
haftmann@33361
  1263
               in  if 0\<le>r-b then (2*q+1, r-b) else (2*q, r)
haftmann@33361
  1264
           end;
haftmann@33361
  1265
haftmann@33361
  1266
    fun negateSnd (q,r:int) = (q,~r);
haftmann@33361
  1267
haftmann@33361
  1268
    fun divmod (a,b) = if 0\<le>a then 
haftmann@33361
  1269
                          if b>0 then posDivAlg (a,b) 
haftmann@33361
  1270
                           else if a=0 then (0,0)
haftmann@33361
  1271
                                else negateSnd (negDivAlg (~a,~b))
haftmann@33361
  1272
                       else 
haftmann@33361
  1273
                          if 0<b then negDivAlg (a,b)
haftmann@33361
  1274
                          else        negateSnd (posDivAlg (~a,~b));
haftmann@33361
  1275
\end{verbatim}
haftmann@33361
  1276
*}
haftmann@33361
  1277
haftmann@33361
  1278
huffman@46551
  1279
subsubsection {* Uniqueness and Monotonicity of Quotients and Remainders *}
haftmann@33361
  1280
haftmann@33361
  1281
lemma unique_quotient_lemma:
haftmann@33361
  1282
     "[| b*q' + r'  \<le> b*q + r;  0 \<le> r';  r' < b;  r < b |]  
haftmann@33361
  1283
      ==> q' \<le> (q::int)"
haftmann@33361
  1284
apply (subgoal_tac "r' + b * (q'-q) \<le> r")
haftmann@33361
  1285
 prefer 2 apply (simp add: right_diff_distrib)
haftmann@33361
  1286
apply (subgoal_tac "0 < b * (1 + q - q') ")
haftmann@33361
  1287
apply (erule_tac [2] order_le_less_trans)
webertj@49962
  1288
 prefer 2 apply (simp add: right_diff_distrib distrib_left)
haftmann@33361
  1289
apply (subgoal_tac "b * q' < b * (1 + q) ")
webertj@49962
  1290
 prefer 2 apply (simp add: right_diff_distrib distrib_left)
haftmann@33361
  1291
apply (simp add: mult_less_cancel_left)
haftmann@33361
  1292
done
haftmann@33361
  1293
haftmann@33361
  1294
lemma unique_quotient_lemma_neg:
haftmann@33361
  1295
     "[| b*q' + r' \<le> b*q + r;  r \<le> 0;  b < r;  b < r' |]  
haftmann@33361
  1296
      ==> q \<le> (q'::int)"
haftmann@33361
  1297
by (rule_tac b = "-b" and r = "-r'" and r' = "-r" in unique_quotient_lemma, 
haftmann@33361
  1298
    auto)
haftmann@33361
  1299
haftmann@33361
  1300
lemma unique_quotient:
bulwahn@46552
  1301
     "[| divmod_int_rel a b (q, r); divmod_int_rel a b (q', r') |]  
haftmann@33361
  1302
      ==> q = q'"
haftmann@33361
  1303
apply (simp add: divmod_int_rel_def linorder_neq_iff split: split_if_asm)
haftmann@33361
  1304
apply (blast intro: order_antisym
haftmann@33361
  1305
             dest: order_eq_refl [THEN unique_quotient_lemma] 
haftmann@33361
  1306
             order_eq_refl [THEN unique_quotient_lemma_neg] sym)+
haftmann@33361
  1307
done
haftmann@33361
  1308
haftmann@33361
  1309
haftmann@33361
  1310
lemma unique_remainder:
bulwahn@46552
  1311
     "[| divmod_int_rel a b (q, r); divmod_int_rel a b (q', r') |]  
haftmann@33361
  1312
      ==> r = r'"
haftmann@33361
  1313
apply (subgoal_tac "q = q'")
haftmann@33361
  1314
 apply (simp add: divmod_int_rel_def)
haftmann@33361
  1315
apply (blast intro: unique_quotient)
haftmann@33361
  1316
done
haftmann@33361
  1317
haftmann@33361
  1318
huffman@46551
  1319
subsubsection {* Correctness of @{term posDivAlg}, the Algorithm for Non-Negative Dividends *}
haftmann@33361
  1320
haftmann@33361
  1321
text{*And positive divisors*}
haftmann@33361
  1322
haftmann@33361
  1323
lemma adjust_eq [simp]:
huffman@47108
  1324
     "adjust b (q, r) = 
huffman@47108
  1325
      (let diff = r - b in  
huffman@47108
  1326
        if 0 \<le> diff then (2 * q + 1, diff)   
haftmann@33361
  1327
                     else (2*q, r))"
huffman@47108
  1328
  by (simp add: Let_def adjust_def)
haftmann@33361
  1329
haftmann@33361
  1330
declare posDivAlg.simps [simp del]
haftmann@33361
  1331
haftmann@33361
  1332
text{*use with a simproc to avoid repeatedly proving the premise*}
haftmann@33361
  1333
lemma posDivAlg_eqn:
haftmann@33361
  1334
     "0 < b ==>  
haftmann@33361
  1335
      posDivAlg a b = (if a<b then (0,a) else adjust b (posDivAlg a (2*b)))"
haftmann@33361
  1336
by (rule posDivAlg.simps [THEN trans], simp)
haftmann@33361
  1337
haftmann@33361
  1338
text{*Correctness of @{term posDivAlg}: it computes quotients correctly*}
haftmann@33361
  1339
theorem posDivAlg_correct:
haftmann@33361
  1340
  assumes "0 \<le> a" and "0 < b"
haftmann@33361
  1341
  shows "divmod_int_rel a b (posDivAlg a b)"
wenzelm@41550
  1342
  using assms
wenzelm@41550
  1343
  apply (induct a b rule: posDivAlg.induct)
wenzelm@41550
  1344
  apply auto
wenzelm@41550
  1345
  apply (simp add: divmod_int_rel_def)
webertj@49962
  1346
  apply (subst posDivAlg_eqn, simp add: distrib_left)
wenzelm@41550
  1347
  apply (case_tac "a < b")
wenzelm@41550
  1348
  apply simp_all
wenzelm@41550
  1349
  apply (erule splitE)
webertj@49962
  1350
  apply (auto simp add: distrib_left Let_def mult_ac mult_2_right)
wenzelm@41550
  1351
  done
haftmann@33361
  1352
haftmann@33361
  1353
huffman@46551
  1354
subsubsection {* Correctness of @{term negDivAlg}, the Algorithm for Negative Dividends *}
haftmann@33361
  1355
haftmann@33361
  1356
text{*And positive divisors*}
haftmann@33361
  1357
haftmann@33361
  1358
declare negDivAlg.simps [simp del]
haftmann@33361
  1359
haftmann@33361
  1360
text{*use with a simproc to avoid repeatedly proving the premise*}
haftmann@33361
  1361
lemma negDivAlg_eqn:
haftmann@33361
  1362
     "0 < b ==>  
haftmann@33361
  1363
      negDivAlg a b =       
haftmann@33361
  1364
       (if 0\<le>a+b then (-1,a+b) else adjust b (negDivAlg a (2*b)))"
haftmann@33361
  1365
by (rule negDivAlg.simps [THEN trans], simp)
haftmann@33361
  1366
haftmann@33361
  1367
(*Correctness of negDivAlg: it computes quotients correctly
haftmann@33361
  1368
  It doesn't work if a=0 because the 0/b equals 0, not -1*)
haftmann@33361
  1369
lemma negDivAlg_correct:
haftmann@33361
  1370
  assumes "a < 0" and "b > 0"
haftmann@33361
  1371
  shows "divmod_int_rel a b (negDivAlg a b)"
wenzelm@41550
  1372
  using assms
wenzelm@41550
  1373
  apply (induct a b rule: negDivAlg.induct)
wenzelm@41550
  1374
  apply (auto simp add: linorder_not_le)
wenzelm@41550
  1375
  apply (simp add: divmod_int_rel_def)
wenzelm@41550
  1376
  apply (subst negDivAlg_eqn, assumption)
wenzelm@41550
  1377
  apply (case_tac "a + b < (0\<Colon>int)")
wenzelm@41550
  1378
  apply simp_all
wenzelm@41550
  1379
  apply (erule splitE)
webertj@49962
  1380
  apply (auto simp add: distrib_left Let_def mult_ac mult_2_right)
wenzelm@41550
  1381
  done
haftmann@33361
  1382
haftmann@33361
  1383
huffman@46551
  1384
subsubsection {* Existence Shown by Proving the Division Algorithm to be Correct *}
haftmann@33361
  1385
haftmann@33361
  1386
(*the case a=0*)
huffman@47139
  1387
lemma divmod_int_rel_0: "divmod_int_rel 0 b (0, 0)"
haftmann@33361
  1388
by (auto simp add: divmod_int_rel_def linorder_neq_iff)
haftmann@33361
  1389
haftmann@33361
  1390
lemma posDivAlg_0 [simp]: "posDivAlg 0 b = (0, 0)"
haftmann@33361
  1391
by (subst posDivAlg.simps, auto)
haftmann@33361
  1392
huffman@47139
  1393
lemma posDivAlg_0_right [simp]: "posDivAlg a 0 = (0, a)"
huffman@47139
  1394
by (subst posDivAlg.simps, auto)
huffman@47139
  1395
haftmann@33361
  1396
lemma negDivAlg_minus1 [simp]: "negDivAlg -1 b = (-1, b - 1)"
haftmann@33361
  1397
by (subst negDivAlg.simps, auto)
haftmann@33361
  1398
huffman@46560
  1399
lemma divmod_int_rel_neg: "divmod_int_rel (-a) (-b) qr ==> divmod_int_rel a b (apsnd uminus qr)"
huffman@47139
  1400
by (auto simp add: divmod_int_rel_def)
huffman@47139
  1401
huffman@47139
  1402
lemma divmod_int_correct: "divmod_int_rel a b (divmod_int a b)"
huffman@47139
  1403
apply (cases "b = 0", simp add: divmod_int_def divmod_int_rel_def)
haftmann@33361
  1404
by (force simp add: linorder_neq_iff divmod_int_rel_0 divmod_int_def divmod_int_rel_neg
haftmann@33361
  1405
                    posDivAlg_correct negDivAlg_correct)
haftmann@33361
  1406
huffman@47141
  1407
lemma divmod_int_unique:
huffman@47141
  1408
  assumes "divmod_int_rel a b qr" 
huffman@47141
  1409
  shows "divmod_int a b = qr"
huffman@47141
  1410
  using assms divmod_int_correct [of a b]
huffman@47141
  1411
  using unique_quotient [of a b] unique_remainder [of a b]
huffman@47141
  1412
  by (metis pair_collapse)
huffman@47141
  1413
huffman@47141
  1414
lemma divmod_int_rel_div_mod: "divmod_int_rel a b (a div b, a mod b)"
huffman@47141
  1415
  using divmod_int_correct by (simp add: divmod_int_mod_div)
huffman@47141
  1416
huffman@47141
  1417
lemma div_int_unique: "divmod_int_rel a b (q, r) \<Longrightarrow> a div b = q"
huffman@47141
  1418
  by (simp add: divmod_int_rel_div_mod [THEN unique_quotient])
huffman@47141
  1419
huffman@47141
  1420
lemma mod_int_unique: "divmod_int_rel a b (q, r) \<Longrightarrow> a mod b = r"
huffman@47141
  1421
  by (simp add: divmod_int_rel_div_mod [THEN unique_remainder])
huffman@47141
  1422
huffman@47141
  1423
instance int :: ring_div
huffman@47141
  1424
proof
huffman@47141
  1425
  fix a b :: int
huffman@47141
  1426
  show "a div b * b + a mod b = a"
huffman@47141
  1427
    using divmod_int_rel_div_mod [of a b]
huffman@47141
  1428
    unfolding divmod_int_rel_def by (simp add: mult_commute)
huffman@47141
  1429
next
huffman@47141
  1430
  fix a b c :: int
huffman@47141
  1431
  assume "b \<noteq> 0"
huffman@47141
  1432
  hence "divmod_int_rel (a + c * b) b (c + a div b, a mod b)"
huffman@47141
  1433
    using divmod_int_rel_div_mod [of a b]
huffman@47141
  1434
    unfolding divmod_int_rel_def by (auto simp: algebra_simps)
huffman@47141
  1435
  thus "(a + c * b) div b = c + a div b"
huffman@47141
  1436
    by (rule div_int_unique)
huffman@47141
  1437
next
huffman@47141
  1438
  fix a b c :: int
huffman@47141
  1439
  assume "c \<noteq> 0"
huffman@47141
  1440
  hence "\<And>q r. divmod_int_rel a b (q, r)
huffman@47141
  1441
    \<Longrightarrow> divmod_int_rel (c * a) (c * b) (q, c * r)"
huffman@47141
  1442
    unfolding divmod_int_rel_def
huffman@47141
  1443
    by - (rule linorder_cases [of 0 b], auto simp: algebra_simps
huffman@47141
  1444
      mult_less_0_iff zero_less_mult_iff mult_strict_right_mono
huffman@47141
  1445
      mult_strict_right_mono_neg zero_le_mult_iff mult_le_0_iff)
huffman@47141
  1446
  hence "divmod_int_rel (c * a) (c * b) (a div b, c * (a mod b))"
huffman@47141
  1447
    using divmod_int_rel_div_mod [of a b] .
huffman@47141
  1448
  thus "(c * a) div (c * b) = a div b"
huffman@47141
  1449
    by (rule div_int_unique)
huffman@47141
  1450
next
huffman@47141
  1451
  fix a :: int show "a div 0 = 0"
huffman@47141
  1452
    by (rule div_int_unique, simp add: divmod_int_rel_def)
huffman@47141
  1453
next
huffman@47141
  1454
  fix a :: int show "0 div a = 0"
huffman@47141
  1455
    by (rule div_int_unique, auto simp add: divmod_int_rel_def)
huffman@47141
  1456
qed
huffman@47141
  1457
haftmann@33361
  1458
text{*Basic laws about division and remainder*}
haftmann@33361
  1459
haftmann@33361
  1460
lemma zmod_zdiv_equality: "(a::int) = b * (a div b) + (a mod b)"
huffman@47141
  1461
  by (fact mod_div_equality2 [symmetric])
haftmann@33361
  1462
haftmann@33361
  1463
text {* Tool setup *}
haftmann@33361
  1464
huffman@47108
  1465
(* FIXME: Theorem list add_0s doesn't exist, because Numeral0 has gone. *)
huffman@47108
  1466
lemmas add_0s = add_0_left add_0_right
huffman@47108
  1467
haftmann@33361
  1468
ML {*
wenzelm@43594
  1469
structure Cancel_Div_Mod_Int = Cancel_Div_Mod
wenzelm@41550
  1470
(
haftmann@33361
  1471
  val div_name = @{const_name div};
haftmann@33361
  1472
  val mod_name = @{const_name mod};
haftmann@33361
  1473
  val mk_binop = HOLogic.mk_binop;
haftmann@33361
  1474
  val mk_sum = Arith_Data.mk_sum HOLogic.intT;
haftmann@33361
  1475
  val dest_sum = Arith_Data.dest_sum;
haftmann@33361
  1476
huffman@47165
  1477
  val div_mod_eqs = map mk_meta_eq [@{thm div_mod_equality}, @{thm div_mod_equality2}];
haftmann@33361
  1478
haftmann@33361
  1479
  val prove_eq_sums = Arith_Data.prove_conv2 all_tac (Arith_Data.simp_all_tac 
haftmann@33361
  1480
    (@{thm diff_minus} :: @{thms add_0s} @ @{thms add_ac}))
wenzelm@41550
  1481
)
haftmann@33361
  1482
*}
haftmann@33361
  1483
wenzelm@43594
  1484
simproc_setup cancel_div_mod_int ("(k::int) + l") = {* K Cancel_Div_Mod_Int.proc *}
wenzelm@43594
  1485
huffman@47141
  1486
lemma pos_mod_conj: "(0::int) < b \<Longrightarrow> 0 \<le> a mod b \<and> a mod b < b"
huffman@47141
  1487
  using divmod_int_correct [of a b]
huffman@47141
  1488
  by (auto simp add: divmod_int_rel_def prod_eq_iff)
haftmann@33361
  1489
wenzelm@45607
  1490
lemmas pos_mod_sign [simp] = pos_mod_conj [THEN conjunct1]
wenzelm@45607
  1491
   and pos_mod_bound [simp] = pos_mod_conj [THEN conjunct2]
haftmann@33361
  1492
huffman@47141
  1493
lemma neg_mod_conj: "b < (0::int) \<Longrightarrow> a mod b \<le> 0 \<and> b < a mod b"
huffman@47141
  1494
  using divmod_int_correct [of a b]
huffman@47141
  1495
  by (auto simp add: divmod_int_rel_def prod_eq_iff)
haftmann@33361
  1496
wenzelm@45607
  1497
lemmas neg_mod_sign [simp] = neg_mod_conj [THEN conjunct1]
wenzelm@45607
  1498
   and neg_mod_bound [simp] = neg_mod_conj [THEN conjunct2]
haftmann@33361
  1499
haftmann@33361
  1500
huffman@46551
  1501
subsubsection {* General Properties of div and mod *}
haftmann@33361
  1502
haftmann@33361
  1503
lemma div_pos_pos_trivial: "[| (0::int) \<le> a;  a < b |] ==> a div b = 0"
huffman@47140
  1504
apply (rule div_int_unique)
haftmann@33361
  1505
apply (auto simp add: divmod_int_rel_def)
haftmann@33361
  1506
done
haftmann@33361
  1507
haftmann@33361
  1508
lemma div_neg_neg_trivial: "[| a \<le> (0::int);  b < a |] ==> a div b = 0"
huffman@47140
  1509
apply (rule div_int_unique)
haftmann@33361
  1510
apply (auto simp add: divmod_int_rel_def)
haftmann@33361
  1511
done
haftmann@33361
  1512
haftmann@33361
  1513
lemma div_pos_neg_trivial: "[| (0::int) < a;  a+b \<le> 0 |] ==> a div b = -1"
huffman@47140
  1514
apply (rule div_int_unique)
haftmann@33361
  1515
apply (auto simp add: divmod_int_rel_def)
haftmann@33361
  1516
done
haftmann@33361
  1517
haftmann@33361
  1518
(*There is no div_neg_pos_trivial because  0 div b = 0 would supersede it*)
haftmann@33361
  1519
haftmann@33361
  1520
lemma mod_pos_pos_trivial: "[| (0::int) \<le> a;  a < b |] ==> a mod b = a"
huffman@47140
  1521
apply (rule_tac q = 0 in mod_int_unique)
haftmann@33361
  1522
apply (auto simp add: divmod_int_rel_def)
haftmann@33361
  1523
done
haftmann@33361
  1524
haftmann@33361
  1525
lemma mod_neg_neg_trivial: "[| a \<le> (0::int);  b < a |] ==> a mod b = a"
huffman@47140
  1526
apply (rule_tac q = 0 in mod_int_unique)
haftmann@33361
  1527
apply (auto simp add: divmod_int_rel_def)
haftmann@33361
  1528
done
haftmann@33361
  1529
haftmann@33361
  1530
lemma mod_pos_neg_trivial: "[| (0::int) < a;  a+b \<le> 0 |] ==> a mod b = a+b"
huffman@47140
  1531
apply (rule_tac q = "-1" in mod_int_unique)
haftmann@33361
  1532
apply (auto simp add: divmod_int_rel_def)
haftmann@33361
  1533
done
haftmann@33361
  1534
haftmann@33361
  1535
text{*There is no @{text mod_neg_pos_trivial}.*}
haftmann@33361
  1536
haftmann@33361
  1537
huffman@46551
  1538
subsubsection {* Laws for div and mod with Unary Minus *}
haftmann@33361
  1539
haftmann@33361
  1540
lemma zminus1_lemma:
huffman@47139
  1541
     "divmod_int_rel a b (q, r) ==> b \<noteq> 0
haftmann@33361
  1542
      ==> divmod_int_rel (-a) b (if r=0 then -q else -q - 1,  
haftmann@33361
  1543
                          if r=0 then 0 else b-r)"
haftmann@33361
  1544
by (force simp add: split_ifs divmod_int_rel_def linorder_neq_iff right_diff_distrib)
haftmann@33361
  1545
haftmann@33361
  1546
haftmann@33361
  1547
lemma zdiv_zminus1_eq_if:
haftmann@33361
  1548
     "b \<noteq> (0::int)  
haftmann@33361
  1549
      ==> (-a) div b =  
haftmann@33361
  1550
          (if a mod b = 0 then - (a div b) else  - (a div b) - 1)"
huffman@47140
  1551
by (blast intro: divmod_int_rel_div_mod [THEN zminus1_lemma, THEN div_int_unique])
haftmann@33361
  1552
haftmann@33361
  1553
lemma zmod_zminus1_eq_if:
haftmann@33361
  1554
     "(-a::int) mod b = (if a mod b = 0 then 0 else  b - (a mod b))"
haftmann@33361
  1555
apply (case_tac "b = 0", simp)
huffman@47140
  1556
apply (blast intro: divmod_int_rel_div_mod [THEN zminus1_lemma, THEN mod_int_unique])
haftmann@33361
  1557
done
haftmann@33361
  1558
haftmann@33361
  1559
lemma zmod_zminus1_not_zero:
haftmann@33361
  1560
  fixes k l :: int
haftmann@33361
  1561
  shows "- k mod l \<noteq> 0 \<Longrightarrow> k mod l \<noteq> 0"
haftmann@33361
  1562
  unfolding zmod_zminus1_eq_if by auto
haftmann@33361
  1563
haftmann@33361
  1564
lemma zdiv_zminus2_eq_if:
haftmann@33361
  1565
     "b \<noteq> (0::int)  
haftmann@33361
  1566
      ==> a div (-b) =  
haftmann@33361
  1567
          (if a mod b = 0 then - (a div b) else  - (a div b) - 1)"
huffman@47159
  1568
by (simp add: zdiv_zminus1_eq_if div_minus_right)
haftmann@33361
  1569
haftmann@33361
  1570
lemma zmod_zminus2_eq_if:
haftmann@33361
  1571
     "a mod (-b::int) = (if a mod b = 0 then 0 else  (a mod b) - b)"
huffman@47159
  1572
by (simp add: zmod_zminus1_eq_if mod_minus_right)
haftmann@33361
  1573
haftmann@33361
  1574
lemma zmod_zminus2_not_zero:
haftmann@33361
  1575
  fixes k l :: int
haftmann@33361
  1576
  shows "k mod - l \<noteq> 0 \<Longrightarrow> k mod l \<noteq> 0"
haftmann@33361
  1577
  unfolding zmod_zminus2_eq_if by auto 
haftmann@33361
  1578
haftmann@33361
  1579
huffman@46551
  1580
subsubsection {* Computation of Division and Remainder *}
haftmann@33361
  1581
haftmann@33361
  1582
lemma div_eq_minus1: "(0::int) < b ==> -1 div b = -1"
haftmann@33361
  1583
by (simp add: div_int_def divmod_int_def)
haftmann@33361
  1584
haftmann@33361
  1585
lemma zmod_minus1: "(0::int) < b ==> -1 mod b = b - 1"
haftmann@33361
  1586
by (simp add: mod_int_def divmod_int_def)
haftmann@33361
  1587
haftmann@33361
  1588
text{*a positive, b positive *}
haftmann@33361
  1589
haftmann@33361
  1590
lemma div_pos_pos: "[| 0 < a;  0 \<le> b |] ==> a div b = fst (posDivAlg a b)"
haftmann@33361
  1591
by (simp add: div_int_def divmod_int_def)
haftmann@33361
  1592
haftmann@33361
  1593
lemma mod_pos_pos: "[| 0 < a;  0 \<le> b |] ==> a mod b = snd (posDivAlg a b)"
haftmann@33361
  1594
by (simp add: mod_int_def divmod_int_def)
haftmann@33361
  1595
haftmann@33361
  1596
text{*a negative, b positive *}
haftmann@33361
  1597
haftmann@33361
  1598
lemma div_neg_pos: "[| a < 0;  0 < b |] ==> a div b = fst (negDivAlg a b)"
haftmann@33361
  1599
by (simp add: div_int_def divmod_int_def)
haftmann@33361
  1600
haftmann@33361
  1601
lemma mod_neg_pos: "[| a < 0;  0 < b |] ==> a mod b = snd (negDivAlg a b)"
haftmann@33361
  1602
by (simp add: mod_int_def divmod_int_def)
haftmann@33361
  1603
haftmann@33361
  1604
text{*a positive, b negative *}
haftmann@33361
  1605
haftmann@33361
  1606
lemma div_pos_neg:
huffman@46560
  1607
     "[| 0 < a;  b < 0 |] ==> a div b = fst (apsnd uminus (negDivAlg (-a) (-b)))"
haftmann@33361
  1608
by (simp add: div_int_def divmod_int_def)
haftmann@33361
  1609
haftmann@33361
  1610
lemma mod_pos_neg:
huffman@46560
  1611
     "[| 0 < a;  b < 0 |] ==> a mod b = snd (apsnd uminus (negDivAlg (-a) (-b)))"
haftmann@33361
  1612
by (simp add: mod_int_def divmod_int_def)
haftmann@33361
  1613
haftmann@33361
  1614
text{*a negative, b negative *}
haftmann@33361
  1615
haftmann@33361
  1616
lemma div_neg_neg:
huffman@46560
  1617
     "[| a < 0;  b \<le> 0 |] ==> a div b = fst (apsnd uminus (posDivAlg (-a) (-b)))"
haftmann@33361
  1618
by (simp add: div_int_def divmod_int_def)
haftmann@33361
  1619
haftmann@33361
  1620
lemma mod_neg_neg:
huffman@46560
  1621
     "[| a < 0;  b \<le> 0 |] ==> a mod b = snd (apsnd uminus (posDivAlg (-a) (-b)))"
haftmann@33361
  1622
by (simp add: mod_int_def divmod_int_def)
haftmann@33361
  1623
haftmann@33361
  1624
text {*Simplify expresions in which div and mod combine numerical constants*}
haftmann@33361
  1625
huffman@45530
  1626
lemma int_div_pos_eq: "\<lbrakk>(a::int) = b * q + r; 0 \<le> r; r < b\<rbrakk> \<Longrightarrow> a div b = q"
huffman@47140
  1627
  by (rule div_int_unique [of a b q r]) (simp add: divmod_int_rel_def)
huffman@45530
  1628
huffman@45530
  1629
lemma int_div_neg_eq: "\<lbrakk>(a::int) = b * q + r; r \<le> 0; b < r\<rbrakk> \<Longrightarrow> a div b = q"
huffman@47140
  1630
  by (rule div_int_unique [of a b q r],
bulwahn@46552
  1631
    simp add: divmod_int_rel_def)
huffman@45530
  1632
huffman@45530
  1633
lemma int_mod_pos_eq: "\<lbrakk>(a::int) = b * q + r; 0 \<le> r; r < b\<rbrakk> \<Longrightarrow> a mod b = r"
huffman@47140
  1634
  by (rule mod_int_unique [of a b q r],
bulwahn@46552
  1635
    simp add: divmod_int_rel_def)
huffman@45530
  1636
huffman@45530
  1637
lemma int_mod_neg_eq: "\<lbrakk>(a::int) = b * q + r; r \<le> 0; b < r\<rbrakk> \<Longrightarrow> a mod b = r"
huffman@47140
  1638
  by (rule mod_int_unique [of a b q r],
bulwahn@46552
  1639
    simp add: divmod_int_rel_def)
huffman@45530
  1640
haftmann@33361
  1641
(* simprocs adapted from HOL/ex/Binary.thy *)
haftmann@33361
  1642
ML {*
haftmann@33361
  1643
local
huffman@45530
  1644
  val mk_number = HOLogic.mk_number HOLogic.intT
huffman@45530
  1645
  val plus = @{term "plus :: int \<Rightarrow> int \<Rightarrow> int"}
huffman@45530
  1646
  val times = @{term "times :: int \<Rightarrow> int \<Rightarrow> int"}
huffman@45530
  1647
  val zero = @{term "0 :: int"}
huffman@45530
  1648
  val less = @{term "op < :: int \<Rightarrow> int \<Rightarrow> bool"}
huffman@45530
  1649
  val le = @{term "op \<le> :: int \<Rightarrow> int \<Rightarrow> bool"}
huffman@45530
  1650
  val simps = @{thms arith_simps} @ @{thms rel_simps} @
huffman@47108
  1651
    map (fn th => th RS sym) [@{thm numeral_1_eq_1}]
huffman@45530
  1652
  fun prove ctxt goal = Goal.prove ctxt [] [] (HOLogic.mk_Trueprop goal)
wenzelm@51717
  1653
    (K (ALLGOALS (full_simp_tac (put_simpset HOL_basic_ss ctxt addsimps simps))));
wenzelm@51717
  1654
  fun binary_proc proc ctxt ct =
haftmann@33361
  1655
    (case Thm.term_of ct of
haftmann@33361
  1656
      _ $ t $ u =>
haftmann@33361
  1657
      (case try (pairself (`(snd o HOLogic.dest_number))) (t, u) of
wenzelm@51717
  1658
        SOME args => proc ctxt args
haftmann@33361
  1659
      | NONE => NONE)
haftmann@33361
  1660
    | _ => NONE);
haftmann@33361
  1661
in
huffman@45530
  1662
  fun divmod_proc posrule negrule =
huffman@45530
  1663
    binary_proc (fn ctxt => fn ((a, t), (b, u)) =>
huffman@45530
  1664
      if b = 0 then NONE else let
huffman@45530
  1665
        val (q, r) = pairself mk_number (Integer.div_mod a b)
huffman@45530
  1666
        val goal1 = HOLogic.mk_eq (t, plus $ (times $ u $ q) $ r)
huffman@45530
  1667
        val (goal2, goal3, rule) = if b > 0
huffman@45530
  1668
          then (le $ zero $ r, less $ r $ u, posrule RS eq_reflection)
huffman@45530
  1669
          else (le $ r $ zero, less $ u $ r, negrule RS eq_reflection)
huffman@45530
  1670
      in SOME (rule OF map (prove ctxt) [goal1, goal2, goal3]) end)
haftmann@33361
  1671
end
haftmann@33361
  1672
*}
haftmann@33361
  1673
huffman@47108
  1674
simproc_setup binary_int_div
huffman@47108
  1675
  ("numeral m div numeral n :: int" |
huffman@47108
  1676
   "numeral m div neg_numeral n :: int" |
huffman@47108
  1677
   "neg_numeral m div numeral n :: int" |
huffman@47108
  1678
   "neg_numeral m div neg_numeral n :: int") =
huffman@45530
  1679
  {* K (divmod_proc @{thm int_div_pos_eq} @{thm int_div_neg_eq}) *}
haftmann@33361
  1680
huffman@47108
  1681
simproc_setup binary_int_mod
huffman@47108
  1682
  ("numeral m mod numeral n :: int" |
huffman@47108
  1683
   "numeral m mod neg_numeral n :: int" |
huffman@47108
  1684
   "neg_numeral m mod numeral n :: int" |
huffman@47108
  1685
   "neg_numeral m mod neg_numeral n :: int") =
huffman@45530
  1686
  {* K (divmod_proc @{thm int_mod_pos_eq} @{thm int_mod_neg_eq}) *}
haftmann@33361
  1687
huffman@47108
  1688
lemmas posDivAlg_eqn_numeral [simp] =
huffman@47108
  1689
    posDivAlg_eqn [of "numeral v" "numeral w", OF zero_less_numeral] for v w
huffman@47108
  1690
huffman@47108
  1691
lemmas negDivAlg_eqn_numeral [simp] =
huffman@47108
  1692
    negDivAlg_eqn [of "numeral v" "neg_numeral w", OF zero_less_numeral] for v w
haftmann@33361
  1693
haftmann@33361
  1694
haftmann@33361
  1695
text{*Special-case simplification *}
haftmann@33361
  1696
haftmann@33361
  1697
(** The last remaining special cases for constant arithmetic:
haftmann@33361
  1698
    1 div z and 1 mod z **)
haftmann@33361
  1699
huffman@47108
  1700
lemmas div_pos_pos_1_numeral [simp] =
huffman@47108
  1701
  div_pos_pos [OF zero_less_one, of "numeral w", OF zero_le_numeral] for w
huffman@47108
  1702
huffman@47108
  1703
lemmas div_pos_neg_1_numeral [simp] =
huffman@47108
  1704
  div_pos_neg [OF zero_less_one, of "neg_numeral w",
huffman@47108
  1705
  OF neg_numeral_less_zero] for w
huffman@47108
  1706
huffman@47108
  1707
lemmas mod_pos_pos_1_numeral [simp] =
huffman@47108
  1708
  mod_pos_pos [OF zero_less_one, of "numeral w", OF zero_le_numeral] for w
huffman@47108
  1709
huffman@47108
  1710
lemmas mod_pos_neg_1_numeral [simp] =
huffman@47108
  1711
  mod_pos_neg [OF zero_less_one, of "neg_numeral w",
huffman@47108
  1712
  OF neg_numeral_less_zero] for w
huffman@47108
  1713
huffman@47108
  1714
lemmas posDivAlg_eqn_1_numeral [simp] =
huffman@47108
  1715
    posDivAlg_eqn [of concl: 1 "numeral w", OF zero_less_numeral] for w
huffman@47108
  1716
huffman@47108
  1717
lemmas negDivAlg_eqn_1_numeral [simp] =
huffman@47108
  1718
    negDivAlg_eqn [of concl: 1 "numeral w", OF zero_less_numeral] for w
haftmann@33361
  1719
haftmann@33361
  1720
huffman@46551
  1721
subsubsection {* Monotonicity in the First Argument (Dividend) *}
haftmann@33361
  1722
haftmann@33361
  1723
lemma zdiv_mono1: "[| a \<le> a';  0 < (b::int) |] ==> a div b \<le> a' div b"
haftmann@33361
  1724
apply (cut_tac a = a and b = b in zmod_zdiv_equality)
haftmann@33361
  1725
apply (cut_tac a = a' and b = b in zmod_zdiv_equality)
haftmann@33361
  1726
apply (rule unique_quotient_lemma)
haftmann@33361
  1727
apply (erule subst)
haftmann@33361
  1728
apply (erule subst, simp_all)
haftmann@33361
  1729
done
haftmann@33361
  1730
haftmann@33361
  1731
lemma zdiv_mono1_neg: "[| a \<le> a';  (b::int) < 0 |] ==> a' div b \<le> a div b"
haftmann@33361
  1732
apply (cut_tac a = a and b = b in zmod_zdiv_equality)
haftmann@33361
  1733
apply (cut_tac a = a' and b = b in zmod_zdiv_equality)
haftmann@33361
  1734
apply (rule unique_quotient_lemma_neg)
haftmann@33361
  1735
apply (erule subst)
haftmann@33361
  1736
apply (erule subst, simp_all)
haftmann@33361
  1737
done
haftmann@33361
  1738
haftmann@33361
  1739
huffman@46551
  1740
subsubsection {* Monotonicity in the Second Argument (Divisor) *}
haftmann@33361
  1741
haftmann@33361
  1742
lemma q_pos_lemma:
haftmann@33361
  1743
     "[| 0 \<le> b'*q' + r'; r' < b';  0 < b' |] ==> 0 \<le> (q'::int)"
haftmann@33361
  1744
apply (subgoal_tac "0 < b'* (q' + 1) ")
haftmann@33361
  1745
 apply (simp add: zero_less_mult_iff)
webertj@49962
  1746
apply (simp add: distrib_left)
haftmann@33361
  1747
done
haftmann@33361
  1748
haftmann@33361
  1749
lemma zdiv_mono2_lemma:
haftmann@33361
  1750
     "[| b*q + r = b'*q' + r';  0 \<le> b'*q' + r';   
haftmann@33361
  1751
         r' < b';  0 \<le> r;  0 < b';  b' \<le> b |]   
haftmann@33361
  1752
      ==> q \<le> (q'::int)"
haftmann@33361
  1753
apply (frule q_pos_lemma, assumption+) 
haftmann@33361
  1754
apply (subgoal_tac "b*q < b* (q' + 1) ")
haftmann@33361
  1755
 apply (simp add: mult_less_cancel_left)
haftmann@33361
  1756
apply (subgoal_tac "b*q = r' - r + b'*q'")
haftmann@33361
  1757
 prefer 2 apply simp
webertj@49962
  1758
apply (simp (no_asm_simp) add: distrib_left)
huffman@44766
  1759
apply (subst add_commute, rule add_less_le_mono, arith)
haftmann@33361
  1760
apply (rule mult_right_mono, auto)
haftmann@33361
  1761
done
haftmann@33361
  1762
haftmann@33361
  1763
lemma zdiv_mono2:
haftmann@33361
  1764
     "[| (0::int) \<le> a;  0 < b';  b' \<le> b |] ==> a div b \<le> a div b'"
haftmann@33361
  1765
apply (subgoal_tac "b \<noteq> 0")
haftmann@33361
  1766
 prefer 2 apply arith
haftmann@33361
  1767
apply (cut_tac a = a and b = b in zmod_zdiv_equality)
haftmann@33361
  1768
apply (cut_tac a = a and b = b' in zmod_zdiv_equality)
haftmann@33361
  1769
apply (rule zdiv_mono2_lemma)
haftmann@33361
  1770
apply (erule subst)
haftmann@33361
  1771
apply (erule subst, simp_all)
haftmann@33361
  1772
done
haftmann@33361
  1773
haftmann@33361
  1774
lemma q_neg_lemma:
haftmann@33361
  1775
     "[| b'*q' + r' < 0;  0 \<le> r';  0 < b' |] ==> q' \<le> (0::int)"
haftmann@33361
  1776
apply (subgoal_tac "b'*q' < 0")
haftmann@33361
  1777
 apply (simp add: mult_less_0_iff, arith)
haftmann@33361
  1778
done
haftmann@33361
  1779
haftmann@33361
  1780
lemma zdiv_mono2_neg_lemma:
haftmann@33361
  1781
     "[| b*q + r = b'*q' + r';  b'*q' + r' < 0;   
haftmann@33361
  1782
         r < b;  0 \<le> r';  0 < b';  b' \<le> b |]   
haftmann@33361
  1783
      ==> q' \<le> (q::int)"
haftmann@33361
  1784
apply (frule q_neg_lemma, assumption+) 
haftmann@33361
  1785
apply (subgoal_tac "b*q' < b* (q + 1) ")
haftmann@33361
  1786
 apply (simp add: mult_less_cancel_left)
webertj@49962
  1787
apply (simp add: distrib_left)
haftmann@33361
  1788
apply (subgoal_tac "b*q' \<le> b'*q'")
haftmann@33361
  1789
 prefer 2 apply (simp add: mult_right_mono_neg, arith)
haftmann@33361
  1790
done
haftmann@33361
  1791
haftmann@33361
  1792
lemma zdiv_mono2_neg:
haftmann@33361
  1793
     "[| a < (0::int);  0 < b';  b' \<le> b |] ==> a div b' \<le> a div b"
haftmann@33361
  1794
apply (cut_tac a = a and b = b in zmod_zdiv_equality)
haftmann@33361
  1795
apply (cut_tac a = a and b = b' in zmod_zdiv_equality)
haftmann@33361
  1796
apply (rule zdiv_mono2_neg_lemma)
haftmann@33361
  1797
apply (erule subst)
haftmann@33361
  1798
apply (erule subst, simp_all)
haftmann@33361
  1799
done
haftmann@33361
  1800
haftmann@33361
  1801
huffman@46551
  1802
subsubsection {* More Algebraic Laws for div and mod *}
haftmann@33361
  1803
haftmann@33361
  1804
text{*proving (a*b) div c = a * (b div c) + a * (b mod c) *}
haftmann@33361
  1805
haftmann@33361
  1806
lemma zmult1_lemma:
bulwahn@46552
  1807
     "[| divmod_int_rel b c (q, r) |]  
haftmann@33361
  1808
      ==> divmod_int_rel (a * b) c (a*q + a*r div c, a*r mod c)"
webertj@49962
  1809
by (auto simp add: split_ifs divmod_int_rel_def linorder_neq_iff distrib_left mult_ac)
haftmann@33361
  1810
haftmann@33361
  1811
lemma zdiv_zmult1_eq: "(a*b) div c = a*(b div c) + a*(b mod c) div (c::int)"
haftmann@33361
  1812
apply (case_tac "c = 0", simp)
huffman@47140
  1813
apply (blast intro: divmod_int_rel_div_mod [THEN zmult1_lemma, THEN div_int_unique])
haftmann@33361
  1814
done
haftmann@33361
  1815
haftmann@33361
  1816
text{*proving (a+b) div c = a div c + b div c + ((a mod c + b mod c) div c) *}
haftmann@33361
  1817
haftmann@33361
  1818
lemma zadd1_lemma:
bulwahn@46552
  1819
     "[| divmod_int_rel a c (aq, ar);  divmod_int_rel b c (bq, br) |]  
haftmann@33361
  1820
      ==> divmod_int_rel (a+b) c (aq + bq + (ar+br) div c, (ar+br) mod c)"
webertj@49962
  1821
by (force simp add: split_ifs divmod_int_rel_def linorder_neq_iff distrib_left)
haftmann@33361
  1822
haftmann@33361
  1823
(*NOT suitable for rewriting: the RHS has an instance of the LHS*)
haftmann@33361
  1824
lemma zdiv_zadd1_eq:
haftmann@33361
  1825
     "(a+b) div (c::int) = a div c + b div c + ((a mod c + b mod c) div c)"
haftmann@33361
  1826
apply (case_tac "c = 0", simp)
huffman@47140
  1827
apply (blast intro: zadd1_lemma [OF divmod_int_rel_div_mod divmod_int_rel_div_mod] div_int_unique)
haftmann@33361
  1828
done
haftmann@33361
  1829
haftmann@33361
  1830
lemma posDivAlg_div_mod:
haftmann@33361
  1831
  assumes "k \<ge> 0"
haftmann@33361
  1832
  and "l \<ge> 0"
haftmann@33361
  1833
  shows "posDivAlg k l = (k div l, k mod l)"
haftmann@33361
  1834
proof (cases "l = 0")
haftmann@33361
  1835
  case True then show ?thesis by (simp add: posDivAlg.simps)
haftmann@33361
  1836
next
haftmann@33361
  1837
  case False with assms posDivAlg_correct
haftmann@33361
  1838
    have "divmod_int_rel k l (fst (posDivAlg k l), snd (posDivAlg k l))"
haftmann@33361
  1839
    by simp
huffman@47140
  1840
  from div_int_unique [OF this] mod_int_unique [OF this]
haftmann@33361
  1841
  show ?thesis by simp
haftmann@33361
  1842
qed
haftmann@33361
  1843
haftmann@33361
  1844
lemma negDivAlg_div_mod:
haftmann@33361
  1845
  assumes "k < 0"
haftmann@33361
  1846
  and "l > 0"
haftmann@33361
  1847
  shows "negDivAlg k l = (k div l, k mod l)"
haftmann@33361
  1848
proof -
haftmann@33361
  1849
  from assms have "l \<noteq> 0" by simp
haftmann@33361
  1850
  from assms negDivAlg_correct
haftmann@33361
  1851
    have "divmod_int_rel k l (fst (negDivAlg k l), snd (negDivAlg k l))"
haftmann@33361
  1852
    by simp
huffman@47140
  1853
  from div_int_unique [OF this] mod_int_unique [OF this]
haftmann@33361
  1854
  show ?thesis by simp
haftmann@33361
  1855
qed
haftmann@33361
  1856
haftmann@33361
  1857
lemma zmod_eq_0_iff: "(m mod d = 0) = (EX q::int. m = d*q)"
haftmann@33361
  1858
by (simp add: dvd_eq_mod_eq_0 [symmetric] dvd_def)
haftmann@33361
  1859
haftmann@33361
  1860
(* REVISIT: should this be generalized to all semiring_div types? *)
haftmann@33361
  1861
lemmas zmod_eq_0D [dest!] = zmod_eq_0_iff [THEN iffD1]
haftmann@33361
  1862
huffman@47108
  1863
lemma zmod_zdiv_equality':
huffman@47108
  1864
  "(m\<Colon>int) mod n = m - (m div n) * n"
huffman@47141
  1865
  using mod_div_equality [of m n] by arith
huffman@47108
  1866
haftmann@33361
  1867
huffman@46551
  1868
subsubsection {* Proving  @{term "a div (b*c) = (a div b) div c"} *}
haftmann@33361
  1869
haftmann@33361
  1870
(*The condition c>0 seems necessary.  Consider that 7 div ~6 = ~2 but
haftmann@33361
  1871
  7 div 2 div ~3 = 3 div ~3 = ~1.  The subcase (a div b) mod c = 0 seems
haftmann@33361
  1872
  to cause particular problems.*)
haftmann@33361
  1873
haftmann@33361
  1874
text{*first, four lemmas to bound the remainder for the cases b<0 and b>0 *}
haftmann@33361
  1875
haftmann@33361
  1876
lemma zmult2_lemma_aux1: "[| (0::int) < c;  b < r;  r \<le> 0 |] ==> b*c < b*(q mod c) + r"
haftmann@33361
  1877
apply (subgoal_tac "b * (c - q mod c) < r * 1")
haftmann@33361
  1878
 apply (simp add: algebra_simps)
haftmann@33361
  1879
apply (rule order_le_less_trans)
haftmann@33361
  1880
 apply (erule_tac [2] mult_strict_right_mono)
haftmann@33361
  1881
 apply (rule mult_left_mono_neg)
huffman@35216
  1882
  using add1_zle_eq[of "q mod c"]apply(simp add: algebra_simps)
haftmann@33361
  1883
 apply (simp)
haftmann@33361
  1884
apply (simp)
haftmann@33361
  1885
done
haftmann@33361
  1886
haftmann@33361
  1887
lemma zmult2_lemma_aux2:
haftmann@33361
  1888
     "[| (0::int) < c;   b < r;  r \<le> 0 |] ==> b * (q mod c) + r \<le> 0"
haftmann@33361
  1889
apply (subgoal_tac "b * (q mod c) \<le> 0")
haftmann@33361
  1890
 apply arith
haftmann@33361
  1891
apply (simp add: mult_le_0_iff)
haftmann@33361
  1892
done
haftmann@33361
  1893
haftmann@33361
  1894
lemma zmult2_lemma_aux3: "[| (0::int) < c;  0 \<le> r;  r < b |] ==> 0 \<le> b * (q mod c) + r"
haftmann@33361
  1895
apply (subgoal_tac "0 \<le> b * (q mod c) ")
haftmann@33361
  1896
apply arith
haftmann@33361
  1897
apply (simp add: zero_le_mult_iff)
haftmann@33361
  1898
done
haftmann@33361
  1899
haftmann@33361
  1900
lemma zmult2_lemma_aux4: "[| (0::int) < c; 0 \<le> r; r < b |] ==> b * (q mod c) + r < b * c"
haftmann@33361
  1901
apply (subgoal_tac "r * 1 < b * (c - q mod c) ")
haftmann@33361
  1902
 apply (simp add: right_diff_distrib)
haftmann@33361
  1903
apply (rule order_less_le_trans)
haftmann@33361
  1904
 apply (erule mult_strict_right_mono)
haftmann@33361
  1905
 apply (rule_tac [2] mult_left_mono)
haftmann@33361
  1906
  apply simp
huffman@35216
  1907
 using add1_zle_eq[of "q mod c"] apply (simp add: algebra_simps)
haftmann@33361
  1908
apply simp
haftmann@33361
  1909
done
haftmann@33361
  1910
bulwahn@46552
  1911
lemma zmult2_lemma: "[| divmod_int_rel a b (q, r); 0 < c |]  
haftmann@33361
  1912
      ==> divmod_int_rel a (b * c) (q div c, b*(q mod c) + r)"
haftmann@33361
  1913
by (auto simp add: mult_ac divmod_int_rel_def linorder_neq_iff
webertj@49962
  1914
                   zero_less_mult_iff distrib_left [symmetric] 
huffman@47139
  1915
                   zmult2_lemma_aux1 zmult2_lemma_aux2 zmult2_lemma_aux3 zmult2_lemma_aux4 mult_less_0_iff split: split_if_asm)
haftmann@33361
  1916
haftmann@33361
  1917
lemma zdiv_zmult2_eq: "(0::int) < c ==> a div (b*c) = (a div b) div c"
haftmann@33361
  1918
apply (case_tac "b = 0", simp)
huffman@47140
  1919
apply (force simp add: divmod_int_rel_div_mod [THEN zmult2_lemma, THEN div_int_unique])
haftmann@33361
  1920
done
haftmann@33361
  1921
haftmann@33361
  1922
lemma zmod_zmult2_eq:
haftmann@33361
  1923
     "(0::int) < c ==> a mod (b*c) = b*(a div b mod c) + a mod b"
haftmann@33361
  1924
apply (case_tac "b = 0", simp)
huffman@47140
  1925
apply (force simp add: divmod_int_rel_div_mod [THEN zmult2_lemma, THEN mod_int_unique])
haftmann@33361
  1926
done
haftmann@33361
  1927
huffman@47108
  1928
lemma div_pos_geq:
huffman@47108
  1929
  fixes k l :: int
huffman@47108
  1930
  assumes "0 < l" and "l \<le> k"
huffman@47108
  1931
  shows "k div l = (k - l) div l + 1"
huffman@47108
  1932
proof -
huffman@47108
  1933
  have "k = (k - l) + l" by simp
huffman@47108
  1934
  then obtain j where k: "k = j + l" ..
huffman@47108
  1935
  with assms show ?thesis by simp
huffman@47108
  1936
qed
huffman@47108
  1937
huffman@47108
  1938
lemma mod_pos_geq:
huffman@47108
  1939
  fixes k l :: int
huffman@47108
  1940
  assumes "0 < l" and "l \<le> k"
huffman@47108
  1941
  shows "k mod l = (k - l) mod l"
huffman@47108
  1942
proof -
huffman@47108
  1943
  have "k = (k - l) + l" by simp
huffman@47108
  1944
  then obtain j where k: "k = j + l" ..
huffman@47108
  1945
  with assms show ?thesis by simp
huffman@47108
  1946
qed
huffman@47108
  1947
haftmann@33361
  1948
huffman@46551
  1949
subsubsection {* Splitting Rules for div and mod *}
haftmann@33361
  1950
haftmann@33361
  1951
text{*The proofs of the two lemmas below are essentially identical*}
haftmann@33361
  1952
haftmann@33361
  1953
lemma split_pos_lemma:
haftmann@33361
  1954
 "0<k ==> 
haftmann@33361
  1955
    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
  1956
apply (rule iffI, clarify)
haftmann@33361
  1957
 apply (erule_tac P="P ?x ?y" in rev_mp)  
haftmann@33361
  1958
 apply (subst mod_add_eq) 
haftmann@33361
  1959
 apply (subst zdiv_zadd1_eq) 
haftmann@33361
  1960
 apply (simp add: div_pos_pos_trivial mod_pos_pos_trivial)  
haftmann@33361
  1961
txt{*converse direction*}
haftmann@33361
  1962
apply (drule_tac x = "n div k" in spec) 
haftmann@33361
  1963
apply (drule_tac x = "n mod k" in spec, simp)
haftmann@33361
  1964
done
haftmann@33361
  1965
haftmann@33361
  1966
lemma split_neg_lemma:
haftmann@33361
  1967
 "k<0 ==>
haftmann@33361
  1968
    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
  1969
apply (rule iffI, clarify)
haftmann@33361
  1970
 apply (erule_tac P="P ?x ?y" in rev_mp)  
haftmann@33361
  1971
 apply (subst mod_add_eq) 
haftmann@33361
  1972
 apply (subst zdiv_zadd1_eq) 
haftmann@33361
  1973
 apply (simp add: div_neg_neg_trivial mod_neg_neg_trivial)  
haftmann@33361
  1974
txt{*converse direction*}
haftmann@33361
  1975
apply (drule_tac x = "n div k" in spec) 
haftmann@33361
  1976
apply (drule_tac x = "n mod k" in spec, simp)
haftmann@33361
  1977
done
haftmann@33361
  1978
haftmann@33361
  1979
lemma split_zdiv:
haftmann@33361
  1980
 "P(n div k :: int) =
haftmann@33361
  1981
  ((k = 0 --> P 0) & 
haftmann@33361
  1982
   (0<k --> (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P i)) & 
haftmann@33361
  1983
   (k<0 --> (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P i)))"
haftmann@33361
  1984
apply (case_tac "k=0", simp)
haftmann@33361
  1985
apply (simp only: linorder_neq_iff)
haftmann@33361
  1986
apply (erule disjE) 
haftmann@33361
  1987
 apply (simp_all add: split_pos_lemma [of concl: "%x y. P x"] 
haftmann@33361
  1988
                      split_neg_lemma [of concl: "%x y. P x"])
haftmann@33361
  1989
done
haftmann@33361
  1990
haftmann@33361
  1991
lemma split_zmod:
haftmann@33361
  1992
 "P(n mod k :: int) =
haftmann@33361
  1993
  ((k = 0 --> P n) & 
haftmann@33361
  1994
   (0<k --> (\<forall>i j. 0\<le>j & j<k & n = k*i + j --> P j)) & 
haftmann@33361
  1995
   (k<0 --> (\<forall>i j. k<j & j\<le>0 & n = k*i + j --> P j)))"
haftmann@33361
  1996
apply (case_tac "k=0", simp)
haftmann@33361
  1997
apply (simp only: linorder_neq_iff)
haftmann@33361
  1998
apply (erule disjE) 
haftmann@33361
  1999
 apply (simp_all add: split_pos_lemma [of concl: "%x y. P y"] 
haftmann@33361
  2000
                      split_neg_lemma [of concl: "%x y. P y"])
haftmann@33361
  2001
done
haftmann@33361
  2002
webertj@33730
  2003
text {* Enable (lin)arith to deal with @{const div} and @{const mod}
webertj@33730
  2004
  when these are applied to some constant that is of the form
huffman@47108
  2005
  @{term "numeral k"}: *}
huffman@47108
  2006
declare split_zdiv [of _ _ "numeral k", arith_split] for k
huffman@47108
  2007
declare split_zmod [of _ _ "numeral k", arith_split] for k
haftmann@33361
  2008
haftmann@33361
  2009
huffman@47166
  2010
subsubsection {* Computing @{text "div"} and @{text "mod"} with shifting *}
huffman@47166
  2011
huffman@47166
  2012
lemma pos_divmod_int_rel_mult_2:
huffman@47166
  2013
  assumes "0 \<le> b"
huffman@47166
  2014
  assumes "divmod_int_rel a b (q, r)"
huffman@47166
  2015
  shows "divmod_int_rel (1 + 2*a) (2*b) (q, 1 + 2*r)"
huffman@47166
  2016
  using assms unfolding divmod_int_rel_def by auto
huffman@47166
  2017
huffman@47166
  2018
lemma neg_divmod_int_rel_mult_2:
huffman@47166
  2019
  assumes "b \<le> 0"
huffman@47166
  2020
  assumes "divmod_int_rel (a + 1) b (q, r)"
huffman@47166
  2021
  shows "divmod_int_rel (1 + 2*a) (2*b) (q, 2*r - 1)"
huffman@47166
  2022
  using assms unfolding divmod_int_rel_def by auto
haftmann@33361
  2023
haftmann@33361
  2024
text{*computing div by shifting *}
haftmann@33361
  2025
haftmann@33361
  2026
lemma pos_zdiv_mult_2: "(0::int) \<le> a ==> (1 + 2*b) div (2*a) = b div a"
huffman@47166
  2027
  using pos_divmod_int_rel_mult_2 [OF _ divmod_int_rel_div_mod]
huffman@47166
  2028
  by (rule div_int_unique)
haftmann@33361
  2029
boehmes@35815
  2030
lemma neg_zdiv_mult_2: 
boehmes@35815
  2031
  assumes A: "a \<le> (0::int)" shows "(1 + 2*b) div (2*a) = (b+1) div a"
huffman@47166
  2032
  using neg_divmod_int_rel_mult_2 [OF A divmod_int_rel_div_mod]
huffman@47166
  2033
  by (rule div_int_unique)
haftmann@33361
  2034
huffman@47108
  2035
(* FIXME: add rules for negative numerals *)
huffman@47108
  2036
lemma zdiv_numeral_Bit0 [simp]:
huffman@47108
  2037
  "numeral (Num.Bit0 v) div numeral (Num.Bit0 w) =
huffman@47108
  2038
    numeral v div (numeral w :: int)"
huffman@47108
  2039
  unfolding numeral.simps unfolding mult_2 [symmetric]
huffman@47108
  2040
  by (rule div_mult_mult1, simp)
huffman@47108
  2041
huffman@47108
  2042
lemma zdiv_numeral_Bit1 [simp]:
huffman@47108
  2043
  "numeral (Num.Bit1 v) div numeral (Num.Bit0 w) =  
huffman@47108
  2044
    (numeral v div (numeral w :: int))"
huffman@47108
  2045
  unfolding numeral.simps
huffman@47108
  2046
  unfolding mult_2 [symmetric] add_commute [of _ 1]
huffman@47108
  2047
  by (rule pos_zdiv_mult_2, simp)
haftmann@33361
  2048
haftmann@33361
  2049
lemma pos_zmod_mult_2:
haftmann@33361
  2050
  fixes a b :: int
haftmann@33361
  2051
  assumes "0 \<le> a"
haftmann@33361
  2052
  shows "(1 + 2 * b) mod (2 * a) = 1 + 2 * (b mod a)"
huffman@47166
  2053
  using pos_divmod_int_rel_mult_2 [OF assms divmod_int_rel_div_mod]
huffman@47166
  2054
  by (rule mod_int_unique)
haftmann@33361
  2055
haftmann@33361
  2056
lemma neg_zmod_mult_2:
haftmann@33361
  2057
  fixes a b :: int
haftmann@33361
  2058
  assumes "a \<le> 0"
haftmann@33361
  2059
  shows "(1 + 2 * b) mod (2 * a) = 2 * ((b + 1) mod a) - 1"
huffman@47166
  2060
  using neg_divmod_int_rel_mult_2 [OF assms divmod_int_rel_div_mod]
huffman@47166
  2061
  by (rule mod_int_unique)
haftmann@33361
  2062
huffman@47108
  2063
(* FIXME: add rules for negative numerals *)
huffman@47108
  2064
lemma zmod_numeral_Bit0 [simp]:
huffman@47108
  2065
  "numeral (Num.Bit0 v) mod numeral (Num.Bit0 w) =  
huffman@47108
  2066
    (2::int) * (numeral v mod numeral w)"
huffman@47108
  2067
  unfolding numeral_Bit0 [of v] numeral_Bit0 [of w]
huffman@47108
  2068
  unfolding mult_2 [symmetric] by (rule mod_mult_mult1)
huffman@47108
  2069
huffman@47108
  2070
lemma zmod_numeral_Bit1 [simp]:
huffman@47108
  2071
  "numeral (Num.Bit1 v) mod numeral (Num.Bit0 w) =
huffman@47108
  2072
    2 * (numeral v mod numeral w) + (1::int)"
huffman@47108
  2073
  unfolding numeral_Bit1 [of v] numeral_Bit0 [of w]
huffman@47108
  2074
  unfolding mult_2 [symmetric] add_commute [of _ 1]
huffman@47108
  2075
  by (rule pos_zmod_mult_2, simp)
haftmann@33361
  2076
nipkow@39489
  2077
lemma zdiv_eq_0_iff:
nipkow@39489
  2078
 "(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
  2079
proof
nipkow@39489
  2080
  assume ?L
nipkow@39489
  2081
  have "?L \<longrightarrow> ?R" by (rule split_zdiv[THEN iffD2]) simp
nipkow@39489
  2082
  with `?L` show ?R by blast
nipkow@39489
  2083
next
nipkow@39489
  2084
  assume ?R thus ?L
nipkow@39489
  2085
    by(auto simp: div_pos_pos_trivial div_neg_neg_trivial)
nipkow@39489
  2086
qed
nipkow@39489
  2087
nipkow@39489
  2088
huffman@46551
  2089
subsubsection {* Quotients of Signs *}
haftmann@33361
  2090
haftmann@33361
  2091
lemma div_neg_pos_less0: "[| a < (0::int);  0 < b |] ==> a div b < 0"
haftmann@33361
  2092
apply (subgoal_tac "a div b \<le> -1", force)
haftmann@33361
  2093
apply (rule order_trans)
haftmann@33361
  2094
apply (rule_tac a' = "-1" in zdiv_mono1)
haftmann@33361
  2095
apply (auto simp add: div_eq_minus1)
haftmann@33361
  2096
done
haftmann@33361
  2097
haftmann@33361
  2098
lemma div_nonneg_neg_le0: "[| (0::int) \<le> a; b < 0 |] ==> a div b \<le> 0"
haftmann@33361
  2099
by (drule zdiv_mono1_neg, auto)
haftmann@33361
  2100
haftmann@33361
  2101
lemma div_nonpos_pos_le0: "[| (a::int) \<le> 0; b > 0 |] ==> a div b \<le> 0"
haftmann@33361
  2102
by (drule zdiv_mono1, auto)
haftmann@33361
  2103
nipkow@33804
  2104
text{* Now for some equivalences of the form @{text"a div b >=< 0 \<longleftrightarrow> \<dots>"}
nipkow@33804
  2105
conditional upon the sign of @{text a} or @{text b}. There are many more.
nipkow@33804
  2106
They should all be simp rules unless that causes too much search. *}
nipkow@33804
  2107
haftmann@33361
  2108
lemma pos_imp_zdiv_nonneg_iff: "(0::int) < b ==> (0 \<le> a div b) = (0 \<le> a)"
haftmann@33361
  2109
apply auto
haftmann@33361
  2110
apply (drule_tac [2] zdiv_mono1)
haftmann@33361
  2111
apply (auto simp add: linorder_neq_iff)
haftmann@33361
  2112
apply (simp (no_asm_use) add: linorder_not_less [symmetric])
haftmann@33361
  2113
apply (blast intro: div_neg_pos_less0)
haftmann@33361
  2114
done
haftmann@33361
  2115
haftmann@33361
  2116
lemma neg_imp_zdiv_nonneg_iff:
nipkow@33804
  2117
  "b < (0::int) ==> (0 \<le> a div b) = (a \<le> (0::int))"
huffman@47159
  2118
apply (subst div_minus_minus [symmetric])
haftmann@33361
  2119
apply (subst pos_imp_zdiv_nonneg_iff, auto)
haftmann@33361
  2120
done
haftmann@33361
  2121
haftmann@33361
  2122
(*But not (a div b \<le> 0 iff a\<le>0); consider a=1, b=2 when a div b = 0.*)
haftmann@33361
  2123
lemma pos_imp_zdiv_neg_iff: "(0::int) < b ==> (a div b < 0) = (a < 0)"
haftmann@33361
  2124
by (simp add: linorder_not_le [symmetric] pos_imp_zdiv_nonneg_iff)
haftmann@33361
  2125
nipkow@39489
  2126
lemma pos_imp_zdiv_pos_iff:
nipkow@39489
  2127
  "0<k \<Longrightarrow> 0 < (i::int) div k \<longleftrightarrow> k \<le> i"
nipkow@39489
  2128
using pos_imp_zdiv_nonneg_iff[of k i] zdiv_eq_0_iff[of i k]
nipkow@39489
  2129
by arith
nipkow@39489
  2130
haftmann@33361
  2131
(*Again the law fails for \<le>: consider a = -1, b = -2 when a div b = 0*)
haftmann@33361
  2132
lemma neg_imp_zdiv_neg_iff: "b < (0::int) ==> (a div b < 0) = (0 < a)"
haftmann@33361
  2133
by (simp add: linorder_not_le [symmetric] neg_imp_zdiv_nonneg_iff)
haftmann@33361
  2134
nipkow@33804
  2135
lemma nonneg1_imp_zdiv_pos_iff:
nipkow@33804
  2136
  "(0::int) <= a \<Longrightarrow> (a div b > 0) = (a >= b & b>0)"
nipkow@33804
  2137
apply rule
nipkow@33804
  2138
 apply rule
nipkow@33804
  2139
  using div_pos_pos_trivial[of a b]apply arith
nipkow@33804
  2140
 apply(cases "b=0")apply simp
nipkow@33804
  2141
 using div_nonneg_neg_le0[of a b]apply arith
nipkow@33804
  2142
using int_one_le_iff_zero_less[of "a div b"] zdiv_mono1[of b a b]apply simp
nipkow@33804
  2143
done
nipkow@33804
  2144
nipkow@39489
  2145
lemma zmod_le_nonneg_dividend: "(m::int) \<ge> 0 ==> m mod k \<le> m"
nipkow@39489
  2146
apply (rule split_zmod[THEN iffD2])
nipkow@44890
  2147
apply(fastforce dest: q_pos_lemma intro: split_mult_pos_le)
nipkow@39489
  2148
done
nipkow@39489
  2149
nipkow@39489
  2150
haftmann@33361
  2151
subsubsection {* The Divides Relation *}
haftmann@33361
  2152
huffman@47268
  2153
lemma dvd_neg_numeral_left [simp]:
huffman@47268
  2154
  fixes y :: "'a::comm_ring_1"
huffman@47268
  2155
  shows "(neg_numeral k) dvd y \<longleftrightarrow> (numeral k) dvd y"
huffman@47268
  2156
  unfolding neg_numeral_def minus_dvd_iff ..
huffman@47268
  2157
huffman@47268
  2158
lemma dvd_neg_numeral_right [simp]:
huffman@47268
  2159
  fixes x :: "'a::comm_ring_1"
huffman@47268
  2160
  shows "x dvd (neg_numeral k) \<longleftrightarrow> x dvd (numeral k)"
huffman@47268
  2161
  unfolding neg_numeral_def dvd_minus_iff ..
haftmann@33361
  2162
huffman@47108
  2163
lemmas dvd_eq_mod_eq_0_numeral [simp] =
huffman@47108
  2164
  dvd_eq_mod_eq_0 [of "numeral x" "numeral y"] for x y
huffman@47108
  2165
huffman@47108
  2166
huffman@47108
  2167
subsubsection {* Further properties *}
huffman@47108
  2168
haftmann@33361
  2169
lemma zmult_div_cancel: "(n::int) * (m div n) = m - (m mod n)"
haftmann@33361
  2170
  using zmod_zdiv_equality[where a="m" and b="n"]
huffman@47142
  2171
  by (simp add: algebra_simps) (* FIXME: generalize *)
haftmann@33361
  2172
haftmann@33361
  2173
lemma zdiv_int: "int (a div b) = (int a) div (int b)"
haftmann@33361
  2174
apply (subst split_div, auto)
haftmann@33361
  2175
apply (subst split_zdiv, auto)
haftmann@33361
  2176
apply (rule_tac a="int (b * i) + int j" and b="int b" and r="int j" and r'=ja in unique_quotient)
haftmann@33361
  2177
apply (auto simp add: divmod_int_rel_def of_nat_mult)
haftmann@33361
  2178
done
haftmann@33361
  2179
haftmann@33361
  2180
lemma zmod_int: "int (a mod b) = (int a) mod (int b)"
haftmann@33361
  2181
apply (subst split_mod, auto)
haftmann@33361
  2182
apply (subst split_zmod, auto)
haftmann@33361
  2183
apply (rule_tac a="int (b * i) + int j" and b="int b" and q="int i" and q'=ia 
haftmann@33361
  2184
       in unique_remainder)
haftmann@33361
  2185
apply (auto simp add: divmod_int_rel_def of_nat_mult)
haftmann@33361
  2186
done
haftmann@33361
  2187
haftmann@33361
  2188
lemma abs_div: "(y::int) dvd x \<Longrightarrow> abs (x div y) = abs x div abs y"
haftmann@33361
  2189
by (unfold dvd_def, cases "y=0", auto simp add: abs_mult)
haftmann@33361
  2190
haftmann@33361
  2191
text{*Suggested by Matthias Daum*}
haftmann@33361
  2192
lemma int_power_div_base:
haftmann@33361
  2193
     "\<lbrakk>0 < m; 0 < k\<rbrakk> \<Longrightarrow> k ^ m div k = (k::int) ^ (m - Suc 0)"
haftmann@33361
  2194
apply (subgoal_tac "k ^ m = k ^ ((m - Suc 0) + Suc 0)")
haftmann@33361
  2195
 apply (erule ssubst)
haftmann@33361
  2196
 apply (simp only: power_add)
haftmann@33361
  2197
 apply simp_all
haftmann@33361
  2198
done
haftmann@33361
  2199
haftmann@33361
  2200
text {* by Brian Huffman *}
haftmann@33361
  2201
lemma zminus_zmod: "- ((x::int) mod m) mod m = - x mod m"
haftmann@33361
  2202
by (rule mod_minus_eq [symmetric])
haftmann@33361
  2203
haftmann@33361
  2204
lemma zdiff_zmod_left: "(x mod m - y) mod m = (x - y) mod (m::int)"
haftmann@33361
  2205
by (rule mod_diff_left_eq [symmetric])
haftmann@33361
  2206
haftmann@33361
  2207
lemma zdiff_zmod_right: "(x - y mod m) mod m = (x - y) mod (m::int)"
haftmann@33361
  2208
by (rule mod_diff_right_eq [symmetric])
haftmann@33361
  2209
haftmann@33361
  2210
lemmas zmod_simps =
haftmann@33361
  2211
  mod_add_left_eq  [symmetric]
haftmann@33361
  2212
  mod_add_right_eq [symmetric]
huffman@47142
  2213
  mod_mult_right_eq[symmetric]
haftmann@33361
  2214
  mod_mult_left_eq [symmetric]
huffman@47164
  2215
  power_mod
haftmann@33361
  2216
  zminus_zmod zdiff_zmod_left zdiff_zmod_right
haftmann@33361
  2217
haftmann@33361
  2218
text {* Distributive laws for function @{text nat}. *}
haftmann@33361
  2219
haftmann@33361
  2220
lemma nat_div_distrib: "0 \<le> x \<Longrightarrow> nat (x div y) = nat x div nat y"
haftmann@33361
  2221
apply (rule linorder_cases [of y 0])
haftmann@33361
  2222
apply (simp add: div_nonneg_neg_le0)
haftmann@33361
  2223
apply simp
haftmann@33361
  2224
apply (simp add: nat_eq_iff pos_imp_zdiv_nonneg_iff zdiv_int)
haftmann@33361
  2225
done
haftmann@33361
  2226
haftmann@33361
  2227
(*Fails if y<0: the LHS collapses to (nat z) but the RHS doesn't*)
haftmann@33361
  2228
lemma nat_mod_distrib:
haftmann@33361
  2229
  "\<lbrakk>0 \<le> x; 0 \<le> y\<rbrakk> \<Longrightarrow> nat (x mod y) = nat x mod nat y"
haftmann@33361
  2230
apply (case_tac "y = 0", simp)
haftmann@33361
  2231
apply (simp add: nat_eq_iff zmod_int)
haftmann@33361
  2232
done
haftmann@33361
  2233
haftmann@33361
  2234
text  {* transfer setup *}
haftmann@33361
  2235
haftmann@33361
  2236
lemma transfer_nat_int_functions:
haftmann@33361
  2237
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) div (nat y) = nat (x div y)"
haftmann@33361
  2238
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> (nat x) mod (nat y) = nat (x mod y)"
haftmann@33361
  2239
  by (auto simp add: nat_div_distrib nat_mod_distrib)
haftmann@33361
  2240
haftmann@33361
  2241
lemma transfer_nat_int_function_closures:
haftmann@33361
  2242
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x div y >= 0"
haftmann@33361
  2243
    "(x::int) >= 0 \<Longrightarrow> y >= 0 \<Longrightarrow> x mod y >= 0"
haftmann@33361
  2244
  apply (cases "y = 0")
haftmann@33361
  2245
  apply (auto simp add: pos_imp_zdiv_nonneg_iff)
haftmann@33361
  2246
  apply (cases "y = 0")
haftmann@33361
  2247
  apply auto
haftmann@33361
  2248
done
haftmann@33361
  2249
haftmann@35644
  2250
declare transfer_morphism_nat_int [transfer add return:
haftmann@33361
  2251
  transfer_nat_int_functions
haftmann@33361
  2252
  transfer_nat_int_function_closures
haftmann@33361
  2253
]
haftmann@33361
  2254
haftmann@33361
  2255
lemma transfer_int_nat_functions:
haftmann@33361
  2256
    "(int x) div (int y) = int (x div y)"
haftmann@33361
  2257
    "(int x) mod (int y) = int (x mod y)"
haftmann@33361
  2258
  by (auto simp add: zdiv_int zmod_int)
haftmann@33361
  2259
haftmann@33361
  2260
lemma transfer_int_nat_function_closures:
haftmann@33361
  2261
    "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x div y)"
haftmann@33361
  2262
    "is_nat x \<Longrightarrow> is_nat y \<Longrightarrow> is_nat (x mod y)"
haftmann@33361
  2263
  by (simp_all only: is_nat_def transfer_nat_int_function_closures)
haftmann@33361
  2264
haftmann@35644
  2265
declare transfer_morphism_int_nat [transfer add return:
haftmann@33361
  2266
  transfer_int_nat_functions
haftmann@33361
  2267
  transfer_int_nat_function_closures
haftmann@33361
  2268
]
haftmann@33361
  2269
haftmann@33361
  2270
text{*Suggested by Matthias Daum*}
haftmann@33361
  2271
lemma int_div_less_self: "\<lbrakk>0 < x; 1 < k\<rbrakk> \<Longrightarrow> x div k < (x::int)"
haftmann@33361
  2272
apply (subgoal_tac "nat x div nat k < nat x")
nipkow@34225
  2273
 apply (simp add: nat_div_distrib [symmetric])
haftmann@33361
  2274
apply (rule Divides.div_less_dividend, simp_all)
haftmann@33361
  2275
done
haftmann@33361
  2276
haftmann@33361
  2277
lemma zmod_eq_dvd_iff: "(x::int) mod n = y mod n \<longleftrightarrow> n dvd x - y"
haftmann@33361
  2278
proof
haftmann@33361
  2279
  assume H: "x mod n = y mod n"
haftmann@33361
  2280
  hence "x mod n - y mod n = 0" by simp
haftmann@33361
  2281
  hence "(x mod n - y mod n) mod n = 0" by simp 
haftmann@33361
  2282
  hence "(x - y) mod n = 0" by (simp add: mod_diff_eq[symmetric])
haftmann@33361
  2283
  thus "n dvd x - y" by (simp add: dvd_eq_mod_eq_0)
haftmann@33361
  2284
next
haftmann@33361
  2285
  assume H: "n dvd x - y"
haftmann@33361
  2286
  then obtain k where k: "x-y = n*k" unfolding dvd_def by blast
haftmann@33361
  2287
  hence "x = n*k + y" by simp
haftmann@33361
  2288
  hence "x mod n = (n*k + y) mod n" by simp
haftmann@33361
  2289
  thus "x mod n = y mod n" by (simp add: mod_add_left_eq)
haftmann@33361
  2290
qed
haftmann@33361
  2291
haftmann@33361
  2292
lemma nat_mod_eq_lemma: assumes xyn: "(x::nat) mod n = y  mod n" and xy:"y \<le> x"
haftmann@33361
  2293
  shows "\<exists>q. x = y + n * q"
haftmann@33361
  2294
proof-
haftmann@33361
  2295
  from xy have th: "int x - int y = int (x - y)" by simp 
haftmann@33361
  2296
  from xyn have "int x mod int n = int y mod int n" 
huffman@46551
  2297
    by (simp add: zmod_int [symmetric])
haftmann@33361
  2298
  hence "int n dvd int x - int y" by (simp only: zmod_eq_dvd_iff[symmetric]) 
haftmann@33361
  2299
  hence "n dvd x - y" by (simp add: th zdvd_int)
haftmann@33361
  2300
  then show ?thesis using xy unfolding dvd_def apply clarsimp apply (rule_tac x="k" in exI) by arith
haftmann@33361
  2301
qed
haftmann@33361
  2302
haftmann@33361
  2303
lemma nat_mod_eq_iff: "(x::nat) mod n = y mod n \<longleftrightarrow> (\<exists>q1 q2. x + n * q1 = y + n * q2)" 
haftmann@33361
  2304
  (is "?lhs = ?rhs")
haftmann@33361
  2305
proof
haftmann@33361
  2306
  assume H: "x mod n = y mod n"
haftmann@33361
  2307
  {assume xy: "x \<le> y"
haftmann@33361
  2308
    from H have th: "y mod n = x mod n" by simp
haftmann@33361
  2309
    from nat_mod_eq_lemma[OF th xy] have ?rhs 
haftmann@33361
  2310
      apply clarify  apply (rule_tac x="q" in exI) by (rule exI[where x="0"], simp)}
haftmann@33361
  2311
  moreover
haftmann@33361
  2312
  {assume xy: "y \<le> x"
haftmann@33361
  2313
    from nat_mod_eq_lemma[OF H xy] have ?rhs 
haftmann@33361
  2314
      apply clarify  apply (rule_tac x="0" in exI) by (rule_tac x="q" in exI, simp)}
haftmann@33361
  2315
  ultimately  show ?rhs using linear[of x y] by blast  
haftmann@33361
  2316
next
haftmann@33361
  2317
  assume ?rhs then obtain q1 q2 where q12: "x + n * q1 = y + n * q2" by blast
haftmann@33361
  2318
  hence "(x + n * q1) mod n = (y + n * q2) mod n" by simp
haftmann@33361
  2319
  thus  ?lhs by simp
haftmann@33361
  2320
qed
haftmann@33361
  2321
huffman@47108
  2322
lemma div_nat_numeral [simp]:
huffman@47108
  2323
  "(numeral v :: nat) div numeral v' = nat (numeral v div numeral v')"
haftmann@33361
  2324
  by (simp add: nat_div_distrib)
haftmann@33361
  2325
huffman@47108
  2326
lemma one_div_nat_numeral [simp]:
huffman@47108
  2327
  "Suc 0 div numeral v' = nat (1 div numeral v')"
huffman@47108
  2328
  by (subst nat_div_distrib, simp_all)
huffman@47108
  2329
huffman@47108
  2330
lemma mod_nat_numeral [simp]:
huffman@47108
  2331
  "(numeral v :: nat) mod numeral v' = nat (numeral v mod numeral v')"
haftmann@33361
  2332
  by (simp add: nat_mod_distrib)
haftmann@33361
  2333
huffman@47108
  2334
lemma one_mod_nat_numeral [simp]:
huffman@47108
  2335
  "Suc 0 mod numeral v' = nat (1 mod numeral v')"
huffman@47108
  2336
  by (subst nat_mod_distrib) simp_all
huffman@47108
  2337
huffman@47108
  2338
lemma mod_2_not_eq_zero_eq_one_int:
huffman@47108
  2339
  fixes k :: int
huffman@47108
  2340
  shows "k mod 2 \<noteq> 0 \<longleftrightarrow> k mod 2 = 1"
huffman@47108
  2341
  by auto
huffman@47108
  2342
huffman@47108
  2343
huffman@47108
  2344
subsubsection {* Tools setup *}
huffman@47108
  2345
huffman@47108
  2346
text {* Nitpick *}
blanchet@34126
  2347
blanchet@41792
  2348
lemmas [nitpick_unfold] = dvd_eq_mod_eq_0 mod_div_equality' zmod_zdiv_equality'
blanchet@34126
  2349
haftmann@35673
  2350
haftmann@33361
  2351
subsubsection {* Code generation *}
haftmann@33361
  2352
haftmann@33361
  2353
definition pdivmod :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
haftmann@33361
  2354
  "pdivmod k l = (\<bar>k\<bar> div \<bar>l\<bar>, \<bar>k\<bar> mod \<bar>l\<bar>)"
haftmann@33361
  2355
haftmann@33361
  2356
lemma pdivmod_posDivAlg [code]:
haftmann@33361
  2357
  "pdivmod k l = (if l = 0 then (0, \<bar>k\<bar>) else posDivAlg \<bar>k\<bar> \<bar>l\<bar>)"
haftmann@33361
  2358
by (subst posDivAlg_div_mod) (simp_all add: pdivmod_def)
haftmann@33361
  2359
haftmann@33361
  2360
lemma divmod_int_pdivmod: "divmod_int k l = (if k = 0 then (0, 0) else if l = 0 then (0, k) else
haftmann@33361
  2361
  apsnd ((op *) (sgn l)) (if 0 < l \<and> 0 \<le> k \<or> l < 0 \<and> k < 0
haftmann@33361
  2362
    then pdivmod k l
haftmann@33361
  2363
    else (let (r, s) = pdivmod k l in
huffman@47108
  2364
       if s = 0 then (- r, 0) else (- r - 1, \<bar>l\<bar> - s))))"
haftmann@33361
  2365
proof -
haftmann@33361
  2366
  have aux: "\<And>q::int. - k = l * q \<longleftrightarrow> k = l * - q" by auto
haftmann@33361
  2367
  show ?thesis
haftmann@33361
  2368
    by (simp add: divmod_int_mod_div pdivmod_def)
haftmann@33361
  2369
      (auto simp add: aux not_less not_le zdiv_zminus1_eq_if
haftmann@33361
  2370
      zmod_zminus1_eq_if zdiv_zminus2_eq_if zmod_zminus2_eq_if)
haftmann@33361
  2371
qed
haftmann@33361
  2372
haftmann@33361
  2373
lemma divmod_int_code [code]: "divmod_int k l = (if k = 0 then (0, 0) else if l = 0 then (0, k) else
haftmann@33361
  2374
  apsnd ((op *) (sgn l)) (if sgn k = sgn l
haftmann@33361
  2375
    then pdivmod k l
haftmann@33361
  2376
    else (let (r, s) = pdivmod k l in
haftmann@33361
  2377
      if s = 0 then (- r, 0) else (- r - 1, \<bar>l\<bar> - s))))"
haftmann@33361
  2378
proof -
haftmann@33361
  2379
  have "k \<noteq> 0 \<Longrightarrow> l \<noteq> 0 \<Longrightarrow> 0 < l \<and> 0 \<le> k \<or> l < 0 \<and> k < 0 \<longleftrightarrow> sgn k = sgn l"
haftmann@33361
  2380
    by (auto simp add: not_less sgn_if)
haftmann@33361
  2381
  then show ?thesis by (simp add: divmod_int_pdivmod)
haftmann@33361
  2382
qed
haftmann@33361
  2383
haftmann@52435
  2384
code_identifier
haftmann@52435
  2385
  code_module Divides \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
haftmann@33364
  2386
haftmann@33361
  2387
end
haftmann@52435
  2388