Lecture 2: Self-interpretation in the Lambda-calculus H. Geuvers - - PowerPoint PPT Presentation

lecture 2 self interpretation in the lambda calculus
SMART_READER_LITE
LIVE PREVIEW

Lecture 2: Self-interpretation in the Lambda-calculus H. Geuvers - - PowerPoint PPT Presentation

Self-interpretation in the Lambda Calculus Radboud University The limitations of Self-interpretation Lecture 2: Self-interpretation in the Lambda-calculus H. Geuvers Radboud University Nijmegen, NL 21st Estonian Winter School in Computer


slide-1
SLIDE 1

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Lecture 2: Self-interpretation in the Lambda-calculus

  • H. Geuvers

Radboud University Nijmegen, NL

21st Estonian Winter School in Computer Science Winter 2016

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 1 / 31

slide-2
SLIDE 2

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Outline

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 2 / 31

slide-3
SLIDE 3

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

More on data types

A data type D is a set with some operations (functions) on it. An k-ary operation is a function f : Dk → D. Thereby a 0-ary operation c : D0 → D is identified with a c ∈ D. A datatype is determined by its operations on D: c1, . . . , ck0 : D0 → D = D f 1

1 , . . . , f 1 k1

: D1 → D f 2

1 , . . . , f 2 k2

: D2 → D . . . Nat has z : Nat, s : Nat → Nat. Tree has l : Tree, j : Tree2 → Tree

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 4 / 31

slide-4
SLIDE 4

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Defining functions on data types in the lambda-calculus

F λ-defines the function f : N → N if f (n) = F n for all n:

N N Λ Λ

  • f
  • F

Can we enode the λ-calculus in itself? F λ-defines the function f : Λ → Λ if f (M) = F M for all M ∈ Λ.

Λ Λ Λ Λ

  • f
  • F

Is just the identity? Why is this useful?

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 5 / 31

slide-5
SLIDE 5

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Defining functions on codes of lambda-terms

Some functions f : Λ → Λ can only be defined on codes of terms, not on terms. So we will need to talk about codes. Λ Λ Λ Λ

  • f
  • F
  • Example. There is no λ-term F such that

F(M N) = M for all M, N

  • There is a λ-term F such that F M N = M for all M, N.

(With a suitable encoding − .)

  • We also have an evaluator E:

E M = M

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 6 / 31

slide-6
SLIDE 6

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Packing and unpacking λ-terms

Given M1, . . . , Mk, define M1, . . . , Mk := λz.z M1 . . . Mk Define Uk

i , with 1 ≤ i ≤ k by

Uk

i

:= λx1 . . . xk.xi Then Uk

i M1 . . . Mk

= Mi M1, . . . , MkUk

i

= Uk

i M1 . . . Mk = Mi

Note that K = U2

1

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 7 / 31

slide-7
SLIDE 7

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Second encoding of data types (B¨

  • hm-Piperno-Guerini)

Consider the data type D with c : D, f : D → D, g : D2 → D The B¨

  • hm-Piperno-Guerini coding (also denoted by t ) is

c = λe.e U3

1 e

f (t) = λe.e U3

2 t e

g(t1, t2) = λe.e U3

3 t1

t2 e

  • Proposition. The constructors (c, f , g) can be λ-defined:

There are lambda terms F, G such that F t = f (t) G t1 t2 = g(t1, t2)

  • Proof. Take

F := λx e.e U3

2 x e

G := λx y e.e U3

3 x y e.

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 8 / 31

slide-8
SLIDE 8

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Recursion

  • Theorem. Given A1, A2, A3 ∈ Λ there is an H ∈ Λ such that

H c = A1H H( f (t) ) = A2 t H H( g(t1, t2) ) = A3 t1 t2 H

  • Proof. Try H = B1, B2, B3.

H c = B1, B2, B3 c = c B1, B2, B3 = B1, B2, B3U3

1B1, B2, B3

= B1B1, B2, B3 = A1B1, B2, B3 if B1 := λz.A1z = A1H H f (t) = B1, B2, B3U3

2 t B1, B2, B3

= B2 t B1, B2, B3 = A2 t H if B2 := λx z.A2xz H g(t1, t2) = A3 t1 t2 H if B3 := λx y z.A3xyz.

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 9 / 31

slide-9
SLIDE 9

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Data type for coding lambda terms

To encode λ-terms we consider the data type D with var : D → D app : D → D → D abs : D → D

  • This data types is a bit strange: there is no base case.
  • It is a priori unclear how to encode the λ-terms in this data
  • type. How to encode the variable binding? (Later slide.)

Like before, we can define the constructors Var, App, Abs: Var := λx e.e U3

1 x e

App := λx y e.e U3

2 x y e

Abs := λx e.e U3

3 x e

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 10 / 31

slide-10
SLIDE 10

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Recursion for the lambda terms data type

Like before, we have a recursion theorem: Theorem I. Given A1, A2, A3 ∈ Λ there is an H ∈ Λ such that H(Var x) = A1 x H H(App x y) = A2 x y H H(Abs x) = A3 x H

  • Proof. Take H = B1, B2, B3 with

