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