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
Bifunctor
, which is available in base
, 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 Either
or (,)
. 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 bimap
for 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:
That p
is going to be something like Either
or (,)
, 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")
So, bimap
is fmap
but for binary type constructors where you want the ability to lift two functions at once.
Profunctor
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 fmap
, contramap
, and bimap
, then you’re ready for dimap
.
(That’s di
as in “dioxide”; bi
was already in use for bimap
, so we had to switch from Latin to Greek.)
Bifunctor |
bimap |
“bi” as in bicycle | “2” in Latin |
Profunctor |
dimap |
“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 z
of f a z
and like contramap
on the a
of f a z
. It’s worth pointing out here that the Profunctor
class also has methods called lmap
and 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.
The (->)
profunctor
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.
What dimap f g
does is take h
and squeeze it in between f
and g
.
= g . h . f dimap 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 -> a0
to change the “argument” type variable froma0
toa1
(this is the contravariant part). - Extends it on the output side by applying
g :: z0 -> z1
to change the “result” type variable fromz0
toz1
(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.
This 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
- The
Phrase
argumenta1
gets passed to thewords
function first, converting it to typea0
,[Word]
. - Where does the
a0
go? To functionh
, the next in our series of composed functions, which in this case isfmap capWord
, producing az0
output, which is still, for this example, a[Word]
. - That
z0
output gets handed off tounwords
next, the last link in the chain of composition, and becomesz1
, which is aPhrase
again.
λ> 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