@@ -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,6 +259,7 @@ 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+
262263 a + b = zipWithM (+) a b
263264
264265 a - b = zipWithM (-) a b
@@ -325,8 +326,7 @@ instance (FromLists cols ()) => FromLists (Either () cols) () where
325326 fromLists [h : t] = Join (One h) (fromLists [t])
326327 fromLists _ = error "Wrong dimensions"
327328
328−instance {-# OVERLAPPABLE #-}
329− (FromLists a (), FromLists b (), Countable a) => FromLists (Either a b) () where
329+instance {-# OVERLAPPABLE #-} (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,15 +336,13 @@ 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 #-}
340− (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
339+instance {-# OVERLAPPABLE #-} (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
341340 fromLists l@([_] : _) =
342341 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
343342 in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
344343 fromLists _ = error "Wrong dimensions"
345344
346−instance (FromLists (Either a b) c, FromLists (Either a b) d, Countable c)
347− => FromLists (Either a b) (Either c d) where
345+instance (FromLists (Either a b) c, FromLists (Either a b) d, Countable c) => FromLists (Either a b) (Either c d) where
348346 fromLists l@(h : t) =
349347 let lh = length h
350348 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
@@ -371,15 +369,16 @@ matrixBuilder' f =
371369matrixBuilder ::
372370 forall e a b.
373371 ( FLN a b,
372+ CountableN b,
374373 Enum a,
375374 Enum b,
376375 Bounded a,
377376 Bounded b,
378377 Eq a,
379− Countable b
378+ CountableDimsN a b
380379 ) => ((a, b) -> e) -> Matrix e (Normalize a) (Normalize b)
381380matrixBuilder f =
382− let r = fromInteger $ natVal (Proxy :: Proxy (Count b))
381+ let r = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
383382 positions = [(a, b) | a <- [minBound .. maxBound], b <- [minBound .. maxBound]]
384383 in fromLists . map (map f) . transpose . buildList r $ positions
385384 where
@@ -435,7 +434,24 @@ fromF ::
435434 ) =>
436435 (a -> b) ->
437436 Matrix e (Normalize a) (Normalize b)
438−fromF = fromF'
437+fromF f =
438+ let minA = minBound @a
439+ maxA = maxBound @a
440+ minB = minBound @b
441+ maxB = maxBound @b
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
452+ where
453+ buildList [] _ = []
454+ buildList l r = take r l : buildList (drop r l) r
439455
440456-- Conversion
441457
@@ -931,7 +947,24 @@ fromFRel' ::
931947 ) =>
932948 (a -> b) ->
933949 Relation cols rows
934−fromFRel' = fromF'
950+fromFRel' f =
951+ let minA = minBound @a
952+ maxA = maxBound @a
953+ minB = minBound @b
954+ maxB = maxBound @b
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
965+ where
966+ buildList [] _ = []
967+ buildList l r = take r l : buildList (drop r l) r
935968
936969-- | Lifts functions to relations with dimensions matching @a@ and @b@
937970-- cardinality's.
@@ -939,11 +972,28 @@ fromFRel ::
939972 forall a b.
940973 ( Liftable Boolean a b,
941974 CountableDimsN a b,
942− FLN b a
975+ FL (Normalize b) (Normalize a)
943976 ) =>
944977 (a -> b) ->
945978 Relation (Normalize a) (Normalize b)
946−fromFRel = fromFRel'
979+fromFRel f =
980+ let minA = minBound @a
981+ maxA = maxBound @a
982+ minB = minBound @b
983+ maxB = maxBound @b
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
994+ where
995+ buildList [] _ = []
996+ buildList l r = take r l : buildList (drop r l) r
947997
948998-- | Lifts a relation function to a Boolean Matrix
949999toRel ::
@@ -953,7 +1003,7 @@ toRel ::
9531003 Enum a,
9541004 Enum b,
9551005 Eq b,
956− CountableDims a b,
1006+ CountableDimsN a b,
9571007 FLN b a
9581008 )
9591009 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
@@ -962,8 +1012,8 @@ toRel f =
9621012 maxA = maxBound @a
9631013 minB = minBound @b
9641014 maxB = maxBound @b
965− ccols = fromInteger $ natVal (Proxy :: Proxy (Count a))
966− rrows = fromInteger $ natVal (Proxy :: Proxy (Count b))
1015+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
1016+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
9671017 elementsA = take ccols [minA .. maxA]
9681018 elementsB = take rrows [minB .. maxB]
9691019 combinations = (,) <$> elementsA <*> elementsB