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
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
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
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
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
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
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
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
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
Linear Head Reduction Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 7 / 30
Linear head reduction, informally (Danos & Regnier, ≈ 1990 ) Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 8 / 30
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
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
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
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
σ -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
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
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
Classical LHR Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 14 / 30
λµ -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
λ 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
Towards Call-by-need Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 17 / 30
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