src/HOL/Groups.thy
author haftmann
Sun Jun 23 21:16:07 2013 +0200 (2013-06-23)
changeset 52435 6646bb548c6b
parent 52289 83ce5d2841e7
child 54147 97a8ff4e4ac9
permissions -rw-r--r--
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
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@52289
   661
class ordered_cancel_comm_monoid_diff = comm_monoid_diff + ordered_ab_semigroup_add_imp_le +
haftmann@52289
   662
  assumes le_iff_add: "a \<le> b \<longleftrightarrow> (\<exists>c. b = a + c)"
haftmann@52289
   663
begin
haftmann@52289
   664
haftmann@52289
   665
context
haftmann@52289
   666
  fixes a b
haftmann@52289
   667
  assumes "a \<le> b"
haftmann@52289
   668
begin
haftmann@52289
   669
haftmann@52289
   670
lemma add_diff_inverse:
haftmann@52289
   671
  "a + (b - a) = b"
haftmann@52289
   672
  using `a \<le> b` by (auto simp add: le_iff_add)
haftmann@52289
   673
haftmann@52289
   674
lemma add_diff_assoc:
haftmann@52289
   675
  "c + (b - a) = c + b - a"
haftmann@52289
   676
  using `a \<le> b` by (auto simp add: le_iff_add add_left_commute [of c])
haftmann@52289
   677
haftmann@52289
   678
lemma add_diff_assoc2:
haftmann@52289
   679
  "b - a + c = b + c - a"
haftmann@52289
   680
  using `a \<le> b` by (auto simp add: le_iff_add add_assoc)
haftmann@52289
   681
haftmann@52289
   682
lemma diff_add_assoc:
haftmann@52289
   683
  "c + b - a = c + (b - a)"
haftmann@52289
   684
  using `a \<le> b` by (simp add: add_commute add_diff_assoc)
haftmann@52289
   685
haftmann@52289
   686
lemma diff_add_assoc2:
haftmann@52289
   687
  "b + c - a = b - a + c"
haftmann@52289
   688
  using `a \<le> b`by (simp add: add_commute add_diff_assoc)
haftmann@52289
   689
haftmann@52289
   690
lemma diff_diff_right:
haftmann@52289
   691
  "c - (b - a) = c + a - b"
haftmann@52289
   692
  by (simp add: add_diff_inverse add_diff_cancel_left [of a c "b - a", symmetric] add_commute)
haftmann@52289
   693
haftmann@52289
   694
lemma diff_add:
haftmann@52289
   695
  "b - a + a = b"
haftmann@52289
   696
  by (simp add: add_commute add_diff_inverse)
haftmann@52289
   697
haftmann@52289
   698
lemma le_add_diff:
haftmann@52289
   699
  "c \<le> b + c - a"
haftmann@52289
   700
  by (auto simp add: add_commute diff_add_assoc2 le_iff_add)
haftmann@52289
   701
haftmann@52289
   702
lemma le_imp_diff_is_add:
haftmann@52289
   703
  "a \<le> b \<Longrightarrow> b - a = c \<longleftrightarrow> b = c + a"
haftmann@52289
   704
  by (auto simp add: add_commute add_diff_inverse)
haftmann@52289
   705
haftmann@52289
   706
lemma le_diff_conv2:
haftmann@52289
   707
  "c \<le> b - a \<longleftrightarrow> c + a \<le> b" (is "?P \<longleftrightarrow> ?Q")
haftmann@52289
   708
proof
haftmann@52289
   709
  assume ?P
haftmann@52289
   710
  then have "c + a \<le> b - a + a" by (rule add_right_mono)
haftmann@52289
   711
  then show ?Q by (simp add: add_diff_inverse add_commute)
haftmann@52289
   712
next
haftmann@52289
   713
  assume ?Q
haftmann@52289
   714
  then have "a + c \<le> a + (b - a)" by (simp add: add_diff_inverse add_commute)
haftmann@52289
   715
  then show ?P by simp
haftmann@52289
   716
qed
haftmann@52289
   717
haftmann@52289
   718
end
haftmann@52289
   719
haftmann@52289
   720
end
haftmann@52289
   721
haftmann@52289
   722
haftmann@25303
   723
subsection {* Support for reasoning about signs *}
haftmann@25303
   724
haftmann@35028
   725
class ordered_comm_monoid_add =
haftmann@35028
   726
  ordered_cancel_ab_semigroup_add + comm_monoid_add
haftmann@25303
   727
begin
haftmann@25303
   728
haftmann@25303
   729
lemma add_pos_nonneg:
nipkow@29667
   730
  assumes "0 < a" and "0 \<le> b" shows "0 < a + b"
haftmann@25303
   731
proof -
haftmann@25303
   732
  have "0 + 0 < a + b" 
haftmann@25303
   733
    using assms by (rule add_less_le_mono)
haftmann@25303
   734
  then show ?thesis by simp
haftmann@25303
   735
qed
haftmann@25303
   736
haftmann@25303
   737
lemma add_pos_pos:
nipkow@29667
   738
  assumes "0 < a" and "0 < b" shows "0 < a + b"
nipkow@29667
   739
by (rule add_pos_nonneg) (insert assms, auto)
haftmann@25303
   740
haftmann@25303
   741
lemma add_nonneg_pos:
nipkow@29667
   742
  assumes "0 \<le> a" and "0 < b" shows "0 < a + b"
haftmann@25303
   743
proof -
haftmann@25303
   744
  have "0 + 0 < a + b" 
haftmann@25303
   745
    using assms by (rule add_le_less_mono)
haftmann@25303
   746
  then show ?thesis by simp
haftmann@25303
   747
qed
haftmann@25303
   748
huffman@36977
   749
lemma add_nonneg_nonneg [simp]:
nipkow@29667
   750
  assumes "0 \<le> a" and "0 \<le> b" shows "0 \<le> a + b"
haftmann@25303
   751
proof -
haftmann@25303
   752
  have "0 + 0 \<le> a + b" 
haftmann@25303
   753
    using assms by (rule add_mono)
haftmann@25303
   754
  then show ?thesis by simp
haftmann@25303
   755
qed
haftmann@25303
   756
huffman@30691
   757
