You are on page 1of 100

Foundations of Computer Science

Pure Lambda Calculus


C. Aravindan
<AravindanC@ssn.edu.in>
Professor of Computer Science and
Assistant Director
SSN School of Advanced Software Engineering

13 August 2013

C. Aravindan (SSN Institutions)

FCS

13 August 2013

1 / 28

Outline
1

Introduction

C. Aravindan (SSN Institutions)

FCS

13 August 2013

2 / 28

Outline
1

Introduction

Boolean data and functional abstractions

C. Aravindan (SSN Institutions)

FCS

13 August 2013

2 / 28

Outline
1

Introduction

Boolean data and functional abstractions

Conditions

C. Aravindan (SSN Institutions)

FCS

13 August 2013

2 / 28

Outline
1

Introduction

Boolean data and functional abstractions

Conditions

Natural Numbers

C. Aravindan (SSN Institutions)

FCS

13 August 2013

2 / 28

Outline
1

Introduction

Boolean data and functional abstractions

Conditions

Natural Numbers

Fixpoints and Recursion

C. Aravindan (SSN Institutions)

FCS

13 August 2013

2 / 28

Outline
1

Introduction

Boolean data and functional abstractions

Conditions

Natural Numbers

Fixpoints and Recursion

Data abstractions

C. Aravindan (SSN Institutions)

FCS

13 August 2013

2 / 28

Outline
1

Introduction

Boolean data and functional abstractions

Conditions

Natural Numbers

Fixpoints and Recursion

Data abstractions

Functional Programming Languages

C. Aravindan (SSN Institutions)

FCS

13 August 2013

2 / 28

Outline
1

Introduction

Boolean data and functional abstractions

Conditions

Natural Numbers

Fixpoints and Recursion

Data abstractions

Functional Programming Languages

Summary

C. Aravindan (SSN Institutions)

FCS

13 August 2013

2 / 28

Lambda Calculus

We have discussed the basics of lambda abstractions and


combinations.
The syntax is extremely simple!
Semantics is given by reductions
Reductions are in general non-deterministic and hence strategies are
needed normal order and applicative order
We have briefly looked at some important theoretical results in the
form of Church-Rosser theorems and Church-Turing hypothesis.
We have also touched upon some principles of programming
languages.

C. Aravindan (SSN Institutions)

FCS

13 August 2013

3 / 28

Pure Lambda Calculus

It is possible to encode both data and functional abstractions in


lambda calculus!

C. Aravindan (SSN Institutions)

FCS

13 August 2013

4 / 28

Boolean data and functions


Consider the following two abstractions:
let T = xy  x
let F = xy  y
We will let these abstractions denote the boolean constants true
and false respectively!

C. Aravindan (SSN Institutions)

FCS

13 August 2013

5 / 28

Boolean data and functions


Consider the following two abstractions:
let T = xy  x
let F = xy  y
We will let these abstractions denote the boolean constants true
and false respectively!
What is so special about them?

C. Aravindan (SSN Institutions)

FCS

13 August 2013

5 / 28

Boolean data and functions


Consider the following two abstractions:
let T = xy  x
let F = xy  y
We will let these abstractions denote the boolean constants true
and false respectively!
What is so special about them?
Consider the following abstraction:
let ANON = xy  x y F
Now evaluate the following: (ANON F F ), (ANON F T ),
(ANON T F ), and (ANON T T ).
What do you think the ANON is actually abstracting?
C. Aravindan (SSN Institutions)

FCS

13 August 2013

5 / 28

Boolean data and functions


ANON is actually performing the logical and operation!
let AND = xy  x y F
(AND T T ) (T T F ) T
(AND F T ) (F T F ) F

C. Aravindan (SSN Institutions)

FCS

13 August 2013

6 / 28

Boolean data and functions


ANON is actually performing the logical and operation!
let AND = xy  x y F
(AND T T ) (T T F ) T
(AND F T ) (F T F ) F
Is this the only lambda expression for and function?

C. Aravindan (SSN Institutions)

FCS

13 August 2013

6 / 28

Boolean data and functions


