\import Algebra.Group
\import Algebra.Meta
\import Algebra.Monoid
\import Algebra.QModule
\import Algebra.Ring
\import Algebra.Semiring
\import Arith.Nat
\import Data.Array
\import Data.Fin
\import Function
\import Function.Meta
\import Logic
\import Logic.Meta
\import Meta
\import Order.Lattice
\import Order.PartialOrder
\import Paths
\import Paths.Meta
\import Set
\import Set.Fin
\import Set.Fin.Instances
\import Set.Set

\class PosetAddMonoid \extends AddMonoid, Poset {
  | <=_+ {a b c d : E} : a <= b -> c <= d -> a + c <= b + d

  \lemma BigSum_<= {n : Nat} {l l' : Array E n} (p : \Pi (j : Fin n) -> l j <= l' j) : BigSum l <= BigSum l' \elim n, l, l'
    | 0, nil, nil => <=-refl
    | suc n, a :: l, a' :: l' => <=_+ (p 0) $ BigSum_<= \lam j => p (suc j)

  \lemma BigSum_>=0 {l : Array E} (q : \Pi (j : Fin l.len) -> 0 <= l j) : 0 <= BigSum l
    => transport (<= _) BigSum_replicate0 $ BigSum_<= {_} {_} {replicate l.len zro} q

  \lemma BigSum_>=index {l : Array E} (q : \Pi (j : Fin l.len) -> 0 <= l j) (j : Fin l.len) : l j <= BigSum l \elim l, j
    | a :: l, 0 => transport (<= _) zro-right $ <=_+ <=-refl $ BigSum_>=0 \lam j => q (suc j)
    | a :: l, suc j => transport (<= _) zro-left $ <=_+ (q 0) $ BigSum_>=index (\lam j => q (suc j)) j

  \lemma BigSum_>=0-zero {l : Array E} (q : \Pi (j : Fin l.len) -> 0 <= l j) (p : BigSum l = 0) (j : Fin l.len) : l j = 0 \elim l, j
    | a :: l, 0 => <=-antisymmetric (=_<= (inv zro-right) <=∘ <=_+ <=-refl (BigSum_>=0 \lam j => q (suc j)) <=∘ =_<= p) (q 0)
    | a :: l, suc j => BigSum_>=0-zero (\lam j => q (suc j)) (<=-antisymmetric (=_<= (inv zro-left) <=∘ <=_+ (q 0) <=-refl <=∘ =_<= p) (BigSum_>=0 \lam j => q (suc j))) j

  \lemma <=_+-positive {a b : E} (a>=0 : 0 <= a) (b>=0 : 0 <= b) : 0 <= a + b
    => transport (<= _) zro-left $ <=_+ a>=0 b>=0

  \lemma *n_<= {n : Nat} {a b : E} (a<=b : a <= b) : n *n a <= n *n b \elim n
    | 0 => <=-refl
    | suc n => <=_+ (*n_<= a<=b) a<=b

  \lemma *n_>=0 {n : Nat} {a : E} (a>=0 : 0 <= a) : 0 <= n *n a \elim n
    | 0 => <=-refl
    | suc n => <=_+-positive (*n_>=0 a>=0) a>=0

  \lemma *n>=id {n : Nat} (n/=0 : n /= 0) {a : E} (a>=0 : 0 <= a) : a <= n *n a \elim n
    | 0 => absurd (n/=0 idp)
    | suc n => transport (<= _) zro-left (<=_+ (*n_>=0 a>=0) <=-refl)

  \lemma *n_<=-nat {n m : Nat} (n<=m : n NatSemiring.<= m) {a : E} (a>=0 : 0 <= a) : n *n a <= m *n a
    => =_<= (inv zro-right) <=∘ <=_+ <=-refl (*n_>=0 a>=0) <=∘ =_<= (inv *n-rdistr *> pmap (*n a) (<=_exists n<=m))
}

\class PosetAbMonoid \extends PosetAddMonoid, AbMonoid {
  -- | {FinSum} analog of {BigSum_<=} on `Fin n`.
  \lemma FinSum_<= {A : FinSet} {f g : A -> E} (p : \Pi (j : A) -> f j <= g j) : FinSum f <= FinSum g
    => \case FinSum_char f \with {
      | inP (e,q) => transport2 (<=) (inv q) (inv (FinSum_char2 e)) $ BigSum_<= \lam j => p (e j)
    }

  \lemma FinSum_>=0 {A : FinSet} {f : A -> E} (p : \Pi (j : A) -> 0 <= f j) : 0 <= FinSum f
    => transport (<= _) (FinSum_zro \lam j => idp) $ FinSum_<= p

  \lemma FinSum_<=-inj {A B : FinSet} (f : A -> B) (inj : IsInj f) {x : B -> E} (p : \Pi (b : B) -> (\Pi (a : A) -> f a /= b) -> 0 <= x b)
    : FinSum (\lam a => x (f a)) <= FinSum x
    => \have dec => AbMonoid.FinSum-inj.dec {\this} {_} {_} {f}
       \in transport2 (<=) zro-right
            (inv $ FinSum_Equiv (later AbMonoid.FinSum-inj.equiv)
                *> FinSum_Or {_} {SigmaFin B \lam b => DecFin (decide {dec b})} {SigmaFin B \lam b => DecFin (decide {NotDecide (dec b)})}) $
            <=_+ (=_<= $ FinSum_Equiv2 (later $ AbMonoid.FinSum-inj.equiv2 inj) \lam a => idp) $ FinSum_>=0 $ later
              \lam s => p s.1 \lam a q => s.2 $ inP (a,q)

  \lemma FinSum_<=-inj2 {A B : FinSet} (f : A -> B) (inj : IsInj f) {x : B -> E} (p : \Pi (b : B) -> (\Pi (a : A) -> f a /= b) -> 0 <= x b)
                        {y : A -> E} (q : \Pi (a : A) -> x (f a) = y a) : FinSum y <= FinSum x
    => transport (<= _) (FinSum-ext q) (FinSum_<=-inj f inj p)
}

\class JoinSemilatticeAddMonoid \extends PosetAddMonoid, JoinSemilattice {
  | join_+-left {a b c : E} : a + (b ∨ c) = (a + b) ∨ (a + c)
  | join_+-right {a b c : E} : (a ∨ b) + c = (a + c) ∨ (b + c)

  -- | The join of the elements of an array and `0`.
  \func pjoin (l : Array E) : E => Big (∨) 0 l

  \lemma Big_join_+-right {b y : E} {l : Array E} : Big (∨) b l + y = Big (∨) (b + y) (map (+ y) l) \elim l
    | nil => idp
    | :: x l => join_+-right *> pmap (_ ∨) Big_join_+-right
}

