src/HOL/Rings.thy
author fleury <Mathias.Fleury@mpi-inf.mpg.de>
Tue Jul 12 13:55:35 2016 +0200 (2016-07-12)
changeset 63456 3365c8ec67bd
parent 63359 99b51ba8da1c
child 63588 d0e2bad67bd4
permissions -rw-r--r--
sharing simp rules between ordered monoids and rings
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
eberlm@63359
   760
lemma dvd_mult_imp_div:
eberlm@63359
   761
  assumes "a * c dvd b"
eberlm@63359
   762
  shows "a dvd b div c"
eberlm@63359
   763
proof (cases "c = 0")
eberlm@63359
   764
  case True then show ?thesis by simp
eberlm@63359
   765
next
eberlm@63359
   766
  case False
eberlm@63359
   767
  from \<open>a * c dvd b\<close> obtain d where "b = a * c * d" ..
eberlm@63359
   768
  with False show ?thesis by (simp add: mult.commute [of a] mult.assoc)
eberlm@63359
   769
qed
eberlm@63359
   770
lp15@60562
   771
haftmann@60517
   772
text \<open>Units: invertible elements in a ring\<close>
haftmann@60517
   773
haftmann@60517
   774
abbreviation is_unit :: "'a \<Rightarrow> bool"
wenzelm@63325
   775
  where "is_unit a \<equiv> a dvd 1"
haftmann@60517
   776
wenzelm@63325
   777
lemma not_is_unit_0 [simp]: "\<not> is_unit 0"
haftmann@60517
   778
  by simp
haftmann@60517
   779
wenzelm@63325
   780
lemma unit_imp_dvd [dest]: "is_unit b \<Longrightarrow> b dvd a"
haftmann@60517
   781
  by (rule dvd_trans [of _ 1]) simp_all
haftmann@60517
   782
haftmann@60517
   783
lemma unit_dvdE:
haftmann@60517
   784
  assumes "is_unit a"
haftmann@60517
   785
  obtains c where "a \<noteq> 0" and "b = a * c"
haftmann@60517
   786
proof -
haftmann@60517
   787
  from assms have "a dvd b" by auto
haftmann@60517
   788
  then obtain c where "b = a * c" ..
haftmann@60517
   789
  moreover from assms have "a \<noteq> 0" by auto
haftmann@60517
   790
  ultimately show thesis using that by blast
haftmann@60517
   791
qed
haftmann@60517
   792
wenzelm@63325
   793
lemma dvd_unit_imp_unit: "a dvd b \<Longrightarrow> is_unit b \<Longrightarrow> is_unit a"
haftmann@60517
   794
  by (rule dvd_trans)
haftmann@60517
   795
haftmann@60517
   796
lemma unit_div_1_unit [simp, intro]:
haftmann@60517
   797
  assumes "is_unit a"
haftmann@60517
   798
  shows "is_unit (1 div a)"
haftmann@60517
   799
proof -
haftmann@60517
   800
  from assms have "1 = 1 div a * a" by simp
haftmann@60517
   801
  then show "is_unit (1 div a)" by (rule dvdI)
haftmann@60517
   802
qed
haftmann@60517
   803
haftmann@60517
   804
lemma is_unitE [elim?]:
haftmann@60517
   805
  assumes "is_unit a"
haftmann@60517
   806
  obtains b where "a \<noteq> 0" and "b \<noteq> 0"
haftmann@60517
   807
    and "is_unit b" and "1 div a = b" and "1 div b = a"
haftmann@60517
   808
    and "a * b = 1" and "c div a = c * b"
haftmann@60517
   809
proof (rule that)
wenzelm@63040
   810
  define b where "b = 1 div a"
haftmann@60517
   811
  then show "1 div a = b" by simp
wenzelm@63325
   812
  from assms b_def show "is_unit b" by simp
wenzelm@63325
   813
  with assms show "a \<noteq> 0" and "b \<noteq> 0" by auto
wenzelm@63325
   814
  from assms b_def show "a * b = 1" by simp
haftmann@60517
   815
  then have "1 = a * b" ..
wenzelm@60758
   816
  with b_def \<open>b \<noteq> 0\<close> show "1 div b = a" by simp
wenzelm@63325
   817
  from assms have "a dvd c" ..
haftmann@60517
   818
  then obtain d where "c = a * d" ..
wenzelm@60758
   819
  with \<open>a \<noteq> 0\<close> \<open>a * b = 1\<close> show "c div a = c * b"
haftmann@60517
   820
    by (simp add: mult.assoc mult.left_commute [of a])
haftmann@60517
   821
qed
haftmann@60517
   822
wenzelm@63325
   823
lemma unit_prod [intro]: "is_unit a \<Longrightarrow> is_unit b \<Longrightarrow> is_unit (a * b)"
lp15@60562
   824
  by (subst mult_1_left [of 1, symmetric]) (rule mult_dvd_mono)
lp15@60562
   825
wenzelm@63325
   826
lemma is_unit_mult_iff: "is_unit (a * b) \<longleftrightarrow> is_unit a \<and> is_unit b"
haftmann@62366
   827
  by (auto dest: dvd_mult_left dvd_mult_right)
haftmann@62366
   828
wenzelm@63325
   829
lemma unit_div [intro]: "is_unit a \<Longrightarrow> is_unit b \<Longrightarrow> is_unit (a div b)"
haftmann@60517
   830
  by (erule is_unitE [of b a]) (simp add: ac_simps unit_prod)
haftmann@60517
   831
haftmann@60517
   832
lemma mult_unit_dvd_iff:
haftmann@60517
   833
  assumes "is_unit b"
haftmann@60517
   834
  shows "a * b dvd c \<longleftrightarrow> a dvd c"
haftmann@60517
   835
proof
haftmann@60517
   836
  assume "a * b dvd c"
haftmann@60517
   837
  with assms show "a dvd c"
haftmann@60517
   838
    by (simp add: dvd_mult_left)
haftmann@60517
   839
next
haftmann@60517
   840
  assume "a dvd c"
haftmann@60517
   841
  then obtain k where "c = a * k" ..
haftmann@60517
   842
  with assms have "c = (a * b) * (1 div b * k)"
haftmann@60517
   843
    by (simp add: mult_ac)
haftmann@60517
   844
  then show "a * b dvd c" by (rule dvdI)
haftmann@60517
   845
qed
haftmann@60517
   846
haftmann@60517
   847
lemma dvd_mult_unit_iff:
haftmann@60517
   848
  assumes "is_unit b"
haftmann@60517
   849
  shows "a dvd c * b \<longleftrightarrow> a dvd c"
haftmann@60517
   850
proof
haftmann@60517
   851
  assume "a dvd c * b"
haftmann@60517
   852
  with assms have "c * b dvd c * (b * (1 div b))"
haftmann@60517
   853
    by (subst mult_assoc [symmetric]) simp
wenzelm@63325
   854
  also from assms have "b * (1 div b) = 1"
wenzelm@63325
   855
    by (rule is_unitE) simp
haftmann@60517
   856
  finally have "c * b dvd c" by simp
wenzelm@60758
   857
  with \<open>a dvd c * b\<close> show "a dvd c" by (rule dvd_trans)
haftmann@60517
   858
next
haftmann@60517
   859
  assume "a dvd c"
haftmann@60517
   860
  then show "a dvd c * b" by simp
haftmann@60517
   861
qed
haftmann@60517
   862
wenzelm@63325
   863
lemma div_unit_dvd_iff: "is_unit b \<Longrightarrow> a div b dvd c \<longleftrightarrow> a dvd c"
haftmann@60517
   864
  by (erule is_unitE [of _ a]) (auto simp add: mult_unit_dvd_iff)
haftmann@60517
   865
wenzelm@63325
   866
lemma dvd_div_unit_iff: "is_unit b \<Longrightarrow> a dvd c div b \<longleftrightarrow> a dvd c"
haftmann@60517
   867
  by (erule is_unitE [of _ c]) (simp add: dvd_mult_unit_iff)
haftmann@60517
   868
haftmann@60517
   869
lemmas unit_dvd_iff = mult_unit_dvd_iff div_unit_dvd_iff
wenzelm@63325
   870
  dvd_mult_unit_iff dvd_div_unit_iff  (* FIXME consider named_theorems *)
haftmann@60517
   871
wenzelm@63325
   872
lemma unit_mult_div_div [simp]: "is_unit a \<Longrightarrow> b * (1 div a) = b div a"
haftmann@60517
   873
  by (erule is_unitE [of _ b]) simp
haftmann@60517
   874
wenzelm@63325
   875
lemma unit_div_mult_self [simp]: "is_unit a \<Longrightarrow> b div a * a = b"
haftmann@60517
   876
  by (rule dvd_div_mult_self) auto
haftmann@60517
   877
wenzelm@63325
   878
lemma unit_div_1_div_1 [simp]: "is_unit a \<Longrightarrow> 1 div (1 div a) = a"
haftmann@60517
   879
  by (erule is_unitE) simp
haftmann@60517
   880
wenzelm@63325
   881
lemma unit_div_mult_swap: "is_unit c \<Longrightarrow> a * (b div c) = (a * b) div c"
haftmann@60517
   882
  by (erule unit_dvdE [of _ b]) (simp add: mult.left_commute [of _ c])
haftmann@60517
   883
wenzelm@63325
   884
lemma unit_div_commute: "is_unit b \<Longrightarrow> (a div b) * c = (a * c) div b"
haftmann@60517
   885
  using unit_div_mult_swap [of b c a] by (simp add: ac_simps)
haftmann@60517
   886
wenzelm@63325
   887
lemma unit_eq_div1: "is_unit b \<Longrightarrow> a div b = c \<longleftrightarrow> a = c * b"
haftmann@60517
   888
  by (auto elim: is_unitE)
haftmann@60517
   889
wenzelm@63325
   890
lemma unit_eq_div2: "is_unit b \<Longrightarrow> a = c div b \<longleftrightarrow> a * b = c"
haftmann@60517
   891
  using unit_eq_div1 [of b c a] by auto
haftmann@60517
   892
wenzelm@63325
   893
lemma unit_mult_left_cancel: "is_unit a \<Longrightarrow> a * b = a * c \<longleftrightarrow> b = c"
wenzelm@63325
   894
  using mult_cancel_left [of a b c] by auto
haftmann@60517
   895
wenzelm@63325
   896
lemma unit_mult_right_cancel: "is_unit a \<Longrightarrow> b * a = c * a \<longleftrightarrow> b = c"
haftmann@60517
   897
  using unit_mult_left_cancel [of a b c] by (auto simp add: ac_simps)
haftmann@60517
   898
haftmann@60517
   899
