src/HOL/Euclidean_Division.thy
author haftmann
Sun Oct 08 22:28:21 2017 +0200 (2017-10-08)
changeset 66810 cc2b490f9dc4
parent 66808 1907167b6038
child 66813 351142796345
permissions -rw-r--r--
generalized simproc
haftmann@64785
     1
(*  Title:      HOL/Euclidean_Division.thy
haftmann@64785
     2
    Author:     Manuel Eberl, TU Muenchen
haftmann@64785
     3
    Author:     Florian Haftmann, TU Muenchen
haftmann@64785
     4
*)
haftmann@64785
     5
haftmann@64785
     6
section \<open>Uniquely determined division in euclidean (semi)rings\<close>
haftmann@64785
     7
haftmann@64785
     8
theory Euclidean_Division
haftmann@66808
     9
  imports Nat_Transfer Lattices_Big
haftmann@64785
    10
begin
haftmann@64785
    11
haftmann@64785
    12
subsection \<open>Euclidean (semi)rings with explicit division and remainder\<close>
haftmann@64785
    13
  
haftmann@64785
    14
class euclidean_semiring = semidom_modulo + normalization_semidom + 
haftmann@64785
    15
  fixes euclidean_size :: "'a \<Rightarrow> nat"
haftmann@64785
    16
  assumes size_0 [simp]: "euclidean_size 0 = 0"
haftmann@64785
    17
  assumes mod_size_less: 
haftmann@64785
    18
    "b \<noteq> 0 \<Longrightarrow> euclidean_size (a mod b) < euclidean_size b"
haftmann@64785
    19
  assumes size_mult_mono:
haftmann@64785
    20
    "b \<noteq> 0 \<Longrightarrow> euclidean_size a \<le> euclidean_size (a * b)"
haftmann@64785
    21
begin
haftmann@64785
    22
haftmann@64785
    23
lemma size_mult_mono': "b \<noteq> 0 \<Longrightarrow> euclidean_size a \<le> euclidean_size (b * a)"
haftmann@64785
    24
  by (subst mult.commute) (rule size_mult_mono)
haftmann@64785
    25
haftmann@64785
    26
lemma euclidean_size_normalize [simp]:
haftmann@64785
    27
  "euclidean_size (normalize a) = euclidean_size a"
haftmann@64785
    28
proof (cases "a = 0")
haftmann@64785
    29
  case True
haftmann@64785
    30
  then show ?thesis
haftmann@64785
    31
    by simp
haftmann@64785
    32
next
haftmann@64785
    33
  case [simp]: False
haftmann@64785
    34
  have "euclidean_size (normalize a) \<le> euclidean_size (normalize a * unit_factor a)"
haftmann@64785
    35
    by (rule size_mult_mono) simp
haftmann@64785
    36
  moreover have "euclidean_size a \<le> euclidean_size (a * (1 div unit_factor a))"
haftmann@64785
    37
    by (rule size_mult_mono) simp
haftmann@64785
    38
  ultimately show ?thesis
haftmann@64785
    39
    by simp
haftmann@64785
    40
qed
haftmann@64785
    41
haftmann@64785
    42
lemma dvd_euclidean_size_eq_imp_dvd:
haftmann@64785
    43
  assumes "a \<noteq> 0" and "euclidean_size a = euclidean_size b"
haftmann@64785
    44
    and "b dvd a" 
haftmann@64785
    45
  shows "a dvd b"
haftmann@64785
    46
proof (rule ccontr)
haftmann@64785
    47
  assume "\<not> a dvd b"
haftmann@64785
    48
  hence "b mod a \<noteq> 0" using mod_0_imp_dvd [of b a] by blast
haftmann@64785
    49
  then have "b mod a \<noteq> 0" by (simp add: mod_eq_0_iff_dvd)
haftmann@64785
    50
  from \<open>b dvd a\<close> have "b dvd b mod a" by (simp add: dvd_mod_iff)
haftmann@64785
    51
  then obtain c where "b mod a = b * c" unfolding dvd_def by blast
haftmann@64785
    52
    with \<open>b mod a \<noteq> 0\<close> have "c \<noteq> 0" by auto
haftmann@64785
    53
  with \<open>b mod a = b * c\<close> have "euclidean_size (b mod a) \<ge> euclidean_size b"
haftmann@64785
    54
    using size_mult_mono by force
haftmann@64785
    55
  moreover from \<open>\<not> a dvd b\<close> and \<open>a \<noteq> 0\<close>
haftmann@64785
    56
  have "euclidean_size (b mod a) < euclidean_size a"
haftmann@64785
    57
    using mod_size_less by blast
haftmann@64785
    58
  ultimately show False using \<open>euclidean_size a = euclidean_size b\<close>
haftmann@64785
    59
    by simp
haftmann@64785
    60
qed
haftmann@64785
    61
haftmann@64785
    62
lemma euclidean_size_times_unit:
haftmann@64785
    63
  assumes "is_unit a"
haftmann@64785
    64
  shows   "euclidean_size (a * b) = euclidean_size b"
haftmann@64785
    65
proof (rule antisym)
haftmann@64785
    66
  from assms have [simp]: "a \<noteq> 0" by auto
