Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauß 29. September 2015 Lehrerbildungsforum Informatik Last update: 30. September 2015 1
Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME Overview Overview Untyped Lambda Calculus Operational Semantics Contextual Semantics Extension by Data Types D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 2/27
Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME Pure (Untyped) Lambda Calculus Lambda Calculus Grammar for expressions e ∈ E λ of the pure lambda calculus: e, e i ∈ E λ ::= x | λx.e | ( e 1 e 2 ) where x ∈ Var λx.e Abstraction ( e 1 e 2 ) Application (of e 1 to e 2 ) We may omit brackets for better readability. The body of abstraction extends as far as possible ( e 1 e 2 e 3 e 4 ) is reconstructed as ((( e 1 e 2 ) e 3 ) e 4 ) . Bracketing is left-associative. D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 3/27
Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME Pure Lambda Calculus: Examples λx.x The identity function λx.λy.x The function that can be applied to two arguments e 1 , e 2 and returns e 1 . λx.λy.λf.f x y Used as encoding of pairs ( λx. ( x x )) ( λx. ( x x )) A lambda-expression which is useless as a program: It is nonterminating. D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 4/27
Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME Free and Bound Variables free variables FV ( e ) and bound variables BV ( e ) are inductively defined: := { x } , if x ∈ Var := ∅ , if x ∈ Var FV ( x ) BV ( x ) FV (( e 1 e 2 )):= FV ( e 1 ) ∪ FV ( e 2 ) BV (( e 1 e 2 )):= BV ( e 1 ) ∪ BV ( e 2 ) := FV ( e ) \ { x } := BV ( e ) ∪ { x } FV ( λx.e ) BV ( λx.e ) D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 5/27
Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME Renaming Bound Variables α -renaming: → λx ′ .e [ x ′ /x ] , if x ′ �∈ FV ( e ) and x ′ �∈ BV ( e ) λx.e α − where α -equivalence = α is the smallest congruence obtained from α − → , i.e. it is inductively defined as α e 1 = α e 2 , if e 1 − → e 2 e = α e e 1 = α e 2 , if e 2 = α e 1 e 1 = α e 3 , if e 1 = α e 2 ∧ e 2 = α e 3 C [ e 1 ] = α C [ e 2 ] , if e 1 = α e 2 D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 6/27
Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME Variable Convention The Convention on Bound Variables: In the expressions mentioned: binders always bind distinct variables, and bound variables are distinct from free variables. This can always be achieved by α -renamings. Example λx.x ( y ( λx.λy.y x )) : the convention is not satisfied. It can be satisfied by α -renaming: λx.x ( y ( λx.λy.y x )) = α λx.x ( y ( λz.λu.u z )) D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 7/27
Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME Substitution We define a more general notion of substitution: e 1 [ e 2 /x ] denotes the substitution of all free occurrences of variable x in e 1 by the expression e 2 : An inductive definition recursing over the expression structure is: x [ e/x ] := e y, if x � = y y [ e/x ] := ( λx.e 1 )[ e/x ] := λx.e 1 λy. ( e 1 [ e/x ]) , if x � = y ( λy.e 1 )[ e/x ] := ( e 1 e 2 )[ e/x ] := ( e 1 [ e/x ] e 2 [ e/x ]) Due to the distinct variable convention: There is no capture of variables. D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 8/27
Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME Substitution Examples: Compute ( x λx.x )[ λy.y / x ] ( λx.x x ) ( λy.y y ) → ( x x ) [( λy.y y ) / x ] = ( λy.y y ) ( λy.y y ) = α ( λx.x x ) ( λy.y y ) D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 9/27
Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME Substitution Examples: Compute ( x λx.x )[ λy.y / x ] 1. result of renaming: ( x λu.u )[ λy.y / x ] 2. result of substitution : (( λy.y ) λu.u ) ( λx.x x ) ( λy.y y ) → ( x x ) [( λy.y y ) / x ] = ( λy.y y ) ( λy.y y ) = α ( λx.x x ) ( λy.y y ) D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 9/27
Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME Some Combinators Example I := λx.x K := λx.λy.x K 2 := λx.λy.y Ω := ( λx 1 . ( x 1 x 1 )) ( λx 2 . ( x 2 x 2 )) Y := λy 1 . ( λx 1 . ( y 1 ( x 1 x 1 ))) ( λy 2 . ( y 2 ( x 2 x 2 ))) The I -combinator is the identity function. K, K 2 are projections, and Ω is non-terminating (diverging). The Y -combinator is a fixpoint-combinator , it has the property that Y e ∼ c e ( Y e ) holds. It can be used to express recursion. D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 10/27
Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME Operational Semantics Small-Step Operational Semantics of the Pure Lazy Lambda-Calculus Beta-Reduction β − → e 1 [ e 2 /x ] ( λx.e 1 ) e 2 C ,β − − → e 2 , if We write e 1 β e 1 = D [ e ′ 1 ] , e ′ → e ′ 2 , and e 2 = D [ e ′ − 2 ] 1 D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 11/27
Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME Operational Semantics: Normal-order reduction Normal order reduction : A deterministic evaluation strategy. Reduce the outermost-leftmost beta-redex: A normal order reduction step no − → is any β -reduction which is performed inside a reduction context: s no − → t if s is of the form R [( λx.s 1 ) s 2 ] and t = R [ s 1 [ s 2 /x ]] for a reduction context R . Grammar for reduction contexts : R ::= [ · ] | ( R e ) no, + transitive closure of no − − − → − → no, ∗ − − → reflexive-transitive closure no,i − − → exactly i normal order reduction steps. D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 12/27
Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME Normal-order reduction: WHNF Closed and no − → -irreducible expressions of the (lazy) lambda calculus are exactly the abstractions. Abstractions are also the weak head normal forms (WHNFs) of the (lazy) lambda calculus. D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 13/27
Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME Converging Expression Definition An expression e ∈ E λ converges (or successfully terminates ) (written as e ⇓ ) iff there exists a sequence of normal order reduction steps starting with e and ending in a WHNF: no, ∗ → e ′ where e ′ is a WHNF. e ⇓ iff e − − If e ⇓ does not hold, then we say that e diverges and write e ⇑ . D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 14/27
Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME Converging Expression Definition An expression e ∈ E λ converges (or successfully terminates ) (written as e ⇓ ) iff there exists a sequence of normal order reduction steps starting with e and ending in a WHNF: no, ∗ → e ′ where e ′ is a WHNF. e ⇓ iff e − − If e ⇓ does not hold, then we say that e diverges and write e ⇑ . Examples • ( λx.x ) ⇓ • (( λx.λy.x ) ( λz.z ) ( λw.w )) ⇓ : no → ( λy.λz.z ) ( λw.w ) no − − → λz.z. • (( λx.x ) ( λy.y )) ⇓ • (( λx.x x ) ( λy.y y )) ⇑ • ( x ( λy.y ) ⇑ D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 14/27
� � Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME Standardization In order to compute WHNFs from an expression, normal-order reduction is sufficient: Proposition C,β, ∗ → e ′ where e ′ is a WHNF. Let e be an expression, such that e − − − Then e ⇓ . s no, ∗ C,β, ∗ � s 1 s 0 C,β, ∗ where s 0 , s 1 are abstractions C,β, ∗ ( → is β -reduction at any occurrence) − − − D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 15/27
Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME Semantics: Contextual Equivalence Definition contextual equivalence Definition Let e 1 , e 2 ∈ E λ . Then e 1 and e 2 are contextually equivalent , written as e 1 ∼ e 2 iff for all contexts C ∈ C λ : C [ e 1 ] ⇓ ⇐ ⇒ C [ e 2 ] ⇓ . D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 16/27
Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME Semantics: Contextual Equivalence Properties of ∼ c Contextual preorder is a precongruence (i.e. it is a partial order and s ≤ c t = ⇒ C [ s ] ≤ c C [ t ] ) Contextual equivalence is a congruence (i.e. it is an equivalence relation and s ∼ c t = ⇒ C [ s ] ∼ c C [ t ] ) Consequence: if s ∼ c t it is possible to replace s by t anywhere in a program P to P ′ and P ∼ c P ′ holds. Divergent closed expressions are equivalent: Let e 1 , e 2 be two closed expressions with e 1 ⇑ and e 2 ⇑ . Then e 1 ∼ c e 2 . D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 17/27
Recommend
More recommend