l8-purelambdacalculus.pdf

100
Foundations of Computer Science Pure Lambda Calculus C. Aravindan <[email protected]> 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

Upload: ashwini-paranthaman

Post on 09-Jul-2016

5 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: L8-PureLambdaCalculus.pdf

Foundations of Computer SciencePure Lambda Calculus

C. Aravindan<[email protected]>

Professor of Computer Science andAssistant Director

SSN School of Advanced Software Engineering

13 August 2013

C. Aravindan (SSN Institutions) FCS 13 August 2013 1 / 28

Page 2: L8-PureLambdaCalculus.pdf

Outline

1 Introduction

2 Boolean data and functional abstractions

3 Conditions

4 Natural Numbers

5 Fixpoints and Recursion

6 Data abstractions

7 Functional Programming Languages

8 Summary

C. Aravindan (SSN Institutions) FCS 13 August 2013 2 / 28

Page 3: L8-PureLambdaCalculus.pdf

Outline

1 Introduction

2 Boolean data and functional abstractions

3 Conditions

4 Natural Numbers

5 Fixpoints and Recursion

6 Data abstractions

7 Functional Programming Languages

8 Summary

C. Aravindan (SSN Institutions) FCS 13 August 2013 2 / 28

Page 4: L8-PureLambdaCalculus.pdf

Outline

1 Introduction

2 Boolean data and functional abstractions

3 Conditions

4 Natural Numbers

5 Fixpoints and Recursion

6 Data abstractions

7 Functional Programming Languages

8 Summary

C. Aravindan (SSN Institutions) FCS 13 August 2013 2 / 28

Page 5: L8-PureLambdaCalculus.pdf

Outline

1 Introduction

2 Boolean data and functional abstractions

3 Conditions

4 Natural Numbers

5 Fixpoints and Recursion

6 Data abstractions

7 Functional Programming Languages

8 Summary

C. Aravindan (SSN Institutions) FCS 13 August 2013 2 / 28

Page 6: L8-PureLambdaCalculus.pdf

Outline

1 Introduction

2 Boolean data and functional abstractions

3 Conditions

4 Natural Numbers

5 Fixpoints and Recursion

6 Data abstractions

7 Functional Programming Languages

8 Summary

C. Aravindan (SSN Institutions) FCS 13 August 2013 2 / 28

Page 7: L8-PureLambdaCalculus.pdf

Outline

1 Introduction

2 Boolean data and functional abstractions

3 Conditions

4 Natural Numbers

5 Fixpoints and Recursion

6 Data abstractions

7 Functional Programming Languages

8 Summary

C. Aravindan (SSN Institutions) FCS 13 August 2013 2 / 28

Page 8: L8-PureLambdaCalculus.pdf

Outline

1 Introduction

2 Boolean data and functional abstractions

3 Conditions

4 Natural Numbers

5 Fixpoints and Recursion

6 Data abstractions

7 Functional Programming Languages

8 Summary

C. Aravindan (SSN Institutions) FCS 13 August 2013 2 / 28

Page 9: L8-PureLambdaCalculus.pdf

Outline

1 Introduction

2 Boolean data and functional abstractions

3 Conditions

4 Natural Numbers

5 Fixpoints and Recursion

6 Data abstractions

7 Functional Programming Languages

8 Summary

C. Aravindan (SSN Institutions) FCS 13 August 2013 2 / 28

Page 10: L8-PureLambdaCalculus.pdf

Lambda Calculus

We have discussed the basics of lambda abstractions andcombinations.The syntax is extremely simple!Semantics is given by reductionsReductions are in general non-deterministic and hence strategies areneeded — normal order and applicative orderWe have briefly looked at some important theoretical results in theform of Church-Rosser theorems and Church-Turing hypothesis.We have also touched upon some principles of programminglanguages.

C. Aravindan (SSN Institutions) FCS 13 August 2013 3 / 28

Page 11: L8-PureLambdaCalculus.pdf

Pure Lambda Calculus

It is possible to encode both data and functional abstractions inlambda calculus!

C. Aravindan (SSN Institutions) FCS 13 August 2013 4 / 28

Page 12: L8-PureLambdaCalculus.pdf

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

Page 13: L8-PureLambdaCalculus.pdf

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

Page 14: L8-PureLambdaCalculus.pdf

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

