@@ -81,7 +81,8 @@ module LAoP.Matrix.Internal
8181 tr,
8282
8383 -- ** Selective operator
84− select,
84+ select,
85+ branch,
8586
8687 -- ** McCarthy's Conditional
8788 cond,
@@ -313,7 +314,7 @@ infixl 2 ===
313314-- | Type class for defining the 'fromList' conversion function.
314315--
315316-- 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.
317318class FromLists e cols rows where
318319 -- | Build a matrix out of a list of list of elements. Throws a runtime
319320 -- error if the dimensions do not match.
@@ -689,7 +690,32 @@ tr (Split a b) = Junc (tr a) (tr b)
689690-- | Selective functors 'select' operator equivalent inspired by the
690691-- ArrowMonad solution presented in the paper.
691692select :: (Num e, FromLists e b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
692−select m y = junc y identity . m
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
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
693719
694720-- McCarthy's Conditional
695721