ANON is actually performing the logical and operation!
let AND = xy  x y F
(AND T T ) (T T F ) T
(AND F T ) (F T F ) F
Is this the only lambda expression for and function?
let AAND = xy  y x y

C. Aravindan (SSN Institutions)

FCS

13 August 2013

6 / 28

Boolean data and functions


ANON is actually performing the logical and operation!
let AND = xy  x y F
(AND T T ) (T T F ) T
(AND F T ) (F T F ) F
Is this the only lambda expression for and function?
let AAND = xy  y x y
Can you think of a lambda expression for logical or operation?

C. Aravindan (SSN Institutions)

FCS

13 August 2013

6 / 28

Boolean data and functions


ANON is actually performing the logical and operation!
let AND = xy  x y F
(AND T T ) (T T F ) T
(AND F T ) (F T F ) F
Is this the only lambda expression for and function?
let AAND = xy  y x y
Can you think of a lambda expression for logical or operation?
let OR = xy  x T y

C. Aravindan (SSN Institutions)

FCS

13 August 2013

6 / 28

Boolean data and functions


ANON is actually performing the logical and operation!
let AND = xy  x y F
(AND T T ) (T T F ) T
(AND F T ) (F T F ) F
Is this the only lambda expression for and function?
let AAND = xy  y x y
Can you think of a lambda expression for logical or operation?
let OR = xy  x T y
How about logical negation?

C. Aravindan (SSN Institutions)

FCS

13 August 2013

6 / 28

Boolean data and functions


ANON is actually performing the logical and operation!
let AND = xy  x y F
(AND T T ) (T T F ) T
(AND F T ) (F T F ) F
Is this the only lambda expression for and function?
let AAND = xy  y x y
Can you think of a lambda expression for logical or operation?
let OR = xy  x T y
How about logical negation?
let NOT = x  x F T
C. Aravindan (SSN Institutions)

FCS

13 August 2013

6 / 28

Conditional evaluation
Conditional evaluation is a very fundamental programming concept
and I do not know of any programming language that does not have
some form of if-then-else!

C. Aravindan (SSN Institutions)

FCS

13 August 2013

7 / 28

Conditional evaluation
Conditional evaluation is a very fundamental programming concept
and I do not know of any programming language that does not have
some form of if-then-else!
Our abstractions of true and false make it extremely simple to
encode if-then-else in lambda calculus:
let ITE = cxy  c x y

C. Aravindan (SSN Institutions)

FCS

13 August 2013

7 / 28

Conditional evaluation
Conditional evaluation is a very fundamental programming concept
and I do not know of any programming language that does not have
some form of if-then-else!
Our abstractions of true and false make it extremely simple to
encode if-then-else in lambda calculus:
let ITE = cxy  c x y
If the expression abstracted by c evaluates to T , then
(T x y ) x

C. Aravindan (SSN Institutions)

FCS

13 August 2013

7 / 28

Conditional evaluation
Conditional evaluation is a very fundamental programming concept
and I do not know of any programming language that does not have
some form of if-then-else!
Our abstractions of true and false make it extremely simple to
encode if-then-else in lambda calculus:
let ITE = cxy  c x y
If the expression abstracted by c evaluates to T , then
(T x y ) x
Else, if the expression abstracted by c evaluates to F , then
(F x y ) y
C. Aravindan (SSN Institutions)

FCS

13 August 2013

7 / 28

Encoding Natural Numbers


If f is a lambda abstraction and x is a lambda expression, then the
Natural numbers including zero (technically, positive integers) can be
encoded as x , f (x ), f (f (x )), , f n (x ),

C. Aravindan (SSN Institutions)

FCS

13 August 2013

8 / 28

Encoding Natural Numbers


If f is a lambda abstraction and x is a lambda expression, then the
Natural numbers including zero (technically, positive integers) can be
encoded as x , f (x ), f (f (x )), , f n (x ),
let 0 = fx  x
let 1 = fx  f x
let 2 = fx  f (f x )
let 3 = fx  f (f (f x ))

C. Aravindan (SSN Institutions)

FCS

13 August 2013

