Compositional Semantics for Composable Continuations From Abortive - - PowerPoint PPT Presentation

compositional semantics for composable continuations
SMART_READER_LITE
LIVE PREVIEW

Compositional Semantics for Composable Continuations From Abortive - - PowerPoint PPT Presentation

Compositional Semantics for Composable Continuations From Abortive to Delimited Control Paul Downen Zena M. Ariola University of Oregon ICFP14 September 1, 2014 The big picture Effects that manipulate control flow, compositionally


slide-1
SLIDE 1

Compositional Semantics for Composable Continuations

From Abortive to Delimited Control

Paul Downen Zena M. Ariola

University of Oregon

ICFP’14 — September 1, 2014

slide-2
SLIDE 2

The big picture

◮ Effects that manipulate control flow, compositionally

◮ Programs can refer to their context, but . . . ◮ Still have local, equational reasoning inside open programs

◮ Logic is an inspiration, . . .

◮ Lessons from logic can fix problems in programming

◮ Even with an untyped mindset

◮ Sometimes, being type-agnostic is liberating!

slide-3
SLIDE 3

Classical control

◮ callcc is the classic control operator, going back to Scheme ◮ Classical control corresponds to classical logic (Griffin, 1990) ◮ Start with pure language, add primitive operations

◮ Start with intuitionistic logic, add classical axioms

◮ Start with a language with continuation variables

◮ Start with a logic with multiple conclusions

slide-4
SLIDE 4

Delimited control

◮ Delimit the scope of effects ◮ Continuations compose like functions ◮ Vastly more expressive power than classical control

◮ Every monadic effect is simulated by delimited control (Filinski, 1994) ◮ Exposes “monadic plumbing” underlying CBV languages

slide-5
SLIDE 5

Roadmap from classical to delimited control Classical λ + callcc λµ

slide-6
SLIDE 6

Roadmap from classical to delimited control Classical λ + callcc λµ Λµ

syntactic relaxation

slide-7
SLIDE 7

Roadmap from classical to delimited control Classical Delimited λ + callcc λ + shift0 + reset0 λµ Λµ

syntactic relaxation

slide-8
SLIDE 8

Classical control

slide-9
SLIDE 9

Operational semantics of callcc

◮ Extension of CBV λ-calculus

V ::= x | | λx.M | | callcc built-in function | | [E] reified evaluation context M, N ::= V | | M N E ::= | | E M | | V E E[(λx.M) V ] → E[M {V /x}] E[callcc V ] → E[V [E]] E[[E ′] V ] → E ′[V ]

slide-10
SLIDE 10

Equational theory for callcc

◮ Reason more generally about open programs ◮ Extension of λc (Moggi, 1989)

βv (λx.M) V = M {V /x} ηv λx.V x = V βΩ (λx.E[x]) M = E[M]

◮ Add axioms that explain behavior of built-in callcc function

(Sabry and Felleisen, 1993; Sabry, 1996)

slide-11
SLIDE 11

Problems of non-compositionality

◮ Equational theory weaker than operational semantics! ◮ Some programs can be evaluated to a value. . .

callcc(λk.λx.k (λ .x)) → → (λx.[] (λ .x))

◮ But the equational theory for callcc cannot reach a value!

callcc(λk.λx.k (λ .x)) = V

◮ How can we know that we have the “whole” context?

slide-12
SLIDE 12

Of jumps and the extent of a continuation

◮ Calling a continuation never returns — it “jumps”

◮ E[[E ′] 1] “jumps” out of E to E ′ ◮ Add variables α, β, . . . that stand for continuations ◮ Applying a continuation (variable) “jumps” (a.k.a. “aborts”)

◮ A jump α M is the same when inside a larger evaluation context

E[α M] = α M E is garbage

◮ A jump delimits the usable extent of a continuation

slide-13
SLIDE 13

A running jump

◮ Let’s try that again ◮ We can evaluate a jump to an answer. . .

α (callcc(λk.λx.k (λ .x))) → → α (λx.[α ] (λ .x))

◮ And the equational theory for callcc reaches that answer!

α (callcc(λk.λx.k (λ .x))) = α (λx.α (λ .x))

slide-14
SLIDE 14

λµ: taking jumps seriously

◮ Syntactically distinguish jumps as “commands”

M, N ::= . . . | | µα.c control abstraction c :: = [α]M command, a.k.a “jump”

◮ Commands “run”

[α](E[(λx.M)V ]) → [α](E[M {V /x}]) [α](E[µβ.c]) → c {[α](E[N])/[β]N}

slide-15
SLIDE 15

λµ: a language of classical logic

◮ Developed as calculus for classical logic (Parigot, 1992) ◮ Originally CBN, but also CBV (extension of λc):

µE [α](E[µβ.c]) = c {[α](E[N])/[β]N} ηµ µα.[α]M = M βµ (λx.µα.[β]M) N = µα.[β]((λx.M) N)

◮ Equational theory contains operational semantics ◮ λµ ≡ λ + callcc!

