@@ -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,6 +259,7 @@ 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
262 a + b = zipWithM (+) a b
263 a + b = zipWithM (+) a b
264 a - b = zipWithM (-) a b
265 a - b = zipWithM (-) a b
@@ -325,8 +326,7 @@ instance (FromLists cols ()) => FromLists (Either () cols) () where
325 fromLists [h : t] = Join (One h) (fromLists [t])
326 fromLists [h : t] = Join (One h) (fromLists [t])
326 fromLists _ = error "Wrong dimensions"
327 fromLists _ = error "Wrong dimensions"
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
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,15 +336,13 @@ 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 #-}
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
341 fromLists l@([_] : _) =
340 fromLists l@([_] : _) =
342 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
341 let rowsA = fromInteger (natVal (Proxy :: Proxy (Count a)))
343 in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
342 in Fork (fromLists (take rowsA l)) (fromLists (drop rowsA l))
344 fromLists _ = error "Wrong dimensions"
343 fromLists _ = error "Wrong dimensions"
346−instance (FromLists (Either a b) c, FromLists (Either a b) d, Countable c)
345+instance (FromLists (Either a b) c, FromLists (Either a b) d, Countable c) => FromLists (Either a b) (Either c d) where
347− => FromLists (Either a b) (Either c d) where
348 fromLists l@(h : t) =
346 fromLists l@(h : t) =
349 let lh = length h
347 let lh = length h
350 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
348 rowsC = fromInteger (natVal (Proxy :: Proxy (Count c)))
@@ -371,15 +369,16 @@ matrixBuilder' f =
371matrixBuilder ::
369matrixBuilder ::
372 forall e a b.
370 forall e a b.
373 ( FLN a b,
371 ( FLN a b,
376 Bounded a,
375 Bounded a,
377 Bounded b,
376 Bounded b,
379− Countable b
378+ CountableDimsN a b
380 ) => ((a, b) -> e) -> Matrix e (Normalize a) (Normalize b)
379 ) => ((a, b) -> e) -> Matrix e (Normalize a) (Normalize b)
381matrixBuilder f =
380matrixBuilder f =
382− let r = fromInteger $ natVal (Proxy :: Proxy (Count b))
381+ let r = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
383 positions = [(a, b) | a <- [minBound .. maxBound], b <- [minBound .. maxBound]]
382 positions = [(a, b) | a <- [minBound .. maxBound], b <- [minBound .. maxBound]]
384 in fromLists . map (map f) . transpose . buildList r $ positions
383 in fromLists . map (map f) . transpose . buildList r $ positions
@@ -435,7 +434,24 @@ fromF ::
436 (a -> b) ->
435 (a -> b) ->
437 Matrix e (Normalize a) (Normalize b)
436 Matrix e (Normalize a) (Normalize b)
438−fromF = fromF'
437+fromF f =
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
440-- Conversion
456-- Conversion
@@ -931,7 +947,24 @@ fromFRel' ::
932 (a -> b) ->
948 (a -> b) ->
933 Relation cols rows
949 Relation cols rows
934−fromFRel' = fromF'
950+fromFRel' f =
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
936-- | Lifts functions to relations with dimensions matching @a@ and @b@
969-- | Lifts functions to relations with dimensions matching @a@ and @b@
937-- cardinality's.
970-- cardinality's.
@@ -939,11 +972,28 @@ fromFRel ::
939 forall a b.
972 forall a b.
940 ( Liftable Boolean a b,
973 ( Liftable Boolean a b,
941 CountableDimsN a b,
974 CountableDimsN a b,
942− FLN b a
975+ FL (Normalize b) (Normalize a)
944 (a -> b) ->
977 (a -> b) ->
945 Relation (Normalize a) (Normalize b)
978 Relation (Normalize a) (Normalize b)
946−fromFRel = fromFRel'
979+fromFRel f =
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
948-- | Lifts a relation function to a Boolean Matrix
998-- | Lifts a relation function to a Boolean Matrix
@@ -953,7 +1003,7 @@ toRel ::
956− CountableDims a b,
1006+ CountableDimsN a b,
959 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
1009 => (a -> b -> Bool) -> Relation (Normalize a) (Normalize b)
@@ -962,8 +1012,8 @@ toRel f =
962 maxA = maxBound @a
1012 maxA = maxBound @a
963 minB = minBound @b
1013 minB = minBound @b
964 maxB = maxBound @b
1014 maxB = maxBound @b
965− ccols = fromInteger $ natVal (Proxy :: Proxy (Count a))
1015+ ccols = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize a)))
966− rrows = fromInteger $ natVal (Proxy :: Proxy (Count b))
1016+ rrows = fromInteger $ natVal (Proxy :: Proxy (Count (Normalize b)))
967 elementsA = take ccols [minA .. maxA]
1017 elementsA = take ccols [minA .. maxA]
968 elementsB = take rrows [minB .. maxB]
1018 elementsB = take rrows [minB .. maxB]
969 combinations = (,) <$> elementsA <*> elementsB
1019 combinations = (,) <$> elementsA <*> elementsB