Explain (.)(.) to me

We can work backwards by "pattern matching" over the combinators' definitions. Given

f a b c d =  a b  (c d) 
          = (a b) (c d)

we proceed

         = B (a b) c d 
         = B B a b c d    -- writing B for (.)

so by eta-contraction

f = B B 

because

a (b c) = B a b c         -- bidirectional equation

by definition. Haskell's (.) is actually the B combinator (see BCKW combinators).


edit: Potentially, many combinators can match the same code. That's why there are many possible combinatory encodings for the same piece of code. For example, (ab)(cd) = (ab)(I(cd)) is a valid transformation, which might lead to some other combinator definition matching that. Choosing the "most appropriate" one is an art (or a search in a search space with somewhat high branching factor).

That's about going backwards, as you asked. But if you want to go "forward", personally, I like the combinatory approach much better over the lambda notation fidgeting. I would even just write many arguments right away, and get rid of the extra ones in the end:

BBabcdefg = B(ab)cdefg = (ab)(cd)efg 

hence,

BBabcd    = B(ab)cd    = (ab)(cd) 

is all there is to it.


Generally (?) (where ? stands for an arbitrary infix operator) is the same as \x y -> x ? y. So we can rewrite f as:

f = (\a b -> a . b) (\c d -> c . d)

Now if we apply the argument to the function, we get:

f = (\b -> (\c d -> c . d) . b)

Now b is just an argument to f, so we can rewrite this as:

f b = (\c d -> c . d) . b

The definition of . is f . g = \x -> f (g x). If replace the outer . with its definition, we get:

f b = \x -> (\c d -> c . d) (b x)

Again we can turn x into a regular parameter:

f b x = (\c d -> c . d) (b x)

Now let's replace the other .:

f b x = (\c d y -> c (d y)) (b x)

Now let's apply the argument:

f b x = \d y -> (b x) (d y)

Now let's move the parameters again:

f b x d y = (b x) (d y)

Done.


You can also gradually append arguments to f:

f = ((.) . )
f x = (.) . x
f x y = ((.) . x) y
      = (.) (x y)
      = ((x y) . )
f x y z = (x y) . z
f x y z t = ((x y) . z) t
          = (x y) (z t)
          = x y (z t)
          = x y $ z t

The result reveals that x and z are actually (binary and unary, respectively) functions, so I'll use different identifiers:

f g x h y = g x (h y)