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