lemma add_neg_nonpos:
nipkow@29667
   758
  assumes "a < 0" and "b \<le> 0" shows "a + b < 0"
haftmann@25303
   759
proof -
haftmann@25303
   760
  have "a + b < 0 + 0"
haftmann@25303
   761
    using assms by (rule add_less_le_mono)
haftmann@25303
   762
  then show ?thesis by simp
haftmann@25303
   763
qed
haftmann@25303
   764
haftmann@25303
   765
lemma add_neg_neg: 
nipkow@29667
   766
  assumes "a < 0" and "b < 0" shows "a + b < 0"
nipkow@29667
   767
by (rule add_neg_nonpos) (insert assms, auto)
haftmann@25303
   768
haftmann@25303
   769
lemma add_nonpos_neg:
nipkow@29667
   770
  assumes "a \<le> 0" and "b < 0" shows "a + b < 0"
haftmann@25303
   771
proof -
haftmann@25303
   772
  have "a + b < 0 + 0"
haftmann@25303
   773
    using assms by (rule add_le_less_mono)
haftmann@25303
   774
  then show ?thesis by simp
haftmann@25303
   775
qed
haftmann@25303
   776
haftmann@25303
   777
lemma add_nonpos_nonpos:
nipkow@29667
   778
  assumes "a \<le> 0" and "b \<le> 0" shows "a + b \<le> 0"
haftmann@25303
   779
proof -
haftmann@25303
   780
  have "a + b \<le> 0 + 0"
haftmann@25303
   781
    using assms by (rule add_mono)
haftmann@25303
   782
  then show ?thesis by simp
haftmann@25303
   783
qed
haftmann@25303
   784
huffman@30691
   785
lemmas add_sign_intros =
huffman@30691
   786
  add_pos_nonneg add_pos_pos add_nonneg_pos add_nonneg_nonneg
huffman@30691
   787
  add_neg_nonpos add_neg_neg add_nonpos_neg add_nonpos_nonpos
huffman@30691
   788
huffman@29886
   789
lemma add_nonneg_eq_0_iff:
huffman@29886
   790
  assumes x: "0 \<le> x" and y: "0 \<le> y"
huffman@29886
   791
  shows "x + y = 0 \<longleftrightarrow> x = 0 \<and> y = 0"
huffman@29886
   792
proof (intro iffI conjI)
huffman@29886
   793
  have "x = x + 0" by simp
huffman@29886
   794
  also have "x + 0 \<le> x + y" using y by (rule add_left_mono)
huffman@29886
   795
  also assume "x + y = 0"
huffman@29886
   796
  also have "0 \<le> x" using x .
huffman@29886
   797
  finally show "x = 0" .
huffman@29886
   798
next
huffman@29886
   799
  have "y = 0 + y" by simp
huffman@29886
   800
  also have "0 + y \<le> x + y" using x by (rule add_right_mono)
huffman@29886
   801
  also assume "x + y = 0"
huffman@29886
   802
  also have "0 \<le> y" using y .
huffman@29886
   803
  finally show "y = 0" .
huffman@29886
   804
next
huffman@29886
   805
  assume "x = 0 \<and> y = 0"
huffman@29886
   806
  then show "x + y = 0" by simp
huffman@29886
   807
qed
huffman@29886
   808
haftmann@25303
   809
end
haftmann@25303
   810
haftmann@35028
   811
class ordered_ab_group_add =
haftmann@35028
   812
  ab_group_add + ordered_ab_semigroup_add
haftmann@25062
   813
begin
haftmann@25062
   814
haftmann@35028
   815
subclass ordered_cancel_ab_semigroup_add ..
haftmann@25062
   816
haftmann@35028
   817
subclass ordered_ab_semigroup_add_imp_le
haftmann@28823
   818
proof
haftmann@25062
   819
  fix a b c :: 'a
haftmann@25062
   820
  assume "c + a \<le> c + b"
haftmann@25062
   821
  hence "(-c) + (c + a) \<le> (-c) + (c + b)" by (rule add_left_mono)
haftmann@25062
   822
  hence "((-c) + c) + a \<le> ((-c) + c) + b" by (simp only: add_assoc)
haftmann@25062
   823
  thus "a \<le> b" by simp
haftmann@25062
   824
qed
haftmann@25062
   825
haftmann@35028
   826
subclass ordered_comm_monoid_add ..
haftmann@25303
   827
haftmann@25077
   828
lemma max_diff_distrib_left:
haftmann@25077
   829
  shows "max x y - z = max (x - z) (y - z)"
nipkow@29667
   830
by (simp add: diff_minus, rule max_add_distrib_left) 
haftmann@25077
   831
haftmann@25077
   832
lemma min_diff_distrib_left:
haftmann@25077
   833
  shows "min x y - z = min (x - z) (y - z)"
nipkow@29667
   834
by (simp add: diff_minus, rule min_add_distrib_left) 
haftmann@25077
   835
haftmann@25077
   836
lemma le_imp_neg_le:
nipkow@29667
   837
  assumes "a \<le> b" shows "-b \<le> -a"
haftmann@25077
   838
proof -
nipkow@29667
   839
  have "-a+a \<le> -a+b" using `a \<le> b` by (rule add_left_mono) 
nipkow@29667
   840
  hence "0 \<le> -a+b" by simp
nipkow@29667
   841
  hence "0 + (-b) \<le> (-a + b) + (-b)" by (rule add_right_mono) 
nipkow@29667
   842
  thus ?thesis by (simp add: add_assoc)
haftmann@25077
   843
qed
haftmann@25077
   844
haftmann@25077
   845
lemma neg_le_iff_le [simp]: "- b \<le> - a \<longleftrightarrow> a \<le> b"
haftmann@25077
   846
proof 
haftmann@25077
   847
  assume "- b \<le> - a"
nipkow@29667
   848
  hence "- (- a) \<le> - (- b)" by (rule le_imp_neg_le)
haftmann@25077
   849
  thus "a\<le>b" by simp
haftmann@25077
   850
next
haftmann@25077
   851
  assume "a\<le>b"
haftmann@25077
   852
  thus "-b \<le> -a" by (rule le_imp_neg_le)
haftmann@25077
   853
qed
haftmann@25077
   854
haftmann@25077
   855