8 / 28

Encoding Natural Numbers


If f is a lambda abstraction and x is a lambda expression, then the
Natural numbers including zero (technically, positive integers) can be
encoded as x , f (x ), f (f (x )), , f n (x ),
let 0 = fx  x
let 1 = fx  f x
let 2 = fx  f (f x )
let 3 = fx  f (f (f x ))

Note that 0 and F are the same lambda expressions! Inferring the
type can help us to interpret the expression appropriately.
C. Aravindan (SSN Institutions)

FCS

13 August 2013

8 / 28

Operations on Natural Numbers: Successor


The successor function can be defined as follows:

C. Aravindan (SSN Institutions)

FCS

13 August 2013

9 / 28

Operations on Natural Numbers: Successor


The successor function can be defined as follows:
let succ = nfx  f (n f x )

C. Aravindan (SSN Institutions)

FCS

13 August 2013

9 / 28

Operations on Natural Numbers: Successor


The successor function can be defined as follows:
let succ = nfx  f (n f x )

Let us reduce succ 2:


succ 2 = ((nfx  f (n f x )) (fx  f (f x )))

C. Aravindan (SSN Institutions)

FCS

13 August 2013

9 / 28

Operations on Natural Numbers: Successor


The successor function can be defined as follows:
let succ = nfx  f (n f x )

Let us reduce succ 2:


succ 2 = ((nfx  f (n f x )) (fx  f (f x )))
= fx  f ((fx  f (f x )) f x )

C. Aravindan (SSN Institutions)

FCS

13 August 2013

9 / 28

Operations on Natural Numbers: Successor


The successor function can be defined as follows:
let succ = nfx  f (n f x )

Let us reduce succ 2:


succ 2 = ((nfx  f (n f x )) (fx  f (f x )))
= fx  f ((fx  f (f x )) f x )
= fx  f (f (f x ))

C. Aravindan (SSN Institutions)

FCS

13 August 2013

9 / 28

Operations on Natural Numbers: Successor


The successor function can be defined as follows:
let succ = nfx  f (n f x )

Let us reduce succ 2:


succ 2 = ((nfx  f (n f x )) (fx  f (f x )))
= fx  f ((fx  f (f x )) f x )
= fx  f (f (f x ))
=3
C. Aravindan (SSN Institutions)

FCS

13 August 2013

9 / 28

Operations on Natural Numbers: Addition

Addition of two numbers can be achieved by the following abstraction:


let add = nmfx  n f (m f x )

C. Aravindan (SSN Institutions)

FCS

13 August 2013

10 / 28

Operations on Natural Numbers: Addition

Addition of two numbers can be achieved by the following abstraction:


let add = nmfx  n f (m f x )

Try reducing (add 2 3)

C. Aravindan (SSN Institutions)

FCS

13 August 2013

10 / 28

Operations on Natural Numbers: Multiplication

Multiplication of two numbers can be achieved by the following


abstraction:
let mult = nmf  n (m f )

C. Aravindan (SSN Institutions)

FCS

13 August 2013

11 / 28

Operations on Natural Numbers: Multiplication

Multiplication of two numbers can be achieved by the following


abstraction:
let mult = nmf  n (m f )
Try reducing (mult 2 3)

C. Aravindan (SSN Institutions)

FCS

13 August 2013

11 / 28

Operations on Natural Numbers: Zero checking

The isZero function can be realized by the following lambda


expression:
let isZero = nxy  n (z  y ) x

C. Aravindan (SSN Institutions)

FCS

13 August 2013

12 / 28

Operations on Natural Numbers: Zero checking

The isZero function can be realized by the following lambda


expression:
let isZero = nxy  n (z  y ) x
It is easy to verify that (isZero 0) reduces to boolean constant T

C. Aravindan (SSN Institutions)

FCS

13 August 2013

12 / 28

Operations on Natural Numbers: Zero checking

The isZero function can be realized by the following lambda


expression:
let isZero = nxy  n (z  y ) x
It is easy to verify that (isZero 0) reduces to boolean constant T
For any non-zero n, (isZero n) reduces to boolean constant F .