lemma unit_div_cancel:
haftmann@60517
   900
  assumes "is_unit a"
haftmann@60517
   901
  shows "b div a = c div a \<longleftrightarrow> b = c"
haftmann@60517
   902
proof -
haftmann@60517
   903
  from assms have "is_unit (1 div a)" by simp
haftmann@60517
   904
  then have "b * (1 div a) = c * (1 div a) \<longleftrightarrow> b = c"
haftmann@60517
   905
    by (rule unit_mult_right_cancel)
haftmann@60517
   906
  with assms show ?thesis by simp
haftmann@60517
   907
qed
lp15@60562
   908
haftmann@60570
   909
lemma is_unit_div_mult2_eq:
haftmann@60570
   910
  assumes "is_unit b" and "is_unit c"
haftmann@60570
   911
  shows "a div (b * c) = a div b div c"
haftmann@60570
   912
proof -
wenzelm@63325
   913
  from assms have "is_unit (b * c)"
wenzelm@63325
   914
    by (simp add: unit_prod)
haftmann@60570
   915
  then have "b * c dvd a"
haftmann@60570
   916
    by (rule unit_imp_dvd)
haftmann@60570
   917
  then show ?thesis
haftmann@60570
   918
    by (rule dvd_div_mult2_eq)
haftmann@60570
   919
qed
haftmann@60570
   920
lp15@60562
   921
lemmas unit_simps = mult_unit_dvd_iff div_unit_dvd_iff dvd_mult_unit_iff
haftmann@60517
   922
  dvd_div_unit_iff unit_div_mult_swap unit_div_commute
lp15@60562
   923
  unit_mult_left_cancel unit_mult_right_cancel unit_div_cancel
haftmann@60517
   924
  unit_eq_div1 unit_eq_div2
haftmann@60517
   925
haftmann@60685
   926
lemma is_unit_divide_mult_cancel_left:
haftmann@60685
   927
  assumes "a \<noteq> 0" and "is_unit b"
haftmann@60685
   928
  shows "a div (a * b) = 1 div b"
haftmann@60685
   929
proof -
haftmann@60685
   930
  from assms have "a div (a * b) = a div a div b"
haftmann@60685
   931
    by (simp add: mult_unit_dvd_iff dvd_div_mult2_eq)
haftmann@60685
   932
  with assms show ?thesis by simp
haftmann@60685
   933
qed
haftmann@60685
   934
haftmann@60685
   935
lemma is_unit_divide_mult_cancel_right:
haftmann@60685
   936
  assumes "a \<noteq> 0" and "is_unit b"
haftmann@60685
   937
  shows "a div (b * a) = 1 div b"
haftmann@60685
   938
  using assms is_unit_divide_mult_cancel_left [of a b] by (simp add: ac_simps)
haftmann@60685
   939
haftmann@60685
   940
end
haftmann@60685
   941
haftmann@60685
   942
class normalization_semidom = algebraic_semidom +
haftmann@60685
   943
  fixes normalize :: "'a \<Rightarrow> 'a"
haftmann@60685
   944
    and unit_factor :: "'a \<Rightarrow> 'a"
haftmann@60685
   945
  assumes unit_factor_mult_normalize [simp]: "unit_factor a * normalize a = a"
haftmann@60685
   946
  assumes normalize_0 [simp]: "normalize 0 = 0"
haftmann@60685
   947
    and unit_factor_0 [simp]: "unit_factor 0 = 0"
haftmann@60685
   948
  assumes is_unit_normalize:
haftmann@60685
   949
    "is_unit a  \<Longrightarrow> normalize a = 1"
hoelzl@62376
   950
  assumes unit_factor_is_unit [iff]:
haftmann@60685
   951
    "a \<noteq> 0 \<Longrightarrow> is_unit (unit_factor a)"
haftmann@60685
   952
  assumes unit_factor_mult: "unit_factor (a * b) = unit_factor a * unit_factor b"
haftmann@60685
   953
begin
haftmann@60685
   954
haftmann@60688
   955
text \<open>
haftmann@60688
   956
  Class @{class normalization_semidom} cultivates the idea that
haftmann@60688
   957
  each integral domain can be split into equivalence classes
haftmann@60688
   958
  whose representants are associated, i.e. divide each other.
haftmann@60688
   959
  @{const normalize} specifies a canonical representant for each equivalence
haftmann@60688
   960
  class.  The rationale behind this is that it is easier to reason about equality
haftmann@60688
   961
  than equivalences, hence we prefer to think about equality of normalized
haftmann@60688
   962
  values rather than associated elements.
haftmann@60688
   963
\<close>
haftmann@60688
   964
wenzelm@63325
   965
lemma unit_factor_dvd [simp]: "a \<noteq> 0 \<Longrightarrow> unit_factor a dvd b"
haftmann@60685
   966
  by (rule unit_imp_dvd) simp
haftmann@60685
   967
wenzelm@63325
   968
lemma unit_factor_self [simp]: "unit_factor a dvd a"
hoelzl@62376
   969
  by (cases "a = 0") simp_all
hoelzl@62376
   970
wenzelm@63325
   971
lemma normalize_mult_unit_factor [simp]: "normalize a * unit_factor a = a"
haftmann@60685
   972
  using unit_factor_mult_normalize [of a] by (simp add: ac_simps)
haftmann@60685
   973
wenzelm@63325
   974
lemma normalize_eq_0_iff [simp]: "normalize 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
wenzelm@63325
   985
lemma unit_factor_eq_0_iff [simp]: "unit_factor a = 0 \<longleftrightarrow> a = 0"
wenzelm@63325
   986
  (is "?P \<longleftrightarrow> ?Q")
haftmann@60685
   987
proof
haftmann@60685
   988
  assume ?P
haftmann@60685
   989
  moreover have "unit_factor a * normalize a = a" by simp
hoelzl@62376
   990
  ultimately show ?Q by simp
haftmann@60685
   991
next
wenzelm@63325
   992
  assume ?Q
wenzelm@63325
   993
  then show ?P by simp
haftmann@60685
   994
qed
haftmann@60685
   995
haftmann@60685
   996
lemma is_unit_unit_factor:
wenzelm@63325
   997
  assumes "is_unit a"
wenzelm@63325
   998
  shows "unit_factor a = a"
hoelzl@62376
   999
proof -
haftmann@60685
  1000
  from assms have "normalize a = 1" by (rule is_unit_normalize)
haftmann@60685
  1001
  moreover from unit_factor_mult_normalize have "unit_factor a * normalize a = a" .
haftmann@60685
  1002
  ultimately show ?thesis by simp
haftmann@60685
  1003
qed
haftmann@60685
  1004
wenzelm@63325
  1005
lemma unit_factor_1 [simp]: "unit_factor 1 = 1"
haftmann@60685
  1006
  by (rule is_unit_unit_factor) simp
haftmann@60685
  1007
wenzelm@63325
  1008
lemma normalize_1 [simp]: "normalize 1 = 1"
haftmann@60685
  1009
  by (rule is_unit_normalize) simp
haftmann@60685
  1010
wenzelm@63325
  1011
lemma normalize_1_iff: "normalize a = 1 \<longleftrightarrow> is_unit a"
wenzelm@63325
  1012
  (is "?P \<longleftrightarrow> ?Q")
haftmann@60685
  1013
proof
wenzelm@63325
  1014
  assume ?Q
wenzelm@63325
  1015
  then show ?P by (rule is_unit_normalize)
haftmann@60685
  1016
next
haftmann@60685
  1017
  assume ?P
haftmann@60685
  1018
  then have "a \<noteq> 0" by auto
haftmann@60685
  1019
  from \<open>?P\<close> have "unit_factor a * normalize a = unit_factor a * 1"
haftmann@60685
  1020
    by simp
haftmann@60685
  1021
  then have "unit_factor a = a"
haftmann@60685
  1022
    by simp
haftmann@60685
  1023
  moreover have "is_unit (unit_factor a)"
haftmann@60685
  1024
    using \<open>a \<noteq> 0\<close> by simp
haftmann@60685
  1025
  ultimately show ?Q by simp
haftmann@60685
  1026
qed
hoelzl@62376
  1027
wenzelm@63325
  1028
lemma div_normalize [simp]: "a div normalize a = unit_factor a"
haftmann@60685
  1029
proof (cases "a = 0")
wenzelm@63325
  1030
  case True
wenzelm@63325
  1031
  then show ?thesis by simp
haftmann@60685
  1032
next
wenzelm@63325
  1033
  case False
wenzelm@63325
  1034
  then have "normalize a \<noteq> 0" by simp
haftmann@60685
  1035
  with nonzero_mult_divide_cancel_right
haftmann@60685
  1036
  have "unit_factor a * normalize a div normalize a = unit_factor a" by blast
haftmann@60685
  1037
  then show ?thesis by simp
haftmann@60685
  1038
qed
haftmann@60685
  1039
wenzelm@63325
  1040
lemma div_unit_factor [simp]: "a div unit_factor a = normalize a"
haftmann@60685
  1041
proof (cases "a = 0")
wenzelm@63325
  1042
  case True
wenzelm@63325
  1043
  then show ?thesis by simp
haftmann@60685
  1044
next
wenzelm@63325
  1045
  case False
wenzelm@63325
  1046
  then have "unit_factor a \<noteq> 0" by simp
haftmann@60685
  1047
  with nonzero_mult_divide_cancel_left
haftmann@60685
  1048
  have "unit_factor a * normalize a div unit_factor a = normalize a" by blast
haftmann@60685
  1049
  then show ?thesis by simp
haftmann@60685
  1050
qed
haftmann@60685
  1051
wenzelm@63325
  1052
lemma normalize_div [simp]: "normalize a div a = 1 div unit_factor a"
haftmann@60685
  1053
proof (cases "a = 0")
wenzelm@63325
  1054
  case True
wenzelm@63325
  1055
  then show ?thesis by simp
haftmann@60685
  1056
next
haftmann@60685
  1057
  case False
haftmann@60685
  1058
  have "normalize a div a = normalize a div (unit_factor a * normalize a)"
haftmann@60685
  1059
    by simp
haftmann@60685
  1060
  also have "\<dots> = 1 div unit_factor a"
haftmann@60685
  1061
    using False by (subst is_unit_divide_mult_cancel_right) simp_all
haftmann@60685
  1062
  finally show ?thesis .
haftmann@60685
  1063
qed
haftmann@60685
  1064
wenzelm@63325
  1065
lemma mult_one_div_unit_factor [simp]: "a * (1 div unit_factor b) = a div unit_factor b"
haftmann@60685
  1066
  by (cases "b = 0") simp_all
haftmann@60685
  1067
wenzelm@63325
  1068
