Polarity and the Logic of Delimited Continuations

Noam Zeilberger Université Paris VII [email protected]

Abstract—Polarized logic is the logic of values and continu- Focusing has been applied successfully to guide proof search ations, and their interaction through continuation-passing style. in intuitionistic logic [23], [6], [33], while distinctions seem- The main limitations of this logic are the limitations of CPS: ingly related to classical polarity have arisen in intuitionistic that continuations cannot be composed, and that programs are fully sequentialized. Delimited control operators were invented type-theoretic settings. Notably, Filinski [15] and Levy [30] in response to the limitations of classical continuation-passing. distinguish value types and computation types to decompose That suggests the question: what is the logic of delimited the monadic view of effects in , while continuations? Watkins et al. [45] distinguish synchronous and asynchronous We offer a simple account of delimited control, through a types (adopting Andreoli’s terminology for positive and neg- natural generalization of the classical notion of polarity. This amounts to breaking the perfect symmetry between positive and ative polarity in classical linear logic) to tame equality in a negative polarity in the following way: answer types are positive. dependent logical framework. Yet, intuitionistic logic seems to Despite this asymmetry, we retain all of the classical polarized blur the precise duality between positive and negative polarity, connectives, and can explain “intuitionistic polarity” (e.g., in and to preclude certain polarizations of the connectives—for systems like CBPV) as a restriction on the use of connectives, i.e., example, none of these systems includes a negative polarity as a logical fragment. Our analysis complements and generalizes existing accounts of delimited control operators, while giving us a disjunction. Moreover, there doesn’t appear to be the same rich logical language through which to understand the interaction connection with double- translation or CPS, although of control with monadic effects. there is some connection to sequentiality (cf. [45], [12], [46]). In short, it is unclear exactly how the phenomenon of polarity I. I in intuitionistic logic is related to the classical case. The original motivation for studying polarity as a property Second, from a practical and theoretical perspective, clas- of logical connectives was to decompose the classical double- sical polarity may not give a sufficiently refined notion of negation translations, and so to better understand the different evaluation order, precisely because of this correspondence ways in which classical logic could be endowed with construc- with classical continuation-passing. The CPS discipline is a tive content [18], [19]. Polarity was a natural distinction in the kind of paranoia, where a term needs to have its entire life of linear logic (positive ⊗, ⊕, ∃ vs. negative , , ∀), planned before it takes a step. This means that a program but once this classification was discovered, it becameO N clear ends up completely sequentialized even in cases where order that polarity in linear logic was an elegant demonstration of of evaluation is irrelevant. Historically, this was the one of a much more general phenomenon. Classical logic could be the reasons for the invention of “composable” or “delimited” understood constructively by way of embedding into linear control operators [13], [10], [11], which allow a program to logic [8], [29], but it could also be understood on its own mix continuation-passing and direct style. Besides numerous just by polarizing the connectives, i.e., by forcing them to practical applications (see the long bibliography of Kiselyov have positive or negative polarity. This was to some extent and Shan [28]), one hint of the foundational relevance of already explicit in Girard’s original papers, and is implicit in delimited control is a tantalizing representation theorem due other constructive interpretations of classical logic, such as to Filinski [14], which states that the operators shift and reset Selinger’s (co)control categories [41]. can be used to perform monadic reflection, i.e., to mediate Formally, applying Andreoli’s notion of focusing [3] to between internal and external views of computational effects. classical logic gives a canonical representation of proofs which Can classical and intuitionistic flavors of polarity be rec- are β-normal, η-long, and in continuation-passing style. The onciled, and can the relationship between polarized logic and particular fragment of CPS is determined by how one polarizes CPS be extended to delimited continuations? the classical connectives. Polarized logic—where the polarity In this paper, we describe a natural generalization of clas- of connectives is made explicit, and connectives come in both sical polarized logic, and explain how it accounts both for positive and negative varieties—can in this sense be seen as intuitionistic polarity and for delimited control. Our basic a logic for expressing evaluation order at the local level of premise is that the perfect symmetry between positive and proofs and types, rather than as a global property of a language negative polarity should be broken: answer types (i.e., con- (cf. [47]). tinuations’ result types) are positive. Starting from this thesis, Two questions arise naturally. we generalize the proof theory of polarity in a more or less First, one does not need to start with classical (linear directed way, with the important difference that the notion or non-linear) logic to study polarization and focalization. of equality on proofs may now incorporate the equations of · | | | • any given monad, relaxing the strict sequentiality of CPS. Contexts ∆, Γ ::= ∆1, ∆2 N P ...... Perhaps surprisingly, we can retain (indeed generalize) all of ∆ [P] Γ ` ∆ ∆ [P] −→ Γ, ∆ ` # the usual polarized connectives (e.g., ), viewing traditional Γ ` [P] Γ ` •P intuitionistic systems as logical fragmentsO —in effect giving a • −→ ` • ` double-negation interpretation of intuitionistic logic. We apply ∆ [ N] Γ, ∆ # ∆ [ N] Γ ∆ ` N ` •N our logical analysis of delimited control operators first to Γ Γ [ ] explain Danvy and Filinski’s original type system, then to re- N ∈ ΓΓ ` [•N] •P ∈ ΓΓ ` [P] visit Filinski’s representation theorem, showing its elementary Γ ` # Γ ` # character and close link to the Yoneda lemma. Γ ` ∆1 Γ ` ∆2 Γ ` · Γ ` ∆1, ∆2 II.     Before we go on to describe this generalization of classical Fig. 1. The classical rules of focusing polarized logic and its relationship to intuitionistic polarity and delimited control, we review the correspondence between use the letters P and N to range over positive and negative classical polarity and undelimited control. Although the con- , respectively. There are two basic stances we nection between polarities and CPS is by now well-established, can take about a : assertion or denial. For positive it is not always well understood, sometimes thought to be an propositions, we write these respective judgments as [P] and exotic property of linear logic. The connection is mundane, •P, while for negative propositions we write N and [•N]. As and can be understood with the intuitions of ordinary func- we will explain shortly, assertion and denial are merely another tional programming. way of reading positionality in “multiple conclusions” sequent Positive and negative polarity are simply two different ways calculus (cf. [38]), while the brackets mark in the sense of defining types. A positive type is defined by describing of Andreoli [3]. the shape of its values—in other words, like any datatype. Simple contexts ∆, Γ are formed by combining (using the If this is taken as a complete definition, i.e., if these are comma) assertions N of negative propositions and denials •P all the possible shapes of a value of positive type, then we of positive propositions, treated up to associativity and unit are justified in using ordinary pattern-matching notation to laws for the empty context (·). We omit hypotheses of the define the continuations for that type. Dually, a negative type is form [P] and [•N] because such “complex” hypotheses are defined by describing the shape of its continuations—similar to invertible, i.e., they can always be replaced by an equivalent how one defines a record or class, by listing its fields/methods. set (read disjunctively) of simple contexts. Again, if this is taken as a complete definition, then a value A hypothetical judgment Γ ` J asserts some judgment of negative type can be constructed by pattern-matching on J relative to the simple context Γ. In addition to assertion continuations. Values of negative type are therefore understood and denial of propositions, we can assert an entire context as control operators, and naturally lazy—they can be computed ∆ (read conjunctively), or a contradiction #. The connection on demand in response to their continuation. Indeed, the between judgments Γ ` J and the typical notation for two- ubiquitous lambda of ordinary functional programming can sided sequents “with punctuation” is a shallow syntactic trans- be understood in this way, as a degenerate kind of control formation. Asserted propositions in Γ can be viewed as a “left operator: defined by matching on a continuation consisting of context” Γ = {N | N ∈ Γ}, and refuted propositions as a “right a value for the argument, and a continuation for the result. L context” Γ = {P | •P ∈ Γ}. Contradiction Γ ` # corresponds The subtlety in this description arises at the interaction of R to a sequent with no distinguished formula (Γ ⇒ Γ ), while positive and negative polarity. A positive value may contain L R Γ ` [P] corresponds to a right-focused sequent (Γ ⇒ Γ ;[P]), negative subcomponents. For a continuation accepting positive L R Γ ` N to a right-inverting sequent (Γ ⇒ Γ ; N), Γ ` •P to type, this is the point where pattern-matching must end by L R a left-inverting sequent (P; Γ ⇒ Γ ), and Γ ` [•N] a left- a value variable, because nothing can be assumed L R focused sequent ([N]; Γ ⇒ Γ ). We can interpret Γ ` ∆ by about the shape of negative values. Again, this is an everyday L R expansion, into a set of sequents. fact of life in ML or Haskell, a reflection of the impossibility of The eight rules in Figure 1 describe the canonical ways pattern-matching against functional values except by binding of deriving Γ ` J, in the sense that any additional forms of a variable. Likewise, a negative continuation can contain proof can always be reduced to these rules. This is the con- positive subcomponents. For a negative value, this is the point tent of Andreoli’s focusing completeness theorem. Something where pattern-matching on continuations must stop, binding that is different from Andreoli’s original presentation (though the positive continuation to a continuation variable. similar to [2]) is that none of the rules mention any logical A. Proofs connectives, but only “structural connectives”. The point is that the canonical rules can be stated generically with respect We use logic to make these intuitions precise. Figure 1 to a “dictionary” for the logical connectives. presents the rules for building focusing proofs in polarized classical logic. The presentation is a bit unconventional, so Definition II.1. A dictionary is a pair of inductively defined we’ll explain it gradually (for more background see [47]). We relations ∆ [P] and ∆ [•N]. It induces a definition ordering (an abstract notion of “subformula”), taking all to the left of an arrow and not in the rule’s conclusion is the propositions in ∆ to be below P (resp. N) if ∆ [P] implicitly universally quantified (with tight ). So in prose (resp. ∆ [•N]). the rule is read, “[For any Γ and P,] If for all ∆ such that ∆ [P], we can derive Γ, ∆ ` #, then Γ ` •P”. Definition II.2. In general we write D :: J to indicate that D Finally, let’s consider the last rule dealing explicitly with is a derivation of the judgment J. A derivation p :: ∆ [P] positive polarity, one of the rules of contradiction: is called a proof pattern, while a derivation d :: ∆ [•N] is called a refutation pattern. •P ∈ ΓΓ ` [P] Γ ` # The idea is that p describes the shape of a proof of P with some “holes” ∆, and likewise d describes the shape of a This says simply that one way of establishing contradiction is refutation of N with holes ∆. to prove a (positive) proposition assumed to be false. These three rules of proof, refutation, and contradiction Example II.3. The following inductive clauses define some are of course entirely sound intuitionistically. They give a of the standard polarized connectives: constructive interpretation of classical logic in the sense that ∆1 [P1] ∆2 [P2] we can consider hypothetical judgments Γ ` # as sequents · [1] ∆1, ∆2 [P1 ⊗ P2] ΓL ⇒ ΓR, and in particular as · ⇒ ΓR when Γ only contains ∆ [P1] ∆ [P2] denials of positive propositions.1 (no proof of 0) ∆ [P1 ⊕ P2] ∆ [P1 ⊕ P2] On the other hand, the rules of proof and refutation for ∆ [•N1] ∆ [•N2] negative propositions do not seem so intuitionistic: (no refutation of >) ∆ [•N1 N2] ∆ [•N1 N2] N N ∆ [•N] −→ Γ, ∆ ` # ∆ [•N] Γ ` ∆ ∆1 [•N1] ∆2 [•N2] Γ ` N Γ ` [•N] · [•⊥] ∆1, ∆2 [•N1 N2] O In particular, for negative propositions, proof means proof-by- ∆1 [P] ∆2 [•N] ∆ [•N] ⊥ contradiction. Negative refutation is intuitionistically sound (to ∆1, ∆2 [•P → N] ∆ [N ] N [↓N] •P [•↑P] refute N, pick a refutation pattern and fill in its holes), but in Most of these rules should be familiar from linear sequent fact it is more restrictive than ordinary intuitionistic refuta- calculus (following the above guide for translating asser- tion. These are really “co-intuitionistic” reasoning principles. tion/denial into left/right positionality), but note that here we Negative polarization thus yields a more direct constructive are only building patterns, and there is no linearity restriction interpretation of classical truth (cf. [43]), although it yields a on the use of hypotheses when building actual proofs and less direct interpretation of classical falsehood. refutations. The connectives ↓ and ↑ play the important role One final, important point about the logical standing of the of coercions between the two polarities, and mark leaves of rules in Figure 1 is that the meaning of contradiction is also patterns.  not complete for the intuitionistic (or co-intuitionistic) reading. The reader can keep this dictionary in mind to get a handle on In particular, we do not have ex falso quodlibet: Γ ` # does our presentation of focusing and its relation to more standard not let us deduce Γ ` [P] (or Γ ` [•N]). Instead, # is the sequent calculi—but must keep in mind that it is only an contradiction of minimal logic [25]. example, an incomplete collection of useful connectives. In Since the rules of focusing define a cut-free sequent calcu- general we need not make any assumptions about the dictio- lus, we should expect some sort of cut-admissibility theorem. nary, even allowing non-well-founded definition orderings (to We state this as a composition principle, together with an encode recursive types). identity principle (corresponding to admissibility of initial Now, consider the canonical rules for proving or refuting a axioms A ⇒ A): positive proposition: Principle (Composition). ∆ [P] Γ ` ∆ ∆ [P] −→ Γ, ∆ ` # 1) If Γ ` [P] and Γ ` •P then Γ ` #. Γ ` [P] Γ ` •P 2) If Γ ` N and Γ ` [•N] then Γ ` #. The rule of proof follows the intuition we described for the 3) If Γ ` ∆ and Γ, ∆ ` # then Γ ` #. meaning of patterns: to prove P, we must pick one of the Principle (Identity). If ∆ ⊆ Γ then Γ ` ∆. possible patterns of proof, and fill in all of its holes. Note that the premise Γ ` ∆ can be expanded, using the canonical rules, In fact, the demonstration of both these principles is almost into a list of premises Γ ` Ni and Γ ` •P j. trivial—the procedures for witnessing them can be given in The rule of refutation follows the intuition we described at an entirely generic way, for an arbitrary dictionary. Whether the beginning of this section, that continuations for positive or not these procedures terminate depends on whether the types may be defined by pattern-matching: to refute P, it definition ordering is well-founded. It is for the dictionary in suffices to consider the shape of any possible proof of P, and Example II.3, but in general won’t be, and we can accept thence derive a contradiction. This is a higher-order rule, and we follow the convention that any meta-variable appearing 1This interpretation of classical logic is essentially Glivenko’s [21]. p σ p Ep d Ed d σ p[σ] p E d E d[σ]  p − d − The rules may look isomorphic, but they are justified in V + K+  V  K very different ways. To justify the ⊗ rule, we assume that − + v K − k V + σ1 σ2 K $v k$V · (σ1,σ2) both premises have canonical introductions, which means E E σ σ + + that we have values V1 = p1[σ1] and V2 = p2[σ2], where p :: ∆ [P ] and σ :: Γ ` ∆ . From this we can construct Fig. 2. Classical canonical forms i i i i i (p1, p2) :: ∆1, ∆2 [P1 ⊗ P2] (using the dictionary) and + + (σ1, σ2) :: Γ ` ∆1, ∆2, then (V1 , V2 ) :: Γ ` [P1 ⊗ P2] by (V +, V +) = (p , p )[σ , σ ]. Without the type annotations, we partiality in the definition of composition, and non-well- 1 2 1 2 1 2 might display this local “justification” like so: foundedness in the derivation of identity. + + (V1 , V2 ) = (p1, p2)[σ1, σ2] + + B. Programs where V1 = p1[σ1], V2 = p2[σ2] The preceding section can already be read directly as an To justify the rule, we consider the conclusion’s canonical N intrinsic definition [39] of a . The uses, i.e., refutation patterns for N1 N2 with holes ∆, and following table describes the correspondence between logical show how to derive Γ, ∆ ` #. AnyN such refutation pattern derivations and well-typed terms, adopting standard terminol- comes from one for either N1 or N2, with the same ∆, and ogy in a faithful way: in either case we can apply one of the premises to obtain Γ, ∆ ` #. Inventing some reasonable notation for patterns, this deriving symbol description reasoning could be summarized as: ∆ [P] p value pattern − − − ∆ [•N] d continuation pattern (V1 , V2 )(π1; d1) = V1 (d1) − − − •P ∈ Γ k continuation variable (V1 , V2 )(π2; d2) = V2 (d2) N ∈ Γ v value variable Operationally, the different readings of these rules correspond Γ ` [P] V + value of type P to the fact that one builds strict pairs, the other lazy. Γ ` N V − value of type N When writing programs, we of course also want to consider Γ ` •P K+ continuation accepting P terms which are not already β-normal and η-long. Thus we Γ ` [•N] K− continuation accepting N can internalize the composition and identity principles and Γ ` ∆ σ substitution for ∆ interpret them dynamically—in §IV-B we will examine this Γ ` # E well-typed expression in a more general setting. In Figure 2 we give a conceptual grammar of the language C. Two ways of saying the same thing induced by focusing proofs, where all we have done is remove the precise type information from Figure 1, leaving only the The elegance of classical polarity is that positive and symbol of the corresponding syntactic class—some concrete negative views are perfectly symmetric. The structure of a syntax for the conclusion is also shown to the right of each negative value is isomorphic to the structure of a positive rule. For example, a rough way of restating the rules of proof continuation, just as the structure of a negative continuation is and refutation is that a positive value (negative continuation) isomorphic to the structure of a positive value. can always be decomposed as a value (cont.) pattern together Of course, this elegant symmetry can also be seen as with a substitution, while a positive continuation (neg. value) needless redundancy, and it is tempting to define a more is determined by a map from value (cont.) patterns to expres- minimal system by cross-section. Indeed, the positive fragment sions. of Figure 1 appears again and again (in different guises) in studies of continuation-passing, including Thielecke’s CPS These rules only allow the construction of terms which calculus [44], response categories [41], Jump-With-Argument are β-normal, η-long, and in CPS. Which fragment of CPS [30], and tensorial logic [34]. While polarized types provide a depends on the types one is considering, particularly their rich language for expressing intuitions about control directly, polarities. We cannot include a detailed review here, but in the classical setting there is fundamentally no loss of the intuition is simple based on the two rules for forming expressivity in limiting oneself to, say, positive types (e.g., expressions: the rule for positive polarity passes a value to instead of building a lazy pair, build a continuation for a strict a continuation variable, while the rule for negative polarity sum, etc.). passes a continuation to a value variable (cf. [47, Ch. 4]). On the other hand, as we discussed, a similar notion of There is no problem with considering additional rules for polarity seems to extend to intuitionistic systems in which constructing terms, so long as we read them as notation for positive and negative fragments are not simply mirror images. building canonical terms (either statically or dynamically). For This suggests that we try to keep our basic intuitions about example, given the dictionary of Example II.3, we can add polarity, but break the perfect symmetry of the classical notion. these rules for building pairs: Of course, we will not do this in an arbitrary way, but one Γ ` [P1] Γ ` [P2] Γ ` N1 Γ ` N2 motivated by our desire to understand the laws of delimited Γ ` [P1 ⊗ P2] Γ ` N1 N2 continuations. N III. G  in a hypothesis P . α ∈ ∆, and can be substituted by a 0 0 It is natural to approach delimited control by first general- positive proposition (P . α)[P /α] = P . P .A parametric izing the answer type of continuations. In classical polarized dictionary is a pair of inductively defined relations ∆ [P] − logic, every expression (i.e., the computation that results and α.∆ [N . ]. from combining a value and a continuation) has type #, or Example III.2. The definitions of the standard negative con- “contradiction”. As we discussed, # is not contradiction in the nectives (see Example II.3) can be generalized like so: usual mathematical sense of the world exploded, but rather in α.∆ [N . −] α.∆ [N . −] the sense of something peculiar happened. Formally, # can be 1 2 (no rule for >) α.∆ [N1 N2 . −] α.∆ [N1 N2 . −] interpreted as a distinguished logical atom. N N So should we simply generalize # to an arbitrary proposi- α.∆1 [N1 . −] α.∆2 [N2 . −] tion? Operational intuitions instead motivate that we restrict α.· [⊥ . −] α.∆1, ∆2 [N1 N2 . −] O the type of answers to be positive: think that the purpose of ∆1 [P] α.∆2 [N . −] a computation is to produce some piece of observable data. α.∆1, ∆2 [P → N . −] α.P . α [↑P . −] In any case, starting from the formal hypothesis of positive answer types, we can proceed to generalize our analysis of The definitions of the standard positive connectives remain ⊥ classical polarity in an almost mechanical way. unchanged except for N , which is redefined as a more general positive connective N  P: A. Positive answers α.∆ [N . −] Generalizing contradiction, for any positive proposition P ∆[P/α] [N  P]  we write simply P to assert it as an ultimate consequence. Generalizing denial, we write (−) . P for an argument towards Now the way we interpret the negative rules is as follows: P (using the funny notation to emphasize that (−) . P, like α.∆ [N . −] −→ Γ, α.∆ ` α α.∆ [N . −] Γ ` ∆[P/α] •(−), is a structural rather than a ). Imagine Γ ` N Γ ` [N . P] that we now go through Figure 1 and uniformly replace # by In particular, we make explicit in the rule of negative proof P, and •(−) by (−) . P. What is the meaning of this formal that the reasoning is parametric in the type of consequence, move? and in the rule of negative refutation that the chosen pattern The rule of positive proof remains unchanged, while the is not limited to a particular consequence. rule of positive refutation is simply parameterized by a con- sequence. The twist comes on the negative side: Finally, we consider the rule for using positive denials. Under the uniform translation it becomes: ∆ [N . P] −→ Γ, ∆ ` P ∆ [N . P] Γ ` ∆ P . P0 ∈ ΓΓ ` [P] Γ ` N Γ ` [N . P] Γ ` P0 We have implicitly updated the dictionary here with a gener- Now, if this were the only rule for deriving ultimate con- alized notion of refutation pattern—without being too precise 0 yet about what that means, let us try to understand the two sequences from a hypothesis P . P , it would be seemingly rules. As in the classical case, the rule of negative refutation incomplete—why shouldn’t we be allowed to further analyze P0 to derive something else? Instead, we will say that P0 is an picks a refutation pattern (now in a generalized sense), and 0 fills in its holes. And the rule of negative proof? Following intermediate consequence (notated .P ), the convention we already established, since P appears to the P . P0 ∈ ΓΓ ` [P] left of the arrow and not in the rule’s conclusion, it is implicitly Γ ` .P0 universally quantified in the premise, i.e., “If for all P and ∆ such that ∆ [N . P], we can derive Γ, ∆ ` P, then Γ ` N”. and tie the knot by adding the following pair of rules: We see that negative proof is still a sort of proof-by- Γ ` [P] Γ ` .P Γ ` P . P0 contradiction, but now polymorphic over consequences. In Γ ` P Γ ` P0 particular, we could almost imagine justifying it intuitionis- tically (bringing to mind Friedman [17]) by instantiating P In other words, ultimate consequences can be shown directly, with N. . . except that this violates polarity. In §III-B, we will or derived by way of intermediate consequences. Collecting explain how for a large class of negative types, a proof-by- all of these remarks, we arrive at Figure 3. contradiction of N can indeed be converted into an intuition- Principle (Composition). istic proof of N. 1) If Γ ` P and Γ ` P . P0 then Γ ` P0. Let us clarify the new structure of the dictionary. We do 2) If ` N and ` N . P0 then ` P0. not want to consider ∆ [N . P] as an arbitrary relation, but Γ Γ [ ] Γ rather restrict to consequence parametric definitions. 3) If Γ ` ∆ and Γ, ∆ ` P then Γ ` P. 4) If Γ ` P1 . P2 and Γ ` P2 . P3 then Γ ` P1 . P3. Definition III.1. We write α.∆ to indicate that the conse- 5) If Γ ` [N1 . P2] and Γ ` P2 . P3 then Γ ` [N1 . P3]. quence variable α is bound in ∆, meaning that it can appear · | | | 0 Contexts ∆, Γ ::= ∆1, ∆2 N P . P Proof: Very similar to the proofs in the classical setting ...... (see Theorems 3.5.1 and 3.5.2 of [47]), with the main differ- ∆ [P] Γ ` ∆ ∆ [P] −→ Γ, ∆ ` P0 ence that the proof of Theorem III.3 applies the assumptions Γ ` [P] Γ ` P . P0 Unit and Ext to deal with the two new rules. − −→ ` − ` T T α.∆ [N . ] Γ, α.∆ α α.∆ [N . ] Γ ∆[P/α] The translation uses only a small fragment of second-order ` ` Γ N Γ [N . P] logic, and sometimes this means we can derive a direct 0 N ∈ ΓΓ ` [N . P] P . P ∈ ΓΓ ` [P] correspondence with (intuitionistic) propositional logic. Let 0 Γ ` P Γ ` .P |A| be the translation that collapses polarized connectives and 0 Γ ` ∆1 Γ ` ∆2 Γ ` [P] Γ ` .P Γ ` P . P erases coercions, i.e., with some abuse of notation: 0 Γ ` · Γ ` ∆1, ∆2 Γ ` P Γ ` P |1| = |>| = T |0| = |⊥| = F | ↓| = | ↑| = · Fig. 3. Generalized rules of focusing |⊗| = | | = ∧ |⊕| = | | = ∨ |→| = || = ⊃ N O Definition III.5. A standard polarized proposition is orderly if it does not contain , and pure if it does not contain . Principle (Identity).  A proposition is immaculateO if it is both orderly and pure. 1) If ∆ ⊆ Γ then Γ ` ∆. ip 2) Γ ` P . P for all P. Theorem III.6. For A immaculate, ` A iff ` |A| As in §II-A, the definition of the procedures for composition Proof: Corollary of Theorems III.3 and III.4, because we ∗ and identity are completely generic in the dictionary. Although can prove in ip2 (which is conservative over ip) that A [I/T] ≡ the long list of composition principles may seem daunting, |A| for immaculate A. These equivalences are immediate in they are just the mutually-recursive of this pro- the case of pure positive connectives and the ↓, rely cedure (which we will describe in more concrete operational on quantifier commutations in the case of orderly negative terms in §IV-B). connectives, and apply a substitution of |P| for α in the case (↑P)∗[I/T] = ∀α.(P∗ ⊃ α) ⊃ α. B. Generalized ¬¬ interpretations Although there exist non-immaculate polarizations of intu- itionistic logic, in general, is unsound with respect to Here we describe how the generalized notion of polarity O corresponds to a generalized class of double-negation in- intuitionistic disjunction, while  is incomplete for impli- terpretations into second-order intuitionistic logic, including cation. For example, it is easy to construct a closed proof P → ⊥ ↑P P → ↑P different interpretations of intuitionistic logic itself. For con- of ( ) (which is isomorphic to ), while in general itO is impossible to prove N M (↓N ⊗ ↓M). We creteness, we limit our attention to the connectives (re)defined N in Examples II.3 and III.2. We begin by defining a positive note that the immaculate fragment coincides precisely with the standard definition of polarized intuitionistic logic (cf. [31], translation P+ together with a negative (dualizing) translation N−α, from polarized formulas to second-order intuitionistic [33]), as well as with the type structure of Call-By-Push-Value formulas free in a monadic T: [30]. But we must also make clear that although and  may 1+ = T 0+ = F >−α = F ⊥−α = T not be immaculate, they are not immoral: theyO are perfectly + + + −α −α −α constructive. As the translation into second-order logic sug- (P1 ⊗ P2) = P1 ∧ P2 (N1 N2) = N1 ∧ N2 O gests, is essentially the Church encoding of disjunction, + + + −α −α −α (P1 ⊕ P2) = P1 ∨ P2 (N1 N2) = N1 ∨ N2 O N while (as we will explain in §IV-C)  internalizes continua- (N  P)+ = N−α[P+/α](P → N)−α = P+ ∧ N−α tions as values. Both connectives are useful computationally, and because they have first-class status as logical connectives, (↓N)+ = ∀α.N−α ⊃ Tα (↑P)−α = P+ ⊃ Tα do not interfere with the pure and orderly lives of their Next we extend the translation to judgments: neighbors. It is also worth considering the logical content of the [P]∗ = P+ P∗ = .P∗ = TPN∗ = ∀α.N−α ⊃ Tα interpretation instantiated with predicates T other than I. For ∗ + + ∗ −α + (P1 . P2) = P1 ⊃ TP2 ([N] . P) = N [P /α] example, taking Tα = # (the fixed atom # representing ∗ ∗ ∗ ∗ absurdity) yields classical logic as in the classical double- (·) = T (∆1, ∆2) = ∆1 ∧ ∆2 negation translations—except that this definition only satisfies Unit ∀α.α ⊃ Tα Ext We also define the formulas T = , T = UnitT if we explicitly assert inconsistency (as in [20]). With ∀αβ.(α ⊃ Tβ) ⊃ (Tα ⊃ Tβ), and MonT = UnitT ∧ ExtT , as well other T, we might hope to recover other modal logics and as the operator Iα = α, observing that for any formula ϕ free intermediate logics (e.g., Tα = α ∨ #, cf. [17], [27]). ip2 ip2 in T, if ` MonT ⊃ ϕ then ` ϕ[I/T]. IV. D     Theorem III.3. If Γ ` J then `ip2 Mon ⊃ Γ∗ ⊃ J∗ T In this section we elaborate the sense in which the gen- Theorem III.4. For any positive or negative proposition A, if eralized rules of focusing provide a foundation for reasoning `ip2 A∗[I/T], then ` A. about delimited continuations and control operators, and their p σ p Ep d Ed d σ p[σ] p E d E d[σ] { }  p − d − We likewise internalize the identity principles. Let ∆ denote V + K+  V  K the list of variables bound by a context, and {p} the list of − + v K − k V + σ1 σ2 K $v k$V · (σ ,σ ) variables bound by a pattern. Then the identity substitution E .E σ σ 1 2 id is defined together with the identity continuation Id, by V + .EK+ {∆} !V+ K+$.E id · id id , id E E the clauses {·} = , {∆1,∆2} = ( {∆1} {∆2}), and

