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