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