lemma normalize_mult: "normalize (a * b) = normalize a * normalize b"
haftmann@60685
  1069
proof (cases "a = 0 \<or> b = 0")
wenzelm@63325
  1070
  case True
wenzelm@63325
  1071
  then show ?thesis by auto
haftmann@60685
  1072
next
haftmann@60685
  1073
  case False
haftmann@60685
  1074
  from unit_factor_mult_normalize have "unit_factor (a * b) * normalize (a * b) = a * b" .
wenzelm@63325
  1075
  then have "normalize (a * b) = a * b div unit_factor (a * b)"
wenzelm@63325
  1076
    by simp
wenzelm@63325
  1077
  also have "\<dots> = a * b div unit_factor (b * a)"
wenzelm@63325
  1078
    by (simp add: ac_simps)
haftmann@60685
  1079
  also have "\<dots> = a * b div unit_factor b div unit_factor a"
haftmann@60685
  1080
    using False by (simp add: unit_factor_mult is_unit_div_mult2_eq [symmetric])
haftmann@60685
  1081
  also have "\<dots> = a * (b div unit_factor b) div unit_factor a"
haftmann@60685
  1082
    using False by (subst unit_div_mult_swap) simp_all
haftmann@60685
  1083
  also have "\<dots> = normalize a * normalize b"
wenzelm@63325
  1084
    using False
wenzelm@63325
  1085
    by (simp add: mult.commute [of a] mult.commute [of "normalize a"] unit_div_mult_swap [symmetric])
haftmann@60685
  1086
  finally show ?thesis .
haftmann@60685
  1087
qed
hoelzl@62376
  1088
wenzelm@63325
  1089
lemma unit_factor_idem [simp]: "unit_factor (unit_factor a) = unit_factor a"
haftmann@60685
  1090
  by (cases "a = 0") (auto intro: is_unit_unit_factor)
haftmann@60685
  1091
wenzelm@63325
  1092
lemma normalize_unit_factor [simp]: "a \<noteq> 0 \<Longrightarrow> normalize (unit_factor a) = 1"
haftmann@60685
  1093
  by (rule is_unit_normalize) simp
hoelzl@62376
  1094
wenzelm@63325
  1095
lemma normalize_idem [simp]: "normalize (normalize a) = normalize a"
haftmann@60685
  1096
proof (cases "a = 0")
wenzelm@63325
  1097
  case True
wenzelm@63325
  1098
  then show ?thesis by simp
haftmann@60685
  1099
next
haftmann@60685
  1100
  case False
wenzelm@63325
  1101
  have "normalize a = normalize (unit_factor a * normalize a)"
wenzelm@63325
  1102
    by simp
haftmann@60685
  1103
  also have "\<dots> = normalize (unit_factor a) * normalize (normalize a)"
haftmann@60685
  1104
    by (simp only: normalize_mult)
wenzelm@63325
  1105
  finally show ?thesis
wenzelm@63325
  1106
    using False by simp_all
haftmann@60685
  1107
qed
haftmann@60685
  1108
haftmann@60685
  1109
lemma unit_factor_normalize [simp]:
haftmann@60685
  1110
  assumes "a \<noteq> 0"
haftmann@60685
  1111
  shows "unit_factor (normalize a) = 1"
haftmann@60685
  1112
proof -
wenzelm@63325
  1113
  from assms have *: "normalize a \<noteq> 0"
wenzelm@63325
  1114
    by simp
haftmann@60685
  1115
  have "unit_factor (normalize a) * normalize (normalize a) = normalize a"
haftmann@60685
  1116
    by (simp only: unit_factor_mult_normalize)
haftmann@60685
  1117
  then have "unit_factor (normalize a) * normalize a = normalize a"
haftmann@60685
  1118
    by simp
wenzelm@63325
  1119
  with * have "unit_factor (normalize a) * normalize a div normalize a = normalize a div normalize a"
haftmann@60685
  1120
    by simp
wenzelm@63325
  1121
  with * show ?thesis
wenzelm@63325
  1122
    by simp
haftmann@60685
  1123
qed
haftmann@60685
  1124
haftmann@60685
  1125
lemma dvd_unit_factor_div:
haftmann@60685
  1126
  assumes "b dvd a"
haftmann@60685
  1127
  shows "unit_factor (a div b) = unit_factor a div unit_factor b"
haftmann@60685
  1128
proof -
haftmann@60685
  1129
  from assms have "a = a div b * b"
haftmann@60685
  1130
    by simp
haftmann@60685
  1131
  then have "unit_factor a = unit_factor (a div b * b)"
haftmann@60685
  1132
    by simp
haftmann@60685
  1133
  then show ?thesis
haftmann@60685
  1134
    by (cases "b = 0") (simp_all add: unit_factor_mult)
haftmann@60685
  1135
qed
haftmann@60685
  1136
haftmann@60685
  1137
lemma dvd_normalize_div:
haftmann@60685
  1138
  assumes "b dvd a"
haftmann@60685
  1139
  shows "normalize (a div b) = normalize a div normalize b"
haftmann@60685
  1140
proof -
haftmann@60685
  1141
  from assms have "a = a div b * b"
haftmann@60685
  1142
    by simp
haftmann@60685
  1143
  then have "normalize a = normalize (a div b * b)"
haftmann@60685
  1144
    by simp
haftmann@60685
  1145
  then show ?thesis
haftmann@60685
  1146
    by (cases "b = 0") (simp_all add: normalize_mult)
haftmann@60685
  1147
qed
haftmann@60685
  1148
wenzelm@63325
  1149
lemma normalize_dvd_iff [simp]: "normalize a dvd b \<longleftrightarrow> a dvd b"
haftmann@60685
  1150
proof -
haftmann@60685
  1151
  have "normalize a dvd b \<longleftrightarrow> unit_factor a * normalize a dvd b"
haftmann@60685
  1152
    using mult_unit_dvd_iff [of "unit_factor a" "normalize a" b]
haftmann@60685
  1153
      by (cases "a = 0") simp_all
haftmann@60685
  1154
  then show ?thesis by simp
haftmann@60685
  1155
qed
haftmann@60685
  1156
wenzelm@63325
  1157
lemma dvd_normalize_iff [simp]: "a dvd normalize b \<longleftrightarrow> a dvd b"
haftmann@60685
  1158
proof -
haftmann@60685
  1159
  have "a dvd normalize  b \<longleftrightarrow> a dvd normalize b * unit_factor b"
haftmann@60685
  1160
    using dvd_mult_unit_iff [of "unit_factor b" a "normalize b"]
haftmann@60685
  1161
      by (cases "b = 0") simp_all
haftmann@60685
  1162
  then show ?thesis by simp
haftmann@60685
  1163
qed
haftmann@60685
  1164
haftmann@60688
  1165
text \<open>
haftmann@60688
  1166
  We avoid an explicit definition of associated elements but prefer
haftmann@60688
  1167
  explicit normalisation instead.  In theory we could define an abbreviation
haftmann@60688
  1168
  like @{prop "associated a b \<longleftrightarrow> normalize a = normalize b"} but this is
haftmann@60688
  1169
  counterproductive without suggestive infix syntax, which we do not want
haftmann@60688
  1170
  to sacrifice for this purpose here.
haftmann@60688
  1171
\<close>
haftmann@60685
  1172
haftmann@60688
  1173
lemma associatedI:
haftmann@60688
  1174
  assumes "a dvd b" and "b dvd a"
haftmann@60688
  1175
  shows "normalize a = normalize b"
haftmann@60685
  1176
proof (cases "a = 0 \<or> b = 0")
wenzelm@63325
  1177
  case True
wenzelm@63325
  1178
  with assms show ?thesis by auto
haftmann@60685
  1179
next
haftmann@60685
  1180
  case False
haftmann@60688
  1181
  from \<open>a dvd b\<close> obtain c where b: "b = a * c" ..
haftmann@60688
  1182
  moreover from \<open>b dvd a\<close> obtain d where a: "a = b * d" ..
wenzelm@63325
  1183
  ultimately have "b * 1 = b * (c * d)"
wenzelm@63325
  1184
    by (simp add: ac_simps)
haftmann@60688
  1185
  with False have "1 = c * d"
haftmann@60688
  1186
    unfolding mult_cancel_left by simp
wenzelm@63325
  1187
  then have "is_unit c" and "is_unit d"
wenzelm@63325
  1188
    by auto
wenzelm@63325
  1189
  with a b show ?thesis
wenzelm@63325
  1190
    by (simp add: normalize_mult is_unit_normalize)
haftmann@60688
  1191
qed
haftmann@60688
  1192
wenzelm@63325
  1193
lemma associatedD1: "normalize a = normalize b \<Longrightarrow> a dvd b"
haftmann@60688
  1194
  using dvd_normalize_iff [of _ b, symmetric] normalize_dvd_iff [of a _, symmetric]
haftmann@60688
  1195
  by simp
haftmann@60688
  1196
wenzelm@63325
  1197
lemma associatedD2: "normalize a = normalize b \<Longrightarrow> b dvd a"
haftmann@60688
  1198
  using dvd_normalize_iff [of _ a, symmetric] normalize_dvd_iff [of b _, symmetric]
haftmann@60688
  1199
  by simp
haftmann@60688
  1200
wenzelm@63325
  1201
lemma associated_unit: "normalize a = normalize b \<Longrightarrow> is_unit a \<Longrightarrow> is_unit b"
haftmann@60688
  1202
  using dvd_unit_imp_unit by (auto dest!: associatedD1 associatedD2)
haftmann@60688
  1203
wenzelm@63325
  1204
lemma associated_iff_dvd: "normalize a = normalize b \<longleftrightarrow> a dvd b \<and> b dvd a"
wenzelm@63325
  1205
  (is "?P \<longleftrightarrow> ?Q")
haftmann@60688
  1206
proof
wenzelm@63325
  1207
  assume ?Q
wenzelm@63325
  1208
  then show ?P by (auto intro!: associatedI)
haftmann@60688
  1209
next
haftmann@60688
  1210
  assume ?P
haftmann@60688
  1211
  then have "unit_factor a * normalize a = unit_factor a * normalize b"
haftmann@60688
  1212
    by simp
haftmann@60688
  1213
  then have *: "normalize b * unit_factor a = a"
haftmann@60688
  1214
    by (simp add: ac_simps)
haftmann@60688
  1215
  show ?Q
haftmann@60688
  1216
  proof (cases "a = 0 \<or> b = 0")
wenzelm@63325
  1217
    case True
wenzelm@63325
  1218
    with \<open>?P\<close> show ?thesis by auto
haftmann@60685
  1219
  next
hoelzl@62376
  1220
    case False