B1 := λx z.A1xz B2 := λx y z.A2xyz B3 := λx z.A3xz. Then the equations hold indeed. . (Exercise: Check this.)

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 11 / 31

slide-11
SLIDE 11

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Coding of lambda terms

Coding lambda terms M M Definition (Mogensen) The coding of λ-terms inside the λ-calculus is defined as follows. x := Var x MN := App M N λx.M := Abs (λx. M ) A variable x is encoded using x itself and abstraction “λx.” is encoded using the same abstraction “λx.”. Note: coding is not λ-definable: there is no term C such that C M = M for all M. The reverse operation, evaluation, is λ-definable.

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 12 / 31

slide-12
SLIDE 12

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Self-interpretation

  • Theorem. There exists a λ-term E (evaluator) such that for all

M ∈ Λ E M = M

  • Proof. By recursion we can find an E such that

E(Var x) = x E(App x y) = E x (E y) E(Abs x) = λz.E (x z) Then E( x ) = E(Var x) = x E( MN ) = E(App M N ) = E M (E N ) = MN E( λx.M ) = E(Abs(λx. M )) = λx.E M = λx.M. Filling in the details of E one has (writing C := λx y z.x z y) E = K, S, C.

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 13 / 31

slide-13
SLIDE 13

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Why is this encoding so cool?

There are many encoding of Λ in itself. Why is this one so nice?

  • Older ones all go through a coding of λ-terms as numbers:

− : Λ → N → Λ. This one is direct, uses λ for λ-abstraction.

  • E M ։ M, also for terms with free variables.
  • E = K, S, C, the initials of S.C. Kleene, one of the

founders of the subject!

  • M1 ≡α M2 ⇒ M1 ≡α M2 .
  • M1¬ ≡α M2 ⇒ M1 =β M2 .
  • One can define a term R with R M ։ N , if M has N as

normal form.

  • There is a Second Fixed Point Theorem (later)
  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 14 / 31

slide-14
SLIDE 14

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Recursion for lambda terms using the encoding

We can state the recursion theorem for the encoded lambda terms slightly differently, as follows. Theorem II. Given A1, A2, A3 ∈ Λ there is an H ∈ Λ such that H x = A1 x H H M N = A2 M N H H λx.M = A3 (λx. M ) H

  • Proof. According to Theorem I, there is an H satisfying

H(Var x) = A1 x H H(App x y) = A2 x y H H(Abs x) = A3 x H These equations immediately imply the ones of the statement of Theorem II (check this!), so the same H suffices. .

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 15 / 31

slide-15
SLIDE 15

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Application 1

If you see someone coming out of ‘arrivals’ in an airport, you cannot determine where he/she comes from. Similarly, there is no F such that for all X, Y ∈ Λ F(X Y ) = X (Given the outcome of applying a function on an argument, there is no way we can determine the function that produced this

  • utcome.)
  • Proposition. There exists an Fi ∈ Λ, i ∈ {1, 2} such that

Fi X1 X2 = Xi .

  • Proof. We do this for i = 1. By the Recursion Theorem II, there

exists F1 s.t. F1( X1 X2 ) = A2 X1 X2 F1 = X1 , taking A2 = U3

1.

This suffices.

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 16 / 31

slide-16
SLIDE 16

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Second fixed point theorem∗

  • Lemma. There exists a term Num ∈ Λ such that for all M ∈ Λ

Num M =β M

  • Proof. Use recursion (Theorem I) for the lambda calculus data

type with

A1 x N = App Var (Var x) A2 m n N = App (App App (N m))(N n) A3 m N = App Abs (Abs (λx.N(m x)))

Second fixed point theorem. For all F there is an X with F X =β X

  • Proof. Let W := λz.F(App z(Num z)) and X := W W . Then

