4 ms·
Profunctors are another great example of variance in functional programming. A profunctor is a type constructor that takes two arguments, one contravariant, one
by evincarofautumn 8y ago
Profunctors are another great example of variance in functional programming. A profunctor is a type constructor that takes two arguments, one contravariant, one covariant.
The simplest example of this is the function type constructor (->): you can lmap (contramap) the first type parameter by composing a function before the input, or rmap (fmap) the second type parameter by composing a function after the result:
pipe :: A -> B
lmap (modifyInput :: A' -> A ) pipe :: A' -> B
rmap (modifyOutput :: B -> B') pipe :: A -> B'
Profunctors are the class of types that behave like functions or “pipes”—anything that takes input of some type and produces output of another type, which you want to be able to stitch together in a pipeline. In fact I think the only way to have a contravariant type parameter in Haskell is to use it on the left of a function arrow, or not use it at all; all Profunctor instances are wrappers for something function-like.