10/12/20151 gc16/3c11 functional programming lecture 3 the lambda calculus a (slightly) deeper look

18
07/20/22 07/20/22 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look A (slightly) deeper look

Upload: philomena-pope

Post on 31-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look

04/19/2304/19/23 11

GC16/3C11 Functional ProgrammingLecture 3

The Lambda CalculusA (slightly) deeper lookA (slightly) deeper look

Page 2: 10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look

04/19/2304/19/23 22

Contents

• Revision: syntax and rulesRevision: syntax and rules -reduction and -reduction and -renaming-renaming• Reduction stategiesReduction stategies• Normal FormsNormal Forms

Page 3: 10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look

04/19/2304/19/23 33

Untyped Extended Syntax (2)

program :: expressionprogram :: expression

expression :: xexpression :: x | constant| constant | operator| operator | expression expression| expression expression | expression operator expression| expression operator expression

| | x x .. expression expression | ( expression )| ( expression )

Page 4: 10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look

04/19/2304/19/23 44

Using (“Applying”) a function

• To apply the previously defined function to To apply the previously defined function to the constant number 3:the constant number 3:

( ( ( ( x . ( x + 1 ) ) 3 )x . ( x + 1 ) ) 3 )

Page 5: 10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look

04/19/2304/19/23 55

Rules for Evaluation (1)

reductionreduction

reductionreduction

reduction reduction ( if x not free in E)( if x not free in E)

x . Ex . E y . E [ y / x ]y . E [ y / x ]

x . E ) z )x . E ) z ) E [ z / x ]E [ z / x ]

x . ( E x )x . ( E x ) E E

Page 6: 10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look

04/19/2304/19/23 66

Rules for Evaluation (2)

rulesrules there is a separate rule for evaluating each there is a separate rule for evaluating each

primitive operator (such as +)primitive operator (such as +) Example: the rule for + says that 3 + 4 Example: the rule for + says that 3 + 4

evaluates to 7evaluates to 7 Name clashesName clashes::

x . ( ( x . ( ( x . ( x + 3 ) ) ( x + 4 ) ) ) 5x . ( x + 3 ) ) ( x + 4 ) ) ) 5

x . ( 5 + 3 ) ) ( 5 + 4 )x . ( 5 + 3 ) ) ( 5 + 4 ) WRONG!!!WRONG!!!

Page 7: 10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look

04/19/2304/19/23 77

reduction and renaming

• The only tricky part of the rulesThe only tricky part of the rules• e1[e2/x] must replace only FREE e1[e2/x] must replace only FREE

occurrences of x in e1 with e2occurrences of x in e1 with e2

x . ( ( x . ( ( x . ( x + 3 ) ) ( x + 4 ) ) ) 5x . ( x + 3 ) ) ( x + 4 ) ) ) 5

( x . ( x + 3 ) ) ( x + 4 )

FREE x

( ( x . ( x + 3 ) ) ( 5 + 4 ) x . ( x + 3 ) ) ( 5 + 4 )

Page 8: 10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look

04/19/2304/19/23 88

reduction and renaming

• Identifying free variables in e1 is not Identifying free variables in e1 is not enough!enough!

• Free Variable Capture:Free Variable Capture:

f . ( f . ( aa . ( f a) ) ) . ( f a) ) ) f . ( f a) ) f . ( f a) )

BOUND a BOUND a FREE a FREE a

( ( aa . ( . (f . ( f a) ) a) ) f . ( f a) ) a) ) CAPTURED aCAPTURED a

Page 9: 10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look

04/19/2304/19/23 99

reduction and renaming

• Rule: during Rule: during reduction, if e2 contains any reduction, if e2 contains any free variables that are bound in e1, then for free variables that are bound in e1, then for each such free variable, it must be each such free variable, it must be --converted inside e1 BEFORE doing converted inside e1 BEFORE doing - -reduction!reduction!

f . ( f . ( aa . ( f a) ) ) . ( f a) ) ) f . ( f a) ) f . ( f a) )

f . ( f . ( bb . ( f b) ) ) . ( f b) ) ) f . ( f a) ) f . ( f a) )

( ( bb . ( . (f . ( f a) ) b) )f . ( f a) ) b) )

Page 10: 10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look

04/19/2304/19/23 1010

Reduction Strategies

• ((((x. e1) e2) is a “reducible expression” or “redex”x. e1) e2) is a “reducible expression” or “redex”

• An expression containing no redexes is in “Normal Form” An expression containing no redexes is in “Normal Form” or NFor NF• Execution = successive Execution = successive -reduction to NF-reduction to NF

• Whether an arbitrary e has a NF is undecidable (Halting Whether an arbitrary e has a NF is undecidable (Halting Problem)Problem)