lemma neg_le_0_iff_le [simp]: "- a \<le> 0 \<longleftrightarrow> 0 \<le> a"
nipkow@29667
   856
by (subst neg_le_iff_le [symmetric], simp)
haftmann@25077
   857
haftmann@25077
   858
lemma neg_0_le_iff_le [simp]: "0 \<le> - a \<longleftrightarrow> a \<le> 0"
nipkow@29667
   859
by (subst neg_le_iff_le [symmetric], simp)
haftmann@25077
   860
haftmann@25077
   861
lemma neg_less_iff_less [simp]: "- b < - a \<longleftrightarrow> a < b"
nipkow@29667
   862
by (force simp add: less_le) 
haftmann@25077
   863
haftmann@25077
   864
lemma neg_less_0_iff_less [simp]: "- a < 0 \<longleftrightarrow> 0 < a"
nipkow@29667
   865
by (subst neg_less_iff_less [symmetric], simp)
haftmann@25077
   866
haftmann@25077
   867
lemma neg_0_less_iff_less [simp]: "0 < - a \<longleftrightarrow> a < 0"
nipkow@29667
   868
by (subst neg_less_iff_less [symmetric], simp)
haftmann@25077
   869
haftmann@25077
   870
text{*The next several equations can make the simplifier loop!*}
haftmann@25077
   871
haftmann@25077
   872
lemma less_minus_iff: "a < - b \<longleftrightarrow> b < - a"
haftmann@25077
   873
proof -
haftmann@25077
   874
  have "(- (-a) < - b) = (b < - a)" by (rule neg_less_iff_less)
haftmann@25077
   875
  thus ?thesis by simp
haftmann@25077
   876
qed
haftmann@25077
   877
haftmann@25077
   878
lemma minus_less_iff: "- a < b \<longleftrightarrow> - b < a"
haftmann@25077
   879
proof -
haftmann@25077
   880
  have "(- a < - (-b)) = (- b < a)" by (rule neg_less_iff_less)
haftmann@25077
   881
  thus ?thesis by simp
haftmann@25077
   882
qed
haftmann@25077
   883
haftmann@25077
   884
lemma le_minus_iff: "a \<le> - b \<longleftrightarrow> b \<le> - a"
haftmann@25077
   885
proof -
haftmann@25077
   886
  have mm: "!! a (b::'a). (-(-a)) < -b \<Longrightarrow> -(-b) < -a" by (simp only: minus_less_iff)
haftmann@25077
   887
  have "(- (- a) <= -b) = (b <= - a)" 
haftmann@25077
   888
    apply (auto simp only: le_less)
haftmann@25077
   889
    apply (drule mm)
haftmann@25077
   890
    apply (simp_all)
haftmann@25077
   891
    apply (drule mm[simplified], assumption)
haftmann@25077
   892
    done
haftmann@25077
   893
  then show ?thesis by simp
haftmann@25077
   894
qed
haftmann@25077
   895
haftmann@25077
   896
lemma minus_le_iff: "- a \<le> b \<longleftrightarrow> - b \<le> a"
nipkow@29667
   897
by (auto simp add: le_less minus_less_iff)
haftmann@25077
   898
haftmann@37884
   899
lemma diff_less_0_iff_less [simp, no_atp]:
haftmann@37884
   900
  "a - b < 0 \<longleftrightarrow> a < b"
haftmann@25077
   901
proof -
haftmann@37884
   902
  have "a - b < 0 \<longleftrightarrow> a + (- b) < b + (- b)" by (simp add: diff_minus)
haftmann@37884
   903
  also have "... \<longleftrightarrow> a < b" by (simp only: add_less_cancel_right)
haftmann@25077
   904
  finally show ?thesis .
haftmann@25077
   905
qed
haftmann@25077
   906
haftmann@37884
   907
lemmas less_iff_diff_less_0 = diff_less_0_iff_less [symmetric]
haftmann@37884
   908
haftmann@36348
   909
lemma diff_less_eq[algebra_simps, field_simps]: "a - b < c \<longleftrightarrow> a < c + b"
haftmann@25077
   910
apply (subst less_iff_diff_less_0 [of a])
haftmann@25077
   911
apply (rule less_iff_diff_less_0 [of _ c, THEN ssubst])
haftmann@25077
   912
apply (simp add: diff_minus add_ac)
haftmann@25077
   913
done
haftmann@25077
   914
haftmann@36348
   915
lemma less_diff_eq[algebra_simps, field_simps]: "a < c - b \<longleftrightarrow> a + b < c"
haftmann@36302
   916
apply (subst less_iff_diff_less_0 [of "a + b"])
haftmann@25077
   917
apply (subst less_iff_diff_less_0 [of a])
haftmann@25077
   918
apply (simp add: diff_minus add_ac)
haftmann@25077
   919
done
haftmann@25077
   920
haftmann@36348
   921
lemma diff_le_eq[algebra_simps, field_simps]: "a - b \<le> c \<longleftrightarrow> a \<le> c + b"
nipkow@29667
   922
by (auto simp add: le_less diff_less_eq diff_add_cancel add_diff_cancel)
haftmann@25077
   923
haftmann@36348
   924
lemma le_diff_eq[algebra_simps, field_simps]: "a \<le> c - b \<longleftrightarrow> a + b \<le> c"
nipkow@29667
   925
by (auto simp add: le_less less_diff_eq diff_add_cancel add_diff_cancel)
haftmann@25077
   926
haftmann@37884
   927
lemma diff_le_0_iff_le [simp, no_atp]:
haftmann@37884
   928
  "a - b \<le> 0 \<longleftrightarrow> a \<le> b"
haftmann@37884
   929
  by (simp add: algebra_simps)
haftmann@37884
   930
haftmann@37884
   931
lemmas le_iff_diff_le_0 = diff_le_0_iff_le [symmetric]
haftmann@37884
   932
haftmann@37884
   933
lemma diff_eq_diff_less:
haftmann@37884
   934
  "a - b = c - d \<Longrightarrow> a < b \<longleftrightarrow> c < d"
haftmann@37884
   935
  by (auto simp only: less_iff_diff_less_0 [of a b] less_iff_diff_less_0 [of c d])
haftmann@37884
   936
haftmann@37889
   937
