classical by need
play

Classical-by-Need (A Classy Call-by-Need) Pierre-Marie Pdrot & - PowerPoint PPT Presentation

Classical-by-Need (A Classy Call-by-Need) Pierre-Marie Pdrot & Alexis Saurin 7th April 2016 ESOP 2016 Pierre-Marie Pdrot & Alexis Saurin Classical-by-Need 7th April 2016 1 / 30 The Two Faces of Computation on Demand = x .


  1. Classical-by-Need (A Classy Call-by-Need) Pierre-Marie Pédrot & Alexis Saurin 7th April 2016 ESOP 2016 Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 1 / 30

  2. The Two Faces of Computation on Demand ∆ = λ x . ( x ) x , Ω = ( ∆ ) ∆ , I = λ y . y Unnecessary computations in call-by-value: = ( λ x . I ) Ω → CBN I M = ( λ x . I ) Ω → CBV M → CBV M → CBV ... M Duplication of computations in call-by-name: = ( ∆ )( I ) I → CBN ( I ) I ( I ) I → CBN ( I )( I ) I → CBN ( I ) I → CBN I N = ( ∆ )( I ) I → CBV ( ∆ ) I → CBN ( I ) I → CBN I N Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 2 / 30

  3. The Two Faces of Computation on Demand ∆ = λ x . ( x ) x , Ω = ( ∆ ) ∆ , I = λ y . y Unnecessary computations in call-by-value: = ( λ x . I ) Ω → CBN I M = ( λ x . I ) Ω → CBV M → CBV M → CBV ... M Duplication of computations in call-by-name: = ( ∆ )( I ) I → CBN ( I ) I ( I ) I → CBN ( I )( I ) I → CBN ( I ) I → CBN I N = ( ∆ )( I ) I → CBV ( ∆ ) I → CBN ( I ) I → CBN I N Ideally, one would like to have one’s cake and eat it too: to postpone evaluating an expression (...) until it is clear that its value is really needed, but also to avoid repeated evaluation. (John Reynolds) Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 2 / 30

  4. Call-by-need λ -calculus Ariola-Felleisen, JFP 97 Syntax terms :: = x | | λ x . t | | ( t ) t t values :: = λ x . t V answers A :: = V | | ( λ x . A ) t evaluation contexts E :: = � | | Et | | ( λ x . E ) t | | ( λ x . E [ x ]) E Reductions ( deref ) ( λ x . E [ x ]) V → ( λ x . E [ V ]) V ( lift ) (( λ x . A ) t ) u → ( λ x . Au ) t ( assoc ) ( λ x . E [ x ]) ( λ y . A ) t → ( λ y . ( λ x . E [ x ]) A ) t Other calculi: Maraist et al, JFP 98: same standard reduction Ariola, Herbelin & S., TLCA 11: in λµ ˜ µ Chang & Felleisen, ESOP 12: single axiom call-by-need Accattoli et al., ICFP 14: explicit substitution call-by-need Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 3 / 30

  5. Classical By-need • Call-by-need is somehow an effect • Not distinguishable from by-name in a pure setting... Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 4 / 30

  6. Classical By-need • Call-by-need is somehow an effect • Not distinguishable from by-name in a pure setting... • But difference observable in presence of other effects! • Several possible interactions • In particular with first-class continuations Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 4 / 30

  7. Classical By-need Calculi? • Previous work: Ariola, Herbelin and S. formulated call-by-need strategies in λµ ˜ µ . • In such a setting: control built-in and by-need wrought out Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 5 / 30

  8. Classical By-need Calculi? • Previous work: Ariola, Herbelin and S. formulated call-by-need strategies in λµ ˜ µ . • In such a setting: control built-in and by-need wrought out • We provide a more canonical presentation of call-by-need • Inspired by this one weird trick from Linear Logic • Naturally provides a classical by-need calculus (actually several) Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 5 / 30

  9. Organization of the Talk • Linear Head Reduction • Classical Linear Head Reduction • From LHR to Call-by-need • Classical By-need Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 6 / 30

  10. Linear Head Reduction Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 7 / 30

  11. Linear head reduction, informally (Danos & Regnier, ≈ 1990 ) Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 8 / 30

  12. Comparison between LHR and call-by-need Striking similarities • Both can be viewed as optimization of standard evaluation strategies; • Both rely on a linear, rather than destructive, substitution; • A variable is substituted only if it is necessary for pursuing the computation; • Both share with call-by-name the same notion of convergence and the induced observational equivalences; • Not easily presented as reduction relation. Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 9 / 30

  13. Krivine Abstract Machine Closures c :: = ( t , σ ) Environments σ :: = 0 | σ +( x : = c ) / Stacks :: = ε | c · π π Processes :: = � c | π � p � (( t ) u , σ ) | π � → � ( t , σ ) | ( u , σ ) · π � Push � ( λ x . t , σ ) | c · π � → � ( t , σ +( x : = c )) | π � Pop � ( x , σ +( x : = c )) | π � → � c | π � Grab � ( x , σ +( y : = c )) | π � → � ( x , σ ) | π � Garbage Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 10 / 30

  14. Krivine Abstract Machine Closures c :: = ( t , σ ) Environments σ :: = 0 | σ +( x : = c ) / Stacks :: = ε | c · π π Processes :: = � c | π � p � (( t ) u , σ ) | π � → � ( t , σ ) | ( u , σ ) · π � Push � ( λ x . t , σ ) | c · π � → � ( t , σ +( x : = c )) | π � Pop � ( x , σ +( x : = c )) | π � → � c | π � Grab � ( x , σ +( y : = c )) | π � → � ( x , σ ) | π � Garbage Is this really (weak) head reduction? Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 10 / 30

  15. Krivine Abstract Machine Closures c :: = ( t , σ ) Environments σ :: = 0 | σ +( x : = c ) / Stacks :: = ε | c · π π Processes :: = � c | π � p � (( t ) u , σ ) | π � → � ( t , σ ) | ( u , σ ) · π � Push � ( λ x . t , σ ) | c · π � → � ( t , σ +( x : = c )) | π � Pop � ( x , σ +( x : = c )) | π � → � c | π � Grab � ( x , σ +( y : = c )) | π � → � ( x , σ ) | π � Garbage Is this really (weak) head reduction? Simulating is not the same as implementing. Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 10 / 30

  16. σ -equivalence (Danos & Regnier, ≈ 1990 ) ( λ x 1 . t ) u 1 u 2 = σ ( λ x 1 . ( t ) u 2 ) u 1 ( λ x 1 . λ x 2 . t ) u = σ λ x 2 . ( λ x 1 . t ) u � Originated in the theory of linear logic proof nets: Inspired by the translation of λ -terms in proof-nets and the induced identification. � A relation capturing the KAM behaviour. � Skips redexes ignored by the KAM. � Up to σ -equivalence, LHR is the usual head reduction, made linear. Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 11 / 30

  17. LHR as a calculus Insensitivity to σ -equivalence can be achieved by a context grammar: Definition ( λ lh ) closure contexts C :: = [ · ] | | ( C [ λ x . C ]) t left evaluation contexts :: = [ · ] | | ( E ) t | | λ x . E E ( β lh ) ( C [ λ x . E [ x ]]) t → ( C [ λ x . E [ t ]]) t + congruence w.r.t E Theorem • β lh is stable by σ -equivalence. • λ lh coincides with Danos-Regnier LHR. Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 12 / 30

  18. Closure Contexts and the KAM Push and Pop transitions implement the computation of closure contexts Proposition Let C be a closure context. There exists [ C ] σ such that: → ∗ � ( C [ t ] , σ ) | π � − Push , Pop � ( t , σ +[ C ] σ ) | π � Conversely, for all t 0 and σ 0 such that → ∗ � ( t , σ ) | π � − Push , Pop � ( t 0 , σ 0 ) | π � there exists C 0 such that t = C 0 [ t 0 ] . [ C ] σ defined by induction over C as follows: [[ · ]] σ ≡ / [ C 1 [ λ x . C 2 ] t ] σ ≡ [ C 1 ] σ +( x : = ( t , σ ))+[ C 2 ] σ +[ C 1 ] σ +( x : =( t , σ )) 0 Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 13 / 30

  19. Classical LHR Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 14 / 30

  20. λµ -calculus variant of the LHR Left stack contexts K : K :: = [ · ] | [ α ] L [ µβ . K ] Classical extension of left contexts and closure contexts: C :: = [ · ] | C 1 [ λ x . C 2 ] t | C 1 [ µα . K [[ α ] C 2 ]] L :: = [ · ] | λ x . L | L t | µβ . [ α ] L Classical LHR: The classical LHR is defined by the following reduction: C [ λ x . L [ x ]] t → clh C [ λ x . L [ t ]] t + congruence w.r.t. L . Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 15 / 30

  21. λ clh is classical LHR Definition ( µ -KAM) σ :: = ··· | σ +( α : = π ) π :: = ··· | ( α , σ ) � ( µα . c , σ ) | π � → Save � ( c , σ +( α : = π )) | ε � � ([ α ] t , σ ) | ε � → Restore � ( t , σ ) | σ ( α ) � As expected, λ clh simulates intensionally the µ KAM: Theorem Let c 1 → clh c 2 where c 1 : = [ α ] L 1 [ C [ λ x . L 2 [ x ]] t ] , then the substitution sequence of process c 1 is either empty or of the form t :: ℓ where ℓ is the substitution sequence of process c 2 . Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 16 / 30

  22. Towards Call-by-need Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 17 / 30

  23. From LHR to Call-by-need In three easy steps! 1 Weak LHR 2 Value passing 3 Closure sharing Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 18 / 30

Recommend


More recommend