slide-16
SLIDE 16

Relaxing the syntax

slide-17
SLIDE 17

Λµ: a more relaxed language

◮ Collapse term/command distinction: M ≡ c

M ::= . . . | | µα.M | | [α]M

◮ Same rules, just more expressive meta-variables:

(λx.[α]x) 1 = [α]1 because [α]x is now a term [α](µ .1) = 1 because 1 is now a command

slide-18
SLIDE 18

Nothing new, nothing gained?

◮ We haven’t added any new constructs ◮ We haven’t added any new rules ◮ As typed calculus, Λµ considered equivalent to Parigot’s λµ ◮ So they’re the same?

slide-19
SLIDE 19

Nothing new, nothing gained?

◮ We haven’t added any new constructs ◮ We haven’t added any new rules ◮ As typed calculus, Λµ considered equivalent to Parigot’s λµ ◮ So they’re the same? No!

slide-20
SLIDE 20

Delimited control

slide-21
SLIDE 21

shift and reset

◮ shift and reset are a common basis for delimited control

reset(E[shift V ]) = reset(V (λx.reset(E[x])))

◮ Continuations return, they are composable like normal functions

2 × reset(10 + (shift(λk.k (k 2)))) = 2 × reset(10 + reset(10 + reset(2))) = 2 × reset(22) = 44

slide-22
SLIDE 22

λ + shift + reset ≤ Λµ

◮ Embedding of shift and reset into Λµ

◮ Equational theory of shift and reset (Kameyama and Hasegawa, 2003)

provable in Λµ

◮ The two-pass CPS transformation for shift and reset (Danvy and

Filinski, 1990) derived from embedding

◮ So λ + shift + reset is a subset of Λµ

µα1.µα2.µα3.4 [α3][α2][α1](f 0)

◮ What covers the whole of Λµ?

slide-23
SLIDE 23

shift0 and reset0

◮ Like shift, except that shift0 removes its surrounding delimiter

reset(E[shift V ]) = reset(V (λx.reset(E[x]))) reset0(E[shift0 V ]) = V (λx.reset0(E[x]))

◮ Many shift0s can “dig” out of many reset0s

slide-24
SLIDE 24

λ + shift0 + reset0 ≡ Λµ

◮ λ with shift0 and reset0 is equivalent to Λµ

◮ Equational theories correspond ◮ CPS transforms correspond ◮ shift0 and reset0 rely on mixing terms with commands

◮ Restricting then relaxing the syntax led us from classical to

delimited control!

slide-25
SLIDE 25

Roadmap from classical to delimited control

Classical Delimited λ + callcc λ + shift0 + reset0 λµ Λµ

syntactic relaxation

slide-26
SLIDE 26

Roadmap from classical to delimited control

Classical Delimited λ + callcc λ + shift0 + reset0 λ + shift + reset λµ Λµ

syntactic relaxation

slide-27
SLIDE 27

Λµ: a framework for delimited control

◮ Encode both shift, reset and shift0, reset0 in Λµ ◮ Provable observational guarantees about the operators

◮ Example: idempotency of reset

reset(reset(M)) = reset(M)

◮ Observational guarantees still hold under composition

◮ reset is still idempotent even if we use shift0 ◮ Safely put together programs using either operators

slide-28
SLIDE 28

More in the paper

◮ Parameterize equational theory by different evaluation strategies

◮ call-by-value, call-by-name, and call-by-need

◮ Improved reasoning for control operators in λ-calculus using

continuation variables

◮ Equational correspondence with compositional transformations

◮ Compositionality and hygiene makes life easier!

slide-29
SLIDE 29

Final words

◮ Control-flow effects: have our cake and eat it too

◮ Expressive capability ◮ Preserve local, open, high-level reasoning ◮ Generic (parametric) treatment of evaluation strategies

◮ Compositionality is powerful ◮ Logic can be a wonderful guide

slide-30
SLIDE 30

References I

  • O. Danvy and A. Filinski. Abstracting control. In LISP and

Functional Programming, pages 151–160, 1990.

  • A. Filinski. Representing monads. In POPL, pages 446–457, 1994.
  • T. Griffin. A formulae-as-types notion of control. In POPL, pages

47–58, 1990.

  • Y. Kameyama and M. Hasegawa. A sound and complete

axiomatization of delimited continuations. In ICFP, pages 177–188, 2003.

  • E. Moggi. Computational λ-calculus and monads. In Logic in

Computer Science, 1989.

slide-31
SLIDE 31

References II

  • M. Parigot. Lambda-my-calculus: An algorithmic interpretation of

classical natural deduction. In LPAR, pages 190–201, 1992.

  • A. Sabry. Note on axiomatizing the semantics of control operators.

Technical Report CIS-TR-96-03, Department of Computer and Information Science, University of Oregon, 1996.

  • A. Sabry and M. Felleisen. Reasoning about programs in

continuation-passing style. Lisp and Symbolic Computation, 6(3-4): 289–360, 1993.