Lec2 Popl 2025
Lec2 Popl 2025
2025
Today’s agenda:
View of a function: a function transforms an input to an output. It just says what it does and not how
it does.
Example 1:
x x+1 f: x → x + 1 (mapping)
increment
x. x + 1
Fig 1 blackbox
we are not allowed to see what happens inside the blackbox. there is no hidden state.
Example 2:
x
sum
y x+ y f: (x,y) → x+y
In the foll., the terms input, arguments, and parameters have the same meaning. Output and result have
the same meaning.
Partial application of a function: What happens when sum is applied to one argument, say, 2?
2
sum
Fig 3
After 2 is consumed, there is still one input arrow to the function. Pictorially, we have
sum2
y 2+y
Fig 4
Page 1 of 5
Currying: is a technique for transforming a function of n arguments to a chain of n functions where each
function has exactly one argument. It uses the concept of partial application of a function. i.e., it applies
one argument at a time.
x y x+y
sum-x sum-y
We will write f(x,y) as f x y as in sin [to be read as application of sine to theta] or log x
Example:
sum x y = x+y
this is a function of 1-argument with argument x and it returns another function [shown in red] In this
way, any function of more than 2 arguments can be transformed to several functions of
1-argument each.
Functions which have other functions as arguments or results are called HOFs.
See carefully the difference between (1) and (2). In (2) the body of the function is also a function of 1-
argument. In (1) or in the code, we must supply the actual parameters of both the arguments together.
Page 2 of 5
Let us now see how to use such functions:
We get y→ 3+y
We get 3+4
A special feature of FP is partial application of a function, which is not allowed in Imperative PLs.
Thus functions are first-class citizens in FP (LC). They can be assigned to variables, passed as parameters
to functions, can also be returned by a function. A function in FP is a HOF, in general.
LC has just three constructs (these are also integral to any PL):
--1. variables
--2. function creation (definition)
--3. Function application
To be read as:
after the dot the body of the function is written—what the function is supposed to do.
the previous example can now be read as a function of two variables (x,y) and the body is x + y
Page 3 of 5
Now we want to work with the functions; pass values to the variables or names or placeholders. This is
called function application.
the above says, apply the function where the placeholder takes the value 5.
this means that we evaluate the function with x=5 so we get 5*5 = 25.
= 3*3 + 4*4
Now x. y. x*x + y*y is the same as y. x. x*x + y*y
1. x. x (x. x) n
2. x. y (x. y) n
3. x. y. x ((x. y. x) n) m)
Summary of key concepts: HOF, partial application of a function, function abstraction, function
application.
==
--variables
--function abstraction
Pure Lambda calculus: there are no constant symbols (0,1,2,....), there are no operators like +, *,..
This means that x. x*x x. x + 1 are not valid terms of pure LC.
But there exist pure lambda terms for 0,1,2,..... and +, *, ...., We shall develop these terms later.
Page 4 of 5
Syntax of Pure Lambda calculus:
M ::= x variables
| (λx. M) abstraction
Syntactic Convention:
𝜆 𝑥. 𝑀 𝑁 is equivalent to 𝜆𝑥. (𝑀 𝑁)
(λx. 5x + 2) 2
= 5.2 + 2 = 10 + 2 = 12
(λ x. M ) N means we look for appropriate places in M for x that can be replaced or substituted by N
If x does not occur in M, the output of the above is M, eg, λx. 0 = 0 for any N
Here M = λ x. x +1
The outer x is captured by the inner λ x, this means that the outer x is not visible inside the inner λ x,
so we can rewrite it as λ x. λ y. y + 1, so M does not contain x, so the answer would be M i.e., λ y. y+1
which by replacement is λ x. x +1
End of lecture
Page 5 of 5