Structured Handling of Scoped Effects: Extended Version

Algebraic effects offer a versatile framework that covers a wide variety of effects. However, the family of operations that delimit scopes are not algebraic and are usually modelled as handlers, thus preventing them from being used freely in conjunction with algebraic operations. Although proposals for scoped operations exist, they are either ad-hoc and unprincipled, or too inconvenient for practical programming. This paper provides the best of both worlds: a theoretically-founded model of scoped effects that is convenient for implementation and reasoning. Our new model is based on an adjunction between a locally finitely presentable category and a category of functorial algebras. Using comparison functors between adjunctions, we show that our new model, an existing indexed model, and a third approach that simulates scoped operations in terms of algebraic ones have equal expressivity for handling scoped operations. We consider our new model to be the sweet spot between ease of implementation and structuredness. Additionally, our approach automatically induces fusion laws of handlers of scoped effects, which are useful for reasoning and optimisation.


Introduction
For a long time, monads [47,62,70] have been the go-to approach for purely functional modelling of and programming with side effects.However, in recent years an alternative approach, algebraic effects [50], is gaining more traction.A big breakthrough has been the introduction of handlers [54], which has made algebraic effects suitable for programming and has led to numerous dedicated languages and libraries implementing algebraic effects and handlers.In comparison to monads, algebraic effects provide a more modular approach to computations with effects, in which the syntax and semantics of effects are separatedcomputations invoking algebraic operations can be defined syntactically, and the semantics of operations are given by handlers separately in possibly many ways.
A disadvantage of algebraic effects is that they are less expressive than monads; not all effects can be easily expressed or composed within their confines.For instance, operations like catch for exception handling, spawn for parallel composition of processes, or once for restricting nondeterminism are not conventional algebraic operations; instead they delimit a computation within their scope.Such operations are usually modelled as handlers, but the problem is that they cannot be freely used amongst other algebraic operations: when a handler implementing a scoped operation is applied to a computation, the computation is transformed from a syntactic tree of algebraic operations into some semantic model implementing the scoped operation.Consequently, all subsequent operations on the computation can only be given in the particular semantic model rather than as mere syntactic operations, thus nullifying the crucial advantage of modularity when separating syntax and semantics of effects.
To remedy the situation, Wu et al. [72] proposed a practical, but ad-hoc, generalization of algebraic effects in Haskell that encompasses scoped effects, that has been adopted by several algebraic effects libraries [34,44,58].More recently, Piróg et al. [48] sought to put this ad-hoc approach for scoped effects on the same formal footing as algebraic effects.Their solution resulted in a construction based on a level-indexed category, called indexed algebras, as the way to give semantics to scoped effects.However, this formalisation introduces a disparity between syntax and semantics that makes indexed algebras not as structured as the programs they interpret, where they use an ad-hoc hybrid fold that requires indexing for the handlers, but not for the program syntax.Moreover, indexed algebras are not ideal for widespread implementation as they require dependent typing, in at least a limited form like gadts [27].
This paper presents a more structured way of handling scoped effects, which we call functorial algebras.They are principled and formally grounded on category theory, and at the same time more structured than the indexed algebras of Piróg et al. [48], in the sense that the structure of functorial algebras directly follows the abstract syntax of programs with scoped effects.Additionally, our approach can be practically implemented without the need for dependent types or gadts, making it available for a wider range of programming languages.In particular, we make the following contributions: -We highlight the loss of modularity when modelling scoped operations as handlers and sketch how the problem is solved using functorial algebras in Haskell, along with a number of programming examples (Section 2); -We develop a category-theoretic foundation of functorial algebras as a notion of handlers of scoped effects.Specifically, we show that there is an adjunction between functorial algebras and a base category, inducing the monad modelling the syntax of scoped effects (Section 3); -We show that the expressivity of functorial algebras, Piróg et al. [48]'s indexed algebras, and simulating scoped effects with algebraic operations and recursion are equal, by constructing interpretation-preserving functors between the three categories of algebras (Section 4); -We present the fusion law of functorial algebras, which is useful for reasoning and optimisation.The fusion law directly follows from the naturality of the adjunction underlying functorial algebras (Section 5).Finally, we discuss related work (Section 6) and conclude (Section 7).

Scoped Effects for the Working Programmer
We start with a recap of handlers of algebraic effects (Section 2.1), and then we highlight the loss of modularity when modelling non-algebraic effectful operations as handlers (Section 2.2).We then show how the problem is solved by modelling them as scoped operations and handling them with functorial algebras in Haskell (Section 2.3), whose categorical foundation will be developed later.

Handlers of Algebraic Effects
For the purpose of demonstration, in this section we base our discussion on a simplistic implementation of effect handlers in Haskell using free monads, although the problem with effect handlers highlighted in this section applies to other more practical implementations of effect handlers, either as libraries (e.g.[29,35]) or standalone languages (e.g.[7,38,42]).
Following Plotkin and Pretnar [54], computational effects, such as exceptions, mutable state, and nondeterminism, are described by signatures of primitive effectful operations.Signatures can be abstractly represented by Haskell functors: class Functor f where fmap :: (a → b) → f a → f b The following functor ES (with the evident Functor instance) is the signature of three operations: throwing an exception, writing and reading an Int-state: Typically, a constructor of a signature functor Σ has a type isomorphic to P → (R → x ) → Σ x for some types P and R. As in (1), the types of the three constructors are isomorphic to Throw :: () → (Void → x ) → ES x , Put :: Int → (() → x ) → ES x and Get :: () → (Int → x ) → ES x respectively where Void is the empty type.Each constructor of a signature functor Σ is thought of as an operation that takes a parameter of type P and produces a result of type R, or equivalently, has R-many possible ways to continue the computation after the operation.Given any (signature) functor Σ, computations invoking operations from Σ are modelled by the following datatype, called the free monad of Σ, data Free Σ a = Return a | Call (Σ (Free Σ a)) whose first case represents a computation that just returns a value, and the second case represents a computation call ing an operation from Σ with more Free Σ a subterms as arguments, which are understood as the continuation of the computation after this call, depending on the outcome of this operation.
The inductive datatype Free Σ a comes with a recursion principle: which folds a tree of operations Free Σ a into a type b, providing a way Σ b → b, usually called a Σ-algebra, to perform operations from Σ on b and a way a → b to transform the returned type a of computations to b.The function handle can be used to give Free Σ a monad instance: The monadic instance allows the programmer to build effectful computations using the do-notation in a clean way.For example, the following program updates the state s to n / s for some n :: Int , and throws an exception when s is 0: where the auxiliary wrapper functions (the so-called smart constructors in the Haskell community) that invoke Call appropriately are The free monad merely models effectful computations syntactically without specifying how these operations are actually implemented.Indeed, the program safeDiv above is defined without saying how mutable state and exceptions are implemented at all.To actually give useful semantics to programs built with free monads, the programmer uses the handle function above to interpret programs with Σ-algebras, which are called handlers in this context.
For example, given a program r ::Free ES a for some a, a handler catchHdl r :: ES (Free ES ) → Free ES that gives the usual semantics to throw is which evaluates r for r ecovery in case of throwing an exception, and leaves other operations untouched in the free monad.An important advantage of the approach of effect handlers is that different semantics of a computational effect can be given by different handlers.For example, suppose that in some scenario one would like to interpret exceptions as unrecoverable errors and stop the execution of the program when an exception is raised.Then the following handler can be defined for this behaviour: As expected, applying these two handlers to the program safeDiv Note that exception throwing and catching are modelled differently in the approach of algebraic effects and handlers, one as an operation in the signature ES and one as a handler, although it is natural to expect both of them to be operations of the effect of exceptions.This asymmetry results from the fact that exception catching is not algebraic: if catch was modelled as a binary operation in the signature, then the monadic bind > > = of the free monad earlier, which intuitively means sequential composition of programs, would imply that (catch r p) Thus the perspective of Plotkin and Pretnar [54] is that non-algebraic operations like catch should be deemed different from algebraic operations, and they can be modelled as handlers (of algebraic operations).