lemma diff_eq_diff_less_eq:
haftmann@37889
   938
  "a - b = c - d \<Longrightarrow> a \<le> b \<longleftrightarrow> c \<le> d"
haftmann@37889
   939
  by (auto simp only: le_iff_diff_le_0 [of a b] le_iff_diff_le_0 [of c d])
haftmann@25077
   940
haftmann@25077
   941
end
haftmann@25077
   942
wenzelm@48891
   943
ML_file "Tools/group_cancel.ML"
huffman@48556
   944
huffman@48556
   945
simproc_setup group_cancel_add ("a + b::'a::ab_group_add") =
huffman@48556
   946
  {* fn phi => fn ss => try Group_Cancel.cancel_add_conv *}
huffman@48556
   947
huffman@48556
   948
simproc_setup group_cancel_diff ("a - b::'a::ab_group_add") =
huffman@48556
   949
  {* fn phi => fn ss => try Group_Cancel.cancel_diff_conv *}
haftmann@37884
   950
huffman@48556
   951
simproc_setup group_cancel_eq ("a = (b::'a::ab_group_add)") =
huffman@48556
   952
  {* fn phi => fn ss => try Group_Cancel.cancel_eq_conv *}
haftmann@37889
   953
huffman@48556
   954
simproc_setup group_cancel_le ("a \<le> (b::'a::ordered_ab_group_add)") =
huffman@48556
   955
  {* fn phi => fn ss => try Group_Cancel.cancel_le_conv *}
huffman@48556
   956
huffman@48556
   957
simproc_setup group_cancel_less ("a < (b::'a::ordered_ab_group_add)") =
huffman@48556
   958
  {* fn phi => fn ss => try Group_Cancel.cancel_less_conv *}
haftmann@37884
   959
haftmann@35028
   960
class linordered_ab_semigroup_add =
haftmann@35028
   961
  linorder + ordered_ab_semigroup_add
haftmann@25062
   962
haftmann@35028
   963
class linordered_cancel_ab_semigroup_add =
haftmann@35028
   964
  linorder + ordered_cancel_ab_semigroup_add
haftmann@25267
   965
begin
haftmann@25062
   966
haftmann@35028
   967
subclass linordered_ab_semigroup_add ..
haftmann@25062
   968
haftmann@35028
   969
subclass ordered_ab_semigroup_add_imp_le
haftmann@28823
   970
proof
haftmann@25062
   971
  fix a b c :: 'a
haftmann@25062
   972
  assume le: "c + a <= c + b"  
haftmann@25062
   973
  show "a <= b"
haftmann@25062
   974
  proof (rule ccontr)
haftmann@25062
   975
    assume w: "~ a \<le> b"
haftmann@25062
   976
    hence "b <= a" by (simp add: linorder_not_le)
haftmann@25062
   977
    hence le2: "c + b <= c + a" by (rule add_left_mono)
haftmann@25062
   978
    have "a = b" 
haftmann@25062
   979
      apply (insert le)
haftmann@25062
   980
      apply (insert le2)
haftmann@25062
   981
      apply (drule antisym, simp_all)
haftmann@25062
   982
      done
haftmann@25062
   983
    with w show False 
haftmann@25062
   984
      by (simp add: linorder_not_le [symmetric])
haftmann@25062
   985
  qed
haftmann@25062
   986
qed
haftmann@25062
   987
haftmann@25267
   988
end
haftmann@25267
   989
haftmann@35028
   990
class linordered_ab_group_add = linorder + ordered_ab_group_add
haftmann@25267
   991
begin
haftmann@25230
   992
haftmann@35028
   993
subclass linordered_cancel_ab_semigroup_add ..
haftmann@25230
   994
haftmann@35036
   995
lemma neg_less_eq_nonneg [simp]:
haftmann@25303
   996
  "- a \<le> a \<longleftrightarrow> 0 \<le> a"
haftmann@25303
   997
proof
haftmann@25303
   998
  assume A: "- a \<le> a" show "0 \<le> a"
haftmann@25303
   999
  proof (rule classical)
haftmann@25303
  1000
    assume "\<not> 0 \<le> a"
haftmann@25303
  1001
    then have "a < 0" by auto
haftmann@25303
  1002
    with A have "- a < 0" by (rule le_less_trans)
haftmann@25303
  1003
    then show ?thesis by auto
haftmann@25303
  1004
  qed
haftmann@25303
  1005
next
haftmann@25303
  1006
  assume A: "0 \<le> a" show "- a \<le> a"
haftmann@25303
  1007
  proof (rule order_trans)
haftmann@25303
  1008
    show "- a \<le> 0" using A by (simp add: minus_le_iff)
haftmann@25303
  1009
  next
haftmann@25303
  1010
    show "0 \<le> a" using A .
haftmann@25303
  1011
  qed
haftmann@25303
  1012
qed
haftmann@35036
  1013
haftmann@35036
  1014
lemma neg_less_nonneg [simp]:
haftmann@35036
  1015
  "- a < a \<longleftrightarrow> 0 < a"
haftmann@35036
  1016
proof
haftmann@35036
  1017
  assume A: "- a < a" show "0 < a"
haftmann@35036
  1018
  proof (rule classical)
haftmann@35036
  1019
    assume "\<not> 0 < a"
haftmann@35036
  1020
    then have "a \<le> 0" by auto
haftmann@35036
  1021
    with A have "- a < 0" by (rule less_le_trans)
haftmann@35036
  1022
    then show ?thesis by auto
haftmann@35036
  1023
  qed
haftmann@35036
  1024
next
haftmann@35036
  1025
  assume A: "0 < a" show "- a < a"
haftmann@35036
  1026
  proof (rule less_trans)
haftmann@35036
  1027
    show "- a < 0" using A by (simp add: minus_le_iff)
haftmann@35036
  1028
  next
haftmann@35036
  1029
    show "0 < a" using A .
haftmann@35036
  1030
  qed
haftmann@35036
  1031
qed
haftmann@35036
  1032
haftmann@35036
  1033
lemma less_eq_neg_nonpos [simp]:
haftmann@25303
  1034
  "a \<le> - a \<longleftrightarrow> a \<le> 0"
haftmann@25303
  1035
