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
Overview Overview Untyped Lambda Calculus Operational Semantics Contextual Semantics Extension by Data Types D. Sabel & M. Schmidt-Schauß Lambda Calculus and Extensions 2/27
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
Pure Lambda Calculus: Examples λx.x λx.λy.x λx.λy.λf.f x y (λx.(x x)) (λx.(x x)) The identity function The function that can be applied to two arguments e 1, e 2 and returns e 1. Used as encoding of pairs A lambda-expression which is useless as a program: It is nonterminating. D. Sabel & M. Schmidt-Schauß Lambda Calculus and Extensions 4/27
Free and Bound Variables free variables FV (e) and bound variables BV (e) are inductively defined: FV (x) :={x}, if x Var BV (x) :=, if x Var FV ((e 1 e 2 )):=FV (e 1 ) FV (e 2 ) BV ((e 1 e 2 )):=BV (e 1 ) BV (e 2 ) FV (λx.e) :=FV (e) \ {x} BV (λx.e) :=BV (e) {x} D. Sabel & M. Schmidt-Schauß Lambda Calculus and Extensions 5/27
Renaming Bound Variables α-renaming: λx.e α λx.e[x /x], if x FV (e) and x BV (e) where α-equivalence = α is the smallest congruence obtained from α, i.e. it is inductively defined as e 1 = α α e 2, if e 1 e2 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
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
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[e/x] := y, if x y (λx.e 1 )[e/x] := λx.e 1 (λy.e 1 )[e/x] := λy.(e 1 [e/x]), if x y (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
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
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
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, K2 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
Operational Semantics Small-Step Operational Semantics of the Pure Lazy Lambda-Calculus Beta-Reduction (λx.e 1 ) e 2 β e1 [e 2 /x] C,β We write e 1 e 2, if e 1 = D[e 1 ], e β 1 e 2, and e 2 = D[e 2 ] D. Sabel & M. Schmidt-Schauß Lambda Calculus and Extensions 11/27
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,+ no, no,i transitive closure of no reflexive-transitive closure exactly i normal order reduction steps. D. Sabel & M. Schmidt-Schauß Lambda Calculus and Extensions 12/27
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
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: e iff e no, e where e is a WHNF. 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
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: e iff e no, e where e is a WHNF. 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
Standardization In order to compute WHNFs from an expression, normal-order reduction is sufficient: Proposition Let e be an expression, such that e C,β, e where e is a WHNF. Then e. s no, C,β, s 0 C,β, s 1 where s 0, s 1 are abstractions ( C,β, is β-reduction at any occurrence) D. Sabel & M. Schmidt-Schauß Lambda Calculus and Extensions 15/27
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
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
Connection to Rewriting and Confluence The reduction C,β, is confluen: e 1 C,β, C,β, e s 3 C,β, C,β, e 2 The confluence of C,β, and the standardization of normal-order reduction imply: Theorem β C,β is correct w.r.t. c, i.e. if e 1 e 2 then e 1 c e 2. D. Sabel & M. Schmidt-Schauß Lambda Calculus and Extensions 18/27
Connection to Rewriting C,β, is called the conversion relation C,β, is a congruence. C,β, c. However : C,β, c. Example: (λx.(x x x) (λx.(x x x)), hence, Ω c (λx.(x x x)) (λx.(x x x)). C,β, But, Ω (λx.(x x x)) (λx.(x x x)), since there is no common reduction successor. D. Sabel & M. Schmidt-Schauß Lambda Calculus and Extensions 19/27
Extension by Data Types LNAME as a Core Language of Haskell D. Sabel & M. Schmidt-Schauß Lambda Calculus and Extensions 20/27
Syntax of LNAME LNAME is an extension of the lazy lambda calculus by three constructs: data: there are primitives for constructing and deconstructing data Sequentialization: seq Supercombinators F: recursive function definitions D. Sabel & M. Schmidt-Schauß Lambda Calculus and Extensions 21/27
Summary: Syntax of LNAME For every f F there is a definition of the form f x 1... x n = e where x i are variables and FV (e) {x 1,..., x n }. Syntax Grammar for expressions of LNAME where x, x i Var, f F, and c T,i K T : e, e i E LNAME ::= x λx.e f (e 1 e 2 ) (c T,i e 1... e ar(ct,i )) case T e of {pat T,1 e 1 ;... ; pat T, T e T } seq e 1 e 2 pat T,i ::= (c T,i x i,1... x i,ar(ct,i )) D. Sabel & M. Schmidt-Schauß Lambda Calculus and Extensions 22/27
Reductions rules of LNAME β (λx.e 1 ) e 2 e 1 [e 2 /x] (case) (seq) (case T (c T,i e 1... e ar(c T,i ) ) of {... ; (c T,i x i,1... x i,ar(ct,i )) e i ;...}) e i [e 1 /x i,1,..., e ar(c T,i ) /x i,ar(c T,i )] seq v e e, if v is a WHNF. (SCβ) f e 1... e n e[e 1 /x 1,..., e n /x n ], if f x 1... x n = e is the definition of f F. D. Sabel & M. Schmidt-Schauß Lambda Calculus and Extensions 23/27
Syntax of LNAME Extension by Data There is a finite nonempty set of type constructors T, where for every T T there are pairwise disjoint finite nonempty sets of data constructors D T = {c T,1,... c T, T }. Every constructor has a fixed arity (a non-negative integer) denoted by ar(t ) or ar(c T,j ), The data constructor c T,i of arity n is applied to n expressions to form a constructor application (c T,i e 1... e ar(ct,i )). Case-expressions for analysing and deconstructing data. Examples type constructor Bool (of arity 0) with data constructors True and False type constructor List (of arity 1) with data constructors Nil (of arity 0) and Cons (of arity 2). A list of three elements: (Cons True (Cons False (Cons True Nil))). D. Sabel & M. Schmidt-Schauß Lambda Calculus and Extensions 24/27
Recursive Definition of Functions Extension We assume that there is a set F of function symbols, also called supercombinators and that for every f F there is a definition of f of the form f x 1... x n = e where x i are variables and e is an expression s.t. FV (e) {x 1,..., x n }. The names from F are treated as constants, and so may also occur in the defining expressions e on the right hand side. The number n in a definition f x 1... x n = e is called the arity of f and written as ar(f). D. Sabel & M. Schmidt-Schauß Lambda Calculus and Extensions 25/27
Recursive Definition of Supercombinators Examples map, head F map F is a recursive supercombinator: map f xs = case List xs of {Nil Nil; (Cons y ys) Cons (f y) (map f ys )} head F is a non-recursive supercombinator: head xs = case List xs of {Nil Ω; (Cons y ys) y} D. Sabel & M. Schmidt-Schauß Lambda Calculus and Extensions 26/27
WHNFs of LNAME Definition WHNFs of LNAME are of the form A functional weak head normal form (FWHNF) in LNAME is any abstraction and any expression of the form (f e 1... e m ) where f F and ar(f) > m. A constructor weak head normal form (CWHNF) is any expression of the form (c T,i e 1... e ar(ct,i )). There are no -irreducible closed expressions that are not WHNFs, for example (True True); These are ruled out in typed calculi. D. Sabel & M. Schmidt-Schauß Lambda Calculus and Extensions 27/27