Scoped Operations as Handlers Are Not Modular
However, this treatment of non-algebraic operations leads to a somewhat subtle complication: as observed by Wu et al. [72], when non-algebraic operations (such as catch) are modelled with handlers, these handlers play a dual role of (i) modelling the syntax of the operation (the scope for which exceptions are caught by catch) and (ii) giving semantics to it (when an exception is caught, run the recovery program).To see the problem more concretely, ideally one would like to have a syntactic operation catch of the following type that acts on computations without giving specific semantics a priori, catch :: Free ES a → Free ES a → Free ES a allowing to write programs like prog = do {x ← catch (safeDiv 5) (return 42); put (x + 1)} ( 4) and the semantics of (both algebraic and non-algebraic) operations in prog can be given separately by handlers.Unfortunately, when catch is modelled as handlers catchHdl or catchHdl ′ as in the last subsection, the program prog must be written differently depending on which handler is used: The issue is that these handlers interpret the operation catch in different semantic models, Free ES a and Free ES (Maybe a), and this affects both the value x that is returned, and the way the subsequent put is expressed.Therefore, nonalgebraic operation catch modelled as handlers is not as modular as algebraic operations, weakening the advantage of programming with algebraic effects.

Scoped Effects and Functorial Algebras
Now we present an overview of a solution to the problem highlighted above by modelling exception catching as scoped effects [48] and handle them using functorial algebras, which will be more formally developed in later sections.

Syntax of Scoped Operations
To achieve modularity for (non-algebraic) operations delimiting scopes, such as catch, which are called scoped operations, Piróg et al. [48] generalise the free monad Free Σ to a monad Prog Σ Γ accommodating both algebraic and scoped operations.The monad is parameterised by two functors Σ and Γ , called the algebraic signature and the scoped signature respectively.The intention is that a constructor Op :: (R → x ) → Σ x of the algebraic signature represents an algebraic operation Op producing an R-value as usual, whereas a constructor Sc :: (N → x ) → Γ x of the scoped signature represents a scoped operation Sc creating N -many scopes enclosing programs.
Example 1.As in the previous subsection, the effect of exceptions has an algebraic operation for throwing exceptions, which produces no values, and a scoped operation for catching exceptions, which creates two scopes, one enclosing the program for which exceptions are caught, and the other enclosing the recovery computation.Thus the algebraic and scoped signatures are respectively Example 2. An effect of explicit nondeterminism has two algebraic operations for nondeterministic choice and a scoped operation Once: The intention is that this effect implements logic programming [21]-solutions to a problem are exhaustively searched: operation Or p q splits a search branch into two; Fail marks a failed branch; and the scoped operation Once p keeps only the first solution found by p, making it semi-deterministic, which is useful for speeding up the search with heuristics from the programmer.
Similar to the free monad, the Prog monad models the syntax of computations invoking operations from Σ and Γ : Thus an element of Prog Σ Γ a can either (i) return an a-value without causing effects, or (ii) call an algebraic operation in Σ with more subterms of Prog Σ Γ a as the continuation after the operation, or (iii) enter the scope of a scoped operation.The third case deserves more explanation: the first Prog in (Γ (Prog Σ Γ (Prog Σ Γ a))) represents the programs enclosed by the scoped operation, and the second Prog represents the continuation of the program after the scoped operation, and thus the boundary between programs inside and outside the scope is kept in the syntax tree, which is necessary because collapsing the boundary might change the meaning of a program.The distinction between algebraic and scoped operations can be seen more clearly from the monadic bind of Prog (the monadic return of Prog is just Return): For algebraic operations, extending the continuation (> > =k ) directly acts on the argument to the algebraic operation, whereas for scoped operation, (> > =k ) acts on the second layer of Prog.With all machinery in place, now we can define the program (4) using Prog that we could not write with Free: prog = do {x ← catch (safeDiv 5) (return 42); put (x + 1)} Handlers of Scoped Operations Similar to Free, the Prog monad merely models the syntax of effectful computations, and more useful semantics need to be given by handlers.Although Piróg et al. [48] developed a notion of indexed algebras for this purpose, indexed algebras turn out to be more complicated than necessary (we will discuss them in Section 4), and the contribution of this paper is a simpler kind of handlers for scoped operations, which we call functorial algebras.
Given signatures Σ and Γ , a functorial algebra for them is a quadruple f , b, ealg , balg for some functor f called the endofunctor carrier, type b called the base carrier.The other two components ealg :: EndoAlg Σ Γ f and balg :: For the base carrier that interprets operations not enclosed by any catch, a straightforward choice is just taking Maybe a as the base carrier for a type a, and setting callB = callE and enterB = enterE , which means that operations inside and outside scopes are interpreted in the same way.
In general, we can define a specialised version of handle (Figure 1) that only takes an endofunctor algebra as input for interpreting operations inside and outside scopes in the same way: Now we provide some intuition for how functorial algebras work.First note that the three fields of EndoAlg in Figure 1 precisely correspond to the three cases of Prog (7).Thus by replacing the constructors of Prog with the correspond fields of EndoAlg, we have a polymorphic function hcata ealg ::∀x .Prog Σ Γ x → f x (Figure 1) turning a program into a value in f .
The function handle (Figure 1) takes a functorial algebra, a function gen :: a → b and a program p as arguments, and it handles all the effectful operations in p by using hcata ealg for interpreting the part of p inside scoped operations and balg for interpreting the outermost layer of p outside any scoped operations.The function gen corresponds to the 'value case' of handlers of algebraic effects, which transforms the a-value returned by a program into the type b for interpretation.
We close this section with some more examples of handling scoped effects with functorial algebras.The supplementary material of this paper also contains an OCaml implementation of functorial algebras and the following examples.
Example 4. The standard way to handle explicit nondeterminism with the semideterministic operator once (Example 2) is using a functorial algebra with the list functor as the endofunctor carrier together with the following algebra: Then applying handleE ndetE to the following program produces [1,2] as expected.In comparison, if once were algebraic, the result would be [1].
Parallel composition of processes is not an operation in the usual algebraic presentations of process calculi [63,64] precisely because it not algebraic: Again, we can model it as a scoped operation, and different scheduling behaviours of processes can be given as different functorial algebras.The supplementary material contains complete code of handling parallel composition using the so-called resumption monad [11,49]:

Categorical Foundations for Scoped Operations
We now move on to a categorical foundation for scoped effects and functorial algebras.First, we recall some standard category theory underlying algebraic effects and handlers (Section 3.1) and also Piróg et al. [48]'s monad P that models the syntax of scoped operations, which is exactly the Prog monad in the Haskell implementation (Section 3.2).Then, we define functorial algebras formally (Section 3.3) and show that there is an adjunction between the category of functorial algebras and the base category (Section 3.4) inducing the monad P , which provides a means to interpret the syntax of scoped operations.The rest of this paper assumes familiarity with basic category theory, such as adjunctions, monads, and initial algebras, which are covered by standard texts [6,43,57].The mathematical notation in this paper is summarised in Appendix B, which may be consulted if the meaning of some symbols are unclear.

Syntax and Semantics of Algebraic Operations
The relationships between equational theories, Lawvere theories, monads, and computational effects are well-studied for decades from many perspectives [25,32,47,50,56,59].Here we recap a simplified version of equational theories by Kelly and Power [32] that we follow to model algebraic and scoped effects on locally finitely presentable (lfp) categories [1].However, we only consider unenriched categories in this paper.
Locally Finitely Presentable Categories The use of lfp categories in this paper is limited to some standard results about the existence of many initial algebras in lfp categories, and thus a reader not familiar with lfp categories may follow this paper with some simple intuition: a category C is lfp if it has all (small) colimits and a set of finitely presentable objects such that every object in C can be obtained by 'glueing' (formally, as filtered colimits of) some finitely presentable objects.For example, Set is lfp with finite sets as its finitely presentable objects, and indeed every set can be obtained by glueing, here meaning taking the union of, all its finite subsets: Other examples of lfp categories include the category of partially ordered sets, the category of graphs, the category of small categories, and presheaf categories (we refer the reader to the excellent exposition [59] for concrete examples), thus lfp categories are widespread to cover many semantic settings of programming languages.
Moreover, an endofunctor F : C → C is said to be finitary if it preserves 'glueing' (filtered colimits), which implies that its values F X are determined by its values at finitely presentable objects: where N i are the finitely presentable objects that generate X when glued together.For example, polynomial functors n∈N P n × (−) n on Set are finitary where P n is a set for every n.
Algebraic Operations on LFP Categories Fixing an lfp category C, we take finitary endofunctors Σ : C → C as signatures of operations on C. Like in Section 2.1, the intuition is that every natural transformation C(R,−) P → Σ− for some object P : C and finitely presentable object R : C standards for an operation taking a parameter of type P and R-many arguments.The category Σ-Alg of Σ-algebras is defined as usual: it has pairs X : C, α : ΣX → X as objects and morphisms h : X → X ′ such that h • α = α ′ • Σh as morphisms X, α → X ′ , α ′ .The following classical results (see e.g.[2,5]) give sufficient conditions for constructing initial and free Σ-algebras: Lemma 1.If category C has finite coproducts and colimits of all ω-chains and functor Σ : C → C preserves them, then the forgetful functor U Σ : Σ-Alg → C forgetting the structure maps has a left adjoint Free Σ : C → Σ-Alg mapping every X : C to a Σ-algebra Σ * X, op X where Σ * X denotes the initial algebra µY .X + ΣY and op X : ΣΣ * X → Σ * X.
Lemma 1 is applicable to our setting since C being lfp directly implies that it has all colimits, and finitary functors Σ preserve colimits of ω-chains because colimits of ω-chains are filtered.Hence we have an adjunction: Free Σ ⊣ U Σ : Σ-Alg → C. We denote the monad from the adjunction by Σ * = U Σ Free Σ (which is implemented as the Free Σ monad in Section 2.1).The idea is still that syntactic terms built from operations in Σ are modelled by the monad Σ * , and semantics of operations are given by Σ-algebras.Given any Σ-algebra X, α : ΣX → X and morphism g : A → X in C, they induce an interpretation morphism handle X,α g : where ǫ X,α : Algebraic Effects and Handlers The perspective of Plotkin and Pretnar [54] is that computational effects are characterised by signatures Σ of primitive effectful operations, and they determine monads Σ * that model programs syntactically.
Then Σ-algebras are handlers [54] of operations that can be applied to programs using (9) to give specific semantics to operations.The approach of algebraic effects has led to a significant body of research on programming with effects and handlers, but it imposes an assumption on the operations to be modelled: the construction of Σ * in Lemma 1 [2,5] implies that the multiplication µ of the monad Σ * satisfies the algebraicity property: This intuitively means that every operation in Σ must be commutative with sequential composition of computations.Many, but not all, effectful operations satisfy this property, and they are called algebraic operations.
Adjoint Approach to Effects The crux of algebraic effects and handlers is the adjunction Free Σ ⊣ U Σ .However, we have not relied on the adjunction being the free/forgetful one at all: given any monad P : C → C that models the syntax of effectful P rograms, if L ⊣ R : D → C is an adjunction such that RL ∼ = P as monads, then objects D in D provide a means to interpret programs P A-for any g : A → RD in C, we have the following interpretation morphism The intuition for g is that it transforms the returned value A of a computation into the carrier RD, so it corresponds to the 'value case' of effect handlers [8].Piróg et al. [48] call this approach the adjoint-theoretic approach to syntax and semantics of effects, and they construct an adjunction between indexed algebras and the base category for modelling scoped operations.Earlier, Levy [39] and Kammar and Plotkin [30] also adopt a similar adjunction-based viewpoint in the treatment of call-by-push-value calculi: value types are interpreted in the base category C, and computation types are interpreted in the algebra category D.
Remark 1.A notable missing part of our treatment is the equations that specify operations in a signature.Following Kelly and Power [32], an equation for a signature Σ : C → C can be formulated as a pair of monad morphisms σ, τ : for some finitary functor Γ , and taking their coequaliser in the category of finitary monads constructs a monad M that represents terms modulo the equation l = r.Although it seems straightforward to extend this formulation of equational theories work with scoped effects, we do not consider equations in this paper for the sake of simplicity.
Remark 2. Working with lfp categories precludes operations with infinite arguments, such as the get operation (1) of mutable state when the state has infinite possible values, but this limitation is not inherent and can be handled by moving to locally κ-presentable categories [1] for some larger cardinal κ.

Syntax of Scoped Operations
Not all operations in programming languages can be adequately modelled as algebraic operations on Set, for example, λ-abstraction [16], memory cell generation [40,50], more generally, effects with dynamically generated instances [64], explicit substitution [19], channel restriction in π-calculus [63], and their syntax are usually modelled in some functor categories.More recently, Piróg et al. [48] extend Ghani and Uustalu [19]'s work to model a family of non-algebraic operations, which they call scoped operations.In this subsection, we review their development in the setting of lfp categories.Throughout the rest of the paper, we fix an lfp category C, and refer to it as the base category, and it is intended to be the category in which types of a programming language are interpreted.Furthermore, we fix two finitary endofunctors Σ, Γ : C → C and call them the algebraic signature and scoped signature respectively.
Syntax Endofunctor P Now our goal is to construct a monad P : C → C that models the syntax of programs with algebraic operations in Σ and non-algebraic scoped operations in Γ .First we construct its underlying endofunctor.When C is Set, the intuition for programs P A is that they are terms inductively built from the following inference rules: where n ranges over finite sets and o ∈ Σn represents an algebraic operation of |n| arguments, and similarly s ∈ Γ n is a scoped operation that creates |n| scopes.The difference between algebraic and scoped operations is manifested by an additional explicit continuation k in the third rule, as it is not the case that sequentially composing s(p) with k equals s(p; k) like for algebraic operations, so the continuation for scoped operations must be explicitly kept in the syntax.When C is any lfp category, these rules translate to the following recursive equation for the functor P : C → C: where the existentially quantified X in the third rule is translated to a coend X:C in C [43].Moreover, the coend in ( 11) is isomorphic to Γ (P (P A)) because by the coend formula of Kan extension, it exactly computes Lan I (Γ P )(P A), i.e. the left Kan-extension of Γ P along the identity functor I : C → C, and by definition Lan I (Γ P ) = Γ P .Thus ( 11) is equivalent to which is exactly the Prog Σ Γ datatype that we saw in the Haskell implementation (7).To obtain a solution to (12), we construct a (higher-order) endofunctor to represent the Grammar where Endo f (C) is the category of finitary endofunctors on C: where Id : C → C is the identity functor.Then Lemma 1 is applicable because Endo f (C) has all small colimits since colimits in functor categories can be computed pointwise and C has all small colimits.Furthermore, G preserves all filtered colimits, in particular colimits of ω-chains, because − • = : ) is finitary following from direct verification.Since initial algebras are precisely free algebras generated by the initial object, by Lemma 1, there is an initial G-algebra P : Endo f (C), in : GP → P and in is an isomorphism.Thus P obtained in this way is indeed a solution to (12)-the endofunctor modelling the syntax of programs with algebraic and scoped operations.
Monadic Structure of P Next we equip the finitary endofunctor P with a monad structure.This can be done in several ways, either by the general result about Σ-monoids [14,16] in Endo f (C), or by [45,Theorem 4.3], or by the following relatively straightforward argument in [48]: by the 'diagonal rule' of computing initial algebras by Backhouse et al. [4], P = µG (13) is isomorphic to Note that P ′ is exactly (Σ +Γ • P ) * as endofunctors by Lemma 1, thus Then we equip P with the same monad structure as the ordinary free monad (Σ + Γ • P ) * .The implementation in (7) is exactly this monad structure.