\class MeetSemilatticeAddMonoid \extends PosetAddMonoid, MeetSemilattice {
  | meet_+-left {a b c : E} : a + b ∧ c = (a + b) ∧ (a + c)
  | meet_+-right {a b c : E} : a ∧ b + c = (a + c) ∧ (b + c)

  \lemma meet_+-subadditive {a b c : E} (a>=0 : 0 <= a) (b>=0 : 0 <= b) (c>=0 : 0 <= c) : a ∧ (b + c) <= a ∧ b + a ∧ c
    => meet-univ (meet-univ (meet-left <=∘ =_<= (inv zro-left) <=∘ <=_+ a>=0 <=-refl) (meet-left <=∘ =_<= (inv zro-left) <=∘ <=_+ b>=0 <=-refl) <=∘ =_<= (inv meet_+-right))
                 (meet-univ (meet-left <=∘ =_<= (inv zro-right) <=∘ <=_+ <=-refl c>=0) meet-right <=∘ =_<= (inv meet_+-right)) <=∘ =_<= (inv meet_+-left)

  \lemma meet_*n-zro {a b : E} (a>=0 : 0 <= a) (b>=0 : 0 <= b) (d : a ∧ b = 0) {n : Nat} : a ∧ (n *n b) = 0 \elim n
    | 0 => <=-antisymmetric meet-right (meet-univ a>=0 <=-refl)
    | suc n => <=-antisymmetric (meet_+-subadditive a>=0 (*n_>=0 b>=0) b>=0 <=∘ =_<= (pmap2 (+) (meet_*n-zro a>=0 b>=0 d) d *> zro-left)) (meet-univ a>=0 (*n_>=0 {_} {suc n} b>=0))
}

\class JoinSemilatticeAbMonoid \extends JoinSemilatticeAddMonoid, PosetAbMonoid
  | join_+-right => +-comm *> join_+-left *> pmap2 (∨) +-comm +-comm

\class MeetSemilatticeAbMonoid \extends MeetSemilatticeAddMonoid, PosetAbMonoid
  | meet_+-right => +-comm *> meet_+-left *> pmap2 (∧) +-comm +-comm

\class PosetAddGroup \extends PosetAddMonoid, AddGroup {
  \lemma from>=0 {x y : E} (p : 0 <= y - x) : x <= y
    => transport2 (<=) zro-left (+-assoc *> pmap (y +) negative-left *> zro-right) (<=_+ p <=-refl)

  \lemma to>=0 {x y : E} (p : x <= y) : 0 <= y - x
    => transport (<= _) negative-right (<=_+ p <=-refl)

  \lemma from<=0 {x y : E} (p : x - y <= 0) : x <= y
    => transport2 (<=) (+-assoc *> pmap (x +) negative-left *> zro-right) zro-left (<=_+ p <=-refl)

  \lemma to<=0 {x y : E} (p : x <= y) : x - y <= 0
    => transport (_ <=) negative-right (<=_+ p <=-refl)

  \lemma negative_<= {x y : E} (x<=y : x <= y) : negative y <= negative x
    => transport2 (<=) (inv +-assoc *> pmap (- y) negative-left *> zro-left) zro-right $ <=_+ <=-refl (to<=0 x<=y)

  \lemma negative_<=-conv {x y : E} (p : negative y <= negative x) : x <= y
    => transport2 (<=) negative-isInv negative-isInv (negative_<= p)

  \lemma <=_- {a b c d : E} (a<=b : a <= b) (d<=c : d <= c) : a - c <= b - d
    => <=_+ a<=b (negative_<= d<=c)

  \lemma negative>=0 {x : E} (x<=0 : x <= 0) : 0 <= negative x
    => transport (<= _) negative_zro (negative_<= x<=0)

  \lemma negative-to<=0 {x : E} (p : 0 <= negative x) : x <= 0
    => negative_<=-conv $ transportInv (<= _) negative_zro p

  \lemma negative<=0 {x : E} (x>=0 : 0 <= x) : negative x <= 0
    => transport (_ <=) negative_zro (negative_<= x>=0)

  \lemma negative-to>=0 {x : E} (p : negative x <= 0) : 0 <= x
    => negative_<=-conv $ transportInv (_ <=) negative_zro p

  \lemma <=_+-cancel-left (x : E) {y z : E} (p : x + y <= x + z) : y <= z
    => transport2 (<=) (inv +-assoc *> pmap (+ y) negative-left *> zro-left) (inv +-assoc *> pmap (+ z) negative-left *> zro-left) (<=_+ <=-refl p)

  \lemma <=_+-cancel-right (z : E) {x y : E} (p : x + z <= y + z) : x <= y
    => transport2 (<=) (+-assoc *> pmap (x +) negative-right *> zro-right) (+-assoc *> pmap (y +) negative-right *> zro-right) (<=_+ p <=-refl)
}

\class PosetAbGroup \extends PosetAddGroup, PosetAbMonoid, AbGroup

\class PosetDivAbGroup \extends PosetAbGroup, TorsionFreeGroup {
  | <=_*n-div {n : Nat} (n/=0 : n /= 0) {a : E} : 0 <= n *n a -> 0 <= a
  | noTorsion n/=0 na=0 => <=-antisymmetric (negative-to<=0 $ <=_*n-div n/=0 $ transportInv (0 <=) *n_negative $ negative>=0 $ =_<= na=0) (<=_*n-div n/=0 $ =_<= $ inv na=0)

  \lemma <=_*n-cancel-left {n : Nat} (n/=0 : n /= 0) {a b : E} (na<=nb : n *n a <= n *n b) : a <= b
    => from>=0 $ <=_*n-div n/=0 $ transportInv (0 <=) *n-ldistr_- (to>=0 na<=nb)

  \lemma double<=0 {x : E} (s : x + x <= 0) : x <= 0
    => negative-to<=0 $ <=_*n-div {_} {2} (\case __) $ transport (0 <=) (inv (pmap (+ _) zro-left *> inv negative_+)) (negative>=0 s)
}

