<<

arXiv:2001.01337v2 [cs.PL] 7 Jan 2020 eitoueanwdarmai oainfrrepresenting for notation diagrammatic new a introduce We a aigtefudtosfradarmai aclsof diagrammatic a for foundations the laying way .Cnrtl,wa ehv nmn r opttosgen- of) computations (sorts are by mind erated in have we what Concretely, ]. tosi h prto lti n oe 2] a ehave we Say [23]. Power a and such Plotkin fixed of spirit the in ations in hneet r rdcdby produced are finding effects of when computa- tions problem (sequential) effectful the of with presentation concerned convenient a are we note this In Introduction 1 theory Lawvere monad, calculus, graphical tation, effects. Keywords generic and theories Lawvere cal- of a terms such in for culus foundation formal a give We effects. algebraic this diagrams, on num- laws a order-theoretic show and we algebraic computa- Additionally, of pure setting. ber effectful on an results to and tions definitions of simplifying extension way this the returned, values computa- possible a the from during tion produced effects the separates notatioexplicitly Our computations. effectful (algebraic) of result the Abstract 00 C SN978-x-xxxx-xxxx-x/YY/MM...$15.00 ISBN ACM 2020. Effects, Algebraic for Calculus Diagrammatic A fteeauto faterm a of evaluation the of ahrta en ooihcobject. monolithic a being than rather seet r efre yoeain,adtu ewould we thus and operations, expect by performed are sepa- unsatisfactor effects rather to is as one This allow effects. not from terms/values does rate and semantical purely con- is the on trary, second, semantical The any produced. effects give the not on information does and syntactical, essentially is usinaetefollowing: the are question ohteeapoce aeterdabc.Tefis one first The drawback. their have approaches these Both 2. 1. ihagbacoeain,adwoelae r either are leaves whose and operations, algebraic with auso h otmsymbol bottom the or values ocluiwt leri ffcsi,eg,[1 3 28]. 23, [11, e.g., semantics in, J operational effects give algebraic with to calculi used computation to are as and known [6] are trees structures These gence. oe n .. 3 13]. [3, e.g., in, lowed and J nvriyo oon n NI ohaAntipolis Sophia INRIA and Bologna of University t t J K K igamtcCluu o leri Effects Algebraic for Calculus Diagrammatic A t K sa lmn in element an is sapsil nnt rewoendsaelabeled are nodes whose tree infinite possibly a is V ob aeo n‘ffc at n vlepart’, ‘value a and part’ ‘effect an of made be to leri ffc,gnrceet igamtcno- diagrammatic effect, generic effect, algebraic λ stesto aus hsi h prahfol- approach the is This values. of the is cluu:hwd erpeetteresult the represent we do how -calculus: λ [email protected] cluierce ihagbacoper- algebraic with enriched -calculi g a Lago Dal Ugo T (V) t w tnadasest this to answers standard Two ? where , leri operations algebraic ⊥ T eoigpr diver- pure denoting sasial monad suitable a is rlmnr Report Preliminary [25 J t n y K ? 1 T v oksillcsapoe nrdcint oasadalge- and monads to introduction proper a lacks still work have browsers different that noticed also have authors The weeks. next the explanations—in informal and examples with h atraetriples are latter The 1 prpit (co)domains. appropriate of unit the eei ffcsw rsn h pair the present we effects generic oexpress to h lil xeso of extension Kleisli the ec ewe leri ffcsand effects algebraic between dence e n ucin,wihw rsn as present we which functions, and set trivial h oa with monad the hr lnt ytmtclyrve n paeit—especiall update and review systematically to plan thors Advertisement for computations. calculus sequential diagrammatic effectful lightweight a obtaining way this ai aeoyter 1]addmi hoy[1]. theory domain and with familiar [18] be theory to category reader basic the assume thus this and of effects, braic version current The mon- operations. on notions algebraic background and some ads give we section, this In Algebraic and Monads Preliminaries: 2 rendering. better a have to tend view- ers pdf ‘desktop’ whereas diagrams, of rendering different lil aeoyof category Kleisli oeigntoso opttos[0 1 0.Aogthose Among are: 30]. 21, [20, computations of notions modeling 2.1. Example >> f ì .Temyemonad maybe The 1. ealta n oa on monad any that Recall (∅) = sals fvle.I re ofcltt acltoswith calculations facilitate to order In values. of list a is nvriyo oon n NI ohaAntipolis Sophia INRIA and Bologna of University : ic ewr in work we Since nti oew hwta ecnrl ntecorrespon- the on rely can we that show we note this In eassume We ewr ih(tog monads (strong) with work We o oai eunig hti,given is, That sequencing. monadic for X η gence. Operations X † = → 1] hsi h aefralmnd on monads all for case the is This [19]. = ∅ T id and J ( T t Y T K ( aifigtefloiglw,for laws, following the satisfying , [email protected] ) X sapair a as T ewrite we , η ) h agtmnd ehv nmn r those are mind in have we monads target The ( ; rnec Gavazzo Francesco X T ob nijcin enn that meaning injection, an be to hsnt sawr npors.Teau- The progress. in work a is note This ( ) T X = Set f . ) † Set ,for 1, ( = ( T ◦ eotnie s h idnotation bind the use oftentimes we , Γ M , o vr set every for 1 µ , f η sstrong. is v η ì X ( >> ) , X : where , − X = = X ) † f ) , f , where , → npaeof place in ; ∅ X ednt by denote We . 1 TY Γ ( on g Γ + sa(eei)eetand effect (generic) a is eei operations generic † , v and ◦ ì {↑} X Set f ) f † n h n with one the and , lil triples Kleisli diagrammatically, † h aeoyof category the , f η oeigdiver- modeling : = † µ ( TX Set : µ ( g ∈ X ) . f † xetfor except , T , Kl ◦ → → T g ( f X ( having is T ) TX TY ) † ) . non- [18]. [25] and the is is y ADiagrammaticCalculusforAlgebraicEffects, UgoDalLagoand Francesco Gavazzo

2. The exception monad E(X ) , X + E modeling compu- 3 A λ-Calculus with Algebraic Effects tations raising exceptions in a given set E. from a given At this point of the work we have to make some design 3. The powerset monad P modeling pure nondeterminism. choices, as well as introduce some (minor) restrictions on 4. The (discrete) subdistribution monad D modeling prob- the collection of monads and effects studied. In order to mo- abilistic nondeterminism. tivate such choices and restrictions, it is convenient to have , S 5. The global state monad G(X ) M(X × S) , modeling a concrete computational calculus with algebraic effects. We imperative computations over a store S (e.g. given a set take the calculus of [3]. L of locations, take S , {0,1}L, meaning that a store assigns to each location a boolean value). Definition3.1. Let Σ beasetofoperationsonagivenmonad , ∞ ∞ 6. The output monad O(X ) C ×M(X ), where C is the T . The calculus ΛΣ has terms and values defined by the fol- set of finite and infinite strings over a fixed alphabet C. lowing grammar, where x ranges over a countable set of 7. Combinationsthereof,suchasmonadsoftheformT (M(X )) variables, and σ ∈ Σ. or T (X × S)S (see [8]). t, s ::= x | λx.t | ts | σ(t, ... , t) Monads alone can structure effects and (sequentially) com- = pose them, but do not have the structure to actually produce v,w :: x | λx.t them (with the irrelevant exception of the trivial effect). For Λ Σ this reason we consider monads coming with effect-triggering Notice that Σ is parametric with respect to a set of op- Σ operations. Following Plotkin and Power [23, 24], we re- erations and to a -algebraic monadT . Although the monad quire such operations to be algebraic. T plays no role in Definition 3.1 and calculi are usually de- fined relying on uninterpreted symbols (which Definition 2.2. An n-ary (set-indexed family of) operation are then interpreted as algebraic operations once giving se- n mantics to the calculus), for the sake of the economy of the σX : TX → TX is algebraic, if for all sets X ,Y , f : X → work we chose the ‘semantic-oriented’ presentation of Def- T (Y ), and µ1, ... , µn ∈ T (X ), we have: inition 3.1. ... >>= = >>= ... >>= (σX (µ1, , µn )) f σY (µ1 f , , µn f ). Example 3.2. 1. Taking the maybe monad (and no opera- tion) we obtain the pure λ-calculus. Example 2.3. Referring to the monads in Example 2.1, the 2. Taking the exception monad with the raising exception following operations are all algebraic. operation(s) of Example 2.3 we obtain a λ-calculus with 1. The maybe monad comes with no operation. exceptions. 2. The exception monad comes with a set of zero-ary op- 3. Taking the powerset with set-theoretic union (as in Ex- erations raisee , for e ∈ E, that simply corresponds to ample 2.3), we obtain the nondeterminism λ-calculus [5, inr (e). 14, 22]. 3. The powerset monad P comes with set-theoretic union 4. Taking the subdistribution monad D and fair probabilis- ∪ modeling binary nondeterministic choice. tic choice (as in Example 2.3) we obtain the probabilistic 4. The subdistribution monad D comes with binary fair λ-calculus of [4]. = 1 + nondeterministic choice ⊕ (recall that (µ⊕ν)(x) 2 µ(x) 5. Takingtheglobalstatemonadandtheoperationforread- 1 2ν(x)). ing and writing stores of Example 2.3 we obtain the im- 5. Theglobalstate monad G(X ) , M(X ×S)S , comes with a perative λ-calculus. set of binary operations readℓ for reading locations, and 6. Taking the output monad and the printing operation(s) a set of unary operations writeℓ for writing locations of Example 2.3 we obtain the λ-calculus with output [7, (see [24, 27]). 29]. 6. The output monad comes with a set of unary operations printc indexed over elements of C for printing. That is, We follow standard notational conventions, as in [2]. In printc.(w, x) = (cw, x). particular, we denote by Λ and V the collection of closed terms (programs) and values. Additionally, we write let x = As highlighted by Example2.3 several computational ef- t in s for (λx.s)t. fects can be modeled using monads and algebraic opera- Next, we want to give, say call-by-value, semantics to ΛΣ. tions. Notable exceptions are continuations and exception To do so, we follow [3] and give a monadic semantics to handling. In this paper we will consider computational ef- the calculus. That is, to any closed term t is associated an fects modeled as a monad T together with a set Σ of alge- element t inT (V) (we call such elements monadic values). Σ Λ braic operations on T . In those cases, we say that T is - Intuitively,J K the map − : → T (V) should be defined as algebraic. follows. J K 2 ADiagrammaticCalculusforAlgebraicEffects ADiagrammaticCalculus for Algebraic Effects,

4.1 Countable Monads v , η(v) First, we have to show that any monadic value has indeed a ‘value component’. Mimicking the terminology employed JtsK , t >>= (v 7→ ( s >>= (w 7→ vw ))) for subdistributions, we refer to such a component as sup- port.Asthesetoftermsis countable,weexpectanymonadic σ(t1, ... , tJn )K , σJ (K t1 , ... , tnJ )K J K value to have countable support. In general, not all monads J K J K J K Obviously, a map − defined in such a way may not ex- come with a notion of support, and even those that have ist. In order to ensureJ K the existence of − [3] required T such a notion may have objects with uncountable support to come with a suitable domain structureJ (notably,K ω-cppo (e.g. the continuation monad [9]). As a consequence, we need enrichment [12]). to isolate the class of monads whose elements have count- able support. Definition 3.3. Let T be a Σ-algebraic monad. We say that Σ First, we need monad to preserves injections (the reason T is -continuous if for any set X , TX carries an ω-cppo why we need such monads will become clear soon). That is, structure such that both >>= and operations in Σ are continu- ι ֒ Tι ֒ ous in all arguments. if A −→ X is the subset inclusion map, then T (A) −−→ T (X ) is an injection (i.e. a mono), which we regard as monadic Proposition 3.4 ([3]). IfT is Σ-continuous, − : Λ → T (V) inclusion. Notice that if T preserves weak pullbacks, then it is the least map satisfying the following laws:J K also preserves monos. This condition is met by all the mon- ads in Example 2.1 (see, e.g., [10]). , ( ) v η v Given a monadic object µ ∈ T (X ), the support of µ is the ι JtsK , t >>= (v 7→ ( s >>= (w 7→ vw ))) smallest set A ֒−→ X such that µ ∈ T (A). We denote such a set by supp(µ). Of course, in general the support of µ need σ(t1, ... , tJn )K , σJ (K t1 , ... , tnJ )K J K not exist and thus we restrict our analysis to monads coming J K J K J K Example 3.5. All monads in Example 2.1 are Σ-continuous with a notion of countable support. (wrt the set of operations of Examples 2.3). The ω-cppo struc- ture is standard (e.g. the one induced by subset inclusion for Definition 4.1. We say that a monad is countable if for any set X and any element µ ∈ T (X ), there exists a smallest P, or by the pointwise order for D), with the exception of ι ∋ the one for the output monad, which is induced by the fol- countable set Y ֒−→ X , denoted by supp(µ), such that µ lowing order: T (Y ) (i.e. there exists ν ∈ T (Y ) such that µ = (Tι)(ν)). (u, µ)⊑(w,ν) ⇐⇒ (µ = inr (↑) ∧ u ⊑ w) Example 4.2. All the monads in Example 2.1 are countable, with the exception of the powerset monad. Nonetheless, we ∨(µ = inl (x) = ν ∧ u = w). can regard P as countable (by taking its countable restric- 4 Countable Monads and Generic Effects tion), the collection of λ-terms being countable itself. In previous section we defined the (result of the) evaluation Remark 1. The notion of support is sometimes formalized of a program t as a monadic value t ∈ T (V). Working throughout the notion of an accessible functor (monad, in our case). Accordingly, a monad T is κ-accessible, for a car- with monadic values has the major advantageJ K of providing semantical information on the effects performed. However, dinal κ, if for any element µ ∈ T (X ) there exists A ⊆ X it also has a major drawback: t being a monolithic object, with cardinality strictly smaller than κ such that µ ∈ T (A). If κ = ℵ , then T is said to be finitary as, intuitively, any it does not allow a clear distinctionJ K between the effects pro- 0 duced by t and the possible results (values) obtained. element in T (X ) has a finite support. Moreover, if T pre- Intuitively, since during the evaluation of t effects can serves weak pullbacks, then it also preserves finite intersec- only be produced by (algebraic) operations, we would ex- tion, and one can define for µ ∈ TX , the support of µ as: (Γ h i i) Γ pect t to be a pair of the form , vi i ∈I , where is a supp(µ) , {A ⊆ X | µ ∈ TA}. mathematicalJ K object describing the effects produced by t, Ù and hvi ii ∈I is the list of possible values returned. Since we deal with elements with countable support, we Notice that since the calculus may be nondeterministic, need to shift from finitary monads to ℵ1-accessible monad. I has cardinality bigger than one in general, and that due As a consequence, preservation of weak pullbacks is not to recursion I may be infinite (but countable). To see that, enough to guarantee the existence of {A ⊆ X | µ ∈ TA}, consider the nondeterministic program t0, where n stands as we now need T to preserve countableÑ intersections. for the (Church)numeral of n, succ is the successor function, and t is recursively defined by the equation t = λx.(x ∪ t(succ x)). 4.2 Generic Effects The purpose of this section is to prove that the aforemen- Working with countable monads we can think of the sup- tioned composition is indeed always possible. port of a monadic object as its ‘value component’. What 3 ADiagrammaticCalculusforAlgebraicEffects, UgoDalLagoand Francesco Gavazzo about its ‘effect component’? Here we show that such a com- elements. That is, we replace I with sets n, where n ∈ N∞ , ponentcanbeformallydescribedusing thenotionofa generic N ∪ {ω} and3 n , {1, ... , n} if n , ω, and n , N, if n = ω. effects [25]. Achieving such a goal, however, requires to in- Formally, we should work ℵ1, the skeleton ofthe category troduce some mathematical abstractions. As a consequence, of countable sets and all functions between them, instead of ∞ ∞ we we first study a concrete example which will then gen- N (notice that ℵ1 and N have the same objects, but the eralise to arbitrary (countable) monads. former has ‘more arrows’, so to speak). Nonetheless, think- ∞ ing of N in place of ℵ1 is perfectly fine for building intu- Example 4.3 (Probabilistic computations). When working itions. Abusing the notation, we write n ∈ ℵ1 to state that n with (discrete) subdistributions, hence with the monad D, is an object of ℵ1. it is oftentimes convenient to represent subdistributions as syntactic objects called formal sums. A formal sum (over a Theorem 4.4 ([9, 16, 26]). For any set X we have the isomor- set X ) is an expression of the form i ∈I pi ; xi , where I is a phism countable set, p ∈[0,1] and x ∈ X ,Í and p ≤ 1. n ∈ℵ1 i i i i TX  T (n)× X n. The notation i ∈I pi ; xi is meant to recallÍ the semantic ¹ counterpart of formalÍ sums, namely subdistributions. How- n ∈ℵ1 n ever, we should keep in mind that formal sums are purely Let us decode Theorem 4.4. First, T (n)× X is a co- syntactical expressions. For instance, abusing a bit the nota- end [17], an abstract notion that is not∫ neededto achieve our ∈ℵ 1 + 1 n 1 (n)× n (n)× tion, 2 ; x0 2 ; x0 and 1; x0 are two distinct formal sums, al- goals. For us, T X is the quotient of n ∈ℵ1 T n though they both denote the Dirac distribution on x0. More X for a suitable∫ equivalence relation. Ý generally, there is an interpretation function I mapping each More precisely, we can translate Theorem 4.4 as follows: formal sum p ; x to a subdistribution µ on X defined i ∈I i i Theorem 4.5. For any set X , all elements in T (X ) can be as µ(x) , Í= p . Additionally, the map I is a surjection, xi x i (non-uniquely) presented as elements in meaning thatÍ any subdistribution can be represented as a (non-unique) formal sum. T (n)× X n ØN∞ Examining a bit more carefully a formal sum i ∈I pi ; xi , n ∈ we see that the latter consists of an I-indexed sequenceÍ hpi ii ∈I Moreover, there is a surjective map I such that for any µ ∈ ∞ of elements in [0,1] and an I-indexed sequence hxi ii ∈I of T (X ) there exists n ∈ N such that µ can be uniquely repre- elements in X . Therefore, a formal sum is just a pair of se- sented as an equivalence class modulo =I , the kernel of I, of I I n quences (hpi ii ∈I , hxi ii ∈I )∈[0,1] × X such that i pi ≤ 1. an element in T (n)× X . But the latter requirement means precisely that hp i is ac- Íi i ∈I n n Replacing n ∈N∞ T ( )× X with the more correct (yet tually a subdistribution on I (the one mapping i to pi ). There- n I morally equivalent) T (n)× X , we achieve the corre- fore, we see that formal sums are just elements in D(I)×X . Ð n ∈ℵ1 Putting all these observations together, we see that for spondence Ý n ∈ℵ1 any µ ∈ D(X ), there exists a countable set I and an element n n I T (X )  T (n)× X = ( T (n)× X )/= F ∈ D(I)× X such that I(F) = µ. As a consequence, stip- ¹ I I 2 nÞ∈ℵ1 ulating two formal sums F1, F2 ∈ D(I)× X to be equal (notation F1 =I F2) if I(F1) = I(F2), then we see that D(X ) Let us see how to prove Theorem 4.5. In the following, I = n is isomorphic to the quotient set ( I D(I)× X )/ I, where we will oftentimes regard a sequence in X as a function in I ranges over countable sets. Ð n → X .

Summing up, Example 4.3 shows that any subdistribution Proof of Theorem 4.5. First, let us define the map I. Given Γ ∈ (n) n → ∈ ℵ I(Γ ) , µ ∈ D(V) can be decomposed as a pair (hpi ii ∈I , hvi ii ∈I ), T and s : X (where n 1), define , s ( )(Γ) I ∈ ( ) for some (countable) set I, where hpi ii ∈I is the ‘effect’ part T s . Next we prove Surjectivity of . Let µ T X . Since of µ, and hv i ∈ is the value part of µ. Even if the such a T is countable, supp(µ) is countable, and thus isomorphic to i i I n decomposition is not unique, we can give definitions and , for some object n of ℵ1. any µ ∈ T (X ) has a countable prove results on such decompositions and extend them to support. Let f : supp(µ) → n be such a bijection. Then we ( ( )( ) −1) I( ( )( ) −1) = ( −1)( ( )( )) = subdistributions by showing invariance with respect to =I, present µ as T f µ , f . Since T f µ , f T f T f µ  which is usually trivial. µ we are done. Can we generalize Example 4.3 to arbitrary (countable) n n We call elements in n ∈ℵ1 T ( )×X formal presentations. monads? Before giving examplesÝ of formal presentations we intro- First, let us observe that since the set I in Example 4.3 duce a diagrammatic notation for them. is countable, we can replace it with an enumeration of its 3Counting from one rather than from zero simplifies the notation. We have also implicitly used this convention writing σ (t1, . . . , tn ) for n-ary alge- 2 Notice that =I is the kernel of I. braic operations. 4 ADiagrammaticCalculusforAlgebraicEffects ADiagrammaticCalculus for Algebraic Effects,

5 A Diagrammatic Notation for Generic before, if µ is the result of evaluating a term, we will have Effects either n = 1 (if the term converges) or n = 0 (if the term diverges). Representing monadic elements as formal presentations has = ( ) the major drawback of introducing bureaucracy in the treat- 1. If n 1, we have triples of the form w, 1, x ) which ment of indexes. Consider, for instance, a program of the we write as written as w x. = form let x = t in s. We try to define let x = t in s using for- 2. If n 0, the we can only have triples of the form Γ mal presentation. First, we evaluate Jt obtaining ( K, hvi ii ∈n ) ∈ (w, ⊥, hi), which we write as (w, ↑) . n T (n)×V , for some n ∈ ℵ1. For any i ∈ n, we then evaluate 3. The case for the subdistribution monad goes exactly as = ∆ m s[x : vi ], obtaining ( i , hwji ij ∈mi ), for some i ∈ ℵ1.Asa in Example 4.3. We can represent a formal sum (hpi ii ∈n, hxi ii ∈n ) = n consequence, evaluating let x t in s should give a generic as hxi ii ∈n xi . Ξ l , i effect ∈ T ( ), for l i mi , obtained by some kind of 4. Replacing ([0,1], +) with ({0,1}, ∨) in Example 4.3 we Γ ∆ composition of with theÍ i s together with the sequence obtain formal presentations for elements in P(X ), which hy i i. The resulting expression is rather heavy to write. n ji ji ∈l we may write as diagrams hb i x . For this reason we introduce a diagrammatic notation for i i ∈n i i formal presentations. 5.1 A Calculus of Diagrams: Sequential Definition 5.1. We represent an object (Γ, hxi ii ∈n ) ∈ T (n)× Composition n X as a diagram of the form One of the strengths of monads (at least concerning their n usage in the semantics of programming languages) is that Γ xi i they naturally support the sequential composition of effects. n Here i ranges over elements in and to each i it is associ- Do diagrams do the same? ated the corresponding xi . That is, the horizontal bar with Let us consider again let x = t in s . Suppose: subscript i and target xi stands for the function i 7→ xi . n J K 1. t = Γ v ; n i i If is finite, we can modify Definition 5.1 by extension- m 2. ForJ K any i ∈ n, s[x := v ] = ∆ i w . ally listing all elements x1, ... , xn. For instance, i i j j x:1 J K Γ : Can we find a diagram for let x = t in s ? Since let x = xn n t in s is nothing but a sequentialJ composition,K a natural pro- is the extensional version of Γ x , for n ∈ N. i i posal is to write Example 5.2. 1. Considerthemaybemonad M. We present n m n n Γ ∆ i w an object µ ∈ M(X ) as a pair in M( )× X , for some i i j j n ∈ N∞. Since = M(n)× X n = (n + {↑}) × X n  (n × Λn) + (Λn × {↑}), for let x t in s . But: is this figure meaningful? That is, do weJ have a notionK of composition for diagrams? We are µ is (presented as) either a pair (k, hxi ii ∈n ) or a pair (↑ going to answer such questions in the affirmative. , hxi ii ∈n ). The former corresponds to the case of conver- gence to xk , whereas the latter to divergence. In particu- 5.1.1 Sequential Composition lar, if µ is the result of evaluating a λ-term, then we will actually have n = 1 (if the term converges) or n = 0 (if There are at least two ways to define the sequential compo- the term diverges). sition of two diagrams/formal presentations. The first one • If n = 1, we obtain pairs of the form (1, hxi)), which relies on the correspondence between generic effects and algebraic operations [25]. we write as ↓ x. • If n = 0, then we can only have the pair (⊥, hi), where Proposition 5.3. Any generic effect Γ ∈ T (n) corresponds hi is the empty sequence. We write such a pair as ↑ . to a n-ary algebraic operation γ , and any n-ary algebraic op- Γ n 2. Consider the output monad O. We present an object µ ∈ eration γ corresponds to a generic effect ∈ T ( ). Moreover, if Γ = ...... O(X ) as a pair in ( , hxi ii ∈n) is a formal presentation of µ, then µ γ ( , η(xi ), ). n ∞ n O(n)× X = C ×(n + {↑}) × X , Proof. The bijection between algebraic operations and generic Γ n n for some n ∈ N∞. Therefore, µ is presented as either effects is given in [25]. Given ∈ T ( ) we defineγX : T (X ) → , † Γ n a triple (w, ↑, hxi ii ∈n), or as a triple (w, k, hxi ii ∈n ). The T (X ) as γ (ìx) (ηX ◦x ì) ( ), where xì : → X . Vice versa, n , former case means that we have divergence, and that the given γX : T (X ) → T (X ) we define Γ γn(ηn). Suppose string w is outputted, whereas the latter case means that now (Γ, hxi ii ∈n) is a formal presentation of µ. We write xì n we converge to xk , and that the string w is outputted. As for the sequence hxi ii ∈n regarded as a function → X . We 5 ADiagrammaticCalculusforAlgebraicEffects, UgoDalLagoand Francesco Gavazzo have: Proposition 5.7. Generic effects form a Lawvere theory, and thus we can define the composition of generic effects as com- µ = I(Γ, hxi ii ∈n ) position on their Lawvere theory. † = (ηX ◦ìx) (Γ) † Proof. Let us begin showing that generic effects form a Law- = (ηX ◦ìx) (γn(ηn)) op vere theory. Let Kl(T )ℵ be the opposite of Kl(T ) restricted = † 1 γn((ηX ◦ìx) ◦ ηn) to objects of ℵ1. Since Kl(T ) has countable coproducts, and → ( ) = γn(ηX ◦ìx) the canonical functor I : Set Kl T preserves them, re- stricting I to ℵ1 we obtain a countable-coproduct preserving where the penultimate equality follows by the defining iden- identity-on-objects functor I : ℵ → Kl(T ) . As a conse- tity law of algebraic operations (see Definition 2.2).  1 ℵ1 quence, Kl(T )ℵ1 is the opposite of a Lawvere theory, and Example 5.4. 1. The generic effect corresponding to op- thus Kl(T )op is a (countable) Lawvere theory. Notice that a ℵ1 c op eration(s) printc : O(X )→O(X ) is ( ,1) generic effect Γ ∈ T (n) corresponds to a map in Kl(T ) (n,1), ℵ1 2. The generic effect corresponding to fair probabilistic choice op i.e. as an operation in the Lawvere theory Kl(T ) . We now ⊕ is the distribution h0.5, 0.5i mapping i ∈ {1,2} to 0.5. ℵ1 define composition of generic effects. Given generic a ef- Corollary 5.5. There exists a notion of sequential composi- fect Γ ∈ T (n) and an n-indexed family of generic effects Γ n op tion for generic effects: given a generic effect ∈ T ( ) and a ∆i ∈ T (mi ) (for any i ∈ n), i.e. arrows Γ ∈ Kl(T ) (n,1) ℵ1 n-indexed family of generic effects ∆i ∈ T (mi ), there exists op and ∆i ∈ Kl(T ) (pi ,1), we can define its composition rely- ℵ1 their sequential composition which is a generic effect in T (l), op ing on composition in Kl(T ) (n,1) as follows. Since ∆i ∈ , ℵ1 for l i ∈n mi . op Kl(T ) (mi ,1) and Kl(T )ℵ has products, we have the map Í ℵ1 1 Proof. Let us consider Γ ∈ T (n) and ∆i ∈ T (mi ), for any op h∆i ii ∈n ∈ Kl(T ) ( mi , n), and thus Γ ◦ n n ℵ1 i i ∈ . The former gives the algebraic operation γ : T (X ) → op lan∆i ii ∈n ∈ Kl(T )Í( mi ,1) is the desired generic effect. T (X ). Similarly, the latter gives a family of algebraic opera- ℵ1 i mi  tions δi : (TX ) → T (X ). Taking products, we obtain Í n mi  hδi ii ∈n γ Γ ∆ i mi mi n Coming back to diagrams, given xi and i yj , T (X ) / i ∈n T (X ) / T (X ) / T (X ) i j Í for any i ∈ n, we write their composition as We then take advantageÎ of Proposition 5.3 and define the n m Γ ∆ Γ ∆ i y composition of and i s as the generic effect associated to i i j j the algebraic operation γ ◦ hδi ii ∈n .  Such a notation gives us several advantages, as we are going The drawback of Corollary 5.5 is that we would like to de- to see. However, we first look at some concrete examples. fine the composition of generic effects without going through their associated algebraic operations. To achieve such a goal, Example 5.8. 1. Consider the maybe monad and consider we look at the countable Lawvere theory [9, 15] induced by representations that can be obtained as the result of com- generic effects. putations. Then sequential composition is defined by the following laws: First, observe that ℵ1 has countable coproducts and, up-to equivalence, it is the free category with countable coprod- ↑ ξ , ↑ ucts on 1, where coproducts are given by standard sums N = + + , (thus, for instance, for n ∈ we have n 1 ··· 1). By ↓ ↓ ξ ↓ ξ n 2. Consider the output monad. As before, we consider rep- duality, ℵop has countable products. | {z } 1 resentations that can be obtained as the result of compu- Definition 5.6 ([26]). A countable Lawvere theory is a small tations. Sequential composition is thus defined: category L with countable products and a strict countable- op (w, ↑) ξ , (w, ↑) product preserving identity-on-objects functor I : ℵ1 → L. w (u, ↑) ξ , (wu, ↑) In particular, objects of L are exactly those of ℵ1, and ev- w u ξ , wu ξ ery function between such objects gives a map in L.Amap in L(n,1) represents an operation of arity n (meaning that 3. Consider the subdistribution monad, and denote by pqì we also consider operations with arity ω), whereas a map scalar of p with qì. We then define sequen- in L(n,m) represent m operations of arity n. In particular, tial composition as: given operations σ ∈ L(n,m) and τ ∈ L(m,l), their compo- n mi [l] pì qìi ξj , pì · hìqi ii ∈n ξ sition τ ◦ σ ∈ L(n,l) gives l operations of arity n. i j k k 6 ADiagrammaticCalculusforAlgebraicEffects ADiagrammaticCalculus for Algebraic Effects,

