Semantics for Local Computational Effects

17
Semantics for Local Computational Effects John Power 1 ,2 Laboratory for the Foundations of Computer Science University of Edinburgh King’s Buildings, Edinburgh, EH9 3JZ, Scotland Abstract Starting with Moggi’s work on monads as refined to Lawvere theories, we give a general construct that extends denotational semantics for a global computational effect canonically to yield deno- tational semantics for a corresponding local computational effect. Our leading example yields a construction of the usual denotational semantics for local state from that for global state. Given any Lawvere theory L, possibly countable and possibly enriched, we first give a universal construction that extends L, hence the global operations and equations of a given effect, to incorporate worlds of arbitrary finite size. Then, making delicate use of the final comodel of the ordinary Lawvere theory L, we give a construct that uniformly allows us to model block, the universality of the final comodel yielding a universal property of the construct. We illustrate both the universal extension of L and the canonical construction of block by seeing how they work in the case of state. Keywords: Lawvere theory, indexed Lawvere theory, model, monad, global state, local state. 1 Introduction Over recent years, largely in collaboration with Martin Hyland and especially Gordon Plotkin, I have been working on the semantics of computational ef- fects, refining Eugenio Moggi’s proposal to use the notion of strong monad: for an overview, see [12], and for the most recent journal paper, see [1]. The central idea has been to refine Moggi’s use of monads [6,7] to the use of count- able, possibly enriched, Lawvere theories: see [14] for a recent exposition of 1 I acknowledge the support of EPSRC grant GR/586372/01: A Theory of Effects for Programming Languages and travel funding from the Japan Science and Technology Agency to visit AIST, Senri-chuo 2 Email: [email protected] Electronic Notes in Theoretical Computer Science 158 (2006) 355–371 1571-0661/$ – see front matter © 2006 Elsevier B.V. All rights reserved. www.elsevier.com/locate/entcs doi:10.1016/j.entcs.2006.04.018

Transcript of Semantics for Local Computational Effects

Semantics for Local Computational Effects

John Power1 ,2

Laboratory for the Foundations of Computer ScienceUniversity of Edinburgh

King’s Buildings, Edinburgh, EH9 3JZ, Scotland

Abstract

Starting with Moggi’s work on monads as refined to Lawvere theories, we give a general constructthat extends denotational semantics for a global computational effect canonically to yield deno-tational semantics for a corresponding local computational effect. Our leading example yields aconstruction of the usual denotational semantics for local state from that for global state. Given anyLawvere theory L, possibly countable and possibly enriched, we first give a universal constructionthat extends L, hence the global operations and equations of a given effect, to incorporate worldsof arbitrary finite size. Then, making delicate use of the final comodel of the ordinary Lawveretheory L, we give a construct that uniformly allows us to model block, the universality of the finalcomodel yielding a universal property of the construct. We illustrate both the universal extensionof L and the canonical construction of block by seeing how they work in the case of state.

Keywords: Lawvere theory, indexed Lawvere theory, model, monad, global state, local state.

1 Introduction

Over recent years, largely in collaboration with Martin Hyland and especiallyGordon Plotkin, I have been working on the semantics of computational ef-fects, refining Eugenio Moggi’s proposal to use the notion of strong monad:for an overview, see [12], and for the most recent journal paper, see [1]. Thecentral idea has been to refine Moggi’s use of monads [6,7] to the use of count-able, possibly enriched, Lawvere theories: see [14] for a recent exposition of

1 I acknowledge the support of EPSRC grant GR/586372/01: A Theory of Effects forProgramming Languages and travel funding from the Japan Science and Technology Agencyto visit AIST, Senri-chuo2 Email: [email protected]

Electronic Notes in Theoretical Computer Science 158 (2006) 355–371

1571-0661/$ – see front matter © 2006 Elsevier B.V. All rights reserved.

www.elsevier.com/locate/entcs

doi:10.1016/j.entcs.2006.04.018

the relationship between the two notions, with examples drawn from compu-tational effects. The central achievement of the work has been to allow anelegant denotational semantic account of the various ways in which individualeffects naturally combine in programming languages like ML [1], the two mainsuch ways being by the sum of Lawvere theories and by their tensor product.

We have successfully investigated other natural questions that arise in thestudy of computational effects too, including questions of operational seman-tics [9] and logics associated with effects [13]. But one question on which wehave made a start but not yet fully resolved is that of giving denotationalsemantics for the process of extending from global computational effects tolocal computational effects [10,11]. That is the question we address here.

