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