• Many different sequences of Many different sequences of -reduction are possible – -reduction are possible – how does this affect the result?how does this affect the result?

Page 11: 10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look

04/19/2304/19/23 1111

Church-Rosser Theorem

• The Church-Rosser theorem proves that all The Church-Rosser theorem proves that all sequences (strategies) that terminate will converge sequences (strategies) that terminate will converge on the same NFon the same NF• Though some may not terminate!Though some may not terminate!

• Corollary: the NF for a given expression is unique Corollary: the NF for a given expression is unique (if it exists)(if it exists)

• So So -reductions can be done in any order-reductions can be done in any order• Even in parallel!Even in parallel!

Page 12: 10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look

04/19/2304/19/23 1212

Normalising orders

• Not all strategies (orders of performing Not all strategies (orders of performing --reductions) terminatereductions) terminate

• So which should we choose?So which should we choose?

• ““leftmost-outermost first” is guaranteed to leftmost-outermost first” is guaranteed to terminate if termination is possibleterminate if termination is possible• ““normalising”normalising”• The basis of Normal Order ReductionThe basis of Normal Order Reduction

Page 13: 10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look

04/19/2304/19/23 1313

Comparing strategies

• Normal Order ReductionNormal Order Reduction• Safe but slowSafe but slow• Corresponds to call-by-name parameter-passingCorresponds to call-by-name parameter-passing

• Applicative Order ReductionApplicative Order Reduction• ““leftmost-innermost first”leftmost-innermost first”• Fast but unsafe (may not terminate, = call-by-value)Fast but unsafe (may not terminate, = call-by-value)

( ( xx . . (x + x)(x + x) ) (3 + 5) ) (3 + 5)

( ( xx . . (3)(3) ) ( ( ) ( ( xx . . (x x)(x x) ) ( ) ( xx . . (x x)(x x) ) ) ) )

Page 14: 10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look

04/19/2304/19/23 1414

Reduction to Normal Form

• Practical implementations almost never reduce to Practical implementations almost never reduce to full NF (wasted computation)full NF (wasted computation)

• Weak Head Normal FormWeak Head Normal Form

• Head Normal FormHead Normal Form

• Normal FormNormal Form

x . ( ( x . ( ( y . ( + ) ) 4 (5 + 6) )y . ( + ) ) 4 (5 + 6) )

x . ( + (5 + 6) )x . ( + (5 + 6) )

x . ( + 11 )x . ( + 11 )

Page 15: 10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look

04/19/2304/19/23 1515

Reduction to Normal Form

• NF is uniqueNF is unique• HNF is not uniqueHNF is not unique• Principal HNF is uniquePrincipal HNF is unique

• always reduce the leftmost (“head”) redexalways reduce the leftmost (“head”) redex

• WHNF not unique WHNF not unique • Different only when result is a functionDifferent only when result is a function

Page 16: 10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look

04/19/2304/19/23 1616

WHNF and Principal HNF

• Both principal HNF and WHNF result from Normal Both principal HNF and WHNF result from Normal Order ReductionOrder Reduction• Leftmost-outermost redex first (the “head” redex)Leftmost-outermost redex first (the “head” redex)

• WHNF:WHNF:• If leftmost-outermost redex is a function, DO NOT If leftmost-outermost redex is a function, DO NOT --

reduce inside the reduce inside the -body-body• Lazy Evaluation!Lazy Evaluation!

• Principal HNF:Principal HNF:• If leftmost-outermost redex is a function, DO If leftmost-outermost redex is a function, DO -reduce -reduce

inside the inside the -body, but apply a normal order strategy -body, but apply a normal order strategy (leftmost-outermost first)(leftmost-outermost first)

Page 17: 10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look

04/19/2304/19/23 1717

Summary

• Revision: syntax and rulesRevision: syntax and rules -reduction and -reduction and -renaming-renaming

• Free variable captureFree variable capture

• Reduction stategiesReduction stategies• Church-Rosser theoremChurch-Rosser theorem• Normal Order, Applicative OrderNormal Order, Applicative Order

• Normal Forms: NF, HNF, WHNFNormal Forms: NF, HNF, WHNF

Page 18: 10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look

04/19/2304/19/23 1818

Enough Lambda Calculus!

• We will return to the Lambda Calculus later in the We will return to the Lambda Calculus later in the course:course:• to see how it can be used in the implementation of a to see how it can be used in the implementation of a

high-level programming languagehigh-level programming language• if time permits: to investigate recursionif time permits: to investigate recursion

• Next we will look at how to program in a high-Next we will look at how to program in a high-level (functional) language based on the lambda level (functional) language based on the lambda calculus - Mirandacalculus - Miranda