# Theory Lattice_Algebras

theory Lattice_Algebras
imports Complex_Main
```(*  Author:     Steven Obua, TU Muenchen *)

section ‹Various algebraic structures combined with a lattice›

theory Lattice_Algebras
imports Complex_Main
begin

begin

lemma add_inf_distrib_left: "a + inf b c = inf (a + b) (a + c)"
apply (rule antisym)
apply (rule add_le_imp_le_left [of "uminus a"])
done

lemma add_inf_distrib_right: "inf a b + c = inf (a + c) (b + c)"
proof -
have "c + inf a b = inf (c + a) (c + b)"
then show ?thesis
qed

end

begin

lemma add_sup_distrib_left: "a + sup b c = sup (a + b) (a + c)"
apply (rule antisym)
apply (rule add_le_imp_le_left [of "uminus a"])
apply (simp only: add.assoc [symmetric], simp)
apply (rule le_supI)
done

lemma add_sup_distrib_right: "sup a b + c = sup (a + c) (b + c)"
proof -
have "c + sup a b = sup (c+a) (c+b)"
then show ?thesis
qed

end

begin

lemma inf_eq_neg_sup: "inf a b = - sup (- a) (- b)"
proof (rule inf_unique)
fix a b c :: 'a
show "- sup (- a) (- b) ≤ a"
by (rule add_le_imp_le_right [of _ "sup (uminus a) (uminus b)"])
show "- sup (-a) (-b) ≤ b"
by (rule add_le_imp_le_right [of _ "sup (uminus a) (uminus b)"])
assume "a ≤ b" "a ≤ c"
then show "a ≤ - sup (-b) (-c)"
by (subst neg_le_iff_le [symmetric]) (simp add: le_supI)
qed

lemma sup_eq_neg_inf: "sup a b = - inf (- a) (- b)"
proof (rule sup_unique)
fix a b c :: 'a
show "a ≤ - inf (- a) (- b)"
by (rule add_le_imp_le_right [of _ "inf (uminus a) (uminus b)"])
show "b ≤ - inf (- a) (- b)"
by (rule add_le_imp_le_right [of _ "inf (uminus a) (uminus b)"])
assume "a ≤ c" "b ≤ c"
then show "- inf (- a) (- b) ≤ c"
by (subst neg_le_iff_le [symmetric]) (simp add: le_infI)
qed

lemma neg_inf_eq_sup: "- inf a b = sup (- a) (- b)"

lemma diff_inf_eq_sup: "a - inf b c = a + sup (- b) (- c)"
using neg_inf_eq_sup [of b c, symmetric] by simp

lemma neg_sup_eq_inf: "- sup a b = inf (- a) (- b)"

lemma diff_sup_eq_inf: "a - sup b c = a + inf (- b) (- c)"
using neg_sup_eq_inf [of b c, symmetric] by simp

lemma add_eq_inf_sup: "a + b = sup a b + inf a b"
proof -
have "0 = - inf 0 (a - b) + inf (a - b) 0"
then have "0 = sup 0 (b - a) + inf (a - b) 0"
then have "0 = (- a + sup a b) + (inf a b + (- b))"
then show ?thesis
qed

subsection ‹Positive Part, Negative Part, Absolute Value›

definition nprt :: "'a ⇒ 'a"
where "nprt x = inf x 0"

definition pprt :: "'a ⇒ 'a"
where "pprt x = sup x 0"

lemma pprt_neg: "pprt (- x) = - nprt x"
proof -
have "sup (- x) 0 = sup (- x) (- 0)"
unfolding minus_zero ..
also have "… = - inf x 0"
unfolding neg_inf_eq_sup ..
finally have "sup (- x) 0 = - inf x 0" .
then show ?thesis
unfolding pprt_def nprt_def .
qed

lemma nprt_neg: "nprt (- x) = - pprt x"
proof -
from pprt_neg have "pprt (- (- x)) = - nprt (- x)" .
then have "pprt x = - nprt (- x)" by simp
then show ?thesis by simp
qed

lemma prts: "a = pprt a + nprt a"

lemma zero_le_pprt[simp]: "0 ≤ pprt a"

lemma nprt_le_zero[simp]: "nprt a ≤ 0"

lemma le_eq_neg: "a ≤ - b ⟷ a + b ≤ 0"
(is "?l = ?r")
proof
assume ?l
then show ?r
apply -
apply (rule add_le_imp_le_right[of _ "uminus b" _])
done
next
assume ?r
then show ?l
apply -
apply (rule add_le_imp_le_right[of _ "b" _])
apply simp
done
qed

lemma pprt_0[simp]: "pprt 0 = 0" by (simp add: pprt_def)
lemma nprt_0[simp]: "nprt 0 = 0" by (simp add: nprt_def)

lemma pprt_eq_id [simp, no_atp]: "0 ≤ x ⟹ pprt x = x"

lemma nprt_eq_id [simp, no_atp]: "x ≤ 0 ⟹ nprt x = x"

lemma pprt_eq_0 [simp, no_atp]: "x ≤ 0 ⟹ pprt x = 0"

lemma nprt_eq_0 [simp, no_atp]: "0 ≤ x ⟹ nprt x = 0"

lemma sup_0_imp_0:
assumes "sup a (- a) = 0"
shows "a = 0"
proof -
have p: "0 ≤ a" if "sup a (- a) = 0" for a :: 'a
proof -
from that have "sup a (- a) + a = a"
by simp
then have "sup (a + a) 0 = a"
then have "sup (a + a) 0 ≤ a"
by simp
then show ?thesis
by (blast intro: order_trans inf_sup_ord)
qed
from assms have **: "sup (-a) (-(-a)) = 0"
from p[OF assms] p[OF **] show "a = 0"
by simp
qed

lemma inf_0_imp_0: "inf a (- a) = 0 ⟹ a = 0"
apply (erule sup_0_imp_0)
done

lemma inf_0_eq_0 [simp, no_atp]: "inf a (- a) = 0 ⟷ a = 0"
apply rule
apply (erule inf_0_imp_0)
apply simp
done

lemma sup_0_eq_0 [simp, no_atp]: "sup a (- a) = 0 ⟷ a = 0"
apply rule
apply (erule sup_0_imp_0)
apply simp
done

lemma zero_le_double_add_iff_zero_le_single_add [simp]: "0 ≤ a + a ⟷ 0 ≤ a"
(is "?lhs ⟷ ?rhs")
proof
show ?rhs if ?lhs
proof -
from that have a: "inf (a + a) 0 = 0"
have "inf a 0 + inf a 0 = inf (inf (a + a) 0) a"  (is "?l = _")
then have "?l = 0 + inf a 0"
then have "inf a 0 = 0"
then show ?thesis
unfolding le_iff_inf by (simp add: inf_commute)
qed
show ?lhs if ?rhs
qed

lemma double_zero [simp]: "a + a = 0 ⟷ a = 0"
(is "?lhs ⟷ ?rhs")
proof
show ?rhs if ?lhs
proof -
from that have "a + a + - a = - a"
by simp
then have "a + (a + - a) = - a"
then have a: "- a = a"
by simp
show ?thesis
apply (rule antisym)
apply (unfold neg_le_iff_le [symmetric, of a])
unfolding a
apply simp
unfolding that
unfolding le_less
apply simp_all
done
qed
show ?lhs if ?rhs
using that by simp
qed

lemma zero_less_double_add_iff_zero_less_single_add [simp]: "0 < a + a ⟷ 0 < a"
proof (cases "a = 0")
case True
then show ?thesis by auto
next
case False
then show ?thesis
unfolding less_le
apply simp
apply rule
apply clarify
apply rule
apply assumption
apply (rule notI)
unfolding double_zero [symmetric, of a]
apply blast
done
qed

lemma double_add_le_zero_iff_single_add_le_zero [simp]: "a + a ≤ 0 ⟷ a ≤ 0"
proof -
have "a + a ≤ 0 ⟷ 0 ≤ - (a + a)"
by (subst le_minus_iff) simp
moreover have "… ⟷ a ≤ 0"
ultimately show ?thesis
by blast
qed

lemma double_add_less_zero_iff_single_less_zero [simp]: "a + a < 0 ⟷ a < 0"
proof -
have "a + a < 0 ⟷ 0 < - (a + a)"
by (subst less_minus_iff) simp
moreover have "… ⟷ a < 0"
ultimately show ?thesis
by blast
qed

declare neg_inf_eq_sup [simp] neg_sup_eq_inf [simp] diff_inf_eq_sup [simp] diff_sup_eq_inf [simp]

lemma le_minus_self_iff: "a ≤ - a ⟷ a ≤ 0"
proof -
from add_le_cancel_left [of "uminus a" "plus a a" zero]
have "a ≤ - a ⟷ a + a ≤ 0"
then show ?thesis
by simp
qed

lemma minus_le_self_iff: "- a ≤ a ⟷ 0 ≤ a"
proof -
have "- a ≤ a ⟷ 0 ≤ a + a"
using add_le_cancel_left [of "uminus a" zero "plus a a"]
then show ?thesis
by simp
qed

lemma zero_le_iff_zero_nprt: "0 ≤ a ⟷ nprt a = 0"
unfolding le_iff_inf by (simp add: nprt_def inf_commute)

lemma le_zero_iff_zero_pprt: "a ≤ 0 ⟷ pprt a = 0"
unfolding le_iff_sup by (simp add: pprt_def sup_commute)

lemma le_zero_iff_pprt_id: "0 ≤ a ⟷ pprt a = a"
unfolding le_iff_sup by (simp add: pprt_def sup_commute)

lemma zero_le_iff_nprt_id: "a ≤ 0 ⟷ nprt a = a"
unfolding le_iff_inf by (simp add: nprt_def inf_commute)

lemma pprt_mono [simp, no_atp]: "a ≤ b ⟹ pprt a ≤ pprt b"
unfolding le_iff_sup by (simp add: pprt_def sup_aci sup_assoc [symmetric, of a])

lemma nprt_mono [simp, no_atp]: "a ≤ b ⟹ nprt a ≤ nprt b"
unfolding le_iff_inf by (simp add: nprt_def inf_aci inf_assoc [symmetric, of a])

end

assumes abs_lattice: "¦a¦ = sup a (- a)"
begin

lemma abs_prts: "¦a¦ = pprt a - nprt a"
proof -
have "0 ≤ ¦a¦"
proof -
have a: "a ≤ ¦a¦" and b: "- a ≤ ¦a¦"
show ?thesis
by (rule add_mono [OF a b, simplified])
qed
then have "0 ≤ sup a (- a)"
unfolding abs_lattice .
then have "sup (sup a (- a)) 0 = sup a (- a)"
by (rule sup_absorb1)
then show ?thesis
qed

proof
have abs_ge_zero [simp]: "0 ≤ ¦a¦" for a
proof -
have a: "a ≤ ¦a¦" and b: "- a ≤ ¦a¦"
show "0 ≤ ¦a¦"
by (rule add_mono [OF a b, simplified])
qed
have abs_leI: "a ≤ b ⟹ - a ≤ b ⟹ ¦a¦ ≤ b" for a b
fix a b
show "0 ≤ ¦a¦"
by simp
show "a ≤ ¦a¦"
show "¦-a¦ = ¦a¦"
show "- a ≤ b ⟹ ¦a¦ ≤ b" if "a ≤ b"
using that by (rule abs_leI)
show "¦a + b¦ ≤ ¦a¦ + ¦b¦"
proof -
have g: "¦a¦ + ¦b¦ = sup (a + b) (sup (- a - b) (sup (- a + b) (a + (- b))))"
(is "_ = sup ?m ?n")
have a: "a + b ≤ sup ?m ?n"
by simp
have b: "- a - b ≤ ?n"
by simp
have c: "?n ≤ sup ?m ?n"
by simp
from b c have d: "- a - b ≤ sup ?m ?n"
by (rule order_trans)
have e: "- a - b = - (a + b)"
by simp
from a d e have "¦a + b¦ ≤ sup ?m ?n"
apply -
apply (drule abs_leI)
done
with g[symmetric] show ?thesis by simp
qed
qed

end

lemma sup_eq_if:
shows "sup a (- a) = (if a < 0 then - a else a)"
using add_le_cancel_right [of a a "- a", symmetric, simplified]
and add_le_cancel_right [of "-a" a a, symmetric, simplified]
by (auto simp: sup_max max.absorb1 max.absorb2)

lemma abs_if_lattice:
shows "¦a¦ = (if a < 0 then - a else a)"
by auto

lemma estimate_by_abs:
fixes a b c :: "'a::lattice_ab_group_add_abs"
assumes "a + b ≤ c"
shows "a ≤ c + ¦b¦"
proof -
from assms have "a ≤ c + (- b)"
have "- b ≤ ¦b¦"
by (rule abs_ge_minus_self)
then have "c + (- b) ≤ c + ¦b¦"
with ‹a ≤ c + (- b)› show ?thesis
by (rule order_trans)
qed

class lattice_ring = ordered_ring + lattice_ab_group_add_abs
begin

end

lemma abs_le_mult:
fixes a b :: "'a::lattice_ring"
shows "¦a * b¦ ≤ ¦a¦ * ¦b¦"
proof -
let ?x = "pprt a * pprt b - pprt a * nprt b - nprt a * pprt b + nprt a * nprt b"
let ?y = "pprt a * pprt b + pprt a * nprt b + nprt a * pprt b + nprt a * nprt b"
have a: "¦a¦ * ¦b¦ = ?x"
by (simp only: abs_prts[of a] abs_prts[of b] algebra_simps)
have bh: "u = a ⟹ v = b ⟹
u * v = pprt a * pprt b + pprt a * nprt b +
nprt a * pprt b + nprt a * nprt b" for u v :: 'a
apply (subst prts[of u], subst prts[of v])
done
note b = this[OF refl[of a] refl[of b]]
have xy: "- ?x ≤ ?y"
apply simp
mult_nonpos_nonpos nprt_le_zero zero_le_pprt)
done
have yx: "?y ≤ ?x"
apply simp
mult_nonpos_nonneg nprt_le_zero zero_le_pprt)
done
have i1: "a * b ≤ ¦a¦ * ¦b¦"
by (simp only: a b yx)
have i2: "- (¦a¦ * ¦b¦) ≤ a * b"
by (simp only: a b xy)
show ?thesis
apply (rule abs_leI)
done
qed

instance lattice_ring ⊆ ordered_ring_abs
proof
fix a b :: "'a::lattice_ring"
assume a: "(0 ≤ a ∨ a ≤ 0) ∧ (0 ≤ b ∨ b ≤ 0)"
show "¦a * b¦ = ¦a¦ * ¦b¦"
proof -
have s: "(0 ≤ a * b) ∨ (a * b ≤ 0)"
apply auto
apply (rule_tac split_mult_pos_le)
apply (rule_tac contrapos_np[of "a * b ≤ 0"])
apply simp
apply (rule_tac split_mult_neg_le)
using a
apply blast
done
have mulprts: "a * b = (pprt a + nprt a) * (pprt b + nprt b)"
show ?thesis
proof (cases "0 ≤ a * b")
case True
then show ?thesis
using a
algebra_simps
iffD1[OF zero_le_iff_zero_nprt] iffD1[OF le_zero_iff_zero_pprt]
iffD1[OF le_zero_iff_pprt_id] iffD1[OF zero_le_iff_nprt_id])
apply(drule (1) mult_nonneg_nonpos[of a b], simp)
apply(drule (1) mult_nonneg_nonpos2[of b a], simp)
done
next
case False
with s have "a * b ≤ 0"
by simp
then show ?thesis
apply (insert a)
apply(drule (1) mult_nonneg_nonneg[of a b],simp)
apply(drule (1) mult_nonpos_nonpos[of a b],simp)
done
qed
qed
qed

lemma mult_le_prts:
fixes a b :: "'a::lattice_ring"
assumes "a1 ≤ a"
and "a ≤ a2"
and "b1 ≤ b"
and "b ≤ b2"
shows "a * b ≤
pprt a2 * pprt b2 + pprt a1 * nprt b2 + nprt a2 * pprt b1 + nprt a1 * nprt b1"
proof -
have "a * b = (pprt a + nprt a) * (pprt b + nprt b)"
by (subst prts[symmetric])+ simp
then have "a * b = pprt a * pprt b + pprt a * nprt b + nprt a * pprt b + nprt a * nprt b"
moreover have "pprt a * pprt b ≤ pprt a2 * pprt b2"
moreover have "pprt a * nprt b ≤ pprt a1 * nprt b2"
proof -
have "pprt a * nprt b ≤ pprt a * nprt b2"
moreover have "pprt a * nprt b2 ≤ pprt a1 * nprt b2"
ultimately show ?thesis
by simp
qed
moreover have "nprt a * pprt b ≤ nprt a2 * pprt b1"
proof -
have "nprt a * pprt b ≤ nprt a2 * pprt b"
moreover have "nprt a2 * pprt b ≤ nprt a2 * pprt b1"
ultimately show ?thesis
by simp
qed
moreover have "nprt a * nprt b ≤ nprt a1 * nprt b1"
proof -
have "nprt a * nprt b ≤ nprt a * nprt b1"
moreover have "nprt a * nprt b1 ≤ nprt a1 * nprt b1"
ultimately show ?thesis
by simp
qed
ultimately show ?thesis
by - (rule add_mono | simp)+
qed

lemma mult_ge_prts:
fixes a b :: "'a::lattice_ring"
assumes "a1 ≤ a"
and "a ≤ a2"
and "b1 ≤ b"
and "b ≤ b2"
shows "a * b ≥
nprt a1 * pprt b2 + nprt a2 * nprt b2 + pprt a1 * pprt b1 + pprt a2 * nprt b1"
proof -
from assms have a1: "- a2 ≤ -a"
by auto
from assms have a2: "- a ≤ -a1"
by auto
from mult_le_prts[of "- a2" "- a" "- a1" "b1" b "b2",
OF a1 a2 assms(3) assms(4), simplified nprt_neg pprt_neg]
have le: "- (a * b) ≤
- nprt a1 * pprt b2 + - nprt a2 * nprt b2 +
- pprt a1 * pprt b1 + - pprt a2 * nprt b1"
by simp
then have "- (- nprt a1 * pprt b2 + - nprt a2 * nprt b2 +
- pprt a1 * pprt b1 + - pprt a2 * nprt b1) ≤ a * b"
by (simp only: minus_le_iff)
then show ?thesis
qed

instance int :: lattice_ring
proof
fix k :: int
show "¦k¦ = sup k (- k)"