equational programming 2020 11 02 lecture 3 lambda
play

equational programming 2020 11 02 lecture 3 lambda-calculus in a - PowerPoint PPT Presentation

equational programming 2020 11 02 lecture 3 lambda-calculus in a nutshell -terms: variable x constant c abstraction ( x . M ) application ( F M ) we consider -terms modulo -conversion -reduction rule: ( x . M ) N M [ x


  1. equational programming 2020 11 02 lecture 3

  2. lambda-calculus in a nutshell λ -terms: variable x constant c abstraction ( λ x . M ) application ( F M ) we consider λ -terms modulo α -conversion β -reduction rule: ( λ x . M ) N → β M [ x := N ]

  3. beta reduction is non-deterministic however: gives unique normal forms see (later): confluence is non-terminating however: there are normalizing strategies see: strategies

  4. beta-normal form: definition and lemma intuition: a normal form (NF) is a result of a computation definition: a λ -term is a β -normal form if it does not contain a β -redex so it cannot do a β -reduction step lemma: M is a β -normal form if and only if M = λ x . M 0 with M 0 a normal form, or M = x M 1 . . . M n with n ≥ 0 and M 1 , . . . , M n normal forms

  5. termination: definitions M is terminating or strongly normalizing if all β -reduction sequences starting in M are finite M is weakly normalizing if there exists a β -reduction sequence starting in M that ends in a β -normal form or: M has a normal form termination and weak normalization are undecidable M is a normal form implies M has a normal form but not vv

  6. weak and strong normalization: quiz ( λ x . x x )( λ fy . ( f y )) is SN ( λ x . x Ω) ( λ y . z ) is WN but not SN ( λ x . x Ω) ( λ y . y ) is not WN ( λ x . x x x ) ( λ x . x x x ) is not WN

  7. overview fixed point combinators strategies call by value or leftmost-innermost call by need lazy reduction

  8. fixed point definition: x ∈ A is a fixed point of f : A → B if f ( x ) = x examples: 0 and 1 are fixed points of f : R → R with x �→ x 2 for λ -calculus: M is a fixed point of F if F M = β M example: every term M is a fixed point of I

  9. towards fixed points take a term F and consider W = λ x . F ( x x ) we have: W W = ( λ x . F ( x x )) W → β F ( W W ) so F ( W W ) = β W W

  10. fixed point combinator definition: Y is een fixed point combinator if F ( Y F ) = β Y F for every λ -term F informally: we can use Y to construct a fixed point for a given term F

  11. fixed point combinators Curry’s fixed point combinator: Y = λ f . ( λ x . f ( x x )) ( λ x . f ( x x )) Turing’s fixed point combinator: T = ( λ x . λ y . y ( x x y )) ( λ x . λ y . y ( x x y ))

  12. consider Curry’s fixed point combinator for an arbirary F we have: Y F = ( λ f . ( λ x . f ( x x )) ( λ x . f ( x x ))) F ( λ x . F ( x x )) ( λ x . F ( x x )) → β F (( λ x . F ( x x )) ( λ x . F ( x x ))) → β F (( λ f . ( λ x . f ( x x )) ( λ x . f ( x x ))) F ) ← = F ( Y F ) and also: F (( λ x . F ( x x )) ( λ x . F ( x x ))) → β F ( F (( λ x . F ( x x )) ( λ x . F ( x x ))))

  13. consider Turing’s fixed point combinator for an arbitrary F we have: T F = ( λ x . λ y . y ( x x y )) ( λ x . λ y . y ( x x y )) F ( λ y . y (t t y )) F → β F (tt F ) → β = F (T F ) with t = λ x . λ y . y ( x x y )

  14. use fixed point operator: example (Hindley) question: define X such that X y = β X (a garbage disposer)

  15. use fixed point operator: example (Hindley) question: define X such that X y = β X (a garbage disposer) X y = β X

  16. use fixed point operator: example (Hindley) question: define X such that X y = β X (a garbage disposer) X y = β X follows from X = β λ y . X

  17. use fixed point operator: example (Hindley) question: define X such that X y = β X (a garbage disposer) X y = β X follows from X = β λ y . X follows from X = β ( λ x . λ y . x ) X

  18. use fixed point operator: example (Hindley) question: define X such that X y = β X (a garbage disposer) X y = β X follows from X = β λ y . X follows from X = β ( λ x . λ y . x ) X follows from X = β Y ( λ x . λ y . x )

  19. use fixed point operator: example (Hindley) question: define X such that X y = β X (a garbage disposer) X y = β X follows from X = β λ y . X follows from X = β ( λ x . λ y . x ) X follows from X = β Y ( λ x . λ y . x ) so define X = Y ( λ x . λ y . x )

  20. overview fixed point combinators strategies call by value or leftmost-innermost call by need lazy reduction

  21. reduction graph of a λ -term terms are the vertices and (all) the reduction steps are the edges a reduction graph may be finite and cycle-free; example: I x a reduction graph may be finite with cycles; example: Ω a reduction graph may be infinite; example: ( λ x . x x x ) ( λ x . x x x ) a reduction graph is not necessarily simple; example: I ( I I ) a reduction graph may be nice to draw; example: ( λ x . I x x ) ( λ x . I x x )

  22. strategy: informal definitions there may be different ways to reduce a term a strategy tells us how to reduce a term more precisely: a subset of → β with the same normal forms a term may be weakly normalizing (WN) but not terminating (SN) a normalizing strategy yields a reduction to normal form if possible a perpetual strategy yields an infinite reduction if possible in general: a strategy gives us a reduction with a desired property

  23. outermost and innermost redexes a redex occurrence is outermost if it is not contained in another redex all outermost redexes of a term are parallel parallel means: no redex is contained in another redex a redex occurrence is innermost if it does not contain another redex all innermost redexes of a term are parallel a set of parallel redexes has a unique leftmost redex which is the leftmost one in the term tree

  24. redexes properties outermost redex every term not in NF has an outermost redex and it has a unique leftmost-outermost redex innermost redex every term not in NF has an innermost redex and it has a unique leftmost-innermost redex lazy redex every term not in WHNF has a lazy redex lazy redex is unique

  25. call by value reduce in every step the leftmost-innermost redex it is not normalizing: ( λ x . y ) Ω → β ( λ x . y ) Ω → β ( λ x . y ) Ω → β . . . it does not copy redexes (example): ( λ x . f x x ) ((( λ x . x ) a )) → β ( λ x . f x x ) a → β f a a it may contract redexes that are not needed: ( λ x . y ) ( I z ) → β ( λ x . y ) z → β y

  26. call by value: + and − + redexes are not copied + easy to implement - not normalizing - a reduction to normal form may reduce redexes that do not contribute to normal form

  27. call by need or leftmost-outermost reduce in every step the leftmost-outermost redex it is normalizing (theorem) example: ( λ x . f x x ) (( λ x . x ) 3) → β f (( λ x . x ) 3) (( λ x . x ) 3) → β f 3 (( λ x . x ) 3) → β f 3 3 example: ( λ x . y ) Ω → β y

  28. call by need: + and − + normalizing + all steps contribute to the normal form - redexes may be copied - difficult to implement

  29. call by need is normalizing proof by Haskell B. Curry 1958 (also recent work) intuition: we get rid of the outermost redex only by reducing it Haskell (the programming language) uses a form of call by need

  30. the rightmost-outermost strategy is not normalizing: (( λ x . λ y . x ) I ) Ω → (( λ x . λ y . x ) I ) Ω → . . .

  31. two strategies call by value or leftmost-innermost or applicative order close to what is done in ML call by need or leftmost-outermost or normal order close to what is done in Haskel (but we will refine this more) example: consider Y u with Y = λ f . ( λ x . f ( x x )) ( λ x . f ( x x ))

  32. compare with Haskell: strategies and lazy evaluation Haskell is a lazy functional programming language that is: do at least as possible, do it last moment, do it at most once how does this correspond to our lambda-calculus model ? very roughly: leftmost-outermost reduction now we refine this a bit

  33. evaluation in Haskell outermost, however, built-in operators are evaluated first example: 0 ∗ (head[1 , 2]) first evaluates head because * is strict in λ -calculus: leftmost-outermost reduction strategy not below a lambda example: \x -> False && x is not evaluated in λ -calculus: reduction to weak head normal form sharing in λ -calculus: only idea

  34. lambda calculus closer to Haskell our basic model: programs are terms, evaluation is β -reduction, a result is a normal form refined model closer to Haskell: programs are terms, evaluation is leftmost-outermost reduction (with sharing), a result is a weak head normal form (what is that?)

  35. weak head normal form (WHNF) intuition of a WHNF: at least one symbol is stable under reduction definition of a WHNF: λ x . P with P an arbitrary λ -term x P 1 . . . P n with n ≥ 0 and P 1 , . . . , P n arbitrary λ -terms examples of WHNFs: x λ x . x x Ω λ x . Ω every normal form is a WHNF, but not necessarily the other way around

  36. sharing intuition (technically non-trivial): ( λ x . x x ) ( I I ) let x = I I in x x → β let x = I in x x → β let x = I in I x → let x = I in x → β let x = I in I → we do not consider sharing in the lambda-calculus part

  37. lazy evaluation in lambda calculus goal: weak head normal form (WHNF) strategy: contract the lazy redex, that is: the leftmost-outermost redex of a term that is not in WHNF this is related to the evaluation strategy of Haskell

  38. infinite lists in Haskell use lazy evaluation in Haskell lists may be finite or potentially infinite ones = 1 : ones we can also compute with infinite lists head ones gives output 1 thanks to lazy evaluation! (what would innermost reduction give?)

Recommend


More recommend