Programming Language Concepts: Lecture 15 Madhavan Mukund Chennai - - PowerPoint PPT Presentation

programming language concepts lecture 15
SMART_READER_LITE
LIVE PREVIEW

Programming Language Concepts: Lecture 15 Madhavan Mukund Chennai - - PowerPoint PPT Presentation

Programming Language Concepts: Lecture 15 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 15, 18 March 2009 -calculus A notation for computable functions


slide-1
SLIDE 1

Programming Language Concepts: Lecture 15

Madhavan Mukund

Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009

PLC 2009, Lecture 15, 18 March 2009

slide-2
SLIDE 2

λ-calculus

◮ A notation for computable functions

◮ Alonzo Church

slide-3
SLIDE 3

λ-calculus

◮ A notation for computable functions

◮ Alonzo Church

◮ How do we describe a function?

◮ By its graph — a binary relation between domain and

codomain

◮ Single-valued ◮ Extensional — graph completely defines the function

slide-4
SLIDE 4

λ-calculus

◮ A notation for computable functions

◮ Alonzo Church

◮ How do we describe a function?

◮ By its graph — a binary relation between domain and

codomain

◮ Single-valued ◮ Extensional — graph completely defines the function

◮ An extensional definition is not suitable for computation

◮ All sorting functions are the same!

slide-5
SLIDE 5

λ-calculus

◮ A notation for computable functions

◮ Alonzo Church

◮ How do we describe a function?

◮ By its graph — a binary relation between domain and

codomain

◮ Single-valued ◮ Extensional — graph completely defines the function

◮ An extensional definition is not suitable for computation

◮ All sorting functions are the same!

◮ Need an intensional definition

◮ How are outputs computed from inputs?

slide-6
SLIDE 6

λ-calculus: syntax

◮ Assume a set Var of variables ◮ Set Λ of lambda expressions is given by

Λ = x | λx.M | MM′ where x ∈ Var, M, M′ ∈ Λ.

slide-7
SLIDE 7

λ-calculus: syntax

◮ Assume a set Var of variables ◮ Set Λ of lambda expressions is given by

Λ = x | λx.M | MM′ where x ∈ Var, M, M′ ∈ Λ.

◮ λx.M : Abstraction

◮ A function of x with computation rule M. ◮ “Abstracts” the computation rule M over arbitrary input

values x

◮ Like writing f (x) = e without assigning a name f

slide-8
SLIDE 8

λ-calculus: syntax

◮ Assume a set Var of variables ◮ Set Λ of lambda expressions is given by

Λ = x | λx.M | MM′ where x ∈ Var, M, M′ ∈ Λ.

◮ λx.M : Abstraction

◮ A function of x with computation rule M. ◮ “Abstracts” the computation rule M over arbitrary input

values x

◮ Like writing f (x) = e without assigning a name f

◮ MM′ : Application

◮ Apply the function M to the argument M′

slide-9
SLIDE 9

λ-calculus: syntax . . .

◮ Can write expressions such as xx — no types!

slide-10
SLIDE 10

λ-calculus: syntax . . .

◮ Can write expressions such as xx — no types! ◮ What can we do without types?

slide-11
SLIDE 11

λ-calculus: syntax . . .

◮ Can write expressions such as xx — no types! ◮ What can we do without types?

◮ Set theory as a basis for mathematics ◮ Bit strings in memory

slide-12
SLIDE 12

λ-calculus: syntax . . .

◮ Can write expressions such as xx — no types! ◮ What can we do without types?

◮ Set theory as a basis for mathematics ◮ Bit strings in memory

◮ In an untyped world, some data is meaningful

slide-13
SLIDE 13

λ-calculus: syntax . . .

◮ Can write expressions such as xx — no types! ◮ What can we do without types?

◮ Set theory as a basis for mathematics ◮ Bit strings in memory

◮ In an untyped world, some data is meaningful ◮ Functions manipulate meaningful data to yield meaningful

data

slide-14
SLIDE 14

λ-calculus: syntax . . .

◮ Can write expressions such as xx — no types! ◮ What can we do without types?

◮ Set theory as a basis for mathematics ◮ Bit strings in memory

◮ In an untyped world, some data is meaningful ◮ Functions manipulate meaningful data to yield meaningful

data

