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