Functions all the way down! 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 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 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 From Stephen Hawking’s A Brief History of Time A well-known scientist (some say it was Bertrand Russell) once gave a public lecture on astronomy. He described how the earth orbits around the sun and how the sun, in turn, orbits around the center of a vast collection of stars called our galaxy. At the end of the lecture, a little old lady at the back of the room got up and said: “What you have told us is rubbish. The world is really a flat plate supported on the back of a giant tortoise.” The scientist gave a superior smile before replying, “What is the tortoise standing on?” “You’re very clever, young man, very clever,” said the old lady. “But it’s turtles all the way down!”

Introduction Turtles all the way down... Turtles all the way down...

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 4 / 29 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 a public lecture on astronomy. He described how the earth orbits around the sun and how the sun, in turn, orbits around the center of a vast collection of stars called our galaxy. At the end of the lecture, a little old lady at the back of the room got up and said: “What you have told us is rubbish. The world is really a flat plate supported on the back of a giant tortoise.” The scientist gave a superior smile before replying, “What is the tortoise standing on?” “You’re very clever, young man, very clever,” said the old lady. “But it’s turtles all the way down!”

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 4 / 29 Functional languages Functions are “first class citizens” (Cristopher Strachey, mid-1960s). LISP family Everything is data; all data is code.

What about replacing data with functions?

Introduction Programming languages and Functions Programming languages and Functions

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

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 5 / 29 LISP family Everything is data; all data is code.

What about replacing data with functions?

Introduction Programming languages and Functions Programming languages and Functions

Most “standard” programming languages There is data, and then functions that act on that data. Functional languages Functions are “first class citizens” (Cristopher Strachey, mid-1960s).

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 5 / 29 What about replacing data with functions?

Introduction Programming languages and Functions Programming languages and Functions

Most “standard” programming languages There is data, and then functions that act on that data. Functional languages Functions are “first class citizens” (Cristopher Strachey, mid-1960s). LISP family Everything is data; all data is code.

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