◮ Can also apply functions to non-meaningful data, but the

result has no significance

slide-15
SLIDE 15

The computation rule β

◮ Basic rule for computing (rewriting) is called β

(λx.M)M′ →β M{x ← M′}

◮ M{x ← M′} : substitute free occurrences of x in M by M′

slide-16
SLIDE 16

The computation rule β

◮ Basic rule for computing (rewriting) is called β

(λx.M)M′ →β M{x ← M′}

◮ M{x ← M′} : substitute free occurrences of x in M by M′ ◮ This is the normal rule we use for functions:

slide-17
SLIDE 17

The computation rule β

◮ Basic rule for computing (rewriting) is called β

(λx.M)M′ →β M{x ← M′}

◮ M{x ← M′} : substitute free occurrences of x in M by M′ ◮ This is the normal rule we use for functions:

f (x) = 2x2 + 3x + 4

slide-18
SLIDE 18

The computation rule β

◮ Basic rule for computing (rewriting) is called β

(λx.M)M′ →β M{x ← M′}

◮ M{x ← M′} : substitute free occurrences of x in M by M′ ◮ This is the normal rule we use for functions:

f (x) = 2x2 + 3x + 4 f (7) = 2 · 72 + 3 · 7 + 4 = (2x2 + 3x + 4){x ← 7}.

slide-19
SLIDE 19

The computation rule β

◮ Basic rule for computing (rewriting) is called β

(λx.M)M′ →β M{x ← M′}

◮ M{x ← M′} : substitute free occurrences of x in M by M′ ◮ This is the normal rule we use for functions:

f (x) = 2x2 + 3x + 4 f (7) = 2 · 72 + 3 · 7 + 4 = (2x2 + 3x + 4){x ← 7}.

◮ β is the only rule we need!

slide-20
SLIDE 20

The computation rule β

◮ Basic rule for computing (rewriting) is called β

(λx.M)M′ →β M{x ← M′}

◮ M{x ← M′} : substitute free occurrences of x in M by M′ ◮ This is the normal rule we use for functions:

f (x) = 2x2 + 3x + 4 f (7) = 2 · 72 + 3 · 7 + 4 = (2x2 + 3x + 4){x ← 7}.

◮ β is the only rule we need! ◮ MM′ is meaningful only if M is of the form λx.M′′

◮ Cannot do anything with expressions like xx

slide-21
SLIDE 21

Variable capture

◮ Consider (λx.(λy.xy))y

slide-22
SLIDE 22

Variable capture

◮ Consider (λx.(λy.xy))y ◮ β yields λy.yy

◮ The y substituted for inner x has been “confused” with the y

bound by λy

◮ Rename bound variables to avoid capture

(λx.(λy.xy))y = (λx.(λz.xz))y →β λz.yz

◮ Renaming bound variables does not change the function

◮ f (x) = 2x + 5 vs f (z) = 2z + 5

slide-23
SLIDE 23

Variable capture

Formally, bound and free variables are defined as

◮ FV (x) = {x}, for any variable x ◮ FV (λx.M) = FV (M) − {x} ◮ FV (MM′) = FV (M) ∪ FV (M′)

slide-24
SLIDE 24

Variable capture

Formally, bound and free variables are defined as

◮ FV (x) = {x}, for any variable x ◮ FV (λx.M) = FV (M) − {x} ◮ FV (MM′) = FV (M) ∪ FV (M′) ◮ BV (x) = ∅, for any variable x ◮ BV (λx.M) = BV (M) ∪ {x} ◮ BV (MM′) = BV (M) ∪ BV (M′)

slide-25
SLIDE 25

Variable capture

Formally, bound and free variables are defined as

◮ FV (x) = {x}, for any variable x ◮ FV (λx.M) = FV (M) − {x} ◮ FV (MM′) = FV (M) ∪ FV (M′) ◮ BV (x) = ∅, for any variable x ◮ BV (λx.M) = BV (M) ∪ {x} ◮ BV (MM′) = BV (M) ∪ BV (M′)

When we apply β to MM′, assume that we always rename the bound variables in M to avoid “capturing” free variables from M′.

slide-26
SLIDE 26

Encoding arithmetic

In set theory, use nesting depth to encode numbers

◮ Encoding of n: n ◮ n = {0, 1, . . . , n−1}