haftmann@60688
  1221
    then have "b dvd normalize b * unit_factor a" and "normalize b * unit_factor a dvd b"
haftmann@60688
  1222
      by (simp_all add: mult_unit_dvd_iff dvd_mult_unit_iff)
haftmann@60688
  1223
    with * show ?thesis by simp
haftmann@60685
  1224
  qed
haftmann@60685
  1225
qed
haftmann@60685
  1226
haftmann@60685
  1227
lemma associated_eqI:
haftmann@60688
  1228
  assumes "a dvd b" and "b dvd a"
haftmann@60688
  1229
  assumes "normalize a = a" and "normalize b = b"
haftmann@60685
  1230
  shows "a = b"
haftmann@60688
  1231
proof -
haftmann@60688
  1232
  from assms have "normalize a = normalize b"
haftmann@60688
  1233
    unfolding associated_iff_dvd by simp
haftmann@60688
  1234
  with \<open>normalize a = a\<close> have "a = normalize b" by simp
haftmann@60688
  1235
  with \<open>normalize b = b\<close> show "a = b" by simp
haftmann@60685
  1236
qed
haftmann@60685
  1237
haftmann@60685
  1238
end
haftmann@60685
  1239
hoelzl@62376
  1240
class ordered_semiring = semiring + ordered_comm_monoid_add +
haftmann@38642
  1241
  assumes mult_left_mono: "a \<le> b \<Longrightarrow> 0 \<le> c \<Longrightarrow> c * a \<le> c * b"
haftmann@38642
  1242
  assumes mult_right_mono: "a \<le> b \<Longrightarrow> 0 \<le> c \<Longrightarrow> a * c \<le> b * c"
haftmann@25230
  1243
begin
haftmann@25230
  1244
wenzelm@63325
  1245
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
  1246
  apply (erule (1) mult_right_mono [THEN order_trans])
wenzelm@63325
  1247
  apply (erule (1) mult_left_mono)
wenzelm@63325
  1248
  done
haftmann@25230
  1249
wenzelm@63325
  1250
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
  1251
  apply (rule mult_mono)
wenzelm@63325
  1252
  apply (fast intro: order_trans)+
wenzelm@63325
  1253
  done
haftmann@25230
  1254
haftmann@25230
  1255
end
krauss@21199
  1256
hoelzl@62377
  1257
class ordered_semiring_0 = semiring_0 + ordered_semiring
haftmann@25267
  1258
begin
paulson@14268
  1259
wenzelm@63325
  1260
lemma mult_nonneg_nonneg [simp]: "0 \<le> a \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> a * b"
wenzelm@63325
  1261
  using mult_left_mono [of 0 b a] by simp
haftmann@25230
  1262
haftmann@25230
  1263
lemma mult_nonneg_nonpos: "0 \<le> a \<Longrightarrow> b \<le> 0 \<Longrightarrow> a * b \<le> 0"
wenzelm@63325
  1264
  using mult_left_mono [of b 0 a] by simp
huffman@30692
  1265
huffman@30692
  1266
lemma mult_nonpos_nonneg: "a \<le> 0 \<Longrightarrow> 0 \<le> b \<Longrightarrow> a * b \<le> 0"
wenzelm@63325
  1267
  using mult_right_mono [of a 0 b] by simp
huffman@30692
  1268
wenzelm@61799
  1269
text \<open>Legacy - use \<open>mult_nonpos_nonneg\<close>\<close>
lp15@60562
  1270
lemma mult_nonneg_nonpos2: "0 \<le> a \<Longrightarrow> b \<le> 0 \<Longrightarrow> b * a \<le> 0"
wenzelm@63325
  1271
  apply (drule mult_right_mono [of b 0])
wenzelm@63325
  1272
  apply auto
wenzelm@63325
  1273
  done
haftmann@25230
  1274
hoelzl@62378
  1275
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
  1276
  by (auto simp add: mult_nonneg_nonpos mult_nonneg_nonpos2)
haftmann@25230
  1277
haftmann@25230
  1278
end
haftmann@25230
  1279
hoelzl@62377
  1280
class ordered_cancel_semiring = ordered_semiring + cancel_comm_monoid_add
hoelzl@62377
  1281
begin
hoelzl@62377
  1282
hoelzl@62377
  1283
subclass semiring_0_cancel ..
hoelzl@62377
  1284
subclass ordered_semiring_0 ..
hoelzl@62377
  1285
hoelzl@62377
  1286
end
hoelzl@62377
  1287
haftmann@38642
  1288
class linordered_semiring = ordered_semiring + linordered_cancel_ab_semigroup_add
haftmann@25267
  1289
begin
haftmann@25230
  1290
haftmann@35028
  1291
subclass ordered_cancel_semiring ..
haftmann@35028
  1292
hoelzl@62376
  1293
subclass ordered_cancel_comm_monoid_add ..
haftmann@25304
  1294
Mathias@63456
  1295
subclass ordered_ab_semigroup_monoid_add_imp_le ..
Mathias@63456
  1296
wenzelm@63325
  1297
lemma mult_left_less_imp_less: "c * a < c * b \<Longrightarrow> 0 \<le> c \<Longrightarrow> a < b"
wenzelm@63325
  1298
  by (force simp add: mult_left_mono not_le [symmetric])
lp15@60562
  1299
wenzelm@63325
  1300
lemma mult_right_less_imp_less: "a * c < b * c \<Longrightarrow> 0 \<le> c \<Longrightarrow> a < b"
wenzelm@63325
  1301
  by (force simp add: mult_right_mono not_le [symmetric])
obua@23521
  1302
haftmann@25186
  1303
end
haftmann@25152
  1304
haftmann@35043
  1305
class linordered_semiring_1 = linordered_semiring + semiring_1
hoelzl@36622
  1306
begin
hoelzl@36622
  1307
hoelzl@36622
  1308
lemma convex_bound_le:
hoelzl@36622
  1309
  assumes "x \<le> a" "y \<le> a" "0 \<le> u" "0 \<le> v" "u + v = 1"
hoelzl@36622
  1310
  shows "u * x + v * y \<le> a"
hoelzl@36622
  1311
proof-
hoelzl@36622
  1312
  from assms have "u * x + v * y \<le> u * a + v * a"
hoelzl@36622
  1313
    by (simp add: add_mono mult_left_mono)
wenzelm@63325
  1314
  with assms show ?thesis
wenzelm@63325
  1315
    unfolding distrib_right[symmetric] by simp
hoelzl@36622
  1316
qed
hoelzl@36622
  1317
hoelzl@36622
  1318
end
haftmann@35043
  1319
haftmann@35043
  1320
class linordered_semiring_strict = semiring + comm_monoid_add + linordered_cancel_ab_semigroup_add +
haftmann@25062
  1321
  assumes mult_strict_left_mono: "a < b \<Longrightarrow> 0 < c \<Longrightarrow> c * a < c * b"
haftmann@25062
  1322
  assumes mult_strict_right_mono: "a < b \<Longrightarrow> 0 < c \<Longrightarrow> a * c < b * c"
haftmann@25267
  1323
begin
paulson@14341
  1324
huffman@27516
  1325
subclass semiring_0_cancel ..
obua@14940
  1326
haftmann@35028
  1327
subclass linordered_semiring
haftmann@28823
  1328
proof
huffman@23550
  1329
  fix a b c :: 'a
huffman@23550
  1330
  assume A: "a \<le> b" "0 \<le> c"
huffman@23550
  1331
  from A show "c * a \<le> c * b"
haftmann@25186
  1332
    unfolding le_less
haftmann@25186
  1333
    using mult_strict_left_mono by (cases "c = 0") auto
huffman@23550
  1334
  from A show "a * c \<le> b * c"
haftmann@25152
  1335
    unfolding le_less
haftmann@25186
  1336
    using mult_strict_right_mono by (cases "c = 0") auto
haftmann@25152
  1337
qed
haftmann@25152
  1338
wenzelm@63325
  1339
lemma mult_left_le_imp_le: "c * a \<le> c * b \<Longrightarrow> 0 < c \<Longrightarrow> a \<le> b"
wenzelm@63325
  1340
  by (auto simp add: mult_strict_left_mono _not_less [symmetric])
lp15@60562
  1341
wenzelm@63325
  1342
lemma mult_right_le_imp_le: "a * c \<le> b * c \<Longrightarrow> 0 < c \<Longrightarrow> a \<le> b"
wenzelm@63325
  1343
  by (auto simp add: mult_strict_right_mono not_less [symmetric])
haftmann@25230
  1344
nipkow@56544
  1345
lemma mult_pos_pos[simp]: "0 < a \<Longrightarrow> 0 < b \<Longrightarrow> 0 < a * b"
wenzelm@63325
  1346
  using mult_strict_left_mono [of 0 b a] by simp
huffman@30692
  1347
huffman@30692
  1348
lemma mult_pos_neg: "0 < a \<Longrightarrow> b < 0 \<Longrightarrow> a * b < 0"
wenzelm@63325
  1349
  using mult_strict_left_mono [of b 0 a] by simp
huffman@30692
  1350
huffman@30692
  1351
lemma mult_neg_pos: "a < 0 \<Longrightarrow> 0 < b \<Longrightarrow> a * b < 0"
wenzelm@63325
  1352
  using mult_strict_right_mono [of a 0 b] by simp
huffman@30692
  1353
wenzelm@61799
  1354
text \<open>Legacy - use \<open>mult_neg_pos\<close>\<close>
lp15@60562
  1355
lemma mult_pos_neg2: "0 < a \<Longrightarrow> b < 0 \<Longrightarrow> b * a < 0"
wenzelm@63325
  1356
  apply (drule mult_strict_right_mono [of b 0])
wenzelm@63325
  1357
  apply auto
wenzelm@63325
  1358
  done
haftmann@25230
  1359
wenzelm@63325
  1360
lemma zero_less_mult_pos: "0 < a * b \<Longrightarrow> 0 < a \<Longrightarrow> 0 < b"
wenzelm@63325
  1361
  apply (cases "b \<le> 0")
wenzelm@63325
  1362
   apply (auto simp add: le_less not_less)
wenzelm@63325
  1363
  apply (drule_tac mult_pos_neg [of a b])
wenzelm@63325
  1364
   apply (auto dest: less_not_sym)
wenzelm@63325
  1365
  done
haftmann@25230
  1366
wenzelm@63325
  1367
lemma zero_less_mult_pos2: "0 < b * a \<Longrightarrow> 0 < a \<Longrightarrow> 0 < b"
wenzelm@63325
  1368
  apply (cases "b \<le> 0")
wenzelm@63325
  1369
   apply (auto simp add: le_less not_less)
wenzelm@63325
  1370
  apply (drule_tac mult_pos_neg2 [of a b])
