@@ -243,7 +243,7 @@ type Trivial a = FromNat (Count a) ~ a
244instance (Num e) => Category (Matrix e) where
244instance (Num e) => Category (Matrix e) where
245 type Object (Matrix e) a = (FL a a, Countable a)
245 type Object (Matrix e) a = (FL a a, Countable a)
246− id = iden
246+ id = iden
247 (.) = comp
247 (.) = comp
249instance NFData e => NFData (Matrix e cols rows) where
249instance NFData e => NFData (Matrix e cols rows) where
@@ -259,7 +259,6 @@ instance Eq e => Eq (Matrix e cols rows) where
259 x@(Join _ _) == y@(Fork _ _) = abideJF x == y
259 x@(Join _ _) == y@(Fork _ _) = abideJF x == y
261instance Num e => Num (Matrix e cols rows) where
261instance Num e => Num (Matrix e cols rows) where
263 a + b = zipWithM (+) a b
262 a + b = zipWithM (+) a b
265 a - b = zipWithM (-) a b
264 a - b = zipWithM (-) a b
@@ -326,7 +325,8 @@ instance (FromLists cols ()) => FromLists (Either () cols) () where
326 fromLists [h : t] = Join (One h) (fromLists [t])
325 fromLists [h : t] = Join (One h) (fromLists [t])
327 fromLists _ = error "Wrong dimensions"
326 fromLists _ = error "Wrong dimensions"
328+instance {-# OVERLAPPABLE #-}
329−instance {-# OVERLAPPABLE #-} (FromLists a (), FromLists b (), Countable a) => FromLists (Either a b) () where
329+ (FromLists a (), FromLists b (), Countable a) => FromLists (Either a b) () where
330 fromLists [l] =
330 fromLists [l] =
331 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
331 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
332 in Join (fromLists [take rowsA l]) (fromLists [drop rowsA l])
332 in Join (fromLists [take rowsA l]) (fromLists [drop rowsA l])
@@ -336,13 +336,15 @@ instance (FromLists () rows) => FromLists () (Either () rows) where
336 fromLists ([h] : t) = Fork (One h) (fromLists t)
336 fromLists ([h] : t) = Fork (One h) (fromLists t)
337 fromLists _ = error "Wrong dimensions"
337 fromLists _ = error "Wrong dimensions"
339+instance {-# OVERLAPPABLE #-}
339−instance {-# OVERLAPPABLE #-} (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
340+ (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
340 fromLists l@([_] : _) =
341 fromLists l@([_] : _) =
341 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
342 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
342 in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
343 in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
343 fromLists _ = error "Wrong dimensions"
344 fromLists _ = error "Wrong dimensions"
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
346 fromLists l@(h : t) =
348 fromLists l@(h : t) =
347 let lh = length h
349 let lh = length h
348 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
350 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
@@ -369,16 +371,15 @@ matrixBuilder' f =
369matrixBuilder ::
371matrixBuilder ::
370 forall e a b.
372 forall e a b.
371 ( FLN a b,
373 ( FLN a b,
375 Bounded a,
376 Bounded a,
376 Bounded b,
377 Bounded b,
378− CountableDimsN a b
379+ Countable b
379 ) => ((a, b) -> e) -> Matrix e (Normalize a) (Normalize b)
380 ) => ((a, b) -> e) -> Matrix e (Normalize a) (Normalize b)
380matrixBuilder f =
381matrixBuilder f =
381− let r = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
382+ let r = fromInteger $ natVal (Proxy :: Proxy (Count b))
382 positions = [(a, b) | a <- [minBound .. maxBound], b <- [minBound .. maxBound]]
383 positions = [(a, b) | a <- [minBound .. maxBound], b <- [minBound .. maxBound]]
383 in fromLists . map (map f) . transpose . buildList r $ positions
384 in fromLists . map (map f) . transpose . buildList r $ positions
@@ -434,24 +435,7 @@ fromF ::
435 (a -> b) ->
436 (a -> b) ->
436 Matrix e (Normalize a) (Normalize b)
437 Matrix e (Normalize a) (Normalize b)
437−fromF f =
438+fromF = fromF'
438− let minA = minBound @a
442− ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
443− rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
444− elementsA = take ccols [minA .. maxA]
445− elementsB = take rrows [minB .. maxB]
446− combinations = (,) <$> elementsA <*> elementsB
447− combAp = map snd . sort . map (\(a, b) -> if f a == b
448− then ((fromEnum a, fromEnum b), 1)
449− else ((fromEnum a, fromEnum b), 0)) $ combinations
450− mList = buildList combAp rrows
451− in tr $ fromLists mList
454− buildList l r = take r l : buildList (drop r l) r
456-- Conversion
440-- Conversion
@@ -805,38 +789,35 @@ corr p = let f = fromF' p :: Matrix e q ()
806-- Pretty print
790-- Pretty print
808−prettyAux :: Show e => [[e]] -> [[e]] -> String
792+fill :: Show e => [[e]] -> String -> String
810−prettyAux [[e]] m = "│ " ++ fill (show e) ++ " │\n"
813− widest = maximum $ fmap length v
814− fill str = replicate (widest - length str - 2) ' ' ++ str
793+fill m str = replicate (widest - length str - 2) ' ' ++ str
815−prettyAux [h] m = "│ " ++ fill (unwords $ map show h) ++ " │\n"
817− v = fmap show m
795+ v = fmap show m
818− widest = maximum $ fmap length v
796+ widest = maximum $ fmap length v
798+fill2 :: Show e => Matrix e cols rows -> String -> String
819− fill str = replicate (widest - length str - 2) ' ' ++ str
799+fill2 m str = replicate (widest - length str - 2) ' ' ++ str
820−prettyAux (h : t) l = "│ " ++ fill (unwords $ map show h) ++ " │\n" ++
823− v = fmap show l
801+ v = map show (toList m)
824− widest = maximum $ fmap length v
802+ widest = maximum $ map length v
825− fill str = replicate (widest - length str - 2) ' ' ++ str
803+
804+prettyAux :: Show e => [[e]] -> [[e]] -> String
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" ++
827-- | Matrix pretty printer
811-- | Matrix pretty printer
828pretty :: (CountableDims cols rows, Show e) => Matrix e cols rows -> String
812pretty :: (CountableDims cols rows, Show e) => Matrix e cols rows -> String
829pretty m = concat
813pretty m = concat
830 [ "┌ ", unwords (replicate (columns m) blank), " ┐\n"
814 [ "┌ ", unwords (replicate (columns m) blank), " ┐\n"
831 , unlines
815 , unlines
832− [ "│ " ++ 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] ]
833 , "└ ", unwords (replicate (columns m) blank), " ┘"
817 , "└ ", unwords (replicate (columns m) blank), " ┘"
836− strings = map show (toList m)
837− widest = maximum $ map length strings
838− fill str = replicate (widest - length str) ' ' ++ str
839− blank = fill ""
820+ blank = fill2 m ""
840 safeGet i j m
821 safeGet i j m
841 | i > rows m || j > columns m || i < 1 || j < 1 = Nothing
822 | i > rows m || j > columns m || i < 1 || j < 1 = Nothing
842 | otherwise = Just $ unsafeGet i j m (toList m)
823 | otherwise = Just $ unsafeGet i j m (toList m)
@@ -947,24 +928,7 @@ fromFRel' ::
948 (a -> b) ->
929 (a -> b) ->
949 Relation cols rows
930 Relation cols rows
950−fromFRel' f =
931+fromFRel' = fromF'
951− let minA = minBound @a
955− ccols = fromInteger $ natVal (Proxy :: Proxy (Count cols))
956− rrows = fromInteger $ natVal (Proxy :: Proxy (Count rows))
957− elementsA = take ccols [minA .. maxA]
958− elementsB = take rrows [minB .. maxB]
959− combinations = (,) <$> elementsA <*> elementsB
960− combAp = map snd . sort . map (\(a, b) -> if f a == b
961− then ((fromEnum a, fromEnum b), nat 1)
962− else ((fromEnum a, fromEnum b), nat 0)) $ combinations
963− mList = buildList combAp rrows
964− in tr $ fromLists mList
967− buildList l r = take r l : buildList (drop r l) r
969-- | Lifts functions to relations with dimensions matching @a@ and @b@
933-- | Lifts functions to relations with dimensions matching @a@ and @b@
970-- cardinality's.
934-- cardinality's.
@@ -972,28 +936,11 @@ fromFRel ::
972 forall a b.
936 forall a b.
973 ( Liftable Boolean a b,
937 ( Liftable Boolean a b,
974 CountableDimsN a b,
938 CountableDimsN a b,
975− FL (Normalize b) (Normalize a)
939+ FLN b a
977 (a -> b) ->
941 (a -> b) ->
978 Relation (Normalize a) (Normalize b)
942 Relation (Normalize a) (Normalize b)
979−fromFRel f =
943+fromFRel = fromFRel'
980− let minA = minBound @a
984− ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
985− rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
986− elementsA = take ccols [minA .. maxA]
987− elementsB = take rrows [minB .. maxB]
988− combinations = (,) <$> elementsA <*> elementsB
989− combAp = map snd . sort . map (\(a, b) -> if f a == b
990− then ((fromEnum a, fromEnum b), nat 1)
991− else ((fromEnum a, fromEnum b), nat 0)) $ combinations
992− mList = buildList combAp rrows
993− in tr $ fromLists mList
996− buildList l r = take r l : buildList (drop r l) r
998-- | Lifts a relation function to a Boolean Matrix
945-- | Lifts a relation function to a Boolean Matrix
@@ -1003,7 +950,7 @@ toRel ::
1006− CountableDimsN a b,
953+ CountableDims a b,
1009 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
956 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
@@ -1012,8 +959,8 @@ toRel f =
1012 maxA = maxBound @a
959 maxA = maxBound @a
1013 minB = minBound @b
960 minB = minBound @b
1014 maxB = maxBound @b
961 maxB = maxBound @b
1015− ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
962+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count a))
1016− rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
963+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count b))
1017 elementsA = take ccols [minA .. maxA]
964 elementsA = take ccols [minA .. maxA]
1018 elementsB = take rrows [minB .. maxB]
965 elementsB = take rrows [minB .. maxB]
1019 combinations = (,) <$> elementsA <*> elementsB
966 combinations = (,) <$> elementsA <*> elementsB