Page 15: L8-PureLambdaCalculus.pdf

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

Page 16: L8-PureLambdaCalculus.pdf

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

Page 17: L8-PureLambdaCalculus.pdf

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

Page 18: L8-PureLambdaCalculus.pdf

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

Page 19: L8-PureLambdaCalculus.pdf

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

Page 20: L8-PureLambdaCalculus.pdf

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

Page 21: L8-PureLambdaCalculus.pdf

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

Page 22: L8-PureLambdaCalculus.pdf

Conditional evaluation

Conditional evaluation is a very fundamental programming conceptand I do not know of any programming language that does not havesome form of “if-then-else”!

Our abstractions of “true” and “false” make it extremely simple toencode “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

Page 23: L8-PureLambdaCalculus.pdf

Conditional evaluation

Conditional evaluation is a very fundamental programming conceptand I do not know of any programming language that does not havesome form of “if-then-else”!Our abstractions of “true” and “false” make it extremely simple toencode “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

Page 24: L8-PureLambdaCalculus.pdf

Conditional evaluation

Conditional evaluation is a very fundamental programming conceptand I do not know of any programming language that does not havesome form of “if-then-else”!Our abstractions of “true” and “false” make it extremely simple toencode “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

Page 25: L8-PureLambdaCalculus.pdf

Conditional evaluation

Conditional evaluation is a very fundamental programming conceptand I do not know of any programming language that does not havesome form of “if-then-else”!Our abstractions of “true” and “false” make it extremely simple toencode “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

Page 26: L8-PureLambdaCalculus.pdf

Encoding Natural Numbers

If f is a lambda abstraction and x is a lambda expression, then theNatural numbers including zero (technically, positive integers) can beencoded 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 thetype can help us to interpret the expression appropriately.

C. Aravindan (SSN Institutions) FCS 13 August 2013 8 / 28

Page 27: L8-PureLambdaCalculus.pdf

Encoding Natural Numbers

If f is a lambda abstraction and x is a lambda expression, then theNatural numbers including zero (technically, positive integers) can beencoded 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 thetype can help us to interpret the expression appropriately.

C. Aravindan (SSN Institutions) FCS 13 August 2013 8 / 28

Page 28: L8-PureLambdaCalculus.pdf

Encoding Natural Numbers

If f is a lambda abstraction and x is a lambda expression, then theNatural numbers including zero (technically, positive integers) can beencoded 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 thetype can help us to interpret the expression appropriately.

C. Aravindan (SSN Institutions) FCS 13 August 2013 8 / 28

Page 29: L8-PureLambdaCalculus.pdf

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

Page 30: L8-PureLambdaCalculus.pdf

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

Page 31: L8-PureLambdaCalculus.pdf

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

Page 32: L8-PureLambdaCalculus.pdf

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

Page 33: L8-PureLambdaCalculus.pdf

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

Page 34: L8-PureLambdaCalculus.pdf

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

Page 35: L8-PureLambdaCalculus.pdf

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

Page 36: L8-PureLambdaCalculus.pdf

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

Page 37: L8-PureLambdaCalculus.pdf

Operations on Natural Numbers: Multiplication

Multiplication of two numbers can be achieved by the followingabstraction:

let mult = λnmf � n (m f )

Try reducing (mult 2 3)

C. Aravindan (SSN Institutions) FCS 13 August 2013 11 / 28

Page 38: L8-PureLambdaCalculus.pdf

Operations on Natural Numbers: Multiplication

Multiplication of two numbers can be achieved by the followingabstraction:

let mult = λnmf � n (m f )

Try reducing (mult 2 3)

C. Aravindan (SSN Institutions) FCS 13 August 2013 11 / 28

Page 39: L8-PureLambdaCalculus.pdf

Operations on Natural Numbers: Zero checking

The “isZero” function can be realized by the following lambdaexpression:

let isZero = λnxy � n (λz � y) x

It is easy to verify that (isZero 0) reduces to boolean constant TFor any non-zero n, (isZero n) reduces to boolean constant F .

C. Aravindan (SSN Institutions) FCS 13 August 2013 12 / 28

Page 40: L8-PureLambdaCalculus.pdf

Operations on Natural Numbers: Zero checking

The “isZero” function can be realized by the following lambdaexpression:

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

