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