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