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 ([ 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
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
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
Proof (4) Case u = t 1 t 2 : Exercise. Nameless Representation of Terms – p.6/29
Now on to the main topic . . . Nameless Representation of Terms Nameless Representation of Terms – p.7/29
Representing Terms ::= t x | λx. t | t 1 t 2 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
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
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
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
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
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
Devise Canonical Representation Maybe we can think of a unique representation for α -equivalent terms. Nameless Representation of Terms – p.13/29
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
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
Formal Definition of de Bruijn Terms We will define a family of sets T n so that the set T i can represent terms with at most i free variables. 0 ≤ k < n t ∈ T n n > 0 k ∈ T n λ.t ∈ T n − 1 t 1 ∈ T n t 2 ∈ T n ( t 1 t 2 ) ∈ T n Nameless Representation of Terms – p.14/29
Free Variables What do we do with y ? λx. y x Nameless Representation of Terms – p.15/29
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
Naming Contexts Let’s simplify Γ to be a sequence of variable names. Γ = x n − 1 , . . . , x 1 , x 0 Then we’ll define dom (Γ) = { x n − 1 , . . . , x 1 , x 0 } And Γ( x ) = rightmost index of x in Γ Nameless Representation of Terms – p.16/29
Converting to Nameless Representation db Γ ( x ) = Γ( x ) db Γ ( λx. t ) = λ.db Γ ,x ( t ) db Γ ( t 1 t 2 ) = db Γ ( t 1 ) db Γ ( t 2 ) Nameless Representation of Terms – p.17/29
Converting to Nameless Representation db Γ ( x ) = Γ( x ) db Γ ( λx. t ) = λ.db Γ ,x ( t ) db Γ ( t 1 t 2 ) = db Γ ( t 1 ) db Γ ( t 2 ) What is the type of db Γ ? Nameless Representation of Terms – p.17/29
Converting to Nameless Representation db Γ ( x ) = Γ( x ) db Γ ( λx. t ) = λ.db Γ ,x ( t ) db Γ ( t 1 t 2 ) = db Γ ( t 1 ) db Γ ( t 2 ) What is the type of db Γ ? db Γ : T λ → T len (Γ) Nameless Representation of Terms – p.17/29
Conversion Example We will work with Γ = x, y, z and will convert the term λx. y x Then we have db x,y,z ( λx. y x ) = λ. db x,y,z,x ( y x ) = λ. db x,y,z,x ( y ) db x,y,z,x ( x ) = λ. 2 0 Nameless Representation of Terms – p.18/29
Defining Substitution Nameless Representation of Terms – p.19/29
Substitution on Nameless Terms We must define [ k �→ s ] t for terms in T n . But how? Nameless Representation of Terms – p.20/29
Substitution on Nameless Terms We must define [ k �→ s ] t for terms in T n . 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
First Attempt � s if k = j [ j �→ s ] k = otherwise k [ j �→ s ]( λ.t ) = λ. [ j �→ s ] t [ j �→ s ]( t 1 t 2 ) = ([ j �→ s ] t 1 ) ([ j �→ s ] t 2 ) Nameless Representation of Terms – p.21/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
Second Attempt � s if k = j [ j �→ s ] k = otherwise k [ j �→ s ]( λ.t ) = λ. [ j + 1 �→ s ] t [ j �→ s ]( t 1 t 2 ) = ([ j �→ s ] t 1 ) ([ j �→ s ] t 2 ) Nameless Representation of Terms – p.23/29
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
Third Attempt (Shifting) ↑ ( k ) = k + 1 ↑ ( λ. t ) = λ. ↑ ( t ) ↑ ( t 1 t 2 ) = ↑ ( t 1 ) ↑ ( t 2 ) � s if k = j [ j �→ s ] k = otherwise k [ j �→ s ]( λ.t ) = λ. [ j + 1 �→↑ ( s )] t [ j �→ s ]( t 1 t 2 ) = ([ j �→ s ] t 1 ) ([ j �→ s ] t 2 ) Nameless Representation of Terms – p.25/29
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
Third Attempt (Shifting with Cut-Off) � k if k < c ↑ c ( k ) = k + 1 if k ≥ c ↑ c ( λ. t ) = λ. ↑ c +1 ( t ) ↑ c ( t 1 t 2 ) = ↑ c ( t 1 ) ↑ c ( t 2 ) � s if k = j [ j �→ s ] k = otherwise k [ j �→ s ]( λ.t ) = λ. [ j + 1 �→↑ 0 ( s )] t [ j �→ s ]( t 1 t 2 ) = ([ j �→ s ] t 1 ) ([ j �→ s ] t 2 ) Nameless Representation of Terms – p.27/29
Generalized Shifting � k if k < c ↑ d c ( k ) = k + d if k ≥ c ↑ d λ. ↑ d c ( λ. t ) = c +1 ( t ) ↑ d ↑ d c ( t 1 ) ↑ d c ( t 1 t 2 ) = c ( t 2 ) Nameless Representation of Terms – p.28/29
Evaluation of de Bruijn Terms The evaluation rule we want is 0 ( v 2 )] t 12 ) E-A PP A BS ( λ. t 12 ) v 2 →↑ − 1 ([0 �→↑ 1 0 Nameless Representation of Terms – p.29/29
Evaluation of de Bruijn Terms The evaluation rule we want is 0 ( v 2 )] t 12 ) E-A PP A BS ( λ. t 12 ) v 2 →↑ − 1 ([0 �→↑ 1 0 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 0 ( ↑ − 1 = ([0 �→ 1](0 1 2))) 1 2 0 ( ↑ − 1 = (1 1 2)) 1 2 0 = 0 0 1 1 2 = db Γ ( x x y y z ) Nameless Representation of Terms – p.29/29
Recommend
More recommend