untyped lambda calculus
play

Untyped Lambda Calculus Principles of Programming Languages CSE 526 - PDF document

Untyped Lambda Calculus Principles of Programming Languages CSE 526 Syntax 1 Variables and Substitution 2 Reductions 3 Nameless Representation 4 Compiled at 13:40 on 2018/02/15 Programming Languages The Untyped Lambda Calculus CSE 526


  1. Untyped Lambda Calculus Principles of Programming Languages CSE 526 Syntax 1 Variables and Substitution 2 Reductions 3 Nameless Representation 4 Compiled at 13:40 on 2018/02/15 Programming Languages The Untyped Lambda Calculus CSE 526 1 / 30 Lambda Calculus A formal notation to study computability and programming. Can be considered as the smallest universal programming language. Universal: Can be used to express any computation that can be performed on a Turing Machine Small: Has only two constructs: abstraction and application. Brief History: Introduced by Church and Kleene in 1930s. Used by Church to study problems in computability. Concepts have heavily influenced functional programming . Used to study types and type systems in programming languages Programming Languages The Untyped Lambda Calculus CSE 526 2 / 30

  2. Syntax Lambda Terms Syntax of the λ -calculus t ::= Terms Variable x | Abstraction λ x . t | t t Application Textual Representation: Use parentheses to represent trees using linear text Programming Languages The Untyped Lambda Calculus CSE 526 3 / 30 Syntax Informal Semantics λ -expressions can be considered as expressions in a functional language Abstraction: ( λ x . t ) is a “function” with formal parameter x that returns (the value of) term t . Example 1: λ x . x is the identity function: one that returns the argument value itself. Example 2: λ x .λ y . x is a function that takes two arguments x and y and returns the first argument. Application: ( t 1 t 2 ) is a “function call” where t 1 is a function and t 2 is the supplied argument. Example: (( λ x . x ) y ) supplies y as the argument to the identity function. Programming Languages The Untyped Lambda Calculus CSE 526 4 / 30

  3. Syntax Syntactic Conventions and Syntactic Sugar Parentheses can be dropped using the following conventions: application is left associative e.g. (( f f ) x ) is same as f f x a λ binds as much as possible to its right. e.g λ f . λ x . f ( f x ) is same as ( λ f . ( λ x . f ( f x ))) Multiple consecutive abstractions can be combined: e.g. λ f .λ x . f ( f x ) is same as λ f x . f ( f x ) Programming Languages The Untyped Lambda Calculus CSE 526 5 / 30 Variables and Substitution The Meaning of Lambda Expressions Recall: λ x . t stands for a function with x as the parameter and (the value of) t as the return value. ( t 1 t 2 ) stands for “calling” the function t 1 with t 2 as the parameter. Example: Consider the expression (( λ wyx . y ( w y x )) ( λ sz . z )) This is an instance of an application. The expression in blue is passed as an argument to the function in red. The meaning of an application: replace every occurrence of the formal parameter in the body of the function with the given argument. In the above example λ yx . y (( λ sz . z ) y x ) 1 λ yx . y (( λ z . z ) x ) 2 λ yx . y x 3 Programming Languages The Untyped Lambda Calculus CSE 526 6 / 30

  4. Variables and Substitution Encoding Booleans in the λ -Calculus Example: B λ -calculus ( true && false ) true λ x . λ y . x ≡ ( λ x . λ y . (( x y ) false ))) false λ x . λ y . y ( λ x . λ y . x ) ( λ x . λ y . y ) λ x . λ y . (( x y ) false ) && → ( λ y . ((( λ x . λ y . x ) y ) false ))) λ x . λ y . (( x true ) y ) || ( λ x . λ y . y ) λ x . (( x false ) true ) ! → ( (( λ x . λ y . x ) ( λ x . λ y . y )) false ) if λ c . λ t . λ e . (( c t ) e ) → ( ( λ y . ( λ x . λ y . y )) false ) This is known as the → ( λ x . λ y . y ) Church encoding of Booleans , or simply Church Booleans . ≡ false Programming Languages The Untyped Lambda Calculus CSE 526 7 / 30 Variables and Substitution Encoding Natural Numbers in the λ -Calculus N λ -calculus 0 λ s . λ z . z λ s . λ z . ( s z ) 1 λ s . λ z . ( s ( s z )) 2 λ s . λ z . ( s ( s ( s z ))) 3 . . . λ n . λ s . λ z . ( s (( n s ) z )) inc plus λ m . λ n . λ s . λ z . (( m s ) (( n s ) z )) λ m . λ n . (( m ( plus n )) 0 ) times iszero λ m . (( m ( λ x . false )) true ) . . . This is known as the Church encoding of Naturals , or simply Church Numerals . Programming Languages The Untyped Lambda Calculus CSE 526 8 / 30

  5. Variables and Substitution Encoding Data Structures in the λ -Calculus fst ( pair ϕ 1 ϕ 2 ) λ f . λ s . λ c . (( c f ) s ) pair ≡ ( λ p . ( p true )) ( pair ϕ 1 ϕ 2 ) fst λ p . ( p true ) → ( pair ϕ 1 ϕ 2 ) true snd λ p . ( p false ) → ∗ ( λ c . (( c ϕ 1 ) ϕ 2 )) true → (( true ϕ 1 ) ϕ 2 ) Example: Let ϕ 1 and ϕ 2 be two arbitrary → ϕ 1 expressions. snd ( pair ϕ 1 ϕ 2 ) pair ϕ 1 ϕ 2 ≡ ( λ p . ( p false )) ( pair ϕ 1 ϕ 2 ) ≡ ( ( λ f . λ s . λ c . (( c f ) s ) ϕ 1 ) ϕ 2 ) → ∗ (( false ϕ 1 ) ϕ 2 ) → ∗ λ c . (( c ϕ 1 ) ϕ 2 ) → ϕ 2 Programming Languages The Untyped Lambda Calculus CSE 526 9 / 30 Variables and Substitution Evaluating Lambda Expressions: An Informal Intro. t ✔ ❚ ✔ ❚ Basic reduction: ( λ x . t 1 ) t 2 → [ x �→ t 2 ] t 1 , ✔ apply ❚ ✔ ❚ � ❅ where ✔ ❚ t 2 λ x ✔ ❚ ✄ ❈ [ x �→ t 2 ] t 1 be the term obtained by replacing all ✔ ❚ t 1 ✄ ❈ ✔ ❚ ✂ ❇ ✄ ❈ ✔ ✂ ❇ ❚ ✄ ❈ “free” occurrences of x in t 1 by t 2 . A sub-term of t of the form ( λ x . t 1 ) t 2 is called a redex of t . apply One step in evaluating a λ -term t is � ❅ apply λ x replacing some redex in t according to the � ❅ above reduction schema. x λ x λ z In general, there may be many redexes in x apply � ❅ a term. z λ x Example: Let id = ( λ x . x ) in term x id ( id ( λ z . id z )) Programming Languages The Untyped Lambda Calculus CSE 526 10 / 30

  6. Variables and Substitution Reduction Strategies A reduction strategy is used to choose a redex where the basic reduction step will be done. Full β -reduction: Pick a redex non-deterministically apply � ❅ Normal Order: choose the left-most, apply λ x outer-most redex. � ❅ x λ x λ z Call-By-Name: like normal-order, but ignore redexes inside abstractions. x apply � ❅ Call-By-Value: choose the right-most, z λ x inner-most redex that is not inside an x abstraction. Programming Languages The Untyped Lambda Calculus CSE 526 11 / 30 Variables and Substitution Evaluating Lambda Expressions The key step in evaluating an application then is: replace every occurrence of a formal parameter with the actual argument . Example: (( λ x . ( λ z . x z )) y ) → ( λ z . y z ) We can formalize the meaning of application by introducing a function, called substitution that maps terms to terms: ( λ x . t 1 ) t 2 → [ x �→ t 2 ] t 1 The central problem now is how we define this substitution function. Programming Languages The Untyped Lambda Calculus CSE 526 12 / 30

  7. Variables and Substitution Substitutions (1 st attempt) [ x �→ s ] x = s [ x �→ s ] y = if y � = x y [ x �→ s ]( λ y . t ) = λ y . [ x �→ s ] t [ x �→ s ]( t 1 t 2 ) = ([ x �→ s ] t 1 ) ([ x �→ s ] t 2 ) Appears to be correct. Example: [ x �→ y ]( λ z . x z ) = ( λ z . y z ) Use: ( λ x . ( λ z . x z )) y ) → ( λ z . y z ) But is incorrect! Example: [ x �→ y ]( λ x . x ) = ( λ x . y ) Use: (( λ x . ( λ x . x )) y ) → ( λ x . y ) Programming Languages The Untyped Lambda Calculus CSE 526 13 / 30 Variables and Substitution Substitutions (2 nd attempt) [ x �→ s ] x = s [ x �→ s ] y = if y � = x y � λ y . t if x = y [ x �→ s ]( λ y . t ) = λ y . [ x �→ s ] t if x � = y [ x �→ s ]( t 1 t 2 ) = ([ x �→ s ] t 1 ) ([ x �→ s ] t 2 ) [ x �→ y ]( λ x . x ) = ( λ x . x ) But is still incorrect! e.g. [ x �→ y ]( λ y . x y ) = ( λ y . y y ) In the result of the above example, one y is local to the function while the other y is not local. But going by our definition, there is no way to distinguish between the two y ’s! Solution: We should get ( λ w . y w ) instead (by suitably renaming “local” variables). Programming Languages The Untyped Lambda Calculus CSE 526 14 / 30

  8. Variables and Substitution Bound and Free Variables: An Informal Intro. Variable x in λ -expression λ x . t is said to be bound . Example 1: x in λ x . x is a bound variable. Example 2: in λ x . ( x y ), x is bound but y is not bound. Rough meaning: parameters are local to a function definition. A variable that is not bound is said to be free . Example 2: in λ x . ( x y ), y is free. Rough meaning: free variables in a function definition are analogous to non-local variables. Programming Languages The Untyped Lambda Calculus CSE 526 15 / 30 Variables and Substitution Bound and Binding Occurrences ❄ ✎☞ ✎☞ λ x . x ✍✌ ✍✌ Bound Occurrence (use) Binding Occurrence (declaration) ❄ ❄ ✎☞ ✎☞ ✎☞ ✎☞ ( λ x . x )( λ z . ( x x z )) ‘ ✍✌ ✍✌ ✍✌ ✍✌ Free Occurrence ❄ ( λ z . ( λ x . z ( x x )) ( λ x . z ( x x ))) ✻ ✻ Programming Languages The Untyped Lambda Calculus CSE 526 16 / 30

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend