@@ -81,7 +81,8 @@ module LAoP.Matrix.Internal
83 -- ** Selective operator
83 -- ** Selective operator
86 -- ** McCarthy's Conditional
87 -- ** McCarthy's Conditional
@@ -313,7 +314,7 @@ infixl 2 ===
313-- | Type class for defining the 'fromList' conversion function.
314-- | Type class for defining the 'fromList' conversion function.
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
699+ CountableDimensions a b,
700+ CountableDimensions c (Either b c),
708+ FromLists e (Either b c) b,
709+ FromLists e (Either b c) c
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
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)
694-- McCarthy's Conditional
720-- McCarthy's Conditional