In order to model the usual global computational effects, with leading ex-ample here being that of global state, one typically starts with a base categorysuch as Set or ωCpo, then, using either a monad or a countable Lawvere the-ory, extends the category to model the effect at hand. For instance, the monadon Set for global state is TS− = (−×S)S , where S is a set of states, typicallygiven by V Loc for a countable set V of values and a finite set Loc of locations.One can then canonically model the computational λ-calculus together withoperations for global state in the Kleisli category Kl(TS).

Expressing this in terms of Lawvere theories, the countable Lawvere the-ory LS generating TS is the theory freely generated by operations lookup :V −→ Loc and update : 1 −→ Loc × V subject to several computationallynatural equations listed in [10]. Observe that the concept of Lawvere theoryis purely syntactic, in that, unlike the monad, it does not depend upon ana priori choice of base category. One can duly model the computational λ-calculus together with operations for global state in a canonically determinedsubcategory of Mod(LS , Set) [15]. And this is representative of the situationfor all computational effects other than continuations.

In order to extend from global to local effects, one typically replaces thebase category, typically Set or ωCpo, for the monad for global state, by thefunctor category [Inj, Set] or [Inj, ωCpo], where Inj is the category of finitesets and injections [8,10,11], or equivalently, the category of natural numbersand monomorphisms. The monad for local state, on [Inj, Set], is

(TLSX)n = (

mε(n/I)∫(Sm × Xm))Sn

where∫

denotes a coend, a sophisticated kind of colimit given by a universaldinatural map [2,5], and where Sn = V n for a given set of values V . Theidea is that in a state with n locations, a computation can create m − n newlocations and return a value, e.g., a function, that depends on them, cf [3,4].

J. Power / Electronic Notes in Theoretical Computer Science 158 (2006) 355–371356

In the case V = 1 this reduces to the monad for local names in [17].

In [10], with an oversight corrected in [11], we gave a sort of algebraicsignature and set of equations that generated the monad TLS for local state.Importantly for us, the signature and equations extended those for globalstate. But it had two drawbacks. First, it used a combination of cartesian andsymmetric monoidal structure on [Inj, Set], so was neither a Lawvere theorynor a symmetric monoidal Lawvere theory, but rather an uncomfortable anduninvestigated hybrid. And second, it applied only to state: the axioms relatedblock with lookup and update, but with no general construct showing how tocombine block with an arbitrary global signature, then instantiating that inthe case of global state. So here, we refine the analysis of [10,11] by giving aconstruction that extends any Lawvere theory to account for localness, doingso without using a hybrid of cartesian and symmetric monoidal structure, and,as best we can, exhibiting universality of the construct. The process uncoverssophisticated denotational structure underlying localness.

In order to extend global structure to local structure, one must first extenda Lawvere theory L to an indexed version of the same theory, i.e., if L has anoperation f : a −→ b, then a local version also needs to have such an operation,but it must have it at each world, and it must be modified to account for thesize of the world. For instance, for state, if one has n locations, there are npossible places at which one may either lookup or update. So we first seek ageneral construct that extends the operations of a global computational effectto become operations of an induced local effect, and that construct mustbe sensitive to the size of each world. The canonical symmetric monoidalstructure of Law allows us to do that: Inj is the free symmetric monoidalcategory on 1 for which the unit is the initial object; Law is another symmetricmonoidal category with unit the initial object; and so the universal propertyallows us to extend any Lawvere theory to an Inj-indexed such theory, thusuniformly allowing us to extend global operations to local operations. Thedetails are in Sections 2 and 3.

Having extended the operations of a global effect to a local effect, one alsoneeds to add an operation block: the central fact of localness is the capacity tocreate a new world. So we further need a general construct that builds blockon top of any global effect. That is subtle, as one needs a canonical way toextend a model at world n to a model at world n + 1 with the new variableassigned a meaning generated by the global operations. In the case of state,it is given by a map of the form

A(n + 1) −→ A(n)V

uniformly in n and subject to natural equations. In order to provide sucha construct canonically, we use a universal property of V relative to the

J. Power / Electronic Notes in Theoretical Computer Science 158 (2006) 355–371 357

