@@ -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,14 +43,23 @@ module LAoP.Matrix.Internal
4343 --
4444 -- There exists two type families that make it easier to write
4545 -- matrix dimensions: 'FromNat' and 'Count'. This approach
46− -- leads to a very straightforward implementation
47− -- of LAoP combinators.
46+ -- leads to a very straightforward implementation
47+ -- of LAoP combinators.
4848
4949 -- * Type safe matrix representation
5050 Matrix (..),
5151
52+ -- * Constraint type aliases
53+ Countable,
54+ CountableDims,
55+ CountableN,
56+ CountableDimsN,
57+ FL,
58+ FLN,
59+ Liftable,
60+ Trivial,
61+
5262 -- * Primitives
53− empty,
5463 one,
5564 join,
5665 fork,
@@ -77,7 +86,9 @@ module LAoP.Matrix.Internal
7786 -- * Misc
7887 -- ** Get dimensions
7988 columns,
89+ columns',
8090 rows,
91+ rows',
8192
8293 -- ** Matrix Transposition
8394 tr,
@@ -119,6 +130,7 @@ module LAoP.Matrix.Internal
119130
120131 -- | Note that given the restrictions imposed it is not possible to
121132 -- implement the standard type classes present in standard Haskell.
133+
122134 -- *** Matrix pairing projections
123135 fstM,
124136 sndM,
@@ -164,17 +176,17 @@ import Data.Kind
164176import Data.List
165177import Data.Maybe
166178import Data.Proxy
179+import Data.Typeable
167180import Data.Void
168181import GHC.TypeLits
169182import Data.Type.Equality
170183import GHC.Generics
171184import Control.DeepSeq
172−import Control.Category
173−import Prelude hiding ((.))
185+import Prelude hiding (id, (.))
186+import qualified Prelude (id, (.))
174187
175188-- | LAoP (Linear Algebra of Programming) Inductive Matrix definition.
176189data Matrix e cols rows where
177− Empty :: Matrix e Void Void
178190 One :: e -> Matrix e () ()
179191 Join :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
180192 Fork :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
@@ -204,7 +216,6 @@ type family Count (d :: Type) :: Nat where
204216--
205217-- Thanks to Li-Yao Xia this type family is super fast.
206218type family FromNat (n :: Nat) :: Type where
207− FromNat 0 = Void
208219 FromNat 1 = ()
209220 FromNat n = FromNat' (Mod n 2 == 0) (FromNat (Div n 2))
210221
@@ -221,29 +232,26 @@ type family Normalize (d :: Type) :: Type where
221232-- | Constraint type synonyms to keep the type signatures less convoluted
222233type Countable a = KnownNat (Count a)
223234type CountableN a = KnownNat (Count (Normalize a))
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)
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)
227239type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
228240type Trivial a = FromNat (Count a) ~ a
229241
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.
242+-- | It is possible to implement a constrained version of the category type
243+-- class.
235244instance (Num e) => Category (Matrix e) where
236− id = undefined
237− (.) = comp
245+ type Object (Matrix e) a = (FL a a, Countable a)
246+ id = iden
247+ (.) = comp
238248
239249instance NFData e => NFData (Matrix e cols rows) where
240− rnf Empty = ()
241250 rnf (One e) = rnf e
242251 rnf (Join a b) = rnf a `seq` rnf b
243252 rnf (Fork a b) = rnf a `seq` rnf b
244253
245254instance Eq e => Eq (Matrix e cols rows) where
246− Empty == Empty = True
247255 (One a) == (One b) = a == b
248256 (Join a b) == (Join c d) = a == c && b == d
249257 (Fork a b) == (Fork c d) = a == c && b == d
@@ -251,25 +259,21 @@ instance Eq e => Eq (Matrix e cols rows) where
251259 x@(Join _ _) == y@(Fork _ _) = abideJF x == y
252260
253261instance Num e => Num (Matrix e cols rows) where
254−
255262 a + b = zipWithM (+) a b
256263
257264 a - b = zipWithM (-) a b
258265
259266 a * b = zipWithM (*) a b
260267
261− abs Empty = Empty
262268 abs (One a) = One (abs a)
263269 abs (Join a b) = Join (abs a) (abs b)
264270 abs (Fork a b) = Fork (abs a) (abs b)
265271
266− signum Empty = Empty
267272 signum (One a) = One (signum a)
268273 signum (Join a b) = Join (signum a) (signum b)
269274 signum (Fork a b) = Fork (signum a) (signum b)
270275
271276instance Ord e => Ord (Matrix e cols rows) where
272− Empty <= Empty = True
273277 (One a) <= (One b) = a <= b
274278 (Join a b) <= (Join c d) = (a <= c) && (b <= d)
275279 (Fork a b) <= (Fork c d) = (a <= c) && (b <= d)
@@ -278,10 +282,6 @@ instance Ord e => Ord (Matrix e cols rows) where
278282
279283-- Primitives
280284
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,40 +312,39 @@ 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 e cols rows where
315+class FromLists 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 e Void Void where
321− fromLists [] = Empty
322− fromLists _ = error "Wrong dimensions"
323−
324−instance {-# OVERLAPPING #-} FromLists e () () where
320+instance FromLists () () where
325321 fromLists [[e]] = One e
326322 fromLists _ = error "Wrong dimensions"
327323
328−instance {-# OVERLAPPING #-} (FromLists e cols ()) => FromLists e (Either () cols) () where
324+instance (FromLists cols ()) => FromLists (Either () cols) () where
329325 fromLists [h : t] = Join (One h) (fromLists [t])
330326 fromLists _ = error "Wrong dimensions"
331327
332−instance {-# OVERLAPPABLE #-} (FromLists e a (), FromLists e b (), Countable a) => FromLists e (Either a b) () where
328+instance {-# OVERLAPPABLE #-}
329+ (FromLists a (), FromLists b (), Countable a) => FromLists (Either a b) () where
333330 fromLists [l] =
334331 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
335332 in Join (fromLists [take rowsA l]) (fromLists [drop rowsA l])
336333 fromLists _ = error "Wrong dimensions"
337334
338−instance {-# OVERLAPPING #-} (FromLists e () rows) => FromLists e () (Either () rows) where
335+instance (FromLists () rows) => FromLists () (Either () rows) where
339336 fromLists ([h] : t) = Fork (One h) (fromLists t)
340337 fromLists _ = error "Wrong dimensions"
341338
342−instance {-# OVERLAPPABLE #-} (FromLists e () a, FromLists e () b, Countable a) => FromLists e () (Either a b) where
343− fromLists l@([_] : _) =
339+instance {-# OVERLAPPABLE #-}
340+ (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
341+ fromLists l@([_] : _) =
344342 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
345343 in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
346344 fromLists _ = error "Wrong dimensions"
347345
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
346+instance (FromLists (Either a b) c, FromLists (Either a b) d, Countable c)
347+ => FromLists (Either a b) (Either c d) where
349348 fromLists l@(h : t) =
350349 let lh = length h
351350 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
@@ -358,8 +357,8 @@ instance {-# OVERLAPPABLE #-} (FromLists e (Either a b) c, FromLists e (Either a
358357-- a construction function that operates with indices.
359358matrixBuilder' ::
360359 forall e cols rows.
361− ( FromLists e cols rows,
362− CountableDimensions cols rows
360+ ( FL cols rows,
361+ CountableDims cols rows
363362 ) => ((Int, Int) -> e) -> Matrix e cols rows
364363matrixBuilder' f =
365364 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
@@ -371,24 +370,28 @@ matrixBuilder' f =
371370-- a construction function that operates with arbitrary types.
372371matrixBuilder ::
373372 forall e a b.
374− ( FromListsN e a b,
373+ ( FLN a b,
375374 Enum a,
376375 Enum b,
377376 Bounded a,
378377 Bounded b,
379378 Eq a,
380− CountableDimensionsN a b
379+ Countable b
381380 ) => ((a, b) -> e) -> Matrix e (Normalize a) (Normalize b)
382381matrixBuilder f =
383− let positions = [(a, b) | a <- [minBound .. maxBound], b <- [minBound .. maxBound]]
384− in fromLists . map (map f) . groupBy (\(x, _) (w, _) -> x == w) $ positions
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)
385388
386389-- | Constructs a column vector matrix
387−col :: (FromLists e () rows) => [e] -> Matrix e () rows
390+col :: (FL () rows) => [e] -> Matrix e () rows
388391col = fromLists . map (: [])
389392
390393-- | Constructs a row vector matrix
391−row :: (FromLists e cols ()) => [e] -> Matrix e cols ()
394+row :: (FL cols ()) => [e] -> Matrix e cols ()
392395row = fromLists . (: [])
393396
394397-- | Lifts functions to matrices with arbitrary dimensions.
@@ -398,8 +401,8 @@ row = fromLists . (: [])
398401fromF' ::
399402 forall a b cols rows e.
400403 ( Liftable e a b,
401− CountableDimensions cols rows,
402− FromLists e rows cols
404+ CountableDims cols rows,
405+ FL rows cols
403406 ) =>
404407 (a -> b) ->
405408 Matrix e cols rows
@@ -427,35 +430,17 @@ fromF' f =
427430fromF ::
428431 forall a b e.
429432 ( Liftable e a b,
430− CountableDimensionsN a b,
431− FromListsN e b a
433+ CountableDimsN a b,
434+ FLN b a
432435 ) =>
433436 (a -> b) ->
434437 Matrix e (Normalize a) (Normalize b)
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
438+fromF = fromF'
453439
454440-- Conversion
455441
456442-- | Converts a matrix to a list of lists of elements.
457443toLists :: Matrix e cols rows -> [[e]]
458−toLists Empty = []
459444toLists (One e) = [[e]]
460445toLists (Fork l r) = toLists l ++ toLists r
461446toLists (Join l r) = zipWith (++) (toLists l) (toLists r)
@@ -467,7 +452,7 @@ toList = concat . toLists
467452-- Zeros Matrix
468453
469454-- | The zero matrix. A matrix wholly filled with zeros.
470−zeros :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
455+zeros :: (Num e, FL cols rows, CountableDims cols rows) => Matrix e cols rows
471456zeros = matrixBuilder' (const 0)
472457
473458-- Ones Matrix
@@ -475,20 +460,20 @@ zeros = matrixBuilder' (const 0)
475460-- | The ones matrix. A matrix wholly filled with ones.
476461--
477462-- Also known as T (Top) matrix.
478−ones :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => Matrix e cols rows
463+ones :: (Num e, FL cols rows, CountableDims cols rows) => Matrix e cols rows
479464ones = matrixBuilder' (const 1)
480465
481466-- Const Matrix
482467
483468-- | The constant matrix constructor. A matrix wholly filled with a given
484469-- value.
485−constant :: (Num e, FromLists e cols rows, CountableDimensions cols rows) => e -> Matrix e cols rows
470+constant :: (Num e, FL cols rows, CountableDims cols rows) => e -> Matrix e cols rows
486471constant e = matrixBuilder' (const e)
487472
488473-- Bang Matrix
489474
490475-- | The T (Top) row vector matrix.
491−bang :: forall e cols. (Num e, Enum e, FromLists e cols (), Countable cols) => Matrix e cols ()
476+bang :: forall e cols. (Num e, Enum e, FL cols (), Countable cols) => Matrix e cols ()
492477bang =
493478 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
494479 in fromLists [take c [1, 1 ..]]
@@ -496,7 +481,7 @@ bang =
496481-- iden Matrix
497482
498483-- | iden matrix.
499−iden :: (Num e, FromLists e cols cols, Countable cols) => Matrix e cols cols
484+iden :: forall e cols . (Num e, FL cols cols, Countable cols) => Matrix e cols cols
500485iden = matrixBuilder' (bool 0 1 . uncurry (==))
501486{-# NOINLINE iden #-}
502487
@@ -507,13 +492,12 @@ iden = matrixBuilder' (bool 0 1 . uncurry (==))
507492-- This definition takes advantage of divide-and-conquer and fusion laws
508493-- from LAoP.
509494comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
510−comp Empty Empty = Empty
511495comp (One a) (One b) = One (a * b)
512496comp (Join a b) (Fork c d) = comp a c + comp b d -- Divide-and-conquer law
513497comp (Fork a b) c = Fork (comp a c) (comp b c) -- Fork fusion law
514498comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
515499{-# NOINLINE comp #-}
516−{-# RULES
500+{-# RULES
517501 "comp/iden1" forall m. comp m iden = m ;
518502 "comp/iden2" forall m. comp iden m = m
519503#-}
@@ -523,7 +507,6 @@ comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
523507infixl 7 .|
524508-- | Scalar multiplication of matrices.
525509(.|) :: Num e => e -> Matrix e cols rows -> Matrix e cols rows
526−(.|) _ Empty = Empty
527510(.|) e (One a) = One (e * a)
528511(.|) e (Join a b) = Join (e .| a) (e .| b)
529512(.|) e (Fork a b) = Fork (e .| a) (e .| b)
@@ -533,7 +516,6 @@ infixl 7 .|
533516infixl 7 ./
534517-- | Scalar multiplication of matrices.
535518(./) :: Fractional e => Matrix e cols rows -> e -> Matrix e cols rows
536−(./) Empty _ = Empty
537519(./) (One a) e = One (a / e)
538520(./) (Join a b) e = Join (a ./ e) (b ./ e)
539521(./) (Fork a b) e = Fork (a ./ e) (b ./ e)
@@ -541,45 +523,59 @@ infixl 7 ./
541523-- Projections
542524
543525-- | Biproduct first component projection
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
526+p1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e (Either m n) m
527+p1 = join id zeros
546528
547529-- | Biproduct second component projection
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
530+p2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e (Either m n) n
531+p2 = join zeros id
550532
551533-- Injections
552534
553535-- | Biproduct first component injection
554−i1 :: (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e m (Either m n)
536+i1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e m (Either m n)
555537i1 = tr p1
556538
557539-- | Biproduct second component injection
558−i2 :: (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e n (Either m n)
540+i2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e n (Either m n)
559541i2 = tr p2
560542
561543-- Dimensions
562544
563545-- | Obtain the number of rows.
564546--
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.
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''.
570550rows :: forall e cols rows. (Countable rows) => Matrix e cols rows -> Int
571551rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
572552
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+
573562-- | Obtain the number of columns.
574−--
575−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
576−-- dimensions in constant time.
577563--
578−-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
579−-- exchange for performance.
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''.
580567columns :: forall e cols rows. (Countable cols) => Matrix e cols rows -> Int
581568columns _ = fromInteger $ natVal (Proxy :: Proxy (Count cols))
582569
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+
583579-- Coproduct Bifunctor
584580
585581infixl 5 -|-
@@ -588,11 +584,11 @@ infixl 5 -|-
588584(-|-) ::
589585 forall e n k m j.
590586 ( Num e,
591− CountableDimensions j k,
592− FromLists e k k,
593− FromLists e j k,
594− FromLists e k j,
595− FromLists e j j
587+ CountableDims j k,
588+ FL k k,
589+ FL j k,
590+ FL k j,
591+ FL j j
596592 ) =>
597593 Matrix e n k ->
598594 Matrix e m j ->
@@ -605,8 +601,8 @@ infixl 5 -|-
605601fstM ::
606602 forall e m k .
607603 ( Num e,
608− CountableDimensions k m,
609− FromLists e (Normalize (m, k)) m,
604+ CountableDims k m,
605+ FL (Normalize (m, k)) m,
610606 CountableN (m, k)
611607 ) => Matrix e (Normalize (m, k)) m
612608fstM = matrixBuilder' f
@@ -620,8 +616,8 @@ fstM = matrixBuilder' f
620616sndM ::
621617 forall e m k .
622618 ( Num e,
623− CountableDimensions k m,
624− FromLists e (Normalize (m, k)) k,
619+ CountableDims k m,
620+ FL (Normalize (m, k)) k,
625621 CountableN (m, k)
626622 ) => Matrix e (Normalize (m, k)) k
627623sndM = matrixBuilder' f
@@ -634,7 +630,7 @@ sndM = matrixBuilder' f
634630-- | Khatri Rao Matrix product also known as matrix pairing.
635631--
636632-- NOTE: That this is not a true categorical product, see for instance:
637−--
633+--
638634-- @
639635-- | fstM . kr a b == a
640636-- kr a b ==> |
@@ -645,10 +641,10 @@ sndM = matrixBuilder' f
645641kr ::
646642 forall e cols a b.
647643 ( Num e,
648− CountableDimensions a b,
644+ CountableDims a b,
649645 CountableN (a, b),
650− FromLists e (Normalize (a, b)) a,
651− FromLists e (Normalize (a, b)) b
646+ FL (Normalize (a, b)) a,
647+ FL (Normalize (a, b)) b
652648 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (Normalize (a, b))
653649kr a b =
654650 let fstM' = fstM @e @a @b
@@ -663,14 +659,14 @@ infixl 4 ><
663659(><) ::
664660 forall e m p n q.
665661 ( Num e,
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− )
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+ )
674670 => Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
675671(><) a b =
676672 let fstM' = fstM @e @m @n
@@ -688,7 +684,6 @@ infixl 4 ><
688684-- @
689685abideJF :: Matrix e cols rows -> Matrix e cols rows
690686abideJF (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
692687abideJF (One e) = One e
693688abideJF (Join a b) = Join (abideJF a) (abideJF b)
694689abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
@@ -702,7 +697,6 @@ abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
702697-- @
703698abideFJ :: Matrix e cols rows -> Matrix e cols rows
704699abideFJ (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
706700abideFJ (One e) = One e
707701abideFJ (Join a b) = Join (abideFJ a) (abideFJ b)
708702abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
@@ -711,7 +705,6 @@ abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
711705
712706-- | Matrix transposition.
713707tr :: Matrix e cols rows -> Matrix e rows cols
714−tr Empty = Empty
715708tr (One e) = One e
716709tr (Join a b) = Fork (tr a) (tr b)
717710tr (Fork a b) = Join (tr a) (tr b)
@@ -720,32 +713,32 @@ tr (Fork a b) = Join (tr a) (tr b)
720713
721714-- | Selective functors 'select' operator equivalent inspired by the
722715-- ArrowMonad solution presented in the paper.
723−select :: (Num e, FromLists e b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
716+select :: (Num e, FL b b, Countable b) => Matrix e cols (Either a b) -> Matrix e a b -> Matrix e cols b
724717select (Fork a b) y = y . a + b -- Divide-and-conquer law
725718select (Join (Fork a c) (Fork b d)) y = join (y . a + c) (y . b + d) -- Pattern matching + DnC law
726−select m y = join y iden . m
719+select m y = join y id . m
727720
728721branch ::
729722 ( Num e,
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
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
741734 )
742735 => Matrix e cols (Either a b) -> Matrix e a c -> Matrix e b c -> Matrix e cols c
743736branch x l r = f x `select` g l `select` r
744737 where
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)
738+ f :: (Num e, Countable a, CountableDims b c, FL a b, FL c b, FL b b, FL b a, FL a a)
746739 => Matrix e cols (Either a b) -> Matrix e cols (Either a (Either b c))
747740 f m = fork (tr i1) (i1 . tr i2) . m
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)
741+ g :: (Num e, CountableDims b c, FL b c, FL c c) => Matrix e a c -> Matrix e a (Either b c)
749742 g m = i2 . m
750743
751744-- McCarthy's Conditional
@@ -754,9 +747,9 @@ branch x l r = f x `select` g l `select` r
754747cond ::
755748 ( Trivial cols,
756749 Countable cols,
757− FromLists e () cols,
758− FromLists e cols (),
759− FromLists e cols cols,
750+ FL () cols,
751+ FL cols (),
752+ FL cols cols,
760753 Bounded a,
761754 Enum a,
762755 Num e,
@@ -769,9 +762,9 @@ cond p f g = join f g . grd p
769762grd ::
770763 ( Trivial q,
771764 Countable q,
772− FromLists e () q,
773− FromLists e q (),
774− FromLists e q q,
765+ FL () q,
766+ FL q (),
767+ FL q q,
775768 Bounded a,
776769 Enum a,
777770 Num e,
@@ -785,14 +778,14 @@ corr ::
785778 forall e a q .
786779 ( Trivial q,
787780 Countable q,
788− FromLists e () q,
789− FromLists e q (),
790− FromLists e q q,
781+ FL () q,
782+ FL q (),
783+ FL q q,
791784 Liftable e a Bool
792785 )
793786 => (a -> Bool) -> Matrix e q q
794787corr p = let f = fromF' p :: Matrix e q ()
795− in kr f (iden :: Matrix e q q)
788+ in kr f (id :: Matrix e q q)
796789
797790-- Pretty print
798791
@@ -816,7 +809,7 @@ prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
816809 fill str = replicate (widest - length str - 2) ' ' ++ str
817810
818811-- | Matrix pretty printer
819−pretty :: (CountableDimensions cols rows, Show e) => Matrix e cols rows -> String
812+pretty :: (CountableDims cols rows, Show e) => Matrix e cols rows -> String
820813pretty m = concat
821814 [ "┌ ", unwords (replicate (columns m) blank), " ┐\n"
822815 , unlines
@@ -845,12 +838,11 @@ pretty m = concat
845838 (safeGet i j m)
846839
847840-- | Matrix pretty printer
848−prettyPrint :: (CountableDimensions cols rows, Show e) => Matrix e cols rows -> IO ()
841+prettyPrint :: (CountableDims cols rows, Show e) => Matrix e cols rows -> IO ()
849842prettyPrint = putStrLn . pretty
850843
851844-- | Zip two matrices with a given binary function
852845zipWithM :: (e -> f -> g) -> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
853−zipWithM _ Empty Empty = Empty
854846zipWithM f (One a) (One b) = One (f a b)
855847zipWithM f (Join a b) (Join c d) = Join (zipWithM f a c) (zipWithM f b d)
856848zipWithM f (Fork a b) (Fork c d) = Fork (zipWithM f a c) (zipWithM f b d)
@@ -875,14 +867,12 @@ fromBool False = nat 0
875867
876868-- | Relational negation
877869negateM :: Relation cols rows -> Relation cols rows
878−negateM Empty = Empty
879870negateM (One (Nat p)) = One (Nat (negate p))
880871negateM (Join a b) = Join (negateM a) (negateM b)
881872negateM (Fork a b) = Fork (negateM a) (negateM b)
882873
883874-- | Relational addition
884875orM :: Relation cols rows -> Relation cols rows -> Relation cols rows
885−orM Empty Empty = Empty
886876orM (One a) (One b) = One (fromBool (toBool a || toBool b))
887877orM (Join a b) (Join c d) = Join (orM a c) (orM b d)
888878orM (Fork a b) (Fork c d) = Fork (orM a c) (orM b d)
@@ -891,7 +881,6 @@ orM x@(Join _ _) y@(Fork _ _) = orM (abideJF x) y
891881
892882-- | Relational multiplication
893883andM :: Relation cols rows -> Relation cols rows -> Relation cols rows
894−andM Empty Empty = Empty
895884andM (One a) (One b) = One (fromBool (toBool a && toBool b))
896885andM (Join a b) (Join c d) = Join (andM a c) (andM b d)
897886andM (Fork a b) (Fork c d) = Fork (andM a c) (andM b d)
@@ -900,7 +889,6 @@ andM x@(Join _ _) y@(Fork _ _) = andM (abideJF x) y
900889
901890-- | Relational subtraction
902891subM :: Relation cols rows -> Relation cols rows -> Relation cols rows
903−subM Empty Empty = Empty
904892subM (One a) (One b) = if a - b < nat 0 then One (nat 0) else One (a - b)
905893subM (Join a b) (Join c d) = Join (subM a c) (subM b d)
906894subM (Fork a b) (Fork c d) = Fork (subM a c) (subM b d)
@@ -909,7 +897,6 @@ subM x@(Join _ _) y@(Fork _ _) = subM (abideJF x) y
909897
910898-- | Matrix relational composition.
911899compRel :: Relation cr rows -> Relation cols cr -> Relation cols rows
912−compRel Empty Empty = Empty
913900compRel (One a) (One b) = One (fromBool (toBool a && toBool b))
914901compRel (Join a b) (Fork c d) = orM (compRel a c) (compRel b d) -- Divide-and-conquer law
915902compRel (Fork a b) c = Fork (compRel a c) (compRel b c) -- Fork fusion law
@@ -917,7 +904,6 @@ compRel c (Join a b) = Join (compRel c a) (compRel c b) -- Join fusion
917904
918905-- | Matrix relational right division
919906divR :: Relation b c -> Relation b a -> Relation a c
920−divR Empty Empty = Empty
921907divR (One a) (One b) = One (fromBool (not (toBool b) || toBool a)) -- b implies a
922908divR (Join a b) (Join c d) = andM (divR a c) (divR b d)
923909divR (Fork a b) c = Fork (divR a c) (divR b c)
@@ -940,58 +926,24 @@ divS s r = divL r s `intersection` divR (tr r) (tr s)
940926fromFRel' ::
941927 forall a b cols rows.
942928 ( Liftable Boolean a b,
943− CountableDimensions cols rows,
944− FromLists Boolean rows cols
929+ CountableDims cols rows,
930+ FL rows cols
945931 ) =>
946932 (a -> b) ->
947933 Relation cols rows
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
934+fromFRel' = fromF'
966935
967936-- | Lifts functions to relations with dimensions matching @a@ and @b@
968937-- cardinality's.
969938fromFRel ::
970939 forall a b.
971940 ( Liftable Boolean a b,
972− CountableDimensionsN a b,
973− FromLists Boolean (Normalize b) (Normalize a)
941+ CountableDimsN a b,
942+ FLN b a
974943 ) =>
975944 (a -> b) ->
976945 Relation (Normalize a) (Normalize b)
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
946+fromFRel = fromFRel'
995947
996948-- | Lifts a relation function to a Boolean Matrix
997949toRel ::
@@ -1001,17 +953,17 @@ toRel ::
1001953 Enum a,
1002954 Enum b,
1003955 Eq b,
1004− CountableDimensionsN a b,
1005− FromListsN Boolean b a
1006− )
956+ CountableDims a b,
957+ FLN b a
958+ )
1007959 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
1008960toRel f =
1009961 let minA = minBound @a
1010962 maxA = maxBound @a
1011963 minB = minBound @b
1012964 maxB = maxBound @b
1013− ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
1014− rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
965+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count a))
966+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count b))
1015967 elementsA = take ccols [minA .. maxA]
1016968 elementsB = take rrows [minB .. maxB]
1017969 combinations = (,) <$> elementsA <*> elementsB