@@ -43,9 +43,9 @@ module LAoP.Dist.Internal
4343 )
4444 where
4545
46−import LAoP.Matrix.Type hiding (TrivialP, Countable, CountableDimensions, CountableN, CountableDimensionsN, Liftable, FromListsN)
46+import LAoP.Matrix.Type hiding (TrivialP, Countable, CountableDimensions, CountableN, CountableDimensionsN, FromListsN)
4747import Prelude hiding (id, (.))
48−import qualified LAoP.Matrix.Internal as I
48+import qualified LAoP.Matrix.Alternative as I
4949import LAoP.Utils
5050import GHC.TypeLits
5151import Data.Proxy
@@ -66,37 +66,26 @@ type Countable a = KnownNat (I.Count a)
6666type CountableN a = KnownNat (I.Count (I.Normalize a))
6767type CountableDimensionsN a b = (CountableN a, CountableN b)
6868type FromListsN a b = I.FromLists Prob (I.Normalize a) (I.Normalize b)
69−type Liftable a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num Prob, Ord Prob)
7069type TrivialP a b = Normalize (a, b) ~ Normalize (Normalize a, Normalize b)
7170
7271-- | Functor instance
73−fmapD ::
74− ( Liftable a b,
75− CountableDimensionsN a b,
76− FromListsN b a
77− )
78− =>
79− (a -> b) -> Dist a -> Dist b
80−fmapD f (D m) = D (fromF' f `comp` m)
72+fmapD :: Liftable a b => (a -> b) -> Dist a -> Dist b
73+fmapD f (D m) = D (fromF f `comp` m)
8174
8275-- | Applicative/Monoidal instance 'unit' function
8376unitD :: Dist ()
8477unitD = D (one 1)
8578
8679-- | Applicative/Monoidal instance 'mult' function
87−multD ::
88− ( CountableDimensionsN a b,
89− CountableN (a, b),
90− FromListsN (a, b) a,
91− FromListsN (a, b) b,
92− TrivialP a b
93− ) => Dist a -> Dist b -> Dist (a, b)
80+multD :: (FstM a b, SndM a b) => Dist a -> Dist b -> Dist (a, b)
9481multD (D a) (D b) = D (kr a b)
9582
9683-- | Selective instance function
97−selectD ::
98− ( FromListsN b b,
99− CountableN b
84+selectD ::
85+ ( EqN a,
86+ EqN b,
87+ EnumerableN b,
88+ ConstructN b
10089 ) => Dist (Either a b) -> Matrix Prob a b -> Dist b
10190selectD (D d) m = D (selectM d m)
10291
@@ -104,17 +93,14 @@ selectD (D d) m = D (selectM d m)
10493-- functions to apply to a given argument;
10594branchD ::
10695 ( Num e,
107− CountableDimensionsN a b,
108− CountableDimensionsN c (Either b c),
109− FromListsN c b,
110− FromListsN a b,
111− FromListsN a a,
112− FromListsN b b,
113− FromListsN c c,
114− FromListsN b a,
115− FromListsN b c,
116− FromListsN (Either b c) b,
117− FromListsN (Either b c) c
96+ EqN a,
97+ EqN b,
98+ EqN c,
99+ ConstructableN a b,
100+ ConstructN c,
101+ EnumerableN a,
102+ EnumerableN b,
103+ EnumerableN c
118104 )
119105 => Dist (Either a b) -> Matrix Prob a c -> Matrix Prob b c -> Dist c
120106branchD x l r = f x `selectD` g l `selectD` r
@@ -124,11 +110,10 @@ branchD x l r = f x `selectD` g l `selectD` r
124110
125111-- | Branch on a Boolean value, skipping unnecessary computations.
126112ifD ::
127− ( CountableDimensionsN a (Either () a),
128− FromListsN a a,
129− FromListsN a (),
130− FromListsN () a,
131− FromListsN (Either () a) a
113+ ( Num e,
114+ EqN a,
115+ ConstructN a,
116+ EnumerableN a
132117 )
133118 => Dist Bool -> Dist a -> Dist a -> Dist a
134119ifD x (D t) (D e) = branchD x' t e
@@ -136,12 +121,8 @@ ifD x (D t) (D e) = branchD x' t e
136121 x' = bool (Right ()) (Left ()) `fmapD` x
137122
138123-- | Monad instance 'return' function
139−returnD :: forall a . (Enum a, FromListsN () a, Countable a) => a -> Dist a
140−returnD a = D (col l)
141− where
142− i = fromInteger $ natVal (Proxy :: Proxy (Count a))
143− x = fromEnum a
144− l = take x [0,0..] ++ [1] ++ take (i - x - 1) [0,0..]
124+returnD :: Liftable () a => a -> Dist a
125+returnD = D . returnM
145126
146127-- | Monad instance '(>>=)' function
147128bindD :: Dist a -> Matrix Prob a b -> Dist b
@@ -162,7 +143,7 @@ bindD (D d) m = D (m `comp` d)
162143
163144-- | Constructs a Bernoulli distribution
164145choose :: (FromListsN () a) => Prob -> Dist a
165−choose prob = D (col [prob, 1 - prob])
146+choose prob = D (colL [prob, 1 - prob])
166147
167148-- | Creates a distribution given a shape function
168149shape :: (FromListsN () a) => (Prob -> Prob) -> [a] -> Dist a
@@ -215,7 +196,7 @@ prettyPrintDist = putStrLn . prettyDist @a
215196-- Auxiliary functions
216197
217198fromFreqs :: (FromListsN () a) => [(a,Prob)] -> Dist a
218−fromFreqs xs = D (col (map (\(x,p) -> p/q) xs))
199+fromFreqs xs = D (colL (map (\(x,p) -> p/q) xs))
219200 where q = sum $ map snd xs
220201
221202normalCurve :: Prob -> Prob -> Prob -> Prob