, , where l i ∈n mi and pì ·hìqi ii ∈n hpiqìi ii ∈n regarded Example 5.9. The object H x is ↓ x in the as an l-indexedÍ sequence. maybe monad M, ε x in the output monad O, and 5.2 A Calculus of Diagrams: 1 x in the subdistribution (resp. powerset) monad D Writing diagrams in sequence for sequential composition (resp. P). has important implications. First of all, the very act of writ- Additionally, diagram equality is preserved by diagrams. ing diagrams in sequence for sequential composition pre- n supposes a notion of equality for diagrams. In fact, what we Theorem 5.10. Let µ ∈ T (X ) be presented as Γ x , i i are doing is de facto building an algebra of diagrams. With- and f : X → T (Y ). Then µ >>= f is presented as 4 out much of a surprise, we say that two diagrams ξ , ρ are n Γ f (x ) equal (notation ξ = ρ) if and only if ξ =I ρ is the map of i i Theorem 4.5. Proof. The proof relies on Proposition 5.3, Corollary 5.5, and = It goes by itself that working with I directly may be the defining identity of algebraic operations of Definition 2.2. quite heavy. For this reason, we now prove a collection of n Suppose µ is presented as Γ x . By Proposition 5.3 algebraic results that dispense us from working with = . i i I we have an algebraic operation γ : T (X )n → T (X ) such Such results concern sequential composition, monadic bind- m that µ = γ (... , η(x ), ...). Suppose now ∆ i y is a ing, and sequential composition. i i j j presentation of f (xi ), for any i ∈ n. In particular, by Propo- mi 5.2.1 Algebra of Sequential Composition sition 5.3 we have algebraic operations δi : T (Y ) → T (Y ) = ...... We immediately notice that composition is associative. This such that f (xi ) δi ( , η(yj ), ). Let property is actually built-in the diagrammatic notation, as n mi n ξ , Γ ∆ y = Γ f (x ) evident when writing the composition of three diagrams: i i j j i i By Proposition 5.3 and Corollary 5.5, ξ is associated to the m lj n i i mi Γ ∆i Ξj xk (1) algebraic operation γ ◦ hδi ii ∈n : T (Y ) ∈n → T (Y ), and i j k Í we have Linguistically, this notation describes associativity of sequenc- = ...... ing, usually expressed by program equivalences of the fol- I(ξ ) γ ( , δi ( , η(yj ), ), ) lowing form: We conclude the thesis as follows:

let x = t in (let y = s in r)≡ let y = (let x = t in s) in r. µ >>= f = γ (... , η(xi ), ...) >>= f = γ (... , η(x ) >>= f , ...) Diagram (1) also highlights another important feature of i = diagrams, namely the way they manage index dependencies. γ (... , f (xi ), ...) n m l The ‘geography’ of (1) shows that i ∈ , j ∈ i , and k ∈ j. = γ (... , δi (... , η(yj ), ...), ...) Moreover, by reading from the right to the left we recover in- = I(ξ ) dex dependencies: we see that since k (being in lj) depends on j which depends (being in mi ) on i, k depends on i as where the second line follows by the defining identity of well. algebraic operations (see Definition 2.2).  Second, there is a trivial generic effect H ∈ T (1) (read cap- An important consequence of Theorem 5.10 is the validity ital η) which behaves as a neutral element for composition. of the following law which makes most of the definitions That is, for any x ∈ X , the diagram H x represent given on diagrams invariant with respect to =I , and thus the computation trivially returning x performing no effect valid definitions on monadic elements5. (i.e. η(x)). We have the following laws: n m Γ xi = ∆ yj f : X → T (Y ) H ξ = ξ i j n m Γ f (x ) = ∆ f (y ) n n i i j j Γ H x = Γ x i i i i Example 5.11. Let us consider monadic terms, i.e. elements Linguistically, these laws correspond to the following pro- inT (Λ). Recall that we have defined an evaluation map − : gram equivalences: Λ T → T (V). Define the monadic extension of − by: J K let x = v in t ≡ t[x := v] n T n J K Γ t , Γ t r i i z i i let x = t in x ≡ t J K 5Notice that an analogous rule holds if we replace f : X → T (Y ) with 4We use letters ξ , ρ, ν, . . . for diagrams f : X → Y . 7 ADiagrammaticCalculusforAlgebraicEffects, UgoDalLagoand Francesco Gavazzo

