src/HOL/OrderedGroup.thy
author haftmann
Mon Feb 08 14:06:51 2010 +0100 (2010-02-08)
changeset 35036 b8c8d01cc20d
parent 35028 108662d50512
permissions -rw-r--r--
separate library theory for type classes combining lattices with various algebraic structures; more simp rules
wenzelm@14770
     1
(*  Title:   HOL/OrderedGroup.thy
wenzelm@29269
     2
    Author:  Gertrud Bauer, Steven Obua, Lawrence C Paulson, Markus Wenzel, Jeremy Avigad
obua@14738
     3
*)
obua@14738
     4
obua@14738
     5
header {* Ordered Groups *}
obua@14738
     6
nipkow@15131
     7
theory OrderedGroup
haftmann@22452
     8
imports Lattices
wenzelm@19798
     9
uses "~~/src/Provers/Arith/abel_cancel.ML"
nipkow@15131
    10
begin
obua@14738
    11
obua@14738
    12
text {*
obua@14738
    13
  The theory of partially ordered groups is taken from the books:
obua@14738
    14
  \begin{itemize}
obua@14738
    15
  \item \emph{Lattice Theory} by Garret Birkhoff, American Mathematical Society 1979 
obua@14738
    16
  \item \emph{Partially Ordered Algebraic Systems}, Pergamon Press 1963
obua@14738
    17
  \end{itemize}
obua@14738
    18
  Most of the used notions can also be looked up in 
obua@14738
    19
  \begin{itemize}
wenzelm@14770
    20
  \item \url{http://www.mathworld.com} by Eric Weisstein et. al.
obua@14738
    21
  \item \emph{Algebra I} by van der Waerden, Springer.
obua@14738
    22
  \end{itemize}
obua@14738
    23
*}
obua@14738
    24
wenzelm@31902
    25
ML {*
haftmann@34973
    26
structure Algebra_Simps = Named_Thms(
wenzelm@31902
    27
  val name = "algebra_simps"
wenzelm@31902
    28
  val description = "algebra simplification rules"
wenzelm@31902
    29
)
nipkow@29667
    30
*}
nipkow@29667
    31
wenzelm@31902
    32
setup Algebra_Simps.setup
nipkow@29667
    33
nipkow@29667
    34
text{* The rewrites accumulated in @{text algebra_simps} deal with the
nipkow@29667
    35
classical algebraic structures of groups, rings and family. They simplify
nipkow@29667
    36
terms by multiplying everything out (in case of a ring) and bringing sums and
nipkow@29667
    37
products into a canonical form (by ordered rewriting). As a result it decides
nipkow@29667
    38
group and ring equalities but also helps with inequalities.
nipkow@29667
    39
nipkow@29667
    40
Of course it also works for fields, but it knows nothing about multiplicative
nipkow@29667
    41
inverses or division. This is catered for by @{text field_simps}. *}
nipkow@29667
    42
nipkow@23085
    43
subsection {* Semigroups and Monoids *}
obua@14738
    44
haftmann@22390
    45
class semigroup_add = plus +
haftmann@34973
    46
  assumes add_assoc [algebra_simps]: "(a + b) + c = a + (b + c)"
haftmann@34973
    47
haftmann@34973
    48
sublocale semigroup_add < plus!: semigroup plus proof
haftmann@34973
    49
qed (fact add_assoc)
haftmann@22390
    50
haftmann@22390
    51
class ab_semigroup_add = semigroup_add +
haftmann@34973
    52
  assumes add_commute [algebra_simps]: "a + b = b + a"
haftmann@34973
    53
haftmann@34973
    54
sublocale ab_semigroup_add < plus!: abel_semigroup plus proof
haftmann@34973
    55
qed (fact add_commute)
haftmann@34973
    56
haftmann@34973
    57
context ab_semigroup_add
haftmann@25062
    58
begin
obua@14738
    59
haftmann@34973
    60
lemmas add_left_commute [algebra_simps] = plus.left_commute
haftmann@25062
    61
haftmann@25062
    62
theorems add_ac = add_assoc add_commute add_left_commute
haftmann@25062
    63
haftmann@25062
    64
end
obua@14738
    65
obua@14738
    66
theorems add_ac = add_assoc add_commute add_left_commute
obua@14738
    67
haftmann@22390
    68
class semigroup_mult = times +
haftmann@34973
    69
  assumes mult_assoc [algebra_simps]: "(a * b) * c = a * (b * c)"
haftmann@34973
    70
haftmann@34973
    71
sublocale semigroup_mult < times!: semigroup times proof
haftmann@34973
    72
qed (fact mult_assoc)
obua@14738
    73
haftmann@22390
    74
class ab_semigroup_mult = semigroup_mult +
haftmann@34973
    75
  assumes mult_commute [algebra_simps]: "a * b = b * a"
haftmann@34973
    76
haftmann@34973
    77
sublocale ab_semigroup_mult < times!: abel_semigroup times proof
haftmann@34973
    78
qed (fact mult_commute)
haftmann@34973
    79
haftmann@34973
    80
context ab_semigroup_mult
haftmann@23181
    81
begin
obua@14738
    82
haftmann@34973
    83
lemmas mult_left_commute [algebra_simps] = times.left_commute
haftmann@25062
    84
haftmann@25062
    85
theorems mult_ac = mult_assoc mult_commute mult_left_commute
haftmann@23181
    86
haftmann@23181
    87
end
obua@14738
    88
obua@14738
    89
theorems mult_ac = mult_assoc mult_commute mult_left_commute
obua@14738
    90
haftmann@26015
    91
class ab_semigroup_idem_mult = ab_semigroup_mult +
haftmann@34973
    92
  assumes mult_idem: "x * x = x"
haftmann@34973
    93
haftmann@34973
    94
sublocale ab_semigroup_idem_mult < times!: semilattice times proof
haftmann@34973
    95
qed (fact mult_idem)
haftmann@34973
    96
haftmann@34973
    97
context ab_semigroup_idem_mult
haftmann@26015
    98
begin
haftmann@26015
    99
haftmann@34973
   100
lemmas mult_left_idem = times.left_idem
haftmann@26015
   101
haftmann@26015
   102
end
haftmann@26015
   103
nipkow@23085
   104
class monoid_add = zero + semigroup_add +
haftmann@25062
   105
  assumes add_0_left [simp]: "0 + a = a"
haftmann@25062
   106
    and add_0_right [simp]: "a + 0 = a"
nipkow@23085
   107
haftmann@26071
   108
lemma zero_reorient: "0 = x \<longleftrightarrow> x = 0"
nipkow@29667
   109
by (rule eq_commute)
haftmann@26071
   110
haftmann@22390
   111
class comm_monoid_add = zero + ab_semigroup_add +
haftmann@25062
   112
  assumes add_0: "0 + a = a"
haftmann@25062
   113
begin
nipkow@23085
   114
haftmann@25062
   115
subclass monoid_add
haftmann@28823
   116
  proof qed (insert add_0, simp_all add: add_commute)
haftmann@25062
   117
haftmann@25062
   118
end
obua@14738
   119
haftmann@22390
   120
class monoid_mult = one + semigroup_mult +
haftmann@25062
   121
  assumes mult_1_left [simp]: "1 * a  = a"
haftmann@25062
   122
  assumes mult_1_right [simp]: "a * 1 = a"
obua@14738
   123
haftmann@26071
   124
lemma one_reorient: "1 = x \<longleftrightarrow> x = 1"
nipkow@29667
   125
by (rule eq_commute)
haftmann@26071
   126
haftmann@22390
   127
class comm_monoid_mult = one + ab_semigroup_mult +
haftmann@25062
   128
  assumes mult_1: "1 * a = a"
haftmann@25062
   129
begin
obua@14738
   130
haftmann@25062
   131
subclass monoid_mult
haftmann@28823
   132
  proof qed (insert mult_1, simp_all add: mult_commute)
haftmann@25062
   133
haftmann@25062
   134
end
obua@14738
   135
haftmann@22390
   136
class cancel_semigroup_add = semigroup_add +
haftmann@25062
   137
  assumes add_left_imp_eq: "a + b = a + c \<Longrightarrow> b = c"
haftmann@25062
   138
  assumes add_right_imp_eq: "b + a = c + a \<Longrightarrow> b = c"
huffman@27474
   139
begin
huffman@27474
   140
huffman@27474
   141
lemma add_left_cancel [simp]:
huffman@27474
   142
  "a + b = a + c \<longleftrightarrow> b = c"
nipkow@29667
   143
by (blast dest: add_left_imp_eq)
huffman@27474
   144
huffman@27474
   145
lemma add_right_cancel [simp]:
huffman@27474
   146
  "b + a = c + a \<longleftrightarrow> b = c"
nipkow@29667
   147
by (blast dest: add_right_imp_eq)
huffman@27474
   148
huffman@27474
   149
end
obua@14738
   150
haftmann@22390
   151
class cancel_ab_semigroup_add = ab_semigroup_add +
haftmann@25062
   152
  assumes add_imp_eq: "a + b = a + c \<Longrightarrow> b = c"
haftmann@25267
   153
begin
obua@14738
   154
haftmann@25267
   155
subclass cancel_semigroup_add
haftmann@28823
   156
proof
haftmann@22390
   157
  fix a b c :: 'a
haftmann@22390
   158
  assume "a + b = a + c" 
haftmann@22390
   159
  then show "b = c" by (rule add_imp_eq)
haftmann@22390
   160
next
obua@14738
   161
  fix a b c :: 'a
obua@14738
   162
  assume "b + a = c + a"
haftmann@22390
   163
  then have "a + b = a + c" by (simp only: add_commute)
haftmann@22390
   164
  then show "b = c" by (rule add_imp_eq)
obua@14738
   165
qed
obua@14738
   166
haftmann@25267
   167
end
haftmann@25267
   168
huffman@29904
   169
class cancel_comm_monoid_add = cancel_ab_semigroup_add + comm_monoid_add
huffman@29904
   170
huffman@29904
   171
nipkow@23085
   172
subsection {* Groups *}
nipkow@23085
   173
haftmann@25762
   174
class group_add = minus + uminus + monoid_add +
haftmann@25062
   175
  assumes left_minus [simp]: "- a + a = 0"
haftmann@25062
   176
  assumes diff_minus: "a - b = a + (- b)"
haftmann@25062
   177
begin
nipkow@23085
   178
huffman@34147
   179
lemma minus_unique:
huffman@34147
   180
  assumes "a + b = 0" shows "- a = b"
huffman@34147
   181
proof -
huffman@34147
   182
  have "- a = - a + (a + b)" using assms by simp
huffman@34147
   183
  also have "\<dots> = b" by (simp add: add_assoc [symmetric])
huffman@34147
   184
  finally show ?thesis .
huffman@34147
   185
qed
huffman@34147
   186
huffman@34147
   187
lemmas equals_zero_I = minus_unique (* legacy name *)
obua@14738
   188
haftmann@25062
   189
lemma minus_zero [simp]: "- 0 = 0"
obua@14738
   190
proof -
huffman@34147
   191
  have "0 + 0 = 0" by (rule add_0_right)
huffman@34147
   192
  thus "- 0 = 0" by (rule minus_unique)
obua@14738
   193
qed
obua@14738
   194
haftmann@25062
   195
lemma minus_minus [simp]: "- (- a) = a"
nipkow@23085
   196
proof -
huffman@34147
   197
  have "- a + a = 0" by (rule left_minus)
huffman@34147
   198
  thus "- (- a) = a" by (rule minus_unique)
nipkow@23085
   199
qed
obua@14738
   200
haftmann@25062
   201
lemma right_minus [simp]: "a + - a = 0"
obua@14738
   202
proof -
haftmann@25062
   203
  have "a + - a = - (- a) + - a" by simp
haftmann@25062
   204
  also have "\<dots> = 0" by (rule left_minus)
obua@14738
   205
  finally show ?thesis .
obua@14738
   206
qed
obua@14738
   207
huffman@34147
   208
lemma minus_add_cancel: "- a + (a + b) = b"
huffman@34147
   209
by (simp add: add_assoc [symmetric])
huffman@34147
   210
huffman@34147
   211
lemma add_minus_cancel: "a + (- a + b) = b"
huffman@34147
   212
by (simp add: add_assoc [symmetric])
huffman@34147
   213
huffman@34147
   214
lemma minus_add: "- (a + b) = - b + - a"
huffman@34147
   215
proof -
huffman@34147
   216
  have "(a + b) + (- b + - a) = 0"
huffman@34147
   217
    by (simp add: add_assoc add_minus_cancel)
huffman@34147
   218
  thus "- (a + b) = - b + - a"
huffman@34147
   219
    by (rule minus_unique)
huffman@34147
   220
qed
huffman@34147
   221
haftmann@25062
   222
lemma right_minus_eq: "a - b = 0 \<longleftrightarrow> a = b"
obua@14738
   223
proof
nipkow@23085
   224
  assume "a - b = 0"
nipkow@23085
   225
  have "a = (a - b) + b" by (simp add:diff_minus add_assoc)
nipkow@23085
   226
  also have "\<dots> = b" using `a - b = 0` by simp
nipkow@23085
   227
  finally show "a = b" .
obua@14738
   228
next
nipkow@23085
   229
  assume "a = b" thus "a - b = 0" by (simp add: diff_minus)
obua@14738
   230
qed
obua@14738
   231
haftmann@25062
   232
lemma diff_self [simp]: "a - a = 0"
nipkow@29667
   233
by (simp add: diff_minus)
obua@14738
   234
haftmann@25062
   235
lemma diff_0 [simp]: "0 - a = - a"
nipkow@29667
   236
by (simp add: diff_minus)
obua@14738
   237
haftmann@25062
   238
lemma diff_0_right [simp]: "a - 0 = a" 
nipkow@29667
   239
by (simp add: diff_minus)
obua@14738
   240
haftmann@25062
   241
lemma diff_minus_eq_add [simp]: "a - - b = a + b"
nipkow@29667
   242
by (simp add: diff_minus)
obua@14738
   243
haftmann@25062
   244
lemma neg_equal_iff_equal [simp]:
haftmann@25062
   245
  "- a = - b \<longleftrightarrow> a = b" 
obua@14738
   246
proof 
obua@14738
   247
  assume "- a = - b"
nipkow@29667
   248
  hence "- (- a) = - (- b)" by simp
haftmann@25062
   249
  thus "a = b" by simp
obua@14738
   250
next
haftmann@25062
   251
  assume "a = b"
haftmann@25062
   252
  thus "- a = - b" by simp
obua@14738
   253
qed
obua@14738
   254
haftmann@25062
   255
lemma neg_equal_0_iff_equal [simp]:
haftmann@25062
   256
  "- a = 0 \<longleftrightarrow> a = 0"
nipkow@29667
   257
by (subst neg_equal_iff_equal [symmetric], simp)
obua@14738
   258
haftmann@25062
   259
lemma neg_0_equal_iff_equal [simp]:
haftmann@25062
   260
  "0 = - a \<longleftrightarrow> 0 = a"
nipkow@29667
   261
by (subst neg_equal_iff_equal [symmetric], simp)
obua@14738
   262
obua@14738
   263
text{*The next two equations can make the simplifier loop!*}
obua@14738
   264
haftmann@25062
   265
lemma equation_minus_iff:
haftmann@25062
   266
  "a = - b \<longleftrightarrow> b = - a"
obua@14738
   267
proof -
haftmann@25062
   268
  have "- (- a) = - b \<longleftrightarrow> - a = b" by (rule neg_equal_iff_equal)
haftmann@25062
   269
  thus ?thesis by (simp add: eq_commute)
haftmann@25062
   270
qed
haftmann@25062
   271
haftmann@25062
   272
lemma minus_equation_iff:
haftmann@25062
   273
  "- a = b \<longleftrightarrow> - b = a"
haftmann@25062
   274
proof -
haftmann@25062
   275
  have "- a = - (- b) \<longleftrightarrow> a = -b" by (rule neg_equal_iff_equal)
obua@14738
   276
  thus ?thesis by (simp add: eq_commute)
obua@14738
   277
qed
obua@14738
   278
huffman@28130
   279
lemma diff_add_cancel: "a - b + b = a"
nipkow@29667
   280
by (simp add: diff_minus add_assoc)
huffman@28130
   281
huffman@28130
   282
lemma add_diff_cancel: "a + b - b = a"
nipkow@29667
   283
by (simp add: diff_minus add_assoc)
nipkow@29667
   284
nipkow@29667
   285
declare diff_minus[symmetric, algebra_simps]
huffman@28130
   286
huffman@29914
   287
lemma eq_neg_iff_add_eq_0: "a = - b \<longleftrightarrow> a + b = 0"
huffman@29914
   288
proof
huffman@29914
   289
  assume "a = - b" then show "a + b = 0" by simp
huffman@29914
   290
next
huffman@29914
   291
  assume "a + b = 0"
huffman@29914
   292
  moreover have "a + (b + - b) = (a + b) + - b"
huffman@29914
   293
    by (simp only: add_assoc)
huffman@29914
   294
  ultimately show "a = - b" by simp
huffman@29914
   295
qed
huffman@29914
   296
haftmann@25062
   297
end
haftmann@25062
   298
haftmann@25762
   299
class ab_group_add = minus + uminus + comm_monoid_add +
haftmann@25062
   300
  assumes ab_left_minus: "- a + a = 0"
haftmann@25062
   301
  assumes ab_diff_minus: "a - b = a + (- b)"
haftmann@25267
   302
begin
haftmann@25062
   303
haftmann@25267
   304
subclass group_add
haftmann@28823
   305
  proof qed (simp_all add: ab_left_minus ab_diff_minus)
haftmann@25062
   306
huffman@29904
   307
subclass cancel_comm_monoid_add
haftmann@28823
   308
proof
haftmann@25062
   309
  fix a b c :: 'a
haftmann@25062
   310
  assume "a + b = a + c"
haftmann@25062
   311
  then have "- a + a + b = - a + a + c"
haftmann@25062
   312
    unfolding add_assoc by simp
haftmann@25062
   313
  then show "b = c" by simp
haftmann@25062
   314
qed
haftmann@25062
   315
nipkow@29667
   316
lemma uminus_add_conv_diff[algebra_simps]:
haftmann@25062
   317
  "- a + b = b - a"
nipkow@29667
   318
by (simp add:diff_minus add_commute)
haftmann@25062
   319
haftmann@25062
   320
lemma minus_add_distrib [simp]:
haftmann@25062
   321
  "- (a + b) = - a + - b"
huffman@34146
   322
by (rule minus_unique) (simp add: add_ac)
haftmann@25062
   323
haftmann@25062
   324
lemma minus_diff_eq [simp]:
haftmann@25062
   325
  "- (a - b) = b - a"
nipkow@29667
   326
by (simp add: diff_minus add_commute)
haftmann@25077
   327
nipkow@29667
   328
lemma add_diff_eq[algebra_simps]: "a + (b - c) = (a + b) - c"
nipkow@29667
   329
by (simp add: diff_minus add_ac)
haftmann@25077
   330
nipkow@29667
   331
lemma diff_add_eq[algebra_simps]: "(a - b) + c = (a + c) - b"
nipkow@29667
   332
by (simp add: diff_minus add_ac)
haftmann@25077
   333
nipkow@29667
   334
lemma diff_eq_eq[algebra_simps]: "a - b = c \<longleftrightarrow> a = c + b"
nipkow@29667
   335
by (auto simp add: diff_minus add_assoc)
haftmann@25077
   336
nipkow@29667
   337
lemma eq_diff_eq[algebra_simps]: "a = c - b \<longleftrightarrow> a + b = c"
nipkow@29667
   338
by (auto simp add: diff_minus add_assoc)
haftmann@25077
   339
nipkow@29667
   340
lemma diff_diff_eq[algebra_simps]: "(a - b) - c = a - (b + c)"
nipkow@29667
   341
by (simp add: diff_minus add_ac)
haftmann@25077
   342
nipkow@29667
   343
lemma diff_diff_eq2[algebra_simps]: "a - (b - c) = (a + c) - b"
nipkow@29667
   344
by (simp add: diff_minus add_ac)
haftmann@25077
   345
haftmann@25077
   346
lemma eq_iff_diff_eq_0: "a = b \<longleftrightarrow> a - b = 0"
nipkow@29667
   347
by (simp add: algebra_simps)
haftmann@25077
   348
huffman@30629
   349
lemma diff_eq_0_iff_eq [simp, noatp]: "a - b = 0 \<longleftrightarrow> a = b"
huffman@30629
   350
by (simp add: algebra_simps)
huffman@30629
   351
haftmann@25062
   352
end
obua@14738
   353
obua@14738
   354
subsection {* (Partially) Ordered Groups *} 
obua@14738
   355
haftmann@35028
   356
class ordered_ab_semigroup_add = order + ab_semigroup_add +
haftmann@25062
   357
  assumes add_left_mono: "a \<le> b \<Longrightarrow> c + a \<le> c + b"
haftmann@25062
   358
begin
haftmann@24380
   359
haftmann@25062
   360
lemma add_right_mono:
haftmann@25062
   361
  "a \<le> b \<Longrightarrow> a + c \<le> b + c"
nipkow@29667
   362
by (simp add: add_commute [of _ c] add_left_mono)
obua@14738
   363
obua@14738
   364
text {* non-strict, in both arguments *}
obua@14738
   365
lemma add_mono:
haftmann@25062
   366
  "a \<le> b \<Longrightarrow> c \<le> d \<Longrightarrow> a + c \<le> b + d"
obua@14738
   367
  apply (erule add_right_mono [THEN order_trans])
obua@14738
   368
  apply (simp add: add_commute add_left_mono)
obua@14738
   369
  done
obua@14738
   370
haftmann@25062
   371
end
haftmann@25062
   372
haftmann@35028
   373
class ordered_cancel_ab_semigroup_add =
haftmann@35028
   374
  ordered_ab_semigroup_add + cancel_ab_semigroup_add
haftmann@25062
   375
begin
haftmann@25062
   376
obua@14738
   377
lemma add_strict_left_mono:
haftmann@25062
   378
  "a < b \<Longrightarrow> c + a < c + b"
nipkow@29667
   379
by (auto simp add: less_le add_left_mono)
obua@14738
   380
obua@14738
   381
lemma add_strict_right_mono:
haftmann@25062
   382
  "a < b \<Longrightarrow> a + c < b + c"
nipkow@29667
   383
by (simp add: add_commute [of _ c] add_strict_left_mono)
obua@14738
   384
obua@14738
   385
text{*Strict monotonicity in both arguments*}
haftmann@25062
   386
lemma add_strict_mono:
haftmann@25062
   387
  "a < b \<Longrightarrow> c < d \<Longrightarrow> a + c < b + d"
haftmann@25062
   388
apply (erule add_strict_right_mono [THEN less_trans])
obua@14738
   389
apply (erule add_strict_left_mono)
obua@14738
   390
done
obua@14738
   391
obua@14738
   392
lemma add_less_le_mono:
haftmann@25062
   393
  "a < b \<Longrightarrow> c \<le> d \<Longrightarrow> a + c < b + d"
haftmann@25062
   394
apply (erule add_strict_right_mono [THEN less_le_trans])
haftmann@25062
   395
apply (erule add_left_mono)
obua@14738
   396
done
obua@14738
   397
obua@14738
   398
lemma add_le_less_mono:
haftmann@25062
   399
  "a \<le> b \<Longrightarrow> c < d \<Longrightarrow> a + c < b + d"
haftmann@25062
   400
apply (erule add_right_mono [THEN le_less_trans])
obua@14738
   401
apply (erule add_strict_left_mono) 
obua@14738
   402
done
obua@14738
   403
haftmann@25062
   404
end
haftmann@25062
   405
haftmann@35028
   406
class ordered_ab_semigroup_add_imp_le =
haftmann@35028
   407
  ordered_cancel_ab_semigroup_add +
haftmann@25062
   408
  assumes add_le_imp_le_left: "c + a \<le> c + b \<Longrightarrow> a \<le> b"
haftmann@25062
   409
begin
haftmann@25062
   410
obua@14738
   411
lemma add_less_imp_less_left:
nipkow@29667
   412
  assumes less: "c + a < c + b" shows "a < b"
obua@14738
   413
proof -
obua@14738
   414
  from less have le: "c + a <= c + b" by (simp add: order_le_less)
obua@14738
   415
  have "a <= b" 
obua@14738
   416
    apply (insert le)
obua@14738
   417
    apply (drule add_le_imp_le_left)
obua@14738
   418
    by (insert le, drule add_le_imp_le_left, assumption)
obua@14738
   419
  moreover have "a \<noteq> b"
obua@14738
   420
  proof (rule ccontr)
obua@14738
   421
    assume "~(a \<noteq> b)"
obua@14738
   422
    then have "a = b" by simp
obua@14738
   423
    then have "c + a = c + b" by simp
obua@14738
   424
    with less show "False"by simp
obua@14738
   425
  qed
obua@14738
   426
  ultimately show "a < b" by (simp add: order_le_less)
obua@14738
   427
qed
obua@14738
   428
obua@14738
   429
lemma add_less_imp_less_right:
haftmann@25062
   430
  "a + c < b + c \<Longrightarrow> a < b"
obua@14738
   431
apply (rule add_less_imp_less_left [of c])
obua@14738
   432
apply (simp add: add_commute)  
obua@14738
   433
done
obua@14738
   434
obua@14738
   435
lemma add_less_cancel_left [simp]:
haftmann@25062
   436
  "c + a < c + b \<longleftrightarrow> a < b"
nipkow@29667
   437
by (blast intro: add_less_imp_less_left add_strict_left_mono) 
obua@14738
   438
obua@14738
   439
lemma add_less_cancel_right [simp]:
haftmann@25062
   440
  "a + c < b + c \<longleftrightarrow> a < b"
nipkow@29667
   441
by (blast intro: add_less_imp_less_right add_strict_right_mono)
obua@14738
   442
obua@14738
   443
lemma add_le_cancel_left [simp]:
haftmann@25062
   444
  "c + a \<le> c + b \<longleftrightarrow> a \<le> b"
nipkow@29667
   445
by (auto, drule add_le_imp_le_left, simp_all add: add_left_mono) 
obua@14738
   446
obua@14738
   447
lemma add_le_cancel_right [simp]:
haftmann@25062
   448
  "a + c \<le> b + c \<longleftrightarrow> a \<le> b"
nipkow@29667
   449
by (simp add: add_commute [of a c] add_commute [of b c])
obua@14738
   450
obua@14738
   451
lemma add_le_imp_le_right:
haftmann@25062
   452
  "a + c \<le> b + c \<Longrightarrow> a \<le> b"
nipkow@29667
   453
by simp
haftmann@25062
   454
haftmann@25077
   455
lemma max_add_distrib_left:
haftmann@25077
   456
  "max x y + z = max (x + z) (y + z)"
haftmann@25077
   457
  unfolding max_def by auto
haftmann@25077
   458
haftmann@25077
   459
lemma min_add_distrib_left:
haftmann@25077
   460
  "min x y + z = min (x + z) (y + z)"
haftmann@25077
   461
  unfolding min_def by auto
haftmann@25077
   462
haftmann@25062
   463
end
haftmann@25062
   464
haftmann@25303
   465
subsection {* Support for reasoning about signs *}
haftmann@25303
   466
haftmann@35028
   467
class ordered_comm_monoid_add =
haftmann@35028
   468
  ordered_cancel_ab_semigroup_add + comm_monoid_add
haftmann@25303
   469
begin
haftmann@25303
   470
haftmann@25303
   471
lemma add_pos_nonneg:
nipkow@29667
   472
  assumes "0 < a" and "0 \<le> b" shows "0 < a + b"
haftmann@25303
   473
proof -
haftmann@25303
   474
  have "0 + 0 < a + b" 
haftmann@25303
   475
    using assms by (rule add_less_le_mono)
haftmann@25303
   476
  then show ?thesis by simp
haftmann@25303
   477
qed
haftmann@25303
   478
haftmann@25303
   479
lemma add_pos_pos:
nipkow@29667
   480
  assumes "0 < a" and "0 < b" shows "0 < a + b"
nipkow@29667
   481
by (rule add_pos_nonneg) (insert assms, auto)
haftmann@25303
   482
haftmann@25303
   483
lemma add_nonneg_pos:
nipkow@29667
   484
  assumes "0 \<le> a" and "0 < b" shows "0 < a + b"
haftmann@25303
   485
proof -
haftmann@25303
   486
  have "0 + 0 < a + b" 
haftmann@25303
   487
    using assms by (rule add_le_less_mono)
haftmann@25303
   488
  then show ?thesis by simp
haftmann@25303
   489
qed
haftmann@25303
   490
haftmann@25303
   491
lemma add_nonneg_nonneg:
nipkow@29667
   492
  assumes "0 \<le> a" and "0 \<le> b" shows "0 \<le> a + b"
haftmann@25303
   493
proof -
haftmann@25303
   494
  have "0 + 0 \<le> a + b" 
haftmann@25303
   495
    using assms by (rule add_mono)
haftmann@25303
   496
  then show ?thesis by simp
haftmann@25303
   497
qed
haftmann@25303
   498
huffman@30691
   499
lemma add_neg_nonpos:
nipkow@29667
   500
  assumes "a < 0" and "b \<le> 0" shows "a + b < 0"
haftmann@25303
   501
proof -
haftmann@25303
   502
  have "a + b < 0 + 0"
haftmann@25303
   503
    using assms by (rule add_less_le_mono)
haftmann@25303
   504
  then show ?thesis by simp
haftmann@25303
   505
qed
haftmann@25303
   506
haftmann@25303
   507
lemma add_neg_neg: 
nipkow@29667
   508
  assumes "a < 0" and "b < 0" shows "a + b < 0"
nipkow@29667
   509
by (rule add_neg_nonpos) (insert assms, auto)
haftmann@25303
   510
haftmann@25303
   511
lemma add_nonpos_neg:
nipkow@29667
   512
  assumes "a \<le> 0" and "b < 0" shows "a + b < 0"
haftmann@25303
   513
proof -
haftmann@25303
   514
  have "a + b < 0 + 0"
haftmann@25303
   515
    using assms by (rule add_le_less_mono)
haftmann@25303
   516
  then show ?thesis by simp
haftmann@25303
   517
qed
haftmann@25303
   518
haftmann@25303
   519
lemma add_nonpos_nonpos:
nipkow@29667
   520
  assumes "a \<le> 0" and "b \<le> 0" shows "a + b \<le> 0"
haftmann@25303
   521
proof -
haftmann@25303
   522
  have "a + b \<le> 0 + 0"
haftmann@25303
   523
    using assms by (rule add_mono)
haftmann@25303
   524
  then show ?thesis by simp
haftmann@25303
   525
qed
haftmann@25303
   526
huffman@30691
   527
lemmas add_sign_intros =
huffman@30691
   528
  add_pos_nonneg add_pos_pos add_nonneg_pos add_nonneg_nonneg
huffman@30691
   529
  add_neg_nonpos add_neg_neg add_nonpos_neg add_nonpos_nonpos
huffman@30691
   530
huffman@29886
   531
lemma add_nonneg_eq_0_iff:
huffman@29886
   532
  assumes x: "0 \<le> x" and y: "0 \<le> y"
huffman@29886
   533
  shows "x + y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
huffman@29886
   534
proof (intro iffI conjI)
huffman@29886
   535
  have "x = x + 0" by simp
huffman@29886
   536
  also have "x + 0 \<le> x + y" using y by (rule add_left_mono)
huffman@29886
   537
  also assume "x + y = 0"
huffman@29886
   538
  also have "0 \<le> x" using x .
huffman@29886
   539
  finally show "x = 0" .
huffman@29886
   540
next
huffman@29886
   541
  have "y = 0 + y" by simp
huffman@29886
   542
  also have "0 + y \<le> x + y" using x by (rule add_right_mono)
huffman@29886
   543
  also assume "x + y = 0"
huffman@29886
   544
  also have "0 \<le> y" using y .
huffman@29886
   545
  finally show "y = 0" .
huffman@29886
   546
next
huffman@29886
   547
  assume "x = 0 \<and> y = 0"
huffman@29886
   548
  then show "x + y = 0" by simp
huffman@29886
   549
qed
huffman@29886
   550
haftmann@25303
   551
end
haftmann@25303
   552
haftmann@35028
   553
class ordered_ab_group_add =
haftmann@35028
   554
  ab_group_add + ordered_ab_semigroup_add
haftmann@25062
   555
begin
haftmann@25062
   556
haftmann@35028
   557
subclass ordered_cancel_ab_semigroup_add ..
haftmann@25062
   558
haftmann@35028
   559
subclass ordered_ab_semigroup_add_imp_le
haftmann@28823
   560
proof
haftmann@25062
   561
  fix a b c :: 'a
haftmann@25062
   562
  assume "c + a \<le> c + b"
haftmann@25062
   563
  hence "(-c) + (c + a) \<le> (-c) + (c + b)" by (rule add_left_mono)
haftmann@25062
   564
  hence "((-c) + c) + a \<le> ((-c) + c) + b" by (simp only: add_assoc)
haftmann@25062
   565
  thus "a \<le> b" by simp
haftmann@25062
   566
qed
haftmann@25062
   567
haftmann@35028
   568
subclass ordered_comm_monoid_add ..
haftmann@25303
   569
haftmann@25077
   570
lemma max_diff_distrib_left:
haftmann@25077
   571
  shows "max x y - z = max (x - z) (y - z)"
nipkow@29667
   572
by (simp add: diff_minus, rule max_add_distrib_left) 
haftmann@25077
   573
haftmann@25077
   574
lemma min_diff_distrib_left:
haftmann@25077
   575
  shows "min x y - z = min (x - z) (y - z)"
nipkow@29667
   576
by (simp add: diff_minus, rule min_add_distrib_left) 
haftmann@25077
   577
haftmann@25077
   578
lemma le_imp_neg_le:
nipkow@29667
   579
  assumes "a \<le> b" shows "-b \<le> -a"
haftmann@25077
   580
proof -
nipkow@29667
   581
  have "-a+a \<le> -a+b" using `a \<le> b` by (rule add_left_mono) 
nipkow@29667
   582
  hence "0 \<le> -a+b" by simp
nipkow@29667
   583
  hence "0 + (-b) \<le> (-a + b) + (-b)" by (rule add_right_mono) 
nipkow@29667
   584
  thus ?thesis by (simp add: add_assoc)
haftmann@25077
   585
qed
haftmann@25077
   586
haftmann@25077
   587
lemma neg_le_iff_le [simp]: "- b \<le> - a \<longleftrightarrow> a \<le> b"
haftmann@25077
   588
proof 
haftmann@25077
   589
  assume "- b \<le> - a"
nipkow@29667
   590
  hence "- (- a) \<le> - (- b)" by (rule le_imp_neg_le)
haftmann@25077
   591
  thus "a\<le>b" by simp
haftmann@25077
   592
next
haftmann@25077
   593
  assume "a\<le>b"
haftmann@25077
   594
  thus "-b \<le> -a" by (rule le_imp_neg_le)
haftmann@25077
   595
qed
haftmann@25077
   596
haftmann@25077
   597
lemma neg_le_0_iff_le [simp]: "- a \<le> 0 \<longleftrightarrow> 0 \<le> a"
nipkow@29667
   598
by (subst neg_le_iff_le [symmetric], simp)
haftmann@25077
   599
haftmann@25077
   600
lemma neg_0_le_iff_le [simp]: "0 \<le> - a \<longleftrightarrow> a \<le> 0"
nipkow@29667
   601
by (subst neg_le_iff_le [symmetric], simp)
haftmann@25077
   602
haftmann@25077
   603
lemma neg_less_iff_less [simp]: "- b < - a \<longleftrightarrow> a < b"
nipkow@29667
   604
by (force simp add: less_le) 
haftmann@25077
   605
haftmann@25077
   606
lemma neg_less_0_iff_less [simp]: "- a < 0 \<longleftrightarrow> 0 < a"
nipkow@29667
   607
by (subst neg_less_iff_less [symmetric], simp)
haftmann@25077
   608
haftmann@25077
   609
lemma neg_0_less_iff_less [simp]: "0 < - a \<longleftrightarrow> a < 0"
nipkow@29667
   610
by (subst neg_less_iff_less [symmetric], simp)
haftmann@25077
   611
haftmann@25077
   612
text{*The next several equations can make the simplifier loop!*}
haftmann@25077
   613
haftmann@25077
   614
lemma less_minus_iff: "a < - b \<longleftrightarrow> b < - a"
haftmann@25077
   615
proof -
haftmann@25077
   616
  have "(- (-a) < - b) = (b < - a)" by (rule neg_less_iff_less)
haftmann@25077
   617
  thus ?thesis by simp
haftmann@25077
   618
qed
haftmann@25077
   619
haftmann@25077
   620
lemma minus_less_iff: "- a < b \<longleftrightarrow> - b < a"
haftmann@25077
   621
proof -
haftmann@25077
   622
  have "(- a < - (-b)) = (- b < a)" by (rule neg_less_iff_less)
haftmann@25077
   623
  thus ?thesis by simp
haftmann@25077
   624
qed
haftmann@25077
   625
haftmann@25077
   626
lemma le_minus_iff: "a \<le> - b \<longleftrightarrow> b \<le> - a"
haftmann@25077
   627
proof -
haftmann@25077
   628
  have mm: "!! a (b::'a). (-(-a)) < -b \<Longrightarrow> -(-b) < -a" by (simp only: minus_less_iff)
haftmann@25077
   629
  have "(- (- a) <= -b) = (b <= - a)" 
haftmann@25077
   630
    apply (auto simp only: le_less)
haftmann@25077
   631
    apply (drule mm)
haftmann@25077
   632
    apply (simp_all)
haftmann@25077
   633
    apply (drule mm[simplified], assumption)
haftmann@25077
   634
    done
haftmann@25077
   635
  then show ?thesis by simp
haftmann@25077
   636
qed
haftmann@25077
   637
haftmann@25077
   638
lemma minus_le_iff: "- a \<le> b \<longleftrightarrow> - b \<le> a"
nipkow@29667
   639
by (auto simp add: le_less minus_less_iff)
haftmann@25077
   640
haftmann@25077
   641
lemma less_iff_diff_less_0: "a < b \<longleftrightarrow> a - b < 0"
haftmann@25077
   642
proof -
haftmann@25077
   643
  have  "(a < b) = (a + (- b) < b + (-b))"  
haftmann@25077
   644
    by (simp only: add_less_cancel_right)
haftmann@25077
   645
  also have "... =  (a - b < 0)" by (simp add: diff_minus)
haftmann@25077
   646
  finally show ?thesis .
haftmann@25077
   647
qed
haftmann@25077
   648
nipkow@29667
   649
lemma diff_less_eq[algebra_simps]: "a - b < c \<longleftrightarrow> a < c + b"
haftmann@25077
   650
apply (subst less_iff_diff_less_0 [of a])
haftmann@25077
   651
apply (rule less_iff_diff_less_0 [of _ c, THEN ssubst])
haftmann@25077
   652
apply (simp add: diff_minus add_ac)
haftmann@25077
   653
done
haftmann@25077
   654
nipkow@29667
   655
lemma less_diff_eq[algebra_simps]: "a < c - b \<longleftrightarrow> a + b < c"
haftmann@25077
   656
apply (subst less_iff_diff_less_0 [of "plus a b"])
haftmann@25077
   657
apply (subst less_iff_diff_less_0 [of a])
haftmann@25077
   658
apply (simp add: diff_minus add_ac)
haftmann@25077
   659
done
haftmann@25077
   660
nipkow@29667
   661
lemma diff_le_eq[algebra_simps]: "a - b \<le> c \<longleftrightarrow> a \<le> c + b"
nipkow@29667
   662
by (auto simp add: le_less diff_less_eq diff_add_cancel add_diff_cancel)
haftmann@25077
   663
nipkow@29667
   664
lemma le_diff_eq[algebra_simps]: "a \<le> c - b \<longleftrightarrow> a + b \<le> c"
nipkow@29667
   665
by (auto simp add: le_less less_diff_eq diff_add_cancel add_diff_cancel)
haftmann@25077
   666
haftmann@25077
   667
lemma le_iff_diff_le_0: "a \<le> b \<longleftrightarrow> a - b \<le> 0"
nipkow@29667
   668
by (simp add: algebra_simps)
haftmann@25077
   669
nipkow@29667
   670
text{*Legacy - use @{text algebra_simps} *}
nipkow@29833
   671
lemmas group_simps[noatp] = algebra_simps
haftmann@25230
   672
haftmann@25077
   673
end
haftmann@25077
   674
nipkow@29667
   675
text{*Legacy - use @{text algebra_simps} *}
nipkow@29833
   676
lemmas group_simps[noatp] = algebra_simps
haftmann@25230
   677
haftmann@35028
   678
class linordered_ab_semigroup_add =
haftmann@35028
   679
  linorder + ordered_ab_semigroup_add
haftmann@25062
   680
haftmann@35028
   681
class linordered_cancel_ab_semigroup_add =
haftmann@35028
   682
  linorder + ordered_cancel_ab_semigroup_add
haftmann@25267
   683
begin
haftmann@25062
   684
haftmann@35028
   685
subclass linordered_ab_semigroup_add ..
haftmann@25062
   686
haftmann@35028
   687
subclass ordered_ab_semigroup_add_imp_le
haftmann@28823
   688
proof
haftmann@25062
   689
  fix a b c :: 'a
haftmann@25062
   690
  assume le: "c + a <= c + b"  
haftmann@25062
   691
  show "a <= b"
haftmann@25062
   692
  proof (rule ccontr)
haftmann@25062
   693
    assume w: "~ a \<le> b"
haftmann@25062
   694
    hence "b <= a" by (simp add: linorder_not_le)
haftmann@25062
   695
    hence le2: "c + b <= c + a" by (rule add_left_mono)
haftmann@25062
   696
    have "a = b" 
haftmann@25062
   697
      apply (insert le)
haftmann@25062
   698
      apply (insert le2)
haftmann@25062
   699
      apply (drule antisym, simp_all)
haftmann@25062
   700
      done
haftmann@25062
   701
    with w show False 
haftmann@25062
   702
      by (simp add: linorder_not_le [symmetric])
haftmann@25062
   703
  qed
haftmann@25062
   704
qed
haftmann@25062
   705
haftmann@25267
   706
end
haftmann@25267
   707
haftmann@35028
   708
class linordered_ab_group_add = linorder + ordered_ab_group_add
haftmann@25267
   709
begin
haftmann@25230
   710
haftmann@35028
   711
subclass linordered_cancel_ab_semigroup_add ..
haftmann@25230
   712
haftmann@35036
   713
lemma neg_less_eq_nonneg [simp]:
haftmann@25303
   714
  "- a \<le> a \<longleftrightarrow> 0 \<le> a"
haftmann@25303
   715
proof
haftmann@25303
   716
  assume A: "- a \<le> a" show "0 \<le> a"
haftmann@25303
   717
  proof (rule classical)
haftmann@25303
   718
    assume "\<not> 0 \<le> a"
haftmann@25303
   719
    then have "a < 0" by auto
haftmann@25303
   720
    with A have "- a < 0" by (rule le_less_trans)
haftmann@25303
   721
    then show ?thesis by auto
haftmann@25303
   722
  qed
haftmann@25303
   723
next
haftmann@25303
   724
  assume A: "0 \<le> a" show "- a \<le> a"
haftmann@25303
   725
  proof (rule order_trans)
haftmann@25303
   726
    show "- a \<le> 0" using A by (simp add: minus_le_iff)
haftmann@25303
   727
  next
haftmann@25303
   728
    show "0 \<le> a" using A .
haftmann@25303
   729
  qed
haftmann@25303
   730
qed
haftmann@35036
   731
haftmann@35036
   732
lemma neg_less_nonneg [simp]:
haftmann@35036
   733
  "- a < a \<longleftrightarrow> 0 < a"
haftmann@35036
   734
proof
haftmann@35036
   735
  assume A: "- a < a" show "0 < a"
haftmann@35036
   736
  proof (rule classical)
haftmann@35036
   737
    assume "\<not> 0 < a"
haftmann@35036
   738
    then have "a \<le> 0" by auto
haftmann@35036
   739
    with A have "- a < 0" by (rule less_le_trans)
haftmann@35036
   740
    then show ?thesis by auto
haftmann@35036
   741
  qed
haftmann@35036
   742
next
haftmann@35036
   743
  assume A: "0 < a" show "- a < a"
haftmann@35036
   744
  proof (rule less_trans)
haftmann@35036
   745
    show "- a < 0" using A by (simp add: minus_le_iff)
haftmann@35036
   746
  next
haftmann@35036
   747
    show "0 < a" using A .
haftmann@35036
   748
  qed
haftmann@35036
   749
qed
haftmann@35036
   750
haftmann@35036
   751
lemma less_eq_neg_nonpos [simp]:
haftmann@25303
   752
  "a \<le> - a \<longleftrightarrow> a \<le> 0"
haftmann@25303
   753
proof
haftmann@25303
   754
  assume A: "a \<le> - a" show "a \<le> 0"
haftmann@25303
   755
  proof (rule classical)
haftmann@25303
   756
    assume "\<not> a \<le> 0"
haftmann@25303
   757
    then have "0 < a" by auto
haftmann@25303
   758
    then have "0 < - a" using A by (rule less_le_trans)
haftmann@25303
   759
    then show ?thesis by auto
haftmann@25303
   760
  qed
haftmann@25303
   761
next
haftmann@25303
   762
  assume A: "a \<le> 0" show "a \<le> - a"
haftmann@25303
   763
  proof (rule order_trans)
haftmann@25303
   764
    show "0 \<le> - a" using A by (simp add: minus_le_iff)
haftmann@25303
   765
  next
haftmann@25303
   766
    show "a \<le> 0" using A .
haftmann@25303
   767
  qed
haftmann@25303
   768
qed
haftmann@25303
   769
haftmann@35036
   770
lemma equal_neg_zero [simp]:
haftmann@25303
   771
  "a = - a \<longleftrightarrow> a = 0"
haftmann@25303
   772
proof
haftmann@25303
   773
  assume "a = 0" then show "a = - a" by simp
haftmann@25303
   774
next
haftmann@25303
   775
  assume A: "a = - a" show "a = 0"
haftmann@25303
   776
  proof (cases "0 \<le> a")
haftmann@25303
   777
    case True with A have "0 \<le> - a" by auto
haftmann@25303
   778
    with le_minus_iff have "a \<le> 0" by simp
haftmann@25303
   779
    with True show ?thesis by (auto intro: order_trans)
haftmann@25303
   780
  next
haftmann@25303
   781
    case False then have B: "a \<le> 0" by auto
haftmann@25303
   782
    with A have "- a \<le> 0" by auto
haftmann@25303
   783
    with B show ?thesis by (auto intro: order_trans)
haftmann@25303
   784
  qed
haftmann@25303
   785
qed
haftmann@25303
   786
haftmann@35036
   787
lemma neg_equal_zero [simp]:
haftmann@25303
   788
  "- a = a \<longleftrightarrow> a = 0"
haftmann@35036
   789
  by (auto dest: sym)
haftmann@35036
   790
haftmann@35036
   791
lemma double_zero [simp]:
haftmann@35036
   792
  "a + a = 0 \<longleftrightarrow> a = 0"
haftmann@35036
   793
proof
haftmann@35036
   794
  assume assm: "a + a = 0"
haftmann@35036
   795
  then have a: "- a = a" by (rule minus_unique)
haftmann@35036
   796
  then show "a = 0" by (simp add: neg_equal_zero)
haftmann@35036
   797
qed simp
haftmann@35036
   798
haftmann@35036
   799
lemma double_zero_sym [simp]:
haftmann@35036
   800
  "0 = a + a \<longleftrightarrow> a = 0"
haftmann@35036
   801
  by (rule, drule sym) simp_all
haftmann@35036
   802
haftmann@35036
   803
lemma zero_less_double_add_iff_zero_less_single_add [simp]:
haftmann@35036
   804
  "0 < a + a \<longleftrightarrow> 0 < a"
haftmann@35036
   805
proof
haftmann@35036
   806
  assume "0 < a + a"
haftmann@35036
   807
  then have "0 - a < a" by (simp only: diff_less_eq)
haftmann@35036
   808
  then have "- a < a" by simp
haftmann@35036
   809
  then show "0 < a" by (simp add: neg_less_nonneg)
haftmann@35036
   810
next
haftmann@35036
   811
  assume "0 < a"
haftmann@35036
   812
  with this have "0 + 0 < a + a"
haftmann@35036
   813
    by (rule add_strict_mono)
haftmann@35036
   814
  then show "0 < a + a" by simp
haftmann@35036
   815
qed
haftmann@35036
   816
haftmann@35036
   817
lemma zero_le_double_add_iff_zero_le_single_add [simp]:
haftmann@35036
   818
  "0 \<le> a + a \<longleftrightarrow> 0 \<le> a"
haftmann@35036
   819
  by (auto simp add: le_less)
haftmann@35036
   820
haftmann@35036
   821
lemma double_add_less_zero_iff_single_add_less_zero [simp]:
haftmann@35036
   822
  "a + a < 0 \<longleftrightarrow> a < 0"
haftmann@35036
   823
proof -
haftmann@35036
   824
  have "\<not> a + a < 0 \<longleftrightarrow> \<not> a < 0"
haftmann@35036
   825
    by (simp add: not_less)
haftmann@35036
   826
  then show ?thesis by simp
haftmann@35036
   827
qed
haftmann@35036
   828
haftmann@35036
   829
lemma double_add_le_zero_iff_single_add_le_zero [simp]:
haftmann@35036
   830
  "a + a \<le> 0 \<longleftrightarrow> a \<le> 0" 
haftmann@35036
   831
proof -
haftmann@35036
   832
  have "\<not> a + a \<le> 0 \<longleftrightarrow> \<not> a \<le> 0"
haftmann@35036
   833
    by (simp add: not_le)
haftmann@35036
   834
  then show ?thesis by simp
haftmann@35036
   835
qed
haftmann@35036
   836
haftmann@35036
   837
lemma le_minus_self_iff:
haftmann@35036
   838
  "a \<le> - a \<longleftrightarrow> a \<le> 0"
haftmann@35036
   839
proof -
haftmann@35036
   840
  from add_le_cancel_left [of "- a" "a + a" 0]
haftmann@35036
   841
  have "a \<le> - a \<longleftrightarrow> a + a \<le> 0" 
haftmann@35036
   842
    by (simp add: add_assoc [symmetric])
haftmann@35036
   843
  thus ?thesis by simp
haftmann@35036
   844
qed
haftmann@35036
   845
haftmann@35036
   846
lemma minus_le_self_iff:
haftmann@35036
   847
  "- a \<le> a \<longleftrightarrow> 0 \<le> a"
haftmann@35036
   848
proof -
haftmann@35036
   849
  from add_le_cancel_left [of "- a" 0 "a + a"]
haftmann@35036
   850
  have "- a \<le> a \<longleftrightarrow> 0 \<le> a + a" 
haftmann@35036
   851
    by (simp add: add_assoc [symmetric])
haftmann@35036
   852
  thus ?thesis by simp
haftmann@35036
   853
qed
haftmann@35036
   854
haftmann@35036
   855
lemma minus_max_eq_min:
haftmann@35036
   856
  "- max x y = min (-x) (-y)"
haftmann@35036
   857
  by (auto simp add: max_def min_def)
haftmann@35036
   858
haftmann@35036
   859
lemma minus_min_eq_max:
haftmann@35036
   860
  "- min x y = max (-x) (-y)"
haftmann@35036
   861
  by (auto simp add: max_def min_def)
haftmann@25303
   862
haftmann@25267
   863
end
haftmann@25267
   864
haftmann@25077
   865
-- {* FIXME localize the following *}
obua@14738
   866
paulson@15234
   867
lemma add_increasing:
haftmann@35028
   868
  fixes c :: "'a::{ordered_ab_semigroup_add_imp_le, comm_monoid_add}"
paulson@15234
   869
  shows  "[|0\<le>a; b\<le>c|] ==> b \<le> a + c"
obua@14738
   870
by (insert add_mono [of 0 a b c], simp)
obua@14738
   871
nipkow@15539
   872
lemma add_increasing2:
haftmann@35028
   873
  fixes c :: "'a::{ordered_ab_semigroup_add_imp_le, comm_monoid_add}"
nipkow@15539
   874
  shows  "[|0\<le>c; b\<le>a|] ==> b \<le> a + c"
nipkow@15539
   875
by (simp add:add_increasing add_commute[of a])
nipkow@15539
   876
paulson@15234
   877
lemma add_strict_increasing:
haftmann@35028
   878
  fixes c :: "'a::{ordered_ab_semigroup_add_imp_le, comm_monoid_add}"
paulson@15234
   879
  shows "[|0<a; b\<le>c|] ==> b < a + c"
paulson@15234
   880
by (insert add_less_le_mono [of 0 a b c], simp)
paulson@15234
   881
paulson@15234
   882
lemma add_strict_increasing2:
haftmann@35028
   883
  fixes c :: "'a::{ordered_ab_semigroup_add_imp_le, comm_monoid_add}"
paulson@15234
   884
  shows "[|0\<le>a; b<c|] ==> b < a + c"
paulson@15234
   885
by (insert add_le_less_mono [of 0 a b c], simp)
paulson@15234
   886
obua@14738
   887
haftmann@35028
   888
class ordered_ab_group_add_abs = ordered_ab_group_add + abs +
haftmann@25303
   889
  assumes abs_ge_zero [simp]: "\<bar>a\<bar> \<ge> 0"
haftmann@25303
   890
    and abs_ge_self: "a \<le> \<bar>a\<bar>"
haftmann@25303
   891
    and abs_leI: "a \<le> b \<Longrightarrow> - a \<le> b \<Longrightarrow> \<bar>a\<bar> \<le> b"
haftmann@25303
   892
    and abs_minus_cancel [simp]: "\<bar>-a\<bar> = \<bar>a\<bar>"
haftmann@25303
   893
    and abs_triangle_ineq: "\<bar>a + b\<bar> \<le> \<bar>a\<bar> + \<bar>b\<bar>"
haftmann@25303
   894
begin
haftmann@25303
   895
haftmann@25307
   896
lemma abs_minus_le_zero: "- \<bar>a\<bar> \<le> 0"
haftmann@25307
   897
  unfolding neg_le_0_iff_le by simp
haftmann@25307
   898
haftmann@25307
   899
lemma abs_of_nonneg [simp]:
nipkow@29667
   900
  assumes nonneg: "0 \<le> a" shows "\<bar>a\<bar> = a"
haftmann@25307
   901
proof (rule antisym)
haftmann@25307
   902
  from nonneg le_imp_neg_le have "- a \<le> 0" by simp
haftmann@25307
   903
  from this nonneg have "- a \<le> a" by (rule order_trans)
haftmann@25307
   904
  then show "\<bar>a\<bar> \<le> a" by (auto intro: abs_leI)
haftmann@25307
   905
qed (rule abs_ge_self)
haftmann@25307
   906
haftmann@25307
   907
lemma abs_idempotent [simp]: "\<bar>\<bar>a\<bar>\<bar> = \<bar>a\<bar>"
nipkow@29667
   908
by (rule antisym)
nipkow@29667
   909
   (auto intro!: abs_ge_self abs_leI order_trans [of "uminus (abs a)" zero "abs a"])
haftmann@25307
   910
haftmann@25307
   911
lemma abs_eq_0 [simp]: "\<bar>a\<bar> = 0 \<longleftrightarrow> a = 0"
haftmann@25307
   912
proof -
haftmann@25307
   913
  have "\<bar>a\<bar> = 0 \<Longrightarrow> a = 0"
haftmann@25307
   914
  proof (rule antisym)
haftmann@25307
   915
    assume zero: "\<bar>a\<bar> = 0"
haftmann@25307
   916
    with abs_ge_self show "a \<le> 0" by auto
haftmann@25307
   917
    from zero have "\<bar>-a\<bar> = 0" by simp
haftmann@25307
   918
    with abs_ge_self [of "uminus a"] have "- a \<le> 0" by auto
haftmann@25307
   919
    with neg_le_0_iff_le show "0 \<le> a" by auto
haftmann@25307
   920
  qed
haftmann@25307
   921
  then show ?thesis by auto
haftmann@25307
   922
qed
haftmann@25307
   923
haftmann@25303
   924
lemma abs_zero [simp]: "\<bar>0\<bar> = 0"
nipkow@29667
   925
by simp
avigad@16775
   926
haftmann@25303
   927
lemma abs_0_eq [simp, noatp]: "0 = \<bar>a\<bar> \<longleftrightarrow> a = 0"
haftmann@25303
   928
proof -
haftmann@25303
   929
  have "0 = \<bar>a\<bar> \<longleftrightarrow> \<bar>a\<bar> = 0" by (simp only: eq_ac)
haftmann@25303
   930
  thus ?thesis by simp
haftmann@25303
   931
qed
haftmann@25303
   932
haftmann@25303
   933
lemma abs_le_zero_iff [simp]: "\<bar>a\<bar> \<le> 0 \<longleftrightarrow> a = 0" 
haftmann@25303
   934
proof
haftmann@25303
   935
  assume "\<bar>a\<bar> \<le> 0"
haftmann@25303
   936
  then have "\<bar>a\<bar> = 0" by (rule antisym) simp
haftmann@25303
   937
  thus "a = 0" by simp
haftmann@25303
   938
next
haftmann@25303
   939
  assume "a = 0"
haftmann@25303
   940
  thus "\<bar>a\<bar> \<le> 0" by simp
haftmann@25303
   941
qed
haftmann@25303
   942
haftmann@25303
   943
lemma zero_less_abs_iff [simp]: "0 < \<bar>a\<bar> \<longleftrightarrow> a \<noteq> 0"
nipkow@29667
   944
by (simp add: less_le)
haftmann@25303
   945
haftmann@25303
   946
lemma abs_not_less_zero [simp]: "\<not> \<bar>a\<bar> < 0"
haftmann@25303
   947
proof -
haftmann@25303
   948
  have a: "\<And>x y. x \<le> y \<Longrightarrow> \<not> y < x" by auto
haftmann@25303
   949
  show ?thesis by (simp add: a)
haftmann@25303
   950
qed
avigad@16775
   951
haftmann@25303
   952
lemma abs_ge_minus_self: "- a \<le> \<bar>a\<bar>"
haftmann@25303
   953
proof -
haftmann@25303
   954
  have "- a \<le> \<bar>-a\<bar>" by (rule abs_ge_self)
haftmann@25303
   955
  then show ?thesis by simp
haftmann@25303
   956
qed
haftmann@25303
   957
haftmann@25303
   958
lemma abs_minus_commute: 
haftmann@25303
   959
  "\<bar>a - b\<bar> = \<bar>b - a\<bar>"
haftmann@25303
   960
proof -
haftmann@25303
   961
  have "\<bar>a - b\<bar> = \<bar>- (a - b)\<bar>" by (simp only: abs_minus_cancel)
haftmann@25303
   962
  also have "... = \<bar>b - a\<bar>" by simp
haftmann@25303
   963
  finally show ?thesis .
haftmann@25303
   964
qed
haftmann@25303
   965
haftmann@25303
   966
lemma abs_of_pos: "0 < a \<Longrightarrow> \<bar>a\<bar> = a"
nipkow@29667
   967
by (rule abs_of_nonneg, rule less_imp_le)
avigad@16775
   968
haftmann@25303
   969
lemma abs_of_nonpos [simp]:
nipkow@29667
   970
  assumes "a \<le> 0" shows "\<bar>a\<bar> = - a"
haftmann@25303
   971
proof -
haftmann@25303
   972
  let ?b = "- a"
haftmann@25303
   973
  have "- ?b \<le> 0 \<Longrightarrow> \<bar>- ?b\<bar> = - (- ?b)"
haftmann@25303
   974
  unfolding abs_minus_cancel [of "?b"]
haftmann@25303
   975
  unfolding neg_le_0_iff_le [of "?b"]
haftmann@25303
   976
  unfolding minus_minus by (erule abs_of_nonneg)
haftmann@25303
   977
  then show ?thesis using assms by auto
haftmann@25303
   978
qed
haftmann@25303
   979
  
haftmann@25303
   980
lemma abs_of_neg: "a < 0 \<Longrightarrow> \<bar>a\<bar> = - a"
nipkow@29667
   981
by (rule abs_of_nonpos, rule less_imp_le)
haftmann@25303
   982
haftmann@25303
   983
lemma abs_le_D1: "\<bar>a\<bar> \<le> b \<Longrightarrow> a \<le> b"
nipkow@29667
   984
by (insert abs_ge_self, blast intro: order_trans)
haftmann@25303
   985
haftmann@25303
   986
lemma abs_le_D2: "\<bar>a\<bar> \<le> b \<Longrightarrow> - a \<le> b"
nipkow@29667
   987
by (insert abs_le_D1 [of "uminus a"], simp)
haftmann@25303
   988
haftmann@25303
   989
lemma abs_le_iff: "\<bar>a\<bar> \<le> b \<longleftrightarrow> a \<le> b \<and> - a \<le> b"
nipkow@29667
   990
by (blast intro: abs_leI dest: abs_le_D1 abs_le_D2)
haftmann@25303
   991
haftmann@25303
   992
lemma abs_triangle_ineq2: "\<bar>a\<bar> - \<bar>b\<bar> \<le> \<bar>a - b\<bar>"
nipkow@29667
   993
  apply (simp add: algebra_simps)
nipkow@29667
   994
  apply (subgoal_tac "abs a = abs (plus b (minus a b))")
haftmann@25303
   995
  apply (erule ssubst)
haftmann@25303
   996
  apply (rule abs_triangle_ineq)
nipkow@29667
   997
  apply (rule arg_cong[of _ _ abs])
nipkow@29667
   998
  apply (simp add: algebra_simps)
avigad@16775
   999
done
avigad@16775
  1000
haftmann@25303
  1001
lemma abs_triangle_ineq3: "\<bar>\<bar>a\<bar> - \<bar>b\<bar>\<bar> \<le> \<bar>a - b\<bar>"
haftmann@25303
  1002
  apply (subst abs_le_iff)
haftmann@25303
  1003
  apply auto
haftmann@25303
  1004
  apply (rule abs_triangle_ineq2)
haftmann@25303
  1005
  apply (subst abs_minus_commute)
haftmann@25303
  1006
  apply (rule abs_triangle_ineq2)
avigad@16775
  1007
done
avigad@16775
  1008
haftmann@25303
  1009
lemma abs_triangle_ineq4: "\<bar>a - b\<bar> \<le> \<bar>a\<bar> + \<bar>b\<bar>"
haftmann@25303
  1010
proof -
nipkow@29667
  1011
  have "abs(a - b) = abs(a + - b)" by (subst diff_minus, rule refl)
nipkow@29667
  1012
  also have "... <= abs a + abs (- b)" by (rule abs_triangle_ineq)
nipkow@29667
  1013
  finally show ?thesis by simp
haftmann@25303
  1014
qed
avigad@16775
  1015
haftmann@25303
  1016
lemma abs_diff_triangle_ineq: "\<bar>a + b - (c + d)\<bar> \<le> \<bar>a - c\<bar> + \<bar>b - d\<bar>"
haftmann@25303
  1017
proof -
haftmann@25303
  1018
  have "\<bar>a + b - (c+d)\<bar> = \<bar>(a-c) + (b-d)\<bar>" by (simp add: diff_minus add_ac)
haftmann@25303
  1019
  also have "... \<le> \<bar>a-c\<bar> + \<bar>b-d\<bar>" by (rule abs_triangle_ineq)
haftmann@25303
  1020
  finally show ?thesis .
haftmann@25303
  1021
qed
avigad@16775
  1022
haftmann@25303
  1023
lemma abs_add_abs [simp]:
haftmann@25303
  1024
  "\<bar>\<bar>a\<bar> + \<bar>b\<bar>\<bar> = \<bar>a\<bar> + \<bar>b\<bar>" (is "?L = ?R")
haftmann@25303
  1025
proof (rule antisym)
haftmann@25303
  1026
  show "?L \<ge> ?R" by(rule abs_ge_self)
haftmann@25303
  1027
next
haftmann@25303
  1028
  have "?L \<le> \<bar>\<bar>a\<bar>\<bar> + \<bar>\<bar>b\<bar>\<bar>" by(rule abs_triangle_ineq)
haftmann@25303
  1029
  also have "\<dots> = ?R" by simp
haftmann@25303
  1030
  finally show "?L \<le> ?R" .
haftmann@25303
  1031
qed
haftmann@25303
  1032
haftmann@25303
  1033
end
obua@14738
  1034
obua@14754
  1035
text {* Needed for abelian cancellation simprocs: *}
obua@14754
  1036
obua@14754
  1037
lemma add_cancel_21: "((x::'a::ab_group_add) + (y + z) = y + u) = (x + z = u)"
obua@14754
  1038
apply (subst add_left_commute)
obua@14754
  1039
apply (subst add_left_cancel)
obua@14754
  1040
apply simp
obua@14754
  1041
done
obua@14754
  1042
obua@14754
  1043
lemma add_cancel_end: "(x + (y + z) = y) = (x = - (z::'a::ab_group_add))"
obua@14754
  1044
apply (subst add_cancel_21[of _ _ _ 0, simplified])
obua@14754
  1045
apply (simp add: add_right_cancel[symmetric, of "x" "-z" "z", simplified])
obua@14754
  1046
done
obua@14754
  1047
haftmann@35028
  1048
lemma less_eqI: "(x::'a::ordered_ab_group_add) - y = x' - y' \<Longrightarrow> (x < y) = (x' < y')"
obua@14754
  1049
by (simp add: less_iff_diff_less_0[of x y] less_iff_diff_less_0[of x' y'])
obua@14754
  1050
haftmann@35028
  1051
lemma le_eqI: "(x::'a::ordered_ab_group_add) - y = x' - y' \<Longrightarrow> (y <= x) = (y' <= x')"
obua@14754
  1052
apply (simp add: le_iff_diff_le_0[of y x] le_iff_diff_le_0[of  y' x'])
obua@14754
  1053
apply (simp add: neg_le_iff_le[symmetric, of "y-x" 0] neg_le_iff_le[symmetric, of "y'-x'" 0])
obua@14754
  1054
done
obua@14754
  1055
obua@14754
  1056
lemma eq_eqI: "(x::'a::ab_group_add) - y = x' - y' \<Longrightarrow> (x = y) = (x' = y')"
huffman@30629
  1057
by (simp only: eq_iff_diff_eq_0[of x y] eq_iff_diff_eq_0[of x' y'])
obua@14754
  1058
obua@14754
  1059
lemma diff_def: "(x::'a::ab_group_add) - y == x + (-y)"
obua@14754
  1060
by (simp add: diff_minus)
obua@14754
  1061
haftmann@25090
  1062
lemma le_add_right_mono: 
obua@15178
  1063
  assumes 
haftmann@35028
  1064
  "a <= b + (c::'a::ordered_ab_group_add)"
obua@15178
  1065
  "c <= d"    
obua@15178
  1066
  shows "a <= b + d"
obua@15178
  1067
  apply (rule_tac order_trans[where y = "b+c"])
obua@15178
  1068
  apply (simp_all add: prems)
obua@15178
  1069
  done
obua@15178
  1070
obua@15178
  1071
haftmann@25090
  1072
subsection {* Tools setup *}
haftmann@25090
  1073
haftmann@35028
  1074
lemma add_mono_thms_linordered_semiring [noatp]:
haftmann@35028
  1075
  fixes i j k :: "'a\<Colon>ordered_ab_semigroup_add"
haftmann@25077
  1076
  shows "i \<le> j \<and> k \<le> l \<Longrightarrow> i + k \<le> j + l"
haftmann@25077
  1077
    and "i = j \<and> k \<le> l \<Longrightarrow> i + k \<le> j + l"
haftmann@25077
  1078
    and "i \<le> j \<and> k = l \<Longrightarrow> i + k \<le> j + l"
haftmann@25077
  1079
    and "i = j \<and> k = l \<Longrightarrow> i + k = j + l"
haftmann@25077
  1080
by (rule add_mono, clarify+)+
haftmann@25077
  1081
haftmann@35028
  1082
lemma add_mono_thms_linordered_field [noatp]:
haftmann@35028
  1083
  fixes i j k :: "'a\<Colon>ordered_cancel_ab_semigroup_add"
haftmann@25077
  1084
  shows "i < j \<and> k = l \<Longrightarrow> i + k < j + l"
haftmann@25077
  1085
    and "i = j \<and> k < l \<Longrightarrow> i + k < j + l"
haftmann@25077
  1086
    and "i < j \<and> k \<le> l \<Longrightarrow> i + k < j + l"
haftmann@25077
  1087
    and "i \<le> j \<and> k < l \<Longrightarrow> i + k < j + l"
haftmann@25077
  1088
    and "i < j \<and> k < l \<Longrightarrow> i + k < j + l"
haftmann@25077
  1089
by (auto intro: add_strict_right_mono add_strict_left_mono
haftmann@25077
  1090
  add_less_le_mono add_le_less_mono add_strict_mono)
haftmann@25077
  1091
paulson@17085
  1092
text{*Simplification of @{term "x-y < 0"}, etc.*}
nipkow@29833
  1093
lemmas diff_less_0_iff_less [simp, noatp] = less_iff_diff_less_0 [symmetric]
nipkow@29833
  1094
lemmas diff_le_0_iff_le [simp, noatp] = le_iff_diff_le_0 [symmetric]
paulson@17085
  1095
haftmann@22482
  1096
ML {*
wenzelm@27250
  1097
structure ab_group_add_cancel = Abel_Cancel
wenzelm@27250
  1098
(
haftmann@22482
  1099
haftmann@22482
  1100
(* term order for abelian groups *)
haftmann@22482
  1101
haftmann@22482
  1102
fun agrp_ord (Const (a, _)) = find_index (fn a' => a = a')
haftmann@34973
  1103
      [@{const_name Algebras.zero}, @{const_name Algebras.plus},
haftmann@34973
  1104
        @{const_name Algebras.uminus}, @{const_name Algebras.minus}]
haftmann@22482
  1105
  | agrp_ord _ = ~1;
haftmann@22482
  1106
wenzelm@29269
  1107
fun termless_agrp (a, b) = (TermOrd.term_lpo agrp_ord (a, b) = LESS);
haftmann@22482
  1108
haftmann@22482
  1109
local
haftmann@22482
  1110
  val ac1 = mk_meta_eq @{thm add_assoc};
haftmann@22482
  1111
  val ac2 = mk_meta_eq @{thm add_commute};
haftmann@22482
  1112
  val ac3 = mk_meta_eq @{thm add_left_commute};
haftmann@34973
  1113
  fun solve_add_ac thy _ (_ $ (Const (@{const_name Algebras.plus},_) $ _ $ _) $ _) =
haftmann@22482
  1114
        SOME ac1
haftmann@34973
  1115
    | solve_add_ac thy _ (_ $ x $ (Const (@{const_name Algebras.plus},_) $ y $ z)) =
haftmann@22482
  1116
        if termless_agrp (y, x) then SOME ac3 else NONE
haftmann@22482
  1117
    | solve_add_ac thy _ (_ $ x $ y) =
haftmann@22482
  1118
        if termless_agrp (y, x) then SOME ac2 else NONE
haftmann@22482
  1119
    | solve_add_ac thy _ _ = NONE
haftmann@22482
  1120
in
wenzelm@32010
  1121
  val add_ac_proc = Simplifier.simproc @{theory}
haftmann@22482
  1122
    "add_ac_proc" ["x + y::'a::ab_semigroup_add"] solve_add_ac;
haftmann@22482
  1123
end;
haftmann@22482
  1124
wenzelm@27250
  1125
val eq_reflection = @{thm eq_reflection};
wenzelm@27250
  1126
  
wenzelm@27250
  1127
val T = @{typ "'a::ab_group_add"};
wenzelm@27250
  1128
haftmann@22482
  1129
val cancel_ss = HOL_basic_ss settermless termless_agrp
haftmann@22482
  1130
  addsimprocs [add_ac_proc] addsimps
nipkow@23085
  1131
  [@{thm add_0_left}, @{thm add_0_right}, @{thm diff_def},
haftmann@22482
  1132
   @{thm minus_add_distrib}, @{thm minus_minus}, @{thm minus_zero},
haftmann@22482
  1133
   @{thm right_minus}, @{thm left_minus}, @{thm add_minus_cancel},
haftmann@22482
  1134
   @{thm minus_add_cancel}];
wenzelm@27250
  1135
wenzelm@27250
  1136
val sum_pats = [@{cterm "x + y::'a::ab_group_add"}, @{cterm "x - y::'a::ab_group_add"}];
haftmann@22482
  1137
  
haftmann@22548
  1138
val eqI_rules = [@{thm less_eqI}, @{thm le_eqI}, @{thm eq_eqI}];
haftmann@22482
  1139
haftmann@22482
  1140
val dest_eqI = 
haftmann@22482
  1141
  fst o HOLogic.dest_bin "op =" HOLogic.boolT o HOLogic.dest_Trueprop o concl_of;
haftmann@22482
  1142
wenzelm@27250
  1143
);
haftmann@22482
  1144
*}
haftmann@22482
  1145
wenzelm@26480
  1146
ML {*
haftmann@22482
  1147
  Addsimprocs [ab_group_add_cancel.sum_conv, ab_group_add_cancel.rel_conv];
haftmann@22482
  1148
*}
paulson@17085
  1149
haftmann@33364
  1150
code_modulename SML
haftmann@33364
  1151
  OrderedGroup Arith
haftmann@33364
  1152
haftmann@33364
  1153
code_modulename OCaml
haftmann@33364
  1154
  OrderedGroup Arith
haftmann@33364
  1155
haftmann@33364
  1156
code_modulename Haskell
haftmann@33364
  1157
  OrderedGroup Arith
haftmann@33364
  1158
obua@14738
  1159
end