diff --git a/src/Probability/Core.idr b/src/Probability/Core.idr index aaa7054..3661ae2 100644 --- a/src/Probability/Core.idr +++ b/src/Probability/Core.idr @@ -5,23 +5,24 @@ import Probability.Utils %default total +%access export + ||| Representation of a probability distribution -abstract data Probability p a = Pr (List (a,p)) -%access public - - ---- Types ---- +public export Prob : Type -> Type Prob = Probability Double +public export Transition : Type -> Type -> Type Transition a b = a -> Prob b +public export Trans : Type -> Type Trans t = Transition t t @@ -52,14 +53,14 @@ shape xs ps = Pr $ zipWith MkPair xs (normalize ps) ---- Instances ---- -instance Functor (Probability p) where +Functor (Probability p) where map f (Pr l) = Pr $ left f <$> l -instance Num p => Applicative (Probability p) where +Num p => Applicative (Probability p) where pure = certainly fm <*> m = Pr [ (f x, q*w) | (f,w) <- runProb fm, (x,q) <- runProb m ] -instance Num p => Monad (Probability p) where +Num p => Monad (Probability p) where d >>= f = Pr [ (y, q*w) | (x,w) <- runProb d, (y,q) <- runProb (f x) ] diff --git a/src/Probability/Display.idr b/src/Probability/Display.idr index 3763489..fe8eda1 100644 --- a/src/Probability/Display.idr +++ b/src/Probability/Display.idr @@ -21,10 +21,10 @@ intPart = cast . cast {to=Integer} fracPart : Double -> Double fracPart x = x - intPart x -fpow f p = if p >= 0 then pow f (cast p) - else 1 / (pow f $ cast $ abs p) ||| Raise a double to an arbitrary integral power fpow : Double -> Integer -> Double +fpow f p = if p >= 0 then pow f (cast p) + else 1 / (pow f $ cast $ abs p) ---- Display Bars ---- diff --git a/src/Probability/Examples/MontyHall.idr b/src/Probability/Examples/MontyHall.idr index ad18120..a9042ed 100644 --- a/src/Probability/Examples/MontyHall.idr +++ b/src/Probability/Examples/MontyHall.idr @@ -11,13 +11,13 @@ import Data.Vect data Door = One | Two | Three -instance Eq Door where +Eq Door where One == One = True Two == Two = True Three == Three = True _ == _ = False -instance Show Door where +Show Door where show One = "Door #1" show Two = "Door #2" show Three = "Door #3" @@ -47,10 +47,10 @@ score {k} v = case k of data GameScore = Score (Vect (S (S n)) Door) -instance Eq GameScore where +Eq GameScore where (Score v) == (Score w) = vecEq v w -instance Show GameScore where +Show GameScore where show (Score v) = let status = if score v then " WIN " else " LOSE " in status ++ show v @@ -58,10 +58,10 @@ instance Show GameScore where data GameOutcome = Outcome (Monty (S (S n))) -instance Eq GameOutcome where +Eq GameOutcome where (Outcome v) == (Outcome w) = score v == score w -instance Show GameOutcome where +Show GameOutcome where show (Outcome v) = if score v then " WIN " else " LOSE " diff --git a/src/Probability/Monad.idr b/src/Probability/Monad.idr index 3504244..6c6a484 100644 --- a/src/Probability/Monad.idr +++ b/src/Probability/Monad.idr @@ -3,6 +3,7 @@ module Probability.Monad %default total +%access export infixr 6 >=> (>=>) : Monad m => (a -> m b) -> (b -> m c) -> a -> m c diff --git a/src/Probability/Utils.idr b/src/Probability/Utils.idr index 799f57d..07adedc 100644 --- a/src/Probability/Utils.idr +++ b/src/Probability/Utils.idr @@ -5,6 +5,7 @@ import Data.Vect %default total +%access export infixl 5 ::~