@@ -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,45 @@ 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 :: (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e (Either m n) m
528+p1 = join iden zeros
528529
529530-- | 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
531+p2 :: (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e (Either m n) n
532+p2 = join zeros iden
532533
533534-- Injections
534535
535536-- | Biproduct first component injection
536−i1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e m (Either m n)
537+i1 :: (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e m (Either m n)
537538i1 = tr p1
538539
539540-- | Biproduct second component injection
540−i2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e n (Either m n)
541+i2 :: (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e n (Either m n)
541542i2 = tr p2
542543
543544-- Dimensions
544545
545546-- | Obtain the number of rows.
546547--
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''.
548+-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
549+-- dimensions in constant time.
550+--
551+-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
552+-- exchange for performance.
550553rows :: forall e cols rows. (Countable rows) => Matrix e cols rows -> Int
551554rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
552555
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−
562556-- | Obtain the number of columns.
557+--
558+-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
559+-- dimensions in constant time.
563560--
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''.
561+-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
562+-- exchange for performance.
567563columns :: forall e cols rows. (Countable cols) => Matrix e cols rows -> Int
568564columns _ = fromInteger $ natVal (Proxy :: Proxy (Count cols))
569565
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−
579566-- Coproduct Bifunctor
580567
581568infixl 5 -|-
@@ -584,11 +571,11 @@ infixl 5 -|-
584571(-|-) ::
585572 forall e n k m j.
586573 ( Num e,
587− CountableDims j k,
588− FL k k,
589− FL j k,
590− FL k j,
591− FL j j
574+ CountableDimensions j k,
575+ FromLists e k k,
576+ FromLists e j k,
577+ FromLists e k j,
578+ FromLists e j j
592579 ) =>
593580 Matrix e n k ->
594581 Matrix e m j ->
@@ -601,11 +588,11 @@ infixl 5 -|-
601588fstM ::
602589 forall e m k .
603590 ( Num e,
604− CountableDims k m,
605− FL (Normalize (m, k)) m,
591+ CountableDimensions k m,
592+ FromLists e (Normalize (m, k)) m,
606593 CountableN (m, k)
607594 ) => Matrix e (Normalize (m, k)) m
608−fstM = matrixBuilder' f
595+fstM = matrixBuilder f
609596 where
610597 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
611598 f (x, y)
@@ -616,11 +603,11 @@ fstM = matrixBuilder' f
616603sndM ::
617604 forall e m k .
618605 ( Num e,
619− CountableDims k m,
620− FL (Normalize (m, k)) k,
606+ CountableDimensions k m,
607+ FromLists e (Normalize (m, k)) k,
621608 CountableN (m, k)
622609 ) => Matrix e (Normalize (m, k)) k
623−sndM = matrixBuilder' f
610+sndM = matrixBuilder f
624611 where
625612 offset = fromInteger (natVal (Proxy :: Proxy (Count k)))
626613 f (x, y)
@@ -630,7 +617,7 @@ sndM = matrixBuilder' f
630617-- | Khatri Rao Matrix product also known as matrix pairing.
631618--
632619-- NOTE: That this is not a true categorical product, see for instance:
633−--
620+--
634621-- @
635622-- | fstM . kr a b == a
636623-- kr a b ==> |
@@ -641,10 +628,10 @@ sndM = matrixBuilder' f
641628kr ::
642629 forall e cols a b.
643630 ( Num e,
644− CountableDims a b,
631+ CountableDimensions a b,
645632 CountableN (a, b),
646− FL (Normalize (a, b)) a,
647− FL (Normalize (a, b)) b
633+ FromLists e (Normalize (a, b)) a,
634+ FromLists e (Normalize (a, b)) b
648635 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (Normalize (a, b))
649636kr a b =
650637 let fstM' = fstM @e @a @b
@@ -659,14 +646,14 @@ infixl 4 ><
659646(><) ::
660647 forall e m p n q.
661648 ( 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− )
649+ CountableDimensions m n,
650+ CountableDimensions p q,
651+ CountableDimensionsN (m, n) (p, q),
652+ FromLists e (Normalize (m, n)) m,
653+ FromLists e (Normalize (m, n)) n,
654+ FromLists e (Normalize (p, q)) p,
655+ FromLists e (Normalize (p, q)) q
656+ )
670657 => Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
671658(><) a b =
672659 let fstM' = fstM @e @m @n
@@ -684,6 +671,7 @@ infixl 4 ><
684671-- @
685672abideJF :: Matrix e cols rows -> Matrix e cols rows
686673abideJF (Join (Fork a c) (Fork b d)) = Fork (Join (abideJF a) (abideJF b)) (Join (abideJF c) (abideJF d)) -- Join-Fork abide law
674+abideJF Empty = Empty
687675abideJF (One e) = One e
688676abideJF (Join a b) = Join (abideJF a) (abideJF b)
689677abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
@@ -697,6 +685,7 @@ abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
697685-- @
698686abideFJ :: Matrix e cols rows -> Matrix e cols rows
699687abideFJ (Fork (Join a b) (Join c d)) = Join (Fork (abideFJ a) (abideFJ c)) (Fork (abideFJ b) (abideFJ d)) -- Fork-Join abide law
688+abideFJ Empty = Empty
700689abideFJ (One e) = One e
701690abideFJ (Join a b) = Join (abideFJ a) (abideFJ b)
702691abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
@@ -705,6 +694,7 @@ abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
705694
706695-- | Matrix transposition.
707696tr :: Matrix e cols rows -> Matrix e rows cols
697+tr Empty = Empty
708698tr (One e) = One e
709699tr (Join a b) = Fork (tr a) (tr b)
710700tr (Fork a b) = Join (tr a) (tr b)
@@ -713,32 +703,32 @@ tr (Fork a b) = Join (tr a) (tr b)
713703
714704-- | Selective functors 'select' operator equivalent inspired by the
715705-- 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
706+select :: (Num e, FromLists e b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
717707select (Fork a b) y = y . a + b -- Divide-and-conquer law
718708select (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
709+select m y = join y iden . m
720710
721711branch ::
722712 ( 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
713+ CountableDimensions a b,
714+ CountableDimensions c (Either b c),
715+ FromLists e c b,
716+ FromLists e a b,
717+ FromLists e a a,
718+ FromLists e b b,
719+ FromLists e c c,
720+ FromLists e b a,
721+ FromLists e b c,
722+ FromLists e (Either b c) b,
723+ FromLists e (Either b c) c
734724 )
735725 => Matrix e cols (Either a b) -> Matrix e a c -> Matrix e b c -> Matrix e cols c
736726branch x l r = f x `select` g l `select` r
737727 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)
728+ 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)
739729 => Matrix e cols (Either a b) -> Matrix e cols (Either a (Either b c))
740730 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)
731+ g :: (Num e, CountableDimensions b c, FromLists e b c, FromLists e c c) => Matrix e a c -> Matrix e a (Either b c)
742732 g m = i2 . m
743733
744734-- McCarthy's Conditional
@@ -747,9 +737,9 @@ branch x l r = f x `select` g l `select` r
747737cond ::
748738 ( Trivial cols,
749739 Countable cols,
750− FL () cols,
751− FL cols (),
752− FL cols cols,
740+ FromLists e () cols,
741+ FromLists e cols (),
742+ FromLists e cols cols,
753743 Bounded a,
754744 Enum a,
755745 Num e,
@@ -762,9 +752,9 @@ cond p f g = join f g . grd p
762752grd ::
763753 ( Trivial q,
764754 Countable q,
765− FL () q,
766− FL q (),
767− FL q q,
755+ FromLists e () q,
756+ FromLists e q (),
757+ FromLists e q q,
768758 Bounded a,
769759 Enum a,
770760 Num e,
@@ -778,14 +768,14 @@ corr ::
778768 forall e a q .
779769 ( Trivial q,
780770 Countable q,
781− FL () q,
782− FL q (),
783− FL q q,
771+ FromLists e () q,
772+ FromLists e q (),
773+ FromLists e q q,
784774 Liftable e a Bool
785775 )
786776 => (a -> Bool) -> Matrix e q q
787777corr p = let f = fromF' p :: Matrix e q ()
788− in kr f (id :: Matrix e q q)
778+ in kr f (iden :: Matrix e q q)
789779
790780-- Pretty print
791781
@@ -809,7 +799,7 @@ prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
809799 fill str = replicate (widest - length str - 2) ' ' ++ str
810800
811801-- | Matrix pretty printer
812−pretty :: (CountableDims cols rows, Show e) => Matrix e cols rows -> String
802+pretty :: (CountableDimensions cols rows, Show e) => Matrix e cols rows -> String
813803pretty m = concat
814804 [ "┌ ", unwords (replicate (columns m) blank), " ┐\n"
815805 , unlines
@@ -838,11 +828,12 @@ pretty m = concat
838828 (safeGet i j m)
839829
840830-- | Matrix pretty printer
841−prettyPrint :: (CountableDims cols rows, Show e) => Matrix e cols rows -> IO ()
831+prettyPrint :: (CountableDimensions cols rows, Show e) => Matrix e cols rows -> IO ()
842832prettyPrint = putStrLn . pretty
843833
844834-- | Zip two matrices with a given binary function
845835zipWithM :: (e -> f -> g) -> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
836+zipWithM _ Empty Empty = Empty
846837zipWithM f (One a) (One b) = One (f a b)
847838zipWithM f (Join a b) (Join c d) = Join (zipWithM f a c) (zipWithM f b d)
848839zipWithM f (Fork a b) (Fork c d) = Fork (zipWithM f a c) (zipWithM f b d)
@@ -867,12 +858,14 @@ fromBool False = nat 0
867858
868859-- | Relational negation
869860negateM :: Relation cols rows -> Relation cols rows
861+negateM Empty = Empty
870862negateM (One (Nat p)) = One (Nat (negate p))
871863negateM (Join a b) = Join (negateM a) (negateM b)
872864negateM (Fork a b) = Fork (negateM a) (negateM b)
873865
874866-- | Relational addition
875867orM :: Relation cols rows -> Relation cols rows -> Relation cols rows
868+orM Empty Empty = Empty
876869orM (One a) (One b) = One (fromBool (toBool a || toBool b))
877870orM (Join a b) (Join c d) = Join (orM a c) (orM b d)
878871orM (Fork a b) (Fork c d) = Fork (orM a c) (orM b d)
@@ -881,6 +874,7 @@ orM x@(Join _ _) y@(Fork _ _) = orM (abideJF x) y
881874
882875-- | Relational multiplication
883876andM :: Relation cols rows -> Relation cols rows -> Relation cols rows
877+andM Empty Empty = Empty
884878andM (One a) (One b) = One (fromBool (toBool a && toBool b))
885879andM (Join a b) (Join c d) = Join (andM a c) (andM b d)
886880andM (Fork a b) (Fork c d) = Fork (andM a c) (andM b d)
@@ -889,6 +883,7 @@ andM x@(Join _ _) y@(Fork _ _) = andM (abideJF x) y
889883
890884-- | Relational subtraction
891885subM :: Relation cols rows -> Relation cols rows -> Relation cols rows
886+subM Empty Empty = Empty
892887subM (One a) (One b) = if a - b < nat 0 then One (nat 0) else One (a - b)
893888subM (Join a b) (Join c d) = Join (subM a c) (subM b d)
894889subM (Fork a b) (Fork c d) = Fork (subM a c) (subM b d)
@@ -897,6 +892,7 @@ subM x@(Join _ _) y@(Fork _ _) = subM (abideJF x) y
897892
898893-- | Matrix relational composition.
899894compRel :: Relation cr rows -> Relation cols cr -> Relation cols rows
895+compRel Empty Empty = Empty
900896compRel (One a) (One b) = One (fromBool (toBool a && toBool b))
901897compRel (Join a b) (Fork c d) = orM (compRel a c) (compRel b d) -- Divide-and-conquer law
902898compRel (Fork a b) c = Fork (compRel a c) (compRel b c) -- Fork fusion law
@@ -904,6 +900,7 @@ compRel c (Join a b) = Join (compRel c a) (compRel c b) -- Join fusion
904900
905901-- | Matrix relational right division
906902divR :: Relation b c -> Relation b a -> Relation a c
903+divR Empty Empty = Empty
907904divR (One a) (One b) = One (fromBool (not (toBool b) || toBool a)) -- b implies a
908905divR (Join a b) (Join c d) = andM (divR a c) (divR b d)
909906divR (Fork a b) c = Fork (divR a c) (divR b c)
@@ -926,24 +923,58 @@ divS s r = divL r s `intersection` divR (tr r) (tr s)
926923fromFRel' ::
927924 forall a b cols rows.
928925 ( Liftable Boolean a b,
929− CountableDims cols rows,
930− FL rows cols
926+ CountableDimensions cols rows,
927+ FromLists Boolean rows cols
931928 ) =>
932929 (a -> b) ->
933930 Relation cols rows
934−fromFRel' = fromF'
931+fromFRel' f =
932+ let minA = minBound @a
933+ maxA = maxBound @a
934+ minB = minBound @b
935+ maxB = maxBound @b
936+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count cols))
937+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count rows))
938+ elementsA = take ccols [minA .. maxA]
939+ elementsB = take rrows [minB .. maxB]
940+ combinations = (,) <$> elementsA <*> elementsB
941+ combAp = map snd . sort . map (\(a, b) -> if f a == b
942+ then ((fromEnum a, fromEnum b), nat 1)
943+ else ((fromEnum a, fromEnum b), nat 0)) $ combinations
944+ mList = buildList combAp rrows
945+ in tr $ fromLists mList
946+ where
947+ buildList [] _ = []
948+ buildList l r = take r l : buildList (drop r l) r
935949
936950-- | Lifts functions to relations with dimensions matching @a@ and @b@
937951-- cardinality's.
938952fromFRel ::
939953 forall a b.
940954 ( Liftable Boolean a b,
941− CountableDimsN a b,
942− FLN b a
955+ CountableDimensionsN a b,
956+ FromLists Boolean (Normalize b) (Normalize a)
943957 ) =>
944958 (a -> b) ->
945959 Relation (Normalize a) (Normalize b)
946−fromFRel = fromFRel'
960+fromFRel f =
961+ let minA = minBound @a
962+ maxA = maxBound @a
963+ minB = minBound @b
964+ maxB = maxBound @b
965+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
966+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
967+ elementsA = take ccols [minA .. maxA]
968+ elementsB = take rrows [minB .. maxB]
969+ combinations = (,) <$> elementsA <*> elementsB
970+ combAp = map snd . sort . map (\(a, b) -> if f a == b
971+ then ((fromEnum a, fromEnum b), nat 1)
972+ else ((fromEnum a, fromEnum b), nat 0)) $ combinations
973+ mList = buildList combAp rrows
974+ in tr $ fromLists mList
975+ where
976+ buildList [] _ = []
977+ buildList l r = take r l : buildList (drop r l) r
947978
948979-- | Lifts a relation function to a Boolean Matrix
949980toRel ::
@@ -953,17 +984,17 @@ toRel ::
953984 Enum a,
954985 Enum b,
955986 Eq b,
956− CountableDims a b,
957− FLN b a
958− )
987+ CountableDimensionsN a b,
988+ FromListsN Boolean b a
989+ )
959990 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
960991toRel f =
961992 let minA = minBound @a
962993 maxA = maxBound @a
963994 minB = minBound @b
964995 maxB = maxBound @b
965− ccols = fromInteger $ natVal (Proxy :: Proxy (Count a))
966− rrows = fromInteger $ natVal (Proxy :: Proxy (Count b))
996+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
997+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
967998 elementsA = take ccols [minA .. maxA]
968999 elementsB = take rrows [minB .. maxB]
9691000 combinations = (,) <$> elementsA <*> elementsB