countable Lawvere theory LV for global state with value set V and one lo-cation: V is the final comodel of LV , i.e., the terminal object of the categoryComod(LV , Set), or, by definition equivalently, the initial object of the cate-gory Mod(LV , Setop). So, homming in with V , the set A(n)V has n+1 lookupand update structures on it, n of them because A(n) has n such structures,with an additional one determined by the universal comodel structure on V .The coherence condition on block asserts that it assigns the (n+1)-st variablethat additional structure on A(n)V , while maintaining the assignments of theearlier variables. That condition, together with a symmetry condition, are allwe need to characterise block, otherwise merely asking that block respect then + 1 global operations. We investigate the comodel structure in Section 4,the block structure in Section 5, and our leading example of state in Section 6.

2 Structure of the Category of Lawvere Theories

Two symmetric monoidal structures on the category Law of Lawvere theo-ries, possibly countable and enriched, were thoroughly investigated in [1] inanalysing the most usual combinations of computational effects: sum and ten-sor. It is the latter that is of primary interest in this paper, although much ofour formal development also holds for the former.

We first recall the definitions of Lawvere theory, map of Lawvere theories,and model of a Lawvere theory. Note that the category Nat of natural numbersand all maps between them has strictly associative finite coproducts, given bythe sum of natural numbers; and consequently the category Natop has strictlyassociative finite products.

Definition 2.1 A Lawvere theory consists of a small category L with strictlyassociative finite products and an identity-on-objects strict finite-product pre-serving functor

J : Natop −→ L

The Lawvere theory is typically denoted merely by L. A map of Lawveretheories from L to L′ is a functor from L to L′ that commutes with thefunctors from Natop: it necessarily strictly preserves finite products. A modelof L in any category C with finite products is a finite product preservingfunctor M : L −→ C.

Observe that the definition forces the objects of a Lawvere theory L tobe exactly the natural numbers. Observe also that the definition of modelspecifically does not require strict preservation of finite products. Finally,note that for any Lawvere theory L, the models of L together with all naturaltransformations forms a category Mod(L, C). If C is locally presentable, the

J. Power / Electronic Notes in Theoretical Computer Science 158 (2006) 355–371358

functor

ev1 : Mod(L, C) −→ C

has a left adjoint, yielding a monad T on C. The category Mod(L, C) is thencoherently equivalent to the category T -Alg. Taking C to be Set, the monadsthus arising from Lawvere theories are exactly the finitary monads on Set.

For this paper, our leading example of a countable Lawvere theory is thatfor state as follows [10]:

Example 2.2 Let V be a countable set of values, and let Loc be a finite setof locations. Put S = V Loc, so S is the set of states with Loc locations takingvalues in V . The countable Lawvere theory LS for state is freely generatedby operations l : V −→ Loc and u : 1 −→ Loc × V subject to equationsexpressible in terms of an equational theory as follows, where loc is allowed torun over elements of Loc and v is allowed to run over elements of V :

(i) lloc(uloc,v(x))v = x

(ii) lloc(lloc(tvv′)v)v′ = lloc(tvv)v

(iii) uloc,v(uloc,v′(x)) = uloc,v′(x)

(iv) uloc,v(lloc(tv′)v′) = uloc,v(tv)

(v) lloc(lloc′(tvv′)v′)v = lloc′(lloc(tvv′)v)v′ where loc �= loc′

(vi) uloc,v(uloc′,v′(x)) = uloc′,v′(uloc,v(x)) where loc �= loc′

(vii) uloc,v(lloc′(tv′)v′) = lloc′(uloc,v(tv′))v′ where loc �= loc′.

These equations can equally be expressed in diagrammatic form [10]. Thefirst four express the interactions between the lookup and update operations,while the final three assert independence of the operations when they rely upondifferent locations. The idea is that the Lawvere theory is simply generated byoperations for lookup and update, subject to natural computational equationsbetween derived operations. A detailed discussion of the situation is givenin [10]. The forgetful functor from Mod(LS, Set) to Set induces the monadTS− = (− × S)S for global state on Set. So, as explained in [10], this tellsus that if one models lookup and update subject to natural computationalequations, one derives the monad for global state, rather than needing to takethe latter as a primitive.

The tensor product of Lawvere theories takes a pair of Lawvere theories Land L′, takes all the operations and equations of each, and insists that eachoperation of L commutes with each operation of L′. The tensor product ishow global state is combined with almost all other computational effects. Itis studied in detail in [1] and is characterised as follows [1]:

J. Power / Electronic Notes in Theoretical Computer Science 158 (2006) 355–371 359

Theorem 2.3 Given Lawvere theories L and L′, the tensor product L ⊗ L′

induces an equivalence of categories

