I'm trying to perform function composition in Haskell, and I'm not sure which operator is the correct one to use.
The docs contain these two type signatures:
(.) :: (b -> c) -> (a -> b) -> a -> c
(<<<) :: Category cat => cat b c -> cat a b -> cat a c
Clearly the difference between these two options is the presence/absence of Category cat, but what does this annotation signify, and how should I use the information to choose one operator over the other?
I also noticed a third variant on the above two signatures when comparing two other operators:
(>>) :: forall a b. m a -> m b -> m b
(>>>) :: Category cat => cat a b -> cat b c -> cat a c
What does the forall annotation mean—is >> for use in a third scenario?
First, you should recognize that
(.)is defined in the Prelude in a function-specific formas well as a more generic function provided by the
Categoryclass:The
(->)instance ofCategorymakes clear that the two are the same for functions:The definition of
(<<<)makes clear that it is just a synonym of(.)intended for symmetry with the
(>>>)operator. You can write eitherf >>> gorg <<< f, whichever makes more sense for your particular use.(>>)is an entirely different operator (at least, as long as you aren't delving too much into the theory of monads).(>>)is a version of(>>=)that ignores the result of the first operand, using it only for its effect.