@@ -243,7 +243,7 @@ type Trivial a = FromNat (Count a) ~ a
243243-- class.
244244instance (Num e) => Category (Matrix e) where
245245 type Object (Matrix e) a = (FL a a, Countable a)
246− id = iden
246+ id = iden
247247 (.) = comp
248248
249249instance NFData e => NFData (Matrix e cols rows) where
@@ -259,7 +259,6 @@ instance Eq e => Eq (Matrix e cols rows) where
259259 x@(Join _ _) == y@(Fork _ _) = abideJF x == y
260260
261261instance Num e => Num (Matrix e cols rows) where
262−
263262 a + b = zipWithM (+) a b
264263
265264 a - b = zipWithM (-) a b
@@ -326,7 +325,8 @@ instance (FromLists cols ()) => FromLists (Either () cols) () where
326325 fromLists [h : t] = Join (One h) (fromLists [t])
327326 fromLists _ = error "Wrong dimensions"
328327
329−instance {-# OVERLAPPABLE #-} (FromLists a (), FromLists b (), Countable a) => FromLists (Either a b) () where
328+instance {-# OVERLAPPABLE #-}
329+ (FromLists a (), FromLists b (), Countable a) => FromLists (Either a b) () where
330330 fromLists [l] =
331331 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
332332 in Join (fromLists [take rowsA l]) (fromLists [drop rowsA l])
@@ -336,13 +336,15 @@ instance (FromLists () rows) => FromLists () (Either () rows) where
336336 fromLists ([h] : t) = Fork (One h) (fromLists t)
337337 fromLists _ = error "Wrong dimensions"
338338
339−instance {-# OVERLAPPABLE #-} (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
339+instance {-# OVERLAPPABLE #-}
340+ (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
340341 fromLists l@([_] : _) =
341342 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
342343 in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
343344 fromLists _ = error "Wrong dimensions"
344345
345−instance (FromLists (Either a b) c, FromLists (Either a b) d, Countable c) => FromLists (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
346348 fromLists l@(h : t) =
347349 let lh = length h
348350 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
@@ -428,29 +430,12 @@ fromF' f =
428430fromF ::
429431 forall a b e.
430432 ( Liftable e a b,
431− CountableDims a b,
433+ CountableDimsN a b,
432434 FLN b a
433435 ) =>
434436 (a -> b) ->
435437 Matrix e (Normalize a) (Normalize b)
436−fromF f =
437− let minA = minBound @a
438− maxA = maxBound @a
439− minB = minBound @b
440− maxB = maxBound @b
441− ccols = fromInteger $ natVal (Proxy :: Proxy (Count a))
442− rrows = fromInteger $ natVal (Proxy :: Proxy (Count b))
443− elementsA = take ccols [minA .. maxA]
444− elementsB = take rrows [minB .. maxB]
445− combinations = (,) <$> elementsA <*> elementsB
446− combAp = map snd . sort . map (\(a, b) -> if f a == b
447− then ((fromEnum a, fromEnum b), 1)
448− else ((fromEnum a, fromEnum b), 0)) $ combinations
449− mList = buildList combAp rrows
450− in tr $ fromLists mList
451− where
452− buildList [] _ = []
453− buildList l r = take r l : buildList (drop r l) r
438+fromF = fromF'
454439
455440-- Conversion
456441
@@ -804,38 +789,35 @@ corr p = let f = fromF' p :: Matrix e q ()
804789
805790-- Pretty print
806791
807−prettyAux :: Show e => [[e]] -> [[e]] -> String
808−prettyAux [] _ = ""
809−prettyAux [[e]] m = "│ " ++ fill (show e) ++ " │\n"
792+fill :: Show e => [[e]] -> String -> String
793+fill m str = replicate (widest - length str - 2) ' ' ++ str
810794 where
811− v = fmap show m
812− widest = maximum $ fmap length v
813− fill str = replicate (widest - length str - 2) ' ' ++ str
814−prettyAux [h] m = "│ " ++ fill (unwords $ map show h) ++ " │\n"
815− where
816− v = fmap show m
817− widest = maximum $ fmap length v
818− fill str = replicate (widest - length str - 2) ' ' ++ str
819−prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
820− prettyAux t l
795+ v = fmap show m
796+ widest = maximum $ fmap length v
797+
798+fill2 :: Show e => Matrix e cols rows -> String -> String
799+fill2 m str = replicate (widest - length str - 2) ' ' ++ str
821800 where
822− v = fmap show l
823− widest = maximum $ fmap length v
824− fill str = replicate (widest - length str - 2) ' ' ++ str
801+ v = map show (toList m)
802+ widest = maximum $ map length v
803+
804+prettyAux :: Show e => [[e]] -> [[e]] -> String
805+prettyAux [] _ = ""
806+prettyAux [[e]] m = "│ " ++ fill m (show e) ++ " │\n"
807+prettyAux [h] m = "│ " ++ fill m (unwords $ map show h) ++ " │\n"
808+prettyAux (h : t) m = "│ " ++ fill m (unwords $ map show h) ++ " │\n" ++
809+ prettyAux t m
825810
826811-- | Matrix pretty printer
827812pretty :: (CountableDims cols rows, Show e) => Matrix e cols rows -> String
828813pretty m = concat
829814 [ "┌ ", unwords (replicate (columns m) blank), " ┐\n"
830815 , unlines
831− [ "│ " ++ unwords (fmap (\j -> fill $ show $ getElem i j m) [1..columns m]) ++ " │" | i <- [1..rows m] ]
816+ [ "│ " ++ unwords (fmap (\j -> fill2 m $ show $ getElem i j m) [1..columns m]) ++ " │" | i <- [1..rows m] ]
832817 , "└ ", unwords (replicate (columns m) blank), " ┘"
833818 ]
834819 where
835− strings = map show (toList m)
836− widest = maximum $ map length strings
837− fill str = replicate (widest - length str) ' ' ++ str
838− blank = fill ""
820+ blank = fill2 m ""
839821 safeGet i j m
840822 | i > rows m || j > columns m || i < 1 || j < 1 = Nothing
841823 | otherwise = Just $ unsafeGet i j m (toList m)
@@ -946,53 +928,19 @@ fromFRel' ::
946928 ) =>
947929 (a -> b) ->
948930 Relation cols rows
949−fromFRel' f =
950− let minA = minBound @a
951− maxA = maxBound @a
952− minB = minBound @b
953− maxB = maxBound @b
954− ccols = fromInteger $ natVal (Proxy :: Proxy (Count cols))
955− rrows = fromInteger $ natVal (Proxy :: Proxy (Count rows))
956− elementsA = take ccols [minA .. maxA]
957− elementsB = take rrows [minB .. maxB]
958− combinations = (,) <$> elementsA <*> elementsB
959− combAp = map snd . sort . map (\(a, b) -> if f a == b
960− then ((fromEnum a, fromEnum b), nat 1)
961− else ((fromEnum a, fromEnum b), nat 0)) $ combinations
962− mList = buildList combAp rrows
963− in tr $ fromLists mList
964− where
965− buildList [] _ = []
966− buildList l r = take r l : buildList (drop r l) r
931+fromFRel' = fromF'
967932
968933-- | Lifts functions to relations with dimensions matching @a@ and @b@
969934-- cardinality's.
970935fromFRel ::
971936 forall a b.
972937 ( Liftable Boolean a b,
973− CountableDims a b,
974− FL (Normalize b) (Normalize a)
938+ CountableDimsN a b,
939+ FLN b a
975940 ) =>
976941 (a -> b) ->
977942 Relation (Normalize a) (Normalize b)
978−fromFRel f =
979− let minA = minBound @a
980− maxA = maxBound @a
981− minB = minBound @b
982− maxB = maxBound @b
983− ccols = fromInteger $ natVal (Proxy :: Proxy (Count a))
984− rrows = fromInteger $ natVal (Proxy :: Proxy (Count b))
985− elementsA = take ccols [minA .. maxA]
986− elementsB = take rrows [minB .. maxB]
987− combinations = (,) <$> elementsA <*> elementsB
988− combAp = map snd . sort . map (\(a, b) -> if f a == b
989− then ((fromEnum a, fromEnum b), nat 1)
990− else ((fromEnum a, fromEnum b), nat 0)) $ combinations
991− mList = buildList combAp rrows
992− in tr $ fromLists mList
993− where
994− buildList [] _ = []
995− buildList l r = take r l : buildList (drop r l) r
943+fromFRel = fromFRel'
996944
997945-- | Lifts a relation function to a Boolean Matrix
998946toRel ::