C. Aravindan (SSN Institutions)

FCS

13 August 2013

12 / 28

Recursion in Lambda Calculus

x is a fixpoint of a function f if f (x ) = x .

C. Aravindan (SSN Institutions)

FCS

13 August 2013

13 / 28

Recursion in Lambda Calculus

x is a fixpoint of a function f if f (x ) = x .
Examples: f (x ) = x 2 , f (x ) = x + 1, f (x ) = x .

C. Aravindan (SSN Institutions)

FCS

13 August 2013

13 / 28

Recursion in Lambda Calculus

x is a fixpoint of a function f if f (x ) = x .
Examples: f (x ) = x 2 , f (x ) = x + 1, f (x ) = x .
In Lambda calculus, if F is a lambda abstraction and N is any lambda
expression, we say that N is a fixpoint of F if (F N) N

C. Aravindan (SSN Institutions)

FCS

13 August 2013

13 / 28

Recursion in Lambda Calculus

x is a fixpoint of a function f if f (x ) = x .
Examples: f (x ) = x 2 , f (x ) = x + 1, f (x ) = x .
In Lambda calculus, if F is a lambda abstraction and N is any lambda
expression, we say that N is a fixpoint of F if (F N) N
Theorem: Every lambda abstraction F has a fixpoint.

C. Aravindan (SSN Institutions)

FCS

13 August 2013

13 / 28

Turings Fixpoint Combinator

let A = xy  y (xxy )
Turings fixpoint combinator is now obtained as let = AA.
For any lambda abstraction F , N = F is a fixpoint of F !

C. Aravindan (SSN Institutions)

FCS

13 August 2013

14 / 28

Turings Fixpoint Combinator

let A = xy  y (xxy )
Turings fixpoint combinator is now obtained as let = AA.
For any lambda abstraction F , N = F is a fixpoint of F !
N = F
= AAF
= (xy  y (xxy )) A F
F (AAF )
= F (F )
= FN

C. Aravindan (SSN Institutions)

FCS

13 August 2013

14 / 28

Fixpoints and Recursion


Consider the following:
fact n = ITE (isZero n) (1) (mult n (fact (pred n)))

C. Aravindan (SSN Institutions)

FCS

13 August 2013

15 / 28

Fixpoints and Recursion


Consider the following:
fact n = ITE (isZero n) (1) (mult n (fact (pred n)))

What is the lambda expression for the abstraction fact?

C. Aravindan (SSN Institutions)

FCS

13 August 2013

15 / 28

Fixpoints and Recursion


Consider the following:
fact n = ITE (isZero n) (1) (mult n (fact (pred n)))

What is the lambda expression for the abstraction fact?


fact = n  ITE (isZero n) (1) (mult n (fact (pred n)))

C. Aravindan (SSN Institutions)

FCS

13 August 2013

15 / 28

Fixpoints and Recursion


Consider the following:
fact n = ITE (isZero n) (1) (mult n (fact (pred n)))

What is the lambda expression for the abstraction fact?


fact = n  ITE (isZero n) (1) (mult n (fact (pred n)))
fact = (fn  ITE (isZero n) (1) (mult n (f (pred n)))) fact

C. Aravindan (SSN Institutions)

FCS

13 August 2013

15 / 28

Fixpoints and Recursion


Consider the following:
fact n = ITE (isZero n) (1) (mult n (fact (pred n)))

What is the lambda expression for the abstraction fact?


fact = n  ITE (isZero n) (1) (mult n (fact (pred n)))
fact = (fn  ITE (isZero n) (1) (mult n (f (pred n)))) fact
If we let F = (fn  ITE (isZero n) (1) (mult n (f (pred n)))), this
expression becomes
fact = F fact
C. Aravindan (SSN Institutions)

FCS

13 August 2013

15 / 28

Fixpoints and Recursion


That means, fact is nothing but a fixpoint of F ! Using Turings
combinator, we can obtain fact as
fact = F

C. Aravindan (SSN Institutions)

FCS

13 August 2013

16 / 28

Fixpoints and Recursion