proof
haftmann@25303
  1036
  assume A: "a \<le> - a" show "a \<le> 0"
haftmann@25303
  1037
  proof (rule classical)
haftmann@25303
  1038
    assume "\<not> a \<le> 0"
haftmann@25303
  1039
    then have "0 < a" by auto
haftmann@25303
  1040
    then have "0 < - a" using A by (rule less_le_trans)
haftmann@25303
  1041
    then show ?thesis by auto
haftmann@25303
  1042
  qed
haftmann@25303
  1043
next
haftmann@25303
  1044
  assume A: "a \<le> 0" show "a \<le> - a"
haftmann@25303
  1045
  proof (rule order_trans)
haftmann@25303
  1046
    show "0 \<le> - a" using A by (simp add: minus_le_iff)
haftmann@25303
  1047
  next
haftmann@25303
  1048
    show "a \<le> 0" using A .
haftmann@25303
  1049
  qed
haftmann@25303
  1050
qed
haftmann@25303
  1051
haftmann@35036
  1052
lemma equal_neg_zero [simp]:
haftmann@25303
  1053
  "a = - a \<longleftrightarrow> a = 0"
haftmann@25303
  1054
proof
haftmann@25303
  1055
  assume "a = 0" then show "a = - a" by simp
haftmann@25303
  1056
next
haftmann@25303
  1057
  assume A: "a = - a" show "a = 0"
haftmann@25303
  1058
  proof (cases "0 \<le> a")
haftmann@25303
  1059
    case True with A have "0 \<le> - a" by auto
haftmann@25303
  1060
    with le_minus_iff have "a \<le> 0" by simp
haftmann@25303
  1061
    with True show ?thesis by (auto intro: order_trans)
haftmann@25303
  1062
  next
haftmann@25303
  1063
    case False then have B: "a \<le> 0" by auto
haftmann@25303
  1064
    with A have "- a \<le> 0" by auto
haftmann@25303
  1065
    with B show ?thesis by (auto intro: order_trans)
haftmann@25303
  1066
  qed
haftmann@25303
  1067
qed
haftmann@25303
  1068
haftmann@35036
  1069
lemma neg_equal_zero [simp]:
haftmann@25303
  1070
  "- a = a \<longleftrightarrow> a = 0"
haftmann@35036
  1071
  by (auto dest: sym)
haftmann@35036
  1072
haftmann@35036
  1073
lemma double_zero [simp]:
haftmann@35036
  1074
  "a + a = 0 \<longleftrightarrow> a = 0"
haftmann@35036
  1075
proof
haftmann@35036
  1076
  assume assm: "a + a = 0"
haftmann@35036
  1077
  then have a: "- a = a" by (rule minus_unique)
huffman@35216
  1078
  then show "a = 0" by (simp only: neg_equal_zero)
haftmann@35036
  1079
qed simp
haftmann@35036
  1080
haftmann@35036
  1081
lemma double_zero_sym [simp]:
haftmann@35036
  1082
  "0 = a + a \<longleftrightarrow> a = 0"
haftmann@35036
  1083
  by (rule, drule sym) simp_all
haftmann@35036
  1084
haftmann@35036
  1085
lemma zero_less_double_add_iff_zero_less_single_add [simp]:
haftmann@35036
  1086
  "0 < a + a \<longleftrightarrow> 0 < a"
haftmann@35036
  1087
proof
haftmann@35036
  1088
  assume "0 < a + a"
haftmann@35036
  1089
  then have "0 - a < a" by (simp only: diff_less_eq)
haftmann@35036
  1090
  then have "- a < a" by simp
huffman@35216
  1091
  then show "0 < a" by (simp only: neg_less_nonneg)
haftmann@35036
  1092
next
haftmann@35036
  1093
  assume "0 < a"
haftmann@35036
  1094
  with this have "0 + 0 < a + a"
haftmann@35036
  1095
    by (rule add_strict_mono)
haftmann@35036
  1096
  then show "0 < a + a" by simp
haftmann@35036
  1097
qed
haftmann@35036
  1098
haftmann@35036
  1099
lemma zero_le_double_add_iff_zero_le_single_add [simp]:
haftmann@35036
  1100
  "0 \<le> a + a \<longleftrightarrow> 0 \<le> a"
haftmann@35036
  1101
  by (auto simp add: le_less)
haftmann@35036
  1102
haftmann@35036
  1103
lemma double_add_less_zero_iff_single_add_less_zero [simp]:
haftmann@35036
  1104
  "a + a < 0 \<longleftrightarrow> a < 0"
haftmann@35036
  1105
proof -
haftmann@35036
  1106
  have "\<not> a + a < 0 \<longleftrightarrow> \<not> a < 0"
haftmann@35036
  1107
    by (simp add: not_less)
haftmann@35036
  1108
  then show ?thesis by simp
haftmann@35036
  1109
qed
haftmann@35036
  1110
haftmann@35036
  1111
lemma double_add_le_zero_iff_single_add_le_zero [simp]:
haftmann@35036
  1112
  "a + a \<le> 0 \<longleftrightarrow> a \<le> 0" 
haftmann@35036
  1113
proof -
haftmann@35036
  1114
  have "\<not> a + a \<le> 0 \<longleftrightarrow> \<not> a \<le> 0"
haftmann@35036
  1115
    by (simp add: not_le)
haftmann@35036
  1116
  then show ?thesis by simp
haftmann@35036
  1117
qed
haftmann@35036
  1118
haftmann@35036
  1119
lemma le_minus_self_iff:
haftmann@35036
  1120
  "a \<le> - a \<longleftrightarrow> a \<le> 0"
haftmann@35036
  1121
proof -
haftmann@35036
  1122
  from add_le_cancel_left [of "- a" "a + a" 0]
haftmann@35036
  1123
  have "a \<le> - a \<longleftrightarrow> a + a \<le> 0" 
haftmann@35036
  1124
    by (simp add: add_assoc [symmetric])
haftmann@35036
  1125
  thus ?thesis by simp
haftmann@35036
  1126
qed
haftmann@35036
  1127
haftmann@35036
  1128
lemma minus_le_self_iff:
haftmann@35036
  1129
  "- a \<le> a \<longleftrightarrow> 0 \<le> a"