Then − T is automatically well-defined, as by Theorem 5.10 Formally, given a countable set X , any formal presenta- T Γ s n − Jis invariantK under diagram equality, and thus it is in- tion ( , xì) ∈ T ( )× X can be extended to a formal presen- ι .JdependentK of the choice of representatives. Notice also how tation (∆,yì) in T (m)× X m, for any n ֒−→ m the diagrammatic notation makes explicit how definitions Γ and results ‘distribute’ over effects. This is the very essence Lemma 5.12. Let X be a countable set. Then any pair ( , xì) ∈ n n ∆ of algebraic effects, as stated in the defining identity of al- T ( )× X can be extended to a formal presentation ( ,yì) in ι gebraic operations (Definition 2.2). The diagrammatic nota- T (m)× X m, for any n ֒−→ m tion extends this identity to any monadic expressions, as the ι T (ι) , ∆ latter are ultimately built using generic effects, which bijec- Proof. Since n ֒−→ m, then T (n) ֒−−−−→ T (m). Take tively correspond to algebraic operations. T (ι)(Γ) and yì to be any map extending xì according to the following diagram (such maps indeed exists): 5.2.2 Algebraic Operations m yì Thanks to Proposition 5.3, diagrams are also a natural way ⑥/ X O ⑥⑥ > to write algebraic operations. In general, if we present ob- ι ⑥⑥ ⑥⑥ xì jects µi ∈ T (X ) as ξi , then we write ⑥ n? ξ:1 n : σ or σ ξi Γ = ∆  ξn i Indeed ( , xì) ( ,yì). ... for the presentation of σ(µ1, , µn ) (that is, we use the no- For instance, we have seen that the trivial generic effect tation σ both for the algebraic operation and the generic H ∈ T (1) corresponds to the map η1 : 1 → T (1). However, effects associated to it). for any n, there is a map ηn : n → T (n) such that, for any Notice that thanks to Theorem 5.10 both σ(µ , ... , µ ) >>= ι n i ∈ n, η(i) = T (ι)(H), for 1 ֒−→ n sending 1 ∈ 1 to i.Asa 1 f and σ(µ >>= f , ... , µ >>= f ) are presented as 1 n consequence, we see that we can indeed think of H as the n σ f (ξi ), ‘real’ trivial effect, and regard the others as its extension to i larger supports. For instance, we can regard 1; x as the pre- where ξi is a presentation of µi , this way encoding the defin- sentation of the Dirac distribution on x, and formal sums ing identity of algebraic operations (Definition 2.2) in dia- such as 1; x + 0;y + 0; z as extensions of 1; x. grams. Linguistically, this corresponds to the program trans- formation Order-theoretic Properties We now analyze the order-theoretic properties of diagrams. For technical reason, we need to re- = ...... = ... let x σ(t1, , tn ) in s ≡ σ( , let x ti in s, ). quire T (1) to have at least two elements6, i.e. η(1) , ⊥. First, Having seen the basic algebra of diagrams, we move to the we observe that there is a bottom effect ⊥ ∈ T (0), which we study of their order-theoretic properties. write as ⊥ . It is easy to see that such element is ↑ in the maybe monad, (ε, ↑) (where ε is the empty string) in the 5.3 A Calculus of Diagrams: Order output monad, and 0 in the subdistribution monad. So far we have focused on algebraic properties of diagrams. Actually, for any n there is a bottom effect ⊥n ∈ T (n) such However, working with Σ-continuous monads, we can ex- n n that ⊥ x ⊑ Γ y . This is a bit unsatisfactory, tend the order ⊑ on diagrams by stipulating ξ ⊑ ρ if and n i i i i only if I(ξ ) ⊑ I(ρ). This way, we see that diagrams en- as we might expect the ‘real’ bottom effect to be ⊥ . That joy plesant order-theoretic properties. Before studying such is actually the case. Σ properties, however, it is useful to relate the elements hxi ii ∈n In fact, since T is -continuous, T (f ) is strict, for any f : n ι ,in a diagram Γ x presenting µ with the support of µ. X → Y . Therefore, for any n, the map T (0 ֒−→ n) is strict i i meaning that ⊥n = T (ι)(⊥). That means that we essentially On Support Given an element µ ∈ T (X ) presented as a n have a unique bottom effect, viz. ⊥ , and we can regard any diagram Γ x , we see that the set {x | i ∈ n} is a i i i ⊥n as its extension to larger supports. In fact, we have the superset of supp(µ). For instance, the Dirac distribution on equality n ( ) ⊥ ξ = ⊥ x (i.e. η x ) is presented as H x (i.e. as the formalsum n i i 1; x). However, we can also represent it as the formal sum as well as the inequality 1; x + 0;y. ι ⊥ ⊑ ξ More generally, given Γ ∈ T (n) and n ֒−→ m, we also have 6 n ֒ T (ι) m = This property ensures the possibility to embed the Boolean algebra the injection T ( ) −−−−→ T ( ). For instance, taking T D ⊥ = ( ) ( )  n (which is isomorphic to) 2 to 1. Notice also that if η 1 , then T 1 1. and hpi ii ∈n ∈ D( ), the subdistribution D(ι)hpi ii ∈n maps i It is a straightforward exercise to verify that all monads mentioned so far to pi if i ∈ n, and to 0 otherwise (i.e. if i ∈ m \ n). satisfy this condition. 8 ADiagrammaticCalculusforAlgebraicEffects ADiagrammaticCalculus for Algebraic Effects,