That means, fact is nothing but a fixpoint of F ! Using Turings
combinator, we can obtain fact as
fact = F

You are welcome to try out the reduction of (fact 2)!!!

C. Aravindan (SSN Institutions)

FCS

13 August 2013

16 / 28

Fixpoints and Recursion


That means, fact is nothing but a fixpoint of F ! Using Turings
combinator, we can obtain fact as
fact = F

You are welcome to try out the reduction of (fact 2)!!!


There is also another fixpoint combinator discovered by Curry:
let Y = f  (x  f (x x )) (x  f (x x ))

C. Aravindan (SSN Institutions)

FCS

13 August 2013

16 / 28

Fixpoints and Recursion


That means, fact is nothing but a fixpoint of F ! Using Turings
combinator, we can obtain fact as
fact = F

You are welcome to try out the reduction of (fact 2)!!!


There is also another fixpoint combinator discovered by Curry:
let Y = f  (x  f (x x )) (x  f (x x ))

You can verify that YF is a fixpoint of any lambda abstraction F .

C. Aravindan (SSN Institutions)

FCS

13 August 2013

16 / 28

Data Abstraction: Pairs

A pair P = (M, N) can be represented by the abstraction x  x M N

C. Aravindan (SSN Institutions)

FCS

13 August 2013

17 / 28

Data Abstraction: Pairs

A pair P = (M, N) can be represented by the abstraction x  x M N


Note that this data abstraction is also a functional abstraction that
takes a function as an argument and applies that on its data fields.

C. Aravindan (SSN Institutions)

FCS

13 August 2013

17 / 28

Data Abstraction: Pairs

A pair P = (M, N) can be represented by the abstraction x  x M N


Note that this data abstraction is also a functional abstraction that
takes a function as an argument and applies that on its data fields.
Methods on pairs: How do we access the first and second elements
of P?

C. Aravindan (SSN Institutions)

FCS

13 August 2013

17 / 28

Data Abstraction: Pairs

A pair P = (M, N) can be represented by the abstraction x  x M N


Note that this data abstraction is also a functional abstraction that
takes a function as an argument and applies that on its data fields.
Methods on pairs: How do we access the first and second elements
of P?
let first = p  p (xy  x )
let second = p  p (xy  y )

C. Aravindan (SSN Institutions)

FCS

13 August 2013

17 / 28

Data Abstration: Pairs

Try reducing (first (3, 6)) and (second (3, 6))

C. Aravindan (SSN Institutions)

FCS

13 August 2013

18 / 28

Data Abstration: Pairs

Try reducing (first (3, 6)) and (second (3, 6))


(first (3, 6)) = (p  p (xy  x )) (x  x 3 6)

C. Aravindan (SSN Institutions)

FCS

13 August 2013

18 / 28

Data Abstration: Pairs

Try reducing (first (3, 6)) and (second (3, 6))


(first (3, 6)) = (p  p (xy  x )) (x  x 3 6)
= (x  x 3 6) (xy  x )

C. Aravindan (SSN Institutions)

FCS

13 August 2013

18 / 28

Data Abstration: Pairs

Try reducing (first (3, 6)) and (second (3, 6))


(first (3, 6)) = (p  p (xy  x )) (x  x 3 6)
= (x  x 3 6) (xy  x )
= (xy  x ) 3 6

C. Aravindan (SSN Institutions)

FCS

13 August 2013

18 / 28

Data Abstration: Pairs

Try reducing (first (3, 6)) and (second (3, 6))


(first (3, 6)) = (p  p (xy  x )) (x  x 3 6)
= (x  x 3 6) (xy  x )
= (xy  x ) 3 6
=3

C. Aravindan (SSN Institutions)

FCS

13 August 2013

18 / 28

Data Abstraction: Tuples

The idea of pair abstraction can be extended to tuple of any length!


An n-tuple T = (M1 , M2 , , Mn ) can be encoded as
x  x M1 M2 Mn

C. Aravindan (SSN Institutions)

FCS

13 August 2013

19 / 28

Data Abstraction: Tuples

The idea of pair abstraction can be extended to tuple of any length!


