DEV Community

Brian Berns
Brian Berns

Posted on • Edited on

2

Profunctors

I'm an F# developer reading a book called A Type of Programming, which is about Haskell and type theory. One of the concepts discussed is profunctors. The definition of a profunctor looks kind of scary:

class Profunctor f where
  dimap :: (a -> c) -> (b -> d) -> f c b -> f a d
Enter fullscreen mode Exit fullscreen mode

What does that mean? It turns out that it's just an abstraction for preprocessing input before it reaches a function and then postprocessing the output of the function. a -> c is the preprocessor, b -> d is the postprocessor, f c b is the function between them, and f a d is the result of attaching the three pieces together in the right order.

One point of confusion is that f c b and f a d are technically profunctors instead of functions, but pretty much every profunctor is just a function at its core, so you can think of the signature as:

-- profunctor instance for raw functions
dimap :: (a -> c) -> (b -> d) -> (c -> b) -> (a -> d)
Enter fullscreen mode Exit fullscreen mode

This says that if you know how to map a's to c's, and c's to b's, and b's to d's, then obviously you can map a's all the way to d's in one fell swoop. That's all there is to profunctors.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay