{-# OPTIONS --cubical --safe #-} open import Semirings module Probability {s} (rng : Semiring s) where open Semiring rng open import Cubical.Core.Everything open import Cubical.Relation.Everything open import Cubical.Foundations.Prelude hiding (_β‘β¨_β©_) open import Cubical.HITs.SetTruncation open import Utils infixr 5 _&_β·_ data π« (A : Set a) : Set (a β s) where [] : π« A _&_β·_ : (p : R) β (x : A) β π« A β π« A dup : β p q x xs β p & x β· q & x β· xs β‘ p + q & x β· xs com : β p x q y xs β p & x β· q & y β· xs β‘ q & y β· p & x β· xs del : β x xs β 0# & x β· xs β‘ xs trunc : isSet (π« A) record β _β_β {a β} (A : Set a) (P : π« A β Set β) : Set (a β β β s) where constructor elim field β _β-set : β {xs} β isSet (P xs) β _β[] : P [] β _β_&_β·_ : β p x xs β P xs β P (p & x β· xs) private z = β _β[]; f = β _β_&_β·_ field β _β-dup : (β p q x xs pxs β PathP (Ξ» i β P (dup p q x xs i)) (f p x (q & x β· xs) (f q x xs pxs)) (f (p + q) x xs pxs)) β _β-com : (β p x q y xs pxs β PathP (Ξ» i β P (com p x q y xs i)) (f p x (q & y β· xs) (f q y xs pxs)) (f q y (p & x β· xs) (f p x xs pxs))) β _β-del : (β x xs pxs β PathP (Ξ» i β P (del x xs i)) (f 0# x xs pxs) pxs) β _ββ : (xs : π« A) β P xs β [] ββ = z β p & x β· xs ββ = f p x xs β xs ββ β dup p q x xs i ββ = β _β-dup p q x xs β xs ββ i β com p x q y xs i ββ = β _β-com p x q y xs β xs ββ i β del x xs i ββ = β _β-del x xs β xs ββ i β trunc xs ys p q i j ββ = elimSquashβ (Ξ» xs β β _β-set {xs}) (trunc xs ys p q) β xs ββ β ys ββ (cong β _ββ p) (cong β _ββ q) i j open β _β_β public elim-syntax : β {a β} β (A : Set a) β (π« A β Set β) β Set (a β β β s) elim-syntax = β _β_β syntax elim-syntax A (Ξ» xs β Pxs) = [ xs βπ« A β Pxs ] record β¦_β_β§ {a β} (A : Set a) (P : π« A β Set β) : Set (a β β β s) where constructor elim-prop field β¦_β§-prop : β {xs} β isProp (P xs) β¦_β§[] : P [] β¦_β§_&_β·_β¨_β© : β p x xs β P xs β P (p & x β· xs) private z = β¦_β§[]; f = β¦_β§_&_β·_β¨_β© β¦_β§β = elim (isPropβisSet β¦_β§-prop) z f (Ξ» p q x xs pxs β toPathP (β¦_β§-prop (transp (Ξ» i β P (dup p q x xs i)) i0 (f p x (q & x β· xs) (f q x xs pxs))) (f (p + q) x xs pxs) )) (Ξ» p x q y xs pxs β toPathP (β¦_β§-prop (transp (Ξ» i β P (com p x q y xs i)) i0 (f p x (q & y β· xs) (f q y xs pxs))) (f q y (p & x β· xs) (f p x xs pxs)))) Ξ» x xs pxs β toPathP (β¦_β§-prop (transp (Ξ» i β P (del x xs i)) i0 ((f 0# x xs pxs))) pxs) β¦_β§β = β β¦_β§β ββ open β¦_β_β§ public elim-prop-syntax : β {a β} β (A : Set a) β (π« A β Set β) β Set (a β β β s) elim-prop-syntax = β¦_β_β§ syntax elim-prop-syntax A (Ξ» xs β Pxs) = β¦ xs βπ« A β Pxs β§ record [_β¦_] {a b} (A : Set a) (B : Set b) : Set (a β b β s) where constructor rec field [_]-set : isSet B [_]_&_β·_ : R β A β B β B [_][] : B private f = [_]_&_β·_; z = [_][] field [_]-dup : β p q x xs β f p x (f q x xs) β‘ f (p + q) x xs [_]-com : β p x q y xs β f p x (f q y xs) β‘ f q y (f p x xs) [_]-del : β x xs β f 0# x xs β‘ xs [_]β = elim [_]-set z (Ξ» p x _ xs β f p x xs) (Ξ» p q x xs β [_]-dup p q x) (Ξ» p x q y xs β [_]-com p x q y) (Ξ» x xs β [_]-del x) [_]β = β [_]β ββ open [_β¦_] public map : (A β B) β π« A β π« B map = Ξ» f β [ mapβ² f ]β module Map where mapβ² : (A β B) β [ A β¦ π« B ] [ mapβ² f ] p & x β· xs = p & f x β· xs [ mapβ² f ][] = [] [ mapβ² f ]-set = trunc [ mapβ² f ]-dup p q x xs = dup p q (f x) xs [ mapβ² f ]-com p x q y xs = com p (f x) q (f y) xs [ mapβ² f ]-del x xs = del (f x) xs infixr 5 _βͺ_ _βͺ_ : π« A β π« A β π« A _βͺ_ = Ξ» xs ys β [ union ys ]β xs module Union where union : π« A β [ A β¦ π« A ] [ union ys ]-set = trunc [ union ys ] p & x β· xs = p & x β· xs [ union ys ][] = ys [ union ys ]-dup = dup [ union ys ]-com = com [ union ys ]-del = del infixl 7 _β_ _β_ : R β π« A β π« A _β_ = Ξ» p β [ p ββ² ]β module Cond where _ββ² : R β [ A β¦ π« A ] [ p ββ² ]-set = trunc [ p ββ² ][] = [] [ p ββ² ] q & x β· xs = p * q & x β· xs [ p ββ² ]-com q x r y xs = com (p * q) x (p * r) y xs [ p ββ² ]-dup q r x xs = p * q & x β· p * r & x β· xs β‘β¨ dup (p * q) (p * r) x xs β© p * q + p * r & x β· xs β‘Λβ¨ cong (_& x β· xs) (*β¨+β© p q r) β© p * (q + r) & x β· xs β [ p ββ² ]-del x xs = p * 0# & x β· xs β‘β¨ cong (_& x β· xs) (*0 p) β© 0# & x β· xs β‘β¨ del x xs β© xs β β« : (A β R) β π« A β R β« = Ξ» f β [ β«β² f ]β module Expect where β«β² : (A β R) β [ A β¦ R ] [ β«β² f ]-set = sIsSet [ β«β² f ] p & x β· xs = p * f x + xs [ β«β² f ][] = 0# [ β«β² f ]-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) (β¨+β©* p q (f x)) β© (p + q) * f x + xs β [ β«β² f ]-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) (+-comm (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) β [ β«β² f ]-del x xs = 0# * f x + xs β‘β¨ cong (_+ xs) (0* (f x)) β© 0# + xs β‘β¨ 0+ (xs) β© xs β syntax β« (Ξ» x β e) = β« e π x pure : A β π« A pure x = 1# & x β· [] βͺ-cons : β p (x : A) xs ys β xs βͺ p & x β· ys β‘ p & x β· xs βͺ ys βͺ-cons = Ξ» p x xs ys β β¦ βͺ-consβ² p x ys β§β xs module UCons where βͺ-consβ² : β p x ys β β¦ xs βπ« A β xs βͺ p & x β· ys β‘ p & x β· xs βͺ ys β§ β¦ βͺ-consβ² p x ys β§-prop = trunc _ _ β¦ βͺ-consβ² p x ys β§[] = refl β¦ βͺ-consβ² p x ys β§ r & y β· xs β¨ P β© = cong (r & y β·_) P β com r y p x (xs βͺ ys) β-distribΚ³ : β p q β (xs : π« A) β p β xs βͺ q β xs β‘ (p + q) β xs β-distribΚ³ = Ξ» p q β β¦ β-distribΚ³β² p q β§β module JDistrib where β-distribΚ³β² : β p q β β¦ xs βπ« A β p β xs βͺ q β xs β‘ (p + q) β xs β§ β¦ β-distribΚ³β² p q β§-prop = trunc _ _ β¦ β-distribΚ³β² p q β§[] = refl β¦ β-distribΚ³β² p q β§ 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 β (xs ys : π« 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 β§-prop = trunc _ _ β¦ β-distribΛ‘β² p ys β§[] = refl β¦ β-distribΛ‘β² p ys β§ q & x β· xs β¨ P β© = p β (q & x β· xs) βͺ p β ys β‘β¨β© p * q & x β· p β xs βͺ p β ys β‘β¨ cong (p * q & x β·_) P β© p * q & x β· p β (xs βͺ ys) β‘β¨β© p β ((q & x β· xs) βͺ ys) β βͺ-assoc : (xs ys zs : π« A) β xs βͺ (ys βͺ zs) β‘ (xs βͺ ys) βͺ zs βͺ-assoc = Ξ» xs ys zs β β¦ βͺ-assocβ² ys zs β§β xs module UAssoc where βͺ-assocβ² : β ys zs β β¦ xs βπ« A β xs βͺ (ys βͺ zs) β‘ (xs βͺ ys) βͺ zs β§ β¦ βͺ-assocβ² ys zs β§-prop = trunc _ _ β¦ βͺ-assocβ² ys zs β§[] = refl β¦ βͺ-assocβ² ys zs β§ p & x β· xs β¨ P β© = cong (p & x β·_) P βͺ-idΚ³ : (xs : π« A) β xs βͺ [] β‘ xs βͺ-idΚ³ = β¦ βͺ-idΚ³β² β§β module UIdR where βͺ-idΚ³β² : β¦ xs βπ« A β xs βͺ [] β‘ xs β§ β¦ βͺ-idΚ³β² β§-prop = trunc _ _ β¦ βͺ-idΚ³β² β§[] = refl β¦ βͺ-idΚ³β² β§ p & x β· xs β¨ P β© = cong (p & x β·_) P βͺ-comm : (xs ys : π« A) β xs βͺ ys β‘ ys βͺ xs βͺ-comm = Ξ» xs ys β β¦ βͺ-commβ² ys β§β xs module UComm where βͺ-commβ² : β ys β β¦ xs βπ« A β xs βͺ ys β‘ ys βͺ xs β§ β¦ βͺ-commβ² ys β§-prop = trunc _ _ β¦ βͺ-commβ² ys β§[] = sym (βͺ-idΚ³ ys) β¦ βͺ-commβ² ys β§ p & x β· xs β¨ P β© = cong (p & x β·_) P β sym (βͺ-cons p x ys xs) 0β : (xs : π« A) β 0# β xs β‘ [] 0β = β¦ 0ββ² β§β module ZeroJ where 0ββ² : β¦ xs βπ« A β 0# β xs β‘ [] β§ β¦ 0ββ² β§-prop = trunc _ _ β¦ 0ββ² β§[] = refl β¦ 0ββ² β§ 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 β© [] β _>>=_ : π« A β (A β π« B) β π« B xs >>= f = [ f =<< ]β xs module Bind where _=<< : (A β π« B) β [ A β¦ π« B ] [ f =<< ] p & x β· xs = p β (f x) βͺ xs [ f =<< ][] = [] [ f =<< ]-set = trunc [ f =<< ]-del x xs = cong (_βͺ xs) (0β (f x)) [ f =<< ]-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 β [ f =<< ]-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) (βͺ-comm (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 β _<*>_ : π« (A β B) β π« A β π« B fs <*> xs = do f β fs x β xs pure (f x) 1β : (xs : π« A) β 1# β xs β‘ xs 1β = β¦ 1ββ² β§β module OneJoin where 1ββ² : β¦ xs βπ« A β 1# β xs β‘ xs β§ β¦ 1ββ² β§-prop = trunc _ _ β¦ 1ββ² β§[] = refl β¦ 1ββ² β§ 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 β >>=-distrib : (xs ys : π« A) (g : A β π« B) β (xs βͺ ys) >>= g β‘ (xs >>= g) βͺ (ys >>= g) >>=-distrib = Ξ» xs ys g β β¦ >>=-distribβ² ys g β§β xs module BindDistrib where >>=-distribβ² : (ys : π« A) (g : A β π« B) β β¦ xs βπ« A β ((xs βͺ ys) >>= g) β‘ (xs >>= g) βͺ (ys >>= g) β§ β¦ >>=-distribβ² ys g β§-prop = trunc _ _ β¦ >>=-distribβ² ys g β§[] = refl β¦ >>=-distribβ² ys g β§ 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) β *-assoc-β : β p q (xs : π« A) β (p * q) β xs β‘ p β (q β xs) *-assoc-β = Ξ» p q β β¦ *-assoc-ββ² p q β§β module MAssocJ where *-assoc-ββ² : β p q β β¦ xs βπ« A β (p * q) β xs β‘ p β (q β xs) β§ β¦ *-assoc-ββ² p q β§-prop = trunc _ _ β¦ *-assoc-ββ² p q β§[] = refl β¦ *-assoc-ββ² p q β§ 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 (xs : π« A) (f : A β π« B) β (p β xs) >>= f β‘ p β (xs >>= f) β-assoc->>= = Ξ» p xs f β β¦ β-assoc->>=β² p f β§β xs module JDistribB where β-assoc->>=β² : β p (f : A β π« B) β β¦ xs βπ« A β (p β xs) >>= f β‘ p β (xs >>= f) β§ β¦ β-assoc->>=β² p f β§-prop = trunc _ _ β¦ β-assoc->>=β² p f β§[] = refl β¦ β-assoc->>=β² p f β§ 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) β module MonadLaws where lawΒΉ : (x : A) β (f : A β π« B) β (pure x >>= f) β‘ f x lawΒΉ x f = pure x >>= f β‘β¨β© (1# & x β· []) >>= f β‘β¨β© 1# β f x βͺ [] >>= f β‘β¨β© 1# β f x βͺ [] β‘β¨ βͺ-idΚ³ (1# β f x) β© 1# β f x β‘β¨ 1β (f x) β© f x β lawΒ² : (xs : π« A) β xs >>= pure β‘ xs lawΒ² = β¦ lawΒ²β² β§β module Law1 where lawΒ²β² : β¦ xs βπ« A β xs >>= pure β‘ xs β§ β¦ lawΒ²β² β§-prop = trunc _ _ β¦ lawΒ²β² β§[] = refl β¦ lawΒ²β² β§ 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 β lawΒ³ : (xs : π« A) β (f : A β π« B) β (g : B β π« C) β ((xs >>= f) >>= g) β‘ xs >>= (Ξ» x β f x >>= g) lawΒ³ = Ξ» xs f g β β¦ lawΒ³β² f g β§β xs module Law3 where lawΒ³β² : (f : A β π« B) β (g : B β π« C) β β¦ xs βπ« A β ((xs >>= f) >>= g) β‘ xs >>= (Ξ» x β f x >>= g) β§ β¦ lawΒ³β² f g β§-prop = trunc _ _ β¦ lawΒ³β² f g β§[] = refl β¦ lawΒ³β² f g β§ 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)) β open import Lists fromListΒΉ : List A β π« A fromListΒΉ = foldr (Ξ» x xs β 1# & x β· xs) []