An n-tuple T = (M1 , M2 , , Mn ) can be encoded as
x  x M1 M2 Mn
The projection function to access the i th element can be given as

C. Aravindan (SSN Institutions)

FCS

13 August 2013

19 / 28

Data Abstraction: Tuples

The idea of pair abstraction can be extended to tuple of any length!


An n-tuple T = (M1 , M2 , , Mn ) can be encoded as
x  x M1 M2 Mn
The projection function to access the i th element can be given as
let in = t.t (x1 x2 xn  xi )

C. Aravindan (SSN Institutions)

FCS

13 August 2013

19 / 28

Data Abstraction: Sequences

We have already seen the fundamental concepts of sequences and


how they are different from tuples
A sequence is nothing but a pair, where the first element is the head
of the sequence and second element is the tail of the sequence
For example,
S = h0, 1, 0, 1i
S = (0 h1, 0, 1i)
S = (0 (1 h0, 1i))
S = (0 (1 (0 h1i)))
S = (0 (1 (0 (1 hi))))

C. Aravindan (SSN Institutions)

FCS

13 August 2013

20 / 28

Data Abstraction: Sequences


In lambda calculus, an empty sequence can be defined as follows:
let nil = xy  y

C. Aravindan (SSN Institutions)

FCS

13 August 2013

21 / 28

Data Abstraction: Sequences


In lambda calculus, an empty sequence can be defined as follows:
let nil = xy  y
And we know how to abstract a pair S = (H TL):
let (H TL) = xy  x H TL

C. Aravindan (SSN Institutions)

FCS

13 August 2013

21 / 28

Data Abstraction: Sequences


In lambda calculus, an empty sequence can be defined as follows:
let nil = xy  y
And we know how to abstract a pair S = (H TL):
let (H TL) = xy  x H TL
Examples:
xy  y

C. Aravindan (SSN Institutions)

FCS

13 August 2013

21 / 28

Data Abstraction: Sequences


In lambda calculus, an empty sequence can be defined as follows:
let nil = xy  y
And we know how to abstract a pair S = (H TL):
let (H TL) = xy  x H TL
Examples:
xy  y
xy  x (fx  f x ) (xy  y )

C. Aravindan (SSN Institutions)

FCS

13 August 2013

21 / 28

Data Abstraction: Sequences


In lambda calculus, an empty sequence can be defined as follows:
let nil = xy  y
And we know how to abstract a pair S = (H TL):
let (H TL) = xy  x H TL
Examples:
xy  y
xy  x (fx  f x ) (xy  y )
xy  x (fx  f (f x )) (xy  x (fx  f x ) (xy  y ))
C. Aravindan (SSN Institutions)

FCS

13 August 2013

21 / 28

Data Abstraction: Sequences


These encodings are special in that recursive operations on sequences
can be easily carried out.

C. Aravindan (SSN Institutions)

FCS

13 August 2013

22 / 28

Data Abstraction: Sequences


These encodings are special in that recursive operations on sequences
can be easily carried out.
Note that a sequence is also a functional abstraction that can be
applied on two arguments: (l P Q)

C. Aravindan (SSN Institutions)

FCS

13 August 2013

22 / 28

Data Abstraction: Sequences


These encodings are special in that recursive operations on sequences
can be easily carried out.
Note that a sequence is also a functional abstraction that can be
applied on two arguments: (l P Q)
If l happens to be nil, this reduces to Q!

C. Aravindan (SSN Institutions)

FCS

13 August 2013

22 / 28

Data Abstraction: Sequences


These encodings are special in that recursive operations on sequences
can be easily carried out.
Note that a sequence is also a functional abstraction that can be
applied on two arguments: (l P Q)
If l happens to be nil, this reduces to Q!
Otherwise, l is a pair (H TL), and so (l P Q) reduces to (P H TL)

C. Aravindan (SSN Institutions)

FCS

13 August 2013

22 / 28

Data Abstraction: Sequences


