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