src/HOL/Rings.thy
author haftmann
Sun Oct 08 22:28:21 2017 +0200 (20 months ago)
changeset 66808 1907167b6038
parent 66807 c3631f32dfeb
child 66810 cc2b490f9dc4
permissions -rw-r--r--
elementary definition of division on natural numbers
haftmann@35050
     1
(*  Title:      HOL/Rings.thy
wenzelm@32960
     2
    Author:     Gertrud Bauer
wenzelm@32960
     3
    Author:     Steven Obua
wenzelm@32960
     4
    Author:     Tobias Nipkow
wenzelm@32960
     5
    Author:     Lawrence C Paulson
wenzelm@32960
     6
    Author:     Markus Wenzel
wenzelm@32960
     7
    Author:     Jeremy Avigad
paulson@14265
     8
*)
paulson@14265
     9
wenzelm@60758
    10
section \<open>Rings\<close>
paulson@14265
    11
haftmann@35050
    12
theory Rings
wenzelm@63588
    13
  imports Groups Set
nipkow@15131
    14
begin
paulson@14504
    15
haftmann@22390
    16
class semiring = ab_semigroup_add + semigroup_mult +
hoelzl@58776
    17
  assumes distrib_right[algebra_simps]: "(a + b) * c = a * c + b * c"
hoelzl@58776
    18
  assumes distrib_left[algebra_simps]: "a * (b + c) = a * b + a * c"
haftmann@25152
    19
begin
haftmann@25152
    20
wenzelm@63325
    21
text \<open>For the \<open>combine_numerals\<close> simproc\<close>
wenzelm@63325
    22
lemma combine_common_factor: "a * e + (b * e + c) = (a + b) * e + c"
wenzelm@63325
    23
  by (simp add: distrib_right ac_simps)
haftmann@25152
    24
haftmann@25152
    25
end
paulson@14504
    26
haftmann@22390
    27
class mult_zero = times + zero +
haftmann@25062
    28
  assumes mult_zero_left [simp]: "0 * a = 0"
haftmann@25062
    29
  assumes mult_zero_right [simp]: "a * 0 = 0"
haftmann@58195
    30
begin
haftmann@58195
    31
wenzelm@63325
    32
lemma mult_not_zero: "a * b \<noteq> 0 \<Longrightarrow> a \<noteq> 0 \<and> b \<noteq> 0"
haftmann@58195
    33
  by auto
haftmann@58195
    34
haftmann@58195
    35
end
krauss@21199
    36
haftmann@58198
    37
class semiring_0 = semiring + comm_monoid_add + mult_zero
haftmann@58198
    38
huffman@29904
    39
class semiring_0_cancel = semiring + cancel_comm_monoid_add
haftmann@25186
    40
begin
paulson@14504
    41
haftmann@25186
    42
subclass semiring_0
haftmann@28823
    43
proof
krauss@21199
    44
  fix a :: 'a
wenzelm@63588
    45
  have "0 * a + 0 * a = 0 * a + 0"
wenzelm@63588
    46
    by (simp add: distrib_right [symmetric])
wenzelm@63588
    47
  then show "0 * a = 0"
wenzelm@63588
    48
    by (simp only: add_left_cancel)
wenzelm@63588
    49
  have "a * 0 + a * 0 = a * 0 + 0"
wenzelm@63588
    50
    by (simp add: distrib_left [symmetric])
wenzelm@63588
    51
  then show "a * 0 = 0"
wenzelm@63588
    52
    by (simp only: add_left_cancel)
krauss@21199
    53
qed
obua@14940
    54
haftmann@25186
    55
end
haftmann@25152
    56
haftmann@22390
    57
class comm_semiring = ab_semigroup_add + ab_semigroup_mult +
haftmann@25062
    58
  assumes distrib: "(a + b) * c = a * c + b * c"
haftmann@25152
    59
begin
paulson@14504
    60
haftmann@25152
    61
subclass semiring
haftmann@28823
    62
proof
obua@14738
    63
  fix a b c :: 'a
wenzelm@63588
    64
  show "(a + b) * c = a * c + b * c"
wenzelm@63588
    65
    by (simp add: distrib)
wenzelm@63588
    66
  have "a * (b + c) = (b + c) * a"
wenzelm@63588
    67
    by (simp add: ac_simps)
wenzelm@63588
    68
  also have "\<dots> = b * a + c * a"
wenzelm@63588
    69
    by (simp only: distrib)
wenzelm@63588
    70
  also have "\<dots> = a * b + a * c"
wenzelm@63588
    71
    by (simp add: ac_simps)
wenzelm@63588
    72
  finally show "a * (b + c) = a * b + a * c"
wenzelm@63588
    73
    by blast
paulson@14504
    74
qed
paulson@14504
    75
haftmann@25152
    76
end
paulson@14504
    77
haftmann@25152
    78
class comm_semiring_0 = comm_semiring + comm_monoid_add + mult_zero
haftmann@25152
    79
begin
haftmann@25152
    80
huffman@27516
    81
subclass semiring_0 ..
haftmann@25152
    82
haftmann@25152
    83
end
paulson@14504
    84
huffman@29904
    85
class comm_semiring_0_cancel = comm_semiring + cancel_comm_monoid_add
haftmann@25186
    86
begin
obua@14940
    87
huffman@27516
    88
subclass semiring_0_cancel ..
obua@14940
    89
huffman@28141
    90
subclass comm_semiring_0 ..
huffman@28141
    91
haftmann@25186
    92
end
krauss@21199
    93
haftmann@22390
    94
class zero_neq_one = zero + one +
haftmann@25062
    95
  assumes zero_neq_one [simp]: "0 \<noteq> 1"
haftmann@26193
    96
begin
haftmann@26193
    97
haftmann@26193
    98
lemma one_neq_zero [simp]: "1 \<noteq> 0"
wenzelm@63325
    99
  by (rule not_sym) (rule zero_neq_one)
haftmann@26193
   100
haftmann@54225
   101
definition of_bool :: "bool \<Rightarrow> 'a"
wenzelm@63325
   102
  where "of_bool p = (if p then 1 else 0)"
haftmann@54225
   103
haftmann@54225
   104
lemma of_bool_eq [simp, code]:
haftmann@54225
   105
  "of_bool False = 0"
haftmann@54225
   106
  "of_bool True = 1"
haftmann@54225
   107
  by (simp_all add: of_bool_def)
haftmann@54225
   108
wenzelm@63325
   109
lemma of_bool_eq_iff: "of_bool p = of_bool q \<longleftrightarrow> p = q"
haftmann@54225
   110
  by (simp add: of_bool_def)
haftmann@54225
   111
wenzelm@63325
   112
lemma split_of_bool [split]: "P (of_bool p) \<longleftrightarrow> (p \<longrightarrow> P 1) \<and> (\<not> p \<longrightarrow> P 0)"
haftmann@55187
   113
  by (cases p) simp_all
haftmann@55187
   114
wenzelm@63325
   115
lemma split_of_bool_asm: "P (of_bool p) \<longleftrightarrow> \<not> (p \<and> \<not> P 1 \<or> \<not> p \<and> \<not> P 0)"
haftmann@55187
   116
  by (cases p) simp_all
lp15@60562
   117
lp15@60562
   118
end
paulson@14265
   119
haftmann@22390
   120
class semiring_1 = zero_neq_one + semiring_0 + monoid_mult
paulson@14504
   121
wenzelm@60758
   122
text \<open>Abstract divisibility\<close>
haftmann@27651
   123
haftmann@27651
   124
class dvd = times
haftmann@27651
   125
begin
haftmann@27651
   126
wenzelm@63325
   127
definition dvd :: "'a \<Rightarrow> 'a \<Rightarrow> bool" (infix "dvd" 50)
wenzelm@63325
   128
  where "b dvd a \<longleftrightarrow> (\<exists>k. a = b * k)"
haftmann@27651
   129
haftmann@27651
   130
lemma dvdI [intro?]: "a = b * k \<Longrightarrow> b dvd a"
haftmann@27651
   131
  unfolding dvd_def ..
haftmann@27651
   132
haftmann@27651
   133
lemma dvdE [elim?]: "b dvd a \<Longrightarrow> (\<And>k. a = b * k \<Longrightarrow> P) \<Longrightarrow> P"
lp15@60562
   134
  unfolding dvd_def by blast
haftmann@27651
   135
haftmann@27651
   136
end
haftmann@27651
   137
haftmann@59009
   138
context comm_monoid_mult
haftmann@25152
   139
begin
obua@14738
   140
haftmann@59009
   141
subclass dvd .
haftmann@25152
   142
wenzelm@63325
   143
lemma dvd_refl [simp]: "a dvd a"
haftmann@28559
   144
proof
haftmann@28559
   145
  show "a = a * 1" by simp
haftmann@27651
   146
qed
haftmann@27651
   147
haftmann@62349
   148
lemma dvd_trans [trans]:
haftmann@27651
   149
  assumes "a dvd b" and "b dvd c"
haftmann@27651
   150
  shows "a dvd c"
haftmann@27651
   151
proof -
wenzelm@63588
   152
  from assms obtain v where "b = a * v"
wenzelm@63588
   153
    by (auto elim!: dvdE)
wenzelm@63588
   154
  moreover from assms obtain w where "c = b * w"
wenzelm@63588
   155
    by (auto elim!: dvdE)
wenzelm@63588
   156
  ultimately have "c = a * (v * w)"
wenzelm@63588
   157
    by (simp add: mult.assoc)
haftmann@28559
   158
  then show ?thesis ..
haftmann@27651
   159
qed
haftmann@27651
   160
wenzelm@63325
   161
lemma subset_divisors_dvd: "{c. c dvd a} \<subseteq> {c. c dvd b} \<longleftrightarrow> a dvd b"
haftmann@62366
   162
  by (auto simp add: subset_iff intro: dvd_trans)
haftmann@62366
   163
wenzelm@63325
   164
lemma strict_subset_divisors_dvd: "{c. c dvd a} \<subset> {c. c dvd b} \<longleftrightarrow> a dvd b \<and> \<not> b dvd a"
haftmann@62366
   165
  by (auto simp add: subset_iff intro: dvd_trans)
haftmann@62366
   166
wenzelm@63325
   167
lemma one_dvd [simp]: "1 dvd a"
haftmann@59009
   168
  by (auto intro!: dvdI)
haftmann@28559
   169
wenzelm@63325
   170
lemma dvd_mult [simp]: "a dvd c \<Longrightarrow> a dvd (b * c)"
haftmann@59009
   171
  by (auto intro!: mult.left_commute dvdI elim!: dvdE)
haftmann@27651
   172
wenzelm@63325
   173
lemma dvd_mult2 [simp]: "a dvd b \<Longrightarrow> a dvd (b * c)"
lp15@60562
   174
  using dvd_mult [of a b c] by (simp add: ac_simps)
haftmann@27651
   175
wenzelm@63325
   176
lemma dvd_triv_right [simp]: "a dvd b * a"
haftmann@59009
   177
  by (rule dvd_mult) (rule dvd_refl)
haftmann@27651
   178
wenzelm@63325
   179
lemma dvd_triv_left [simp]: "a dvd a * b"
haftmann@59009
   180
  by (rule dvd_mult2) (rule dvd_refl)
haftmann@27651
   181
haftmann@27651
   182
lemma mult_dvd_mono:
nipkow@30042
   183
  assumes "a dvd b"
nipkow@30042
   184
    and "c dvd d"
haftmann@27651
   185
  shows "a * c dvd b * d"
haftmann@27651
   186
proof -
wenzelm@60758
   187
  from \<open>a dvd b\<close> obtain b' where "b = a * b'" ..
wenzelm@60758
   188
  moreover from \<open>c dvd d\<close> obtain d' where "d = c * d'" ..
wenzelm@63588
   189
  ultimately have "b * d = (a * c) * (b' * d')"
wenzelm@63588
   190
    by (simp add: ac_simps)
haftmann@27651
   191
  then show ?thesis ..
haftmann@27651
   192
qed
haftmann@27651
   193
wenzelm@63325
   194
lemma dvd_mult_left: "a * b dvd c \<Longrightarrow> a dvd c"
haftmann@59009
   195
  by (simp add: dvd_def mult.assoc) blast
haftmann@27651
   196
wenzelm@63325
   197
lemma dvd_mult_right: "a * b dvd c \<Longrightarrow> b dvd c"
haftmann@59009
   198
  using dvd_mult_left [of b a c] by (simp add: ac_simps)
lp15@60562
   199
haftmann@59009
   200
end
haftmann@59009
   201
haftmann@59009
   202
class comm_semiring_1 = zero_neq_one + comm_semiring_0 + comm_monoid_mult
haftmann@59009
   203
begin
haftmann@59009
   204
haftmann@59009
   205
subclass semiring_1 ..
haftmann@27651
   206
wenzelm@63325
   207
lemma dvd_0_left_iff [simp]: "0 dvd a \<longleftrightarrow> a = 0"
haftmann@59009
   208
  by (auto intro: dvd_refl elim!: dvdE)
haftmann@27651
   209
wenzelm@63325
   210
lemma dvd_0_right [iff]: "a dvd 0"
haftmann@59009
   211
proof
haftmann@59009
   212
  show "0 = a * 0" by simp
haftmann@59009
   213
qed
haftmann@59009
   214
wenzelm@63325
   215
lemma dvd_0_left: "0 dvd a \<Longrightarrow> a = 0"
haftmann@59009
   216
  by simp
haftmann@59009
   217
haftmann@59009
   218
lemma dvd_add [simp]:
haftmann@59009
   219
  assumes "a dvd b" and "a dvd c"
haftmann@59009
   220
  shows "a dvd (b + c)"
haftmann@27651
   221
proof -
wenzelm@60758
   222
  from \<open>a dvd b\<close> obtain b' where "b = a * b'" ..
wenzelm@60758
   223
  moreover from \<open>a dvd c\<close> obtain c' where "c = a * c'" ..
wenzelm@63588
   224
  ultimately have "b + c = a * (b' + c')"
wenzelm@63588
   225
    by (simp add: distrib_left)
haftmann@27651
   226
  then show ?thesis ..
haftmann@27651
   227
qed
haftmann@27651
   228
haftmann@25152
   229
end
paulson@14421
   230
huffman@29904
   231
class semiring_1_cancel = semiring + cancel_comm_monoid_add
huffman@29904
   232
  + zero_neq_one + monoid_mult
haftmann@25267
   233
begin
obua@14940
   234
huffman@27516
   235
subclass semiring_0_cancel ..
haftmann@25512
   236
huffman@27516
   237
subclass semiring_1 ..
haftmann@25267
   238
haftmann@25267
   239
end
krauss@21199
   240
wenzelm@63325
   241
class comm_semiring_1_cancel =
wenzelm@63325
   242
  comm_semiring + cancel_comm_monoid_add + zero_neq_one + comm_monoid_mult +
lp15@60562
   243
  assumes right_diff_distrib' [algebra_simps]: "a * (b - c) = a * b - a * c"
haftmann@25267
   244
begin
obua@14738
   245
huffman@27516
   246
subclass semiring_1_cancel ..
huffman@27516
   247
subclass comm_semiring_0_cancel ..
huffman@27516
   248
subclass comm_semiring_1 ..
haftmann@25267
   249
wenzelm@63325
   250
lemma left_diff_distrib' [algebra_simps]: "(b - c) * a = b * a - c * a"
haftmann@59816
   251
  by (simp add: algebra_simps)
haftmann@59816
   252
wenzelm@63325
   253
lemma dvd_add_times_triv_left_iff [simp]: "a dvd c * a + b \<longleftrightarrow> a dvd b"
haftmann@59816
   254
proof -
haftmann@59816
   255
  have "a dvd a * c + b \<longleftrightarrow> a dvd b" (is "?P \<longleftrightarrow> ?Q")
haftmann@59816
   256
  proof
wenzelm@63325
   257
    assume ?Q
wenzelm@63325
   258
    then show ?P by simp
haftmann@59816
   259
  next
haftmann@59816
   260
    assume ?P
haftmann@59816
   261
    then obtain d where "a * c + b = a * d" ..
haftmann@59816
   262
    then have "a * c + b - a * c = a * d - a * c" by simp
haftmann@59816
   263
    then have "b = a * d - a * c" by simp
lp15@60562
   264
    then have "b = a * (d - c)" by (simp add: algebra_simps)
haftmann@59816
   265
    then show ?Q ..
haftmann@59816
   266
  qed
haftmann@59816
   267
  then show "a dvd c * a + b \<longleftrightarrow> a dvd b" by (simp add: ac_simps)
haftmann@59816
   268
qed
haftmann@59816
   269
wenzelm@63325
   270
lemma dvd_add_times_triv_right_iff [simp]: "a dvd b + c * a \<longleftrightarrow> a dvd b"
haftmann@59816
   271
  using dvd_add_times_triv_left_iff [of a c b] by (simp add: ac_simps)
haftmann@59816
   272
wenzelm@63325
   273
lemma dvd_add_triv_left_iff [simp]: "a dvd a + b \<longleftrightarrow> a dvd b"
haftmann@59816
   274
  using dvd_add_times_triv_left_iff [of a 1 b] by simp
haftmann@59816
   275
wenzelm@63325
   276
lemma dvd_add_triv_right_iff [simp]: "a dvd b + a \<longleftrightarrow> a dvd b"
haftmann@59816
   277
  using dvd_add_times_triv_right_iff [of a b 1] by simp
haftmann@59816
   278
haftmann@59816
   279
lemma dvd_add_right_iff:
haftmann@59816
   280
  assumes "a dvd b"
haftmann@59816
   281
  shows "a dvd b + c \<longleftrightarrow> a dvd c" (is "?P \<longleftrightarrow> ?Q")
haftmann@59816
   282
proof
wenzelm@63325
   283
  assume ?P
wenzelm@63325
   284
  then obtain d where "b + c = a * d" ..
wenzelm@60758
   285
  moreover from \<open>a dvd b\<close> obtain e where "b = a * e" ..
haftmann@59816
   286
  ultimately have "a * e + c = a * d" by simp
haftmann@59816
   287
  then have "a * e + c - a * e = a * d - a * e" by simp
haftmann@59816
   288
  then have "c = a * d - a * e" by simp
haftmann@59816
   289
  then have "c = a * (d - e)" by (simp add: algebra_simps)
haftmann@59816
   290
  then show ?Q ..
haftmann@59816
   291
next
wenzelm@63325
   292
  assume ?Q
wenzelm@63325
   293
  with assms show ?P by simp
haftmann@59816
   294
qed
haftmann@59816
   295
wenzelm@63325
   296
lemma dvd_add_left_iff: "a dvd c \<Longrightarrow> a dvd b + c \<longleftrightarrow> a dvd b"
wenzelm@63325
   297
  using dvd_add_right_iff [of a c b] by (simp add: ac_simps)
haftmann@59816
   298
haftmann@59816
   299
end
haftmann@59816
   300
haftmann@22390
   301
class ring = semiring + ab_group_add
haftmann@25267
   302
begin
haftmann@25152
   303
huffman@27516
   304
subclass semiring_0_cancel ..
haftmann@25152
   305
wenzelm@60758
   306
text \<open>Distribution rules\<close>
haftmann@25152
   307
haftmann@25152
   308
lemma minus_mult_left: "- (a * b) = - a * b"
wenzelm@63325
   309
  by (rule minus_unique) (simp add: distrib_right [symmetric])
haftmann@25152
   310
haftmann@25152
   311
lemma minus_mult_right: "- (a * b) = a * - b"
wenzelm@63325
   312
  by (rule minus_unique) (simp add: distrib_left [symmetric])
haftmann@25152
   313
wenzelm@63325
   314
text \<open>Extract signs from products\<close>
blanchet@54147
   315
lemmas mult_minus_left [simp] = minus_mult_left [symmetric]
blanchet@54147
   316
lemmas mult_minus_right [simp] = minus_mult_right [symmetric]
huffman@29407
   317
haftmann@25152
   318
lemma minus_mult_minus [simp]: "- a * - b = a * b"
wenzelm@63325
   319
  by simp
haftmann@25152
   320
haftmann@25152
   321
lemma minus_mult_commute: "- a * b = a * - b"
wenzelm@63325
   322
  by simp
nipkow@29667
   323
wenzelm@63325
   324
lemma right_diff_distrib [algebra_simps]: "a * (b - c) = a * b - a * c"
haftmann@54230
   325
  using distrib_left [of a b "-c "] by simp
nipkow@29667
   326
wenzelm@63325
   327
lemma left_diff_distrib [algebra_simps]: "(a - b) * c = a * c - b * c"
haftmann@54230
   328
  using distrib_right [of a "- b" c] by simp
