A Diagrammatic Calculus for Algebraic Effects
aa r X i v : . [ c s . P L ] J a n A Diagrammatic Calculus for Algebraic Effects
Preliminary Report
Ugo Dal Lago
University of Bologna and INRIA Sophia Antipolis [email protected]
Francesco Gavazzo
University of Bologna and INRIA Sophia Antipolis [email protected]
Abstract
We introduce a new diagrammatic notation for representingthe result of (algebraic) effectful computations. Our notationexplicitly separates the effects produced during a computa-tion from the possible values returned, this way simplifyingthe extension of definitions and results on pure computa-tions to an effectful setting. Additionally, we show a num-ber of algebraic and order-theoretic laws on diagrams, thisway laying the foundations for a diagrammatic calculus ofalgebraic effects. We give a formal foundation for such a cal-culus in terms of Lawvere theories and generic effects.
Keywords algebraic effect, generic effect, diagrammatic no-tation, graphical calculus, monad, Lawvere theory
In this note we are concerned with the problem of findinga convenient presentation of effectful (sequential) computa-tions when effects are produced by algebraic operations [25 ? ]. Concretely, what we have in mind are computations gen-erated by (sorts of) λ -calculi enriched with algebraic oper-ations in the spirit of Plotkin and Power [23]. Say we havefixed such a λ -calculus: how do we represent the result J t K of the evaluation of a term t ? Two standard answers to thisquestion are the following:1. J t K is a possibly infinite tree whose nodes are labeledwith algebraic operations, and whose leaves are eithervalues or the bottom symbol ⊥ denoting pure diver-gence. These structures are known as computationtrees [6] and are used to give operational semanticsto calculi with algebraic effects in, e.g., [11, 23, 28].2. J t K is an element in T (V) , where T is a suitable monadand V is the set of values. This is the approach fol-lowed in, e.g., [3, 13].Both these approaches have their drawback. The first oneis essentially syntactical, and does not give any semanticalinformation on the effects produced. The second, on the con-trary, is purely semantical and does not allow one to sepa-rate terms/values from effects. This is rather unsatisfactoryas effects are performed by operations, and thus we wouldexpect J t K to be made of an ‘effect part’ and a ‘value part’,rather than being a monolithic object. A Diagrammatic Calculus for Algebraic Effects,
In this note we show that we can rely on the correspon-dence between algebraic effects and generic operations [25]to express J t K as a pair ( Γ , ® v ) , where Γ is a (generic) effect and ® v is a list of values. In order to facilitate calculations withgeneric effects we present the pair ( Γ , ® v ) diagrammatically,this way obtaining a lightweight diagrammatic calculus foreffectful sequential computations. Advertisement
This note is a work in progress. The au-thors plan to systematically review and update it—especiallywith examples and informal explanations—in the next weeks.The authors have also noticed that different browsers havedifferent rendering of diagrams, whereas ‘desktop’ pdf view-ers tend to have a better rendering.
In this section, we give some background notions on mon-ads and algebraic operations. The current version of thiswork still lacks a proper introduction to monads and alge-braic effects, and thus assume the reader to be familiar withbasic category theory [18] and domain theory [1].We work with (strong) monads on Set , the category ofset and functions, which we present as
Kleisli triples [18].The latter are triples ( T , η , − † ) , where f † : T X → TY isthe Kleisli extension of f : X → TY and η : X → T X isthe unit of T , satisfying the following laws, for f , д havingappropriate (co)domains. η † X = id T ( X ) ; f † ◦ η X = f ; д † ◦ f † = ( д † ◦ f ) † .Since we work in Set , we oftentimes use the bind notation >> = for monadic sequencing. That is, given µ ∈ T ( X ) and f : X → T ( Y ) , we write µ >> = f in place of f † ( µ ) .We assume η to be an injection, meaning that T is non-trivial [19]. This is the case for all monads on Set , except forthe monad with T ( X ) = X , and the one with T (∅) = ∅ and T ( X ) =
1, for X , ∅ . We denote by Kl ( T ) theKleisli category of T . Example 2.1.
The target monads we have in mind are thosemodeling notions of computations [20, 21, 30]. Among thoseare:1. The maybe monad M ( X ) , X + {↑} modeling diver-gence. Recall that any monad on
Set is strong.1
Diagrammatic Calculus for Algebraic Effects, Ugo Dal Lago and Francesco Gavazzo
2. The exception monad E( X ) , X + E modeling compu-tations raising exceptions in a given set E . from a given3. The powerset monad P modeling pure nondeterminism.4. The (discrete) subdistribution monad D modeling prob-abilistic nondeterminism.5. The global state monad G( X ) , M ( X × S ) S , modelingimperative computations over a store S (e.g. given a set L of locations, take S , {
0, 1 } L , meaning that a storeassigns to each location a boolean value).6. The output monad O( X ) , C ∞ × M ( X ) , where C ∞ is theset of finite and infinite strings over a fixed alphabet C .7. Combinations thereof, such as monads of the form T ( M ( X )) or T ( X × S ) S (see [8]).Monads alone can structure effects and (sequentially) com-pose them, but do not have the structure to actually producethem (with the irrelevant exception of the trivial effect). Forthis reason we consider monads coming with effect-triggeringoperations. Following Plotkin and Power [23, 24], we re-quire such operations to be algebraic. Definition 2.2. An n -ary (set-indexed family of) operation σ X : T X n → T X is algebraic , if for all sets X , Y , f : X → T ( Y ) , and µ , . . . , µ n ∈ T ( X ) , we have: ( σ X ( µ , . . . , µ n )) >> = f = σ Y ( µ >> = f , . . . , µ n >> = f ) . Example 2.3.
Referring to the monads in Example 2.1, thefollowing operations are all algebraic.1. The maybe monad comes with no operation.2. The exception monad comes with a set of zero-ary op-erations raise e , for e ∈ E , that simply corresponds to inr ( e ) .3. The powerset monad P comes with set-theoretic union ∪ modeling binary nondeterministic choice.4. The subdistribution monad D comes with binary fairnondeterministic choice ⊕ (recall that ( µ ⊕ ν )( x ) = µ ( x ) + ν ( x ) ).5. The global state monad G( X ) , M ( X × S ) S , comes with aset of binary operations read ℓ for reading locations, anda set of unary operations write ℓ for writing locations(see [24, 27]).6. The output monad comes with a set of unary operations print c indexed over elements of C for printing. That is, print c . ( w , x ) = ( cw , x ) .As highlighted by Example2.3 several computational ef-fects can be modeled using monads and algebraic opera-tions. Notable exceptions are continuations and exceptionhandling. In this paper we will consider computational ef-fects modeled as a monad T together with a set Σ of alge-braic operations on T . In those cases, we say that T is Σ -algebraic. λ -Calculus with Algebraic Effects At this point of the work we have to make some designchoices, as well as introduce some (minor) restrictions onthe collection of monads and effects studied. In order to mo-tivate such choices and restrictions, it is convenient to havea concrete computational calculus with algebraic effects. Wetake the calculus of [3].
Definition 3.1.
Let Σ be a set of operations on a given monad T . The calculus Λ Σ has terms and values defined by the fol-lowing grammar, where x ranges over a countable set ofvariables, and σ ∈ Σ . t , s :: = x | λx . t | ts | σ ( t , . . . , t ) v , w :: = x | λx . t Notice that Λ Σ is parametric with respect to a set Σ of op-erations and to a Σ -algebraic monad T . Although the monad T plays no role in Definition 3.1 and calculi are usually de-fined relying on uninterpreted operation symbols (whichare then interpreted as algebraic operations once giving se-mantics to the calculus), for the sake of the economy of thework we chose the ‘semantic-oriented’ presentation of Def-inition 3.1. Example 3.2.
1. Taking the maybe monad (and no opera-tion) we obtain the pure λ -calculus.2. Taking the exception monad with the raising exceptionoperation(s) of Example 2.3 we obtain a λ -calculus withexceptions.3. Taking the powerset with set-theoretic union (as in Ex-ample 2.3), we obtain the nondeterminism λ -calculus [5,14, 22].4. Taking the subdistribution monad D and fair probabilis-tic choice (as in Example 2.3) we obtain the probabilistic λ -calculus of [4].5. Taking the global state monad and the operation for read-ing and writing stores of Example 2.3 we obtain the im-perative λ -calculus.6. Taking the output monad and the printing operation(s)of Example 2.3 we obtain the λ -calculus with output [7,29].We follow standard notational conventions, as in [2]. Inparticular, we denote by Λ and V the collection of closed terms (programs) and values. Additionally, we write let x = t in s for ( λx . s ) t .Next, we want to give, say call-by-value, semantics to Λ Σ .To do so, we follow [3] and give a monadic semantics tothe calculus. That is, to any closed term t is associated anelement J t K in T (V) (we call such elements monadic values).Intuitively, the map J − K : Λ → T (V) should be defined asfollows. Diagrammatic Calculus for Algebraic Effects A Diagrammatic Calculus for Algebraic Effects, J v K , η ( v ) J ts K , J t K >> = ( v
7→ ( J s K >> = ( w J vw K ))) J σ ( t , . . . , t n ) K , σ ( J t K , . . . , J t n K ) Obviously, a map J − K defined in such a way may not ex-ist. In order to ensure the existence of J − K [3] required T to come with a suitable domain structure (notably, ω -cppoenrichment [12]). Definition 3.3.
Let T be a Σ -algebraic monad. We say that T is Σ - continuous if for any set X , T X carries an ω -cppostructure such that both >> = and operations in Σ are continu-ous in all arguments. Proposition 3.4 ([3]) . If T is Σ -continuous, J − K : Λ → T (V) is the least map satisfying the following laws: J v K , η ( v ) J ts K , J t K >> = ( v
7→ ( J s K >> = ( w J vw K ))) J σ ( t , . . . , t n ) K , σ ( J t K , . . . , J t n K ) Example 3.5.
All monads in Example 2.1 are Σ -continuous(wrt the set of operations of Examples 2.3). The ω -cppo struc-ture is standard (e.g. the one induced by subset inclusion for P , or by the pointwise order for D ), with the exception ofthe one for the output monad, which is induced by the fol-lowing order: ( u , µ ) ⊑ ( w , ν ) ⇐⇒ ( µ = inr (↑) ∧ u ⊑ w )∨ ( µ = inl ( x ) = ν ∧ u = w ) . In previous section we defined the (result of the) evaluationof a program t as a monadic value J t K ∈ T (V) . Workingwith monadic values has the major advantage of providingsemantical information on the effects performed. However,it also has a major drawback: J t K being a monolithic object,it does not allow a clear distinction between the effects pro-duced by t and the possible results (values) obtained.Intuitively, since during the evaluation of t effects canonly be produced by (algebraic) operations, we would ex-pect J t K to be a pair of the form ( Γ , h v i i i ∈ I i) , where Γ is amathematical object describing the effects produced by t ,and h v i i i ∈ I is the list of possible values returned.Notice that since the calculus may be nondeterministic, I has cardinality bigger than one in general, and that dueto recursion I may be infinite (but countable). To see that,consider the nondeterministic program t , where n standsfor the (Church) numeral of n , succ is the successor function,and t is recursively defined by the equation t = λx . ( x ∪ t ( succ x )) .The purpose of this section is to prove that the aforemen-tioned composition is indeed always possible. First, we have to show that any monadic value has indeeda ‘value component’. Mimicking the terminology employedfor subdistributions, we refer to such a component as sup-port . As the set of terms is countable, we expect any monadicvalue to have countable support. In general, not all monadscome with a notion of support, and even those that havesuch a notion may have objects with uncountable support(e.g. the continuation monad [9]). As a consequence, we needto isolate the class of monads whose elements have count-able support.First, we need monad to preserves injections (the reasonwhy we need such monads will become clear soon). That is,if A ι ֒ −→ X is the subset inclusion map, then T ( A ) T ι ֒ −−→ T ( X ) is an injection (i.e. a mono), which we regard as monadicinclusion. Notice that if T preserves weak pullbacks, then italso preserves monos. This condition is met by all the mon-ads in Example 2.1 (see, e.g., [10]).Given a monadic object µ ∈ T ( X ) , the support of µ is the smallest set A ι ֒ −→ X such that µ ∈ T ( A ) . We denote such aset by supp ( µ ) . Of course, in general the support of µ neednot exist and thus we restrict our analysis to monads comingwith a notion of countable support. Definition 4.1.
We say that a monad is countable if for anyset X and any element µ ∈ T ( X ) , there exists a smallestcountable set Y ι ֒ −→ X , denoted by supp ( µ ) , such that µ ∈ T ( Y ) (i.e. there exists ν ∈ T ( Y ) such that µ = ( T ι )( ν ) ). Example 4.2.
All the monads in Example 2.1 are countable,with the exception of the powerset monad. Nonetheless, wecan regard P as countable (by taking its countable restric-tion), the collection of λ -terms being countable itself. Remark 1.
The notion of support is sometimes formalizedthroughout the notion of an accessible functor (monad, inour case). Accordingly, a monad T is κ -accessible, for a car-dinal κ , if for any element µ ∈ T ( X ) there exists A ⊆ X with cardinality strictly smaller than κ such that µ ∈ T ( A ) .If κ = ℵ , then T is said to be finitary as, intuitively, anyelement in T ( X ) has a finite support. Moreover, if T pre-serves weak pullbacks, then it also preserves finite intersec-tion, and one can define for µ ∈ T X , the support of µ as: supp ( µ ) , Ù { A ⊆ X | µ ∈ T A } .Since we deal with elements with countable support, weneed to shift from finitary monads to ℵ -accessible monad.As a consequence, preservation of weak pullbacks is notenough to guarantee the existence of Ñ { A ⊆ X | µ ∈ T A } ,as we now need T to preserve countable intersections. Working with countable monads we can think of the sup-port of a monadic object as its ‘value component’. What Diagrammatic Calculus for Algebraic Effects, Ugo Dal Lago and Francesco Gavazzo about its ‘effect component’? Here we show that such a com-ponent can be formally described using the notion of a genericeffects [25]. Achieving such a goal, however, requires to in-troduce some mathematical abstractions. As a consequence,we we first study a concrete example which will then gen-eralise to arbitrary (countable) monads.
Example 4.3 (Probabilistic computations) . When workingwith (discrete) subdistributions, hence with the monad D ,it is oftentimes convenient to represent subdistributions assyntactic objects called formal sums . A formal sum (over aset X ) is an expression of the form Í i ∈ I p i ; x i , where I is acountable set, p i ∈ [
0, 1 ] and x i ∈ X , and Í i p i ≤ Í i ∈ I p i ; x i is meant to recall the semanticcounterpart of formal sums, namely subdistributions. How-ever, we should keep in mind that formal sums are purelysyntactical expressions. For instance, abusing a bit the nota-tion, ; x + ; x and 1; x are two distinct formal sums, al-though they both denote the Dirac distribution on x . Moregenerally, there is an interpretation function I mapping eachformal sum Í i ∈ I p i ; x i to a subdistribution µ on X definedas µ ( x ) , Í x i = x p i . Additionally, the map I is a surjection ,meaning that any subdistribution can be represented as a(non-unique) formal sum.Examining a bit more carefully a formal sum Í i ∈ I p i ; x i ,we see that the latter consists of an I -indexed sequence h p i i i ∈ I of elements in [
0, 1 ] and an I -indexed sequence h x i i i ∈ I ofelements in X . Therefore, a formal sum is just a pair of se-quences (h p i i i ∈ I , h x i i i ∈ I ) ∈ [
0, 1 ] I × X I such that Í i p i ≤ h p i i i ∈ I is ac-tually a subdistribution on I (the one mapping i to p i ). There-fore, we see that formal sums are just elements in D( I ) × X I .Putting all these observations together, we see that forany µ ∈ D( X ) , there exists a countable set I and an element F ∈ D( I ) × X I such that I( F ) = µ . As a consequence, stip-ulating two formal sums F , F ∈ D( I ) × X I to be equal (notation F = I F ) if I( F ) = I( F ) , then we see that D( X ) is isomorphic to the quotient set ( Ð I D( I ) × X I )/ = I , where I ranges over countable sets.Summing up, Example 4.3 shows that any subdistribution µ ∈ D(V) can be decomposed as a pair (h p i i i ∈ I , h v i i i ∈ I ) ,for some (countable) set I , where h p i i i ∈ I is the ‘effect’ partof µ , and h v i i i ∈ I is the value part of µ . Even if the such adecomposition is not unique, we can give definitions andprove results on such decompositions and extend them tosubdistributions by showing invariance with respect to = I ,which is usually trivial.Can we generalize Example 4.3 to arbitrary (countable)monads?First, let us observe that since the set I in Example 4.3is countable, we can replace it with an enumeration of its Notice that = I is the kernel of I . elements. That is, we replace I with sets n , where n ∈ N ∞ , N ∪ { ω } and n , { . . . , n } if n , ω , and n , N , if n = ω .Formally, we should work ℵ , the skeleton of the categoryof countable sets and all functions between them, instead of N ∞ (notice that ℵ and N ∞ have the same objects, but theformer has ‘more arrows’, so to speak). Nonetheless, think-ing of N ∞ in place of ℵ is perfectly fine for building intu-itions. Abusing the notation, we write n ∈ ℵ to state that n is an object of ℵ . Theorem 4.4 ([9, 16, 26]) . For any set X we have the isomor-phism T X (cid:27) ∫ n ∈ℵ T ( n ) × X n .Let us decode Theorem 4.4. First, ∫ n ∈ℵ T ( n ) × X n is a co-end [17], an abstract notion that is not needed to achieve ourgoals. For us, ∫ n ∈ℵ T ( n )× X n is the quotient of Ý n ∈ℵ T ( n )× X n for a suitable equivalence relation.More precisely, we can translate Theorem 4.4 as follows: Theorem 4.5.
For any set X , all elements in T ( X ) can be(non-uniquely) presented as elements in Ø n ∈ N ∞ T ( n ) × X n Moreover, there is a surjective map I such that for any µ ∈ T ( X ) there exists n ∈ N ∞ such that µ can be uniquely repre-sented as an equivalence class modulo = I , the kernel of I , ofan element in T ( n ) × X n . Replacing Ð n ∈ N ∞ T ( n ) × X n with the more correct (yetmorally equivalent) Ý n ∈ℵ T ( n ) × X n , we achieve the corre-spondence T ( X ) (cid:27) ∫ n ∈ℵ T ( n ) × X n = ( Þ n ∈ℵ T ( n ) × X n )/ = I Let us see how to prove Theorem 4.5. In the following,we will oftentimes regard a sequence in X n as a function in n → X . Proof of Theorem 4.5.
First, let us define the map I . Given Γ ∈ T ( n ) and s : n → X (where n ∈ ℵ ), define I( Γ , s ) , T ( s )( Γ ) . Next we prove Surjectivity of I . Let µ ∈ T ( X ) . Since T is countable, supp ( µ ) is countable, and thus isomorphic to n , for some object n of ℵ . any µ ∈ T ( X ) has a countablesupport. Let f : supp ( µ ) → n be such a bijection. Then wepresent µ as ( T ( f )( µ ) , f − ) . Since I( T ( f )( µ ) , f − ) = T ( f − )( T ( f )( µ )) = µ we are done. (cid:3) We call elements in Ý n ∈ℵ T ( n ) × X n formal presentations .Before giving examples of formal presentations we intro-duce a diagrammatic notation for them. Counting from one rather than from zero simplifies the notation. We havealso implicitly used this convention writing σ ( t , . . . , t n ) for n -ary alge-braic operations.4 Diagrammatic Calculus for Algebraic Effects A Diagrammatic Calculus for Algebraic Effects,
Representing monadic elements as formal presentations hasthe major drawback of introducing bureaucracy in the treat-ment of indexes. Consider, for instance, a program of theform let x = t in s . We try to define J let x = t in s K using for-mal presentation. First, we evaluate t obtaining ( Γ , h v i i i ∈ n ) ∈ T ( n ) × V n , for some n ∈ ℵ . For any i ∈ n , we then evaluate s [ x : = v i ] , obtaining ( ∆ i , h w j i i j ∈ m i ) , for some m i ∈ ℵ . As aconsequence, evaluating let x = t in s should give a genericeffect Ξ ∈ T ( l ) , for l , Í i m i , obtained by some kind ofcomposition of Γ with the ∆ i s together with the sequence h y j i i j i ∈ l i . The resulting expression is rather heavy to write.For this reason we introduce a diagrammatic notation forformal presentations. Definition 5.1.
We represent an object ( Γ , h x i i i ∈ n ) ∈ T ( n )× X n as a diagram of the form Γ n i x i Here i ranges over elements in n and to each i it is associ-ated the corresponding x i . That is, the horizontal bar withsubscript i and target x i stands for the function i x i .If n is finite, we can modify Definition 5.1 by extension-ally listing all elements x , . . . , x n . For instance, ::: Γ x x n is the extensional version of Γ n i x i , for n ∈ N . Example 5.2.
1. Consider the maybe monad M . We presentan object µ ∈ M ( X ) as a pair in M ( n ) × X n , for some n ∈ N ∞ . Since M ( n ) × X n = ( n + {↑}) × X n (cid:27) ( n × Λ n ) + ( Λ n × {↑}) , µ is (presented as) either a pair ( k , h x i i i ∈ n ) or a pair (↑ , h x i i i ∈ n ) . The former corresponds to the case of conver-gence to x k , whereas the latter to divergence. In particu-lar, if µ is the result of evaluating a λ -term, then we willactually have n = n = • If n =
1, we obtain pairs of the form ( h x i) ), whichwe write as ↓ x . • If n =
0, then we can only have the pair (⊥ , hi) , where hi is the empty sequence. We write such a pair as ↑ .2. Consider the output monad O . We present an object µ ∈O( X ) as a pair in O( n ) × X n = C ∞ × ( n + {↑}) × X n ,for some n ∈ N ∞ . Therefore, µ is presented as eithera triple ( w , ↑ , h x i i i ∈ n ) , or as a triple ( w , k , h x i i i ∈ n ) . Theformer case means that we have divergence, and that thestring w is outputted, whereas the latter case means thatwe converge to x k , and that the string w is outputted. As before, if µ is the result of evaluating a term, we will haveeither n = n = n =
1, we have triples of the form ( w , 1, x ) ) whichwe write as written as w x .2. If n =
0, the we can only have triples of the form ( w , ⊥ , hi) , which we write as ( w , ↑) .3. The case for the subdistribution monad goes exactly asin Example 4.3. We can represent a formal sum (h p i i i ∈ n , h x i i i ∈ n ) as h x i i i ∈ n n i x i .4. Replacing ([
0, 1 ] , + ) with ({
0, 1 } , ∨) in Example 4.3 weobtain formal presentations for elements in P( X ) , whichwe may write as diagrams h b i i i ∈ n n i x i . One of the strengths of monads (at least concerning theirusage in the semantics of programming languages) is thatthey naturally support the sequential composition of effects.Do diagrams do the same?Let us consider again J let x = t in s K . Suppose:1. J t K = Γ n i v i ;2. For any i ∈ n , J s [ x : = v i ] K = ∆ i m i j w j .Can we find a diagram for J let x = t in s K ? Since let x = t in s is nothing but a sequential composition, a natural pro-posal is to write Γ n i ∆ i m i j w j for J let x = t in s K . But: is this figure meaningful? That is,do we have a notion of composition for diagrams? We aregoing to answer such questions in the affirmative. There are at least two ways to define the sequential compo-sition of two diagrams/formal presentations. The first onerelies on the correspondence between generic effects andalgebraic operations [25].
Proposition 5.3.
Any generic effect Γ ∈ T ( n ) correspondsto a n -ary algebraic operation γ , and any n -ary algebraic op-eration γ corresponds to a generic effect Γ ∈ T ( n ) . Moreover, if ( Γ , h x i i i ∈ n ) is a formal presentation of µ , then µ = γ ( . . . , η ( x i ) , . . . ) .Proof. The bijection between algebraic operations and genericeffects is given in [25]. Given Γ ∈ T ( n ) we define γ X : T ( X ) n → T ( X ) as γ (® x ) , ( η X ◦ ® x ) † ( Γ ) , where ® x : n → X . Vice versa,given γ X : T ( X ) n → T ( X ) we define Γ , γ n ( η n ) . Supposenow ( Γ , h x i i i ∈ n ) is a formal presentation of µ . We write ® x for the sequence h x i i i ∈ n regarded as a function n → X . We Diagrammatic Calculus for Algebraic Effects, Ugo Dal Lago and Francesco Gavazzo have: µ = I( Γ , h x i i i ∈ n ) = ( η X ◦ ® x ) † ( Γ ) = ( η X ◦ ® x ) † ( γ n ( η n )) = γ n (( η X ◦ ® x ) † ◦ η n ) = γ n ( η X ◦ ® x ) where the penultimate equality follows by the defining iden-tity law of algebraic operations (see Definition 2.2). (cid:3) Example 5.4.
1. The generic effect corresponding to op-eration(s) print c : O( X ) → O( X ) is ( c , 1 )
2. The generic effect corresponding to fair probabilistic choice ⊕ is the distribution h i mapping i ∈ {
1, 2 } to 0.5. Corollary 5.5.
There exists a notion of sequential composi-tion for generic effects: given a generic effect Γ ∈ T ( n ) and a n -indexed family of generic effects ∆ i ∈ T ( m i ) , there existstheir sequential composition which is a generic effect in T ( l ) ,for l , Í i ∈ n m i .Proof. Let us consider Γ ∈ T ( n ) and ∆ i ∈ T ( m i ) , for any i ∈ n . The former gives the algebraic operation γ : T ( X ) n → T ( X ) . Similarly, the latter gives a family of algebraic opera-tions δ i : ( T X ) m i → T ( X ) . Taking products, we obtain T ( X ) Í i m i (cid:27) / / Î i ∈ n T ( X ) m i h δ i i i ∈ n / / T ( X ) n γ / / T ( X ) We then take advantage of Proposition 5.3 and define thecomposition of Γ and ∆ i s as the generic effect associated tothe algebraic operation γ ◦ h δ i i i ∈ n . (cid:3) The drawback of Corollary 5.5 is that we would like to de-fine the composition of generic effects without going throughtheir associated algebraic operations. To achieve such a goal,we look at the countable Lawvere theory [9, 15] induced bygeneric effects.First, observe that ℵ has countable coproducts and, up-toequivalence, it is the free category with countable coprod-ucts on , where coproducts are given by standard sums(thus, for instance, for n ∈ N we have n = + · · · + | {z } n ). Byduality, ℵ op has countable products. Definition 5.6 ([26]) . A countable Lawvere theory is a smallcategory L with countable products and a strict countable-product preserving identity-on-objects functor I : ℵ op →L .In particular, objects of L are exactly those of ℵ , and ev-ery function between such objects gives a map in L . A mapin L( n , 1 ) represents an operation of arity n (meaning thatwe also consider operations with arity ω ), whereas a mapin L( n , m ) represent m operations of arity n . In particular,given operations σ ∈ L( n , m ) and τ ∈ L( m , l ) , their compo-sition τ ◦ σ ∈ L( n , l ) gives l operations of arity n . Proposition 5.7.
Generic effects form a Lawvere theory, andthus we can define the composition of generic effects as com-position on their Lawvere theory.Proof.
Let us begin showing that generic effects form a Law-vere theory. Let Kl ( T ) op ℵ be the opposite of Kl ( T ) restrictedto objects of ℵ . Since Kl ( T ) has countable coproducts, andthe canonical functor I : Set → Kl ( T ) preserves them, re-stricting I to ℵ we obtain a countable-coproduct preservingidentity-on-objects functor I : ℵ → Kl ( T ) ℵ . As a conse-quence, Kl ( T ) ℵ is the opposite of a Lawvere theory, andthus Kl ( T ) op ℵ is a (countable) Lawvere theory. Notice that ageneric effect Γ ∈ T ( n ) corresponds to a map in Kl ( T ) op ℵ ( n , 1 ) ,i.e. as an operation in the Lawvere theory Kl ( T ) op ℵ . We nowdefine composition of generic effects. Given generic a ef-fect Γ ∈ T ( n ) and an n -indexed family of generic effects ∆ i ∈ T ( m i ) (for any i ∈ n ), i.e. arrows Γ ∈ Kl ( T ) op ℵ ( n , 1 ) and ∆ i ∈ Kl ( T ) op ℵ ( p i , 1 ) , we can define its composition rely-ing on composition in Kl ( T ) op ℵ ( n , 1 ) as follows. Since ∆ i ∈ Kl ( T ) op ℵ ( m i , 1 ) and Kl ( T ) ℵ has products, we have the map h ∆ i i i ∈ n ∈ Kl ( T ) op ℵ ( Í i m i , n ) , and thus Γ ◦ lan ∆ i i i ∈ n ∈ Kl ( T ) op ℵ ( Í i m i , 1 ) is the desired generic effect. (cid:3) Coming back to diagrams, given Γ n i x i and ∆ i m i j y j ,for any i ∈ n , we write their composition as Γ n i ∆ i m i j y j Such a notation gives us several advantages, as we are goingto see. However, we first look at some concrete examples.
Example 5.8.
1. Consider the maybe monad and considerrepresentations that can be obtained as the result of com-putations. Then sequential composition is defined by thefollowing laws: ↑ ξ , ↑↓ ↓ ξ , ↓ ξ
2. Consider the output monad. As before, we consider rep-resentations that can be obtained as the result of compu-tations. Sequential composition is thus defined: ( w , ↑) ξ , ( w , ↑) w ( u , ↑) ξ , ( wu , ↑) w u ξ , wu ξ
3. Consider the subdistribution monad, and denote by p ® q scalar multiplication of p with ® q . We then define sequen-tial composition as: ® p n i ® q i m i j ξ j , ® p · h® q i i i ∈ n [ l ] k ξ k Diagrammatic Calculus for Algebraic Effects A Diagrammatic Calculus for Algebraic Effects, where l , Í i ∈ n m i and ® p · h® q i i i ∈ n , h p i ® q i i i ∈ n regardedas an l -indexed sequence. Writing diagrams in sequence for sequential compositionhas important implications. First of all, the very act of writ-ing diagrams in sequence for sequential composition pre-supposes a notion of equality for diagrams. In fact, what weare doing is de facto building an algebra of diagrams. With-out much of a surprise, we say that two diagrams ξ , ρ areequal (notation ξ = ρ ) if and only if ξ = I ρ is the map ofTheorem 4.5.It goes by itself that working with = I directly may bequite heavy. For this reason, we now prove a collection ofalgebraic results that dispense us from working with = I .Such results concern sequential composition, monadic bind-ing, and sequential composition. We immediately notice that composition is associative. Thisproperty is actually built-in the diagrammatic notation, asevident when writing the composition of three diagrams: Γ n i ∆ i m i j Ξ j l j k x k (1)Linguistically, this notation describes associativity of sequenc-ing, usually expressed by program equivalences of the fol-lowing form: let x = t in ( let y = s in r ) ≡ let y = ( let x = t in s ) in r .Diagram (1) also highlights another important feature ofdiagrams, namely the way they manage index dependencies.The ‘geography’ of (1) shows that i ∈ n , j ∈ m i , and k ∈ l j .Moreover, by reading from the right to the left we recover in-dex dependencies: we see that since k (being in l j ) dependson j which depends (being in m i ) on i , k depends on i aswell.Second, there is a trivial generic effect H ∈ T ( ) (read cap-ital η ) which behaves as a neutral element for composition.That is, for any x ∈ X , the diagram H x representthe computation trivially returning x performing no effect(i.e. η ( x ) ). We have the following laws: H ξ = ξ Γ n i H x i = Γ n i x i Linguistically, these laws correspond to the following pro-gram equivalences: let x = v in t ≡ t [ x : = v ] let x = t in x ≡ t We use letters ξ , ρ , ν , . . . for diagrams Example 5.9.
The object
H x is ↓ x in themaybe monad M , ε x in the output monad O , and1 x in the subdistribution (resp. powerset) monad D (resp. P ).Additionally, diagram equality is preserved by diagrams. Theorem 5.10.
Let µ ∈ T ( X ) be presented as Γ n i x i ,and f : X → T ( Y ) . Then µ >> = f is presented as Γ n i f ( x i ) Proof.
The proof relies on Proposition 5.3, Corollary 5.5, andthe defining identity of algebraic operations of Definition 2.2.Suppose µ is presented as Γ n i x i . By Proposition 5.3we have an algebraic operation γ : T ( X ) n → T ( X ) suchthat µ = γ ( . . . , η ( x i ) , . . . ) . Suppose now ∆ i m i j y j is apresentation of f ( x i ) , for any i ∈ n . In particular, by Propo-sition 5.3 we have algebraic operations δ i : T ( Y ) m i → T ( Y ) such that f ( x i ) = δ i ( . . . , η ( y j ) , . . . ) . Let ξ , Γ n i ∆ i m i j y j = Γ n i f ( x i ) By Proposition 5.3 and Corollary 5.5, ξ is associated to thealgebraic operation γ ◦ h δ i i i ∈ n : T ( Y ) Í i ∈ n m i → T ( Y ) , andwe have I( ξ ) = γ ( . . . , δ i ( . . . , η ( y j ) , . . . ) , . . . ) We conclude the thesis as follows: µ >> = f = γ ( . . . , η ( x i ) , . . . ) >> = f = γ ( . . . , η ( x i ) >> = f , . . . ) = γ ( . . . , f ( x i ) , . . . ) = γ ( . . . , δ i ( . . . , η ( y j ) , . . . ) , . . . ) = I( ξ ) where the second line follows by the defining identity ofalgebraic operations (see Definition 2.2). (cid:3) An important consequence of Theorem 5.10 is the validityof the following law which makes most of the definitionsgiven on diagrams invariant with respect to = I , and thusvalid definitions on monadic elements . Γ n i x i = ∆ m j y j f : X → T ( Y ) Γ n i f ( x i ) = ∆ m j f ( y j ) Example 5.11.
Let us consider monadic terms, i.e. elementsin T ( Λ ) . Recall that we have defined an evaluation map J − K : Λ → T (V) . Define the monadic extension of J − K T by: r Γ n i t i z T , Γ n i J t i K Notice that an analogous rule holds if we replace f : X → T ( Y ) with f : X → Y .7 Diagrammatic Calculus for Algebraic Effects, Ugo Dal Lago and Francesco Gavazzo
Then J − K T is automatically well-defined, as by Theorem 5.10 J − K T is invariant under diagram equality, and thus it is in-dependent of the choice of representatives. Notice also howthe diagrammatic notation makes explicit how definitionsand results ‘distribute’ over effects. This is the very essenceof algebraic effects, as stated in the defining identity of al-gebraic operations (Definition 2.2). The diagrammatic nota-tion extends this identity to any monadic expressions, as thelatter are ultimately built using generic effects, which bijec-tively correspond to algebraic operations. Thanks to Proposition 5.3, diagrams are also a natural wayto write algebraic operations. In general, if we present ob-jects µ i ∈ T ( X ) as ξ i , then we write ::: σ ξ ξ n or σ n i ξ i for the presentation of σ ( µ , . . . , µ n ) (that is, we use the no-tation σ both for the algebraic operation and the genericeffects associated to it).Notice that thanks to Theorem 5.10 both σ ( µ , . . . , µ n ) >> = f and σ ( µ >> = f , . . . , µ n >> = f ) are presented as σ n i f ( ξ i ) ,where ξ i is a presentation of µ i , this way encoding the defin-ing identity of algebraic operations (Definition 2.2) in dia-grams. Linguistically, this corresponds to the program trans-formation let x = σ ( t , . . . , t n ) in s ≡ σ ( . . . , let x = t i in s , . . . ) .Having seen the basic algebra of diagrams, we move to thestudy of their order-theoretic properties. So far we have focused on algebraic properties of diagrams.However, working with Σ -continuous monads, we can ex-tend the order ⊑ on diagrams by stipulating ξ ⊑ ρ if andonly if I( ξ ) ⊑ I( ρ ) . This way, we see that diagrams en-joy plesant order-theoretic properties. Before studying suchproperties, however, it is useful to relate the elements h x i i i ∈ n in a diagram Γ n i x i presenting µ with the support of µ . On Support
Given an element µ ∈ T ( X ) presented as adiagram Γ n i x i , we see that the set { x i | i ∈ n } is asuperset of supp ( µ ) . For instance, the Dirac distribution on x (i.e. η ( x ) ) is presented as H x (i.e. as the formal sum1; x ). However, we can also represent it as the formal sum1; x + y .More generally, given Γ ∈ T ( n ) and n ι ֒ −→ m , we also havethe injection T ( n ) T ( ι ) ֒ −−−−→ T ( m ) . For instance, taking T = D and h p i i i ∈ n ∈ D( n ) , the subdistribution D( ι )h p i i i ∈ n maps i to p i if i ∈ n , and to 0 otherwise (i.e. if i ∈ m \ n ). Formally, given a countable set X , any formal presenta-tion ( Γ , ® x ) ∈ T ( s ) × X n can be extended to a formal presen-tation ( ∆ , ® y ) in T ( m ) × X m , for any n ι ֒ −→ m . Lemma 5.12.
Let X be a countable set. Then any pair ( Γ , ® x ) ∈ T ( n ) × X n can be extended to a formal presentation ( ∆ , ® y ) in T ( m ) × X m , for any n ι ֒ −→ m Proof.
Since n ι ֒ −→ m , then T ( n ) T ( ι ) ֒ −−−−→ T ( m ) . Take ∆ , T ( ι )( Γ ) and ® y to be any map extending ® x according to thefollowing diagram (such maps indeed exists): m ® y / / X n ?(cid:31) ι O O ® x > > ⑥⑥⑥⑥⑥⑥⑥⑥ Indeed ( Γ , ® x ) = ( ∆ , ® y ) . (cid:3) For instance, we have seen that the trivial generic effect H ∈ T ( ) corresponds to the map η : → T ( ) . However,for any n , there is a map η n : n → T ( n ) such that, for any i ∈ n , η ( i ) = T ( ι )( H ) , for ι ֒ −→ n sending 1 ∈ to i . As aconsequence, we see that we can indeed think of H as the‘real’ trivial effect, and regard the others as its extension tolarger supports. For instance, we can regard 1; x as the pre-sentation of the Dirac distribution on x , and formal sumssuch as 1; x + y + z as extensions of 1; x . Order-theoretic Properties
We now analyze the order-theoreticproperties of diagrams. For technical reason, we need to re-quire T ( ) to have at least two elements , i.e. η ( ) , ⊥ . First,we observe that there is a bottom effect ⊥ ∈ T ( ) , which wewrite as ⊥ . It is easy to see that such element is ↑ in themaybe monad, ( ε , ↑) (where ε is the empty string) in theoutput monad, and 0 in the subdistribution monad.Actually, for any n there is a bottom effect ⊥ n ∈ T ( n ) suchthat ⊥ n n i x i ⊑ Γ n i y i . This is a bit unsatisfactory,as we might expect the ‘real’ bottom effect to be ⊥ . Thatis actually the case.In fact, since T is Σ -continuous, T ( f ) is strict, for any f : X → Y . Therefore, for any n , the map T ( ι ֒ −→ n ) is strict,meaning that ⊥ n = T ( ι )(⊥) . That means that we essentiallyhave a unique bottom effect, viz. ⊥ , and we can regard any ⊥ n as its extension to larger supports. In fact, we have theequality ⊥ n n i ξ i = ⊥ as well as the inequality ⊥ ⊑ ξ This property ensures the possibility to embed the Boolean algebra(which is isomorphic to) to . Notice also that if ⊥ = η ( ) , then T ( ) (cid:27) .It is a straightforward exercise to verify that all monads mentioned so farsatisfy this condition.8 Diagrammatic Calculus for Algebraic Effects A Diagrammatic Calculus for Algebraic Effects,
For this reason we write ⊥ in place of ⊥ n .We may also ask whether the ‘dual’ equality holds, i.e. if Γ n i ⊥ = ⊥ This is not the case. In fact, there are algebraic operationssuch as print − that are not strict. However, if effects are com-mutative , then such an equality holds. Definition 5.13.
We say that a monad is commutative if Γ n i ∆ m j x i , j = ∆ m j Γ n i x i , j Notice that m is independent of i , and n of j .Please observe that Definition 5.13 is rather different fromstandard definitions of commutative monads one meets inthe literature. Indeed, one of the advantages of our notationis to allow for a simple, operational definition of commuta-tivity of monads. Accordingly, we easily notice that the com-mutativity equation in Definition 5.13 is just the semanticalcounterpart of the following program equivalence: let x = t in ( let y = s in r ) ≡ let y = s in ( let x = t in r ) where x ∈ FV ( s ) and y < FV ( t ) . Proposition 5.14. If T is commutative, then Γ n i ⊥ = ⊥ Proof.
Calculate Γ n i ⊥ = Γ n i ⊥ n n i i = ⊥ n n i Γ n i i = ⊥ (cid:3) Having clarified the role of the bottom effect, let us nowmove to monotonicity laws.
Proposition 5.15.
The following monotonicity laws hold: Γ n i x i ⊑ ∆ m j y j f : X → T ( Y ) Γ n i f ( x i ) ⊑ ∆ m j f ( y j ) f , д : X → T ( Y ) f ⊑ д Γ n i f ( x i ) ⊑ Γ n i д ( x i ) Proof.
We prove the first rule (the second is similar). Let ξ , Γ n i f ( x i ) , ρ , ∆ m j f ( y j ) , γ the algebraicoperation corresponding to Γ , and δ the one correspondingto ∆ . Notice that by hypothesis we know γ ( . . . , η ( x i ) , . . . ) ⊑ δ ( . . . , η ( x i ) , . . . ) , and recall that f † is monotone. We have I( ξ ) ⊑ I( ρ ) . For: I( ξ ) = f † ( γ ( . . . , η ( x i ) , . . . ))⊑ f † ( δ ( . . . , η ( x i ) , . . . )) = I( ρ ) . (cid:3) Corollary 5.16.
The following monotonicity law hold: ∀ i ∈ n . ξ i ⊑ ρ i Γ n i ξ i ⊑ Γ n i ρ i Proof.
Define f , д : n → T ( X ) by f ( i ) , ξ i and д ( i ) , ρ j . Clearly, we have f ⊑ д . Moreover, since Γ n i ξ i = Γ n i f ( i ) and Γ n i ρ i ⊑ Γ n i д ( i ) we concludethe thesis by Proposition 5.15. (cid:3) We have introduced a diagrammatic notation and calculusfor countable monads with algebraic operations. We haveshown by means of examples and general results some ofadvantages of our notation. Additionally, the authors arecurrently using this notation to prove new, nontrivial the-orems on calculi with algebraic effects whose proofs turnedout to be extremely heavy using the standard, linear nota-tion. We leave as a future work the investigation of furtherapplications of such a notation.
References [1] S. Abramsky and A. Jung. 1994. Domain Theory. In
Handbook of Logicin Computer Science . Clarendon Press, 1–168.[2] H.P. Barendregt. 1984.
The lambda calculus: its syntax and semantics .North-Holland.[3] U. Dal Lago, F. Gavazzo, and P.B. Levy. 2017. Effectful applicativebisimilarity: Monads, relators, and Howe’s method. In
Proc. of LICS2017 . 1–12.[4] Ugo Dal Lago and Margherita Zorzi. 2012. Probabilistic operationalsemantics for the lambda calculus.
RAIRO - Theor. Inf. and Applic.
Inf. Comput.
J.ACM
24, 1 (1977), 68–95.[7] Andrew Donald Gordon. 1992.
Functional programming and in-put/output . Ph.D. Dissertation. University of Cambridge, UK. http://ethos.bl.uk/OrderDetails.do?uin=uk.bl.ethos.259479 [8] Martin Hyland, Gordon D. Plotkin, and John Power. 2006. Combiningeffects: Sum and tensor.
Theor. Comput. Sci.
Electr.Notes Theor. Comput. Sci.
172 (2007), 437–458.[10] B. Jacobs. 2016.
Introduction to Coalgebra: Towards Mathematics ofStates and Observation . Cambridge Tracts in Theoretical ComputerScience, Vol. 59. Cambridge University Press.[11] Patricia Johann, Alex Simpson, and Janis Voigtländer. 2010. A GenericOperational Metatheory for Algebraic Effects. In
Proc. of LICS 2010 .IEEE Computer Society, 209–218.[12] Gregory M. Kelly. 2005. Basic Concepts of Enriched Category Theory.
Reprints in Theory and Applications of Categories
10 (2005), 1–136.[13] Ugo Dal Lago and Francesco Gavazzo. 2019. Effectful Normal FormBisimulation. In
Proc. of ESOP 2019 . To appear.[14] S.B. Lassen. 1998.
Relational Reasoning about Functions and Nondeter-minism . Ph.D. Dissertation. Dept. of Computer Science, University ofAarhus.[15] William F. Lawvere. 2004.
Functorial Semantics of Algebraic Theories .Ph.D. Dissertation.9
Diagrammatic Calculus for Algebraic Effects, Ugo Dal Lago and 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.
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 AlgebraicEffects. In
Proc. of FOSSACS 2001 . 1–24.[24] Gordon D. Plotkin and John Power. 2001. Semantics for AlgebraicOperations.
Electr. Notes Theor. Comput. Sci.
45 (2001), 332–345.[25] Gordon D. Plotkin and John Power. 2003. Algebraic Operations andGeneric Effects.
Applied Categorical Structures
11, 1 (2003), 69–94.[26] John Power. 2006. Countable Lawvere Theories and ComputationalEffects.
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.
Proc. of ESOP 2018 . 300–326.[29] Jerzy Tiuryn and Mitchell Wand. 1996. Untyped lambda-calculuswith 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