Nameless Representation of Terms CIS500: Software Foundations - - PowerPoint PPT Presentation

nameless representation of terms
SMART_READER_LITE
LIVE PREVIEW

Nameless Representation of Terms CIS500: Software Foundations - - PowerPoint PPT Presentation

Nameless Representation of Terms CIS500: Software Foundations Nameless Representation of Terms p.1/29 First, some review . . . A Proof on -Terms Nameless Representation of Terms p.2/29 Proof (1) We want to prove that if z FV ([


slide-1
SLIDE 1

Nameless Representation of Terms

CIS500: Software Foundations

Nameless Representation of Terms – p.1/29

slide-2
SLIDE 2

First, some review . . .

A Proof on λ-Terms

Nameless Representation of Terms – p.2/29

slide-3
SLIDE 3

Proof (1)

We want to prove that if z ∈ FV ([x → v]u) then z ∈ (FV (u) \ {x}) ∪ FV (v) In other words, FV ([x → v]u) ⊆ (FV (u) \ {x}) ∪ FV (v) Proof by induction on the structure of u.

Nameless Representation of Terms – p.3/29

slide-4
SLIDE 4

Proof (2)

Case u = x: Then [x → v]u = v, and FV (v) ⊆ FV (u) \ {x} ∪ FV (v) Case u = y, where y = x: Then [x → v]u = y, and FV (u) = FV (y) = {y} ⊆ ({y} \ {x}) ∪ FV (v) = (FV (u) \ {x}) ∪ FV (v)

Nameless Representation of Terms – p.4/29

slide-5
SLIDE 5

Proof (3)

Case u = λy. t, where y = x: Then [x → v]u = λy. [x → v]t By the IH, FV ([x → v]t) ⊆ (FV (t) \ {x}) ∪ FV (v). So FV ([x → v]u) = FV (λy. [x → v] t) = FV ([x → v] t) \ {y} ⊆ ((FV (t) \ {x}) ∪ FV (v)) \ {y} ⊆ (FV (t) \ {x} \ {y}) ∪ FV (v) = (FV (t) \ {y} \ {x}) ∪ FV (v) = (FV (u) \ {x}) ∪ FV (v)

Nameless Representation of Terms – p.5/29

slide-6
SLIDE 6

Proof (4)

Case u = t1 t2: Exercise.

Nameless Representation of Terms – p.6/29

slide-7
SLIDE 7

Now on to the main topic . . .

Nameless Representation of Terms

Nameless Representation of Terms – p.7/29

slide-8
SLIDE 8

Representing Terms

t ::= x | λx. t | t1 t2 Choosing a concrete way to represent terms is necessary when using computers to work with λ-terms. Implementing programming language evaluators. Writing machine-checkable definitions and proofs of theorems.

Nameless Representation of Terms – p.8/29

slide-9
SLIDE 9

Variable Capture

[x → λy. z](λz. x) = λz. λy. z How can we be sure that our implementation doesn’t make this mistake?

Nameless Representation of Terms – p.9/29

slide-10
SLIDE 10

Idea: Rename During Substitution

Rename z to z′ before applying substitution. [x → λy. z](λz. x) = λz′. λy. z

Nameless Representation of Terms – p.10/29

slide-11
SLIDE 11

Idea: “Barendregt Convention”

We can make sure our terms never use the same variable name twice. So we must always start with [x → λy. z](λz′. x)

Nameless Representation of Terms – p.11/29

slide-12
SLIDE 12

Idea: “Barendregt Convention”

We can make sure our terms never use the same variable name twice. So we must always start with [x → λy. z](λz′. x) But then what happens here? [x → λy. z](λz. x x)

Nameless Representation of Terms – p.11/29

slide-13
SLIDE 13

More Extreme Proposals

Explicit Substitutions: Make substitutions part of the syntax and encode renaming into the evaluation rules. Combinators: Find a language with applications but no variables or binding, and translate terms to this langauge.

Nameless Representation of Terms – p.12/29

slide-14
SLIDE 14

Devise Canonical Representation

Maybe we can think of a unique representation for α-equivalent terms.

Nameless Representation of Terms – p.13/29

slide-15
SLIDE 15

Devise Canonical Representation

Maybe we can think of a unique representation for α-equivalent terms. For λx. λy. x (y x) we could write λ. λ. 1 (0 1) Is this representation unique?

Nameless Representation of Terms – p.13/29

slide-16
SLIDE 16

Devise Canonical Representation

Maybe we can think of a unique representation for α-equivalent terms. For λx. λy. x (y x) we could write λ. λ. 1 (0 1) Is this representation unique? What about free variables?

Nameless Representation of Terms – p.13/29

slide-17
SLIDE 17

Formal Definition of de Bruijn Terms

We will define a family of sets Tn so that the set Ti can represent terms with at most i free variables. 0 ≤ k < n k ∈ Tn t ∈ Tn n > 0 λ.t ∈ Tn−1 t1 ∈ Tn t2 ∈ Tn (t1 t2) ∈ Tn

Nameless Representation of Terms – p.14/29

slide-18
SLIDE 18

Free Variables

What do we do with y? λx. y x

Nameless Representation of Terms – p.15/29

slide-19
SLIDE 19

Free Variables

What do we do with y? λx. y x We need some sort of context of definitions, for example Γ = x → 4, y → 3, z → 2, a → 1, b → 0 Then we should be able to define a function dbΓ, such that dbΓ(x (y z)) = 4 (3 2) dbΓ(λx. y x) = λ. 4 0

Nameless Representation of Terms – p.15/29

slide-20
SLIDE 20

Naming Contexts

Let’s simplify Γ to be a sequence of variable names. Γ = xn−1, . . . , x1, x0 Then we’ll define dom(Γ) = {xn−1, . . . , x1, x0} And Γ(x) = rightmost index of x in Γ

Nameless Representation of Terms – p.16/29

slide-21
SLIDE 21

Converting to Nameless Representation

dbΓ(x) = Γ(x) dbΓ(λx. t) = λ.dbΓ,x(t) dbΓ(t1 t2) = dbΓ(t1) dbΓ(t2)

Nameless Representation of Terms – p.17/29

slide-22
SLIDE 22

Converting to Nameless Representation

dbΓ(x) = Γ(x) dbΓ(λx. t) = λ.dbΓ,x(t) dbΓ(t1 t2) = dbΓ(t1) dbΓ(t2) What is the type of dbΓ?

Nameless Representation of Terms – p.17/29

slide-23
SLIDE 23

Converting to Nameless Representation

dbΓ(x) = Γ(x) dbΓ(λx. t) = λ.dbΓ,x(t) dbΓ(t1 t2) = dbΓ(t1) dbΓ(t2) What is the type of dbΓ? dbΓ : Tλ → Tlen(Γ)

Nameless Representation of Terms – p.17/29

slide-24
SLIDE 24

Conversion Example

We will work with Γ = x, y, z and will convert the term λx. y x Then we have dbx,y,z(λx. y x) = λ. dbx,y,z,x(y x) = λ. dbx,y,z,x(y) dbx,y,z,x(x) = λ. 2 0

Nameless Representation of Terms – p.18/29

slide-25
SLIDE 25

Defining Substitution

Nameless Representation of Terms – p.19/29

slide-26
SLIDE 26

Substitution on Nameless Terms

We must define [k → s]t for terms in Tn. But how?

Nameless Representation of Terms – p.20/29

slide-27
SLIDE 27

Substitution on Nameless Terms

We must define [k → s]t for terms in Tn. But how? We want to guarantee dbΓ([x → s]t) = [Γ(x) → dbΓ(s)]dbΓ(t) for all Γ such that FV (s) ∪ FV (t) ∪ {x} ⊆ dom(Γ)

Nameless Representation of Terms – p.20/29

slide-28
SLIDE 28

First Attempt

[j → s]k = s if k = j k

  • therwise

[j → s](λ.t) = λ. [j → s]t [j → s](t1 t2) = ([j → s]t1) ([j → s]t2)

Nameless Representation of Terms – p.21/29

slide-29
SLIDE 29

Counter-Example

[x → λz. z](x (λy. y)) Let Γ = x. dbΓ([x → λz. z](x (λy. y))) = dbΓ((λz. z) (λy. y)) = (λ. 0) (λ. 0) but [Γ(x) → dbΓ(λz. z)]dbΓ(x (λy. y)) = [0 → λ. 0](0 (λ.0)) = (λ. 0) (λ. [0 → λ. 0]0) = (λ. 0) (λ. λ. 0)

Nameless Representation of Terms – p.22/29

slide-30
SLIDE 30

Second Attempt

[j → s]k = s if k = j k

  • therwise

[j → s](λ.t) = λ. [j + 1 → s]t [j → s](t1 t2) = ([j → s]t1) ([j → s]t2)

Nameless Representation of Terms – p.23/29

slide-31
SLIDE 31

Counter-Example

[x → λy. w]λz. x Let Γ = x, w. dbΓ([x → λy. w]λz. x) = dbΓ(λz. λy. w) = λ. dbΓ,z(λy. w) = λ. λ. dbΓ,z,y(w) = λ. λ. 2 but [Γ(x) → dbΓ(λy. w)]dbΓ(λz. x) = [1 → λ. 1]λ. 2 = λ. [2 → λ. 1]2 = λ. λ. 1

Nameless Representation of Terms – p.24/29

slide-32
SLIDE 32

Third Attempt (Shifting)

↑ (k) = k + 1 ↑ (λ. t) = λ. ↑ (t) ↑ (t1 t2) = ↑ (t1) ↑ (t2) [j → s]k = s if k = j k

  • therwise

[j → s](λ.t) = λ. [j + 1 →↑ (s)]t [j → s](t1 t2) = ([j → s]t1) ([j → s]t2)

Nameless Representation of Terms – p.25/29

slide-33
SLIDE 33

Counter-Example

[x → λy. w y]λz. x Let Γ = x, w. dbΓ([x → λy. w y]λz. x) = dbΓ(λz. λy. w y) = λ. λ. dbΓ,z,y(w y) = λ. λ. 2 0 but [Γ(x) → dbΓ(λy. w y)]dbΓ(λz. x) = [1 → λ. 1 0]λ. 2 = λ. [2 →↑ (λ. 1 0)]2 = λ. [2 → λ. 2 1]2 = λ. λ. 2 1

Nameless Representation of Terms – p.26/29

slide-34
SLIDE 34

Third Attempt (Shifting with Cut-Off)

↑c (k) = k if k < c k + 1 if k ≥ c ↑c (λ. t) = λ. ↑c+1 (t) ↑c (t1 t2) = ↑c (t1) ↑c (t2) [j → s]k = s if k = j k

  • therwise

[j → s](λ.t) = λ. [j + 1 →↑0 (s)]t [j → s](t1 t2) = ([j → s]t1) ([j → s]t2)

Nameless Representation of Terms – p.27/29

slide-35
SLIDE 35

Generalized Shifting

↑d

c (k)

= k if k < c k + d if k ≥ c ↑d

c (λ. t)

= λ. ↑d

c+1 (t)

↑d

c (t1 t2)

= ↑d

c (t1) ↑d c (t2)

Nameless Representation of Terms – p.28/29

slide-36
SLIDE 36

Evaluation of de Bruijn Terms

The evaluation rule we want is (λ. t12) v2 →↑−1 ([0 →↑1

0 (v2)]t12) E-APPABS

Nameless Representation of Terms – p.29/29

slide-37
SLIDE 37

Evaluation of de Bruijn Terms

The evaluation rule we want is (λ. t12) v2 →↑−1 ([0 →↑1

0 (v2)]t12) E-APPABS

Consider this example. Let’s say our context is Γ = z, y, x. dbΓ((λw. w x y) x y z) = (λ. 0 1 2) 0 1 2 → (↑−1 ([0 →↑1

0 (0)](0 1 2))) 1 2

= (↑−1 ([0 → 1](0 1 2))) 1 2 = (↑−1 (1 1 2)) 1 2 = 0 0 1 1 2 = dbΓ(x x y y z)

Nameless Representation of Terms – p.29/29