haftmann@35036
  1130
proof -
haftmann@35036
  1131
  from add_le_cancel_left [of "- a" 0 "a + a"]
haftmann@35036
  1132
  have "- a \<le> a \<longleftrightarrow> 0 \<le> a + a" 
haftmann@35036
  1133
    by (simp add: add_assoc [symmetric])
haftmann@35036
  1134
  thus ?thesis by simp
haftmann@35036
  1135
qed
haftmann@35036
  1136
haftmann@35036
  1137
lemma minus_max_eq_min:
haftmann@35036
  1138
  "- max x y = min (-x) (-y)"
haftmann@35036
  1139
  by (auto simp add: max_def min_def)
haftmann@35036
  1140
haftmann@35036
  1141
lemma minus_min_eq_max:
haftmann@35036
  1142
  "- min x y = max (-x) (-y)"
haftmann@35036
  1143
  by (auto simp add: max_def min_def)
haftmann@25303
  1144
haftmann@25267
  1145
end
haftmann@25267
  1146
haftmann@36302
  1147
context ordered_comm_monoid_add
haftmann@36302
  1148
begin
obua@14738
  1149
paulson@15234
  1150
lemma add_increasing:
haftmann@36302
  1151
  "0 \<le> a \<Longrightarrow> b \<le> c \<Longrightarrow> b \<le> a + c"
haftmann@36302
  1152
  by (insert add_mono [of 0 a b c], simp)
obua@14738
  1153
nipkow@15539
  1154
lemma add_increasing2:
haftmann@36302
  1155
  "0 \<le> c \<Longrightarrow> b \<le> a \<Longrightarrow> b \<le> a + c"
haftmann@36302
  1156
  by (simp add: add_increasing add_commute [of a])
nipkow@15539
  1157
paulson@15234
  1158
lemma add_strict_increasing:
haftmann@36302
  1159
  "0 < a \<Longrightarrow> b \<le> c \<Longrightarrow> b < a + c"
haftmann@36302
  1160
  by (insert add_less_le_mono [of 0 a b c], simp)
paulson@15234
  1161
paulson@15234
  1162
lemma add_strict_increasing2:
haftmann@36302
  1163
  "0 \<le> a \<Longrightarrow> b < c \<Longrightarrow> b < a + c"
haftmann@36302
  1164
  by (insert add_le_less_mono [of 0 a b c], simp)
haftmann@36302
  1165
haftmann@36302
  1166
end
paulson@15234
  1167
haftmann@35092
  1168
class abs =
haftmann@35092
  1169
  fixes abs :: "'a \<Rightarrow> 'a"
haftmann@35092
  1170
begin
haftmann@35092
  1171
haftmann@35092
  1172
notation (xsymbols)
haftmann@35092
  1173
  abs  ("\<bar>_\<bar>")
haftmann@35092
  1174
haftmann@35092
  1175
notation (HTML output)
haftmann@35092
  1176
  abs  ("\<bar>_\<bar>")
haftmann@35092
  1177
haftmann@35092
  1178
end
haftmann@35092
  1179
haftmann@35092
  1180
class sgn =
haftmann@35092
  1181
  fixes sgn :: "'a \<Rightarrow> 'a"
haftmann@35092
  1182
haftmann@35092
  1183
class abs_if = minus + uminus + ord + zero + abs +
haftmann@35092
  1184
  assumes abs_if: "\<bar>a\<bar> = (if a < 0 then - a else a)"
haftmann@35092
  1185
haftmann@35092
  1186
class sgn_if = minus + uminus + zero + one + ord + sgn +
haftmann@35092
  1187
  assumes sgn_if: "sgn x = (if x = 0 then 0 else if 0 < x then 1 else - 1)"
haftmann@35092
  1188
begin
haftmann@35092
  1189
haftmann@35092
  1190
lemma sgn0 [simp]: "sgn 0 = 0"
haftmann@35092
  1191
  by (simp add:sgn_if)
haftmann@35092
  1192
haftmann@35092
  1193
end
obua@14738
  1194
haftmann@35028
  1195
class ordered_ab_group_add_abs = ordered_ab_group_add + abs +
haftmann@25303
  1196
  assumes abs_ge_zero [simp]: "\<bar>a\<bar> \<ge> 0"
haftmann@25303
  1197
    and abs_ge_self: "a \<le> \<bar>a\<bar>"
haftmann@25303
  1198
    and abs_leI: "a \<le> b \<Longrightarrow> - a \<le> b \<Longrightarrow> \<bar>a\<bar> \<le> b"
haftmann@25303
  1199
    and abs_minus_cancel [simp]: "\<bar>-a\<bar> = \<bar>a\<bar>"
haftmann@25303
  1200
    and abs_triangle_ineq: "\<bar>a + b\<bar> \<le> \<bar>a\<bar> + \<bar>b\<bar>"
haftmann@25303
  1201
begin
haftmann@25303
  1202
haftmann@25307
  1203
lemma abs_minus_le_zero: "- \<bar>a\<bar> \<le> 0"
haftmann@25307
  1204
  unfolding neg_le_0_iff_le by simp
haftmann@25307
  1205
haftmann@25307
  1206
lemma abs_of_nonneg [simp]:
nipkow@29667
  1207
  assumes nonneg: "0 \<le> a" shows "\<bar>a\<bar> = a"
haftmann@25307
  1208
proof (rule antisym)
haftmann@25307
  1209
  from nonneg le_imp_neg_le have "- a \<le> 0" by simp
haftmann@25307
  1210
  from this nonneg have "- a \<le> a" by (rule order_trans)
haftmann@25307
  1211
  then show "\<bar>a\<bar> \<le> a" by (auto intro: abs_leI)
haftmann@25307
  1212
qed (rule abs_ge_self)
haftmann@25307
  1213
haftmann@25307
  1214
lemma abs_idempotent [simp]: "\<bar>\<bar>a\<bar>\<bar> = \<bar>a\<bar>"
nipkow@29667
  1215
by (rule antisym)
haftmann@36302
  1216
   (auto intro!: abs_ge_self abs_leI order_trans [of "- \<bar>a\<bar>" 0 "\<bar>a\<bar>"])
haftmann@25307
  1217
haftmann@25307
  1218