haftmann@25152
   329
wenzelm@63325
   330
lemmas ring_distribs = distrib_left distrib_right left_diff_distrib right_diff_distrib
haftmann@25152
   331
wenzelm@63325
   332
lemma eq_add_iff1: "a * e + c = b * e + d \<longleftrightarrow> (a - b) * e + c = d"
wenzelm@63325
   333
  by (simp add: algebra_simps)
haftmann@25230
   334
wenzelm@63325
   335
lemma eq_add_iff2: "a * e + c = b * e + d \<longleftrightarrow> c = (b - a) * e + d"
wenzelm@63325
   336
  by (simp add: algebra_simps)
haftmann@25230
   337
haftmann@25152
   338
end
haftmann@25152
   339
wenzelm@63325
   340
lemmas ring_distribs = distrib_left distrib_right left_diff_distrib right_diff_distrib
haftmann@25152
   341
haftmann@22390
   342
class comm_ring = comm_semiring + ab_group_add
haftmann@25267
   343
begin
obua@14738
   344
huffman@27516
   345
subclass ring ..
huffman@28141
   346
subclass comm_semiring_0_cancel ..
haftmann@25267
   347
wenzelm@63325
   348
lemma square_diff_square_factored: "x * x - y * y = (x + y) * (x - y)"
huffman@44350
   349
  by (simp add: algebra_simps)
huffman@44350
   350
haftmann@25267
   351
end
obua@14738
   352
haftmann@22390
   353
class ring_1 = ring + zero_neq_one + monoid_mult
haftmann@25267
   354
begin
paulson@14265
   355
huffman@27516
   356
subclass semiring_1_cancel ..
haftmann@25267
   357
wenzelm@63325
   358
lemma square_diff_one_factored: "x * x - 1 = (x + 1) * (x - 1)"
huffman@44346
   359
  by (simp add: algebra_simps)
huffman@44346
   360
haftmann@25267
   361
end
haftmann@25152
   362
haftmann@22390
   363
class comm_ring_1 = comm_ring + zero_neq_one + comm_monoid_mult
haftmann@25267
   364
begin
obua@14738
   365
huffman@27516
   366
subclass ring_1 ..
lp15@60562
   367
subclass comm_semiring_1_cancel
haftmann@59816
   368
  by unfold_locales (simp add: algebra_simps)
haftmann@58647
   369
huffman@29465
   370
lemma dvd_minus_iff [simp]: "x dvd - y \<longleftrightarrow> x dvd y"
huffman@29408
   371
proof
huffman@29408
   372
  assume "x dvd - y"
huffman@29408
   373
  then have "x dvd - 1 * - y" by (rule dvd_mult)
huffman@29408
   374
  then show "x dvd y" by simp
huffman@29408
   375
next
huffman@29408
   376
  assume "x dvd y"
huffman@29408
   377
  then have "x dvd - 1 * y" by (rule dvd_mult)
huffman@29408
   378
  then show "x dvd - y" by simp
huffman@29408
   379
qed
huffman@29408
   380
huffman@29465
   381
lemma minus_dvd_iff [simp]: "- x dvd y \<longleftrightarrow> x dvd y"
huffman@29408
   382
proof
huffman@29408
   383
  assume "- x dvd y"
huffman@29408
   384
  then obtain k where "y = - x * k" ..
huffman@29408
   385
  then have "y = x * - k" by simp
huffman@29408
   386
  then show "x dvd y" ..
huffman@29408
   387
next
huffman@29408
   388
  assume "x dvd y"
huffman@29408
   389
  then obtain k where "y = x * k" ..
huffman@29408
   390
  then have "y = - x * - k" by simp
huffman@29408
   391
  then show "- x dvd y" ..
huffman@29408
   392
qed
huffman@29408
   393
wenzelm@63325
   394
lemma dvd_diff [simp]: "x dvd y \<Longrightarrow> x dvd z \<Longrightarrow> x dvd (y - z)"
haftmann@54230
   395
  using dvd_add [of x y "- z"] by simp
huffman@29409
   396
haftmann@25267
   397
end
haftmann@25152
   398
haftmann@59833
   399
class semiring_no_zero_divisors = semiring_0 +
haftmann@59833
   400
  assumes no_zero_divisors: "a \<noteq> 0 \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> a * b \<noteq> 0"
haftmann@25230
   401
begin
haftmann@25230
   402
haftmann@59833
   403
lemma divisors_zero:
haftmann@59833
   404
  assumes "a * b = 0"
haftmann@59833
   405
  shows "a = 0 \<or> b = 0"
haftmann@59833
   406
proof (rule classical)
wenzelm@63325
   407
  assume "\<not> ?thesis"
haftmann@59833
   408
  then have "a \<noteq> 0" and "b \<noteq> 0" by auto
haftmann@59833
   409
  with no_zero_divisors have "a * b \<noteq> 0" by blast
haftmann@59833
   410
  with assms show ?thesis by simp
haftmann@59833
   411
qed
haftmann@59833
   412
wenzelm@63325
   413
lemma mult_eq_0_iff [simp]: "a * b = 0 \<longleftrightarrow> a = 0 \<or> b = 0"
haftmann@25230
   414
proof (cases "a = 0 \<or> b = 0")
wenzelm@63325
   415
  case False
wenzelm@63325
   416
  then have "a \<noteq> 0" and "b \<noteq> 0" by auto
haftmann@25230
   417
    then show ?thesis using no_zero_divisors by simp
haftmann@25230
   418
next
wenzelm@63325
   419
  case True
wenzelm@63325
   420
  then show ?thesis by auto
haftmann@25230
   421
qed
haftmann@25230
   422
haftmann@58952
   423
end
haftmann@58952
   424
haftmann@62481
   425
class semiring_1_no_zero_divisors = semiring_1 + semiring_no_zero_divisors
haftmann@62481
   426
haftmann@60516
   427
class semiring_no_zero_divisors_cancel = semiring_no_zero_divisors +
haftmann@60516
   428
  assumes mult_cancel_right [simp]: "a * c = b * c \<longleftrightarrow> c = 0 \<or> a = b"
haftmann@60516
   429
    and mult_cancel_left [simp]: "c * a = c * b \<longleftrightarrow> c = 0 \<or> a = b"
haftmann@58952
   430
begin
haftmann@58952
   431
wenzelm@63325
   432
lemma mult_left_cancel: "c \<noteq> 0 \<Longrightarrow> c * a = c * b \<longleftrightarrow> a = b"
lp15@60562
   433
  by simp
lp15@56217
   434
wenzelm@63325
   435
lemma mult_right_cancel: "c \<noteq> 0 \<Longrightarrow> a * c = b * c \<longleftrightarrow> a = b"
lp15@60562
   436
  by simp
lp15@56217
   437
haftmann@25230
   438
end
huffman@22990
   439
haftmann@60516
   440
class ring_no_zero_divisors = ring + semiring_no_zero_divisors
haftmann@60516
   441
begin
haftmann@60516
   442
haftmann@60516
   443
subclass semiring_no_zero_divisors_cancel
haftmann@60516
   444
proof
haftmann@60516
   445
  fix a b c
haftmann@60516
   446
  have "a * c = b * c \<longleftrightarrow> (a - b) * c = 0"
haftmann@60516
   447
    by (simp add: algebra_simps)
haftmann@60516
   448
  also have "\<dots> \<longleftrightarrow> c = 0 \<or> a = b"
haftmann@60516
   449
    by auto
haftmann@60516
   450
  finally show "a * c = b * c \<longleftrightarrow> c = 0 \<or> a = b" .
haftmann@60516
   451
  have "c * a = c * b \<longleftrightarrow> c * (a - b) = 0"
haftmann@60516
   452
    by (simp add: algebra_simps)
haftmann@60516
   453
  also have "\<dots> \<longleftrightarrow> c = 0 \<or> a = b"
haftmann@60516
   454
    by auto
haftmann@60516
   455
  finally show "c * a = c * b \<longleftrightarrow> c = 0 \<or> a = b" .
haftmann@60516
   456
qed
haftmann@60516
   457
haftmann@60516
   458
end
haftmann@60516
   459
huffman@23544
   460
class ring_1_no_zero_divisors = ring_1 + ring_no_zero_divisors
haftmann@26274
   461
begin
haftmann@26274
   462
haftmann@62481
   463
subclass semiring_1_no_zero_divisors ..
haftmann@62481
   464
wenzelm@63325
   465
lemma square_eq_1_iff: "x * x = 1 \<longleftrightarrow> x = 1 \<or> x = - 1"
huffman@36821
   466
proof -
huffman@36821
   467
  have "(x - 1) * (x + 1) = x * x - 1"
huffman@36821
   468
    by (simp add: algebra_simps)
wenzelm@63325
   469
  then have "x * x = 1 \<longleftrightarrow> (x - 1) * (x + 1) = 0"
huffman@36821
   470
    by simp
wenzelm@63325
   471
  then show ?thesis
huffman@36821
   472
    by (simp add: eq_neg_iff_add_eq_0)
huffman@36821
   473
qed
huffman@36821
   474
wenzelm@63325
   475
lemma mult_cancel_right1 [simp]: "c = b * c \<longleftrightarrow> c = 0 \<or> b = 1"
wenzelm@63325
   476
  using mult_cancel_right [of 1 c b] by auto
haftmann@26274
   477
wenzelm@63325
   478
lemma mult_cancel_right2 [simp]: "a * c = c \<longleftrightarrow> c = 0 \<or> a = 1"
wenzelm@63325
   479
  using mult_cancel_right [of a c 1] by simp
lp15@60562
   480
wenzelm@63325
   481
lemma mult_cancel_left1 [simp]: "c = c * b \<longleftrightarrow> c = 0 \<or> b = 1"
wenzelm@63325
   482
  using mult_cancel_left [of c 1 b] by force
haftmann@26274
   483
wenzelm@63325
   484
lemma mult_cancel_left2 [simp]: "c * a = c \<longleftrightarrow> c = 0 \<or> a = 1"
wenzelm@63325
   485
  using mult_cancel_left [of c a 1] by simp
haftmann@26274
   486
haftmann@26274
   487
end
huffman@22990
   488
lp15@60562
   489
class semidom = comm_semiring_1_cancel + semiring_no_zero_divisors
haftmann@62481
   490
begin
haftmann@62481
   491
haftmann@62481
   492
subclass semiring_1_no_zero_divisors ..
haftmann@62481
   493
haftmann@62481
   494
end
haftmann@59833
   495
haftmann@59833
   496
class idom = comm_ring_1 + semiring_no_zero_divisors
haftmann@25186
   497
begin
paulson@14421
   498
haftmann@59833
   499
subclass semidom ..
haftmann@59833
   500
huffman@27516
   501
subclass ring_1_no_zero_divisors ..
huffman@22990
   502
wenzelm@63325
   503
lemma dvd_mult_cancel_right [simp]: "a * c dvd b * c \<longleftrightarrow> c = 0 \<or> a dvd b"
huffman@29981
   504
proof -
huffman@29981
   505
  have "a * c dvd b * c \<longleftrightarrow> (\<exists>k. b * c = (a * k) * c)"
haftmann@57514
   506
    unfolding dvd_def by (simp add: ac_simps)
huffman@29981
   507
  also have "(\<exists>k. b * c = (a * k) * c) \<longleftrightarrow> c = 0 \<or> a dvd b"
huffman@29981
   508
    unfolding dvd_def by simp
huffman@29981
   509
  finally show ?thesis .
huffman@29981
   510
qed
huffman@29981
   511
wenzelm@63325
   512
lemma dvd_mult_cancel_left [simp]: "c * a dvd c * b \<longleftrightarrow> c = 0 \<or> a dvd b"
huffman@29981
   513
proof -
huffman@29981
   514
  have "c * a dvd c * b \<longleftrightarrow> (\<exists>k. b * c = (a * k) * c)"
haftmann@57514
   515
    unfolding dvd_def by (simp add: ac_simps)
huffman@29981
   516
  also have "(\<exists>k. b * c = (a * k) * c) \<longleftrightarrow> c = 0 \<or> a dvd b"
huffman@29981
   517
    unfolding dvd_def by simp
huffman@29981
   518
  finally show ?thesis .
huffman@29981
   519
qed
huffman@29981
   520
haftmann@60516
   521
lemma square_eq_iff: "a * a = b * b \<longleftrightarrow> a = b \<or> a = - b"
haftmann@59833
   522
proof
haftmann@59833
   523
  assume "a * a = b * b"
haftmann@59833
   524
  then have "(a - b) * (a + b) = 0"
haftmann@59833
   525
    by (simp add: algebra_simps)
haftmann@59833
   526
  then show "a = b \<or> a = - b"
haftmann@59833
   527
    by (simp add: eq_neg_iff_add_eq_0)
haftmann@59833
   528
next
haftmann@59833
   529
  assume "a = b \<or> a = - b"
haftmann@59833
   530
  then show "a * a = b * b" by auto
haftmann@59833
   531
qed
haftmann@59833
   532
haftmann@25186
   533
end
haftmann@25152
   534
haftmann@64290
   535
class idom_abs_sgn = idom + abs + sgn +
haftmann@64290
   536
  assumes sgn_mult_abs: "sgn a * \<bar>a\<bar> = a"
haftmann@64290
   537
    and sgn_sgn [simp]: "sgn (sgn a) = sgn a"
haftmann@64290
   538
    and abs_abs [simp]: "\<bar>\<bar>a\<bar>\<bar> = \<bar>a\<bar>"
haftmann@64290
   539
    and abs_0 [simp]: "\<bar>0\<bar> = 0"
haftmann@64290
   540
    and sgn_0 [simp]: "sgn 0 = 0"
haftmann@64290
   541
    and sgn_1 [simp]: "sgn 1 = 1"
haftmann@64290
   542
    and sgn_minus_1: "sgn (- 1) = - 1"
haftmann@64290
   543
    and sgn_mult: "sgn (a * b) = sgn a * sgn b"
haftmann@64290
   544
begin
haftmann@64290
   545
haftmann@64290
   546
lemma sgn_eq_0_iff:
haftmann@64290
   547
  "sgn a = 0 \<longleftrightarrow> a = 0"
haftmann@64290
   548
proof -
haftmann@64290
   549
  { assume "sgn a = 0"
haftmann@64290
   550
    then have "sgn a * \<bar>a\<bar> = 0"
haftmann@64290
   551
      by simp
haftmann@64290
   552
    then have "a = 0"
haftmann@64290
   553
      by (simp add: sgn_mult_abs)
haftmann@64290
   554
  } then show ?thesis
haftmann@64290
   555
    by auto
haftmann@64290
   556
qed
haftmann@64290
   557
haftmann@64290
   558
lemma abs_eq_0_iff:
haftmann@64290
   559
  "\<bar>a\<bar> = 0 \<longleftrightarrow> a = 0"
haftmann@64290
   560
proof -
haftmann@64290
   561
  { assume "\<bar>a\<bar> = 0"
haftmann@64290
   562
    then have "sgn a * \<bar>a\<bar> = 0"
haftmann@64290
   563
      by simp
haftmann@64290
   564
    then have "a = 0"
haftmann@64290
   565
      by (simp add: sgn_mult_abs)
haftmann@64290
   566
  } then show ?thesis
haftmann@64290
   567
    by auto
haftmann@64290
   568
qed
haftmann@64290
   569
haftmann@64290
   570
lemma abs_mult_sgn:
haftmann@64290
   571
  "\<bar>a\<bar> * sgn a = a"
haftmann@64290
   572
  using sgn_mult_abs [of a] by (simp add: ac_simps)
haftmann@64290
   573
haftmann@64290
   574
lemma abs_1 [simp]:
haftmann@64290
   575
  "\<bar>1\<bar> = 1"
haftmann@64290
   576
  using sgn_mult_abs [of 1] by simp
haftmann@64290
   577
haftmann@64290
   578
lemma sgn_abs [simp]:
haftmann@64290
   579
  "\<bar>sgn a\<bar> = of_bool (a \<noteq> 0)"
haftmann@64290
   580
  using sgn_mult_abs [of "sgn a"] mult_cancel_left [of "sgn a" "\<bar>sgn a\<bar>" 1]
haftmann@64290
   581
  by (auto simp add: sgn_eq_0_iff)
haftmann@64290
   582
haftmann@64290
   583
lemma abs_sgn [simp]:
haftmann@64290
   584
  "sgn \<bar>a\<bar> = of_bool (a \<noteq> 0)"
haftmann@64290
   585
  using sgn_mult_abs [of "\<bar>a\<bar>"] mult_cancel_right [of "sgn \<bar>a\<bar>" "\<bar>a\<bar>" 1]
haftmann@64290
   586
  by (auto simp add: abs_eq_0_iff)
haftmann@64290
   587
haftmann@64290
   588
lemma abs_mult:
haftmann@64290
   589
  "\<bar>a * b\<bar> = \<bar>a\<bar> * \<bar>b\<bar>"
haftmann@64290
   590
proof (cases "a = 0 \<or> b = 0")
haftmann@64290
   591
  case True
haftmann@64290
   592
  then show ?thesis
haftmann@64290
   593
    by auto
haftmann@64290
   594
next
haftmann@64290
   595
  case False
haftmann@64290
   596
  then have *: "sgn (a * b) \<noteq> 0"
haftmann@64290
   597
    by (simp add: sgn_eq_0_iff)
haftmann@64290
   598
  from abs_mult_sgn [of "a * b"] abs_mult_sgn [of a] abs_mult_sgn [of b]
haftmann@64290
   599
  have "\<bar>a * b\<bar> * sgn (a * b) = \<bar>a\<bar> * sgn a * \<bar>b\<bar> * sgn b"
haftmann@64290
   600
    by (simp add: ac_simps)
haftmann@64290
   601
  then have "\<bar>a * b\<bar> * sgn (a * b) = \<bar>a\<bar> * \<bar>b\<bar> * sgn (a * b)"
haftmann@64290
   602
    by (simp add: sgn_mult ac_simps)
haftmann@64290
   603
  with * show ?thesis
haftmann@64290
   604
    by simp
haftmann@64290
   605
qed
haftmann@64290
   606
haftmann@64290
   607
lemma sgn_minus [simp]:
haftmann@64290
   608
  "sgn (- a) = - sgn a"
haftmann@64290
   609
proof -
haftmann@64290
   610
  from sgn_minus_1 have "sgn (- 1 * a) = - 1 * sgn a"
haftmann@64290
   611
    by (simp only: sgn_mult)
haftmann@64290
   612
  then show ?thesis
haftmann@64290
   613
    by simp
haftmann@64290
   614
qed
haftmann@64290
   615
haftmann@64290
   616
lemma abs_minus [simp]:
haftmann@64290
   617
  "\<bar>- a\<bar> = \<bar>a\<bar>"
haftmann@64290
   618
proof -
haftmann@64290
   619
  have [simp]: "\<bar>- 1\<bar> = 1"
haftmann@64290
   620
    using sgn_mult_abs [of "- 1"] by simp
haftmann@64290
   621
  then have "\<bar>- 1 * a\<bar> = 1 * \<bar>a\<bar>"
haftmann@64290
   622
    by (simp only: abs_mult)
haftmann@64290
   623
  then show ?thesis
haftmann@64290
   624
    by simp
haftmann@64290
   625
qed
haftmann@64290
   626
haftmann@64290
   627
end
haftmann@64290
   628
wenzelm@60758
   629
text \<open>
haftmann@35302
   630
  The theory of partially ordered rings is taken from the books:
wenzelm@63325
   631
    \<^item> \<^emph>\<open>Lattice Theory\<close> by Garret Birkhoff, American Mathematical Society, 1979
wenzelm@63325
   632
    \<^item> \<^emph>\<open>Partially Ordered Algebraic Systems\<close>, Pergamon Press, 1963
wenzelm@63325
   633
lp15@60562
   634
  Most of the used notions can also be looked up in
wenzelm@63680
   635
    \<^item> \<^url>\<open>http://www.mathworld.com\<close> by Eric Weisstein et. al.
wenzelm@63325
   636
    \<^item> \<^emph>\<open>Algebra I\<close> by van der Waerden, Springer
wenzelm@60758
   637
\<close>
haftmann@35302
   638
haftmann@63950
   639
text \<open>Syntactic division operator\<close>
haftmann@63950
   640
haftmann@60353
   641
class divide =
haftmann@60429
   642
  fixes divide :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixl "div" 70)
haftmann@60353
   643
wenzelm@60758
   644
setup \<open>Sign.add_const_constraint (@{const_name "divide"}, SOME @{typ "'a \<Rightarrow> 'a \<Rightarrow> 'a"})\<close>
haftmann@60353
   645
haftmann@60353
   646
context semiring
haftmann@60353
   647
