0% found this document useful (0 votes)
8 views5 pages

Lec2 Popl 2025

The lecture covers the foundations of functional programming, emphasizing the concept of functions as transformations of inputs to outputs without hidden states. Key topics include function abstraction, partial application, currying, and higher-order functions, which allow functions to accept other functions as arguments or return them as results. The syntax and constructs of pure lambda calculus are also introduced, highlighting the significance of variables, function creation, and application.

Uploaded by

thisisfakemail99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Lec2 Popl 2025

The lecture covers the foundations of functional programming, emphasizing the concept of functions as transformations of inputs to outputs without hidden states. Key topics include function abstraction, partial application, currying, and higher-order functions, which allow functions to accept other functions as arguments or return them as results. The syntax and constructs of pure lambda calculus are also introduced, highlighting the significance of variables, function creation, and application.

Uploaded by

thisisfakemail99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lecture 2 24.1.

2025

Today’s agenda:

Foundations of functional programming:

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

Fig 2 (x,y) means the two inputs are fed together

In the foll., the terms input, arguments, and parameters have the same meaning. Output and result have
the same meaning.

anonymous form of a function

--function name is irrelevant (anonymous form) f(x,y)  g(x,y)

--names of arguments of a function are irrelevant f(x,y)  f(z1,z2)

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.

The sum function (which is uncurried) after applying Currying is given as

x y x+y
sum-x sum-y

Fig 5 x. y. x + 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:

int sum (int x, int y) this code corresponds to fig 2


{
return x + y;

Let us just specify what this function is doing. So we get

sum x y = x+y

Can be rewritten as: f x y = x + y or fuv = u + v

In anonymous mode we can write this as:

(x y) → x + y // function of 2-argument (1)

Now we need to understand how currying works: partial application

Can be rewritten as:

x → (y → x + y ) // function of 1-argument (2)

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.

such a function (2) is an example of a higher-order function (HOF).

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:

Now if we supply 3 for the argument x , [in (2)] x → (y → x + y )

We get y→ 3+y

Similarly if we supply 4 for the argument 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.

We will study the design of a generic Functional PL called Lambda Calculus.

LC has just three constructs (these are also integral to any PL):

--1. variables
--2. function creation (definition)
--3. Function application

Eg, int sum(int x, int y)


{
return x + y;
}

main(){ ….. sum(5,3);….}

Recall that procedures can be expressed in anonymous mode.

So we can write the above procedure as: x. y. x + y [fig 2]

Example of another function: x. x + 1 [fig 1]

To be read as:

 denotes function abstraction (definition)

x means a function of one argument (x)—variable-(name)

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.

This is done as: (x. x*x ) 5

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.

similarly for the other example we have

(( x. y. x*x + y*y ) 3) 4

= (y. 3*3 + y*y) 4

= 3*3 + 4*4

Now x. y. x*x + y*y is the same as y. x. x*x + y*y

Let us work out the above

((y. x. x*x + y*y) 4) 3

= (x. x*x + 4*4) 3 = 3*3 + 4*4

More examples: what will these functions return?

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.
==

So we have only three constructs:

--variables

--function abstraction

--function application [highest precedence]

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

| (M N) application (M1 M2)

instead of (M N) we can write (M1 M2).

Syntactic Convention:

f g h is equiv to (f g) h since function application is left associative.

𝜆 𝑥. 𝑀 𝑁 is equivalent to 𝜆𝑥. (𝑀 𝑁)

𝜆𝑥. 𝜆𝑦. 𝜆𝑧. 𝑀 is equivalent to 𝜆 𝑥𝑦𝑧. 𝑀

How is a function application done?

(λ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

By appropriate place we mean that x is not captured by another lambda term in M.

that is, x occurs free in M.

Eg, (λ x. λ x. x + 1) 1 how would this be evaluated?

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy