@@ -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,
@@ -75,7 +66,6 @@ module LAoP.Matrix.Internal
7566 toLists,
7667 toList,
7768 matrixBuilder,
78− matrixBuilder',
7969 row,
8070 col,
8171 zeros,
@@ -86,9 +76,7 @@ module LAoP.Matrix.Internal
8676 -- * Misc
8777 -- ** Get dimensions
8878 columns,
89− columns',
9079 rows,
91− rows',
9280
9381 -- ** Matrix Transposition
9482 tr,
@@ -130,7 +118,6 @@ module LAoP.Matrix.Internal
130118
131119 -- | Note that given the restrictions imposed it is not possible to
132120 -- implement the standard type classes present in standard Haskell.
133−
134121 -- *** Matrix pairing projections
135122 fstM,
136123 sndM,
@@ -176,17 +163,17 @@ import Data.Kind
176163import Data.List
177164import Data.Maybe
178165import Data.Proxy
179−import Data.Typeable
180166import Data.Void
181167import GHC.TypeLits
182168import Data.Type.Equality
183169import GHC.Generics
184170import Control.DeepSeq
185−import Prelude hiding (id, (.))
186−import qualified Prelude (id, (.))
171+import Control.Category
172+import Prelude hiding ((.))
187173
188174-- | LAoP (Linear Algebra of Programming) Inductive Matrix definition.
189175data Matrix e cols rows where
176+ Empty :: Matrix e Void Void
190177 One :: e -> Matrix e () ()
191178 Join :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
192179 Fork :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
@@ -216,6 +203,7 @@ type family Count (d :: Type) :: Nat where
216203--
217204-- Thanks to Li-Yao Xia this type family is super fast.
218205type family FromNat (n :: Nat) :: Type where
206+ FromNat 0 = Void
219207 FromNat 1 = ()
220208 FromNat n = FromNat' (Mod n 2 == 0) (FromNat (Div n 2))
221209
@@ -232,26 +220,29 @@ type family Normalize (d :: Type) :: Type where
232220-- | Constraint type synonyms to keep the type signatures less convoluted
233221type Countable a = KnownNat (Count a)
234222type 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)
223+type CountableDimensions a b = (Countable a, Countable b)
224+type CountableDimensionsN a b = (CountableN a, CountableN b)
225+type FromListsN e a b = FromLists e (Normalize a) (Normalize b)
239226type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
240227type Trivial a = FromNat (Count a) ~ a
241228
242−-- | It is possible to implement a constrained version of the category type
243−-- class.
229+-- | It isn't possible to implement the 'id' function so it's
230+-- implementation is 'undefined'. However 'comp' can be and this partial
231+-- class implementation exists just to make the code more readable.
232+--
233+-- Please use 'iden' instead.
244234instance (Num e) => Category (Matrix e) where
245− type Object (Matrix e) a = (FL a a, Countable a)
246− id = iden
247− (.) = comp
235+ id = undefined
236+ (.) = 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)))
@@ -354,44 +354,24 @@ instance (FromLists (Either a b) c, FromLists (Either a b) d, Countable c)
354354 else error "Not all rows have the same length"
355355
356356-- | Matrix builder function. Constructs a matrix provided with
357−-- a construction function that operates with indices.
358−matrixBuilder' ::
357+-- a construction function.
358+matrixBuilder ::
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
363−matrixBuilder' f =
363+matrixBuilder f =
364364 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
365365 r = fromInteger $ natVal (Proxy :: Proxy (Count rows))
366366 positions = [(a, b) | a <- [0 .. (r - 1)], b <- [0 .. (c - 1)]]
367367 in fromLists . map (map f) . groupBy (\(x, _) (w, _) -> x == w) $ positions
368368
369−-- | Matrix builder function. Constructs a matrix provided with
370−-- a construction function that operates with arbitrary types.
371−matrixBuilder ::
372− forall e a b.
373− ( FLN a b,
374− Enum a,
375− Enum b,
376− Bounded a,
377− Bounded b,
378− Eq a,
379− Countable b
380− ) => ((a, b) -> e) -> Matrix e (Normalize a) (Normalize b)
381−matrixBuilder 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)
388−
389369-- | Constructs a column vector matrix
390−col :: (FL () rows) => [e] -> Matrix e () rows
370+col :: (FromLists e () rows) => [e] -> Matrix e () rows
391371col = fromLists . map (: [])
392372
393373-- | Constructs a row vector matrix
394−row :: (FL cols ()) => [e] -> Matrix e cols ()
374+row :: (FromLists e cols ()) => [e] -> Matrix e cols ()
395375row = fromLists . (: [])
396376
397377-- | Lifts functions to matrices with arbitrary dimensions.
@@ -401,8 +381,8 @@ row = fromLists . (: [])
401381fromF' ::
402382 forall a b cols rows e.
403383 ( Liftable e a b,
404− CountableDims cols rows,
405− FL rows cols
384+ CountableDimensions cols rows,
385+ FromLists e rows cols
406386 ) =>
407387 (a -> b) ->
408388 Matrix e cols rows
@@ -430,17 +410,35 @@ fromF' f =
430410fromF ::
431411 forall a b e.
432412 ( Liftable e a b,
433− CountableDimsN a b,
434− FLN b a
413+ CountableDimensionsN a b,
414+ FromListsN e b a
435415 ) =>
436416 (a -> b) ->
437417 Matrix e (Normalize a) (Normalize b)
438−fromF = fromF'
418+fromF f =
419+ let minA = minBound @a
420+ maxA = maxBound @a
421+ minB = minBound @b
422+ maxB = maxBound @b
423+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
424+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
425+ elementsA = take ccols [minA .. maxA]
426+ elementsB = take rrows [minB .. maxB]
427+ combinations = (,) <$> elementsA <*> elementsB
428+ combAp = map snd . sort . map (\(a, b) -> if f a == b
429+ then ((fromEnum a, fromEnum b), 1)
430+ else ((fromEnum a, fromEnum b), 0)) $ combinations
431+ mList = buildList combAp rrows
432+ in tr $ fromLists mList
433+ where
434+ buildList [] _ = []
435+ buildList l r = take r l : buildList (drop r l) r
439436
440437-- Conversion
441438
442439-- | Converts a matrix to a list of lists of elements.
443440toLists :: Matrix e cols rows -> [[e]]
441+toLists Empty = []
444442toLists (One e) = [[e]]
445443toLists (Fork l r) = toLists l ++ toLists r
446444toLists (Join l r) = zipWith (++) (toLists l) (toLists r)
@@ -452,28 +450,28 @@ toList = concat . toLists
452450-- Zeros Matrix
453451
454452-- | The zero matrix. A matrix wholly filled with zeros.
455−zeros :: (Num e, FL cols rows, CountableDims cols rows) => Matrix e cols rows
456−zeros = matrixBuilder' (const 0)
453+zeros :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
454+zeros = matrixBuilder (const 0)
457455
458456-- Ones Matrix
459457
460458-- | The ones matrix. A matrix wholly filled with ones.
461459--
462460-- Also known as T (Top) matrix.
463−ones :: (Num e, FL cols rows, CountableDims cols rows) => Matrix e cols rows
464−ones = matrixBuilder' (const 1)
461+ones :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
462+ones = matrixBuilder (const 1)
465463
466464-- Const Matrix
467465
468466-- | The constant matrix constructor. A matrix wholly filled with a given
469467-- value.
470−constant :: (Num e, FL cols rows, CountableDims cols rows) => e -> Matrix e cols rows
471−constant e = matrixBuilder' (const e)
468+constant :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => e -> Matrix e cols rows
469+constant e = matrixBuilder (const e)
472470
473471-- Bang Matrix
474472
475473-- | The T (Top) row vector matrix.
476−bang :: forall e cols. (Num e, Enum e, FL cols (), Countable cols) => Matrix e cols ()
474+bang :: forall e cols. (Num e, Enum e, FromLists e cols (), Countable cols) => Matrix e cols ()
477475bang =
478476 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
479477 in fromLists [take c [1, 1 ..]]
@@ -481,8 +479,8 @@ bang =
481479-- iden Matrix
482480
483481-- | iden matrix.
484−iden :: forall e cols . (Num e, FL cols cols, Countable cols) => Matrix e cols cols
485−iden = matrixBuilder' (bool 0 1 . uncurry (==))
482+iden :: (Num e, FromLists e cols cols, Countable cols) => Matrix e cols cols
483+iden = matrixBuilder (bool 0 1 . uncurry (==))
486484{-# NOINLINE iden #-}
487485
488486-- Matrix composition (MMM)
@@ -492,12 +490,13 @@ iden = matrixBuilder' (bool 0 1 . uncurry (==))
492490-- This definition takes advantage of divide-and-conquer and fusion laws
493491-- from LAoP.
494492comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
493+comp Empty Empty = Empty
495494comp (One a) (One b) = One (a * b)
496495comp (Join a b) (Fork c d) = comp a c + comp b d -- Divide-and-conquer law
497496comp (Fork a b) c = Fork (comp a c) (comp b c) -- Fork fusion law
498497comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
499498{-# NOINLINE comp #-}
500−{-# RULES
499+{-# RULES
501500 "comp/iden1" forall m. comp m iden = m ;
502501 "comp/iden2" forall m. comp iden m = m
503502#-}
@@ -507,6 +506,7 @@ comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
507506infixl 7 .|
508507-- | Scalar multiplication of matrices.
509508(.|) :: Num e => e -> Matrix e cols rows -> Matrix e cols rows
509+(.|) _ Empty = Empty
510510(.|) e (One a) = One (e * a)
511511(.|) e (Join a b) = Join (e .| a) (e .| b)
512512(.|) e (Fork a b) = Fork (e .| a) (e .| b)
@@ -516,6 +516,7 @@ infixl 7 .|
516516infixl 7 ./
517517-- | Scalar multiplication of matrices.
518518(./) :: Fractional e => Matrix e cols rows -> e -> Matrix e cols rows
519+(./) Empty _ = Empty
519520(./) (One a) e = One (a / e)
520521(./) (Join a b) e = Join (a ./ e) (b ./ e)
521522(./) (Fork a b) e = Fork (a ./ e) (b ./ e)
@@ -523,59 +524,51 @@ infixl 7 ./
523524-- Projections
524525
525526-- | 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
527+p1 :: forall e m n. (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e (Either m n) m
528+p1 =
529+ let iden = iden :: Matrix e m m
530+ zero = zeros :: Matrix e n m
531+ in join iden zero
528532
529533-- | 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
534+p2 :: forall e m n. (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e (Either m n) n
535+p2 =
536+ let iden = iden :: Matrix e n n
537+ zero = zeros :: Matrix e m n
538+ in join zero iden
532539
533540-- Injections
534541
535542-- | Biproduct first component injection
536−i1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e m (Either m n)
543+i1 :: (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e m (Either m n)
537544i1 = tr p1
538545
539546-- | Biproduct second component injection
540−i2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e n (Either m n)
547+i2 :: (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e n (Either m n)
541548i2 = tr p2
542549
543550-- Dimensions
544551
545552-- | Obtain the number of rows.
546553--
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''.
554+-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
555+-- dimensions in constant time.
556+--
557+-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
558+-- exchange for performance.
550559rows :: forall e cols rows. (Countable rows) => Matrix e cols rows -> Int
551560rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
552561
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−
562562-- | Obtain the number of columns.
563+--
564+-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
565+-- dimensions in constant time.
563566--
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''.
567+-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
568+-- exchange for performance.
567569columns :: forall e cols rows. (Countable cols) => Matrix e cols rows -> Int
568570columns _ = fromInteger $ natVal (Proxy :: Proxy (Count cols))
569571
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−
579572-- Coproduct Bifunctor
580573
581574infixl 5 -|-
@@ -584,11 +577,11 @@ infixl 5 -|-
584577(-|-) ::
585578 forall e n k m j.
586579 ( Num e,
587− CountableDims j k,
588− FL k k,
589− FL j k,
590− FL k j,
591− FL j j
580+ CountableDimensions j k,
581+ FromLists e k k,
582+ FromLists e j k,
583+ FromLists e k j,
584+ FromLists e j j
592585 ) =>
593586 Matrix e n k ->
594587 Matrix e m j ->
@@ -601,11 +594,11 @@ infixl 5 -|-
601594fstM ::
602595 forall e m k .
603596 ( Num e,
604− CountableDims k m,
605− FL (Normalize (m, k)) m,
597+ CountableDimensions k m,
598+ FromLists e (Normalize (m, k)) m,
606599 CountableN (m, k)
607600 ) => Matrix e (Normalize (m, k)) m
608−fstM = matrixBuilder' f
601+fstM = matrixBuilder f
609602 where
610603 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
611604 f (x, y)
@@ -616,11 +609,11 @@ fstM = matrixBuilder' f
616609sndM ::
617610 forall e m k .
618611 ( Num e,
619− CountableDims k m,
620− FL (Normalize (m, k)) k,
612+ CountableDimensions k m,
613+ FromLists e (Normalize (m, k)) k,
621614 CountableN (m, k)
622615 ) => Matrix e (Normalize (m, k)) k
623−sndM = matrixBuilder' f
616+sndM = matrixBuilder f
624617 where
625618 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
626619 f (x, y)
@@ -630,7 +623,7 @@ sndM = matrixBuilder' f
630623-- | Khatri Rao Matrix product also known as matrix pairing.
631624--
632625-- NOTE: That this is not a true categorical product, see for instance:
633−--
626+--
634627-- @
635628-- | fstM . kr a b == a
636629-- kr a b ==> |
@@ -641,15 +634,15 @@ sndM = matrixBuilder' f
641634kr ::
642635 forall e cols a b.
643636 ( Num e,
644− CountableDims a b,
637+ CountableDimensions a b,
645638 CountableN (a, b),
646− FL (Normalize (a, b)) a,
647− FL (Normalize (a, b)) b
639+ FromLists e (Normalize (a, b)) a,
640+ FromLists e (Normalize (a, b)) b
648641 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (Normalize (a, b))
649642kr a b =
650643 let fstM' = fstM @e @a @b
651644 sndM' = sndM @e @a @b
652− in (tr fstM' . a) * (tr sndM' . b)
645+ in tr fstM' . a * tr sndM' . b
653646
654647-- Product Bifunctor (Kronecker)
655648
@@ -659,14 +652,14 @@ infixl 4 ><
659652(><) ::
660653 forall e m p n q.
661654 ( 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− )
655+ CountableDimensions m n,
656+ CountableDimensions p q,
657+ CountableDimensionsN (m, n) (p, q),
658+ FromLists e (Normalize (m, n)) m,
659+ FromLists e (Normalize (m, n)) n,
660+ FromLists e (Normalize (p, q)) p,
661+ FromLists e (Normalize (p, q)) q
662+ )
670663 => Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
671664(><) a b =
672665 let fstM' = fstM @e @m @n
@@ -684,6 +677,7 @@ infixl 4 ><
684677-- @
685678abideJF :: Matrix e cols rows -> Matrix e cols rows
686679abideJF (Join (Fork a c) (Fork b d)) = Fork (Join (abideJF a) (abideJF b)) (Join (abideJF c) (abideJF d)) -- Join-Fork abide law
680+abideJF Empty = Empty
687681abideJF (One e) = One e
688682abideJF (Join a b) = Join (abideJF a) (abideJF b)
689683abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
@@ -697,6 +691,7 @@ abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
697691-- @
698692abideFJ :: Matrix e cols rows -> Matrix e cols rows
699693abideFJ (Fork (Join a b) (Join c d)) = Join (Fork (abideFJ a) (abideFJ c)) (Fork (abideFJ b) (abideFJ d)) -- Fork-Join abide law
694+abideFJ Empty = Empty
700695abideFJ (One e) = One e
701696abideFJ (Join a b) = Join (abideFJ a) (abideFJ b)
702697abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
@@ -705,6 +700,7 @@ abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
705700
706701-- | Matrix transposition.
707702tr :: Matrix e cols rows -> Matrix e rows cols
703+tr Empty = Empty
708704tr (One e) = One e
709705tr (Join a b) = Fork (tr a) (tr b)
710706tr (Fork a b) = Join (tr a) (tr b)
@@ -713,32 +709,32 @@ tr (Fork a b) = Join (tr a) (tr b)
713709
714710-- | Selective functors 'select' operator equivalent inspired by the
715711-- 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
712+select :: (Num e, FromLists e b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
717713select (Fork a b) y = y . a + b -- Divide-and-conquer law
718714select (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
715+select m y = join y iden . m
720716
721717branch ::
722718 ( 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
719+ CountableDimensions a b,
720+ CountableDimensions c (Either b c),
721+ FromLists e c b,
722+ FromLists e a b,
723+ FromLists e a a,
724+ FromLists e b b,
725+ FromLists e c c,
726+ FromLists e b a,
727+ FromLists e b c,
728+ FromLists e (Either b c) b,
729+ FromLists e (Either b c) c
734730 )
735731 => Matrix e cols (Either a b) -> Matrix e a c -> Matrix e b c -> Matrix e cols c
736732branch x l r = f x `select` g l `select` r
737733 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)
734+ 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)
739735 => Matrix e cols (Either a b) -> Matrix e cols (Either a (Either b c))
740736 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)
737+ g :: (Num e, CountableDimensions b c, FromLists e b c, FromLists e c c) => Matrix e a c -> Matrix e a (Either b c)
742738 g m = i2 . m
743739
744740-- McCarthy's Conditional
@@ -747,9 +743,9 @@ branch x l r = f x `select` g l `select` r
747743cond ::
748744 ( Trivial cols,
749745 Countable cols,
750− FL () cols,
751− FL cols (),
752− FL cols cols,
746+ FromLists e () cols,
747+ FromLists e cols (),
748+ FromLists e cols cols,
753749 Bounded a,
754750 Enum a,
755751 Num e,
@@ -762,9 +758,9 @@ cond p f g = join f g . grd p
762758grd ::
763759 ( Trivial q,
764760 Countable q,
765− FL () q,
766− FL q (),
767− FL q q,
761+ FromLists e () q,
762+ FromLists e q (),
763+ FromLists e q q,
768764 Bounded a,
769765 Enum a,
770766 Num e,
@@ -778,14 +774,14 @@ corr ::
778774 forall e a q .
779775 ( Trivial q,
780776 Countable q,
781− FL () q,
782− FL q (),
783− FL q q,
777+ FromLists e () q,
778+ FromLists e q (),
779+ FromLists e q q,
784780 Liftable e a Bool
785781 )
786782 => (a -> Bool) -> Matrix e q q
787783corr p = let f = fromF' p :: Matrix e q ()
788− in kr f (id :: Matrix e q q)
784+ in kr f (iden :: Matrix e q q)
789785
790786-- Pretty print
791787
@@ -809,7 +805,7 @@ prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
809805 fill str = replicate (widest - length str - 2) ' ' ++ str
810806
811807-- | Matrix pretty printer
812−pretty :: (CountableDims cols rows, Show e) => Matrix e cols rows -> String
808+pretty :: (CountableDimensions cols rows, Show e) => Matrix e cols rows -> String
813809pretty m = concat
814810 [ "┌ ", unwords (replicate (columns m) blank), " ┐\n"
815811 , unlines
@@ -838,11 +834,12 @@ pretty m = concat
838834 (safeGet i j m)
839835
840836-- | Matrix pretty printer
841−prettyPrint :: (CountableDims cols rows, Show e) => Matrix e cols rows -> IO ()
837+prettyPrint :: (CountableDimensions cols rows, Show e) => Matrix e cols rows -> IO ()
842838prettyPrint = putStrLn . pretty
843839
844840-- | Zip two matrices with a given binary function
845841zipWithM :: (e -> f -> g) -> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
842+zipWithM _ Empty Empty = Empty
846843zipWithM f (One a) (One b) = One (f a b)
847844zipWithM f (Join a b) (Join c d) = Join (zipWithM f a c) (zipWithM f b d)
848845zipWithM f (Fork a b) (Fork c d) = Fork (zipWithM f a c) (zipWithM f b d)
@@ -867,12 +864,14 @@ fromBool False = nat 0
867864
868865-- | Relational negation
869866negateM :: Relation cols rows -> Relation cols rows
867+negateM Empty = Empty
870868negateM (One (Nat p)) = One (Nat (negate p))
871869negateM (Join a b) = Join (negateM a) (negateM b)
872870negateM (Fork a b) = Fork (negateM a) (negateM b)
873871
874872-- | Relational addition
875873orM :: Relation cols rows -> Relation cols rows -> Relation cols rows
874+orM Empty Empty = Empty
876875orM (One a) (One b) = One (fromBool (toBool a || toBool b))
877876orM (Join a b) (Join c d) = Join (orM a c) (orM b d)
878877orM (Fork a b) (Fork c d) = Fork (orM a c) (orM b d)
@@ -881,6 +880,7 @@ orM x@(Join _ _) y@(Fork _ _) = orM (abideJF x) y
881880
882881-- | Relational multiplication
883882andM :: Relation cols rows -> Relation cols rows -> Relation cols rows
883+andM Empty Empty = Empty
884884andM (One a) (One b) = One (fromBool (toBool a && toBool b))
885885andM (Join a b) (Join c d) = Join (andM a c) (andM b d)
886886andM (Fork a b) (Fork c d) = Fork (andM a c) (andM b d)
@@ -889,6 +889,7 @@ andM x@(Join _ _) y@(Fork _ _) = andM (abideJF x) y
889889
890890-- | Relational subtraction
891891subM :: Relation cols rows -> Relation cols rows -> Relation cols rows
892+subM Empty Empty = Empty
892893subM (One a) (One b) = if a - b < nat 0 then One (nat 0) else One (a - b)
893894subM (Join a b) (Join c d) = Join (subM a c) (subM b d)
894895subM (Fork a b) (Fork c d) = Fork (subM a c) (subM b d)
@@ -897,6 +898,7 @@ subM x@(Join _ _) y@(Fork _ _) = subM (abideJF x) y
897898
898899-- | Matrix relational composition.
899900compRel :: Relation cr rows -> Relation cols cr -> Relation cols rows
901+compRel Empty Empty = Empty
900902compRel (One a) (One b) = One (fromBool (toBool a && toBool b))
901903compRel (Join a b) (Fork c d) = orM (compRel a c) (compRel b d) -- Divide-and-conquer law
902904compRel (Fork a b) c = Fork (compRel a c) (compRel b c) -- Fork fusion law
@@ -904,6 +906,7 @@ compRel c (Join a b) = Join (compRel c a) (compRel c b) -- Join fusion
904906
905907-- | Matrix relational right division
906908divR :: Relation b c -> Relation b a -> Relation a c
909+divR Empty Empty = Empty
907910divR (One a) (One b) = One (fromBool (not (toBool b) || toBool a)) -- b implies a
908911divR (Join a b) (Join c d) = andM (divR a c) (divR b d)
909912divR (Fork a b) c = Fork (divR a c) (divR b c)
@@ -926,24 +929,58 @@ divS s r = divL r s `intersection` divR (tr r) (tr s)
926929fromFRel' ::
927930 forall a b cols rows.
928931 ( Liftable Boolean a b,
929− CountableDims cols rows,
930− FL rows cols
932+ CountableDimensions cols rows,
933+ FromLists Boolean rows cols
931934 ) =>
932935 (a -> b) ->
933936 Relation cols rows
934−fromFRel' = fromF'
937+fromFRel' f =
938+ let minA = minBound @a
939+ maxA = maxBound @a
940+ minB = minBound @b
941+ maxB = maxBound @b
942+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count cols))
943+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count rows))
944+ elementsA = take ccols [minA .. maxA]
945+ elementsB = take rrows [minB .. maxB]
946+ combinations = (,) <$> elementsA <*> elementsB
947+ combAp = map snd . sort . map (\(a, b) -> if f a == b
948+ then ((fromEnum a, fromEnum b), nat 1)
949+ else ((fromEnum a, fromEnum b), nat 0)) $ combinations
950+ mList = buildList combAp rrows
951+ in tr $ fromLists mList
952+ where
953+ buildList [] _ = []
954+ buildList l r = take r l : buildList (drop r l) r
935955
936956-- | Lifts functions to relations with dimensions matching @a@ and @b@
937957-- cardinality's.
938958fromFRel ::
939959 forall a b.
940960 ( Liftable Boolean a b,
941− CountableDimsN a b,
942− FLN b a
961+ CountableDimensionsN a b,
962+ FromLists Boolean (Normalize b) (Normalize a)
943963 ) =>
944964 (a -> b) ->
945965 Relation (Normalize a) (Normalize b)
946−fromFRel = fromFRel'
966+fromFRel f =
967+ let minA = minBound @a
968+ maxA = maxBound @a
969+ minB = minBound @b
970+ maxB = maxBound @b
971+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
972+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
973+ elementsA = take ccols [minA .. maxA]
974+ elementsB = take rrows [minB .. maxB]
975+ combinations = (,) <$> elementsA <*> elementsB
976+ combAp = map snd . sort . map (\(a, b) -> if f a == b
977+ then ((fromEnum a, fromEnum b), nat 1)
978+ else ((fromEnum a, fromEnum b), nat 0)) $ combinations
979+ mList = buildList combAp rrows
980+ in tr $ fromLists mList
981+ where
982+ buildList [] _ = []
983+ buildList l r = take r l : buildList (drop r l) r
947984
948985-- | Lifts a relation function to a Boolean Matrix
949986toRel ::
@@ -953,17 +990,17 @@ toRel ::
953990 Enum a,
954991 Enum b,
955992 Eq b,
956− CountableDims a b,
957− FLN b a
958− )
993+ CountableDimensionsN a b,
994+ FromListsN Boolean b a
995+ )
959996 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
960997toRel f =
961998 let minA = minBound @a
962999 maxA = maxBound @a
9631000 minB = minBound @b
9641001 maxB = maxBound @b
965− ccols = fromInteger $ natVal (Proxy :: Proxy (Count a))
966− rrows = fromInteger $ natVal (Proxy :: Proxy (Count b))
1002+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
1003+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
9671004 elementsA = take ccols [minA .. maxA]
9681005 elementsB = take rrows [minB .. maxB]
9691006 combinations = (,) <$> elementsA <*> elementsB