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