X = W W = F(App W (Num W ) = F(App W W ) = F W W = F X .

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 17 / 31

slide-17
SLIDE 17

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Application 2∗

Self-modifying programs For a given T (the program transformer) there exists a program P such that P ck = ck+1 if k is even, = T P ck

  • therwise.
  • On even inputs, P performs a standard operation (adding 1)
  • On odd inputs, the program P first modifies its own code

using T. To find P, apply the second fixed-point theorem to F := λp x.if (Even x) then (x + 1) else(T p x)

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 18 / 31

slide-18
SLIDE 18

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Lambda-terms themselves are “black boxes”

  • There is no λ-term F such that

F(M N) = M for all M, N

  • There is no λ-term F such that

F(M N) = N for all M, N On the other hand, we can compute with the codes of λ-terms

  • There is a λ-term F such that F M N = M for all M, N.
  • There is a λ-term F such that F M N = N for all M, N.
  • We also have an evaluator E:

E M = M

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 20 / 31

slide-19
SLIDE 19

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Analogy with programming

program text p compiler − → executable ˆ p Text Black box

  • inspect,
  • input/output (“call”)
  • edit,
  • pass around
  • . . .

p : . . . . . . . . . . . . . . . . . . ↓ In − → ˆ p − → Out

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 21 / 31

slide-20
SLIDE 20

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Universal programmable machine

Specification of a universal (programmable) machine U: p : . . . . . . . . . . . . . . . . . . − → x − → U − → ˆ p(x) Reflection over the programs / programming language: What functions (programs) can we write on program text?

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 22 / 31

slide-21
SLIDE 21

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Programs about programs

Examples of programs one can write p : . . . . . . . . . − → M1 − → true if p contains a “while” false if p contains no “while” p : . . . . . . . . . − → M2 − → true if |p| ≤ 1000 false if |p| > 1000 What about this?? p : . . . . . . . . . − → M3 − → true if ˆ p halts on all inputs false if ˆ p diverges on some input

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 23 / 31

slide-22
SLIDE 22

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

The Halting problem is undecidable

Theorem It is impossible to write a program H with the following specification p : . . . . . . . . . . . . . . . . . . − → x − → H − → true if ˆ p halts on x false if ˆ p diverges on x The undecidability of the Halting Problem was first proven by Turing, for his Turing machines. We will prove it for the λ-calculus.

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 24 / 31

slide-23
SLIDE 23

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Remember recursion for lambda terms using encoding

Theorem II. Given A1, A2, A3 ∈ Λ there is an H ∈ Λ such that H x = A1 x H H M N = A2 M N H H λx.M = A3 (λx. M ) H Example There is a λ-term C satisfying C x = c0 C M N = c1 C λx.M = c2 Proof Just take A1 := λxy.c0, A2 := λxyz.c1 and A3 := λxy.c2.

  • Exercise: Write a function R that checks whether a code of a term

is a redex. (So: R M = T if M is a redex and R M = F if M is not a redex.)

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 25 / 31

slide-24
SLIDE 24

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

We need to compute with codes

Recall that T = λx y.x and F = λx y.y. The following are impossible to define with λ-terms themselves. There is no term G satisfying G M = T if M has a normal form G M = F if M has no normal form There is no term H (compare the Halting problem) satisfying H M N = T if M N has a normal form H M N = F if M N has no normal form That these are impossible is not surprising: we can’t “look inside a black box”. What if we recast these question with coded λ-terms?

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 26 / 31

slide-25
SLIDE 25

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

The Halting problem for λ-calculus

The Halting problem is undecidable (λ-calculus version): Theorem There is no term H satisfying H M N = T if M N has a normal form H M N = F if M N has no normal form Proof Suppose H exists, Consider Q := λx.H x x Ω T Then Q Q = H Q Q Ω T = TΩ T = Ω if Q Q has a normal form FΩ T = T if Q Q has no normal form

  • Contradiction. So H doesn’t exist.

.

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 27 / 31

slide-26
SLIDE 26

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

The “Blank tape” problem for λ-calculus

Theorem There is no term B satisfying B M = T if M has a normal form B M = F if M has no normal form Proof Suppose B exists, Consider Q := λx.B x Ω T By the second fixed point theorem, there is a R such that Q R = R, that is B R Ω T = R. Now we have R = B R Ω T = T Ω T = Ω if R has a normal form = F Ω T = T if R has no normal form

  • Contradiction. So B doesn’t exist.
  • (NB. There may be a proof by reducing H to B; I didn’t see it.)
  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 28 / 31

slide-27
SLIDE 27

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Some other terms one can(not) write

Example There is a term L satisfying L M = cn if n is the number of λ’s inside M Example There is a term V satisfying V M = T if M is of the shape x P1 . . . Pn for some n, P, V M = F if M is not of the shape x P1 . . . Pn. Example There is no term H′ satisfying H′ M N = T if M N has a normal form H′ M N = F if M N has no normal form Proof Reduce B to H′. (Show that, if H′ exists, then we can also define B.)

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 29 / 31

slide-28
SLIDE 28

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Scott’s theorem

The impossibility (undecidability) results we have seen are all instances of a general theorem due to Scott. We phrase it here purely in terms of λ calculus. We assume a coding function − : Λ → Λ for which we have λ-terms App and Num satisfying App M N = M N Num M = M Definition Two disjoint subsets of Λ, A, B ⊆ Λ are separable if there is a λ-term F such that F M = T/F for every M and M ∈ A ⇒ F M = T M ∈ B ⇒ F M = F Theorem (Scott) If A, B are non-empty and closed under =β, then they are not separable.

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 30 / 31

slide-29
SLIDE 29

Self-interpretation in the Lambda Calculus The limitations of Self-interpretation

Radboud University

Proof of Scott’s theorem

Let A, B ⊆ Λ closed under =β and say a ∈ A and b ∈ B. Suppose: F separates A and B, so F M = T/F for every M and M ∈ A ⇒ F M = T M ∈ B ⇒ F M = F We define H := λy.if F(App y (Num y)) then b else a and we consider J := H H : J = H H = if F(App H (Num H )) then b else a = if F( H H ) then b else a = b iff H H ∈ A a iff H H ∈ B This is a contradiction. So F doesn’t exist.

  • H. Geuvers - Radboud Univ.

EWSCS 2016 Self-interpretation in λ-calculus 31 / 31