The Duality of Construction Paul Downen Zena M. Ariola University - - PowerPoint PPT Presentation

the duality of construction
SMART_READER_LITE
LIVE PREVIEW

The Duality of Construction Paul Downen Zena M. Ariola University - - PowerPoint PPT Presentation

The Duality of Construction Paul Downen Zena M. Ariola University of Oregon April 9, 2014 The sequent calculus Sequent calculus vs. Natural deduction Natural deduction tells us about pure functional programming Sequent calculus tells


slide-1
SLIDE 1

The Duality of Construction

Paul Downen Zena M. Ariola

University of Oregon

April 9, 2014

slide-2
SLIDE 2

The sequent calculus

slide-3
SLIDE 3

Sequent calculus vs. Natural deduction

◮ Natural deduction tells us about pure functional

programming

◮ Sequent calculus tells us about programming with

duality

◮ Flow of Information: producers are dual to consumers ◮ Evaluation: Call-by-value is dual to call-by-name ◮ Construction: data structures are dual to co-data (abstract
  • bjects with procedural interface)
slide-4
SLIDE 4

Previous Work

◮ Curien and Herbelin (2000) ◮ Wadler (2003, 2005) ◮ Zeilberger (2008, 2009) ◮ Munch-Maccagnoni (2009) and Curien (2010)

slide-5
SLIDE 5

Sequent calculus: a symmetric language

Commands c v| |e Producers v Consumers e (contexts) Function abstraction λx.v Function call (call stack) v · e Input variable x Output variable (co-variable) α Output abstraction µα.c Input abstraction (let binding) ˜ µx.c . . . . . .

slide-6
SLIDE 6

Flow of information v| |˜ µx.c c {v/x}

flow of information productive info

slide-7
SLIDE 7

Flow of information c {e/α} µα.c| |e

flow of information consumptive info

slide-8
SLIDE 8

Not so fast. . .

slide-9
SLIDE 9

Fundamental dilemma of classical computation

µα.c1| |˜ µx.c2 c1 {(˜ µx.c2)/α} c2 {(µα.c1)/x}

slide-10
SLIDE 10

Fundamental dilemma of classical computation

µ .c1| |˜ µ .c2 c1 c2

slide-11
SLIDE 11

Impact of strategy on substitution

◮ Call-by-value: Refined notion of “value”

◮ Subset of producers (terms) ◮ Variables stand in for values

◮ Call-by-name: Refined notion of “strict context”

(“co-value”)

◮ Subset of consumers (co-terms) ◮ Co-variables stand in for co-values
slide-12
SLIDE 12

Parameterizing by the strategy

◮ Single calculus uses unspecified “values” and

“co-values”

◮ Only substitute (co-)values for (co-)variables ◮ Strategy = definition of (co-)values ◮ Impact of strategy isolated to the two

parameterized rules for substitution

slide-13
SLIDE 13

Parameterizing by the strategy (µE) µα.c| |E = c {E/α} (˜ µV) V | |˜ µx.c = c {V /x} (ηµ) µα.v| |α = v (η˜

µ)

˜ µx.x| |e = e

slide-14
SLIDE 14

Some strategies (and their dual)

Call-by-value:

◮ Variables are values ◮ Every consumer is a co-value

is dual to. . . Call-by-name:

◮ Every producer is a value ◮ Co-variables are co-values

slide-15
SLIDE 15

Some strategies (and their dual)

Call-by-value: V ∈ Value ::= x E ∈ CoValue ::= e is dual to. . . Call-by-name: V ∈ Value ::= v E ∈ CoValue ::= α

slide-16
SLIDE 16

Some strategies (and their dual)

Lazy call-by-value (aka call-by-need):

◮ Values same as call-by-value ◮ Co-values may contain delayed let-bindings

is dual to. . . Lazy call-by-name:

◮ Values may contain delayed co-let-bindings (callcc) ◮ Co-values same as call-by-name

slide-17
SLIDE 17

Some strategies (and their dual)

Lazy call-by-value (aka call-by-need): V ∈ Value ::= x E ∈ CoValue ::= α | | ˜ µx.v| |˜ µy.x| |E is dual to. . . Lazy call-by-name: V ∈ Value ::= x | | µα.µβ.V | |α| |e E ∈ CoValue ::= α

slide-18
SLIDE 18

Two dual approaches to

  • rganize information
slide-19
SLIDE 19

Data types

◮ Defined by rules of creation (constructors) ◮ Producer: fixed shapes given by constructors ◮ Consumer: case analysis on constructions ◮ Like ADTs in ML and Haskell

slide-20
SLIDE 20

Declaring sums as data (G)ADT: data Either a b where Left :: a → Either a b Right :: b → Either a b Sequent: data a ⊕ b where Left : a ⊢ a ⊕ b| Right : b ⊢ a ⊕ b|

slide-21
SLIDE 21

Declaring sums as data

◮ Producer: two constructors (left or right)

Left(v1) Right(v2)

◮ Consumer: consider shape of input

◮ “If I’m given Left, do this” ◮ “If I’m given Right, do that”

˜ µ[Left(x).c1| Right(y).c2]

slide-22
SLIDE 22

Co-data types

◮ Defined by rules of observation (messages) ◮ Consumer: fixed shapes given by observations ◮ Producer: case analysis on messages ◮ Like interfaces for abstract objects

slide-23
SLIDE 23

Declaring products as co-data codata a & b where First : |a & b ⊢ a Second : |a & b ⊢ b

slide-24
SLIDE 24

Declaring products as co-data

◮ Consumer: two observations (first or second)

First[e1] Second[e2]

◮ Producer: consider shape of output

◮ “If I’m asked for first, do this” ◮ “If I’m asked for second, do that”

