93
Functions all the way down! Lambda Calculus and Church Encoding Ivan Lazar Miljenovic Maths PhD Journal Club 14 May, 2009 Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 1 / 29

Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Functions all the way down!Lambda Calculus and Church Encoding

Ivan Lazar Miljenovic

Maths PhD Journal Club

14 May, 2009

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 1 / 29

Page 2: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Outline

1 Introduction

2 Theory of Computing

3 The λ-Calculus

4 Church Encoding

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 2 / 29

Page 3: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Introduction

Introduction

1 Introduction

Turtles all the way down...

Programming languages and Functions

Functions all the way down

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 3 / 29

Page 4: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Introduction Turtles all the way down...

Turtles all the way down...

From Stephen Hawking’s A Brief History of Time

A well-known scientist (some say it was Bertrand Russell) once gave apublic lecture on astronomy. He described how the earth orbits around thesun and how the sun, in turn, orbits around the center of a vast collectionof stars called our galaxy.At the end of the lecture, a little old lady at the back of the room got upand said: “What you have told us is rubbish. The world is really a flatplate supported on the back of a giant tortoise.” The scientist gave asuperior smile before replying, “What is the tortoise standing on?” “You’revery clever, young man, very clever,” said the old lady. “But it’s turtles allthe way down!”

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 4 / 29

Page 5: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Introduction Turtles all the way down...

Turtles all the way down...

From Stephen Hawking’s A Brief History of Time

A well-known scientist (some say it was Bertrand Russell) once gave apublic lecture on astronomy. He described how the earth orbits around thesun and how the sun, in turn, orbits around the center of a vast collectionof stars called our galaxy.At the end of the lecture, a little old lady at the back of the room got upand said: “What you have told us is rubbish. The world is really a flatplate supported on the back of a giant tortoise.” The scientist gave asuperior smile before replying, “What is the tortoise standing on?” “You’revery clever, young man, very clever,” said the old lady. “But it’s turtles allthe way down!”

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 4 / 29

Page 6: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Introduction Programming languages and Functions

Programming languages and Functions

Most “standard” programming languages There is data, andthen functions that act on that data.

Functional languages Functions are “first class citizens” (CristopherStrachey, mid-1960s).

LISP family Everything is data; all data is code.

What about replacing data with functions?

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 5 / 29

Page 7: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Introduction Programming languages and Functions

Programming languages and Functions

Most “standard” programming languages There is data, andthen functions that act on that data.

Functional languages Functions are “first class citizens” (CristopherStrachey, mid-1960s).

LISP family Everything is data; all data is code.

What about replacing data with functions?

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 5 / 29

Page 8: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Introduction Programming languages and Functions

Programming languages and Functions

Most “standard” programming languages There is data, andthen functions that act on that data.

Functional languages Functions are “first class citizens” (CristopherStrachey, mid-1960s).

LISP family Everything is data; all data is code.

What about replacing data with functions?

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 5 / 29

Page 9: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Introduction Programming languages and Functions

Programming languages and Functions

Most “standard” programming languages There is data, andthen functions that act on that data.

Functional languages Functions are “first class citizens” (CristopherStrachey, mid-1960s).

LISP family Everything is data; all data is code.

What about replacing data with functions?

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 5 / 29

Page 10: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Introduction Functions all the way down

Functions all the way down

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 6 / 29

Page 11: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Theory of Computing

Theory of Computing

2 Theory of Computing

The Entscheidungsproblem

Solutions to the Entscheidungsproblem

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 7 / 29

Page 12: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Theory of Computing The Entscheidungsproblem

The EntscheidungsproblemThe decidability problem

Posed by David Hilbert in 1928:

Definition (The Entscheidungsproblem)

Given a description of a formal language and a mathematical statement inthat language, determine if the statement is true or false.

Original Statement

Generalisation of his question “Is mathematics decidable”.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 8 / 29

Page 13: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Theory of Computing The Entscheidungsproblem

The EntscheidungsproblemThe decidability problem

Posed by David Hilbert in 1928:

Definition (The Entscheidungsproblem)

