@@ -30,7 +30,7 @@
3030-- motivation behind the library, the underlying theory, and implementation details.
3131--
3232-- This module offers many of the combinators mentioned in the work of
33−-- Macedo (2012) and Oliveira (2012).
33+-- Macedo (2012) and Oliveira (2012).
3434--
3535-- This is an Internal module and it is no supposed to be imported.
3636--
@@ -43,23 +43,14 @@ module LAoP.Matrix.Internal
4343 --
4444 -- There exists two type families that make it easier to write
4545 -- matrix dimensions: 'FromNat' and 'Count'. This approach
46− -- leads to a very straightforward implementation
47− -- of LAoP combinators.
46+ -- leads to a very straightforward implementation
47+ -- of LAoP combinators.
4848
4949 -- * Type safe matrix representation
5050 Matrix (..),
5151
52− -- * Constraint type aliases
53− Countable,
54− CountableDims,
55− CountableN,
56− CountableDimsN,
57− FL,
58− FLN,
59− Liftable,
60− Trivial,
61−
6252 -- * Primitives
53+ empty,
6354 one,
6455 join,
6556 fork,
@@ -86,9 +77,7 @@ module LAoP.Matrix.Internal
8677 -- * Misc
8778 -- ** Get dimensions
8879 columns,
89− columns',
9080 rows,
91− rows',
9281
9382 -- ** Matrix Transposition
9483 tr,
@@ -176,7 +165,6 @@ import Data.Kind
176165import Data.List
177166import Data.Maybe
178167import Data.Proxy
179−import Data.Typeable
180168import Data.Void
181169import GHC.TypeLits
182170import Data.Type.Equality
@@ -187,6 +175,7 @@ import qualified Prelude (id, (.))
187175
188176-- | LAoP (Linear Algebra of Programming) Inductive Matrix definition.
189177data Matrix e cols rows where
178+ Empty :: Matrix e Void Void
190179 One :: e -> Matrix e () ()
191180 Join :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
192181 Fork :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
@@ -216,6 +205,7 @@ type family Count (d :: Type) :: Nat where
216205--
217206-- Thanks to Li-Yao Xia this type family is super fast.
218207type family FromNat (n :: Nat) :: Type where
208+ FromNat 0 = Void
219209 FromNat 1 = ()
220210 FromNat n = FromNat' (Mod n 2 == 0) (FromNat (Div n 2))
221211
@@ -232,26 +222,27 @@ type family Normalize (d :: Type) :: Type where
232222-- | Constraint type synonyms to keep the type signatures less convoluted
233223type Countable a = KnownNat (Count a)
234224type CountableN a = KnownNat (Count (Normalize a))
235−type CountableDims a b = (Countable a, Countable b)
236−type CountableDimsN a b = (CountableN a, CountableN b)
237−type FL a b = FromLists a b
238−type FLN a b = FromLists (Normalize a) (Normalize b)
225+type CountableDimensions a b = (Countable a, Countable b)
226+type CountableDimensionsN a b = (CountableN a, CountableN b)
227+type FromListsN e a b = FromLists e (Normalize a) (Normalize b)
239228type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
240229type Trivial a = FromNat (Count a) ~ a
241230
242231-- | It is possible to implement a constrained version of the category type
243232-- class.
244233instance (Num e) => Category (Matrix e) where
245− type Object (Matrix e) a = (FL a a, Countable a)
246− id = iden
234+ type Object (Matrix e) a = (FromLists e a a, Countable a)
235+ id = iden
247236 (.) = comp
248237
249238instance NFData e => NFData (Matrix e cols rows) where
239+ rnf Empty = ()
250240 rnf (One e) = rnf e
251241 rnf (Join a b) = rnf a `seq` rnf b
252242 rnf (Fork a b) = rnf a `seq` rnf b
253243
254244instance Eq e => Eq (Matrix e cols rows) where
245+ Empty == Empty = True
255246 (One a) == (One b) = a == b
256247 (Join a b) == (Join c d) = a == c && b == d
257248 (Fork a b) == (Fork c d) = a == c && b == d
@@ -259,21 +250,25 @@ instance Eq e => Eq (Matrix e cols rows) where
259250 x@(Join _ _) == y@(Fork _ _) = abideJF x == y
260251
261252instance Num e => Num (Matrix e cols rows) where
253+
262254 a + b = zipWithM (+) a b
263255
264256 a - b = zipWithM (-) a b
265257
266258 a * b = zipWithM (*) a b
267259
260+ abs Empty = Empty
268261 abs (One a) = One (abs a)
269262 abs (Join a b) = Join (abs a) (abs b)
270263 abs (Fork a b) = Fork (abs a) (abs b)
271264
265+ signum Empty = Empty
272266 signum (One a) = One (signum a)
273267 signum (Join a b) = Join (signum a) (signum b)
274268 signum (Fork a b) = Fork (signum a) (signum b)
275269
276270instance Ord e => Ord (Matrix e cols rows) where
271+ Empty <= Empty = True
277272 (One a) <= (One b) = a <= b
278273 (Join a b) <= (Join c d) = (a <= c) && (b <= d)
279274 (Fork a b) <= (Fork c d) = (a <= c) && (b <= d)
@@ -282,6 +277,10 @@ instance Ord e => Ord (Matrix e cols rows) where
282277
283278-- Primitives
284279
280+-- | Empty matrix constructor
281+empty :: Matrix e Void Void
282+empty = Empty
283+
285284-- | Unit matrix constructor
286285one :: e -> Matrix e () ()
287286one = One
@@ -312,39 +311,40 @@ infixl 2 ===
312311--
313312-- Given that it is not possible to branch on types at the term level type
314313-- classes are needed very much like an inductive definition but on types.
315−class FromLists cols rows where
314+class FromLists e cols rows where
316315 -- | Build a matrix out of a list of list of elements. Throws a runtime
317316 -- error if the dimensions do not match.
318317 fromLists :: [[e]] -> Matrix e cols rows
319318
320−instance FromLists () () where
319+instance FromLists e Void Void where
320+ fromLists [] = Empty
321+ fromLists _ = error "Wrong dimensions"
322+
323+instance {-# OVERLAPPING #-} FromLists e () () where
321324 fromLists [[e]] = One e
322325 fromLists _ = error "Wrong dimensions"
323326
324−instance (FromLists cols ()) => FromLists (Either () cols) () where
327+instance {-# OVERLAPPING #-} (FromLists e cols ()) => FromLists e (Either () cols) () where
325328 fromLists [h : t] = Join (One h) (fromLists [t])
326329 fromLists _ = error "Wrong dimensions"
327330
328−instance {-# OVERLAPPABLE #-}
329− (FromLists a (), FromLists b (), Countable a) => FromLists (Either a b) () where
331+instance {-# OVERLAPPABLE #-} (FromLists e a (), FromLists e b (), Countable a) => FromLists e (Either a b) () where
330332 fromLists [l] =
331333 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
332334 in Join (fromLists [take rowsA l]) (fromLists [drop rowsA l])
333335 fromLists _ = error "Wrong dimensions"
334336
335−instance (FromLists () rows) => FromLists () (Either () rows) where
337+instance {-# OVERLAPPING #-} (FromLists e () rows) => FromLists e () (Either () rows) where
336338 fromLists ([h] : t) = Fork (One h) (fromLists t)
337339 fromLists _ = error "Wrong dimensions"
338340
339−instance {-# OVERLAPPABLE #-}
340− (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
341− fromLists l@([_] : _) =
341+instance {-# OVERLAPPABLE #-} (FromLists e () a, FromLists e () b, Countable a) => FromLists e () (Either a b) where
342+ fromLists l@([_] : _) =
342343 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
343344 in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
344345 fromLists _ = error "Wrong dimensions"
345346
346−instance (FromLists (Either a b) c, FromLists (Either a b) d, Countable c)
347− => FromLists (Either a b) (Either c d) where
347+instance {-# OVERLAPPABLE #-} (FromLists e (Either a b) c, FromLists e (Either a b) d, Countable c) => FromLists e (Either a b) (Either c d) where
348348 fromLists l@(h : t) =
349349 let lh = length h
350350 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
@@ -357,8 +357,8 @@ instance (FromLists (Either a b) c, FromLists (Either a b) d, Countable c)
357357-- a construction function that operates with indices.
358358matrixBuilder' ::
359359 forall e cols rows.
360− ( FL cols rows,
361− CountableDims cols rows
360+ ( FromLists e cols rows,
361+ CountableDimensions cols rows
362362 ) => ((Int, Int) -> e) -> Matrix e cols rows
363363matrixBuilder' f =
364364 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
@@ -370,16 +370,17 @@ matrixBuilder' f =
370370-- a construction function that operates with arbitrary types.
371371matrixBuilder ::
372372 forall e a b.
373− ( FLN a b,
373+ ( FromListsN e a b,
374+ CountableN b,
374375 Enum a,
375376 Enum b,
376377 Bounded a,
377378 Bounded b,
378379 Eq a,
379− Countable b
380+ CountableDimensionsN a b
380381 ) => ((a, b) -> e) -> Matrix e (Normalize a) (Normalize b)
381382matrixBuilder f =
382− let r = fromInteger $ natVal (Proxy :: Proxy (Count b))
383+ let r = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
383384 positions = [(a, b) | a <- [minBound .. maxBound], b <- [minBound .. maxBound]]
384385 in fromLists . map (map f) . transpose . buildList r $ positions
385386 where
@@ -387,11 +388,11 @@ matrixBuilder f =
387388 buildList r l = take r l : buildList r (drop r l)
388389
389390-- | Constructs a column vector matrix
390−col :: (FL () rows) => [e] -> Matrix e () rows
391+col :: (FromLists e () rows) => [e] -> Matrix e () rows
391392col = fromLists . map (: [])
392393
393394-- | Constructs a row vector matrix
394−row :: (FL cols ()) => [e] -> Matrix e cols ()
395+row :: (FromLists e cols ()) => [e] -> Matrix e cols ()
395396row = fromLists . (: [])
396397
397398-- | Lifts functions to matrices with arbitrary dimensions.
@@ -401,8 +402,8 @@ row = fromLists . (: [])
401402fromF' ::
402403 forall a b cols rows e.
403404 ( Liftable e a b,
404− CountableDims cols rows,
405− FL rows cols
405+ CountableDimensions cols rows,
406+ FromLists e rows cols
406407 ) =>
407408 (a -> b) ->
408409 Matrix e cols rows
@@ -430,17 +431,35 @@ fromF' f =
430431fromF ::
431432 forall a b e.
432433 ( Liftable e a b,
433− CountableDimsN a b,
434− FLN b a
434+ CountableDimensionsN a b,
435+ FromListsN e b a
435436 ) =>
436437 (a -> b) ->
437438 Matrix e (Normalize a) (Normalize b)
438−fromF = fromF'
439+fromF f =
440+ let minA = minBound @a
441+ maxA = maxBound @a
442+ minB = minBound @b
443+ maxB = maxBound @b
444+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
445+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
446+ elementsA = take ccols [minA .. maxA]
447+ elementsB = take rrows [minB .. maxB]
448+ combinations = (,) <$> elementsA <*> elementsB
449+ combAp = map snd . sort . map (\(a, b) -> if f a == b
450+ then ((fromEnum a, fromEnum b), 1)
451+ else ((fromEnum a, fromEnum b), 0)) $ combinations
452+ mList = buildList combAp rrows
453+ in tr $ fromLists mList
454+ where
455+ buildList [] _ = []
456+ buildList l r = take r l : buildList (drop r l) r
439457
440458-- Conversion
441459
442460-- | Converts a matrix to a list of lists of elements.
443461toLists :: Matrix e cols rows -> [[e]]
462+toLists Empty = []
444463toLists (One e) = [[e]]
445464toLists (Fork l r) = toLists l ++ toLists r
446465toLists (Join l r) = zipWith (++) (toLists l) (toLists r)
@@ -452,7 +471,7 @@ toList = concat . toLists
452471-- Zeros Matrix
453472
454473-- | The zero matrix. A matrix wholly filled with zeros.
455−zeros :: (Num e, FL cols rows, CountableDims cols rows) => Matrix e cols rows
474+zeros :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
456475zeros = matrixBuilder' (const 0)
457476
458477-- Ones Matrix
@@ -460,20 +479,20 @@ zeros = matrixBuilder' (const 0)
460479-- | The ones matrix. A matrix wholly filled with ones.
461480--
462481-- Also known as T (Top) matrix.
463−ones :: (Num e, FL cols rows, CountableDims cols rows) => Matrix e cols rows
482+ones :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
464483ones = matrixBuilder' (const 1)
465484
466485-- Const Matrix
467486
468487-- | The constant matrix constructor. A matrix wholly filled with a given
469488-- value.
470−constant :: (Num e, FL cols rows, CountableDims cols rows) => e -> Matrix e cols rows
489+constant :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => e -> Matrix e cols rows
471490constant e = matrixBuilder' (const e)
472491
473492-- Bang Matrix
474493
475494-- | The T (Top) row vector matrix.
476−bang :: forall e cols. (Num e, Enum e, FL cols (), Countable cols) => Matrix e cols ()
495+bang :: forall e cols. (Num e, Enum e, FromLists e cols (), Countable cols) => Matrix e cols ()
477496bang =
478497 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
479498 in fromLists [take c [1, 1 ..]]
@@ -481,7 +500,7 @@ bang =
481500-- iden Matrix
482501
483502-- | iden matrix.
484−iden :: forall e cols . (Num e, FL cols cols, Countable cols) => Matrix e cols cols
503+iden :: forall e cols . (Num e, FromLists e cols cols, Countable cols) => Matrix e cols cols
485504iden = matrixBuilder' (bool 0 1 . uncurry (==))
486505{-# NOINLINE iden #-}
487506
@@ -492,12 +511,13 @@ iden = matrixBuilder' (bool 0 1 . uncurry (==))
492511-- This definition takes advantage of divide-and-conquer and fusion laws
493512-- from LAoP.
494513comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
514+comp Empty Empty = Empty
495515comp (One a) (One b) = One (a * b)
496516comp (Join a b) (Fork c d) = comp a c + comp b d -- Divide-and-conquer law
497517comp (Fork a b) c = Fork (comp a c) (comp b c) -- Fork fusion law
498518comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
499519{-# NOINLINE comp #-}
500−{-# RULES
520+{-# RULES
501521 "comp/iden1" forall m. comp m iden = m ;
502522 "comp/iden2" forall m. comp iden m = m
503523#-}
@@ -507,6 +527,7 @@ comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
507527infixl 7 .|
508528-- | Scalar multiplication of matrices.
509529(.|) :: Num e => e -> Matrix e cols rows -> Matrix e cols rows
530+(.|) _ Empty = Empty
510531(.|) e (One a) = One (e * a)
511532(.|) e (Join a b) = Join (e .| a) (e .| b)
512533(.|) e (Fork a b) = Fork (e .| a) (e .| b)
@@ -516,6 +537,7 @@ infixl 7 .|
516537infixl 7 ./
517538-- | Scalar multiplication of matrices.
518539(./) :: Fractional e => Matrix e cols rows -> e -> Matrix e cols rows
540+(./) Empty _ = Empty
519541(./) (One a) e = One (a / e)
520542(./) (Join a b) e = Join (a ./ e) (b ./ e)
521543(./) (Fork a b) e = Fork (a ./ e) (b ./ e)
@@ -523,59 +545,45 @@ infixl 7 ./
523545-- Projections
524546
525547-- | Biproduct first component projection
526−p1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e (Either m n) m
548+p1 :: (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e (Either m n) m
527549p1 = join id zeros
528550
529551-- | Biproduct second component projection
530−p2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e (Either m n) n
552+p2 :: (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e (Either m n) n
531553p2 = join zeros id
532554
533555-- Injections
534556
535557-- | Biproduct first component injection
536−i1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e m (Either m n)
558+i1 :: (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e m (Either m n)
537559i1 = tr p1
538560
539561-- | Biproduct second component injection
540−i2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e n (Either m n)
562+i2 :: (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e n (Either m n)
541563i2 = tr p2
542564
543565-- Dimensions
544566
545567-- | Obtain the number of rows.
546568--
547−-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
548−-- dimensions in constant time. For a version that doesn't require the
549−-- constraint see 'rows''.
569+-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
570+-- dimensions in constant time.
571+--
572+-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
573+-- exchange for performance.
550574rows :: forall e cols rows. (Countable rows) => Matrix e cols rows -> Int
551575rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
552576
553−-- | Obtain the number of rows in an inefficient manner, but without any
554−-- constraints.
555−--
556−-- For a more efficient version see 'rows'.
557−rows' :: Matrix e cols rows -> Int
558−rows' (One _) = 1
559−rows' (Join lhs _) = rows' lhs
560−rows' (Fork top bottom) = rows' top + rows' bottom
561−
562577-- | Obtain the number of columns.
578+--
579+-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
580+-- dimensions in constant time.
563581--
564−-- NOTE: The 'KnownNat' constraint is needed in order to obtain the
565−-- dimensions in constant time. For a version that doesn't require the
566−-- constraint see 'columns''.
582+-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
583+-- exchange for performance.
567584columns :: forall e cols rows. (Countable cols) => Matrix e cols rows -> Int
568585columns _ = fromInteger $ natVal (Proxy :: Proxy (Count cols))
569586
570−-- | Obtain the number of columns in an inefficient manner, but without any
571−-- constraints.
572−--
573−-- For a more efficient version see 'columns'.
574−columns' :: Matrix e cols rows -> Int
575−columns' (One _) = 1
576−columns' (Join lhs rhs) = columns' lhs + columns' rhs
577−columns' (Fork top _) = columns' top
578−
579587-- Coproduct Bifunctor
580588
581589infixl 5 -|-
@@ -584,11 +592,11 @@ infixl 5 -|-
584592(-|-) ::
585593 forall e n k m j.
586594 ( Num e,
587− CountableDims j k,
588− FL k k,
589− FL j k,
590− FL k j,
591− FL j j
595+ CountableDimensions j k,
596+ FromLists e k k,
597+ FromLists e j k,
598+ FromLists e k j,
599+ FromLists e j j
592600 ) =>
593601 Matrix e n k ->
594602 Matrix e m j ->
@@ -601,8 +609,8 @@ infixl 5 -|-
601609fstM ::
602610 forall e m k .
603611 ( Num e,
604− CountableDims k m,
605− FL (Normalize (m, k)) m,
612+ CountableDimensions k m,
613+ FromLists e (Normalize (m, k)) m,
606614 CountableN (m, k)
607615 ) => Matrix e (Normalize (m, k)) m
608616fstM = matrixBuilder' f
@@ -616,8 +624,8 @@ fstM = matrixBuilder' f
616624sndM ::
617625 forall e m k .
618626 ( Num e,
619− CountableDims k m,
620− FL (Normalize (m, k)) k,
627+ CountableDimensions k m,
628+ FromLists e (Normalize (m, k)) k,
621629 CountableN (m, k)
622630 ) => Matrix e (Normalize (m, k)) k
623631sndM = matrixBuilder' f
@@ -630,7 +638,7 @@ sndM = matrixBuilder' f
630638-- | Khatri Rao Matrix product also known as matrix pairing.
631639--
632640-- NOTE: That this is not a true categorical product, see for instance:
633−--
641+--
634642-- @
635643-- | fstM . kr a b == a
636644-- kr a b ==> |
@@ -641,10 +649,10 @@ sndM = matrixBuilder' f
641649kr ::
642650 forall e cols a b.
643651 ( Num e,
644− CountableDims a b,
652+ CountableDimensions a b,
645653 CountableN (a, b),
646− FL (Normalize (a, b)) a,
647− FL (Normalize (a, b)) b
654+ FromLists e (Normalize (a, b)) a,
655+ FromLists e (Normalize (a, b)) b
648656 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (Normalize (a, b))
649657kr a b =
650658 let fstM' = fstM @e @a @b
@@ -659,14 +667,14 @@ infixl 4 ><
659667(><) ::
660668 forall e m p n q.
661669 ( Num e,
662− CountableDims m n,
663− CountableDims p q,
664− CountableDimsN (m, n) (p, q),
665− FL (Normalize (m, n)) m,
666− FL (Normalize (m, n)) n,
667− FL (Normalize (p, q)) p,
668− FL (Normalize (p, q)) q
669− )
670+ CountableDimensions m n,
671+ CountableDimensions p q,
672+ CountableDimensionsN (m, n) (p, q),
673+ FromLists e (Normalize (m, n)) m,
674+ FromLists e (Normalize (m, n)) n,
675+ FromLists e (Normalize (p, q)) p,
676+ FromLists e (Normalize (p, q)) q
677+ )
670678 => Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
671679(><) a b =
672680 let fstM' = fstM @e @m @n
@@ -684,6 +692,7 @@ infixl 4 ><
684692-- @
685693abideJF :: Matrix e cols rows -> Matrix e cols rows
686694abideJF (Join (Fork a c) (Fork b d)) = Fork (Join (abideJF a) (abideJF b)) (Join (abideJF c) (abideJF d)) -- Join-Fork abide law
695+abideJF Empty = Empty
687696abideJF (One e) = One e
688697abideJF (Join a b) = Join (abideJF a) (abideJF b)
689698abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
@@ -697,6 +706,7 @@ abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
697706-- @
698707abideFJ :: Matrix e cols rows -> Matrix e cols rows
699708abideFJ (Fork (Join a b) (Join c d)) = Join (Fork (abideFJ a) (abideFJ c)) (Fork (abideFJ b) (abideFJ d)) -- Fork-Join abide law
709+abideFJ Empty = Empty
700710abideFJ (One e) = One e
701711abideFJ (Join a b) = Join (abideFJ a) (abideFJ b)
702712abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
@@ -705,6 +715,7 @@ abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
705715
706716-- | Matrix transposition.
707717tr :: Matrix e cols rows -> Matrix e rows cols
718+tr Empty = Empty
708719tr (One e) = One e
709720tr (Join a b) = Fork (tr a) (tr b)
710721tr (Fork a b) = Join (tr a) (tr b)
@@ -713,32 +724,32 @@ tr (Fork a b) = Join (tr a) (tr b)
713724
714725-- | Selective functors 'select' operator equivalent inspired by the
715726-- ArrowMonad solution presented in the paper.
716−select :: (Num e, FL b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
727+select :: (Num e, FromLists e b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
717728select (Fork a b) y = y . a + b -- Divide-and-conquer law
718729select (Join (Fork a c) (Fork b d)) y = join (y . a + c) (y . b + d) -- Pattern matching + DnC law
719730select m y = join y id . m
720731
721732branch ::
722733 ( Num e,
723− CountableDims a b,
724− CountableDims c (Either b c),
725− FL c b,
726− FL a b,
727− FL a a,
728− FL b b,
729− FL c c,
730− FL b a,
731− FL b c,
732− FL (Either b c) b,
733− FL (Either b c) c
734+ CountableDimensions a b,
735+ CountableDimensions c (Either b c),
736+ FromLists e c b,
737+ FromLists e a b,
738+ FromLists e a a,
739+ FromLists e b b,
740+ FromLists e c c,
741+ FromLists e b a,
742+ FromLists e b c,
743+ FromLists e (Either b c) b,
744+ FromLists e (Either b c) c
734745 )
735746 => Matrix e cols (Either a b) -> Matrix e a c -> Matrix e b c -> Matrix e cols c
736747branch x l r = f x `select` g l `select` r
737748 where
738− f :: (Num e, Countable a, CountableDims b c, FL a b, FL c b, FL b b, FL b a, FL a a)
749+ 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)
739750 => Matrix e cols (Either a b) -> Matrix e cols (Either a (Either b c))
740751 f m = fork (tr i1) (i1 . tr i2) . m
741− g :: (Num e, CountableDims b c, FL b c, FL c c) => Matrix e a c -> Matrix e a (Either b c)
752+ g :: (Num e, CountableDimensions b c, FromLists e b c, FromLists e c c) => Matrix e a c -> Matrix e a (Either b c)
742753 g m = i2 . m
743754
744755-- McCarthy's Conditional
@@ -747,9 +758,9 @@ branch x l r = f x `select` g l `select` r
747758cond ::
748759 ( Trivial cols,
749760 Countable cols,
750− FL () cols,
751− FL cols (),
752− FL cols cols,
761+ FromLists e () cols,
762+ FromLists e cols (),
763+ FromLists e cols cols,
753764 Bounded a,
754765 Enum a,
755766 Num e,
@@ -762,9 +773,9 @@ cond p f g = join f g . grd p
762773grd ::
763774 ( Trivial q,
764775 Countable q,
765− FL () q,
766− FL q (),
767− FL q q,
776+ FromLists e () q,
777+ FromLists e q (),
778+ FromLists e q q,
768779 Bounded a,
769780 Enum a,
770781 Num e,
@@ -778,9 +789,9 @@ corr ::
778789 forall e a q .
779790 ( Trivial q,
780791 Countable q,
781− FL () q,
782− FL q (),
783− FL q q,
792+ FromLists e () q,
793+ FromLists e q (),
794+ FromLists e q q,
784795 Liftable e a Bool
785796 )
786797 => (a -> Bool) -> Matrix e q q
@@ -809,7 +820,7 @@ prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
809820 fill str = replicate (widest - length str - 2) ' ' ++ str
810821
811822-- | Matrix pretty printer
812−pretty :: (CountableDims cols rows, Show e) => Matrix e cols rows -> String
823+pretty :: (CountableDimensions cols rows, Show e) => Matrix e cols rows -> String
813824pretty m = concat
814825 [ "┌ ", unwords (replicate (columns m) blank), " ┐\n"
815826 , unlines
@@ -838,11 +849,12 @@ pretty m = concat
838849 (safeGet i j m)
839850
840851-- | Matrix pretty printer
841−prettyPrint :: (CountableDims cols rows, Show e) => Matrix e cols rows -> IO ()
852+prettyPrint :: (CountableDimensions cols rows, Show e) => Matrix e cols rows -> IO ()
842853prettyPrint = putStrLn . pretty
843854
844855-- | Zip two matrices with a given binary function
845856zipWithM :: (e -> f -> g) -> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
857+zipWithM _ Empty Empty = Empty
846858zipWithM f (One a) (One b) = One (f a b)
847859zipWithM f (Join a b) (Join c d) = Join (zipWithM f a c) (zipWithM f b d)
848860zipWithM f (Fork a b) (Fork c d) = Fork (zipWithM f a c) (zipWithM f b d)
@@ -867,12 +879,14 @@ fromBool False = nat 0
867879
868880-- | Relational negation
869881negateM :: Relation cols rows -> Relation cols rows
882+negateM Empty = Empty
870883negateM (One (Nat p)) = One (Nat (negate p))
871884negateM (Join a b) = Join (negateM a) (negateM b)
872885negateM (Fork a b) = Fork (negateM a) (negateM b)
873886
874887-- | Relational addition
875888orM :: Relation cols rows -> Relation cols rows -> Relation cols rows
889+orM Empty Empty = Empty
876890orM (One a) (One b) = One (fromBool (toBool a || toBool b))
877891orM (Join a b) (Join c d) = Join (orM a c) (orM b d)
878892orM (Fork a b) (Fork c d) = Fork (orM a c) (orM b d)
@@ -881,6 +895,7 @@ orM x@(Join _ _) y@(Fork _ _) = orM (abideJF x) y
881895
882896-- | Relational multiplication
883897andM :: Relation cols rows -> Relation cols rows -> Relation cols rows
898+andM Empty Empty = Empty
884899andM (One a) (One b) = One (fromBool (toBool a && toBool b))
885900andM (Join a b) (Join c d) = Join (andM a c) (andM b d)
886901andM (Fork a b) (Fork c d) = Fork (andM a c) (andM b d)
@@ -889,6 +904,7 @@ andM x@(Join _ _) y@(Fork _ _) = andM (abideJF x) y
889904
890905-- | Relational subtraction
891906subM :: Relation cols rows -> Relation cols rows -> Relation cols rows
907+subM Empty Empty = Empty
892908subM (One a) (One b) = if a - b < nat 0 then One (nat 0) else One (a - b)
893909subM (Join a b) (Join c d) = Join (subM a c) (subM b d)
894910subM (Fork a b) (Fork c d) = Fork (subM a c) (subM b d)
@@ -897,6 +913,7 @@ subM x@(Join _ _) y@(Fork _ _) = subM (abideJF x) y
897913
898914-- | Matrix relational composition.
899915compRel :: Relation cr rows -> Relation cols cr -> Relation cols rows
916+compRel Empty Empty = Empty
900917compRel (One a) (One b) = One (fromBool (toBool a && toBool b))
901918compRel (Join a b) (Fork c d) = orM (compRel a c) (compRel b d) -- Divide-and-conquer law
902919compRel (Fork a b) c = Fork (compRel a c) (compRel b c) -- Fork fusion law
@@ -904,6 +921,7 @@ compRel c (Join a b) = Join (compRel c a) (compRel c b) -- Join fusion
904921
905922-- | Matrix relational right division
906923divR :: Relation b c -> Relation b a -> Relation a c
924+divR Empty Empty = Empty
907925divR (One a) (One b) = One (fromBool (not (toBool b) || toBool a)) -- b implies a
908926divR (Join a b) (Join c d) = andM (divR a c) (divR b d)
909927divR (Fork a b) c = Fork (divR a c) (divR b c)
@@ -926,24 +944,58 @@ divS s r = divL r s `intersection` divR (tr r) (tr s)
926944fromFRel' ::
927945 forall a b cols rows.
928946 ( Liftable Boolean a b,
929− CountableDims cols rows,
930− FL rows cols
947+ CountableDimensions cols rows,
948+ FromLists Boolean rows cols
931949 ) =>
932950 (a -> b) ->
933951 Relation cols rows
934−fromFRel' = fromF'
952+fromFRel' f =
953+ let minA = minBound @a
954+ maxA = maxBound @a
955+ minB = minBound @b
956+ maxB = maxBound @b
957+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count cols))
958+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count rows))
959+ elementsA = take ccols [minA .. maxA]
960+ elementsB = take rrows [minB .. maxB]
961+ combinations = (,) <$> elementsA <*> elementsB
962+ combAp = map snd . sort . map (\(a, b) -> if f a == b
963+ then ((fromEnum a, fromEnum b), nat 1)
964+ else ((fromEnum a, fromEnum b), nat 0)) $ combinations
965+ mList = buildList combAp rrows
966+ in tr $ fromLists mList
967+ where
968+ buildList [] _ = []
969+ buildList l r = take r l : buildList (drop r l) r
935970
936971-- | Lifts functions to relations with dimensions matching @a@ and @b@
937972-- cardinality's.
938973fromFRel ::
939974 forall a b.
940975 ( Liftable Boolean a b,
941− CountableDimsN a b,
942− FLN b a
976+ CountableDimensionsN a b,
977+ FromLists Boolean (Normalize b) (Normalize a)
943978 ) =>
944979 (a -> b) ->
945980 Relation (Normalize a) (Normalize b)
946−fromFRel = fromFRel'
981+fromFRel f =
982+ let minA = minBound @a
983+ maxA = maxBound @a
984+ minB = minBound @b
985+ maxB = maxBound @b
986+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
987+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
988+ elementsA = take ccols [minA .. maxA]
989+ elementsB = take rrows [minB .. maxB]
990+ combinations = (,) <$> elementsA <*> elementsB
991+ combAp = map snd . sort . map (\(a, b) -> if f a == b
992+ then ((fromEnum a, fromEnum b), nat 1)
993+ else ((fromEnum a, fromEnum b), nat 0)) $ combinations
994+ mList = buildList combAp rrows
995+ in tr $ fromLists mList
996+ where
997+ buildList [] _ = []
998+ buildList l r = take r l : buildList (drop r l) r
947999
9481000-- | Lifts a relation function to a Boolean Matrix
9491001toRel ::
@@ -953,17 +1005,17 @@ toRel ::
9531005 Enum a,
9541006 Enum b,
9551007 Eq b,
956− CountableDims a b,
957− FLN b a
958− )
1008+ CountableDimensionsN a b,
1009+ FromListsN Boolean b a
1010+ )
9591011 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
9601012toRel f =
9611013 let minA = minBound @a
9621014 maxA = maxBound @a
9631015 minB = minBound @b
9641016 maxB = maxBound @b
965− ccols = fromInteger $ natVal (Proxy :: Proxy (Count a))
966− rrows = fromInteger $ natVal (Proxy :: Proxy (Count b))
1017+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
1018+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
9671019 elementsA = take ccols [minA .. maxA]
9681020 elementsB = take rrows [minB .. maxB]
9691021 combinations = (,) <$> elementsA <*> elementsB