wenzelm@63325
  1371
   apply (auto dest: less_not_sym)
wenzelm@63325
  1372
  done
wenzelm@63325
  1373
wenzelm@63325
  1374
text \<open>Strict monotonicity in both arguments\<close>
haftmann@26193
  1375
lemma mult_strict_mono:
haftmann@26193
  1376
  assumes "a < b" and "c < d" and "0 < b" and "0 \<le> c"
haftmann@26193
  1377
  shows "a * c < b * d"
wenzelm@63325
  1378
  using assms
wenzelm@63325
  1379
  apply (cases "c = 0")
wenzelm@63325
  1380
  apply simp
haftmann@26193
  1381
  apply (erule mult_strict_right_mono [THEN less_trans])
wenzelm@63325
  1382
  apply (auto simp add: le_less)
wenzelm@63325
  1383
  apply (erule (1) mult_strict_left_mono)
haftmann@26193
  1384
  done
haftmann@26193
  1385
wenzelm@63325
  1386
text \<open>This weaker variant has more natural premises\<close>
haftmann@26193
  1387
lemma mult_strict_mono':
haftmann@26193
  1388
  assumes "a < b" and "c < d" and "0 \<le> a" and "0 \<le> c"
haftmann@26193
  1389
  shows "a * c < b * d"
wenzelm@63325
  1390
  by (rule mult_strict_mono) (insert assms, auto)
haftmann@26193
  1391
haftmann@26193
  1392
lemma mult_less_le_imp_less:
haftmann@26193
  1393
  assumes "a < b" and "c \<le> d" and "0 \<le> a" and "0 < c"
haftmann@26193
  1394
  shows "a * c < b * d"
wenzelm@63325
  1395
  using assms
wenzelm@63325
  1396
  apply (subgoal_tac "a * c < b * c")
haftmann@26193
  1397
  apply (erule less_le_trans)
haftmann@26193
  1398
  apply (erule mult_left_mono)
haftmann@26193
  1399
  apply simp
wenzelm@63325
  1400
  apply (erule (1) mult_strict_right_mono)
haftmann@26193
  1401
  done
haftmann@26193
  1402
haftmann@26193
  1403
lemma mult_le_less_imp_less:
haftmann@26193
  1404
  assumes "a \<le> b" and "c < d" and "0 < a" and "0 \<le> c"
haftmann@26193
  1405
  shows "a * c < b * d"
wenzelm@63325
  1406
  using assms
wenzelm@63325
  1407
  apply (subgoal_tac "a * c \<le> b * c")
haftmann@26193
  1408
  apply (erule le_less_trans)
haftmann@26193
  1409
  apply (erule mult_strict_left_mono)
haftmann@26193
  1410
  apply simp
wenzelm@63325
  1411
  apply (erule (1) mult_right_mono)
haftmann@26193
  1412
  done
haftmann@26193
  1413
haftmann@25230
  1414
end
haftmann@25230
  1415
haftmann@35097
  1416
class linordered_semiring_1_strict = linordered_semiring_strict + semiring_1
hoelzl@36622
  1417
begin
hoelzl@36622
  1418
hoelzl@36622
  1419
subclass linordered_semiring_1 ..
hoelzl@36622
  1420
hoelzl@36622
  1421
lemma convex_bound_lt:
hoelzl@36622
  1422
  assumes "x < a" "y < a" "0 \<le> u" "0 \<le> v" "u + v = 1"
hoelzl@36622
  1423
  shows "u * x + v * y < a"
hoelzl@36622
  1424
proof -
hoelzl@36622
  1425
  from assms have "u * x + v * y < u * a + v * a"
wenzelm@63325
  1426
    by (cases "u = 0") (auto intro!: add_less_le_mono mult_strict_left_mono mult_left_mono)
wenzelm@63325
  1427
  with assms show ?thesis
wenzelm@63325
  1428
    unfolding distrib_right[symmetric] by simp
hoelzl@36622
  1429
qed
hoelzl@36622
  1430
hoelzl@36622
  1431
end
haftmann@33319
  1432
lp15@60562
  1433
class ordered_comm_semiring = comm_semiring_0 + ordered_ab_semigroup_add +
haftmann@38642
  1434
  assumes comm_mult_left_mono: "a \<le> b \<Longrightarrow> 0 \<le> c \<Longrightarrow> c * a \<le> c * b"
haftmann@25186
  1435
begin
haftmann@25152
  1436
haftmann@35028
  1437
subclass ordered_semiring
haftmann@28823
  1438
proof
krauss@21199
  1439
  fix a b c :: 'a
huffman@23550
  1440
  assume "a \<le> b" "0 \<le> c"
wenzelm@63325
  1441
  then show "c * a \<le> c * b" by (rule comm_mult_left_mono)
wenzelm@63325
  1442
  then show "a * c \<le> b * c" by (simp only: mult.commute)
krauss@21199
  1443
qed
paulson@14265
  1444
haftmann@25267
  1445
end
haftmann@25267
  1446
haftmann@38642
  1447
class ordered_cancel_comm_semiring = ordered_comm_semiring + cancel_comm_monoid_add
haftmann@25267
  1448
begin
paulson@14265
  1449
haftmann@38642
  1450
subclass comm_semiring_0_cancel ..
haftmann@35028
  1451
subclass ordered_comm_semiring ..
haftmann@35028
  1452
subclass ordered_cancel_semiring ..
haftmann@25267
  1453
haftmann@25267
  1454
end
haftmann@25267
  1455
haftmann@35028
  1456
class linordered_comm_semiring_strict = comm_semiring_0 + linordered_cancel_ab_semigroup_add +
haftmann@38642
  1457
  assumes comm_mult_strict_left_mono: "a < b \<Longrightarrow> 0 < c \<Longrightarrow> c * a < c * b"
haftmann@25267
  1458
begin
haftmann@25267
  1459
haftmann@35043
  1460
subclass linordered_semiring_strict
haftmann@28823
  1461
proof
huffman@23550
  1462
  fix a b c :: 'a
huffman@23550
  1463
  assume "a < b" "0 < c"
wenzelm@63325
  1464
  then show "c * a < c * b" by (rule comm_mult_strict_left_mono)
wenzelm@63325
  1465
  then show "a * c < b * c" by (simp only: mult.commute)
huffman@23550
  1466
qed
paulson@14272
  1467
haftmann@35028
  1468
subclass ordered_cancel_comm_semiring
haftmann@28823
  1469
proof
huffman@23550
  1470
  fix a b c :: 'a
huffman@23550
  1471
  assume "a \<le> b" "0 \<le> c"
wenzelm@63325
  1472
  then show "c * a \<le> c * b"
haftmann@25186
  1473
    unfolding le_less
haftmann@26193
  1474
    using mult_strict_left_mono by (cases "c = 0") auto
huffman@23550
  1475
qed
paulson@14272
  1476
haftmann@25267
  1477
end
haftmann@25230
  1478
lp15@60562
  1479
class ordered_ring = ring + ordered_cancel_semiring
haftmann@25267
  1480
begin
haftmann@25230
  1481
haftmann@35028
  1482
subclass ordered_ab_group_add ..
paulson@14270
  1483
wenzelm@63325
  1484
lemma less_add_iff1: "a * e + c < b * e + d \<longleftrightarrow> (a - b) * e + c < d"
wenzelm@63325
  1485
  by (simp add: algebra_simps)
haftmann@25230
  1486
wenzelm@63325
  1487
lemma less_add_iff2: "a * e + c < b * e + d \<longleftrightarrow> c < (b - a) * e + d"
wenzelm@63325
  1488
  by (simp add: algebra_simps)
haftmann@25230
  1489
wenzelm@63325
  1490
lemma le_add_iff1: "a * e + c \<le> b * e + d \<longleftrightarrow> (a - b) * e + c \<le> d"
wenzelm@63325
  1491
  by (simp add: algebra_simps)
haftmann@25230
  1492
wenzelm@63325
  1493
lemma le_add_iff2: "a * e + c \<le> b * e + d \<longleftrightarrow> c \<le> (b - a) * e + d"
wenzelm@63325
  1494
  by (simp add: algebra_simps)
haftmann@25230
  1495
wenzelm@63325
  1496
lemma mult_left_mono_neg: "b \<le> a \<Longrightarrow> c \<le> 0 \<Longrightarrow> c * a \<le> c * b"
haftmann@36301
  1497
  apply (drule mult_left_mono [of _ _ "- c"])
huffman@35216
  1498
  apply simp_all
haftmann@25230
  1499
  done
haftmann@25230
  1500
wenzelm@63325
  1501
lemma mult_right_mono_neg: "b \<le> a \<Longrightarrow> c \<le> 0 \<Longrightarrow> a * c \<le> b * c"
haftmann@36301
  1502
  apply (drule mult_right_mono [of _ _ "- c"])
huffman@35216
  1503
  apply simp_all
haftmann@25230
  1504
  done
haftmann@25230
  1505
huffman@30692
  1506
lemma mult_nonpos_nonpos: "a \<le> 0 \<Longrightarrow> b \<le> 0 \<Longrightarrow> 0 \<le> a * b"
wenzelm@63325
  1507
  using mult_right_mono_neg [of a 0 b] by simp
haftmann@25230
  1508
wenzelm@63325
  1509
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
  1510
  by (auto simp add: mult_nonpos_nonpos)
haftmann@25186
  1511
haftmann@25186
  1512
end
paulson@14270
  1513
haftmann@35028
  1514
class linordered_ring = ring + linordered_semiring + linordered_ab_group_add + abs_if
haftmann@25304
  1515
begin
haftmann@25304
  1516
haftmann@35028
  1517
subclass ordered_ring ..
haftmann@35028
  1518
haftmann@35028
  1519
subclass ordered_ab_group_add_abs
haftmann@28823
  1520
proof
haftmann@25304
  1521
  fix a b
haftmann@25304
  1522
  show "\<bar>a + b\<bar> \<le> \<bar>a\<bar> + \<bar>b\<bar>"
wenzelm@63325
  1523
    by (auto simp add: abs_if not_le not_less algebra_simps
wenzelm@63325
  1524
        simp del: add.commute dest: add_neg_neg add_nonneg_nonneg)
huffman@35216
  1525
qed (auto simp add: abs_if)
haftmann@25304
  1526
huffman@35631
  1527
lemma zero_le_square [simp]: "0 \<le> a * a"
wenzelm@63325
  1528
  using linear [of 0 a] by (auto simp add: mult_nonpos_nonpos)
huffman@35631
  1529
huffman@35631
  1530
lemma not_square_less_zero [simp]: "\<not> (a * a < 0)"
huffman@35631
  1531
  by (simp add: not_less)