Mod(L ⊗ L′, Set) � Mod(L, Mod(L′, Set))

The tensor forms a symmetric monoidal structure on Law, with unit givenby the initial object of Law, which is Natop together with the identity functor.The same remarks apply trivially to Law together with binary coproduct.Moreover, one may replace finiteness by countability routinely in the above,and one may also enrich routinely [1]. We denote the n-fold tensor product ofa Lawvere theory L with itself by L(n).

The example of the tensor product of primary interest to us here is on thecategory Lawc of countable Lawvere theories and is as follows [1]:

Theorem 2.4 Let LS be the countable Lawvere theory for global state set S,and let LS′ be the countable Lawvere theory for global state set S ′. Then thetensor product LS ⊗ LS′ is the countable Lawvere theory for global state setS × S ′.

Corollary 2.5 Let LV be the countable Lawvere theory for global state V ,understood to be a set of values V with one location. Then the n-fold ten-sor product L

(n)V is the countable Lawvere theory for global state V n, i.e., the

Lawvere theory for global state with value set V and n locations.

So in particular, the n-fold tensor product L(n)V of LV , the Lawvere theory

for global state for a countable set V of values and one location, is preciselywhat one needs to study when one has n worlds in order to account for theoperations of global state, namely lookup and update. One must later addblock, together with its coherence relationships with lookup and update, asadditional structure.

3 Universal Structure of the Category of Natural Num-

bers and Injections

The category Inj of natural numbers and monomorphisms, equivalently thecategory of finite sets and injections, does not have finite coproducts. Inparticular, because of the absence of codiagonals, the sum of two naturalnumbers does not act as their binary coproduct. But Inj does have an initialobject and, together with the sum of natural numbers, that equips Inj witha symmetric monoidal closed structure for which the unit is the initial object.It is elementary to verify that it is the free symmetric monoidal category on1 with unit the initial object.

J. Power / Electronic Notes in Theoretical Computer Science 158 (2006) 355–371360

In Section 2, we saw that the tensor product of Lawvere theories, possiblycountable or enriched, makes Law into a symmetric monoidal category withunit the initial object. So we can use the universal property in the light ofCorollary 2.5. The universal property allows us, starting with an arbitraryLawvere theory L, to generate a functor

Inj −→ Law

We can generalise the latter data to consider, for any small category D, afunctor

D −→ Law

regard that notion as a generalised form of Lawvere theory, analyse it assuch, then restrict again to the particular case of Inj to see whether we canderive some account of a local version of a computational effect from a globalcomputational effect. It does not account for block, which must be doneseparately, but it does uniformly account for that global structure that mustpersist to the local level. This outline motivates the body of theory of thissection.

Definition 3.1 For any small category D, a D-indexed Lawvere theory is afunctor L : D −→ Law, i.e., a D-shaped diagram in the category Law. Amodel M of a D-indexed Lawvere theory L in a category C with finite productsconsists of, for every object d of D, a model

Md : Ld −→ C

of Ld in C, together with, for each map f : d −→ d′ in D, a natural transfor-mation

Ld

Md � C

⇓ Mf

Ld′

Lf

Md′� C

Id

respecting the composition and identities of D.The latter means that Mid isthe identity and, given maps f : d −→ d′ and f ′ : d′ −→ d′′, the naturaltransformation Mf ′f is given by pasting Mf ′ on to Mf .

It is evident, with a little thought, how to extend the set of models of aD-indexed Lawvere theory L in a category C with finite products to give acategory Mod(L, C): the arrows are D-indexed families of natural transfor-mations that are coherent with respect to the Mf ’s. It is routine to extend the

J. Power / Electronic Notes in Theoretical Computer Science 158 (2006) 355–371 361

definition and the following analysis of indexed Lawvere theories to accountfor countability and for enrichment, e.g., in the category ωCpo.

For any D-indexed Lawvere theory L and category C with finite products,there is a forgetful functor

U : Mod(L, C) −→ [D, C]

taking a model M to the functor sending d to Md(1).

Proposition 3.2 For any D-indexed Lawvere theory L and any locally pre-sentable category C, the forgetful functor U : Mod(L, C) −→ [D, C] has a leftadjoint F given pointwise, i.e., for any functor X : D −→ C,

(FX)d = FdXd

where Fd � Ud : Mod(Ld) −→ C is the ordinary adjunction induced by theordinary Lawvere theory Ld. Moreover, the adjunction exhibits Mod(L, C) asmonadic over [D, C].

