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