-Calculus Christine Rizkallah CSE, UNSW (and data61) Term3 2019 - - PowerPoint PPT Presentation

calculus christine rizkallah
SMART_READER_LITE
LIVE PREVIEW

-Calculus Christine Rizkallah CSE, UNSW (and data61) Term3 2019 - - PowerPoint PPT Presentation

-Calculus Church Encodings -Calculus Christine Rizkallah CSE, UNSW (and data61) Term3 2019 1 -Calculus Church Encodings -Calculus The term language we defined for Higher Order Abstract Syntax is almost a full featured programming


slide-1
SLIDE 1

λ-Calculus Church Encodings

λ-Calculus Christine Rizkallah

CSE, UNSW (and data61) Term3 2019

1

slide-2
SLIDE 2

λ-Calculus Church Encodings

λ-Calculus

The term language we defined for Higher Order Abstract Syntax is almost a full featured programming language. Just enrich the syntax slightly: t ::= Symbol | x (variables) | t1 t2 (application) | λx. t (λ-abstraction) There is just one rule to evaluate terms, called β-reduction: (λx. t) u →β t[x := u] Just as in Haskell, (λx. t) denotes a function that, given an argument for x, will return t.

2

slide-3
SLIDE 3

λ-Calculus Church Encodings

Syntax Concerns

Function application is left associative: f a b c = ((f a) b) c λ-abstraction extends as far as possible: λa. f a b = λa. (f a b) All functions are unary, like Haskell. Multiple argument functions are modelled with nested λ-abstractions: λx.λy. x + y

3

slide-4
SLIDE 4

λ-Calculus Church Encodings

β-reduction

β-reduction is a congruence: (λx. t) u →β t[x := u] t →β t′ s t →β s t′ s →β s′ s t →β s′ t t →β t′ λx. t →β λx. t′ This means we can pick any reducible subexpression (called a redex) and perform β-reduction.

4

slide-5
SLIDE 5

λ-Calculus Church Encodings

β-reduction

β-reduction is a congruence: (λx. t) u →β t[x := u] t →β t′ s t →β s t′ s →β s′ s t →β s′ t t →β t′ λx. t →β λx. t′ This means we can pick any reducible subexpression (called a redex) and perform β-reduction. Example: (λx. λy. f (y x)) 5 (λx. x)

5

slide-6
SLIDE 6

λ-Calculus Church Encodings

β-reduction

β-reduction is a congruence: (λx. t) u →β t[x := u] t →β t′ s t →β s t′ s →β s′ s t →β s′ t t →β t′ λx. t →β λx. t′ This means we can pick any reducible subexpression (called a redex) and perform β-reduction. Example: (λx. λy. f (y x)) 5 (λx. x) →β (λy. f (y 5)) (λx. x)

6

slide-7
SLIDE 7

λ-Calculus Church Encodings

β-reduction

β-reduction is a congruence: (λx. t) u →β t[x := u] t →β t′ s t →β s t′ s →β s′ s t →β s′ t t →β t′ λx. t →β λx. t′ This means we can pick any reducible subexpression (called a redex) and perform β-reduction. Example: (λx. λy. f (y x)) 5 (λx. x) →β (λy. f (y 5)) (λx. x) →β f ((λx. x) 5)

7

slide-8
SLIDE 8

λ-Calculus Church Encodings

β-reduction

β-reduction is a congruence: (λx. t) u →β t[x := u] t →β t′ s t →β s t′ s →β s′ s t →β s′ t t →β t′ λx. t →β λx. t′ This means we can pick any reducible subexpression (called a redex) and perform β-reduction. Example: (λx. λy. f (y x)) 5 (λx. x) →β (λy. f (y 5)) (λx. x) →β f ((λx. x) 5) →β f 5

8

slide-9
SLIDE 9

λ-Calculus Church Encodings

Confluence

Supposing we arrive via one reduction path to an expression that cannot be reduced further (called a normal form), then any other reduction path will result in the same normal form.

9

slide-10
SLIDE 10

λ-Calculus Church Encodings

Confluence

Supposing we arrive via one reduction path to an expression that cannot be reduced further (called a normal form), then any other reduction path will result in the same normal form. Therefore, the distinction between call by name and call by value does not affect our normal forms. (left: call by name, right: call by value)

slide-11
SLIDE 11

λ-Calculus Church Encodings

Confluence

Supposing we arrive via one reduction path to an expression that cannot be reduced further (called a normal form), then any other reduction path will result in the same normal form. (λa. a) ((λy. f y) 5) (λy. f y) 5 f 5 Therefore, the distinction between call by name and call by value does not affect our normal forms. (left: call by name, right: call by value)

slide-12
SLIDE 12

λ-Calculus Church Encodings

Confluence

Supposing we arrive via one reduction path to an expression that cannot be reduced further (called a normal form), then any other reduction path will result in the same normal form. (λa. a) ((λy. f y) 5) (λy. f y) 5 f 5 (λa. a) (f 5) Therefore, the distinction between call by name and call by value does not affect our normal forms.

12

slide-13
SLIDE 13

λ-Calculus Church Encodings

Confluence

Supposing we arrive via one reduction path to an expression that cannot be reduced further (called a normal form), then any other reduction path will result in the same normal form. (λa. a) ((λy. f y) 5) (λy. f y) 5 f 5 (λa. a) (f 5) Therefore, the distinction between call by name and call by value does not affect our normal forms. (left: call by name, right: call by value)

13

slide-14
SLIDE 14

λ-Calculus Church Encodings

Equivalence

Confluence means we can define another notion of equivalence, which equates more than α-equivalence. Two terms are αβ-equivalent, written s ≡αβ t if they β-reduce to α-equivalent normal forms.

