Haskell: (.) . (.) Explained

Let’s Get a Grasp of Composition Equivalences in Haskell.

Michal Ševčík
2 min readJan 10, 2021

I’m a JavaScript developer by day and a Haskell fan by night. And just recently, I stumbled upon (.) . (.) in a Haskell. At first, I had no idea whatsoever what this is.

If you’re facing the same problem, you’ve come to the right place. Let’s see what it is and how do we get there.

The dot (.) operator

In a Haskell, the . operator is a function composition operator. It is defined as follows:

(.) :: (b -> c) -> (a -> b) -> a -> c
(.) f g = \x -> f (g x)

Most people use the operator as an infix operator, so (.) f g is just the same as f . g.

(.) . (.) is f(g x y)

Function composition plays well when functions take a single argument. But not so well when the inner function takes more than one argument. And that’s exactly what (.) . (.) is for.

It’s just a point-free version of f g x y = f(g x y). But how do we get from f g x y = f(g x y) to (.) . (.)?

First, let’s establish some ground rules.

Composition Equivalences

Rule #1

f . g = f(g)

Rule #2

Known also as left section.

(f . ) g = f . g

Rule #3

Known also as right section.

( . g) f = f . g

Rule #4

The prefix notation vs. the infix notation.

(.) f g = f . g

Rule #5

Factoring out a parameter.

(f . (g x)) = ((f . ) . g) x

Eta conversion

Besides the five rules, we will also need to know about eta conversion.

f x = abs x is the same as f = abs.

Going from the first to the second is eta reduction. Going from the second to the first would be eta abstraction.

Going from “f(g x y)” to “(.) . (.)”

OK, so let’s apply what we’ve learned so far.

f g x y = f(g x y)
f g x y = (f . (g x)) y // factor out parameter
f g x = (f . (g x)) // eta reduction
f g x = ((f .) . g)) x // factor out parameter
f g = ((f .) . g)) // eta reduction
f g = (.) (f .) g // infix notation to prefix notation
f = (.) (f .) // eta reduction
f = (.) ((.) f) // infix notation to prefix notation
f = (.) . (.) f // factor out parameter
(.) . (.) // eta reduction

And that’s all there’s to it, really. I hope this helps you shed some light on the matter.

Resources

--

--