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