@@ -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,23 @@ 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
5252 -- * Constraint type aliases
5353 Countable,
54− CountableDims,
54+ CountableDimensions,
5555 CountableN,
56− CountableDimsN,
57− FL,
58− FLN,
56+ CountableDimensionsN,
57+ FromListsN,
5958 Liftable,
6059 Trivial,
6160
6261 -- * Primitives
62+ empty,
6363 one,
6464 join,
6565 fork,
@@ -86,9 +86,7 @@ module LAoP.Matrix.Internal
8686 -- * Misc
8787 -- ** Get dimensions
8888 columns,
89− columns',
9089 rows,
91− rows',
9290
9391 -- ** Matrix Transposition
9492 tr,
@@ -176,7 +174,6 @@ import Data.Kind
176174import Data.List
177175import Data.Maybe
178176import Data.Proxy
179−import Data.Typeable
180177import Data.Void
181178import GHC.TypeLits
182179import Data.Type.Equality
@@ -187,6 +184,7 @@ import qualified Prelude (id, (.))
187184
188185-- | LAoP (Linear Algebra of Programming) Inductive Matrix definition.
189186data Matrix e cols rows where
187+ Empty :: Matrix e Void Void
190188 One :: e -> Matrix e () ()
191189 Join :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
192190 Fork :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
@@ -216,6 +214,7 @@ type family Count (d :: Type) :: Nat where
216214--
217215-- Thanks to Li-Yao Xia this type family is super fast.
218216type family FromNat (n :: Nat) :: Type where
217+ FromNat 0 = Void
219218 FromNat 1 = ()
220219 FromNat n = FromNat' (Mod n 2 == 0) (FromNat (Div n 2))
221220
@@ -232,26 +231,27 @@ type family Normalize (d :: Type) :: Type where
232231-- | Constraint type synonyms to keep the type signatures less convoluted
233232type Countable a = KnownNat (Count a)
234233type 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)
234+type CountableDimensions a b = (Countable a, Countable b)
235+type CountableDimensionsN a b = (CountableN a, CountableN b)
236+type FromListsN e a b = FromLists e (Normalize a) (Normalize b)
239237type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
240238type Trivial a = FromNat (Count a) ~ a
241239
242240-- | It is possible to implement a constrained version of the category type
243241-- class.
244242instance (Num e) => Category (Matrix e) where
245− type Object (Matrix e) a = (FL a a, Countable a)
246− id = iden
243+ type Object (Matrix e) a = (FromLists e a a, Countable a)
244+ id = iden
247245 (.) = comp
248246
249247instance NFData e => NFData (Matrix e cols rows) where
248+ rnf Empty = ()
250249 rnf (One e) = rnf e
251250 rnf (Join a b) = rnf a `seq` rnf b
252251 rnf (Fork a b) = rnf a `seq` rnf b
253252
254253instance Eq e => Eq (Matrix e cols rows) where
254+ Empty == Empty = True
255255 (One a) == (One b) = a == b
256256 (Join a b) == (Join c d) = a == c && b == d
257257 (Fork a b) == (Fork c d) = a == c && b == d
@@ -259,21 +259,25 @@ instance Eq e => Eq (Matrix e cols rows) where
259259 x@(Join _ _) == y@(Fork _ _) = abideJF x == y
260260
261261instance Num e => Num (Matrix e cols rows) where
262+
262263 a + b = zipWithM (+) a b
263264
264265 a - b = zipWithM (-) a b
265266
266267 a * b = zipWithM (*) a b
267268
269+ abs Empty = Empty
268270 abs (One a) = One (abs a)
269271 abs (Join a b) = Join (abs a) (abs b)
270272 abs (Fork a b) = Fork (abs a) (abs b)
271273
274+ signum Empty = Empty
272275 signum (One a) = One (signum a)
273276 signum (Join a b) = Join (signum a) (signum b)
274277 signum (Fork a b) = Fork (signum a) (signum b)
275278
276279instance Ord e => Ord (Matrix e cols rows) where
280+ Empty <= Empty = True
277281 (One a) <= (One b) = a <= b
278282 (Join a b) <= (Join c d) = (a <= c) && (b <= d)
279283 (Fork a b) <= (Fork c d) = (a <= c) && (b <= d)
@@ -282,6 +286,10 @@ instance Ord e => Ord (Matrix e cols rows) where
282286
283287-- Primitives
284288
289+-- | Empty matrix constructor
290+empty :: Matrix e Void Void
291+empty = Empty
292+
285293-- | Unit matrix constructor
286294one :: e -> Matrix e () ()
287295one = One
@@ -312,39 +320,40 @@ infixl 2 ===
312320--
313321-- Given that it is not possible to branch on types at the term level type
314322-- classes are needed very much like an inductive definition but on types.
315−class FromLists cols rows where
323+class FromLists e cols rows where
316324 -- | Build a matrix out of a list of list of elements. Throws a runtime
317325 -- error if the dimensions do not match.
318326 fromLists :: [[e]] -> Matrix e cols rows
319327
320−instance FromLists () () where
328+instance FromLists e Void Void where
329+ fromLists [] = Empty
330+ fromLists _ = error "Wrong dimensions"
331+
332+instance {-# OVERLAPPING #-} FromLists e () () where
321333 fromLists [[e]] = One e
322334 fromLists _ = error "Wrong dimensions"
323335
324−instance (FromLists cols ()) => FromLists (Either () cols) () where
336+instance {-# OVERLAPPING #-} (FromLists e cols ()) => FromLists e (Either () cols) () where
325337 fromLists [h : t] = Join (One h) (fromLists [t])
326338 fromLists _ = error "Wrong dimensions"
327339
328−instance {-# OVERLAPPABLE #-}
329− (FromLists a (), FromLists b (), Countable a) => FromLists (Either a b) () where
340+instance {-# OVERLAPPABLE #-} (FromLists e a (), FromLists e b (), Countable a) => FromLists e (Either a b) () where
330341 fromLists [l] =
331342 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
332343 in Join (fromLists [take rowsA l]) (fromLists [drop rowsA l])
333344 fromLists _ = error "Wrong dimensions"
334345
335−instance (FromLists () rows) => FromLists () (Either () rows) where
346+instance {-# OVERLAPPING #-} (FromLists e () rows) => FromLists e () (Either () rows) where
336347 fromLists ([h] : t) = Fork (One h) (fromLists t)
337348 fromLists _ = error "Wrong dimensions"
338349
339−instance {-# OVERLAPPABLE #-}
340− (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
341− fromLists l@([_] : _) =
350+instance {-# OVERLAPPABLE #-} (FromLists e () a, FromLists e () b, Countable a) => FromLists e () (Either a b) where
351+ fromLists l@([_] : _) =
342352 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
343353 in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
344354 fromLists _ = error "Wrong dimensions"
345355
346−instance (FromLists (Either a b) c, FromLists (Either a b) d, Countable c)
347− => FromLists (Either a b) (Either c d) where
356+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
348357 fromLists l@(h : t) =
349358 let lh = length h
350359 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
@@ -357,8 +366,8 @@ instance (FromLists (Either a b) c, FromLists (Either a b) d, Countable c)
357366-- a construction function that operates with indices.
358367matrixBuilder' ::
359368 forall e cols rows.
360− ( FL cols rows,
361− CountableDims cols rows
369+ ( FromLists e cols rows,
370+ CountableDimensions cols rows
362371 ) => ((Int, Int) -> e) -> Matrix e cols rows
363372matrixBuilder' f =
364373 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
@@ -370,16 +379,17 @@ matrixBuilder' f =
370379-- a construction function that operates with arbitrary types.
371380matrixBuilder ::
372381 forall e a b.
373− ( FLN a b,
382+ ( FromListsN e a b,
383+ CountableN b,
374384 Enum a,
375385 Enum b,
376386 Bounded a,
377387 Bounded b,
378388 Eq a,
379− Countable b
389+ CountableDimensionsN a b
380390 ) => ((a, b) -> e) -> Matrix e (Normalize a) (Normalize b)
381391matrixBuilder f =
382− let r = fromInteger $ natVal (Proxy :: Proxy (Count b))
392+ let r = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
383393 positions = [(a, b) | a <- [minBound .. maxBound], b <- [minBound .. maxBound]]
384394 in fromLists . map (map f) . transpose . buildList r $ positions
385395 where
@@ -387,11 +397,11 @@ matrixBuilder f =
387397 buildList r l = take r l : buildList r (drop r l)
388398
389399-- | Constructs a column vector matrix
390−col :: (FL () rows) => [e] -> Matrix e () rows
400+col :: (FromLists e () rows) => [e] -> Matrix e () rows
391401col = fromLists . map (: [])
392402
393403-- | Constructs a row vector matrix
394−row :: (FL cols ()) => [e] -> Matrix e cols ()
404+row :: (FromLists e cols ()) => [e] -> Matrix e cols ()
395405row = fromLists . (: [])
396406
397407-- | Lifts functions to matrices with arbitrary dimensions.
@@ -401,8 +411,8 @@ row = fromLists . (: [])
401411fromF' ::
402412 forall a b cols rows e.
403413 ( Liftable e a b,
404− CountableDims cols rows,
405− FL rows cols
414+ CountableDimensions cols rows,
415+ FromLists e rows cols
406416 ) =>
407417 (a -> b) ->
408418 Matrix e cols rows
@@ -430,17 +440,35 @@ fromF' f =
430440fromF ::
431441 forall a b e.
432442 ( Liftable e a b,
433− CountableDimsN a b,
434− FLN b a
443+ CountableDimensionsN a b,
444+ FromListsN e b a
435445 ) =>
436446 (a -> b) ->
437447 Matrix e (Normalize a) (Normalize b)
438−fromF = fromF'
448+fromF f =
449+ let minA = minBound @a
450+ maxA = maxBound @a
451+ minB = minBound @b
452+ maxB = maxBound @b
453+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
454+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
455+ elementsA = take ccols [minA .. maxA]
456+ elementsB = take rrows [minB .. maxB]
457+ combinations = (,) <$> elementsA <*> elementsB
458+ combAp = map snd . sort . map (\(a, b) -> if f a == b
459+ then ((fromEnum a, fromEnum b), 1)
460+ else ((fromEnum a, fromEnum b), 0)) $ combinations
461+ mList = buildList combAp rrows
462+ in tr $ fromLists mList
463+ where
464+ buildList [] _ = []
465+ buildList l r = take r l : buildList (drop r l) r
439466
440467-- Conversion
441468
442469-- | Converts a matrix to a list of lists of elements.
443470toLists :: Matrix e cols rows -> [[e]]
471+toLists Empty = []
444472toLists (One e) = [[e]]
445473toLists (Fork l r) = toLists l ++ toLists r
446474toLists (Join l r) = zipWith (++) (toLists l) (toLists r)
@@ -452,7 +480,7 @@ toList = concat . toLists
452480-- Zeros Matrix
453481
454482-- | The zero matrix. A matrix wholly filled with zeros.
455−zeros :: (Num e, FL cols rows, CountableDims cols rows) => Matrix e cols rows
483+zeros :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
456484zeros = matrixBuilder' (const 0)
457485
458486-- Ones Matrix
@@ -460,20 +488,20 @@ zeros = matrixBuilder' (const 0)
460488-- | The ones matrix. A matrix wholly filled with ones.
461489--
462490-- Also known as T (Top) matrix.
463−ones :: (Num e, FL cols rows, CountableDims cols rows) => Matrix e cols rows
491+ones :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
464492ones = matrixBuilder' (const 1)
465493
466494-- Const Matrix
467495
468496-- | The constant matrix constructor. A matrix wholly filled with a given
469497-- value.
470−constant :: (Num e, FL cols rows, CountableDims cols rows) => e -> Matrix e cols rows
498+constant :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => e -> Matrix e cols rows
471499constant e = matrixBuilder' (const e)
472500
473501-- Bang Matrix
474502
475503-- | The T (Top) row vector matrix.
476−bang :: forall e cols. (Num e, Enum e, FL cols (), Countable cols) => Matrix e cols ()
504+bang :: forall e cols. (Num e, Enum e, FromLists e cols (), Countable cols) => Matrix e cols ()
477505bang =
478506 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
479507 in fromLists [take c [1, 1 ..]]
@@ -481,7 +509,7 @@ bang =
481509-- iden Matrix
482510
483511-- | iden matrix.
484−iden :: forall e cols . (Num e, FL cols cols, Countable cols) => Matrix e cols cols
512+iden :: forall e cols . (Num e, FromLists e cols cols, Countable cols) => Matrix e cols cols
485513iden = matrixBuilder' (bool 0 1 . uncurry (==))
486514{-# NOINLINE iden #-}
487515
@@ -492,12 +520,13 @@ iden = matrixBuilder' (bool 0 1 . uncurry (==))
492520-- This definition takes advantage of divide-and-conquer and fusion laws
493521-- from LAoP.
494522comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
523+comp Empty Empty = Empty
495524comp (One a) (One b) = One (a * b)
496525comp (Join a b) (Fork c d) = comp a c + comp b d -- Divide-and-conquer law
497526comp (Fork a b) c = Fork (comp a c) (comp b c) -- Fork fusion law
498527comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
499528{-# NOINLINE comp #-}
500−{-# RULES
529+{-# RULES
501530 "comp/iden1" forall m. comp m iden = m ;
502531 "comp/iden2" forall m. comp iden m = m
503532#-}
@@ -507,6 +536,7 @@ comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
507536infixl 7 .|
508537-- | Scalar multiplication of matrices.
509538(.|) :: Num e => e -> Matrix e cols rows -> Matrix e cols rows
539+(.|) _ Empty = Empty
510540(.|) e (One a) = One (e * a)
511541(.|) e (Join a b) = Join (e .| a) (e .| b)
512542(.|) e (Fork a b) = Fork (e .| a) (e .| b)
@@ -516,6 +546,7 @@ infixl 7 .|
516546infixl 7 ./
517547-- | Scalar multiplication of matrices.
518548(./) :: Fractional e => Matrix e cols rows -> e -> Matrix e cols rows
549+(./) Empty _ = Empty
519550(./) (One a) e = One (a / e)
520551(./) (Join a b) e = Join (a ./ e) (b ./ e)
521552(./) (Fork a b) e = Fork (a ./ e) (b ./ e)
@@ -523,59 +554,45 @@ infixl 7 ./
523554-- Projections
524555
525556-- | Biproduct first component projection
526−p1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e (Either m n) m
557+p1 :: (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e (Either m n) m
527558p1 = join id zeros
528559
529560-- | Biproduct second component projection
530−p2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e (Either m n) n
561+p2 :: (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e (Either m n) n
531562p2 = join zeros id
532563
533564-- Injections
534565
535566-- | Biproduct first component injection
536−i1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e m (Either m n)
567+i1 :: (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e m (Either m n)
537568i1 = tr p1
538569
539570-- | Biproduct second component injection
540−i2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e n (Either m n)
571+i2 :: (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e n (Either m n)
541572i2 = tr p2
542573
543574-- Dimensions
544575
545576-- | Obtain the number of rows.
546577--
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''.
578+-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
579+-- dimensions in constant time.
580+--
581+-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
582+-- exchange for performance.
550583rows :: forall e cols rows. (Countable rows) => Matrix e cols rows -> Int
551584rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
552585
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−
562586-- | Obtain the number of columns.
587+--
588+-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
589+-- dimensions in constant time.
563590--
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''.
591+-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
592+-- exchange for performance.
567593columns :: forall e cols rows. (Countable cols) => Matrix e cols rows -> Int
568594columns _ = fromInteger $ natVal (Proxy :: Proxy (Count cols))
569595
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−
579596-- Coproduct Bifunctor
580597
581598infixl 5 -|-
@@ -584,11 +601,11 @@ infixl 5 -|-
584601(-|-) ::
585602 forall e n k m j.
586603 ( Num e,
587− CountableDims j k,
588− FL k k,
589− FL j k,
590− FL k j,
591− FL j j
604+ CountableDimensions j k,
605+ FromLists e k k,
606+ FromLists e j k,
607+ FromLists e k j,
608+ FromLists e j j
592609 ) =>
593610 Matrix e n k ->
594611 Matrix e m j ->
@@ -601,8 +618,8 @@ infixl 5 -|-
601618fstM ::
602619 forall e m k .
603620 ( Num e,
604− CountableDims k m,
605− FL (Normalize (m, k)) m,
621+ CountableDimensions k m,
622+ FromLists e (Normalize (m, k)) m,
606623 CountableN (m, k)
607624 ) => Matrix e (Normalize (m, k)) m
608625fstM = matrixBuilder' f
@@ -616,8 +633,8 @@ fstM = matrixBuilder' f
616633sndM ::
617634 forall e m k .
618635 ( Num e,
619− CountableDims k m,
620− FL (Normalize (m, k)) k,
636+ CountableDimensions k m,
637+ FromLists e (Normalize (m, k)) k,
621638 CountableN (m, k)
622639 ) => Matrix e (Normalize (m, k)) k
623640sndM = matrixBuilder' f
@@ -630,7 +647,7 @@ sndM = matrixBuilder' f
630647-- | Khatri Rao Matrix product also known as matrix pairing.
631648--
632649-- NOTE: That this is not a true categorical product, see for instance:
633−--
650+--
634651-- @
635652-- | fstM . kr a b == a
636653-- kr a b ==> |
@@ -641,10 +658,10 @@ sndM = matrixBuilder' f
641658kr ::
642659 forall e cols a b.
643660 ( Num e,
644− CountableDims a b,
661+ CountableDimensions a b,
645662 CountableN (a, b),
646− FL (Normalize (a, b)) a,
647− FL (Normalize (a, b)) b
663+ FromLists e (Normalize (a, b)) a,
664+ FromLists e (Normalize (a, b)) b
648665 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (Normalize (a, b))
649666kr a b =
650667 let fstM' = fstM @e @a @b
@@ -659,14 +676,14 @@ infixl 4 ><
659676(><) ::
660677 forall e m p n q.
661678 ( 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− )
679+ CountableDimensions m n,
680+ CountableDimensions p q,
681+ CountableDimensionsN (m, n) (p, q),
682+ FromLists e (Normalize (m, n)) m,
683+ FromLists e (Normalize (m, n)) n,
684+ FromLists e (Normalize (p, q)) p,
685+ FromLists e (Normalize (p, q)) q
686+ )
670687 => Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
671688(><) a b =
672689 let fstM' = fstM @e @m @n
@@ -684,6 +701,7 @@ infixl 4 ><
684701-- @
685702abideJF :: Matrix e cols rows -> Matrix e cols rows
686703abideJF (Join (Fork a c) (Fork b d)) = Fork (Join (abideJF a) (abideJF b)) (Join (abideJF c) (abideJF d)) -- Join-Fork abide law
704+abideJF Empty = Empty
687705abideJF (One e) = One e
688706abideJF (Join a b) = Join (abideJF a) (abideJF b)
689707abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
@@ -697,6 +715,7 @@ abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
697715-- @
698716abideFJ :: Matrix e cols rows -> Matrix e cols rows
699717abideFJ (Fork (Join a b) (Join c d)) = Join (Fork (abideFJ a) (abideFJ c)) (Fork (abideFJ b) (abideFJ d)) -- Fork-Join abide law
718+abideFJ Empty = Empty
700719abideFJ (One e) = One e
701720abideFJ (Join a b) = Join (abideFJ a) (abideFJ b)
702721abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
@@ -705,6 +724,7 @@ abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
705724
706725-- | Matrix transposition.
707726tr :: Matrix e cols rows -> Matrix e rows cols
727+tr Empty = Empty
708728tr (One e) = One e
709729tr (Join a b) = Fork (tr a) (tr b)
710730tr (Fork a b) = Join (tr a) (tr b)
@@ -713,32 +733,32 @@ tr (Fork a b) = Join (tr a) (tr b)
713733
714734-- | Selective functors 'select' operator equivalent inspired by the
715735-- 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
736+select :: (Num e, FromLists e b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
717737select (Fork a b) y = y . a + b -- Divide-and-conquer law
718738select (Join (Fork a c) (Fork b d)) y = join (y . a + c) (y . b + d) -- Pattern matching + DnC law
719739select m y = join y id . m
720740
721741branch ::
722742 ( 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
743+ CountableDimensions a b,
744+ CountableDimensions c (Either b c),
745+ FromLists e c b,
746+ FromLists e a b,
747+ FromLists e a a,
748+ FromLists e b b,
749+ FromLists e c c,
750+ FromLists e b a,
751+ FromLists e b c,
752+ FromLists e (Either b c) b,
753+ FromLists e (Either b c) c
734754 )
735755 => Matrix e cols (Either a b) -> Matrix e a c -> Matrix e b c -> Matrix e cols c
736756branch x l r = f x `select` g l `select` r
737757 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)
758+ 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)
739759 => Matrix e cols (Either a b) -> Matrix e cols (Either a (Either b c))
740760 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)
761+ g :: (Num e, CountableDimensions b c, FromLists e b c, FromLists e c c) => Matrix e a c -> Matrix e a (Either b c)
742762 g m = i2 . m
743763
744764-- McCarthy's Conditional
@@ -747,9 +767,9 @@ branch x l r = f x `select` g l `select` r
747767cond ::
748768 ( Trivial cols,
749769 Countable cols,
750− FL () cols,
751− FL cols (),
752− FL cols cols,
770+ FromLists e () cols,
771+ FromLists e cols (),
772+ FromLists e cols cols,
753773 Bounded a,
754774 Enum a,
755775 Num e,
@@ -762,9 +782,9 @@ cond p f g = join f g . grd p
762782grd ::
763783 ( Trivial q,
764784 Countable q,
765− FL () q,
766− FL q (),
767− FL q q,
785+ FromLists e () q,
786+ FromLists e q (),
787+ FromLists e q q,
768788 Bounded a,
769789 Enum a,
770790 Num e,
@@ -778,9 +798,9 @@ corr ::
778798 forall e a q .
779799 ( Trivial q,
780800 Countable q,
781− FL () q,
782− FL q (),
783− FL q q,
801+ FromLists e () q,
802+ FromLists e q (),
803+ FromLists e q q,
784804 Liftable e a Bool
785805 )
786806 => (a -> Bool) -> Matrix e q q
@@ -809,7 +829,7 @@ prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
809829 fill str = replicate (widest - length str - 2) ' ' ++ str
810830
811831-- | Matrix pretty printer
812−pretty :: (CountableDims cols rows, Show e) => Matrix e cols rows -> String
832+pretty :: (CountableDimensions cols rows, Show e) => Matrix e cols rows -> String
813833pretty m = concat
814834 [ "┌ ", unwords (replicate (columns m) blank), " ┐\n"
815835 , unlines
@@ -838,11 +858,12 @@ pretty m = concat
838858 (safeGet i j m)
839859
840860-- | Matrix pretty printer
841−prettyPrint :: (CountableDims cols rows, Show e) => Matrix e cols rows -> IO ()
861+prettyPrint :: (CountableDimensions cols rows, Show e) => Matrix e cols rows -> IO ()
842862prettyPrint = putStrLn . pretty
843863
844864-- | Zip two matrices with a given binary function
845865zipWithM :: (e -> f -> g) -> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
866+zipWithM _ Empty Empty = Empty
846867zipWithM f (One a) (One b) = One (f a b)
847868zipWithM f (Join a b) (Join c d) = Join (zipWithM f a c) (zipWithM f b d)
848869zipWithM f (Fork a b) (Fork c d) = Fork (zipWithM f a c) (zipWithM f b d)
@@ -867,12 +888,14 @@ fromBool False = nat 0
867888
868889-- | Relational negation
869890negateM :: Relation cols rows -> Relation cols rows
891+negateM Empty = Empty
870892negateM (One (Nat p)) = One (Nat (negate p))
871893negateM (Join a b) = Join (negateM a) (negateM b)
872894negateM (Fork a b) = Fork (negateM a) (negateM b)
873895
874896-- | Relational addition
875897orM :: Relation cols rows -> Relation cols rows -> Relation cols rows
898+orM Empty Empty = Empty
876899orM (One a) (One b) = One (fromBool (toBool a || toBool b))
877900orM (Join a b) (Join c d) = Join (orM a c) (orM b d)
878901orM (Fork a b) (Fork c d) = Fork (orM a c) (orM b d)
@@ -881,6 +904,7 @@ orM x@(Join _ _) y@(Fork _ _) = orM (abideJF x) y
881904
882905-- | Relational multiplication
883906andM :: Relation cols rows -> Relation cols rows -> Relation cols rows
907+andM Empty Empty = Empty
884908andM (One a) (One b) = One (fromBool (toBool a && toBool b))
885909andM (Join a b) (Join c d) = Join (andM a c) (andM b d)
886910andM (Fork a b) (Fork c d) = Fork (andM a c) (andM b d)
@@ -889,6 +913,7 @@ andM x@(Join _ _) y@(Fork _ _) = andM (abideJF x) y
889913
890914-- | Relational subtraction
891915subM :: Relation cols rows -> Relation cols rows -> Relation cols rows
916+subM Empty Empty = Empty
892917subM (One a) (One b) = if a - b < nat 0 then One (nat 0) else One (a - b)
893918subM (Join a b) (Join c d) = Join (subM a c) (subM b d)
894919subM (Fork a b) (Fork c d) = Fork (subM a c) (subM b d)
@@ -897,6 +922,7 @@ subM x@(Join _ _) y@(Fork _ _) = subM (abideJF x) y
897922
898923-- | Matrix relational composition.
899924compRel :: Relation cr rows -> Relation cols cr -> Relation cols rows
925+compRel Empty Empty = Empty
900926compRel (One a) (One b) = One (fromBool (toBool a && toBool b))
901927compRel (Join a b) (Fork c d) = orM (compRel a c) (compRel b d) -- Divide-and-conquer law
902928compRel (Fork a b) c = Fork (compRel a c) (compRel b c) -- Fork fusion law
@@ -904,6 +930,7 @@ compRel c (Join a b) = Join (compRel c a) (compRel c b) -- Join fusion
904930
905931-- | Matrix relational right division
906932divR :: Relation b c -> Relation b a -> Relation a c
933+divR Empty Empty = Empty
907934divR (One a) (One b) = One (fromBool (not (toBool b) || toBool a)) -- b implies a
908935divR (Join a b) (Join c d) = andM (divR a c) (divR b d)
909936divR (Fork a b) c = Fork (divR a c) (divR b c)
@@ -926,24 +953,58 @@ divS s r = divL r s `intersection` divR (tr r) (tr s)
926953fromFRel' ::
927954 forall a b cols rows.
928955 ( Liftable Boolean a b,
929− CountableDims cols rows,
930− FL rows cols
956+ CountableDimensions cols rows,
957+ FromLists Boolean rows cols
931958 ) =>
932959 (a -> b) ->
933960 Relation cols rows
934−fromFRel' = fromF'
961+fromFRel' f =
962+ let minA = minBound @a
963+ maxA = maxBound @a
964+ minB = minBound @b
965+ maxB = maxBound @b
966+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count cols))
967+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count rows))
968+ elementsA = take ccols [minA .. maxA]
969+ elementsB = take rrows [minB .. maxB]
970+ combinations = (,) <$> elementsA <*> elementsB
971+ combAp = map snd . sort . map (\(a, b) -> if f a == b
972+ then ((fromEnum a, fromEnum b), nat 1)
973+ else ((fromEnum a, fromEnum b), nat 0)) $ combinations
974+ mList = buildList combAp rrows
975+ in tr $ fromLists mList
976+ where
977+ buildList [] _ = []
978+ buildList l r = take r l : buildList (drop r l) r
935979
936980-- | Lifts functions to relations with dimensions matching @a@ and @b@
937981-- cardinality's.
938982fromFRel ::
939983 forall a b.
940984 ( Liftable Boolean a b,
941− CountableDimsN a b,
942− FLN b a
985+ CountableDimensionsN a b,
986+ FromLists Boolean (Normalize b) (Normalize a)
943987 ) =>
944988 (a -> b) ->
945989 Relation (Normalize a) (Normalize b)
946−fromFRel = fromFRel'
990+fromFRel f =
991+ let minA = minBound @a
992+ maxA = maxBound @a
993+ minB = minBound @b
994+ maxB = maxBound @b
995+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
996+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
997+ elementsA = take ccols [minA .. maxA]
998+ elementsB = take rrows [minB .. maxB]
999+ combinations = (,) <$> elementsA <*> elementsB
1000+ combAp = map snd . sort . map (\(a, b) -> if f a == b
1001+ then ((fromEnum a, fromEnum b), nat 1)
1002+ else ((fromEnum a, fromEnum b), nat 0)) $ combinations
1003+ mList = buildList combAp rrows
1004+ in tr $ fromLists mList
1005+ where
1006+ buildList [] _ = []
1007+ buildList l r = take r l : buildList (drop r l) r
9471008
9481009-- | Lifts a relation function to a Boolean Matrix
9491010toRel ::
@@ -953,17 +1014,17 @@ toRel ::
9531014 Enum a,
9541015 Enum b,
9551016 Eq b,
956− CountableDims a b,
957− FLN b a
958− )
1017+ CountableDimensionsN a b,
1018+ FromListsN Boolean b a
1019+ )
9591020 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
9601021toRel f =
9611022 let minA = minBound @a
9621023 maxA = maxBound @a
9631024 minB = minBound @b
9641025 maxB = maxBound @b
965− ccols = fromInteger $ natVal (Proxy :: Proxy (Count a))
966− rrows = fromInteger $ natVal (Proxy :: Proxy (Count b))
1026+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
1027+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
9671028 elementsA = take ccols [minA .. maxA]
9681029 elementsB = take rrows [minB .. maxB]
9691030 combinations = (,) <$> elementsA <*> elementsB