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