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