For this reason we write ⊥ in place of ⊥n . Corollary 5.16. The following monotonicity law hold: We may also ask whether the ‘dual’ equality holds, i.e. if ∀i ∈ n. ξi ⊑ ρi n n n Γ ⊥ = ⊥ Γ ξ ⊑ Γ ρ i i i i i This is not the case. In fact, there are algebraic operations Proof. Define f ,g : n → T (X ) by f (i) , ξi and g(i) , such as print− that are not strict. However, if effects are com- n ρ . Clearly, we have f ⊑ g. Moreover, since Γ ξ = mutative, then such an equality holds. j i i n n n Γ f (i) and Γ ρ ⊑ Γ g(i) we conclude Definition 5.13. We say that a monad is commutative if i i i i  n m m n the thesis by Proposition 5.15. Γ ∆ x = ∆ Γ x i j i,j j i i,j Notice that m is independent of i, and n of j. 6 Conclusion We have introduced a diagrammatic notation and calculus Please observe that Definition 5.13 is rather different from for countable monads with algebraic operations. We have standard definitions of commutative monads one meets in shown by means of examples and general results some of the literature. Indeed, one of the advantages of our notation advantages of our notation. Additionally, the authors are is to allow for a simple, operational definition of commuta- currently using this notation to prove new, nontrivial the- tivity of monads. Accordingly, we easily notice that the com- orems on calculi with algebraic effects whose proofs turned mutativity equation in Definition 5.13 is just the semantical out to be extremely heavy using the standard, linear nota- counterpart of the following program equivalence: tion. We leave as a future work the investigation of further let x = t in (let y = s in r)≡ let y = s in (let x = t in r) applications of such a notation. where x ∈ FV (s) and y < FV (t). References Proposition 5.14. If T is commutative, then [1] S. Abramsky and A. Jung. 1994. Domain Theory. In Handbook of Logic n Γ ⊥ = ⊥ in Computer Science. Clarendon Press, 1–168. i [2] H.P. Barendregt. 1984. The lambda calculus: its syntax and semantics. Proof. Calculate North-Holland. n n n [3] U. Dal Lago, F. Gavazzo, and P.B. Levy. 2017. Effectful applicative Γ ⊥ = Γ ⊥ i i i n i bisimilarity: Monads, relators, and Howe’s method. In Proc. of LICS n n 2017. 1–12. = ⊥ Γ i n i i [4] Ugo Dal Lago and Margherita Zorzi. 2012. Probabilistic operational = semantics for the lambda calculus. RAIRO - Theor. Inf. and Applic. 46, ⊥ 3 (2012), 413–450.  [5] U. De Liguoro and A. Piperno. 1995. Non Deterministic Extensions of Untyped Lambda-Calculus. Inf. Comput. 122, 2 (1995), 149–177. Having clarified the role of the bottom effect, let us now [6] Joseph A. Goguen, James W. Thatcher, Eric G. Wagner, and Jesse B. move to monotonicity laws. Wright. 1977. Initial Algebra Semantics and Continuous . J. ACM 24, 1 (1977), 68–95. Proposition 5.15. The following monotonicity laws hold: [7] Andrew Donald Gordon. 1992. Functional programming and in- n m put/output. Ph.D. Dissertation. University of Cambridge, UK. Γ x ⊑ ∆ y f : X → T (Y ) i i j j hp://ethos.bl.uk/OrderDetails.do?uin=uk.bl.ethos.259479 n m [8] Martin Hyland, Gordon D. Plotkin, and John Power. 2006. Combining Γ f (x )⊑ ∆ f (y ) i i j j effects: Sum and tensor. Theor. Comput. Sci. 357, 1-3 (2006), 70–99. [9] Martin Hyland and John Power. 2007. The Category Theoretic Under- f ,g : X → T (Y ) f ⊑ g standing of Universal Algebra: Lawvere Theories and Monads. Electr. n n Notes Theor. Comput. Sci. 172 (2007), 437–458. Γ f (xi )⊑ Γ g(xi ) i i [10] B. Jacobs. 2016. Introduction to Coalgebra: Towards Mathematics of Proof. We prove the first rule (the second is similar). Let States and Observation. Cambridge Tracts in Theoretical Computer n m ξ , Γ f (x ), ρ , ∆ f (y ), γ the algebraic Science, Vol. 59. Cambridge University Press. i i j j [11] Patricia Johann, Alex Simpson, and Janis Voigtländer. 2010. A Generic operation corresponding to Γ, and δ the one corresponding Operational Metatheory for Algebraic Effects. In Proc. of LICS 2010. to ∆. Notice that by hypothesis we know γ (... , η(xi ), ...)⊑ IEEE Computer Society, 209–218. † [12] Gregory M. Kelly. 2005. Basic Concepts of Enriched Category Theory. δ(... , η(xi ), ...), and recall that f is monotone. We have I(ξ )⊑I(ρ). For: Reprints in Theory and Applications of Categories 10 (2005), 1–136. [13] Ugo Dal Lago and Francesco Gavazzo. 2019. Effectful Normal Form † I(ξ ) = f (γ (... , η(xi ), ...)) Bisimulation. In Proc. of ESOP 2019. To appear. [14] S.B. Lassen. 1998. Relational Reasoning about Functions and Nondeter- † ⊑ f (δ(... , η(xi ), ...)) minism. Ph.D. Dissertation. Dept. of Computer Science, University of = I(ρ). Aarhus. [15] William F. Lawvere. 2004. Functorial Semantics of Algebraic Theories.  Ph.D. Dissertation. 9 ADiagrammaticCalculusforAlgebraicEffects, UgoDalLagoand Francesco Gavazzo

