the duality of construction
play

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


  1. The Duality of Construction Paul Downen Zena M. Ariola University of Oregon April 9, 2014

  2. The sequent calculus

  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 objects with procedural interface)

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

  5. Sequent calculus: a symmetric language Commands c � v | | e � Producers v Consumers e (contexts) Function abstraction Function call (call stack) λ x . v v · e Input variable Output variable (co-variable) x α Output abstraction Input abstraction (let binding) µα. c µ x . c ˜ . . . . . .

  6. Flow of information flow of information � v | | ˜ µ x . c � c { v / x } productive info

  7. Flow of information flow of information c { e /α } � µα. c | | e � consumptive info

  8. Not so fast. . .

  9. Fundamental dilemma of classical computation � µα. c 1 | | ˜ µ x . c 2 � c 1 { (˜ µ x . c 2 ) /α } c 2 { ( µα. c 1 ) / x }

  10. Fundamental dilemma of classical computation � µ . c 1 | | ˜ µ . c 2 � c 1 c 2

  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

  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

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

  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

  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 ::= α

  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

  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 ::= α

  18. Two dual approaches to organize information

  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

  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 |

  21. Declaring sums as data ◮ Producer: two constructors (left or right) Left ( v 1 ) Right ( v 2 ) ◮ Consumer: consider shape of input ◮ “If I’m given Left, do this” ◮ “If I’m given Right, do that” µ [ Left ( x ) . c 1 | Right ( y ) . c 2 ] ˜

  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

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

  24. Declaring products as co-data ◮ Consumer: two observations (first or second) First [ e 1 ] Second [ e 2 ] ◮ Producer: consider shape of output ◮ “If I’m asked for first, do this” ◮ “If I’m asked for second, do that” µ ( First [ α ] . c 1 | Second [ β ] . c 2 )

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

  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

  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)

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

  29. Evaluating sums as data � � � � µ [ Left ( x ) . ˜ c 1 � � ( β ) Left ( v ) = � v | | ˜ µ x . c 1 � � � | Right ( y ) . c 2 ] � � � � � � � � µ [ Left ( x ) . ˜ c 1 � � ( β ) Right ( v ) = � v | | ˜ µ y . c 2 � � � | Right ( y ) . c 2 ] � � � � µ [ Left ( x ) . ˜ � Left ( x ) | | γ � ( η ) | γ � ] = γ | Right ( y ) . � Right ( y ) |

  30. Evaluating products as co-data � � � � µ ( First [ α ] . c 1 � � ( β ) � First [ e ] = � µα. c 1 | | e � � � | Second [ β ] . c 2 ) � � � � � � � µ ( First [ α ] . c 1 � � ( β ) � Second [ e ] = � µβ. c 2 | | e � � � | Second [ β ] . c 2 ) � � � µ ( First [ α ] . � z | | First [ α ] � ( η ) | Second [ β ] � ) = z | Second [ β ] . � z |

  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

  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

  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

  34. Questions? � Answers!

  35. Interleaving multiple strategies

  36. Conflicts between strategies � µα. c 1 | | ˜ µ x . c 2 � µα. c 1 µ x . c 2 ˜ CBV non-value co-value CBN value non-co-value

  37. Conflicts between strategies � µα. c 1 | | ˜ µ x . c 2 � µα. c 1 µ x . c 2 ˜ CBV non-value co-value CBN value non-co-value OK

  38. Conflicts between strategies � µα. c 1 | | ˜ µ x . c 2 � µα. c 1 µ x . c 2 ˜ CBV non-value co-value CBN value non-co-value OK

  39. Conflicts between strategies � µα. c 1 | | ˜ µ x . c 2 � µα. c 1 µ x . c 2 ˜ CBV non-value co-value CBN value non-co-value non-deterministic

  40. Conflicts between strategies � µα. c 1 | | ˜ µ x . c 2 � µα. c 1 µ x . c 2 ˜ CBV non-value co-value CBN value non-co-value stuck

  41. Well-kindedness preserves consistency Γ ⊢ v :: S| ∆ Γ | e :: S ⊢ ∆ Cut � v | | e � : Γ ⊢ ∆ ◮ � 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

  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

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

  44. The problem with annotating commands ◮ Annotating commands (cuts) with a strategy: ◮ � v | | e � V : call-by-value | e � N : call-by-name ◮ � v | ◮ Loss of determinism µ . c 2 � V � N � µ . c 1 | | ˜ µ x . � x | | ˜ µ ˜ µ or η ˜ ˜ µ µ . c 2 � V µ . c 2 � N � µ . c 1 | | ˜ � µ . c 1 | | ˜ µ µ ˜ c 1 c 2

Recommend


More recommend