src/HOL/OrderedGroup.thy
author haftmann
Tue Nov 06 13:12:48 2007 +0100 (2007-11-06)
changeset 25307 389902f0a0c8
parent 25303 0699e20feabd
child 25512 4134f7c782e2
permissions -rw-r--r--
simplified specification of *_abs class
     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 subsection {* Support for reasoning about signs *}
   398 
   399 class pordered_comm_monoid_add =
   400   pordered_cancel_ab_semigroup_add + comm_monoid_add
   401 begin
   402 
   403 lemma add_pos_nonneg:
   404   assumes "0 < a" and "0 \<le> b"
   405     shows "0 < a + b"
   406 proof -
   407   have "0 + 0 < a + b" 
   408     using assms by (rule add_less_le_mono)
   409   then show ?thesis by simp
   410 qed
   411 
   412 lemma add_pos_pos:
   413   assumes "0 < a" and "0 < b"
   414     shows "0 < a + b"
   415   by (rule add_pos_nonneg) (insert assms, auto)
   416 
   417 lemma add_nonneg_pos:
   418   assumes "0 \<le> a" and "0 < b"
   419     shows "0 < a + b"
   420 proof -
   421   have "0 + 0 < a + b" 
   422     using assms by (rule add_le_less_mono)
   423   then show ?thesis by simp
   424 qed
   425 
   426 lemma add_nonneg_nonneg:
   427   assumes "0 \<le> a" and "0 \<le> b"
   428     shows "0 \<le> a + b"
   429 proof -
   430   have "0 + 0 \<le> a + b" 
   431     using assms by (rule add_mono)
   432   then show ?thesis by simp
   433 qed
   434 
   435 lemma add_neg_nonpos: 
   436   assumes "a < 0" and "b \<le> 0"
   437   shows "a + b < 0"
   438 proof -
   439   have "a + b < 0 + 0"
   440     using assms by (rule add_less_le_mono)
   441   then show ?thesis by simp
   442 qed
   443 
   444 lemma add_neg_neg: 
   445   assumes "a < 0" and "b < 0"
   446   shows "a + b < 0"
   447   by (rule add_neg_nonpos) (insert assms, auto)
   448 
   449 lemma add_nonpos_neg:
   450   assumes "a \<le> 0" and "b < 0"
   451   shows "a + b < 0"
   452 proof -
   453   have "a + b < 0 + 0"
   454     using assms by (rule add_le_less_mono)
   455   then show ?thesis by simp
   456 qed
   457 
   458 lemma add_nonpos_nonpos:
   459   assumes "a \<le> 0" and "b \<le> 0"
   460   shows "a + b \<le> 0"
   461 proof -
   462   have "a + b \<le> 0 + 0"
   463     using assms by (rule add_mono)
   464   then show ?thesis by simp
   465 qed
   466 
   467 end
   468 
   469 class pordered_ab_group_add =
   470   ab_group_add + pordered_ab_semigroup_add
   471 begin
   472 
   473 subclass pordered_cancel_ab_semigroup_add
   474   by unfold_locales
   475 
   476 subclass pordered_ab_semigroup_add_imp_le
   477 proof unfold_locales
   478   fix a b c :: 'a
   479   assume "c + a \<le> c + b"
   480   hence "(-c) + (c + a) \<le> (-c) + (c + b)" by (rule add_left_mono)
   481   hence "((-c) + c) + a \<le> ((-c) + c) + b" by (simp only: add_assoc)
   482   thus "a \<le> b" by simp
   483 qed
   484 
   485 subclass pordered_comm_monoid_add
   486   by unfold_locales
   487 
   488 lemma max_diff_distrib_left:
   489   shows "max x y - z = max (x - z) (y - z)"
   490   by (simp add: diff_minus, rule max_add_distrib_left) 
   491 
   492 lemma min_diff_distrib_left:
   493   shows "min x y - z = min (x - z) (y - z)"
   494   by (simp add: diff_minus, rule min_add_distrib_left) 
   495 
   496 lemma le_imp_neg_le:
   497   assumes "a \<le> b"
   498   shows "-b \<le> -a"
   499 proof -
   500   have "-a+a \<le> -a+b"
   501     using `a \<le> b` by (rule add_left_mono) 
   502   hence "0 \<le> -a+b"
   503     by simp
   504   hence "0 + (-b) \<le> (-a + b) + (-b)"
   505     by (rule add_right_mono) 
   506   thus ?thesis
   507     by (simp add: add_assoc)
   508 qed
   509 
   510 lemma neg_le_iff_le [simp]: "- b \<le> - a \<longleftrightarrow> a \<le> b"
   511 proof 
   512   assume "- b \<le> - a"
   513   hence "- (- a) \<le> - (- b)"
   514     by (rule le_imp_neg_le)
   515   thus "a\<le>b" by simp
   516 next
   517   assume "a\<le>b"
   518   thus "-b \<le> -a" by (rule le_imp_neg_le)
   519 qed
   520 
   521 lemma neg_le_0_iff_le [simp]: "- a \<le> 0 \<longleftrightarrow> 0 \<le> a"
   522   by (subst neg_le_iff_le [symmetric], simp)
   523 
   524 lemma neg_0_le_iff_le [simp]: "0 \<le> - a \<longleftrightarrow> a \<le> 0"
   525   by (subst neg_le_iff_le [symmetric], simp)
   526 
   527 lemma neg_less_iff_less [simp]: "- b < - a \<longleftrightarrow> a < b"
   528   by (force simp add: less_le) 
   529 
   530 lemma neg_less_0_iff_less [simp]: "- a < 0 \<longleftrightarrow> 0 < a"
   531   by (subst neg_less_iff_less [symmetric], simp)
   532 
   533 lemma neg_0_less_iff_less [simp]: "0 < - a \<longleftrightarrow> a < 0"
   534   by (subst neg_less_iff_less [symmetric], simp)
   535 
   536 text{*The next several equations can make the simplifier loop!*}
   537 
   538 lemma less_minus_iff: "a < - b \<longleftrightarrow> b < - a"
   539 proof -
   540   have "(- (-a) < - b) = (b < - a)" by (rule neg_less_iff_less)
   541   thus ?thesis by simp
   542 qed
   543 
   544 lemma minus_less_iff: "- a < b \<longleftrightarrow> - b < a"
   545 proof -
   546   have "(- a < - (-b)) = (- b < a)" by (rule neg_less_iff_less)
   547   thus ?thesis by simp
   548 qed
   549 
   550 lemma le_minus_iff: "a \<le> - b \<longleftrightarrow> b \<le> - a"
   551 proof -
   552   have mm: "!! a (b::'a). (-(-a)) < -b \<Longrightarrow> -(-b) < -a" by (simp only: minus_less_iff)
   553   have "(- (- a) <= -b) = (b <= - a)" 
   554     apply (auto simp only: le_less)
   555     apply (drule mm)
   556     apply (simp_all)
   557     apply (drule mm[simplified], assumption)
   558     done
   559   then show ?thesis by simp
   560 qed
   561 
   562 lemma minus_le_iff: "- a \<le> b \<longleftrightarrow> - b \<le> a"
   563   by (auto simp add: le_less minus_less_iff)
   564 
   565 lemma less_iff_diff_less_0: "a < b \<longleftrightarrow> a - b < 0"
   566 proof -
   567   have  "(a < b) = (a + (- b) < b + (-b))"  
   568     by (simp only: add_less_cancel_right)
   569   also have "... =  (a - b < 0)" by (simp add: diff_minus)
   570   finally show ?thesis .
   571 qed
   572 
   573 lemma diff_less_eq: "a - b < c \<longleftrightarrow> a < c + b"
   574 apply (subst less_iff_diff_less_0 [of a])
   575 apply (rule less_iff_diff_less_0 [of _ c, THEN ssubst])
   576 apply (simp add: diff_minus add_ac)
   577 done
   578 
   579 lemma less_diff_eq: "a < c - b \<longleftrightarrow> a + b < c"
   580 apply (subst less_iff_diff_less_0 [of "plus a b"])
   581 apply (subst less_iff_diff_less_0 [of a])
   582 apply (simp add: diff_minus add_ac)
   583 done
   584 
   585 lemma diff_le_eq: "a - b \<le> c \<longleftrightarrow> a \<le> c + b"
   586   by (auto simp add: le_less diff_less_eq diff_add_cancel add_diff_cancel)
   587 
   588 lemma le_diff_eq: "a \<le> c - b \<longleftrightarrow> a + b \<le> c"
   589   by (auto simp add: le_less less_diff_eq diff_add_cancel add_diff_cancel)
   590 
   591 lemmas compare_rls =
   592        diff_minus [symmetric]
   593        add_diff_eq diff_add_eq diff_diff_eq diff_diff_eq2
   594        diff_less_eq less_diff_eq diff_le_eq le_diff_eq
   595        diff_eq_eq eq_diff_eq
   596 
   597 text{*This list of rewrites simplifies (in)equalities by bringing subtractions
   598   to the top and then moving negative terms to the other side.
   599   Use with @{text add_ac}*}
   600 lemmas (in -) compare_rls =
   601        diff_minus [symmetric]
   602        add_diff_eq diff_add_eq diff_diff_eq diff_diff_eq2
   603        diff_less_eq less_diff_eq diff_le_eq le_diff_eq
   604        diff_eq_eq eq_diff_eq
   605 
   606 lemma le_iff_diff_le_0: "a \<le> b \<longleftrightarrow> a - b \<le> 0"
   607   by (simp add: compare_rls)
   608 
   609 lemmas group_simps =
   610   add_ac
   611   add_diff_eq diff_add_eq diff_diff_eq diff_diff_eq2
   612   diff_eq_eq eq_diff_eq diff_minus [symmetric] uminus_add_conv_diff
   613   diff_less_eq less_diff_eq diff_le_eq le_diff_eq
   614 
   615 end
   616 
   617 lemmas group_simps =
   618   mult_ac
   619   add_ac
   620   add_diff_eq diff_add_eq diff_diff_eq diff_diff_eq2
   621   diff_eq_eq eq_diff_eq diff_minus [symmetric] uminus_add_conv_diff
   622   diff_less_eq less_diff_eq diff_le_eq le_diff_eq
   623 
   624 class ordered_ab_semigroup_add =
   625   linorder + pordered_ab_semigroup_add
   626 
   627 class ordered_cancel_ab_semigroup_add =
   628   linorder + pordered_cancel_ab_semigroup_add
   629 begin
   630 
   631 subclass ordered_ab_semigroup_add
   632   by unfold_locales
   633 
   634 subclass pordered_ab_semigroup_add_imp_le
   635 proof unfold_locales
   636   fix a b c :: 'a
   637   assume le: "c + a <= c + b"  
   638   show "a <= b"
   639   proof (rule ccontr)
   640     assume w: "~ a \<le> b"
   641     hence "b <= a" by (simp add: linorder_not_le)
   642     hence le2: "c + b <= c + a" by (rule add_left_mono)
   643     have "a = b" 
   644       apply (insert le)
   645       apply (insert le2)
   646       apply (drule antisym, simp_all)
   647       done
   648     with w show False 
   649       by (simp add: linorder_not_le [symmetric])
   650   qed
   651 qed
   652 
   653 end
   654 
   655 class ordered_ab_group_add =
   656   linorder + pordered_ab_group_add
   657 begin
   658 
   659 subclass ordered_cancel_ab_semigroup_add 
   660   by unfold_locales
   661 
   662 lemma neg_less_eq_nonneg:
   663   "- a \<le> a \<longleftrightarrow> 0 \<le> a"
   664 proof
   665   assume A: "- a \<le> a" show "0 \<le> a"
   666   proof (rule classical)
   667     assume "\<not> 0 \<le> a"
   668     then have "a < 0" by auto
   669     with A have "- a < 0" by (rule le_less_trans)
   670     then show ?thesis by auto
   671   qed
   672 next
   673   assume A: "0 \<le> a" show "- a \<le> a"
   674   proof (rule order_trans)
   675     show "- a \<le> 0" using A by (simp add: minus_le_iff)
   676   next
   677     show "0 \<le> a" using A .
   678   qed
   679 qed
   680   
   681 lemma less_eq_neg_nonpos:
   682   "a \<le> - a \<longleftrightarrow> a \<le> 0"
   683 proof
   684   assume A: "a \<le> - a" show "a \<le> 0"
   685   proof (rule classical)
   686     assume "\<not> a \<le> 0"
   687     then have "0 < a" by auto
   688     then have "0 < - a" using A by (rule less_le_trans)
   689     then show ?thesis by auto
   690   qed
   691 next
   692   assume A: "a \<le> 0" show "a \<le> - a"
   693   proof (rule order_trans)
   694     show "0 \<le> - a" using A by (simp add: minus_le_iff)
   695   next
   696     show "a \<le> 0" using A .
   697   qed
   698 qed
   699 
   700 lemma equal_neg_zero:
   701   "a = - a \<longleftrightarrow> a = 0"
   702 proof
   703   assume "a = 0" then show "a = - a" by simp
   704 next
   705   assume A: "a = - a" show "a = 0"
   706   proof (cases "0 \<le> a")
   707     case True with A have "0 \<le> - a" by auto
   708     with le_minus_iff have "a \<le> 0" by simp
   709     with True show ?thesis by (auto intro: order_trans)
   710   next
   711     case False then have B: "a \<le> 0" by auto
   712     with A have "- a \<le> 0" by auto
   713     with B show ?thesis by (auto intro: order_trans)
   714   qed
   715 qed
   716 
   717 lemma neg_equal_zero:
   718   "- a = a \<longleftrightarrow> a = 0"
   719   unfolding equal_neg_zero [symmetric] by auto
   720 
   721 end
   722 
   723 -- {* FIXME localize the following *}
   724 
   725 lemma add_increasing:
   726   fixes c :: "'a::{pordered_ab_semigroup_add_imp_le, comm_monoid_add}"
   727   shows  "[|0\<le>a; b\<le>c|] ==> b \<le> a + c"
   728 by (insert add_mono [of 0 a b c], simp)
   729 
   730 lemma add_increasing2:
   731   fixes c :: "'a::{pordered_ab_semigroup_add_imp_le, comm_monoid_add}"
   732   shows  "[|0\<le>c; b\<le>a|] ==> b \<le> a + c"
   733 by (simp add:add_increasing add_commute[of a])
   734 
   735 lemma add_strict_increasing:
   736   fixes c :: "'a::{pordered_ab_semigroup_add_imp_le, comm_monoid_add}"
   737   shows "[|0<a; b\<le>c|] ==> b < a + c"
   738 by (insert add_less_le_mono [of 0 a b c], simp)
   739 
   740 lemma add_strict_increasing2:
   741   fixes c :: "'a::{pordered_ab_semigroup_add_imp_le, comm_monoid_add}"
   742   shows "[|0\<le>a; b<c|] ==> b < a + c"
   743 by (insert add_le_less_mono [of 0 a b c], simp)
   744 
   745 
   746 class pordered_ab_group_add_abs = pordered_ab_group_add + abs +
   747   assumes abs_ge_zero [simp]: "\<bar>a\<bar> \<ge> 0"
   748     and abs_ge_self: "a \<le> \<bar>a\<bar>"
   749     and abs_leI: "a \<le> b \<Longrightarrow> - a \<le> b \<Longrightarrow> \<bar>a\<bar> \<le> b"
   750     and abs_minus_cancel [simp]: "\<bar>-a\<bar> = \<bar>a\<bar>"
   751     and abs_triangle_ineq: "\<bar>a + b\<bar> \<le> \<bar>a\<bar> + \<bar>b\<bar>"
   752 begin
   753 
   754 lemma abs_minus_le_zero: "- \<bar>a\<bar> \<le> 0"
   755   unfolding neg_le_0_iff_le by simp
   756 
   757 lemma abs_of_nonneg [simp]:
   758   assumes nonneg: "0 \<le> a"
   759   shows "\<bar>a\<bar> = a"
   760 proof (rule antisym)
   761   from nonneg le_imp_neg_le have "- a \<le> 0" by simp
   762   from this nonneg have "- a \<le> a" by (rule order_trans)
   763   then show "\<bar>a\<bar> \<le> a" by (auto intro: abs_leI)
   764 qed (rule abs_ge_self)
   765 
   766 lemma abs_idempotent [simp]: "\<bar>\<bar>a\<bar>\<bar> = \<bar>a\<bar>"
   767   by (rule antisym)
   768     (auto intro!: abs_ge_self abs_leI order_trans [of "uminus (abs a)" zero "abs a"])
   769 
   770 lemma abs_eq_0 [simp]: "\<bar>a\<bar> = 0 \<longleftrightarrow> a = 0"
   771 proof -
   772   have "\<bar>a\<bar> = 0 \<Longrightarrow> a = 0"
   773   proof (rule antisym)
   774     assume zero: "\<bar>a\<bar> = 0"
   775     with abs_ge_self show "a \<le> 0" by auto
   776     from zero have "\<bar>-a\<bar> = 0" by simp
   777     with abs_ge_self [of "uminus a"] have "- a \<le> 0" by auto
   778     with neg_le_0_iff_le show "0 \<le> a" by auto
   779   qed
   780   then show ?thesis by auto
   781 qed
   782 
   783 lemma abs_zero [simp]: "\<bar>0\<bar> = 0"
   784   by simp
   785 
   786 lemma abs_0_eq [simp, noatp]: "0 = \<bar>a\<bar> \<longleftrightarrow> a = 0"
   787 proof -
   788   have "0 = \<bar>a\<bar> \<longleftrightarrow> \<bar>a\<bar> = 0" by (simp only: eq_ac)
   789   thus ?thesis by simp
   790 qed
   791 
   792 lemma abs_le_zero_iff [simp]: "\<bar>a\<bar> \<le> 0 \<longleftrightarrow> a = 0" 
   793 proof
   794   assume "\<bar>a\<bar> \<le> 0"
   795   then have "\<bar>a\<bar> = 0" by (rule antisym) simp
   796   thus "a = 0" by simp
   797 next
   798   assume "a = 0"
   799   thus "\<bar>a\<bar> \<le> 0" by simp
   800 qed
   801 
   802 lemma zero_less_abs_iff [simp]: "0 < \<bar>a\<bar> \<longleftrightarrow> a \<noteq> 0"
   803   by (simp add: less_le)
   804 
   805 lemma abs_not_less_zero [simp]: "\<not> \<bar>a\<bar> < 0"
   806 proof -
   807   have a: "\<And>x y. x \<le> y \<Longrightarrow> \<not> y < x" by auto
   808   show ?thesis by (simp add: a)
   809 qed
   810 
   811 lemma abs_ge_minus_self: "- a \<le> \<bar>a\<bar>"
   812 proof -
   813   have "- a \<le> \<bar>-a\<bar>" by (rule abs_ge_self)
   814   then show ?thesis by simp
   815 qed
   816 
   817 lemma abs_minus_commute: 
   818   "\<bar>a - b\<bar> = \<bar>b - a\<bar>"
   819 proof -
   820   have "\<bar>a - b\<bar> = \<bar>- (a - b)\<bar>" by (simp only: abs_minus_cancel)
   821   also have "... = \<bar>b - a\<bar>" by simp
   822   finally show ?thesis .
   823 qed
   824 
   825 lemma abs_of_pos: "0 < a \<Longrightarrow> \<bar>a\<bar> = a"
   826   by (rule abs_of_nonneg, rule less_imp_le)
   827 
   828 lemma abs_of_nonpos [simp]:
   829   assumes "a \<le> 0"
   830   shows "\<bar>a\<bar> = - a"
   831 proof -
   832   let ?b = "- a"
   833   have "- ?b \<le> 0 \<Longrightarrow> \<bar>- ?b\<bar> = - (- ?b)"
   834   unfolding abs_minus_cancel [of "?b"]
   835   unfolding neg_le_0_iff_le [of "?b"]
   836   unfolding minus_minus by (erule abs_of_nonneg)
   837   then show ?thesis using assms by auto
   838 qed
   839   
   840 lemma abs_of_neg: "a < 0 \<Longrightarrow> \<bar>a\<bar> = - a"
   841   by (rule abs_of_nonpos, rule less_imp_le)
   842 
   843 lemma abs_le_D1: "\<bar>a\<bar> \<le> b \<Longrightarrow> a \<le> b"
   844   by (insert abs_ge_self, blast intro: order_trans)
   845 
   846 lemma abs_le_D2: "\<bar>a\<bar> \<le> b \<Longrightarrow> - a \<le> b"
   847   by (insert abs_le_D1 [of "uminus a"], simp)
   848 
   849 lemma abs_le_iff: "\<bar>a\<bar> \<le> b \<longleftrightarrow> a \<le> b \<and> - a \<le> b"
   850   by (blast intro: abs_leI dest: abs_le_D1 abs_le_D2)
   851 
   852 lemma abs_triangle_ineq2: "\<bar>a\<bar> - \<bar>b\<bar> \<le> \<bar>a - b\<bar>"
   853   apply (simp add: compare_rls)
   854   apply (subgoal_tac "abs a = abs (plus (minus a b) b)")
   855   apply (erule ssubst)
   856   apply (rule abs_triangle_ineq)
   857   apply (rule arg_cong) back
   858   apply (simp add: compare_rls)
   859 done
   860 
   861 lemma abs_triangle_ineq3: "\<bar>\<bar>a\<bar> - \<bar>b\<bar>\<bar> \<le> \<bar>a - b\<bar>"
   862   apply (subst abs_le_iff)
   863   apply auto
   864   apply (rule abs_triangle_ineq2)
   865   apply (subst abs_minus_commute)
   866   apply (rule abs_triangle_ineq2)
   867 done
   868 
   869 lemma abs_triangle_ineq4: "\<bar>a - b\<bar> \<le> \<bar>a\<bar> + \<bar>b\<bar>"
   870 proof -
   871   have "abs(a - b) = abs(a + - b)"
   872     by (subst diff_minus, rule refl)
   873   also have "... <= abs a + abs (- b)"
   874     by (rule abs_triangle_ineq)
   875   finally show ?thesis
   876     by simp
   877 qed
   878 
   879 lemma abs_diff_triangle_ineq: "\<bar>a + b - (c + d)\<bar> \<le> \<bar>a - c\<bar> + \<bar>b - d\<bar>"
   880 proof -
   881   have "\<bar>a + b - (c+d)\<bar> = \<bar>(a-c) + (b-d)\<bar>" by (simp add: diff_minus add_ac)
   882   also have "... \<le> \<bar>a-c\<bar> + \<bar>b-d\<bar>" by (rule abs_triangle_ineq)
   883   finally show ?thesis .
   884 qed
   885 
   886 lemma abs_add_abs [simp]:
   887   "\<bar>\<bar>a\<bar> + \<bar>b\<bar>\<bar> = \<bar>a\<bar> + \<bar>b\<bar>" (is "?L = ?R")
   888 proof (rule antisym)
   889   show "?L \<ge> ?R" by(rule abs_ge_self)
   890 next
   891   have "?L \<le> \<bar>\<bar>a\<bar>\<bar> + \<bar>\<bar>b\<bar>\<bar>" by(rule abs_triangle_ineq)
   892   also have "\<dots> = ?R" by simp
   893   finally show "?L \<le> ?R" .
   894 qed
   895 
   896 end
   897 
   898 
   899 subsection {* Lattice Ordered (Abelian) Groups *}
   900 
   901 class lordered_ab_group_add_meet = pordered_ab_group_add + lower_semilattice
   902 begin
   903 
   904 lemma add_inf_distrib_left:
   905   "a + inf b c = inf (a + b) (a + c)"
   906 apply (rule antisym)
   907 apply (simp_all add: le_infI)
   908 apply (rule add_le_imp_le_left [of "uminus a"])
   909 apply (simp only: add_assoc [symmetric], simp)
   910 apply rule
   911 apply (rule add_le_imp_le_left[of "a"], simp only: add_assoc[symmetric], simp)+
   912 done
   913 
   914 lemma add_inf_distrib_right:
   915   "inf a b + c = inf (a + c) (b + c)"
   916 proof -
   917   have "c + inf a b = inf (c+a) (c+b)" by (simp add: add_inf_distrib_left)
   918   thus ?thesis by (simp add: add_commute)
   919 qed
   920 
   921 end
   922 
   923 class lordered_ab_group_add_join = pordered_ab_group_add + upper_semilattice
   924 begin
   925 
   926 lemma add_sup_distrib_left:
   927   "a + sup b c = sup (a + b) (a + c)" 
   928 apply (rule antisym)
   929 apply (rule add_le_imp_le_left [of "uminus a"])
   930 apply (simp only: add_assoc[symmetric], simp)
   931 apply rule
   932 apply (rule add_le_imp_le_left [of "a"], simp only: add_assoc[symmetric], simp)+
   933 apply (rule le_supI)
   934 apply (simp_all)
   935 done
   936 
   937 lemma add_sup_distrib_right:
   938   "sup a b + c = sup (a+c) (b+c)"
   939 proof -
   940   have "c + sup a b = sup (c+a) (c+b)" by (simp add: add_sup_distrib_left)
   941   thus ?thesis by (simp add: add_commute)
   942 qed
   943 
   944 end
   945 
   946 class lordered_ab_group_add = pordered_ab_group_add + lattice
   947 begin
   948 
   949 subclass lordered_ab_group_add_meet by unfold_locales
   950 subclass lordered_ab_group_add_join by unfold_locales
   951 
   952 lemmas add_sup_inf_distribs = add_inf_distrib_right add_inf_distrib_left add_sup_distrib_right add_sup_distrib_left
   953 
   954 lemma inf_eq_neg_sup: "inf a b = - sup (-a) (-b)"
   955 proof (rule inf_unique)
   956   fix a b :: 'a
   957   show "- sup (-a) (-b) \<le> a"
   958     by (rule add_le_imp_le_right [of _ "sup (uminus a) (uminus b)"])
   959       (simp, simp add: add_sup_distrib_left)
   960 next
   961   fix a b :: 'a
   962   show "- sup (-a) (-b) \<le> b"
   963     by (rule add_le_imp_le_right [of _ "sup (uminus a) (uminus b)"])
   964       (simp, simp add: add_sup_distrib_left)
   965 next
   966   fix a b c :: 'a
   967   assume "a \<le> b" "a \<le> c"
   968   then show "a \<le> - sup (-b) (-c)" by (subst neg_le_iff_le [symmetric])
   969     (simp add: le_supI)
   970 qed
   971   
   972 lemma sup_eq_neg_inf: "sup a b = - inf (-a) (-b)"
   973 proof (rule sup_unique)
   974   fix a b :: 'a
   975   show "a \<le> - inf (-a) (-b)"
   976     by (rule add_le_imp_le_right [of _ "inf (uminus a) (uminus b)"])
   977       (simp, simp add: add_inf_distrib_left)
   978 next
   979   fix a b :: 'a
   980   show "b \<le> - inf (-a) (-b)"
   981     by (rule add_le_imp_le_right [of _ "inf (uminus a) (uminus b)"])
   982       (simp, simp add: add_inf_distrib_left)
   983 next
   984   fix a b c :: 'a
   985   assume "a \<le> c" "b \<le> c"
   986   then show "- inf (-a) (-b) \<le> c" by (subst neg_le_iff_le [symmetric])
   987     (simp add: le_infI)
   988 qed
   989 
   990 lemma neg_inf_eq_sup: "- inf a b = sup (-a) (-b)"
   991   by (simp add: inf_eq_neg_sup)
   992 
   993 lemma neg_sup_eq_inf: "- sup a b = inf (-a) (-b)"
   994   by (simp add: sup_eq_neg_inf)
   995 
   996 lemma add_eq_inf_sup: "a + b = sup a b + inf a b"
   997 proof -
   998   have "0 = - inf 0 (a-b) + inf (a-b) 0" by (simp add: inf_commute)
   999   hence "0 = sup 0 (b-a) + inf (a-b) 0" by (simp add: inf_eq_neg_sup)
  1000   hence "0 = (-a + sup a b) + (inf a b + (-b))"
  1001     apply (simp add: add_sup_distrib_left add_inf_distrib_right)
  1002     by (simp add: diff_minus add_commute)
  1003   thus ?thesis
  1004     apply (simp add: compare_rls)
  1005     apply (subst add_left_cancel [symmetric, of "plus a b" "plus (sup a b) (inf a b)" "uminus a"])
  1006     apply (simp only: add_assoc, simp add: add_assoc[symmetric])
  1007     done
  1008 qed
  1009 
  1010 subsection {* Positive Part, Negative Part, Absolute Value *}
  1011 
  1012 definition
  1013   nprt :: "'a \<Rightarrow> 'a" where
  1014   "nprt x = inf x 0"
  1015 
  1016 definition
  1017   pprt :: "'a \<Rightarrow> 'a" where
  1018   "pprt x = sup x 0"
  1019 
  1020 lemma pprt_neg: "pprt (- x) = - nprt x"
  1021 proof -
  1022   have "sup (- x) 0 = sup (- x) (- 0)" unfolding minus_zero ..
  1023   also have "\<dots> = - inf x 0" unfolding neg_inf_eq_sup ..
  1024   finally have "sup (- x) 0 = - inf x 0" .
  1025   then show ?thesis unfolding pprt_def nprt_def .
  1026 qed
  1027 
  1028 lemma nprt_neg: "nprt (- x) = - pprt x"
  1029 proof -
  1030   from pprt_neg have "pprt (- (- x)) = - nprt (- x)" .
  1031   then have "pprt x = - nprt (- x)" by simp
  1032   then show ?thesis by simp
  1033 qed
  1034 
  1035 lemma prts: "a = pprt a + nprt a"
  1036   by (simp add: pprt_def nprt_def add_eq_inf_sup[symmetric])
  1037 
  1038 lemma zero_le_pprt[simp]: "0 \<le> pprt a"
  1039   by (simp add: pprt_def)
  1040 
  1041 lemma nprt_le_zero[simp]: "nprt a \<le> 0"
  1042   by (simp add: nprt_def)
  1043 
  1044 lemma le_eq_neg: "a \<le> - b \<longleftrightarrow> a + b \<le> 0" (is "?l = ?r")
  1045 proof -
  1046   have a: "?l \<longrightarrow> ?r"
  1047     apply (auto)
  1048     apply (rule add_le_imp_le_right[of _ "uminus b" _])
  1049     apply (simp add: add_assoc)
  1050     done
  1051   have b: "?r \<longrightarrow> ?l"
  1052     apply (auto)
  1053     apply (rule add_le_imp_le_right[of _ "b" _])
  1054     apply (simp)
  1055     done
  1056   from a b show ?thesis by blast
  1057 qed
  1058 
  1059 lemma pprt_0[simp]: "pprt 0 = 0" by (simp add: pprt_def)
  1060 lemma nprt_0[simp]: "nprt 0 = 0" by (simp add: nprt_def)
  1061 
  1062 lemma pprt_eq_id [simp, noatp]: "0 \<le> x \<Longrightarrow> pprt x = x"
  1063   by (simp add: pprt_def le_iff_sup sup_ACI)
  1064 
  1065 lemma nprt_eq_id [simp, noatp]: "x \<le> 0 \<Longrightarrow> nprt x = x"
  1066   by (simp add: nprt_def le_iff_inf inf_ACI)
  1067 
  1068 lemma pprt_eq_0 [simp, noatp]: "x \<le> 0 \<Longrightarrow> pprt x = 0"
  1069   by (simp add: pprt_def le_iff_sup sup_ACI)
  1070 
  1071 lemma nprt_eq_0 [simp, noatp]: "0 \<le> x \<Longrightarrow> nprt x = 0"
  1072   by (simp add: nprt_def le_iff_inf inf_ACI)
  1073 
  1074 lemma sup_0_imp_0: "sup a (- a) = 0 \<Longrightarrow> a = 0"
  1075 proof -
  1076   {
  1077     fix a::'a
  1078     assume hyp: "sup a (-a) = 0"
  1079     hence "sup a (-a) + a = a" by (simp)
  1080     hence "sup (a+a) 0 = a" by (simp add: add_sup_distrib_right) 
  1081     hence "sup (a+a) 0 <= a" by (simp)
  1082     hence "0 <= a" by (blast intro: order_trans inf_sup_ord)
  1083   }
  1084   note p = this
  1085   assume hyp:"sup a (-a) = 0"
  1086   hence hyp2:"sup (-a) (-(-a)) = 0" by (simp add: sup_commute)
  1087   from p[OF hyp] p[OF hyp2] show "a = 0" by simp
  1088 qed
  1089 
  1090 lemma inf_0_imp_0: "inf a (-a) = 0 \<Longrightarrow> a = 0"
  1091 apply (simp add: inf_eq_neg_sup)
  1092 apply (simp add: sup_commute)
  1093 apply (erule sup_0_imp_0)
  1094 done
  1095 
  1096 lemma inf_0_eq_0 [simp, noatp]: "inf a (- a) = 0 \<longleftrightarrow> a = 0"
  1097   by (rule, erule inf_0_imp_0) simp
  1098 
  1099 lemma sup_0_eq_0 [simp, noatp]: "sup a (- a) = 0 \<longleftrightarrow> a = 0"
  1100   by (rule, erule sup_0_imp_0) simp
  1101 
  1102 lemma zero_le_double_add_iff_zero_le_single_add [simp]:
  1103   "0 \<le> a + a \<longleftrightarrow> 0 \<le> a"
  1104 proof
  1105   assume "0 <= a + a"
  1106   hence a:"inf (a+a) 0 = 0" by (simp add: le_iff_inf inf_commute)
  1107   have "(inf a 0)+(inf a 0) = inf (inf (a+a) 0) a" (is "?l=_")
  1108     by (simp add: add_sup_inf_distribs inf_ACI)
  1109   hence "?l = 0 + inf a 0" by (simp add: a, simp add: inf_commute)
  1110   hence "inf a 0 = 0" by (simp only: add_right_cancel)
  1111   then show "0 <= a" by (simp add: le_iff_inf inf_commute)    
  1112 next  
  1113   assume a: "0 <= a"
  1114   show "0 <= a + a" by (simp add: add_mono[OF a a, simplified])
  1115 qed
  1116 
  1117 lemma double_zero: "a + a = 0 \<longleftrightarrow> a = 0"
  1118 proof
  1119   assume assm: "a + a = 0"
  1120   then have "a + a + - a = - a" by simp
  1121   then have "a + (a + - a) = - a" by (simp only: add_assoc)
  1122   then have a: "- a = a" by simp (*FIXME tune proof*)
  1123   show "a = 0" apply (rule antisym)
  1124   apply (unfold neg_le_iff_le [symmetric, of a])
  1125   unfolding a apply simp
  1126   unfolding zero_le_double_add_iff_zero_le_single_add [symmetric, of a]
  1127   unfolding assm unfolding le_less apply simp_all done
  1128 next
  1129   assume "a = 0" then show "a + a = 0" by simp
  1130 qed
  1131 
  1132 lemma zero_less_double_add_iff_zero_less_single_add:
  1133   "0 < a + a \<longleftrightarrow> 0 < a"
  1134 proof (cases "a = 0")
  1135   case True then show ?thesis by auto
  1136 next
  1137   case False then show ?thesis (*FIXME tune proof*)
  1138   unfolding less_le apply simp apply rule
  1139   apply clarify
  1140   apply rule
  1141   apply assumption
  1142   apply (rule notI)
  1143   unfolding double_zero [symmetric, of a] apply simp
  1144   done
  1145 qed
  1146 
  1147 lemma double_add_le_zero_iff_single_add_le_zero [simp]:
  1148   "a + a \<le> 0 \<longleftrightarrow> a \<le> 0" 
  1149 proof -
  1150   have "a + a \<le> 0 \<longleftrightarrow> 0 \<le> - (a + a)" by (subst le_minus_iff, simp)
  1151   moreover have "\<dots> \<longleftrightarrow> a \<le> 0" by (simp add: zero_le_double_add_iff_zero_le_single_add)
  1152   ultimately show ?thesis by blast
  1153 qed
  1154 
  1155 lemma double_add_less_zero_iff_single_less_zero [simp]:
  1156   "a + a < 0 \<longleftrightarrow> a < 0"
  1157 proof -
  1158   have "a + a < 0 \<longleftrightarrow> 0 < - (a + a)" by (subst less_minus_iff, simp)
  1159   moreover have "\<dots> \<longleftrightarrow> a < 0" by (simp add: zero_less_double_add_iff_zero_less_single_add)
  1160   ultimately show ?thesis by blast
  1161 qed
  1162 
  1163 declare neg_inf_eq_sup [simp] neg_sup_eq_inf [simp]
  1164 
  1165 lemma le_minus_self_iff: "a \<le> - a \<longleftrightarrow> a \<le> 0"
  1166 proof -
  1167   from add_le_cancel_left [of "uminus a" "plus a a" zero]
  1168   have "(a <= -a) = (a+a <= 0)" 
  1169     by (simp add: add_assoc[symmetric])
  1170   thus ?thesis by simp
  1171 qed
  1172 
  1173 lemma minus_le_self_iff: "- a \<le> a \<longleftrightarrow> 0 \<le> a"
  1174 proof -
  1175   from add_le_cancel_left [of "uminus a" zero "plus a a"]
  1176   have "(-a <= a) = (0 <= a+a)" 
  1177     by (simp add: add_assoc[symmetric])
  1178   thus ?thesis by simp
  1179 qed
  1180 
  1181 lemma zero_le_iff_zero_nprt: "0 \<le> a \<longleftrightarrow> nprt a = 0"
  1182   by (simp add: le_iff_inf nprt_def inf_commute)
  1183 
  1184 lemma le_zero_iff_zero_pprt: "a \<le> 0 \<longleftrightarrow> pprt a = 0"
  1185   by (simp add: le_iff_sup pprt_def sup_commute)
  1186 
  1187 lemma le_zero_iff_pprt_id: "0 \<le> a \<longleftrightarrow> pprt a = a"
  1188   by (simp add: le_iff_sup pprt_def sup_commute)
  1189 
  1190 lemma zero_le_iff_nprt_id: "a \<le> 0 \<longleftrightarrow> nprt a = a"
  1191   by (simp add: le_iff_inf nprt_def inf_commute)
  1192 
  1193 lemma pprt_mono [simp, noatp]: "a \<le> b \<Longrightarrow> pprt a \<le> pprt b"
  1194   by (simp add: le_iff_sup pprt_def sup_ACI sup_assoc [symmetric, of a])
  1195 
  1196 lemma nprt_mono [simp, noatp]: "a \<le> b \<Longrightarrow> nprt a \<le> nprt b"
  1197   by (simp add: le_iff_inf nprt_def inf_ACI inf_assoc [symmetric, of a])
  1198 
  1199 end
  1200 
  1201 lemmas add_sup_inf_distribs = add_inf_distrib_right add_inf_distrib_left add_sup_distrib_right add_sup_distrib_left
  1202 
  1203 
  1204 class lordered_ab_group_add_abs = lordered_ab_group_add + abs +
  1205   assumes abs_lattice: "\<bar>a\<bar> = sup a (- a)"
  1206 begin
  1207 
  1208 lemma abs_prts: "\<bar>a\<bar> = pprt a - nprt a"
  1209 proof -
  1210   have "0 \<le> \<bar>a\<bar>"
  1211   proof -
  1212     have a: "a \<le> \<bar>a\<bar>" and b: "- a \<le> \<bar>a\<bar>" by (auto simp add: abs_lattice)
  1213     show ?thesis by (rule add_mono [OF a b, simplified])
  1214   qed
  1215   then have "0 \<le> sup a (- a)" unfolding abs_lattice .
  1216   then have "sup (sup a (- a)) 0 = sup a (- a)" by (rule sup_absorb1)
  1217   then show ?thesis
  1218     by (simp add: add_sup_inf_distribs sup_ACI
  1219       pprt_def nprt_def diff_minus abs_lattice)
  1220 qed
  1221 
  1222 subclass pordered_ab_group_add_abs
  1223 proof -
  1224   have abs_ge_zero [simp]: "\<And>a. 0 \<le> \<bar>a\<bar>"
  1225   proof -
  1226     fix a b
  1227     have a: "a \<le> \<bar>a\<bar>" and b: "- a \<le> \<bar>a\<bar>" by (auto simp add: abs_lattice)
  1228     show "0 \<le> \<bar>a\<bar>" by (rule add_mono [OF a b, simplified])
  1229   qed
  1230   have abs_leI: "\<And>a b. a \<le> b \<Longrightarrow> - a \<le> b \<Longrightarrow> \<bar>a\<bar> \<le> b"
  1231     by (simp add: abs_lattice le_supI)
  1232   show ?thesis
  1233   proof unfold_locales
  1234     fix a
  1235     show "0 \<le> \<bar>a\<bar>" by simp
  1236   next
  1237     fix a
  1238     show "a \<le> \<bar>a\<bar>"
  1239       by (auto simp add: abs_lattice)
  1240   next
  1241     fix a
  1242     show "\<bar>-a\<bar> = \<bar>a\<bar>"
  1243       by (simp add: abs_lattice sup_commute)
  1244   next
  1245     fix a b
  1246     show "a \<le> b \<Longrightarrow> - a \<le> b \<Longrightarrow> \<bar>a\<bar> \<le> b" by (erule abs_leI)
  1247   next
  1248     fix a b
  1249     show "\<bar>a + b\<bar> \<le> \<bar>a\<bar> + \<bar>b\<bar>"
  1250     proof -
  1251       have g:"abs a + abs b = sup (a+b) (sup (-a-b) (sup (-a+b) (a + (-b))))" (is "_=sup ?m ?n")
  1252         by (simp add: abs_lattice add_sup_inf_distribs sup_ACI diff_minus)
  1253       have a:"a+b <= sup ?m ?n" by (simp)
  1254       have b:"-a-b <= ?n" by (simp) 
  1255       have c:"?n <= sup ?m ?n" by (simp)
  1256       from b c have d: "-a-b <= sup ?m ?n" by(rule order_trans)
  1257       have e:"-a-b = -(a+b)" by (simp add: diff_minus)
  1258       from a d e have "abs(a+b) <= sup ?m ?n" 
  1259         by (drule_tac abs_leI, auto)
  1260       with g[symmetric] show ?thesis by simp
  1261     qed
  1262   qed auto
  1263 qed
  1264 
  1265 end
  1266 
  1267 lemma sup_eq_if:
  1268   fixes a :: "'a\<Colon>{lordered_ab_group_add, linorder}"
  1269   shows "sup a (- a) = (if a < 0 then - a else a)"
  1270 proof -
  1271   note add_le_cancel_right [of a a "- a", symmetric, simplified]
  1272   moreover note add_le_cancel_right [of "-a" a a, symmetric, simplified]
  1273   then show ?thesis by (auto simp: sup_max max_def)
  1274 qed
  1275 
  1276 lemma abs_if_lattice:
  1277   fixes a :: "'a\<Colon>{lordered_ab_group_add_abs, linorder}"
  1278   shows "\<bar>a\<bar> = (if a < 0 then - a else a)"
  1279   by auto
  1280 
  1281 
  1282 text {* Needed for abelian cancellation simprocs: *}
  1283 
  1284 lemma add_cancel_21: "((x::'a::ab_group_add) + (y + z) = y + u) = (x + z = u)"
  1285 apply (subst add_left_commute)
  1286 apply (subst add_left_cancel)
  1287 apply simp
  1288 done
  1289 
  1290 lemma add_cancel_end: "(x + (y + z) = y) = (x = - (z::'a::ab_group_add))"
  1291 apply (subst add_cancel_21[of _ _ _ 0, simplified])
  1292 apply (simp add: add_right_cancel[symmetric, of "x" "-z" "z", simplified])
  1293 done
  1294 
  1295 lemma less_eqI: "(x::'a::pordered_ab_group_add) - y = x' - y' \<Longrightarrow> (x < y) = (x' < y')"
  1296 by (simp add: less_iff_diff_less_0[of x y] less_iff_diff_less_0[of x' y'])
  1297 
  1298 lemma le_eqI: "(x::'a::pordered_ab_group_add) - y = x' - y' \<Longrightarrow> (y <= x) = (y' <= x')"
  1299 apply (simp add: le_iff_diff_le_0[of y x] le_iff_diff_le_0[of  y' x'])
  1300 apply (simp add: neg_le_iff_le[symmetric, of "y-x" 0] neg_le_iff_le[symmetric, of "y'-x'" 0])
  1301 done
  1302 
  1303 lemma eq_eqI: "(x::'a::ab_group_add) - y = x' - y' \<Longrightarrow> (x = y) = (x' = y')"
  1304 by (simp add: eq_iff_diff_eq_0[of x y] eq_iff_diff_eq_0[of x' y'])
  1305 
  1306 lemma diff_def: "(x::'a::ab_group_add) - y == x + (-y)"
  1307 by (simp add: diff_minus)
  1308 
  1309 lemma add_minus_cancel: "(a::'a::ab_group_add) + (-a + b) = b"
  1310 by (simp add: add_assoc[symmetric])
  1311 
  1312 lemma le_add_right_mono: 
  1313   assumes 
  1314   "a <= b + (c::'a::pordered_ab_group_add)"
  1315   "c <= d"    
  1316   shows "a <= b + d"
  1317   apply (rule_tac order_trans[where y = "b+c"])
  1318   apply (simp_all add: prems)
  1319   done
  1320 
  1321 lemma estimate_by_abs:
  1322   "a + b <= (c::'a::lordered_ab_group_add_abs) \<Longrightarrow> a <= c + abs b" 
  1323 proof -
  1324   assume "a+b <= c"
  1325   hence 2: "a <= c+(-b)" by (simp add: group_simps)
  1326   have 3: "(-b) <= abs b" by (rule abs_ge_minus_self)
  1327   show ?thesis by (rule le_add_right_mono[OF 2 3])
  1328 qed
  1329 
  1330 subsection {* Tools setup *}
  1331 
  1332 lemma add_mono_thms_ordered_semiring [noatp]:
  1333   fixes i j k :: "'a\<Colon>pordered_ab_semigroup_add"
  1334   shows "i \<le> j \<and> k \<le> l \<Longrightarrow> i + k \<le> j + l"
  1335     and "i = j \<and> k \<le> l \<Longrightarrow> i + k \<le> j + l"
  1336     and "i \<le> j \<and> k = l \<Longrightarrow> i + k \<le> j + l"
  1337     and "i = j \<and> k = l \<Longrightarrow> i + k = j + l"
  1338 by (rule add_mono, clarify+)+
  1339 
  1340 lemma add_mono_thms_ordered_field [noatp]:
  1341   fixes i j k :: "'a\<Colon>pordered_cancel_ab_semigroup_add"
  1342   shows "i < j \<and> k = l \<Longrightarrow> i + k < j + l"
  1343     and "i = j \<and> k < l \<Longrightarrow> i + k < j + l"
  1344     and "i < j \<and> k \<le> l \<Longrightarrow> i + k < j + l"
  1345     and "i \<le> j \<and> k < l \<Longrightarrow> i + k < j + l"
  1346     and "i < j \<and> k < l \<Longrightarrow> i + k < j + l"
  1347 by (auto intro: add_strict_right_mono add_strict_left_mono
  1348   add_less_le_mono add_le_less_mono add_strict_mono)
  1349 
  1350 text{*Simplification of @{term "x-y < 0"}, etc.*}
  1351 lemmas diff_less_0_iff_less [simp] = less_iff_diff_less_0 [symmetric]
  1352 lemmas diff_eq_0_iff_eq [simp, noatp] = eq_iff_diff_eq_0 [symmetric]
  1353 lemmas diff_le_0_iff_le [simp] = le_iff_diff_le_0 [symmetric]
  1354 
  1355 ML {*
  1356 structure ab_group_add_cancel = Abel_Cancel(
  1357 struct
  1358 
  1359 (* term order for abelian groups *)
  1360 
  1361 fun agrp_ord (Const (a, _)) = find_index (fn a' => a = a')
  1362       [@{const_name HOL.zero}, @{const_name HOL.plus},
  1363         @{const_name HOL.uminus}, @{const_name HOL.minus}]
  1364   | agrp_ord _ = ~1;
  1365 
  1366 fun termless_agrp (a, b) = (Term.term_lpo agrp_ord (a, b) = LESS);
  1367 
  1368 local
  1369   val ac1 = mk_meta_eq @{thm add_assoc};
  1370   val ac2 = mk_meta_eq @{thm add_commute};
  1371   val ac3 = mk_meta_eq @{thm add_left_commute};
  1372   fun solve_add_ac thy _ (_ $ (Const (@{const_name HOL.plus},_) $ _ $ _) $ _) =
  1373         SOME ac1
  1374     | solve_add_ac thy _ (_ $ x $ (Const (@{const_name HOL.plus},_) $ y $ z)) =
  1375         if termless_agrp (y, x) then SOME ac3 else NONE
  1376     | solve_add_ac thy _ (_ $ x $ y) =
  1377         if termless_agrp (y, x) then SOME ac2 else NONE
  1378     | solve_add_ac thy _ _ = NONE
  1379 in
  1380   val add_ac_proc = Simplifier.simproc @{theory}
  1381     "add_ac_proc" ["x + y::'a::ab_semigroup_add"] solve_add_ac;
  1382 end;
  1383 
  1384 val cancel_ss = HOL_basic_ss settermless termless_agrp
  1385   addsimprocs [add_ac_proc] addsimps
  1386   [@{thm add_0_left}, @{thm add_0_right}, @{thm diff_def},
  1387    @{thm minus_add_distrib}, @{thm minus_minus}, @{thm minus_zero},
  1388    @{thm right_minus}, @{thm left_minus}, @{thm add_minus_cancel},
  1389    @{thm minus_add_cancel}];
  1390   
  1391 val eq_reflection = @{thm eq_reflection};
  1392   
  1393 val thy_ref = Theory.check_thy @{theory};
  1394 
  1395 val T = @{typ "'a\<Colon>ab_group_add"};
  1396 
  1397 val eqI_rules = [@{thm less_eqI}, @{thm le_eqI}, @{thm eq_eqI}];
  1398 
  1399 val dest_eqI = 
  1400   fst o HOLogic.dest_bin "op =" HOLogic.boolT o HOLogic.dest_Trueprop o concl_of;
  1401 
  1402 end);
  1403 *}
  1404 
  1405 ML_setup {*
  1406   Addsimprocs [ab_group_add_cancel.sum_conv, ab_group_add_cancel.rel_conv];
  1407 *}
  1408 
  1409 end