Profunctors are bifunctors that are contravariant in their first type argument and covariant in their second one. Make sure that you understand contravariance first. Then we just need to talk about bifunctors, and finally we will get to profunctors.
Bifunctor, which is available in
Data.Bifunctor is a lot like
Functor. It offers a nice solution for those times when you don’t want to ignore the leftmost type argument of a binary type constructor, such as
(,). Its core operation,
bimap, closely resembles
fmap, except it lifts two functions into the new context, allowing you to apply one or both.
Ye gods that’s a lot of variables! Let’s clean that up a bit. We’ll be talking about the
Either and tuples so let’s go ahead and see what those look like:Each
@ symbol in these examples is a visible type application, using the
TypeApplications GHC language extension.
@Either :: (a0 -> z0) bimap -> (a1 -> z1) -> Either a0 a1 -> Either z0 z1
@(,) :: (a0 -> z0) bimap -> (a1 -> z1) -> (a0, a1) -> (z0, z1)
bimap takes two unary functions as arguments along with a value, such as
(1, 3) or
Left 5, and applies whichever function it can – both if it can! We’ll partially apply
bimap here so that we can reuse it:
p is going to be something like
(,), taking two type arguments, although in this case we already know both those type arguments have to be strings. Let’s try it out.
λ> greet (Left "Julie") Left "hello Julie" λ> greet (Right "March") Right "goodbye March"
We can use the function on
Either values, even though only one “side” is present at the value level at a time. We can also use it on a two-tuple and use both functions at once:
λ> greet ("Julie", "to all that") ("hello Julie","goodbye to all that")
fmap but for binary type constructors where you want the ability to lift two functions at once.
Profunctors are bifunctors that are contravariant in the first argument and covariant in the second one. While people do incredibly magical looking things with profunctors, if you’ve understood
bimap, then you’re ready for
di as in “dioxide”;
bi was already in use for
bimap, so we had to switch from Latin to Greek.)
||“bi” as in bicycle||“2” in Latin|
||“di” as in dioxide||“2” in Greek|
The core operation of the
Profunctor class is
dimap – get ready for some type variable soup.
We can start by looking at where it differs from the
Bifunctor definition: We have renamed all of the type variables for the moment, just to highlight this particular comparison.
bimap :: (a -> b) -> (c -> d) -> f a c -> f b d -- ^^^^^^ dimap :: (b -> a) -> (c -> d) -> f a c -> f b d -- ^^^^^^
Earlier we saw what
bimap looks like with
Either and tuple types, but we cannot also implement
dimap for these types. That’s because of the contravariance in the first argument; as we saw with
Contravariant, basically everything in Haskell that are contravariant functors are function types.
Informally, what we’re going to have is a bifunctor that acts like
fmap on the
f a z and like
contramap on the
f a z. It’s worth pointing out here that the
Profunctor class also has methods called
rmap (for left and right map, respectively), and their implementations for the function type are
flip (.) and
(.). There’s a lot of function composition going on under the hood here.
Since we’ve been talking about about functors of functions, we’re going to continue to do so as that is the simplest profunctor example we could start with.
dimap f g does is take
h and squeeze it in between
= g . h . fdimap f g h
Or, to put it another way,
dimap f g starts with
f a0 z0 and
- Extends it on the input side by applying
f :: a1 -> a0to change the “argument” type variable from
a1(this is the contravariant part).
- Extends it on the output side by applying
g :: z0 -> z1to change the “result” type variable from
z1(this is the covariant part).
Thus giving a result of
f a1 z1.
Words and phrases
We’ll take a contrived but uncomplicated example and step very carefully through the flow of types. We will annotate the types as best we can to try to make it more clear.
dimap, since we’re working with the function profunctor, takes three functions as input. They are:
words :: Phrase -> [Word] unwords :: [Word] -> Phrase fmap capWord :: [Word] -> [Word]
dimap :: (a1 -> a0) -- f - words :: Phrase -> [Word] -> (z0 -> z1) -- g - unwords :: [Word] -> Phrase -> p a0 z0 -- h - fmap capWord :: [Word] -> [Word] -> p a1 z1 -- Phrase -> Phrase
a1gets passed to the
wordsfunction first, converting it to type
- Where does the
a0go? To function
h, the next in our series of composed functions, which in this case is
fmap capWord, producing a
z0output, which is still, for this example, a
z0output gets handed off to
unwordsnext, the last link in the chain of composition, and becomes
z1, which is a
λ> capitalize "Julie loves DONUTS" "Julie Loves Donuts"
You can pull out the
words . unwords not-quite-isomorphism by partially applying
dimap, in case you have other ways in which you’d like to alter phrases as lists of words:
λ> capPhrase "one two three" "One Two Three" λ> takeTwoWords "one two three" "one two" it :: Phrase