Home

From Category Theory for Programmers - Series 1

Category Theory 7.1 - Functoriality, bifunctors

tail :: [a] -> [a]
-- | but tail doesn't check if the list is empty
safeTail :: [a] -> Maybe [a]
-- | definition for empty
safeTail  [ ] = Nothing
-- | with a list
safeTail (x * xs) = Just xs
-- | list of maybe ints
mis :: Maybe [int]
-- | square function
sq :: Int -> Int
-- | fmap is then
fmap (fmap sq) mis
class Bifunctor f where
-- | needs to lift two functions at the same time
-- | corresponds to fmap
bimap :: (a -> a`) -> (b -> b`) -> (f a b -> f a` b`)

(part 2 Category Theory 7.2 - Monoidal Categories, Functoriality of ADTs, Profunctors)

-- | map any type a to type c
data Const c a = Const c
instance Functor (Const c) where
    -- | fmap :: (a -> b) Const c a -> Const c b
    fmap f (Const c) = Const c
data Identity a = Identity a
    fmap f (Identity a) = Identity (f a)
 
-- | can be used to define any type constructor
data Maybe a = Nothing | Just a
    Either () (Identity a)
    -- | same as
    Either Const () a (Identity a)

 
class Contravariant f where
    contramap :: (b -> a) -> (f a -> f b)
class Profunctor p where
    dimap :: (a` -> a) -> (b -> b`) -> p a b -> p a` b`