\begin{code}
{-# OPTIONS --cubical --safe --postfix-projections #-}
open import Algebra
open import HLevels
open import Level
module Control.Monad.Weighted.Expect {ā} {š
: Type ā} (rng : Semiring š
) (cIsSet : isSet š
) where
open Semiring rng renaming (+-comm to +-com)
open import Level
open import Path
open import Path.Reasoning
open import Data.Sigma
open import Control.Monad.Weighted.Definition rng
open import Control.Monad.Weighted.Eliminators rng
open import Control.Monad.Weighted.Functor rng
open import Control.Monad.Weighted.Functor.TypeDef
open import Function
open import Data.Lift
open import HITs.PropositionalTruncation
open import Data.Sum
\end{code}
%<*expect-alg>
\begin{code}
ā« : (A ā š
) ā Ī¦ A š
ā« f .fst (p ā x ā· _ āØ ā«fxs ā©) = p * f x + ā«fxs
ā« f .fst [] = 0#
\end{code}
%</expect-alg>
%<*com>
\begin{code}
ā« f .snd .c-com p x q y _ r =
p * f x + (q * f y + r)
ā”ĖāØ +-assoc _ _ r ā©
p * f x + q * f y + r
ā”āØ cong (_+ r) (+-com _ _) ā©
q * f y + p * f x + r
ā”āØ +-assoc _ _ r ā©
q * f y + (p * f x + r) ā
\end{code}
%</com>
%<*dup>
\begin{code}
ā« f .snd .c-dup p q x _ r =
p * f x + (q * f x + r)
ā”ĖāØ +-assoc _ _ r ā©
(p * f x + q * f x) + r
ā”ĖāØ cong (_+ r) (āØ+ā©* _ _ _) ā©
(p + q) * f x + r ā
\end{code}
%</dup>
%<*del>
\begin{code}
ā« f .snd .c-del x _ r =
0# * f x + r
ā”āØ cong (_+ r) (0* (f x)) ā©
0# + r
ā”āØ 0+ r ā©
r ā
\end{code}
%</del>
\begin{code}
ā« f .snd .c-set _ = cIsSet
expect : (A ā š
) ā Weighted A ā š
expect f xs = ā¦ ā« f ā§ xs
syntax expect (Ī» x ā e) = ā« e š x
open import Algebra.SemiringLiterals rng
open import Data.Nat hiding (_+_; _*_)
open import Data.Unit
open import Data.Nat.Literals
open import Literals.Number
_/_ : š
ā š
ā š
x / y = x
ā
: š
ā
= 1#
die : Weighted ā
\end{code}
%<*die>
\begin{code}
die = ā
ā 1 ā· ā
ā 2 ā· ā
ā 3 ā· ā
ā 4 ā· ā
ā 5 ā· ā
ā 6 ā· []
\end{code}
%</die>
\begin{code}
open import Data.Bool
open import Agda.Builtin.Nat using (_<_)
open import Algebra.SemiringLiterals
open import Data.Unit.UniversePolymorphic
ā[3<] : š
\end{code}
%<*p-3>
\begin{code}
ā[3<] = (ā« if 3 < x then 1 else 0 š x) die
\end{code}
%</p-3>
\begin{code}
open import Control.Monad.Weighted.Union rng
\end{code}
%<*int-distrib>
\begin{code}
ā«-distrib : ā (f : A ā š
) (ys : Weighted A) ā ĪØ[ xs ā¦ A ] ā¦ ā« f ā§ xs + ā¦ ā« f ā§ ys ā” ā¦ ā« f ā§ (xs āŖ ys)
ā«-distrib f ys .fst [] = 0+ (ā¦ ā« f ā§ ys)
ā«-distrib f ys .fst (w ā x ā· xs āØ PāØxsā© ā©) =
ā¦ ā« f ā§ (w ā x ā· xs) + ā¦ ā« f ā§ ys ā”āØā©
(w * f x + ā¦ ā« f ā§ xs) + ā¦ ā« f ā§ ys ā”āØ +-assoc _ _ _ ā©
w * f x + (ā¦ ā« f ā§ xs + ā¦ ā« f ā§ ys) ā”āØ cong (w * f x +_) PāØxsā© ā©
w * f x + ā¦ ā« f ā§ (xs āŖ ys) ā”āØā©
ā¦ ā« f ā§ (w ā x ā· xs āŖ ys) ā
ā«-distrib f ys .snd = prop-coh Ī» _ ā cIsSet _ _
\end{code}
%</int-distrib>