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