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

programming language concepts lecture 16
SMART_READER_LITE
LIVE PREVIEW

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

Programming Language Concepts: Lecture 16 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 16, 23 March 2009 -calculus: syntax Assume a set Var of variables


slide-1
SLIDE 1

Programming Language Concepts: Lecture 16

Madhavan Mukund

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

PLC 2009, Lecture 16, 23 March 2009

slide-2
SLIDE 2

λ-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-3
SLIDE 3

λ-calculus: syntax

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

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

◮ 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-4
SLIDE 4

λ-calculus: syntax

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

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

◮ 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′

◮ When we apply β to MM′, assume that we always rename the

bound variables in M to avoid “capturing” free variables from M′.

slide-5
SLIDE 5

Encoding arithmetic

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

slide-6
SLIDE 6

Encoding arithmetic

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

◮ 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-7
SLIDE 7

Encoding arithmetic functions . . .

Successor

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

slide-8
SLIDE 8

Encoding arithmetic functions . . .

Successor

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

(λpfx.f (pfx))n

slide-9
SLIDE 9

Encoding arithmetic functions . . .

Successor

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

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

slide-10
SLIDE 10

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-11
SLIDE 11

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-12
SLIDE 12

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-13
SLIDE 13

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-14
SLIDE 14

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). (λpqfx.pf (qfx))mn

slide-15
SLIDE 15

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). (λpqfx.pf (qfx))mn →β (λqfx.mf (qfx))n

slide-16
SLIDE 16

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). (λpqfx.pf (qfx))mn →β (λqfx.mf (qfx))n →β (λfx.mf (nfx))

slide-17
SLIDE 17

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). (λpqfx.pf (qfx))mn →β (λqfx.mf (qfx))n →β (λfx.mf (nfx)) →β (λfx.mf (f nx))

slide-18
SLIDE 18

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). (λpqfx.pf (qfx))mn →β (λqfx.mf (qfx))n →β (λfx.mf (nfx)) →β (λfx.mf (f nx)) →β (λfx.f m(f nx))

slide-19
SLIDE 19

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). (λpqfx.pf (qfx))mn →β (λqfx.mf (qfx))n →β (λfx.mf (nfx)) →β (λfx.mf (f nx)) →β (λfx.f m(f nx)) = (λfx.f m+nx)

slide-20
SLIDE 20

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). (λpqfx.pf (qfx))mn →β (λqfx.mf (qfx))n →β (λfx.mf (nfx)) →β (λfx.mf (f nx)) →β (λfx.f m(f nx)) = (λfx.f m+nx) = m+n

slide-21
SLIDE 21

Encoding arithmetic functions . . .

◮ If functions are applied to meaningful terms we get

meaningful answers!

slide-22
SLIDE 22

Encoding arithmetic functions . . .

◮ If functions are applied to meaningful terms we get

meaningful answers! Other functions: multiplication : λpqfx.q(pf )x exponentiation : λpq.(pq)

slide-23
SLIDE 23

One step reduction

◮ Can have other reduction rules like β

slide-24
SLIDE 24

One step reduction

◮ Can have other reduction rules like β ◮ Observe that λx.(Mx) and M are equivalent with respect to

β-reduction

slide-25
SLIDE 25

One step reduction

◮ Can have other reduction rules like β ◮ Observe that λx.(Mx) and M are equivalent with respect to

β-reduction

◮ New reduction rule η

λx.(Mx) →η M

slide-26
SLIDE 26

One step reduction

◮ Can have other reduction rules like β ◮ Observe that λx.(Mx) and M are equivalent with respect to

β-reduction

◮ New reduction rule η

λx.(Mx) →η M

◮ Given basic rules β, η, . . . , we are allowed to use them “in

any context”

slide-27
SLIDE 27

One step reduction

◮ Can have other reduction rules like β ◮ Observe that λx.(Mx) and M are equivalent with respect to

β-reduction

◮ New reduction rule η

λx.(Mx) →η M

◮ Given basic rules β, η, . . . , we are allowed to use them “in

any context”

◮ Define a one step reduction relation → inductively

M →x M′ M → M

x ∈ {β, η, . . .}

slide-28
SLIDE 28

One step reduction

◮ Can have other reduction rules like β ◮ Observe that λx.(Mx) and M are equivalent with respect to

