reductions
play

Reductions So far, two reductions that preserve the CS 611 - PDF document

Reductions So far, two reductions that preserve the CS 611 meaning of a lambda calculus Advanced Programming Languages expression: Andrew Myers ( x e ) ( x e { x / x }) (if x FV e ) Cornell University


  1. Reductions • So far, two reductions that preserve the CS 611 meaning of a lambda calculus Advanced Programming Languages expression: Andrew Myers α ( λ x e ) → ( λ x’ e { x’ / x }) (if x ’ ∉ FV � e � ) Cornell University β (( λ x e 1 ) e 2 ) → e 1 { e 2 / x } Lecture 9: Reduction orders and normal forms CS 611 Fall '00 -- Andrew Myers, Cornell University 2 Extensionality Reductions • Two functions are equal by extension if • Three reductions that preserve the they have the same meaning: they give meaning of a lambda calculus the same result when applied to the expression (open or closed) same argument • With lazy evaluation, expressions α ( λ x ( e x )) and e are equal by extension ( λ x e ) → ( λ x � e { x � / x }) (if x � ∉ FV � e � ) β (( λ x e 1 ) e 2 ) → e 1 { e 2 / x } ( λ x ( e x )) e � = e e � (if x ∉ FV � e � ) η ( λ x ( e x )) → e (if x ∉ FV � e � ) η • η -reduction: ( λ x ( e x )) → e (if x ∉ FV � e � ) CS 611 Fall '00 -- Andrew Myers, Cornell University 3 CS 611 Fall '00 -- Andrew Myers, Cornell University 4 Normal form Normal order • Lazy evaluation (call-by-name) • A lambda expression is in normal form e 0 � ( λ x e 2 ) when no reductions can be performed ( e 0 e 1 ) � e 2 { e 1 / x } on it or on any of its sub-expressions • Normal order evaluation : apply β (or η ) reductions • Normal form is defined relative to a set of allowed reductions – is a value to leftmost redex till no reductions can be applied ( normal form ) • Reducible expressions are called • Always finds a normal form if there is one redexes • Substitutes unevaluated form of actual parameters • What is the normal form for • Hard to understand, implement with imperative lang. LOOP = (( λ x x) ( λ x x)) ? CS 611 Fall '00 -- Andrew Myers, Cornell University 5 CS 611 Fall '00 -- Andrew Myers, Cornell University 6 1

  2. Applicative order Divergence • (single-argument) call-by-value: only β - • Applicative order may diverge even substitute when the argument is fully reduced: argument evaluated before call when a normal form exists e 0 → e � 0 • Example: ( e 0 e 1 ) → ( e � 0 e 1 ) (( λ b c) LOOP ) • Need special non-strict if form: e 1 → e � 1 ( IF TRUE 0 Y) ( v e 1 ) → ( v e � 1 ) • What if we allow any arbitrary order of evaluation? (( λ x e ) v ) → e { v / x } CS 611 Fall '00 -- Andrew Myers, Cornell University 7 CS 611 Fall '00 -- Andrew Myers, Cornell University 8 Non-deterministic Church-Rosser theorem evaluation • Non-determinism in evaluation order does e 0 → e � 0 e → e � not result in non-determinism of result e 0 ( e 0 e 1 ) → ( e � 0 e 1 ) ( λ x e ) → ( λ x e � ) • Formally: e 1 e 2 e 1 → e � 1 ( e 0 → * e 1 ∧ e 0 → * e 2 ) e 3 ( e 0 e 1 ) → ( e 0 e � 1 ) � ∃ e 3 . e 1 → * e 3 ∧ e 2 → * e’ 3 ∧ e 3 = e’ 3 • Implies: only one normal form for an α expression (( λ x e 1 ) e 2 ) → e 1 { e 2 / x } (β) • Transition relation → has the Church-Rosser property or diamond property if this theorem is true ( x ∉ FV � e � ) (η) • β+η, β− only evaluation have this property ( λ x ( e x )) → e CS 611 Fall '00 -- Andrew Myers, Cornell University 9 CS 611 Fall '00 -- Andrew Myers, Cornell University 10 Concurrency Evaluation Contexts • Let context C be an expression with a hole • Transition rules for application permit [ ⋅ ] where a redex may be reduced parallel evaluation of • C[ e ] with redex e reduces to some C[ e � ] operator and operand e 0 → e � 0 • Church-Rosser: any ( e 0 e 1 ) → ( e � 0 e 1 ) allowed interleaving • Normal order: C = [ ⋅ ] | C e gives same result e 1 → e � 1 C[( λ x e 1 ) e 2 ] → C[ e 1 { e 2 / x }] • Many commonly-used C[ λ x ( e x )] → C[ e ] (if x ∉ FV � e � ) ( e 0 e 1 ) → ( e 0 e � 1 ) languages do not have Church-Rosser property • Applicative order: C= [ ⋅ ] | C e | ( λ x e ) C C: int x=1, y = (x = 2)+x • Intuition: lambda calculus is functional; value C[( λ x e ) v ] → C[ e { v / x }] of expression determined locally (no store) CS 611 Fall '00 -- Andrew Myers, Cornell University 11 CS 611 Fall '00 -- Andrew Myers, Cornell University 12 2

  3. Simplifying λ λ calculus DeBruijn indices λ λ • Can we capture essential properties of • Idea: name of formal argument of abstraction is not needed lambda calculus in an even simpler e ::= λ e 0 | e 0 e 1 | n language? • Variable name n tells how many lambdas to • Can we get rid of (or restrict) variables? walk up in AST –S & K combinators: closed expressions are trees of applications of only S and K (no IDENTITY � ( λ a a) = ( λ 0) variables or abstractions!) TRUE � ( λ x ( λ y x)) = ( λ (λ 1)) –can reduce even to single combinator (X) FALSE = 0 � ( λ x ( λ y y)) = ( λ (λ 0)) –de-Bruijn indices: all variable names are 2 � ( λ f ( λ a (f (f a))) = ( λ (λ (1 (1 0)))) integers CS 611 Fall '00 -- Andrew Myers, Cornell University 13 CS 611 Fall '00 -- Andrew Myers, Cornell University 14 Evaluation tradeoffs Translating to DeBruijn indices • A function DB � e � that compiles a closed • Normal order reduction always finds lambda expression e into DeBruijn index normal form – but requires substitution representation of arbitrary expressions • Need extra argument N : Var → ω to keep • Applicative order reduction substitutes track of indices of each identifier DB � e � = T � e, Ø � only values – but may diverge T � ( e 0 e 1 ) � N = (T � e 0 � N T � e 1 � N ) “unnecessarily” T � x � N = N ( x ) • Can we do better? T � ( λ x e ) � N = ( λ T � e � ( λ y ∈ Var. if x = y then 0 else 1+ N ( y ))) CS 611 Fall '00 -- Andrew Myers, Cornell University 15 CS 611 Fall '00 -- Andrew Myers, Cornell University 16 3

Recommend


More recommend