14

slide-15
SLIDE 15

λ-Calculus Church Encodings

Equivalence

Confluence means we can define another notion of equivalence, which equates more than α-equivalence. Two terms are αβ-equivalent, written s ≡αβ t if they β-reduce to α-equivalent normal forms. η There is also another equation that cannot be proven from β-equivalence alone, called η-reduction: (λx. f x) →η f Adding this reduction to the system preserves confluence and uniqueness of normal forms, so we have a notion of αβη-equivalence also.

15

slide-16
SLIDE 16

λ-Calculus Church Encodings

Normal Forms

Does every term in λ-calculus have a normal form?

16

slide-17
SLIDE 17

λ-Calculus Church Encodings

Normal Forms

Does every term in λ-calculus have a normal form? (λx. x x)(λx. x x) Try to β-reduce this!

(the answer is that it doesn’t have a normal form)

17

slide-18
SLIDE 18

λ-Calculus Church Encodings

Why learn this stuff?

λ-calculus is a Turing-complete programming language. λ-calculus is the foundation for every functional programming language and some non-functional ones. λ-calculus is the foundation of Higher Order Logic and Type Theory, the two main foundations used for mathematics in interactive proof assistants. λ-calculus is the smallest example of a usable programming language, so it’s good for teaching about programming languages.

18

slide-19
SLIDE 19

λ-Calculus Church Encodings

Making λ-Calculus Usable

In order to demonstrate that λ calculus is actually a usable programming language, we will demonstrate how to encode booleans and natural numbers as λ-terms, along with their

  • perations.

General Idea We transform a data type into the type of its eliminator. In other words, we make a function that can serve the same purpose as the data type at its use sites.

19

slide-20
SLIDE 20

λ-Calculus Church Encodings

Booleans

How do we use booleans?

20

slide-21
SLIDE 21

λ-Calculus Church Encodings

Booleans

How do we use booleans? To choose between two results!

21

slide-22
SLIDE 22

λ-Calculus Church Encodings

Booleans

How do we use booleans? To choose between two results! So, a boolean will be a function that, given two arguments, returns the first one if it is true and the second one if it is false: True ≡ λa. λb. a False ≡ λa. λb. b How do we write an if statement?

22

slide-23
SLIDE 23

λ-Calculus Church Encodings

Booleans

How do we use booleans? To choose between two results! So, a boolean will be a function that, given two arguments, returns the first one if it is true and the second one if it is false: True ≡ λa. λb. a False ≡ λa. λb. b How do we write an if statement? If ≡ λc. λt. λe. c t e

23

slide-24
SLIDE 24

λ-Calculus Church Encodings

Booleans

How do we use booleans? To choose between two results! So, a boolean will be a function that, given two arguments, returns the first one if it is true and the second one if it is false: True ≡ λa. λb. a False ≡ λa. λb. b How do we write an if statement? If ≡ λc. λt. λe. c t e Example (Test it out!) Try β-normalising If True False True.

24

slide-25
SLIDE 25

λ-Calculus Church Encodings

Natural Numbers

How do we use natural numbers?

25

slide-26
SLIDE 26

λ-Calculus Church Encodings

Natural Numbers

How do we use natural numbers? To do something n times!

26

slide-27
SLIDE 27

λ-Calculus Church Encodings

Natural Numbers

How do we use natural numbers? To do something n times! So, a natural number will be a function that takes a function f and a value x, and applies the function f to x that number of times: Zero ≡ λf . λx. x One ≡ λf . λx. f x Two ≡ λf . λx. f (f x) . . . How do we write Suc?

27

slide-28
SLIDE 28

λ-Calculus Church Encodings

Natural Numbers

How do we use natural numbers? To do something n times! So, a natural number will be a function that takes a function f and a value x, and applies the function f to x that number of times: Zero ≡ λf . λx. x One ≡ λf . λx. f x Two ≡ λf . λx. f (f x) . . . How do we write Suc? Suc ≡ λn. λf . λx. f (n f x)

28

slide-29
SLIDE 29

λ-Calculus Church Encodings

Natural Numbers

How do we use natural numbers? To do something n times! So, a natural number will be a function that takes a function f and a value x, and applies the function f to x that number of times: Zero ≡ λf . λx. x One ≡ λf . λx. f x Two ≡ λf . λx. f (f x) . . . How do we write Suc? Suc ≡ λn. λf . λx. f (n f x) How do we write Add?

29

slide-30
SLIDE 30

λ-Calculus Church Encodings

Natural Numbers

How do we use natural numbers? To do something n times! So, a natural number will be a function that takes a function f and a value x, and applies the function f to x that number of times: Zero ≡ λf . λx. x One ≡ λf . λx. f x Two ≡ λf . λx. f (f x) . . . How do we write Suc? Suc ≡ λn. λf . λx. f (n f x) How do we write Add? Add ≡ λm.λn. λf . λx. m f (n f x)

30

slide-31
SLIDE 31

λ-Calculus Church Encodings

Natural Numbers

How do we use natural numbers? To do something n times! So, a natural number will be a function that takes a function f and a value x, and applies the function f to x that number of times: Zero ≡ λf . λx. x One ≡ λf . λx. f x Two ≡ λf . λx. f (f x) . . . How do we write Suc? Suc ≡ λn. λf . λx. f (n f x) How do we write Add? Add ≡ λm.λn. λf . λx. m f (n f x)

31

slide-32
SLIDE 32

λ-Calculus Church Encodings

Natural Number Practice

Example Try β-normalising Suc One. Example Try writing a different λ-term for defining Suc. Example Try writing a λ-term for defining Multiply.

32