lemma abs_eq_0 [simp]: "\<bar>a\<bar> = 0 \<longleftrightarrow> a = 0"
haftmann@25307
  1219
proof -
haftmann@25307
  1220
  have "\<bar>a\<bar> = 0 \<Longrightarrow> a = 0"
haftmann@25307
  1221
  proof (rule antisym)
haftmann@25307
  1222
    assume zero: "\<bar>a\<bar> = 0"
haftmann@25307
  1223
    with abs_ge_self show "a \<le> 0" by auto
haftmann@25307
  1224
    from zero have "\<bar>-a\<bar> = 0" by simp
haftmann@36302
  1225
    with abs_ge_self [of "- a"] have "- a \<le> 0" by auto
haftmann@25307
  1226
    with neg_le_0_iff_le show "0 \<le> a" by auto
haftmann@25307
  1227
  qed
haftmann@25307
  1228
  then show ?thesis by auto
haftmann@25307
  1229
qed
haftmann@25307
  1230
haftmann@25303
  1231
lemma abs_zero [simp]: "\<bar>0\<bar> = 0"
nipkow@29667
  1232
by simp
avigad@16775
  1233
blanchet@35828
  1234
lemma abs_0_eq [simp, no_atp]: "0 = \<bar>a\<bar> \<longleftrightarrow> a = 0"
haftmann@25303
  1235
proof -
haftmann@25303
  1236
  have "0 = \<bar>a\<bar> \<longleftrightarrow> \<bar>a\<bar> = 0" by (simp only: eq_ac)
haftmann@25303
  1237
  thus ?thesis by simp
haftmann@25303
  1238
qed
haftmann@25303
  1239
haftmann@25303
  1240
lemma abs_le_zero_iff [simp]: "\<bar>a\<bar> \<le> 0 \<longleftrightarrow> a = 0" 
haftmann@25303
  1241
proof
haftmann@25303
  1242
  assume "\<bar>a\<bar> \<le> 0"
haftmann@25303
  1243
  then have "\<bar>a\<bar> = 0" by (rule antisym) simp
haftmann@25303
  1244
  thus "a = 0" by simp
haftmann@25303
  1245
next
haftmann@25303
  1246
  assume "a = 0"
haftmann@25303
  1247
  thus "\<bar>a\<bar> \<le> 0" by simp
haftmann@25303
  1248
qed
haftmann@25303
  1249
haftmann@25303
  1250
lemma zero_less_abs_iff [simp]: "0 < \<bar>a\<bar> \<longleftrightarrow> a \<noteq> 0"
nipkow@29667
  1251
by (simp add: less_le)
haftmann@25303
  1252
haftmann@25303
  1253
lemma abs_not_less_zero [simp]: "\<not> \<bar>a\<bar> < 0"
haftmann@25303
  1254
proof -
haftmann@25303
  1255
  have a: "\<And>x y. x \<le> y \<Longrightarrow> \<not> y < x" by auto
haftmann@25303
  1256
  show ?thesis by (simp add: a)
haftmann@25303
  1257
qed
avigad@16775
  1258
haftmann@25303
  1259
lemma abs_ge_minus_self: "- a \<le> \<bar>a\<bar>"
haftmann@25303
  1260
proof -
haftmann@25303
  1261
  have "- a \<le> \<bar>-a\<bar>" by (rule abs_ge_self)
haftmann@25303
  1262
  then show ?thesis by simp
haftmann@25303
  1263
qed
haftmann@25303
  1264
haftmann@25303
  1265
lemma abs_minus_commute: 
haftmann@25303
  1266
  "\<bar>a - b\<bar> = \<bar>b - a\<bar>"
haftmann@25303
  1267
proof -
haftmann@25303
  1268
  have "\<bar>a - b\<bar> = \<bar>- (a - b)\<bar>" by (simp only: abs_minus_cancel)
haftmann@25303
  1269
  also have "... = \<bar>b - a\<bar>" by simp
haftmann@25303
  1270
  finally show ?thesis .
haftmann@25303
  1271
qed
haftmann@25303
  1272
haftmann@25303
  1273
lemma abs_of_pos: "0 < a \<Longrightarrow> \<bar>a\<bar> = a"
nipkow@29667
  1274
by (rule abs_of_nonneg, rule less_imp_le)
avigad@16775
  1275
haftmann@25303
  1276
lemma abs_of_nonpos [simp]:
nipkow@29667
  1277
  assumes "a \<le> 0" shows "\<bar>a\<bar> = - a"
haftmann@25303
  1278
proof -
haftmann@25303
  1279
  let ?b = "- a"
haftmann@25303
  1280
  have "- ?b \<le> 0 \<Longrightarrow> \<bar>- ?b\<bar> = - (- ?b)"
haftmann@25303
  1281
  unfolding abs_minus_cancel [of "?b"]
haftmann@25303
  1282
  unfolding neg_le_0_iff_le [of "?b"]
haftmann@25303
  1283
  unfolding minus_minus by (erule abs_of_nonneg)
haftmann@25303
  1284
  then show ?thesis using assms by auto
haftmann@25303
  1285
qed
haftmann@25303
  1286
  
haftmann@25303
  1287
lemma abs_of_neg: "a < 0 \<Longrightarrow> \<bar>a\<bar> = - a"
nipkow@29667
  1288
by (rule abs_of_nonpos, rule less_imp_le)
haftmann@25303
  1289
haftmann@25303
  1290
lemma abs_le_D1: "\<bar>a\<bar> \<le> b \<Longrightarrow> a \<le> b"
nipkow@29667
  1291
by (insert abs_ge_self, blast intro: order_trans)
haftmann@25303
  1292
haftmann@25303
  1293
lemma abs_le_D2: "\<bar>a\<bar> \<le> b \<Longrightarrow> - a \<le> b"
haftmann@36302
  1294
by (insert abs_le_D1 [of "- a"], simp)
haftmann@25303
  1295
haftmann@25303
  1296
lemma abs_le_iff: "\<bar>a\<bar> \<le> b \<longleftrightarrow> a \<le> b \<and> - a \<le> b"
nipkow@29667
  1297
by (blast intro: abs_leI dest: abs_le_D1 abs_le_D2)
haftmann@25303
  1298