β-reduction

◮ New reduction rule η

λx.(Mx) →η M

◮ Given basic rules β, η, . . . , we are allowed to use them “in

any context”

◮ Define a one step reduction relation → inductively

M →x M′ M → M

x ∈ {β, η, . . .} M → M′ λx.M → λx.M′

slide-29
SLIDE 29

One step reduction

◮ Can have other reduction rules like β ◮ Observe that λx.(Mx) and M are equivalent with respect to

β-reduction

◮ New reduction rule η

λx.(Mx) →η M

◮ Given basic rules β, η, . . . , we are allowed to use them “in

any context”

◮ Define a one step reduction relation → inductively

M →x M′ M → M

x ∈ {β, η, . . .} M → M′ λx.M → λx.M′ M → M′ MN → M′N

slide-30
SLIDE 30

One step reduction

◮ Can have other reduction rules like β ◮ Observe that λx.(Mx) and M are equivalent with respect to

β-reduction

◮ New reduction rule η

λx.(Mx) →η M

◮ Given basic rules β, η, . . . , we are allowed to use them “in

any context”

◮ Define a one step reduction relation → inductively

M →x M′ M → M

x ∈ {β, η, . . .} M → M′ λx.M → λx.M′ M → M′ MN → M′N N → N′ MN → MN′

slide-31
SLIDE 31

Computability

◮ Church numerals encode n ∈ N

slide-32
SLIDE 32

Computability

◮ Church numerals encode n ∈ N ◮ Can we encode computable functions f : Nk → N?

◮ Let f be the encoding of computable function f

slide-33
SLIDE 33

Computability

◮ Church numerals encode n ∈ N ◮ Can we encode computable functions f : Nk → N?

◮ Let f be the encoding of computable function f ◮ Want f n1n2 . . . nk →∗ f (n1, n2, . . . , nk)

slide-34
SLIDE 34

Computability

◮ Church numerals encode n ∈ N ◮ Can we encode computable functions f : Nk → N?

◮ Let f be the encoding of computable function f ◮ Want f n1n2 . . . nk →∗ f (n1, n2, . . . , nk) ◮ Note! currying . . .

slide-35
SLIDE 35

Computability

◮ Church numerals encode n ∈ N ◮ Can we encode computable functions f : Nk → N?

◮ Let f be the encoding of computable function f ◮ Want f n1n2 . . . nk →∗ f (n1, n2, . . . , nk) ◮ Note! currying . . .

◮ We must first decide on a syntax for computable functions

slide-36
SLIDE 36

Recursive functions

Recursive functions [G¨

  • del]

◮ Equivalent to Turing machines, . . .

slide-37
SLIDE 37

Recursive functions

Recursive functions [G¨

  • del]

◮ Equivalent to Turing machines, . . .

Initial functions

◮ Zero: Z(n) = 0. ◮ Successor: S(n) = n+1. ◮ Projection: Πk i (n1, n2, . . . , nk) = ni

slide-38
SLIDE 38

Recursive functions

Recursive functions [G¨

  • del]

◮ Equivalent to Turing machines, . . .

Initial functions

◮ Zero: Z(n) = 0. ◮ Successor: S(n) = n+1. ◮ Projection: Πk i (n1, n2, . . . , nk) = ni

Composition Given f : Nk → N and g1, g2, . . . , gk : Nh → N, f ◦ (g1, g2, . . . , gk)(n1, n2, . . . , nh) = f (g1(n1, n2, . . . , nh), g2(n1, n2, . . . , nh), . . . , gk(n1, n2, . . . , nh))

slide-39
SLIDE 39

Recursive functions

Recursive functions [G¨

  • del]

◮ Equivalent to Turing machines, . . .

Initial functions

◮ Zero: Z(n) = 0. ◮ Successor: S(n) = n+1. ◮ Projection: Πk i (n1, n2, . . . , nk) = ni

Composition Given f : Nk → N and g1, g2, . . . , gk : Nh → N, f ◦ (g1, g2, . . . , gk)(n1, n2, . . . , nh) = f (g1(n1, n2, . . . , nh), g2(n1, n2, . . . , nh), . . . , gk(n1, n2, . . . , nh)) For instance, f (n) = n + 2 is S ◦ S

slide-40
SLIDE 40

Recursive functions . . .

Primitive recursion Given g : Nk → N and h : Nk+2 → N define f : Nk+1 → N by primitive recursion as follows: f (0, n1, n2, . . . , nk) = g(n1, n2, . . . , nk) f (n+1, n1, . . . , nk) = h(n, f (n, n1, n2, . . . , nk), n1, . . . , nk)

slide-41
SLIDE 41

Recursive functions . . .

Primitive recursion Given g : Nk → N and h : Nk+2 → N define f : Nk+1 → N by primitive recursion as follows: f (0, n1, n2, . . . , nk) = g(n1, n2, . . . , nk) f (n+1, n1, . . . , nk) = h(n, f (n, n1, n2, . . . , nk), n1, . . . , nk) Examples

◮ Define plus(n, m) = n+m from

g = Π1

1

h = S ◦ Π3

2

slide-42
SLIDE 42

Recursive functions . . .

Primitive recursion Given g : Nk → N and h : Nk+2 → N define f : Nk+1 → N by primitive recursion as follows: f (0, n1, n2, . . . , nk) = g(n1, n2, . . . , nk) f (n+1, n1, . . . , nk) = h(n, f (n, n1, n2, . . . , nk), n1, . . . , nk) Examples

◮ Define plus(n, m) = n+m from

g = Π1

1

h = S ◦ Π3

2

plus(0, n) = g(n) = Π1

1(n)

= n plus(m+1, n) = h(m, plus(m, n), n) = S ◦ Π3

2(m, plus(m, n), n)

= S(plus(m, n))

slide-43
SLIDE 43

Recursive functions . . .

Primitive recursion Given g : Nk → N and h : Nk+2 → N define f : Nk+1 → N by primitive recursion as follows: f (0, n1, n2, . . . , nk) = g(n1, n2, . . . , nk) f (n+1, n1, . . . , nk) = h(n, f (n, n1, n2, . . . , nk), n1, . . . , nk) Examples

◮ Define times(n, m) = n · m from

g = Z h = plus ◦ (Π3

3, Π3 2)

slide-44
SLIDE 44

Recursive functions . . .

Primitive recursion Given g : Nk → N and h : Nk+2 → N define f : Nk+1 → N by primitive recursion as follows: f (0, n1, n2, . . . , nk) = g(n1, n2, . . . , nk) f (n+1, n1, . . . , nk) = h(n, f (n, n1, n2, . . . , nk), n1, . . . , nk) Examples

◮ Define times(n, m) = n · m from

g = Z h = plus ◦ (Π3

3, Π3 2)

Note Primitive recursive functions are total!

slide-45
SLIDE 45

Recursive functions . . .

Minimalization Given g : Nk+1 → N, define f : Nk → N by minimalization from g f (n1, n2, . . . , nk) = µn.(g(n, n1, n2 . . . , nk) = 0) where µn.P(n) returns the least natural number n such that P(n) holds

slide-46
SLIDE 46

Recursive functions . . .

Minimalization Given g : Nk+1 → N, define f : Nk → N by minimalization from g f (n1, n2, . . . , nk) = µn.(g(n, n1, n2 . . . , nk) = 0) where µn.P(n) returns the least natural number n such that P(n) holds Equivalent to computing a while loop

n := 0; while (g(n,n1,n2,...,nk) != 0) {n := n+1}; return n;

slide-47
SLIDE 47

Recursive functions . . .

Minimalization Given g : Nk+1 → N, define f : Nk → N by minimalization from g f (n1, n2, . . . , nk) = µn.(g(n, n1, n2 . . . , nk) = 0) where µn.P(n) returns the least natural number n such that P(n) holds Equivalent to computing a while loop

n := 0; while (g(n,n1,n2,...,nk) != 0) {n := n+1}; return n;

Define log2 n as µn.(n − 2k)

◮ First k for which n − 2k = 0 is log2 n

slide-48
SLIDE 48

Recursive functions . . .

Minimalization Given g : Nk+1 → N, define f : Nk → N by minimalization from g f (n1, n2, . . . , nk) = µn.(g(n, n1, n2 . . . , nk) = 0) where µn.P(n) returns the least natural number n such that P(n) holds Equivalent to computing a while loop