slide-27
SLIDE 27

Encoding arithmetic

In set theory, use nesting depth to encode numbers

◮ Encoding of n: n ◮ n = {0, 1, . . . , n−1}

Thus = ∅ 1 = {∅} 2 = {∅, {∅}} 3 = {∅, {∅}, {∅, {∅}}} . . .

slide-28
SLIDE 28

Encoding arithmetic

In set theory, use nesting depth to encode numbers

◮ Encoding of n: n ◮ n = {0, 1, . . . , n−1}

Thus = ∅ 1 = {∅} 2 = {∅, {∅}} 3 = {∅, {∅}, {∅, {∅}}} . . . In λ-calculus, encode n by number of times we apply a function

slide-29
SLIDE 29

Encoding arithmetic . . .

Church numerals = λfx.x n + 1 = λfx.f (nfx)

slide-30
SLIDE 30

Encoding arithmetic . . .

Church numerals = λfx.x n + 1 = λfx.f (nfx) For instance 1 = λfx.f (0fx) = λfx.(f ((λfx.x)fx))

slide-31
SLIDE 31

Encoding arithmetic . . .

Church numerals = λfx.x n + 1 = λfx.f (nfx) For instance 1 = λfx.f (0fx) = λfx.(f ((λfx.x)fx)) Note that 0gy →β (λx.x)y →β y. Hence 1 = . . . = λfx.(f ((λfx.x)fx

  • apply β

)) →β λfx.(fx) So 1gy →β (λx.(gx))y →β gy

slide-32
SLIDE 32

Church numerals . . .

2 = λfx.f (1fx) = λfx.(f (λfx.(fx)fx)

  • apply β

) →β λfx.(f (fx)) so, 2gy →β λx.(g(gx))y = g(gy)

slide-33
SLIDE 33

Church numerals . . .

2 = λfx.f (1fx) = λfx.(f (λfx.(fx)fx)

  • apply β

) →β λfx.(f (fx)) so, 2gy →β λx.(g(gx))y = g(gy)

◮ Let gky denote g(g(. . . (gy))) with k applications of g to y ◮ Show by induction that

n = λfx.f (n−1fx) →β . . . →β λfx.(f nx)

slide-34
SLIDE 34

Encoding arithmetic functions . . .

Successor

◮ succ(n) = n + 1 ◮ Define as λpfx.f (pfx)

slide-35
SLIDE 35

Encoding arithmetic functions . . .

Successor

◮ succ(n) = n + 1 ◮ Define as λpfx.f (pfx)

(λpfx.f (pfx))n

slide-36
SLIDE 36

Encoding arithmetic functions . . .

Successor

◮ succ(n) = n + 1 ◮ Define as λpfx.f (pfx)

(λpfx.f (pfx))n →β λfx.f (nfx)

slide-37
SLIDE 37

Encoding arithmetic functions . . .

Successor

◮ succ(n) = n + 1 ◮ Define as λpfx.f (pfx)

(λpfx.f (pfx))n →β λfx.f (nfx) →β λfx.f (f nx)

slide-38
SLIDE 38

Encoding arithmetic functions . . .

Successor

◮ succ(n) = n + 1 ◮ Define as λpfx.f (pfx)

(λpfx.f (pfx))n →β λfx.f (nfx) →β λfx.f (f nx) = λfx.f n+1x

slide-39
SLIDE 39

Encoding arithmetic functions . . .

Successor

◮ succ(n) = n + 1 ◮ Define as λpfx.f (pfx)

(λpfx.f (pfx))n →β λfx.f (nfx) →β λfx.f (f nx) = λfx.f n+1x = n+1

slide-40
SLIDE 40

Encoding arithmetic functions . . .

Successor

◮ succ(n) = n + 1 ◮ Define as λpfx.f (pfx)

(λpfx.f (pfx))n →β λfx.f (nfx) →β λfx.f (f nx) = λfx.f n+1x = n+1 plus: λpqfx.pf (qfx).

slide-41
SLIDE 41

Encoding arithmetic functions . . .

Successor

◮ succ(n) = n + 1 ◮ Define as λpfx.f (pfx)

(λpfx.f (pfx))n →β λfx.f (nfx) →β λfx.f (f nx) = λfx.f n+1x = n+1 plus: λpqfx.pf (qfx).