µ(First[α].c1| Second[β].c2)

slide-25
SLIDE 25

Declaring functions as co-data codata a → b where Call : a|a → b ⊢ b

slide-26
SLIDE 26

Declaring functions as co-data

◮ Consumer: one observation (function call)

◮ Argument ◮ What to do with result

Call[v, e]

◮ Producer: consider shape of output

◮ Function pops argument off call-stack

µ(Call[x, α].c) = λx.µα.c

slide-27
SLIDE 27

Evaluating data and co-data

◮ Two fundamental principles of data and co-data:

◮ β: Case analysis breaks apart structure ◮ η: Forwarding is unobservable

◮ Does not perform substitution

◮ And therefore does not reference strategy ◮ Hold in the presence of effects (control, non-termination)
slide-28
SLIDE 28

Evaluating functions as co-data (β) λx.v ′| |v · e = v| |˜ µx.v ′| |e (η) λx.µα.z| |x · α = z (β) µ(Call[x, α].c)| |Call[v, e] = v| |˜ µx.µα.c| |e (η) µ(Call[x, α].z| |Call[x, α]) = z

slide-29
SLIDE 29

Evaluating sums as data

(β)

  • Left(v)
  • ˜

µ[ Left(x). c1 | Right(y). c2]

  • = v|

|˜ µx.c1 (β)

  • Right(v)
  • ˜

µ[ Left(x). c1 | Right(y). c2]

  • = v|

|˜ µy.c2 (η) ˜ µ[ Left(x). Left(x)| |γ | Right(y). Right(y)| |γ] = γ

slide-30
SLIDE 30

Evaluating products as co-data

(β)

  • µ( First[α].

c1 | Second[β]. c2)

  • First[e]
  • = µα.c1|

|e (β)

  • µ( First[α].

c1 | Second[β]. c2)

  • Second[e]
  • = µβ.c2|

|e (η) µ( First[α]. z| |First[α] | Second[β]. z| |Second[β]) = z

slide-31
SLIDE 31

General characterization of data and co-data

◮ Constructors dual to messages, case abstractions dual to

abstract objects

◮ All basic connectives of linear/polarized logic fit into same

general pattern

◮ The ordinary: →, ⊗, ⊕, &, . . . ◮ The exotic: `, ¬, . . .

◮ All other behavior derived from β, η, and substitution:

◮ Usual call-by-name and call-by-value λ-calculus β and η rules ◮ Wadler’s (2003) ς rules for lifting components out of structures
slide-32
SLIDE 32

Summary

◮ Single theory of the sequent calculus

parameterized by various strategies

◮ User-defined data and co-data defined by β and η

independent of strategy

◮ Illustrate call-by-name, call-by-value, and lazy

versions of both

slide-33
SLIDE 33

Summary

◮ Generalize known dualities of computation

◮ General duality between various strategies ◮ General duality between data and co-data types

◮ Two or more strategies in the same program

◮ Use kinds to denote strategies ◮ Well-kindedness preserves consistency ◮ Extends the polarized view of evaluation strategy
slide-34
SLIDE 34

Questions?

  • Answers!
slide-35
SLIDE 35

Interleaving multiple strategies

slide-36
SLIDE 36

Conflicts between strategies µα.c1| |˜ µx.c2 µα.c1 ˜ µx.c2 CBV non-value co-value CBN value non-co-value

slide-37
SLIDE 37

Conflicts between strategies µα.c1| |˜ µx.c2 µα.c1 ˜ µx.c2 CBV non-value co-value CBN value non-co-value OK

slide-38
SLIDE 38

Conflicts between strategies µα.c1| |˜ µx.c2 µα.c1 ˜ µx.c2 CBV non-value co-value CBN value non-co-value OK

slide-39
SLIDE 39

Conflicts between strategies µα.c1| |˜ µx.c2 µα.c1 ˜ µx.c2 CBV non-value co-value CBN value non-co-value non-deterministic

slide-40
SLIDE 40

Conflicts between strategies µα.c1| |˜ µx.c2 µα.c1 ˜ µx.c2 CBV non-value co-value CBN value non-co-value stuck

slide-41
SLIDE 41

Well-kindedness preserves consistency

Γ ⊢ v :: S|∆ Γ|e :: S ⊢ ∆ v| |e : Γ ⊢ ∆ Cut

◮ CBV |

|CBV : well-kinded, call-by-value command

◮ CBN|

|CBN: well-kinded, call-by-name command

◮ CBV |

|CBN: ill-kinded, non-deterministic command

◮ CBN|

|CBV : ill-kinded, stuck command

slide-42
SLIDE 42

The polarized regime . . . as an instance of the general theory:

◮ Only two kinds (therefore only two strategies)

◮ Positive: call-by-value ◮ Negative: call-by-name

◮ Pick strategy of (co-)data types to maximize η

◮ Positive: data ◮ Negative: co-data
slide-43
SLIDE 43

Annotating variables

Γ, x :: S ⊢ xS :: S|∆ Var Γ|αS :: S ⊢ α :: S, ∆ CoVar c : (Γ ⊢ α :: S, ∆) Γ ⊢ µαS.c :: S|∆ Act c : (Γ, x :: S ⊢ ∆) Γ|˜ µxS.c :: S ⊢ ∆ CoAct

slide-44
SLIDE 44

The problem with annotating commands

◮ Annotating commands (cuts) with a strategy:

◮ v|

|eV: call-by-value

◮ v|

|eN : call-by-name

◮ Loss of determinism

µ .c1| |˜ µx.x| |˜ µ .c2VN µ .c1| |˜ µ .c2N c2 ˜ µ µ .c1| |˜ µ .c2V c1 µ ˜ µ ˜ µ or η˜

µ