n := 0; while (g(n,n1,n2,...,nk) != 0) {n := n+1}; return n;

Define log2 n as µn.(n − 2k)

◮ First k for which n − 2k = 0 is log2 n

Not defined for all n!

slide-49
SLIDE 49

Encoding recursive functions . . .

◮ n ≡ λfx.(f nx).

slide-50
SLIDE 50

Encoding recursive functions . . .

◮ n ≡ λfx.(f nx). ◮ Successor succ ≡ λnfx.(f (nfx)) such that

succn →∗ n + 1.

slide-51
SLIDE 51

Encoding recursive functions . . .

◮ n ≡ λfx.(f nx). ◮ Successor succ ≡ λnfx.(f (nfx)) such that

succn →∗ n + 1.

◮ Zero Z ≡ λx.(λgy.y).

slide-52
SLIDE 52

Encoding recursive functions . . .

◮ n ≡ λfx.(f nx). ◮ Successor succ ≡ λnfx.(f (nfx)) such that

succn →∗ n + 1.

◮ Zero Z ≡ λx.(λgy.y). ◮ Projection Πk i ≡ λx1x2 . . . xk.xi.

slide-53
SLIDE 53

Encoding recursive functions . . .

◮ n ≡ λfx.(f nx). ◮ Successor succ ≡ λnfx.(f (nfx)) such that

succn →∗ n + 1.

◮ Zero Z ≡ λx.(λgy.y). ◮ Projection Πk i ≡ λx1x2 . . . xk.xi.

Composition is easy

slide-54
SLIDE 54

Encoding recursive functions . . .

Primitive recursion

◮ Assume f (n+1) is defined in terms of g and h(n, f (n))

slide-55
SLIDE 55

Encoding recursive functions . . .

Primitive recursion

◮ Assume f (n+1) is defined in terms of g and h(n, f (n)) ◮ Convert recursion into iteration

Define t(n) = (n, f (n))

◮ Functions fst and snd extract first and second component of a

pair t(0) = (0, f (0)) = (0, g) t(n+1) = (n+1, f (n+1)) = (n+1, h(n, f (n))) = (succ(fst(t(n))), h(fst(t(n)), snd(t(n))))

◮ Clearly, f (n) = snd(t(n))

slide-56
SLIDE 56

Recursive functions . . .

Primitive Recursion

◮ We will evaluate t(n) bottom up

◮ Much like dynamic programming for recursive functions

slide-57
SLIDE 57

Recursive functions . . .

Primitive Recursion

◮ We will evaluate t(n) bottom up

◮ Much like dynamic programming for recursive functions

◮ Define a function step that does the following

step(n, f (n)) = (n+1, f (n+1))

slide-58
SLIDE 58

Recursive functions . . .

Primitive Recursion

◮ We will evaluate t(n) bottom up

◮ Much like dynamic programming for recursive functions

◮ Define a function step that does the following

step(n, f (n)) = (n+1, f (n+1))

◮ So, t(n) = stepn(0, f (0)) = stepn(0, g) . . .

slide-59
SLIDE 59

Recursive functions . . .

Primitive Recursion

◮ We will evaluate t(n) bottom up

◮ Much like dynamic programming for recursive functions

◮ Define a function step that does the following

step(n, f (n)) = (n+1, f (n+1))

◮ So, t(n) = stepn(0, f (0)) = stepn(0, g) . . . ◮ . . . and f (n) = snd(t(n)) = snd(stepn(0, g))

In the next class, we will provide a λ-calculus translation for step

◮ Will require constructions for building pairs and decomposing

them using fst and snd

slide-60
SLIDE 60

Recursive functions . . .

Minimalization

◮ To evaluate

f (n1, n2, . . . , nk) = µn.(g(n, n1, n2 . . . , nk) = 0) we go back to the idea of computing a while loop

n := 0; while (g(n,n1,n2,...,nk) != 0) {n := n+1}; return n;

◮ Implement the while loop using recursion

f(n1,n2,...,nk) = check(0,n1,n2...nk) where check(n,n1,n2...nk){ if (iszero(g(n,n1,n2,...,nk)) {return n;} else {check(n+1,n1,n2,...,nk);} }

◮ Need a mechanism to encode booleans, if-then-else in

λ-calculus