begin
haftmann@60353
   648
haftmann@60353
   649
lemma [field_simps]:
haftmann@60429
   650
  shows distrib_left_NO_MATCH: "NO_MATCH (x div y) a \<Longrightarrow> a * (b + c) = a * b + a * c"
haftmann@60429
   651
    and distrib_right_NO_MATCH: "NO_MATCH (x div y) c \<Longrightarrow> (a + b) * c = a * c + b * c"
haftmann@60353
   652
  by (rule distrib_left distrib_right)+
haftmann@60353
   653
haftmann@60353
   654
end
haftmann@60353
   655
haftmann@60353
   656
context ring
haftmann@60353
   657
begin
haftmann@60353
   658
haftmann@60353
   659
lemma [field_simps]:
haftmann@60429
   660
  shows left_diff_distrib_NO_MATCH: "NO_MATCH (x div y) c \<Longrightarrow> (a - b) * c = a * c - b * c"
haftmann@60429
   661
    and right_diff_distrib_NO_MATCH: "NO_MATCH (x div y) a \<Longrightarrow> a * (b - c) = a * b - a * c"
haftmann@60353
   662
  by (rule left_diff_distrib right_diff_distrib)+
haftmann@60353
   663
haftmann@60353
   664
end
haftmann@60353
   665
wenzelm@60758
   666
setup \<open>Sign.add_const_constraint (@{const_name "divide"}, SOME @{typ "'a::divide \<Rightarrow> 'a \<Rightarrow> 'a"})\<close>
haftmann@60353
   667
haftmann@63950
   668
text \<open>Algebraic classes with division\<close>
haftmann@63950
   669
  
haftmann@60353
   670
class semidom_divide = semidom + divide +
haftmann@64240
   671
  assumes nonzero_mult_div_cancel_right [simp]: "b \<noteq> 0 \<Longrightarrow> (a * b) div b = a"
haftmann@64240
   672
  assumes div_by_0 [simp]: "a div 0 = 0"
haftmann@60353
   673
begin
haftmann@60353
   674
haftmann@64240
   675
lemma nonzero_mult_div_cancel_left [simp]: "a \<noteq> 0 \<Longrightarrow> (a * b) div a = b"
haftmann@64240
   676
  using nonzero_mult_div_cancel_right [of a b] by (simp add: ac_simps)
haftmann@60353
   677
haftmann@60516
   678
subclass semiring_no_zero_divisors_cancel
haftmann@60516
   679
proof
wenzelm@63325
   680
  show *: "a * c = b * c \<longleftrightarrow> c = 0 \<or> a = b" for a b c
wenzelm@63325
   681
  proof (cases "c = 0")
wenzelm@63325
   682
    case True
wenzelm@63325
   683
    then show ?thesis by simp
wenzelm@63325
   684
  next
wenzelm@63325
   685
    case False
wenzelm@63588
   686
    have "a = b" if "a * c = b * c"
wenzelm@63588
   687
    proof -
wenzelm@63588
   688
      from that have "a * c div c = b * c div c"
wenzelm@63325
   689
        by simp
wenzelm@63588
   690
      with False show ?thesis
wenzelm@63325
   691
        by simp
wenzelm@63588
   692
    qed
wenzelm@63325
   693
    then show ?thesis by auto
wenzelm@63325
   694
  qed
wenzelm@63325
   695
  show "c * a = c * b \<longleftrightarrow> c = 0 \<or> a = b" for a b c
wenzelm@63325
   696
    using * [of a c b] by (simp add: ac_simps)
haftmann@60516
   697
qed
haftmann@60516
   698
wenzelm@63325
   699
lemma div_self [simp]: "a \<noteq> 0 \<Longrightarrow> a div a = 1"
haftmann@64240
   700
  using nonzero_mult_div_cancel_left [of a 1] by simp
haftmann@60516
   701
haftmann@64240
   702
lemma div_0 [simp]: "0 div a = 0"
haftmann@60570
   703
proof (cases "a = 0")
wenzelm@63325
   704
  case True
wenzelm@63325
   705
  then show ?thesis by simp
haftmann@60570
   706
next
wenzelm@63325
   707
  case False
wenzelm@63325
   708
  then have "a * 0 div a = 0"
haftmann@64240
   709
    by (rule nonzero_mult_div_cancel_left)
haftmann@60570
   710
  then show ?thesis by simp
hoelzl@62376
   711
qed
haftmann@60570
   712
haftmann@64240
   713
lemma div_by_1 [simp]: "a div 1 = a"
haftmann@64240
   714
  using nonzero_mult_div_cancel_left [of 1 a] by simp
haftmann@60690
   715
haftmann@64591
   716
lemma dvd_div_eq_0_iff:
haftmann@64591
   717
  assumes "b dvd a"
haftmann@64591
   718
  shows "a div b = 0 \<longleftrightarrow> a = 0"
haftmann@64591
   719
  using assms by (elim dvdE, cases "b = 0") simp_all  
haftmann@64591
   720
haftmann@64591
   721
lemma dvd_div_eq_cancel:
haftmann@64591
   722
  "a div c = b div c \<Longrightarrow> c dvd a \<Longrightarrow> c dvd b \<Longrightarrow> a = b"
haftmann@64591
   723
  by (elim dvdE, cases "c = 0") simp_all
haftmann@64591
   724
haftmann@64591
   725
lemma dvd_div_eq_iff:
haftmann@64591
   726
  "c dvd a \<Longrightarrow> c dvd b \<Longrightarrow> a div c = b div c \<longleftrightarrow> a = b"
haftmann@64591
   727
  by (elim dvdE, cases "c = 0") simp_all
haftmann@64591
   728
haftmann@60867
   729
end
haftmann@60867
   730
haftmann@60867
   731
class idom_divide = idom + semidom_divide
haftmann@64591
   732
begin
haftmann@64591
   733
haftmann@64592
   734
lemma dvd_neg_div:
haftmann@64591
   735
  assumes "b dvd a"
haftmann@64591
   736
  shows "- a div b = - (a div b)"
haftmann@64591
   737
proof (cases "b = 0")
haftmann@64591
   738
  case True
haftmann@64591
   739
  then show ?thesis by simp
haftmann@64591
   740
next
haftmann@64591
   741
  case False
haftmann@64591
   742
  from assms obtain c where "a = b * c" ..
haftmann@64592
   743
  then have "- a div b = (b * - c) div b"
haftmann@64592
   744
    by simp
haftmann@64592
   745
  from False also have "\<dots> = - c"
haftmann@64592
   746
    by (rule nonzero_mult_div_cancel_left)  
haftmann@64592
   747
  with False \<open>a = b * c\<close> show ?thesis
haftmann@64591
   748
    by simp
haftmann@64592
   749
qed
haftmann@64592
   750
haftmann@64592
   751
lemma dvd_div_neg:
haftmann@64592
   752
  assumes "b dvd a"
haftmann@64592
   753
  shows "a div - b = - (a div b)"
haftmann@64592
   754
proof (cases "b = 0")
haftmann@64592
   755
  case True
haftmann@64592
   756
  then show ?thesis by simp
haftmann@64592
   757
next
haftmann@64592
   758
  case False
haftmann@64592
   759
  then have "- b \<noteq> 0"
haftmann@64592
   760
    by simp
haftmann@64592
   761
  from assms obtain c where "a = b * c" ..
haftmann@64592
   762
  then have "a div - b = (- b * - c) div - b"
haftmann@64592
   763
    by simp
haftmann@64592
   764
  from \<open>- b \<noteq> 0\<close> also have "\<dots> = - c"
haftmann@64592
   765
    by (rule nonzero_mult_div_cancel_left)  
haftmann@64592
   766
  with False \<open>a = b * c\<close> show ?thesis
haftmann@64591
   767
    by simp
haftmann@64591
   768
qed
haftmann@64591
   769
haftmann@64591
   770
end
haftmann@60867
   771
haftmann@60867
   772
class algebraic_semidom = semidom_divide
haftmann@60867
   773
begin
haftmann@60867
   774
haftmann@60867
   775
text \<open>
haftmann@60867
   776
  Class @{class algebraic_semidom} enriches a integral domain
haftmann@60867
   777
  by notions from algebra, like units in a ring.
haftmann@60867
   778
  It is a separate class to avoid spoiling fields with notions
haftmann@60867
   779
  which are degenerated there.
haftmann@60867
   780
\<close>
haftmann@60867
   781
haftmann@60690
   782
lemma dvd_times_left_cancel_iff [simp]:
haftmann@60690
   783
  assumes "a \<noteq> 0"
wenzelm@63588
   784
  shows "a * b dvd a * c \<longleftrightarrow> b dvd c"