Page 41: L8-PureLambdaCalculus.pdf

Operations on Natural Numbers: Zero checking

The “isZero” function can be realized by the following lambdaexpression:

let isZero = λnxy � n (λz � y) x

It is easy to verify that (isZero 0) reduces to boolean constant TFor any non-zero n, (isZero n) reduces to boolean constant F .

C. Aravindan (SSN Institutions) FCS 13 August 2013 12 / 28

Page 42: L8-PureLambdaCalculus.pdf

Recursion in Lambda Calculus

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

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

β NTheorem: Every lambda abstraction F has a fixpoint.

C. Aravindan (SSN Institutions) FCS 13 August 2013 13 / 28

Page 43: L8-PureLambdaCalculus.pdf

Recursion in Lambda Calculus

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

In Lambda calculus, if F is a lambda abstraction and N is any lambdaexpression, we say that N is a fixpoint of F if (F N)⇒∗

β NTheorem: Every lambda abstraction F has a fixpoint.

C. Aravindan (SSN Institutions) FCS 13 August 2013 13 / 28

Page 44: L8-PureLambdaCalculus.pdf

Recursion in Lambda Calculus

x is a fixpoint of a function f if f (x) = x .Examples: f (x) = x2, f (x) = x + 1, f (x) = x .In Lambda calculus, if F is a lambda abstraction and N is any lambdaexpression, 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

Page 45: L8-PureLambdaCalculus.pdf

Recursion in Lambda Calculus

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

β NTheorem: Every lambda abstraction F has a fixpoint.

C. Aravindan (SSN Institutions) FCS 13 August 2013 13 / 28

Page 46: L8-PureLambdaCalculus.pdf

Turing’s Fixpoint Combinator

let A = λxy � y(xxy)

Turing’s 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

Page 47: L8-PureLambdaCalculus.pdf

Turing’s Fixpoint Combinator

let A = λxy � y(xxy)

Turing’s 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

Page 48: L8-PureLambdaCalculus.pdf

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)))), thisexpression becomes

fact = F fact

C. Aravindan (SSN Institutions) FCS 13 August 2013 15 / 28

Page 49: L8-PureLambdaCalculus.pdf

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)))), thisexpression becomes

fact = F fact

C. Aravindan (SSN Institutions) FCS 13 August 2013 15 / 28

Page 50: L8-PureLambdaCalculus.pdf

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)))), thisexpression becomes

fact = F fact

C. Aravindan (SSN Institutions) FCS 13 August 2013 15 / 28

Page 51: L8-PureLambdaCalculus.pdf

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)))), thisexpression becomes

fact = F fact

C. Aravindan (SSN Institutions) FCS 13 August 2013 15 / 28

Page 52: L8-PureLambdaCalculus.pdf

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)))), thisexpression becomes

fact = F fact

C. Aravindan (SSN Institutions) FCS 13 August 2013 15 / 28

Page 53: L8-PureLambdaCalculus.pdf

Fixpoints and Recursion

That means, fact is nothing but a fixpoint of F ! Using Turing’scombinator, 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

Page 54: L8-PureLambdaCalculus.pdf

Fixpoints and Recursion

That means, fact is nothing but a fixpoint of F ! Using Turing’scombinator, 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

Page 55: L8-PureLambdaCalculus.pdf

Fixpoints and Recursion

That means, fact is nothing but a fixpoint of F ! Using Turing’scombinator, 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

Page 56: L8-PureLambdaCalculus.pdf

Fixpoints and Recursion

That means, fact is nothing but a fixpoint of F ! Using Turing’scombinator, 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

Page 57: L8-PureLambdaCalculus.pdf

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 thattakes a function as an argument and applies that on its data fields.Methods on pairs: How do we access the first and second elementsof P?

let first = λp � p (λxy � x)

let second = λp � p (λxy � y)

C. Aravindan (SSN Institutions) FCS 13 August 2013 17 / 28

Page 58: L8-PureLambdaCalculus.pdf

Data Abstraction: Pairs

A pair P = (M,N) can be represented by the abstraction λx � x M NNote that this data abstraction is also a functional abstraction thattakes a function as an argument and applies that on its data fields.

Methods on pairs: How do we access the first and second elementsof P?

let first = λp � p (λxy � x)

let second = λp � p (λxy � y)

