@@ -87,10 +87,9 @@ module LAoP.Matrix.Type
8787 fromLists,
8888 toLists,
8989 toList,
90− matrixBuilder',
9190 matrixBuilder,
92− row,
93− col,
91+ rowL,
92+ colL,
9493 zeros,
9594 ones,
9695 bang,
@@ -125,7 +124,7 @@ module LAoP.Matrix.Type
125124 (./),
126125
127126 -- ** McCarthy's Conditional
128− cond,
127+ -- cond,
129128
130129 -- ** Matrix "abiding"
131130 abideJF,
@@ -168,7 +167,6 @@ module LAoP.Matrix.Type
168167 -- implement the standard type classes present in standard Haskell.
169168 iden,
170169 comp,
171− fromF',
172170 fromF,
173171
174172 -- * Relation
@@ -181,12 +179,11 @@ module LAoP.Matrix.Type
181179where
182180
183181import Data.Void
184−import Data.Proxy
185182import Data.Kind
186183import GHC.TypeLits
187184import Control.DeepSeq
188185import LAoP.Utils
189−import qualified LAoP.Matrix.Internal as I
186+import qualified LAoP.Matrix.Alternative as I
190187import Prelude hiding (id, (.))
191188
192189newtype Matrix e (cols :: Type) (rows :: Type) = M (I.Matrix e (I.Normalize cols) (I.Normalize rows))
@@ -198,27 +195,30 @@ type CountableDimensions a b = (Countable a, Countable b)
198195type CountableN a = KnownNat (I.Count (I.Normalize a))
199196type CountableDimensionsN a b = (CountableN a, CountableN b)
200197type FromListsN e a b = I.FromLists e (I.Normalize a) (I.Normalize b)
201−type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
198+type Liftable a b = (Eq b, Enumerable a, Enum a, EnumN a, Enum b, EnumN b, ConstructableN a b)
202199type Trivial a = I.Normalize (I.Normalize a) ~ I.Normalize (I.Normalize (I.Normalize a))
203200type Trivial2 a = I.Normalize a ~ I.Normalize (I.Normalize a)
204−type Trivial3 a = I.FromNat (I.Count (I.Normalize (I.Normalize a))) ~ I.Normalize (I.Normalize a)
205201type TrivialP a b = I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
202+type EqN a = Eq (I.Normalize a)
203+type EnumN a = Enum (I.Normalize a)
204+type Enumerable a = (Enum a, Bounded a)
205+type EnumerableN a = (Enum (I.Normalize a), Bounded (I.Normalize a))
206+type ConstructNorm a = (Enum a, Enum (I.Normalize a))
207+type ConstructN a = I.Construct (I.Normalize a)
208+type ConstructableN a b = (I.Construct (I.Normalize a), I.Construct (I.Normalize b))
206209
207210-- | It is possible to implement a constrained version of the category type
208211-- class.
209212instance (Num e) => Category (Matrix e) where
210− type Object (Matrix e) a = (FromListsN e a a, CountableN a)
213+ type Object (Matrix e) a = (Num e, EqN a, ConstructN a, EnumerableN a)
211214 id = iden
212215 (.) = comp
213216
214217-- | Bifunctor equivalent function
215218bimapM ::
216− ( Liftable e a b,
217− Liftable e c d,
218− CountableDimensionsN a c,
219− CountableDimensionsN b d,
220− FromListsN e d c,
221− FromListsN e b a
219+ ( Num e,
220+ Liftable a b,
221+ Liftable c d
222222 ) => (a -> b) -> (c -> d) -> Matrix e a c -> Matrix e b d
223223bimapM f g m = fromF g . m . tr (fromF f)
224224
@@ -272,9 +272,8 @@ infixl 2 ===
272272
273273-- | Functor instance equivalent function
274274fmapM ::
275− ( Liftable e a b,
276− CountableDimensionsN a b,
277− FromListsN e b a
275+ ( Num e,
276+ Liftable a b
278277 )
279278 =>
280279 (a -> b) -> Matrix e c a -> Matrix e c b
@@ -286,29 +285,18 @@ unitM = one 1
286285
287286-- | Applicative instance equivalent 'unit' function,
288287multM ::
289− ( CountableDimensionsN a b,
290− CountableN (a, b),
291− Num e,
292− FromListsN e (a, b) a,
293− FromListsN e (a, b) b,
294− TrivialP a b
288+ ( Num e,
289+ FstM a b,
290+ SndM a b
295291 ) => Matrix e c a -> Matrix e c b -> Matrix e c (a, b)
296292multM = kr
297293
298294-- | Monad instance equivalent 'return' function,
299−returnM ::
300− forall e a .
295+returnM ::
301296 ( Num e,
302− Enum e,
303− Enum a,
304− FromListsN e () a,
305− Countable a
297+ Liftable () a
306298 ) => a -> Matrix e One a
307−returnM a = col l
308− where
309− i = fromInteger $ natVal (Proxy :: Proxy (I.Count a))
310− x = fromEnum a
311− l = take x [0,0..] ++ [1] ++ take (i - x - 1) [0,0..]
299+returnM = point
312300
313301-- | Monad instance equivalent '(>>=)' function,
314302bindM :: (Num e) => Matrix e a b -> Matrix e b c -> Matrix e a c
@@ -321,63 +309,39 @@ bindM = flip comp
321309fromLists :: (FromListsN e cols rows) => [[e]] -> Matrix e cols rows
322310fromLists = M . I.fromLists
323311
324−-- | Matrix builder function. Constructs a matrix provided with
325−-- a construction function that operates with indices.
326−matrixBuilder' ::
327− (FromListsN e cols rows, CountableDimensionsN cols rows )
328− => ((Int, Int) -> e) -> Matrix e cols rows
329−matrixBuilder' = M . I.matrixBuilder'
330−
331312-- | Matrix builder function. Constructs a matrix provided with
332313-- a construction function that operates with arbitrary types.
333314matrixBuilder ::
334− ( FromListsN e a b,
335− Enum a,
336− Enum b,
337− Bounded a,
338− Bounded b,
339− Eq a,
340− CountableDimensionsN a b
341− ) => ((a, b) -> e) -> Matrix e a b
342−matrixBuilder f = M (I.matrixBuilder f)
315+ ( Num e,
316+ Enumerable a,
317+ Enum b,
318+ EnumN a,
319+ EnumN b,
320+ ConstructableN a b
321+ )
322+ => ((a, b) -> e) -> Matrix e a b
323+matrixBuilder = M . I.matrixBuilderN
343324
344325-- | Constructs a column vector matrix
345−col :: (FromListsN e () rows) => [e] -> Matrix e One rows
346−col = M . I.col
326+colL :: (FromListsN e () rows) => [e] -> Matrix e One rows
327+colL = M . I.colL
347328
348329-- | Constructs a row vector matrix
349−row :: (FromListsN e cols ()) => [e] -> Matrix e cols One
350−row = M . I.row
351−
352−-- | Lifts functions to matrices with arbitrary dimensions.
353−--
354−-- NOTE: Be careful to not ask for a matrix bigger than the cardinality of
355−-- types @a@ or @b@ allows.
356−fromF' ::
357− ( Liftable e a b,
358− CountableDimensionsN cols rows,
359− FromListsN e rows cols
360− ) =>
361− (a -> b) -> Matrix e cols rows
362−fromF' = M . I.fromF'
330+rowL :: (FromListsN e cols ()) => [e] -> Matrix e cols One
331+rowL = M . I.rowL
363332
364333-- | Lifts functions to matrices with dimensions matching @a@ and @b@
365334-- cardinality's.
366335fromF ::
367− ( Liftable e a b,
368− CountableDimensionsN a b,
369− FromListsN e b a
370− ) =>
371− (a -> b) -> Matrix e a b
372−fromF = M . I.fromF
336+ ( Num e,
337+ Liftable a b
338+ )
339+ => (a -> b) -> Matrix e a b
340+fromF = M . I.fromFN
373341
374342-- | Lifts relation functions to Boolean Matrix
375−toRel ::
376− ( Liftable (Natural 0 1) a b,
377− CountableDimensionsN a b,
378− FromListsN (Natural 0 1) b a
379− ) => (a -> b -> Bool) -> Matrix (Natural 0 1) a b
380−toRel = M . I.toRel
343+toRel :: Liftable a b => (a -> b -> Bool) -> Matrix (Natural 0 1) a b
344+toRel = M . I.toRelN
381345
382346-- Conversion
383347
@@ -393,8 +357,11 @@ toList (M m) = I.toList m
393357
394358-- | The zero matrix. A matrix wholly filled with zeros.
395359zeros ::
396− (Num e, FromListsN e cols rows, CountableDimensionsN cols rows)
397− => Matrix e cols rows
360+ ( Num e,
361+ ConstructableN cols rows,
362+ EnumerableN cols
363+ )
364+ => Matrix e cols rows
398365zeros = M I.zeros
399366
400367-- Ones Matrix
@@ -403,8 +370,11 @@ zeros = M I.zeros
403370--
404371-- Also known as T (Top) matrix.
405372ones ::
406− (Num e, FromListsN e cols rows, CountableDimensionsN cols rows)
407− => Matrix e cols rows
373+ ( Num e,
374+ ConstructableN cols rows,
375+ EnumerableN cols
376+ )
377+ => Matrix e cols rows
408378ones = M I.ones
409379
410380-- Const Matrix
@@ -412,28 +382,28 @@ ones = M I.ones
412382-- | The constant matrix constructor. A matrix wholly filled with a given
413383-- value.
414384constant ::
415− (Num e, FromListsN e cols rows, CountableDimensionsN cols rows)
416− => e -> Matrix e cols rows
385+ ( Num e,
386+ ConstructableN cols rows,
387+ EnumerableN cols
388+ )
389+ => e -> Matrix e cols rows
417390constant = M . I.constant
418391
419392-- Bang Matrix
420393
421394-- | The T (Top) row vector matrix.
422395bang ::
423− forall e cols.
424− (Num e, Enum e, FromListsN e cols (), CountableN cols) =>
425− Matrix e cols One
426−bang = M I.bang
396+ ( Num e,
397+ ConstructN cols,
398+ EnumerableN cols
399+ )
400+ => Matrix e cols One
401+bang = ones
427402
428403-- | Point constant matrix
429−point ::
430− ( Bounded a,
431− Enum a,
432− Eq a,
433− Num e,
434− Ord e,
435− CountableN a,
436− FromListsN e a One
404+point ::
405+ ( Num e,
406+ Liftable () a
437407 ) => a -> Matrix e One a
438408point = fromF . const
439409
@@ -441,8 +411,12 @@ point = fromF . const
441411
442412-- | iden matrix
443413iden ::
444− (Num e, FromListsN e a a, CountableN a) =>
445− Matrix e a a
414+ ( Num e,
415+ EqN a,
416+ ConstructN a,
417+ EnumerableN a
418+ )
419+ => Matrix e a a
446420iden = M I.iden
447421{-# NOINLINE iden #-}
448422
@@ -476,44 +450,48 @@ infixl 7 ./
476450
477451-- | Biproduct first component projection
478452p1 ::
479− ( Num e,
480− CountableDimensionsN n m,
481− FromListsN e n m,
482− FromListsN e m m
483− ) =>
484− Matrix e (Either m n) m
453+ ( Num e,
454+ EqN m,
455+ ConstructableN m n,
456+ EnumerableN m,
457+ EnumerableN n
458+ )
459+ => Matrix e (Either m n) m
485460p1 = M I.p1
486461
487462-- | Biproduct second component projection
488463p2 ::
489− ( Num e,
490− CountableDimensionsN n m,
491− FromListsN e m n,
492− FromListsN e n n
493− ) =>
494− Matrix e (Either m n) n
464+ ( Num e,
465+ EqN n,
466+ ConstructableN m n,
467+ EnumerableN m,
468+ EnumerableN n
469+ )
470+ => Matrix e (Either m n) n
495471p2 = M I.p2
496472
497473-- Injections
498474
499475-- | Biproduct first component injection
500476i1 ::
501− ( Num e,
502− CountableDimensionsN n m,
503− FromListsN e n m,
504− FromListsN e m m
505− ) =>
506− Matrix e m (Either m n)
477+ ( Num e,
478+ EqN m,
479+ ConstructableN m n,
480+ EnumerableN m,
481+ EnumerableN n
482+ )
483+ => Matrix e m (Either m n)
507484i1 = tr p1
508485
509486-- | Biproduct second component injection
510487i2 ::
511− ( Num e,
512− CountableDimensionsN n m,
513− FromListsN e m n,
514− FromListsN e n n
515− ) =>
516− Matrix e n (Either m n)
488+ ( Num e,
489+ EqN n,
490+ ConstructableN m n,
491+ EnumerableN m,
492+ EnumerableN n
493+ )
494+ => Matrix e n (Either m n)
517495i2 = tr p2
518496
519497-- Dimensions
@@ -525,7 +503,7 @@ i2 = tr p2
525503--
526504-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
527505-- exchange for performance.
528−rows :: (CountableN rows) => Matrix e cols rows -> Int
506+rows :: CountableN rows => Matrix e cols rows -> Int
529507rows (M m) = I.rows m
530508
531509-- | Obtain the number of columns.
@@ -535,7 +513,7 @@ rows (M m) = I.rows m
535513--
536514-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
537515-- exchange for performance.
538−columns :: (CountableN cols) => Matrix e cols rows -> Int
516+columns :: CountableN cols => Matrix e cols rows -> Int
539517columns (M m) = I.columns m
540518
541519-- Coproduct Bifunctor
@@ -544,41 +522,53 @@ infixl 5 -|-
544522
545523-- | Matrix coproduct functor also known as matrix direct sum.
546524(-|-) ::
547− ( Num e,
548− CountableDimensionsN j k,
549− FromListsN e k k,
550− FromListsN e j k,
551− FromListsN e k j,
552− FromListsN e j j
553− ) =>
554− Matrix e n k ->
555− Matrix e m j ->
556− Matrix e (Either n m) (Either k j)
525+ ( Num e,
526+ EqN k,
527+ EqN j,
528+ ConstructableN k j,
529+ EnumerableN k,
530+ EnumerableN j
531+ ) => Matrix e n k -> Matrix e m j -> Matrix e (Either n m) (Either k j)
557532(-|-) (M a) (M b) = M ((I.-|-) a b)
558533
559534-- Khatri Rao Product and projections
560535
561−-- | Khatri Rao product first component projection matrix.
562−fstM ::
563− forall e m k .
564− ( Num e,
565− CountableDimensionsN m k,
566− CountableN (m, k),
567− FromListsN e (m, k) m,
568− TrivialP m k
569− ) => Matrix e (m, k) m
570−fstM = M (I.fstM @e @(I.Normalize m) @(I.Normalize k))
536+type FstM a b =
537+ ( EqN a,
538+ ConstructableN (I.Normalize a) (I.Normalize (a, b)),
539+ ConstructN (I.Normalize a, I.Normalize b),
540+ EnumerableN a,
541+ EnumerableN b,
542+ EnumN (I.Normalize a, I.Normalize b),
543+ Trivial2 a,
544+ TrivialP a b
545+ )
571546
547+-- | Khatri Rao product first component projection matrix.
548+fstM ::
549+ forall e a b .
550+ ( Num e,
551+ FstM a b
552+ ) => Matrix e (a, b) a
553+fstM = M (I.fstM @e @(I.Normalize a) @(I.Normalize b))
554+
555+type SndM a b =
556+ ( EqN b,
557+ ConstructableN (I.Normalize b) (I.Normalize (a, b)),
558+ ConstructN (I.Normalize a, I.Normalize b),
559+ EnumerableN a,
560+ EnumerableN b,
561+ EnumN (I.Normalize a, I.Normalize b),
562+ Trivial2 b,
563+ TrivialP a b
564+ )
572565-- | Khatri Rao product second component projection matrix.
573−sndM ::
574− forall e m k.
575− ( Num e,
576− CountableDimensionsN k m,
577− CountableN (m, k),
578− FromListsN e (m, k) k,
579− TrivialP m k
580− ) => Matrix e (m, k) k
581−sndM = M (I.sndM @e @(I.Normalize m) @(I.Normalize k))
566+sndM ::
567+ forall e a b.
568+ ( Num e,
569+ SndM a b
570+ ) => Matrix e (a, b) b
571+sndM = M (I.sndM @e @(I.Normalize a) @(I.Normalize b))
582572
583573-- | Khatri Rao Matrix product also known as matrix pairing.
584574--
@@ -592,14 +582,12 @@ sndM = M (I.sndM @e @(I.Normalize m) @(I.Normalize k))
592582--
593583-- __Emphasis__ on the implication symbol.
594584kr ::
595− forall e cols a b.
596− ( Num e,
597− CountableDimensionsN a b,
598− CountableN (a, b),
599− FromListsN e (a, b) a,
600− FromListsN e (a, b) b,
601− TrivialP a b
602− ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (a, b)
585+ forall e cols a b.
586+ ( Num e,
587+ FstM a b,
588+ SndM a b
589+ )
590+ => Matrix e cols a -> Matrix e cols b -> Matrix e cols (a, b)
603591kr a b =
604592 let fstM' = fstM @e @a @b
605593 sndM' = sndM @e @a @b
@@ -608,21 +596,38 @@ kr a b =
608596-- Product Bifunctor (Kronecker)
609597
610598infixl 4 ><
599+type Kronecker m n p q =
600+ ( EqN (I.Normalize m),
601+ EqN (I.Normalize n),
602+ EqN (I.Normalize p),
603+ EqN (I.Normalize q),
604+ ConstructableN (I.Normalize m) (I.Normalize n),
605+ ConstructableN (I.Normalize p) (I.Normalize q),
606+ ConstructableN (I.Normalize m, I.Normalize n) (I.Normalize p, I.Normalize q),
607+ ConstructableN (I.Normalize (I.Normalize m, I.Normalize n)) (I.Normalize (I.Normalize p, I.Normalize q)),
608+ FstM m n,
609+ SndM m n,
610+ EnumerableN (I.Normalize m),
611+ EnumerableN (I.Normalize n),
612+ EnumerableN (I.Normalize p),
613+ EnumerableN (I.Normalize q),
614+ EnumerableN (I.Normalize m, I.Normalize n),
615+ EnumerableN (I.Normalize p, I.Normalize q),
616+ Trivial2 m,
617+ Trivial2 n,
618+ Trivial2 p,
619+ Trivial2 q,
620+ TrivialP p q,
621+ TrivialP m m
622+ )
611623
612624-- | Matrix product functor also known as Kronecker product
613625(><) ::
614− forall e m p n q.
615− ( Num e,
616− CountableDimensionsN m n,
617− CountableDimensionsN p q,
618− CountableDimensionsN (m, n) (p, q),
619− FromListsN e (m, n) m,
620− FromListsN e (m, n) n,
621− FromListsN e (p, q) p,
622− FromListsN e (p, q) q,
623− TrivialP m n,
624− TrivialP p q
625− ) => Matrix e m p -> Matrix e n q -> Matrix e (m, n) (p, q)
626+ forall e m p n q.
627+ ( Num e,
628+ Kronecker m n p q
629+ )
630+ => Matrix e m p -> Matrix e n q -> Matrix e (m, n) (p, q)
626631(><) a b =
627632 let fstM' = fstM @e @m @n
628633 sndM' = sndM @e @m @n
@@ -662,34 +667,23 @@ tr (M m) = M (I.tr m)
662667
663668-- | Selective functors 'select' operator equivalent inspired by the
664669-- ArrowMonad solution presented in the paper.
665−selectM ::
666− ( Num e,
667− FromListsN e b b,
668− CountableN b
669− ) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
670+selectM :: (Num e, EqN b, ConstructN b, EnumerableN b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
670671selectM (M m) (M y) = M (I.select m y)
671672
672673-- McCarthy's Conditional
673674
674675-- | McCarthy's Conditional expresses probabilistic choice.
675−cond ::
676− ( Trivial a,
677− Trivial2 a,
678− Trivial3 a,
679− CountableN a,
680− FromListsN e () a,
681− FromListsN e a (),
682− FromListsN e a a,
683− Liftable e a Bool
684− )
685− =>
686− (a -> Bool) -> Matrix e a b -> Matrix e a b -> Matrix e a b
687−cond p (M a) (M b) = M (I.cond p a b)
676+-- cond ::
677+-- (
678+-- )
679+-- =>
680+-- (a -> Bool) -> Matrix e a b -> Matrix e a b -> Matrix e a b
681+-- cond p (M a) (M b) =
688682
689683-- Pretty print
690684
691685-- | Matrix pretty printer
692−pretty :: (CountableDimensionsN cols rows, Show e) => Matrix e cols rows -> String
686+pretty :: (Show e, CountableDimensionsN cols rows) => Matrix e cols rows -> String
693687pretty (M m) = I.pretty m
694688
695689-- | Matrix pretty printer