idk(p) = Id $ k $ p[id{p}] idv(d) = d[id{d}] $ v Fig. 4. Generalized canonical forms Id(p) =!p[id{p}] interaction with side-effects. We do not have space to include C. Delimited control operators many examples here, and the reader is instead referred to our We have used the logical notion of polarity to reconstruct Twelf formalization,2 which closely follows and elaborates this the operational notions of value, continuation, substitution section. and expression. Delimited control operators can already be seen as living inside this logical universe—but where? As we A. Syntax of normal forms explained earlier, negative values are themselves a general Since Figure 3 is only a slight modification of Figure 1, form of control operator, defined by pattern-matching on the interpretation of §II-B also only requires minor adjust- continuations. Recall their generic introduction rule: ment. Our taxonomy of values, continuations, substitutions, α.∆ [N . −] −→ Γ, α.∆ ` α and expressions remains unchanged, except that continuations Γ ` N are associated with positive answer types, and expressions with positive types that are either synthesized or checked The requirement of answer type polymorphism is a strong (depending on whether they correspond to intermediate or restriction, however, which can be relaxed by replacing values ultimate consequences). In Figure 4, we again give an abstract of type N with continuations of type N  Pi .Po (parse this as syntax of canonical forms, derived from Figure 3 in the same (N  Pi).Po). By definition of the positive-polarity implication way Figure 2 was derived from Figure 1. , note the following derived inference rule: B. Dynamic α.∆ [N . −] −→ Γ, ∆[Pi/α] ` Po Γ ` N P . P Here we explain how to turn the composition and identity  i o principles of §III-A into dynamic processes, as usual in a type- Decomposing delimited control effects as a triple brings to generic way. First we introduce a bit of notation: mind Danvy and Filinski’s original type-and-effect system 1. K+ $ E 2. K− $ V − 3. E[σ] 4. K+ ◦ K+ 5. K+ ◦ K− [10] for shift and reset (DF89), but whereas DF89 requires 2 1 2 1 various non-logical rules (including the four-place connective Each of these constructs internalizes one of the composition τ/δ → α/δ to represent functions with embedded control ef- principles, swapping the order of the two premises and leaving fects), we simply instantiate N with basic logical connectives. type constraints implicit. Principles (1) and (2) are justified by By instantiating N with P → N we derive the typing rule the following reductions to principles (3)–(5): for functions with embedded effects, and by instantiating it K+$!p[σ] { K+(p)[σ] d[σ] $ V − { V −(d)[σ] with ↑P we derive the “shift” operator, which binds a single

+ + + + + − + − continuation variable: K $ (K1 $ .E) { (K ◦ K1 ) $ .EK $ (K1 $ v) { (K ◦ K1 ) $ v Γ, P . Pi ` Po Principles (3) and (4) are reduced as follows: Γ ` ↑P  Pi . Po 3) E[σ] is the capture-avoiding substitution of σ into E (which creates instances of (1) and (2)) We emphasize that we are not inventing any new rules here, 4) K+ ◦ K+ is defined by (K+ ◦ K+)(p) = K+ $ K+(p) merely expanding definitions. A useful exercise for the reader 2 1 2 1 2 1 is to likewise verify the following rules for building strict and Finally, (5) reduces to multiple applications of (4), relying on lazy pairs of terms with delimited control effects, by the same the following observation: sort of transformations we gave at the end of §II-B (we elide Proposition IV.1. Any α.∆ decomposes as the interleaving the fixed context Γ): of a constant context ∆0 with a parametric context α.P . 1 ↑P1  Px . Po ↑P2  Pi . Px N1  Pi . Po N2  Pi . Po α, . . . , P . α. Thus any substitution σ for P/α decomposes n ∆[ ] ↑(P1 ⊗ P2)  Pi . Po N1 N2  Pi . Po as the interleaving of a substitution σ0 for ∆0 together with N + + Observe that the answer types in the ⊗ rule now reflect left- a substitution (K1,..., Kn) for P1 . P,..., Pn . P, which we 0 + + to-right evaluation, whereas the unbiased rule reflects lazy express as σ = σ + (K ,..., Kn). 1 evaluation. N Composition principle (5) is therefore reduced as: This analysis is very close to Kiselyov and Shan [28], + 0 0 + 0 + 0 + 0 + + 0 + who also make use of three kinds of “arrows”. More or K2 ◦ d[σ + (K1 ,..., Kn )] { d[σ + (K2 ◦ K1 ,..., K2 ◦ Kn )] less, what we write →, , and ., they write as →, ↑, and 2http://www.pps.jussieu.fr/~noam/delimited/ ↓ (unfortunately clashing with our notation for the polarity coercions). An important feature of their type system, which case (i.e., when each type has finitely many patterns and the is more general than DF89, is that it allows access to delim- definition ordering is well-founded). Combining this with The- ited continuations beyond the nearest dynamically-enclosing orem III.6, it should be possible to connect equality of focusing delimiter. Essentially, this is because (their) ↓ can be nested. proofs in the free/identity monad cases to normalization-by- Here, the same is achieved by internalizing . with , since evaluation-based decision procedures for with any positive continuation K :: P . Pr can be represented as weak/strong sums [9], [1]. a positive value ↑K :: ↑P  Pr. Like Kiselyov and Shan, we E. Monadic reflection derive the “reset” operation, which converts ↑P  P. Po to Po by plugging the continuation with the value ↑Id, where Id is We end this section by briefly relating the concepts we’ve the identity continuation P . P. been exploring to Filinski’s monadic reflection [14], [16], Finally, we note that the relationship between values of a way of mediating between the “internal view” of effects type N and continuations of type N  Pi . Po accords with (typical of Haskell) where monads are used as a way of Asai and Kameyama’s definition [4] of purity as answer type organizing purely functional code, and the “external view” polymorphism. (typical of ML) where at least a few effectful operations are built into the of the language. Intuitively, D. Equality and external effects reflect takes a pure piece of data of some monadic type Tα Again, we view the identity and composition principles of and performs the effect it represents, yielding a computation §IV-B as included among potentially many different notations of type α. Conversely, reify takes an effectful computation of for canonical forms (cf. [40]). Equivalence of notations is type α and represents it as a pure term of type Tα. “Filinski’s defined as equality of their denoted canonical forms. Now, representation theorem” is the observation that the delimited in the classical case, we placed no non-trivial equations on control operators shift and reset can be used to implement focusing proofs themselves (only α-equivalence), with every reflect and reify for any monad [14]. syntactically distinct expression representing a semantically To begin, we will explain Filinski’s representation theorem distinct, sequential evaluation strategy. In the general setting, in terms of elementary category theory [32].3 We then describe though, we have new possibilities. Consider the following how to program this theorem directly, given one more natural expression (where .E $ K+ is sugar for K+ $ .E): generalization of the proof theory of polarity. Let Set be the category of sets. A monad on Set is a triple .E1 $ p1 .E2 $ p2 E(p1,p2)   of a functor T : Set → Set, a map ηP : P → TP (the unit of ∗ Assuming that p1 and p2 bind distinct variables and that .E2 the monad) for each object P ∈ Set, and a map K : TP1 → does not depend on p1, the preceding might be considered TP2 (the operation) for each map K : P1 → TP2, equivalent to satisfying the equations ∗ ∗ ∗ ∗ ∗ ∗ .E2 $ p2 .E1 $ p1 E(p1,p2) η = Id K ◦ η = K (K ◦ K ) = K ◦ K   P TP 1 2 1 2 On the other hand, intuitively this equivalence is only valid if Given a monad T on Set, the Kleisli category SetT shares .E1 and .E2 do not have interfering side-effects. the same objects as Set and has morphisms SetT (P1, P2) = As hinted by the translation of §III-B, we can interpret Set(P1, TP2), with identity and composition defined using proofs/programs with respect to an ambient notion of com- the monad’s unit and extension operation. The functor ET : ∗ putation T, indeed a monad [36]. The syntactic inclusion ! of SetT → Set is defined by ET P = TP and ET K = K . + values V in (checking) expressions E may be recognized as Now by the Yoneda lemma, for every object P ∈ SetT , there the unit of the monad, and the application of a continuation is a bijection between ET P and the set of natural transforma- + K to a (synthesizing) expression .E as its bind operation. In tions from the hom-functor SetT (P, −) to ET . Concretely, given general, then, which additional equations we need to place on an element E ∈ TP, we can form the natural transformation these canonical forms depends on the monad we have in mind. which maps any K : P → TP0 to K∗(E) ∈ TP0. Conversely, This general question is beyond our scope here, but we can given V ∈ Nat(SetT (P, −), ET ), we can obtain V(ηP) ∈ TP. consider a few interesting cases: We claim that the two directions of the Yoneda isomorphism • T the free monad: no additional equations. are Filinski’s original implementation of reflect and reify, up • T a commutative monad [26]: the above and similar to minor details (e.g., Filinski uses a universal type to get equations should be derivable. around limitations of ML’s type system). • T = I (identity monad): further equation should be To see this directly in polarized logic, we have to make derivable, expressing that computations can be freely explicit some of the type structure which has thus far been duplicated or thrown away. implicit in our analysis. We explained (§III-B, §IV-D) that The commutative monad equations are dealt with in CLF polarized types and terms can be interpreted with respect to a [45] in terms of a notion of concurrent contexts (which monad, implicit and hence fixed. Suppose we make the monad generalize evaluation contexts), and we believe this approach 3The connection between Filinski and Yoneda has as far as I know not can be adapted to validate the additional equations of the been previously pointed out, although a very similar connection was drawn identity monad, yielding a decision procedure in the finitary in a blog post by Dan Piponi [37]. explicit on the negative side, defining a class of negative types [5] M. Biernacka, D. Biernacki, and O. Danvy. An operational foundation for delimited continuations in the CPS hierarchy. Logical Methods in NT for any internal monad T. An internal monad is defined , 2(1), 2005. as above, by a family of continuations ηP :: P . TP and [6] K. Chaudhuri, F. Pfenning, and G. Price. A logical characterization of − ∗ 0 ` 0 forward and backward chaining in the inverse method. IJCAR 2006. continuation transformers ( ) :: P.TP TP.TP , satisfying [7] A. Ciabattoni, N. Galatos, and K. Terui. From axioms to analytic rules the above equations. The positive-to-negative coercion is then in nonclassical logics. LICS 2008. T [8] V. Danos, J.-B. Joinet, and H. Schellinx. A new deconstructive logic: written ↑ P (annotated explicitly with an internal monad), Linear logic. The Journal of Symbolic Logic, 62(3):755–807, 1997. defined by α.P . Tα [↑T P . −]. We forgo recapitulating the [9] O. Danvy. Type-directed partial evaluation. POPL 1996. [10] O. Danvy and A. Filinski. A functional abstraction of typed contexts. generic rules of inference and composition for negative types Tech. Rep. 89/12, DIKU, Univ. of Copenhagen, Denmark, 1989. NT , which are straightforward generalizations of the rules with [11] O. Danvy and A. Filinski. Abstracting control. LFP 1990. 4 [12] R. Dyckhoff and S. Lengrand. Call-by-value λ-calculus and LJQ. the implicit monad. Journal of Logic and Computation, 17:1109–1134, December 2007. Now, Filinski’s representation theorem is realized as a [13] M. Felleisen. The theory and practice of first-class prompts. POPL 1988. [14] A. Filinski. Representing monads. POPL 1994. simple isomorphism between expressions of positive type TP [15] A. Filinski. Controlling Effects. PhD thesis, Carnegie Mellon University, and values of negative type ↑T P: given an expression E :: TP, Pittsburgh, Pennsylvania, May 1996. ∗ T [16] A. Filinski. Monads in action. POPL 2010. we can form the negative value ↑k k $ E :: ↑ P, and [17] H. M. Friedman. Classically and intuitionistically provably recursive conversely, given a negative value V − ::↑T P, we can form the functions. Higher Set Theory, 21–27. Springer-Verlag, 1978. − [18] J.-Y. Girard. A new constructive logic: Classical logic. Math. Structures expression ↑ηP $ V :: TP. in Computer Science, 1:255–296, 1991. [19] J.-Y. Girard. On the unity of logic. Annals of Pure and Applied Logic, 59(3):201–217, 1993. V. C [20] J.-Y. Girard. Locus Solum: From the rules of logic to the logic of rules. Math. Structures in Computer Science, 11(3):301–506, 2001. Much of the classical literature on polarity takes place in the [21] V. I. Glivenko. Sur quelques points de la logique de M. Brouwer. Bulletins de la classe des sciences, 15:183–188, 1929. context of classical linear logic, with elegant connections to [22] H. Herbelin. On the logical content of control delimiters. Talk at the game semantics and continuation-passing. On the other hand, PPS/πr2 seminar Théorie des types et réalisabilité, Nov. 18, 2009. [23] J. M. Howe. Proof search issues in some non-classical logics. PhD less systematic polarity-like distinctions have been invented to thesis, University of St Andrews, Dec. 1998. tackle difficult problems in intuitionistic circles, particularly to [24] M. Hyland, P. B. Levy, G. D. Plotkin, and J. Power. Combining algebraic effects with continuations. TCS, 375(1-3):20–40, 2007. better understand effectful programming and dependent types. [25] I. Johansson. Der Minimalkalkül: Ein reduzieerter intuitionistischer We have shown that “intuitionistic polarity” really does arise Formalismus. Compositio Mathematica, 4:119–136, 1937. [26] D. J. King and P. Wadler. Combining monads. Glasgow Workshop on from the same principles as classical polarity, but in the more Functional Programming, 1992. general framework of -passing, and that [27] O. Kiselyov. Constructive law of excluded middle. Post to TYPES mailing list, September 10, 2009. this setting enriches constructive logic with delimited control [28] O. Kiselyov and C. Shan. A substructural type system for delimited operators. continuations. TLCA 2007. [29] O. Laurent and L. Regnier. About translations of classical logic into Although we believe this particular generalization of the polarized linear logic. LICS 2003. notion of polarity is well-motivated, it is also clearly only a [30] P. B. Levy. Call-By-Push-Value. PhD thesis, Queen Mary, University of London, 2001. step towards better understanding and further generalization. [31] C. Liang and D. Miller. Focusing and polarization in intuitionistic logic. How does our interpretation of negative polarity relate to the CSL 2007. [32] S. MacLane. Categories for the Working Mathematician Springer- algebraic view in other recent studies of control and effects Verlag, 1971. [24], [35]? Can the manipulations we made in §IV-E be related [33] S. McLaughlin and F. Pfenning. Imogen: Focusing the polarized inverse method for intuitionistic propositional logic. ICLP 2008. to the more abstract study of the Yoneda lemma [42]? Can [34] P.-A. Melliès and N. Tabareau. Resource modalities in game semantics. the hierarchy of control operators [11], [5] be related to the Annals of Pure and Applied Logic, 161(5):632–653, 2010. [35] R. E. Møgelberg and A. Simpson Relational parametricity for compu- polarized generalization of the quantifier hierarchy [7], and to tational effects. LICS 2007. hierarchies in higher category theory? And what can we learn [36] E. Moggi. Notions of computation and monads. Information and Computation, 93(1):55–92, 1991. from such relationships? [37] D. Piponi. Reverse engineering machines with the Yoneda lemma, Nov. 12, 2006. Weblog entry, A Neighborhood of Infinity. Acknowledgments. Many thanks: Andrzej Filinski, Jonas [38] G. Restall. Multiple conclusions. In 12th International Congress on Logic, Methodology and Philosophy of Science, pp. 189–205, 2005. Frey, Hugo Herbelin (particularly for a thought-provoking talk [39] J. C. Reynolds. Theories of Programming Languages. Cambridge University Press, Cambridge, England, 1998. [22]), Danko Ilik, Dan Licata, Paul-André Melliès, Jeffrey [40] H. Schwichtenberg. Finite notations for infinite terms. Annals of Pure Sarnat, Robert Simmons, and the LICS reviewers. and Applied Logic, 94(1–3):201–222, 1998. [41] P. Selinger. Control categories and duality: on the categorical semantics  of the lambda-mu calculus. MSCS, 11(2):207–260, 2001. R [42] R. Street and R. Walters. Yoneda structures on 2-categories. Journal of [1] T. Altenkirch, P. Dybjer, M. Hofmann, and P. Scott. Normalization by Algebra, 50:350–379, 1978. evaluation for typed lambda calculus with coproducts. LICS 2001. [43] T. Streicher and B. Reus. Classical logic, continuation semantics and [2] J.-M. Andreoli. Focussing and proof construction. Annals of Pure and abstract machines. JFP, 8(6):543–572, Nov. 1998. Applied Logic, 107(1):131–163, 2001. [44] H. Thielecke. Categorical Structure of Continuation Passing Style. PhD [3] J.-M. Andreoli. Logic programming with focusing proofs in linear logic. thesis, Laboratory for Foundations of Computer Science, University of Journal of Logic and Computation, 2(3):297–347, 1992. Edinburgh, 1997. [4] K. Asai and Y. Kameyama. Polymorphic delimited continuations. [45] K. Watkins, I. Cervesato, F. Pfenning, and D. Walker. A concurrent APLAS 2007. logical framework I: Judgments and properties. TR CMU-CS-02-101, Dept. of CS, Carnegie Mellon Univ., 2002. Revised May 2003. [46] N. Zeilberger. Focusing & higher-order abstract syntax. POPL 2008. 4 Intuitively, negative types NT are functors SetT → Set (answer type- [47] N. Zeilberger. The logical basis of evaluation order and pattern- indexed sets of continuations), and negative values are natural transformations matching. PhD thesis, Carnegie Mellon University, Apr. 2009. T NT → ET . The coercion ↑ represents the Yoneda embedding.