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