lambda calculus and extensions [.5ex] as …1 lambda calculus and extensions as foundation of...

29
1 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

Upload: others

Post on 14-Aug-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

1

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

Page 2: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

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

Page 3: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME

Pure (Untyped) Lambda Calculus

Lambda CalculusGrammar for expressions e ∈ Eλ of the pure lambda calculus:

e, ei ∈ Eλ ::= x | λx.e | (e1 e2) where x ∈ Var

λx.e Abstraction(e1 e2) Application (of e1 to e2)

We may omit brackets for better readability.

The body of abstraction extends as far as possible

(e1 e2 e3 e4) is reconstructed as (((e1 e2) e3) e4).Bracketing is left-associative.

D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 3/27

Page 4: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

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 twoarguments e1, e2 and returns e1.

λx.λy.λf.f x y Used as encoding of pairs

(λx.(x x)) (λx.(x x)) A lambda-expression which is useless asa program: It is nonterminating.

D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 4/27

Page 5: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME

Free and Bound Variables

free variables FV (e) and bound variables BV (e) are inductivelydefined:

FV (x) :=x, if x ∈ VarFV ((e1 e2)):=FV (e1) ∪ FV (e2)FV (λx.e) :=FV (e) \ x

BV (x) :=∅, if x ∈ VarBV ((e1 e2)):=BV (e1) ∪ BV (e2)BV (λx.e) :=BV (e) ∪ x

D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 5/27

Page 6: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME

Renaming Bound Variables

α-renaming:

λx.eα−→ λx′.e[x′/x], if x′ 6∈ FV (e) and x′ 6∈ BV (e)

where α-equivalence =α is the smallest congruence obtained fromα−→, i.e. it is inductively defined as

e1 =α e2, if e1α−→ e2

e =α ee1 =α e2, if e2 =α e1e1 =α e3, if e1 =α e2 ∧ e2 =α e3

C[e1] =α C[e2], if e1 =α e2

D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 6/27

Page 7: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

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

Page 8: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME

Substitution

We define a more general notion of substitution:e1[e2/x] denotes the substitution of all free occurrences of variablex in e1 by the expression e2:

An inductive definition recursing over the expression structure is:

x[e/x] := ey[e/x] := y, if x 6= y(λx.e1)[e/x] := λx.e1(λy.e1)[e/x] := λy.(e1[e/x]), if x 6= y(e1 e2)[e/x] := (e1[e/x] e2[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

Page 9: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

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

Page 10: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

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

Page 11: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME

Some Combinators

Example

I := λx.xK := λx.λy.xK2 := λx.λy.yΩ := (λx1.(x1 x1)) (λx2.(x2 x2))Y := λy1.(λx1.(y1 (x1 x1))) (λy2.(y2 (x2 x2)))

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 propertythat 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

Page 12: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME

Operational Semantics

Small-Step Operational Semantics

of the

Pure Lazy Lambda-Calculus

Beta-Reduction

(λx.e1) e2β−→ e1[e2/x]

We write e1C,β−−→ e2, if

e1 = D[e′1], e′1β−→ e′2, and e2 = D[e′2]

D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 11/27

Page 13: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

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 isperformed inside a reduction context:

sno−→ t if s is of the form R[(λx.s1) s2]

and t = R[s1[s2/x]]for a reduction context R.Grammar for reduction contexts: R ::= [·] | (R e)

no,+−−−→ transitive closure ofno−→

no,∗−−→ reflexive-transitive closureno,i−−→ exactly i normal order reduction steps.

D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 12/27

Page 14: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME

Normal-order reduction: WHNF

Closed andno−→-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

Page 15: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

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 orderreduction steps starting with e and ending in a WHNF:

e⇓ iff eno,∗−−→ 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

Page 16: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

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 orderreduction steps starting with e and ending in a WHNF:

e⇓ iff eno,∗−−→ 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

Page 17: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME

Standardization

In order to compute WHNFs from an expression,normal-order reduction is sufficient:

Proposition

Let e be an expression, such that eC,β,∗−−−→ e′ where e′ is a WHNF.

Then e⇓.

s

no,∗

C,β,∗

s0

C,β,∗// s1

where s0, s1 are abstractions

(C,β,∗−−−→ is β-reduction at any occurrence)

D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 15/27

Page 18: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME

Semantics: Contextual Equivalence

Definition contextual equivalence

Definition

Let e1, e2 ∈ Eλ.Then e1 and e2 are contextually equivalent, written as e1 ∼ e2 ifffor all contexts C ∈ Cλ: C[e1]⇓ ⇐⇒ C[e2]⇓.

D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 16/27

Page 19: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME

Semantics: Contextual Equivalence

Properties of ∼cContextual preorder is a precongruence (i.e. it is a partialorder and s ≤c t =⇒ C[s] ≤c C[t] )

Contextual equivalence is a congruence (i.e. it is anequivalence 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 aprogram P to P ′ and P ∼c P ′ holds.

Divergent closed expressions are equivalent:Let e1, e2 be two closed expressions with e1⇑ and e2⇑.Then e1 ∼c e2.

D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 17/27

Page 20: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME

Connection to Rewriting and Confluence

The reductionC,β,∗−−−→ is confluen:

eC,β,∗

||

C,β,∗

""e1

C,β,∗""

e2C,β,∗

||s3

The confluence ofC,β,∗−−−→ and the standardization of normal-order

reduction imply:

Theorem

β−→ is correct w.r.t. ∼c, i.e. if e1C,β−−→ e2 then e1 ∼c e2.

D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 18/27

Page 21: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME

Connection to Rewriting

C,β,∗←−−→ is called the conversion relationC,β,∗←−−→ is a congruence.C,β,∗←−−→ ⊆ ∼c.

However :C,β,∗←−−→ 6= ∼c.

Example:(λx.(x x x) (λx.(x x x))⇑, hence,Ω ∼c (λx.(x x x)) (λx.(x x x)).

But, Ω 6C,β,∗←−−→ (λ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

Page 22: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME

Extension by Data Types

LNAME

as a Core Language of Haskell

D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 20/27

Page 23: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME

Syntax of LNAME

LNAME isan extension of the lazy lambda calculusby three constructs:

data: there are primitives for constructing and deconstructingdata

Sequentialization: seq

Supercombinators ∈ F : recursive function definitions

D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 21/27

Page 24: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME

Summary: Syntax of LNAME

For every f ∈ F there is a definition of the form f x1 . . . xn = ewhere xi are variables and FV (e) ⊆ x1, . . . , xn.

Syntax

Grammar for expressions of LNAMEwhere x, xi ∈ Var, f ∈ F , and cT,i ∈ KT :

e, ei ∈ ELNAME ::= x | λx.e | f | (e1 e2) | (cT,i e1 . . . ear(cT,i))

| caseT e of patT,1 → e1; . . . ;

patT,|T | → e|T || seq e1 e2

patT,i ::= (cT,i xi,1 . . . xi,ar(cT,i))

D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 22/27

Page 25: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME

Reductions rules of LNAME

β (λx.e1) e2 → e1[e2/x]

(case) (caseT (cT,i e′1 . . . e′ar(cT,i)) of

. . . ; (cT,i xi,1 . . . xi,ar(cT,i))→ ei; . . .)→ ei[e

′1/xi,1, . . . , e

′ar(cT,i)

/xi,ar(cT,i)]

(seq) seq v e → e, if v is a WHNF.

(SCβ) f e1 . . . en → e[e1/x1, . . . , en/xn],if f x1 . . . xn = e is the definition of f ∈ F .

D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 23/27

Page 26: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME

Syntax of LNAME

Extension by Data

There is a finite nonempty set of type constructors T , wherefor every T ∈ T there are pairwise disjoint finite nonemptysets of data constructors DT = cT,1, . . . cT,|T |.Every constructor has a fixed arity (a non-negative integer)denoted by ar(T ) or ar(cT,j),

The data constructor cT,i of arity n is applied to n expressionsto form a constructor application (cT,i e1 . . . ear(cT,i)).

Case-expressions for analysing and deconstructing data.

Examplestype constructor Bool (of arity 0) with data constructors True

and False

type constructor List (of arity 1) with data constructors Nil (ofarity 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

Page 27: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME

Recursive Definition of Functions

Extension We assume that there is a set F of function symbols,also called supercombinatorsand that for every f ∈ F there is a definition of f of the form

f x1 . . . xn = e

where xi are variables and e is an expressions.t. FV (e) ⊆ x1, . . . , xn.

The names from F are treated as constants, and so may also occurin the defining expressions e on the right hand side.

The number n in a definition f x1 . . . xn = e is called the arity off and written as ar(f).

D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 25/27

Page 28: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME

Recursive Definition of Supercombinators

Examples map, head ∈ Fmap ∈ F is a recursive supercombinator:

map f xs = caseList xs ofNil→ Nil;(Cons y ys) → Cons (f y) (map f ys )

head ∈ F is a non-recursive supercombinator:

head xs = caseList xs of Nil→ Ω; (Cons y ys)→ y

D. Sabel & M. Schmidt-Schauß · · Lambda Calculus and Extensions 26/27

Page 29: Lambda Calculus and Extensions [.5ex] as …1 Lambda Calculus and Extensions as Foundation of Functional Programming David Sabel and Manfred Schmidt-Schauˇ 29. September 2015 Lehrerbildungsforum

Introduction Lambda Calculus Connection to Rewriting and Confluence LNAME

WHNFs of LNAME

Definition

WHNFs of LNAME are of the form

A functional weak head normal form (FWHNF) in LNAME isany abstraction and any expression of the form (f e1 . . . em)where f ∈ F and ar(f) > m.

A constructor weak head normal form (CWHNF) is anyexpression of the form (cT,i e1 . . . ear(cT,i)).

There areno−→-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