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

classical by need
SMART_READER_LITE
LIVE PREVIEW

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 .


slide-1
SLIDE 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

slide-2
SLIDE 2

The Two Faces of Computation on Demand

∆ = λx.(x)x, Ω = (∆)∆, I = λy.y

Unnecessary computations in call-by-value: M = (λx.I)Ω →CBN I M = (λx.I)Ω →CBV M →CBV M →CBV ... Duplication of computations in call-by-name: N = (∆)(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

Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 2 / 30

slide-3
SLIDE 3

The Two Faces of Computation on Demand

∆ = λx.(x)x, Ω = (∆)∆, I = λy.y

Unnecessary computations in call-by-value: M = (λx.I)Ω →CBN I M = (λx.I)Ω →CBV M →CBV M →CBV ... Duplication of computations in call-by-name: N = (∆)(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 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

slide-4
SLIDE 4

Call-by-need λ-calculus

Ariola-Felleisen, JFP 97

Syntax

terms t ::= x | | λx.t | | (t)t values V ::= λx.t 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

slide-5
SLIDE 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

slide-6
SLIDE 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

slide-7
SLIDE 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

slide-8
SLIDE 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

slide-9
SLIDE 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

slide-10
SLIDE 10

Linear Head Reduction

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

slide-11
SLIDE 11

Linear head reduction, informally

(Danos & Regnier, ≈ 1990)

Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 8 / 30

slide-12
SLIDE 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

slide-13
SLIDE 13

Krivine Abstract Machine

Closures c ::= (t,σ) Environments σ ::= / 0 | σ +(x := c) Stacks π ::= ε | c·π Processes p ::= c | π Push ((t)u,σ) | π → (t,σ) | (u,σ)·π Pop (λx.t,σ) | c·π → (t,σ +(x := c)) | π Grab (x,σ +(x := c)) | π → c | π Garbage (x,σ +(y := c)) | π → (x,σ) | π

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

slide-14
SLIDE 14

Krivine Abstract Machine

Closures c ::= (t,σ) Environments σ ::= / 0 | σ +(x := c) Stacks π ::= ε | c·π Processes p ::= c | π Push ((t)u,σ) | π → (t,σ) | (u,σ)·π Pop (λx.t,σ) | c·π → (t,σ +(x := c)) | π Grab (x,σ +(x := c)) | π → c | π Garbage (x,σ +(y := c)) | π → (x,σ) | π

Is this really (weak) head reduction?

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

slide-15
SLIDE 15

Krivine Abstract Machine

Closures c ::= (t,σ) Environments σ ::= / 0 | σ +(x := c) Stacks π ::= ε | c·π Processes p ::= c | π Push ((t)u,σ) | π → (t,σ) | (u,σ)·π Pop (λx.t,σ) | c·π → (t,σ +(x := c)) | π Grab (x,σ +(x := c)) | π → c | π Garbage (x,σ +(y := c)) | π → (x,σ) | π

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

slide-16
SLIDE 16

σ-equivalence

(Danos & Regnier, ≈ 1990)

(λx1.t)u1u2 =σ (λx1.(t)u2)u1 (λx1.λx2.t)u =σ λx2.(λx1.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

slide-17
SLIDE 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 ::= [·] | | (E)t | | λx.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

slide-18
SLIDE 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 t0 and σ0 such that (t,σ) | π − →∗

Push,Pop (t0,σ0) | π

there exists C0 such that t = C0[t0].

[C ]σ defined by induction over C as follows: [[·]]σ ≡ / [C1[λx.C2] t]σ ≡ [C1]σ +(x := (t,σ))+[C2]σ+[C1]σ +(x:=(t,σ))

Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 13 / 30

slide-19
SLIDE 19

Classical LHR

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

slide-20
SLIDE 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

slide-21
SLIDE 21

λclh is classical LHR

Definition (µ-KAM)

σ ::= ··· | σ +(α := π) π ::= ··· | (α,σ) (µα.c,σ) | π →Save (c,σ +(α := π)) | ε ([α]t,σ) | ε →Restore (t,σ) | σ(α) As expected, λclh simulates intensionally the µKAM:

Theorem

Let c1 →clh c2 where c1 := [α]L1[C [λx.L2[x]] t], then the substitution sequence of process c1 is either empty or of the form t :: ℓ where ℓ is the substitution sequence of process c2.

Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 16 / 30

slide-22
SLIDE 22

Towards Call-by-need

Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 17 / 30

slide-23
SLIDE 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

slide-24
SLIDE 24

(Step 1) Weak LHR

We need to track λ-abstractions that pertain to a closure context.

Definition (Marked λ-calculus)

t,u ::= x | (t)u | λx.t | ℓx.t We only consider well-balanced terms.

Definition (Marked closure contexts)

C ::=[·] | (C1[ℓx.C2])t Such contexts are a more structured version of explicit substitutions (C1[ℓx.C2])t ∼ = let x := t in C1[C2]

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

slide-25
SLIDE 25

(Step 1) Weak LHR

Definition (Weak LHR)

Weak left contexts Ew ::= [·] | (Ew)t | ℓx.Ew (βwlh) C [λx.t]u → C [ℓx.t]u C [ℓx.Ew[x]]t → C [ℓx.Ew[t]]t + congruence w.r.t. Ew This reduction is still stable by σ.

Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 20 / 30

slide-26
SLIDE 26

(Step 2) Call-by-“value” LHR

We restrict substitution to values-up-to closures: W ::= C [λx.t] and adapt the contexts accordingly: Value left contexts Ev ::= [·] | (Ev)t | ℓx.Ev | (C [ℓx.Ev

1[x]])Ev 2

The call-by-value weak LHR is then obtained straightforwardly:

Definition (By-value LHR)

(βwlv) C [λx.t]u → C [ℓx.t]u C [ℓx.Ev[x]]W → C [ℓx.Ev[W]]W + congruence w.r.t. Ev Still stable by σ.

Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 21 / 30

slide-27
SLIDE 27

By-value ?

  • λwlv already implements a call-by-need strategy
  • Not a reduction scheme from the literature, though.

There is a duplication of computation: (C ′[ℓx.Ev[x]])C [V] → (C ′[ℓx.Ev[C [V]]])C [V] C is copied, which will end up in recomputing its bound terms if ever they are going to be used throughout the reduction.

Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 22 / 30

slide-28
SLIDE 28

(Step 3) Closure sharing

Solve this similarly to the Assoc rule in Ariola-Felleisen’s calculus:

Definition (By-value LHR with sharing)

(βwls) C [λx.t]u → C [ℓx.t]u C ′[ℓx.Ev[x]]C [V] → C [C ′[ℓx.Ev[V]]V] + congruence w.r.t. Ev

Theorem

λwls is essentially Chang-Felleisen’s calculus.

Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 23 / 30

slide-29
SLIDE 29

Classical By-need

(At last!)

Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 24 / 30

slide-30
SLIDE 30

Classical By Need

Following the same three steps...

C ::= [·] | (C1[ℓx.C2])t | C1[µα.Kv[[α]C2]] Ev ::= [·] | (Ev)t | ℓx.Ev | (C [ℓx.Ev

1[x]])Ev 2 | µα.Kv[[α]Ev]

Kv ::= [·] | [α]Ev[µβ.Kv]

Definition (Classical-by-need)

(βcls) C [λx.t]u → C [ℓx.t]u C ′[ℓx.Ev[x]]C [V] → C [C ′[ℓx.Ev[V]]V] C ′[ℓx.Ev[x]]C [µα.c] → C [µα.c{α := [α](C ′[ℓx.Ev[x]])_}] + congruence w.r.t. Kv

Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 25 / 30

slide-31
SLIDE 31

A bit too powerful

  • A very smart stack substitution!
  • Thanks to closure contexts, never need to substitute stacks eagerly
  • ... except when a µα.c term needed

This does not look like anything known from the literature, so we can’t relate it to a previous calculus...

Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 26 / 30

slide-32
SLIDE 32

A Dumber Classical By Need

C ::= [·] | (C1[ℓx.C2])t Ev ::= [·] | (Ev)t | ℓx.Ev | (C [ℓx.Ev

1[x]])Ev 2

Kv ::= [·] | [α]Ev[µβ.Kv]

Definition (Classical-by-need with Intuitionistic Contexts)

(βcls′) C [λx.t]u → C [ℓx.t]u C ′[ℓx.Ev[x]]C [V] → C [C ′[ℓx.Ev[V]]V] [α]Ev[µβ.Kv[[β]t] → [α]Ev[µβ.Kv[[α]Ev[t]] + congruence w.r.t. Kv

Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 27 / 30

slide-33
SLIDE 33

Comparison with AHS classical call-by-need calculus

  • Ariola, Herbelin and S. proposed a classical by-need λ-calculus

derived from a call-by-need λµ ˜ µ-calculus.

  • In that calculus, β is implemented by plain βv-rule, a feature of

sequent calculus.

  • Correspondence with a modified version of this calculus, AHS’,

featuring a deref-rule à la Ariola-Felleisen:

Theorem

For any command c, there exists an infinite standard reduction in AHS’-calculus starting from c iff there exists an infinite reduction starting from c in the classical by-need calculus with Intuitionistic contexts.

Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 28 / 30

slide-34
SLIDE 34

Conclusion

  • Reformulation of LHR;
  • Extension to the λµ-calculus / classical logic;
  • Connection between LHR and call-by-need by deriving call-by-need

from LHR. Surprisingly, this connection seemed to have remained unexploited (and unnoticed?) until our work and Accattolli et al work.

Lazy = Demand-driven + Memoization + Sharing (weak LHR) (by value) (closure shar.)

  • Closure contexts are not new but we made explicit their central role

for both LHR and call-by-need, which are essentially calculi with reductions up-to closure contexts.

  • We defined a classical by-need calculus, again from LHR.

Pierre-Marie Pédrot & Alexis Saurin Classical-by-Need 7th April 2016 29 / 30

slide-35
SLIDE 35

Thanks

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