@@ -176,6 +176,7 @@ import Data.Kind
176176import Data.List
177177import Data.Maybe
178178import Data.Proxy
179+import Data.Typeable
179180import Data.Void
180181import GHC.TypeLits
181182import Data.Type.Equality
@@ -242,7 +243,7 @@ type Trivial a = FromNat (Count a) ~ a
242243-- class.
243244instance (Num e) => Category (Matrix e) where
244245 type Object (Matrix e) a = (FL a a, Countable a)
245− id = iden
246+ id = iden
246247 (.) = comp
247248
248249instance NFData e => NFData (Matrix e cols rows) where
@@ -258,7 +259,6 @@ instance Eq e => Eq (Matrix e cols rows) where
258259 x@(Join _ _) == y@(Fork _ _) = abideJF x == y
259260
260261instance Num e => Num (Matrix e cols rows) where
261−
262262 a + b = zipWithM (+) a b
263263
264264 a - b = zipWithM (-) a b
@@ -317,31 +317,34 @@ class FromLists cols rows where
317317 -- error if the dimensions do not match.
318318 fromLists :: [[e]] -> Matrix e cols rows
319319
320−instance {-# OVERLAPPING #-} FromLists () () where
320+instance FromLists () () where
321321 fromLists [[e]] = One e
322322 fromLists _ = error "Wrong dimensions"
323323
324−instance {-# OVERLAPPING #-} (FromLists cols ()) => FromLists (Either () cols) () where
324+instance (FromLists cols ()) => FromLists (Either () cols) () where
325325 fromLists [h : t] = Join (One h) (fromLists [t])
326326 fromLists _ = error "Wrong dimensions"
327327
328−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
329330 fromLists [l] =
330331 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
331332 in Join (fromLists [take rowsA l]) (fromLists [drop rowsA l])
332333 fromLists _ = error "Wrong dimensions"
333334
334−instance {-# OVERLAPPING #-} (FromLists () rows) => FromLists () (Either () rows) where
335+instance (FromLists () rows) => FromLists () (Either () rows) where
335336 fromLists ([h] : t) = Fork (One h) (fromLists t)
336337 fromLists _ = error "Wrong dimensions"
337338
338−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
339341 fromLists l@([_] : _) =
340342 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
341343 in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
342344 fromLists _ = error "Wrong dimensions"
343345
344−instance {-# OVERLAPPABLE #-} (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
345348 fromLists l@(h : t) =
346349 let lh = length h
347350 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
@@ -368,16 +371,15 @@ matrixBuilder' f =
368371matrixBuilder ::
369372 forall e a b.
370373 ( FLN a b,
371− CountableN b,
372374 Enum a,
373375 Enum b,
374376 Bounded a,
375377 Bounded b,
376378 Eq a,
377− CountableDimsN a b
379+ Countable b
378380 ) => ((a, b) -> e) -> Matrix e (Normalize a) (Normalize b)
379381matrixBuilder f =
380− let r = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
382+ let r = fromInteger $ natVal (Proxy :: Proxy (Count b))
381383 positions = [(a, b) | a <- [minBound .. maxBound], b <- [minBound .. maxBound]]
382384 in fromLists . map (map f) . transpose . buildList r $ positions
383385 where
@@ -433,24 +435,7 @@ fromF ::
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 (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
438+fromF = fromF'
454439
455440-- Conversion
456441
@@ -946,24 +931,7 @@ fromFRel' ::
946931 ) =>
947932 (a -> b) ->
948933 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
934+fromFRel' = fromF'
967935
968936-- | Lifts functions to relations with dimensions matching @a@ and @b@
969937-- cardinality's.
@@ -971,28 +939,11 @@ fromFRel ::
971939 forall a b.
972940 ( Liftable Boolean a b,
973941 CountableDimsN a b,
974− FL (Normalize b) (Normalize a)
942+ FLN b a
975943 ) =>
976944 (a -> b) ->
977945 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 (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
946+fromFRel = fromFRel'
996947
997948-- | Lifts a relation function to a Boolean Matrix
998949toRel ::
@@ -1002,7 +953,7 @@ toRel ::
1002953 Enum a,
1003954 Enum b,
1004955 Eq b,
1005− CountableDimsN a b,
956+ CountableDims a b,
1006957 FLN b a
1007958 )
1008959 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
@@ -1011,8 +962,8 @@ toRel f =
1011962 maxA = maxBound @a
1012963 minB = minBound @b
1013964 maxB = maxBound @b
1014− ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
1015− rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
965+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count a))
966+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count b))
1016967 elementsA = take ccols [minA .. maxA]
1017968 elementsB = take rrows [minB .. maxB]
1018969 combinations = (,) <$> elementsA <*> elementsB