src/HOL/OrderedGroup.thy
author nipkow
Wed Jan 28 16:29:16 2009 +0100 (2009-01-28)
changeset 29667 53103fc8ffa3
parent 29269 5c25a2012975
child 29668 33ba3faeaa0e
permissions -rw-r--r--
Replaced group_ and ring_simps by algebra_simps;
removed compare_rls - use algebra_simps now
wenzelm@14770
     1
(*  Title:   HOL/OrderedGroup.thy
wenzelm@29269
     2
    Author:  Gertrud Bauer, Steven Obua, Lawrence C Paulson, Markus Wenzel, Jeremy Avigad
obua@14738
     3
*)
obua@14738
     4
obua@14738
     5
header {* Ordered Groups *}
obua@14738
     6
nipkow@15131
     7
theory OrderedGroup
haftmann@22452
     8
imports Lattices
wenzelm@19798
     9
uses "~~/src/Provers/Arith/abel_cancel.ML"
nipkow@15131
    10
begin
obua@14738
    11
obua@14738
    12
text {*
obua@14738
    13
  The theory of partially ordered groups is taken from the books:
obua@14738
    14
  \begin{itemize}
obua@14738
    15
  \item \emph{Lattice Theory} by Garret Birkhoff, American Mathematical Society 1979 
obua@14738
    16
  \item \emph{Partially Ordered Algebraic Systems}, Pergamon Press 1963
obua@14738
    17
  \end{itemize}
obua@14738
    18
  Most of the used notions can also be looked up in 
obua@14738
    19
  \begin{itemize}
wenzelm@14770
    20
  \item \url{http://www.mathworld.com} by Eric Weisstein et. al.
obua@14738
    21
  \item \emph{Algebra I} by van der Waerden, Springer.
obua@14738
    22
  \end{itemize}
obua@14738
    23
*}
obua@14738
    24
nipkow@29667
    25
ML{*
nipkow@29667
    26
structure AlgebraSimps =
nipkow@29667
    27
  NamedThmsFun(val name = "algebra_simps"
nipkow@29667
    28
               val description = "algebra simplification rules");
nipkow@29667
    29
*}
nipkow@29667
    30
nipkow@29667
    31
setup AlgebraSimps.setup
nipkow@29667
    32
nipkow@29667
    33
text{* The rewrites accumulated in @{text algebra_simps} deal with the
nipkow@29667
    34
classical algebraic structures of groups, rings and family. They simplify
nipkow@29667
    35
terms by multiplying everything out (in case of a ring) and bringing sums and
nipkow@29667
    36
products into a canonical form (by ordered rewriting). As a result it decides
nipkow@29667
    37
group and ring equalities but also helps with inequalities.
nipkow@29667
    38
nipkow@29667
    39
Of course it also works for fields, but it knows nothing about multiplicative
nipkow@29667
    40
inverses or division. This is catered for by @{text field_simps}. *}
nipkow@29667
    41
nipkow@23085
    42
subsection {* Semigroups and Monoids *}
obua@14738
    43
haftmann@22390
    44
class semigroup_add = plus +
nipkow@29667
    45
  assumes add_assoc[algebra_simps]: "(a + b) + c = a + (b + c)"
haftmann@22390
    46
haftmann@22390
    47
class ab_semigroup_add = semigroup_add +
nipkow@29667
    48
  assumes add_commute[algebra_simps]: "a + b = b + a"
haftmann@25062
    49
begin
obua@14738
    50
nipkow@29667
    51
lemma add_left_commute[algebra_simps]: "a + (b + c) = b + (a + c)"
nipkow@29667
    52
by (rule mk_left_commute [of "plus", OF add_assoc add_commute])
haftmann@25062
    53
haftmann@25062
    54
theorems add_ac = add_assoc add_commute add_left_commute
haftmann@25062
    55
haftmann@25062
    56
end
obua@14738
    57
obua@14738
    58
theorems add_ac = add_assoc add_commute add_left_commute
obua@14738
    59
haftmann@22390
    60
class semigroup_mult = times +
nipkow@29667
    61
  assumes mult_assoc[algebra_simps]: "(a * b) * c = a * (b * c)"
obua@14738
    62
haftmann@22390
    63
class ab_semigroup_mult = semigroup_mult +
nipkow@29667
    64
  assumes mult_commute[algebra_simps]: "a * b = b * a"
haftmann@23181
    65
begin
obua@14738
    66
nipkow@29667
    67
lemma mult_left_commute[algebra_simps]: "a * (b * c) = b * (a * c)"
nipkow@29667
    68
by (rule mk_left_commute [of "times", OF mult_assoc mult_commute])
haftmann@25062
    69
haftmann@25062
    70
theorems mult_ac = mult_assoc mult_commute mult_left_commute
haftmann@23181
    71
haftmann@23181
    72
end
obua@14738
    73
obua@14738
    74
theorems mult_ac = mult_assoc mult_commute mult_left_commute
obua@14738
    75
haftmann@26015
    76
class ab_semigroup_idem_mult = ab_semigroup_mult +
nipkow@29667
    77
  assumes mult_idem[simp]: "x * x = x"
haftmann@26015
    78
begin
haftmann@26015
    79
nipkow@29667
    80
lemma mult_left_idem[simp]: "x * (x * y) = x * y"
haftmann@26015
    81
  unfolding mult_assoc [symmetric, of x] mult_idem ..
haftmann@26015
    82
haftmann@26015
    83
end
haftmann@26015
    84
nipkow@23085
    85
class monoid_add = zero + semigroup_add +
haftmann@25062
    86
  assumes add_0_left [simp]: "0 + a = a"
haftmann@25062
    87
    and add_0_right [simp]: "a + 0 = a"
nipkow@23085
    88
haftmann@26071
    89
lemma zero_reorient: "0 = x \<longleftrightarrow> x = 0"
nipkow@29667
    90
by (rule eq_commute)
haftmann@26071
    91
haftmann@22390
    92
class comm_monoid_add = zero + ab_semigroup_add +
haftmann@25062
    93
  assumes add_0: "0 + a = a"
haftmann@25062
    94
begin
nipkow@23085
    95
haftmann@25062
    96
subclass monoid_add
haftmann@28823
    97
  proof qed (insert add_0, simp_all add: add_commute)
haftmann@25062
    98
haftmann@25062
    99
end
obua@14738
   100
haftmann@22390
   101
class monoid_mult = one + semigroup_mult +
haftmann@25062
   102
  assumes mult_1_left [simp]: "1 * a  = a"
haftmann@25062
   103
  assumes mult_1_right [simp]: "a * 1 = a"
obua@14738
   104
haftmann@26071
   105
lemma one_reorient: "1 = x \<longleftrightarrow> x = 1"
nipkow@29667
   106
by (rule eq_commute)
haftmann@26071
   107
haftmann@22390
   108
class comm_monoid_mult = one + ab_semigroup_mult +
haftmann@25062
   109
  assumes mult_1: "1 * a = a"
haftmann@25062
   110
begin
obua@14738
   111
haftmann@25062
   112
subclass monoid_mult
haftmann@28823
   113
  proof qed (insert mult_1, simp_all add: mult_commute)
haftmann@25062
   114
haftmann@25062
   115
end
obua@14738
   116
haftmann@22390
   117
class cancel_semigroup_add = semigroup_add +
haftmann@25062
   118
  assumes add_left_imp_eq: "a + b = a + c \<Longrightarrow> b = c"
haftmann@25062
   119
  assumes add_right_imp_eq: "b + a = c + a \<Longrightarrow> b = c"
huffman@27474
   120
begin
huffman@27474
   121
huffman@27474
   122
lemma add_left_cancel [simp]:
huffman@27474
   123
  "a + b = a + c \<longleftrightarrow> b = c"
nipkow@29667
   124
by (blast dest: add_left_imp_eq)
huffman@27474
   125
huffman@27474
   126
lemma add_right_cancel [simp]:
huffman@27474
   127
  "b + a = c + a \<longleftrightarrow> b = c"
nipkow@29667
   128
by (blast dest: add_right_imp_eq)
huffman@27474
   129
huffman@27474
   130
end
obua@14738
   131
haftmann@22390
   132
class cancel_ab_semigroup_add = ab_semigroup_add +
haftmann@25062
   133
  assumes add_imp_eq: "a + b = a + c \<Longrightarrow> b = c"
haftmann@25267
   134
begin
obua@14738
   135
haftmann@25267
   136
subclass cancel_semigroup_add
haftmann@28823
   137
proof
haftmann@22390
   138
  fix a b c :: 'a
haftmann@22390
   139
  assume "a + b = a + c" 
haftmann@22390
   140
  then show "b = c" by (rule add_imp_eq)
haftmann@22390
   141
next
obua@14738
   142
  fix a b c :: 'a
obua@14738
   143
  assume "b + a = c + a"
haftmann@22390
   144
  then have "a + b = a + c" by (simp only: add_commute)
haftmann@22390
   145
  then show "b = c" by (rule add_imp_eq)
obua@14738
   146
qed
obua@14738
   147
haftmann@25267
   148
end
haftmann@25267
   149
nipkow@23085
   150
subsection {* Groups *}
nipkow@23085
   151
haftmann@25762
   152
class group_add = minus + uminus + monoid_add +
haftmann@25062
   153
  assumes left_minus [simp]: "- a + a = 0"
haftmann@25062
   154
  assumes diff_minus: "a - b = a + (- b)"
haftmann@25062
   155
begin
nipkow@23085
   156
haftmann@25062
   157
lemma minus_add_cancel: "- a + (a + b) = b"
nipkow@29667
   158
by (simp add: add_assoc[symmetric])
obua@14738
   159
haftmann@25062
   160
lemma minus_zero [simp]: "- 0 = 0"
obua@14738
   161
proof -
haftmann@25062
   162
  have "- 0 = - 0 + (0 + 0)" by (simp only: add_0_right)
haftmann@25062
   163
  also have "\<dots> = 0" by (rule minus_add_cancel)
obua@14738
   164
  finally show ?thesis .
obua@14738
   165
qed
obua@14738
   166
haftmann@25062
   167
lemma minus_minus [simp]: "- (- a) = a"
nipkow@23085
   168
proof -
haftmann@25062
   169
  have "- (- a) = - (- a) + (- a + a)" by simp
haftmann@25062
   170
  also have "\<dots> = a" by (rule minus_add_cancel)
nipkow@23085
   171
  finally show ?thesis .
nipkow@23085
   172
qed
obua@14738
   173
haftmann@25062
   174
lemma right_minus [simp]: "a + - a = 0"
obua@14738
   175
proof -
haftmann@25062
   176
  have "a + - a = - (- a) + - a" by simp
haftmann@25062
   177
  also have "\<dots> = 0" by (rule left_minus)
obua@14738
   178
  finally show ?thesis .
obua@14738
   179
qed
obua@14738
   180
haftmann@25062
   181
lemma right_minus_eq: "a - b = 0 \<longleftrightarrow> a = b"
obua@14738
   182
proof
nipkow@23085
   183
  assume "a - b = 0"
nipkow@23085
   184
  have "a = (a - b) + b" by (simp add:diff_minus add_assoc)
nipkow@23085
   185
  also have "\<dots> = b" using `a - b = 0` by simp
nipkow@23085
   186
  finally show "a = b" .
obua@14738
   187
next
nipkow@23085
   188
  assume "a = b" thus "a - b = 0" by (simp add: diff_minus)
obua@14738
   189
qed
obua@14738
   190
haftmann@25062
   191
lemma equals_zero_I:
nipkow@29667
   192
  assumes "a + b = 0" shows "- a = b"
nipkow@23085
   193
proof -
haftmann@25062
   194
  have "- a = - a + (a + b)" using assms by simp
haftmann@25062
   195
  also have "\<dots> = b" by (simp add: add_assoc[symmetric])
nipkow@23085
   196
  finally show ?thesis .
nipkow@23085
   197
qed
obua@14738
   198
haftmann@25062
   199
lemma diff_self [simp]: "a - a = 0"
nipkow@29667
   200
by (simp add: diff_minus)
obua@14738
   201
haftmann@25062
   202
lemma diff_0 [simp]: "0 - a = - a"
nipkow@29667
   203
by (simp add: diff_minus)
obua@14738
   204
haftmann@25062
   205
lemma diff_0_right [simp]: "a - 0 = a" 
nipkow@29667
   206
by (simp add: diff_minus)
obua@14738
   207
haftmann@25062
   208
lemma diff_minus_eq_add [simp]: "a - - b = a + b"
nipkow@29667
   209
by (simp add: diff_minus)
obua@14738
   210
haftmann@25062
   211
lemma neg_equal_iff_equal [simp]:
haftmann@25062
   212
  "- a = - b \<longleftrightarrow> a = b" 
obua@14738
   213
proof 
obua@14738
   214
  assume "- a = - b"
nipkow@29667
   215
  hence "- (- a) = - (- b)" by simp
haftmann@25062
   216
  thus "a = b" by simp
obua@14738
   217
next
haftmann@25062
   218
  assume "a = b"
haftmann@25062
   219
  thus "- a = - b" by simp
obua@14738
   220
qed
obua@14738
   221
haftmann@25062
   222
lemma neg_equal_0_iff_equal [simp]:
haftmann@25062
   223
  "- a = 0 \<longleftrightarrow> a = 0"
nipkow@29667
   224
by (subst neg_equal_iff_equal [symmetric], simp)
obua@14738
   225
haftmann@25062
   226
lemma neg_0_equal_iff_equal [simp]:
haftmann@25062
   227
  "0 = - a \<longleftrightarrow> 0 = a"
nipkow@29667
   228
by (subst neg_equal_iff_equal [symmetric], simp)
obua@14738
   229
obua@14738
   230
text{*The next two equations can make the simplifier loop!*}
obua@14738
   231
haftmann@25062
   232
lemma equation_minus_iff:
haftmann@25062
   233
  "a = - b \<longleftrightarrow> b = - a"
obua@14738
   234
proof -
haftmann@25062
   235
  have "- (- a) = - b \<longleftrightarrow> - a = b" by (rule neg_equal_iff_equal)
haftmann@25062
   236
  thus ?thesis by (simp add: eq_commute)
haftmann@25062
   237
qed
haftmann@25062
   238
haftmann@25062
   239
lemma minus_equation_iff:
haftmann@25062
   240
  "- a = b \<longleftrightarrow> - b = a"
haftmann@25062
   241
proof -
haftmann@25062
   242
  have "- a = - (- b) \<longleftrightarrow> a = -b" by (rule neg_equal_iff_equal)
obua@14738
   243
  thus ?thesis by (simp add: eq_commute)
obua@14738
   244
qed
obua@14738
   245
huffman@28130
   246
lemma diff_add_cancel: "a - b + b = a"
nipkow@29667
   247
by (simp add: diff_minus add_assoc)
huffman@28130
   248
huffman@28130
   249
lemma add_diff_cancel: "a + b - b = a"
nipkow@29667
   250
by (simp add: diff_minus add_assoc)
nipkow@29667
   251
nipkow@29667
   252
declare diff_minus[symmetric, algebra_simps]
huffman@28130
   253
haftmann@25062
   254
end
haftmann@25062
   255
haftmann@25762
   256
class ab_group_add = minus + uminus + comm_monoid_add +
haftmann@25062
   257
  assumes ab_left_minus: "- a + a = 0"
haftmann@25062
   258
  assumes ab_diff_minus: "a - b = a + (- b)"
haftmann@25267
   259
begin
haftmann@25062
   260
haftmann@25267
   261
subclass group_add
haftmann@28823
   262
  proof qed (simp_all add: ab_left_minus ab_diff_minus)
haftmann@25062
   263
haftmann@25267
   264
subclass cancel_ab_semigroup_add
haftmann@28823
   265
proof
haftmann@25062
   266
  fix a b c :: 'a
haftmann@25062
   267
  assume "a + b = a + c"
haftmann@25062
   268
  then have "- a + a + b = - a + a + c"
haftmann@25062
   269
    unfolding add_assoc by simp
haftmann@25062
   270
  then show "b = c" by simp
haftmann@25062
   271
qed
haftmann@25062
   272
nipkow@29667
   273
lemma uminus_add_conv_diff[algebra_simps]:
haftmann@25062
   274
  "- a + b = b - a"
nipkow@29667
   275
by (simp add:diff_minus add_commute)
haftmann@25062
   276
haftmann@25062
   277
lemma minus_add_distrib [simp]:
haftmann@25062
   278
  "- (a + b) = - a + - b"
nipkow@29667
   279
by (rule equals_zero_I) (simp add: add_ac)
haftmann@25062
   280
haftmann@25062
   281
lemma minus_diff_eq [simp]:
haftmann@25062
   282
  "- (a - b) = b - a"
nipkow@29667
   283
by (simp add: diff_minus add_commute)
haftmann@25077
   284
nipkow@29667
   285
lemma add_diff_eq[algebra_simps]: "a + (b - c) = (a + b) - c"
nipkow@29667
   286
by (simp add: diff_minus add_ac)
haftmann@25077
   287
nipkow@29667
   288
lemma diff_add_eq[algebra_simps]: "(a - b) + c = (a + c) - b"
nipkow@29667
   289
by (simp add: diff_minus add_ac)
haftmann@25077
   290
nipkow@29667
   291
lemma diff_eq_eq[algebra_simps]: "a - b = c \<longleftrightarrow> a = c + b"
nipkow@29667
   292
by (auto simp add: diff_minus add_assoc)
haftmann@25077
   293
nipkow@29667
   294
lemma eq_diff_eq[algebra_simps]: "a = c - b \<longleftrightarrow> a + b = c"
nipkow@29667
   295
by (auto simp add: diff_minus add_assoc)
haftmann@25077
   296
nipkow@29667
   297
lemma diff_diff_eq[algebra_simps]: "(a - b) - c = a - (b + c)"
nipkow@29667
   298
by (simp add: diff_minus add_ac)
haftmann@25077
   299
nipkow@29667
   300
lemma diff_diff_eq2[algebra_simps]: "a - (b - c) = (a + c) - b"
nipkow@29667
   301
by (simp add: diff_minus add_ac)
haftmann@25077
   302
haftmann@25077
   303
lemma eq_iff_diff_eq_0: "a = b \<longleftrightarrow> a - b = 0"
nipkow@29667
   304
by (simp add: algebra_simps)
haftmann@25077
   305
haftmann@25062
   306
end
obua@14738
   307
obua@14738
   308
subsection {* (Partially) Ordered Groups *} 
obua@14738
   309
haftmann@22390
   310
class pordered_ab_semigroup_add = order + ab_semigroup_add +
haftmann@25062
   311
  assumes add_left_mono: "a \<le> b \<Longrightarrow> c + a \<le> c + b"
haftmann@25062
   312
begin
haftmann@24380
   313
haftmann@25062
   314
lemma add_right_mono:
haftmann@25062
   315
  "a \<le> b \<Longrightarrow> a + c \<le> b + c"
nipkow@29667
   316
by (simp add: add_commute [of _ c] add_left_mono)
obua@14738
   317
obua@14738
   318
text {* non-strict, in both arguments *}
obua@14738
   319
lemma add_mono:
haftmann@25062
   320
  "a \<le> b \<Longrightarrow> c \<le> d \<Longrightarrow> a + c \<le> b + d"
obua@14738
   321
  apply (erule add_right_mono [THEN order_trans])
obua@14738
   322
  apply (simp add: add_commute add_left_mono)
obua@14738
   323
  done
obua@14738
   324
haftmann@25062
   325
end
haftmann@25062
   326
haftmann@25062
   327
class pordered_cancel_ab_semigroup_add =
haftmann@25062
   328
  pordered_ab_semigroup_add + cancel_ab_semigroup_add
haftmann@25062
   329
begin
haftmann@25062
   330
obua@14738
   331
lemma add_strict_left_mono:
haftmann@25062
   332
  "a < b \<Longrightarrow> c + a < c + b"
nipkow@29667
   333
by (auto simp add: less_le add_left_mono)
obua@14738
   334
obua@14738
   335
lemma add_strict_right_mono:
haftmann@25062
   336
  "a < b \<Longrightarrow> a + c < b + c"
nipkow@29667
   337
by (simp add: add_commute [of _ c] add_strict_left_mono)
obua@14738
   338
obua@14738
   339
text{*Strict monotonicity in both arguments*}
haftmann@25062
   340
lemma add_strict_mono:
haftmann@25062
   341
  "a < b \<Longrightarrow> c < d \<Longrightarrow> a + c < b + d"
haftmann@25062
   342
apply (erule add_strict_right_mono [THEN less_trans])
obua@14738
   343
apply (erule add_strict_left_mono)
obua@14738
   344
done
obua@14738
   345
obua@14738
   346
lemma add_less_le_mono:
haftmann@25062
   347
  "a < b \<Longrightarrow> c \<le> d \<Longrightarrow> a + c < b + d"
haftmann@25062
   348
apply (erule add_strict_right_mono [THEN less_le_trans])
haftmann@25062
   349
apply (erule add_left_mono)
obua@14738
   350
done
obua@14738
   351
obua@14738
   352
lemma add_le_less_mono:
haftmann@25062
   353
  "a \<le> b \<Longrightarrow> c < d \<Longrightarrow> a + c < b + d"
haftmann@25062
   354
apply (erule add_right_mono [THEN le_less_trans])
obua@14738
   355
apply (erule add_strict_left_mono) 
obua@14738
   356
done
obua@14738
   357
haftmann@25062
   358
end
haftmann@25062
   359
haftmann@25062
   360
class pordered_ab_semigroup_add_imp_le =
haftmann@25062
   361
  pordered_cancel_ab_semigroup_add +
haftmann@25062
   362
  assumes add_le_imp_le_left: "c + a \<le> c + b \<Longrightarrow> a \<le> b"
haftmann@25062
   363
begin
haftmann@25062
   364
obua@14738
   365
lemma add_less_imp_less_left:
nipkow@29667
   366
  assumes less: "c + a < c + b" shows "a < b"
obua@14738
   367
proof -
obua@14738
   368
  from less have le: "c + a <= c + b" by (simp add: order_le_less)
obua@14738
   369
  have "a <= b" 
obua@14738
   370
    apply (insert le)
obua@14738
   371
    apply (drule add_le_imp_le_left)
obua@14738
   372
    by (insert le, drule add_le_imp_le_left, assumption)
obua@14738
   373
  moreover have "a \<noteq> b"
obua@14738
   374
  proof (rule ccontr)
obua@14738
   375
    assume "~(a \<noteq> b)"
obua@14738
   376
    then have "a = b" by simp
obua@14738
   377
    then have "c + a = c + b" by simp
obua@14738
   378
    with less show "False"by simp
obua@14738
   379
  qed
obua@14738
   380
  ultimately show "a < b" by (simp add: order_le_less)
obua@14738
   381
qed
obua@14738
   382
obua@14738
   383
lemma add_less_imp_less_right:
haftmann@25062
   384
  "a + c < b + c \<Longrightarrow> a < b"
obua@14738
   385
apply (rule add_less_imp_less_left [of c])
obua@14738
   386
apply (simp add: add_commute)  
obua@14738
   387
done
obua@14738
   388
obua@14738
   389
lemma add_less_cancel_left [simp]:
haftmann@25062
   390
  "c + a < c + b \<longleftrightarrow> a < b"
nipkow@29667
   391
by (blast intro: add_less_imp_less_left add_strict_left_mono) 
obua@14738
   392
obua@14738
   393
lemma add_less_cancel_right [simp]:
haftmann@25062
   394
  "a + c < b + c \<longleftrightarrow> a < b"
nipkow@29667
   395
by (blast intro: add_less_imp_less_right add_strict_right_mono)
obua@14738
   396
obua@14738
   397
lemma add_le_cancel_left [simp]:
haftmann@25062
   398
  "c + a \<le> c + b \<longleftrightarrow> a \<le> b"
nipkow@29667
   399
by (auto, drule add_le_imp_le_left, simp_all add: add_left_mono) 
obua@14738
   400
obua@14738
   401
lemma add_le_cancel_right [simp]:
haftmann@25062
   402
  "a + c \<le> b + c \<longleftrightarrow> a \<le> b"
nipkow@29667
   403
by (simp add: add_commute [of a c] add_commute [of b c])
obua@14738
   404
obua@14738
   405
lemma add_le_imp_le_right:
haftmann@25062
   406
  "a + c \<le> b + c \<Longrightarrow> a \<le> b"
nipkow@29667
   407
by simp
haftmann@25062
   408
haftmann@25077
   409
lemma max_add_distrib_left:
haftmann@25077
   410
  "max x y + z = max (x + z) (y + z)"
haftmann@25077
   411
  unfolding max_def by auto
haftmann@25077
   412
haftmann@25077
   413
lemma min_add_distrib_left:
haftmann@25077
   414
  "min x y + z = min (x + z) (y + z)"
haftmann@25077
   415
  unfolding min_def by auto
haftmann@25077
   416
haftmann@25062
   417
end
haftmann@25062
   418
haftmann@25303
   419
subsection {* Support for reasoning about signs *}
haftmann@25303
   420
haftmann@25303
   421
class pordered_comm_monoid_add =
haftmann@25303
   422
  pordered_cancel_ab_semigroup_add + comm_monoid_add
haftmann@25303
   423
begin
haftmann@25303
   424
haftmann@25303
   425
lemma add_pos_nonneg:
nipkow@29667
   426
  assumes "0 < a" and "0 \<le> b" shows "0 < a + b"
haftmann@25303
   427
proof -
haftmann@25303
   428
  have "0 + 0 < a + b" 
haftmann@25303
   429
    using assms by (rule add_less_le_mono)
haftmann@25303
   430
  then show ?thesis by simp
haftmann@25303
   431
qed
haftmann@25303
   432
haftmann@25303
   433
lemma add_pos_pos:
nipkow@29667
   434
  assumes "0 < a" and "0 < b" shows "0 < a + b"
nipkow@29667
   435
by (rule add_pos_nonneg) (insert assms, auto)
haftmann@25303
   436
haftmann@25303
   437
lemma add_nonneg_pos:
nipkow@29667
   438
  assumes "0 \<le> a" and "0 < b" shows "0 < a + b"
haftmann@25303
   439
proof -
haftmann@25303
   440
  have "0 + 0 < a + b" 
haftmann@25303
   441
    using assms by (rule add_le_less_mono)
haftmann@25303
   442
  then show ?thesis by simp
haftmann@25303
   443
qed
haftmann@25303
   444
haftmann@25303
   445
lemma add_nonneg_nonneg:
nipkow@29667
   446
  assumes "0 \<le> a" and "0 \<le> b" shows "0 \<le> a + b"
haftmann@25303
   447
proof -
haftmann@25303
   448
  have "0 + 0 \<le> a + b" 
haftmann@25303
   449
    using assms by (rule add_mono)
haftmann@25303
   450
  then show ?thesis by simp
haftmann@25303
   451
qed
haftmann@25303
   452
haftmann@25303
   453
lemma add_neg_nonpos: 
nipkow@29667
   454
  assumes "a < 0" and "b \<le> 0" shows "a + b < 0"
haftmann@25303
   455
proof -
haftmann@25303
   456
  have "a + b < 0 + 0"
haftmann@25303
   457
    using assms by (rule add_less_le_mono)
haftmann@25303
   458
  then show ?thesis by simp
haftmann@25303
   459
qed
haftmann@25303
   460
haftmann@25303
   461
lemma add_neg_neg: 
nipkow@29667
   462
  assumes "a < 0" and "b < 0" shows "a + b < 0"
nipkow@29667
   463
by (rule add_neg_nonpos) (insert assms, auto)
haftmann@25303
   464
haftmann@25303
   465
lemma add_nonpos_neg:
nipkow@29667
   466
  assumes "a \<le> 0" and "b < 0" shows "a + b < 0"
haftmann@25303
   467
proof -
haftmann@25303
   468
  have "a + b < 0 + 0"
haftmann@25303
   469
    using assms by (rule add_le_less_mono)
haftmann@25303
   470
  then show ?thesis by simp
haftmann@25303
   471
qed
haftmann@25303
   472
haftmann@25303
   473
lemma add_nonpos_nonpos:
nipkow@29667
   474
  assumes "a \<le> 0" and "b \<le> 0" shows "a + b \<le> 0"
haftmann@25303
   475
proof -
haftmann@25303
   476
  have "a + b \<le> 0 + 0"
haftmann@25303
   477
    using assms by (rule add_mono)
haftmann@25303
   478
  then show ?thesis by simp
haftmann@25303
   479
qed
haftmann@25303
   480
haftmann@25303
   481
end
haftmann@25303
   482
haftmann@25062
   483
class pordered_ab_group_add =
haftmann@25062
   484
  ab_group_add + pordered_ab_semigroup_add
haftmann@25062
   485
begin
haftmann@25062
   486
huffman@27516
   487
subclass pordered_cancel_ab_semigroup_add ..
haftmann@25062
   488
haftmann@25062
   489
subclass pordered_ab_semigroup_add_imp_le
haftmann@28823
   490
proof
haftmann@25062
   491
  fix a b c :: 'a
haftmann@25062
   492
  assume "c + a \<le> c + b"
haftmann@25062
   493
  hence "(-c) + (c + a) \<le> (-c) + (c + b)" by (rule add_left_mono)
haftmann@25062
   494
  hence "((-c) + c) + a \<le> ((-c) + c) + b" by (simp only: add_assoc)
haftmann@25062
   495
  thus "a \<le> b" by simp
haftmann@25062
   496
qed
haftmann@25062
   497
huffman@27516
   498
subclass pordered_comm_monoid_add ..
haftmann@25303
   499
haftmann@25077
   500
lemma max_diff_distrib_left:
haftmann@25077
   501
  shows "max x y - z = max (x - z) (y - z)"
nipkow@29667
   502
by (simp add: diff_minus, rule max_add_distrib_left) 
haftmann@25077
   503
haftmann@25077
   504
lemma min_diff_distrib_left:
haftmann@25077
   505
  shows "min x y - z = min (x - z) (y - z)"
nipkow@29667
   506
by (simp add: diff_minus, rule min_add_distrib_left) 
haftmann@25077
   507
haftmann@25077
   508
lemma le_imp_neg_le:
nipkow@29667
   509
  assumes "a \<le> b" shows "-b \<le> -a"
haftmann@25077
   510
proof -
nipkow@29667
   511
  have "-a+a \<le> -a+b" using `a \<le> b` by (rule add_left_mono) 
nipkow@29667
   512
  hence "0 \<le> -a+b" by simp
nipkow@29667
   513
  hence "0 + (-b) \<le> (-a + b) + (-b)" by (rule add_right_mono) 
nipkow@29667
   514
  thus ?thesis by (simp add: add_assoc)
haftmann@25077
   515
qed
haftmann@25077
   516
haftmann@25077
   517
lemma neg_le_iff_le [simp]: "- b \<le> - a \<longleftrightarrow> a \<le> b"
haftmann@25077
   518
proof 
haftmann@25077
   519
  assume "- b \<le> - a"
nipkow@29667
   520
  hence "- (- a) \<le> - (- b)" by (rule le_imp_neg_le)
haftmann@25077
   521
  thus "a\<le>b" by simp
haftmann@25077
   522
next
haftmann@25077
   523
  assume "a\<le>b"
haftmann@25077
   524
  thus "-b \<le> -a" by (rule le_imp_neg_le)
haftmann@25077
   525
qed
haftmann@25077
   526
haftmann@25077
   527
lemma neg_le_0_iff_le [simp]: "- a \<le> 0 \<longleftrightarrow> 0 \<le> a"
nipkow@29667
   528
by (subst neg_le_iff_le [symmetric], simp)
haftmann@25077
   529
haftmann@25077
   530
lemma neg_0_le_iff_le [simp]: "0 \<le> - a \<longleftrightarrow> a \<le> 0"
nipkow@29667
   531
by (subst neg_le_iff_le [symmetric], simp)
haftmann@25077
   532
haftmann@25077
   533
lemma neg_less_iff_less [simp]: "- b < - a \<longleftrightarrow> a < b"
nipkow@29667
   534
by (force simp add: less_le) 
haftmann@25077
   535
haftmann@25077
   536
lemma neg_less_0_iff_less [simp]: "- a < 0 \<longleftrightarrow> 0 < a"
nipkow@29667
   537
by (subst neg_less_iff_less [symmetric], simp)
haftmann@25077
   538
haftmann@25077
   539
lemma neg_0_less_iff_less [simp]: "0 < - a \<longleftrightarrow> a < 0"
nipkow@29667
   540
by (subst neg_less_iff_less [symmetric], simp)
haftmann@25077
   541
haftmann@25077
   542
text{*The next several equations can make the simplifier loop!*}
haftmann@25077
   543
haftmann@25077
   544
lemma less_minus_iff: "a < - b \<longleftrightarrow> b < - a"
haftmann@25077
   545
proof -
haftmann@25077
   546
  have "(- (-a) < - b) = (b < - a)" by (rule neg_less_iff_less)
haftmann@25077
   547
  thus ?thesis by simp
haftmann@25077
   548
qed
haftmann@25077
   549
haftmann@25077
   550
lemma minus_less_iff: "- a < b \<longleftrightarrow> - b < a"
haftmann@25077
   551
proof -
haftmann@25077
   552
  have "(- a < - (-b)) = (- b < a)" by (rule neg_less_iff_less)
haftmann@25077
   553
  thus ?thesis by simp
haftmann@25077
   554
qed
haftmann@25077
   555
haftmann@25077
   556
lemma le_minus_iff: "a \<le> - b \<longleftrightarrow> b \<le> - a"
haftmann@25077
   557
proof -
haftmann@25077
   558
  have mm: "!! a (b::'a). (-(-a)) < -b \<Longrightarrow> -(-b) < -a" by (simp only: minus_less_iff)
haftmann@25077
   559
  have "(- (- a) <= -b) = (b <= - a)" 
haftmann@25077
   560
    apply (auto simp only: le_less)
haftmann@25077
   561
    apply (drule mm)
haftmann@25077
   562
    apply (simp_all)
haftmann@25077
   563
    apply (drule mm[simplified], assumption)
haftmann@25077
   564
    done
haftmann@25077
   565
  then show ?thesis by simp
haftmann@25077
   566
qed
haftmann@25077
   567
haftmann@25077
   568
lemma minus_le_iff: "- a \<le> b \<longleftrightarrow> - b \<le> a"
nipkow@29667
   569
by (auto simp add: le_less minus_less_iff)
haftmann@25077
   570
haftmann@25077
   571
lemma less_iff_diff_less_0: "a < b \<longleftrightarrow> a - b < 0"
haftmann@25077
   572
proof -
haftmann@25077
   573
  have  "(a < b) = (a + (- b) < b + (-b))"  
haftmann@25077
   574
    by (simp only: add_less_cancel_right)
haftmann@25077
   575
  also have "... =  (a - b < 0)" by (simp add: diff_minus)
haftmann@25077
   576
  finally show ?thesis .
haftmann@25077
   577
qed
haftmann@25077
   578
nipkow@29667
   579
lemma diff_less_eq[algebra_simps]: "a - b < c \<longleftrightarrow> a < c + b"
haftmann@25077
   580
apply (subst less_iff_diff_less_0 [of a])
haftmann@25077
   581
apply (rule less_iff_diff_less_0 [of _ c, THEN ssubst])
haftmann@25077
   582
apply (simp add: diff_minus add_ac)
haftmann@25077
   583
done
haftmann@25077
   584
nipkow@29667
   585
lemma less_diff_eq[algebra_simps]: "a < c - b \<longleftrightarrow> a + b < c"
haftmann@25077
   586
apply (subst less_iff_diff_less_0 [of "plus a b"])
haftmann@25077
   587
apply (subst less_iff_diff_less_0 [of a])
haftmann@25077
   588
apply (simp add: diff_minus add_ac)
haftmann@25077
   589
done
haftmann@25077
   590
nipkow@29667
   591
lemma diff_le_eq[algebra_simps]: "a - b \<le> c \<longleftrightarrow> a \<le> c + b"
nipkow@29667
   592
by (auto simp add: le_less diff_less_eq diff_add_cancel add_diff_cancel)
haftmann@25077
   593
nipkow@29667
   594
lemma le_diff_eq[algebra_simps]: "a \<le> c - b \<longleftrightarrow> a + b \<le> c"
nipkow@29667
   595
by (auto simp add: le_less less_diff_eq diff_add_cancel add_diff_cancel)
haftmann@25077
   596
haftmann@25077
   597
lemma le_iff_diff_le_0: "a \<le> b \<longleftrightarrow> a - b \<le> 0"
nipkow@29667
   598
by (simp add: algebra_simps)
haftmann@25077
   599
nipkow@29667
   600
text{*Legacy - use @{text algebra_simps} *}
nipkow@29667
   601
lemmas group_simps = algebra_simps
haftmann@25230
   602
haftmann@25077
   603
end
haftmann@25077
   604
nipkow@29667
   605
text{*Legacy - use @{text algebra_simps} *}
nipkow@29667
   606
lemmas group_simps = algebra_simps
haftmann@25230
   607
haftmann@25062
   608
class ordered_ab_semigroup_add =
haftmann@25062
   609
  linorder + pordered_ab_semigroup_add
haftmann@25062
   610
haftmann@25062
   611
class ordered_cancel_ab_semigroup_add =
haftmann@25062
   612
  linorder + pordered_cancel_ab_semigroup_add
haftmann@25267
   613
begin
haftmann@25062
   614
huffman@27516
   615
subclass ordered_ab_semigroup_add ..
haftmann@25062
   616
haftmann@25267
   617
subclass pordered_ab_semigroup_add_imp_le
haftmann@28823
   618
proof
haftmann@25062
   619
  fix a b c :: 'a
haftmann@25062
   620
  assume le: "c + a <= c + b"  
haftmann@25062
   621
  show "a <= b"
haftmann@25062
   622
  proof (rule ccontr)
haftmann@25062
   623
    assume w: "~ a \<le> b"
haftmann@25062
   624
    hence "b <= a" by (simp add: linorder_not_le)
haftmann@25062
   625
    hence le2: "c + b <= c + a" by (rule add_left_mono)
haftmann@25062
   626
    have "a = b" 
haftmann@25062
   627
      apply (insert le)
haftmann@25062
   628
      apply (insert le2)
haftmann@25062
   629
      apply (drule antisym, simp_all)
haftmann@25062
   630
      done
haftmann@25062
   631
    with w show False 
haftmann@25062
   632
      by (simp add: linorder_not_le [symmetric])
haftmann@25062
   633
  qed
haftmann@25062
   634
qed
haftmann@25062
   635
haftmann@25267
   636
end
haftmann@25267
   637
haftmann@25230
   638
class ordered_ab_group_add =
haftmann@25230
   639
  linorder + pordered_ab_group_add
haftmann@25267
   640
begin
haftmann@25230
   641
huffman@27516
   642
subclass ordered_cancel_ab_semigroup_add ..
haftmann@25230
   643
haftmann@25303
   644
lemma neg_less_eq_nonneg:
haftmann@25303
   645
  "- a \<le> a \<longleftrightarrow> 0 \<le> a"
haftmann@25303
   646
proof
haftmann@25303
   647
  assume A: "- a \<le> a" show "0 \<le> a"
haftmann@25303
   648
  proof (rule classical)
haftmann@25303
   649
    assume "\<not> 0 \<le> a"
haftmann@25303
   650
    then have "a < 0" by auto
haftmann@25303
   651
    with A have "- a < 0" by (rule le_less_trans)
haftmann@25303
   652
    then show ?thesis by auto
haftmann@25303
   653
  qed
haftmann@25303
   654
next
haftmann@25303
   655
  assume A: "0 \<le> a" show "- a \<le> a"
haftmann@25303
   656
  proof (rule order_trans)
haftmann@25303
   657
    show "- a \<le> 0" using A by (simp add: minus_le_iff)
haftmann@25303
   658
  next
haftmann@25303
   659
    show "0 \<le> a" using A .
haftmann@25303
   660
  qed
haftmann@25303
   661
qed
haftmann@25303
   662
  
haftmann@25303
   663
lemma less_eq_neg_nonpos:
haftmann@25303
   664
  "a \<le> - a \<longleftrightarrow> a \<le> 0"
haftmann@25303
   665
proof
haftmann@25303
   666
  assume A: "a \<le> - a" show "a \<le> 0"
haftmann@25303
   667
  proof (rule classical)
haftmann@25303
   668
    assume "\<not> a \<le> 0"
haftmann@25303
   669
    then have "0 < a" by auto
haftmann@25303
   670
    then have "0 < - a" using A by (rule less_le_trans)
haftmann@25303
   671
    then show ?thesis by auto
haftmann@25303
   672
  qed
haftmann@25303
   673
next
haftmann@25303
   674
  assume A: "a \<le> 0" show "a \<le> - a"
haftmann@25303
   675
  proof (rule order_trans)
haftmann@25303
   676
    show "0 \<le> - a" using A by (simp add: minus_le_iff)
haftmann@25303
   677
  next
haftmann@25303
   678
    show "a \<le> 0" using A .
haftmann@25303
   679
  qed
haftmann@25303
   680
qed
haftmann@25303
   681
haftmann@25303
   682
lemma equal_neg_zero:
haftmann@25303
   683
  "a = - a \<longleftrightarrow> a = 0"
haftmann@25303
   684
proof
haftmann@25303
   685
  assume "a = 0" then show "a = - a" by simp
haftmann@25303
   686
next
haftmann@25303
   687
  assume A: "a = - a" show "a = 0"
haftmann@25303
   688
  proof (cases "0 \<le> a")
haftmann@25303
   689
    case True with A have "0 \<le> - a" by auto
haftmann@25303
   690
    with le_minus_iff have "a \<le> 0" by simp
haftmann@25303
   691
    with True show ?thesis by (auto intro: order_trans)
haftmann@25303
   692
  next
haftmann@25303
   693
    case False then have B: "a \<le> 0" by auto
haftmann@25303
   694
    with A have "- a \<le> 0" by auto
haftmann@25303
   695
    with B show ?thesis by (auto intro: order_trans)
haftmann@25303
   696
  qed
haftmann@25303
   697
qed
haftmann@25303
   698
haftmann@25303
   699
lemma neg_equal_zero:
haftmann@25303
   700
  "- a = a \<longleftrightarrow> a = 0"
haftmann@25303
   701
  unfolding equal_neg_zero [symmetric] by auto
haftmann@25303
   702
haftmann@25267
   703
end
haftmann@25267
   704
haftmann@25077
   705
-- {* FIXME localize the following *}
obua@14738
   706
paulson@15234
   707
lemma add_increasing:
paulson@15234
   708
  fixes c :: "'a::{pordered_ab_semigroup_add_imp_le, comm_monoid_add}"
paulson@15234
   709
  shows  "[|0\<le>a; b\<le>c|] ==> b \<le> a + c"
obua@14738
   710
by (insert add_mono [of 0 a b c], simp)
obua@14738
   711
nipkow@15539
   712
lemma add_increasing2:
nipkow@15539
   713
  fixes c :: "'a::{pordered_ab_semigroup_add_imp_le, comm_monoid_add}"
nipkow@15539
   714
  shows  "[|0\<le>c; b\<le>a|] ==> b \<le> a + c"
nipkow@15539
   715
by (simp add:add_increasing add_commute[of a])
nipkow@15539
   716
paulson@15234
   717
lemma add_strict_increasing:
paulson@15234
   718
  fixes c :: "'a::{pordered_ab_semigroup_add_imp_le, comm_monoid_add}"
paulson@15234
   719
  shows "[|0<a; b\<le>c|] ==> b < a + c"
paulson@15234
   720
by (insert add_less_le_mono [of 0 a b c], simp)
paulson@15234
   721
paulson@15234
   722
lemma add_strict_increasing2:
paulson@15234
   723
  fixes c :: "'a::{pordered_ab_semigroup_add_imp_le, comm_monoid_add}"
paulson@15234
   724
  shows "[|0\<le>a; b<c|] ==> b < a + c"
paulson@15234
   725
by (insert add_le_less_mono [of 0 a b c], simp)
paulson@15234
   726
obua@14738
   727
haftmann@25303
   728
class pordered_ab_group_add_abs = pordered_ab_group_add + abs +
haftmann@25303
   729
  assumes abs_ge_zero [simp]: "\<bar>a\<bar> \<ge> 0"
haftmann@25303
   730
    and abs_ge_self: "a \<le> \<bar>a\<bar>"
haftmann@25303
   731
    and abs_leI: "a \<le> b \<Longrightarrow> - a \<le> b \<Longrightarrow> \<bar>a\<bar> \<le> b"
haftmann@25303
   732
    and abs_minus_cancel [simp]: "\<bar>-a\<bar> = \<bar>a\<bar>"
haftmann@25303
   733
    and abs_triangle_ineq: "\<bar>a + b\<bar> \<le> \<bar>a\<bar> + \<bar>b\<bar>"
haftmann@25303
   734
begin
haftmann@25303
   735
haftmann@25307
   736
lemma abs_minus_le_zero: "- \<bar>a\<bar> \<le> 0"
haftmann@25307
   737
  unfolding neg_le_0_iff_le by simp
haftmann@25307
   738
haftmann@25307
   739
lemma abs_of_nonneg [simp]:
nipkow@29667
   740
  assumes nonneg: "0 \<le> a" shows "\<bar>a\<bar> = a"
haftmann@25307
   741
proof (rule antisym)
haftmann@25307
   742
  from nonneg le_imp_neg_le have "- a \<le> 0" by simp
haftmann@25307
   743
  from this nonneg have "- a \<le> a" by (rule order_trans)
haftmann@25307
   744
  then show "\<bar>a\<bar> \<le> a" by (auto intro: abs_leI)
haftmann@25307
   745
qed (rule abs_ge_self)
haftmann@25307
   746
haftmann@25307
   747
lemma abs_idempotent [simp]: "\<bar>\<bar>a\<bar>\<bar> = \<bar>a\<bar>"
nipkow@29667
   748
by (rule antisym)
nipkow@29667
   749
   (auto intro!: abs_ge_self abs_leI order_trans [of "uminus (abs a)" zero "abs a"])
haftmann@25307
   750
haftmann@25307
   751
lemma abs_eq_0 [simp]: "\<bar>a\<bar> = 0 \<longleftrightarrow> a = 0"
haftmann@25307
   752
proof -
haftmann@25307
   753
  have "\<bar>a\<bar> = 0 \<Longrightarrow> a = 0"
haftmann@25307
   754
  proof (rule antisym)
haftmann@25307
   755
    assume zero: "\<bar>a\<bar> = 0"
haftmann@25307
   756
    with abs_ge_self show "a \<le> 0" by auto
haftmann@25307
   757
    from zero have "\<bar>-a\<bar> = 0" by simp
haftmann@25307
   758
    with abs_ge_self [of "uminus a"] have "- a \<le> 0" by auto
haftmann@25307
   759
    with neg_le_0_iff_le show "0 \<le> a" by auto
haftmann@25307
   760
  qed
haftmann@25307
   761
  then show ?thesis by auto
haftmann@25307
   762
qed
haftmann@25307
   763
haftmann@25303
   764
lemma abs_zero [simp]: "\<bar>0\<bar> = 0"
nipkow@29667
   765
by simp
avigad@16775
   766
haftmann@25303
   767
lemma abs_0_eq [simp, noatp]: "0 = \<bar>a\<bar> \<longleftrightarrow> a = 0"
haftmann@25303
   768
proof -
haftmann@25303
   769
  have "0 = \<bar>a\<bar> \<longleftrightarrow> \<bar>a\<bar> = 0" by (simp only: eq_ac)
haftmann@25303
   770
  thus ?thesis by simp
haftmann@25303
   771
qed
haftmann@25303
   772
haftmann@25303
   773
lemma abs_le_zero_iff [simp]: "\<bar>a\<bar> \<le> 0 \<longleftrightarrow> a = 0" 
haftmann@25303
   774
proof
haftmann@25303
   775
  assume "\<bar>a\<bar> \<le> 0"
haftmann@25303
   776
  then have "\<bar>a\<bar> = 0" by (rule antisym) simp
haftmann@25303
   777
  thus "a = 0" by simp
haftmann@25303
   778
next
haftmann@25303
   779
  assume "a = 0"
haftmann@25303
   780
  thus "\<bar>a\<bar> \<le> 0" by simp
haftmann@25303
   781
qed
haftmann@25303
   782
haftmann@25303
   783
lemma zero_less_abs_iff [simp]: "0 < \<bar>a\<bar> \<longleftrightarrow> a \<noteq> 0"
nipkow@29667
   784
by (simp add: less_le)
haftmann@25303
   785
haftmann@25303
   786
lemma abs_not_less_zero [simp]: "\<not> \<bar>a\<bar> < 0"
haftmann@25303
   787
proof -
haftmann@25303
   788
  have a: "\<And>x y. x \<le> y \<Longrightarrow> \<not> y < x" by auto
haftmann@25303
   789
  show ?thesis by (simp add: a)
haftmann@25303
   790
qed
avigad@16775
   791
haftmann@25303
   792
lemma abs_ge_minus_self: "- a \<le> \<bar>a\<bar>"
haftmann@25303
   793
proof -
haftmann@25303
   794
  have "- a \<le> \<bar>-a\<bar>" by (rule abs_ge_self)
haftmann@25303
   795
  then show ?thesis by simp
haftmann@25303
   796
qed
haftmann@25303
   797
haftmann@25303
   798
lemma abs_minus_commute: 
haftmann@25303
   799
  "\<bar>a - b\<bar> = \<bar>b - a\<bar>"
haftmann@25303
   800
proof -
haftmann@25303
   801
  have "\<bar>a - b\<bar> = \<bar>- (a - b)\<bar>" by (simp only: abs_minus_cancel)
haftmann@25303
   802
  also have "... = \<bar>b - a\<bar>" by simp
haftmann@25303
   803
  finally show ?thesis .
haftmann@25303
   804
qed
haftmann@25303
   805
haftmann@25303
   806
lemma abs_of_pos: "0 < a \<Longrightarrow> \<bar>a\<bar> = a"
nipkow@29667
   807
by (rule abs_of_nonneg, rule less_imp_le)
avigad@16775
   808
haftmann@25303
   809
lemma abs_of_nonpos [simp]:
nipkow@29667
   810
  assumes "a \<le> 0" shows "\<bar>a\<bar> = - a"
haftmann@25303
   811
proof -
haftmann@25303
   812
  let ?b = "- a"
haftmann@25303
   813
  have "- ?b \<le> 0 \<Longrightarrow> \<bar>- ?b\<bar> = - (- ?b)"
haftmann@25303
   814
  unfolding abs_minus_cancel [of "?b"]
haftmann@25303
   815
  unfolding neg_le_0_iff_le [of "?b"]
haftmann@25303
   816
  unfolding minus_minus by (erule abs_of_nonneg)
haftmann@25303
   817
  then show ?thesis using assms by auto
haftmann@25303
   818
qed
haftmann@25303
   819
  
haftmann@25303
   820
lemma abs_of_neg: "a < 0 \<Longrightarrow> \<bar>a\<bar> = - a"
nipkow@29667
   821
by (rule abs_of_nonpos, rule less_imp_le)
haftmann@25303
   822
haftmann@25303
   823
lemma abs_le_D1: "\<bar>a\<bar> \<le> b \<Longrightarrow> a \<le> b"
nipkow@29667
   824
by (insert abs_ge_self, blast intro: order_trans)
haftmann@25303
   825
haftmann@25303
   826
lemma abs_le_D2: "\<bar>a\<bar> \<le> b \<Longrightarrow> - a \<le> b"
nipkow@29667
   827
by (insert abs_le_D1 [of "uminus a"], simp)
haftmann@25303
   828
haftmann@25303
   829
lemma abs_le_iff: "\<bar>a\<bar> \<le> b \<longleftrightarrow> a \<le> b \<and> - a \<le> b"
nipkow@29667
   830
by (blast intro: abs_leI dest: abs_le_D1 abs_le_D2)
haftmann@25303
   831
haftmann@25303
   832
lemma abs_triangle_ineq2: "\<bar>a\<bar> - \<bar>b\<bar> \<le> \<bar>a - b\<bar>"
nipkow@29667
   833
  apply (simp add: algebra_simps)
nipkow@29667
   834
  apply (subgoal_tac "abs a = abs (plus b (minus a b))")
haftmann@25303
   835
  apply (erule ssubst)
haftmann@25303
   836
  apply (rule abs_triangle_ineq)
nipkow@29667
   837
  apply (rule arg_cong[of _ _ abs])
nipkow@29667
   838
  apply (simp add: algebra_simps)
avigad@16775
   839
done
avigad@16775
   840
haftmann@25303
   841
lemma abs_triangle_ineq3: "\<bar>\<bar>a\<bar> - \<bar>b\<bar>\<bar> \<le> \<bar>a - b\<bar>"
haftmann@25303
   842
  apply (subst abs_le_iff)
haftmann@25303
   843
  apply auto
haftmann@25303
   844
  apply (rule abs_triangle_ineq2)
haftmann@25303
   845
  apply (subst abs_minus_commute)
haftmann@25303
   846
  apply (rule abs_triangle_ineq2)
avigad@16775
   847
done
avigad@16775
   848
haftmann@25303
   849
lemma abs_triangle_ineq4: "\<bar>a - b\<bar> \<le> \<bar>a\<bar> + \<bar>b\<bar>"
haftmann@25303
   850
proof -
nipkow@29667
   851
  have "abs(a - b) = abs(a + - b)" by (subst diff_minus, rule refl)
nipkow@29667
   852
  also have "... <= abs a + abs (- b)" by (rule abs_triangle_ineq)
nipkow@29667
   853
  finally show ?thesis by simp
haftmann@25303
   854
qed
avigad@16775
   855
haftmann@25303
   856
lemma abs_diff_triangle_ineq: "\<bar>a + b - (c + d)\<bar> \<le> \<bar>a - c\<bar> + \<bar>b - d\<bar>"
haftmann@25303
   857
proof -
haftmann@25303
   858
  have "\<bar>a + b - (c+d)\<bar> = \<bar>(a-c) + (b-d)\<bar>" by (simp add: diff_minus add_ac)
haftmann@25303
   859
  also have "... \<le> \<bar>a-c\<bar> + \<bar>b-d\<bar>" by (rule abs_triangle_ineq)
haftmann@25303
   860
  finally show ?thesis .
haftmann@25303
   861
qed
avigad@16775
   862
haftmann@25303
   863
lemma abs_add_abs [simp]:
haftmann@25303
   864
  "\<bar>\<bar>a\<bar> + \<bar>b\<bar>\<bar> = \<bar>a\<bar> + \<bar>b\<bar>" (is "?L = ?R")
haftmann@25303
   865
proof (rule antisym)
haftmann@25303
   866
  show "?L \<ge> ?R" by(rule abs_ge_self)
haftmann@25303
   867
next
haftmann@25303
   868
  have "?L \<le> \<bar>\<bar>a\<bar>\<bar> + \<bar>\<bar>b\<bar>\<bar>" by(rule abs_triangle_ineq)
haftmann@25303
   869
  also have "\<dots> = ?R" by simp
haftmann@25303
   870
  finally show "?L \<le> ?R" .
haftmann@25303
   871
qed
haftmann@25303
   872
haftmann@25303
   873
end
obua@14738
   874
haftmann@22452
   875
obua@14738
   876
subsection {* Lattice Ordered (Abelian) Groups *}
obua@14738
   877
haftmann@25303
   878
class lordered_ab_group_add_meet = pordered_ab_group_add + lower_semilattice
haftmann@25090
   879
begin
obua@14738
   880
haftmann@25090
   881
lemma add_inf_distrib_left:
haftmann@25090
   882
  "a + inf b c = inf (a + b) (a + c)"
haftmann@25090
   883
apply (rule antisym)
haftmann@22422
   884
apply (simp_all add: le_infI)
haftmann@25090
   885
apply (rule add_le_imp_le_left [of "uminus a"])
haftmann@25090
   886
apply (simp only: add_assoc [symmetric], simp)
nipkow@21312
   887
apply rule
nipkow@21312
   888
apply (rule add_le_imp_le_left[of "a"], simp only: add_assoc[symmetric], simp)+
obua@14738
   889
done
obua@14738
   890
haftmann@25090
   891
lemma add_inf_distrib_right:
haftmann@25090
   892
  "inf a b + c = inf (a + c) (b + c)"
haftmann@25090
   893
proof -
haftmann@25090
   894
  have "c + inf a b = inf (c+a) (c+b)" by (simp add: add_inf_distrib_left)
haftmann@25090
   895
  thus ?thesis by (simp add: add_commute)
haftmann@25090
   896
qed
haftmann@25090
   897
haftmann@25090
   898
end
haftmann@25090
   899
haftmann@25303
   900
class lordered_ab_group_add_join = pordered_ab_group_add + upper_semilattice
haftmann@25090
   901
begin
haftmann@25090
   902
haftmann@25090
   903
lemma add_sup_distrib_left:
haftmann@25090
   904
  "a + sup b c = sup (a + b) (a + c)" 
haftmann@25090
   905
apply (rule antisym)
haftmann@25090
   906
apply (rule add_le_imp_le_left [of "uminus a"])
obua@14738
   907
apply (simp only: add_assoc[symmetric], simp)
nipkow@21312
   908
apply rule
nipkow@21312
   909
apply (rule add_le_imp_le_left [of "a"], simp only: add_assoc[symmetric], simp)+
haftmann@22422
   910
apply (rule le_supI)
nipkow@21312
   911
apply (simp_all)
obua@14738
   912
done
obua@14738
   913
haftmann@25090
   914
lemma add_sup_distrib_right:
haftmann@25090
   915
  "sup a b + c = sup (a+c) (b+c)"
obua@14738
   916
proof -
haftmann@22452
   917
  have "c + sup a b = sup (c+a) (c+b)" by (simp add: add_sup_distrib_left)
obua@14738
   918
  thus ?thesis by (simp add: add_commute)
obua@14738
   919
qed
obua@14738
   920
haftmann@25090
   921
end
haftmann@25090
   922
haftmann@25303
   923
class lordered_ab_group_add = pordered_ab_group_add + lattice
haftmann@25090
   924
begin
haftmann@25090
   925
huffman@27516
   926
subclass lordered_ab_group_add_meet ..
huffman@27516
   927
subclass lordered_ab_group_add_join ..
haftmann@25090
   928
haftmann@22422
   929
lemmas add_sup_inf_distribs = add_inf_distrib_right add_inf_distrib_left add_sup_distrib_right add_sup_distrib_left
obua@14738
   930
haftmann@25090
   931
lemma inf_eq_neg_sup: "inf a b = - sup (-a) (-b)"
haftmann@22452
   932
proof (rule inf_unique)
haftmann@22452
   933
  fix a b :: 'a
haftmann@25090
   934
  show "- sup (-a) (-b) \<le> a"
haftmann@25090
   935
    by (rule add_le_imp_le_right [of _ "sup (uminus a) (uminus b)"])
haftmann@25090
   936
      (simp, simp add: add_sup_distrib_left)
haftmann@22452
   937
next
haftmann@22452
   938
  fix a b :: 'a
haftmann@25090
   939
  show "- sup (-a) (-b) \<le> b"
haftmann@25090
   940
    by (rule add_le_imp_le_right [of _ "sup (uminus a) (uminus b)"])
haftmann@25090
   941
      (simp, simp add: add_sup_distrib_left)
haftmann@22452
   942
next
haftmann@22452
   943
  fix a b c :: 'a
haftmann@22452
   944
  assume "a \<le> b" "a \<le> c"
haftmann@22452
   945
  then show "a \<le> - sup (-b) (-c)" by (subst neg_le_iff_le [symmetric])
haftmann@22452
   946
    (simp add: le_supI)
haftmann@22452
   947
qed
haftmann@22452
   948
  
haftmann@25090
   949
lemma sup_eq_neg_inf: "sup a b = - inf (-a) (-b)"
haftmann@22452
   950
proof (rule sup_unique)
haftmann@22452
   951
  fix a b :: 'a
haftmann@25090
   952
  show "a \<le> - inf (-a) (-b)"
haftmann@25090
   953
    by (rule add_le_imp_le_right [of _ "inf (uminus a) (uminus b)"])
haftmann@25090
   954
      (simp, simp add: add_inf_distrib_left)
haftmann@22452
   955
next
haftmann@22452
   956
  fix a b :: 'a
haftmann@25090
   957
  show "b \<le> - inf (-a) (-b)"
haftmann@25090
   958
    by (rule add_le_imp_le_right [of _ "inf (uminus a) (uminus b)"])
haftmann@25090
   959
      (simp, simp add: add_inf_distrib_left)
haftmann@22452
   960
next
haftmann@22452
   961
  fix a b c :: 'a
haftmann@22452
   962
  assume "a \<le> c" "b \<le> c"
haftmann@22452
   963
  then show "- inf (-a) (-b) \<le> c" by (subst neg_le_iff_le [symmetric])
haftmann@22452
   964
    (simp add: le_infI)
haftmann@22452
   965
qed
obua@14738
   966
haftmann@25230
   967
lemma neg_inf_eq_sup: "- inf a b = sup (-a) (-b)"
nipkow@29667
   968
by (simp add: inf_eq_neg_sup)
haftmann@25230
   969
haftmann@25230
   970
lemma neg_sup_eq_inf: "- sup a b = inf (-a) (-b)"
nipkow@29667
   971
by (simp add: sup_eq_neg_inf)
haftmann@25230
   972
haftmann@25090
   973
lemma add_eq_inf_sup: "a + b = sup a b + inf a b"
obua@14738
   974
proof -
haftmann@22422
   975
  have "0 = - inf 0 (a-b) + inf (a-b) 0" by (simp add: inf_commute)
haftmann@22422
   976
  hence "0 = sup 0 (b-a) + inf (a-b) 0" by (simp add: inf_eq_neg_sup)
haftmann@22422
   977
  hence "0 = (-a + sup a b) + (inf a b + (-b))"
nipkow@29667
   978
    by (simp add: add_sup_distrib_left add_inf_distrib_right)
nipkow@29667
   979
       (simp add: algebra_simps)
nipkow@29667
   980
  thus ?thesis by (simp add: algebra_simps)
obua@14738
   981
qed
obua@14738
   982
obua@14738
   983
subsection {* Positive Part, Negative Part, Absolute Value *}
obua@14738
   984
haftmann@22422
   985
definition
haftmann@25090
   986
  nprt :: "'a \<Rightarrow> 'a" where
haftmann@22422
   987
  "nprt x = inf x 0"
haftmann@22422
   988
haftmann@22422
   989
definition
haftmann@25090
   990
  pprt :: "'a \<Rightarrow> 'a" where
haftmann@22422
   991
  "pprt x = sup x 0"
obua@14738
   992
haftmann@25230
   993
lemma pprt_neg: "pprt (- x) = - nprt x"
haftmann@25230
   994
proof -
haftmann@25230
   995
  have "sup (- x) 0 = sup (- x) (- 0)" unfolding minus_zero ..
haftmann@25230
   996
  also have "\<dots> = - inf x 0" unfolding neg_inf_eq_sup ..
haftmann@25230
   997
  finally have "sup (- x) 0 = - inf x 0" .
haftmann@25230
   998
  then show ?thesis unfolding pprt_def nprt_def .
haftmann@25230
   999
qed
haftmann@25230
  1000
haftmann@25230
  1001
lemma nprt_neg: "nprt (- x) = - pprt x"
haftmann@25230
  1002
proof -
haftmann@25230
  1003
  from pprt_neg have "pprt (- (- x)) = - nprt (- x)" .
haftmann@25230
  1004
  then have "pprt x = - nprt (- x)" by simp
haftmann@25230
  1005
  then show ?thesis by simp
haftmann@25230
  1006
qed
haftmann@25230
  1007
obua@14738
  1008
lemma prts: "a = pprt a + nprt a"
nipkow@29667
  1009
by (simp add: pprt_def nprt_def add_eq_inf_sup[symmetric])
obua@14738
  1010
obua@14738
  1011
lemma zero_le_pprt[simp]: "0 \<le> pprt a"
nipkow@29667
  1012
by (simp add: pprt_def)
obua@14738
  1013
obua@14738
  1014
lemma nprt_le_zero[simp]: "nprt a \<le> 0"
nipkow@29667
  1015
by (simp add: nprt_def)
obua@14738
  1016
haftmann@25090
  1017
lemma le_eq_neg: "a \<le> - b \<longleftrightarrow> a + b \<le> 0" (is "?l = ?r")
obua@14738
  1018
proof -
obua@14738
  1019
  have a: "?l \<longrightarrow> ?r"
obua@14738
  1020
    apply (auto)
haftmann@25090
  1021
    apply (rule add_le_imp_le_right[of _ "uminus b" _])
obua@14738
  1022
    apply (simp add: add_assoc)
obua@14738
  1023
    done
obua@14738
  1024
  have b: "?r \<longrightarrow> ?l"
obua@14738
  1025
    apply (auto)
obua@14738
  1026
    apply (rule add_le_imp_le_right[of _ "b" _])
obua@14738
  1027
    apply (simp)
obua@14738
  1028
    done
obua@14738
  1029
  from a b show ?thesis by blast
obua@14738
  1030
qed
obua@14738
  1031
obua@15580
  1032
lemma pprt_0[simp]: "pprt 0 = 0" by (simp add: pprt_def)
obua@15580
  1033
lemma nprt_0[simp]: "nprt 0 = 0" by (simp add: nprt_def)
obua@15580
  1034
haftmann@25090
  1035
lemma pprt_eq_id [simp, noatp]: "0 \<le> x \<Longrightarrow> pprt x = x"
nipkow@29667
  1036
by (simp add: pprt_def le_iff_sup sup_ACI)
obua@15580
  1037
haftmann@25090
  1038
lemma nprt_eq_id [simp, noatp]: "x \<le> 0 \<Longrightarrow> nprt x = x"
nipkow@29667
  1039
by (simp add: nprt_def le_iff_inf inf_ACI)
obua@15580
  1040
haftmann@25090
  1041
lemma pprt_eq_0 [simp, noatp]: "x \<le> 0 \<Longrightarrow> pprt x = 0"
nipkow@29667
  1042
by (simp add: pprt_def le_iff_sup sup_ACI)
obua@15580
  1043
haftmann@25090
  1044
lemma nprt_eq_0 [simp, noatp]: "0 \<le> x \<Longrightarrow> nprt x = 0"
nipkow@29667
  1045
by (simp add: nprt_def le_iff_inf inf_ACI)
obua@15580
  1046
haftmann@25090
  1047
lemma sup_0_imp_0: "sup a (- a) = 0 \<Longrightarrow> a = 0"
obua@14738
  1048
proof -
obua@14738
  1049
  {
obua@14738
  1050
    fix a::'a
haftmann@22422
  1051
    assume hyp: "sup a (-a) = 0"
haftmann@22422
  1052
    hence "sup a (-a) + a = a" by (simp)
haftmann@22422
  1053
    hence "sup (a+a) 0 = a" by (simp add: add_sup_distrib_right) 
haftmann@22422
  1054
    hence "sup (a+a) 0 <= a" by (simp)
haftmann@22422
  1055
    hence "0 <= a" by (blast intro: order_trans inf_sup_ord)
obua@14738
  1056
  }
obua@14738
  1057
  note p = this
haftmann@22422
  1058
  assume hyp:"sup a (-a) = 0"
haftmann@22422
  1059
  hence hyp2:"sup (-a) (-(-a)) = 0" by (simp add: sup_commute)
obua@14738
  1060
  from p[OF hyp] p[OF hyp2] show "a = 0" by simp
obua@14738
  1061
qed
obua@14738
  1062
haftmann@25090
  1063
lemma inf_0_imp_0: "inf a (-a) = 0 \<Longrightarrow> a = 0"
haftmann@22422
  1064
apply (simp add: inf_eq_neg_sup)
haftmann@22422
  1065
apply (simp add: sup_commute)
haftmann@22422
  1066
apply (erule sup_0_imp_0)
paulson@15481
  1067
done
obua@14738
  1068
haftmann@25090
  1069
lemma inf_0_eq_0 [simp, noatp]: "inf a (- a) = 0 \<longleftrightarrow> a = 0"
nipkow@29667
  1070
by (rule, erule inf_0_imp_0) simp
obua@14738
  1071
haftmann@25090
  1072
lemma sup_0_eq_0 [simp, noatp]: "sup a (- a) = 0 \<longleftrightarrow> a = 0"
nipkow@29667
  1073
by (rule, erule sup_0_imp_0) simp
obua@14738
  1074
haftmann@25090
  1075
lemma zero_le_double_add_iff_zero_le_single_add [simp]:
haftmann@25090
  1076
  "0 \<le> a + a \<longleftrightarrow> 0 \<le> a"
obua@14738
  1077
proof
obua@14738
  1078
  assume "0 <= a + a"
haftmann@22422
  1079
  hence a:"inf (a+a) 0 = 0" by (simp add: le_iff_inf inf_commute)
haftmann@25090
  1080
  have "(inf a 0)+(inf a 0) = inf (inf (a+a) 0) a" (is "?l=_")
haftmann@25090
  1081
    by (simp add: add_sup_inf_distribs inf_ACI)
haftmann@22422
  1082
  hence "?l = 0 + inf a 0" by (simp add: a, simp add: inf_commute)
haftmann@22422
  1083
  hence "inf a 0 = 0" by (simp only: add_right_cancel)
haftmann@22422
  1084
  then show "0 <= a" by (simp add: le_iff_inf inf_commute)    
obua@14738
  1085
next  
obua@14738
  1086
  assume a: "0 <= a"
obua@14738
  1087
  show "0 <= a + a" by (simp add: add_mono[OF a a, simplified])
obua@14738
  1088
qed
obua@14738
  1089
haftmann@25090
  1090
lemma double_zero: "a + a = 0 \<longleftrightarrow> a = 0"
haftmann@25090
  1091
proof
haftmann@25090
  1092
  assume assm: "a + a = 0"
haftmann@25090
  1093
  then have "a + a + - a = - a" by simp
haftmann@25090
  1094
  then have "a + (a + - a) = - a" by (simp only: add_assoc)
haftmann@25090
  1095
  then have a: "- a = a" by simp (*FIXME tune proof*)
haftmann@25102
  1096
  show "a = 0" apply (rule antisym)
haftmann@25090
  1097
  apply (unfold neg_le_iff_le [symmetric, of a])
haftmann@25090
  1098
  unfolding a apply simp
haftmann@25090
  1099
  unfolding zero_le_double_add_iff_zero_le_single_add [symmetric, of a]
haftmann@25090
  1100
  unfolding assm unfolding le_less apply simp_all done
haftmann@25090
  1101
next
haftmann@25090
  1102
  assume "a = 0" then show "a + a = 0" by simp
haftmann@25090
  1103
qed
haftmann@25090
  1104
haftmann@25090
  1105
lemma zero_less_double_add_iff_zero_less_single_add:
haftmann@25090
  1106
  "0 < a + a \<longleftrightarrow> 0 < a"
haftmann@25090
  1107
proof (cases "a = 0")
haftmann@25090
  1108
  case True then show ?thesis by auto
haftmann@25090
  1109
next
haftmann@25090
  1110
  case False then show ?thesis (*FIXME tune proof*)
haftmann@25090
  1111
  unfolding less_le apply simp apply rule
haftmann@25090
  1112
  apply clarify
haftmann@25090
  1113
  apply rule
haftmann@25090
  1114
  apply assumption
haftmann@25090
  1115
  apply (rule notI)
haftmann@25090
  1116
  unfolding double_zero [symmetric, of a] apply simp
haftmann@25090
  1117
  done
haftmann@25090
  1118
qed
haftmann@25090
  1119
haftmann@25090
  1120
lemma double_add_le_zero_iff_single_add_le_zero [simp]:
haftmann@25090
  1121
  "a + a \<le> 0 \<longleftrightarrow> a \<le> 0" 
obua@14738
  1122
proof -
haftmann@25090
  1123
  have "a + a \<le> 0 \<longleftrightarrow> 0 \<le> - (a + a)" by (subst le_minus_iff, simp)
haftmann@25090
  1124
  moreover have "\<dots> \<longleftrightarrow> a \<le> 0" by (simp add: zero_le_double_add_iff_zero_le_single_add)
obua@14738
  1125
  ultimately show ?thesis by blast
obua@14738
  1126
qed
obua@14738
  1127
haftmann@25090
  1128
lemma double_add_less_zero_iff_single_less_zero [simp]:
haftmann@25090
  1129
  "a + a < 0 \<longleftrightarrow> a < 0"
haftmann@25090
  1130
proof -
haftmann@25090
  1131
  have "a + a < 0 \<longleftrightarrow> 0 < - (a + a)" by (subst less_minus_iff, simp)
haftmann@25090
  1132
  moreover have "\<dots> \<longleftrightarrow> a < 0" by (simp add: zero_less_double_add_iff_zero_less_single_add)
haftmann@25090
  1133
  ultimately show ?thesis by blast
obua@14738
  1134
qed
obua@14738
  1135
haftmann@25230
  1136
declare neg_inf_eq_sup [simp] neg_sup_eq_inf [simp]
haftmann@25230
  1137
haftmann@25230
  1138
lemma le_minus_self_iff: "a \<le> - a \<longleftrightarrow> a \<le> 0"
haftmann@25230
  1139
proof -
haftmann@25230
  1140
  from add_le_cancel_left [of "uminus a" "plus a a" zero]
haftmann@25230
  1141
  have "(a <= -a) = (a+a <= 0)" 
haftmann@25230
  1142
    by (simp add: add_assoc[symmetric])
haftmann@25230
  1143
  thus ?thesis by simp
haftmann@25230
  1144
qed
haftmann@25230
  1145
haftmann@25230
  1146
lemma minus_le_self_iff: "- a \<le> a \<longleftrightarrow> 0 \<le> a"
haftmann@25230
  1147
proof -
haftmann@25230
  1148
  from add_le_cancel_left [of "uminus a" zero "plus a a"]
haftmann@25230
  1149
  have "(-a <= a) = (0 <= a+a)" 
haftmann@25230
  1150
    by (simp add: add_assoc[symmetric])
haftmann@25230
  1151
  thus ?thesis by simp
haftmann@25230
  1152
qed
haftmann@25230
  1153
haftmann@25230
  1154
lemma zero_le_iff_zero_nprt: "0 \<le> a \<longleftrightarrow> nprt a = 0"
nipkow@29667
  1155
by (simp add: le_iff_inf nprt_def inf_commute)
haftmann@25230
  1156
haftmann@25230
  1157
lemma le_zero_iff_zero_pprt: "a \<le> 0 \<longleftrightarrow> pprt a = 0"
nipkow@29667
  1158
by (simp add: le_iff_sup pprt_def sup_commute)
haftmann@25230
  1159
haftmann@25230
  1160
lemma le_zero_iff_pprt_id: "0 \<le> a \<longleftrightarrow> pprt a = a"
nipkow@29667
  1161
by (simp add: le_iff_sup pprt_def sup_commute)
haftmann@25230
  1162
haftmann@25230
  1163
lemma zero_le_iff_nprt_id: "a \<le> 0 \<longleftrightarrow> nprt a = a"
nipkow@29667
  1164
by (simp add: le_iff_inf nprt_def inf_commute)
haftmann@25230
  1165
haftmann@25230
  1166
lemma pprt_mono [simp, noatp]: "a \<le> b \<Longrightarrow> pprt a \<le> pprt b"
nipkow@29667
  1167
by (simp add: le_iff_sup pprt_def sup_ACI sup_assoc [symmetric, of a])
haftmann@25230
  1168
haftmann@25230
  1169
lemma nprt_mono [simp, noatp]: "a \<le> b \<Longrightarrow> nprt a \<le> nprt b"
nipkow@29667
  1170
by (simp add: le_iff_inf nprt_def inf_ACI inf_assoc [symmetric, of a])
haftmann@25230
  1171
haftmann@25090
  1172
end
haftmann@25090
  1173
haftmann@25090
  1174
lemmas add_sup_inf_distribs = add_inf_distrib_right add_inf_distrib_left add_sup_distrib_right add_sup_distrib_left
haftmann@25090
  1175
haftmann@25230
  1176
haftmann@25303
  1177
class lordered_ab_group_add_abs = lordered_ab_group_add + abs +
haftmann@25230
  1178
  assumes abs_lattice: "\<bar>a\<bar> = sup a (- a)"
haftmann@25230
  1179
begin
haftmann@25230
  1180
haftmann@25230
  1181
lemma abs_prts: "\<bar>a\<bar> = pprt a - nprt a"
haftmann@25230
  1182
proof -
haftmann@25230
  1183
  have "0 \<le> \<bar>a\<bar>"
haftmann@25230
  1184
  proof -
haftmann@25230
  1185
    have a: "a \<le> \<bar>a\<bar>" and b: "- a \<le> \<bar>a\<bar>" by (auto simp add: abs_lattice)
haftmann@25230
  1186
    show ?thesis by (rule add_mono [OF a b, simplified])
haftmann@25230
  1187
  qed
haftmann@25230
  1188
  then have "0 \<le> sup a (- a)" unfolding abs_lattice .
haftmann@25230
  1189
  then have "sup (sup a (- a)) 0 = sup a (- a)" by (rule sup_absorb1)
haftmann@25230
  1190
  then show ?thesis
haftmann@25230
  1191
    by (simp add: add_sup_inf_distribs sup_ACI
haftmann@25230
  1192
      pprt_def nprt_def diff_minus abs_lattice)
haftmann@25230
  1193
qed
haftmann@25230
  1194
haftmann@25230
  1195
subclass pordered_ab_group_add_abs
haftmann@25230
  1196
proof -
haftmann@25230
  1197
  have abs_ge_zero [simp]: "\<And>a. 0 \<le> \<bar>a\<bar>"
haftmann@25230
  1198
  proof -
haftmann@25230
  1199
    fix a b
haftmann@25230
  1200
    have a: "a \<le> \<bar>a\<bar>" and b: "- a \<le> \<bar>a\<bar>" by (auto simp add: abs_lattice)
haftmann@25230
  1201
    show "0 \<le> \<bar>a\<bar>" by (rule add_mono [OF a b, simplified])
haftmann@25230
  1202
  qed
haftmann@25230
  1203
  have abs_leI: "\<And>a b. a \<le> b \<Longrightarrow> - a \<le> b \<Longrightarrow> \<bar>a\<bar> \<le> b"
haftmann@25230
  1204
    by (simp add: abs_lattice le_supI)
haftmann@25230
  1205
  show ?thesis
haftmann@28823
  1206
  proof
haftmann@25230
  1207
    fix a
haftmann@25230
  1208
    show "0 \<le> \<bar>a\<bar>" by simp
haftmann@25230
  1209
  next
haftmann@25230
  1210
    fix a
nipkow@29667
  1211
    show "a \<le> \<bar>a\<bar>" by (auto simp add: abs_lattice)
haftmann@25230
  1212
  next
haftmann@25230
  1213
    fix a
nipkow@29667
  1214
    show "\<bar>-a\<bar> = \<bar>a\<bar>" by (simp add: abs_lattice sup_commute)
haftmann@25230
  1215
  next
haftmann@25230
  1216
    fix a b
haftmann@25230
  1217
    show "a \<le> b \<Longrightarrow> - a \<le> b \<Longrightarrow> \<bar>a\<bar> \<le> b" by (erule abs_leI)
haftmann@25230
  1218
  next
haftmann@25230
  1219
    fix a b
haftmann@25230
  1220
    show "\<bar>a + b\<bar> \<le> \<bar>a\<bar> + \<bar>b\<bar>"
haftmann@25230
  1221
    proof -
haftmann@25230
  1222
      have g:"abs a + abs b = sup (a+b) (sup (-a-b) (sup (-a+b) (a + (-b))))" (is "_=sup ?m ?n")
haftmann@25230
  1223
        by (simp add: abs_lattice add_sup_inf_distribs sup_ACI diff_minus)
haftmann@25230
  1224
      have a:"a+b <= sup ?m ?n" by (simp)
haftmann@25230
  1225
      have b:"-a-b <= ?n" by (simp) 
haftmann@25230
  1226
      have c:"?n <= sup ?m ?n" by (simp)
haftmann@25230
  1227
      from b c have d: "-a-b <= sup ?m ?n" by(rule order_trans)
haftmann@25230
  1228
      have e:"-a-b = -(a+b)" by (simp add: diff_minus)
nipkow@29667
  1229
      from a d e have "abs(a+b) <= sup ?m ?n" by (drule_tac abs_leI, auto)
haftmann@25230
  1230
      with g[symmetric] show ?thesis by simp
haftmann@25230
  1231
    qed
haftmann@25230
  1232
  qed auto
haftmann@25230
  1233
qed
haftmann@25230
  1234
haftmann@25230
  1235
end
haftmann@25230
  1236
haftmann@25090
  1237
lemma sup_eq_if:
haftmann@25303
  1238
  fixes a :: "'a\<Colon>{lordered_ab_group_add, linorder}"
haftmann@25090
  1239
  shows "sup a (- a) = (if a < 0 then - a else a)"
haftmann@25090
  1240
proof -
haftmann@25090
  1241
  note add_le_cancel_right [of a a "- a", symmetric, simplified]
haftmann@25090
  1242
  moreover note add_le_cancel_right [of "-a" a a, symmetric, simplified]
haftmann@25090
  1243
  then show ?thesis by (auto simp: sup_max max_def)
haftmann@25090
  1244
qed
haftmann@25090
  1245
haftmann@25090
  1246
lemma abs_if_lattice:
haftmann@25303
  1247
  fixes a :: "'a\<Colon>{lordered_ab_group_add_abs, linorder}"
haftmann@25090
  1248
  shows "\<bar>a\<bar> = (if a < 0 then - a else a)"
nipkow@29667
  1249
by auto
haftmann@25090
  1250
haftmann@25090
  1251
obua@14754
  1252
text {* Needed for abelian cancellation simprocs: *}
obua@14754
  1253
obua@14754
  1254
lemma add_cancel_21: "((x::'a::ab_group_add) + (y + z) = y + u) = (x + z = u)"
obua@14754
  1255
apply (subst add_left_commute)
obua@14754
  1256
apply (subst add_left_cancel)
obua@14754
  1257
apply simp
obua@14754
  1258
done
obua@14754
  1259
obua@14754
  1260
lemma add_cancel_end: "(x + (y + z) = y) = (x = - (z::'a::ab_group_add))"
obua@14754
  1261
apply (subst add_cancel_21[of _ _ _ 0, simplified])
obua@14754
  1262
apply (simp add: add_right_cancel[symmetric, of "x" "-z" "z", simplified])
obua@14754
  1263
done
obua@14754
  1264
obua@14754
  1265
lemma less_eqI: "(x::'a::pordered_ab_group_add) - y = x' - y' \<Longrightarrow> (x < y) = (x' < y')"
obua@14754
  1266
by (simp add: less_iff_diff_less_0[of x y] less_iff_diff_less_0[of x' y'])
obua@14754
  1267
obua@14754
  1268
lemma le_eqI: "(x::'a::pordered_ab_group_add) - y = x' - y' \<Longrightarrow> (y <= x) = (y' <= x')"
obua@14754
  1269
apply (simp add: le_iff_diff_le_0[of y x] le_iff_diff_le_0[of  y' x'])
obua@14754
  1270
apply (simp add: neg_le_iff_le[symmetric, of "y-x" 0] neg_le_iff_le[symmetric, of "y'-x'" 0])
obua@14754
  1271
done
obua@14754
  1272
obua@14754
  1273
lemma eq_eqI: "(x::'a::ab_group_add) - y = x' - y' \<Longrightarrow> (x = y) = (x' = y')"
obua@14754
  1274
by (simp add: eq_iff_diff_eq_0[of x y] eq_iff_diff_eq_0[of x' y'])
obua@14754
  1275
obua@14754
  1276
lemma diff_def: "(x::'a::ab_group_add) - y == x + (-y)"
obua@14754
  1277
by (simp add: diff_minus)
obua@14754
  1278
obua@14754
  1279
lemma add_minus_cancel: "(a::'a::ab_group_add) + (-a + b) = b"
obua@14754
  1280
by (simp add: add_assoc[symmetric])
obua@14754
  1281
haftmann@25090
  1282
lemma le_add_right_mono: 
obua@15178
  1283
  assumes 
obua@15178
  1284
  "a <= b + (c::'a::pordered_ab_group_add)"
obua@15178
  1285
  "c <= d"    
obua@15178
  1286
  shows "a <= b + d"
obua@15178
  1287
  apply (rule_tac order_trans[where y = "b+c"])
obua@15178
  1288
  apply (simp_all add: prems)
obua@15178
  1289
  done
obua@15178
  1290
obua@15178
  1291
lemma estimate_by_abs:
haftmann@25303
  1292
  "a + b <= (c::'a::lordered_ab_group_add_abs) \<Longrightarrow> a <= c + abs b" 
obua@15178
  1293
proof -
nipkow@23477
  1294
  assume "a+b <= c"
nipkow@29667
  1295
  hence 2: "a <= c+(-b)" by (simp add: algebra_simps)
obua@15178
  1296
  have 3: "(-b) <= abs b" by (rule abs_ge_minus_self)
obua@15178
  1297
  show ?thesis by (rule le_add_right_mono[OF 2 3])
obua@15178
  1298
qed
obua@15178
  1299
haftmann@25090
  1300
subsection {* Tools setup *}
haftmann@25090
  1301
haftmann@25077
  1302
lemma add_mono_thms_ordered_semiring [noatp]:
haftmann@25077
  1303
  fixes i j k :: "'a\<Colon>pordered_ab_semigroup_add"
haftmann@25077
  1304
  shows "i \<le> j \<and> k \<le> l \<Longrightarrow> i + k \<le> j + l"
haftmann@25077
  1305
    and "i = j \<and> k \<le> l \<Longrightarrow> i + k \<le> j + l"
haftmann@25077
  1306
    and "i \<le> j \<and> k = l \<Longrightarrow> i + k \<le> j + l"
haftmann@25077
  1307
    and "i = j \<and> k = l \<Longrightarrow> i + k = j + l"
haftmann@25077
  1308
by (rule add_mono, clarify+)+
haftmann@25077
  1309
haftmann@25077
  1310
lemma add_mono_thms_ordered_field [noatp]:
haftmann@25077
  1311
  fixes i j k :: "'a\<Colon>pordered_cancel_ab_semigroup_add"
haftmann@25077
  1312
  shows "i < j \<and> k = l \<Longrightarrow> i + k < j + l"
haftmann@25077
  1313
    and "i = j \<and> k < l \<Longrightarrow> i + k < j + l"
haftmann@25077
  1314
    and "i < j \<and> k \<le> l \<Longrightarrow> i + k < j + l"
haftmann@25077
  1315
    and "i \<le> j \<and> k < l \<Longrightarrow> i + k < j + l"
haftmann@25077
  1316
    and "i < j \<and> k < l \<Longrightarrow> i + k < j + l"
haftmann@25077
  1317
by (auto intro: add_strict_right_mono add_strict_left_mono
haftmann@25077
  1318
  add_less_le_mono add_le_less_mono add_strict_mono)
haftmann@25077
  1319
paulson@17085
  1320
text{*Simplification of @{term "x-y < 0"}, etc.*}
haftmann@24380
  1321
lemmas diff_less_0_iff_less [simp] = less_iff_diff_less_0 [symmetric]
haftmann@24380
  1322
lemmas diff_eq_0_iff_eq [simp, noatp] = eq_iff_diff_eq_0 [symmetric]
haftmann@24380
  1323
lemmas diff_le_0_iff_le [simp] = le_iff_diff_le_0 [symmetric]
paulson@17085
  1324
haftmann@22482
  1325
ML {*
wenzelm@27250
  1326
structure ab_group_add_cancel = Abel_Cancel
wenzelm@27250
  1327
(
haftmann@22482
  1328
haftmann@22482
  1329
(* term order for abelian groups *)
haftmann@22482
  1330
haftmann@22482
  1331
fun agrp_ord (Const (a, _)) = find_index (fn a' => a = a')
haftmann@22997
  1332
      [@{const_name HOL.zero}, @{const_name HOL.plus},
haftmann@22997
  1333
        @{const_name HOL.uminus}, @{const_name HOL.minus}]
haftmann@22482
  1334
  | agrp_ord _ = ~1;
haftmann@22482
  1335
wenzelm@29269
  1336
fun termless_agrp (a, b) = (TermOrd.term_lpo agrp_ord (a, b) = LESS);
haftmann@22482
  1337
haftmann@22482
  1338
local
haftmann@22482
  1339
  val ac1 = mk_meta_eq @{thm add_assoc};
haftmann@22482
  1340
  val ac2 = mk_meta_eq @{thm add_commute};
haftmann@22482
  1341
  val ac3 = mk_meta_eq @{thm add_left_commute};
haftmann@22997
  1342
  fun solve_add_ac thy _ (_ $ (Const (@{const_name HOL.plus},_) $ _ $ _) $ _) =
haftmann@22482
  1343
        SOME ac1
haftmann@22997
  1344
    | solve_add_ac thy _ (_ $ x $ (Const (@{const_name HOL.plus},_) $ y $ z)) =
haftmann@22482
  1345
        if termless_agrp (y, x) then SOME ac3 else NONE
haftmann@22482
  1346
    | solve_add_ac thy _ (_ $ x $ y) =
haftmann@22482
  1347
        if termless_agrp (y, x) then SOME ac2 else NONE
haftmann@22482
  1348
    | solve_add_ac thy _ _ = NONE
haftmann@22482
  1349
in
wenzelm@28262
  1350
  val add_ac_proc = Simplifier.simproc (the_context ())
haftmann@22482
  1351
    "add_ac_proc" ["x + y::'a::ab_semigroup_add"] solve_add_ac;
haftmann@22482
  1352
end;
haftmann@22482
  1353
wenzelm@27250
  1354
val eq_reflection = @{thm eq_reflection};
wenzelm@27250
  1355
  
wenzelm@27250
  1356
val T = @{typ "'a::ab_group_add"};
wenzelm@27250
  1357
haftmann@22482
  1358
val cancel_ss = HOL_basic_ss settermless termless_agrp
haftmann@22482
  1359
  addsimprocs [add_ac_proc] addsimps
nipkow@23085
  1360
  [@{thm add_0_left}, @{thm add_0_right}, @{thm diff_def},
haftmann@22482
  1361
   @{thm minus_add_distrib}, @{thm minus_minus}, @{thm minus_zero},
haftmann@22482
  1362
   @{thm right_minus}, @{thm left_minus}, @{thm add_minus_cancel},
haftmann@22482
  1363
   @{thm minus_add_cancel}];
wenzelm@27250
  1364
wenzelm@27250
  1365
val sum_pats = [@{cterm "x + y::'a::ab_group_add"}, @{cterm "x - y::'a::ab_group_add"}];
haftmann@22482
  1366
  
haftmann@22548
  1367
val eqI_rules = [@{thm less_eqI}, @{thm le_eqI}, @{thm eq_eqI}];
haftmann@22482
  1368
haftmann@22482
  1369
val dest_eqI = 
haftmann@22482
  1370
  fst o HOLogic.dest_bin "op =" HOLogic.boolT o HOLogic.dest_Trueprop o concl_of;
haftmann@22482
  1371
wenzelm@27250
  1372
);
haftmann@22482
  1373
*}
haftmann@22482
  1374
wenzelm@26480
  1375
ML {*
haftmann@22482
  1376
  Addsimprocs [ab_group_add_cancel.sum_conv, ab_group_add_cancel.rel_conv];
haftmann@22482
  1377
*}
paulson@17085
  1378
obua@14738
  1379
end