Proof. It follows from the study of ordinary Lawvere theories that an adjointFd exists for each d. One can routinely verify that such adjoints collectivelysatisfy the adjointness property for the forgetful functor U . Monadicity can beproved in several ways, one such proof being by Beck’s monadicity theorem.�

Our leading class of examples of D-indexed Lawvere theories has D = Injand arises as follows:

Definition 3.3 Given any ordinary Lawvere theory L, denote by L⊗ the Inj-indexed Lawvere theory freely generated by the tensor structure of Law andthe fact of Inj being the free symmetric monoidal category on 1 with unit theinitial object. So (L⊗)n = L(n).

Applying Proposition 3.2 to the Inj-indexed Lawvere theories that thusarise allows us to conclude the following:

Corollary 3.4 Given any ordinary Lawvere theory L and any locally pre-sentable category C, let T be the monad on C induced by the forgetful functor

Mod(L, C) −→ C

Then, the forgetful functor

Mod(L⊗, C) −→ [Inj, C]

exhibits Mod(L⊗, C) as monadic over [Inj, C] with monad T⊗ given by

(T⊗X)n = T (n)Xn

where T (n) is the monad on C induced by L(n).

Consider the significance of Corollary 3.4 in the example of global state.Recall from Corollary 2.5 that if LV is the countable Lawvere theory for global

J. Power / Electronic Notes in Theoretical Computer Science 158 (2006) 355–371362

state with value set V and one location, (LV )⊗ sends a natural number n tothe Lawvere theory LV n , i.e., the Lawvere theory for global state with value setV and n locations. Our leading example of an Inj-indexed Lawvere theory isthus generated by global state, and its category of models allows us to extendand reformulate Example 2.2 as follows:

Example 3.5 The category [Inj, Set] is a cartesian closed category. Given aset V of values, let V also denote the constant functor from Inj to Set at theobject V of Set. Let Loc denote the object of [Inj, Set] given by the inclusionof Inj into Set. In [10], using these conventions, we gave an enriched version ofExample 2.2, with enrichment in the cartesian closed category [Inj, Set]. Onefreely has operations l : V −→ Loc and u : 1 −→ Loc× V subject to enrichedversions of the equations listed in Example 2.2. Denote this [Inj, Set]-enrichedcountable Lawvere theory by LS. Then the category Mod(LS, [Inj, Set]) ofmodels of LS in [Inj, Set] is equivalent to the category Mod((LV )⊗, Set). ByCorollary 3.4, the induced monad T on [Inj, Set] is given by

(TX)n = TV nXn

i..e, the value of TX at n is given by applying the monad for global statewith value set V and n locations to the set Xn. Although they are equivalentcategories, it is considerably easier to calculate with Mod((LV )⊗, Set) thanwith Mod(LS , [Inj, Set]) as done in [10]. For an object of Mod((LV )⊗, Set)consists of a model Mn of L(n) for each n such that the maps in Inj are sent tostructure-respecting functions. In contrast, calculation with Mod(LS, [Inj, Set])involves the exponential in [Inj, Set], which is complicated.

The significance of this example is that it includes all operations and allequations that extend from global state as modelled in Set to [Inj, Set]. Theonly other operation one needs in order to model local state is a block oper-ation subject to two sorts of equations: two equations only involving block,and equations relating block with the operations that exist globally. So, tocomplete the analysis of this paper, we add an account of block on a basecategory of the form Mod(L⊗, C).

4 The Final Comodel

In Section 5, we will need to introduce a block operation. In order to do that,we need a general construction that extends an arbitrary model of L(n) to amodel of L(n+1): for block makes an assignment of structure to a new variable.In this section, we investigate a canonical such construction. The constructionrelies upon the notion of comodel of a Lawvere theory, the existence of a finalcomodel, and to exhibit our leading example, a characterisation of the final

J. Power / Electronic Notes in Theoretical Computer Science 158 (2006) 355–371 363

comodel in the setting of global state [16].

Definition 4.1 A comodel of a Lawvere theory L in a category C with finitecoproducts is a model of L in Cop.

Comodels and natural transformations form a category Comod(L, C), witha canonical forgetful functor to C determined by evaluation at 1.

The category of comodels of a Lawvere theory is often much less substantialthan the category of models. For instance, taking the Lawvere theory fornondeterminism [10], the category of models is that of semilattices, while thatof comodels is empty! And the category of comodels for the Lawvere theoryfor global exceptions is simply Set. Such triviality does not extend to globalstate, whose category of comodels yields a category of data structures [16].

