Lambda calculus
Originally posted on dev.to/gillchristian.
I figured for some of the days of #100DaysOfFP it makes more sense to write something longer than a tweet. So every now and then I'll post in dev.to instead of only a tweet. ðŸ˜„
This week the Delft Haskell Study Group started again. I attended last year but didn't put in as much focus as I wanted, so I decided to give it another shot this year.
The goal of the study group is to read some chapter/s of Haskell Programming from First Principles and then go through the exercices together.
My goal for these series is to share a few things from the chapter. More than a blogpost this is a collection of notes that could serve as a recap of the chapter.
Back to the roots
Even the greatest mathematicians, the ones that we would put into our mythology of great mathematicians, had to do a great deal of leg work in order to get to the solution in the end.
â€• Daniel Tammett
The first chapter of the book doesn't go directly into Haskell and programming but instead introduces us to Lambda calculus. Why? Because it's the foundation on top of what Functional Programming, and thus Haskell, is based.
Lambda calculus is "a model of computation devised in the 1930s by Alonzo Church. [...] Functional programming languages are all based on the lambda calculus" ^{1}.
Functional programming: "a computer programming paradigm that relies on functions modeled on mathematical functions [...] programs are a combination of expressions".
Expressions: "include concrete values, variables, and also functions".
Functions: are "expressions that are applied to an argument or input, and once applied, can be reduced or evaluated". And they define a "relation between a set of possible inputs and a set of possible outputs".
Lambda expressions
In lambda calculus we have: expressions, variables and abstractions. Where expressions are a superset as they can be variables, abstractions or a combination of them.
Variables: names for potential inputs of functions.
Abstractions: functions. A lambda term consisting of a head (the lamda) and a body, and it's applied to an argument.
Argument: a value.
Application: applying a lambda function to an argument.
Î»x.x
Let's break that down ^{2}:
Î» x . x
^â”€â”¬â”€^
â””â”€â”€â”€â”€â”€â”€ extent of the head of the lambda.
Î» x . x
^â”€â”€â”€â”€â”€â”€ the single parameter of the
function. This binds any
variables with the same name
in the body of the function.
Î» x . x
^â”€â”€ body, the expression the lambda
returns when applied. This is a
bound variable.
Functions in lambda calculus are anonymous. But we still call them abstractions as they generalize a problem and it's resolution.
Alpha equivalence:
Î»x.x
Î»d.d
Î»z.z
Beta reduction
"When we apply a function to an argument, we substitute the input expression for all instances of bound variables within the body of the abstraction. You also eliminate the head of the abstraction, since its only purpose was to bind a variable." ^{3}
(Î»x.x) 2
[x âˆ¶= 2]
2
The argument can be anything, even other lamda:
(Î»x.x)(Î»y.y)
[x âˆ¶= (Î»y.y)]
Î»y.y
Applications in the lambda calculus are left associative:
(Î»x.x)(Î»y.y)z
[x := (Î»y.y)]
(Î»y.y)z
[y := z]
z
Some variables might be free, meaning they aren't bound in the head:
Î»x.xy
If we apply such lambda:
(Î»x.xy)z
[x := z]
zy
Multiple arguments
"Each lambda can only bind one parameter and can only accept one argument. Functions that require multiple arguments have multiple, nested heads. When you apply it once and eliminate the first (leftmost) head, the next one is applied and so on" ^{4}. This is called currying.
Î»xy.xy === Î»x.(Î»y.xy)
Beta normal form: when you cannot beta reduce the terms any further.
Î»x.x  beta reduced, no arguments to apply
(Î»x.x)z  not beta reduced, `z` hasn't been applied
Combinator: lambda term with no free variables.
Î»xy.x  Combinator
Î»xyz.xz(yz)  Combinator
Î»y.x  Not combinator
Î»x.xz  Not combinator
Divergence: lambdas that never reduce to a normal form are said to diverge.
(Î»x.xx)(Î»x.xx)
[x := (Î»x.xx)]
(Î»x.xx)(Î»x.xx)
[x := (Î»x.xx)]
(Î»x.xx)(Î»x.xx)
[x := (Î»x.xx)]
(Î»x.xx)(Î»x.xx)
...
In programming, terms that diverge don't produce an actual answer, at least not a meaningful one.
But why?
After reading this chapter the first time it was not clear to me how all of this applied to Haskell, or Functional Programming in general.
Turns out that the process of reducing the expressions (beta reduction) is how Haskell code evaluates.
The following lambda expression that reduces to z
(Î»abc.cba)yz(Î»wv.w)
[a := y]
(Î»bc.cby)z(Î»wv.w)
[b := z]
(Î»c.czy)(Î»wv.w)
[c := (Î»wv.w)]
(Î»wv.w)zy
[w := z]
(Î»v.z)y
[v := y]
z
It's the equivalent of this in Haskell:
 y = 1
 z = 2
(\a b c > c b a) 1 2 (\w v > w)
(\b c > c b 1) 2 (\w v > w)
(\c > c 2 1) (\w v > w)
(\w v > w) 2 1
(\v > 2) 1
2
If we run it in GHCi we get the same result:
Î»> (\a b c > c b a) 1 2 (\w v > w)
2

Haskell Programming from First Principles, page 10. ↩