Download presentation
Presentation is loading. Please wait.
Published byPhilomena Pope Modified over 9 years ago
1
10/12/20151 GC16/3C11 Functional Programming Lecture 3 The Lambda Calculus A (slightly) deeper look
2
10/12/20152 Contents Revision: syntax and rules Revision: syntax and rules -reduction and -renaming Reduction stategies Reduction stategies Normal Forms Normal Forms
3
10/12/20153 Untyped Extended Syntax (2) program :: expression expression :: x | constant | constant | operator | operator | expression expression | expression expression | expression operator expression | expression operator expression | x. expression | x. expression | ( expression ) | ( expression )
4
10/12/20154 Using (“Applying”) a function To apply the previously defined function to the constant number 3: To apply the previously defined function to the constant number 3: ( ( x. ( x + 1 ) ) 3 )
5
10/12/20155 Rules for Evaluation (1) reduction reduction reduction ( if x not free in E) x. E y. E [ y / x ] x. E ) z ) E [ z / x ] x. ( E x ) E
6
10/12/20156 Rules for Evaluation (2) rules there is a separate rule for evaluating each primitive operator (such as +) Example: the rule for + says that 3 + 4 evaluates to 7 Name clashes : x. ( ( x. ( x + 3 ) ) ( x + 4 ) ) ) 5 x. ( 5 + 3 ) ) ( 5 + 4 ) WRONG!!!
7
10/12/20157 reduction and renaming The only tricky part of the rules The only tricky part of the rules e1[e2/x] must replace only FREE occurrences of x in e1 with e2 e1[e2/x] must replace only FREE occurrences of x in e1 with e2 x. ( ( x. ( x + 3 ) ) ( x + 4 ) ) ) 5 ( x. ( x + 3 ) ) ( x + 4 ) FREE x ( x. ( x + 3 ) ) ( 5 + 4 ) ( x. ( x + 3 ) ) ( 5 + 4 )
8
10/12/20158 reduction and renaming Identifying free variables in e1 is not enough! Identifying free variables in e1 is not enough! Free Variable Capture: Free Variable Capture: f. ( a. ( f a) ) ) f. ( f a) ) BOUND a FREE a ( a. ( f. ( f a) ) a) ) CAPTURED a
9
10/12/20159 reduction and renaming Rule: during reduction, if e2 contains any free variables that are bound in e1, then for each such free variable, it must be - converted inside e1 BEFORE doing - reduction! Rule: during reduction, if e2 contains any free variables that are bound in e1, then for each such free variable, it must be - converted inside e1 BEFORE doing - reduction! f. ( a. ( f a) ) ) f. ( f a) ) f. ( b. ( f b) ) ) f. ( f a) ) ( b. ( f. ( f a) ) b) )
10
10/12/201510 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” or NF An expression containing no redexes is in “Normal Form” or NF Execution = successive -reduction to NF Execution = successive -reduction to NF Whether an arbitrary e has a NF is undecidable (Halting Problem) Whether an arbitrary e has a NF is undecidable (Halting Problem) Many different sequences of -reduction are possible – how does this affect the result? Many different sequences of -reduction are possible – how does this affect the result?
11
10/12/201511 Church-Rosser Theorem The Church-Rosser theorem proves that all sequences (strategies) that terminate will converge on the same NF The Church-Rosser theorem proves that all sequences (strategies) that terminate will converge on the same NF Though some may not terminate! Though some may not terminate! Corollary: the NF for a given expression is unique (if it exists) Corollary: the NF for a given expression is unique (if it exists) So -reductions can be done in any order So -reductions can be done in any order Even in parallel! Even in parallel!
12
10/12/201512 Normalising orders Not all strategies (orders of performing - reductions) terminate Not all strategies (orders of performing - reductions) terminate So which should we choose? So which should we choose? “leftmost-outermost first” is guaranteed to terminate if termination is possible “leftmost-outermost first” is guaranteed to terminate if termination is possible “normalising” “normalising” The basis of Normal Order Reduction The basis of Normal Order Reduction
13
10/12/201513 Comparing strategies Normal Order Reduction Normal Order Reduction Safe but slow Safe but slow Corresponds to call-by-name parameter-passing Corresponds to call-by-name parameter-passing Applicative Order Reduction Applicative 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) ( x. (x + x) ) (3 + 5) ( x. (3) ) ( ( x. (x x) ) ( x. (x x) ) )
14
10/12/201514 Reduction to Normal Form Practical implementations almost never reduce to full NF (wasted computation) Practical implementations almost never reduce to full NF (wasted computation) Weak Head Normal Form Weak Head Normal Form Head Normal Form Head Normal Form Normal Form Normal Form x. ( ( y. ( + ) ) 4 (5 + 6) ) x. ( + (5 + 6) ) x. ( + 11 )
15
10/12/201515 Reduction to Normal Form NF is unique NF is unique HNF is not unique HNF is not unique Principal HNF is unique Principal HNF is unique always reduce the leftmost (“head”) redex always reduce the leftmost (“head”) redex WHNF not unique WHNF not unique Different only when result is a function Different only when result is a function
16
10/12/201516 WHNF and Principal HNF Both principal HNF and WHNF result from Normal Order Reduction Both principal HNF and WHNF result from Normal Order 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 - reduce inside the -body If leftmost-outermost redex is a function, DO NOT - reduce inside the -body Lazy Evaluation! Lazy Evaluation! Principal HNF: Principal HNF: If leftmost-outermost redex is a function, DO -reduce inside the -body, but apply a normal order strategy (leftmost-outermost first) If leftmost-outermost redex is a function, DO -reduce inside the -body, but apply a normal order strategy (leftmost-outermost first)
17
10/12/201517 Summary Revision: syntax and rules Revision: syntax and rules -reduction and -renaming Free variable capture Free variable capture Reduction stategies Reduction stategies Church-Rosser theorem Church-Rosser theorem Normal Order, Applicative Order Normal Order, Applicative Order Normal Forms: NF, HNF, WHNF Normal Forms: NF, HNF, WHNF
18
10/12/201518 Enough Lambda Calculus! We will return to the Lambda Calculus later in the course: We will return to the Lambda Calculus later in the course: to see how it can be used in the implementation of a high-level programming language to see how it can be used in the implementation of a high-level programming language if time permits: to investigate recursion if time permits: to investigate recursion Next we will look at how to program in a high- level (functional) language based on the lambda calculus - Miranda Next we will look at how to program in a high- level (functional) language based on the lambda calculus - Miranda
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.