Haskell Language Category Theory Haskell Applicative in terms of Category Theory


Example

A Haskell's Functor allows one to map any type a (an object of Hask) to a type F a and also map a function a -> b (a morphism of Hask) to a function with type F a -> F b. This corresponds to a Category Theory definition in a sense that functor preserves basic category structure.

A monoidal category is a category that has some additional structure:

Taking a pair as our product, this definition can be translated to Haskell in the following way:

class Functor f => Monoidal f where
    mcat :: f a -> f b -> f (a,b)
    munit :: f ()

The Applicative class is equivalent to this Monoidal one and thus can be implemented in terms of it:

instance Monoidal f => Applicative f where
    pure x = fmap (const x) munit
    f <*> fa = (\(f, a) -> f a) <$> (mcat f fa)