huffman@35631
  1532
wenzelm@61944
  1533
proposition abs_eq_iff: "\<bar>x\<bar> = \<bar>y\<bar> \<longleftrightarrow> x = y \<or> x = -y"
nipkow@62390
  1534
  by (auto simp add: abs_if split: if_split_asm)
lp15@61762
  1535
wenzelm@63325
  1536
lemma sum_squares_ge_zero: "0 \<le> x * x + y * y"
haftmann@62347
  1537
  by (intro add_nonneg_nonneg zero_le_square)
haftmann@62347
  1538
wenzelm@63325
  1539
lemma not_sum_squares_lt_zero: "\<not> x * x + y * y < 0"
haftmann@62347
  1540
  by (simp add: not_less sum_squares_ge_zero)
haftmann@62347
  1541
haftmann@25304
  1542
end
obua@23521
  1543
haftmann@35043
  1544
class linordered_ring_strict = ring + linordered_semiring_strict
haftmann@25304
  1545
  + ordered_ab_group_add + abs_if
haftmann@25230
  1546
begin
paulson@14348
  1547
haftmann@35028
  1548
subclass linordered_ring ..
haftmann@25304
  1549
huffman@30692
  1550
lemma mult_strict_left_mono_neg: "b < a \<Longrightarrow> c < 0 \<Longrightarrow> c * a < c * b"
wenzelm@63325
  1551
  using mult_strict_left_mono [of b a "- c"] by simp
huffman@30692
  1552
huffman@30692
  1553
lemma mult_strict_right_mono_neg: "b < a \<Longrightarrow> c < 0 \<Longrightarrow> a * c < b * c"
wenzelm@63325
  1554
  using mult_strict_right_mono [of b a "- c"] by simp
huffman@30692
  1555
huffman@30692
  1556
lemma mult_neg_neg: "a < 0 \<Longrightarrow> b < 0 \<Longrightarrow> 0 < a * b"
wenzelm@63325
  1557
  using mult_strict_right_mono_neg [of a 0 b] by simp
obua@14738
  1558
haftmann@25917
  1559
subclass ring_no_zero_divisors
haftmann@28823
  1560
proof
haftmann@25917
  1561
  fix a b
wenzelm@63325
  1562
  assume "a \<noteq> 0"
wenzelm@63325
  1563
  then have A: "a < 0 \<or> 0 < a" by (simp add: neq_iff)
wenzelm@63325
  1564
  assume "b \<noteq> 0"
wenzelm@63325
  1565
  then have B: "b < 0 \<or> 0 < b" by (simp add: neq_iff)
haftmann@25917
  1566
  have "a * b < 0 \<or> 0 < a * b"
haftmann@25917
  1567
  proof (cases "a < 0")
wenzelm@63325
  1568
    case A': True
wenzelm@63325
  1569
    show ?thesis
wenzelm@63325
  1570
    proof (cases "b < 0")
wenzelm@63325
  1571
      case True
wenzelm@63325
  1572
      with A' show ?thesis by (auto dest: mult_neg_neg)
haftmann@25917
  1573
    next
wenzelm@63325
  1574
      case False
wenzelm@63325
  1575
      with B have "0 < b" by auto
haftmann@25917
  1576
      with A' show ?thesis by (auto dest: mult_strict_right_mono)
haftmann@25917
  1577
    qed
haftmann@25917
  1578
  next
wenzelm@63325
  1579
    case False
wenzelm@63325
  1580
    with A have A': "0 < a" by auto
wenzelm@63325
  1581
    show ?thesis
wenzelm@63325
  1582
    proof (cases "b < 0")
wenzelm@63325
  1583
      case True
wenzelm@63325
  1584
      with A' show ?thesis
wenzelm@63325
  1585
        by (auto dest: mult_strict_right_mono_neg)
haftmann@25917
  1586
    next
wenzelm@63325
  1587
      case False
wenzelm@63325
  1588
      with B have "0 < b" by auto
nipkow@56544
  1589
      with A' show ?thesis by auto
haftmann@25917
  1590
    qed
haftmann@25917
  1591
  qed
wenzelm@63325
  1592
  then show "a * b \<noteq> 0"
wenzelm@63325
  1593
    by (simp add: neq_iff)
haftmann@25917
  1594
qed
haftmann@25304
  1595
hoelzl@56480
  1596
lemma zero_less_mult_iff: "0 < a * b \<longleftrightarrow> 0 < a \<and> 0 < b \<or> a < 0 \<and> b < 0"
hoelzl@56480
  1597
  by (cases a 0 b 0 rule: linorder_cases[case_product linorder_cases])
nipkow@56544
  1598
     (auto simp add: mult_neg_neg not_less le_less dest: zero_less_mult_pos zero_less_mult_pos2)
huffman@22990
  1599
hoelzl@56480
  1600
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
  1601
  by (auto simp add: eq_commute [of 0] le_less not_less zero_less_mult_iff)
paulson@14265
  1602
wenzelm@63325
  1603
lemma mult_less_0_iff: "a * b < 0 \<longleftrightarrow> 0 < a \<and> b < 0 \<or> a < 0 \<and> 0 < b"
wenzelm@63325
  1604
  using zero_less_mult_iff [of "- a" b] by auto
paulson@14265
  1605
wenzelm@63325
  1606
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
  1607
  using zero_le_mult_iff [of "- a" b] by auto
haftmann@25917
  1608
wenzelm@63325
  1609
text \<open>
wenzelm@63325
  1610
  Cancellation laws for @{term "c * a < c * b"} and @{term "a * c < b * c"},
wenzelm@63325
  1611
  also with the relations \<open>\<le>\<close> and equality.
wenzelm@63325
  1612
\<close>
haftmann@26193
  1613
wenzelm@63325
  1614
text \<open>
wenzelm@63325
  1615
  These ``disjunction'' versions produce two cases when the comparison is
wenzelm@63325
  1616
  an assumption, but effectively four when the comparison is a goal.
wenzelm@63325
  1617
\<close>
haftmann@26193
  1618
wenzelm@63325
  1619
lemma mult_less_cancel_right_disj: "a * c < b * c \<longleftrightarrow> 0 < c \<and> a < b \<or> c < 0 \<and>  b < a"
haftmann@26193
  1620
  apply (cases "c = 0")
wenzelm@63325
  1621
  apply (auto simp add: neq_iff mult_strict_right_mono mult_strict_right_mono_neg)
wenzelm@63325
  1622
  apply (auto simp add: not_less not_le [symmetric, of "a*c"] not_le [symmetric, of a])
haftmann@26193
  1623
  apply (erule_tac [!] notE)
wenzelm@63325
  1624
  apply (auto simp add: less_imp_le mult_right_mono mult_right_mono_neg)
haftmann@26193
  1625
  done
haftmann@26193
  1626
wenzelm@63325
  1627
lemma mult_less_cancel_left_disj: "c * a < c * b \<longleftrightarrow> 0 < c \<and> a < b \<or> c < 0 \<and>  b < a"
haftmann@26193
  1628
  apply (cases "c = 0")
wenzelm@63325
  1629
  apply (auto simp add: neq_iff mult_strict_left_mono mult_strict_left_mono_neg)
wenzelm@63325
  1630
  apply (auto simp add: not_less not_le [symmetric, of "c * a"] not_le [symmetric, of a])
haftmann@26193
  1631
  apply (erule_tac [!] notE)
wenzelm@63325
  1632
  apply (auto simp add: less_imp_le mult_left_mono mult_left_mono_neg)
haftmann@26193
  1633
  done
haftmann@26193
  1634
wenzelm@63325
  1635
text \<open>
wenzelm@63325
  1636
  The ``conjunction of implication'' lemmas produce two cases when the
wenzelm@63325
  1637
  comparison is a goal, but give four when the comparison is an assumption.
wenzelm@63325
  1638
\<close>
haftmann@26193
  1639
wenzelm@63325
  1640
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
  1641
  using mult_less_cancel_right_disj [of a c b] by auto
haftmann@26193
  1642
wenzelm@63325
  1643
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
  1644
  using mult_less_cancel_left_disj [of c a b] by auto
haftmann@26193
  1645
wenzelm@63325
  1646
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
  1647
  by (simp add: not_less [symmetric] mult_less_cancel_right_disj)
haftmann@26193
  1648
wenzelm@63325
  1649
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
  1650
  by (simp add: not_less [symmetric] mult_less_cancel_left_disj)
haftmann@26193
  1651
wenzelm@63325
  1652
lemma mult_le_cancel_left_pos: "0 < c \<Longrightarrow> c * a \<le> c * b \<longleftrightarrow> a \<le> b"
wenzelm@63325
  1653
  by (auto simp: mult_le_cancel_left)
nipkow@30649
  1654
wenzelm@63325
  1655
lemma mult_le_cancel_left_neg: "c < 0 \<Longrightarrow> c * a \<le> c * b \<longleftrightarrow> b \<le> a"
wenzelm@63325
  1656
  by (auto simp: mult_le_cancel_left)
nipkow@30649
  1657
wenzelm@63325
  1658
lemma mult_less_cancel_left_pos: "0 < c \<Longrightarrow> c * a < c * b \<longleftrightarrow> a < b"
wenzelm@63325
  1659
  by (auto simp: mult_less_cancel_left)
nipkow@30649
  1660
wenzelm@63325
  1661
lemma mult_less_cancel_left_neg: "c < 0 \<Longrightarrow> c * a < c * b \<longleftrightarrow> b < a"
wenzelm@63325
  1662
  by (auto simp: mult_less_cancel_left)
nipkow@30649
  1663
haftmann@25917
  1664
end
paulson@14265
  1665
huffman@30692
  1666
lemmas mult_sign_intros =
huffman@30692
  1667
  mult_nonneg_nonneg mult_nonneg_nonpos
huffman@30692
  1668
  mult_nonpos_nonneg mult_nonpos_nonpos
huffman@30692
  1669
  mult_pos_pos mult_pos_neg
huffman@30692
  1670
  mult_neg_pos mult_neg_neg
haftmann@25230
  1671
haftmann@35028
  1672
class ordered_comm_ring = comm_ring + ordered_comm_semiring
haftmann@25267
  1673
begin
haftmann@25230
  1674
haftmann@35028
  1675
subclass ordered_ring ..
haftmann@35028
  1676
subclass ordered_cancel_comm_semiring ..
haftmann@25230
  1677
haftmann@25267
  1678
end
haftmann@25230
  1679
hoelzl@62378
  1680
class zero_less_one = order + zero + one +
haftmann@25230
  1681
  assumes zero_less_one [simp]: "0 < 1"
hoelzl@62378
  1682
hoelzl@62378
  1683
