Post · 8 min read· pp. 64–66

Function composition

Here’s some musing about functions, finishing with a cool trick which Sebastian Ullrich showed me. If you don’t know what ((∘) ∘ (∘)) means, read on!

Lambdas

The “functional programming” way of thinking about functions is not usually taught to mathematicians, but it’s easy to understand. In mathematics we sometimes write x\mapsto x^2 to denote the function sending x to x^2. Computer scientists prefer “lambda notation”, namely \lambda x, x^2, or λ x, x^2. Computer scientists will use lambda notation for multivariable functions too — whereas a mathematician might write (x,y)\mapsto x+y to denote the addition function, in lambda notation this is just λ x y, x + y.

Currying

Another trick that computer scientists seem to like is the process of “currying” functions. A mathematician might say that addition is a function \mathbb{R}^2\to\mathbb{R}, because it takes as input a pair (x,y) of real numbers and outputs one real number, namely their sum. Computer scientists like to think of this function in a slightly different way — they would say that addition was a function ℝ → (ℝ → ℝ). What they mean by this is that the function takes as input some real number x and returns a function from the reals to the reals, namely the function sending y to x+y. It’s not difficult to convince yourself that these two ways of looking at the situation are completely equivalent. You could think of that first arrow as the function sending the real number x to the function y\mapsto x+y. For example it sends the real number 3 to the function y\mapsto 3+y. Computer scientists even have a cool convention about bracketing of arrows, where they can even write ℝ → ℝ → ℝ, and this means ℝ → (ℝ → ℝ) by definition (even though it looks like a monstrosity to a mathematician).

Infix and prefix

Historically the symbols for functions like addition were written in between the numbers you were adding. This is called “infix notation”. When defining new functions, this is usually not the notation chosen. For example if I define a function f sending a pair (x,y) of real numbers to x^2+y^2 I would write f(x,y)=x^2+y^2, and here the notation f for the function goes before the x and y rather than in the middle. There’s a trick, used in Haskell and, I discovered this week, also in Lean, to change an “infix notation” function like + to a “prefix notation” function like f — you simply put brackets around the operator. So for example (+) x y just means x + y.

Now here’s a cool thing about lambda notation. Let’s go back to the function x\mapsto 3 + x, sending our input variable x to 3 + x. Using lambda notation I could just write

λ x, 3 + x

Using the infix/prefix trick, I could even write

λ x, (+) 3 x.

Writing it this way, you see that this function takes x as an input and then it returns f x, where f is the slightly weird-looking (to a mathematician) function (+) 3. So in fact we could just write this function as

(+) 3

and indeed you can see in Lean that this works:

text
def f := (+) 3 

#eval f 2

prints 5 as the result of the evaluation. Click here to check this using the Lean Web Editor.

Composition

Function composition is pretty easy to understand. If X,Y,Z are sets and we have functions f: X \to Y and g : Y \to Z, then their composition is the function from X to Z sending x to g(f(x)). There is notation for this in mathematics — the “\circ” command in LaTeX. We write h = g \circ f and then h(x) is defined to be g(f(x)). In Lean one would write

text
variables {X Y Z : Type} {f : X → Y} {g : Y → Z}

def h : X → Z := g ∘ f 

example (x : X) : h x = g (f x) := rfl 

The rfl just means “this is true by definition”, and the code compiles, meaning that indeed h x is by definition g (f x). Note that those brackets are necessary — if we wrote g f x then Lean would try to evaluate g at f and would give an error, because f is not an element of X.

We see that ∘ is another example of an infix operator, but in this case the associated prefix operator (∘) has an actual name, namely function.comp, as one can see in Lean by typing #print notation ∘ (to get a ∘ you can type \circ just like in LaTeX).

The fun part

Removing lambdas from code is kind of cool so I try to use it when I can. Any code which looks like h := λ x, blah_blah_blah x can often be shortened to just h := blah_blah_blah. But for this trick to work the x really must be the last thing written, with no brackets. For example, if I wanted to do this trick with h := λ x, g (f x) I can’t just remove the lambda and the xs, because of that close bracket — I have to use function composition notation and turn the code into h := λ x, (g ∘ f) x before I can do the trick and rewrite as h := (g ∘ f) or even h := g ∘ f.

Recently when writing some Lean code I ran into a version of this situation with more than one variable though, and pulling off the trick was harder. I had sets W X Y Z and functions f : W → X → Y (remember this just how a computer scientist writes f : W \times X \to Y) and g : Y → Z and I wanted to compose them to get a function (g \circ f) : W \times X \to Z, except I wanted the curried version W → X → Z, defined in lambda notation as λ w x, g (f w x). Because we’re using the curried version, I couldn’t use function composition! You see, g ∘ f only works if the target of f is the source of g, and here the target of f is the functions X → Y whereas the source of g is Y. How could I remove the lambda?

Sebastian Ullrich pointed out the following extremely devious trick to me though on the Lean chat — the function I want is just ((∘) ∘ (∘)) g f! And indeed he was right:

text
variables {W X Y Z : Type} {f : W → X → Y} {g : Y → Z}

def h : W → X → Z := ((∘) ∘ (∘)) g f

example (w : W) (x : X) : h w x = g (f w x) := rfl 

Try it here using the Lean Web Editor if you, like me, couldn’t initially believe that this monstrosity even made sense.

Can you prove that ((∘) ∘ (∘)) g f does what Lean says it does? If you type #check ((∘) ∘ (∘)) into Lean you’ll see Lean’s take on what it thinks of this funny construction.

Recall that (∘) A B C is the same as (A ∘ B) C, which is the same as A (B C). Recall also that Lean evaluates things from left to right, so (f g) h means the same as f g h. Now see if you can evaluate ((∘) ∘ (∘)) g f w x! Spoiler alert — I give the first move below.

A hint for the solution

Here’s the first move — ((∘) ∘ (∘)) g f w x = (∘) ((∘) g) f w x.