\begin{code}
{-# OPTIONS --cubical --safe #-}
open import Algebra
open import Level
module Control.Monad.Weighted.Monad {β} {π
: 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.Cond rng
open import Control.Monad.Weighted.Functor rng
open import Control.Monad.Weighted.Functor.TypeDef
\end{code}
%<*bind-alg>
\begin{code}
bind-alg : (A β Weighted B) β
Ξ¦ A (Weighted B)
bind-alg f .fst (p β x β· _ β¨ xs β©) = (p β f x) βͺ xs
bind-alg f .fst [] = []
\end{code}
%</bind-alg>
\begin{code}
bind-alg f .snd .c-set _ = trunc
bind-alg f .snd .c-del x _ xs = cong (_βͺ xs) (0β (f x))
bind-alg f .snd .c-dup p q x _ xs =
p β f x βͺ (q β f x βͺ xs) β‘β¨ βͺ-assoc (p β f x) (q β f x) xs β©
(p β f x βͺ q β f x) βͺ xs β‘β¨ cong (_βͺ xs) (β-distribΚ³ p q (f x)) β©
(p + q β f x) βͺ xs β
bind-alg f .snd .c-com p x q y _ xs =
p β f x βͺ q β f y βͺ xs β‘β¨ βͺ-assoc (p β f x) (q β f y) xs β©
(p β f x βͺ q β f y) βͺ xs β‘β¨ cong (_βͺ xs) (βͺ-com (p β f x) (q β f y)) β©
(q β f y βͺ p β f x) βͺ xs β‘Λβ¨ βͺ-assoc (q β f y) (p β f x) xs β©
q β f y βͺ p β f x βͺ xs β
\end{code}
%<*bind-impl>
\begin{code}
_>>=_ : Weighted A β (A β Weighted B) β
Weighted B
xs >>= f = β¦ bind-alg f β§ xs
\end{code}
%</bind-impl>
%<*pure>
\begin{code}
pure : A β Weighted A
pure x = 1# β x β· []
\end{code}
%</pure>
\begin{code}
_>>_ : Weighted A β Weighted B β Weighted B
xs >> ys = do
_ β xs
ys
_<*>_ : Weighted (A β B) β Weighted A β Weighted B
fs <*> xs = do
f β fs
x β xs
pure (f x)
>>=-distrib : (xs ys : Weighted A) (g : A β Weighted B) β (xs βͺ ys) >>= g β‘ (xs >>= g) βͺ (ys >>= g)
>>=-distrib xs ys g = β¦ >>=-distribβ² ys g β§ xs
where
>>=-distribβ² : (ys : Weighted A) (g : A β Weighted B) β Ξ¨[ xs β¦ A ] ((xs βͺ ys) >>= g) β‘ (xs >>= g) βͺ (ys >>= g)
>>=-distribβ² ys g .fst [] = refl
>>=-distribβ² ys g .fst (p β x β· xs β¨ P β©) =
(((p β x β· xs) βͺ ys) >>= g) β‘β¨β©
(p β x β· xs βͺ ys) >>= g β‘β¨β©
p β g x βͺ ((xs βͺ ys) >>= g) β‘β¨ cong (p β g x βͺ_) P β©
p β g x βͺ ((xs >>= g) βͺ (ys >>= g)) β‘β¨ βͺ-assoc (p β g x) (xs >>= g) (ys >>= g) β©
(p β g x βͺ (xs >>= g)) βͺ (ys >>= g) β‘β¨β©
((p β x β· xs) >>= g) βͺ (ys >>= g) β
>>=-distribβ² ys g .snd = eq-coh
β-assoc->>= : β p (xs : Weighted A) (f : A β Weighted B) β (p β xs) >>= f β‘ p β (xs >>= f)
β-assoc->>= p xs f = β¦ β-assoc->>=β² p f β§ xs
where
β-assoc->>=β² : β p (f : A β Weighted B) β Ξ¨[ xs β¦ A ] (p β xs) >>= f β‘ p β (xs >>= f)
β-assoc->>=β² p f .fst [] = refl
β-assoc->>=β² p f .fst (q β x β· xs β¨ P β©) =
(p β (q β x β· xs)) >>= f β‘β¨β©
(p * q β x β· p β xs) >>= f β‘β¨β©
((p * q) β f x) βͺ ((p β xs) >>= f) β‘β¨ cong (((p * q) β f x) βͺ_) P β©
((p * q) β f x) βͺ (p β (xs >>= f)) β‘β¨ cong (_βͺ (p β (xs >>= f))) (*-assoc-β p q (f x)) β©
(p β (q β f x)) βͺ (p β (xs >>= f)) β‘β¨ β-distribΛ‘ p (q β f x) (xs >>= f) β©
p β ((q β x β· xs) >>= f) β
β-assoc->>=β² p f .snd = eq-coh
>>=-idΛ‘ : (x : A) β (f : A β Weighted B)
β (pure x >>= f) β‘ f x
>>=-idΛ‘ x f =
pure x >>= f β‘β¨β©
(1# β x β· []) >>= f β‘β¨β©
1# β f x βͺ [] >>= f β‘β¨β©
1# β f x βͺ [] β‘β¨ βͺ-idr (1# β f x) β©
1# β f x β‘β¨ 1β (f x) β©
f x β
>>=-idΚ³ : (xs : Weighted A) β xs >>= pure β‘ xs
>>=-idΚ³ = β¦ >>=-idΚ³β² β§
where
>>=-idΚ³β² : Ξ¨[ xs β¦ A ] xs >>= pure β‘ xs
>>=-idΚ³β² .fst [] = refl
>>=-idΚ³β² .fst (p β x β· xs β¨ P β©) =
((p β x β· xs) >>= pure) β‘β¨β©
p β (pure x) βͺ (xs >>= pure) β‘β¨β©
p β (1# β x β· []) βͺ (xs >>= pure) β‘β¨β©
p * 1# β x β· [] βͺ (xs >>= pure) β‘β¨β©
p * 1# β x β· (xs >>= pure) β‘β¨ cong (_β x β· (xs >>= pure)) (*1 p) β©
p β x β· xs >>= pure β‘β¨ cong (p β x β·_) P β©
p β x β· xs β
>>=-idΚ³β² .snd = eq-coh
>>=-assoc : (xs : Weighted A) β (f : A β Weighted B) β (g : B β Weighted C)
β ((xs >>= f) >>= g) β‘ xs >>= (Ξ» x β f x >>= g)
>>=-assoc xs f g = β¦ >>=-assocβ² f g β§ xs
where
>>=-assocβ² : (f : A β Weighted B) β (g : B β Weighted C) β Ξ¨[ xs β¦ A ] ((xs >>= f) >>= g) β‘ xs >>= (Ξ» x β f x >>= g)
>>=-assocβ² f g .fst [] = refl
>>=-assocβ² f g .fst (p β x β· xs β¨ P β©) =
(((p β x β· xs) >>= f) >>= g) β‘β¨β©
((p β f x βͺ (xs >>= f)) >>= g) β‘β¨ >>=-distrib (p β f x) (xs >>= f) g β©
((p β f x) >>= g) βͺ ((xs >>= f) >>= g) β‘β¨ cong ((p β f x) >>= g βͺ_) P β©
((p β f x) >>= g) βͺ (xs >>= (Ξ» y β f y >>= g)) β‘β¨ cong (_βͺ (xs >>= (Ξ» y β f y >>= g))) (β-assoc->>= p (f x) g) β©
p β (f x >>= g) βͺ (xs >>= (Ξ» y β f y >>= g)) β‘β¨β©
((p β x β· xs) >>= (Ξ» y β f y >>= g)) β
>>=-assocβ² f g .snd = eq-coh
\end{code}