We do not yet have a full general account of localness, and it may wellbe that the triviality of the above examples is good rather than bad: whenone passes from global structure to local structure, nondeterminism is usuallyglobal rather than local in nature, and there is a sense in which exceptionscarry little structure with them. Interactive input/output seems to requirea more subtle analysis that extends that of this paper, as there one has twosorts of locations, one for each of the input and the output. So the analysisof this paper is far from complete, but the final comodel does, as we shall see,play a fundamental role in local state, which is the leading example.

If C is locally presentable as a cartesian closed category, as are Set andωCpo, the forgetful functor has a right adjoint, presenting Comod(L, C) ascomonadic over C. In particular, we have the following:

Theorem 4.2 For any Lawvere theory L, the category Comod(L, Set) has aterminal object, the final comodel.

This extends without fuss to countability and enrichment as usual in thispaper. Our leading example, which is also the leading example of [16], is givenby global state:

Example 4.3 Let LS be the countable Lawvere theory for global state, withS = V Loc and where Loc is finite. The induced comonad on Set is given by(−)S × S, and the final comodel is therefore given by S = V Loc. A fortiori,the final comodel of LV in Set is given by V , with structural maps given by

loc = δ : V −→ V × V

and

upd = π2 = t × V : V × V −→ 1 × V ∼= V

In general, given a comodel A : Lop −→ Set of an arbitrary Lawvere theory

J. Power / Electronic Notes in Theoretical Computer Science 158 (2006) 355–371364

L in Set and given an arbitrary set X, the composite

LA � Setop X−

� Set

preserves finite products. Thus XA1 inherits an L-model structure from theL-comodel A. More generally, for any Lawvere theory L′ and any modelM : L′ −→ Set, the composite

LA � Setop M−

� Mod(L′, Set)

preserves finite products. By Theorem 2.3, to give such a finite product pre-serving functor is equivalent to giving a model of L ⊗ L′. This is natural inA, yielding the following:

Proposition 4.4 For any Lawvere theory L and any comodel A of L in Set,and for any Lawvere theory L′, exponentiation (−)A induces a functor

Mod(L′, Set) −→ Mod(L ⊗ L′, Set)

Corollary 4.5 For any Lawvere theory L, let F be the final comodel of L inSet. Then exponentiation (−)F induces a functor

Mod(L(n), Set) −→ Mod(L(n+1), Set)

Moreover, this is natural in n as an object of Inj.

This duly generalises to the countable setting and also to the enrichedsetting. We can duly extend our running example of global state, Example 3.5,as follows:

Example 4.6 Consider a model M of the Inj-indexed Lawvere theory (LV )⊗in Set. Each (Mn1)V canonically possesses the structure of a model of L

(n+1)V :

it inherits n LV -structures from those of Mn1, while the final LV -comodelstructure of V given by Example 4.3 induces a further LV -structure on (Mn1)V .

The significance for us of this example is that blockn will be a map ofL

(n+1)V -models from Mn+1 to MV

n .

5 Introducing Block

We have already foreshadowed block in Section 4. In general, one starts simplywith a Lawvere theory L. By Theorem 4.2, that Lawvere theory L must havea final comodel F . By Corollary 4.5, exponentiation (−)F induces a functor

Mod(L(n), Set) −→ Mod(L(n+1), Set)

J. Power / Electronic Notes in Theoretical Computer Science 158 (2006) 355–371 365

natural in n.

Definition 5.1 Given an arbitrary Lawvere theory L, a block-algebra consistsof a model M of the Inj-indexed Lawvere theory L⊗ in Set, together with anindexed family of maps

blockn : Mn+1 −→ MFn

in Mod(L(n+1), Set), natural with respect to Inj, satisfying the following twoequations:

Mn(1)(Minc)1� Mn+1(1)

(Mn1)F

(blockn)1

(Mn 1) t �

Mn+2

s � Mn+2

MFn+1

blockn+1

�MF

n+1

blockn+1

(MFn )F

blockFn

s� (MF

n )F

blockFn

where the two occurrences of s mean swapping the last two locations andswapping the two copies of type F respectively.

The axioms here are clearly elementary. The first asserts that block onlyaffects the newly created location, while the second asserts that in creatingtwo new locations and assigning values to them, one can do so in either orderproviding one remembers which location is being assigned which value.