haftmann@64785
    67
  thus "euclidean_size (a * b) \<ge> euclidean_size b" by (rule size_mult_mono')
haftmann@64785
    68
  from assms have "is_unit (1 div a)" by simp
haftmann@64785
    69
  hence "1 div a \<noteq> 0" by (intro notI) simp_all
haftmann@64785
    70
  hence "euclidean_size (a * b) \<le> euclidean_size ((1 div a) * (a * b))"
haftmann@64785
    71
    by (rule size_mult_mono')
haftmann@64785
    72
  also from assms have "(1 div a) * (a * b) = b"
haftmann@64785
    73
    by (simp add: algebra_simps unit_div_mult_swap)
haftmann@64785
    74
  finally show "euclidean_size (a * b) \<le> euclidean_size b" .
haftmann@64785
    75
qed
haftmann@64785
    76
haftmann@64785
    77
lemma euclidean_size_unit:
haftmann@64785
    78
  "is_unit a \<Longrightarrow> euclidean_size a = euclidean_size 1"
haftmann@64785
    79
  using euclidean_size_times_unit [of a 1] by simp
haftmann@64785
    80
haftmann@64785
    81
lemma unit_iff_euclidean_size: 
haftmann@64785
    82
  "is_unit a \<longleftrightarrow> euclidean_size a = euclidean_size 1 \<and> a \<noteq> 0"
haftmann@64785
    83
proof safe
haftmann@64785
    84
  assume A: "a \<noteq> 0" and B: "euclidean_size a = euclidean_size 1"
haftmann@64785
    85
  show "is_unit a"
haftmann@64785
    86
    by (rule dvd_euclidean_size_eq_imp_dvd [OF A B]) simp_all
haftmann@64785
    87
qed (auto intro: euclidean_size_unit)
haftmann@64785
    88
haftmann@64785
    89
lemma euclidean_size_times_nonunit:
haftmann@64785
    90
  assumes "a \<noteq> 0" "b \<noteq> 0" "\<not> is_unit a"
haftmann@64785
    91
  shows   "euclidean_size b < euclidean_size (a * b)"
haftmann@64785
    92
proof (rule ccontr)
haftmann@64785
    93
  assume "\<not>euclidean_size b < euclidean_size (a * b)"
haftmann@64785
    94
  with size_mult_mono'[OF assms(1), of b] 
haftmann@64785
    95
    have eq: "euclidean_size (a * b) = euclidean_size b" by simp
haftmann@64785
    96
  have "a * b dvd b"
haftmann@64785
    97
    by (rule dvd_euclidean_size_eq_imp_dvd [OF _ eq]) (insert assms, simp_all)
haftmann@64785
    98
  hence "a * b dvd 1 * b" by simp
haftmann@64785
    99
  with \<open>b \<noteq> 0\<close> have "is_unit a" by (subst (asm) dvd_times_right_cancel_iff)
haftmann@64785
   100
  with assms(3) show False by contradiction
haftmann@64785
   101
qed
haftmann@64785
   102
haftmann@64785
   103
lemma dvd_imp_size_le:
haftmann@64785
   104
  assumes "a dvd b" "b \<noteq> 0" 
haftmann@64785
   105
  shows   "euclidean_size a \<le> euclidean_size b"
haftmann@64785
   106
  using assms by (auto elim!: dvdE simp: size_mult_mono)
haftmann@64785
   107
haftmann@64785
   108
lemma dvd_proper_imp_size_less:
haftmann@64785
   109
  assumes "a dvd b" "\<not> b dvd a" "b \<noteq> 0" 
haftmann@64785
   110
  shows   "euclidean_size a < euclidean_size b"
haftmann@64785
   111
proof -
haftmann@64785
   112
  from assms(1) obtain c where "b = a * c" by (erule dvdE)
haftmann@64785
   113
  hence z: "b = c * a" by (simp add: mult.commute)
haftmann@64785
   114
  from z assms have "\<not>is_unit c" by (auto simp: mult.commute mult_unit_dvd_iff)
haftmann@64785
   115
  with z assms show ?thesis
haftmann@64785
   116
    by (auto intro!: euclidean_size_times_nonunit)
haftmann@64785
   117
qed
haftmann@64785
   118
haftmann@66798
   119
lemma unit_imp_mod_eq_0:
haftmann@66798
   120
  "a mod b = 0" if "is_unit b"
haftmann@66798
   121
  using that by (simp add: mod_eq_0_iff_dvd unit_imp_dvd)
haftmann@66798
   122
haftmann@64785
   123
end
haftmann@64785
   124
haftmann@64785
   125
class euclidean_ring = idom_modulo + euclidean_semiring
haftmann@64785
   126
haftmann@64785
   127
  
haftmann@66806
   128
subsection \<open>Euclidean (semi)rings with cancel rules\<close>
haftmann@66806
   129
haftmann@66806
   130
class euclidean_semiring_cancel = euclidean_semiring +
haftmann@66806
   131
  assumes div_mult_self1 [simp]: "b \<noteq> 0 \<Longrightarrow> (a + c * b) div b = c + a div b"
haftmann@66806
   132
  and div_mult_mult1 [simp]: "c \<noteq> 0 \<Longrightarrow> (c * a) div (c * b) = a div b"
haftmann@66806
   133
begin
haftmann@66806
   134
haftmann@66806
   135
lemma div_mult_self2 [simp]:
haftmann@66806
   136
  assumes "b \<noteq> 0"
haftmann@66806
   137
  shows "(a + b * c) div b = c + a div b"
haftmann@66806
   138
  using assms div_mult_self1 [of b a c] by (simp add: mult.commute)
haftmann@66806
   139
haftmann@66806
   140
lemma div_mult_self3 [simp]:
haftmann@66806
   141
  assumes "b \<noteq> 0"
haftmann@66806
   142
  shows "(c * b + a) div b = c + a div b"
haftmann@66806
   143
  using assms by (simp add: add.commute)
haftmann@66806
   144
haftmann@66806
   145
lemma div_mult_self4 [simp]:
haftmann@66806
   146
  assumes "b \<noteq> 0"
haftmann@66806
   147
  shows "(b * c + a) div b = c + a div b"
haftmann@66806
   148
  using assms by (simp add: add.commute)
haftmann@66806
   149
haftmann@66806
   150
lemma mod_mult_self1 [simp]: "(a + c * b) mod b = a mod b"
haftmann@66806
   151
proof (cases "b = 0")
haftmann@66806
   152
  case True then show ?thesis by simp
haftmann@66806
   153
next
haftmann@66806
   154
  case False
haftmann@66806
   155
  have "a + c * b = (a + c * b) div b * b + (a + c * b) mod b"
haftmann@66806
   156
    by (simp add: div_mult_mod_eq)
haftmann@66806
   157
  also from False div_mult_self1 [of b a c] have
haftmann@66806
   158
    "\<dots> = (c + a div b) * b + (a + c * b) mod b"
haftmann@66806
   159
      by (simp add: algebra_simps)
haftmann@66806
   160
  finally have "a = a div b * b + (a + c * b) mod b"
haftmann@66806
   161
    by (simp add: add.commute [of a] add.assoc distrib_right)
haftmann@66806
   162
  then have "a div b * b + (a + c * b) mod b = a div b * b + a mod b"
haftmann@66806
   163
    by (simp add: div_mult_mod_eq)
haftmann@66806
   164
  then show ?thesis by simp
haftmann@66806
   165
qed
haftmann@66806
   166
haftmann@66806
   167
lemma mod_mult_self2 [simp]:
haftmann@66806
   168
  "(a + b * c) mod b = a mod b"
haftmann@66806
   169
  by (simp add: mult.commute [of b])
haftmann@66806
   170
haftmann@66806
   171
lemma mod_mult_self3 [simp]:
haftmann@66806
   172
  "(c * b + a) mod b = a mod b"
haftmann@66806
   173
  by (simp add: add.commute)
haftmann@66806
   174
haftmann@66806
   175
lemma mod_mult_self4 [simp]:
haftmann@66806
   176
  "(b * c + a) mod b = a mod b"
haftmann@66806
   177
  by (simp add: add.commute)
haftmann@66806
   178
haftmann@66806
   179
lemma mod_mult_self1_is_0 [simp]:
haftmann@66806
   180
  "b * a mod b = 0"
haftmann@66806
   181
  using mod_mult_self2 [of 0 b a] by simp
haftmann@66806
   182
haftmann@66806
   183
lemma mod_mult_self2_is_0 [simp]:
haftmann@66806
   184
  "a * b mod b = 0"
haftmann@66806
   185
  using mod_mult_self1 [of 0 a b] by simp
haftmann@66806
   186
haftmann@66806
   187
lemma div_add_self1:
haftmann@66806
   188
  assumes "b \<noteq> 0"
haftmann@66806
   189
  shows "(b + a) div b = a div b + 1"
haftmann@66806
   190
  using assms div_mult_self1 [of b a 1] by (simp add: add.commute)
haftmann@66806
   191
haftmann@66806
   192
lemma div_add_self2:
haftmann@66806
   193
  assumes "b \<noteq> 0"
haftmann@66806
   194
  shows "(a + b) div b = a div b + 1"
haftmann@66806
   195
  using assms div_add_self1 [of b a] by (simp add: add.commute)
haftmann@66806
   196
haftmann@66806
   197
lemma mod_add_self1 [simp]:
haftmann@66806
   198
  "(b + a) mod b = a mod b"
haftmann@66806
   199
  using mod_mult_self1 [of a 1 b] by (simp add: add.commute)
haftmann@66806
   200
haftmann@66806
   201
lemma mod_add_self2 [simp]:
haftmann@66806
   202
  "(a + b) mod b = a mod b"
haftmann@66806
   203
  using mod_mult_self1 [of a 1 b] by simp
haftmann@66806
   204
haftmann@66806
   205
lemma mod_div_trivial [simp]:
haftmann@66806
   206
  "a mod b div b = 0"
haftmann@66806
   207
proof (cases "b = 0")
haftmann@66806
   208
  assume "b = 0"
haftmann@66806
   209
  thus ?thesis by simp
haftmann@66806
   210
next
haftmann@66806
   211
  assume "b \<noteq> 0"
haftmann@66806
   212
  hence "a div b + a mod b div b = (a mod b + a div b * b) div b"
haftmann@66806
   213
    by (rule div_mult_self1 [symmetric])
haftmann@66806
   214
  also have "\<dots> = a div b"
haftmann@66806
   215
    by (simp only: mod_div_mult_eq)
haftmann@66806
   216
  also have "\<dots> = a div b + 0"
haftmann@66806
   217
    by simp
haftmann@66806
   218
  finally show ?thesis
haftmann@66806
   219
    by (rule add_left_imp_eq)
haftmann@66806
   220
qed
haftmann@66806
   221
haftmann@66806
   222
lemma mod_mod_trivial [simp]:
haftmann@66806
   223
  "a mod b mod b = a mod b"
haftmann@66806
   224
proof -
haftmann@66806
   225
  have "a mod b mod b = (a mod b + a div b * b) mod b"
haftmann@66806
   226
    by (simp only: mod_mult_self1)
haftmann@66806
   227
  also have "\<dots> = a mod b"
haftmann@66806
   228
    by (simp only: mod_div_mult_eq)
haftmann@66806
   229
  finally show ?thesis .
haftmann@66806
   230
qed
haftmann@66806
   231
haftmann@66806
   232
lemma mod_mod_cancel:
haftmann@66806
   233
  assumes "c dvd b"
haftmann@66806
   234
  shows "a mod b mod c = a mod c"
haftmann@66806
   235
proof -
haftmann@66806
   236
  from \<open>c dvd b\<close> obtain k where "b = c * k"
haftmann@66806
   237
    by (rule dvdE)
haftmann@66806
   238
  have "a mod b mod c = a mod (c * k) mod c"
haftmann@66806
   239
    by (simp only: \<open>b = c * k\<close>)
haftmann@66806
   240
  also have "\<dots> = (a mod (c * k) + a div (c * k) * k * c) mod c"
haftmann@66806
   241
    by (simp only: mod_mult_self1)
haftmann@66806
   242
  also have "\<dots> = (a div (c * k) * (c * k) + a mod (c * k)) mod c"
haftmann@66806
   243
    by (simp only: ac_simps)
haftmann@66806
   244
  also have "\<dots> = a mod c"
haftmann@66806
   245
    by (simp only: div_mult_mod_eq)
haftmann@66806
   246
  finally show ?thesis .
haftmann@66806
   247
qed
haftmann@66806
   248
haftmann@66806
   249
lemma div_mult_mult2 [simp]:
haftmann@66806
   250
  "c \<noteq> 0 \<Longrightarrow> (a * c) div (b * c) = a div b"
haftmann@66806
   251
  by (drule div_mult_mult1) (simp add: mult.commute)
haftmann@66806
   252
haftmann@66806
   253
lemma div_mult_mult1_if [simp]:
haftmann@66806
   254
  "(c * a) div (c * b) = (if c = 0 then 0 else a div b)"
haftmann@66806
   255
  by simp_all
haftmann@66806
   256
haftmann@66806
   257
lemma mod_mult_mult1:
haftmann@66806
   258
  "(c * a) mod (c * b) = c * (a mod b)"
haftmann@66806
   259
proof (cases "c = 0")
haftmann@66806
   260
  case True then show ?thesis by simp
haftmann@66806
   261
next
haftmann@66806
   262
  case False
haftmann@66806
   263
  from div_mult_mod_eq
haftmann@66806
   264
  have "((c * a) div (c * b)) * (c * b) + (c * a) mod (c * b) = c * a" .
haftmann@66806
   265
  with False have "c * ((a div b) * b + a mod b) + (c * a) mod (c * b)
haftmann@66806
   266
    = c * a + c * (a mod b)" by (simp add: algebra_simps)
haftmann@66806
   267
  with div_mult_mod_eq show ?thesis by simp
haftmann@66806
   268
qed
haftmann@66806
   269
haftmann@66806
   270
lemma mod_mult_mult2:
haftmann@66806
   271
  "(a * c) mod (b * c) = (a mod b) * c"
haftmann@66806
   272
  using mod_mult_mult1 [of c a b] by (simp add: mult.commute)
haftmann@66806
   273
haftmann@66806
   274
lemma mult_mod_left: "(a mod b) * c = (a * c) mod (b * c)"
haftmann@66806
   275
  by (fact mod_mult_mult2 [symmetric])
haftmann@66806
   276
haftmann@66806
   277
lemma mult_mod_right: "c * (a mod b) = (c * a) mod (c * b)"
haftmann@66806
   278
  by (fact mod_mult_mult1 [symmetric])
haftmann@66806
   279
haftmann@66806
   280
lemma dvd_mod: "k dvd m \<Longrightarrow> k dvd n \<Longrightarrow> k dvd (m mod n)"
haftmann@66806
   281
  unfolding dvd_def by (auto simp add: mod_mult_mult1)
haftmann@66806
   282
haftmann@66806
   283
lemma div_plus_div_distrib_dvd_left:
haftmann@66806
   284
  "c dvd a \<Longrightarrow> (a + b) div c = a div c + b div c"
haftmann@66806
   285
  by (cases "c = 0") (auto elim: dvdE)
haftmann@66806
   286
haftmann@66806
   287
lemma div_plus_div_distrib_dvd_right:
haftmann@66806
   288
  "c dvd b \<Longrightarrow> (a + b) div c = a div c + b div c"
haftmann@66806
   289
  using div_plus_div_distrib_dvd_left [of c b a]
haftmann@66806
   290
  by (simp add: ac_simps)
haftmann@66806
   291
haftmann@66806
   292
named_theorems mod_simps
haftmann@66806
   293
haftmann@66806
   294
text \<open>Addition respects modular equivalence.\<close>
haftmann@66806
   295
haftmann@66806
   296
lemma mod_add_left_eq [mod_simps]:
haftmann@66806
   297
  "(a mod c + b) mod c = (a + b) mod c"
haftmann@66806
   298
proof -
haftmann@66806
   299
  have "(a + b) mod c = (a div c * c + a mod c + b) mod c"
haftmann@66806
   300
    by (simp only: div_mult_mod_eq)
haftmann@66806
   301
  also have "\<dots> = (a mod c + b + a div c * c) mod c"
haftmann@66806
   302
    by (simp only: ac_simps)
haftmann@66806
   303
  also have "\<dots> = (a mod c + b) mod c"
haftmann@66806
   304
    by (rule mod_mult_self1)
haftmann@66806
   305
  finally show ?thesis
haftmann@66806
   306
    by (rule sym)
haftmann@66806
   307
qed
haftmann@66806
   308
haftmann@66806
   309
lemma mod_add_right_eq [mod_simps]:
haftmann@66806
   310
  "(a + b mod c) mod c = (a + b) mod c"
haftmann@66806
   311
  using mod_add_left_eq [of b c a] by (simp add: ac_simps)
haftmann@66806
   312
haftmann@66806
   313
lemma mod_add_eq:
haftmann@66806
   314
  "(a mod c + b mod c) mod c = (a + b) mod c"
haftmann@66806
   315
  by (simp add: mod_add_left_eq mod_add_right_eq)
haftmann@66806
   316
haftmann@66806
   317
lemma mod_sum_eq [mod_simps]:
haftmann@66806
   318
  "(\<Sum>i\<in>A. f i mod a) mod a = sum f A mod a"
haftmann@66806
   319
proof (induct A rule: infinite_finite_induct)
haftmann@66806
   320
  case (insert i A)
haftmann@66806
   321
  then have "(\<Sum>i\<in>insert i A. f i mod a) mod a
haftmann@66806
   322
    = (f i mod a + (\<Sum>i\<in>A. f i mod a)) mod a"
haftmann@66806
   323
    by simp
haftmann@66806
   324
  also have "\<dots> = (f i + (\<Sum>i\<in>A. f i mod a) mod a) mod a"
haftmann@66806
   325
    by (simp add: mod_simps)
haftmann@66806
   326
  also have "\<dots> = (f i + (\<Sum>i\<in>A. f i) mod a) mod a"
haftmann@66806
   327
    by (simp add: insert.hyps)
haftmann@66806
   328
  finally show ?case
haftmann@66806
   329
    by (simp add: insert.hyps mod_simps)
haftmann@66806
   330
qed simp_all
haftmann@66806
   331
haftmann@66806
   332
lemma mod_add_cong:
haftmann@66806
   333
  assumes "a mod c = a' mod c"
haftmann@66806
   334
  assumes "b mod c = b' mod c"
haftmann@66806
   335
  shows "(a + b) mod c = (a' + b') mod c"
haftmann@66806
   336
proof -
haftmann@66806
   337
  have "(a mod c + b mod c) mod c = (a' mod c + b' mod c) mod c"
haftmann@66806
   338
    unfolding assms ..
haftmann@66806
   339
  then show ?thesis
haftmann@66806
   340
    by (simp add: mod_add_eq)
haftmann@66806
   341
qed
haftmann@66806
   342
haftmann@66806
   343
text \<open>Multiplication respects modular equivalence.\<close>
haftmann@66806
   344
haftmann@66806
   345
lemma mod_mult_left_eq [mod_simps]:
haftmann@66806
   346
  "((a mod c) * b) mod c = (a * b) mod c"
haftmann@66806
   347
proof -
haftmann@66806
   348
  have "(a * b) mod c = ((a div c * c + a mod c) * b) mod c"
haftmann@66806
   349
    by (simp only: div_mult_mod_eq)
haftmann@66806
   350
  also have "\<dots> = (a mod c * b + a div c * b * c) mod c"
haftmann@66806
   351
    by (simp only: algebra_simps)
haftmann@66806
   352
  also have "\<dots> = (a mod c * b) mod c"
haftmann@66806
   353
    by (rule mod_mult_self1)
haftmann@66806
   354
  finally show ?thesis
haftmann@66806
   355
    by (rule sym)
haftmann@66806
   356
qed
haftmann@66806
   357
haftmann@66806
   358
lemma mod_mult_right_eq [mod_simps]:
haftmann@66806
   359
  "(a * (b mod c)) mod c = (a * b) mod c"
haftmann@66806
   360
  using mod_mult_left_eq [of b c a] by (simp add: ac_simps)
haftmann@66806
   361
haftmann@66806
   362
lemma mod_mult_eq:
haftmann@66806
   363
  "((a mod c) * (b mod c)) mod c = (a * b) mod c"
haftmann@66806
   364
  by (simp add: mod_mult_left_eq mod_mult_right_eq)
haftmann@66806
   365
haftmann@66806
   366
lemma mod_prod_eq [mod_simps]:
haftmann@66806
   367
  "(\<Prod>i\<in>A. f i mod a) mod a = prod f A mod a"
haftmann@66806
   368
proof (induct A rule: infinite_finite_induct)
haftmann@66806
   369
  case (insert i A)
haftmann@66806
   370
  then have "(\<Prod>i\<in>insert i A. f i mod a) mod a
haftmann@66806
   371
    = (f i mod a * (\<Prod>i\<in>A. f i mod a)) mod a"
haftmann@66806
   372
    by simp
haftmann@66806
   373
  also have "\<dots> = (f i * ((\<Prod>i\<in>A. f i mod a) mod a)) mod a"
haftmann@66806
   374
    by (simp add: mod_simps)
haftmann@66806
   375
  also have "\<dots> = (f i * ((\<Prod>i\<in>A. f i) mod a)) mod a"
haftmann@66806
   376
    by (simp add: insert.hyps)
haftmann@66806
   377
  finally show ?case
haftmann@66806
   378
    by (simp add: insert.hyps mod_simps)
haftmann@66806
   379
qed simp_all
haftmann@66806
   380
haftmann@66806
   381
lemma mod_mult_cong:
haftmann@66806
   382
  assumes "a mod c = a' mod c"
haftmann@66806
   383
  assumes "b mod c = b' mod c"
haftmann@66806
   384
  shows "(a * b) mod c = (a' * b') mod c"
haftmann@66806
   385
proof -
haftmann@66806
   386
  have "(a mod c * (b mod c)) mod c = (a' mod c * (b' mod c)) mod c"
haftmann@66806
   387
    unfolding assms ..
haftmann@66806
   388
  then show ?thesis
haftmann@66806
   389
    by (simp add: mod_mult_eq)
haftmann@66806
   390
qed
haftmann@66806
   391
haftmann@66806
   392
text \<open>Exponentiation respects modular equivalence.\<close>
haftmann@66806
   393
haftmann@66806
   394
lemma power_mod [mod_simps]: 
haftmann@66806
   395
  "((a mod b) ^ n) mod b = (a ^ n) mod b"
haftmann@66806
   396
proof (induct n)
haftmann@66806
   397
  case 0
haftmann@66806
   398
  then show ?case by simp
haftmann@66806
   399
next
haftmann@66806
   400
  case (Suc n)
haftmann@66806
   401
  have "(a mod b) ^ Suc n mod b = (a mod b) * ((a mod b) ^ n mod b) mod b"
haftmann@66806
   402
    by (simp add: mod_mult_right_eq)
haftmann@66806
   403
  with Suc show ?case
haftmann@66806
   404
    by (simp add: mod_mult_left_eq mod_mult_right_eq)
haftmann@66806
   405
qed
haftmann@66806
   406
haftmann@66806
   407
end
haftmann@66806
   408
haftmann@66806
   409
haftmann@66806
   410
class euclidean_ring_cancel = euclidean_ring + euclidean_semiring_cancel
haftmann@66806
   411
begin
haftmann@66806
   412
haftmann@66806
   413
subclass idom_divide ..
haftmann@66806
   414
haftmann@66806
   415
lemma div_minus_minus [simp]: "(- a) div (- b) = a div b"
haftmann@66806
   416
  using div_mult_mult1 [of "- 1" a b] by simp
haftmann@66806
   417
haftmann@66806
   418
lemma mod_minus_minus [simp]: "(- a) mod (- b) = - (a mod b)"
haftmann@66806
   419
  using mod_mult_mult1 [of "- 1" a b] by simp
haftmann@66806
   420
haftmann@66806
   421
lemma div_minus_right: "a div (- b) = (- a) div b"
haftmann@66806
   422
  using div_minus_minus [of "- a" b] by simp
haftmann@66806
   423
haftmann@66806
   424
lemma mod_minus_right: "a mod (- b) = - ((- a) mod b)"
haftmann@66806
   425
  using mod_minus_minus [of "- a" b] by simp
haftmann@66806
   426
haftmann@66806
   427
lemma div_minus1_right [simp]: "a div (- 1) = - a"
haftmann@66806
   428
  using div_minus_right [of a 1] by simp
haftmann@66806
   429
haftmann@66806
   430
lemma mod_minus1_right [simp]: "a mod (- 1) = 0"
haftmann@66806
   431
  using mod_minus_right [of a 1] by simp
haftmann@66806
   432
haftmann@66806
   433
text \<open>Negation respects modular equivalence.\<close>
haftmann@66806
   434
haftmann@66806
   435
lemma mod_minus_eq [mod_simps]:
haftmann@66806
   436
  "(- (a mod b)) mod b = (- a) mod b"
haftmann@66806
   437
proof -
haftmann@66806
   438
  have "(- a) mod b = (- (a div b * b + a mod b)) mod b"
haftmann@66806
   439
    by (simp only: div_mult_mod_eq)
haftmann@66806
   440
  also have "\<dots> = (- (a mod b) + - (a div b) * b) mod b"
haftmann@66806
   441
    by (simp add: ac_simps)
haftmann@66806
   442
  also have "\<dots> = (- (a mod b)) mod b"
haftmann@66806
   443
    by (rule mod_mult_self1)
haftmann@66806
   444
  finally show ?thesis
haftmann@66806
   445
    by (rule sym)
haftmann@66806
   446
qed
haftmann@66806
   447
haftmann@66806
   448
lemma mod_minus_cong:
haftmann@66806
   449
  assumes "a mod b = a' mod b"
haftmann@66806
   450
  shows "(- a) mod b = (- a') mod b"
haftmann@66806
   451
proof -
haftmann@66806
   452
  have "(- (a mod b)) mod b = (- (a' mod b)) mod b"
haftmann@66806
   453
    unfolding assms ..
haftmann@66806
   454
  then show ?thesis
haftmann@66806
   455
    by (simp add: mod_minus_eq)
haftmann@66806
   456
qed
haftmann@66806
   457
haftmann@66806
   458
text \<open>Subtraction respects modular equivalence.\<close>
haftmann@66806
   459
haftmann@66806
   460
lemma mod_diff_left_eq [mod_simps]:
haftmann@66806
   461
  "(a mod c - b) mod c = (a - b) mod c"
haftmann@66806
   462
  using mod_add_cong [of a c "a mod c" "- b" "- b"]
haftmann@66806
   463
  by simp
haftmann@66806
   464
haftmann@66806
   465
lemma mod_diff_right_eq [mod_simps]:
haftmann@66806
   466
  "(a - b mod c) mod c = (a - b) mod c"
haftmann@66806
   467
  using mod_add_cong [of a c a "- b" "- (b mod c)"] mod_minus_cong [of "b mod c" c b]
haftmann@66806
   468
  by simp
haftmann@66806
   469
haftmann@66806
   470
lemma mod_diff_eq:
haftmann@66806
   471
  "(a mod c - b mod c) mod c = (a - b) mod c"
haftmann@66806
   472
  using mod_add_cong [of a c "a mod c" "- b" "- (b mod c)"] mod_minus_cong [of "b mod c" c b]
haftmann@66806
   473
  by simp
haftmann@66806
   474
haftmann@66806
   475
lemma mod_diff_cong:
haftmann@66806
   476
  assumes "a mod c = a' mod c"
haftmann@66806
   477
  assumes "b mod c = b' mod c"
haftmann@66806
   478
  shows "(a - b) mod c = (a' - b') mod c"
haftmann@66806
   479
  using assms mod_add_cong [of a c a' "- b" "- b'"] mod_minus_cong [of b c "b'"]
haftmann@66806
   480
  by simp
haftmann@66806
   481
haftmann@66806
   482
lemma minus_mod_self2 [simp]:
haftmann@66806
   483
  "(a - b) mod b = a mod b"
haftmann@66806
   484
  using mod_diff_right_eq [of a b b]
haftmann@66806
   485
  by (simp add: mod_diff_right_eq)
haftmann@66806
   486
haftmann@66806
   487
lemma minus_mod_self1 [simp]:
haftmann@66806
   488
  "(b - a) mod b = - a mod b"
haftmann@66806
   489
  using mod_add_self2 [of "- a" b] by simp
haftmann@66806
   490
haftmann@66806
   491
lemma mod_eq_dvd_iff:
haftmann@66806
   492
  "a mod c = b mod c \<longleftrightarrow> c dvd a - b" (is "?P \<longleftrightarrow> ?Q")
haftmann@66806
   493
proof
haftmann@66806
   494
  assume ?P
haftmann@66806
   495
  then have "(a mod c - b mod c) mod c = 0"
haftmann@66806
   496
    by simp
haftmann@66806
   497
  then show ?Q
haftmann@66806
   498
    by (simp add: dvd_eq_mod_eq_0 mod_simps)
haftmann@66806
   499
next
haftmann@66806
   500
  assume ?Q
haftmann@66806
   501
  then obtain d where d: "a - b = c * d" ..
haftmann@66806
   502
  then have "a = c * d + b"
haftmann@66806
   503
    by (simp add: algebra_simps)
haftmann@66806
   504
  then show ?P by simp
haftmann@66806
   505
qed
haftmann@66806
   506
haftmann@66806
   507
end
haftmann@66806
   508
haftmann@66806
   509
  
haftmann@64785
   510
subsection \<open>Uniquely determined division\<close>
haftmann@64785
   511
  
haftmann@64785
   512
class unique_euclidean_semiring = euclidean_semiring + 
haftmann@64785
   513
  fixes uniqueness_constraint :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
haftmann@64785
   514
  assumes size_mono_mult:
haftmann@64785
   515
    "b \<noteq> 0 \<Longrightarrow> euclidean_size a < euclidean_size c
haftmann@64785
   516
      \<Longrightarrow> euclidean_size (a * b) < euclidean_size (c * b)"
haftmann@64785
   517
    -- \<open>FIXME justify\<close>
haftmann@64785
   518
  assumes uniqueness_constraint_mono_mult:
haftmann@64785
   519
    "uniqueness_constraint a b \<Longrightarrow> uniqueness_constraint (a * c) (b * c)"
haftmann@64785
   520
  assumes uniqueness_constraint_mod:
haftmann@64785
   521
    "b \<noteq> 0 \<Longrightarrow> \<not> b dvd a \<Longrightarrow> uniqueness_constraint (a mod b) b"
haftmann@64785
   522
  assumes div_bounded:
haftmann@64785
   523
    "b \<noteq> 0 \<Longrightarrow> uniqueness_constraint r b
haftmann@64785
   524
    \<Longrightarrow> euclidean_size r < euclidean_size b
haftmann@64785
   525
    \<Longrightarrow> (q * b + r) div b = q"
haftmann@64785
   526
begin
haftmann@64785
   527
haftmann@64785
   528
lemma divmod_cases [case_names divides remainder by0]:
haftmann@64785
   529
  obtains 
haftmann@64785
   530
    (divides) q where "b \<noteq> 0"
haftmann@64785
   531
      and "a div b = q"
haftmann@64785
   532
      and "a mod b = 0"
haftmann@64785
   533
      and "a = q * b"
haftmann@64785
   534
  | (remainder) q r where "b \<noteq> 0" and "r \<noteq> 0"
haftmann@64785
   535
      and "uniqueness_constraint r b"
haftmann@64785
   536
      and "euclidean_size r < euclidean_size b"
haftmann@64785
   537
      and "a div b = q"
haftmann@64785
   538
      and "a mod b = r"
haftmann@64785
   539
      and "a = q * b + r"
haftmann@64785
   540
  | (by0) "b = 0"
haftmann@64785
   541
proof (cases "b = 0")
haftmann@64785
   542
  case True
haftmann@64785
   543
  then show thesis
haftmann@64785
   544
  by (rule by0)
haftmann@64785
   545
next
haftmann@64785
   546
  case False
haftmann@64785
   547
  show thesis
haftmann@64785
   548
  proof (cases "b dvd a")
haftmann@64785
   549
    case True
haftmann@64785
   550
    then obtain q where "a = b * q" ..
haftmann@64785
   551
    with \<open>b \<noteq> 0\<close> divides
haftmann@64785
   552
    show thesis
haftmann@64785
   553
      by (simp add: ac_simps)
haftmann@64785
   554
  next
haftmann@64785
   555
    case False
haftmann@64785
   556
    then have "a mod b \<noteq> 0"
haftmann@64785
   557
      by (simp add: mod_eq_0_iff_dvd)
haftmann@64785
   558
    moreover from \<open>b \<noteq> 0\<close> \<open>\<not> b dvd a\<close> have "uniqueness_constraint (a mod b) b"
haftmann@64785
   559
      by (rule uniqueness_constraint_mod)
haftmann@64785
   560
    moreover have "euclidean_size (a mod b) < euclidean_size b"
haftmann@64785
   561
      using \<open>b \<noteq> 0\<close> by (rule mod_size_less)
haftmann@64785
   562
    moreover have "a = a div b * b + a mod b"
haftmann@64785
   563
      by (simp add: div_mult_mod_eq)
haftmann@64785
   564
    ultimately show thesis
haftmann@64785
   565
      using \<open>b \<noteq> 0\<close> by (blast intro: remainder)
haftmann@64785
   566
  qed
haftmann@64785
   567
qed
haftmann@64785
   568
haftmann@64785
   569
lemma div_eqI:
haftmann@64785
   570
  "a div b = q" if "b \<noteq> 0" "uniqueness_constraint r b"
haftmann@64785
   571
    "euclidean_size r < euclidean_size b" "q * b + r = a"
haftmann@64785
   572
proof -
haftmann@64785
   573
  from that have "(q * b + r) div b = q"
haftmann@64785
   574
    by (auto intro: div_bounded)
haftmann@64785
   575
  with that show ?thesis
haftmann@64785
   576
    by simp
haftmann@64785
   577
qed
haftmann@64785
   578
haftmann@64785
   579
lemma mod_eqI:
haftmann@64785
   580
  "a mod b = r" if "b \<noteq> 0" "uniqueness_constraint r b"
haftmann@64785
   581
    "euclidean_size r < euclidean_size b" "q * b + r = a" 
haftmann@64785
   582
proof -
haftmann@64785
   583
  from that have "a div b = q"
haftmann@64785
   584
    by (rule div_eqI)
haftmann@64785
   585
  moreover have "a div b * b + a mod b = a"
haftmann@64785
   586
    by (fact div_mult_mod_eq)
haftmann@64785
   587
  ultimately have "a div b * b + a mod b = a div b * b + r"
haftmann@64785
   588
    using \<open>q * b + r = a\<close> by simp
haftmann@64785
   589
  then show ?thesis
haftmann@64785
   590
    by simp
haftmann@64785
   591
qed
haftmann@64785
   592
haftmann@66806
   593
subclass euclidean_semiring_cancel
haftmann@66806
   594
proof
haftmann@66806
   595
  show "(a + c * b) div b = c + a div b" if "b \<noteq> 0" for a b c
haftmann@66806
   596
  proof (cases a b rule: divmod_cases)
haftmann@66806
   597
    case by0
haftmann@66806
   598
    with \<open>b \<noteq> 0\<close> show ?thesis
haftmann@66806
   599
      by simp
haftmann@66806
   600
  next
haftmann@66806
   601
    case (divides q)
haftmann@66806
   602
    then show ?thesis
haftmann@66806
   603
      by (simp add: ac_simps)
haftmann@66806
   604
  next
haftmann@66806
   605
    case (remainder q r)
haftmann@66806
   606
    then show ?thesis
haftmann@66806
   607
      by (auto intro: div_eqI simp add: algebra_simps)
haftmann@66806
   608
  qed
haftmann@66806
   609
next
haftmann@66806
   610
  show"(c * a) div (c * b) = a div b" if "c \<noteq> 0" for a b c
haftmann@66806
   611
  proof (cases a b rule: divmod_cases)
haftmann@66806
   612
    case by0
haftmann@66806
   613
    then show ?thesis
haftmann@66806
   614
      by simp
haftmann@66806
   615
  next
haftmann@66806
   616
    case (divides q)
haftmann@66806
   617
    with \<open>c \<noteq> 0\<close> show ?thesis
haftmann@66806
   618
      by (simp add: mult.left_commute [of c])
haftmann@66806
   619
  next
haftmann@66806
   620
    case (remainder q r)
haftmann@66806
   621
    from \<open>b \<noteq> 0\<close> \<open>c \<noteq> 0\<close> have "b * c \<noteq> 0"
haftmann@66806
   622
      by simp
haftmann@66806
   623
    from remainder \<open>c \<noteq> 0\<close>
haftmann@66806
   624
    have "uniqueness_constraint (r * c) (b * c)"
haftmann@66806
   625
      and "euclidean_size (r * c) < euclidean_size (b * c)"
haftmann@66806
   626
      by (simp_all add: uniqueness_constraint_mono_mult uniqueness_constraint_mod size_mono_mult)
haftmann@66806
   627
    with remainder show ?thesis
haftmann@66806
   628
      by (auto intro!: div_eqI [of _ "c * (a mod b)"] simp add: algebra_simps)
haftmann@66806
   629
        (use \<open>b * c \<noteq> 0\<close> in simp)
haftmann@66806
   630
  qed
haftmann@66806
   631
qed
haftmann@66806
   632
haftmann@64785
   633
end
haftmann@64785
   634
haftmann@64785
   635
class unique_euclidean_ring = euclidean_ring + unique_euclidean_semiring
haftmann@66806
   636
begin
haftmann@66806
   637
  
haftmann@66806
   638
subclass euclidean_ring_cancel ..
haftmann@64785
   639
haftmann@64785
   640
end
haftmann@66806
   641
haftmann@66808
   642
haftmann@66808
   643
subsection \<open>Euclidean division on @{typ nat}\<close>
haftmann@66808
   644
haftmann@66808
   645
instantiation nat :: unique_euclidean_semiring
haftmann@66808
   646
begin
haftmann@66808
   647
haftmann@66808
   648
definition normalize_nat :: "nat \<Rightarrow> nat"
haftmann@66808
   649
  where [simp]: "normalize = (id :: nat \<Rightarrow> nat)"
haftmann@66808
   650
haftmann@66808
   651
definition unit_factor_nat :: "nat \<Rightarrow> nat"
haftmann@66808
   652
  where "unit_factor n = (if n = 0 then 0 else 1 :: nat)"
haftmann@66808
   653
haftmann@66808
   654
lemma unit_factor_simps [simp]:
haftmann@66808
   655
  "unit_factor 0 = (0::nat)"
haftmann@66808
   656
  "unit_factor (Suc n) = 1"
haftmann@66808
   657
  by (simp_all add: unit_factor_nat_def)
haftmann@66808
   658
haftmann@66808
   659
definition euclidean_size_nat :: "nat \<Rightarrow> nat"
haftmann@66808
   660
  where [simp]: "euclidean_size_nat = id"
haftmann@66808
   661
haftmann@66808
   662
definition uniqueness_constraint_nat :: "nat \<Rightarrow> nat \<Rightarrow> bool"
haftmann@66808
   663
  where [simp]: "uniqueness_constraint_nat = \<top>"
haftmann@66808
   664
haftmann@66808
   665
definition divide_nat :: "nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@66808
   666
  where "m div n = (if n = 0 then 0 else Max {k::nat. k * n \<le> m})"
haftmann@66808
   667
haftmann@66808
   668
definition modulo_nat :: "nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@66808
   669
  where "m mod n = m - (m div n * (n::nat))"
haftmann@66808
   670
haftmann@66808
   671
instance proof
haftmann@66808
   672
  fix m n :: nat
haftmann@66808
   673
  have ex: "\<exists>k. k * n \<le> l" for l :: nat
haftmann@66808
   674
    by (rule exI [of _ 0]) simp
haftmann@66808
   675
  have fin: "finite {k. k * n \<le> l}" if "n > 0" for l
haftmann@66808
   676
  proof -
haftmann@66808
   677
    from that have "{k. k * n \<le> l} \<subseteq> {k. k \<le> l}"
haftmann@66808
   678
      by (cases n) auto
haftmann@66808
   679
    then show ?thesis
haftmann@66808
   680
      by (rule finite_subset) simp
haftmann@66808
   681
  qed
haftmann@66808
   682
  have mult_div_unfold: "n * (m div n) = Max {l. l \<le> m \<and> n dvd l}"
haftmann@66808
   683
  proof (cases "n = 0")
haftmann@66808
   684
    case True
haftmann@66808
   685
    moreover have "{l. l = 0 \<and> l \<le> m} = {0::nat}"
haftmann@66808
   686
      by auto
haftmann@66808
   687
    ultimately show ?thesis
haftmann@66808
   688
      by simp
haftmann@66808
   689
  next
haftmann@66808
   690
    case False
haftmann@66808
   691
    with ex [of m] fin have "n * Max {k. k * n \<le> m} = Max (times n ` {k. k * n \<le> m})"
haftmann@66808
   692
      by (auto simp add: nat_mult_max_right intro: hom_Max_commute)
haftmann@66808
   693
    also have "times n ` {k. k * n \<le> m} = {l. l \<le> m \<and> n dvd l}"
haftmann@66808
   694
      by (auto simp add: ac_simps elim!: dvdE)
haftmann@66808
   695
    finally show ?thesis
haftmann@66808
   696
      using False by (simp add: divide_nat_def ac_simps)
haftmann@66808
   697
  qed
haftmann@66808
   698
  show "n div 0 = 0"
haftmann@66808
   699
    by (simp add: divide_nat_def)
haftmann@66808
   700
  have less_eq: "m div n * n \<le> m"
haftmann@66808
   701
    by (auto simp add: mult_div_unfold ac_simps intro: Max.boundedI)
haftmann@66808
   702
  then show "m div n * n + m mod n = m"
haftmann@66808
   703
    by (simp add: modulo_nat_def)
haftmann@66808
   704
  assume "n \<noteq> 0" 
haftmann@66808
   705
  show "m * n div n = m"
haftmann@66808
   706
    using \<open>n \<noteq> 0\<close> by (auto simp add: divide_nat_def ac_simps intro: Max_eqI)
haftmann@66808
   707
  show "euclidean_size (m mod n) < euclidean_size n"
haftmann@66808
   708
  proof -
haftmann@66808
   709
    have "m < Suc (m div n) * n"
haftmann@66808
   710
    proof (rule ccontr)
haftmann@66808
   711
      assume "\<not> m < Suc (m div n) * n"
haftmann@66808
   712
      then have "Suc (m div n) * n \<le> m"
haftmann@66808
   713
        by (simp add: not_less)
haftmann@66808
   714
      moreover from \<open>n \<noteq> 0\<close> have "Max {k. k * n \<le> m} < Suc (m div n)"
haftmann@66808
   715
        by (simp add: divide_nat_def)
haftmann@66808
   716
      with \<open>n \<noteq> 0\<close> ex fin have "\<And>k. k * n \<le> m \<Longrightarrow> k < Suc (m div n)"
haftmann@66808
   717
        by auto
haftmann@66808
   718
      ultimately have "Suc (m div n) < Suc (m div n)"
haftmann@66808
   719
        by blast
haftmann@66808
   720
      then show False
haftmann@66808
   721
        by simp
haftmann@66808
   722
    qed
haftmann@66808
   723
    with \<open>n \<noteq> 0\<close> show ?thesis
haftmann@66808
   724
      by (simp add: modulo_nat_def)
haftmann@66808
   725
  qed
haftmann@66808
   726
  show "euclidean_size m \<le> euclidean_size (m * n)"
haftmann@66808
   727
    using \<open>n \<noteq> 0\<close> by (cases n) simp_all
haftmann@66808
   728
  fix q r :: nat
haftmann@66808
   729
  show "(q * n + r) div n = q" if "euclidean_size r < euclidean_size n"
haftmann@66808
   730
  proof -
haftmann@66808
   731
    from that have "r < n"
haftmann@66808
   732
      by simp
haftmann@66808
   733
    have "k \<le> q" if "k * n \<le> q * n + r" for k
haftmann@66808
   734
    proof (rule ccontr)
haftmann@66808
   735
      assume "\<not> k \<le> q"
haftmann@66808
   736
      then have "q < k"
haftmann@66808
   737
        by simp
haftmann@66808
   738
      then obtain l where "k = Suc (q + l)"
haftmann@66808
   739
        by (auto simp add: less_iff_Suc_add)
haftmann@66808
   740
      with \<open>r < n\<close> that show False
haftmann@66808
   741
        by (simp add: algebra_simps)
haftmann@66808
   742
    qed
haftmann@66808
   743
    with \<open>n \<noteq> 0\<close> ex fin show ?thesis
haftmann@66808
   744
      by (auto simp add: divide_nat_def Max_eq_iff)
haftmann@66808
   745
  qed
haftmann@66808
   746
qed (simp_all add: unit_factor_nat_def)
haftmann@66808
   747
haftmann@66806
   748
end
haftmann@66808
   749
haftmann@66808
   750
text \<open>Tool support\<close>
haftmann@66808
   751
haftmann@66808
   752
ML \<open>
haftmann@66808
   753
structure Cancel_Div_Mod_Nat = Cancel_Div_Mod
haftmann@66808
   754
(
haftmann@66808
   755
  val div_name = @{const_name divide};
haftmann@66808
   756
  val mod_name = @{const_name modulo};
haftmann@66808
   757
  val mk_binop = HOLogic.mk_binop;
haftmann@66808
   758
  val mk_plus = HOLogic.mk_binop @{const_name Groups.plus};
haftmann@66808
   759
  val dest_plus = HOLogic.dest_bin @{const_name Groups.plus} HOLogic.natT;
haftmann@66810
   760
  fun mk_sum' [] = HOLogic.zero
haftmann@66810
   761
    | mk_sum' [t] = t
haftmann@66810
   762
    | mk_sum' (t :: ts) = mk_plus (t, mk_sum' ts);
haftmann@66810
   763
  fun mk_sum _ = mk_sum';
haftmann@66808
   764
  fun dest_sum tm =
haftmann@66808
   765
    if HOLogic.is_zero tm then []
haftmann@66808
   766
    else
haftmann@66808
   767
      (case try HOLogic.dest_Suc tm of
haftmann@66808
   768
        SOME t => HOLogic.Suc_zero :: dest_sum t
haftmann@66808
   769
      | NONE =>
haftmann@66808
   770
          (case try dest_plus tm of
haftmann@66808
   771
            SOME (t, u) => dest_sum t @ dest_sum u
haftmann@66808
   772
          | NONE => [tm]));
haftmann@66808
   773
haftmann@66808
   774
  val div_mod_eqs = map mk_meta_eq @{thms cancel_div_mod_rules};
haftmann@66808
   775
haftmann@66808
   776
  val prove_eq_sums = Arith_Data.prove_conv2 all_tac
haftmann@66808
   777
    (Arith_Data.simp_all_tac @{thms add_0_left add_0_right ac_simps})
haftmann@66808
   778
)
haftmann@66808
   779
\<close>
haftmann@66808
   780
haftmann@66808
   781
simproc_setup cancel_div_mod_nat ("(m::nat) + n") =
haftmann@66808
   782
  \<open>K Cancel_Div_Mod_Nat.proc\<close>
haftmann@66808
   783
haftmann@66808
   784
lemma div_nat_eqI:
haftmann@66808
   785
  "m div n = q" if "n * q \<le> m" and "m < n * Suc q" for m n q :: nat
haftmann@66808
   786
  by (rule div_eqI [of _ "m - n * q"]) (use that in \<open>simp_all add: algebra_simps\<close>)
haftmann@66808
   787
haftmann@66808
   788
lemma mod_nat_eqI:
haftmann@66808
   789
  "m mod n = r" if "r < n" and "r \<le> m" and "n dvd m - r" for m n r :: nat
haftmann@66808
   790
  by (rule mod_eqI [of _ _ "(m - r) div n"]) (use that in \<open>simp_all add: algebra_simps\<close>)
haftmann@66808
   791
haftmann@66808
   792
lemma div_mult_self_is_m [simp]:
haftmann@66808
   793
  "m * n div n = m" if "n > 0" for m n :: nat
haftmann@66808
   794
  using that by simp
haftmann@66808
   795
haftmann@66808
   796
lemma div_mult_self1_is_m [simp]:
haftmann@66808
   797
  "n * m div n = m" if "n > 0" for m n :: nat
haftmann@66808
   798
  using that by simp
haftmann@66808
   799
haftmann@66808
   800
lemma mod_less_divisor [simp]:
haftmann@66808
   801
  "m mod n < n" if "n > 0" for m n :: nat
haftmann@66808
   802
  using mod_size_less [of n m] that by simp
haftmann@66808
   803
haftmann@66808
   804
lemma mod_le_divisor [simp]:
haftmann@66808
   805
  "m mod n \<le> n" if "n > 0" for m n :: nat
haftmann@66808
   806
  using that by (auto simp add: le_less)
haftmann@66808
   807
haftmann@66808
   808
lemma div_times_less_eq_dividend [simp]:
haftmann@66808
   809
  "m div n * n \<le> m" for m n :: nat
haftmann@66808
   810
  by (simp add: minus_mod_eq_div_mult [symmetric])
haftmann@66808
   811
haftmann@66808
   812
lemma times_div_less_eq_dividend [simp]:
haftmann@66808
   813
  "n * (m div n) \<le> m" for m n :: nat
haftmann@66808
   814
  using div_times_less_eq_dividend [of m n]
haftmann@66808
   815
  by (simp add: ac_simps)
haftmann@66808
   816
haftmann@66808
   817
lemma dividend_less_div_times:
haftmann@66808
   818
  "m < n + (m div n) * n" if "0 < n" for m n :: nat
haftmann@66808
   819
proof -
haftmann@66808
   820
  from that have "m mod n < n"
haftmann@66808
   821
    by simp
haftmann@66808
   822
  then show ?thesis
haftmann@66808
   823
    by (simp add: minus_mod_eq_div_mult [symmetric])
haftmann@66808
   824
qed
haftmann@66808
   825
haftmann@66808
   826
lemma dividend_less_times_div:
haftmann@66808
   827
  "m < n + n * (m div n)" if "0 < n" for m n :: nat
haftmann@66808
   828
  using dividend_less_div_times [of n m] that
haftmann@66808
   829
  by (simp add: ac_simps)
haftmann@66808
   830
haftmann@66808
   831
lemma mod_Suc_le_divisor [simp]:
haftmann@66808
   832
  "m mod Suc n \<le> n"
haftmann@66808
   833
  using mod_less_divisor [of "Suc n" m] by arith
haftmann@66808
   834
haftmann@66808
   835
lemma mod_less_eq_dividend [simp]:
haftmann@66808
   836
  "m mod n \<le> m" for m n :: nat
haftmann@66808
   837
proof (rule add_leD2)
haftmann@66808
   838
  from div_mult_mod_eq have "m div n * n + m mod n = m" .
haftmann@66808
   839
  then show "m div n * n + m mod n \<le> m" by auto
haftmann@66808
   840
qed
haftmann@66808
   841
haftmann@66808
   842
lemma
haftmann@66808
   843
  div_less [simp]: "m div n = 0"
haftmann@66808
   844
  and mod_less [simp]: "m mod n = m"
haftmann@66808
   845
  if "m < n" for m n :: nat
haftmann@66808
   846
  using that by (auto intro: div_eqI mod_eqI) 
haftmann@66808
   847
haftmann@66808
   848
lemma le_div_geq:
haftmann@66808
   849
  "m div n = Suc ((m - n) div n)" if "0 < n" and "n \<le> m" for m n :: nat
haftmann@66808
   850
proof -
haftmann@66808
   851
  from \<open>n \<le> m\<close> obtain q where "m = n + q"
haftmann@66808
   852
    by (auto simp add: le_iff_add)
haftmann@66808
   853
  with \<open>0 < n\<close> show ?thesis
haftmann@66808
   854
    by (simp add: div_add_self1)
haftmann@66808
   855
qed
haftmann@66808
   856
haftmann@66808
   857
lemma le_mod_geq:
haftmann@66808
   858
  "m mod n = (m - n) mod n" if "n \<le> m" for m n :: nat
haftmann@66808
   859
proof -
haftmann@66808
   860
  from \<open>n \<le> m\<close> obtain q where "m = n + q"
haftmann@66808
   861
    by (auto simp add: le_iff_add)
haftmann@66808
   862
  then show ?thesis
haftmann@66808
   863
    by simp
haftmann@66808
   864
qed
haftmann@66808
   865
haftmann@66808
   866
lemma div_if:
haftmann@66808
   867
  "m div n = (if m < n \<or> n = 0 then 0 else Suc ((m - n) div n))"
haftmann@66808
   868
  by (simp add: le_div_geq)
haftmann@66808
   869
haftmann@66808
   870
lemma mod_if:
haftmann@66808
   871
  "m mod n = (if m < n then m else (m - n) mod n)" for m n :: nat
haftmann@66808
   872
  by (simp add: le_mod_geq)
haftmann@66808
   873
haftmann@66808
   874
lemma div_eq_0_iff:
haftmann@66808
   875
  "m div n = 0 \<longleftrightarrow> m < n \<or> n = 0" for m n :: nat
haftmann@66808
   876
  by (simp add: div_if)
haftmann@66808
   877
haftmann@66808
   878
lemma div_greater_zero_iff:
haftmann@66808
   879
  "m div n > 0 \<longleftrightarrow> n \<le> m \<and> n > 0" for m n :: nat
haftmann@66808
   880
  using div_eq_0_iff [of m n] by auto
haftmann@66808
   881
haftmann@66808
   882
lemma mod_greater_zero_iff_not_dvd:
haftmann@66808
   883
  "m mod n > 0 \<longleftrightarrow> \<not> n dvd m" for m n :: nat
haftmann@66808
   884
  by (simp add: dvd_eq_mod_eq_0)
haftmann@66808
   885
haftmann@66808
   886
lemma div_by_Suc_0 [simp]:
haftmann@66808
   887
  "m div Suc 0 = m"
haftmann@66808
   888
  using div_by_1 [of m] by simp
haftmann@66808
   889
haftmann@66808
   890
lemma mod_by_Suc_0 [simp]:
haftmann@66808
   891
  "m mod Suc 0 = 0"
haftmann@66808
   892
  using mod_by_1 [of m] by simp
haftmann@66808
   893
haftmann@66808
   894
lemma div2_Suc_Suc [simp]:
haftmann@66808
   895
  "Suc (Suc m) div 2 = Suc (m div 2)"
haftmann@66808
   896
  by (simp add: numeral_2_eq_2 le_div_geq)
haftmann@66808
   897
haftmann@66808
   898
lemma Suc_n_div_2_gt_zero [simp]:
haftmann@66808
   899
  "0 < Suc n div 2" if "n > 0" for n :: nat
haftmann@66808
   900
  using that by (cases n) simp_all
haftmann@66808
   901
haftmann@66808
   902
lemma div_2_gt_zero [simp]:
haftmann@66808
   903
  "0 < n div 2" if "Suc 0 < n" for n :: nat
haftmann@66808
   904
  using that Suc_n_div_2_gt_zero [of "n - 1"] by simp
haftmann@66808
   905
haftmann@66808
   906
lemma mod2_Suc_Suc [simp]:
haftmann@66808
   907
  "Suc (Suc m) mod 2 = m mod 2"
haftmann@66808
   908
  by (simp add: numeral_2_eq_2 le_mod_geq)
haftmann@66808
   909
haftmann@66808
   910
lemma add_self_div_2 [simp]:
haftmann@66808
   911
  "(m + m) div 2 = m" for m :: nat
haftmann@66808
   912
  by (simp add: mult_2 [symmetric])
haftmann@66808
   913
haftmann@66808
   914
lemma add_self_mod_2 [simp]:
haftmann@66808
   915
  "(m + m) mod 2 = 0" for m :: nat
haftmann@66808
   916
  by (simp add: mult_2 [symmetric])
haftmann@66808
   917
haftmann@66808
   918
lemma mod2_gr_0 [simp]:
haftmann@66808
   919
  "0 < m mod 2 \<longleftrightarrow> m mod 2 = 1" for m :: nat
haftmann@66808
   920
proof -
haftmann@66808
   921
  have "m mod 2 < 2"
haftmann@66808
   922
    by (rule mod_less_divisor) simp
haftmann@66808
   923
  then have "m mod 2 = 0 \<or> m mod 2 = 1"
haftmann@66808
   924
    by arith
haftmann@66808
   925
  then show ?thesis
haftmann@66808
   926
    by auto     
haftmann@66808
   927
qed
haftmann@66808
   928
haftmann@66808
   929
lemma mod_Suc_eq [mod_simps]:
haftmann@66808
   930
  "Suc (m mod n) mod n = Suc m mod n"
haftmann@66808
   931
proof -
haftmann@66808
   932
  have "(m mod n + 1) mod n = (m + 1) mod n"
haftmann@66808
   933
    by (simp only: mod_simps)
haftmann@66808
   934
  then show ?thesis
haftmann@66808
   935
    by simp
haftmann@66808
   936
qed
haftmann@66808
   937
haftmann@66808
   938
lemma mod_Suc_Suc_eq [mod_simps]:
haftmann@66808
   939
  "Suc (Suc (m mod n)) mod n = Suc (Suc m) mod n"
haftmann@66808
   940
proof -
haftmann@66808
   941
  have "(m mod n + 2) mod n = (m + 2) mod n"
haftmann@66808
   942
    by (simp only: mod_simps)
haftmann@66808
   943
  then show ?thesis
haftmann@66808
   944
    by simp
haftmann@66808
   945
qed
haftmann@66808
   946
haftmann@66808
   947
lemma
haftmann@66808
   948
  Suc_mod_mult_self1 [simp]: "Suc (m + k * n) mod n = Suc m mod n"
haftmann@66808
   949
  and Suc_mod_mult_self2 [simp]: "Suc (m + n * k) mod n = Suc m mod n"
haftmann@66808
   950
  and Suc_mod_mult_self3 [simp]: "Suc (k * n + m) mod n = Suc m mod n"
haftmann@66808
   951
  and Suc_mod_mult_self4 [simp]: "Suc (n * k + m) mod n = Suc m mod n"
haftmann@66808
   952
  by (subst mod_Suc_eq [symmetric], simp add: mod_simps)+
haftmann@66808
   953
haftmann@66808
   954
lemma div_mult1_eq: -- \<open>TODO: Generalization candidate\<close>
haftmann@66808
   955
  "(a * b) div c = a * (b div c) + a * (b mod c) div c" for a b c :: nat
haftmann@66808
   956
  apply (cases "c = 0")
haftmann@66808
   957
   apply simp
haftmann@66808
   958
  apply (rule div_eqI [of _ "(a * (b mod c)) mod c"])
haftmann@66808
   959
     apply (auto simp add: algebra_simps distrib_left [symmetric])
haftmann@66808
   960
  done
haftmann@66808
   961
haftmann@66808
   962
lemma div_add1_eq: -- \<open>NOT suitable for rewriting: the RHS has an instance of the LHS\<close>
haftmann@66808
   963
   -- \<open>TODO: Generalization candidate\<close>
haftmann@66808
   964
  "(a + b) div c = a div c + b div c + ((a mod c + b mod c) div c)" for a b c :: nat
haftmann@66808
   965
  apply (cases "c = 0")
haftmann@66808
   966
   apply simp
haftmann@66808
   967
  apply (rule div_eqI [of _ "(a mod c + b mod c) mod c"])
haftmann@66808
   968
  apply (auto simp add: algebra_simps)
haftmann@66808
   969
  done
haftmann@66808
   970
haftmann@66808
   971
context
haftmann@66808
   972
  fixes m n q :: nat
haftmann@66808
   973
begin
haftmann@66808
   974
haftmann@66808
   975
private lemma eucl_rel_mult2:
haftmann@66808
   976
  "m mod n + n * (m div n mod q) < n * q"
haftmann@66808
   977
  if "n > 0" and "q > 0"
haftmann@66808
   978
proof -
haftmann@66808
   979
  from \<open>n > 0\<close> have "m mod n < n"
haftmann@66808
   980
    by (rule mod_less_divisor)
haftmann@66808
   981
  from \<open>q > 0\<close> have "m div n mod q < q"
haftmann@66808
   982
    by (rule mod_less_divisor)
haftmann@66808
   983
  then obtain s where "q = Suc (m div n mod q + s)"
haftmann@66808
   984
    by (blast dest: less_imp_Suc_add)
haftmann@66808
   985
  moreover have "m mod n + n * (m div n mod q) < n * Suc (m div n mod q + s)"
haftmann@66808
   986
    using \<open>m mod n < n\<close> by (simp add: add_mult_distrib2)
haftmann@66808
   987
  ultimately show ?thesis
haftmann@66808
   988
    by simp
haftmann@66808
   989
qed
haftmann@66808
   990
haftmann@66808
   991
lemma div_mult2_eq:
haftmann@66808
   992
  "m div (n * q) = (m div n) div q"
haftmann@66808
   993
proof (cases "n = 0 \<or> q = 0")
haftmann@66808
   994
  case True
haftmann@66808
   995
  then show ?thesis
haftmann@66808
   996
    by auto
haftmann@66808
   997
next
haftmann@66808
   998
  case False
haftmann@66808
   999
  with eucl_rel_mult2 show ?thesis
haftmann@66808
  1000
    by (auto intro: div_eqI [of _ "n * (m div n mod q) + m mod n"]
haftmann@66808
  1001
      simp add: algebra_simps add_mult_distrib2 [symmetric])
haftmann@66808
  1002
qed
haftmann@66808
  1003
haftmann@66808
  1004
lemma mod_mult2_eq:
haftmann@66808
  1005
  "m mod (n * q) = n * (m div n mod q) + m mod n"
haftmann@66808
  1006
proof (cases "n = 0 \<or> q = 0")
haftmann@66808
  1007
  case True
haftmann@66808
  1008
  then show ?thesis
haftmann@66808
  1009
    by auto
haftmann@66808
  1010
next
haftmann@66808
  1011
  case False
haftmann@66808
  1012
  with eucl_rel_mult2 show ?thesis
haftmann@66808
  1013
    by (auto intro: mod_eqI [of _ _ "(m div n) div q"]
haftmann@66808
  1014
      simp add: algebra_simps add_mult_distrib2 [symmetric])
haftmann@66808
  1015
qed
haftmann@66808
  1016
haftmann@66808
  1017
end
haftmann@66808
  1018
haftmann@66808
  1019
lemma div_le_mono:
haftmann@66808
  1020
  "m div k \<le> n div k" if "m \<le> n" for m n k :: nat
haftmann@66808
  1021
proof -
haftmann@66808
  1022
  from that obtain q where "n = m + q"
haftmann@66808
  1023
    by (auto simp add: le_iff_add)
haftmann@66808
  1024
  then show ?thesis
haftmann@66808
  1025
    by (simp add: div_add1_eq [of m q k])
haftmann@66808
  1026
qed
haftmann@66808
  1027
haftmann@66808
  1028
text \<open>Antimonotonicity of @{const divide} in second argument\<close>
haftmann@66808
  1029
haftmann@66808
  1030
lemma div_le_mono2:
haftmann@66808
  1031
  "k div n \<le> k div m" if "0 < m" and "m \<le> n" for m n k :: nat
haftmann@66808
  1032
using that proof (induct k arbitrary: m rule: less_induct)
haftmann@66808
  1033
  case (less k)
haftmann@66808
  1034
  show ?case
haftmann@66808
  1035
  proof (cases "n \<le> k")
haftmann@66808
  1036
    case False
haftmann@66808
  1037
    then show ?thesis
haftmann@66808
  1038
      by simp
haftmann@66808
  1039
  next
haftmann@66808
  1040
    case True
haftmann@66808
  1041
    have "(k - n) div n \<le> (k - m) div n"
haftmann@66808
  1042
      using less.prems
haftmann@66808
  1043
      by (blast intro: div_le_mono diff_le_mono2)
haftmann@66808
  1044
    also have "\<dots> \<le> (k - m) div m"
haftmann@66808
  1045
      using \<open>n \<le> k\<close> less.prems less.hyps [of "k - m" m]
haftmann@66808
  1046
      by simp
haftmann@66808
  1047
    finally show ?thesis
haftmann@66808
  1048
      using \<open>n \<le> k\<close> less.prems
haftmann@66808
  1049
      by (simp add: le_div_geq)
haftmann@66808
  1050
  qed
haftmann@66808
  1051
qed
haftmann@66808
  1052
haftmann@66808
  1053
lemma div_le_dividend [simp]:
haftmann@66808
  1054
  "m div n \<le> m" for m n :: nat
haftmann@66808
  1055
  using div_le_mono2 [of 1 n m] by (cases "n = 0") simp_all
haftmann@66808
  1056
haftmann@66808
  1057
lemma div_less_dividend [simp]:
haftmann@66808
  1058
  "m div n < m" if "1 < n" and "0 < m" for m n :: nat
haftmann@66808
  1059
using that proof (induct m rule: less_induct)
haftmann@66808
  1060
  case (less m)
haftmann@66808
  1061
  show ?case
haftmann@66808
  1062
  proof (cases "n < m")
haftmann@66808
  1063
    case False
haftmann@66808
  1064
    with less show ?thesis
haftmann@66808
  1065
      by (cases "n = m") simp_all
haftmann@66808
  1066
  next
haftmann@66808
  1067
    case True
haftmann@66808
  1068
    then show ?thesis
haftmann@66808
  1069
      using less.hyps [of "m - n"] less.prems
haftmann@66808
  1070
      by (simp add: le_div_geq)
haftmann@66808
  1071
  qed
haftmann@66808
  1072
qed
haftmann@66808
  1073
haftmann@66808
  1074
lemma div_eq_dividend_iff:
haftmann@66808
  1075
  "m div n = m \<longleftrightarrow> n = 1" if "m > 0" for m n :: nat
haftmann@66808
  1076
proof
haftmann@66808
  1077
  assume "n = 1"
haftmann@66808
  1078
  then show "m div n = m"
haftmann@66808
  1079
    by simp
haftmann@66808
  1080
next
haftmann@66808
  1081
  assume P: "m div n = m"
haftmann@66808
  1082
  show "n = 1"
haftmann@66808
  1083
  proof (rule ccontr)
haftmann@66808
  1084
    have "n \<noteq> 0"
haftmann@66808
  1085
      by (rule ccontr) (use that P in auto)
haftmann@66808
  1086
    moreover assume "n \<noteq> 1"
haftmann@66808
  1087
    ultimately have "n > 1"
haftmann@66808
  1088
      by simp
haftmann@66808
  1089
    with that have "m div n < m"
haftmann@66808
  1090
      by simp
haftmann@66808
  1091
    with P show False
haftmann@66808
  1092
      by simp
haftmann@66808
  1093
  qed
haftmann@66808
  1094
qed
haftmann@66808
  1095
haftmann@66808
  1096
lemma less_mult_imp_div_less:
haftmann@66808
  1097
  "m div n < i" if "m < i * n" for m n i :: nat
haftmann@66808
  1098
proof -
haftmann@66808
  1099
  from that have "i * n > 0"
haftmann@66808
  1100
    by (cases "i * n = 0") simp_all
haftmann@66808
  1101
  then have "i > 0" and "n > 0"
haftmann@66808
  1102
    by simp_all
haftmann@66808
  1103
  have "m div n * n \<le> m"
haftmann@66808
  1104
    by simp
haftmann@66808
  1105
  then have "m div n * n < i * n"
haftmann@66808
  1106
    using that by (rule le_less_trans)
haftmann@66808
  1107
  with \<open>n > 0\<close> show ?thesis
haftmann@66808
  1108
    by simp
haftmann@66808
  1109
qed
haftmann@66808
  1110
haftmann@66808
  1111
text \<open>A fact for the mutilated chess board\<close>
haftmann@66808
  1112
haftmann@66808
  1113
lemma mod_Suc:
haftmann@66808
  1114
  "Suc m mod n = (if Suc (m mod n) = n then 0 else Suc (m mod n))" (is "_ = ?rhs")
haftmann@66808
  1115
proof (cases "n = 0")
haftmann@66808
  1116
  case True
haftmann@66808
  1117
  then show ?thesis
haftmann@66808
  1118
    by simp
haftmann@66808
  1119
next
haftmann@66808
  1120
  case False
haftmann@66808
  1121
  have "Suc m mod n = Suc (m mod n) mod n"
haftmann@66808
  1122
    by (simp add: mod_simps)
haftmann@66808
  1123
  also have "\<dots> = ?rhs"
haftmann@66808
  1124
    using False by (auto intro!: mod_nat_eqI intro: neq_le_trans simp add: Suc_le_eq)
haftmann@66808
  1125
  finally show ?thesis .
haftmann@66808
  1126
qed
haftmann@66808
  1127
haftmann@66808
  1128
lemma Suc_times_mod_eq:
haftmann@66808
  1129
  "Suc (m * n) mod m = 1" if "Suc 0 < m"
haftmann@66808
  1130
  using that by (simp add: mod_Suc)
haftmann@66808
  1131
haftmann@66808
  1132
lemma Suc_times_numeral_mod_eq [simp]:
haftmann@66808
  1133
  "Suc (numeral k * n) mod numeral k = 1" if "numeral k \<noteq> (1::nat)"
haftmann@66808
  1134
  by (rule Suc_times_mod_eq) (use that in simp)
haftmann@66808
  1135
haftmann@66808
  1136
lemma Suc_div_le_mono [simp]:
haftmann@66808
  1137
  "m div n \<le> Suc m div n"
haftmann@66808
  1138
  by (simp add: div_le_mono)
haftmann@66808
  1139
haftmann@66808
  1140
text \<open>These lemmas collapse some needless occurrences of Suc:
haftmann@66808
  1141
  at least three Sucs, since two and fewer are rewritten back to Suc again!
haftmann@66808
  1142
  We already have some rules to simplify operands smaller than 3.\<close>
haftmann@66808
  1143
haftmann@66808
  1144
lemma div_Suc_eq_div_add3 [simp]:
haftmann@66808
  1145
  "m div Suc (Suc (Suc n)) = m div (3 + n)"
haftmann@66808
  1146
  by (simp add: Suc3_eq_add_3)
haftmann@66808
  1147
haftmann@66808
  1148
lemma mod_Suc_eq_mod_add3 [simp]:
haftmann@66808
  1149
  "m mod Suc (Suc (Suc n)) = m mod (3 + n)"
haftmann@66808
  1150
  by (simp add: Suc3_eq_add_3)
haftmann@66808
  1151
haftmann@66808
  1152
lemma Suc_div_eq_add3_div:
haftmann@66808
  1153
  "Suc (Suc (Suc m)) div n = (3 + m) div n"
haftmann@66808
  1154
  by (simp add: Suc3_eq_add_3)
haftmann@66808
  1155
haftmann@66808
  1156
lemma Suc_mod_eq_add3_mod:
haftmann@66808
  1157
  "Suc (Suc (Suc m)) mod n = (3 + m) mod n"
haftmann@66808
  1158
  by (simp add: Suc3_eq_add_3)
haftmann@66808
  1159
haftmann@66808
  1160
lemmas Suc_div_eq_add3_div_numeral [simp] =
haftmann@66808
  1161
  Suc_div_eq_add3_div [of _ "numeral v"] for v
haftmann@66808
  1162
haftmann@66808
  1163
lemmas Suc_mod_eq_add3_mod_numeral [simp] =
haftmann@66808
  1164
  Suc_mod_eq_add3_mod [of _ "numeral v"] for v
haftmann@66808
  1165
haftmann@66808
  1166
lemma (in field_char_0) of_nat_div:
haftmann@66808
  1167
  "of_nat (m div n) = ((of_nat m - of_nat (m mod n)) / of_nat n)"
haftmann@66808
  1168
proof -
haftmann@66808
  1169
  have "of_nat (m div n) = ((of_nat (m div n * n + m mod n) - of_nat (m mod n)) / of_nat n :: 'a)"
haftmann@66808
  1170
    unfolding of_nat_add by (cases "n = 0") simp_all
haftmann@66808
  1171
  then show ?thesis
haftmann@66808
  1172
    by simp
haftmann@66808
  1173
qed
haftmann@66808
  1174
haftmann@66808
  1175
text \<open>An ``induction'' law for modulus arithmetic.\<close>
haftmann@66808
  1176
haftmann@66808
  1177
lemma mod_induct [consumes 3, case_names step]:
haftmann@66808
  1178
  "P m" if "P n" and "n < p" and "m < p"
haftmann@66808
  1179
    and step: "\<And>n. n < p \<Longrightarrow> P n \<Longrightarrow> P (Suc n mod p)"
haftmann@66808
  1180
using \<open>m < p\<close> proof (induct m)
haftmann@66808
  1181
  case 0
haftmann@66808
  1182
  show ?case
haftmann@66808
  1183
  proof (rule ccontr)
haftmann@66808
  1184
    assume "\<not> P 0"
haftmann@66808
  1185
    from \<open>n < p\<close> have "0 < p"
haftmann@66808
  1186
      by simp
haftmann@66808
  1187
    from \<open>n < p\<close> obtain m where "0 < m" and "p = n + m"
haftmann@66808
  1188
      by (blast dest: less_imp_add_positive)
haftmann@66808
  1189
    with \<open>P n\<close> have "P (p - m)"
haftmann@66808
  1190
      by simp
haftmann@66808
  1191
    moreover have "\<not> P (p - m)"
haftmann@66808
  1192
    using \<open>0 < m\<close> proof (induct m)
haftmann@66808
  1193
      case 0
haftmann@66808
  1194
      then show ?case
haftmann@66808
  1195
        by simp
haftmann@66808
  1196
    next
haftmann@66808
  1197
      case (Suc m)
haftmann@66808
  1198
      show ?case
haftmann@66808
  1199
      proof
haftmann@66808
  1200
        assume P: "P (p - Suc m)"
haftmann@66808
  1201
        with \<open>\<not> P 0\<close> have "Suc m < p"
haftmann@66808
  1202
          by (auto intro: ccontr) 
haftmann@66808
  1203
        then have "Suc (p - Suc m) = p - m"
haftmann@66808
  1204
          by arith
haftmann@66808
  1205
        moreover from \<open>0 < p\<close> have "p - Suc m < p"
haftmann@66808
  1206
          by arith
haftmann@66808
  1207
        with P step have "P ((Suc (p - Suc m)) mod p)"
haftmann@66808
  1208
          by blast
haftmann@66808
  1209
        ultimately show False
haftmann@66808
  1210
          using \<open>\<not> P 0\<close> Suc.hyps by (cases "m = 0") simp_all
haftmann@66808
  1211
      qed
haftmann@66808
  1212
    qed
haftmann@66808
  1213
    ultimately show False
haftmann@66808
  1214
      by blast
haftmann@66808
  1215
  qed
haftmann@66808
  1216
next
haftmann@66808
  1217
  case (Suc m)
haftmann@66808
  1218
  then have "m < p" and mod: "Suc m mod p = Suc m"
haftmann@66808
  1219
    by simp_all
haftmann@66808
  1220
  from \<open>m < p\<close> have "P m"
haftmann@66808
  1221
    by (rule Suc.hyps)
haftmann@66808
  1222
  with \<open>m < p\<close> have "P (Suc m mod p)"
haftmann@66808
  1223
    by (rule step)
haftmann@66808
  1224
  with mod show ?case
haftmann@66808
  1225
    by simp
haftmann@66808
  1226
qed
haftmann@66808
  1227
haftmann@66808
  1228
lemma split_div:
haftmann@66808
  1229
  "P (m div n) \<longleftrightarrow> (n = 0 \<longrightarrow> P 0) \<and> (n \<noteq> 0 \<longrightarrow>
haftmann@66808
  1230
     (\<forall>i j. j < n \<longrightarrow> m = n * i + j \<longrightarrow> P i))"
haftmann@66808
  1231
     (is "?P = ?Q") for m n :: nat
haftmann@66808
  1232
proof (cases "n = 0")
haftmann@66808
  1233
  case True
haftmann@66808
  1234
  then show ?thesis
haftmann@66808
  1235
    by simp
haftmann@66808
  1236
next
haftmann@66808
  1237
  case False
haftmann@66808
  1238
  show ?thesis
haftmann@66808
  1239
  proof
haftmann@66808
  1240
    assume ?P
haftmann@66808
  1241
    with False show ?Q
haftmann@66808
  1242
      by auto
haftmann@66808
  1243
  next
haftmann@66808
  1244
    assume ?Q
haftmann@66808
  1245
    with False have *: "\<And>i j. j < n \<Longrightarrow> m = n * i + j \<Longrightarrow> P i"
haftmann@66808
  1246
      by simp
haftmann@66808
  1247
    with False show ?P
haftmann@66808
  1248
      by (auto intro: * [of "m mod n"])
haftmann@66808
  1249
  qed
haftmann@66808
  1250
qed
haftmann@66808
  1251
haftmann@66808
  1252
lemma split_div':
haftmann@66808
  1253
  "P (m div n) \<longleftrightarrow> n = 0 \<and> P 0 \<or> (\<exists>q. (n * q \<le> m \<and> m < n * Suc q) \<and> P q)"
haftmann@66808
  1254
proof (cases "n = 0")
haftmann@66808
  1255
  case True
haftmann@66808
  1256
  then show ?thesis
haftmann@66808
  1257
    by simp
haftmann@66808
  1258
next
haftmann@66808
  1259
  case False
haftmann@66808
  1260
  then have "n * q \<le> m \<and> m < n * Suc q \<longleftrightarrow> m div n = q" for q
haftmann@66808
  1261
    by (auto intro: div_nat_eqI dividend_less_times_div)
haftmann@66808
  1262
  then show ?thesis
haftmann@66808
  1263
    by auto
haftmann@66808
  1264
qed
haftmann@66808
  1265
haftmann@66808
  1266
lemma split_mod:
haftmann@66808
  1267
  "P (m mod n) \<longleftrightarrow> (n = 0 \<longrightarrow> P m) \<and> (n \<noteq> 0 \<longrightarrow>
haftmann@66808
  1268
     (\<forall>i j. j < n \<longrightarrow> m = n * i + j \<longrightarrow> P j))"
