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