Most “standard” programming languages There is data, and then functions that act on that data. Functional languages Functions are “first class citizens” (Cristopher Strachey, 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 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 Theory of Computing Theory of Computing

2 Theory of Computing The Solutions to the Entscheidungsproblem

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 7 / 29 Definition (The Entscheidungsproblem) Given a description of a formal language and a mathematical statement in that language, determine if the statement is true or false.

Original Statement Generalisation of his question “Is decidable”.

Theory of Computing The Entscheidungsproblem The Entscheidungsproblem The decidability problem

Posed by David Hilbert in 1928:

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 8 / 29 Original Statement Generalisation of his question “Is mathematics decidable”.

Theory of Computing The Entscheidungsproblem The Entscheidungsproblem The decidability problem

Posed by David Hilbert in 1928:

Definition (The Entscheidungsproblem) Given a description of a formal language and a mathematical statement in that language, determine if the statement is true or false.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 8 / 29 Theory of Computing The Entscheidungsproblem The Entscheidungsproblem The decidability problem

Posed by David Hilbert in 1928:

Definition (The Entscheidungsproblem) Given a description of a formal language and a mathematical statement in that 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 Every effectively calculable function is a computable function. λ-Calculus, Turing machines, etc. are equivalent.

15 April, 1936

An Unsolvable Problem of Elementary Number Theory by Alonzo Church

Theorem (Church-Turing Thesis)

Theory of Computing Solutions to the Entscheidungsproblem Solutions to the Entscheidungsproblem The Halting Problem

January, 1937

On Computable Numbers, with an Application to the Entscheidungsproblem by Alan Turing

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 9 / 29 Every effectively calculable function is a computable function. λ-Calculus, Turing machines, etc. are equivalent.

January, 1937

On Computable Numbers, with an Application to the Entscheidungsproblem by Alan Turing

Theorem (Church-Turing Thesis)

Theory of Computing Solutions to the Entscheidungsproblem Solutions to the Entscheidungsproblem The Halting Problem

15 April, 1936

An Unsolvable Problem of Elementary Number Theory by Alonzo Church

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 9 / 29 Every effectively calculable function is a computable function. λ-Calculus, Turing machines, etc. are equivalent.

Theory of Computing Solutions to the Entscheidungsproblem Solutions to the Entscheidungsproblem The Halting Problem

15 April, 1936

An Unsolvable Problem of Elementary Number Theory by Alonzo Church

January, 1937

On Computable Numbers, with an Application to the Entscheidungsproblem by Alan Turing

Theorem (Church-Turing Thesis)

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 9 / 29 λ-Calculus, Turing machines, etc. are equivalent.

Theory of Computing Solutions to the Entscheidungsproblem Solutions to the Entscheidungsproblem The Halting Problem

15 April, 1936

An Unsolvable Problem of Elementary Number Theory by Alonzo Church

January, 1937

On Computable Numbers, with an Application to the Entscheidungsproblem by Alan Turing

Theorem (Church-Turing Thesis) Every effectively calculable function is a computable function.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 9 / 29 Theory of Computing Solutions to the Entscheidungsproblem Solutions to the Entscheidungsproblem The Halting Problem

15 April, 1936

An Unsolvable Problem of Elementary Number Theory by Alonzo Church

January, 1937

On Computable Numbers, with an Application to the 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 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 “. . . a formal system invented in the 1920s by Alonzo Church, in which all computation is reduced to the basic operations of function definition and application.” “Its importance arises from the fact that it can be viewed simultaneously as a simple programming language in which computations can be described and as a mathematical object about which rigorous statements can be proved.”

The λ-Calculus What is the λ-Calculus? What is the λ-Calculus?

Types and Programming Languages

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 11 / 29 “Its importance arises from the fact that it can be viewed simultaneously as a simple programming language in which computations can be described and as a mathematical object about which rigorous statements can be proved.”

The λ-Calculus What is the λ-Calculus? What is the λ-Calculus?

Types and Programming Languages “. . . a formal system invented in the 1920s by Alonzo Church, in which all computation is reduced to the basic operations of function definition and application.”

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 11 / 29 The λ-Calculus What is the λ-Calculus? What is the λ-Calculus?

Types and Programming Languages “. . . a formal system invented in the 1920s by Alonzo Church, in which all computation is reduced to the basic operations of function definition and application.” “Its importance arises from the fact that it can be viewed simultaneously as a simple programming language in which computations can be described and as a mathematical object about which rigorous statements can be proved.”

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 11 / 29 1936 Alonzo Church also invents every language that will ever be but does it better. His lambda calculus is ignored because it is insufficiently C-like. This criticism occurs in spite of the fact that C has not yet been invented. 1970 . . . Lambdas are relegated to relative obscurity until Java makes them popular by not having them.

The λ-Calculus What is the λ-Calculus? What is the λ-Calculus?

A Brief, Incomplete, and Mostly Wrong History of Programming Languages

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 12 / 29 1970 . . . Lambdas are relegated to relative obscurity until Java makes them popular by not having them.

The λ-Calculus What is the λ-Calculus? What is the λ-Calculus?

A Brief, Incomplete, and Mostly Wrong History of Programming Languages 1936 Alonzo Church also invents every language that will ever be but does it better. His lambda calculus is ignored because it is insufficiently C-like. This criticism occurs in spite of the fact that C has not yet been invented.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 12 / 29 The λ-Calculus What is the λ-Calculus? What is the λ-Calculus?

A Brief, Incomplete, and Mostly Wrong History of Programming Languages 1936 Alonzo Church also invents every language that will ever be but does it better. His lambda calculus is ignored because it is insufficiently C-like. This criticism occurs in spite of the fact that C has not yet been invented. 1970 . . . Lambdas are relegated to relative obscurity until Java makes them popular by not having them.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 12 / 29 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.

The λ-Calculus Definition of the λ-Calculus Definition of the λ-Calculus

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

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 13 / 29 λ 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.

The λ-Calculus Definition of the λ-Calculus Definition of the λ-Calculus

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

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 13 / 29 t t The application of one term to another. Also use parentheses for grouping.

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.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 13 / 29 Also use parentheses for grouping.

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.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 13 / 29 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 Anonymous Unary Higher-order Recursive Left-associative: abc = (ab)c Extend as far right as possible

The λ-Calculus Definition of the λ-Calculus Definition of the λ-Calculus

Functions are:

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 14 / 29 Unary Higher-order Recursive Left-associative: abc = (ab)c Extend as far right as possible

The λ-Calculus Definition of the λ-Calculus Definition of the λ-Calculus

Functions are: Anonymous

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 14 / 29 Higher-order Recursive Left-associative: abc = (ab)c Extend as far right as possible

The λ-Calculus Definition of the λ-Calculus Definition of the λ-Calculus

Functions are: Anonymous Unary

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 14 / 29 Recursive Left-associative: abc = (ab)c Extend as far right as possible

The λ-Calculus Definition of the λ-Calculus Definition of the λ-Calculus

Functions are: Anonymous Unary Higher-order

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 14 / 29 Left-associative: abc = (ab)c Extend as far right as possible

The λ-Calculus Definition of the λ-Calculus Definition of the λ-Calculus

Functions are: Anonymous Unary Higher-order Recursive

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 14 / 29 Extend as far right as possible

The λ-Calculus Definition of the λ-Calculus Definition of the λ-Calculus

Functions are: Anonymous Unary Higher-order Recursive Left-associative: abc = (ab)c

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 14 / 29 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 Identity function: λ x. x Mathematics: (λ x. x + 2) 3 Multiple arguments: λ x. λ y. x + y Factorials: ( 1 n = 0 g = λ f . λ n. n × f (n − 1) n > 0 Y = λ h. (λ x. h (x x)) (λ x. h (x x)) fact = λ n. Y g n

The λ-Calculus Informal Examples Informal Examples

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 15 / 29 Mathematics: (λ x. x + 2) 3 Multiple arguments: λ x. λ y. x + y Factorials: ( 1 n = 0 g = λ f . λ n. n × f (n − 1) n > 0 Y = λ h. (λ x. h (x x)) (λ x. h (x x)) fact = λ n. Y g n

The λ-Calculus Informal Examples Informal Examples

Identity function: λ x. x

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 15 / 29 Multiple arguments: λ x. λ y. x + y Factorials: ( 1 n = 0 g = λ f . λ n. n × f (n − 1) n > 0 Y = λ h. (λ x. h (x x)) (λ x. h (x x)) fact = λ n. Y g n

The λ-Calculus Informal Examples Informal Examples

Identity function: λ x. x Mathematics: (λ x. x + 2) 3

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 15 / 29 Factorials: ( 1 n = 0 g = λ f . λ n. n × f (n − 1) n > 0 Y = λ h. (λ x. h (x x)) (λ x. h (x x)) fact = λ n. Y g n

The λ-Calculus Informal Examples Informal Examples

Identity function: λ x. x Mathematics: (λ x. x + 2) 3 Multiple arguments: λ x. λ y. x + y

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 15 / 29 The λ-Calculus Informal Examples Informal Examples

Identity function: λ x. x Mathematics: (λ x. x + 2) 3 Multiple arguments: λ x. λ y. x + y Factorials: ( 1 n = 0 g = λ f . λ n. 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 Functional Languages Based on the λ-Calculus, treat computation as the evaluation of mathematical functions and typically avoids state and mutable data. LISP family Lisp is the second oldest programming language after FORTRAN; loosely based on the λ-Calculus but technically classed as multi-paradigm. Anonymous Functions Found in many other languages: C#, C++Ox, Matlab, etc.

The λ-Calculus Usage of the λ-Calculus Usage of the λ-Calculus

Usage of the λ-Calculus in programming languages:

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 16 / 29 LISP family Lisp is the second oldest programming language after FORTRAN; loosely based on the λ-Calculus but technically classed as multi-paradigm. Anonymous Functions Found in many other languages: C#, C++Ox, Matlab, etc.

The λ-Calculus Usage of the λ-Calculus Usage of the λ-Calculus

Usage of the λ-Calculus in programming languages: Functional Languages Based on the λ-Calculus, treat computation as the evaluation of mathematical functions and typically avoids state and mutable data.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 16 / 29 Anonymous Functions Found in many other languages: C#, C++Ox, Matlab, etc.

The λ-Calculus Usage of the λ-Calculus Usage of the λ-Calculus

Usage of the λ-Calculus in programming languages: Functional Languages Based on the λ-Calculus, treat computation as the evaluation of mathematical functions and typically avoids state and mutable data. LISP family Lisp is the second oldest programming language after FORTRAN; loosely based on the λ-Calculus but technically classed as multi-paradigm.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 16 / 29 The λ-Calculus Usage of the λ-Calculus Usage of the λ-Calculus

Usage of the λ-Calculus in programming languages: Functional Languages Based on the λ-Calculus, treat computation as the evaluation of mathematical functions and typically avoids state and mutable data. LISP family Lisp is the second oldest programming language after FORTRAN; loosely based on the λ-Calculus but technically classed 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 Church Encoding Church Encoding

4 Church Encoding What is Church Encoding? Church Numerals Peano Isomorphism 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 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

Church Encoding What is Church Encoding? What is Church Encoding?

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 18 / 29 Encodings are available for: Natural numbers Booleans Pairs Lists

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.

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

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:

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

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

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

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

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

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

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 18 / 29 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 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.

Church Encoding Church Numerals Church Numerals

Devised by Alonzo Church in 1941 to represent natural numbers:

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 19 / 29 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 Church Numerals

0 ≡ λ f . id 1 ≡ λ f . f 2 ≡ λ f . f · f 3 ≡ λ f . f · f · f ... n ≡ λ f . f n

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

Church Encoding Church Numerals Church Numerals

Alternatively:

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

Alternatively: Church Numerals

0 ≡ λ f . id 1 ≡ λ f . f 2 ≡ λ f . f · f 3 ≡ λ 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 : succ n = λ f . f · n f : plus m n = λ f . m f · n f : mult m n = m · n : pow m n = n m

Church Encoding Church Numerals Church Numerals

Operations on Church Numerals (due to Rosser):

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 20 / 29 Addition: plus m n = λ f . m f · n f Multiplication: mult m n = m · n Exponentiation: pow m n = n m

Church Encoding Church Numerals Church Numerals

Operations on Church Numerals (due to Rosser): Successor function: succ n = λ f . f · n f

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 20 / 29 Multiplication: mult m n = m · n Exponentiation: pow m n = n m

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

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 20 / 29 Exponentiation: pow m n = n m

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

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 20 / 29 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 Definition (Peano Numbers)

data Nat = Zero | Succ Nat

one :: Nat one = 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 → a fold succ zero Zero = zero fold succ zero (Succ n) = succ (fold succ zero n)

Church Encoding Peano Arithmetic Peano Arithmetic

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

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 21 / 29 We can operate on Peano numbers using what is known as a fold function: Definition (Fold on Peano Numbers)

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

Church Encoding Peano Arithmetic Peano Arithmetic

We can derive the Church Numerals from the unary representation of the natural numbers, also known as the Peano numeral system. Definition (Peano Numbers)

data Nat = Zero | Succ Nat

one :: Nat one = Succ Zero

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

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

Church Encoding Peano Arithmetic Peano Arithmetic

We can derive the Church Numerals from the unary representation of the natural numbers, also known as the Peano numeral system. Definition (Peano Numbers)

data Nat = Zero | Succ Nat

one :: Nat one = Succ Zero

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

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

We can derive the Church Numerals from the unary representation of the natural numbers, also known as the Peano numeral system. Definition (Peano Numbers)

data Nat = Zero | Succ Nat

one :: Nat one = 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 → a fold succ zero Zero = zero fold succ zero (Succ n) = succ (fold succ zero n)

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 21 / 29 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 Converting between the two

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

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

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

Church Encoding Peano Arithmetic Peano Arithmetic Isomorphism between Church and Peano

We can make an isormorphism between Church Numerals and Peano Numbers:

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 23 / 29 Church Encoding Peano Arithmetic Peano Arithmetic Isomorphism between Church and Peano

We can make an isormorphism between Church Numerals and Peano Numbers: Converting between the two

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

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

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

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 23 / 29 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!

Church Encoding Peano Arithmetic Peano Arithmetic Isomorphism between Church and Peano

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

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 24 / 29 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!

Church Encoding Peano Arithmetic Peano Arithmetic Isomorphism 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)

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 24 / 29 Multiplication: mult m n = m (n+) 0 Exponentiation: pow m n = n (m×) 1 Completely different formulation from before!

Church Encoding Peano Arithmetic Peano Arithmetic Isomorphism 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

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 24 / 29 Exponentiation: pow m n = n (m×) 1 Completely different formulation from before!

Church Encoding Peano Arithmetic Peano Arithmetic Isomorphism 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

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

Church Encoding Peano Arithmetic Peano Arithmetic Isomorphism 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

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 24 / 29 Church Encoding Peano Arithmetic Peano Arithmetic Isomorphism 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 . . . but they get very messy very quickly.

Church Encoding Other operations on Church Numerals Other operations on Church Numerals

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

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 25 / 29 Church Encoding Other operations on Church Numerals Other operations on Church Numerals

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

. . . but they get very messy very quickly.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 25 / 29 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.

Church Encoding Other Church Encodings Other Church Encodings

Other Church Encodings of interest:

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

pair ≡ λ f . λ s. λ b. b f s fst ≡ λ p. p true snd ≡ λ p. p false

Church Lists: Too complicated to define here.

Church Encoding Other Church Encodings Other Church Encodings

Other Church Encodings of interest: Church Booleans:

true ≡ λ t. λ f . t false ≡ λ t. λ f . f

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 26 / 29 Church Lists: Too complicated to define here.

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

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 26 / 29 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 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.

Church Encoding Representation of Church Encodings Representation of Church Encodings

What do the different Church Encodings represent?

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 27 / 29 Church Booleans: Selector functions (one-line if statement, etc.). Church Pairs: Hard-coded Church Booleans. Church Lists: Combine elements using a combining function.

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.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 27 / 29 Church Pairs: Hard-coded Church Booleans. Church Lists: Combine elements using a combining function.

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

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 27 / 29 Church Lists: Combine elements using a combining function.

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.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 27 / 29 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 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 need intermediary data.

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

Church Encoding Are Church Encodings practical? Are Church Encodings practical?

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 28 / 29 But keep them in mind and try to use functions if you don’t need intermediary data.

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

Church Encoding Are Church Encodings practical? Are Church Encodings practical?

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

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 28 / 29 OK, I lied: some optimizers, etc. internally use Church Encodings . . . but you should really know what you’re doing!

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 need intermediary data.

Ivan Lazar Miljenovic (Journal Club) Functions all the way down! 14 May, 2009 28 / 29 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 need intermediary data.

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

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

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