-- | {LatticeAbGroup} is a {PosetAbGroup} which has meets and joins.
\class LatticeAbGroup \extends PosetAbGroup, MeetSemilatticeAbMonoid, JoinSemilatticeAbMonoid, DistributiveLattice, PosetDivAbGroup {
  | meet_+-left {a} {b} {c} => <=-antisymmetric (meet-univ (<=_+ <=-refl meet-left) (<=_+ <=-refl meet-right)) $ <=_+-cancel-left (negative a) $ meet-univ (<=_+ <=-refl meet-left <=∘ =_<= (inv +-assoc *> pmap (+ b) negative-left *> zro-left)) (<=_+ <=-refl meet-right <=∘ =_<= (inv +-assoc *> pmap (+ c) negative-left *> zro-left)) <=∘ =_<= (inv zro-left *> pmap (+ _) (inv negative-left) *> +-assoc)
  | join_+-left {a} {b} {c} => <=-antisymmetric (<=_+-cancel-left (negative a) $ (inv +-assoc *> pmap (+ _) negative-left *> zro-left) =<= join-univ (<=_+-cancel-left a $ join-left <=∘ =_<= (inv zro-left *> pmap (+ _) (inv negative-right) *> +-assoc)) (<=_+-cancel-left a $ join-right <=∘ =_<= (inv zro-left *> pmap (+ _) (inv negative-right) *> +-assoc))) $ join-univ (<=_+ <=-refl join-left) (<=_+ <=-refl join-right)
  | ldistr>= {x} {y} {z} => (inv $ meet_+-right *> pmap2 (∧) (+-assoc *> pmap (x +) (negative-left {_} {y ∨ z}) *> zro-right) zro-left) =<= <=_+-cancel-left _ ((inv +-assoc *> pmap (+ _) negative-left *> zro-left) =<= join-univ (<=_+-cancel-left _ $ meet_+-right =<= meet-monotone (+-assoc =<= <=_+ <=-refl (<=_+ <=-refl join-left <=∘ =_<= negative-left) <=∘ =_<= zro-right) (=_<= zro-left) <=∘ join-left <=∘ =_<= (inv (pmap (+ _) negative-right *> zro-left) *> +-assoc)) (<=_+-cancel-left _ $ meet_+-right =<= meet-monotone (+-assoc =<= <=_+ <=-refl (<=_+ <=-refl join-right <=∘ =_<= negative-left) <=∘ =_<= zro-right) (=_<= zro-left) <=∘ join-right <=∘ =_<= (inv (pmap (+ _) negative-right *> zro-left) *> +-assoc)))
  | <=_*n-div {n} n/=0 {a} na>=0 =>
    \have | q>=0 : 0 <= negative (a ∧ 0) => negative>=0 meet-right
          | p>=0 : 0 <= a - a ∧ 0 => to>=0 meet-left
          | pq : (a - a ∧ 0) ∧ negative (a ∧ 0) = 0 => pmap (_ ∧) (inv zro-left) *> inv meet_+-right *> negative-right
          | nq<=np : n *n negative (a ∧ 0) <= n *n (a - a ∧ 0) => transport (<= _) zro-left (<=_+ na>=0 <=-refl) <=∘ =_<= (inv *n-ldistr)
          | nq=0 : n *n negative (a ∧ 0) = 0 => <=-antisymmetric (meet-univ nq<=np <=-refl <=∘ =_<= (meet_*n-zro (*n_>=0 p>=0) q>=0 $ meet-comm *> meet_*n-zro q>=0 p>=0 (meet-comm *> pq))) (*n_>=0 q>=0)
    \in negative-to>=0 (*n>=id n/=0 q>=0 <=∘ =_<= nq=0) <=∘ meet-left

  \lemma meet_negative {x y : E} : negative (x ∧ y) = negative x ∨ negative y
    => <=-antisymmetric (negative_<= (meet-univ (negative_<= join-left <=∘ =_<= negative-isInv) (negative_<= join-right <=∘ =_<= negative-isInv)) <=∘ =_<= negative-isInv) $ join-univ (negative_<= meet-left) (negative_<= meet-right)

  \lemma join_negative {x y : E} : negative (x ∨ y) = negative x ∧ negative y
    => <=-antisymmetric (meet-univ (negative_<= join-left) (negative_<= join-right)) $ inv negative-isInv =<= negative_<= (join-univ (inv negative-isInv =<= negative_<= meet-left) (inv negative-isInv =<= negative_<= meet-right))

  \lemma modularity {x y : E} : x + y = (x ∨ y) + (x ∧ y)
    => cancel-right (negative (x ∧ y)) $ pmap (_ +) meet_negative *> join_+-left *> pmap2 (∨) (+-comm *> inv +-assoc *> pmap (+ y) negative-left *> zro-left) (+-assoc *> pmap (x +) negative-right *> zro-right) *> join-comm *> inv (+-assoc *> pmap (_ +) negative-right *> zro-right)

  \func abs (x : E) => x ∨ negative x

  \lemma abs>=id {x : E} : x <= abs x
    => join-left

  \lemma abs>=neg {x : E} : negative x <= abs x
    => join-right

  \lemma abs_negative {x : E} : abs (negative x) = abs x
    => <=-antisymmetric (join-univ join-right $ rewrite negative-isInv join-left) (join-univ (inv negative-isInv =<= join-right) join-left)

  \lemma abs-ofPos {x : E} (x>=0 : 0 <= x) : abs x = x
    => <=-antisymmetric (join-univ <=-refl $ transport2 (<=) zro-left negative-right (<=_+ x>=0 <=-refl) <=∘ x>=0) join-left

  \lemma abs-ofNeg {x : E} (x<=0 : x <= 0) : abs x = negative x
    => inv abs_negative *> abs-ofPos (rewrite negative_zro in negative_<= x<=0)

  \lemma abs_abs {x : E} : abs (abs x) = abs x
    => <=-antisymmetric (join-univ <=-refl $ negative_<= abs>=id <=∘ abs>=neg) abs>=id

  \lemma abs_+ {x y : E} : abs (x + y) <= abs x + abs y
    => join-univ (<=_+ join-left join-left) $ rewrite (negative_+,+-comm) $ <=_+ join-right join-right

  \lemma abs_- {x y : E} : abs (x - y) = abs (y - x)
    => pmap abs (inv negative-isInv) *> abs_negative *> simplify

  \lemma abs_-left {x y : E} : abs x - abs y <= abs (x - y)
    => transport (_ <=) (+-assoc *> pmap (_ +) negative-right *> zro-right) $ <=_+ (transport (abs __ <= _) (+-assoc *> pmap (x +) negative-left *> zro-right) abs_+) <=-refl

  \lemma abs_-right {x y : E} : abs y - abs x <= abs (x - y)
    => transport (_ <=) abs_- abs_-left

  \lemma abs_zro : abs zro = zro
    => <=-antisymmetric (join-univ <=-refl $ rewrite negative_zro <=-refl) abs>=id

  \lemma abs_zro-ext {x : E} (p : abs x = zro) : x = zro
    => <=-antisymmetric (transport (_ <=) p join-left) $ transport2 (<=) negative_zro negative-isInv $ negative_<= $ transport (_ <=) p join-right

  \lemma abs>=_- {x y : E} : y - x <= abs (x - y)
    => transportInv (_ <=) negative_+ (later $ rewrite negative-isInv <=-refl) <=∘ abs>=neg

  \lemma abs-univ {x y z : E} (p : x - y <= z) (q : y - x <= z) : abs (x - y) <= z
    => join-univ p (negative_- =<= q)

  \lemma abs>=0 {x : E} : 0 <= abs x
    => \have | aa>=0 : 0 <= abs x + abs x => transport (<= _) negative-right $ <=_+ abs>=id abs>=neg
             | ma>=m : abs x ∧ 0 <= (abs x ∧ 0) + abs x => meet-univ (meet-right <=∘ aa>=0) (meet-left <=∘ =_<= (inv zro-left)) <=∘ =_<= (inv meet_+-right)
       \in <=_+-cancel-left _ $ transportInv (<= _) zro-right ma>=m

  \lemma join-lip {a b c d : E} : abs ((a ∨ b) - (c ∨ d)) <= abs (a - c) + abs (b - d)
    => \have | step {x y u s : E} (y<=u : y <= u) (p : abs (x - y) <= s) : x <= u + s
               => =_<= equation.abGroup <=∘ <=_+ y<=u (abs>=id <=∘ p)
             | half {a b c d : E} : (a ∨ b) - (c ∨ d) <= abs (a - c) + abs (b - d)
               => \have j : a ∨ b <= (c ∨ d) + (abs (a - c) + abs (b - d))
                        => join-univ (step join-left $ =_<= (inv zro-right) <=∘ <=_+ <=-refl abs>=0)
                                     (step join-right $ =_<= (inv zro-left) <=∘ <=_+ abs>=0 <=-refl)
                  \in <=_+ j <=-refl <=∘ =_<= (pmap (__ + negative (c ∨ d)) +-comm *> +-assoc *> pmap (_ +) negative-right *> zro-right)
       \in abs-univ half $ transport (_ <=) (pmap2 (+) abs_- abs_-) half

  \lemma join-bound {a b : E} : abs (a ∨ b) <= abs a + abs b
    => \have | e1 : abs ((a ∨ b) - (0 ∨ 0)) = abs (a ∨ b) => pmap abs (pmap (_ -) join-idemp *> minus_zro)
             | e2 : abs (a - 0) + abs (b - 0) = abs a + abs b => pmap2 (+) (pmap abs minus_zro) (pmap abs minus_zro)
       \in transport2 (<=) e1 e2 join-lip

  \lemma meet-lip {a b c d : E} : abs ((a ∧ b) - (c ∧ d)) <= abs (a - c) + abs (b - d)
    => \have neg-minus {x y : E} : negative (x - y) = negative x - negative y => equation.abGroup
       \in =_<= (inv abs_negative *> pmap abs (neg-minus *> pmap2 (-) meet_negative meet_negative)) <=∘ join-lip
            <=∘ =_<= (pmap2 (+) (pmap abs (inv neg-minus) *> abs_negative) (pmap abs (inv neg-minus) *> abs_negative))

  -- | The clipping of {b} to `[-s,s]` is bounded by {s}.
  \lemma clip-abs {b s : E} (s>=0 : 0 <= s) : abs ((b ∧ s) ∨ negative s) <= s
    => join-univ (join-univ meet-right (negative<=0 s>=0 <=∘ s>=0)) (=_<= join_negative <=∘ meet-right <=∘ =_<= negative-isInv)

  -- | Clipping {b} to `[-s,s]` does not increase its distance to an element {h} with `|h| ≤ s`.
  \lemma clip-dist {h b s : E} (hs : abs h <= s) : abs (h - ((b ∧ s) ∨ negative s)) <= abs (h - b)
    => \have | h<=s : h <= s => abs>=id <=∘ hs
             | -s<=h : negative s <= h => negative_<= (abs>=neg <=∘ hs) <=∘ =_<= negative-isInv
             | e : h = (h ∧ s) ∨ negative s => inv (pmap (__ ∨ _) (meet_<= h<=s) *> join-comm *> join_<= -s<=h)
       \in =_<= (pmap (\lam z => abs (z - _)) e) <=∘ join-lip <=∘ <=_+ meet-lip <=-refl
            <=∘ =_<= (pmap2 (+) (pmap (_ +) (pmap abs negative-right *> abs_zro) *> zro-right) (pmap abs negative-right *> abs_zro) *> zro-right)

  -- | The clipping $(b \wedge s) \vee (-s)$ of {b} to `[-s,s]`.
  \func clip (s b : E) : E => (b ∧ s) ∨ negative s

  -- | Clipping is `1`-Lipschitz.
  \lemma clip-lip {s a b : E} : abs (clip s a - clip s b) <= abs (a - b)
    => join-lip <=∘ <=_+ meet-lip <=-refl
         <=∘ =_<= (pmap2 (+) (pmap (_ +) (pmap abs negative-right *> abs_zro) *> zro-right) (pmap abs negative-right *> abs_zro) *> zro-right)

  -- | Clipping does not change an element {a} with `|a| ≤ s`.
  \lemma clip-id {s a : E} (as : abs a <= s) : clip s a = a
    => pmap (__ ∨ _) (meet_<= (abs>=id <=∘ as)) *> join-comm *> join_<= (negative_<= (abs>=neg <=∘ as) <=∘ =_<= negative-isInv)

  -- | Clipping at $|b| \wedge s$ is clipping at `s`, for $s \ge 0$.
  \lemma clip-abs-meet {s b : E} (s>=0 : 0 <= s) : clip (abs b ∧ s) b = clip s b
    => \have | e1 : b ∧ (abs b ∧ s) = b ∧ s => inv meet-assoc *> pmap (__ ∧ s) (meet_<= abs>=id)
             | m : negative (abs b) <= b ∧ s => meet-univ (negative_<= abs>=neg <=∘ =_<= negative-isInv) (negative<=0 abs>=0 <=∘ s>=0)
       \in pmap2 (∨) e1 meet_negative *> inv join-assoc *> pmap (__ ∨ negative s) (join-comm *> join_<= m)

  -- | $|\mathrm{clip}_s(b)| = |b| \wedge s$ for $s \ge 0$.
  \lemma clip-abs-eq {s b : E} (s>=0 : 0 <= s) : abs (clip s b) = abs b ∧ s
    => <=-antisymmetric
        (meet-univ (=_<= (inv (pmap abs (pmap (_ -) (clip-id (=_<= abs_zro <=∘ s>=0)) *> minus_zro))) <=∘ clip-lip <=∘ =_<= (pmap abs minus_zro)) (clip-abs s>=0))
        (rdistr>= <=∘ join-univ (join-left <=∘ abs>=id)
          (meet-univ (meet-left <=∘ join-left) meet-right <=∘ =_<= (inv (join_negative *> pmap2 (∧) meet_negative negative-isInv)) <=∘ abs>=neg))

  \func part+ (x : E) => x ∨ 0

  \func part- (x : E) => negative x ∨ 0

  \lemma part+>=0 {a : E} : 0 <= part+ a
    => join-right

  \lemma part+<=abs {x : E} : part+ x <= abs x
    => join-univ abs>=id abs>=0

  \lemma part+-mono {a b : E} (p : a <= b) : part+ a <= part+ b
    => join-monotone p <=-refl

  \lemma part+-idemp {a : E} : part+ (part+ a) = part+ a
    => join-assoc *> pmap (a ∨) join-idemp

  \lemma part+_join {a b : E} : part+ (a ∨ b) = part+ a ∨ part+ b
    => <=-antisymmetric (join-univ (join-univ (join-left <=∘ join-left) (join-left <=∘ join-right)) (join-right <=∘ join-right))
      (join-univ (join-monotone join-left <=-refl) (join-monotone join-right <=-refl))

  \lemma part+_meet {a b : E} : part+ (a ∧ b) = part+ a ∧ part+ b
    => join-comm *> lcodistr *> pmap2 (∧) join-comm join-comm

  \lemma part+_+ {a b : E} : part+ (a + b) <= part+ a + part+ b
    => join-univ (<=_+ join-left join-left) (=_<= (inv zro-left) <=∘ <=_+ part+>=0 part+>=0)

  \lemma part+_*n {n : Nat} {a : E} : part+ (n *n a) = n *n part+ a
    => inv $ join_*n *> pmap (_ ∨) *n_zro

  \lemma part+-shift {b c : E} (c>=0 : 0 <= c) : part+ (part+ b - c) = part+ (b - c)
    => pmap (∨ 0) join_+-right *> join-assoc *> pmap (_ ∨) (<=-antisymmetric (join-univ (=_<= zro-left <=∘ negative<=0 c>=0) <=-refl) join-right)

  \lemma meet_negative<=0 {a : E} : a ∧ negative a <= 0
    => double<=0 $ <=_+ meet-left meet-right <=∘ =_<= negative-right

  \lemma minus-join {x y z : E} : x - (y ∨ z) = (x - y) ∧ (x - z)
    => pmap (x +) join_negative *> meet_+-left

  \lemma part+-ofPos {x : E} (x>=0 : 0 <= x) : part+ x = x
    => <=-antisymmetric (join-univ <=-refl x>=0) join-left

  \lemma parts_join {x : E} : part+ x ∨ part- x = abs x
    => <=-antisymmetric (join-univ (join-univ abs>=id abs>=0) (join-univ abs>=neg abs>=0)) $ join-univ (join-left <=∘ join-left) (join-left <=∘ join-right)

  \lemma parts_meet {x : E} : part+ x ∧ part- x = 0
    => cancel-left (part+ x ∨ part- x) $ inv modularity *> parts_+ *> inv (zro-right *> parts_join)

  \lemma parts_+ {x : E} : part+ x + part- x = abs x
    => join_+-right *> pmap2 (∨) (join_+-left *> join-comm *> pmap2 (∨) zro-right negative-right) zro-left *> parts_join

  \lemma parts_- {x : E} : part+ x - part- x = x
    => cancel-right (part- x) $ +-assoc *> pmap (_ +) negative-left *> zro-right *> join-comm *> inv (join_+-left *> pmap2 (∨) negative-right zro-right)

  \lemma part-_diff_part+ {x y : E} : part- (x - y) = part+ (y - x)
    => pmap (∨ 0) negative_-

  \lemma part+_diff_meet {x y : E} : part+ (x - y) = x - x ∧ y
    => inv $ pmap (x +) meet_negative *> join_+-left *> join-comm *> pmap (_ ∨) negative-right

  \lemma part+_diff_join {x y : E} : part+ (x - y) = x ∨ y - y
    => inv $ join_+-right *> pmap (_ ∨) negative-right

  \lemma part-_diff_meet {x y : E} : part- (x - y) = y - x ∧ y
    => part-_diff_part+ *> part+_diff_meet *> pmap (y -) meet-comm

  \lemma part-_diff_join {x y : E} : part- (x - y) = x ∨ y - x
    => part-_diff_part+ *> part+_diff_join *> pmap (- x) join-comm

  \lemma join_+-double {x y : E} : x + y <= (x + x) ∨ (y + y)
    => transport2 (x + __ <= __) zro-left (pmap (x +) join_+-right *> join_+-left *> pmap2 (x + __ ∨ __) (+-assoc *> pmap (x +) negative-left *> zro-right) (inv +-assoc *> pmap (+ y) (pmap (x +) negative_- *> +-comm *> +-assoc *> pmap (y +) negative-left *> zro-right))) $ <=_+ <=-refl $ <=_+ abs>=0 <=-refl

  \lemma join_+-comm {x y : E} : (x ∨ y) + (x ∨ y) = (x + x) ∨ (y + y)
    => join_+-left *> pmap2 (∨) join_+-right join_+-right *> <=-antisymmetric (join-univ (join-univ join-left $ +-comm =<= join_+-double) (join-univ join_+-double join-right)) (join-univ (join-left <=∘ join-left) (join-right <=∘ join-right))

  \lemma join_*n {n : Nat} {x y : E} : n *n (x ∨ y) = (n *n x) ∨ (n *n y)
    => <=-antisymmetric (<=_+-cancel-right ((NatSemiring.pow 2 n -' n) *n (x ∨ y)) $ (inv *n-rdistr *> pmap (*n _) (<=_exists $ NatSemiring.<=-less id<pow2) *> steps *> inv (pmap2 (∨) (inv *n-rdistr *> pmap (*n _) (<=_exists $ NatSemiring.<=-less id<pow2)) (inv *n-rdistr *> pmap (*n _) (<=_exists $ NatSemiring.<=-less id<pow2)))) =<= join-univ (<=_+ join-left $ *n_<= join-left) (<=_+ join-right $ *n_<= join-right)) $ join-univ (*n_<= join-left) (*n_<= join-right)
    \where {
      \private \lemma steps {k : Nat} {x y : E} : NatSemiring.pow 2 k *n (x ∨ y) = (NatSemiring.pow 2 k *n x) ∨ (NatSemiring.pow 2 k *n y) \elim k
        | 0 => zro-left *> inv (pmap2 (∨) zro-left zro-left)
        | suc k => *n-rdistr *> pmap2 (+) steps steps *> join_+-comm *> inv (pmap2 (∨) (pmap (*n x) (NatSemiring.*-comm {_} {2}) *> *n-assoc *> +-assoc *> zro-left) (pmap (*n y) (NatSemiring.*-comm {_} {2}) *> *n-assoc *> +-assoc *> zro-left))
    }

  \lemma abs_*n {n : Nat} {x : E} : n *n abs x = abs (n *n x)
    => join_*n *> pmap (_ ∨) *n_negative

  \lemma meet_*n {n : Nat} {x y : E} : n *n (x ∧ y) = (n *n x) ∧ (n *n y)
    => inv negative-isInv *> pmap negative (inv *n_negative *> pmap (n *n) meet_negative *> join_*n *> pmap2 (∨) *n_negative *n_negative *> inv meet_negative) *> negative-isInv

  \type AreDisjoint (x y : E) : \Prop
    => abs x ∧ abs y = 0

  \lemma pos-disjoint {x y : E} (x>=0 : 0 <= x) (y>=0 : 0 <= y) (d : x ∧ y = 0) : AreDisjoint x y
    => pmap2 (∧) (abs-ofPos x>=0) (abs-ofPos y>=0) *> d

  \lemma disjoint_join>=0 {x y : E} (d : AreDisjoint x y) : 0 <= x ∨ y
    => negative-to>=0 $ =_<= join_negative <=∘ meet-monotone abs>=neg abs>=neg <=∘ =_<= d

  \lemma disjoint_+_join {x y : E} (d : AreDisjoint x y) : x + y <= x ∨ y
    => =_<= modularity <=∘ <=_+ <=-refl (meet-monotone abs>=id abs>=id <=∘ =_<= d) <=∘ =_<= zro-right

  \lemma disjoint_pos_+_join {x y : E} (x>=0 : 0 <= x) (y>=0 : 0 <= y) (d : x ∧ y = 0) : x + y = x ∨ y
    => <=-antisymmetric (disjoint_+_join (pos-disjoint x>=0 y>=0 d)) $
        join-univ (=_<= (inv zro-right) <=∘ <=_+ <=-refl y>=0) (=_<= (inv zro-left) <=∘ <=_+ x>=0 <=-refl)

  \lemma disjoint_abs_+-left {x y : E} (d : AreDisjoint x y) : abs x <= abs (x + y)
    => \have | rev : abs x - abs y <= abs (x + y) => transport2 (\lam a b => abs x - a <= abs b) abs_negative (pmap (x +) negative-isInv) abs_-left
             | eq : abs x = (abs x - abs y) ∨ 0 => inv zro-right *> pmap (abs x +) (inv negative_zro *> pmap negative (inv d *> meet-comm) *> meet_negative) *> join_+-left *> pmap (_ ∨) negative-right
       \in =_<= eq <=∘ join-univ rev abs>=0

  \lemma disjoint_abs_+-right {x y : E} (d : AreDisjoint x y) : abs y <= abs (x + y)
    => transport (_ <=) (pmap abs +-comm) $ disjoint_abs_+-left (meet-comm *> d)

  \lemma disjoint_abs_+ {x y : E} (d : AreDisjoint x y) : abs (x + y) = abs x + abs y
    => <=-antisymmetric abs_+ $ =_<= (modularity *> pmap (_ +) d *> zro-right) <=∘ join-univ (disjoint_abs_+-left d) (disjoint_abs_+-right d)

  \lemma disjoint_+-right {x y z : E} (d : AreDisjoint x y) (d' : AreDisjoint x z) : AreDisjoint x (y + z)
    => <=-antisymmetric (meet-monotone <=-refl abs_+ <=∘ meet_+-subadditive abs>=0 abs>=0 abs>=0 <=∘ =_<= (pmap2 (+) d d' *> zro-left)) (meet-univ abs>=0 abs>=0)

  \lemma disjoint_BigSum-right {x : E} {l : Array E} (d : \Pi (j : Fin l.len) -> AreDisjoint x (l j)) : AreDisjoint x (BigSum l) \elim l
    | nil => pmap (_ ∧) abs_zro *> <=-antisymmetric meet-right (meet-univ abs>=0 <=-refl)
    | a :: l => disjoint_+-right (d 0) (disjoint_BigSum-right \lam j => d (suc j))

  \lemma disjoint_abs_BigSum {l : Array E} (d : \Pi {i j : Fin l.len} -> i /= j -> AreDisjoint (l i) (l j)) : abs (BigSum l) = BigSum (map abs l) \elim l
    | nil => abs_zro
    | a :: l => disjoint_abs_+ (disjoint_BigSum-right \lam j => d {0} {suc j} (/=-sym fsuc/=0)) *> pmap (_ +) (disjoint_abs_BigSum \lam {i} {j} p => d {suc i} {suc j} (fsuc/= p))

  \lemma disjoint-ldistr {a b c : E} (a>=0 : 0 <= a) (b>=0 : 0 <= b) (c>=0 : 0 <= c) (d : b ∧ c = 0) : a ∧ (b + c) = a ∧ b + a ∧ c
    => meet-comm *> disjoint-rdistr b>=0 c>=0 a>=0 d *> pmap2 (+) meet-comm meet-comm

  \lemma disjoint-rdistr {a b c : E} (a>=0 : 0 <= a) (b>=0 : 0 <= b) (c>=0 : 0 <= c) (d : a ∧ b = 0) : (a + b) ∧ c = a ∧ c + b ∧ c
    => \have dc : (a ∧ c) ∧ (b ∧ c) = 0 => <=-antisymmetric (meet-monotone meet-left meet-left <=∘ =_<= d) (meet-univ (meet-univ a>=0 c>=0) (meet-univ b>=0 c>=0))
       \in pmap (__ ∧ c) (modularity *> pmap (_ +) d *> zro-right) *> meet-rdistr *> inv (modularity *> pmap (_ +) dc *> zro-right)

  \lemma part+-split {a b : E} : a = part+ (a - b) + a ∧ b
    => inv $ pmap (+ _) part+_diff_meet *> +-assoc *> pmap (a +) negative-left *> zro-right

  \lemma join_part+ {a b : E} : a ∨ b = part+ (a - b) + b
    => pmap2 (∨) (inv $ +-assoc *> pmap (a +) negative-left *> zro-right) (inv zro-left) *> inv join_+-right

  \lemma meet_part+ {a b : E} : a ∧ b = a - part+ (a - b)
    => inv $ pmap (a -) part+_diff_meet *> equation.abGroup

  -- | The spectral preorder: `a <=s b` if the positive part of `a` lies in the ideal generated by the positive part of `b`.
  \type \infix 4 <=s (a b : E) : \Prop
    => ∃ (n : Nat) (part+ a <= n *n part+ b)

  \lemma <=s-refl {a : E} : a <=s a
    => inP (1, =_<= (inv zro-left))

  \lemma <=s-trans {a b c : E} (p : a <=s b) (q : b <=s c) : a <=s c \elim p, q
    | inP (n,p), inP (m,q) => inP (n Nat.* m, p <=∘ *n_<= q <=∘ =_<= (inv *n-assoc))

  \lemma <=_<=s {a b : E} (p : a <= b) : a <=s b
    => inP (1, part+-mono p <=∘ =_<= (inv zro-left))

  \lemma <=s_<=0 {a b : E} (p : a <=s b) (b<=0 : b <= 0) : a <= 0 \elim p
    | inP (n,p) => join-left <=∘ p <=∘ =_<= (pmap (n *n) (<=-antisymmetric (join-univ b<=0 <=-refl) join-right) *> *n_zro)

  \lemma <=s-join {a b c : E} (p : a <=s c) (q : b <=s c) : a ∨ b <=s c \elim p, q
    | inP (n,p), inP (m,q) => inP (n Nat.+ m, =_<= part+_join <=∘ join-univ (p <=∘ *n_<=-nat (NatSemiring.<=_+ NatSemiring.<=-refl zero<=_) part+>=0) (q <=∘ *n_<=-nat (NatSemiring.<=_+ zero<=_ NatSemiring.<=-refl) part+>=0))

  \lemma <=s-meet {a b c : E} (p : a <=s b) (q : a <=s c) : a <=s b ∧ c \elim p, q
    | inP (n,p), inP (m,q) => inP (n Nat.+ m, meet-univ (p <=∘ *n_<=-nat (NatSemiring.<=_+ NatSemiring.<=-refl zero<=_) part+>=0) (q <=∘ *n_<=-nat (NatSemiring.<=_+ zero<=_ NatSemiring.<=-refl) part+>=0) <=∘ =_<= (inv meet_*n *> inv (pmap (_ *n) part+_meet)))

  \lemma <=s-join-left {a b : E} : a <=s a ∨ b
    => <=_<=s join-left

  \lemma <=s-join-right {a b : E} : b <=s a ∨ b
    => <=_<=s join-right

  \lemma <=s-meet-left {a b : E} : a ∧ b <=s a
    => <=_<=s meet-left

  \lemma <=s-meet-right {a b : E} : a ∧ b <=s b
    => <=_<=s meet-right

  \lemma <=s-part+ {a : E} : a <=s part+ a
    => <=_<=s join-left

  \lemma part+-<=s {a : E} : part+ a <=s a
    => inP (1, =_<= (part+-idemp *> inv zro-left))

  \lemma *n_<=s {n : Nat} {a : E} : n *n a <=s a
    => inP (n, =_<= part+_*n)

  \lemma <=s_*n {n : Nat} (n/=0 : n /= 0) {a : E} : a <=s n *n a
    => inP (1, *n>=id n/=0 part+>=0 <=∘ =_<= (inv part+_*n *> inv zro-left))

  \lemma <=s_+ {a b : E} : a + b <=s a ∨ b
    => inP (2, part+_+ <=∘ <=_+ (part+-mono join-left) (part+-mono join-right) <=∘ =_<= (pmap (+ _) (inv zro-left)))

  \lemma meet_<=s_+ {x y : E} : x ∧ y <=s x + y
    => <=s-trans (<=s_*n {_} {2} (\case __)) $ <=_<=s $ =_<= (pmap (+ _) zro-left) <=∘ <=_+ meet-left meet-right

  \lemma pjoin-univ {l : Array E} {c : E} (p : \Pi (j : Fin l.len) -> l j <=s c) : pjoin l <=s c \elim l
    | nil => inP (0, =_<= join-idemp)
    | x :: l => <=s-join (p 0) (pjoin-univ \lam j => p (suc j))

  \lemma pjoin_*n {n : Nat} {l : Array E} : n *n pjoin l = pjoin (map (n *n) l) \elim l
    | nil => *n_zro
    | x :: l => join_*n *> pmap (_ ∨) pjoin_*n

  \lemma pjoin-const {x : E} {l : Array E} (h : \Pi (j : Fin l.len) -> l j = x) : pjoin l <= part+ x \elim l
    | nil => join-right
    | y :: l => join-univ (=_<= (h 0) <=∘ join-left) (pjoin-const \lam j => h (suc j))

  \type IsSolid (U : Set E) => ∀ {y} {x : U} (abs y <= abs x -> U y)
} \where {
    -- TODO: Make constructor classes
    \protected \class FromMeet \extends LatticeAbGroup
      | join a b => negative (negative a ∧ negative b)
      | join-left => inv negative-isInv =<= negative_<= meet-left
      | join-right => inv negative-isInv =<= negative_<= meet-right
      | join-univ x<=z y<=z => negative_<= (meet-univ (negative_<= x<=z) (negative_<= y<=z)) <=∘ =_<= negative-isInv

    \protected \class FromJoin \extends LatticeAbGroup
      | meet a b => negative (negative a ∨ negative b)
      | meet-left => negative_<= join-left <=∘ =_<= negative-isInv
      | meet-right => negative_<= join-right <=∘ =_<= negative-isInv
      | meet-univ z<=x z<=y => inv negative-isInv =<= negative_<= (join-univ (negative_<= z<=x) (negative_<= z<=y))
  }

\class PosetSemiring \extends Semiring, PosetAbMonoid {
  | zro<=ide : 0 <= 1
  | <=_*_positive-left {x y z : E} (x<=y : x <= y) (z>=0 : 0 <= z) : x * z <= y * z
  | <=_*_positive-right {x y z : E} (x>=0 : 0 <= x) (y<=z : y <= z) : x * y <= x * z

  \lemma <=_*_positive_positive {x y : E} (x>=0 : 0 <= x) (y>=0 : 0 <= y) : 0 <= x * y
    => transport (<= _) zro_*-right (<=_*_positive-right x>=0 y>=0)

  \lemma <=_*_positive_negative {x y : E} (x>=0 : 0 <= x) (y<=0 : y <= 0) : x * y <= 0
    => transport (_ <=) zro_*-right (<=_*_positive-right x>=0 y<=0)

  \lemma <=_*_negative_positive {x y : E} (x<=0 : x <= 0) (y>=0 : 0 <= y) : x * y <= 0
    => transport (_ <=) zro_*-left (<=_*_positive-left x<=0 y>=0)

  \lemma natCoef>=0 {n : Nat} : zro <= natCoef n \elim n
    | 0 => rewrite natCoefZero <=-refl
    | suc n => transport2 (<=) zro-left (inv (natCoefSuc n)) $ <=_+ natCoef>=0 zro<=ide

  \lemma natCoef_<= {n m : Nat} (p : n NatSemiring.<= m) : natCoef n <= natCoef m
    => rewrite (inv (<=_exists p), natCoef_+) $ transport (<= _) zro-right $ <=_+ <=-refl natCoef>=0

  \lemma <=_*-monotone {a b c d : E} (a<=b : a <= b) (c<=d : c <= d) (c>=0 : 0 <= c) (b>=0 : 0 <= b) : a * c <= b * d
    => <=_*_positive-left a<=b c>=0 <=∘ <=_*_positive-right b>=0 c<=d

  \lemma pow>=0 {a : E} {k : Nat} (a>=0 : 0 <= a) : 0 <= pow a k \elim k
    | 0 => zro<=ide
    | suc k => <=_*_positive_positive (pow>=0 a>=0) a>=0

  \lemma pow_<=-monotone {a b : E} {k : Nat} (a>=0 : 0 <= a) (a<=b : a <= b) : pow a k <= pow b k \elim k
    | 0 => <=-refl
    | suc k => <=_*_positive-right (pow>=0 a>=0) a<=b <=∘ <=_*_positive-left (pow_<=-monotone a>=0 a<=b) (a>=0 <=∘ a<=b)

  \lemma pow<=id {a : E} (a>=0 : 0 <= a) (a<=1 : a <= 1) {k : Nat} (k/=0 : k /= 0) : pow a k <= a \elim k
    | 0 => \case k/=0 idp
    | 1 => transportInv (<= _) ide-left <=-refl
    | suc (suc k) => transport (_ <=) ide-right $ <=_*_positive-right (pow>=0 {_} {a} {suc k} a>=0) a<=1 <=∘ <=_*_positive-left (pow<=id a>=0 a<=1 suc/=0) (a>=0 <=∘ a<=1)

  \lemma pow>=id {a : E} (a>=1 : 1 <= a) {k : Nat} (k/=0 : k /= 0) : a <= pow a k \elim k
    | 0 => \case k/=0 idp
    | 1 => transportInv (a <=) ide-left <=-refl
    | suc (suc k) =>
      \have a>=0 => zro<=ide <=∘ a>=1
      \in transport (<= _) ide-right $ <=_*_positive-right a>=0 a>=1 <=∘ <=_*_positive-left (pow>=id a>=1 suc/=0) a>=0

  \lemma pow<=1 {a : E} (a>=0 : 0 <= a) (a<=1 : a <= 1) {k : Nat} : pow a k <= 1 \elim k
    | 0 => <=-refl
    | suc k => pow<=id a>=0 a<=1 {suc k} (\case __) <=∘ a<=1

  \lemma pow>=1 {a : E} (a>=1 : a >= 1) {k : Nat} : pow a k >= 1 \elim k
    | 0 => <=-refl
    | suc k => <=-transitive a>=1 (rewriteI {1} ide-left (<=_*_positive-left (pow>=1 a>=1) (zro<=ide <=∘ a>=1)))

  \lemma pow_<=-degree {a : E} (a>=0 : 0 <= a) (a<=1 : a <= 1) {n k : Nat} (n<=k : n NatSemiring.<= k) : pow a k <= pow a n
    => rewrite (inv (<=_exists n<=k), pow_+) $ transport (_ <=) ide-right $ <=_*_positive-right (pow>=0 a>=0) $ pow<=1 a>=0 a<=1

  \lemma pow_<=-degree_>=1 {a : E} (p : a >= 1) {n m : Nat} (m<=n : m NatSemiring.<= n) : pow a m <= pow a n
    => rewriteI (<=_exists m<=n, inv Monoid.pow_+) $ rewriteI {1} ide-right $
        <=_*_positive-right (PosetSemiring.pow>=0 (zro<=ide <=∘ p)) (PosetSemiring.pow>=1 p)

  \lemma powSum>=0 {a : E} (a>=0 : 0 <= a) (n : Nat) : 0 <= powSum a n
    => BigSum_>=0 (\lam k => pow>=0 a>=0)

  -- | {powSum} is monotone in the base.
  \lemma powSum-mono {a b : E} (a>=0 : 0 <= a) (a<=b : a <= b) (n : Nat) : powSum a n <= powSum b n
    => BigSum_<= (\lam k => pow_<=-monotone a>=0 a<=b)

  -- | For `a >= 1`, `powSum a (suc m) <= (suc m) * a^m`.
  \lemma powSum-bound {a : E} (a>=1 : 1 <= a) (m : Nat) : powSum a (suc m) <= natCoef (suc m) * pow a m
    => BigSum_<= {_} {suc m} {\new Array E (suc m) (\lam k => pow a k)} {replicate (suc m) (pow a m)}
        (\lam k => pow_<=-degree_>=1 a>=1 (<_suc_<= (fin_< k)))
       <=∘ =_<= (BigSum_replicate {_} {pow a m} {suc m})
}

\class PosetRing \extends PosetSemiring, Ring, PosetAbGroup
  | <=_*-positive {x y : E} (x>=0 : 0 <= x) (y>=0 : 0 <= y) : 0 <= x * y
  | <=_*_positive-left x<=y z>=0 => from>=0 $ transport (0 <=) rdistr_- $ <=_*-positive (to>=0 x<=y) z>=0
  | <=_*_positive-right x>=0 y<=z => from>=0 $ transport (0 <=) ldistr_- $ <=_*-positive x>=0 (to>=0 y<=z)