on master help sign in
Optimize Select
eb22ee6Armando Santos
src/LAoP/Dist.hs+1
@@ -54,6 +54,7 @@ module LAoP.Dist
54
54
55 -- * Selective equivalent functions
55 -- * Selective equivalent functions
56 selectD,
56 selectD,
57+ selectD2,
57
58
58 -- * Monad equivalent functions
59 -- * Monad equivalent functions
59 returnD,
60 returnD,
src/LAoP/Dist/Internal.hs+9−2
@@ -25,6 +25,7 @@ module LAoP.Dist.Internal
25 unitD,
25 unitD,
26 multD,
26 multD,
27 selectD,
27 selectD,
28+ selectD2,
28 returnD,
29 returnD,
29 bindD,
30 bindD,
30 (??),
31 (??),
@@ -49,7 +50,7 @@ import Data.Proxy
49import Data.List (sortBy)
50import Data.List (sortBy)
50
51
51-- | Type synonym for probability value
52-- | Type synonym for probability value
52−type Prob = Double
53+type Prob = Float
53
54
54-- | Type synonym for column vector matrices. This represents a probability
55-- | Type synonym for column vector matrices. This represents a probability
55-- distribution.
56-- distribution.
@@ -94,8 +95,14 @@ selectD ::
94 FromListsN b b,
95 FromListsN b b,
95 CountableN b
96 CountableN b
96 ) => Dist (Either a b) -> Matrix Prob a b -> Dist b
97 ) => Dist (Either a b) -> Matrix Prob a b -> Dist b
97−selectD (D d) m = D (junc m identity `comp` d)
98+selectD (D d) m = D (selectM d m)
98
99
100+selectD2 ::
101+ ( TrivialE a b,
102+ FromListsN b b,
103+ CountableN b
104+ ) => Dist (Either a b) -> Matrix Prob a b -> Dist b
105+selectD2 (D d) m = D (junc m identity `comp` d)
99
106
100-- | Monad instance 'return' function
107-- | Monad instance 'return' function
101returnD :: forall a . (Enum a, FromListsN () a, Countable a) => a -> Dist a
108returnD :: forall a . (Enum a, FromListsN () a, Countable a) => a -> Dist a
src/LAoP/Matrix/Internal.hs+29−3
@@ -81,7 +81,8 @@ module LAoP.Matrix.Internal
81 tr,
81 tr,
82
82
83 -- ** Selective operator
83 -- ** Selective operator
84− select,
84+ select,
85+ branch,
85
86
86 -- ** McCarthy's Conditional
87 -- ** McCarthy's Conditional
87 cond,
88 cond,
@@ -313,7 +314,7 @@ infixl 2 ===
313-- | Type class for defining the 'fromList' conversion function.
314-- | Type class for defining the 'fromList' conversion function.
314--
315--
315-- Given that it is not possible to branch on types at the term level type
316-- Given that it is not possible to branch on types at the term level type
316−-- classes are needed bery much like an inductive definition but on types.
317+-- classes are needed very much like an inductive definition but on types.
317class FromLists e cols rows where
318class FromLists e cols rows where
318 -- | Build a matrix out of a list of list of elements. Throws a runtime
319 -- | Build a matrix out of a list of list of elements. Throws a runtime
319 -- error if the dimensions do not match.
320 -- error if the dimensions do not match.
@@ -689,7 +690,32 @@ tr (Split a b) = Junc (tr a) (tr b)
689-- | Selective functors 'select' operator equivalent inspired by the
690-- | Selective functors 'select' operator equivalent inspired by the
690-- ArrowMonad solution presented in the paper.
691-- ArrowMonad solution presented in the paper.
691select :: (Num e, FromLists e b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
692select :: (Num e, FromLists e b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
693+select (Split a b) y = y . a + b -- Divide-and-conquer law
694+select (Junc (Split a c) (Split b d)) y = junc (y . a + c) (y . b + d) -- Pattern matching + DnC law
692−select m y = junc y identity . m
695+select m y = junc y identity . m
696+
697+branch ::
698+ ( Num e,
699+ CountableDimensions a b,
700+ CountableDimensions c (Either b c),
701+ FromLists e c b,
702+ FromLists e a b,
703+ FromLists e a a,
704+ FromLists e b b,
705+ FromLists e c c,
706+ FromLists e b a,
707+ FromLists e b c,
708+ FromLists e (Either b c) b,
709+ FromLists e (Either b c) c
710+ )
711+ => Matrix e cols (Either a b) -> Matrix e a c -> Matrix e b c -> Matrix e cols c
712+branch x l r = f x `select` g l `select` r
713+ where
714+ f :: (Num e, Countable a, CountableDimensions b c, FromLists e a b, FromLists e c b, FromLists e b b, FromLists e b a, FromLists e a a)
715+ => Matrix e cols (Either a b) -> Matrix e cols (Either a (Either b c))
716+ f m = split (tr i1) (i1 . tr i2) . m
717+ g :: (Num e, CountableDimensions b c, FromLists e b c, FromLists e c c) => Matrix e a c -> Matrix e a (Either b c)
718+ g m = i2 . m
693
719
694-- McCarthy's Conditional
720-- McCarthy's Conditional
695
721