@@ -176,7 +176,6 @@ import Data.Kind
176176import Data.List
177177import Data.Maybe
178178import Data.Proxy
179−import Data.Typeable
180179import Data.Void
181180import GHC.TypeLits
182181import Data.Type.Equality
@@ -243,7 +242,7 @@ type Trivial a = FromNat (Count a) ~ a
243242-- class.
244243instance (Num e) => Category (Matrix e) where
245244 type Object (Matrix e) a = (FL a a, Countable a)
246− id = iden
245+ id = iden
247246 (.) = comp
248247
249248instance NFData e => NFData (Matrix e cols rows) where
@@ -259,6 +258,7 @@ instance Eq e => Eq (Matrix e cols rows) where
259258 x@(Join _ _) == y@(Fork _ _) = abideJF x == y
260259
261260instance Num e => Num (Matrix e cols rows) where
261+
262262 a + b = zipWithM (+) a b
263263
264264 a - b = zipWithM (-) a b
@@ -317,34 +317,31 @@ class FromLists cols rows where
317317 -- error if the dimensions do not match.
318318 fromLists :: [[e]] -> Matrix e cols rows
319319
320−instance FromLists () () where
320+instance {-# OVERLAPPING #-} FromLists () () where
321321 fromLists [[e]] = One e
322322 fromLists _ = error "Wrong dimensions"
323323
324−instance (FromLists cols ()) => FromLists (Either () cols) () where
324+instance {-# OVERLAPPING #-} (FromLists cols ()) => FromLists (Either () cols) () where
325325 fromLists [h : t] = Join (One h) (fromLists [t])
326326 fromLists _ = error "Wrong dimensions"
327327
328−instance {-# OVERLAPPABLE #-}
329− (FromLists a (), FromLists b (), Countable a) => FromLists (Either a b) () where
328+instance {-# OVERLAPPABLE #-} (FromLists a (), FromLists b (), Countable a) => FromLists (Either a b) () where
330329 fromLists [l] =
331330 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
332331 in Join (fromLists [take rowsA l]) (fromLists [drop rowsA l])
333332 fromLists _ = error "Wrong dimensions"
334333
335−instance (FromLists () rows) => FromLists () (Either () rows) where
334+instance {-# OVERLAPPING #-} (FromLists () rows) => FromLists () (Either () rows) where
336335 fromLists ([h] : t) = Fork (One h) (fromLists t)
337336 fromLists _ = error "Wrong dimensions"
338337
339−instance {-# OVERLAPPABLE #-}
340− (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
338+instance {-# OVERLAPPABLE #-} (FromLists () a, FromLists () b, Countable a) => FromLists () (Either a b) where
341339 fromLists l@([_] : _) =
342340 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
343341 in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
344342 fromLists _ = error "Wrong dimensions"
345343
346−instance (FromLists (Either a b) c, FromLists (Either a b) d, Countable c)
347− => FromLists (Either a b) (Either c d) where
344+instance {-# OVERLAPPABLE #-} (FromLists (Either a b) c, FromLists (Either a b) d, Countable c) => FromLists (Either a b) (Either c d) where
348345 fromLists l@(h : t) =
349346 let lh = length h
350347 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
@@ -371,15 +368,16 @@ matrixBuilder' f =
371368matrixBuilder ::
372369 forall e a b.
373370 ( FLN a b,
371+ CountableN b,
374372 Enum a,
375373 Enum b,
376374 Bounded a,
377375 Bounded b,
378376 Eq a,
379− Countable b
377+ CountableDimsN a b
380378 ) => ((a, b) -> e) -> Matrix e (Normalize a) (Normalize b)
381379matrixBuilder f =
382− let r = fromInteger $ natVal (Proxy :: Proxy (Count b))
380+ let r = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
383381 positions = [(a, b) | a <- [minBound .. maxBound], b <- [minBound .. maxBound]]
384382 in fromLists . map (map f) . transpose . buildList r $ positions
385383 where
@@ -435,7 +433,24 @@ fromF ::
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 (Normalize a)))
442+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize 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,7 +946,24 @@ 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.
@@ -939,11 +971,28 @@ fromFRel ::
939971 forall a b.
940972 ( Liftable Boolean a b,
941973 CountableDimsN a b,
942− FLN b a
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 (Normalize a)))
984+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize 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 ::
@@ -953,7 +1002,7 @@ toRel ::
9531002 Enum a,
9541003 Enum b,
9551004 Eq b,
956− CountableDims a b,
1005+ CountableDimsN a b,
9571006 FLN b a
9581007 )
9591008 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
@@ -962,8 +1011,8 @@ toRel f =
9621011 maxA = maxBound @a
9631012 minB = minBound @b
9641013 maxB = maxBound @b
965− ccols = fromInteger $ natVal (Proxy :: Proxy (Count a))
966− rrows = fromInteger $ natVal (Proxy :: Proxy (Count b))
1014+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
1015+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
9671016 elementsA = take ccols [minA .. maxA]
9681017 elementsB = take rrows [minB .. maxB]
9691018 combinations = (,) <$> elementsA <*> elementsB