@@ -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)))
@@ -430,12 +428,29 @@ fromF' f =
430428fromF ::
431429 forall a b e.
432430 ( Liftable e a b,
433− CountableDimsN a b,
431+ CountableDims a b,
434432 FLN b a
435433 ) =>
436434 (a -> b) ->
437435 Matrix e (Normalize a) (Normalize b)
438−fromF = fromF'
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
439454
440455-- Conversion
441456
@@ -931,19 +946,53 @@ fromFRel' ::
931946 ) =>
932947 (a -> b) ->
933948 Relation cols rows
934−fromFRel' = fromF'
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
935967
936968-- | Lifts functions to relations with dimensions matching @a@ and @b@
937969-- cardinality's.
938970fromFRel ::
939971 forall a b.
940972 ( Liftable Boolean a b,
941− CountableDimsN a b,
942− FLN b a
973+ CountableDims a b,
974+ FL (Normalize b) (Normalize a)
943975 ) =>
944976 (a -> b) ->
945977 Relation (Normalize a) (Normalize b)
946−fromFRel = fromFRel'
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
947996
948997-- | Lifts a relation function to a Boolean Matrix
949998toRel ::