class linordered_nonzero_semiring = ordered_comm_semiring + monoid_mult + linorder + zero_less_one
hoelzl@62378
  1684
begin
hoelzl@62378
  1685
hoelzl@62378
  1686
subclass zero_neq_one
wenzelm@63325
  1687
  by standard (insert zero_less_one, blast)
hoelzl@62378
  1688
hoelzl@62378
  1689
subclass comm_semiring_1
wenzelm@63325
  1690
  by standard (rule mult_1_left)
hoelzl@62378
  1691
hoelzl@62378
  1692
lemma zero_le_one [simp]: "0 \<le> 1"
wenzelm@63325
  1693
  by (rule zero_less_one [THEN less_imp_le])
hoelzl@62378
  1694
hoelzl@62378
  1695
lemma not_one_le_zero [simp]: "\<not> 1 \<le> 0"
wenzelm@63325
  1696
  by (simp add: not_le)
hoelzl@62378
  1697
hoelzl@62378
  1698
lemma not_one_less_zero [simp]: "\<not> 1 < 0"
wenzelm@63325
  1699
  by (simp add: not_less)
hoelzl@62378
  1700
hoelzl@62378
  1701
lemma mult_left_le: "c \<le> 1 \<Longrightarrow> 0 \<le> a \<Longrightarrow> a * c \<le> a"
hoelzl@62378
  1702
  using mult_left_mono[of c 1 a] by simp
hoelzl@62378
  1703
hoelzl@62378
  1704
lemma mult_le_one: "a \<le> 1 \<Longrightarrow> 0 \<le> b \<Longrightarrow> b \<le> 1 \<Longrightarrow> a * b \<le> 1"
hoelzl@62378
  1705
  using mult_mono[of a 1 b 1] by simp
hoelzl@62378
  1706
hoelzl@62378
  1707
lemma zero_less_two: "0 < 1 + 1"
hoelzl@62378
  1708
  using add_pos_pos[OF zero_less_one zero_less_one] .
hoelzl@62378
  1709
hoelzl@62378
  1710
end
hoelzl@62378
  1711
hoelzl@62378
  1712
class linordered_semidom = semidom + linordered_comm_semiring_strict + zero_less_one +
lp15@60562
  1713
  assumes le_add_diff_inverse2 [simp]: "b \<le> a \<Longrightarrow> a - b + b = a"
haftmann@25230
  1714
begin
haftmann@25230
  1715
wenzelm@63325
  1716
subclass linordered_nonzero_semiring ..
hoelzl@62378
  1717
wenzelm@60758
  1718
text \<open>Addition is the inverse of subtraction.\<close>
lp15@60562
  1719
lp15@60562
  1720
lemma le_add_diff_inverse [simp]: "b \<le> a \<Longrightarrow> b + (a - b) = a"
lp15@60562
  1721
  by (frule le_add_diff_inverse2) (simp add: add.commute)
lp15@60562
  1722
hoelzl@62378
  1723
lemma add_diff_inverse: "\<not> a < b \<Longrightarrow> b + (a - b) = a"
lp15@60562
  1724
  by simp
lp15@60615
  1725
wenzelm@63325
  1726
lemma add_le_imp_le_diff: "i + k \<le> n \<Longrightarrow> i \<le> n - k"
lp15@60615
  1727
  apply (subst add_le_cancel_right [where c=k, symmetric])
lp15@60615
  1728
  apply (frule le_add_diff_inverse2)
lp15@60615
  1729
  apply (simp only: add.assoc [symmetric])
wenzelm@63325
  1730
  using add_implies_diff apply fastforce
wenzelm@63325
  1731
  done
lp15@60615
  1732
hoelzl@62376
  1733
lemma add_le_add_imp_diff_le:
wenzelm@63325
  1734
  assumes 1: "i + k \<le> n"
wenzelm@63325
  1735
    and 2: "n \<le> j + k"
wenzelm@63325
  1736
  shows "i + k \<le> n \<Longrightarrow> n \<le> j + k \<Longrightarrow> n - k \<le> j"
lp15@60615
  1737
proof -
lp15@60615
  1738
  have "n - (i + k) + (i + k) = n"
wenzelm@63325
  1739
    using 1 by simp
lp15@60615
  1740
  moreover have "n - k = n - k - i + i"
wenzelm@63325
  1741
    using 1 by (simp add: add_le_imp_le_diff)
lp15@60615
  1742
  ultimately show ?thesis
wenzelm@63325
  1743
    using 2
lp15@60615
  1744
    apply (simp add: add.assoc [symmetric])
