Haskell Language Functor Class Definition of Functor and Laws


Example

class Functor f where
    fmap :: (a -> b) -> f a -> f b

One way of looking at it is that fmap lifts a function of values into a function of values in a context f.

A correct instance of Functor should satisfy the functor laws, though these are not enforced by the compiler:

fmap id = id                    -- identity
fmap f . fmap g = fmap (f . g)  -- composition

There's a commonly-used infix alias for fmap called <$>.

infixl 4 <$>
(<$>) :: Functor f => (a -> b) -> f a -> f b
(<$>) = fmap