[16] F. E. J. Linton. 1966. Some Aspects of Equational Categories. In Pro- ceedings of the Conference on Categorical Algebra, S. Eilenberg, D. K. Harrison, S. MacLane, and H. Röhrl (Eds.). Springer Berlin Heidelberg, 84–94. [17] Fosco Loregian. 2015. Coend calculus. arXiv:math.CT/1501.02503 [18] S. MacLane. 1971. Categories for the Working Mathematician. Springer- Verlag. [19] Ernest G. Manes. 2002. Taut Monads and T0-spaces. Theor. Comput. Sci. 275, 1-2 (2002), 79–109. [20] Eugenio Moggi. 1989. Computational Lambda-Calculus and Monads. In Proc. of LICS 1989. IEEE Computer Society, 14–23. [21] Eugenio Moggi. 1991. Notions of Computation and Monads. Inf. Com- put. 93, 1 (1991), 55–92. [22] C.-H. Luke Ong. 1993. Non-Determinism in a Functional Setting. In Proc. of LICS 1993. IEEE Computer Society, 275–286. [23] Gordon D. Plotkin and John Power. 2001. Adequacy for Algebraic Effects. In Proc. of FOSSACS 2001. 1–24. [24] Gordon D. Plotkin and John Power. 2001. Semantics for Algebraic Operations. Electr. Notes Theor. Comput. Sci. 45 (2001), 332–345. [25] Gordon D. Plotkin and John Power. 2003. Algebraic Operations and Generic Effects. Applied Categorical Structures 11, 1 (2003), 69–94. [26] John Power. 2006. Countable Lawvere Theories and Computational Effects. Electr. Notes Theor. Comput. Sci. 161 (2006), 59–71. [27] Matija Pretnar. 2015. An Introduction to Algebraic Effects and Han- dlers. Invited tutorial paper. Electr. Notes Theor. Comput. Sci. 319 (2015), 19–35. [28] A. Simpson and N. Voorneveld. 2018. Behavioural equivalence via modalities for algebraic effects. In Proc. of ESOP 2018. 300–326. [29] Jerzy Tiuryn and Mitchell Wand. 1996. Untyped lambda-calculus with input-output. In Trees in Algebra and Programming — CAAP ’96, Hélène Kirchner (Ed.). Springer Berlin Heidelberg, Berlin, Heidelberg, 317–329. [30] Philip Wadler. 1995. Monads for Functional Programming. In Ad- vanced Functional Programming, First International Spring School on Advanced Functional Programming Techniques, Båstad, Sweden, May 24-30, 1995, Tutorial Text. 24–52.

10