1 / 14

CS 611: Lecture 10

CS 611: Lecture 10. More Lambda Calculus September 20, 1999 Cornell University Computer Science Department Andrew Myers. Why is substitution hard?. Substitution on lambda expressions: ( l y e 0 ) [ e 1 / x ]  ( l y’ e 0 [ y’/y ][ e 1 / x ]) where y ’  FV  e 0 

eman
Download Presentation

CS 611: Lecture 10

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 611: Lecture 10 More Lambda CalculusSeptember 20, 1999 Cornell University Computer Science Department Andrew Myers

  2. Why is substitution hard? • Substitution on lambda expressions: (l y e0) [e1 / x ] (l y’ e0[y’/y][e1/x ]) where y’  FV e0 y’  FV e1 • Abstract syntax DAGs without names: apply l (l x e) (e0 e1) e e0 e1 var CS 611—Semantics of Programming Languages—Andrew Myers

  3. l l var var Example On expressions: (l y e0) [e1 / x ] (l y’ e0[y’/y][e1/x ]) (l a(b(l b a))) [ (l a a) / b ] = (l a’ (b(l b a’)) [(l a a)/ b]) = (l a’ (b(l b a’)) [(l a a) / b]) = (l a’ ((l aa) (l b a’))) On graphs: l l ... ... apply apply l var l var var var var var CS 611—Semantics of Programming Languages—Andrew Myers

  4. Holes in scope • Reuse of an identifier creates a hole in scope (l a ((b(l a (b a))) a)) scope of outer “a” • Variable is inaccessible within the region in which it is shadowed • Common source of programming errors CS 611—Semantics of Programming Languages—Andrew Myers

  5. Reductions • Lambda calculus reductions • alpha (change of variable) (l x e)  (l x’ e[x’/x]) if (x’ FV e) • beta (application) (( x e1) e2)  e1[e2 / x] • eta (removal of extra abstraction) (l x (e x))  e if (x  FV e) • Expressions are equivalent if they • have the same Stoy diagram/abstract DAG • can be converted to each other using alpha renaming on sub-expressions • Evaluation is a sequence of reductions a b h CS 611—Semantics of Programming Languages—Andrew Myers

  6. Normal order • Currently defined operational semantics: lazy evaluation (call-by-name) e0 (lx e2) (e0e1)  e2 [ x / e1 ] • Normal order evaluation: apply b (or h) reductions to leftmost redex till no reductions can be applied (normal form) • Always finds a normal form if there is one • Substitutes unevaluated form of actual parameters • Hard to understand, implement with imperative lang. CS 611—Semantics of Programming Languages—Andrew Myers

  7. e11e’1 (e0e1) 1 (e0e’1) e01e’0 (e0v) 1 (e’0v) ((lxe) v) 1 e[v/x] Applicative order • Only b-substitute when the argument is fully reduced: argument evaluated before call • Almost call-by-value CS 611—Semantics of Programming Languages—Andrew Myers

  8. Applicative order • Applicative order may diverge even when a normal form exists • Example: ((b c) ((a (a a)) (a (a a)))) • Need special non-strictif form: (IFTRUE 0 Y) • What if we allow any arbitrary order of evaluation? CS 611—Semantics of Programming Languages—Andrew Myers

  9. x FV e (lx (e x))  1e Non-deterministic evaluation e01e’0 (e0e1) 1 (e’0e1) e1e’ (lxe) 1 (lxe’) e11e’1 (e0e1) 1 (e0e’1) (( x e1) e2) 1e1[e2 / x] (b) (h) CS 611—Semantics of Programming Languages—Andrew Myers

  10. e0 e1 e2 e3 Church-Rosser theorem • Non-determinism in evaluation order does not result in non-determinism of result • Formally: (e0 *e1  e0 *e2 )  e3 . e1 *e3 e2 *e’3  e3 = e’3 • Implies: only one normal form for an expression • Transition relation has the Church-Rosser property or diamond property if this theorem is true a CS 611—Semantics of Programming Languages—Andrew Myers

  11. e01e’0 (e0e1) 1 (e’0e1) e11e’1 (e0e1) 1 (e0e’1) Concurrency • Transition rules for application permit parallel evaluation ofoperator and operand • Church-Rosser: anyinterleaving of computationyields same result • Many commonly-usedlanguages do not haveChurch-Rosser property C: int x=1, y = (x = 2)+x • Intuition: lambda calculus is functional; value of expression determined locally (no store) CS 611—Semantics of Programming Languages—Andrew Myers

  12. Simplifying lambda calculus • Can we capture essential properties of lambda calculus in an even simpler language? • Can we get rid of (or restrict) variables? • S & K combinators: closed expressions are trees of applications of only S and K (no variables or abstractions!) • de-Bruijn indices: all variable names are integers CS 611—Semantics of Programming Languages—Andrew Myers

  13. DeBruijn indices • Idea: name of formal argument of abstraction is not needed e ::= le0 | e0e1 | n • Variable name n tells how many lambdas to walk up in AST IDENTITY (l a a) = (l 0) TRUE (l x (l y x)) = (l (l 1)) FALSE = 0  (l x (l y y)) = (l (l 0)) 2  (l f (l a (f (f a))) = (l (l (1 (1 0)))) CS 611—Semantics of Programming Languages—Andrew Myers

  14. Translating to DeBruijn indices • A function DB e that compiles a closed lambda expression e into DeBruijn index representation (closed = no free identifiers) • Need extra argument N : symbolinteger to keep track of indices of each identifier • DB e = T e, Ø • T (e0 e1), N  = (T e0 , N T e1 , N) • T x , N = N(x) • T (l xe) , N = (l T e, (l y . if x = y then 0 else 1+N(y))) CS 611—Semantics of Programming Languages—Andrew Myers

More Related