Functorial Algebras of Scoped Operations
To interpret the monad P (12) modelling the syntax of scoped operations, it is natural to expect that semantics is given by G-algebras on Endo f (C) so that interpretation is then the catamorphisms from µG to G-algebras.And following the adjoint-theoretic approach (10), we would like to have an adjunction G-Alg C ⊣ such that the induced monad is isomorphic to P .However, there seems no natural way to construct such an adjunction unless we replace Galgebras with a slight extension of it, which we referred to as functorial algebras, as the notion for giving semantics to scoped operations.In the following, we first define functorial algebras formally (Definition 1) and then show the adjunction between the category of functorial algebras and the base category (Theorem 1), which allows us to interpret P with functorial algebras.
A functorial algebra is carried by an endofunctor H : C → C with additionally an object X in C. The endofunctor H also comes with a morphism α G : GH → H in Endo f (C), and the object X is equipped with a morphism α I : ΣX + Γ HX → X in C. The intuition is that given a program of type P X ∼ = X + Σ(P X) + Γ (P (P X)), the middle P in Γ P P corresponds to the part of a program enclosed by some scoped operations (i.e. the p in {s(p)> > =k}), and this part of the program is interpreted by H with α G .After the enclosed part is interpreted, α I interprets the outermost layer of the program by X with α I in the same way as interpreting free monads of algebraic operations.More precisely, let I : Endo f (C) × C → C be a bi-functor such that3 for all H : Endo f (C) and X : C and all morphisms σ : H → H ′ and f : X → X ′ .Then we define an endofunctor Fn : Definition 1 (Functorial Algebras).A functorial algebra is an object H, X in Endo f (C)×C paired with a structure map Fn H, X → H, X , or equivalently it is a quadruple Functorial algebras and their morphisms form a category Fn-Alg.
Example 8. We reformulate our programming example of nondeterministic choice with once shown Example 4 in the formal definition.Let C = Set in this example and 1 = {⋆} be some singleton set.We define signature endofunctors so that Σ represents nullary algebraic operation fail and binary algebraic operation or , and Γ represents the unary scoped operation once that creates one scope.Let List : Set → Set be the endofunctor mapping a set X to the set of finite lists with elements from X.We define natural transformations x where nil is the empty list; + + is list concatenation; and cons x xs is the list with an element x in front of xs.Then for any set X, List , List X carries a functorial algebra with structure maps where η List : Id → List wraps any element into a singleton list.
The last example exhibits that one can define a functorial algebra carried by H, HX from a G-algebra on H : Endo f (C) by simply choosing the object component to be HX for an arbitrary X : C. In other words, there is a faithful functor G-Alg → Fn-Alg, which results in functorial algebras that interpret the outermost layer of a program-the part not enclosed by any scoped operationin the same way as the inner layers.But in general, the object component of functorial algebras offers the flexibility that the outermost layer can be interpreted differently from the inner layers, as in the following example.
Example 9. Continuing Example 8, if one is only interested in the final number of possible outcomes, then one can define a functorial algebra List , N, α G , α I where α G is (17) and α I (ι 1 (ι 1 ⋆)) = 0,

Interpreting with Functorial Algebras
In the rest of this section we show how functorial algebras can be used to interpret programs P A (12) with scoped operations.We first construct a simple adjunction ↑ ⊣ ↓ between the base category C and Endo f (C) × C, which is then composed with the free/forgetful adjunction Free Fn ⊣ U Fn between Endo f (C) × C and Fn-Alg for the functor Fn (16).The resulting adjunction ( 18) is proven to induce a monad T isomorphic to P (Theorem 1), and by the adjoint-theoretic approach to syntax and semantics (10), this adjunction provides a means to interpret scoped operations modelled with the monad P (Theorem 2).
First we define functor ↑ : C → Endo f (C) × C such that ↑ X = 0, X where 0 : Endo f (C) is the initial endofunctor-the constant functor sending everything to the initial object in C. The functor ↑ is left adjoint to the projection functor ↓ : Endo f (C) × C → C of the second component.
Then we would like to compose ↑ ⊣ ↓ with the free-forgetful adjunction Free Fn ⊣ U Fn for the endofunctor Fn (16) on Endo f (C) × C, and the latter adjunction indeed exists.
and we compose them to obtain an adjunction Free Fn ↑ ⊣ ↓ U Fn between Fn-Alg and C, giving rise to a monad T = ↓ U Fn Free Fn ↑.In the rest of this section, we prove that T is isomorphic to P (11) in the category of monads, which is crucial in this paper, since it allows us to interpret scoped operations modelled by the monad P with functorial algebras Fn-Alg.
We first establish a technical lemma characterising the free Fn-algebra on the product category Endo f (C) × C in terms of the free algebras in C and Endo f (C).Lemma 3.There is a natural isomorphism between Free Fn and the following where op G * H : G(G * H) → G * H and op ) * X are the structure maps of the free G-algebra and I G * H -algebra respectively.
Proof sketch.It follows from the formula of computing free algebras from initial algebras Lemma 1.A detailed proof can be found in the appendix.
Proof sketch.By ( 14) and ( 15), P ∼ = (Σ+Γ P ) * = (I P ) * as monads, it is sufficient to show that T ∼ = (I P ) * as monads, which follows from Lemma 3 by careful calculation.A detailed proof can be found in Appendix C. Remark 3. In general, the adjunction Free Fn ↑ ⊣ ↓ U Fn is not monadic since the right adjoint ↓ U Fn does not reflect isomorphisms, which is a necessary condition for it to be monadic by Beck's monadicity theorem [43].This entails that the category Fn-Alg of functorial algebras is not equivalent to the category of Eilenberg-Moore algebras.Nonetheless, as we will see later in Section 4, functorial algebras and Eilenberg-Moore algebras have the same expressive power for interpreting scoped operations in the base category.
The isomorphism established Theorem 1 enables us to interpret programs modelled by the monad P using functorial algebras following (10): for any functorial algebra H, X, α G , α I (Definition 1), and any morphism g : A → X in the base category C, there is a morphism which interprets programs P A with the functorial algebra H, X, α G , α I .Furthermore, we can derive the following recursive formula (20) for this interpretation morphism, which is exactly the Haskell implementation in Figure 1.
Theorem 2 (Interpreting with Functorial Algebras).For any functorial algebra α = H, X, α G , α I as in Definition 1, and any morphism g : A → X for some A in the base category C, let h = α G : P → H be the catamorphism from the initial G-algebra P to the G-algebra α G : GH → H.The interpretation of P A with this algebra α and g satisfies where in • : P → Id + Σ • P + Γ • P • P is the isomorphism between P and GP ; morphisms α I Σ = α I • ι 1 : ΣX → X and α I Γ = α I • ι 2 : Γ HX → X are the two components of α I : ΣX + Γ HX → X.
Proof sketch.It can be calculated by plugging the formula of ǫ for the adjunction ↓ U Fn ⊣ Free Fn ↑ (Lemma 1) into (19).
To summarise, we have defined a notion of functorial algebras that we use to handle scoped operations.The heart of the development is the adjunction (18) that induces a monad isomorphic to the monad P (12) that models the syntax of programs with scoped operations, following which we derive a recursive formula (20) that interprets programs with functor algebras.The formula is exactly the implementation in Figure 1: the datatype EndoAlg represents the α G in (20); datatype BaseAlg corresponds to α I ; function hcata implements α G .

Comparing the Models of Scoped Operations
Functorial algebras are not the only option for interpreting scoped operations.In this section we compare functorial algebras with two other approaches, one being Piróg et al. [48]'s indexed algebras and the other one being Eilenberg-Moore (EM) algebras of the monad P (12), which simulate scoped operations with algebraic operations.After a brief description of these two kinds of algebras, we compare them and show that their expressive power is in fact equivalent.

