@@ -1,3 +1,4 @@
1+{-# LANGUAGE TypeFamilies #-}
1{-# LANGUAGE ConstraintKinds #-}
2{-# LANGUAGE ConstraintKinds #-}
2{-# LANGUAGE GADTs #-}
3{-# LANGUAGE GADTs #-}
3{-# LANGUAGE UndecidableInstances #-}
4{-# LANGUAGE UndecidableInstances #-}
@@ -178,9 +179,9 @@ import Data.Void
178import qualified LAoP.Matrix.Internal as I
179import qualified LAoP.Matrix.Internal as I
179import LAoP.Utils.Internal
180import LAoP.Utils.Internal
180import Control.DeepSeq
181import Control.DeepSeq
181−import qualified Control.Category as C
182import Data.Bool
182import Data.Bool
183import GHC.TypeLits
183import GHC.TypeLits
184+import Prelude hiding (id, (.))
185-- | Boolean type synonym for working with boolean matrices
186-- | Boolean type synonym for working with boolean matrices
186type Boolean = Natural 0 1
187type Boolean = Natural 0 1
@@ -202,14 +203,12 @@ type Liftable a b = (Bounded a, Bounded b, Enum a, Enum b, Eq b, Num
202type Trivial a = I.Normalize a ~ I.Normalize (I.Normalize a)
203type Trivial a = I.Normalize a ~ I.Normalize (I.Normalize a)
203type TrivialP a b = I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
204type TrivialP a b = I.Normalize (a, b) ~ I.Normalize (I.Normalize a, I.Normalize b)
205−-- | It isn't possible to implement the 'id' function so it's
206+-- | It is possible to implement a constrained version of the category type
206−-- implementation is 'undefined'. However 'comp' can be and this partial
207+-- class.
207−-- class implementation exists just to make the code more readable.
209−-- Please use 'iden' instead.
210−instance C.Category Relation where
208+instance Category Relation where
211− id = undefined
209+ type Object Relation a = (FromListsN a a, CountableN a)
212− (.) = comp
211+ (.) = comp
214instance Num (Relation a b) where
213instance Num (Relation a b) where
215 -- | Matrix addition becomes Boolean matrix disjointion
214 -- | Matrix addition becomes Boolean matrix disjointion
@@ -387,7 +386,7 @@ belongs = toRel elemR
387-- Also known as ⊥ (Bottom) Relation or empty Relation.
386-- Also known as ⊥ (Bottom) Relation or empty Relation.
390−-- r ``comp`` ⊥ == ⊥ ``comp`` r == ⊥
389+-- r `.` ⊥ == ⊥ `.` r == ⊥
391-- ⊥ ``sse`` R && R ``sse`` T == True
390-- ⊥ ``sse`` R && R ``sse`` T == True
@@ -433,7 +432,7 @@ point = fromF . const
433-- | iden matrix
432-- | iden matrix
436−-- 'iden' ``comp`` r == r == r ``comp`` 'iden'
435+-- 'iden' `.` r == r == r `.` 'iden'
439 (FromListsN a a, CountableN a) => Relation a a
438 (FromListsN a a, CountableN a) => Relation a a
@@ -442,7 +441,7 @@ iden = relationBuilder' (bool (nat 0) (nat 1) . uncurry (==))
442-- | Relational composition
441-- | Relational composition
445−-- r ``comp`` (s ``comp`` p) = (r ``comp`` s) ``comp`` p
444+-- r `.` (s `.` p) = (r `.` s) `.` p
447comp :: Relation b c -> Relation a b -> Relation a c
446comp :: Relation b c -> Relation a b -> Relation a c
448comp (R a) (R b) = R (I.compRel a b)
447comp (R a) (R b) = R (I.compRel a b)
@@ -506,7 +505,7 @@ pointAp ::
506 FromListsN a One,
505 FromListsN a One,
507 FromListsN b One
506 FromListsN b One
508 ) => a -> b -> Relation a b -> Relation One One
507 ) => a -> b -> Relation a b -> Relation One One
509−pointAp a b r = conv (point b) `comp` r `comp` point a
508+pointAp a b r = conv (point b) . r . point a
511-- | Relational application
510-- | Relational application
@@ -518,7 +517,7 @@ pointApBool ::
518 FromListsN a One,
517 FromListsN a One,
519 FromListsN b One
518 FromListsN b One
520 ) => a -> b -> Relation a b -> Bool
519 ) => a -> b -> Relation a b -> Bool
521−pointApBool a b r = toBool $ conv (point b) `comp` r `comp` point a
520+pointApBool a b r = toBool $ conv (point b) . r . point a
523-- | Relational converse
522-- | Relational converse
@@ -560,8 +559,8 @@ intersection a b = a * b
561-- (r ``union`` s) ``union`` t == r `'union' (s ``union`` t)
560-- (r ``union`` s) ``union`` t == r `'union' (s ``union`` t)
562-- r ``union`` s ``sse`` x == r ``sse`` x && s ``sse`` x
561-- r ``union`` s ``sse`` x == r ``sse`` x && s ``sse`` x
563−-- r ``comp`` (s ``union`` t) == (r ``comp`` s) ``union`` (r ``comp`` t)
562+-- r `.` (s ``union`` t) == (r `.` s) ``union`` (r `.` t)
564−-- (s ``union`` t) ``comp`` r == (s ``comp`` r) ``union`` (t ``comp`` r)
563+-- (s ``union`` t) `.` r == (s `.` r) ``union`` (t `.` r)
566union :: Relation a b -> Relation a b -> Relation a b
565union :: Relation a b -> Relation a b -> Relation a b
567union a b = a + b
566union a b = a + b
@@ -569,29 +568,29 @@ union a b = a + b
569-- | Relation Kernel
568-- | Relation Kernel
572−-- 'ker' r == 'conv' r ``comp`` r
571+-- 'ker' r == 'conv' r `.` r
573-- 'ker' r == 'img' ('conv' r)
572-- 'ker' r == 'img' ('conv' r)
575ker :: Relation a b -> Relation a a
574ker :: Relation a b -> Relation a a
576−ker r = conv r `comp` r
575+ker r = conv r . r
578-- | Relation Image
577-- | Relation Image
581−-- 'img' r == r ``comp`` conv r
580+-- 'img' r == r `.` conv r
582-- 'img' r == 'ker' ('conv' r)
581-- 'img' r == 'ker' ('conv' r)
584img :: Relation a b -> Relation b b
583img :: Relation a b -> Relation b b
585−img r = r `comp` conv r
584+img r = r . conv r
587-- | Function division. Special case of 'divS'.
586-- | Function division. Special case of 'divS'.
589-- NOTE: _This is only valid_ if @f@ and @g@ are 'function's, i.e. 'simple' and
588-- NOTE: _This is only valid_ if @f@ and @g@ are 'function's, i.e. 'simple' and
590-- 'entire'.
589-- 'entire'.
592−-- @'divisionF' f g == 'conv' g ``comp`` f@
591+-- @'divisionF' f g == 'conv' g `.` f@
593divisionF :: Relation a c -> Relation b c -> Relation a b
592divisionF :: Relation a c -> Relation b c -> Relation a b
594−divisionF f g = conv g `comp` f
593+divisionF f g = conv g . f
596-- Taxonomy of binary relations
595-- Taxonomy of binary relations
@@ -617,8 +616,8 @@ surjective = reflexive . img
617-- relations:
616-- relations:
620−-- f ``comp`` r ``sse`` s == r ``sse`` f ``comp`` s
619+-- f `.` r ``sse`` s == r ``sse`` f `.` s
621−-- r ``comp`` f ``sse`` s == r ``sse`` s ``comp`` f
620+-- r `.` f ``sse`` s == r ``sse`` s `.` f
623function ::
622function ::
624 ( CountableDimensionsN a b,
623 ( CountableDimensionsN a b,
@@ -672,29 +671,29 @@ bijection r = injection r && surjection r
673-- Properties of relations
672-- Properties of relations
675−-- | A 'Relation' @r@ is 'reflexive' 'iff' @'iden' ``sse`` r@
674+-- | A 'Relation' @r@ is 'reflexive' 'iff' @'id' ``sse`` r@
676reflexive :: (CountableN a, FromListsN a a) => Relation a a -> Bool
675reflexive :: (CountableN a, FromListsN a a) => Relation a a -> Bool
677−reflexive r = iden <= r
676+reflexive r = id <= r
679−-- | A 'Relation' @r@ is 'coreflexive' 'iff' @r ``sse`` 'iden'@
678+-- | A 'Relation' @r@ is 'coreflexive' 'iff' @r ``sse`` 'id'@
680coreflexive :: (CountableN a, FromListsN a a) => Relation a a -> Bool
679coreflexive :: (CountableN a, FromListsN a a) => Relation a a -> Bool
681−coreflexive r = r <= iden
680+coreflexive r = r <= id
683−-- | A 'Relation' @r@ is 'transitive' 'iff' @(r ``comp`` r) ``sse`` r@
682+-- | A 'Relation' @r@ is 'transitive' 'iff' @(r `.` r) ``sse`` r@
684transitive :: Relation a a -> Bool
683transitive :: Relation a a -> Bool
685−transitive r = (r `comp` r) `sse` r
684+transitive r = (r . r) `sse` r
687-- | A 'Relation' @r@ is 'symmetric' 'iff' @r == 'conv' r@
686-- | A 'Relation' @r@ is 'symmetric' 'iff' @r == 'conv' r@
688symmetric :: Relation a a -> Bool
687symmetric :: Relation a a -> Bool
689symmetric r = r == conv r
688symmetric r = r == conv r
691−-- | A 'Relation' @r@ is anti-symmetric 'iff' @(r ``intersection`` 'conv' r) ``sse`` 'iden'@
690+-- | A 'Relation' @r@ is anti-symmetric 'iff' @(r ``intersection`` 'conv' r) ``sse`` 'id'@
692antiSymmetric :: (CountableN a, FromListsN a a) => Relation a a -> Bool
691antiSymmetric :: (CountableN a, FromListsN a a) => Relation a a -> Bool
693−antiSymmetric r = (r `intersection` conv r) `sse` iden
692+antiSymmetric r = (r `intersection` conv r) `sse` id
695−-- | A 'Relation' @r@ is 'irreflexive' 'iff' @(r ``intersection`` 'iden') == 'zeros'@
694+-- | A 'Relation' @r@ is 'irreflexive' 'iff' @(r ``intersection`` 'id') == 'zeros'@
696irreflexive :: (CountableN a, FromListsN a a) => Relation a a -> Bool
695irreflexive :: (CountableN a, FromListsN a a) => Relation a a -> Bool
697−irreflexive r = (r `intersection` iden) == zeros
696+irreflexive r = (r `intersection` id) == zeros
699-- | A 'Relation' @r@ is 'connected' 'iff' @(r ``union`` 'conv' r) == 'ones'@
698-- | A 'Relation' @r@ is 'connected' 'iff' @(r ``union`` 'conv' r) == 'ones'@
700connected :: (CountableN a, FromListsN a a) => Relation a a -> Bool
699connected :: (CountableN a, FromListsN a a) => Relation a a -> Bool
@@ -721,9 +720,9 @@ partialEquivalence :: (CountableN a, FromListsN a a) => Relation a a -> Bool
721partialEquivalence r = partialOrder r && equivalence r
720partialEquivalence r = partialOrder r && equivalence r
723-- | A 'Relation' @r@ is 'difunctional' or regular wherever
722-- | A 'Relation' @r@ is 'difunctional' or regular wherever
724−-- @r ``comp`` 'conv' r ``comp`` r == r@
723+-- @r `.` 'conv' r `.` r == r@
725difunctional :: Relation a b -> Bool
724difunctional :: Relation a b -> Bool
726−difunctional r = r `comp` conv r `comp` r == r
725+difunctional r = r . conv r . r == r
728-- Relational pairing
727-- Relational pairing
@@ -732,17 +731,17 @@ difunctional r = r `comp` conv r `comp` r == r
732-- NOTE: That this is not a true categorical product, see for instance:
731-- NOTE: That this is not a true categorical product, see for instance:
735−-- | 'fstR' ``comp`` 'splitR' a b ``sse`` a
734+-- | 'fstR' `.` 'splitR' a b ``sse`` a
736-- 'splitR' a b <=> |
735-- 'splitR' a b <=> |
737−-- | 'sndR' ``comp`` 'splitR' a b ``sse`` b
736+-- | 'sndR' `.` 'splitR' a b ``sse`` b
740-- __Emphasis__ on the 'sse'.
739-- __Emphasis__ on the 'sse'.
743−-- 'splitR' r s ``comp`` f == 'splitR' (r ``comp`` f) (s ``comp`` f)
742+-- 'splitR' r s `.` f == 'splitR' (r `.` f) (s `.` f)
744−-- (R '><' S) ``comp`` 'splitR' p q == 'splitR' (r ``comp`` p) (s ``comp`` q)
743+-- (R '><' S) `.` 'splitR' p q == 'splitR' (r `.` p) (s `.` q)
745−-- 'conv' ('splitR' r s) ``comp`` 'splitR' x y == ('conv' r ``comp`` x) ``intersection`` ('conv' s ``comp`` y)
744+-- 'conv' ('splitR' r s) `.` 'splitR' x y == ('conv' r `.` x) ``intersection`` ('conv' s `.` y)
@@ -761,7 +760,7 @@ splitR (R f) (R g) = R (I.kr f g)
761-- | Relational pairing first component projection
760-- | Relational pairing first component projection
764−-- 'fstR' ``comp`` 'splitR' r s ``sse`` r
763+-- 'fstR' `.` 'splitR' r s ``sse`` r
767 forall a b .
766 forall a b .
@@ -776,7 +775,7 @@ fstR = R (I.fstM @Boolean @(I.Normalize a) @(I.Normalize b))
776-- | Relational pairing second component projection
775-- | Relational pairing second component projection
779−-- 'sndR' ``comp`` 'splitR' r s ``sse`` s
778+-- 'sndR' `.` 'splitR' r s ``sse`` s
782 forall a b .
781 forall a b .
@@ -794,8 +793,8 @@ infixl 4 ><
794-- | Relational pairing functor
793-- | Relational pairing functor
797−-- r '><' s == 'splitR' (r ``comp`` fstR) (s ``comp`` sndR)
796+-- r '><' s == 'splitR' (r `.` fstR) (s `.` sndR)
798−-- (r '><' s) ``comp`` (p '><' q) == (r ``comp`` p) '><' (s ``comp`` q)
797+-- (r '><' s) `.` (p '><' q) == (r `.` p) '><' (s `.` q)
801 ( CountableDimensionsN a b,
800 ( CountableDimensionsN a b,
@@ -816,13 +815,13 @@ infixl 4 ><
816-- | Relational coproduct.
815-- | Relational coproduct.
819−-- | 'eitherR' a b ``comp`` 'i1' == a
818+-- | 'eitherR' a b `.` 'i1' == a
820-- 'eitherR' a b <=> |
819-- 'eitherR' a b <=> |
821−-- | 'eitherR' a b ``comp`` 'i2' == b
820+-- | 'eitherR' a b `.` 'i2' == b
825−-- 'eitherR' r s ``comp`` 'conv' ('eitherR' t u) == (r ``comp`` 'conv' t) ``union`` (s ``comp`` 'conv' u)
824+-- 'eitherR' r s `.` 'conv' ('eitherR' t u) == (r `.` 'conv' t) ``union`` (s `.` 'conv' u)
@@ -834,8 +833,8 @@ eitherR = join
834-- | Relational coproduct first component injection
833-- | Relational coproduct first component injection
837−-- 'img' 'i1' ``union`` 'img' 'i2' == 'iden'
836+-- 'img' 'i1' ``union`` 'img' 'i2' == 'id'
838−-- 'i1' ``comp`` 'i2' = 'zeros'
837+-- 'i1' `.` 'i2' = 'zeros'
841 ( CountableDimensionsN a b,
840 ( CountableDimensionsN a b,
@@ -848,8 +847,8 @@ i1 = R I.i1
848-- | Relational coproduct second component injection
847-- | Relational coproduct second component injection
851−-- 'img' 'i1' ``union`` 'img' 'i2' == 'iden'
850+-- 'img' 'i1' ``union`` 'img' 'i2' == 'id'
852−-- 'i1' ``comp`` 'i2' = 'zeros'
851+-- 'i1' `.` 'i2' = 'zeros'
855 ( CountableDimensionsN a b,
854 ( CountableDimensionsN a b,
@@ -864,7 +863,7 @@ infixl 5 -|-
864-- | Relational coproduct functor.
863-- | Relational coproduct functor.
867−-- r '-|-' s == 'eitherR' ('i1' ``comp`` r) ('i2' ``comp`` s)
866+-- r '-|-' s == 'eitherR' ('i1' `.` r) ('i2' `.` s)
870 ( CountableDimensionsN b d,
869 ( CountableDimensionsN b d,
@@ -897,7 +896,7 @@ trans ::
897 TrivialP c b
896 TrivialP c b
899 => Relation (a, b) c -> Relation a (c, b)
898 => Relation (a, b) c -> Relation a (c, b)
900−trans r = splitR r sndR `comp` conv fstR
899+trans r = splitR r sndR . conv fstR
902-- | Relational 'untrans'
901-- | Relational 'untrans'
@@ -918,17 +917,17 @@ untrans ::
918 TrivialP c b
917 TrivialP c b
920 => Relation a (c, b) -> Relation (a, b) c
919 => Relation a (c, b) -> Relation (a, b) c
921−untrans s = fstR `comp` conv (splitR (conv s) sndR)
920+untrans s = fstR . conv (splitR (conv s) sndR)
923-- | Transforms predicate @p@ into a correflexive relation.
922-- | Transforms predicate @p@ into a correflexive relation.
926−-- 'predR' ('const' True) == 'iden'
925+-- 'predR' ('const' True) == 'id'
927-- 'predR' ('const' False) == 'zeros'
926-- 'predR' ('const' False) == 'zeros'
931−-- 'predR' q ``comp`` 'predR' p == 'predR' q ``union`` 'predR' p
930+-- 'predR' q `.` 'predR' p == 'predR' q ``union`` 'predR' p
934 ( Bounded a,
933 ( Bounded a,
@@ -938,14 +937,14 @@ predR ::
938 FromListsN Bool a
937 FromListsN Bool a
940 => Relation a Bool -> Relation a a
939 => Relation a Bool -> Relation a a
941−predR p = iden `intersection` divisionF (fromF (const True)) p
940+predR p = id `intersection` divisionF (fromF (const True)) p
943-- | Equalizes functions @f@ and @g@.
942-- | Equalizes functions @f@ and @g@.
944-- That is, @'equalizer' f g@ is the largest coreflexive
943-- That is, @'equalizer' f g@ is the largest coreflexive
945-- that restricts @g@ so that @f@ and @g@ yield the same outputs.
944-- that restricts @g@ so that @f@ and @g@ yield the same outputs.
948−-- 'equalizer' r r == 'iden'
947+-- 'equalizer' r r == 'id'
949-- 'equalizer' ('point' True) ('point' False) == 'zeros'
948-- 'equalizer' ('point' True) ('point' False) == 'zeros'
951equalizer ::
950equalizer ::
@@ -953,12 +952,12 @@ equalizer ::
953 FromListsN a a
952 FromListsN a a
955 => Relation a b -> Relation a b -> Relation a a
954 => Relation a b -> Relation a b -> Relation a a
956−equalizer f g = iden `intersection` divisionF f g
955+equalizer f g = id `intersection` divisionF f g
958-- | Relational conditional guard.
957-- | Relational conditional guard.
961−-- 'guard' p = 'i2' ``overriddenBy`` 'i1' ``comp`` 'predR' p
960+-- 'guard' p = 'i2' ``overriddenBy`` 'i1' `.` 'predR' p
964 ( Bounded b,
963 ( Bounded b,
@@ -978,27 +977,27 @@ cond ::
978 FromListsN Bool b
977 FromListsN Bool b
980 => Relation b Bool -> Relation b c -> Relation b c -> Relation b c
979 => Relation b Bool -> Relation b c -> Relation b c -> Relation b c
981−cond p r s = eitherR r s `comp` guard p
980+cond p r s = eitherR r s . guard p
983-- | Relational domain.
982-- | Relational domain.
985-- For injective relations, 'domain' and 'ker'nel coincide,
984-- For injective relations, 'domain' and 'ker'nel coincide,
986−-- since @'ker' r ``sse`` 'iden'@ in such situations.
985+-- since @'ker' r ``sse`` 'id'@ in such situations.
987domain ::
986domain ::
988 ( CountableN a,
987 ( CountableN a,
989 FromListsN a a
988 FromListsN a a
990 ) => Relation a b -> Relation a a
989 ) => Relation a b -> Relation a a
991−domain r = ker r `intersection` iden
990+domain r = ker r `intersection` id
993-- | Relational range.
992-- | Relational range.
995-- For functions, 'range' and 'img' (image) coincide,
994-- For functions, 'range' and 'img' (image) coincide,
996−-- since @'img' f ``sse`` 'iden'@ for any @f@.
995+-- since @'img' f ``sse`` id@ for any @f@.
998 ( CountableN b,
997 ( CountableN b,
999 FromListsN b b
998 FromListsN b b
1000 ) => Relation a b -> Relation b b
999 ) => Relation a b -> Relation b b
1001−range r = img r `intersection` iden
1000+range r = img r `intersection` id
1003-- Relation pretty print
1002-- Relation pretty print