These encodings are special in that recursive operations on sequences
can be easily carried out.
Note that a sequence is also a functional abstraction that can be
applied on two arguments: (l P Q)
If l happens to be nil, this reduces to Q!
Otherwise, l is a pair (H TL), and so (l P Q) reduces to (P H TL)
Can you now suggest a lambda abstraction to check if a given
sequence is empty?

C. Aravindan (SSN Institutions)

FCS

13 August 2013

22 / 28

Data Abstraction: Sequences


These encodings are special in that recursive operations on sequences
can be easily carried out.
Note that a sequence is also a functional abstraction that can be
applied on two arguments: (l P Q)
If l happens to be nil, this reduces to Q!
Otherwise, l is a pair (H TL), and so (l P Q) reduces to (P H TL)
Can you now suggest a lambda abstraction to check if a given
sequence is empty?
Let us define a lambda abstraction to find the sum of a given
sequence of numbers:

C. Aravindan (SSN Institutions)

FCS

13 August 2013

22 / 28

Data Abstraction: Sequences


These encodings are special in that recursive operations on sequences
can be easily carried out.
Note that a sequence is also a functional abstraction that can be
applied on two arguments: (l P Q)
If l happens to be nil, this reduces to Q!
Otherwise, l is a pair (H TL), and so (l P Q) reduces to (P H TL)
Can you now suggest a lambda abstraction to check if a given
sequence is empty?
Let us define a lambda abstraction to find the sum of a given
sequence of numbers:
let addlist l = l (ht  add h (addlist t)) (0)

C. Aravindan (SSN Institutions)

FCS

13 August 2013

22 / 28

Data Abstraction: Sequences


These encodings are special in that recursive operations on sequences
can be easily carried out.
Note that a sequence is also a functional abstraction that can be
applied on two arguments: (l P Q)
If l happens to be nil, this reduces to Q!
Otherwise, l is a pair (H TL), and so (l P Q) reduces to (P H TL)
Can you now suggest a lambda abstraction to check if a given
sequence is empty?
Let us define a lambda abstraction to find the sum of a given
sequence of numbers:
let addlist l = l (ht  add h (addlist t)) (0)

See how to reduce (addlist (5 hi))


C. Aravindan (SSN Institutions)

FCS

13 August 2013

22 / 28

Data Abstraction: Binary Trees

Like a sequence, a binary tree can also be encoded as a pair of left


sub-tree and right sub-tree

C. Aravindan (SSN Institutions)

FCS

13 August 2013

23 / 28

Data Abstraction: Binary Trees

Like a sequence, a binary tree can also be encoded as a pair of left


sub-tree and right sub-tree
We can let a leaf to be labelled by a natural number

C. Aravindan (SSN Institutions)

FCS

13 August 2013

23 / 28

Data Abstraction: Binary Trees

Like a sequence, a binary tree can also be encoded as a pair of left


sub-tree and right sub-tree
We can let a leaf to be labelled by a natural number
The lambda encodings are very similar to those of sequences:
let leaf (n) = xy  x n
let node(L, R) = xy  y L R

C. Aravindan (SSN Institutions)

FCS

13 August 2013

23 / 28

Data Abstraction: Binary Trees

Like a sequence, a binary tree can also be encoded as a pair of left


sub-tree and right sub-tree
We can let a leaf to be labelled by a natural number
The lambda encodings are very similar to those of sequences:
let leaf (n) = xy  x n
let node(L, R) = xy  y L R
Note that we have not talked about an empty tree! A tree has to be
either a leaf or a node.

C. Aravindan (SSN Institutions)

FCS

13 August 2013

23 / 28

Data Abstraction: Binary Trees


Following are some examples of binary trees:
T 1 = (leaf 10)
= xy  x 10

C. Aravindan (SSN Institutions)

FCS

13 August 2013

24 / 28

Data Abstraction: Binary Trees


Following are some examples of binary trees:
T 1 = (leaf 10)
= xy  x 10
T 2 = (leaf 5)
= xy  x 5

C. Aravindan (SSN Institutions)

FCS

13 August 2013

24 / 28

Data Abstraction: Binary Trees