C. Aravindan (SSN Institutions) FCS 13 August 2013 17 / 28

Page 59: L8-PureLambdaCalculus.pdf

Data Abstraction: Pairs

A pair P = (M,N) can be represented by the abstraction λx � x M NNote that this data abstraction is also a functional abstraction thattakes a function as an argument and applies that on its data fields.Methods on pairs: How do we access the first and second elementsof P?

let first = λp � p (λxy � x)

let second = λp � p (λxy � y)

C. Aravindan (SSN Institutions) FCS 13 August 2013 17 / 28

Page 60: L8-PureLambdaCalculus.pdf

Data Abstraction: Pairs

A pair P = (M,N) can be represented by the abstraction λx � x M NNote that this data abstraction is also a functional abstraction thattakes a function as an argument and applies that on its data fields.Methods on pairs: How do we access the first and second elementsof P?

let first = λp � p (λxy � x)

let second = λp � p (λxy � y)

C. Aravindan (SSN Institutions) FCS 13 August 2013 17 / 28

Page 61: L8-PureLambdaCalculus.pdf

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

Page 62: L8-PureLambdaCalculus.pdf

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

Page 63: L8-PureLambdaCalculus.pdf

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

Page 64: L8-PureLambdaCalculus.pdf

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

Page 65: L8-PureLambdaCalculus.pdf

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

Page 66: L8-PureLambdaCalculus.pdf

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 πni = λt.t (λx1x2 · · · xn � xi )

C. Aravindan (SSN Institutions) FCS 13 August 2013 19 / 28

Page 67: L8-PureLambdaCalculus.pdf

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 πni = λt.t (λx1x2 · · · xn � xi )

C. Aravindan (SSN Institutions) FCS 13 August 2013 19 / 28

Page 68: L8-PureLambdaCalculus.pdf

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 πni = λt.t (λx1x2 · · · xn � xi )

C. Aravindan (SSN Institutions) FCS 13 August 2013 19 / 28

Page 69: L8-PureLambdaCalculus.pdf

Data Abstraction: Sequences

We have already seen the fundamental concepts of sequences andhow they are different from tuplesA sequence is nothing but a pair, where the first element is the headof the sequence and second element is the tail of the sequenceFor example,

S = 〈0, 1, 0, 1〉

S = (0 • 〈1, 0, 1〉)

S = (0 • (1 • 〈0, 1〉))

S = (0 • (1 • (0 • 〈1〉)))

S = (0 • (1 • (0 • (1 • 〈〉))))

C. Aravindan (SSN Institutions) FCS 13 August 2013 20 / 28

Page 70: L8-PureLambdaCalculus.pdf

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

Page 71: L8-PureLambdaCalculus.pdf

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

Page 72: L8-PureLambdaCalculus.pdf

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

Page 73: L8-PureLambdaCalculus.pdf

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

Page 74: L8-PureLambdaCalculus.pdf

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

Page 75: L8-PureLambdaCalculus.pdf

Data Abstraction: Sequences

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

Note that a sequence is also a functional abstraction that can beapplied 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 givensequence is empty?Let us define a lambda abstraction to find the sum of a givensequence of numbers:

let addlist l = l (λht � add h (addlist t)) (0)

See how to reduce (addlist (5 • 〈〉))

C. Aravindan (SSN Institutions) FCS 13 August 2013 22 / 28

Page 76: L8-PureLambdaCalculus.pdf

Data Abstraction: Sequences

These encodings are special in that recursive operations on sequencescan be easily carried out.Note that a sequence is also a functional abstraction that can beapplied 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 givensequence is empty?Let us define a lambda abstraction to find the sum of a givensequence of numbers:

let addlist l = l (λht � add h (addlist t)) (0)

See how to reduce (addlist (5 • 〈〉))

C. Aravindan (SSN Institutions) FCS 13 August 2013 22 / 28

Page 77: L8-PureLambdaCalculus.pdf

Data Abstraction: Sequences

These encodings are special in that recursive operations on sequencescan be easily carried out.Note that a sequence is also a functional abstraction that can beapplied 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 givensequence is empty?Let us define a lambda abstraction to find the sum of a givensequence of numbers:

let addlist l = l (λht � add h (addlist t)) (0)

See how to reduce (addlist (5 • 〈〉))

C. Aravindan (SSN Institutions) FCS 13 August 2013 22 / 28

