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