@@ -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,
@@ -165,6 +176,7 @@ import Data.Kind
165176import Data.List
166177import Data.Maybe
167178import Data.Proxy
179+import Data.Typeable
168180import Data.Void
169181import GHC.TypeLits
170182import Data.Type.Equality
@@ -175,7 +187,6 @@ import qualified Prelude (id, (.))
175187
176188-- | LAoP (Linear Algebra of Programming) Inductive Matrix definition.
177189data Matrix e cols rows where
178− Empty :: Matrix e Void Void
179190 One :: e -> Matrix e () ()
180191 Join :: Matrix e a rows -> Matrix e b rows -> Matrix e (Either a b) rows
181192 Fork :: Matrix e cols a -> Matrix e cols b -> Matrix e cols (Either a b)
@@ -205,7 +216,6 @@ type family Count (d :: Type) :: Nat where
205216--
206217-- Thanks to Li-Yao Xia this type family is super fast.
207218type family FromNat (n :: Nat) :: Type where
208− FromNat 0 = Void
209219 FromNat 1 = ()
210220 FromNat n = FromNat' (Mod n 2 == 0) (FromNat (Div n 2))
211221
@@ -222,27 +232,26 @@ type family Normalize (d :: Type) :: Type where
222232-- | Constraint type synonyms to keep the type signatures less convoluted
223233type Countable a = KnownNat (Count a)
224234type CountableN a = KnownNat (Count (Normalize a))
225−type CountableDimensions a b = (Countable a, Countable b)
226−type CountableDimensionsN a b = (CountableN a, CountableN b)
227−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)
228239type Liftable e a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num e, Ord e)
229240type Trivial a = FromNat (Count a) ~ a
230241
231242-- | It is possible to implement a constrained version of the category type
232243-- class.
233244instance (Num e) => Category (Matrix e) where
234− type Object (Matrix e) a = (FromLists e a a, Countable a)
235− id = iden
245+ type Object (Matrix e) a = (FL a a, Countable a)
246+ id = iden
236247 (.) = comp
237248
238249instance NFData e => NFData (Matrix e cols rows) where
239− rnf Empty = ()
240250 rnf (One e) = rnf e
241251 rnf (Join a b) = rnf a `seq` rnf b
242252 rnf (Fork a b) = rnf a `seq` rnf b
243253
244254instance Eq e => Eq (Matrix e cols rows) where
245− Empty == Empty = True
246255 (One a) == (One b) = a == b
247256 (Join a b) == (Join c d) = a == c && b == d
248257 (Fork a b) == (Fork c d) = a == c && b == d
@@ -250,25 +259,21 @@ instance Eq e => Eq (Matrix e cols rows) where
250259 x@(Join _ _) == y@(Fork _ _) = abideJF x == y
251260
252261instance Num e => Num (Matrix e cols rows) where
253−
254262 a + b = zipWithM (+) a b
255263
256264 a - b = zipWithM (-) a b
257265
258266 a * b = zipWithM (*) a b
259267
260− abs Empty = Empty
261268 abs (One a) = One (abs a)
262269 abs (Join a b) = Join (abs a) (abs b)
263270 abs (Fork a b) = Fork (abs a) (abs b)
264271
265− signum Empty = Empty
266272 signum (One a) = One (signum a)
267273 signum (Join a b) = Join (signum a) (signum b)
268274 signum (Fork a b) = Fork (signum a) (signum b)
269275
270276instance Ord e => Ord (Matrix e cols rows) where
271− Empty <= Empty = True
272277 (One a) <= (One b) = a <= b
273278 (Join a b) <= (Join c d) = (a <= c) && (b <= d)
274279 (Fork a b) <= (Fork c d) = (a <= c) && (b <= d)
@@ -277,10 +282,6 @@ instance Ord e => Ord (Matrix e cols rows) where
277282
278283-- Primitives
279284
280−-- | Empty matrix constructor
281−empty :: Matrix e Void Void
282−empty = Empty
283−
284285-- | Unit matrix constructor
285286one :: e -> Matrix e () ()
286287one = One
@@ -311,40 +312,39 @@ infixl 2 ===
311312--
312313-- Given that it is not possible to branch on types at the term level type
313314-- classes are needed very much like an inductive definition but on types.
314−class FromLists e cols rows where
315+class FromLists cols rows where
315316 -- | Build a matrix out of a list of list of elements. Throws a runtime
316317 -- error if the dimensions do not match.
317318 fromLists :: [[e]] -> Matrix e cols rows
318319
319−instance FromLists e Void Void where
320− fromLists [] = Empty
321− fromLists _ = error "Wrong dimensions"
322−
323−instance {-# OVERLAPPING #-} FromLists e () () where
320+instance FromLists () () where
324321 fromLists [[e]] = One e
325322 fromLists _ = error "Wrong dimensions"
326323
327−instance {-# OVERLAPPING #-} (FromLists e cols ()) => FromLists e (Either () cols) () where
324+instance (FromLists cols ()) => FromLists (Either () cols) () where
328325 fromLists [h : t] = Join (One h) (fromLists [t])
329326 fromLists _ = error "Wrong dimensions"
330327
331−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
332330 fromLists [l] =
333331 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
334332 in Join (fromLists [take rowsA l]) (fromLists [drop rowsA l])
335333 fromLists _ = error "Wrong dimensions"
336334
337−instance {-# OVERLAPPING #-} (FromLists e () rows) => FromLists e () (Either () rows) where
335+instance (FromLists () rows) => FromLists () (Either () rows) where
338336 fromLists ([h] : t) = Fork (One h) (fromLists t)
339337 fromLists _ = error "Wrong dimensions"
340338
341−instance {-# OVERLAPPABLE #-} (FromLists e () a, FromLists e () b, Countable a) => FromLists e () (Either a b) where
342− fromLists l@([_] : _) =
339+instance {-# OVERLAPPABLE #-}
340+ (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
341+ fromLists l@([_] : _) =
343342 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
344343 in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
345344 fromLists _ = error "Wrong dimensions"
346345
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
346+instance (FromLists (Either a b) c, FromLists (Either a b) d, Countable c)
347+ => FromLists (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)))
@@ -357,8 +357,8 @@ instance {-# OVERLAPPABLE #-} (FromLists e (Either a b) c, FromLists e (Either a
357357-- a construction function that operates with indices.
358358matrixBuilder' ::
359359 forall e cols rows.
360− ( FromLists e cols rows,
361− CountableDimensions cols rows
360+ ( FL cols rows,
361+ CountableDims cols rows
362362 ) => ((Int, Int) -> e) -> Matrix e cols rows
363363matrixBuilder' f =
364364 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
@@ -370,17 +370,16 @@ matrixBuilder' f =
370370-- a construction function that operates with arbitrary types.
371371matrixBuilder ::
372372 forall e a b.
373− ( FromListsN e a b,
374− CountableN 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 r = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
382+ let r = fromInteger $ natVal (Proxy :: Proxy (Count b))
384383 positions = [(a, b) | a <- [minBound .. maxBound], b <- [minBound .. maxBound]]
385384 in fromLists . map (map f) . transpose . buildList r $ positions
386385 where
@@ -388,11 +387,11 @@ matrixBuilder f =
388387 buildList r l = take r l : buildList r (drop r l)
389388
390389-- | Constructs a column vector matrix
391−col :: (FromLists e () rows) => [e] -> Matrix e () rows
390+col :: (FL () rows) => [e] -> Matrix e () rows
392391col = fromLists . map (: [])
393392
394393-- | Constructs a row vector matrix
395−row :: (FromLists e cols ()) => [e] -> Matrix e cols ()
394+row :: (FL cols ()) => [e] -> Matrix e cols ()
396395row = fromLists . (: [])
397396
398397-- | Lifts functions to matrices with arbitrary dimensions.
@@ -402,8 +401,8 @@ row = fromLists . (: [])
402401fromF' ::
403402 forall a b cols rows e.
404403 ( Liftable e a b,
405− CountableDimensions cols rows,
406− FromLists e rows cols
404+ CountableDims cols rows,
405+ FL rows cols
407406 ) =>
408407 (a -> b) ->
409408 Matrix e cols rows
@@ -431,35 +430,17 @@ fromF' f =
431430fromF ::
432431 forall a b e.
433432 ( Liftable e a b,
434− CountableDimensionsN a b,
435− FromListsN e b a
433+ CountableDimsN a b,
434+ FLN b a
436435 ) =>
437436 (a -> b) ->
438437 Matrix e (Normalize a) (Normalize b)
439−fromF f =
440− let minA = minBound @a
441− maxA = maxBound @a
442− minB = minBound @b
443− maxB = maxBound @b
444− ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
445− rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
446− elementsA = take ccols [minA .. maxA]
447− elementsB = take rrows [minB .. maxB]
448− combinations = (,) <$> elementsA <*> elementsB
449− combAp = map snd . sort . map (\(a, b) -> if f a == b
450− then ((fromEnum a, fromEnum b), 1)
451− else ((fromEnum a, fromEnum b), 0)) $ combinations
452− mList = buildList combAp rrows
453− in tr $ fromLists mList
454− where
455− buildList [] _ = []
456− buildList l r = take r l : buildList (drop r l) r
438+fromF = fromF'
457439
458440-- Conversion
459441
460442-- | Converts a matrix to a list of lists of elements.
461443toLists :: Matrix e cols rows -> [[e]]
462−toLists Empty = []
463444toLists (One e) = [[e]]
464445toLists (Fork l r) = toLists l ++ toLists r
465446toLists (Join l r) = zipWith (++) (toLists l) (toLists r)
@@ -471,7 +452,7 @@ toList = concat . toLists
471452-- Zeros Matrix
472453
473454-- | The zero matrix. A matrix wholly filled with zeros.
474−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
475456zeros = matrixBuilder' (const 0)
476457
477458-- Ones Matrix
@@ -479,20 +460,20 @@ zeros = matrixBuilder' (const 0)
479460-- | The ones matrix. A matrix wholly filled with ones.
480461--
481462-- Also known as T (Top) matrix.
482−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
483464ones = matrixBuilder' (const 1)
484465
485466-- Const Matrix
486467
487468-- | The constant matrix constructor. A matrix wholly filled with a given
488469-- value.
489−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
490471constant e = matrixBuilder' (const e)
491472
492473-- Bang Matrix
493474
494475-- | The T (Top) row vector matrix.
495−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 ()
496477bang =
497478 let c = fromInteger $ natVal (Proxy :: Proxy (Count cols))
498479 in fromLists [take c [1, 1 ..]]
@@ -500,7 +481,7 @@ bang =
500481-- iden Matrix
501482
502483-- | iden matrix.
503−iden :: forall e cols . (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
504485iden = matrixBuilder' (bool 0 1 . uncurry (==))
505486{-# NOINLINE iden #-}
506487
@@ -511,13 +492,12 @@ iden = matrixBuilder' (bool 0 1 . uncurry (==))
511492-- This definition takes advantage of divide-and-conquer and fusion laws
512493-- from LAoP.
513494comp :: (Num e) => Matrix e cr rows -> Matrix e cols cr -> Matrix e cols rows
514−comp Empty Empty = Empty
515495comp (One a) (One b) = One (a * b)
516496comp (Join a b) (Fork c d) = comp a c + comp b d -- Divide-and-conquer law
517497comp (Fork a b) c = Fork (comp a c) (comp b c) -- Fork fusion law
518498comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
519499{-# NOINLINE comp #-}
520−{-# RULES
500+{-# RULES
521501 "comp/iden1" forall m. comp m iden = m ;
522502 "comp/iden2" forall m. comp iden m = m
523503#-}
@@ -527,7 +507,6 @@ comp c (Join a b) = Join (comp c a) (comp c b) -- Join fusion law
527507infixl 7 .|
528508-- | Scalar multiplication of matrices.
529509(.|) :: Num e => e -> Matrix e cols rows -> Matrix e cols rows
530−(.|) _ Empty = Empty
531510(.|) e (One a) = One (e * a)
532511(.|) e (Join a b) = Join (e .| a) (e .| b)
533512(.|) e (Fork a b) = Fork (e .| a) (e .| b)
@@ -537,7 +516,6 @@ infixl 7 .|
537516infixl 7 ./
538517-- | Scalar multiplication of matrices.
539518(./) :: Fractional e => Matrix e cols rows -> e -> Matrix e cols rows
540−(./) Empty _ = Empty
541519(./) (One a) e = One (a / e)
542520(./) (Join a b) e = Join (a ./ e) (b ./ e)
543521(./) (Fork a b) e = Fork (a ./ e) (b ./ e)
@@ -545,45 +523,59 @@ infixl 7 ./
545523-- Projections
546524
547525-- | Biproduct first component projection
548−p1 :: (Num e, CountableDimensions n m, FromLists e n m, FromLists e m m) => Matrix e (Either m n) m
526+p1 :: (Num e, CountableDims n m, FL n m, FL m m) => Matrix e (Either m n) m
549527p1 = join id zeros
550528
551529-- | Biproduct second component projection
552−p2 :: (Num e, CountableDimensions n m, FromLists e m n, FromLists e n n) => Matrix e (Either m n) n
530+p2 :: (Num e, CountableDims n m, FL m n, FL n n) => Matrix e (Either m n) n
553531p2 = join zeros id
554532
555533-- Injections
556534
557535-- | Biproduct first component injection
558−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)
559537i1 = tr p1
560538
561539-- | Biproduct second component injection
562−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)
563541i2 = tr p2
564542
565543-- Dimensions
566544
567545-- | Obtain the number of rows.
568546--
569−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
570−-- dimensions in constant time.
571−--
572−-- TODO: A 'rows' function that does not need the 'KnownNat' constraint in
573−-- 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''.
574550rows :: forall e cols rows. (Countable rows) => Matrix e cols rows -> Int
575551rows _ = fromInteger $ natVal (Proxy :: Proxy (Count rows))
576552
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+
577562-- | Obtain the number of columns.
578−--
579−-- NOTE: The 'KnownNat' constaint is needed in order to obtain the
580−-- dimensions in constant time.
581563--
582−-- TODO: A 'columns' function that does not need the 'KnownNat' constraint in
583−-- 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''.
584567columns :: forall e cols rows. (Countable cols) => Matrix e cols rows -> Int
585568columns _ = fromInteger $ natVal (Proxy :: Proxy (Count cols))
586569
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+
587579-- Coproduct Bifunctor
588580
589581infixl 5 -|-
@@ -592,11 +584,11 @@ infixl 5 -|-
592584(-|-) ::
593585 forall e n k m j.
594586 ( Num e,
595− CountableDimensions j k,
596− FromLists e k k,
597− FromLists e j k,
598− FromLists e k j,
599− FromLists e j j
587+ CountableDims j k,
588+ FL k k,
589+ FL j k,
590+ FL k j,
591+ FL j j
600592 ) =>
601593 Matrix e n k ->
602594 Matrix e m j ->
@@ -609,8 +601,8 @@ infixl 5 -|-
609601fstM ::
610602 forall e m k .
611603 ( Num e,
612− CountableDimensions k m,
613− FromLists e (Normalize (m, k)) m,
604+ CountableDims k m,
605+ FL (Normalize (m, k)) m,
614606 CountableN (m, k)
615607 ) => Matrix e (Normalize (m, k)) m
616608fstM = matrixBuilder' f
@@ -624,8 +616,8 @@ fstM = matrixBuilder' f
624616sndM ::
625617 forall e m k .
626618 ( Num e,
627− CountableDimensions k m,
628− FromLists e (Normalize (m, k)) k,
619+ CountableDims k m,
620+ FL (Normalize (m, k)) k,
629621 CountableN (m, k)
630622 ) => Matrix e (Normalize (m, k)) k
631623sndM = matrixBuilder' f
@@ -638,7 +630,7 @@ sndM = matrixBuilder' f
638630-- | Khatri Rao Matrix product also known as matrix pairing.
639631--
640632-- NOTE: That this is not a true categorical product, see for instance:
641−--
633+--
642634-- @
643635-- | fstM . kr a b == a
644636-- kr a b ==> |
@@ -649,10 +641,10 @@ sndM = matrixBuilder' f
649641kr ::
650642 forall e cols a b.
651643 ( Num e,
652− CountableDimensions a b,
644+ CountableDims a b,
653645 CountableN (a, b),
654− FromLists e (Normalize (a, b)) a,
655− FromLists e (Normalize (a, b)) b
646+ FL (Normalize (a, b)) a,
647+ FL (Normalize (a, b)) b
656648 ) => Matrix e cols a -> Matrix e cols b -> Matrix e cols (Normalize (a, b))
657649kr a b =
658650 let fstM' = fstM @e @a @b
@@ -667,14 +659,14 @@ infixl 4 ><
667659(><) ::
668660 forall e m p n q.
669661 ( Num e,
670− CountableDimensions m n,
671− CountableDimensions p q,
672− CountableDimensionsN (m, n) (p, q),
673− FromLists e (Normalize (m, n)) m,
674− FromLists e (Normalize (m, n)) n,
675− FromLists e (Normalize (p, q)) p,
676− FromLists e (Normalize (p, q)) q
677− )
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+ )
678670 => Matrix e m p -> Matrix e n q -> Matrix e (Normalize (m, n)) (Normalize (p, q))
679671(><) a b =
680672 let fstM' = fstM @e @m @n
@@ -692,7 +684,6 @@ infixl 4 ><
692684-- @
693685abideJF :: Matrix e cols rows -> Matrix e cols rows
694686abideJF (Join (Fork a c) (Fork b d)) = Fork (Join (abideJF a) (abideJF b)) (Join (abideJF c) (abideJF d)) -- Join-Fork abide law
695−abideJF Empty = Empty
696687abideJF (One e) = One e
697688abideJF (Join a b) = Join (abideJF a) (abideJF b)
698689abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
@@ -706,7 +697,6 @@ abideJF (Fork a b) = Fork (abideJF a) (abideJF b)
706697-- @
707698abideFJ :: Matrix e cols rows -> Matrix e cols rows
708699abideFJ (Fork (Join a b) (Join c d)) = Join (Fork (abideFJ a) (abideFJ c)) (Fork (abideFJ b) (abideFJ d)) -- Fork-Join abide law
709−abideFJ Empty = Empty
710700abideFJ (One e) = One e
711701abideFJ (Join a b) = Join (abideFJ a) (abideFJ b)
712702abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
@@ -715,7 +705,6 @@ abideFJ (Fork a b) = Fork (abideFJ a) (abideFJ b)
715705
716706-- | Matrix transposition.
717707tr :: Matrix e cols rows -> Matrix e rows cols
718−tr Empty = Empty
719708tr (One e) = One e
720709tr (Join a b) = Fork (tr a) (tr b)
721710tr (Fork a b) = Join (tr a) (tr b)
@@ -724,32 +713,32 @@ tr (Fork a b) = Join (tr a) (tr b)
724713
725714-- | Selective functors 'select' operator equivalent inspired by the
726715-- ArrowMonad solution presented in the paper.
727−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
728717select (Fork a b) y = y . a + b -- Divide-and-conquer law
729718select (Join (Fork a c) (Fork b d)) y = join (y . a + c) (y . b + d) -- Pattern matching + DnC law
730719select m y = join y id . m
731720
732721branch ::
733722 ( Num e,
734− CountableDimensions a b,
735− CountableDimensions c (Either b c),
736− FromLists e c b,
737− FromLists e a b,
738− FromLists e a a,
739− FromLists e b b,
740− FromLists e c c,
741− FromLists e b a,
742− FromLists e b c,
743− FromLists e (Either b c) b,
744− 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
745734 )
746735 => Matrix e cols (Either a b) -> Matrix e a c -> Matrix e b c -> Matrix e cols c
747736branch x l r = f x `select` g l `select` r
748737 where
749− 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)
750739 => Matrix e cols (Either a b) -> Matrix e cols (Either a (Either b c))
751740 f m = fork (tr i1) (i1 . tr i2) . m
752− 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)
753742 g m = i2 . m
754743
755744-- McCarthy's Conditional
@@ -758,9 +747,9 @@ branch x l r = f x `select` g l `select` r
758747cond ::
759748 ( Trivial cols,
760749 Countable cols,
761− FromLists e () cols,
762− FromLists e cols (),
763− FromLists e cols cols,
750+ FL () cols,
751+ FL cols (),
752+ FL cols cols,
764753 Bounded a,
765754 Enum a,
766755 Num e,
@@ -773,9 +762,9 @@ cond p f g = join f g . grd p
773762grd ::
774763 ( Trivial q,
775764 Countable q,
776− FromLists e () q,
777− FromLists e q (),
778− FromLists e q q,
765+ FL () q,
766+ FL q (),
767+ FL q q,
779768 Bounded a,
780769 Enum a,
781770 Num e,
@@ -789,9 +778,9 @@ corr ::
789778 forall e a q .
790779 ( Trivial q,
791780 Countable q,
792− FromLists e () q,
793− FromLists e q (),
794− FromLists e q q,
781+ FL () q,
782+ FL q (),
783+ FL q q,
795784 Liftable e a Bool
796785 )
797786 => (a -> Bool) -> Matrix e q q
@@ -820,7 +809,7 @@ prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
820809 fill str = replicate (widest - length str - 2) ' ' ++ str
821810
822811-- | Matrix pretty printer
823−pretty :: (CountableDimensions cols rows, Show e) => Matrix e cols rows -> String
812+pretty :: (CountableDims cols rows, Show e) => Matrix e cols rows -> String
824813pretty m = concat
825814 [ "┌ ", unwords (replicate (columns m) blank), " ┐\n"
826815 , unlines
@@ -849,12 +838,11 @@ pretty m = concat
849838 (safeGet i j m)
850839
851840-- | Matrix pretty printer
852−prettyPrint :: (CountableDimensions cols rows, Show e) => Matrix e cols rows -> IO ()
841+prettyPrint :: (CountableDims cols rows, Show e) => Matrix e cols rows -> IO ()
853842prettyPrint = putStrLn . pretty
854843
855844-- | Zip two matrices with a given binary function
856845zipWithM :: (e -> f -> g) -> Matrix e cols rows -> Matrix f cols rows -> Matrix g cols rows
857−zipWithM _ Empty Empty = Empty
858846zipWithM f (One a) (One b) = One (f a b)
859847zipWithM f (Join a b) (Join c d) = Join (zipWithM f a c) (zipWithM f b d)
860848zipWithM f (Fork a b) (Fork c d) = Fork (zipWithM f a c) (zipWithM f b d)
@@ -879,14 +867,12 @@ fromBool False = nat 0
879867
880868-- | Relational negation
881869negateM :: Relation cols rows -> Relation cols rows
882−negateM Empty = Empty
883870negateM (One (Nat p)) = One (Nat (negate p))
884871negateM (Join a b) = Join (negateM a) (negateM b)
885872negateM (Fork a b) = Fork (negateM a) (negateM b)
886873
887874-- | Relational addition
888875orM :: Relation cols rows -> Relation cols rows -> Relation cols rows
889−orM Empty Empty = Empty
890876orM (One a) (One b) = One (fromBool (toBool a || toBool b))
891877orM (Join a b) (Join c d) = Join (orM a c) (orM b d)
892878orM (Fork a b) (Fork c d) = Fork (orM a c) (orM b d)
@@ -895,7 +881,6 @@ orM x@(Join _ _) y@(Fork _ _) = orM (abideJF x) y
895881
896882-- | Relational multiplication
897883andM :: Relation cols rows -> Relation cols rows -> Relation cols rows
898−andM Empty Empty = Empty
899884andM (One a) (One b) = One (fromBool (toBool a && toBool b))
900885andM (Join a b) (Join c d) = Join (andM a c) (andM b d)
901886andM (Fork a b) (Fork c d) = Fork (andM a c) (andM b d)
@@ -904,7 +889,6 @@ andM x@(Join _ _) y@(Fork _ _) = andM (abideJF x) y
904889
905890-- | Relational subtraction
906891subM :: Relation cols rows -> Relation cols rows -> Relation cols rows
907−subM Empty Empty = Empty
908892subM (One a) (One b) = if a - b < nat 0 then One (nat 0) else One (a - b)
909893subM (Join a b) (Join c d) = Join (subM a c) (subM b d)
910894subM (Fork a b) (Fork c d) = Fork (subM a c) (subM b d)
@@ -913,7 +897,6 @@ subM x@(Join _ _) y@(Fork _ _) = subM (abideJF x) y
913897
914898-- | Matrix relational composition.
915899compRel :: Relation cr rows -> Relation cols cr -> Relation cols rows
916−compRel Empty Empty = Empty
917900compRel (One a) (One b) = One (fromBool (toBool a && toBool b))
918901compRel (Join a b) (Fork c d) = orM (compRel a c) (compRel b d) -- Divide-and-conquer law
919902compRel (Fork a b) c = Fork (compRel a c) (compRel b c) -- Fork fusion law
@@ -921,7 +904,6 @@ compRel c (Join a b) = Join (compRel c a) (compRel c b) -- Join fusion
921904
922905-- | Matrix relational right division
923906divR :: Relation b c -> Relation b a -> Relation a c
924−divR Empty Empty = Empty
925907divR (One a) (One b) = One (fromBool (not (toBool b) || toBool a)) -- b implies a
926908divR (Join a b) (Join c d) = andM (divR a c) (divR b d)
927909divR (Fork a b) c = Fork (divR a c) (divR b c)
@@ -944,58 +926,24 @@ divS s r = divL r s `intersection` divR (tr r) (tr s)
944926fromFRel' ::
945927 forall a b cols rows.
946928 ( Liftable Boolean a b,
947− CountableDimensions cols rows,
948− FromLists Boolean rows cols
929+ CountableDims cols rows,
930+ FL rows cols
949931 ) =>
950932 (a -> b) ->
951933 Relation cols rows
952−fromFRel' f =
953− let minA = minBound @a
954− maxA = maxBound @a
955− minB = minBound @b
956− maxB = maxBound @b
957− ccols = fromInteger $ natVal (Proxy :: Proxy (Count cols))
958− rrows = fromInteger $ natVal (Proxy :: Proxy (Count rows))
959− elementsA = take ccols [minA .. maxA]
960− elementsB = take rrows [minB .. maxB]
961− combinations = (,) <$> elementsA <*> elementsB
962− combAp = map snd . sort . map (\(a, b) -> if f a == b
963− then ((fromEnum a, fromEnum b), nat 1)
964− else ((fromEnum a, fromEnum b), nat 0)) $ combinations
965− mList = buildList combAp rrows
966− in tr $ fromLists mList
967− where
968− buildList [] _ = []
969− buildList l r = take r l : buildList (drop r l) r
934+fromFRel' = fromF'
970935
971936-- | Lifts functions to relations with dimensions matching @a@ and @b@
972937-- cardinality's.
973938fromFRel ::
974939 forall a b.
975940 ( Liftable Boolean a b,
976− CountableDimensionsN a b,
977− FromLists Boolean (Normalize b) (Normalize a)
941+ CountableDimsN a b,
942+ FLN b a
978943 ) =>
979944 (a -> b) ->
980945 Relation (Normalize a) (Normalize b)
981−fromFRel f =
982− let minA = minBound @a
983− maxA = maxBound @a
984− minB = minBound @b
985− maxB = maxBound @b
986− ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
987− rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
988− elementsA = take ccols [minA .. maxA]
989− elementsB = take rrows [minB .. maxB]
990− combinations = (,) <$> elementsA <*> elementsB
991− combAp = map snd . sort . map (\(a, b) -> if f a == b
992− then ((fromEnum a, fromEnum b), nat 1)
993− else ((fromEnum a, fromEnum b), nat 0)) $ combinations
994− mList = buildList combAp rrows
995− in tr $ fromLists mList
996− where
997− buildList [] _ = []
998− buildList l r = take r l : buildList (drop r l) r
946+fromFRel = fromFRel'
999947
1000948-- | Lifts a relation function to a Boolean Matrix
1001949toRel ::
@@ -1005,17 +953,17 @@ toRel ::
1005953 Enum a,
1006954 Enum b,
1007955 Eq b,
1008− CountableDimensionsN a b,
1009− FromListsN Boolean b a
1010− )
956+ CountableDims a b,
957+ FLN b a
958+ )
1011959 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
1012960toRel f =
1013961 let minA = minBound @a
1014962 maxA = maxBound @a
1015963 minB = minBound @b
1016964 maxB = maxBound @b
1017− ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
1018− rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
965+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count a))
966+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count b))
1019967 elementsA = take ccols [minA .. maxA]
1020968 elementsB = take rrows [minB .. maxB]
1021969 combinations = (,) <$> elementsA <*> elementsB