Interpreting Scoped Operations with Eilenberg-Moore Algebras
In standard algebraic effects, handlers are just Σ-algebras for some signature functor Σ : C → C, and it is well known that the category Σ-Alg of Σ-algebras is equivalent to the category C Σ * of EM algebras of the monad Σ * .Thus handlers of algebraic operations are exactly EM algebras of the monad Σ * modelling the syntax of algebraic operations.This observation suggests that we may also use EM algebras of the monad P (12) as the notion of handlers for scoped operations.Lemma 4. EM algebras of P are equivalent to (Σ + Γ • P )-algebras.In other words, an EM algebra of P is equivalently a tuple Proof.Recall that the monad structure of P ( 14) is exactly the monad structure of the free monad (Σ + Γ • P ) * , and therefore they have the same EM algebras.Moreover, EM algebras of (Σ + Γ • P ) * are equivalent to plain (Σ + Γ • P )algebras by the monadicity of the free-forgetful adjunction.
Thus we obtain a way of interpreting scoped operations based on the freeforgetful adjunction Free Σ+Γ • P ⊣ U Σ+Γ • P : given an EM algebra α = X, α Σ , α Γ of P as in (21), then for any A : C and morphism g : A → X, the interpretation of P A by g and this EM algebra is The formula (22) can also be turned into a recursive form: that suits implementation (see Appendix D for more details).
Interpreting scoped operation with EM algebras can be understood as simulating scoped operations with algebraic operations and general recursion: a signature (Σ, Γ ) of algebraic-and-scoped operations is simulated by a signature (Σ +Γ •P ) of algebraic operations where P is recursively given by (Σ +Γ •P ) * .In this way, one can simulate scoped operation in languages implementing algebraic effects that allow signatures of operation to be recursive, such as [7,20,38], but not the original design by Plotkin and Pretnar [54], which requires signatures of operations to mention only some base types.
The downside of this simulating approach is that the denotational semantics of the language becomes more complex and usually involves solving some domain-theoretic recursive equations, like in [7].Moreover, this approach typically requires handlers to be defined with general recursion, which obscures the inherent structure of scoped operations, making reasoning about handlers of scoped operations more difficult.

Indexed Algebras of Scoped Effects
Indexed algebras of scoped operations by Piróg et al. [48] are yet another way of interpreting scoped operations.They are based on the following adjunction: where C |N| is the functor category from the discrete category |N| of natural numbers to the base category C. That is to say, an object in C |N| is a family of objects A i in C indexed by natural numbers i ∈ |N|, and a morphism τ : A → B in C |N| is a family of morphisms τ i : A i → B i in C with no coherence conditions between the levels.An endofunctor Ix : C |N| → C |N| is defined to characterise indexed algebras: where ⊳ and ⊲ are functors C |N| → C |N| shifting indices such that (⊳A) i = A i+1 and (⊲A) 0 = 0 and (⊲A) i+1 = A i .Then objects in Ix-Alg are called indexed algebras.Furthermore, since a morphism (⊲A) → A is in bijection with A → (⊳A), an indexed algebra can be given by the following tuple: The operational intuition for it is that the carrier A i at level i interprets the part of syntax enclosed by i layers of scopes, and when interpreting a scoped operation Γ (P (P X)) at layer i, the part of syntax outside the scope is first interpreted, resulting in Γ (P A i ), and then the indexed algebra provides a way p to promote the carrier to the next level, resulting in Γ (P A i+1 ).After the inner layer is also interpreted as Γ A i+1 , the indexed algebra provides a way d to d emote the carrier, producing A i again.Additionally the morphism a interprets ordinary algebraic operations.
Example 10.Example 8 for nondeterministic choice with once can be re-expressed with an indexed algebra as follows.For any set X, we define an indexed object A : C |N| by A 0 = List X and A i+1 = List A i .The object A carries an indexed algebra with the following structure maps: for all i ∈ N, a i (ι 1 ⋆) = nil and The adjunction Free Ix ⊣ U Ix in ( 24) is the free-forgetful adjunction for Ix on C |N| .The other adjunction ↾⊣⇃ is given by ⇃ A = A 0 , (↾ X) 0 = X, and (↾ X) i+1 = 0 for all i ∈ N. Importantly, Piróg et al. [48] show that the monad induced by the adjunction ( 24) is isomorphic to monad P (12), thus indexed algebras can also be used to interpret scoped operations in the same way as what we do for functorial algebras in Section 3.4.Interpreting with indexed algebras can also be implemented in Haskell with GHC's DataKinds extension for type-level natural numbers (which can be found in Appendix 4).

Comparison of Resolutions
Now we come back to the real subject of this section-comparing the expressivity of the three ways for interpreting scoped operations.Specifically, we construct comparison functors between the respective categories of the three kinds of algebras, which translate one kind of algebras to another in a way preserving the induced interpretation in the base category.Categorically, the three kinds of algebras correspond to three resolutions of the monad P , which form a category of resolutions (Definition 2) with comparison functors as morphisms.In this category, the Eilenberg-Moore resolution is the terminal object, and thus it automatically gives us comparison functors translating other kinds of algebras to EM algebras.To complete the circle of translations, we then construct comparison functors K EM Fn : C P → Fn-Alg translating EM algebras to functorial ones (Section 4.4) and K Fn Ix : Fn-Alg → Ix-Alg translating functorial algebras to indexed ones (Section 4.5).The situation is pictured in Figure 2. [37]).Given a monad M on C, the category Res(M ) of resolutions of M has as objects adjunctions

