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