@@ -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,17 +370,16 @@ matrixBuilder' f =
371370-- a construction function that operates with arbitrary types.
372371matrixBuilder ::
373372 forall e a b.
374− ( FromListsN e a b,
375− CountableN b,
373+ ( FLN a b,
376374 Enum a,
377375 Enum b,
378376 Bounded a,
379377 Bounded b,
380378 Eq a,
381− CountableDimensionsN a b
379+ Countable b
382380 ) => ((a, b) -> e) -> Matrix e (Normalize a) (Normalize b)
383381matrixBuilder f =
384− let r = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
382+ let r = fromInteger $ natVal (Proxy :: Proxy (Count b))
385383 positions = [(a, b) | a <- [minBound .. maxBound], b <- [minBound .. maxBound]]
386384 in fromLists . map (map f) . transpose . buildList r $ positions
387385 where
@@ -389,11 +387,11 @@ matrixBuilder f =
389387 buildList r l = take r l : buildList r (drop r l)
390388
391389-- | Constructs a column vector matrix
392−col :: (FromLists e () rows) => [e] -> Matrix e () rows
390+col :: (FL () rows) => [e] -> Matrix e () rows
393391col = fromLists . map (: [])
394392
395393-- | Constructs a row vector matrix
396−row :: (FromLists e cols ()) => [e] -> Matrix e cols ()
394+row :: (FL cols ()) => [e] -> Matrix e cols ()
397395row = fromLists . (: [])
398396
399397-- | Lifts functions to matrices with arbitrary dimensions.
@@ -403,8 +401,8 @@ row = fromLists . (: [])
403401fromF' ::
404402 forall a b cols rows e.
405403 ( Liftable e a b,
406− CountableDimensions cols rows,
407− FromLists e rows cols
404+ CountableDims cols rows,
405+ FL rows cols
408406 ) =>
409407 (a -> b) ->
410408 Matrix e cols rows
@@ -432,35 +430,17 @@ fromF' f =
432430fromF ::
433431 forall a b e.
434432 ( Liftable e a b,
435− CountableDimensionsN a b,
436− FromListsN e b a
433+ CountableDimsN a b,
434+ FLN b a
437435 ) =>
438436 (a -> b) ->
439437 Matrix e (Normalize a) (Normalize b)
440−fromF f =
441− let minA = minBound @a
442− maxA = maxBound @a
443− minB = minBound @b
444− maxB = maxBound @b
445− ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
446− rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
447− elementsA = take ccols [minA .. maxA]
448− elementsB = take rrows [minB .. maxB]
449− combinations = (,) <$> elementsA <*> elementsB
450− combAp = map snd . sort . map (\(a, b) -> if f a == b
451− then ((fromEnum a, fromEnum b), 1)
452− else ((fromEnum a, fromEnum b), 0)) $ combinations
453− mList = buildList combAp rrows
454− in tr $ fromLists mList
455− where
456− buildList [] _ = []
457− buildList l r = take r l : buildList (drop r l) r
438+fromF = fromF'
458439
459440-- Conversion
460441
461442-- | Converts a matrix to a list of lists of elements.
462443toLists :: Matrix e cols rows -> [[e]]
463−toLists Empty = []
464444toLists (One e) = [[e]]
465445toLists (Fork l r) = toLists l ++ toLists r
466446toLists (Join l r) = zipWith (++) (toLists l) (toLists r)
@@ -472,7 +452,7 @@ toList = concat . toLists
472452-- Zeros Matrix
473453
474454-- | The zero matrix. A matrix wholly filled with zeros.
475−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
476456zeros = matrixBuilder' (const 0)
477457
478458-- Ones Matrix
@@ -480,20 +460,20 @@ zeros = matrixBuilder' (const 0)
480460-- | The ones matrix. A matrix wholly filled with ones.
481461--
482462-- Also known as T (Top) matrix.
483−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
484464ones = matrixBuilder' (const 1)
485465
486466-- Const Matrix
487467
488468-- | The constant matrix constructor. A matrix wholly filled with a given
489469-- value.
490−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
491471constant e = matrixBuilder' (const e)
492472
493473-- Bang Matrix
494474
495475-- | The T (Top) row vector matrix.
496−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 ()
497477bang =
498478 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
499479 in fromLists [take c [1, 1 ..]]
@@ -501,7 +481,7 @@ bang =
501481-- iden Matrix
502482
503483-- | iden matrix.
504−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
505485iden = matrixBuilder' (bool 0 1 . uncurry (==))
506486{-# NOINLINE iden #-}
507487
@@ -512,13 +492,12 @@ iden = matrixBuilder' (bool 0 1 . uncurry (==))
512492-- This definition takes advantage of divide-and-conquer and fusion laws
513493-- from LAoP.
514494comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
515−comp Empty Empty = Empty
516495comp (One a) (One b) = One (a * b)
517496comp (Join a b) (Fork c d) = comp a c + comp b d -- Divide-and-conquer law
518497comp (Fork a b) c = Fork (comp a c) (comp b c) -- Fork fusion law
519498comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
520499{-# NOINLINE comp #-}
521−{-# RULES
500+{-# RULES
522501 "comp/iden1" forall m. comp m iden = m ;
523502 "comp/iden2" forall m. comp iden m = m
524503#-}
@@ -528,7 +507,6 @@ comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
528507infixl 7 .|
529508-- | Scalar multiplication of matrices.
530509(.|) :: Num e => e -> Matrix e cols rows -> Matrix e cols rows
531−(.|) _ Empty = Empty
532510(.|) e (One a) = One (e * a)
533511(.|) e (Join a b) = Join (e .| a) (e .| b)
534512(.|) e (Fork a b) = Fork (e .| a) (e .| b)
@@ -538,7 +516,6 @@ infixl 7 .|
538516infixl 7 ./
539517-- | Scalar multiplication of matrices.
540518(./) :: Fractional e => Matrix e cols rows -> e -> Matrix e cols rows
541−(./) Empty _ = Empty
542519(./) (One a) e = One (a / e)
543520(./) (Join a b) e = Join (a ./ e) (b ./ e)
544521(./) (Fork a b) e = Fork (a ./ e) (b ./ e)
@@ -546,45 +523,59 @@ infixl 7 ./
546523-- Projections
547524
548525-- | Biproduct first component projection
549−p1 :: (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e (Either m n) m
550−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
551528
552529-- | Biproduct second component projection
553−p2 :: (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e (Either m n) n
554−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
555532
556533-- Injections
557534
558535-- | Biproduct first component injection
559−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)
560537i1 = tr p1
561538
562539-- | Biproduct second component injection
563−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)
564541i2 = tr p2
565542
566543-- Dimensions
567544
568545-- | Obtain the number of rows.
569546--
570−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
571−-- dimensions in constant time.
572−--
573−-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
574−-- 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''.
575550rows :: forall e cols rows. (Countable rows) => Matrix e cols rows -> Int
576551rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
577552
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+
578562-- | Obtain the number of columns.
579−--
580−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
581−-- dimensions in constant time.
582563--
583−-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
584−-- 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''.
585567columns :: forall e cols rows. (Countable cols) => Matrix e cols rows -> Int
586568columns _ = fromInteger $ natVal (Proxy :: Proxy (Count cols))
587569
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+
588579-- Coproduct Bifunctor
589580
590581infixl 5 -|-
@@ -593,11 +584,11 @@ infixl 5 -|-
593584(-|-) ::
594585 forall e n k m j.
595586 ( Num e,
596− CountableDimensions j k,
597− FromLists e k k,
598− FromLists e j k,
599− FromLists e k j,
600− FromLists e j j
587+ CountableDims j k,
588+ FL k k,
589+ FL j k,
590+ FL k j,
591+ FL j j
601592 ) =>
602593 Matrix e n k ->
603594 Matrix e m j ->
@@ -610,8 +601,8 @@ infixl 5 -|-
610601fstM ::
611602 forall e m k .
612603 ( Num e,
613− CountableDimensions k m,
614− FromLists e (Normalize (m, k)) m,
604+ CountableDims k m,
605+ FL (Normalize (m, k)) m,
615606 CountableN (m, k)
616607 ) => Matrix e (Normalize (m, k)) m
617608fstM = matrixBuilder' f
@@ -625,8 +616,8 @@ fstM = matrixBuilder' f
625616sndM ::
626617 forall e m k .
627618 ( Num e,
628− CountableDimensions k m,
629− FromLists e (Normalize (m, k)) k,
619+ CountableDims k m,
620+ FL (Normalize (m, k)) k,
630621 CountableN (m, k)
631622 ) => Matrix e (Normalize (m, k)) k
632623sndM = matrixBuilder' f
@@ -639,7 +630,7 @@ sndM = matrixBuilder' f
639630-- | Khatri Rao Matrix product also known as matrix pairing.
640631--
641632-- NOTE: That this is not a true categorical product, see for instance:
642−--
633+--
643634-- @
644635-- | fstM . kr a b == a
645636-- kr a b ==> |
@@ -650,10 +641,10 @@ sndM = matrixBuilder' f
650641kr ::
651642 forall e cols a b.
652643 ( Num e,
653− CountableDimensions a b,
644+ CountableDims a b,
654645 CountableN (a, b),
655− FromLists e (Normalize (a, b)) a,
656− FromLists e (Normalize (a, b)) b
646+ FL (Normalize (a, b)) a,
647+ FL (Normalize (a, b)) b
657648 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (Normalize (a, b))
658649kr a b =
659650 let fstM' = fstM @e @a @b
@@ -668,14 +659,14 @@ infixl 4 ><
668659(><) ::
669660 forall e m p n q.
670661 ( Num e,
671− CountableDimensions m n,
672− CountableDimensions p q,
673− CountableDimensionsN (m, n) (p, q),
674− FromLists e (Normalize (m, n)) m,
675− FromLists e (Normalize (m, n)) n,
676− FromLists e (Normalize (p, q)) p,
677− FromLists e (Normalize (p, q)) q
678− )
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+ )
679670 => Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
680671(><) a b =
681672 let fstM' = fstM @e @m @n
@@ -693,7 +684,6 @@ infixl 4 ><
693684-- @
694685abideJF :: Matrix e cols rows -> Matrix e cols rows
695686abideJF (Join (Fork a c) (Fork b d)) = Fork (Join (abideJF a) (abideJF b)) (Join (abideJF c) (abideJF d)) -- Join-Fork abide law
696−abideJF Empty = Empty
697687abideJF (One e) = One e
698688abideJF (Join a b) = Join (abideJF a) (abideJF b)
699689abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
@@ -707,7 +697,6 @@ abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
707697-- @
708698abideFJ :: Matrix e cols rows -> Matrix e cols rows
709699abideFJ (Fork (Join a b) (Join c d)) = Join (Fork (abideFJ a) (abideFJ c)) (Fork (abideFJ b) (abideFJ d)) -- Fork-Join abide law
710−abideFJ Empty = Empty
711700abideFJ (One e) = One e
712701abideFJ (Join a b) = Join (abideFJ a) (abideFJ b)
713702abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
@@ -716,7 +705,6 @@ abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
716705
717706-- | Matrix transposition.
718707tr :: Matrix e cols rows -> Matrix e rows cols
719−tr Empty = Empty
720708tr (One e) = One e
721709tr (Join a b) = Fork (tr a) (tr b)
722710tr (Fork a b) = Join (tr a) (tr b)
@@ -725,32 +713,32 @@ tr (Fork a b) = Join (tr a) (tr b)
725713
726714-- | Selective functors 'select' operator equivalent inspired by the
727715-- ArrowMonad solution presented in the paper.
728−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
729717select (Fork a b) y = y . a + b -- Divide-and-conquer law
730718select (Join (Fork a c) (Fork b d)) y = join (y . a + c) (y . b + d) -- Pattern matching + DnC law
731−select m y = join y iden . m
719+select m y = join y id . m
732720
733721branch ::
734722 ( Num e,
735− CountableDimensions a b,
736− CountableDimensions c (Either b c),
737− FromLists e c b,
738− FromLists e a b,
739− FromLists e a a,
740− FromLists e b b,
741− FromLists e c c,
742− FromLists e b a,
743− FromLists e b c,
744− FromLists e (Either b c) b,
745− 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
746734 )
747735 => Matrix e cols (Either a b) -> Matrix e a c -> Matrix e b c -> Matrix e cols c
748736branch x l r = f x `select` g l `select` r
749737 where
750− 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)
751739 => Matrix e cols (Either a b) -> Matrix e cols (Either a (Either b c))
752740 f m = fork (tr i1) (i1 . tr i2) . m
753− 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)
754742 g m = i2 . m
755743
756744-- McCarthy's Conditional
@@ -759,9 +747,9 @@ branch x l r = f x `select` g l `select` r
759747cond ::
760748 ( Trivial cols,
761749 Countable cols,
762− FromLists e () cols,
763− FromLists e cols (),
764− FromLists e cols cols,
750+ FL () cols,
751+ FL cols (),
752+ FL cols cols,
765753 Bounded a,
766754 Enum a,
767755 Num e,
@@ -774,9 +762,9 @@ cond p f g = join f g . grd p
774762grd ::
775763 ( Trivial q,
776764 Countable q,
777− FromLists e () q,
778− FromLists e q (),
779− FromLists e q q,
765+ FL () q,
766+ FL q (),
767+ FL q q,
780768 Bounded a,
781769 Enum a,
782770 Num e,
@@ -790,14 +778,14 @@ corr ::
790778 forall e a q .
791779 ( Trivial q,
792780 Countable q,
793− FromLists e () q,
794− FromLists e q (),
795− FromLists e q q,
781+ FL () q,
782+ FL q (),
783+ FL q q,
796784 Liftable e a Bool
797785 )
798786 => (a -> Bool) -> Matrix e q q
799787corr p = let f = fromF' p :: Matrix e q ()
800− in kr f (iden :: Matrix e q q)
788+ in kr f (id :: Matrix e q q)
801789
802790-- Pretty print
803791
@@ -821,7 +809,7 @@ prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
821809 fill str = replicate (widest - length str - 2) ' ' ++ str
822810
823811-- | Matrix pretty printer
824−pretty :: (CountableDimensions cols rows, Show e) => Matrix e cols rows -> String
812+pretty :: (CountableDims cols rows, Show e) => Matrix e cols rows -> String
825813pretty m = concat
826814 [ "┌ ", unwords (replicate (columns m) blank), " ┐\n"
827815 , unlines
@@ -850,12 +838,11 @@ pretty m = concat
850838 (safeGet i j m)
851839
852840-- | Matrix pretty printer
853−prettyPrint :: (CountableDimensions cols rows, Show e) => Matrix e cols rows -> IO ()
841+prettyPrint :: (CountableDims cols rows, Show e) => Matrix e cols rows -> IO ()
854842prettyPrint = putStrLn . pretty
855843
856844-- | Zip two matrices with a given binary function
857845zipWithM :: (e -> f -> g) -> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
858−zipWithM _ Empty Empty = Empty
859846zipWithM f (One a) (One b) = One (f a b)
860847zipWithM f (Join a b) (Join c d) = Join (zipWithM f a c) (zipWithM f b d)
861848zipWithM f (Fork a b) (Fork c d) = Fork (zipWithM f a c) (zipWithM f b d)
@@ -880,14 +867,12 @@ fromBool False = nat 0
880867
881868-- | Relational negation
882869negateM :: Relation cols rows -> Relation cols rows
883−negateM Empty = Empty
884870negateM (One (Nat p)) = One (Nat (negate p))
885871negateM (Join a b) = Join (negateM a) (negateM b)
886872negateM (Fork a b) = Fork (negateM a) (negateM b)
887873
888874-- | Relational addition
889875orM :: Relation cols rows -> Relation cols rows -> Relation cols rows
890−orM Empty Empty = Empty
891876orM (One a) (One b) = One (fromBool (toBool a || toBool b))
892877orM (Join a b) (Join c d) = Join (orM a c) (orM b d)
893878orM (Fork a b) (Fork c d) = Fork (orM a c) (orM b d)
@@ -896,7 +881,6 @@ orM x@(Join _ _) y@(Fork _ _) = orM (abideJF x) y
896881
897882-- | Relational multiplication
898883andM :: Relation cols rows -> Relation cols rows -> Relation cols rows
899−andM Empty Empty = Empty
900884andM (One a) (One b) = One (fromBool (toBool a && toBool b))
901885andM (Join a b) (Join c d) = Join (andM a c) (andM b d)
902886andM (Fork a b) (Fork c d) = Fork (andM a c) (andM b d)
@@ -905,7 +889,6 @@ andM x@(Join _ _) y@(Fork _ _) = andM (abideJF x) y
905889
906890-- | Relational subtraction
907891subM :: Relation cols rows -> Relation cols rows -> Relation cols rows
908−subM Empty Empty = Empty
909892subM (One a) (One b) = if a - b < nat 0 then One (nat 0) else One (a - b)
910893subM (Join a b) (Join c d) = Join (subM a c) (subM b d)
911894subM (Fork a b) (Fork c d) = Fork (subM a c) (subM b d)
@@ -914,7 +897,6 @@ subM x@(Join _ _) y@(Fork _ _) = subM (abideJF x) y
914897
915898-- | Matrix relational composition.
916899compRel :: Relation cr rows -> Relation cols cr -> Relation cols rows
917−compRel Empty Empty = Empty
918900compRel (One a) (One b) = One (fromBool (toBool a && toBool b))
919901compRel (Join a b) (Fork c d) = orM (compRel a c) (compRel b d) -- Divide-and-conquer law
920902compRel (Fork a b) c = Fork (compRel a c) (compRel b c) -- Fork fusion law
@@ -922,7 +904,6 @@ compRel c (Join a b) = Join (compRel c a) (compRel c b) -- Join fusion
922904
923905-- | Matrix relational right division
924906divR :: Relation b c -> Relation b a -> Relation a c
925−divR Empty Empty = Empty
926907divR (One a) (One b) = One (fromBool (not (toBool b) || toBool a)) -- b implies a
927908divR (Join a b) (Join c d) = andM (divR a c) (divR b d)
928909divR (Fork a b) c = Fork (divR a c) (divR b c)
@@ -945,58 +926,24 @@ divS s r = divL r s `intersection` divR (tr r) (tr s)
945926fromFRel' ::
946927 forall a b cols rows.
947928 ( Liftable Boolean a b,
948− CountableDimensions cols rows,
949− FromLists Boolean rows cols
929+ CountableDims cols rows,
930+ FL rows cols
950931 ) =>
951932 (a -> b) ->
952933 Relation cols rows
953−fromFRel' f =
954− let minA = minBound @a
955− maxA = maxBound @a
956− minB = minBound @b
957− maxB = maxBound @b
958− ccols = fromInteger $ natVal (Proxy :: Proxy (Count cols))
959− rrows = fromInteger $ natVal (Proxy :: Proxy (Count rows))
960− elementsA = take ccols [minA .. maxA]
961− elementsB = take rrows [minB .. maxB]
962− combinations = (,) <$> elementsA <*> elementsB
963− combAp = map snd . sort . map (\(a, b) -> if f a == b
964− then ((fromEnum a, fromEnum b), nat 1)
965− else ((fromEnum a, fromEnum b), nat 0)) $ combinations
966− mList = buildList combAp rrows
967− in tr $ fromLists mList
968− where
969− buildList [] _ = []
970− buildList l r = take r l : buildList (drop r l) r
934+fromFRel' = fromF'
971935
972936-- | Lifts functions to relations with dimensions matching @a@ and @b@
973937-- cardinality's.
974938fromFRel ::
975939 forall a b.
976940 ( Liftable Boolean a b,
977− CountableDimensionsN a b,
978− FromLists Boolean (Normalize b) (Normalize a)
941+ CountableDimsN a b,
942+ FLN b a
979943 ) =>
980944 (a -> b) ->
981945 Relation (Normalize a) (Normalize b)
982−fromFRel f =
983− let minA = minBound @a
984− maxA = maxBound @a
985− minB = minBound @b
986− maxB = maxBound @b
987− ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
988− rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
989− elementsA = take ccols [minA .. maxA]
990− elementsB = take rrows [minB .. maxB]
991− combinations = (,) <$> elementsA <*> elementsB
992− combAp = map snd . sort . map (\(a, b) -> if f a == b
993− then ((fromEnum a, fromEnum b), nat 1)
994− else ((fromEnum a, fromEnum b), nat 0)) $ combinations
995− mList = buildList combAp rrows
996− in tr $ fromLists mList
997− where
998− buildList [] _ = []
999− buildList l r = take r l : buildList (drop r l) r
946+fromFRel = fromFRel'
1000947
1001948-- | Lifts a relation function to a Boolean Matrix
1002949toRel ::
@@ -1006,17 +953,17 @@ toRel ::
1006953 Enum a,
1007954 Enum b,
1008955 Eq b,
1009− CountableDimensionsN a b,
1010− FromListsN Boolean b a
1011− )
956+ CountableDims a b,
957+ FLN b a
958+ )
1012959 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
1013960toRel f =
1014961 let minA = minBound @a
1015962 maxA = maxBound @a
1016963 minB = minBound @b
1017964 maxB = maxBound @b
1018− ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
1019− rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
965+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count a))
966+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count b))
1020967 elementsA = take ccols [minA .. maxA]
1021968 elementsB = take rrows [minB .. maxB]
1022969 combinations = (,) <$> elementsA <*> elementsB