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