One can routinely form a category of block-algebras, the maps being mapsof models of L⊗ that respect the block structure. We denote the category byblock-Alg.

J. Power / Electronic Notes in Theoretical Computer Science 158 (2006) 355–371366

Theorem 5.2 The composite forgetful functor

block − Alg −→ [Inj, Set]

is monadic.

Proof. There are several proofs of this: a block-algebra is, by construction,given by adding operations subject to equations to a model of L⊗, and suchmodels are in turn given by operations and equations on an object of [Inj, Set].Combining these facts, a block-algebra consists of an object of [Inj, Set] to-gether with operations subject to universally defined equations, and so thecategory of such is monadic over [Inj, Set]. �

This result completes the general theory of the paper. In Section 6, wecheck that, if L is the Lawvere theory for global state with value set V and onelocation, it yields the usual monad TLS for local state on [Inj, Set], followingO’Hearn et al [3,4,8,17] and agreeing with the monad of [10,11].

The notion of block here clearly deserves detailed comparison with the stat-ically scoped block mechanism of Algol-like languages, which was a primarymotivation for O’Hearn and Tennent [8]. In particular, it should relate closelyto the subtle kinds of program equivalence investigated by O’Hearn and Ten-nent. But at present, it is not clear to me precisely how to do that: Algol is acall-by-name language, whereas computational effects have been investigatedalmost exclusively in a call-by-value setting, and the precise ways in whichan object S of states appears is quite different. For O’Hearn and Tennent,one can only access it via a construct such as Comm = S → S rather thanbeing allowed to use it as a parameter as is typically done in the work aboutcomputational effects. So for the moment, we leave it as an intriguing avenuefor further investigation.

To end our general analysis, I would just remark that, although I believethis is a clear improvement in understanding the structure of local computa-tional effects relative to the corresponding global effects, this result is not yetas good as I seek. In particular, a major attraction of Lawvere theories relativeto monads is the clearer distinction they allow between syntax and semantics:one can consider the models of a Lawvere theory L in any category with finiteproducts, whereas in order to study a monad, one must first choose a basecategory on which it must lie, typically Set. But in this section, although Lis a Lawvere theory, block is only studied semantically, i.e., in terms of a basecategory of models. So in due course, I hope to refine the analysis of thissection in a way that allows one to consider block independently of a categoryof models. That should be possible, involving a subtle form of gradedness ofa Lawvere theory, but I do not know how to do it yet.

J. Power / Electronic Notes in Theoretical Computer Science 158 (2006) 355–371 367

6 From Global to Local State

In this section, we check that the structures we have developed in the paper,notably block in Section 5, yield the usual monad for local state from theLawvere theory for global state.

The monad for local state, on [Inj, Set], is

(TLSX)n = (

mε(n/I)∫(Sm × Xm))Sn

where∫

denotes a coend, a sophisticated kind of colimit given by a universaldinatural map [2,5], and where Sn = V n for a given set of values V , cf [3,4].If V = 1 it reduces to the monad for local names in [17].

The behaviour of TLS on injective maps f : n −→ n′ is as follows: decom-pose n′ as the sum n+n′′, note that S(p+n′′) = Sp×Sn′′, and use covarianceof X. So the map

(

mε(n/I)∫(Sm × Xm))Sn × Sn × Sn′′ −→

m′′ε((n+n′′)/I)∫(Sm′′ × Xm′′)

evaluates at Sn, then maps the m-th component of the first coend into the(m+n′′)-th component of the second, using the above isomorphism for S andfunctoriality of X.

In [10,11], an algebraic signature generating TLS was given by defining thecorresponding category LS([Inj, Set]) of algebras as follows, subject to somenot entirely trivial rewriting: an algebra consists of

• an object A of [Inj, Set]

• a lookup map with components ln : n × A(n)V −→ A(n)

• an update map with components un : n × A(n) −→ A(n)V

• a block map with components bn : A(n + 1) −→ A(n)V

subject to commutativity of seven interaction diagrams and six commutativitydiagrams. The interaction diagrams consist of the four interaction diagramsfor global state, together with

A(n + 1)V ln+1 · (evn+1 × id)� A(n + 1)

(A(n)V )V

bVn

A(n)δ� A(n)V

bn

J. Power / Electronic Notes in Theoretical Computer Science 158 (2006) 355–371368

A(n + 1)un+1 · (evn+1 × id)� A(n + 1)V

A(n)V

bn

(A(n)t)V� (A(n)V )V

bVn

A(n)A(inc)� A(n + 1)