Given a description of a formal language and a mathematical statement inthat language, determine if the statement is true or false.

Original Statement

Generalisation of his question “Is mathematics decidable”.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 8 / 29

Page 14: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Theory of Computing The Entscheidungsproblem

The EntscheidungsproblemThe decidability problem

Posed by David Hilbert in 1928:

Definition (The Entscheidungsproblem)

Given a description of a formal language and a mathematical statement inthat language, determine if the statement is true or false.

Original Statement

Generalisation of his question “Is mathematics decidable”.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 8 / 29

Page 15: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Theory of Computing Solutions to the Entscheidungsproblem

Solutions to the EntscheidungsproblemThe Halting Problem

15 April, 1936

An Unsolvable Problem of Elementary NumberTheory by Alonzo Church

January, 1937

On Computable Numbers, with an Application tothe Entscheidungsproblem by Alan Turing

Theorem (Church-Turing Thesis)

Every effectively calculable function is a computable function.

λ-Calculus, Turing machines, etc. are equivalent.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 9 / 29

Page 16: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Theory of Computing Solutions to the Entscheidungsproblem

Solutions to the EntscheidungsproblemThe Halting Problem

15 April, 1936

An Unsolvable Problem of Elementary NumberTheory by Alonzo Church

January, 1937

On Computable Numbers, with an Application tothe Entscheidungsproblem by Alan Turing

Theorem (Church-Turing Thesis)

Every effectively calculable function is a computable function.

λ-Calculus, Turing machines, etc. are equivalent.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 9 / 29

Page 17: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Theory of Computing Solutions to the Entscheidungsproblem

Solutions to the EntscheidungsproblemThe Halting Problem

15 April, 1936

An Unsolvable Problem of Elementary NumberTheory by Alonzo Church

January, 1937

On Computable Numbers, with an Application tothe Entscheidungsproblem by Alan Turing

Theorem (Church-Turing Thesis)

Every effectively calculable function is a computable function.

λ-Calculus, Turing machines, etc. are equivalent.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 9 / 29

Page 18: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Theory of Computing Solutions to the Entscheidungsproblem

Solutions to the EntscheidungsproblemThe Halting Problem

15 April, 1936

An Unsolvable Problem of Elementary NumberTheory by Alonzo Church

January, 1937

On Computable Numbers, with an Application tothe Entscheidungsproblem by Alan Turing

Theorem (Church-Turing Thesis)

Every effectively calculable function is a computable function.

λ-Calculus, Turing machines, etc. are equivalent.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 9 / 29

Page 19: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Theory of Computing Solutions to the Entscheidungsproblem

Solutions to the EntscheidungsproblemThe Halting Problem

15 April, 1936

An Unsolvable Problem of Elementary NumberTheory by Alonzo Church

January, 1937

On Computable Numbers, with an Application tothe Entscheidungsproblem by Alan Turing

Theorem (Church-Turing Thesis)

Every effectively calculable function is a computable function.

λ-Calculus, Turing machines, etc. are equivalent.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 9 / 29

Page 20: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus

The λ-Calculus

3 The λ-Calculus

What is the λ-Calculus?

Definition of the λ-Calculus

Informal Examples

Usage of the λ-Calculus

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 10 / 29

Page 21: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus What is the λ-Calculus?

What is the λ-Calculus?

Types and Programming Languages

“. . . a formal system invented in the 1920s by Alonzo Church, inwhich all computation is reduced to the basic operations of functiondefinition and application.”

“Its importance arises from the fact that it can be viewedsimultaneously as a simple programming language in whichcomputations can be described and as a mathematical object aboutwhich rigorous statements can be proved.”

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 11 / 29

Page 22: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus What is the λ-Calculus?

What is the λ-Calculus?

Types and Programming Languages

“. . . a formal system invented in the 1920s by Alonzo Church, inwhich all computation is reduced to the basic operations of functiondefinition and application.”

“Its importance arises from the fact that it can be viewedsimultaneously as a simple programming language in whichcomputations can be described and as a mathematical object aboutwhich rigorous statements can be proved.”

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 11 / 29

Page 23: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus What is the λ-Calculus?

