\begin{code} {-# OPTIONS --cubical --safe #-} open import Algebra open import Level module Control.Monad.Weighted.Cond {β} {π : Type β} (rng : Semiring π ) where open Semiring rng open import Level open import Path open import Path.Reasoning open import HLevels open import Data.Sigma open import Control.Monad.Weighted.Definition rng open import Control.Monad.Weighted.Eliminators rng open import Control.Monad.Weighted.Union rng open import Control.Monad.Weighted.Functor rng open import Control.Monad.Weighted.Functor.TypeDef \end{code} %<*cond-alg> \begin{code} β-alg : π β Ξ¦ A (Weighted A) β-alg w .fst (p β x β· xs β¨ wβxs β©) = (w * p) β x β· wβxs β-alg w .fst [] = [] \end{code} %</cond-alg> %<*cond-coh> \begin{code} β-alg w .snd .c-com p x q y xs wβxs = com (w * p) x (w * q) y wβxs β-alg w .snd .c-dup p q x xs wβxs = w * p β x β· w * q β x β· wβxs β‘β¨ dup (w * p) (w * q) x wβxs β© w * p + w * q β x β· wβxs β‘Λβ¨ cong (_β x β· wβxs) (*β¨+β© w p q) β© w * (p + q) β x β· wβxs β β-alg w .snd .c-del x xs wβxs = w * 0# β x β· wβxs β‘β¨ cong (_β x β· wβxs) (*0 w) β© 0# β x β· wβxs β‘β¨ del x wβxs β© wβxs β \end{code} %</cond-coh> \begin{code} β-alg c .snd .c-set _ = trunc infixr 5.5 _β_ \end{code} %<*cond-impl> \begin{code} _β_ : π β Weighted A β Weighted A x β xs = β¦ β-alg x β§ xs \end{code} %</cond-impl> \begin{code} _ : β {w wβ wβ wβ} β {x y z : A} β \end{code} %<*cond-example> \begin{code} w β (wβ β x β· wβ β y β· wβ β z β· []) β‘ (w * wβ) β x β· (w * wβ) β y β· (w * wβ) β z β· [] \end{code} %</cond-example> \begin{code} _ = refl β-distribΚ³ : β p q β (xs : Weighted A) β (p β xs) βͺ (q β xs) β‘ p + q β xs β-distribΚ³ p q = β¦ β-distribΚ³β² p q β§ where β-distribΚ³β² : β p q β Ξ¨[ xs β¦ A ] (p β xs) βͺ (q β xs) β‘ (p + q) β xs β-distribΚ³β² p q .fst [] = refl β-distribΚ³β² p q .fst (r β x β· xs β¨ P β©) = (p β (r β x β· xs)) βͺ (q β (r β x β· xs)) β‘Λβ¨ βͺ-cons (q * r) x (p β (r β x β· xs)) (q β xs) β© q * r β x β· (p β (r β x β· xs)) βͺ q β xs β‘β¨ cong (_βͺ q β xs) (dup (q * r) (p * r) x (p β xs)) β© q * r + p * r β x β· (p β xs) βͺ q β xs β‘Λβ¨ cong (_β x β· ((p β xs) βͺ (q β xs))) (β¨+β©* q p r) β© (q + p) * r β x β· (p β xs) βͺ (q β xs) β‘β¨ cong ((q + p) * r β x β·_) P β© (q + p) * r β x β· (p + q) β xs β‘β¨ cong (Ξ» pq β pq * r β x β· (p + q) β xs) (+-comm q p) β© (p + q) * r β x β· (p + q) β xs β‘β¨β© (p + q) β (r β x β· xs) β β-distribΚ³β² p q .snd = eq-coh β-distribΛ‘ : β p β (xs ys : Weighted A) β (p β xs) βͺ (p β ys) β‘ p β (xs βͺ ys) β-distribΛ‘ = Ξ» p xs ys β β¦ β-distribΛ‘β² p ys β§ xs module JDistribL where β-distribΛ‘β² : β p ys β Ξ¨[ xs β¦ A ] (p β xs) βͺ (p β ys) β‘ p β (xs βͺ ys) β-distribΛ‘β² p ys .fst [] = refl β-distribΛ‘β² p ys .fst (q β x β· xs β¨ P β©) = cong (p * q β x β·_) P β-distribΛ‘β² p ys .snd = eq-coh 0β : (xs : Weighted A) β 0# β xs β‘ [] 0β = β¦ 0ββ² β§ where 0ββ² : Ξ¨[ xs β¦ A ] 0# β xs β‘ [] 0ββ² .fst [] = refl 0ββ² .fst (p β x β· xs β¨ P β©) = 0# β (p β x β· xs) β‘β¨β© 0# * p β x β· 0# β xs β‘β¨ cong (_β x β· 0# β xs) (0* p) β© 0# β x β· 0# β xs β‘β¨ del x (0# β xs) β© 0# β xs β‘β¨ P β© [] β 0ββ² .snd = eq-coh 1β : (xs : Weighted A) β 1# β xs β‘ xs 1β = β¦ 1ββ² β§ where 1ββ² : Ξ¨[ xs β¦ A ] 1# β xs β‘ xs 1ββ² .fst [] = refl 1ββ² .fst (p β x β· xs β¨ P β©) = 1# β (p β x β· xs) β‘β¨β© 1# * p β x β· 1# β xs β‘β¨ cong (_β x β· 1# β xs) (1* p) β© p β x β· 1# β xs β‘β¨ cong (p β x β·_) P β© p β x β· xs β 1ββ² .snd = eq-coh *-assoc-β : β p q (xs : Weighted A) β (p * q) β xs β‘ p β (q β xs) *-assoc-β p q = β¦ *-assoc-ββ² p q β§ where *-assoc-ββ² : β p q β Ξ¨[ xs β¦ A ] (p * q) β xs β‘ p β (q β xs) *-assoc-ββ² p q .fst [] = refl *-assoc-ββ² p q .fst (r β x β· xs β¨ P β©) = p * q β (r β x β· xs) β‘β¨β© p * q * r β x β· (p * q β xs) β‘β¨ cong (_β x β· (p * q β xs)) (*-assoc p q r) β© p * (q * r) β x β· (p * q β xs) β‘β¨ cong (p * (q * r) β x β·_) P β© p * (q * r) β x β· (p β (q β xs)) β‘β¨β© p β (q β (r β x β· xs)) β *-assoc-ββ² p q .snd = eq-coh ββ : (x : π ) β x β ([] {A = A}) β‘ [] ββ x = refl \end{code}