Page 78: L8-PureLambdaCalculus.pdf

Data Abstraction: Sequences

These encodings are special in that recursive operations on sequencescan be easily carried out.Note that a sequence is also a functional abstraction that can beapplied 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 givensequence is empty?Let us define a lambda abstraction to find the sum of a givensequence of numbers:

let addlist l = l (λht � add h (addlist t)) (0)

See how to reduce (addlist (5 • 〈〉))

C. Aravindan (SSN Institutions) FCS 13 August 2013 22 / 28

Page 79: L8-PureLambdaCalculus.pdf

Data Abstraction: Sequences

These encodings are special in that recursive operations on sequencescan be easily carried out.Note that a sequence is also a functional abstraction that can beapplied 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 givensequence is empty?

Let us define a lambda abstraction to find the sum of a givensequence of numbers:

let addlist l = l (λht � add h (addlist t)) (0)

See how to reduce (addlist (5 • 〈〉))

C. Aravindan (SSN Institutions) FCS 13 August 2013 22 / 28

Page 80: L8-PureLambdaCalculus.pdf

Data Abstraction: Sequences

These encodings are special in that recursive operations on sequencescan be easily carried out.Note that a sequence is also a functional abstraction that can beapplied 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 givensequence is empty?Let us define a lambda abstraction to find the sum of a givensequence of numbers:

let addlist l = l (λht � add h (addlist t)) (0)

See how to reduce (addlist (5 • 〈〉))

C. Aravindan (SSN Institutions) FCS 13 August 2013 22 / 28

Page 81: L8-PureLambdaCalculus.pdf

Data Abstraction: Sequences

These encodings are special in that recursive operations on sequencescan be easily carried out.Note that a sequence is also a functional abstraction that can beapplied 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 givensequence is empty?Let us define a lambda abstraction to find the sum of a givensequence of numbers:

let addlist l = l (λht � add h (addlist t)) (0)

See how to reduce (addlist (5 • 〈〉))

C. Aravindan (SSN Institutions) FCS 13 August 2013 22 / 28

Page 82: L8-PureLambdaCalculus.pdf

Data Abstraction: Sequences

These encodings are special in that recursive operations on sequencescan be easily carried out.Note that a sequence is also a functional abstraction that can beapplied 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 givensequence is empty?Let us define a lambda abstraction to find the sum of a givensequence of numbers:

let addlist l = l (λht � add h (addlist t)) (0)

See how to reduce (addlist (5 • 〈〉))

C. Aravindan (SSN Institutions) FCS 13 August 2013 22 / 28

Page 83: L8-PureLambdaCalculus.pdf

Data Abstraction: Binary Trees

Like a sequence, a binary tree can also be encoded as a pair of leftsub-tree and right sub-tree

We can let a leaf to be labelled by a natural numberThe 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 beeither a leaf or a node.

C. Aravindan (SSN Institutions) FCS 13 August 2013 23 / 28

Page 84: L8-PureLambdaCalculus.pdf

Data Abstraction: Binary Trees

Like a sequence, a binary tree can also be encoded as a pair of leftsub-tree and right sub-treeWe 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 beeither a leaf or a node.

C. Aravindan (SSN Institutions) FCS 13 August 2013 23 / 28

Page 85: L8-PureLambdaCalculus.pdf

Data Abstraction: Binary Trees

Like a sequence, a binary tree can also be encoded as a pair of leftsub-tree and right sub-treeWe can let a leaf to be labelled by a natural numberThe 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 beeither a leaf or a node.

C. Aravindan (SSN Institutions) FCS 13 August 2013 23 / 28

Page 86: L8-PureLambdaCalculus.pdf

Data Abstraction: Binary Trees

Like a sequence, a binary tree can also be encoded as a pair of leftsub-tree and right sub-treeWe can let a leaf to be labelled by a natural numberThe 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 beeither a leaf or a node.

C. Aravindan (SSN Institutions) FCS 13 August 2013 23 / 28

Page 87: L8-PureLambdaCalculus.pdf

Data Abstraction: Binary Trees

Following are some examples of binary trees:

T1 = (leaf 10)

= λxy � x 10

T2 = (leaf 5)

= λxy � x 5

T3 = (node T1 T2) = (node (leaf 10) (leaf 5))

