@@ -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,
@@ -130,7 +119,6 @@ module LAoP.Matrix.Internal
130119
131120 -- | Note that given the restrictions imposed it is not possible to
132121 -- implement the standard type classes present in standard Haskell.
133−
134122 -- *** Matrix pairing projections
135123 fstM,
136124 sndM,
@@ -176,17 +164,17 @@ import Data.Kind
176164import Data.List
177165import Data.Maybe
178166import Data.Proxy
179−import Data.Typeable
180167import Data.Void
181168import GHC.TypeLits
182169import Data.Type.Equality
183170import GHC.Generics
184171import Control.DeepSeq
185−import Prelude hiding (id, (.))
186−import qualified Prelude (id, (.))
172+import Control.Category
173+import Prelude hiding ((.))
187174
188175-- | LAoP (Linear Algebra of Programming) Inductive Matrix definition.
189176data Matrix e cols rows where
177+ Empty :: Matrix e Void Void
190178 One :: e -> Matrix e () ()
191179 Join :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
192180 Fork :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
@@ -216,6 +204,7 @@ type family Count (d :: Type) :: Nat where
216204--
217205-- Thanks to Li-Yao Xia this type family is super fast.
218206type family FromNat (n :: Nat) :: Type where
207+ FromNat 0 = Void
219208 FromNat 1 = ()
220209 FromNat n = FromNat' (Mod n 2 == 0) (FromNat (Div n 2))
221210
@@ -232,26 +221,29 @@ type family Normalize (d :: Type) :: Type where
232221-- | Constraint type synonyms to keep the type signatures less convoluted
233222type Countable a = KnownNat (Count a)
234223type 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)
224+type CountableDimensions a b = (Countable a, Countable b)
225+type CountableDimensionsN a b = (CountableN a, CountableN b)
226+type FromListsN e a b = FromLists e (Normalize a) (Normalize b)
239227type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
240228type Trivial a = FromNat (Count a) ~ a
241229
242−-- | It is possible to implement a constrained version of the category type
243−-- class.
230+-- | It isn't possible to implement the 'id' function so it's
231+-- implementation is 'undefined'. However 'comp' can be and this partial
232+-- class implementation exists just to make the code more readable.
233+--
234+-- Please use 'iden' instead.
244235instance (Num e) => Category (Matrix e) where
245− type Object (Matrix e) a = (FL a a, Countable a)
246− id = iden
247− (.) = comp
236+ id = undefined
237+ (.) = comp
248238
249239instance NFData e => NFData (Matrix e cols rows) where
240+ rnf Empty = ()
250241 rnf (One e) = rnf e
251242 rnf (Join a b) = rnf a `seq` rnf b
252243 rnf (Fork a b) = rnf a `seq` rnf b
253244
254245instance Eq e => Eq (Matrix e cols rows) where
246+ Empty == Empty = True
255247 (One a) == (One b) = a == b
256248 (Join a b) == (Join c d) = a == c && b == d
257249 (Fork a b) == (Fork c d) = a == c && b == d
@@ -259,21 +251,25 @@ instance Eq e => Eq (Matrix e cols rows) where
259251 x@(Join _ _) == y@(Fork _ _) = abideJF x == y
260252
261253instance Num e => Num (Matrix e cols rows) where
254+
262255 a + b = zipWithM (+) a b
263256
264257 a - b = zipWithM (-) a b
265258
266259 a * b = zipWithM (*) a b
267260
261+ abs Empty = Empty
268262 abs (One a) = One (abs a)
269263 abs (Join a b) = Join (abs a) (abs b)
270264 abs (Fork a b) = Fork (abs a) (abs b)
271265
266+ signum Empty = Empty
272267 signum (One a) = One (signum a)
273268 signum (Join a b) = Join (signum a) (signum b)
274269 signum (Fork a b) = Fork (signum a) (signum b)
275270
276271instance Ord e => Ord (Matrix e cols rows) where
272+ Empty <= Empty = True
277273 (One a) <= (One b) = a <= b
278274 (Join a b) <= (Join c d) = (a <= c) && (b <= d)
279275 (Fork a b) <= (Fork c d) = (a <= c) && (b <= d)
@@ -282,6 +278,10 @@ instance Ord e => Ord (Matrix e cols rows) where
282278
283279-- Primitives
284280
281+-- | Empty matrix constructor
282+empty :: Matrix e Void Void
283+empty = Empty
284+
285285-- | Unit matrix constructor
286286one :: e -> Matrix e () ()
287287one = One
@@ -312,39 +312,40 @@ infixl 2 ===
312312--
313313-- Given that it is not possible to branch on types at the term level type
314314-- classes are needed very much like an inductive definition but on types.
315−class FromLists cols rows where
315+class FromLists e cols rows where
316316 -- | Build a matrix out of a list of list of elements. Throws a runtime
317317 -- error if the dimensions do not match.
318318 fromLists :: [[e]] -> Matrix e cols rows
319319
320−instance FromLists () () where
320+instance FromLists e Void Void where
321+ fromLists [] = Empty
322+ fromLists _ = error "Wrong dimensions"
323+
324+instance {-# OVERLAPPING #-} FromLists e () () where
321325 fromLists [[e]] = One e
322326 fromLists _ = error "Wrong dimensions"
323327
324−instance (FromLists cols ()) => FromLists (Either () cols) () where
328+instance {-# OVERLAPPING #-} (FromLists e cols ()) => FromLists e (Either () cols) () where
325329 fromLists [h : t] = Join (One h) (fromLists [t])
326330 fromLists _ = error "Wrong dimensions"
327331
328−instance {-# OVERLAPPABLE #-}
329− (FromLists a (), FromLists b (), Countable a) => FromLists (Either a b) () where
332+instance {-# OVERLAPPABLE #-} (FromLists e a (), FromLists e b (), Countable a) => FromLists e (Either a b) () where
330333 fromLists [l] =
331334 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
332335 in Join (fromLists [take rowsA l]) (fromLists [drop rowsA l])
333336 fromLists _ = error "Wrong dimensions"
334337
335−instance (FromLists () rows) => FromLists () (Either () rows) where
338+instance {-# OVERLAPPING #-} (FromLists e () rows) => FromLists e () (Either () rows) where
336339 fromLists ([h] : t) = Fork (One h) (fromLists t)
337340 fromLists _ = error "Wrong dimensions"
338341
339−instance {-# OVERLAPPABLE #-}
340− (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
341− fromLists l@([_] : _) =
342+instance {-# OVERLAPPABLE #-} (FromLists e () a, FromLists e () b, Countable a) => FromLists e () (Either a b) where
343+ fromLists l@([_] : _) =
342344 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
343345 in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
344346 fromLists _ = error "Wrong dimensions"
345347
346−instance (FromLists (Either a b) c, FromLists (Either a b) d, Countable c)
347− => FromLists (Either a b) (Either c d) where
348+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
348349 fromLists l@(h : t) =
349350 let lh = length h
350351 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
@@ -357,8 +358,8 @@ instance (FromLists (Either a b) c, FromLists (Either a b) d, Countable c)
357358-- a construction function that operates with indices.
358359matrixBuilder' ::
359360 forall e cols rows.
360− ( FL cols rows,
361− CountableDims cols rows
361+ ( FromLists e cols rows,
362+ CountableDimensions cols rows
362363 ) => ((Int, Int) -> e) -> Matrix e cols rows
363364matrixBuilder' f =
364365 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
@@ -370,28 +371,24 @@ matrixBuilder' f =
370371-- a construction function that operates with arbitrary types.
371372matrixBuilder ::
372373 forall e a b.
373− ( FLN a b,
374+ ( FromListsN e a 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− positions = [(a, b) | a <- [minBound .. maxBound], b <- [minBound .. maxBound]]
384− in fromLists . map (map f) . transpose . buildList r $ positions
385− where
386− buildList _ [] = []
387− buildList r l = take r l : buildList r (drop r l)
383+ let positions = [(a, b) | a <- [minBound .. maxBound], b <- [minBound .. maxBound]]
384+ in fromLists . map (map f) . groupBy (\(x, _) (w, _) -> x == w) $ positions
388385
389386-- | Constructs a column vector matrix
390−col :: (FL () rows) => [e] -> Matrix e () rows
387+col :: (FromLists e () rows) => [e] -> Matrix e () rows
391388col = fromLists . map (: [])
392389
393390-- | Constructs a row vector matrix
394−row :: (FL cols ()) => [e] -> Matrix e cols ()
391+row :: (FromLists e cols ()) => [e] -> Matrix e cols ()
395392row = fromLists . (: [])
396393
397394-- | Lifts functions to matrices with arbitrary dimensions.
@@ -401,8 +398,8 @@ row = fromLists . (: [])
401398fromF' ::
402399 forall a b cols rows e.
403400 ( Liftable e a b,
404− CountableDims cols rows,
405− FL rows cols
401+ CountableDimensions cols rows,
402+ FromLists e rows cols
406403 ) =>
407404 (a -> b) ->
408405 Matrix e cols rows
@@ -430,17 +427,35 @@ fromF' f =
430427fromF ::
431428 forall a b e.
432429 ( Liftable e a b,
433− CountableDimsN a b,
434− FLN b a
430+ CountableDimensionsN a b,
431+ FromListsN e b a
435432 ) =>
436433 (a -> b) ->
437434 Matrix e (Normalize a) (Normalize b)
438−fromF = fromF'
435+fromF f =
436+ let minA = minBound @a
437+ maxA = maxBound @a
438+ minB = minBound @b
439+ maxB = maxBound @b
440+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
441+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
442+ elementsA = take ccols [minA .. maxA]
443+ elementsB = take rrows [minB .. maxB]
444+ combinations = (,) <$> elementsA <*> elementsB
445+ combAp = map snd . sort . map (\(a, b) -> if f a == b
446+ then ((fromEnum a, fromEnum b), 1)
447+ else ((fromEnum a, fromEnum b), 0)) $ combinations
448+ mList = buildList combAp rrows
449+ in tr $ fromLists mList
450+ where
451+ buildList [] _ = []
452+ buildList l r = take r l : buildList (drop r l) r
439453
440454-- Conversion
441455
442456-- | Converts a matrix to a list of lists of elements.
443457toLists :: Matrix e cols rows -> [[e]]
458+toLists Empty = []
444459toLists (One e) = [[e]]
445460toLists (Fork l r) = toLists l ++ toLists r
446461toLists (Join l r) = zipWith (++) (toLists l) (toLists r)
@@ -452,7 +467,7 @@ toList = concat . toLists
452467-- Zeros Matrix
453468
454469-- | The zero matrix. A matrix wholly filled with zeros.
455−zeros :: (Num e, FL cols rows, CountableDims cols rows) => Matrix e cols rows
470+zeros :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
456471zeros = matrixBuilder' (const 0)
457472
458473-- Ones Matrix
@@ -460,20 +475,20 @@ zeros = matrixBuilder' (const 0)
460475-- | The ones matrix. A matrix wholly filled with ones.
461476--
462477-- Also known as T (Top) matrix.
463−ones :: (Num e, FL cols rows, CountableDims cols rows) => Matrix e cols rows
478+ones :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
464479ones = matrixBuilder' (const 1)
465480
466481-- Const Matrix
467482
468483-- | The constant matrix constructor. A matrix wholly filled with a given
469484-- value.
470−constant :: (Num e, FL cols rows, CountableDims cols rows) => e -> Matrix e cols rows
485+constant :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => e -> Matrix e cols rows
471486constant e = matrixBuilder' (const e)
472487
473488-- Bang Matrix
474489
475490-- | The T (Top) row vector matrix.
476−bang :: forall e cols. (Num e, Enum e, FL cols (), Countable cols) => Matrix e cols ()
491+bang :: forall e cols. (Num e, Enum e, FromLists e cols (), Countable cols) => Matrix e cols ()
477492bang =
478493 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
479494 in fromLists [take c [1, 1 ..]]
@@ -481,7 +496,7 @@ bang =
481496-- iden Matrix
482497
483498-- | iden matrix.
484−iden :: forall e cols . (Num e, FL cols cols, Countable cols) => Matrix e cols cols
499+iden :: (Num e, FromLists e cols cols, Countable cols) => Matrix e cols cols
485500iden = matrixBuilder' (bool 0 1 . uncurry (==))
486501{-# NOINLINE iden #-}
487502
@@ -492,12 +507,13 @@ iden = matrixBuilder' (bool 0 1 . uncurry (==))
492507-- This definition takes advantage of divide-and-conquer and fusion laws
493508-- from LAoP.
494509comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
510+comp Empty Empty = Empty
495511comp (One a) (One b) = One (a * b)
496512comp (Join a b) (Fork c d) = comp a c + comp b d -- Divide-and-conquer law
497513comp (Fork a b) c = Fork (comp a c) (comp b c) -- Fork fusion law
498514comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
499515{-# NOINLINE comp #-}
500−{-# RULES
516+{-# RULES
501517 "comp/iden1" forall m. comp m iden = m ;
502518 "comp/iden2" forall m. comp iden m = m
503519#-}
@@ -507,6 +523,7 @@ comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
507523infixl 7 .|
508524-- | Scalar multiplication of matrices.
509525(.|) :: Num e => e -> Matrix e cols rows -> Matrix e cols rows
526+(.|) _ Empty = Empty
510527(.|) e (One a) = One (e * a)
511528(.|) e (Join a b) = Join (e .| a) (e .| b)
512529(.|) e (Fork a b) = Fork (e .| a) (e .| b)
@@ -516,6 +533,7 @@ infixl 7 .|
516533infixl 7 ./
517534-- | Scalar multiplication of matrices.
518535(./) :: Fractional e => Matrix e cols rows -> e -> Matrix e cols rows
536+(./) Empty _ = Empty
519537(./) (One a) e = One (a / e)
520538(./) (Join a b) e = Join (a ./ e) (b ./ e)
521539(./) (Fork a b) e = Fork (a ./ e) (b ./ e)
@@ -523,59 +541,45 @@ infixl 7 ./
523541-- Projections
524542
525543-- | Biproduct first component projection
526−p1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e (Either m n) m
527−p1 = join id zeros
544+p1 :: (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e (Either m n) m
545+p1 = join iden zeros
528546
529547-- | Biproduct second component projection
530−p2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e (Either m n) n
531−p2 = join zeros id
548+p2 :: (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e (Either m n) n
549+p2 = join zeros iden
532550
533551-- Injections
534552
535553-- | Biproduct first component injection
536−i1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e m (Either m n)
554+i1 :: (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e m (Either m n)
537555i1 = tr p1
538556
539557-- | Biproduct second component injection
540−i2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e n (Either m n)
558+i2 :: (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e n (Either m n)
541559i2 = tr p2
542560
543561-- Dimensions
544562
545563-- | Obtain the number of rows.
546564--
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''.
565+-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
566+-- dimensions in constant time.
567+--
568+-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
569+-- exchange for performance.
550570rows :: forall e cols rows. (Countable rows) => Matrix e cols rows -> Int
551571rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
552572
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−
562573-- | Obtain the number of columns.
574+--
575+-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
576+-- dimensions in constant time.
563577--
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''.
578+-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
579+-- exchange for performance.
567580columns :: forall e cols rows. (Countable cols) => Matrix e cols rows -> Int
568581columns _ = fromInteger $ natVal (Proxy :: Proxy (Count cols))
569582
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−
579583-- Coproduct Bifunctor
580584
581585infixl 5 -|-
@@ -584,11 +588,11 @@ infixl 5 -|-
584588(-|-) ::
585589 forall e n k m j.
586590 ( Num e,
587− CountableDims j k,
588− FL k k,
589− FL j k,
590− FL k j,
591− FL j j
591+ CountableDimensions j k,
592+ FromLists e k k,
593+ FromLists e j k,
594+ FromLists e k j,
595+ FromLists e j j
592596 ) =>
593597 Matrix e n k ->
594598 Matrix e m j ->
@@ -601,8 +605,8 @@ infixl 5 -|-
601605fstM ::
602606 forall e m k .
603607 ( Num e,
604− CountableDims k m,
605− FL (Normalize (m, k)) m,
608+ CountableDimensions k m,
609+ FromLists e (Normalize (m, k)) m,
606610 CountableN (m, k)
607611 ) => Matrix e (Normalize (m, k)) m
608612fstM = matrixBuilder' f
@@ -616,8 +620,8 @@ fstM = matrixBuilder' f
616620sndM ::
617621 forall e m k .
618622 ( Num e,
619− CountableDims k m,
620− FL (Normalize (m, k)) k,
623+ CountableDimensions k m,
624+ FromLists e (Normalize (m, k)) k,
621625 CountableN (m, k)
622626 ) => Matrix e (Normalize (m, k)) k
623627sndM = matrixBuilder' f
@@ -630,7 +634,7 @@ sndM = matrixBuilder' f
630634-- | Khatri Rao Matrix product also known as matrix pairing.
631635--
632636-- NOTE: That this is not a true categorical product, see for instance:
633−--
637+--
634638-- @
635639-- | fstM . kr a b == a
636640-- kr a b ==> |
@@ -641,10 +645,10 @@ sndM = matrixBuilder' f
641645kr ::
642646 forall e cols a b.
643647 ( Num e,
644− CountableDims a b,
648+ CountableDimensions a b,
645649 CountableN (a, b),
646− FL (Normalize (a, b)) a,
647− FL (Normalize (a, b)) b
650+ FromLists e (Normalize (a, b)) a,
651+ FromLists e (Normalize (a, b)) b
648652 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (Normalize (a, b))
649653kr a b =
650654 let fstM' = fstM @e @a @b
@@ -659,14 +663,14 @@ infixl 4 ><
659663(><) ::
660664 forall e m p n q.
661665 ( 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− )
666+ CountableDimensions m n,
667+ CountableDimensions p q,
668+ CountableDimensionsN (m, n) (p, q),
669+ FromLists e (Normalize (m, n)) m,
670+ FromLists e (Normalize (m, n)) n,
671+ FromLists e (Normalize (p, q)) p,
672+ FromLists e (Normalize (p, q)) q
673+ )
670674 => Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
671675(><) a b =
672676 let fstM' = fstM @e @m @n
@@ -684,6 +688,7 @@ infixl 4 ><
684688-- @
685689abideJF :: Matrix e cols rows -> Matrix e cols rows
686690abideJF (Join (Fork a c) (Fork b d)) = Fork (Join (abideJF a) (abideJF b)) (Join (abideJF c) (abideJF d)) -- Join-Fork abide law
691+abideJF Empty = Empty
687692abideJF (One e) = One e
688693abideJF (Join a b) = Join (abideJF a) (abideJF b)
689694abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
@@ -697,6 +702,7 @@ abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
697702-- @
698703abideFJ :: Matrix e cols rows -> Matrix e cols rows
699704abideFJ (Fork (Join a b) (Join c d)) = Join (Fork (abideFJ a) (abideFJ c)) (Fork (abideFJ b) (abideFJ d)) -- Fork-Join abide law
705+abideFJ Empty = Empty
700706abideFJ (One e) = One e
701707abideFJ (Join a b) = Join (abideFJ a) (abideFJ b)
702708abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
@@ -705,6 +711,7 @@ abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
705711
706712-- | Matrix transposition.
707713tr :: Matrix e cols rows -> Matrix e rows cols
714+tr Empty = Empty
708715tr (One e) = One e
709716tr (Join a b) = Fork (tr a) (tr b)
710717tr (Fork a b) = Join (tr a) (tr b)
@@ -713,32 +720,32 @@ tr (Fork a b) = Join (tr a) (tr b)
713720
714721-- | Selective functors 'select' operator equivalent inspired by the
715722-- 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
723+select :: (Num e, FromLists e b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
717724select (Fork a b) y = y . a + b -- Divide-and-conquer law
718725select (Join (Fork a c) (Fork b d)) y = join (y . a + c) (y . b + d) -- Pattern matching + DnC law
719−select m y = join y id . m
726+select m y = join y iden . m
720727
721728branch ::
722729 ( 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
730+ CountableDimensions a b,
731+ CountableDimensions c (Either b c),
732+ FromLists e c b,
733+ FromLists e a b,
734+ FromLists e a a,
735+ FromLists e b b,
736+ FromLists e c c,
737+ FromLists e b a,
738+ FromLists e b c,
739+ FromLists e (Either b c) b,
740+ FromLists e (Either b c) c
734741 )
735742 => Matrix e cols (Either a b) -> Matrix e a c -> Matrix e b c -> Matrix e cols c
736743branch x l r = f x `select` g l `select` r
737744 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)
745+ 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)
739746 => Matrix e cols (Either a b) -> Matrix e cols (Either a (Either b c))
740747 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)
748+ g :: (Num e, CountableDimensions b c, FromLists e b c, FromLists e c c) => Matrix e a c -> Matrix e a (Either b c)
742749 g m = i2 . m
743750
744751-- McCarthy's Conditional
@@ -747,9 +754,9 @@ branch x l r = f x `select` g l `select` r
747754cond ::
748755 ( Trivial cols,
749756 Countable cols,
750− FL () cols,
751− FL cols (),
752− FL cols cols,
757+ FromLists e () cols,
758+ FromLists e cols (),
759+ FromLists e cols cols,
753760 Bounded a,
754761 Enum a,
755762 Num e,
@@ -762,9 +769,9 @@ cond p f g = join f g . grd p
762769grd ::
763770 ( Trivial q,
764771 Countable q,
765− FL () q,
766− FL q (),
767− FL q q,
772+ FromLists e () q,
773+ FromLists e q (),
774+ FromLists e q q,
768775 Bounded a,
769776 Enum a,
770777 Num e,
@@ -778,14 +785,14 @@ corr ::
778785 forall e a q .
779786 ( Trivial q,
780787 Countable q,
781− FL () q,
782− FL q (),
783− FL q q,
788+ FromLists e () q,
789+ FromLists e q (),
790+ FromLists e q q,
784791 Liftable e a Bool
785792 )
786793 => (a -> Bool) -> Matrix e q q
787794corr p = let f = fromF' p :: Matrix e q ()
788− in kr f (id :: Matrix e q q)
795+ in kr f (iden :: Matrix e q q)
789796
790797-- Pretty print
791798
@@ -809,7 +816,7 @@ prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
809816 fill str = replicate (widest - length str - 2) ' ' ++ str
810817
811818-- | Matrix pretty printer
812−pretty :: (CountableDims cols rows, Show e) => Matrix e cols rows -> String
819+pretty :: (CountableDimensions cols rows, Show e) => Matrix e cols rows -> String
813820pretty m = concat
814821 [ "┌ ", unwords (replicate (columns m) blank), " ┐\n"
815822 , unlines
@@ -838,11 +845,12 @@ pretty m = concat
838845 (safeGet i j m)
839846
840847-- | Matrix pretty printer
841−prettyPrint :: (CountableDims cols rows, Show e) => Matrix e cols rows -> IO ()
848+prettyPrint :: (CountableDimensions cols rows, Show e) => Matrix e cols rows -> IO ()
842849prettyPrint = putStrLn . pretty
843850
844851-- | Zip two matrices with a given binary function
845852zipWithM :: (e -> f -> g) -> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
853+zipWithM _ Empty Empty = Empty
846854zipWithM f (One a) (One b) = One (f a b)
847855zipWithM f (Join a b) (Join c d) = Join (zipWithM f a c) (zipWithM f b d)
848856zipWithM f (Fork a b) (Fork c d) = Fork (zipWithM f a c) (zipWithM f b d)
@@ -867,12 +875,14 @@ fromBool False = nat 0
867875
868876-- | Relational negation
869877negateM :: Relation cols rows -> Relation cols rows
878+negateM Empty = Empty
870879negateM (One (Nat p)) = One (Nat (negate p))
871880negateM (Join a b) = Join (negateM a) (negateM b)
872881negateM (Fork a b) = Fork (negateM a) (negateM b)
873882
874883-- | Relational addition
875884orM :: Relation cols rows -> Relation cols rows -> Relation cols rows
885+orM Empty Empty = Empty
876886orM (One a) (One b) = One (fromBool (toBool a || toBool b))
877887orM (Join a b) (Join c d) = Join (orM a c) (orM b d)
878888orM (Fork a b) (Fork c d) = Fork (orM a c) (orM b d)
@@ -881,6 +891,7 @@ orM x@(Join _ _) y@(Fork _ _) = orM (abideJF x) y
881891
882892-- | Relational multiplication
883893andM :: Relation cols rows -> Relation cols rows -> Relation cols rows
894+andM Empty Empty = Empty
884895andM (One a) (One b) = One (fromBool (toBool a && toBool b))
885896andM (Join a b) (Join c d) = Join (andM a c) (andM b d)
886897andM (Fork a b) (Fork c d) = Fork (andM a c) (andM b d)
@@ -889,6 +900,7 @@ andM x@(Join _ _) y@(Fork _ _) = andM (abideJF x) y
889900
890901-- | Relational subtraction
891902subM :: Relation cols rows -> Relation cols rows -> Relation cols rows
903+subM Empty Empty = Empty
892904subM (One a) (One b) = if a - b < nat 0 then One (nat 0) else One (a - b)
893905subM (Join a b) (Join c d) = Join (subM a c) (subM b d)
894906subM (Fork a b) (Fork c d) = Fork (subM a c) (subM b d)
@@ -897,6 +909,7 @@ subM x@(Join _ _) y@(Fork _ _) = subM (abideJF x) y
897909
898910-- | Matrix relational composition.
899911compRel :: Relation cr rows -> Relation cols cr -> Relation cols rows
912+compRel Empty Empty = Empty
900913compRel (One a) (One b) = One (fromBool (toBool a && toBool b))
901914compRel (Join a b) (Fork c d) = orM (compRel a c) (compRel b d) -- Divide-and-conquer law
902915compRel (Fork a b) c = Fork (compRel a c) (compRel b c) -- Fork fusion law
@@ -904,6 +917,7 @@ compRel c (Join a b) = Join (compRel c a) (compRel c b) -- Join fusion
904917
905918-- | Matrix relational right division
906919divR :: Relation b c -> Relation b a -> Relation a c
920+divR Empty Empty = Empty
907921divR (One a) (One b) = One (fromBool (not (toBool b) || toBool a)) -- b implies a
908922divR (Join a b) (Join c d) = andM (divR a c) (divR b d)
909923divR (Fork a b) c = Fork (divR a c) (divR b c)
@@ -926,24 +940,58 @@ divS s r = divL r s `intersection` divR (tr r) (tr s)
926940fromFRel' ::
927941 forall a b cols rows.
928942 ( Liftable Boolean a b,
929− CountableDims cols rows,
930− FL rows cols
943+ CountableDimensions cols rows,
944+ FromLists Boolean rows cols
931945 ) =>
932946 (a -> b) ->
933947 Relation cols rows
934−fromFRel' = fromF'
948+fromFRel' f =
949+ let minA = minBound @a
950+ maxA = maxBound @a
951+ minB = minBound @b
952+ maxB = maxBound @b
953+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count cols))
954+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count rows))
955+ elementsA = take ccols [minA .. maxA]
956+ elementsB = take rrows [minB .. maxB]
957+ combinations = (,) <$> elementsA <*> elementsB
958+ combAp = map snd . sort . map (\(a, b) -> if f a == b
959+ then ((fromEnum a, fromEnum b), nat 1)
960+ else ((fromEnum a, fromEnum b), nat 0)) $ combinations
961+ mList = buildList combAp rrows
962+ in tr $ fromLists mList
963+ where
964+ buildList [] _ = []
965+ buildList l r = take r l : buildList (drop r l) r
935966
936967-- | Lifts functions to relations with dimensions matching @a@ and @b@
937968-- cardinality's.
938969fromFRel ::
939970 forall a b.
940971 ( Liftable Boolean a b,
941− CountableDimsN a b,
942− FLN b a
972+ CountableDimensionsN a b,
973+ FromLists Boolean (Normalize b) (Normalize a)
943974 ) =>
944975 (a -> b) ->
945976 Relation (Normalize a) (Normalize b)
946−fromFRel = fromFRel'
977+fromFRel f =
978+ let minA = minBound @a
979+ maxA = maxBound @a
980+ minB = minBound @b
981+ maxB = maxBound @b
982+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
983+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
984+ elementsA = take ccols [minA .. maxA]
985+ elementsB = take rrows [minB .. maxB]
986+ combinations = (,) <$> elementsA <*> elementsB
987+ combAp = map snd . sort . map (\(a, b) -> if f a == b
988+ then ((fromEnum a, fromEnum b), nat 1)
989+ else ((fromEnum a, fromEnum b), nat 0)) $ combinations
990+ mList = buildList combAp rrows
991+ in tr $ fromLists mList
992+ where
993+ buildList [] _ = []
994+ buildList l r = take r l : buildList (drop r l) r
947995
948996-- | Lifts a relation function to a Boolean Matrix
949997toRel ::
@@ -953,17 +1001,17 @@ toRel ::
9531001 Enum a,
9541002 Enum b,
9551003 Eq b,
956− CountableDims a b,
957− FLN b a
958− )
1004+ CountableDimensionsN a b,
1005+ FromListsN Boolean b a
1006+ )
9591007 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
9601008toRel f =
9611009 let minA = minBound @a
9621010 maxA = maxBound @a
9631011 minB = minBound @b
9641012 maxB = maxBound @b
965− ccols = fromInteger $ natVal (Proxy :: Proxy (Count a))
966− rrows = fromInteger $ natVal (Proxy :: Proxy (Count b))
1013+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
1014+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
9671015 elementsA = take ccols [minA .. maxA]
9681016 elementsB = take rrows [minB .. maxB]
9691017 combinations = (,) <$> elementsA <*> elementsB