Following are some examples of binary trees:
T 1 = (leaf 10)
= xy  x 10
T 2 = (leaf 5)
= xy  x 5
T 3 = (node T 1 T 2) = (node (leaf 10) (leaf 5))
= xy  y T 1 T 2 = xy  y (xy  x 10) (xy  x 5)

C. Aravindan (SSN Institutions)

FCS

13 August 2013

24 / 28

Data Abstraction: Binary Trees


Following are some examples of binary trees:
T 1 = (leaf 10)
= xy  x 10
T 2 = (leaf 5)
= xy  x 5
T 3 = (node T 1 T 2) = (node (leaf 10) (leaf 5))
= xy  y T 1 T 2 = xy  y (xy  x 10) (xy  x 5)
T 4 = (node (node (node (leaf 20) (node (leaf 6) (leaf 2))) (leaf 1))
(node (leaf 12) (node (leaf 7) (node (leaf 5) (leaf 3)))))
C. Aravindan (SSN Institutions)

FCS

13 August 2013

24 / 28

Data Abstraction: Binary Trees

Recursive operations on trees are easy to code.

C. Aravindan (SSN Institutions)

FCS

13 August 2013

25 / 28

Data Abstraction: Binary Trees

Recursive operations on trees are easy to code.


Consider (t P Q).

C. Aravindan (SSN Institutions)

FCS

13 August 2013

25 / 28

Data Abstraction: Binary Trees

Recursive operations on trees are easy to code.


Consider (t P Q).
If t is a leaf, this reduces to (P n)

C. Aravindan (SSN Institutions)

FCS

13 August 2013

25 / 28

Data Abstraction: Binary Trees

Recursive operations on trees are easy to code.


Consider (t P Q).
If t is a leaf, this reduces to (P n)
Otherwise, t is a node, and this reduces to (Q l r )

C. Aravindan (SSN Institutions)

FCS

13 August 2013

25 / 28

Data Abstraction: Binary Trees

Recursive operations on trees are easy to code.


Consider (t P Q).
If t is a leaf, this reduces to (P n)
Otherwise, t is a node, and this reduces to (Q l r )
Can you now think of a lambda abstraction for adding all the
numbers in a tree?

C. Aravindan (SSN Institutions)

FCS

13 August 2013

25 / 28

Data Abstraction: Binary Trees

Recursive operations on trees are easy to code.


Consider (t P Q).
If t is a leaf, this reduces to (P n)
Otherwise, t is a node, and this reduces to (Q l r )
Can you now think of a lambda abstraction for adding all the
numbers in a tree?
let addtree t = t (n  n) (lr  add (addtree l) (addtree r ))

C. Aravindan (SSN Institutions)

FCS

13 August 2013

25 / 28

Data Abstraction: Binary Trees

Recursive operations on trees are easy to code.


Consider (t P Q).
If t is a leaf, this reduces to (P n)
Otherwise, t is a node, and this reduces to (Q l r )
Can you now think of a lambda abstraction for adding all the
numbers in a tree?
let addtree t = t (n  n) (lr  add (addtree l) (addtree r ))

Try reducing (addtree T 4)!

C. Aravindan (SSN Institutions)

FCS

13 August 2013

25 / 28

Functional Programming Languages

C. Aravindan (SSN Institutions)

FCS

13 August 2013

26 / 28

Summary

We have seen that programming can be done using pure lambda


calculus
Basic data types such as boolean and numbers can be easily encoded
and basic operations on them can be easily carried out
We have seen that if-then-else can be easily encoded as a
functional abstraction
Beauty of the lambda calculus is that every functional abstraction has
a fixpoint and that is used to solve recursive expressions.
It is also easy to build higher-order data structures such as pairs,
tuples, sequences, and binay trees

C. Aravindan (SSN Institutions)

FCS

13 August 2013

27 / 28

What next?

Review the basics of sets, relations, and function.


Practice various proof techniques
Work on the exercises given during the lectures.
Review the basics of algorithm design and complexity analysis of
algorithms
In the next couple of lectures, we will look at some basic functional
programming concepts using ML

C. Aravindan (SSN Institutions)

FCS

13 August 2013

28 / 28

You might also like