What is the λ-Calculus?

Types and Programming Languages

“. . . a formal system invented in the 1920s by Alonzo Church, inwhich all computation is reduced to the basic operations of functiondefinition and application.”

“Its importance arises from the fact that it can be viewedsimultaneously as a simple programming language in whichcomputations can be described and as a mathematical object aboutwhich rigorous statements can be proved.”

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 11 / 29

Page 24: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus What is the λ-Calculus?

What is the λ-Calculus?

A Brief, Incomplete, and Mostly Wrong History ofProgramming Languages

1936 Alonzo Church also invents every language that will ever bebut does it better. His lambda calculus is ignored because itis insufficiently C-like. This criticism occurs in spite of thefact that C has not yet been invented.

1970 . . . Lambdas are relegated to relative obscurity until Javamakes them popular by not having them.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 12 / 29

Page 25: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus What is the λ-Calculus?

What is the λ-Calculus?

A Brief, Incomplete, and Mostly Wrong History ofProgramming Languages

1936 Alonzo Church also invents every language that will ever bebut does it better. His lambda calculus is ignored because itis insufficiently C-like. This criticism occurs in spite of thefact that C has not yet been invented.

1970 . . . Lambdas are relegated to relative obscurity until Javamakes them popular by not having them.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 12 / 29

Page 26: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus What is the λ-Calculus?

What is the λ-Calculus?

A Brief, Incomplete, and Mostly Wrong History ofProgramming Languages

1936 Alonzo Church also invents every language that will ever bebut does it better. His lambda calculus is ignored because itis insufficiently C-like. This criticism occurs in spite of thefact that C has not yet been invented.

1970 . . . Lambdas are relegated to relative obscurity until Javamakes them popular by not having them.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 12 / 29

Page 27: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus Definition of the λ-Calculus

Definition of the λ-Calculus

A term t in the λ-Calculus is one of three things:

x A variable.

λ x . t The abstraction of a variable x from a term.

t t The application of one term to another.

Also use parentheses for grouping.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 13 / 29

Page 28: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus Definition of the λ-Calculus

Definition of the λ-Calculus

A term t in the λ-Calculus is one of three things:

x A variable.

λ x . t The abstraction of a variable x from a term.

t t The application of one term to another.

Also use parentheses for grouping.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 13 / 29

Page 29: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus Definition of the λ-Calculus

Definition of the λ-Calculus

A term t in the λ-Calculus is one of three things:

x A variable.

λ x . t The abstraction of a variable x from a term.

t t The application of one term to another.

Also use parentheses for grouping.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 13 / 29

Page 30: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus Definition of the λ-Calculus

Definition of the λ-Calculus

A term t in the λ-Calculus is one of three things:

x A variable.

λ x . t The abstraction of a variable x from a term.

t t The application of one term to another.

Also use parentheses for grouping.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 13 / 29

Page 31: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus Definition of the λ-Calculus

Definition of the λ-Calculus

A term t in the λ-Calculus is one of three things:

x A variable.

λ x . t The abstraction of a variable x from a term.

t t The application of one term to another.

Also use parentheses for grouping.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 13 / 29

Page 32: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus Definition of the λ-Calculus

Definition of the λ-Calculus

Functions are:

Anonymous

Unary

Higher-order

Recursive

Left-associative: abc = (ab)c

Extend as far right as possible

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 14 / 29

Page 33: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus Definition of the λ-Calculus

Definition of the λ-Calculus

Functions are:

Anonymous

Unary

Higher-order

Recursive

Left-associative: abc = (ab)c

Extend as far right as possible

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 14 / 29

Page 34: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus Definition of the λ-Calculus

Definition of the λ-Calculus

Functions are:

Anonymous

Unary

Higher-order

Recursive

Left-associative: abc = (ab)c

Extend as far right as possible

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 14 / 29

Page 35: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus Definition of the λ-Calculus

Definition of the λ-Calculus

Functions are:

Anonymous

Unary

Higher-order

Recursive

Left-associative: abc = (ab)c

Extend as far right as possible

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 14 / 29

