После определения отношения Set (a, b) я определил экземпляры полугрупповых и категорий на основе одной и той же функции композиции.
Удивительно, но для отношения [(a, b)] это было не так возможно.
Почему?
Поиск Control.Category.Constracted с Hoogle не дает положительного результата. Прямая ссылка на библиотеку:
http://hackage.haskell.org/package/constrained-categories-0.3.1.1
{-# LANGUAGE GADTs, TypeFamilies, ConstraintKinds #-}
module Question2 where
import Data.Semigroupoid
import Control.Category.Constrained
import Data.List as L
import Data.Map as M
import Data.Set as S
-- ============================== Set (a,b) ==============================
data RelationSP a b where
IdRSP :: RelationSP a a
RSP :: (Ord a, Ord b) => Set (a,b) -> RelationSP a b
compRSP :: RelationSP b c-> RelationSP a b -> RelationSP a c
RSP r1 `compRSP` RSP r2 = RSP $
S.foldr (\(x,y) acc -> S.union acc (S.map (\(_,b) -> (x,b)) $ S.filter (\(w,z) -> y == w) r1)
) S.empty r2
instance Semigroupoid RelationSP where
o = compRSP
instance Category RelationSP where
type Object RelationSP o = Ord o
id = IdRSP
(.) = compRSP
-- ============================== [(a,b)] ==============================
data RelationLP a b where
IdLP :: RelationLP a a
RLP :: (Eq a, Eq b) => [(a,b)] -> RelationLP a b
-- RLP :: [(a,b)] -> RelationLP a b -- Possible, if you do not define the Semigroupoid instance.
compRLP :: (Eq a1, Eq b, Eq a2) =>
RelationLP a2 b -> RelationLP a1 a2 -> RelationLP a1 b
RLP r2 `compRLP` RLP r1 = RLP $ L.nub [(a, b) | (t, b) <- r2, (a, t') <- r1, t == t']
instance Semigroupoid RelationLP where
o = compRLP
{- ERROR:
No instance for (Eq i) arising from a use of ‘compRLP’
Possible fix:
add (Eq i) to the context of
the type signature for:
o :: forall j k1 i.
RelationLP j k1 -> RelationLP i j -> RelationLP i k1 -}
instance Category RelationLP where
type Object RelationLP o = Eq o
id = IdLP
(.) = compRLP