= λxy � y T1 T2 = λxy � y (λxy � x 10) (λxy � x 5)

T4 = (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

Page 88: L8-PureLambdaCalculus.pdf

Data Abstraction: Binary Trees

Following are some examples of binary trees:

T1 = (leaf 10)

= λxy � x 10

T2 = (leaf 5)

= λxy � x 5

T3 = (node T1 T2) = (node (leaf 10) (leaf 5))

= λxy � y T1 T2 = λxy � y (λxy � x 10) (λxy � x 5)

T4 = (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

Page 89: L8-PureLambdaCalculus.pdf

Data Abstraction: Binary Trees

Following are some examples of binary trees:

T1 = (leaf 10)

= λxy � x 10

T2 = (leaf 5)

= λxy � x 5

T3 = (node T1 T2) = (node (leaf 10) (leaf 5))

= λxy � y T1 T2 = λxy � y (λxy � x 10) (λxy � x 5)

T4 = (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

Page 90: L8-PureLambdaCalculus.pdf

Data Abstraction: Binary Trees

Following are some examples of binary trees:

T1 = (leaf 10)

= λxy � x 10

T2 = (leaf 5)

= λxy � x 5

T3 = (node T1 T2) = (node (leaf 10) (leaf 5))

= λxy � y T1 T2 = λxy � y (λxy � x 10) (λxy � x 5)

T4 = (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

Page 91: L8-PureLambdaCalculus.pdf

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 thenumbers in a tree?

let addtree t = t (λn � n) (λlr � add (addtree l) (addtree r))

Try reducing (addtree T4)!

C. Aravindan (SSN Institutions) FCS 13 August 2013 25 / 28

Page 92: L8-PureLambdaCalculus.pdf

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 thenumbers in a tree?

let addtree t = t (λn � n) (λlr � add (addtree l) (addtree r))

Try reducing (addtree T4)!

C. Aravindan (SSN Institutions) FCS 13 August 2013 25 / 28

Page 93: L8-PureLambdaCalculus.pdf

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 thenumbers in a tree?

let addtree t = t (λn � n) (λlr � add (addtree l) (addtree r))

Try reducing (addtree T4)!

C. Aravindan (SSN Institutions) FCS 13 August 2013 25 / 28

Page 94: L8-PureLambdaCalculus.pdf

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 thenumbers in a tree?

let addtree t = t (λn � n) (λlr � add (addtree l) (addtree r))

Try reducing (addtree T4)!

C. Aravindan (SSN Institutions) FCS 13 August 2013 25 / 28

Page 95: L8-PureLambdaCalculus.pdf

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 thenumbers in a tree?

let addtree t = t (λn � n) (λlr � add (addtree l) (addtree r))

Try reducing (addtree T4)!

C. Aravindan (SSN Institutions) FCS 13 August 2013 25 / 28

Page 96: L8-PureLambdaCalculus.pdf

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 thenumbers in a tree?

let addtree t = t (λn � n) (λlr � add (addtree l) (addtree r))

Try reducing (addtree T4)!

C. Aravindan (SSN Institutions) FCS 13 August 2013 25 / 28

Page 97: L8-PureLambdaCalculus.pdf

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 thenumbers in a tree?

let addtree t = t (λn � n) (λlr � add (addtree l) (addtree r))

Try reducing (addtree T4)!

C. Aravindan (SSN Institutions) FCS 13 August 2013 25 / 28

Page 98: L8-PureLambdaCalculus.pdf

Functional Programming Languages

C. Aravindan (SSN Institutions) FCS 13 August 2013 26 / 28

Page 99: L8-PureLambdaCalculus.pdf

Summary

We have seen that programming can be done using pure lambdacalculusBasic data types such as boolean and numbers can be easily encodedand basic operations on them can be easily carried outWe have seen that “if-then-else” can be easily encoded as afunctional abstractionBeauty of the lambda calculus is that every functional abstraction hasa 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

Page 100: L8-PureLambdaCalculus.pdf

What next?

Review the basics of sets, relations, and function.Practice various proof techniquesWork on the exercises given during the lectures.Review the basics of algorithm design and complexity analysis ofalgorithmsIn the next couple of lectures, we will look at some basic functionalprogramming concepts using ML

C. Aravindan (SSN Institutions) FCS 13 August 2013 28 / 28