haftmann@25303
  1299
lemma abs_triangle_ineq2: "\<bar>a\<bar> - \<bar>b\<bar> \<le> \<bar>a - b\<bar>"
haftmann@36302
  1300
proof -
haftmann@36302
  1301
  have "\<bar>a\<bar> = \<bar>b + (a - b)\<bar>"
haftmann@36302
  1302
    by (simp add: algebra_simps add_diff_cancel)
haftmann@36302
  1303
  then have "\<bar>a\<bar> \<le> \<bar>b\<bar> + \<bar>a - b\<bar>"
haftmann@36302
  1304
    by (simp add: abs_triangle_ineq)
haftmann@36302
  1305
  then show ?thesis
haftmann@36302
  1306
    by (simp add: algebra_simps)
haftmann@36302
  1307
qed
haftmann@36302
  1308
haftmann@36302
  1309
lemma abs_triangle_ineq2_sym: "\<bar>a\<bar> - \<bar>b\<bar> \<le> \<bar>b - a\<bar>"
haftmann@36302
  1310
  by (simp only: abs_minus_commute [of b] abs_triangle_ineq2)
avigad@16775
  1311
haftmann@25303
  1312
lemma abs_triangle_ineq3: "\<bar>\<bar>a\<bar> - \<bar>b\<bar>\<bar> \<le> \<bar>a - b\<bar>"
haftmann@36302
  1313
  by (simp add: abs_le_iff abs_triangle_ineq2 abs_triangle_ineq2_sym)
avigad@16775
  1314
haftmann@25303
  1315
lemma abs_triangle_ineq4: "\<bar>a - b\<bar> \<le> \<bar>a\<bar> + \<bar>b\<bar>"
haftmann@25303
  1316
proof -
haftmann@36302
  1317
  have "\<bar>a - b\<bar> = \<bar>a + - b\<bar>" by (subst diff_minus, rule refl)
haftmann@36302
  1318
  also have "... \<le> \<bar>a\<bar> + \<bar>- b\<bar>" by (rule abs_triangle_ineq)
nipkow@29667
  1319
  finally show ?thesis by simp
haftmann@25303
  1320
qed
avigad@16775
  1321
haftmann@25303
  1322
lemma abs_diff_triangle_ineq: "\<bar>a + b - (c + d)\<bar> \<le> \<bar>a - c\<bar> + \<bar>b - d\<bar>"
haftmann@25303
  1323
proof -
haftmann@25303
  1324
  have "\<bar>a + b - (c+d)\<bar> = \<bar>(a-c) + (b-d)\<bar>" by (simp add: diff_minus add_ac)
haftmann@25303
  1325
  also have "... \<le> \<bar>a-c\<bar> + \<bar>b-d\<bar>" by (rule abs_triangle_ineq)
haftmann@25303
  1326
  finally show ?thesis .
haftmann@25303
  1327
qed
avigad@16775
  1328
haftmann@25303
  1329
lemma abs_add_abs [simp]:
haftmann@25303
  1330
  "\<bar>\<bar>a\<bar> + \<bar>b\<bar>\<bar> = \<bar>a\<bar> + \<bar>b\<bar>" (is "?L = ?R")
haftmann@25303
  1331
proof (rule antisym)
haftmann@25303
  1332
  show "?L \<ge> ?R" by(rule abs_ge_self)
haftmann@25303
  1333
next
haftmann@25303
  1334
  have "?L \<le> \<bar>\<bar>a\<bar>\<bar> + \<bar>\<bar>b\<bar>\<bar>" by(rule abs_triangle_ineq)
haftmann@25303
  1335
  also have "\<dots> = ?R" by simp
haftmann@25303
  1336
  finally show "?L \<le> ?R" .
haftmann@25303
  1337
qed
haftmann@25303
  1338
haftmann@25303
  1339
end
obua@14738
  1340
obua@15178
  1341
haftmann@25090
  1342
subsection {* Tools setup *}
haftmann@25090
  1343
blanchet@35828
  1344
lemma add_mono_thms_linordered_semiring [no_atp]:
haftmann@35028
  1345
  fixes i j k :: "'a\<Colon>ordered_ab_semigroup_add"
haftmann@25077
  1346
  shows "i \<le> j \<and> k \<le> l \<Longrightarrow> i + k \<le> j + l"
haftmann@25077
  1347
    and "i = j \<and> k \<le> l \<Longrightarrow> i + k \<le> j + l"
haftmann@25077
  1348
    and "i \<le> j \<and> k = l \<Longrightarrow> i + k \<le> j + l"
haftmann@25077
  1349
    and "i = j \<and> k = l \<Longrightarrow> i + k = j + l"
haftmann@25077
  1350
by (rule add_mono, clarify+)+
haftmann@25077
  1351
blanchet@35828
  1352
lemma add_mono_thms_linordered_field [no_atp]:
haftmann@35028
  1353
  fixes i j k :: "'a\<Colon>ordered_cancel_ab_semigroup_add"
haftmann@25077
  1354
  shows "i < j \<and> k = l \<Longrightarrow> i + k < j + l"
haftmann@25077
  1355
    and "i = j \<and> k < l \<Longrightarrow> i + k < j + l"
haftmann@25077
  1356
    and "i < j \<and> k \<le> l \<Longrightarrow> i + k < j + l"
haftmann@25077
  1357
    and "i \<le> j \<and> k < l \<Longrightarrow> i + k < j + l"
haftmann@25077
  1358
    and "i < j \<and> k < l \<Longrightarrow> i + k < j + l"
haftmann@25077
  1359
by (auto intro: add_strict_right_mono add_strict_left_mono
haftmann@25077
  1360
  add_less_le_mono add_le_less_mono add_strict_mono)
haftmann@25077
  1361
haftmann@52435
  1362
code_identifier
haftmann@52435
  1363
  code_module Groups \<rightharpoonup> (SML) Arith and (OCaml) Arith and (Haskell) Arith
haftmann@33364
  1364
haftmann@37889
  1365
haftmann@37889
  1366
text {* Legacy *}
haftmann@37889
  1367
haftmann@37889
  1368
lemmas diff_def = diff_minus
haftmann@37889
  1369
obua@14738
  1370
end
haftmann@49388
  1371