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