haftmann@66808
  1269
     (is "?P \<longleftrightarrow> ?Q") for m n :: nat
haftmann@66808
  1270
proof (cases "n = 0")
haftmann@66808
  1271
  case True
haftmann@66808
  1272
  then show ?thesis
haftmann@66808
  1273
    by simp
haftmann@66808
  1274
next
haftmann@66808
  1275
  case False
haftmann@66808
  1276
  show ?thesis
haftmann@66808
  1277
  proof
haftmann@66808
  1278
    assume ?P
haftmann@66808
  1279
    with False show ?Q
haftmann@66808
  1280
      by auto
haftmann@66808
  1281
  next
haftmann@66808
  1282
    assume ?Q
haftmann@66808
  1283
    with False have *: "\<And>i j. j < n \<Longrightarrow> m = n * i + j \<Longrightarrow> P j"
haftmann@66808
  1284
      by simp
haftmann@66808
  1285
    with False show ?P
haftmann@66808
  1286
      by (auto intro: * [of _ "m div n"])
haftmann@66808
  1287
  qed
haftmann@66808
  1288
qed
haftmann@66808
  1289
haftmann@66808
  1290
haftmann@66808
  1291
subsection \<open>Code generation\<close>
haftmann@66808
  1292
haftmann@66808
  1293
code_identifier
haftmann@66808
  1294
  code_module Euclidean_Division \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
haftmann@66808
  1295
haftmann@66808
  1296
end