Definition 2 (Resolutions and Comparison Functors
called a comparison functor, such that it commutes with the left and right adjoints, i.e.KL = L ′ and R ′ K = R. We have seen adjunctions for indexed algebras, EM algebras and functorial algebras respectively, each inducing the monad P up to isomorphism, so each of them can be identified with an object in the category Res(E).For each resolution D, L, R, η, ǫ , we have been using the objects D in D to interpret scoped operations modelled by P : for any morphism g : A → RD in C, the interpretation of P A by D and g is handle where each side interprets M A using L ⊣ R and L ′ ⊣ R ′ respectively.
Proof.Since L ⊣ R and L ′ ⊣ R ′ induce the same monad, their unit must coincide η = η ′ .Together with the commutativity properties KL = L ′ and R ′ K = R, it makes a comparison functor a special case of a map of adjunctions.Then by Proposition 1 in [43, page 99], it holds that Kǫ = ǫ ′ K, and we have This lemma implies that if there is a comparison functor K from some resolution L ⊣ R : D → C to L ′ ⊣ R ′ : D ′ → C of the monad P , then K can translate a D object to a D ′ object that preserves the induced interpretation.Thus the expressive power of D for interpreting P is not greater than D ′ , in the sense that every handle D g that one can obtain from D in D can also be obtained by an algebra KD in D ′ .Thus the three kinds of algebras for interpreting scoped operations have the same expressivity if we can construct a circle of comparison functors between their categories, which is what we do in the following.
Translating to EM Algebras As shown in [43], an important property of the Eilenberg-Moore adjunction is that it is the terminal object in the category Res(M ) for any monad M , which means that there uniquely exists a comparison functor from every resolution to the Eilenberg-Moore resolution.Specifically, given a resolution D, L, R, η, ǫ of a monad M , the unique comparison functor K from D to the category C M of the Eilenberg-Moore algebras is There uniquely exist comparison functors K Ix EM : Ix-Alg → C P and K Fn EM : Fn-Alg → C P from the resolutions of indexed algebras and functorial algebras to the resolution of EM algebras.

Translating EM Algebras to Functorial Algebras
Now we construct a comparison functor K EM Fn : C P → Fn-Alg translating EM algebras to functorial ones.The idea is straightforward: given an EM algebra X, we map it to the functorial algebra with X for interpreting the outermost layer and the functor P for interpreting the inner layers, which essentially leaves the inner layers uninterpreted before they get to the outermost layer.
Since C P is isomorphic to (Σ +Γ • P )-Alg, we can define K EM Fn on (Σ +Γ • P )algebras instead.Given any X : C, α : (Σ + Γ • P )X → X , it is mapped by K EM Fn to the functorial algebra P , X, in : GP → P , α : (Σ + Γ • P )X → X and for any morphism f in (Σ + Γ • P )-Alg, it is mapped to id P , f .To show K EM Fn is a comparison functor, we only need to show that it commutes with the left and right adjoints of both resolutions.Details can be found in Appendix C.

Lemma 7. Functor K EM
Fn is a comparison functor from the Eilenberg-Moore resolution of P to the resolution Free Fn ↑ ⊣ ↓ U Fn of functorial algebras.

Translating Functorial Algebras to Indexed Algebras
At this point we have comparison functors Ix-Alg To complete the circle of translations, we construct a comparison functor K Fn Ix : Fn-Alg → Ix-Alg in this subsection.The idea of this translation is that given a functorial algebra carried by endofunctor H : C C and object X : C, we map it to an indexed algebra by iterating the endofunctor H on X.More precisely, K Fn Ix : Fn-Alg → Ix-Alg maps a functorial algebra to an indexed algebra carried by A : C |N| such that A i = H i X, i.e. iterating H i-times on X.The structure maps of this indexed algebra a : ΣA → A, d : Γ (⊳A) → A, p : A → (⊳A) are given by Ix is a comparison functor from the resolution Free Fn ↑ ⊣ ↓ U Fn of functorial algebras to the resolution Free Ix ↾ ⊣ ⇃ U Ix of indexed algebras.
Proof sketch.Again we only need to show the required commutativities for K Fn Ix to be a comparison functor: ↓ U Fn ∼ = ⇃ U Ix K Fn Ix and K Fn Ix Free Fn ↑ ∼ = Free Ix ↾.The first one is easy, and the second one follows from Piróg et al. [48]'s explicit characterisation of Free Ix ↾ X.More details can be found in Appendix C.
Since comparison functors preserve interpretation (Lemma 5), the lemma above implies that the expressivity of functorial algebras is not greater than indexed ones.Together with the comparison functors defined earlier, we conclude that the three kinds of algebras-indexed, functorial and Eilenberg-Moore algebras-have the same expressivity for interpreting scoped operations.Figure 2 summarises the comparison functors and resolutions that we have studied.
Remark 4.Although the three kinds of algebras have the same expressivity in theory, they structure the interpretation of scoped operations in different ways: EM algebras impose no constraint on how the part of syntax enclosed by scopes is handled; indexed algebras demand them to be handled layer by layer but impose no coherent conditions between the layers; functorial algebras additionally force all inner layers must be handled in a uniform way by an endofunctor.
On the whole, it is a trade-off simplicity and structuredness: EM algebras are the simplest for implementation, whereas the structuredness of functorial algebras make them easier to reason about.This is another instance of the preference for structured programming over unstructured language features, in the same way as structured loops being favoured over goto, although they have the same expressivity in theory [13].

Fusion Laws of Interpretation
A crucial advantage of the adjoint-theoretic approach to syntax and semantics is that the naturality of an adjunction directly offers fusion laws of interpretation that fuse a morphism after an interpretation into a single interpretation, which have proven to be a powerful tool for reasoning and optimisation [12,23,67,68,71,73].In this section, we present the fusion law for functorial algebras.

Fusion Laws of Interpretation
Recall that given any resolution L ⊣ R with counit ǫ of some monad M : C → C where L : C → D, for any g : A → RD, we have an interpretation morphism Then whenever we have a morphism in the form of (f • handle D g)-an interpretation followed by some morphism-the following fusion law allows one to fuse it into a single interpretation morphism.

Lemma 9 (Interpretation Fusion
Proof.We have f Then by the naturality of the counit ǫ, R(h Applying the lemma to the three resolutions of P gives us three fusion laws: for any D : D where D ∈ {Ix-Alg, Fn-Alg, C P }, one can fuse f • handle D g into a single interpretation if one can make f a D-homomorphism.Particularly, the following is the fusion law for functorial algebras.

Corollary (Fusion Law for Functorial Algebras
be a functorial algebra (Definition 1) and g : Example 11.Let α be the functorial algebra of nondeterminism with once in Example 8 and len : List A → N be the function mapping a list to its length.Then using the fusion law, len • handle α g = handle β (len • g) if we can find a suitable functorial algebra β : Fn-Alg and h : α → β s.t.↓ U Fn h = len.In fact, a suitable β is just the functorial algebra in Example 9 and h = id , len .
Example 12.Although Piróg et al. [48] propose the adjunction (24) to interpret scoped operations with indexed algebras, their Haskell implementation is not a faithful implementation of the interpretation morphism (26), but rather a more efficient one skipping the step of transforming P to the isomorphic free indexed algebra (⇃ U Ix Free Ix ↾).However, it is previously unclear whether this implementation indeed coincides with the interpretation morphism (26) due to the discrepancy between the syntax monad P and indexed algebras.This issue is in fact one of the original motivations for us to develop functorial algebras-a way to interpret P that directly follows the syntactic structure.Using the comparison functors to transform between indexed and functorial algebras, we can reason about Piróg et al. [48]'s implementation with functorial algebras, and its correctness can be established using fusion laws.Due to limited space, this extended case study is in Appendix E.

Related Work
The most closely related work is that of Piróg et al. [48] on categorical models of scoped effects.That work in turn builds on Wu et al. [72] who introduced the notion of scoped effects after identifying modularity problems with using algebraic effect handlers for catching exceptions [54].Scoped effects have found their way into several Haskell implementations of algebraic effects and handlers [34,44,58].
Effect Handlers and Modularity Spivey [62], Moggi [46] and Wadler [69] initiated monads for modeling and programming with computational effects.Soon after, the desire arose to define complex monads by combining modular definitions of individual effects [28,65], and monad transformers were developed to meet this need [41].Yet, several years later, algebraic effects were proposed as an alternative more structured approach for defining and combining computational effects [24,50,51].The addition of handlers [54] has made them practical for implementation and many languages and libraries have been developed since.Schrijvers et al. [60] have characterized modular handlers by means of modular carriers, and shown that they correspond to a subclass of monad transformers.Forster et al. [18] have also shown that algebraic effects, monads and delimited control are macro-expressible in terms of each other in an untyped language but not in a simply typed language.
Scoped operations are generally not algebraic operations in the original design of algebraic effects [50], but as we have seen in Section 4.1, an alternative view on Eilenberg-Moore algebras of scoped operations is regarding them as handlers of algebraic operations of signature Σ + Γ P .However, the functor Σ + Γ P involves the type P modelling computations, and thus it is not a valid signature of algebraic effects in the original design of effect handlers [53,54], in which the signature of algebraic effects can only be built from some base types to avoid the interdependence of the denotations of signature functors and computations.In spite of that, many later implementations of effect handlers such as Eff [7], Koka [38] and Frank [42] do not impose this restriction on signature functors (at the cost that the denotational semantics involves solving recursive domaintheoretic equations), and thus scoped operations can be implemented in these languages with EM algebras as handlers.
Other variations of scoped effects have been suggested.Recently, Poulsen et al. [55] and van den Berg et al. [9] have proposed a notion of staged or latent effect, which is a variant of scoped effects, for modelling the deferred execution of computations inside lambda abstractions and similar constructs.Ahman and Pretnar [3] investigate asynchronous effects, and they note that interrupt handlers are in fact scoped operations.We have not yet investigated this in our framework, but it will be an interesting use case.
Abstract Syntax This work focusses on the problem of abstract syntax and semantics of programs.The practical benefit of abstract syntax is that it allows for generic programming in languages like Haskell that have support for, e.g.type classes, gadts [27] and so on.As an example, Swierstra [66] showed that it is possible to modularly create compilers by formalising syntax in Haskell.
The problem of formalising abstract syntax categorically for operations with variable binding was first addressed by Fiore et al. [16,17].Subsequently, Ghani and Uustalu [19] model the abstract syntax of explicit substitutions as an initial algebra in the endofunctor category and show that it is a monad.Piróg et al. [48] and this paper use a monad P , is a slight generalisation of the monad of explicit substitutions, to model the syntax of scoped operations.The datatype underlying P is an instance of nested datatypes studied by Bird and Paterson [10] and Johann and Ghani [26].
In this paper we have not treated equations on effectful operations, which are both theoretically and practically important.Plotkin and Power [50] show that theories of various effects with suitable equations determine their corresponding monads, and later Hyland et al. [24] show that certain combinations of effect theories are equivalent to monad transformers.Equations are also used for reasoning about programs with algebraic effects and handlers [36,52,73].Possible ways to extend scoped effects with equations include the approach in [31] (Remark 1), the categorical framework of equational systems [14], second order Lawvere theories [15], and syntactic frameworks like [64].

Conclusion
The motivation of this work is to develop a modular approach to the syntax and semantics of scoped operations.We believe our proposal, functorial algebras, is at a sweet spot in the trade-off between structuredness and simplicity, allowing practical examples of scoped operations to be programmed and reasoned about naturally, and implementable in modern functional languages such as Haskell and OCaml.We put our model and two other models for interpreting scoped effects in the same framework of resolutions of the monad modelling syntax.By constructing interpretation-preserving functors between the three kinds of algebras, we showed that they have equivalent expressivity for interpreting scoped effects, although they form non-equivalent categories.The uniform theoretical framework also induces fusion laws of interpretation in a straightforward way.
There are two strains of work that should be pursued from here.The first one would be investigating ways to compose algebras of scoped operations.The second one would be the design of a language supporting handlers of scoped operations natively and its type system and operational semantics.

A Figures
Fn-Alg

B Notation
This section summarises the mathematical notation used in this paper.
Typeface Conventions We use boldface variable such as C and D for abstract categories and typewriter font for specific categories such as Set.Functors and objects are denoted by capitalised letters such as F , K, G, X, A in general, but functors representing signatures of operations are always denoted by Greek letters Γ and Σ, and some concrete functors are denoted by typewriter font such as Free and Fn.Morphisms and natural transformations are denoted by uncapitalised letters such as f , g and h and Greek letters such as α, β and τ .Sometimes we write η RL or ǫ RL to make clear the monad for (co)units.

Categories
For monads, letters P , T , M are used, and µ is used for their multiplication.In particular, P is used for the monad of programs with algebraic and scoped operations, while T is used for the monad from the adjunction Free Fn ↑⊣↓ U Fn .
Functor G denotes the functor encoding the grammar (13), and functor I (15) and Fn (16) are used for defining functorial algebras.Similarly, functors Ix, ⊳ and ⊲ are used for defining indexed algebras (Section 4.2).
In some of the examples in the paper, we make use of the functor List : Set → Set on Set that sends every set X to the set of finite lists with elements from X.We use xs + +ys for list concatenation and cons x xs for a list with x in front of xs.
Comparison functors (Section 4) are denoted by K, and in particular K EM Fn and K Fn Ix .

Objects and Morphisms
We write f • g for vertical composition of morphisms, and F • G for horizontal composition of natural transformations and composition of functors.Products in a category are denoted by X 1 × • • • × X n for finite products or i∈I X i for some set I, and coproducts are denoted by X 1 + • • • + X n or i∈I X i for some set I. The injection morphisms into coproducts are denoted by ι i : X i → X 1 + • • • + X n for each i, and the morphisms out of coproducts are Initial objects are generally written as 0, and the unique morphism from the initial object to an object X is denoted by !: 0 → X.In particular, the initial object 0 in a functor category is the constant functor mapping to the initial object in the codomain.
The (carrier of the) initial algebra of an endofunctor G : C → C is denoted by µG or µY .GY .The structure map of the initial algebra is denoted by in : G(µG) → G and its inverse is in • .The unique morphism from the initial Galgebra to a G-algebra α : GX → X, i.e. the catamorphism for α, is denoted by α : µG → X.
Given an endofunctor Σ : C → C, the free monad over it is denoted by Σ * .Given a Σ-algebra X : C, α : ΣX → X and a morphism g : A → X, the interpretation morphism is denoted by handle X,α g : Σ * A → X.The notation handle is also used for functorial and indexed algebras.The structure map of the free algebra Σ * A is denoted by op : ΣΣ * A → Σ * A.
Functorial algebras are usually denoted by H, X, α G , α I (Definition 1), where H is an endofunctor on the base category C and X is an object in C.

C Proofs Omitted in the Main Paper
This section contains the proofs omitted in the paper.The following two lemmas are a more detailed version of the standard results Lemma 1.We refer the reader to [2,5] for their proofs.
where [id , α] denotes the catamorphism from the initial algebra µY .X + ΣY to X.Moreover, this adjunction is strictly monadic.
Lemma (2).The endofunctor Fn (16) on Endo f (C) × C has free algebras, i.e. there is a functor Free Fn : Endo Proof.Since C is lfp, it is cocomplete, and it follows that Endo f (C) × C is cocomplete because colimits in functor categories and product categories can be computed pointwise [43,Thm V.3.2].It is also easy verification that Fn preserves all colimits of ω-chains following from the fact that G, Σ and Γ , and all functors in Endo f (C) preserve colimits of ω-chains in their domains respectively.Hence by Lemma 1, there is a functor Free Fn left adjoint to U Fn .

Lemma (3).
There is a natural isomorphism between Free Fn and the following functor ) * X are the structure maps of the free G-algebra and I G * H -algebra respectively.
Proof.By Lemma 11, the free Fn-algebra generated by H, X can be constructed from the initial algebra of a functor Fn H,X : Then we show that an initial Fn H,X -algebra carried by G * H, (I G * H ) * X with structure map To see that this Fn H,X -algebra is initial, consider any C, D in C C × C with structure map j 1 , j 2 : Fn H,X C, D → C, D .We have where we use subscripts of ⌈•⌉ to indicate the B for some ⌈f ⌉ : LA → B.
The calculation shows that the Fn H,X -algebra homomorphism (k 1 , k 2 ) uniquely exists, and thus G * H, (I G * H ) * X with structure map i 1 , i 2 is initial.Then by Lemma 11, this initial algebra gives the free Fn-algebra generated by H, X , and thus we have the isomorphism between Free Fn and Free Fn in the lemma.
This characterisation of free Fn-algebras also allows us to express the unit and counit of the adjunction Free Fn ⊣ U Fn in terms of those of some simpler adjunctions.
Lemma 12. Letting the φ be the isomorphism in Lemma 3, the unit of adjunction Free Fn ⊣ U Fn is Note that this EM algebra is defined with recursion: the part of syntax enclosed by the scope of once is interpreted by a recursive call to handle EM with the algebra itself.

E Handling with Indexed Algebras by Hybrid Fold
This section elaborates Example 12 about using fusion laws and functorial algebra to prove the correctness of a recursive scheme used by Piróg et al. [48] to interpret scoped operations with indexed algebras.Although Piróg et al. [48] propose the adjunction (Free Ix ↾) ⊣ (⇃ U Ix ) for interpreting scoped operations with indexed algebras, they use the recursive function hfold (Figure 4) in their implementation to interpret P (12) with indexed algebras.Compared to a faithful implementation of handle, their hfold is more efficient since it skips transforming P to the free indexed algebra (⇃ U Ix Free Ix ↾) but directly works on P .Thus we call it a hybrid fold since it works on a syntactic structure P that is not freely generated by its type of algebras giving semantics.
While the definition of hfold is computationally intuitive, Piróg et al. [48] did not provide formal justification for this recursive definition.In this subsection, we fill the gap by showing that hfold coincides with handle with indexed algebras.We divide the proof into three parts for clarity: after making the problem precise (Section E.1), we first show that the hfold for an indexed algebra A is equivalently a catamorphism from P in Endo f (C) (Section E.2), which is then a special case of interpreting with functorial algebras (Section E.3), and finally we translate this functorial algebra into the category Ix-Alg of indexed algebras using K Fn Ix , and show that it induces the same interpretation as the one from the indexed algebra A that we start with (Section E.4).

E.1 Semantic Problem of Hybrid Fold
where in • : P → Id + Σ • P + Γ • P • P is the isomorphism between P and GP , and h 1 , h 2 and h 3 correspond to the three cases of hfold respectively: In general, an equation in the form of (33) does not necessarily have a (unique) solution.Thus the semantics of the function hfold is not clear.We settle this problem with the following result.
Theorem 4 (Hybrid Folds Coincide with Interpretation).There exists a unique solution to (33) and it coincides with the interpretation with indexed algebra A at level 0 (26): h 0 = handle A,a,d,p id : P A 0 → A 0 We prove the theorem in the rest of this section with the tools that we have developed.

E.2 Hybrid Fold Is an Adjoint Fold
The first step of our proof is to show the unique existence of the solution to (33) based on the observation that it is an adjoint fold equation [22] with the adjunction between right Kan extension [43] and composition with A. Hinze [22] shows the following theorem stating that adjoint fold equations have a unique solution.
Then by Theorem 5 we have the following result.
Lemma 13 (Unique Existence of Hybrid Fold).The recursive definition h : P A → A (33) of hybrid folds (Figure 4) has a unique solution: where ǫ A : (Ran A A) • A → A is the counit, and ⌊Φ Ran A A (ǫ A )⌋ is the catamorphism from the initial G-algebra P (12) to the G-algebra carried by Ran A A with structure map ⌊Φ Ran A A (ǫ A )⌋.

E.3 Catamorphism as Interpretation
We have shown that hfold A for all indexed algebras A uniquely exists, and now to show its coincidence with handle A (the second part of Theorem 4), we show that the hybrid fold coincides with handle with some functorial algebra, and then use the comparison functor K Fn Ix (Section 4.5) to translate this functorial algebra into an indexed algebra.
The following lemma relating G-Alg and Fn-Alg is straightforward.
Now consider the G-algebra in Lemma 13 carried by Ran A A with structure map α G = ⌊Φ RanAA (ǫ A )⌋, we can trivially make it a functorial algebra α by Lemma 14, hoping for applications of the fusion law of functorial algebras (5.1): The catamorphism to α G and handle α are related by α G A0 = handle α (α G A0 • ι 1 ), which can be shown by checking that the formula (20) for computing handle α is exactly the defining equation of the catamorphism α G A0 .Plugging the identity into (36), we obtain Now we have made some progress because the right-hand side takes exactly the form for applications of the fusion law-an interpretation followed by a morphism (ǫ A ) 0 : (Ran A A)A 0 → A 0 .In order to apply the fusion law, we need to make (ǫ A ) 0 : (Ran A A)A 0 → A 0 a functorial algebra homomorphism from α (37).With some exploration, we can handle :: (Σ b → b) → (a → b) → Free Σ a → b handle alg g (Return x ) = g x handle alg g (Call op) = alg (fmap (handle alg g) op) Thus for an algebraic operation o, (o p) > > = k and o (p > > = k ) have the same representation, whereas for a scoped operation s, (s p) > > = k and s (p > > = k ) have different representations, which is precisely the distinction between algebraic and scoped operations.The constructors Call and Enter are clumsy to work with, and for writing programs more naturally, we define smart constructors for operations.Generally, for algebraic operations Op :: F x → Σ x and scoped operations Sc :: G x → Γ x , the smart constructors are op :: F (Prog Σ Γ a) → Prog Σ Γ a sc :: G (Prog Σ Γ a) → Prog Σ Γ a op = Call • Op sc = Enter • fmap (fmap return) • Sc For example, the smart constructor for Catch (Example 1) is catch :: Prog Σ Catch a → Prog Σ Catch a → Prog Σ Catch a catch h r = Enter (Catch (fmap return h) (fmap return r ))

Example 3 .
Fig. 1: A Haskell implementation of handling with functorial algebras

Lemma 2 .
The endofunctor Fn(16) on Endo f (C) × C has free algebras, i.e. there is a functor Free Fn :Endo f (C) × C → Fn-Alg left adjoint to the forgetful functor U Fn : Fn-Alg → Endo f (C) × C.Proof sketch.It can be verified that Fn is finitary and then we apply Lemma 1.A detailed proof can be found in Appendix C.These two adjunctions are depicted in the following diagram: D g = R(ǫ D • Lg) : P A = RLA → RD.Crucially, we show that interpretations are preserved by comparison functors.Lemma 5 (Preservation of Interpretation).Let K : D → D ′ be any comparison functor between resolutions D, L, R, η, ǫ and D ′ , L ′ , R ′ , η ′ , ǫ ′ of some monad M : C → C. For any object D in D and any g : A → RD in C,

Fig. 2 :Fig. 3 : 1 dataFig. 4 :
Fig. 2: The resolutions of functorial, indexed and Eilenberg-Moore algebras For any two categories C and D, we write C × D for their product category, whose objects are denoted by X, Y where X : C and Y : D and morphisms are also denoted by f , g where f : X → X ′ and g : Y → Y ′ .The functor category from C to D is denoted by D C .Specifically, the category of finitary endofunctors on C is denoted by Endo f (C).Given a monad P : C → C, the category of Eilenberg-Moore algebras is denoted by C P .Given an endofunctor F on C, the category of F -algebras is F -Alg.Functors An adjoint pair of functors L : C → D and R : D → C is denoted by L ⊣ R where L is the left adjoint.The associated unit of the adjunction is η : Id → RL and the counit is ǫ : LR → Id, where Id is the identity functor.

Fix
an indexed algebra carried by A : C |N| in this section:A : C |N| , a : Σ • A → A, d : Γ •(⊳A) → A, p : A → (⊳A)(32)For notational convenience, we define a functor S :|N| → |N| such that Sn = n + 1, then ⊳A = A • S since (⊳A) n = A n+1 = A(Sn).With functor S, we can view p and d as p : A → A • S and d : Γ • A • S → A. Then the recursive definition of hfold in Figure 4 can be understood as a morphism h : P • A → A in C |N| satisfying the equation Given any adjunction L ⊣ R : D → C, an endofunctor G : D → D whose initial algebra µG, in exists, and a natural transformation Φ : C(L−, B) → C(LD−, B) for some B : C, then there exists a unique x : L(µG) → B satisfying x = Φ µG (x) • Lin • (34) and the unique solution satisfies ⌊x⌋ = ⌊Φ RB (ǫ B )⌋ where ⌊•⌋ : C(LD, C) → D(D, RC) is the isomorphism for the adjunction L ⊣ R.Since h : P A → A and P = µG : Endo f (C), to apply this theorem to (33), we only need to (i) make (−• A) : Endo f (C) → C |N| a left adjoint and (ii) make [h 1 , h 2 , h 3 ] in(33) an instance of Φ P (h) for some natural transformation Φ:-For (i), the functor − • A is left adjoint to the right Kan extension along A, that is a functor Ran A : C |N| → Endo f (C), which always exists when C is lfp.-For (ii), we define a natural transformation Φ :C |N| (− • A, A) → C |N| ((G−) • A, A)such that for all H : Endo f (C) and f ∈ C |N| (H • A, A),Φ H (f ) = [f 1 , f 2 , f 3 ] •(in • • A) handle ealg (BaseAlg callE enterE ) returnE Applying handleE excE to the following program produces Just 43 as expected.
Together with the usual algebraic operations get and put of state, Local can be interpreted with a functorial algebra carried by the state monad type State s a = s → (s, a).The essential part of the functorial algebra is the following enterE for Local (complete code in the supplementary material): [33] once (or (return 1) (return 3)); or (return n) (return (n + 1))} Example 5.In the last example we used the list functor to interpret explicit nondeterminism, resulting in the depth-first search (DFS) strategy for searching.Noted by Spivey[61], other search strategies can be implemented by other choices of functors.For example, depth-bounded search (DBS) can be implemented with the functor Int → [a ], and breadth-first search (BFS) can be implemented with the functor [[a ]] (or Kidney and Wu[33]'s more efficient LevelT functor).A powerful application of scoped effects is modelling search strategies:data Strategy x = DFS x | BFS x | DBSInt x so that the programmer can freely specify the search strategy of nondeterministic choices in a scope.The algebraic signature Choice and scoped signature Strategy can be handled by a functorial algebra carried by the endofunctor ([a ], [[a ]], Int → [a ]) and a base type [a ] (assuming that depth-first search is the default strategy).The complete code is in the supplementary material.Example 6.A scoped operation for the effect of mutable state is the operation local s p that executes the program p with a state s and restores to the original state after p finishes.Thus (local s p; k ) is different from local s (p; k ), and local should be modelled as a scoped operations of signature data Local s a = Local s a.