wenzelm@63588
   785
    (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@60690
   786
proof
wenzelm@63588
   787
  assume ?lhs
wenzelm@63325
   788
  then obtain d where "a * c = a * b * d" ..
haftmann@60690
   789
  with assms have "c = b * d" by (simp add: ac_simps)
wenzelm@63588
   790
  then show ?rhs ..
haftmann@60690
   791
next
wenzelm@63588
   792
  assume ?rhs
wenzelm@63325
   793
  then obtain d where "c = b * d" ..
haftmann@60690
   794
  then have "a * c = a * b * d" by (simp add: ac_simps)
wenzelm@63588
   795
  then show ?lhs ..
haftmann@60690
   796
qed
hoelzl@62376
   797
haftmann@60690
   798
lemma dvd_times_right_cancel_iff [simp]:
haftmann@60690
   799
  assumes "a \<noteq> 0"
wenzelm@63588
   800
  shows "b * a dvd c * a \<longleftrightarrow> b dvd c"
wenzelm@63325
   801
  using dvd_times_left_cancel_iff [of a b c] assms by (simp add: ac_simps)
hoelzl@62376
   802
haftmann@60690
   803
lemma div_dvd_iff_mult:
haftmann@60690
   804
  assumes "b \<noteq> 0" and "b dvd a"
haftmann@60690
   805
  shows "a div b dvd c \<longleftrightarrow> a dvd c * b"
haftmann@60690
   806
proof -
haftmann@60690
   807
  from \<open>b dvd a\<close> obtain d where "a = b * d" ..
haftmann@60690
   808
  with \<open>b \<noteq> 0\<close> show ?thesis by (simp add: ac_simps)
haftmann@60690
   809
qed
haftmann@60690
   810
haftmann@60690
   811
lemma dvd_div_iff_mult:
haftmann@60690
   812
  assumes "c \<noteq> 0" and "c dvd b"
haftmann@60690
   813
  shows "a dvd b div c \<longleftrightarrow> a * c dvd b"
haftmann@60690
   814
proof -
haftmann@60690
   815
  from \<open>c dvd b\<close> obtain d where "b = c * d" ..
haftmann@60690
   816
  with \<open>c \<noteq> 0\<close> show ?thesis by (simp add: mult.commute [of a])
haftmann@60690
   817
qed
haftmann@60690
   818
haftmann@60867
   819
lemma div_dvd_div [simp]:
haftmann@60867
   820
  assumes "a dvd b" and "a dvd c"
haftmann@60867
   821
  shows "b div a dvd c div a \<longleftrightarrow> b dvd c"
haftmann@60867
   822
proof (cases "a = 0")
wenzelm@63325
   823
  case True
wenzelm@63325
   824
  with assms show ?thesis by simp
haftmann@60867
   825
next
haftmann@60867
   826
  case False
haftmann@60867
   827
  moreover from assms obtain k l where "b = a * k" and "c = a * l"
haftmann@60867
   828
    by (auto elim!: dvdE)
haftmann@60867
   829
  ultimately show ?thesis by simp
haftmann@60867
   830
qed
haftmann@60353
   831
haftmann@60867
   832
lemma div_add [simp]:
haftmann@60867
   833
  assumes "c dvd a" and "c dvd b"
haftmann@60867
   834
  shows "(a + b) div c = a div c + b div c"
haftmann@60867
   835
proof (cases "c = 0")
wenzelm@63325
   836
  case True
wenzelm@63325
   837
  then show ?thesis by simp
haftmann@60867
   838
next
haftmann@60867
   839
  case False
haftmann@60867
   840
  moreover from assms obtain k l where "a = c * k" and "b = c * l"
haftmann@60867
   841
    by (auto elim!: dvdE)
haftmann@60867
   842
  moreover have "c * k + c * l = c * (k + l)"
haftmann@60867
   843
    by (simp add: algebra_simps)
haftmann@60867
   844
  ultimately show ?thesis
haftmann@60867
   845
    by simp
haftmann@60867
   846
qed
haftmann@60517
   847
haftmann@60867
   848
lemma div_mult_div_if_dvd:
haftmann@60867
   849
  assumes "b dvd a" and "d dvd c"
haftmann@60867
   850
  shows "(a div b) * (c div d) = (a * c) div (b * d)"
haftmann@60867
   851
proof (cases "b = 0 \<or> c = 0")
wenzelm@63325
   852
  case True
wenzelm@63325
   853
  with assms show ?thesis by auto
haftmann@60867
   854
next
haftmann@60867
   855
  case False
haftmann@60867
   856
  moreover from assms obtain k l where "a = b * k" and "c = d * l"
haftmann@60867
   857
    by (auto elim!: dvdE)
haftmann@60867
   858
  moreover have "b * k * (d * l) div (b * d) = (b * d) * (k * l) div (b * d)"
haftmann@60867
   859
    by (simp add: ac_simps)
haftmann@60867
   860
  ultimately show ?thesis by simp
haftmann@60867
   861
qed
haftmann@60867
   862
haftmann@60867
   863
lemma dvd_div_eq_mult:
haftmann@60867
   864
  assumes "a \<noteq> 0" and "a dvd b"
haftmann@60867
   865
  shows "b div a = c \<longleftrightarrow> b = c * a"
wenzelm@63588
   866
    (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@60867
   867
proof
wenzelm@63588
   868
  assume ?rhs
wenzelm@63588
   869
  then show ?lhs by (simp add: assms)
haftmann@60867
   870
next
wenzelm@63588
   871
  assume ?lhs
haftmann@60867
   872
  then have "b div a * a = c * a" by simp
wenzelm@63325
   873
  moreover from assms have "b div a * a = b"
haftmann@60867
   874
    by (auto elim!: dvdE simp add: ac_simps)
wenzelm@63588
   875
  ultimately show ?rhs by simp
haftmann@60867
   876
qed
haftmann@60688
   877
wenzelm@63325
   878
lemma dvd_div_mult_self [simp]: "a dvd b \<Longrightarrow> b div a * a = b"
haftmann@60517
   879
  by (cases "a = 0") (auto elim: dvdE simp add: ac_simps)
haftmann@60517
   880
wenzelm@63325
   881
lemma dvd_mult_div_cancel [simp]: "a dvd b \<Longrightarrow> a * (b div a) = b"
haftmann@60517
   882
  using dvd_div_mult_self [of a b] by (simp add: ac_simps)
lp15@60562
   883
haftmann@60517
   884
lemma div_mult_swap:
haftmann@60517
   885
  assumes "c dvd b"
haftmann@60517
   886
  shows "a * (b div c) = (a * b) div c"
haftmann@60517
   887
proof (cases "c = 0")
wenzelm@63325
   888
  case True
wenzelm@63325
   889
  then show ?thesis by simp
haftmann@60517
   890
next
wenzelm@63325
   891
  case False
wenzelm@63325
   892
  from assms obtain d where "b = c * d" ..
haftmann@60517
   893
  moreover from False have "a * divide (d * c) c = ((a * d) * c) div c"
haftmann@60517
   894
    by simp
haftmann@60517
   895
  ultimately show ?thesis by (simp add: ac_simps)
haftmann@60517
   896
qed
haftmann@60517
   897
wenzelm@63325
   898
lemma dvd_div_mult: "c dvd b \<Longrightarrow> b div c * a = (b * a) div c"
wenzelm@63325
   899
  using div_mult_swap [of c b a] by (simp add: ac_simps)
haftmann@60517
   900
haftmann@60570
   901
lemma dvd_div_mult2_eq:
haftmann@60570
   902
  assumes "b * c dvd a"
haftmann@60570
   903
  shows "a div (b * c) = a div b div c"
wenzelm@63325
   904
proof -
wenzelm@63325
   905
  from assms obtain k where "a = b * c * k" ..
haftmann@60570
   906
  then show ?thesis
haftmann@60570
   907
    by (cases "b = 0 \<or> c = 0") (auto, simp add: ac_simps)
haftmann@60570
   908
qed
haftmann@60570
   909
haftmann@60867
   910
lemma dvd_div_div_eq_mult:
haftmann@60867
   911
  assumes "a \<noteq> 0" "c \<noteq> 0" and "a dvd b" "c dvd d"
wenzelm@63588
   912
  shows "b div a = d div c \<longleftrightarrow> b * c = a * d"
wenzelm@63588
   913
    (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@60867
   914
proof -
haftmann@60867
   915
  from assms have "a * c \<noteq> 0" by simp
wenzelm@63588
   916
  then have "?lhs \<longleftrightarrow> b div a * (a * c) = d div c * (a * c)"
haftmann@60867
   917
    by simp
haftmann@60867
   918
  also have "\<dots> \<longleftrightarrow> (a * (b div a)) * c = (c * (d div c)) * a"
haftmann@60867
   919
    by (simp add: ac_simps)
haftmann@60867
   920
  also have "\<dots> \<longleftrightarrow> (a * b div a) * c = (c * d div c) * a"
haftmann@60867
   921
    using assms by (simp add: div_mult_swap)
wenzelm@63588
   922
  also have "\<dots> \<longleftrightarrow> ?rhs"
haftmann@60867
   923
    using assms by (simp add: ac_simps)
haftmann@60867
   924
  finally show ?thesis .
haftmann@60867
   925
qed
haftmann@60867
   926
eberlm@63359
   927
lemma dvd_mult_imp_div:
eberlm@63359
   928
  assumes "a * c dvd b"
eberlm@63359
   929
  shows "a dvd b div c"
eberlm@63359
   930
proof (cases "c = 0")
eberlm@63359
   931
  case True then show ?thesis by simp
eberlm@63359
   932
next
eberlm@63359
   933
  case False
eberlm@63359
   934
  from \<open>a * c dvd b\<close> obtain d where "b = a * c * d" ..
wenzelm@63588
   935
  with False show ?thesis
wenzelm@63588
   936
    by (simp add: mult.commute [of a] mult.assoc)
eberlm@63359
   937
qed
eberlm@63359
   938
haftmann@64592
   939
lemma div_div_eq_right:
haftmann@64592
   940
  assumes "c dvd b" "b dvd a"
haftmann@64592
   941
  shows   "a div (b div c) = a div b * c"
haftmann@64592
   942
proof (cases "c = 0 \<or> b = 0")
haftmann@64592
   943
  case True
haftmann@64592
   944
  then show ?thesis
haftmann@64592
   945
    by auto
haftmann@64592
   946
next
haftmann@64592
   947
  case False
haftmann@64592
   948
  from assms obtain r s where "b = c * r" and "a = c * r * s"
haftmann@64592
   949
    by (blast elim: dvdE)
haftmann@64592
   950
  moreover with False have "r \<noteq> 0"
haftmann@64592
   951
    by auto
haftmann@64592
   952
  ultimately show ?thesis using False
haftmann@64592
   953
    by simp (simp add: mult.commute [of _ r] mult.assoc mult.commute [of c])
haftmann@64592
   954
qed
haftmann@64592
   955
haftmann@64592
   956
lemma div_div_div_same:
haftmann@64592
   957
  assumes "d dvd b" "b dvd a"
haftmann@64592
   958
  shows   "(a div d) div (b div d) = a div b"
haftmann@64592
   959
proof (cases "b = 0 \<or> d = 0")
haftmann@64592
   960
  case True
haftmann@64592
   961
  with assms show ?thesis
haftmann@64592
   962
    by auto
haftmann@64592
   963
next
haftmann@64592
   964
  case False
haftmann@64592
   965
  from assms obtain r s
haftmann@64592
   966
    where "a = d * r * s" and "b = d * r"
haftmann@64592
   967
    by (blast elim: dvdE)
haftmann@64592
   968
  with False show ?thesis
haftmann@64592
   969
    by simp (simp add: ac_simps)
haftmann@64592
   970
qed
haftmann@64592
   971
lp15@60562
   972
haftmann@60517
   973
text \<open>Units: invertible elements in a ring\<close>
haftmann@60517
   974
haftmann@60517
   975
abbreviation is_unit :: "'a \<Rightarrow> bool"
wenzelm@63325
   976
  where "is_unit a \<equiv> a dvd 1"
haftmann@60517
   977
wenzelm@63325
   978
lemma not_is_unit_0 [simp]: "\<not> is_unit 0"
haftmann@60517
   979
  by simp
haftmann@60517
   980
wenzelm@63325
   981
lemma unit_imp_dvd [dest]: "is_unit b \<Longrightarrow> b dvd a"
haftmann@60517
   982
  by (rule dvd_trans [of _ 1]) simp_all
haftmann@60517
   983
haftmann@60517
   984
lemma unit_dvdE:
haftmann@60517
   985
  assumes "is_unit a"
haftmann@60517
   986
  obtains c where "a \<noteq> 0" and "b = a * c"
haftmann@60517
   987
proof -
haftmann@60517
   988
  from assms have "a dvd b" by auto
haftmann@60517
   989
  then obtain c where "b = a * c" ..
haftmann@60517
   990
  moreover from assms have "a \<noteq> 0" by auto
haftmann@60517
   991
  ultimately show thesis using that by blast
haftmann@60517
   992
qed
haftmann@60517
   993
wenzelm@63325
   994
lemma dvd_unit_imp_unit: "a dvd b \<Longrightarrow> is_unit b \<Longrightarrow> is_unit a"
haftmann@60517
   995
  by (rule dvd_trans)
haftmann@60517
   996
haftmann@60517
   997
lemma unit_div_1_unit [simp, intro]:
haftmann@60517
   998
  assumes "is_unit a"
haftmann@60517
   999
  shows "is_unit (1 div a)"
haftmann@60517
  1000
proof -
haftmann@60517
  1001
  from assms have "1 = 1 div a * a" by simp
haftmann@60517
  1002
  then show "is_unit (1 div a)" by (rule dvdI)
haftmann@60517
  1003
qed
haftmann@60517
  1004
haftmann@60517
  1005
lemma is_unitE [elim?]:
haftmann@60517
  1006
  assumes "is_unit a"
haftmann@60517
  1007
  obtains b where "a \<noteq> 0" and "b \<noteq> 0"
haftmann@60517
  1008
    and "is_unit b" and "1 div a = b" and "1 div b = a"
haftmann@60517
  1009
    and "a * b = 1" and "c div a = c * b"
haftmann@60517
  1010
proof (rule that)
wenzelm@63040
  1011
  define b where "b = 1 div a"
haftmann@60517
  1012
  then show "1 div a = b" by simp
wenzelm@63325
  1013
  from assms b_def show "is_unit b" by simp
wenzelm@63325
  1014
  with assms show "a \<noteq> 0" and "b \<noteq> 0" by auto
wenzelm@63325
  1015
  from assms b_def show "a * b = 1" by simp
haftmann@60517
  1016
  then have "1 = a * b" ..
wenzelm@60758
  1017
  with b_def \<open>b \<noteq> 0\<close> show "1 div b = a" by simp
wenzelm@63325
  1018
  from assms have "a dvd c" ..
haftmann@60517
  1019
  then obtain d where "c = a * d" ..
wenzelm@60758
  1020
  with \<open>a \<noteq> 0\<close> \<open>a * b = 1\<close> show "c div a = c * b"
haftmann@60517
  1021
    by (simp add: mult.assoc mult.left_commute [of a])
haftmann@60517
  1022
qed
haftmann@60517
  1023
wenzelm@63325
  1024
lemma unit_prod [intro]: "is_unit a \<Longrightarrow> is_unit b \<Longrightarrow> is_unit (a * b)"
lp15@60562
  1025
  by (subst mult_1_left [of 1, symmetric]) (rule mult_dvd_mono)
lp15@60562
  1026
wenzelm@63325
  1027
lemma is_unit_mult_iff: "is_unit (a * b) \<longleftrightarrow> is_unit a \<and> is_unit b"
haftmann@62366
  1028
  by (auto dest: dvd_mult_left dvd_mult_right)
haftmann@62366
  1029
wenzelm@63325
  1030
lemma unit_div [intro]: "is_unit a \<Longrightarrow> is_unit b \<Longrightarrow> is_unit (a div b)"
haftmann@60517
  1031
  by (erule is_unitE [of b a]) (simp add: ac_simps unit_prod)
haftmann@60517
  1032
haftmann@60517
  1033
lemma mult_unit_dvd_iff:
haftmann@60517
  1034
  assumes "is_unit b"
haftmann@60517
  1035
  shows "a * b dvd c \<longleftrightarrow> a dvd c"
haftmann@60517
  1036
proof
haftmann@60517
  1037
  assume "a * b dvd c"
haftmann@60517
  1038
  with assms show "a dvd c"
haftmann@60517
  1039
    by (simp add: dvd_mult_left)
haftmann@60517
  1040
next
haftmann@60517
  1041
  assume "a dvd c"
haftmann@60517
  1042
  then obtain k where "c = a * k" ..
haftmann@60517
  1043
  with assms have "c = (a * b) * (1 div b * k)"
haftmann@60517
  1044
    by (simp add: mult_ac)
haftmann@60517
  1045
  then show "a * b dvd c" by (rule dvdI)
haftmann@60517
  1046
qed
haftmann@60517
  1047
haftmann@63924
  1048
lemma mult_unit_dvd_iff': "is_unit a \<Longrightarrow> (a * b) dvd c \<longleftrightarrow> b dvd c"
haftmann@63924
  1049
  using mult_unit_dvd_iff [of a b c] by (simp add: ac_simps)
haftmann@63924
  1050
haftmann@60517
  1051
lemma dvd_mult_unit_iff:
haftmann@60517
  1052
  assumes "is_unit b"
haftmann@60517
  1053
  shows "a dvd c * b \<longleftrightarrow> a dvd c"
haftmann@60517
  1054
proof
haftmann@60517
  1055
  assume "a dvd c * b"
haftmann@60517
  1056
  with assms have "c * b dvd c * (b * (1 div b))"
haftmann@60517
  1057
    by (subst mult_assoc [symmetric]) simp
wenzelm@63325
  1058
  also from assms have "b * (1 div b) = 1"
wenzelm@63325
  1059
    by (rule is_unitE) simp
haftmann@60517
  1060
  finally have "c * b dvd c" by simp
wenzelm@60758
  1061
  with \<open>a dvd c * b\<close> show "a dvd c" by (rule dvd_trans)
haftmann@60517
  1062
next
haftmann@60517
  1063
  assume "a dvd c"
haftmann@60517
  1064
  then show "a dvd c * b" by simp
haftmann@60517
  1065
qed
haftmann@60517
  1066
haftmann@63924
  1067
lemma dvd_mult_unit_iff': "is_unit b \<Longrightarrow> a dvd b * c \<longleftrightarrow> a dvd c"
haftmann@63924
  1068
  using dvd_mult_unit_iff [of b a c] by (simp add: ac_simps)
haftmann@63924
  1069
wenzelm@63325
  1070
lemma div_unit_dvd_iff: "is_unit b \<Longrightarrow> a div b dvd c \<longleftrightarrow> a dvd c"
haftmann@60517
  1071
  by (erule is_unitE [of _ a]) (auto simp add: mult_unit_dvd_iff)
haftmann@60517
  1072
wenzelm@63325
  1073
lemma dvd_div_unit_iff: "is_unit b \<Longrightarrow> a dvd c div b \<longleftrightarrow> a dvd c"
haftmann@60517
  1074
  by (erule is_unitE [of _ c]) (simp add: dvd_mult_unit_iff)
haftmann@60517
  1075
haftmann@63924
  1076
lemmas unit_dvd_iff = mult_unit_dvd_iff mult_unit_dvd_iff'
haftmann@63924
  1077
  dvd_mult_unit_iff dvd_mult_unit_iff' 
haftmann@63924
  1078
  div_unit_dvd_iff dvd_div_unit_iff (* FIXME consider named_theorems *)
haftmann@60517
  1079
wenzelm@63325
  1080
lemma unit_mult_div_div [simp]: "is_unit a \<Longrightarrow> b * (1 div a) = b div a"
haftmann@60517
  1081
  by (erule is_unitE [of _ b]) simp
haftmann@60517
  1082
wenzelm@63325
  1083
lemma unit_div_mult_self [simp]: "is_unit a \<Longrightarrow> b div a * a = b"
haftmann@60517
  1084
  by (rule dvd_div_mult_self) auto
haftmann@60517
  1085
wenzelm@63325
  1086
lemma unit_div_1_div_1 [simp]: "is_unit a \<Longrightarrow> 1 div (1 div a) = a"
haftmann@60517
  1087
  by (erule is_unitE) simp
haftmann@60517
  1088
wenzelm@63325
  1089
lemma unit_div_mult_swap: "is_unit c \<Longrightarrow> a * (b div c) = (a * b) div c"
haftmann@60517
  1090
  by (erule unit_dvdE [of _ b]) (simp add: mult.left_commute [of _ c])
haftmann@60517
  1091
wenzelm@63325
  1092
lemma unit_div_commute: "is_unit b \<Longrightarrow> (a div b) * c = (a * c) div b"
haftmann@60517
  1093
  using unit_div_mult_swap [of b c a] by (simp add: ac_simps)
haftmann@60517
  1094
wenzelm@63325
  1095
lemma unit_eq_div1: "is_unit b \<Longrightarrow> a div b = c \<longleftrightarrow> a = c * b"
haftmann@60517
  1096
  by (auto elim: is_unitE)
haftmann@60517
  1097
wenzelm@63325
  1098
lemma unit_eq_div2: "is_unit b \<Longrightarrow> a = c div b \<longleftrightarrow> a * b = c"
haftmann@60517
  1099
  using unit_eq_div1 [of b c a] by auto
haftmann@60517
  1100
wenzelm@63325
  1101
lemma unit_mult_left_cancel: "is_unit a \<Longrightarrow> a * b = a * c \<longleftrightarrow> b = c"
wenzelm@63325
  1102
  using mult_cancel_left [of a b c] by auto
haftmann@60517
  1103
wenzelm@63325
  1104
lemma unit_mult_right_cancel: "is_unit a \<Longrightarrow> b * a = c * a \<longleftrightarrow> b = c"
haftmann@60517
  1105
  using unit_mult_left_cancel [of a b c] by (auto simp add: ac_simps)
haftmann@60517
  1106
haftmann@60517
  1107
lemma unit_div_cancel:
haftmann@60517
  1108
  assumes "is_unit a"
haftmann@60517
  1109
  shows "b div a = c div a \<longleftrightarrow> b = c"
haftmann@60517
  1110
proof -
haftmann@60517
  1111
  from assms have "is_unit (1 div a)" by simp
haftmann@60517
  1112
  then have "b * (1 div a) = c * (1 div a) \<longleftrightarrow> b = c"
haftmann@60517
  1113
    by (rule unit_mult_right_cancel)
haftmann@60517
  1114
  with assms show ?thesis by simp
haftmann@60517
  1115
qed
lp15@60562
  1116
haftmann@60570
  1117
lemma is_unit_div_mult2_eq:
haftmann@60570
  1118
  assumes "is_unit b" and "is_unit c"
haftmann@60570
  1119
  shows "a div (b * c) = a div b div c"
haftmann@60570
  1120
proof -
wenzelm@63325
  1121
  from assms have "is_unit (b * c)"
wenzelm@63325
  1122
    by (simp add: unit_prod)
haftmann@60570
  1123
  then have "b * c dvd a"
haftmann@60570
  1124
    by (rule unit_imp_dvd)
haftmann@60570
  1125
  then show ?thesis
haftmann@60570
  1126
    by (rule dvd_div_mult2_eq)
haftmann@60570
  1127
qed
haftmann@60570
  1128
lp15@60562
  1129
lemmas unit_simps = mult_unit_dvd_iff div_unit_dvd_iff dvd_mult_unit_iff
haftmann@60517
  1130
  dvd_div_unit_iff unit_div_mult_swap unit_div_commute
lp15@60562
  1131
  unit_mult_left_cancel unit_mult_right_cancel unit_div_cancel
haftmann@60517
  1132
  unit_eq_div1 unit_eq_div2
haftmann@60517
  1133
haftmann@64240
  1134
lemma is_unit_div_mult_cancel_left:
haftmann@60685
  1135
  assumes "a \<noteq> 0" and "is_unit b"
haftmann@60685
  1136
  shows "a div (a * b) = 1 div b"
haftmann@60685
  1137
proof -
haftmann@60685
  1138
  from assms have "a div (a * b) = a div a div b"
haftmann@60685
  1139
    by (simp add: mult_unit_dvd_iff dvd_div_mult2_eq)
haftmann@60685
  1140
  with assms show ?thesis by simp
haftmann@60685
  1141
qed
haftmann@60685
  1142
haftmann@64240
  1143
lemma is_unit_div_mult_cancel_right:
haftmann@60685
  1144
  assumes "a \<noteq> 0" and "is_unit b"
haftmann@60685
  1145
  shows "a div (b * a) = 1 div b"
haftmann@64240
  1146
  using assms is_unit_div_mult_cancel_left [of a b] by (simp add: ac_simps)
haftmann@60685
  1147
haftmann@64591
  1148
lemma unit_div_eq_0_iff:
haftmann@64591
  1149
  assumes "is_unit b"
haftmann@64591
  1150
  shows "a div b = 0 \<longleftrightarrow> a = 0"
haftmann@64591
  1151
  by (rule dvd_div_eq_0_iff) (insert assms, auto)  
haftmann@64591
  1152
haftmann@64591
  1153
lemma div_mult_unit2:
haftmann@64591
  1154
  "is_unit c \<Longrightarrow> b dvd a \<Longrightarrow> a div (b * c) = a div b div c"
haftmann@64591
  1155
  by (rule dvd_div_mult2_eq) (simp_all add: mult_unit_dvd_iff)
haftmann@64591
  1156
haftmann@60685
  1157
end
haftmann@60685
  1158
haftmann@64848
  1159
class unit_factor =
haftmann@64848
  1160
  fixes unit_factor :: "'a \<Rightarrow> 'a"
haftmann@64848
  1161
haftmann@64848
  1162
class semidom_divide_unit_factor = semidom_divide + unit_factor +
haftmann@64848
  1163
  assumes unit_factor_0 [simp]: "unit_factor 0 = 0"
haftmann@64848
  1164
    and is_unit_unit_factor: "a dvd 1 \<Longrightarrow> unit_factor a = a"
haftmann@64848
  1165
    and unit_factor_is_unit: "a \<noteq> 0 \<Longrightarrow> unit_factor a dvd 1"
haftmann@64848
  1166
    and unit_factor_mult: "unit_factor (a * b) = unit_factor a * unit_factor b"
haftmann@64848
  1167
  -- \<open>This fine-grained hierarchy will later on allow lean normalization of polynomials\<close>
haftmann@64848
  1168
  
haftmann@64848
  1169
class normalization_semidom = algebraic_semidom + semidom_divide_unit_factor +
haftmann@60685
  1170
  fixes normalize :: "'a \<Rightarrow> 'a"
haftmann@60685
  1171
  assumes unit_factor_mult_normalize [simp]: "unit_factor a * normalize a = a"
wenzelm@63588
  1172
    and normalize_0 [simp]: "normalize 0 = 0"
haftmann@60685
  1173
begin
haftmann@60685
  1174
haftmann@60688
  1175
text \<open>
wenzelm@63588
  1176
  Class @{class normalization_semidom} cultivates the idea that each integral
wenzelm@63588
  1177
  domain can be split into equivalence classes whose representants are
wenzelm@63588
  1178
  associated, i.e. divide each other. @{const normalize} specifies a canonical
wenzelm@63588
  1179
  representant for each equivalence class. The rationale behind this is that
wenzelm@63588
  1180
  it is easier to reason about equality than equivalences, hence we prefer to
wenzelm@63588
  1181
  think about equality of normalized values rather than associated elements.
haftmann@60688
  1182
\<close>
haftmann@60688
  1183
haftmann@64848
  1184
declare unit_factor_is_unit [iff]
haftmann@64848
  1185
  
wenzelm@63325
  1186
lemma unit_factor_dvd [simp]: "a \<noteq> 0 \<Longrightarrow> unit_factor a dvd b"
haftmann@60685
  1187
  by (rule unit_imp_dvd) simp
haftmann@60685
  1188
wenzelm@63325
  1189
lemma unit_factor_self [simp]: "unit_factor a dvd a"
hoelzl@62376
  1190
  by (cases "a = 0") simp_all
hoelzl@62376
  1191
wenzelm@63325
  1192
lemma normalize_mult_unit_factor [simp]: "normalize a * unit_factor a = a"
haftmann@60685
  1193
  using unit_factor_mult_normalize [of a] by (simp add: ac_simps)
haftmann@60685
  1194
wenzelm@63325
  1195
lemma normalize_eq_0_iff [simp]: "normalize a = 0 \<longleftrightarrow> a = 0"
wenzelm@63588
  1196
  (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@60685
  1197
proof
wenzelm@63588
  1198
  assume ?lhs
haftmann@60685
  1199
  moreover have "unit_factor a * normalize a = a" by simp
wenzelm@63588
  1200
  ultimately show ?rhs by simp
haftmann@60685
  1201
next
wenzelm@63588
  1202
  assume ?rhs
wenzelm@63588
  1203
  then show ?lhs by simp
haftmann@60685
  1204
qed
haftmann@60685
  1205
wenzelm@63325
  1206
lemma unit_factor_eq_0_iff [simp]: "unit_factor a = 0 \<longleftrightarrow> a = 0"
wenzelm@63588
  1207
  (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@60685
  1208
proof
wenzelm@63588
  1209
  assume ?lhs
haftmann@60685
  1210
  moreover have "unit_factor a * normalize a = a" by simp
wenzelm@63588
  1211
  ultimately show ?rhs by simp
haftmann@60685
  1212
next
wenzelm@63588
  1213
  assume ?rhs
wenzelm@63588
  1214
  then show ?lhs by simp
haftmann@60685
  1215
qed
haftmann@60685
  1216
haftmann@64848
  1217
lemma div_unit_factor [simp]: "a div unit_factor a = normalize a"
haftmann@64848
  1218
proof (cases "a = 0")
haftmann@64848
  1219
  case True
haftmann@64848
  1220
  then show ?thesis by simp
haftmann@64848
  1221
next
haftmann@64848
  1222
  case False
haftmann@64848
  1223
  then have "unit_factor a \<noteq> 0"
haftmann@64848
  1224
    by simp
haftmann@64848
  1225
  with nonzero_mult_div_cancel_left
haftmann@64848
  1226
  have "unit_factor a * normalize a div unit_factor a = normalize a"
haftmann@64848
  1227
    by blast
haftmann@64848
  1228
  then show ?thesis by simp
haftmann@64848
  1229
qed
haftmann@64848
  1230
haftmann@64848
  1231
lemma normalize_div [simp]: "normalize a div a = 1 div unit_factor a"
haftmann@64848
  1232
proof (cases "a = 0")
haftmann@64848
  1233
  case True
haftmann@64848
  1234
  then show ?thesis by simp
haftmann@64848
  1235
next
haftmann@64848
  1236
  case False
haftmann@64848
  1237
  have "normalize a div a = normalize a div (unit_factor a * normalize a)"
haftmann@64848
  1238
    by simp
haftmann@64848
  1239
  also have "\<dots> = 1 div unit_factor a"
haftmann@64848
  1240
    using False by (subst is_unit_div_mult_cancel_right) simp_all
haftmann@64848
  1241
  finally show ?thesis .
haftmann@64848
  1242
qed
haftmann@64848
  1243
haftmann@64848
  1244
lemma is_unit_normalize:
wenzelm@63325
  1245
  assumes "is_unit a"
haftmann@64848
  1246
  shows "normalize a = 1"
hoelzl@62376
  1247
proof -
haftmann@64848
  1248
  from assms have "unit_factor a = a"
haftmann@64848
  1249
    by (rule is_unit_unit_factor)
haftmann@64848
  1250
  moreover from assms have "a \<noteq> 0"
haftmann@64848
  1251
    by auto
haftmann@64848
  1252
  moreover have "normalize a = a div unit_factor a"
haftmann@64848
  1253
    by simp
haftmann@64848
  1254
  ultimately show ?thesis
haftmann@64848
  1255
    by simp
haftmann@60685
  1256
qed
haftmann@60685
  1257
wenzelm@63325
  1258
lemma unit_factor_1 [simp]: "unit_factor 1 = 1"
haftmann@60685
  1259
  by (rule is_unit_unit_factor) simp
haftmann@60685
  1260
wenzelm@63325
  1261
lemma normalize_1 [simp]: "normalize 1 = 1"
haftmann@60685
  1262
  by (rule is_unit_normalize) simp
haftmann@60685
  1263
wenzelm@63325
  1264
lemma normalize_1_iff: "normalize a = 1 \<longleftrightarrow> is_unit a"
wenzelm@63588
  1265
  (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@60685
  1266
proof
wenzelm@63588
  1267
  assume ?rhs
wenzelm@63588
  1268
  then show ?lhs by (rule is_unit_normalize)
haftmann@60685
  1269
next
wenzelm@63588
  1270
  assume ?lhs
wenzelm@63588
  1271
  then have "unit_factor a * normalize a = unit_factor a * 1"
haftmann@60685
  1272
    by simp
haftmann@60685
  1273
  then have "unit_factor a = a"
haftmann@60685
  1274
    by simp
wenzelm@63588
  1275
  moreover
wenzelm@63588
  1276
  from \<open>?lhs\<close> have "a \<noteq> 0" by auto
wenzelm@63588
  1277
  then have "is_unit (unit_factor a)" by simp
wenzelm@63588
  1278
  ultimately show ?rhs by simp
haftmann@60685
  1279
qed
hoelzl@62376
  1280
wenzelm@63325
  1281
lemma div_normalize [simp]: "a div normalize a = unit_factor a"
haftmann@60685
  1282
proof (cases "a = 0")
wenzelm@63325
  1283
  case True
wenzelm@63325
  1284
  then show ?thesis by simp
haftmann@60685
  1285
next
wenzelm@63325
  1286
  case False
wenzelm@63325
  1287
  then have "normalize a \<noteq> 0" by simp
haftmann@64240
  1288
  with nonzero_mult_div_cancel_right
haftmann@60685
  1289
  have "unit_factor a * normalize a div normalize a = unit_factor a" by blast
haftmann@60685
  1290
  then show ?thesis by simp
haftmann@60685
  1291
qed
haftmann@60685
  1292
wenzelm@63325
  1293
lemma mult_one_div_unit_factor [simp]: "a * (1 div unit_factor b) = a div unit_factor b"
haftmann@60685
  1294
  by (cases "b = 0") simp_all
haftmann@60685
  1295
haftmann@63947
  1296
lemma inv_unit_factor_eq_0_iff [simp]:
haftmann@63947
  1297
  "1 div unit_factor a = 0 \<longleftrightarrow> a = 0"
haftmann@63947
  1298
  (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@63947
  1299
proof
haftmann@63947
  1300
  assume ?lhs
haftmann@63947
  1301
  then have "a * (1 div unit_factor a) = a * 0"
haftmann@63947
  1302
    by simp
haftmann@63947
  1303
  then show ?rhs
haftmann@63947
  1304
    by simp
haftmann@63947
  1305
next
haftmann@63947
  1306
  assume ?rhs
haftmann@63947
  1307
  then show ?lhs by simp
haftmann@63947
  1308
qed
haftmann@63947
  1309
wenzelm@63325
  1310
lemma normalize_mult: "normalize (a * b) = normalize a * normalize b"
haftmann@60685
  1311
proof (cases "a = 0 \<or> b = 0")
wenzelm@63325
  1312
  case True
wenzelm@63325
  1313
  then show ?thesis by auto
haftmann@60685
  1314
next
haftmann@60685
  1315
  case False
wenzelm@63588
  1316
  have "unit_factor (a * b) * normalize (a * b) = a * b"
wenzelm@63588
  1317
    by (rule unit_factor_mult_normalize)
wenzelm@63325
  1318
  then have "normalize (a * b) = a * b div unit_factor (a * b)"
wenzelm@63325
  1319
    by simp
wenzelm@63325
  1320
  also have "\<dots> = a * b div unit_factor (b * a)"
wenzelm@63325
  1321
    by (simp add: ac_simps)
haftmann@60685
  1322
  also have "\<dots> = a * b div unit_factor b div unit_factor a"
haftmann@60685
  1323
    using False by (simp add: unit_factor_mult is_unit_div_mult2_eq [symmetric])
haftmann@60685
  1324
  also have "\<dots> = a * (b div unit_factor b) div unit_factor a"
haftmann@60685
  1325
    using False by (subst unit_div_mult_swap) simp_all
haftmann@60685
  1326
  also have "\<dots> = normalize a * normalize b"
wenzelm@63325
  1327
    using False
wenzelm@63325
  1328
    by (simp add: mult.commute [of a] mult.commute [of "normalize a"] unit_div_mult_swap [symmetric])
haftmann@60685
  1329
  finally show ?thesis .
haftmann@60685
  1330
qed
hoelzl@62376
  1331
wenzelm@63325
  1332
lemma unit_factor_idem [simp]: "unit_factor (unit_factor a) = unit_factor a"
haftmann@60685
  1333
  by (cases "a = 0") (auto intro: is_unit_unit_factor)
haftmann@60685
  1334
wenzelm@63325
  1335
lemma normalize_unit_factor [simp]: "a \<noteq> 0 \<Longrightarrow> normalize (unit_factor a) = 1"
haftmann@60685
  1336
  by (rule is_unit_normalize) simp
hoelzl@62376
  1337
wenzelm@63325
  1338
lemma normalize_idem [simp]: "normalize (normalize a) = normalize a"
haftmann@60685
  1339
proof (cases "a = 0")
wenzelm@63325
  1340
  case True
wenzelm@63325
  1341
  then show ?thesis by simp
haftmann@60685
  1342
next
haftmann@60685
  1343
  case False
wenzelm@63325
  1344
  have "normalize a = normalize (unit_factor a * normalize a)"
wenzelm@63325
  1345
    by simp
haftmann@60685
  1346
  also have "\<dots> = normalize (unit_factor a) * normalize (normalize a)"
haftmann@60685
  1347
    by (simp only: normalize_mult)
wenzelm@63325
  1348
  finally show ?thesis
wenzelm@63325
  1349
    using False by simp_all
haftmann@60685
  1350
qed
haftmann@60685
  1351
haftmann@60685
  1352
lemma unit_factor_normalize [simp]:
haftmann@60685
  1353
  assumes "a \<noteq> 0"
haftmann@60685
  1354
  shows "unit_factor (normalize a) = 1"
haftmann@60685
  1355
proof -
wenzelm@63325
  1356
  from assms have *: "normalize a \<noteq> 0"
wenzelm@63325
  1357
    by simp
haftmann@60685
  1358
  have "unit_factor (normalize a) * normalize (normalize a) = normalize a"
haftmann@60685
  1359
    by (simp only: unit_factor_mult_normalize)
haftmann@60685
  1360
  then have "unit_factor (normalize a) * normalize a = normalize a"
haftmann@60685
  1361
    by simp
wenzelm@63325
  1362
  with * have "unit_factor (normalize a) * normalize a div normalize a = normalize a div normalize a"
haftmann@60685
  1363
    by simp
wenzelm@63325
  1364
  with * show ?thesis
wenzelm@63325
  1365
    by simp
haftmann@60685
  1366
qed
haftmann@60685
  1367
haftmann@60685
  1368
lemma dvd_unit_factor_div:
haftmann@60685
  1369
  assumes "b dvd a"
haftmann@60685
  1370
  shows "unit_factor (a div b) = unit_factor a div unit_factor b"
haftmann@60685
  1371
proof -
haftmann@60685
  1372
  from assms have "a = a div b * b"
haftmann@60685
  1373
    by simp
haftmann@60685
  1374
  then have "unit_factor a = unit_factor (a div b * b)"
haftmann@60685
  1375
    by simp
haftmann@60685
  1376
  then show ?thesis
haftmann@60685
  1377
    by (cases "b = 0") (simp_all add: unit_factor_mult)
haftmann@60685
  1378
qed
haftmann@60685
  1379
haftmann@60685
  1380
lemma dvd_normalize_div:
haftmann@60685
  1381
  assumes "b dvd a"
haftmann@60685
  1382
  shows "normalize (a div b) = normalize a div normalize b"
haftmann@60685
  1383
proof -
haftmann@60685
  1384
  from assms have "a = a div b * b"
haftmann@60685
  1385
    by simp
haftmann@60685
  1386
  then have "normalize a = normalize (a div b * b)"
haftmann@60685
  1387
    by simp
haftmann@60685
  1388
  then show ?thesis
haftmann@60685
  1389
    by (cases "b = 0") (simp_all add: normalize_mult)
haftmann@60685
  1390
qed
haftmann@60685
  1391
wenzelm@63325
  1392
lemma normalize_dvd_iff [simp]: "normalize a dvd b \<longleftrightarrow> a dvd b"
haftmann@60685
  1393
proof -
haftmann@60685
  1394
  have "normalize a dvd b \<longleftrightarrow> unit_factor a * normalize a dvd b"
haftmann@60685
  1395
    using mult_unit_dvd_iff [of "unit_factor a" "normalize a" b]
haftmann@60685
  1396
      by (cases "a = 0") simp_all
haftmann@60685
  1397
  then show ?thesis by simp
haftmann@60685
  1398
qed
haftmann@60685
  1399
wenzelm@63325
  1400
lemma dvd_normalize_iff [simp]: "a dvd normalize b \<longleftrightarrow> a dvd b"
haftmann@60685
  1401
proof -
haftmann@60685
  1402
  have "a dvd normalize  b \<longleftrightarrow> a dvd normalize b * unit_factor b"
haftmann@60685
  1403
    using dvd_mult_unit_iff [of "unit_factor b" a "normalize b"]
haftmann@60685
  1404
      by (cases "b = 0") simp_all
haftmann@60685
  1405
  then show ?thesis by simp
haftmann@60685
  1406
qed
haftmann@60685
  1407
haftmann@65811
  1408
lemma normalize_idem_imp_unit_factor_eq:
haftmann@65811
  1409
  assumes "normalize a = a"
haftmann@65811
  1410
  shows "unit_factor a = of_bool (a \<noteq> 0)"
haftmann@65811
  1411
proof (cases "a = 0")
haftmann@65811
  1412
  case True
haftmann@65811
  1413
  then show ?thesis
haftmann@65811
  1414
    by simp
haftmann@65811
  1415
next
haftmann@65811
  1416
  case False
haftmann@65811
  1417
  then show ?thesis
haftmann@65811
  1418
    using assms unit_factor_normalize [of a] by simp
haftmann@65811
  1419
qed
haftmann@65811
  1420
haftmann@65811
  1421
lemma normalize_idem_imp_is_unit_iff:
haftmann@65811
  1422
  assumes "normalize a = a"
haftmann@65811
  1423
  shows "is_unit a \<longleftrightarrow> a = 1"
haftmann@65811
  1424
  using assms by (cases "a = 0") (auto dest: is_unit_normalize)
haftmann@65811
  1425
haftmann@60688
  1426
text \<open>
wenzelm@63588
  1427
  We avoid an explicit definition of associated elements but prefer explicit
wenzelm@63588
  1428
  normalisation instead. In theory we could define an abbreviation like @{prop
wenzelm@63588
  1429
  "associated a b \<longleftrightarrow> normalize a = normalize b"} but this is counterproductive
wenzelm@63588
  1430
  without suggestive infix syntax, which we do not want to sacrifice for this
wenzelm@63588
  1431
  purpose here.
haftmann@60688
  1432
\<close>
haftmann@60685
  1433
haftmann@60688
  1434
lemma associatedI:
haftmann@60688
  1435
  assumes "a dvd b" and "b dvd a"
haftmann@60688
  1436
  shows "normalize a = normalize b"
haftmann@60685
  1437
proof (cases "a = 0 \<or> b = 0")
wenzelm@63325
  1438
  case True
wenzelm@63325
  1439
  with assms show ?thesis by auto
haftmann@60685
  1440
next
haftmann@60685
  1441
  case False
haftmann@60688
  1442
  from \<open>a dvd b\<close> obtain c where b: "b = a * c" ..
haftmann@60688
  1443
  moreover from \<open>b dvd a\<close> obtain d where a: "a = b * d" ..
wenzelm@63325
  1444
  ultimately have "b * 1 = b * (c * d)"
wenzelm@63325
  1445
    by (simp add: ac_simps)
haftmann@60688
  1446
  with False have "1 = c * d"
haftmann@60688
  1447
    unfolding mult_cancel_left by simp
wenzelm@63325
  1448
  then have "is_unit c" and "is_unit d"
wenzelm@63325
  1449
    by auto
wenzelm@63325
  1450
  with a b show ?thesis
wenzelm@63325
  1451
    by (simp add: normalize_mult is_unit_normalize)
haftmann@60688
  1452
qed
haftmann@60688
  1453
wenzelm@63325
  1454
lemma associatedD1: "normalize a = normalize b \<Longrightarrow> a dvd b"
haftmann@60688
  1455
  using dvd_normalize_iff [of _ b, symmetric] normalize_dvd_iff [of a _, symmetric]
haftmann@60688
  1456
  by simp
haftmann@60688
  1457
wenzelm@63325
  1458
lemma associatedD2: "normalize a = normalize b \<Longrightarrow> b dvd a"
haftmann@60688
  1459
  using dvd_normalize_iff [of _ a, symmetric] normalize_dvd_iff [of b _, symmetric]
haftmann@60688
  1460
  by simp
haftmann@60688
  1461
wenzelm@63325
  1462
lemma associated_unit: "normalize a = normalize b \<Longrightarrow> is_unit a \<Longrightarrow> is_unit b"
haftmann@60688
  1463
  using dvd_unit_imp_unit by (auto dest!: associatedD1 associatedD2)
haftmann@60688
  1464
wenzelm@63325
  1465
lemma associated_iff_dvd: "normalize a = normalize b \<longleftrightarrow> a dvd b \<and> b dvd a"
wenzelm@63588
  1466
  (is "?lhs \<longleftrightarrow> ?rhs")
haftmann@60688
  1467
proof
wenzelm@63588
  1468
  assume ?rhs
wenzelm@63588
  1469
  then show ?lhs by (auto intro!: associatedI)
haftmann@60688
  1470
next
wenzelm@63588
  1471
  assume ?lhs
haftmann@60688
  1472
  then have "unit_factor a * normalize a = unit_factor a * normalize b"
haftmann@60688
  1473
    by simp
haftmann@60688
  1474
  then have *: "normalize b * unit_factor a = a"
haftmann@60688
  1475
    by (simp add: ac_simps)
wenzelm@63588
  1476
  show ?rhs
haftmann@60688
  1477
  proof (cases "a = 0 \<or> b = 0")
wenzelm@63325
  1478
    case True
wenzelm@63588
  1479
    with \<open>?lhs\<close> show ?thesis by auto
haftmann@60685
  1480
  next
hoelzl@62376
  1481
    case False
haftmann@60688
  1482
    then have "b dvd normalize b * unit_factor a" and "normalize b * unit_factor a dvd b"
haftmann@60688
  1483
      by (simp_all add: mult_unit_dvd_iff dvd_mult_unit_iff)
haftmann@60688
  1484
    with * show ?thesis by simp
haftmann@60685
  1485
  qed
haftmann@60685
  1486
qed
haftmann@60685
  1487
haftmann@60685
  1488
lemma associated_eqI:
haftmann@60688
  1489
  assumes "a dvd b" and "b dvd a"
haftmann@60688
  1490
  assumes "normalize a = a" and "normalize b = b"
haftmann@60685
  1491
  shows "a = b"
haftmann@60688
  1492
proof -
haftmann@60688
  1493
  from assms have "normalize a = normalize b"
haftmann@60688
  1494
    unfolding associated_iff_dvd by simp
wenzelm@63588
  1495
  with \<open>normalize a = a\<close> have "a = normalize b"
wenzelm@63588
  1496
    by simp
wenzelm@63588
  1497
  with \<open>normalize b = b\<close> show "a = b"
wenzelm@63588
  1498
    by simp
haftmann@60685
  1499
qed
haftmann@60685
  1500
haftmann@64591
  1501
lemma normalize_unit_factor_eqI:
haftmann@64591
  1502
  assumes "normalize a = normalize b"
haftmann@64591
  1503
    and "unit_factor a = unit_factor b"
haftmann@64591
  1504
  shows "a = b"
haftmann@64591
  1505
proof -
haftmann@64591
  1506
  from assms have "unit_factor a * normalize a = unit_factor b * normalize b"
haftmann@64591
  1507
    by simp
haftmann@64591
  1508
  then show ?thesis
haftmann@64591
  1509
    by simp
haftmann@64591
  1510
qed
haftmann@64591
  1511
haftmann@60685
  1512
end
haftmann@60685
  1513
haftmann@64164
  1514
haftmann@64164
  1515
text \<open>Syntactic division remainder operator\<close>
haftmann@64164
  1516
haftmann@64164
  1517
class modulo = dvd + divide +
haftmann@64164
  1518
  fixes modulo :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixl "mod" 70)
haftmann@64164
  1519
haftmann@64164
  1520
text \<open>Arbitrary quotient and remainder partitions\<close>
haftmann@64164
  1521
haftmann@64164
  1522
class semiring_modulo = comm_semiring_1_cancel + divide + modulo +
haftmann@64242
  1523
  assumes div_mult_mod_eq: "a div b * b + a mod b = a"
haftmann@64164
  1524
begin
haftmann@64164
  1525
haftmann@64164
  1526
lemma mod_div_decomp:
haftmann@64164
  1527
  fixes a b
haftmann@64164
  1528
  obtains q r where "q = a div b" and "r = a mod b"
haftmann@64164
  1529
    and "a = q * b + r"
haftmann@64164
  1530
proof -
haftmann@64242
  1531
  from div_mult_mod_eq have "a = a div b * b + a mod b" by simp
haftmann@64164
  1532
  moreover have "a div b = a div b" ..
haftmann@64164
  1533
  moreover have "a mod b = a mod b" ..
haftmann@64164
  1534
  note that ultimately show thesis by blast
haftmann@64164
  1535
qed
haftmann@64164
  1536
haftmann@64242
  1537
lemma mult_div_mod_eq: "b * (a div b) + a mod b = a"
haftmann@64242
  1538
  using div_mult_mod_eq [of a b] by (simp add: ac_simps)
haftmann@64164
  1539
haftmann@64242
  1540
lemma mod_div_mult_eq: "a mod b + a div b * b = a"
haftmann@64242
  1541
  using div_mult_mod_eq [of a b] by (simp add: ac_simps)
haftmann@64164
  1542
haftmann@64242
  1543
lemma mod_mult_div_eq: "a mod b + b * (a div b) = a"
haftmann@64242
  1544
  using div_mult_mod_eq [of a b] by (simp add: ac_simps)
haftmann@64164
  1545
haftmann@64242
  1546
lemma minus_div_mult_eq_mod: "a - a div b * b = a mod b"
haftmann@64242
  1547
  by (rule add_implies_diff [symmetric]) (fact mod_div_mult_eq)
haftmann@64164
  1548
haftmann@64242
  1549
lemma minus_mult_div_eq_mod: "a - b * (a div b) = a mod b"
haftmann@64242
  1550
  by (rule add_implies_diff [symmetric]) (fact mod_mult_div_eq)
haftmann@64164
  1551
haftmann@64242
  1552
lemma minus_mod_eq_div_mult: "a - a mod b = a div b * b"
haftmann@64242
  1553
  by (rule add_implies_diff [symmetric]) (fact div_mult_mod_eq)
haftmann@64164
  1554
haftmann@64242
  1555
lemma minus_mod_eq_mult_div: "a - a mod b = b * (a div b)"
haftmann@64242
  1556
  by (rule add_implies_diff [symmetric]) (fact mult_div_mod_eq)
haftmann@64164
  1557
haftmann@64164
  1558
end
haftmann@64242
  1559
haftmann@64164
  1560
haftmann@66807
  1561
text \<open>Quotient and remainder in integral domains\<close>
haftmann@66807
  1562
haftmann@66807
  1563
class semidom_modulo = algebraic_semidom + semiring_modulo
haftmann@66807
  1564
begin
haftmann@66807
  1565
haftmann@66807
  1566
lemma mod_0 [simp]: "0 mod a = 0"
haftmann@66807
  1567
  using div_mult_mod_eq [of 0 a] by simp
haftmann@66807
  1568
haftmann@66807
  1569
lemma mod_by_0 [simp]: "a mod 0 = a"
haftmann@66807
  1570
  using div_mult_mod_eq [of a 0] by simp
haftmann@66807
  1571
haftmann@66807
  1572
lemma mod_by_1 [simp]:
haftmann@66807
  1573
  "a mod 1 = 0"
haftmann@66807
  1574
proof -
haftmann@66807
  1575
  from div_mult_mod_eq [of a one] div_by_1 have "a + a mod 1 = a" by simp
haftmann@66807
  1576
  then have "a + a mod 1 = a + 0" by simp
haftmann@66807
  1577
  then show ?thesis by (rule add_left_imp_eq)
haftmann@66807
  1578
qed
haftmann@66807
  1579
haftmann@66807
  1580
lemma mod_self [simp]:
haftmann@66807
  1581
  "a mod a = 0"
haftmann@66807
  1582
  using div_mult_mod_eq [of a a] by simp
haftmann@66807
  1583
haftmann@66807
  1584
lemma dvd_imp_mod_0 [simp]:
haftmann@66807
  1585
  assumes "a dvd b"
haftmann@66807
  1586
  shows "b mod a = 0"
haftmann@66807
  1587
  using assms minus_div_mult_eq_mod [of b a] by simp
haftmann@66807
  1588
haftmann@66807
  1589
lemma mod_0_imp_dvd: 
haftmann@66807
  1590
  assumes "a mod b = 0"
haftmann@66807
  1591
  shows   "b dvd a"
haftmann@66807
  1592
proof -
haftmann@66807
  1593
  have "b dvd ((a div b) * b)" by simp
haftmann@66807
  1594
  also have "(a div b) * b = a"
haftmann@66807
  1595
    using div_mult_mod_eq [of a b] by (simp add: assms)
haftmann@66807
  1596
  finally show ?thesis .
haftmann@66807
  1597
qed
haftmann@66807
  1598
haftmann@66807
  1599
lemma mod_eq_0_iff_dvd:
haftmann@66807
  1600
  "a mod b = 0 \<longleftrightarrow> b dvd a"
haftmann@66807
  1601
  by (auto intro: mod_0_imp_dvd)
haftmann@66807
  1602
haftmann@66807
  1603
lemma dvd_eq_mod_eq_0 [nitpick_unfold, code]:
haftmann@66807
  1604
  "a dvd b \<longleftrightarrow> b mod a = 0"
haftmann@66807
  1605
  by (simp add: mod_eq_0_iff_dvd)
haftmann@66807
  1606
haftmann@66807
  1607
lemma dvd_mod_iff: 
haftmann@66807
  1608
  assumes "c dvd b"
haftmann@66807
  1609
  shows "c dvd a mod b \<longleftrightarrow> c dvd a"
haftmann@66807
  1610
proof -
haftmann@66807
  1611
  from assms have "(c dvd a mod b) \<longleftrightarrow> (c dvd ((a div b) * b + a mod b))" 
haftmann@66807
  1612
    by (simp add: dvd_add_right_iff)
haftmann@66807
  1613
  also have "(a div b) * b + a mod b = a"
haftmann@66807
  1614
    using div_mult_mod_eq [of a b] by simp
haftmann@66807
  1615
  finally show ?thesis .
haftmann@66807
  1616
qed
haftmann@66807
  1617
haftmann@66807
  1618
lemma dvd_mod_imp_dvd:
haftmann@66807
  1619
  assumes "c dvd a mod b" and "c dvd b"
haftmann@66807
  1620
  shows "c dvd a"
haftmann@66807
  1621
  using assms dvd_mod_iff [of c b a] by simp
haftmann@66807
  1622
haftmann@66808
  1623
lemma dvd_minus_mod [simp]:
haftmann@66808
  1624
  "b dvd a - a mod b"
haftmann@66808
  1625
  by (simp add: minus_mod_eq_div_mult)
haftmann@66808
  1626
haftmann@66807
  1627
end
haftmann@66807
  1628
haftmann@66807
  1629
class idom_modulo = idom + semidom_modulo
haftmann@66807
  1630
begin
haftmann@66807
  1631
haftmann@66807
  1632
subclass idom_divide ..
haftmann@66807
  1633
haftmann@66807
  1634
lemma div_diff [simp]:
haftmann@66807
  1635
  "c dvd a \<Longrightarrow> c dvd b \<Longrightarrow> (a - b) div c = a div c - b div c"
haftmann@66807
  1636
  using div_add [of _  _ "- b"] by (simp add: dvd_neg_div)
haftmann@66807
  1637
haftmann@66807
  1638
end
haftmann@66807
  1639
haftmann@66807
  1640
hoelzl@62376
  1641
class ordered_semiring = semiring + ordered_comm_monoid_add +
haftmann@38642
  1642
  assumes mult_left_mono: "a \<le> b \<Longrightarrow> 0 \<le> c \<Longrightarrow> c * a \<le> c * b"
haftmann@38642
  1643
  assumes mult_right_mono: "a \<le> b \<Longrightarrow> 0 \<le> c \<Longrightarrow> a * c \<le> b * c"
haftmann@25230
  1644
begin
haftmann@25230
  1645
wenzelm@63325
  1646
lemma mult_mono: "a \<le> b \<Longrightarrow> c \<le> d \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> c \<Longrightarrow> a * c \<le> b * d"
wenzelm@63325
  1647
  apply (erule (1) mult_right_mono [THEN order_trans])
wenzelm@63325
  1648
  apply (erule (1) mult_left_mono)
wenzelm@63325
  1649
  done
haftmann@25230
  1650
wenzelm@63325
  1651
lemma mult_mono': "a \<le> b \<Longrightarrow> c \<le> d \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 \<le> c \<Longrightarrow> a * c \<le> b * d"
wenzelm@63588
  1652
  by (rule mult_mono) (fast intro: order_trans)+
haftmann@25230
  1653
haftmann@25230
  1654
end
krauss@21199
  1655
hoelzl@62377
  1656
class ordered_semiring_0 = semiring_0 + ordered_semiring
haftmann@25267
  1657
begin
paulson@14268
  1658
wenzelm@63325
  1659
lemma mult_nonneg_nonneg [simp]: "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> a * b"
wenzelm@63325
  1660
  using mult_left_mono [of 0 b a] by simp
haftmann@25230
  1661
haftmann@25230
  1662
lemma mult_nonneg_nonpos: "0 \<le> a \<Longrightarrow> b \<le> 0 \<Longrightarrow> a * b \<le> 0"
wenzelm@63325
  1663
  using mult_left_mono [of b 0 a] by simp
huffman@30692
  1664
huffman@30692
  1665
lemma mult_nonpos_nonneg: "a \<le> 0 \<Longrightarrow> 0 \<le> b \<Longrightarrow> a * b \<le> 0"
wenzelm@63325
  1666
  using mult_right_mono [of a 0 b] by simp
huffman@30692
  1667
wenzelm@63588
  1668
text \<open>Legacy -- use @{thm [source] mult_nonpos_nonneg}.\<close>
lp15@60562
  1669
lemma mult_nonneg_nonpos2: "0 \<le> a \<Longrightarrow> b \<le> 0 \<Longrightarrow> b * a \<le> 0"
wenzelm@63588
  1670
  by (drule mult_right_mono [of b 0]) auto
haftmann@25230
  1671
hoelzl@62378
  1672
lemma split_mult_neg_le: "(0 \<le> a \<and> b \<le> 0) \<or> (a \<le> 0 \<and> 0 \<le> b) \<Longrightarrow> a * b \<le> 0"
wenzelm@63325
  1673
  by (auto simp add: mult_nonneg_nonpos mult_nonneg_nonpos2)
haftmann@25230
  1674
haftmann@25230
  1675
end
haftmann@25230
  1676
hoelzl@62377
  1677
class ordered_cancel_semiring = ordered_semiring + cancel_comm_monoid_add
hoelzl@62377
  1678
begin
hoelzl@62377
  1679
hoelzl@62377
  1680
subclass semiring_0_cancel ..
wenzelm@63588
  1681
hoelzl@62377
  1682
subclass ordered_semiring_0 ..
hoelzl@62377
  1683
hoelzl@62377
  1684
end
hoelzl@62377
  1685
haftmann@38642
  1686
class linordered_semiring = ordered_semiring + linordered_cancel_ab_semigroup_add
haftmann@25267
  1687
begin
haftmann@25230
  1688
haftmann@35028
  1689
subclass ordered_cancel_semiring ..
haftmann@35028
  1690
hoelzl@62376
  1691
subclass ordered_cancel_comm_monoid_add ..
haftmann@25304
  1692
Mathias@63456
  1693
subclass ordered_ab_semigroup_monoid_add_imp_le ..
Mathias@63456
  1694
wenzelm@63325
  1695
lemma mult_left_less_imp_less: "c * a < c * b \<Longrightarrow> 0 \<le> c \<Longrightarrow> a < b"
wenzelm@63325
  1696
  by (force simp add: mult_left_mono not_le [symmetric])
lp15@60562
  1697
wenzelm@63325
  1698
lemma mult_right_less_imp_less: "a * c < b * c \<Longrightarrow> 0 \<le> c \<Longrightarrow> a < b"
wenzelm@63325
  1699
  by (force simp add: mult_right_mono not_le [symmetric])
obua@23521
  1700
haftmann@25186
  1701
end
haftmann@25152
  1702
haftmann@35043
  1703
class linordered_semiring_1 = linordered_semiring + semiring_1
hoelzl@36622
  1704
begin
hoelzl@36622
  1705
hoelzl@36622
  1706
lemma convex_bound_le:
hoelzl@36622
  1707
  assumes "x \<le> a" "y \<le> a" "0 \<le> u" "0 \<le> v" "u + v = 1"
hoelzl@36622
  1708
  shows "u * x + v * y \<le> a"
hoelzl@36622
  1709
proof-
hoelzl@36622
  1710
  from assms have "u * x + v * y \<le> u * a + v * a"
hoelzl@36622
  1711
    by (simp add: add_mono mult_left_mono)
wenzelm@63325
  1712
  with assms show ?thesis
wenzelm@63325
  1713
    unfolding distrib_right[symmetric] by simp
hoelzl@36622
  1714
qed
hoelzl@36622
  1715
hoelzl@36622
  1716
end
haftmann@35043
  1717
haftmann@35043
  1718
class linordered_semiring_strict = semiring + comm_monoid_add + linordered_cancel_ab_semigroup_add +
haftmann@25062
  1719
  assumes mult_strict_left_mono: "a < b \<Longrightarrow> 0 < c \<Longrightarrow> c * a < c * b"
haftmann@25062
  1720
  assumes mult_strict_right_mono: "a < b \<Longrightarrow> 0 < c \<Longrightarrow> a * c < b * c"
haftmann@25267
  1721
begin
paulson@14341
  1722
huffman@27516
  1723
subclass semiring_0_cancel ..
obua@14940
  1724
haftmann@35028
  1725
subclass linordered_semiring
haftmann@28823
  1726
proof
huffman@23550
  1727
  fix a b c :: 'a
wenzelm@63588
  1728
  assume *: "a \<le> b" "0 \<le> c"
wenzelm@63588
  1729
  then show "c * a \<le> c * b"
haftmann@25186
  1730
    unfolding le_less
haftmann@25186
  1731
    using mult_strict_left_mono by (cases "c = 0") auto
wenzelm@63588
  1732
  from * show "a * c \<le> b * c"
haftmann@25152
  1733
    unfolding le_less
haftmann@25186
  1734
    using mult_strict_right_mono by (cases "c = 0") auto
haftmann@25152
  1735
qed
haftmann@25152
  1736
wenzelm@63325
  1737
lemma mult_left_le_imp_le: "c * a \<le> c * b \<Longrightarrow> 0 < c \<Longrightarrow> a \<le> b"
wenzelm@63325
  1738
  by (auto simp add: mult_strict_left_mono _not_less [symmetric])
lp15@60562
  1739
wenzelm@63325
  1740
lemma mult_right_le_imp_le: "a * c \<le> b * c \<Longrightarrow> 0 < c \<Longrightarrow> a \<le> b"
wenzelm@63325
  1741
  by (auto simp add: mult_strict_right_mono not_less [symmetric])
haftmann@25230
  1742
nipkow@56544
  1743
lemma mult_pos_pos[simp]: "0 < a \<Longrightarrow> 0 < b \<Longrightarrow> 0 < a * b"
wenzelm@63325
  1744
  using mult_strict_left_mono [of 0 b a] by simp
huffman@30692
  1745
huffman@30692
  1746
lemma mult_pos_neg: "0 < a \<Longrightarrow> b < 0 \<Longrightarrow> a * b < 0"
wenzelm@63325
  1747
  using mult_strict_left_mono [of b 0 a] by simp
huffman@30692
  1748
huffman@30692
  1749
lemma mult_neg_pos: "a < 0 \<Longrightarrow> 0 < b \<Longrightarrow> a * b < 0"
wenzelm@63325
  1750
  using mult_strict_right_mono [of a 0 b] by simp
huffman@30692
  1751
wenzelm@63588
  1752
text \<open>Legacy -- use @{thm [source] mult_neg_pos}.\<close>
lp15@60562
  1753
lemma mult_pos_neg2: "0 < a \<Longrightarrow> b < 0 \<Longrightarrow> b * a < 0"
wenzelm@63588
  1754
  by (drule mult_strict_right_mono [of b 0]) auto
haftmann@25230
  1755
wenzelm@63325
  1756
lemma zero_less_mult_pos: "0 < a * b \<Longrightarrow> 0 < a \<Longrightarrow> 0 < b"
wenzelm@63325
  1757
  apply (cases "b \<le> 0")
wenzelm@63325
  1758
   apply (auto simp add: le_less not_less)
wenzelm@63325
  1759
  apply (drule_tac mult_pos_neg [of a b])
wenzelm@63325
  1760
   apply (auto dest: less_not_sym)
wenzelm@63325
  1761
  done
haftmann@25230
  1762
wenzelm@63325
  1763
lemma zero_less_mult_pos2: "0 < b * a \<Longrightarrow> 0 < a \<Longrightarrow> 0 < b"
wenzelm@63325
  1764
  apply (cases "b \<le> 0")
wenzelm@63325
  1765
   apply (auto simp add: le_less not_less)
wenzelm@63325
  1766
  apply (drule_tac mult_pos_neg2 [of a b])
wenzelm@63325
  1767
   apply (auto dest: less_not_sym)
wenzelm@63325
  1768
  done
wenzelm@63325
  1769
wenzelm@63325
  1770
text \<open>Strict monotonicity in both arguments\<close>
haftmann@26193
  1771
lemma mult_strict_mono:
haftmann@26193
  1772
  assumes "a < b" and "c < d" and "0 < b" and "0 \<le> c"
haftmann@26193
  1773
  shows "a * c < b * d"
wenzelm@63325
  1774
  using assms
wenzelm@63325
  1775
  apply (cases "c = 0")
wenzelm@63588
  1776
   apply simp
haftmann@26193
  1777
  apply (erule mult_strict_right_mono [THEN less_trans])
wenzelm@63588
  1778
   apply (auto simp add: le_less)
wenzelm@63325
  1779
  apply (erule (1) mult_strict_left_mono)
haftmann@26193
  1780
  done
haftmann@26193
  1781
wenzelm@63325
  1782
text \<open>This weaker variant has more natural premises\<close>
haftmann@26193
  1783
lemma mult_strict_mono':
haftmann@26193
  1784
  assumes "a < b" and "c < d" and "0 \<le> a" and "0 \<le> c"
haftmann@26193
  1785
  shows "a * c < b * d"
wenzelm@63325
  1786
  by (rule mult_strict_mono) (insert assms, auto)
haftmann@26193
  1787
haftmann@26193
  1788
lemma mult_less_le_imp_less:
haftmann@26193
  1789
  assumes "a < b" and "c \<le> d" and "0 \<le> a" and "0 < c"
haftmann@26193
  1790
  shows "a * c < b * d"
wenzelm@63325
  1791
  using assms
wenzelm@63325
  1792
  apply (subgoal_tac "a * c < b * c")
wenzelm@63588
  1793
   apply (erule less_le_trans)
wenzelm@63588
  1794
   apply (erule mult_left_mono)
wenzelm@63588
  1795
   apply simp
wenzelm@63325
  1796
  apply (erule (1) mult_strict_right_mono)
haftmann@26193
  1797
  done
haftmann@26193
  1798
haftmann@26193
  1799
lemma mult_le_less_imp_less:
haftmann@26193
  1800
  assumes "a \<le> b" and "c < d" and "0 < a" and "0 \<le> c"
haftmann@26193
  1801
  shows "a * c < b * d"
wenzelm@63325
  1802
  using assms
wenzelm@63325
  1803
  apply (subgoal_tac "a * c \<le> b * c")
wenzelm@63588
  1804
   apply (erule le_less_trans)
wenzelm@63588
  1805
   apply (erule mult_strict_left_mono)
wenzelm@63588
  1806
   apply simp
wenzelm@63325
  1807
  apply (erule (1) mult_right_mono)
haftmann@26193
  1808
  done
haftmann@26193
  1809
haftmann@25230
  1810
end
haftmann@25230
  1811
haftmann@35097
  1812
class linordered_semiring_1_strict = linordered_semiring_strict + semiring_1
hoelzl@36622
  1813
begin
hoelzl@36622
  1814
hoelzl@36622
  1815
subclass linordered_semiring_1 ..
hoelzl@36622
  1816
hoelzl@36622
  1817
lemma convex_bound_lt:
hoelzl@36622
  1818
  assumes "x < a" "y < a" "0 \<le> u" "0 \<le> v" "u + v = 1"
hoelzl@36622
  1819
  shows "u * x + v * y < a"
hoelzl@36622
  1820
proof -
hoelzl@36622
  1821
  from assms have "u * x + v * y < u * a + v * a"
wenzelm@63325
  1822
    by (cases "u = 0") (auto intro!: add_less_le_mono mult_strict_left_mono mult_left_mono)
wenzelm@63325
  1823
  with assms show ?thesis
wenzelm@63325
  1824
    unfolding distrib_right[symmetric] by simp
hoelzl@36622
  1825
qed
hoelzl@36622
  1826
hoelzl@36622
  1827
end
haftmann@33319
  1828
lp15@60562
  1829
class ordered_comm_semiring = comm_semiring_0 + ordered_ab_semigroup_add +
haftmann@38642
  1830
  assumes comm_mult_left_mono: "a \<le> b \<Longrightarrow> 0 \<le> c \<Longrightarrow> c * a \<le> c * b"
haftmann@25186
  1831
begin
haftmann@25152
  1832
haftmann@35028
  1833
subclass ordered_semiring
haftmann@28823
  1834
proof
krauss@21199
  1835
  fix a b c :: 'a
huffman@23550
  1836
  assume "a \<le> b" "0 \<le> c"
wenzelm@63325
  1837
  then show "c * a \<le> c * b" by (rule comm_mult_left_mono)
wenzelm@63325
  1838
  then show "a * c \<le> b * c" by (simp only: mult.commute)
krauss@21199
  1839
qed
paulson@14265
  1840
haftmann@25267
  1841
end
haftmann@25267
  1842
haftmann@38642
  1843
class ordered_cancel_comm_semiring = ordered_comm_semiring + cancel_comm_monoid_add
haftmann@25267
  1844
begin
paulson@14265
  1845
haftmann@38642
  1846
subclass comm_semiring_0_cancel ..
haftmann@35028
  1847
subclass ordered_comm_semiring ..
haftmann@35028
  1848
subclass ordered_cancel_semiring ..
haftmann@25267
  1849
haftmann@25267
  1850
end
haftmann@25267
  1851
haftmann@35028
  1852
class linordered_comm_semiring_strict = comm_semiring_0 + linordered_cancel_ab_semigroup_add +
haftmann@38642
  1853
  assumes comm_mult_strict_left_mono: "a < b \<Longrightarrow> 0 < c \<Longrightarrow> c * a < c * b"
haftmann@25267
  1854
begin
haftmann@25267
  1855
haftmann@35043
  1856
subclass linordered_semiring_strict
haftmann@28823
  1857
proof
huffman@23550
  1858
  fix a b c :: 'a
huffman@23550
  1859
  assume "a < b" "0 < c"
wenzelm@63588
  1860
  then show "c * a < c * b"
wenzelm@63588
  1861
    by (rule comm_mult_strict_left_mono)
wenzelm@63588
  1862
  then show "a * c < b * c"
wenzelm@63588
  1863
    by (simp only: mult.commute)
huffman@23550
  1864
qed
paulson@14272
  1865
haftmann@35028
  1866
subclass ordered_cancel_comm_semiring
haftmann@28823
  1867
proof
huffman@23550
  1868
  fix a b c :: 'a
huffman@23550
  1869
  assume "a \<le> b" "0 \<le> c"
wenzelm@63325
  1870
  then show "c * a \<le> c * b"
haftmann@25186
  1871
    unfolding le_less
haftmann@26193
  1872
    using mult_strict_left_mono by (cases "c = 0") auto
huffman@23550
  1873
qed
paulson@14272
  1874
haftmann@25267
  1875
end
haftmann@25230
  1876
lp15@60562
  1877
class ordered_ring = ring + ordered_cancel_semiring
haftmann@25267
  1878
begin
haftmann@25230
  1879
haftmann@35028
  1880
subclass ordered_ab_group_add ..
paulson@14270
  1881
wenzelm@63325
  1882
lemma less_add_iff1: "a * e + c < b * e + d \<longleftrightarrow> (a - b) * e + c < d"
wenzelm@63325
  1883
  by (simp add: algebra_simps)
haftmann@25230
  1884
wenzelm@63325
  1885
lemma less_add_iff2: "a * e + c < b * e + d \<longleftrightarrow> c < (b - a) * e + d"
wenzelm@63325
  1886
  by (simp add: algebra_simps)
haftmann@25230
  1887
wenzelm@63325
  1888
lemma le_add_iff1: "a * e + c \<le> b * e + d \<longleftrightarrow> (a - b) * e + c \<le> d"
wenzelm@63325
  1889
  by (simp add: algebra_simps)
haftmann@25230
  1890
wenzelm@63325
  1891
lemma le_add_iff2: "a * e + c \<le> b * e + d \<longleftrightarrow> c \<le> (b - a) * e + d"
wenzelm@63325
  1892
  by (simp add: algebra_simps)
haftmann@25230
  1893
wenzelm@63325
  1894
lemma mult_left_mono_neg: "b \<le> a \<Longrightarrow> c \<le> 0 \<Longrightarrow> c * a \<le> c * b"
haftmann@36301
  1895
  apply (drule mult_left_mono [of _ _ "- c"])
huffman@35216
  1896
  apply simp_all
haftmann@25230
  1897
  done
haftmann@25230
  1898
wenzelm@63325
  1899
lemma mult_right_mono_neg: "b \<le> a \<Longrightarrow> c \<le> 0 \<Longrightarrow> a * c \<le> b * c"
haftmann@36301
  1900
  apply (drule mult_right_mono [of _ _ "- c"])
huffman@35216
  1901
  apply simp_all
haftmann@25230
  1902
  done
haftmann@25230
  1903
huffman@30692
  1904
lemma mult_nonpos_nonpos: "a \<le> 0 \<Longrightarrow> b \<le> 0 \<Longrightarrow> 0 \<le> a * b"
wenzelm@63325
  1905
  using mult_right_mono_neg [of a 0 b] by simp
haftmann@25230
  1906
wenzelm@63325
  1907
lemma split_mult_pos_le: "(0 \<le> a \<and> 0 \<le> b) \<or> (a \<le> 0 \<and> b \<le> 0) \<Longrightarrow> 0 \<le> a * b"
wenzelm@63325
  1908
  by (auto simp add: mult_nonpos_nonpos)
haftmann@25186
  1909
haftmann@25186
  1910
end
paulson@14270
  1911
haftmann@64290
  1912
class abs_if = minus + uminus + ord + zero + abs +
haftmann@64290
  1913
  assumes abs_if: "\<bar>a\<bar> = (if a < 0 then - a else a)"
haftmann@64290
  1914
haftmann@35028
  1915
class linordered_ring = ring + linordered_semiring + linordered_ab_group_add + abs_if
haftmann@25304
  1916
begin
haftmann@25304
  1917
haftmann@35028
  1918
subclass ordered_ring ..
haftmann@35028
  1919
haftmann@35028
  1920
subclass ordered_ab_group_add_abs
haftmann@28823
  1921
proof
haftmann@25304
  1922
  fix a b
haftmann@25304
  1923
  show "\<bar>a + b\<bar> \<le> \<bar>a\<bar> + \<bar>b\<bar>"
wenzelm@63325
  1924
    by (auto simp add: abs_if not_le not_less algebra_simps
wenzelm@63325
  1925
        simp del: add.commute dest: add_neg_neg add_nonneg_nonneg)
wenzelm@63588
  1926
qed (auto simp: abs_if)
haftmann@25304
  1927
huffman@35631
  1928
lemma zero_le_square [simp]: "0 \<le> a * a"
wenzelm@63325
  1929
  using linear [of 0 a] by (auto simp add: mult_nonpos_nonpos)
huffman@35631
  1930
huffman@35631
  1931
lemma not_square_less_zero [simp]: "\<not> (a * a < 0)"
huffman@35631
  1932
  by (simp add: not_less)
huffman@35631
  1933
wenzelm@61944
  1934
proposition abs_eq_iff: "\<bar>x\<bar> = \<bar>y\<bar> \<longleftrightarrow> x = y \<or> x = -y"
nipkow@62390
  1935
  by (auto simp add: abs_if split: if_split_asm)
lp15@61762
  1936
haftmann@64848
  1937
lemma abs_eq_iff':
haftmann@64848
  1938
  "\<bar>a\<bar> = b \<longleftrightarrow> b \<ge> 0 \<and> (a = b \<or> a = - b)"
haftmann@64848
  1939
  by (cases "a \<ge> 0") auto
haftmann@64848
  1940
haftmann@64848
  1941
lemma eq_abs_iff':
haftmann@64848
  1942
  "a = \<bar>b\<bar> \<longleftrightarrow> a \<ge> 0 \<and> (b = a \<or> b = - a)"
haftmann@64848
  1943
  using abs_eq_iff' [of b a] by auto
haftmann@64848
  1944
wenzelm@63325
  1945
lemma sum_squares_ge_zero: "0 \<le> x * x + y * y"
haftmann@62347
  1946
  by (intro add_nonneg_nonneg zero_le_square)
haftmann@62347
  1947
wenzelm@63325
  1948
lemma not_sum_squares_lt_zero: "\<not> x * x + y * y < 0"
haftmann@62347
  1949
  by (simp add: not_less sum_squares_ge_zero)
haftmann@62347
  1950
haftmann@25304
  1951
end
obua@23521
  1952
haftmann@35043
  1953
class linordered_ring_strict = ring + linordered_semiring_strict
haftmann@25304
  1954
  + ordered_ab_group_add + abs_if
haftmann@25230
  1955
begin
paulson@14348
  1956
haftmann@35028
  1957
subclass linordered_ring ..
haftmann@25304
  1958
huffman@30692
  1959
lemma mult_strict_left_mono_neg: "b < a \<Longrightarrow> c < 0 \<Longrightarrow> c * a < c * b"
wenzelm@63325
  1960
  using mult_strict_left_mono [of b a "- c"] by simp
huffman@30692
  1961
huffman@30692
  1962
lemma mult_strict_right_mono_neg: "b < a \<Longrightarrow> c < 0 \<Longrightarrow> a * c < b * c"
wenzelm@63325
  1963
  using mult_strict_right_mono [of b a "- c"] by simp
huffman@30692
  1964
huffman@30692
  1965
lemma mult_neg_neg: "a < 0 \<Longrightarrow> b < 0 \<Longrightarrow> 0 < a * b"
wenzelm@63325
  1966
  using mult_strict_right_mono_neg [of a 0 b] by simp
obua@14738
  1967
haftmann@25917
  1968
subclass ring_no_zero_divisors
haftmann@28823
  1969
proof
haftmann@25917
  1970
  fix a b
wenzelm@63325
  1971
  assume "a \<noteq> 0"
wenzelm@63588
  1972
  then have a: "a < 0 \<or> 0 < a" by (simp add: neq_iff)
wenzelm@63325
  1973
  assume "b \<noteq> 0"
wenzelm@63588
  1974
  then have b: "b < 0 \<or> 0 < b" by (simp add: neq_iff)
haftmann@25917
  1975
  have "a * b < 0 \<or> 0 < a * b"
haftmann@25917
  1976
  proof (cases "a < 0")
wenzelm@63588
  1977
    case True
wenzelm@63325
  1978
    show ?thesis
wenzelm@63325
  1979
    proof (cases "b < 0")
wenzelm@63325
  1980
      case True
wenzelm@63588
  1981
      with \<open>a < 0\<close> show ?thesis by (auto dest: mult_neg_neg)
haftmann@25917
  1982
    next
wenzelm@63325
  1983
      case False
wenzelm@63588
  1984
      with b have "0 < b" by auto
wenzelm@63588
  1985
      with \<open>a < 0\<close> show ?thesis by (auto dest: mult_strict_right_mono)
haftmann@25917
  1986
    qed
haftmann@25917
  1987
  next
wenzelm@63325
  1988
    case False
wenzelm@63588
  1989
    with a have "0 < a" by auto
wenzelm@63325
  1990
    show ?thesis
wenzelm@63325
  1991
    proof (cases "b < 0")
wenzelm@63325
  1992
      case True
wenzelm@63588
  1993
      with \<open>0 < a\<close> show ?thesis
wenzelm@63325
  1994
        by (auto dest: mult_strict_right_mono_neg)
haftmann@25917
  1995
    next
wenzelm@63325
  1996
      case False
wenzelm@63588
  1997
      with b have "0 < b" by auto
wenzelm@63588
  1998
      with \<open>0 < a\<close> show ?thesis by auto
haftmann@25917
  1999
    qed
haftmann@25917
  2000
  qed
wenzelm@63325
  2001
  then show "a * b \<noteq> 0"
wenzelm@63325
  2002
    by (simp add: neq_iff)
haftmann@25917
  2003
qed
haftmann@25304
  2004
hoelzl@56480
  2005
lemma zero_less_mult_iff: "0 < a * b \<longleftrightarrow> 0 < a \<and> 0 < b \<or> a < 0 \<and> b < 0"
hoelzl@56480
  2006
  by (cases a 0 b 0 rule: linorder_cases[case_product linorder_cases])
nipkow@56544
  2007
     (auto simp add: mult_neg_neg not_less le_less dest: zero_less_mult_pos zero_less_mult_pos2)
huffman@22990
  2008
hoelzl@56480
  2009
lemma zero_le_mult_iff: "0 \<le> a * b \<longleftrightarrow> 0 \<le> a \<and> 0 \<le> b \<or> a \<le> 0 \<and> b \<le> 0"
hoelzl@56480
  2010
  by (auto simp add: eq_commute [of 0] le_less not_less zero_less_mult_iff)
paulson@14265
  2011
wenzelm@63325
  2012
lemma mult_less_0_iff: "a * b < 0 \<longleftrightarrow> 0 < a \<and> b < 0 \<or> a < 0 \<and> 0 < b"
wenzelm@63325
  2013
  using zero_less_mult_iff [of "- a" b] by auto
paulson@14265
  2014
wenzelm@63325
  2015
lemma mult_le_0_iff: "a * b \<le> 0 \<longleftrightarrow> 0 \<le> a \<and> b \<le> 0 \<or> a \<le> 0 \<and> 0 \<le> b"
wenzelm@63325
  2016
  using zero_le_mult_iff [of "- a" b] by auto
haftmann@25917
  2017
wenzelm@63325
  2018
text \<open>
wenzelm@63325
  2019
  Cancellation laws for @{term "c * a < c * b"} and @{term "a * c < b * c"},
wenzelm@63325
  2020
  also with the relations \<open>\<le>\<close> and equality.
wenzelm@63325
  2021
\<close>
haftmann@26193
  2022
wenzelm@63325
  2023
text \<open>
wenzelm@63325
  2024
  These ``disjunction'' versions produce two cases when the comparison is
wenzelm@63325
  2025
  an assumption, but effectively four when the comparison is a goal.
wenzelm@63325
  2026
\<close>
haftmann@26193
  2027
wenzelm@63325
  2028
lemma mult_less_cancel_right_disj: "a * c < b * c \<longleftrightarrow> 0 < c \<and> a < b \<or> c < 0 \<and>  b < a"
haftmann@26193
  2029
  apply (cases "c = 0")
wenzelm@63588
  2030
   apply (auto simp add: neq_iff mult_strict_right_mono mult_strict_right_mono_neg)
wenzelm@63588
  2031
     apply (auto simp add: not_less not_le [symmetric, of "a*c"] not_le [symmetric, of a])
wenzelm@63588
  2032
     apply (erule_tac [!] notE)
wenzelm@63588
  2033
     apply (auto simp add: less_imp_le mult_right_mono mult_right_mono_neg)
haftmann@26193
  2034
  done
haftmann@26193
  2035
wenzelm@63325
  2036
lemma mult_less_cancel_left_disj: "c * a < c * b \<longleftrightarrow> 0 < c \<and> a < b \<or> c < 0 \<and>  b < a"
haftmann@26193
  2037
  apply (cases "c = 0")
wenzelm@63588
  2038
   apply (auto simp add: neq_iff mult_strict_left_mono mult_strict_left_mono_neg)
wenzelm@63588
  2039
     apply (auto simp add: not_less not_le [symmetric, of "c * a"] not_le [symmetric, of a])
wenzelm@63588
  2040
     apply (erule_tac [!] notE)
wenzelm@63588
  2041
     apply (auto simp add: less_imp_le mult_left_mono mult_left_mono_neg)
haftmann@26193
  2042
  done
haftmann@26193
  2043
wenzelm@63325
  2044
text \<open>
wenzelm@63325
  2045
  The ``conjunction of implication'' lemmas produce two cases when the
wenzelm@63325
  2046
  comparison is a goal, but give four when the comparison is an assumption.
wenzelm@63325
  2047
\<close>
haftmann@26193
  2048
wenzelm@63325
  2049
lemma mult_less_cancel_right: "a * c < b * c \<longleftrightarrow> (0 \<le> c \<longrightarrow> a < b) \<and> (c \<le> 0 \<longrightarrow> b < a)"
haftmann@26193
  2050
  using mult_less_cancel_right_disj [of a c b] by auto
haftmann@26193
  2051
wenzelm@63325
  2052
lemma mult_less_cancel_left: "c * a < c * b \<longleftrightarrow> (0 \<le> c \<longrightarrow> a < b) \<and> (c \<le> 0 \<longrightarrow> b < a)"
haftmann@26193
  2053
  using mult_less_cancel_left_disj [of c a b] by auto
haftmann@26193
  2054
wenzelm@63325
  2055
lemma mult_le_cancel_right: "a * c \<le> b * c \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> a)"
wenzelm@63325
  2056
  by (simp add: not_less [symmetric] mult_less_cancel_right_disj)
haftmann@26193
  2057
wenzelm@63325
  2058
lemma mult_le_cancel_left: "c * a \<le> c * b \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> a)"
wenzelm@63325
  2059
  by (simp add: not_less [symmetric] mult_less_cancel_left_disj)
haftmann@26193
  2060
wenzelm@63325
  2061
lemma mult_le_cancel_left_pos: "0 < c \<Longrightarrow> c * a \<le> c * b \<longleftrightarrow> a \<le> b"
wenzelm@63325
  2062
  by (auto simp: mult_le_cancel_left)
nipkow@30649
  2063
wenzelm@63325
  2064
lemma mult_le_cancel_left_neg: "c < 0 \<Longrightarrow> c * a \<le> c * b \<longleftrightarrow> b \<le> a"
wenzelm@63325
  2065
  by (auto simp: mult_le_cancel_left)
nipkow@30649
  2066
wenzelm@63325
  2067
lemma mult_less_cancel_left_pos: "0 < c \<Longrightarrow> c * a < c * b \<longleftrightarrow> a < b"
wenzelm@63325
  2068
  by (auto simp: mult_less_cancel_left)
nipkow@30649
  2069
wenzelm@63325
  2070
lemma mult_less_cancel_left_neg: "c < 0 \<Longrightarrow> c * a < c * b \<longleftrightarrow> b < a"
wenzelm@63325
  2071
  by (auto simp: mult_less_cancel_left)
nipkow@30649
  2072
haftmann@25917
  2073
end
paulson@14265
  2074
huffman@30692
  2075
lemmas mult_sign_intros =
huffman@30692
  2076
  mult_nonneg_nonneg mult_nonneg_nonpos
huffman@30692
  2077
  mult_nonpos_nonneg mult_nonpos_nonpos
huffman@30692
  2078
  mult_pos_pos mult_pos_neg
huffman@30692
  2079
  mult_neg_pos mult_neg_neg
haftmann@25230
  2080
haftmann@35028
  2081
class ordered_comm_ring = comm_ring + ordered_comm_semiring
haftmann@25267
  2082
begin
haftmann@25230
  2083
haftmann@35028
  2084
subclass ordered_ring ..
haftmann@35028
  2085
subclass ordered_cancel_comm_semiring ..
haftmann@25230
  2086
haftmann@25267
  2087
end
haftmann@25230
  2088
hoelzl@62378
  2089
class zero_less_one = order + zero + one +
haftmann@25230
  2090
  assumes zero_less_one [simp]: "0 < 1"
hoelzl@62378
  2091
hoelzl@62378
  2092
class linordered_nonzero_semiring = ordered_comm_semiring + monoid_mult + linorder + zero_less_one
hoelzl@62378
  2093
begin
hoelzl@62378
  2094
hoelzl@62378
  2095
subclass zero_neq_one
wenzelm@63325
  2096
  by standard (insert zero_less_one, blast)
hoelzl@62378
  2097
hoelzl@62378
  2098
subclass comm_semiring_1
wenzelm@63325
  2099
  by standard (rule mult_1_left)
hoelzl@62378
  2100
hoelzl@62378
  2101
lemma zero_le_one [simp]: "0 \<le> 1"
wenzelm@63325
  2102
  by (rule zero_less_one [THEN less_imp_le])
hoelzl@62378
  2103
hoelzl@62378
  2104
lemma not_one_le_zero [simp]: "\<not> 1 \<le> 0"
wenzelm@63325
  2105
  by (simp add: not_le)
hoelzl@62378
  2106
hoelzl@62378
  2107
lemma not_one_less_zero [simp]: "\<not> 1 < 0"
wenzelm@63325
  2108
  by (simp add: not_less)
hoelzl@62378
  2109
hoelzl@62378
  2110
lemma mult_left_le: "c \<le> 1 \<Longrightarrow> 0 \<le> a \<Longrightarrow> a * c \<le> a"
hoelzl@62378
  2111
  using mult_left_mono[of c 1 a] by simp
hoelzl@62378
  2112
hoelzl@62378
  2113
lemma mult_le_one: "a \<le> 1 \<Longrightarrow> 0 \<le> b \<Longrightarrow> b \<le> 1 \<Longrightarrow> a * b \<le> 1"
hoelzl@62378
  2114
  using mult_mono[of a 1 b 1] by simp
hoelzl@62378
  2115
hoelzl@62378
  2116
lemma zero_less_two: "0 < 1 + 1"
hoelzl@62378
  2117
  using add_pos_pos[OF zero_less_one zero_less_one] .
hoelzl@62378
  2118
hoelzl@62378
  2119
end
hoelzl@62378
  2120
hoelzl@62378
  2121
class linordered_semidom = semidom + linordered_comm_semiring_strict + zero_less_one +
lp15@60562
  2122
  assumes le_add_diff_inverse2 [simp]: "b \<le> a \<Longrightarrow> a - b + b = a"
haftmann@25230
  2123
begin
haftmann@25230
  2124
wenzelm@63325
  2125
subclass linordered_nonzero_semiring ..
hoelzl@62378
  2126
wenzelm@60758
  2127
text \<open>Addition is the inverse of subtraction.\<close>
lp15@60562
  2128
lp15@60562
  2129
lemma le_add_diff_inverse [simp]: "b \<le> a \<Longrightarrow> b + (a - b) = a"
lp15@60562
  2130
  by (frule le_add_diff_inverse2) (simp add: add.commute)
lp15@60562
  2131
hoelzl@62378
  2132
lemma add_diff_inverse: "\<not> a < b \<Longrightarrow> b + (a - b) = a"
lp15@60562
  2133
  by simp
lp15@60615
  2134
wenzelm@63325
  2135
lemma add_le_imp_le_diff: "i + k \<le> n \<Longrightarrow> i \<le> n - k"
lp15@60615
  2136
  apply (subst add_le_cancel_right [where c=k, symmetric])
lp15@60615
  2137
  apply (frule le_add_diff_inverse2)
lp15@60615
  2138
  apply (simp only: add.assoc [symmetric])
wenzelm@63588
  2139
  using add_implies_diff
wenzelm@63588
  2140
  apply fastforce
wenzelm@63325
  2141
  done
lp15@60615
  2142
hoelzl@62376
  2143
lemma add_le_add_imp_diff_le:
wenzelm@63325
  2144
  assumes 1: "i + k \<le> n"
wenzelm@63325
  2145
    and 2: "n \<le> j + k"
wenzelm@63325
  2146
  shows "i + k \<le> n \<Longrightarrow> n \<le> j + k \<Longrightarrow> n - k \<le> j"
lp15@60615
  2147
proof -
lp15@60615
  2148
  have "n - (i + k) + (i + k) = n"
wenzelm@63325
  2149
    using 1 by simp
lp15@60615
  2150
  moreover have "n - k = n - k - i + i"
wenzelm@63325
  2151
    using 1 by (simp add: add_le_imp_le_diff)
lp15@60615
  2152
  ultimately show ?thesis
wenzelm@63325
  2153
    using 2
lp15@60615
  2154
    apply (simp add: add.assoc [symmetric])
wenzelm@63325
  2155
    apply (rule add_le_imp_le_diff [of _ k "j + k", simplified add_diff_cancel_right'])
wenzelm@63325
  2156
    apply (simp add: add.commute diff_diff_add)
wenzelm@63325
  2157
    done
lp15@60615
  2158
qed
lp15@60615
  2159
wenzelm@63325
  2160
lemma less_1_mult: "1 < m \<Longrightarrow> 1 < n \<Longrightarrow> 1 < m * n"
hoelzl@62378
  2161
  using mult_strict_mono [of 1 m 1 n] by (simp add: less_trans [OF zero_less_one])
hoelzl@59000
  2162
haftmann@25230
  2163
end
haftmann@25230
  2164
hoelzl@62378
  2165
class linordered_idom =
haftmann@64290
  2166
  comm_ring_1 + linordered_comm_semiring_strict + ordered_ab_group_add + abs_if + sgn +
haftmann@64290
  2167
  assumes sgn_if: "sgn x = (if x = 0 then 0 else if 0 < x then 1 else - 1)"
haftmann@25917
  2168
begin
haftmann@25917
  2169
hoelzl@36622
  2170
subclass linordered_semiring_1_strict ..
haftmann@35043
  2171
subclass linordered_ring_strict ..
haftmann@35028
  2172
subclass ordered_comm_ring ..
huffman@27516
  2173
subclass idom ..
haftmann@25917
  2174
haftmann@35028
  2175
subclass linordered_semidom
haftmann@28823
  2176
proof
haftmann@26193
  2177
  have "0 \<le> 1 * 1" by (rule zero_le_square)
wenzelm@63325
  2178
  then show "0 < 1" by (simp add: le_less)
wenzelm@63588
  2179
  show "b \<le> a \<Longrightarrow> a - b + b = a" for a b by simp
lp15@60562
  2180
qed
haftmann@25917
  2181
haftmann@64290
  2182
subclass idom_abs_sgn
haftmann@64290
  2183
  by standard
haftmann@64290
  2184
    (auto simp add: sgn_if abs_if zero_less_mult_iff)
haftmann@64290
  2185
haftmann@35028
  2186
lemma linorder_neqE_linordered_idom:
wenzelm@63325
  2187
  assumes "x \<noteq> y"
wenzelm@63325
  2188
  obtains "x < y" | "y < x"
haftmann@26193
  2189
  using assms by (rule neqE)
haftmann@26193
  2190
wenzelm@63588
  2191
text \<open>These cancellation simp rules also produce two cases when the comparison is a goal.\<close>
haftmann@26274
  2192
wenzelm@63325
  2193
lemma mult_le_cancel_right1: "c \<le> b * c \<longleftrightarrow> (0 < c \<longrightarrow> 1 \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> 1)"
wenzelm@63325
  2194
  using mult_le_cancel_right [of 1 c b] by simp
haftmann@26274
  2195
wenzelm@63325
  2196
lemma mult_le_cancel_right2: "a * c \<le> c \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> 1) \<and> (c < 0 \<longrightarrow> 1 \<le> a)"
wenzelm@63325
  2197
  using mult_le_cancel_right [of a c 1] by simp
haftmann@26274
  2198
wenzelm@63325
  2199
lemma mult_le_cancel_left1: "c \<le> c * b \<longleftrightarrow> (0 < c \<longrightarrow> 1 \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> 1)"
wenzelm@63325
  2200
  using mult_le_cancel_left [of c 1 b] by simp
haftmann@26274
  2201
wenzelm@63325
  2202
lemma mult_le_cancel_left2: "c * a \<le> c \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> 1) \<and> (c < 0 \<longrightarrow> 1 \<le> a)"
wenzelm@63325
  2203
  using mult_le_cancel_left [of c a 1] by simp
haftmann@26274
  2204
wenzelm@63325
  2205
lemma mult_less_cancel_right1: "c < b * c \<longleftrightarrow> (0 \<le> c \<longrightarrow> 1 < b) \<and> (c \<le> 0 \<longrightarrow> b < 1)"
wenzelm@63325
  2206
  using mult_less_cancel_right [of 1 c b] by simp
haftmann@26274
  2207
wenzelm@63325
  2208
lemma mult_less_cancel_right2: "a * c < c \<longleftrightarrow> (0 \<le> c \<longrightarrow> a < 1) \<and> (c \<le> 0 \<longrightarrow> 1 < a)"
wenzelm@63325
  2209
  using mult_less_cancel_right [of a c 1] by simp
haftmann@26274
  2210
wenzelm@63325
  2211
lemma mult_less_cancel_left1: "c < c * b \<longleftrightarrow> (0 \<le> c \<longrightarrow> 1 < b) \<and> (c \<le> 0 \<longrightarrow> b < 1)"
wenzelm@63325
  2212
  using mult_less_cancel_left [of c 1 b] by simp
haftmann@26274
  2213
wenzelm@63325
  2214
lemma mult_less_cancel_left2: "c * a < c \<longleftrightarrow> (0 \<le> c \<longrightarrow> a < 1) \<and> (c \<le> 0 \<longrightarrow> 1 < a)"
wenzelm@63325
  2215
  using mult_less_cancel_left [of c a 1] by simp
haftmann@26274
  2216
wenzelm@63325
  2217
lemma sgn_0_0: "sgn a = 0 \<longleftrightarrow> a = 0"
haftmann@64290
  2218
  by (fact sgn_eq_0_iff)
haftmann@27651
  2219
wenzelm@63325
  2220
lemma sgn_1_pos: "sgn a = 1 \<longleftrightarrow> a > 0"
wenzelm@63325
  2221
  unfolding sgn_if by simp
haftmann@27651
  2222
wenzelm@63325
  2223
lemma sgn_1_neg: "sgn a = - 1 \<longleftrightarrow> a < 0"
wenzelm@63325
  2224
  unfolding sgn_if by auto
haftmann@27651
  2225
wenzelm@63325
  2226
lemma sgn_pos [simp]: "0 < a \<Longrightarrow> sgn a = 1"
wenzelm@63325
  2227
  by (simp only: sgn_1_pos)
haftmann@29940
  2228
wenzelm@63325
  2229
lemma sgn_neg [simp]: "a < 0 \<Longrightarrow> sgn a = - 1"
wenzelm@63325
  2230
  by (simp only: sgn_1_neg)
haftmann@29940
  2231
haftmann@36301
  2232
lemma abs_sgn: "\<bar>k\<bar> = k * sgn k"
wenzelm@63325
  2233
  unfolding sgn_if abs_if by auto
nipkow@29700
  2234
wenzelm@63325
  2235
lemma sgn_greater [simp]: "0 < sgn a \<longleftrightarrow> 0 < a"
haftmann@29940
  2236
  unfolding sgn_if by auto
haftmann@29940
  2237
wenzelm@63325
  2238
lemma sgn_less [simp]: "sgn a < 0 \<longleftrightarrow> a < 0"
haftmann@29940
  2239
  unfolding sgn_if by auto
haftmann@29940
  2240
haftmann@64239
  2241
lemma abs_sgn_eq_1 [simp]:
haftmann@64239
  2242
  "a \<noteq> 0 \<Longrightarrow> \<bar>sgn a\<bar> = 1"
haftmann@64290
  2243
  by simp
haftmann@64239
  2244
wenzelm@63325
  2245
lemma abs_sgn_eq: "\<bar>sgn a\<bar> = (if a = 0 then 0 else 1)"
haftmann@62347
  2246
  by (simp add: sgn_if)
haftmann@62347
  2247
haftmann@64713
  2248
lemma sgn_mult_self_eq [simp]:
haftmann@64713
  2249
  "sgn a * sgn a = of_bool (a \<noteq> 0)"
haftmann@64713
  2250
  by (cases "a > 0") simp_all
haftmann@64713
  2251
haftmann@64713
  2252
lemma abs_mult_self_eq [simp]:
haftmann@64713
  2253
  "\<bar>a\<bar> * \<bar>a\<bar> = a * a"
haftmann@64713
  2254
  by (cases "a > 0") simp_all
haftmann@64713
  2255
haftmann@64713
  2256
lemma same_sgn_sgn_add:
haftmann@64713
  2257
  "sgn (a + b) = sgn a" if "sgn b = sgn a"
haftmann@64713
  2258
proof (cases a 0 rule: linorder_cases)
haftmann@64713
  2259
  case equal
haftmann@64713
  2260
  with that show ?thesis
haftmann@64713
  2261
    by simp
haftmann@64713
  2262
next
haftmann@64713
  2263
  case less
haftmann@64713
  2264
  with that have "b < 0"
haftmann@64713
  2265
    by (simp add: sgn_1_neg)
haftmann@64713
  2266
  with \<open>a < 0\<close> have "a + b < 0"
haftmann@64713
  2267
    by (rule add_neg_neg)
haftmann@64713
  2268
  with \<open>a < 0\<close> show ?thesis
haftmann@64713
  2269
    by simp
haftmann@64713
  2270
next
haftmann@64713
  2271
  case greater
haftmann@64713
  2272
  with that have "b > 0"
haftmann@64713
  2273
    by (simp add: sgn_1_pos)
haftmann@64713
  2274
  with \<open>a > 0\<close> have "a + b > 0"
haftmann@64713
  2275
    by (rule add_pos_pos)
haftmann@64713
  2276
  with \<open>a > 0\<close> show ?thesis
haftmann@64713
  2277
    by simp
haftmann@64713
  2278
qed
haftmann@64713
  2279
haftmann@64713
  2280
lemma same_sgn_abs_add:
haftmann@64713
  2281
  "\<bar>a + b\<bar> = \<bar>a\<bar> + \<bar>b\<bar>" if "sgn b = sgn a"
haftmann@64713
  2282
proof -
haftmann@64713
  2283
  have "a + b = sgn a * \<bar>a\<bar> + sgn b * \<bar>b\<bar>"
haftmann@64713
  2284
    by (simp add: sgn_mult_abs)
haftmann@64713
  2285
  also have "\<dots> = sgn a * (\<bar>a\<bar> + \<bar>b\<bar>)"
haftmann@64713
  2286
    using that by (simp add: algebra_simps)
haftmann@64713
  2287
  finally show ?thesis
haftmann@64713
  2288
    by (auto simp add: abs_mult)
haftmann@64713
  2289
qed
haftmann@64713
  2290
haftmann@36301
  2291
lemma abs_dvd_iff [simp]: "\<bar>m\<bar> dvd k \<longleftrightarrow> m dvd k"
huffman@29949
  2292
  by (simp add: abs_if)
huffman@29949
  2293
haftmann@36301
  2294
lemma dvd_abs_iff [simp]: "m dvd \<bar>k\<bar> \<longleftrightarrow> m dvd k"
huffman@29949
  2295
  by (simp add: abs_if)
haftmann@29653
  2296
wenzelm@63325
  2297
lemma dvd_if_abs_eq: "\<bar>l\<bar> = \<bar>k\<bar> \<Longrightarrow> l dvd k"
wenzelm@63325
  2298
  by (subst abs_dvd_iff [symmetric]) simp
nipkow@33676
  2299
wenzelm@63325
  2300
text \<open>
wenzelm@63325
  2301
  The following lemmas can be proven in more general structures, but
wenzelm@63325
  2302
  are dangerous as simp rules in absence of @{thm neg_equal_zero},
wenzelm@63325
  2303
  @{thm neg_less_pos}, @{thm neg_less_eq_nonneg}.
wenzelm@63325
  2304
\<close>
haftmann@54489
  2305
wenzelm@63325
  2306
lemma equation_minus_iff_1 [simp, no_atp]: "1 = - a \<longleftrightarrow> a = - 1"
haftmann@54489
  2307
  by (fact equation_minus_iff)
haftmann@54489
  2308
wenzelm@63325
  2309
lemma minus_equation_iff_1 [simp, no_atp]: "- a = 1 \<longleftrightarrow> a = - 1"
haftmann@54489
  2310
  by (subst minus_equation_iff, auto)
haftmann@54489
  2311
wenzelm@63325
  2312
lemma le_minus_iff_1 [simp, no_atp]: "1 \<le> - b \<longleftrightarrow> b \<le> - 1"
haftmann@54489
  2313
  by (fact le_minus_iff)
haftmann@54489
  2314
wenzelm@63325
  2315
lemma minus_le_iff_1 [simp, no_atp]: "- a \<le> 1 \<longleftrightarrow> - 1 \<le> a"
haftmann@54489
  2316
  by (fact minus_le_iff)
haftmann@54489
  2317
wenzelm@63325
  2318
lemma less_minus_iff_1 [simp, no_atp]: "1 < - b \<longleftrightarrow> b < - 1"
haftmann@54489
  2319
  by (fact less_minus_iff)
haftmann@54489
  2320
wenzelm@63325
  2321
lemma minus_less_iff_1 [simp, no_atp]: "- a < 1 \<longleftrightarrow> - 1 < a"
haftmann@54489
  2322
  by (fact minus_less_iff)
haftmann@54489
  2323
lp15@66793
  2324
lemma add_less_zeroD:
lp15@66793
  2325
  shows "x+y < 0 \<Longrightarrow> x<0 \<or> y<0"
lp15@66793
  2326
  by (auto simp: not_less intro: le_less_trans [of _ "x+y"])
lp15@66793
  2327
haftmann@25917
  2328
end
haftmann@25230
  2329
wenzelm@60758
  2330
text \<open>Simprules for comparisons where common factors can be cancelled.\<close>
paulson@15234
  2331
blanchet@54147
  2332
lemmas mult_compare_simps =
wenzelm@63325
  2333
  mult_le_cancel_right mult_le_cancel_left
wenzelm@63325
  2334
  mult_le_cancel_right1 mult_le_cancel_right2
wenzelm@63325
  2335
  mult_le_cancel_left1 mult_le_cancel_left2
wenzelm@63325
  2336
  mult_less_cancel_right mult_less_cancel_left
wenzelm@63325
  2337
  mult_less_cancel_right1 mult_less_cancel_right2
wenzelm@63325
  2338
  mult_less_cancel_left1 mult_less_cancel_left2
wenzelm@63325
  2339
  mult_cancel_right mult_cancel_left
wenzelm@63325
  2340
  mult_cancel_right1 mult_cancel_right2
wenzelm@63325
  2341
  mult_cancel_left1 mult_cancel_left2
wenzelm@63325
  2342
paulson@15234
  2343
wenzelm@60758
  2344
text \<open>Reasoning about inequalities with division\<close>
avigad@16775
  2345
haftmann@35028
  2346
context linordered_semidom
haftmann@25193
  2347
begin
haftmann@25193
  2348
haftmann@25193
  2349
lemma less_add_one: "a < a + 1"
paulson@14293
  2350
proof -
haftmann@25193
  2351
  have "a + 0 < a + 1"
nipkow@23482
  2352
    by (blast intro: zero_less_one add_strict_left_mono)
wenzelm@63325
  2353
  then show ?thesis by simp
paulson@14293
  2354
qed
paulson@14293
  2355
haftmann@25193
  2356
end
paulson@14365
  2357
haftmann@36301
  2358
context linordered_idom
haftmann@36301
  2359
begin
paulson@15234
  2360
wenzelm@63325
  2361
lemma mult_right_le_one_le: "0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> x * y \<le> x"
haftmann@59833
  2362
  by (rule mult_left_le)
haftmann@36301
  2363
wenzelm@63325
  2364
lemma mult_left_le_one_le: "0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> y * x \<le> x"
haftmann@36301
  2365
  by (auto simp add: mult_le_cancel_right2)
haftmann@36301
  2366
haftmann@36301
  2367
end
haftmann@36301
  2368
wenzelm@60758
  2369
text \<open>Absolute Value\<close>
paulson@14293
  2370
haftmann@35028
  2371
context linordered_idom
haftmann@25304
  2372
begin
haftmann@25304
  2373
wenzelm@63325
  2374
lemma mult_sgn_abs: "sgn x * \<bar>x\<bar> = x"
haftmann@64290
  2375
  by (fact sgn_mult_abs)
haftmann@25304
  2376
haftmann@64290
  2377
lemma abs_one: "\<bar>1\<bar> = 1"
haftmann@64290
  2378
  by (fact abs_1)
haftmann@36301
  2379
haftmann@25304
  2380
end
nipkow@24491
  2381
haftmann@35028
  2382
class ordered_ring_abs = ordered_ring + ordered_ab_group_add_abs +
haftmann@25304
  2383
  assumes abs_eq_mult:
haftmann@25304
  2384
    "(0 \<le> a \<or> a \<le> 0) \<and> (0 \<le> b \<or> b \<le> 0) \<Longrightarrow> \<bar>a * b\<bar> = \<bar>a\<bar> * \<bar>b\<bar>"
haftmann@25304
  2385
haftmann@35028
  2386
context linordered_idom
haftmann@30961
  2387
begin
haftmann@30961
  2388
wenzelm@63325
  2389
subclass ordered_ring_abs
wenzelm@63588
  2390
  by standard (auto simp: abs_if not_less mult_less_0_iff)
haftmann@30961
  2391
wenzelm@63325
  2392
lemma abs_mult_self [simp]: "\<bar>a\<bar> * \<bar>a\<bar> = a * a"
lp15@60562
  2393
  by (simp add: abs_if)
haftmann@30961
  2394
paulson@14294
  2395
lemma abs_mult_less:
wenzelm@63325
  2396
  assumes ac: "\<bar>a\<bar> < c"
wenzelm@63325
  2397
    and bd: "\<bar>b\<bar> < d"
wenzelm@63325
  2398
  shows "\<bar>a\<bar> * \<bar>b\<bar> < c * d"
paulson@14294
  2399
proof -
wenzelm@63325
  2400
  from ac have "0 < c"
wenzelm@63325
  2401
    by (blast intro: le_less_trans abs_ge_zero)
wenzelm@63325
  2402
  with bd show ?thesis by (simp add: ac mult_strict_mono)
paulson@14294
  2403
qed
paulson@14293
  2404
wenzelm@63325
  2405
lemma abs_less_iff: "\<bar>a\<bar> < b \<longleftrightarrow> a < b \<and> - a < b"
haftmann@36301
  2406
  by (simp add: less_le abs_le_iff) (auto simp add: abs_if)
obua@14738
  2407
wenzelm@63325
  2408
lemma abs_mult_pos: "0 \<le> x \<Longrightarrow> \<bar>y\<bar> * x = \<bar>y * x\<bar>"
haftmann@36301
  2409
  by (simp add: abs_mult)
haftmann@36301
  2410
wenzelm@63325
  2411
lemma abs_diff_less_iff: "\<bar>x - a\<bar> < r \<longleftrightarrow> a - r < x \<and> x < a + r"
hoelzl@51520
  2412
  by (auto simp add: diff_less_eq ac_simps abs_less_iff)
hoelzl@51520
  2413
wenzelm@63325
  2414
lemma abs_diff_le_iff: "\<bar>x - a\<bar> \<le> r \<longleftrightarrow> a - r \<le> x \<and> x \<le> a + r"
lp15@59865
  2415
  by (auto simp add: diff_le_eq ac_simps abs_le_iff)
lp15@59865
  2416
lp15@62626
  2417
lemma abs_add_one_gt_zero: "0 < 1 + \<bar>x\<bar>"
wenzelm@63325
  2418
  by (auto simp: abs_if not_less intro: zero_less_one add_strict_increasing less_trans)
lp15@62626
  2419
haftmann@36301
  2420
end
avigad@16775
  2421
hoelzl@62376
  2422
subsection \<open>Dioids\<close>
hoelzl@62376
  2423
wenzelm@63325
  2424
text \<open>
wenzelm@63325
  2425
  Dioids are the alternative extensions of semirings, a semiring can
wenzelm@63325
  2426
  either be a ring or a dioid but never both.
wenzelm@63325
  2427
\<close>
hoelzl@62376
  2428
hoelzl@62376
  2429
class dioid = semiring_1 + canonically_ordered_monoid_add
hoelzl@62376
  2430
begin
hoelzl@62376
  2431
hoelzl@62376
  2432
subclass ordered_semiring
wenzelm@63325
  2433
  by standard (auto simp: le_iff_add distrib_left distrib_right)
hoelzl@62376
  2434
hoelzl@62376
  2435
end
hoelzl@62376
  2436
hoelzl@62376
  2437
haftmann@59557
  2438
hide_fact (open) comm_mult_left_mono comm_mult_strict_left_mono distrib
haftmann@59557
  2439
haftmann@52435
  2440
code_identifier
haftmann@52435
  2441
  code_module Rings \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
haftmann@33364
  2442
paulson@14265
  2443
end