open import Graded.Modality
module Graded.Modality.Properties.Addition
  {a} {M : Set a} (𝕄 : Semiring-with-meet M) where
open Semiring-with-meet 𝕄
open import Graded.Modality.Properties.Meet 𝕄
open import Graded.Modality.Properties.Multiplication 𝕄
open import Graded.Modality.Properties.PartialOrder 𝕄
open import Tools.Algebra M
open import Tools.Function
open import Tools.Product
open import Tools.PropositionalEquality
import Tools.Reasoning.PartialOrder
import Tools.Reasoning.PropositionalEquality
private
  variable
    p p′ q q′ r r′ : M
+-monotoneˡ : p ≤ q → p + r ≤ q + r
+-monotoneˡ p≤q = trans (+-congʳ p≤q) (+-distribʳ-∧ _ _ _)
+-monotoneʳ : p ≤ q → r + p ≤ r + q
+-monotoneʳ p≤q = trans (+-congˡ p≤q) (+-distribˡ-∧ _ _ _)
+-monotone : p ≤ p′ → q ≤ q′ → p + q ≤ p′ + q′
+-monotone p≤p′ q≤q′ = ≤-trans (+-monotoneˡ p≤p′) (+-monotoneʳ q≤q′)
opaque
  
  +-decreasingˡ : 𝟙 ≤ 𝟘 → p + q ≤ p
  +-decreasingˡ {p} {q} 𝟙≤𝟘 = begin
    p + q  ≤⟨ +-monotoneʳ (≤𝟘⇔𝟙≤𝟘 .proj₂ 𝟙≤𝟘) ⟩
    p + 𝟘  ≡⟨ +-identityʳ _ ⟩
    p      ∎
    where
    open Tools.Reasoning.PartialOrder ≤-poset
opaque
  
  +-decreasingˡ→𝟙≤𝟘 : (∀ {p q} → p + q ≤ p) → 𝟙 ≤ 𝟘
  +-decreasingˡ→𝟙≤𝟘 =
    (∀ {p q} → p + q ≤ p)  →⟨ (λ hyp → hyp) ⟩
    𝟘 + 𝟙 ≤ 𝟘              →⟨ ≤-trans (≤-reflexive (sym (+-identityˡ _))) ⟩
    𝟙 ≤ 𝟘                  □
opaque
  
  +-decreasingʳ : 𝟙 ≤ 𝟘 → p + q ≤ q
  +-decreasingʳ {p} {q} 𝟙≤𝟘 = begin
    p + q  ≡⟨ +-comm _ _ ⟩
    q + p  ≤⟨ +-decreasingˡ 𝟙≤𝟘 ⟩
    q      ∎
    where
    open Tools.Reasoning.PartialOrder ≤-poset
opaque
  
  +-decreasingʳ→𝟙≤𝟘 : (∀ {p q} → p + q ≤ q) → 𝟙 ≤ 𝟘
  +-decreasingʳ→𝟙≤𝟘 =
    (∀ {p q} → p + q ≤ q)  →⟨ (λ hyp → hyp) ⟩
    𝟙 + 𝟘 ≤ 𝟘              →⟨ ≤-trans (≤-reflexive (sym (+-identityʳ _))) ⟩
    𝟙 ≤ 𝟘                  □
+-sub-interchangeable-∧ : _+_ SubInterchangeable _∧_ by _≤_
+-sub-interchangeable-∧ p q p′ q′ = begin
  (p ∧ q) + (p′ ∧ q′)                            ≈⟨ +-distribˡ-∧ _ _ _ ⟩
  ((p ∧ q) + p′) ∧ ((p ∧ q) + q′)                ≈⟨ ∧-cong (+-distribʳ-∧ _ _ _) (+-distribʳ-∧ _ _ _) ⟩
  ((p + p′) ∧ (q + p′)) ∧ ((p + q′) ∧ (q + q′))  ≤⟨ ∧-monotone (∧-decreasingˡ _ _) (∧-decreasingʳ _ _) ⟩
  (p + p′) ∧ (q + q′)                            ∎
  where
  open Tools.Reasoning.PartialOrder ≤-poset
+-sub-interchangeable-+ : _+_ SubInterchangeable _+_ by _≡_
+-sub-interchangeable-+ p q r s =
  (p + q) + (r + s)  ≡⟨ +-assoc _ _ _ ⟩
  p + (q + (r + s))  ≡˘⟨ cong (_ +_) (+-assoc _ _ _) ⟩
  p + ((q + r) + s)  ≡⟨ cong (_ +_) (cong (_+ _) (+-comm _ _)) ⟩
  p + ((r + q) + s)  ≡⟨ cong (_ +_) (+-assoc _ _ _) ⟩
  p + (r + (q + s))  ≡˘⟨ +-assoc _ _ _ ⟩
  (p + r) + (q + s)  ∎
  where
  open Tools.Reasoning.PropositionalEquality
opaque
  
  ω·+≤ω·ˡ : ω · (p + q) ≤ ω · p
  ω·+≤ω·ˡ {p} {q} = begin
    ω · (p + q)  ≡⟨ ·-congˡ $ +-comm _ _ ⟩
    ω · (q + p)  ≤⟨ ω·+≤ω·ʳ ⟩
    ω · p        ∎
    where
    open Tools.Reasoning.PartialOrder ≤-poset
opaque
  
  ω≤𝟘 : ω ≤ 𝟘
  ω≤𝟘 = begin
    ω            ≡˘⟨ ·-identityʳ _ ⟩
    ω · 𝟙        ≡˘⟨ ·-congˡ $ +-identityʳ _ ⟩
    ω · (𝟙 + 𝟘)  ≤⟨ ω·+≤ω·ʳ ⟩
    ω · 𝟘        ≡⟨ ·-zeroʳ _ ⟩
    𝟘            ∎
    where
    open Tools.Reasoning.PartialOrder ≤-poset
opaque
  
  ω≤𝟘∧𝟙 : ω ≤ 𝟘 ∧ 𝟙
  ω≤𝟘∧𝟙 = ∧-greatest-lower-bound ω≤𝟘 ω≤𝟙
opaque
  
  ω+ω≤ω : ω + ω ≤ ω
  ω+ω≤ω = begin
    ω + ω          ≡˘⟨ +-cong (·-identityʳ _) (·-identityʳ _) ⟩
    ω · 𝟙 + ω · 𝟙  ≡˘⟨ ·-distribˡ-+ _ _ _ ⟩
    ω · (𝟙 + 𝟙)    ≤⟨ ω·+≤ω·ʳ ⟩
    ω · 𝟙          ≡⟨ ·-identityʳ _ ⟩
    ω              ∎
    where
    open Tools.Reasoning.PartialOrder ≤-poset