Page 36: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus Definition of the λ-Calculus

Definition of the λ-Calculus

Functions are:

Anonymous

Unary

Higher-order

Recursive

Left-associative: abc = (ab)c

Extend as far right as possible

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 14 / 29

Page 37: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus Definition of the λ-Calculus

Definition of the λ-Calculus

Functions are:

Anonymous

Unary

Higher-order

Recursive

Left-associative: abc = (ab)c

Extend as far right as possible

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 14 / 29

Page 38: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus Definition of the λ-Calculus

Definition of the λ-Calculus

Functions are:

Anonymous

Unary

Higher-order

Recursive

Left-associative: abc = (ab)c

Extend as far right as possible

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 14 / 29

Page 39: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus Informal Examples

Informal Examples

Identity function:λ x . x

Mathematics:(λ x . x + 2) 3

Multiple arguments:λ x . λ y . x + y

Factorials:

g = λ f . λ n.

{1 n = 0

n × f (n − 1) n > 0

Y = λ h. (λ x . h (x x)) (λ x . h (x x))

fact = λ n. Y g n

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 15 / 29

Page 40: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus Informal Examples

Informal Examples

Identity function:λ x . x

Mathematics:(λ x . x + 2) 3

Multiple arguments:λ x . λ y . x + y

Factorials:

g = λ f . λ n.

{1 n = 0

n × f (n − 1) n > 0

Y = λ h. (λ x . h (x x)) (λ x . h (x x))

fact = λ n. Y g n

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 15 / 29

Page 41: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus Informal Examples

Informal Examples

Identity function:λ x . x

Mathematics:(λ x . x + 2) 3

Multiple arguments:λ x . λ y . x + y

Factorials:

g = λ f . λ n.

{1 n = 0

n × f (n − 1) n > 0

Y = λ h. (λ x . h (x x)) (λ x . h (x x))

fact = λ n. Y g n

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 15 / 29

Page 42: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus Informal Examples

Informal Examples

Identity function:λ x . x

Mathematics:(λ x . x + 2) 3

Multiple arguments:λ x . λ y . x + y

Factorials:

g = λ f . λ n.

{1 n = 0

n × f (n − 1) n > 0

Y = λ h. (λ x . h (x x)) (λ x . h (x x))

fact = λ n. Y g n

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 15 / 29

Page 43: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus Informal Examples

Informal Examples

Identity function:λ x . x

Mathematics:(λ x . x + 2) 3

Multiple arguments:λ x . λ y . x + y

Factorials:

g = λ f . λ n.

{1 n = 0

n × f (n − 1) n > 0

Y = λ h. (λ x . h (x x)) (λ x . h (x x))

fact = λ n. Y g n

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 15 / 29

Page 44: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus Usage of the λ-Calculus

Usage of the λ-Calculus

Usage of the λ-Calculus in programming languages:

Functional Languages Based on the λ-Calculus, treat computationas the evaluation of mathematical functions and typicallyavoids state and mutable data.

LISP family Lisp is the second oldest programming language afterFORTRAN; loosely based on the λ-Calculus but technicallyclassed as multi-paradigm.

Anonymous Functions Found in many other languages: C#,C++Ox, Matlab, etc.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 16 / 29

Page 45: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus Usage of the λ-Calculus

Usage of the λ-Calculus

Usage of the λ-Calculus in programming languages:

Functional Languages Based on the λ-Calculus, treat computationas the evaluation of mathematical functions and typicallyavoids state and mutable data.

LISP family Lisp is the second oldest programming language afterFORTRAN; loosely based on the λ-Calculus but technicallyclassed as multi-paradigm.

Anonymous Functions Found in many other languages: C#,C++Ox, Matlab, etc.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 16 / 29

Page 46: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus Usage of the λ-Calculus

Usage of the λ-Calculus

Usage of the λ-Calculus in programming languages:

Functional Languages Based on the λ-Calculus, treat computationas the evaluation of mathematical functions and typicallyavoids state and mutable data.

LISP family Lisp is the second oldest programming language afterFORTRAN; loosely based on the λ-Calculus but technicallyclassed as multi-paradigm.

Anonymous Functions Found in many other languages: C#,C++Ox, Matlab, etc.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 16 / 29

Page 47: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

The λ-Calculus Usage of the λ-Calculus

Usage of the λ-Calculus

Usage of the λ-Calculus in programming languages:

Functional Languages Based on the λ-Calculus, treat computationas the evaluation of mathematical functions and typicallyavoids state and mutable data.

LISP family Lisp is the second oldest programming language afterFORTRAN; loosely based on the λ-Calculus but technicallyclassed as multi-paradigm.

Anonymous Functions Found in many other languages: C#,C++Ox, Matlab, etc.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 16 / 29

Page 48: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding

Church Encoding

4 Church Encoding

What is Church Encoding?

Church Numerals

Peano ArithmeticIsomorphism between Church and Peano

Other operations on Church Numerals

Other Church Encodings

Representation of Church Encodings

Are Church Encodings practical?

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 17 / 29

Page 49: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding What is Church Encoding?

What is Church Encoding?

Definition (Church Encoding)

A means of embedding data and operations on them by using theλ-Calculus.

Encodings are available for:

Natural numbers

Booleans

Pairs

Lists

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 18 / 29

Page 50: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding What is Church Encoding?

What is Church Encoding?

Definition (Church Encoding)

A means of embedding data and operations on them by using theλ-Calculus.

Encodings are available for:

Natural numbers

Booleans

Pairs

Lists

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 18 / 29

Page 51: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding What is Church Encoding?

What is Church Encoding?

Definition (Church Encoding)

A means of embedding data and operations on them by using theλ-Calculus.

Encodings are available for:

Natural numbers

Booleans

Pairs

Lists

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 18 / 29

Page 52: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding What is Church Encoding?

What is Church Encoding?

Definition (Church Encoding)

A means of embedding data and operations on them by using theλ-Calculus.

Encodings are available for:

Natural numbers

Booleans

Pairs

Lists

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 18 / 29

Page 53: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding What is Church Encoding?

What is Church Encoding?

Definition (Church Encoding)

A means of embedding data and operations on them by using theλ-Calculus.

Encodings are available for:

Natural numbers

Booleans

Pairs

Lists

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 18 / 29

Page 54: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding What is Church Encoding?

What is Church Encoding?

Definition (Church Encoding)

A means of embedding data and operations on them by using theλ-Calculus.

Encodings are available for:

Natural numbers

Booleans

Pairs

Lists

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 18 / 29

Page 55: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding What is Church Encoding?

What is Church Encoding?

Definition (Church Encoding)

A means of embedding data and operations on them by using theλ-Calculus.

Encodings are available for:

Natural numbers

Booleans

Pairs

Lists

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 18 / 29

Page 56: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Church Numerals

Church Numerals

Devised by Alonzo Church in 1941 to represent natural numbers:

Church Numerals

0 ≡ λ f . λ x . x

1 ≡ λ f . λ x . f x

2 ≡ λ f . λ x . f (f x)

3 ≡ λ f . λ x . f (f (f x))

. . .

n ≡ λ f . λ x . f n x

where f 0 = id, f n+1 = f · f n.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 19 / 29

Page 57: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Church Numerals

Church Numerals

Devised by Alonzo Church in 1941 to represent natural numbers:

Church Numerals

0 ≡ λ f . λ x . x

1 ≡ λ f . λ x . f x

2 ≡ λ f . λ x . f (f x)

3 ≡ λ f . λ x . f (f (f x))

. . .

n ≡ λ f . λ x . f n x

where f 0 = id, f n+1 = f · f n.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 19 / 29

Page 58: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Church Numerals

Church Numerals

Alternatively:

Church Numerals

0 ≡ λ f . id

1 ≡ λ f . f

2 ≡ λ f . f · f3 ≡ λ f . f · f · f. . .

n ≡ λ f . f n

where f 0 = id, f n+1 = f · f n.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 19 / 29

Page 59: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Church Numerals

Church Numerals

Alternatively:

Church Numerals

0 ≡ λ f . id

1 ≡ λ f . f

2 ≡ λ f . f · f3 ≡ λ f . f · f · f. . .

n ≡ λ f . f n

where f 0 = id, f n+1 = f · f n.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 19 / 29

Page 60: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Church Numerals

Church Numerals

Operations on Church Numerals (due to Rosser):

Successor function: succ n = λ f . f · n f

Addition: plus m n = λ f .m f · n f

Multiplication: mult m n = m · nExponentiation: pow m n = n m

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 20 / 29

Page 61: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Church Numerals

Church Numerals

Operations on Church Numerals (due to Rosser):

Successor function: succ n = λ f . f · n f

Addition: plus m n = λ f .m f · n f

Multiplication: mult m n = m · nExponentiation: pow m n = n m

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 20 / 29

Page 62: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Church Numerals

Church Numerals

Operations on Church Numerals (due to Rosser):

Successor function: succ n = λ f . f · n f

Addition: plus m n = λ f .m f · n f

Multiplication: mult m n = m · nExponentiation: pow m n = n m

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 20 / 29

Page 63: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Church Numerals

Church Numerals

Operations on Church Numerals (due to Rosser):

Successor function: succ n = λ f . f · n f

Addition: plus m n = λ f .m f · n f

Multiplication: mult m n = m · n

Exponentiation: pow m n = n m

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 20 / 29

Page 64: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Church Numerals

Church Numerals

Operations on Church Numerals (due to Rosser):

Successor function: succ n = λ f . f · n f

Addition: plus m n = λ f .m f · n f

Multiplication: mult m n = m · nExponentiation: pow m n = n m

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 20 / 29

Page 65: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Peano Arithmetic

Peano Arithmetic

We can derive the Church Numerals from the unary representation of thenatural numbers, also known as the Peano numeral system.

Definition (Peano Numbers)

data Nat = Zero | Succ Nat

one :: Natone = Succ Zero

We can operate on Peano numbers using what is known as a fold function:

Definition (Fold on Peano Numbers)

fold :: (a → a) → a → Nat → afold succ zero Zero = zerofold succ zero (Succ n) = succ (fold succ zero n)

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 21 / 29

Page 66: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Peano Arithmetic

Peano Arithmetic

We can derive the Church Numerals from the unary representation of thenatural numbers, also known as the Peano numeral system.

Definition (Peano Numbers)

data Nat = Zero | Succ Nat

one :: Natone = Succ Zero

We can operate on Peano numbers using what is known as a fold function:

Definition (Fold on Peano Numbers)

fold :: (a → a) → a → Nat → afold succ zero Zero = zerofold succ zero (Succ n) = succ (fold succ zero n)

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 21 / 29

Page 67: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Peano Arithmetic

Peano Arithmetic

We can derive the Church Numerals from the unary representation of thenatural numbers, also known as the Peano numeral system.

Definition (Peano Numbers)

data Nat = Zero | Succ Nat

one :: Natone = Succ Zero

We can operate on Peano numbers using what is known as a fold function:

Definition (Fold on Peano Numbers)

fold :: (a → a) → a → Nat → afold succ zero Zero = zerofold succ zero (Succ n) = succ (fold succ zero n)

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 21 / 29

Page 68: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Peano Arithmetic

Peano Arithmetic

We can derive the Church Numerals from the unary representation of thenatural numbers, also known as the Peano numeral system.

Definition (Peano Numbers)

data Nat = Zero | Succ Nat

one :: Natone = Succ Zero

We can operate on Peano numbers using what is known as a fold function:

Definition (Fold on Peano Numbers)

fold :: (a → a) → a → Nat → afold succ zero Zero = zerofold succ zero (Succ n) = succ (fold succ zero n)

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 21 / 29

Page 69: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Peano Arithmetic

Peano Arithmetic

Operations on Nat

plus , mult , pow :: Nat → Nat → Nat

plus m n = fold Succ n m

mult m n = fold (add n) Zero m

pow m n = fold (mult m) one n

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 22 / 29

Page 70: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Peano Arithmetic

Peano ArithmeticIsomorphism between Church and Peano

We can make an isormorphism between Church Numerals and PeanoNumbers:

Converting between the two

type Church a = (a → a) → a → a

nat :: Church Nat → Natnat c = c Succ Zero

church :: Nat → Church achurch n = λ succ → λ zero → fold succ zero n

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 23 / 29

Page 71: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Peano Arithmetic

Peano ArithmeticIsomorphism between Church and Peano

We can make an isormorphism between Church Numerals and PeanoNumbers:

Converting between the two

type Church a = (a → a) → a → a

nat :: Church Nat → Natnat c = c Succ Zero

church :: Nat → Church achurch n = λ succ → λ zero → fold succ zero n

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 23 / 29

Page 72: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Peano Arithmetic

Peano ArithmeticIsomorphism between Church and Peano

This leads to a new formulation for operations on Church Numerals:

Successor function: succ c = λ s. λ z . s (c s z)

Addition: plus m n = m succ n

Multiplication: mult m n = m (n+) 0

Exponentiation: pow m n = n (m×) 1

Completely different formulation from before!

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 24 / 29

Page 73: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Peano Arithmetic

Peano ArithmeticIsomorphism between Church and Peano

This leads to a new formulation for operations on Church Numerals:

Successor function: succ c = λ s. λ z . s (c s z)

Addition: plus m n = m succ n

Multiplication: mult m n = m (n+) 0

Exponentiation: pow m n = n (m×) 1

Completely different formulation from before!

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 24 / 29

Page 74: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Peano Arithmetic

Peano ArithmeticIsomorphism between Church and Peano

This leads to a new formulation for operations on Church Numerals:

Successor function: succ c = λ s. λ z . s (c s z)

Addition: plus m n = m succ n

Multiplication: mult m n = m (n+) 0

Exponentiation: pow m n = n (m×) 1

Completely different formulation from before!

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 24 / 29

Page 75: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Peano Arithmetic

Peano ArithmeticIsomorphism between Church and Peano

This leads to a new formulation for operations on Church Numerals:

Successor function: succ c = λ s. λ z . s (c s z)

Addition: plus m n = m succ n

Multiplication: mult m n = m (n+) 0

Exponentiation: pow m n = n (m×) 1

Completely different formulation from before!

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 24 / 29

Page 76: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Peano Arithmetic

Peano ArithmeticIsomorphism between Church and Peano

This leads to a new formulation for operations on Church Numerals:

Successor function: succ c = λ s. λ z . s (c s z)

Addition: plus m n = m succ n

Multiplication: mult m n = m (n+) 0

Exponentiation: pow m n = n (m×) 1

Completely different formulation from before!

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 24 / 29

Page 77: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Peano Arithmetic

Peano ArithmeticIsomorphism between Church and Peano

This leads to a new formulation for operations on Church Numerals:

Successor function: succ c = λ s. λ z . s (c s z)

Addition: plus m n = m succ n

Multiplication: mult m n = m (n+) 0

Exponentiation: pow m n = n (m×) 1

Completely different formulation from before!

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 24 / 29

Page 78: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Other operations on Church Numerals

Other operations on Church Numerals

We can also define operations such as equal , pred and subtract on ChurchNumerals . . .

. . . but they get very messy very quickly.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 25 / 29

Page 79: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Other operations on Church Numerals

Other operations on Church Numerals

We can also define operations such as equal , pred and subtract on ChurchNumerals . . .

. . . but they get very messy very quickly.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 25 / 29

Page 80: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Other Church Encodings

Other Church Encodings

Other Church Encodings of interest:

Church Booleans:

true ≡ λ t. λ f . t

false ≡ λ t. λ f . f

Church Pairs:

pair ≡ λ f . λ s. λ b. b f s

fst ≡ λ p. p true

snd ≡ λ p. p false

Church Lists: Too complicated to define here.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 26 / 29

Page 81: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Other Church Encodings

Other Church Encodings

Other Church Encodings of interest:

Church Booleans:

true ≡ λ t. λ f . t

false ≡ λ t. λ f . f

Church Pairs:

pair ≡ λ f . λ s. λ b. b f s

fst ≡ λ p. p true

snd ≡ λ p. p false

Church Lists: Too complicated to define here.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 26 / 29

Page 82: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Other Church Encodings

Other Church Encodings

Other Church Encodings of interest:

Church Booleans:

true ≡ λ t. λ f . t

false ≡ λ t. λ f . f

Church Pairs:

pair ≡ λ f . λ s. λ b. b f s

fst ≡ λ p. p true

snd ≡ λ p. p false

Church Lists: Too complicated to define here.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 26 / 29

Page 83: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Other Church Encodings

Other Church Encodings

Other Church Encodings of interest:

Church Booleans:

true ≡ λ t. λ f . t

false ≡ λ t. λ f . f

Church Pairs:

pair ≡ λ f . λ s. λ b. b f s

fst ≡ λ p. p true

snd ≡ λ p. p false

Church Lists: Too complicated to define here.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 26 / 29

Page 84: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Representation of Church Encodings

Representation of Church Encodings

What do the different Church Encodings represent?

Church Numerals: Apply the function f on z a total of n times.

Church Booleans: Selector functions (one-line if statement, etc.).

Church Pairs: Hard-coded Church Booleans.

Church Lists: Combine elements using a combining function.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 27 / 29

Page 85: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Representation of Church Encodings

Representation of Church Encodings

What do the different Church Encodings represent?

Church Numerals: Apply the function f on z a total of n times.

Church Booleans: Selector functions (one-line if statement, etc.).

Church Pairs: Hard-coded Church Booleans.

Church Lists: Combine elements using a combining function.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 27 / 29

Page 86: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Representation of Church Encodings

Representation of Church Encodings

What do the different Church Encodings represent?

Church Numerals: Apply the function f on z a total of n times.

Church Booleans: Selector functions (one-line if statement, etc.).

Church Pairs: Hard-coded Church Booleans.

Church Lists: Combine elements using a combining function.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 27 / 29

Page 87: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Representation of Church Encodings

Representation of Church Encodings

What do the different Church Encodings represent?

Church Numerals: Apply the function f on z a total of n times.

Church Booleans: Selector functions (one-line if statement, etc.).

Church Pairs: Hard-coded Church Booleans.

Church Lists: Combine elements using a combining function.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 27 / 29

Page 88: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Representation of Church Encodings

Representation of Church Encodings

What do the different Church Encodings represent?

Church Numerals: Apply the function f on z a total of n times.

Church Booleans: Selector functions (one-line if statement, etc.).

Church Pairs: Hard-coded Church Booleans.

Church Lists: Combine elements using a combining function.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 27 / 29

Page 89: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Are Church Encodings practical?

Are Church Encodings practical?

Not really, as you’re keeping too many functions in memory . . .

But keep them in mind and try to use functions if you don’t needintermediary data.

OK, I lied: some optimizers, etc. internally use Church Encodings . . . butyou should really know what you’re doing!

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 28 / 29

Page 90: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Are Church Encodings practical?

Are Church Encodings practical?

Not really, as you’re keeping too many functions in memory . . .

But keep them in mind and try to use functions if you don’t needintermediary data.

OK, I lied: some optimizers, etc. internally use Church Encodings . . . butyou should really know what you’re doing!

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 28 / 29

Page 91: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Are Church Encodings practical?

Are Church Encodings practical?

Not really, as you’re keeping too many functions in memory . . .

But keep them in mind and try to use functions if you don’t needintermediary data.

OK, I lied: some optimizers, etc. internally use Church Encodings . . . butyou should really know what you’re doing!

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 28 / 29

Page 92: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Church Encoding Are Church Encodings practical?

Are Church Encodings practical?

Not really, as you’re keeping too many functions in memory . . .

But keep them in mind and try to use functions if you don’t needintermediary data.

OK, I lied: some optimizers, etc. internally use Church Encodings . . . butyou should really know what you’re doing!

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 28 / 29

Page 93: Ivan Lazar Miljenovic · 2009. 5. 14. · Turtles all the way down... From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once

Conclusion

Conclusion

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 29 / 29