wenzelm@63325
  1745
    apply (rule add_le_imp_le_diff [of _ k "j + k", simplified add_diff_cancel_right'])
wenzelm@63325
  1746
    apply (simp add: add.commute diff_diff_add)
wenzelm@63325
  1747
    done
lp15@60615
  1748
qed
lp15@60615
  1749
wenzelm@63325
  1750
lemma less_1_mult: "1 < m \<Longrightarrow> 1 < n \<Longrightarrow> 1 < m * n"
hoelzl@62378
  1751
  using mult_strict_mono [of 1 m 1 n] by (simp add: less_trans [OF zero_less_one])
hoelzl@59000
  1752
haftmann@25230
  1753
end
haftmann@25230
  1754
hoelzl@62378
  1755
class linordered_idom =
hoelzl@62378
  1756
  comm_ring_1 + linordered_comm_semiring_strict + ordered_ab_group_add + abs_if + sgn_if
haftmann@25917
  1757
begin
haftmann@25917
  1758
hoelzl@36622
  1759
subclass linordered_semiring_1_strict ..
haftmann@35043
  1760
subclass linordered_ring_strict ..
haftmann@35028
  1761
subclass ordered_comm_ring ..
huffman@27516
  1762
subclass idom ..
haftmann@25917
  1763
haftmann@35028
  1764
subclass linordered_semidom
haftmann@28823
  1765
proof
haftmann@26193
  1766
  have "0 \<le> 1 * 1" by (rule zero_le_square)
wenzelm@63325
  1767
  then show "0 < 1" by (simp add: le_less)
wenzelm@63325
  1768
  show "b \<le> a \<Longrightarrow> a - b + b = a" for a b
lp15@60562
  1769
    by simp
lp15@60562
  1770
qed
haftmann@25917
  1771
haftmann@35028
  1772
lemma linorder_neqE_linordered_idom:
wenzelm@63325
  1773
  assumes "x \<noteq> y"
wenzelm@63325
  1774
  obtains "x < y" | "y < x"
haftmann@26193
  1775
  using assms by (rule neqE)
haftmann@26193
  1776
wenzelm@60758
  1777
text \<open>These cancellation simprules also produce two cases when the comparison is a goal.\<close>
haftmann@26274
  1778
wenzelm@63325
  1779
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
  1780
  using mult_le_cancel_right [of 1 c b] by simp
haftmann@26274
  1781
wenzelm@63325
  1782
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
  1783
  using mult_le_cancel_right [of a c 1] by simp
haftmann@26274
  1784
wenzelm@63325
  1785
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
  1786
  using mult_le_cancel_left [of c 1 b] by simp
haftmann@26274
  1787
wenzelm@63325
  1788
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
  1789
  using mult_le_cancel_left [of c a 1] by simp
haftmann@26274
  1790
wenzelm@63325
  1791
lemma mult_less_cancel_right1: "c < b * c \<longleftrightarrow> (0 \<le> c \<longrightarrow> 1 < b) \<and> (c \<le> 0 \<longrightarrow> b < 1)"
wenzelm@63325
  1792
  using mult_less_cancel_right [of 1 c b] by simp
haftmann@26274
  1793
wenzelm@63325
  1794
lemma mult_less_cancel_right2: "a * c < c \<longleftrightarrow> (0 \<le> c \<longrightarrow> a < 1) \<and> (c \<le> 0 \<longrightarrow> 1 < a)"
wenzelm@63325
  1795
  using mult_less_cancel_right [of a c 1] by simp
haftmann@26274
  1796
wenzelm@63325
  1797
lemma mult_less_cancel_left1: "c < c * b \<longleftrightarrow> (0 \<le> c \<longrightarrow> 1 < b) \<and> (c \<le> 0 \<longrightarrow> b < 1)"
wenzelm@63325
  1798
  using mult_less_cancel_left [of c 1 b] by simp
haftmann@26274
  1799
wenzelm@63325
  1800
lemma mult_less_cancel_left2: "c * a < c \<longleftrightarrow> (0 \<le> c \<longrightarrow> a < 1) \<and> (c \<le> 0 \<longrightarrow> 1 < a)"
wenzelm@63325
  1801
  using mult_less_cancel_left [of c a 1] by simp
haftmann@26274
  1802
wenzelm@63325
  1803
lemma sgn_sgn [simp]: "sgn (sgn a) = sgn a"
wenzelm@63325
  1804
  unfolding sgn_if by simp
haftmann@27651
  1805
wenzelm@63325
  1806
lemma sgn_0_0: "sgn a = 0 \<longleftrightarrow> a = 0"
wenzelm@63325
  1807
  unfolding sgn_if by simp
haftmann@27651
  1808
wenzelm@63325
  1809
lemma sgn_1_pos: "sgn a = 1 \<longleftrightarrow> a > 0"
wenzelm@63325
  1810
  unfolding sgn_if by simp
haftmann@27651
  1811
wenzelm@63325
  1812
lemma sgn_1_neg: "sgn a = - 1 \<longleftrightarrow> a < 0"
wenzelm@63325
  1813
  unfolding sgn_if by auto
haftmann@27651
  1814
wenzelm@63325
  1815
lemma sgn_pos [simp]: "0 < a \<Longrightarrow> sgn a = 1"
wenzelm@63325
  1816
  by (simp only: sgn_1_pos)
haftmann@29940
  1817
wenzelm@63325
  1818
lemma sgn_neg [simp]: "a < 0 \<Longrightarrow> sgn a = - 1"
wenzelm@63325
  1819
  by (simp only: sgn_1_neg)
haftmann@29940
  1820
wenzelm@63325
  1821
lemma sgn_times: "sgn (a * b) = sgn a * sgn b"
wenzelm@63325
  1822
  by (auto simp add: sgn_if zero_less_mult_iff)
haftmann@27651
  1823
haftmann@36301
  1824
lemma abs_sgn: "\<bar>k\<bar> = k * sgn k"
wenzelm@63325
  1825
  unfolding sgn_if abs_if by auto
nipkow@29700
  1826
wenzelm@63325
  1827
lemma sgn_greater [simp]: "0 < sgn a \<longleftrightarrow> 0 < a"
haftmann@29940
  1828
  unfolding sgn_if by auto
haftmann@29940
  1829
wenzelm@63325
  1830
lemma sgn_less [simp]: "sgn a < 0 \<longleftrightarrow> a < 0"
haftmann@29940
  1831
  unfolding sgn_if by auto
haftmann@29940
  1832
wenzelm@63325
  1833
lemma abs_sgn_eq: "\<bar>sgn a\<bar> = (if a = 0 then 0 else 1)"
haftmann@62347
  1834
  by (simp add: sgn_if)
haftmann@62347
  1835
haftmann@36301
  1836
lemma abs_dvd_iff [simp]: "\<bar>m\<bar> dvd k \<longleftrightarrow> m dvd k"
huffman@29949
  1837
  by (simp add: abs_if)
huffman@29949
  1838
haftmann@36301
  1839
lemma dvd_abs_iff [simp]: "m dvd \<bar>k\<bar> \<longleftrightarrow> m dvd k"
huffman@29949
  1840
  by (simp add: abs_if)
haftmann@29653
  1841
wenzelm@63325
  1842
lemma dvd_if_abs_eq: "\<bar>l\<bar> = \<bar>k\<bar> \<Longrightarrow> l dvd k"
wenzelm@63325
  1843
  by (subst abs_dvd_iff [symmetric]) simp
nipkow@33676
  1844
wenzelm@63325
  1845
text \<open>
wenzelm@63325
  1846
  The following lemmas can be proven in more general structures, but
wenzelm@63325
  1847
  are dangerous as simp rules in absence of @{thm neg_equal_zero},
wenzelm@63325
  1848
  @{thm neg_less_pos}, @{thm neg_less_eq_nonneg}.
wenzelm@63325
  1849
\<close>
haftmann@54489
  1850
wenzelm@63325
  1851
lemma equation_minus_iff_1 [simp, no_atp]: "1 = - a \<longleftrightarrow> a = - 1"
haftmann@54489
  1852
  by (fact equation_minus_iff)
haftmann@54489
  1853
wenzelm@63325
  1854
lemma minus_equation_iff_1 [simp, no_atp]: "- a = 1 \<longleftrightarrow> a = - 1"
haftmann@54489
  1855
  by (subst minus_equation_iff, auto)
haftmann@54489
  1856
wenzelm@63325
  1857
lemma le_minus_iff_1 [simp, no_atp]: "1 \<le> - b \<longleftrightarrow> b \<le> - 1"
haftmann@54489
  1858
  by (fact le_minus_iff)
haftmann@54489
  1859
wenzelm@63325
  1860
lemma minus_le_iff_1 [simp, no_atp]: "- a \<le> 1 \<longleftrightarrow> - 1 \<le> a"
haftmann@54489
  1861
  by (fact minus_le_iff)
haftmann@54489
  1862
wenzelm@63325
  1863
lemma less_minus_iff_1 [simp, no_atp]: "1 < - b \<longleftrightarrow> b < - 1"
haftmann@54489
  1864
  by (fact less_minus_iff)
haftmann@54489
  1865
wenzelm@63325
  1866
lemma minus_less_iff_1 [simp, no_atp]: "- a < 1 \<longleftrightarrow> - 1 < a"
haftmann@54489
  1867
  by (fact minus_less_iff)
haftmann@54489
  1868
haftmann@25917
  1869
end
haftmann@25230
  1870
wenzelm@60758
  1871
text \<open>Simprules for comparisons where common factors can be cancelled.\<close>
paulson@15234
  1872
blanchet@54147
  1873
lemmas mult_compare_simps =
wenzelm@63325
  1874
  mult_le_cancel_right mult_le_cancel_left
wenzelm@63325
  1875
  mult_le_cancel_right1 mult_le_cancel_right2
wenzelm@63325
  1876
  mult_le_cancel_left1 mult_le_cancel_left2
wenzelm@63325
  1877
  mult_less_cancel_right mult_less_cancel_left
wenzelm@63325
  1878
  mult_less_cancel_right1 mult_less_cancel_right2
wenzelm@63325
  1879
  mult_less_cancel_left1 mult_less_cancel_left2
wenzelm@63325
  1880
  mult_cancel_right mult_cancel_left
wenzelm@63325
  1881
  mult_cancel_right1 mult_cancel_right2
wenzelm@63325
  1882
  mult_cancel_left1 mult_cancel_left2
wenzelm@63325
  1883
paulson@15234
  1884
wenzelm@60758
  1885
text \<open>Reasoning about inequalities with division\<close>
avigad@16775
  1886
haftmann@35028
  1887
context linordered_semidom
haftmann@25193
  1888
begin
haftmann@25193
  1889
haftmann@25193
  1890
lemma less_add_one: "a < a + 1"
paulson@14293
  1891
proof -
haftmann@25193
  1892
  have "a + 0 < a + 1"
nipkow@23482
  1893
    by (blast intro: zero_less_one add_strict_left_mono)
wenzelm@63325
  1894
  then show ?thesis by simp
paulson@14293
  1895
qed
paulson@14293
  1896
haftmann@25193
  1897
end
paulson@14365
  1898
haftmann@36301
  1899
context linordered_idom
haftmann@36301
  1900
begin
paulson@15234
  1901
wenzelm@63325
  1902
lemma mult_right_le_one_le: "0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> x * y \<le> x"
haftmann@59833
  1903
  by (rule mult_left_le)
haftmann@36301
  1904
wenzelm@63325
  1905
lemma mult_left_le_one_le: "0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> y \<le> 1 \<Longrightarrow> y * x \<le> x"
haftmann@36301
  1906
  by (auto simp add: mult_le_cancel_right2)
haftmann@36301
  1907
haftmann@36301
  1908
end
haftmann@36301
  1909
wenzelm@60758
  1910
text \<open>Absolute Value\<close>
paulson@14293
  1911
haftmann@35028
  1912
context linordered_idom
haftmann@25304
  1913
begin
haftmann@25304
  1914
wenzelm@63325
  1915
lemma mult_sgn_abs: "sgn x * \<bar>x\<bar> = x"
haftmann@25304
  1916
  unfolding abs_if sgn_if by auto
haftmann@25304
  1917
wenzelm@63325
  1918
lemma abs_one [simp]: "\<bar>1\<bar> = 1"
huffman@44921
  1919
  by (simp add: abs_if)
haftmann@36301
  1920
haftmann@25304
  1921
end
nipkow@24491
  1922
haftmann@35028
  1923
class ordered_ring_abs = ordered_ring + ordered_ab_group_add_abs +
haftmann@25304
  1924
  assumes abs_eq_mult:
haftmann@25304
  1925
    "(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
  1926
haftmann@35028
  1927
context linordered_idom
haftmann@30961
  1928
begin
haftmann@30961
  1929
wenzelm@63325
  1930
subclass ordered_ring_abs
wenzelm@63325
  1931
  by standard (auto simp add: abs_if not_less mult_less_0_iff)
haftmann@30961
  1932
wenzelm@63325
  1933
lemma abs_mult: "\<bar>a * b\<bar> = \<bar>a\<bar> * \<bar>b\<bar>"
haftmann@30961
  1934
  by (rule abs_eq_mult) auto
haftmann@30961
  1935
wenzelm@63325
  1936
lemma abs_mult_self [simp]: "\<bar>a\<bar> * \<bar>a\<bar> = a * a"
lp15@60562
  1937
  by (simp add: abs_if)
haftmann@30961
  1938
paulson@14294
  1939
lemma abs_mult_less:
wenzelm@63325
  1940
  assumes ac: "\<bar>a\<bar> < c"
wenzelm@63325
  1941
    and bd: "\<bar>b\<bar> < d"
wenzelm@63325
  1942
  shows "\<bar>a\<bar> * \<bar>b\<bar> < c * d"
paulson@14294
  1943
proof -
wenzelm@63325
  1944
  from ac have "0 < c"
wenzelm@63325
  1945
    by (blast intro: le_less_trans abs_ge_zero)
wenzelm@63325
  1946
  with bd show ?thesis by (simp add: ac mult_strict_mono)
paulson@14294
  1947
qed
paulson@14293
  1948
wenzelm@63325
  1949
lemma abs_less_iff: "\<bar>a\<bar> < b \<longleftrightarrow> a < b \<and> - a < b"
haftmann@36301
  1950
  by (simp add: less_le abs_le_iff) (auto simp add: abs_if)
obua@14738
  1951
wenzelm@63325
  1952
lemma abs_mult_pos: "0 \<le> x \<Longrightarrow> \<bar>y\<bar> * x = \<bar>y * x\<bar>"
haftmann@36301
  1953
  by (simp add: abs_mult)
haftmann@36301
  1954
wenzelm@63325
  1955
lemma abs_diff_less_iff: "\<bar>x - a\<bar> < r \<longleftrightarrow> a - r < x \<and> x < a + r"
hoelzl@51520
  1956
  by (auto simp add: diff_less_eq ac_simps abs_less_iff)
hoelzl@51520
  1957
wenzelm@63325
  1958
lemma abs_diff_le_iff: "\<bar>x - a\<bar> \<le> r \<longleftrightarrow> a - r \<le> x \<and> x \<le> a + r"
lp15@59865
  1959
  by (auto simp add: diff_le_eq ac_simps abs_le_iff)
lp15@59865
  1960
lp15@62626
  1961
lemma abs_add_one_gt_zero: "0 < 1 + \<bar>x\<bar>"
wenzelm@63325
  1962
  by (auto simp: abs_if not_less intro: zero_less_one add_strict_increasing less_trans)
lp15@62626
  1963
haftmann@36301
  1964
end
avigad@16775
  1965
hoelzl@62376
  1966
subsection \<open>Dioids\<close>
hoelzl@62376
  1967
wenzelm@63325
  1968
text \<open>
wenzelm@63325
  1969
  Dioids are the alternative extensions of semirings, a semiring can
wenzelm@63325
  1970
  either be a ring or a dioid but never both.
wenzelm@63325
  1971
\<close>
hoelzl@62376
  1972
hoelzl@62376
  1973
class dioid = semiring_1 + canonically_ordered_monoid_add
hoelzl@62376
  1974
begin
hoelzl@62376
  1975
hoelzl@62376
  1976
subclass ordered_semiring
wenzelm@63325
  1977
  by standard (auto simp: le_iff_add distrib_left distrib_right)
hoelzl@62376
  1978
hoelzl@62376
  1979
end
hoelzl@62376
  1980
hoelzl@62376
  1981
haftmann@59557
  1982
hide_fact (open) comm_mult_left_mono comm_mult_strict_left_mono distrib
haftmann@59557
  1983
haftmann@52435
  1984
code_identifier
haftmann@52435
  1985
  code_module Rings \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
haftmann@33364
  1986
paulson@14265
  1987
end