A(n)V

bn

A(n) t�

The commutation diagrams are those for global state together with

A(n + 2)s� A(n + 2)

A(n + 1)V

bn+1

�A(n + 1)V

bn+1

(A(n)V )V

bVn

s� (A(n)V )V

bVn

and, for k less than or equal to n,

A(n + 1)V ln+1 · (evk × id)� A(n + 1)

(A(n)V )V

bVn

ln · (evk × id)� A(n)V

bn

J. Power / Electronic Notes in Theoretical Computer Science 158 (2006) 355–371 369

A(n + 1)un+1 · (evk × id)� A(n + 1)V

A(n)V

bn

un · (evk × id)� (A(n)V )V

bVn

One can observe directly the correspondence between these axioms andthose we have developed in previous sections: recalling that we refer to ncopies of each of lookup and update rather than one n-parametrised versionof each, we have already accounted for precisely the diagrams arising fromglobal state in Example 3.5. So to give A together with l and u is equivalentto giving a model M of (LV )⊗ in Set. Now recall, from Example 4.3, that Vis the final comodel of LV . So, using Example 4.6, the first two interactionaxioms together with the last two commutation axioms are equivalent to theassertion that bn : Mn+1 −→ MV

n is a map in Mod(L(n+1), Set). The remainingtwo axioms are exactly the axioms for block in Section 5. This, together withthe characterisation of TLS in [10,11], allows us to conclude the following:

Theorem 6.1 Taking L to be the countable Lawvere theory LV for globalstate with value set V and one location, the category block-Alg is equivalentto LS([Inj, Set]) and is thus monadic over [Inj, Set] with monad TLS that forlocal state.

References

[1] Hyland, J.M.E., G.D. Plotkin and John Power, Combining Effects: Sum and Tensor,Theoretical Computer Science (to appear).

[2] Kelly, G.M., “Basic Concepts of Enriched Category Theory,” Cambridge University Press,Cambridge, 1982.

[3] Levy, P.B., Call-by-Push-Value: A Subsuming Paradigm, “Proc. TLCA ’99,” Lecture Notes inComputer Science 1581 (1999) 228–242.

[4] Levy, P.B., “Call-by-push-value. A Functional/Imperative Synthesis,” Semantic Structures inComputation, Springer, Berlin, 2004.

[5] Mac Lane, S., “Categories for the Working Mathematician,” Springer, Berlin, 1971.

[6] Moggi, E., Computational Lambda-Calculus and Monads, Proc. LICS ’89, IEEE Press (1989),14–23.

[7] Moggi, E., Notions of computation and monads, Information and Computation 93 (1991),55–92.

J. Power / Electronic Notes in Theoretical Computer Science 158 (2006) 355–371370

[8] O’Hearn, P.W., and R.D. Tennent, “Algol-like Languages,” Progress in Theoretical ComputerScience, Birkhauser, Boston, 1997.

[9] Plotkin, G.D., and John Power, Adequacy for Algebraic Effects, “Proc. FOSSACS 2001,”Lecture Notes in Computer Science 2030 (2001), 1–24.

[10] Plotkin, G.D., and John Power, Notions of Computation Determine Monads, “Proc. FOSSACS2002,” Lecture Notes in Computer Science 2303 (2002) 342–356.

[11] Plotkin, G.D., and John Power, Erratum, See the webpagehttp://homepages.inf.ed.ac.uk/gdp/publications/ .

[12] Plotkin, G.D., and John Power, Computational Effects and Operations: an Overview, Proc.Domains 2002, Electronic Notes in Theoretical Computer Science 73 (2002) 149–163.

[13] Plotkin, G.D., and John Power, Logic for Computational Effects: work in progress,Proc. International Workshop on Formal Methods 03, British Computer Society ElectronicWorkshops in Computing (2003).

[14] Power, John, Countable Lawvere Theories and Computational Effects, Proc. MFCSIT 04,Electronic Notes in Theoretical Computer Science (to appear).

[15] Power, John, Generic Models for Computational Effects, Theoretical Computer Science (toappear).

[16] Power, John, and Olha Shkaravska, From Models to Comodels: State and Arrays, Proc. CMCS2004, Electronic Notes in Theoretical Computer Science 100 (2004) 185–200.

[17] Stark, I., Categorical Models for Local Names, Lisp and Symbolic Computation 9 (1996) 77–107.

J. Power / Electronic Notes in Theoretical Computer Science 158 (2006) 355–371 371