Available online at www.sciencedirect.com

ScienceDirect

Electronic Notes in Theoretical Computer Science 203 (2008) 263-284

www.elsevier.com/locate/entcs

Comonadic Notions of Computation

Tarmo Uustalu1

Institute of Cybernetics at Tallinn University of Technology, Akadeemia tee 21, EE-12618 Tallinn, Estonia

Varmo Vene2

Dept. of Computer Science, University of Tartu, J. Liivi 2, EE-50409 Tartu, Estonia

Abstract

We argue that symmetric (semi)monoidal comonads provide a means to structure context-dependent notions of computation such as notions of dataflow computation (computation on streams) and of tree relabelling as in attribute evaluation. We propose a generic semantics for extensions of simply typed lambda calculus with context-dependent operations analogous to the Moggi-style semantics for effectful languages based on strong monads. This continues the work in the early 90s by Brookes, Geva and Van Stone on the use of computational comonads in intensional semantics.

Keywords: context-dependent computation, dataflow computation, tree transformations, symmetric monoidal comonads, coKleisli semantics

1 Introduction

Since the seminal work by Moggi in the late 80s [25], monads, more precisely, strong monads, have become a generally accepted tool for structuring effectful notions of computation, such as computation with exceptions, output, computation using an environment, state-transforming, nondeterministic and probabilistic computation etc. The idea is to use a Kleisli category as the category of impure, effectful functions, with the Kleisli inclusion giving an embedding of the pure functions from the base category. Although finer and coarser accounts of effects based on Lawvere theories [27] (this reference is only the first in a series of papers; for more recent presentations, see [28,18]) and arrows/Freyd categories [17,30] also exist, the monadic approach remains central and best known. In particular, monads are part of the standard libraries of Haskell.

1 tarmo@cs.ioc.ee

2 varmo@cs.ut.ee

1571-0661/$ - see front matter © 2008 Elsevier B.V. All rights reserved. doi:10.1016/j.entcs.2008.05.029

But monads do not capture all meaningful kinds of impure computation. In particular, they exclude some natural notions where, instead of producing effects, computations consume something beyond just values ("contexts" of values). Hence it is natural to ask whether comonads, likely with some additional structure (as strength for monads), can deliver a solution for such cases. (Combinations of monads and comonads via distributive laws can then hopefully account for notions of computation that are both effectful and context-dependent.) That this may well be so was hinted very early on by Brookes et al. [8,9] who demonstrated that what they called computational comonads can be used to make denotational semantics inten-sional. While that work was not directly involved with general context-dependent computation, intensional semantics is a form of impure instrumentation of denota-tional semantics that fits well into this idiom.

In functional programming, Kieburtz [20] was first to advocate comonads as tools for structuring context-dependent computations and gave some interesting examples. The specific application of comonads to environment-passing computation or implicit parameters has been discussed by Lewis et al. [22].

In this paper, we proceed directly from the motivation to treat some important notions of context-dependent computation, namely notions of dataflow computation (stream-based computation) and notions of computation on trees such as tree relabellings in attribute evaluation. We demonstrate that a rather elegant framework for working with these notions of computation is given by symmetric (semi)monoidal comonads. Reassuringly, strong monads appear associated with symmetric monoidal comonads also in works on the categorical semantics of intu-itionistic linear and modal logic [4,7]. We describe some aspects of the structure of coKleisli categories corresponding to symmetric (semi)monoidal comonads and describe then a general interpretation of languages for context-dependent computation into such categories.

We have previously described our proposal at work on language processors for dataflow computation [34] and attribute evaluation [35] implemented in Haskell. In this paper, written with a different slant, we look into the underlying theory, concentrating on the issue of the most appropriate additional structure for comonads.

The organization of the paper is the following. First, we present a compressed recap of strong monads, their Kleisli categories and the semantics of effectful languages a la Moggi. Then we develop our analogous account of context-dependent computation based on coKleisli categories of symmetric (semi)monoidal comonads. We emphasize the important differences resulting from the fact that, despite dualizing from monads to comonads, we are still interested in transferring as much of a given Cartesian closed structure (possibly with coproducts and a uniform parameterized fixpoint operation) as possible. Finally, we briefly comment on the relation to computational comonads and some important advanced issues that we intend to treat in due detail elsewhere.

We assume that the reader knows symmetric monoidal closed and Cartesian closed categories and the categorical semantics of simply typed lambda calculus. We reproduce some basics about monads, comonads, strong functors/monads and

symmetric monoidal functors/comonads.

2 Monads and effectful computation

We begin by a schematic review of the monad-based approach to effectful computation. The purpose is to recall the central ideas, the technical machinery and the big "scheme of things" of this case, so we can establish a standard of what we want to achieve in the case of comonads and context-dependent computation.

2.1 Monads

The starting-point in the monadic approach to (call-by-value) effectful computation is the idea that impure, effectful functions from A to B must be nothing else than pure functions from A to TB. Here pure functions live in a base category C and T is an endofunctor on C that describes the notion of effect of interest; it is useful to think of TA as the type of effectful computations of values of a given type A.

For this to work, impure functions must have identities and compose. Therefore T cannot merely be a functor, but must be a monad.

A monad on a category C is given by a functor T : C — C (the underlying functor), two natural transformations n : Idc — T (the unit) and f : TT — T (the multiplication) satisfying the conditions

TATTA TTTA^^ TTA tva\ ^^ ta \»a

TTA^^ TA TTATA

This definition says that (T, n, f) is a monoid in the endofunctor category [C, C ] wrt. its (Idc, *) monoidal structure.

A monad T on a category C induces a category Kl(T) called the Kleisli category of T defined by

• an object is an object of C,

• a map of from A to B is a map of C from A to TB,

• id^ =df A -— TA,

• if k : A —T B, £ : B —T C, then t oT k =df A TB TC where t =df TB TTC -— TC.

Note that it is the unit n and multiplication f that make the Kleisli identity idT and composition oT possible; the laws of the identity and composition follow from those of n and f.

From C there is an identity-on-objects inclusion functor J to Kl(T), defined on maps by: if f : A — B, then Jf =df A -— B -— TB = A -— TA -— TB.

(It is truly an inclusion, if n is mono, but we ignore this.) It has a right adjoint U : Kl(T) ^ C given by: UA =df TA and, if k : A ^T B, then Uk =df TA TB.

In our application, we use the Kleisli category Kl(T) as the category of effectful functions and J as an embedding from the category of pure functions C. Accordingly, for us, the function va = JidA : A u TA is the pure identity function on A turned into a trivially effectful function. Jf : A u TB is a general pure function f : A u B viewed as trivially effectful. ha = id^A : TTA u TA "flattens" an effectful computation of an effectful computation. k* : TA u TB is an effectful function k : A u TB extended into one that can input an effectful computation.

Well-known examples of monads on Cartesian categories 3 (optionally with co-products, optionally closed), e.g., Set, include the following. The exceptions monad is given by

• TA =df A + E where E is some object (of exceptions),

• Va =df A —U A + E,

• Ha =df (A + E) + E A + E

and is used for computation with exceptions. Properly impure functions are possible thanks to the error-raise operation raiseA =df E A + E. The output monad is given by

• TA =df A x E where (E,e,m) is some monoid (of output traces), e.g., the type of lists of a fixed element type with nil and append,

• VA =df A —U A x 1 '—U A x E,

• HA =df (A x E) x E —u A x (E x E) A x E

and is used to handle observable output or time. The important operation for generating impure functions is print : E —^ 1 x E. The environment monad is given by:

• TA =df E ^ A where E is some object (of environments),

• VA =df A(A x E —U A) : A u E ^ A,

• HA =df A((E ^ (E ^ A)) x E {e-U] (E ^ A) x E —U A)

: E ^ (E ^ A) u E ^ A.

It is used to deal with a readable environment. The native operation is ask =df A(1 x E —U E) : 1 u E ^ E.

Further important well-known examples include the state monad, the continuations monad, free monads and free completely iterative monads [2].

2.2 Strong monads

To be able to use a Kleisli category as a category of computation, we also need it to have datatypes. At the very least, it must support something product-like and in particular something approximating local composition to interpret let.

3 By a Cartesian category we mean one with finite products (the word is also used to refer to categories with finite limits).

In order for these to exist, the monad must be strong.

A strong functor on a monoidal category (C,I, <8>) is given by an endofunctor F on C together with a natural transformation sU,B : A ® FB — F(A ® B) (the (tensorial) strength) satisfying

I ® FA—1^F(I ® A) (A ® B) ® FC-^^^->■ F((A ® B) ® C)

иц Jfuia aa,b,fcI \/Faa,b,c

FA FA A ® (B ® FCda^bca ® F(B ® C)s^cF(A ® (B ® C))

(Note that a monad can generally have more than one strength.)

A strong natural transformation between two strong functors (F, slF), (G, slG) is a natural transformation т : F — G satisfying

A ® FB—ab F (A ® B)

■ |G " >'a,b

A ® G (A ® B )

A strong monad on a monoidal category (C,I, &) is a monad (T, n, f) where T is a strong functor and n, f are strong natural transformations. The latter part means that n, f satisfy

A & B A & B A & TTB i^T(A & TB) TT(A & B)

id A ^Va^b ^a®b

A & TB^^BT(A & B) A & TB-^ab-T(A & B)

(Note that Id is canonically strong and strong F, G make GF canonically strong.)

A strong functor (F, sl) on a symmetric monoidal category (C,I, &) is automatically bistrong: it is also endowed with a costrength sr a,b : FA & B — F(A & B) whose properties are symmetric to those of a strength. It is defined by

srAB =df FA & B c-— B & FA F(B & A) F— F(A & B) A bistrong monad (T, sl, sr) is called commutative, if it satisfies TA & TB T(TA & B)Tsab TT(A & B)

sra,tb|

T(A & TB) Ha®b TT(A & B)-—-- T(A & B)

Strength is not a very restrictive condition. In particular, on Set, every monad is strong, with a unique strength. The reason is that any endofunctor F on Set

has a unique functorial strength fsxy : X ^ Y u FX ^ FY (internalizing its functoriality) and any natural transformation between endofunctors on Set is functorially strong. Functorial strength implies tensorial strength. Commutativity is rarer. An example of a commutative monad is the exponent monad.

Given a Cartesian category C and a (1, x) strong monad T on it, we can manufacture "preproducts" in Kl(T) using the products of C and the strength sl like this:

1T =df 1 Ao xT Ai =df Ao x Ai

fstT =df n ◦ fst sndT =df n ◦ snd

!T =df n ◦ ! (ko ,ki)T =df sl* o sr o (ko,ki)

With this definition, the typing rules for products hold, but not all laws. In particular, the beta-laws fstToT(k0,k1)T = k0 and sndToT(k0,k1)T = k1 do not hold. And the binary operation xT on objects does not extend to a bifunctor (although it is a functor in each argument separately). But some other important laws, such as, e.g., the eta-law (fstT, sndT)T = idT, survive. In particular, (Kl(T),J) is a Freyd category on C, i.e., a symmetric premonoidal category with an identity-on-objects functor from C strictly preserving the (1, x) symmetric premonoidal structure of C and also centrality. (Since all maps of C are central, the latter part really means sending all maps of C to central maps of Kl(T)).

If C is Cartesian closed, then "pre-exponents" in Kl(T) can be defined from the exponents of C by

A ^T B =df A ^ TB evT =df ev AT(k) =df n o A(k)

It is not true that A ^T ~ : Kl(T) u Kl(T) is right adjoint to ~xT A : Kl(T) u Kl(T). So ^T is not a true exponent functor wrt. the preproduct functor xT. However A ^T ~ : Kl(T) uC is right adjoint to J —x A) : Cu Kl(T):

J(C x A) ut B C x A u TB CU A ^ TB C u A B

2.3 Semantics of effectful languages

Given a strong monad T on a Cartesian closed category C, the pure part of an effectful language can be interpreted into Kl(T) in the standard way, relying on the generic pre-(Cartesian closed) structure of Kleisli categories of strong monads.

mT =df an object of Kl(T) = that object of C

[A x B]T =df [A]T xT [B]T = [A]T x [BI t

[A ^ B]T =df [A]T [B]T = [A]T ^ T[ B]t

[C]T =df [Cq]t xT ...xT [Cn-!]T = [Cq]t x .. . x [Cn i]t

=df nt = n o ni

(x) let x ^ t in u\T =df [(x,x) u]T oT (idT, [(x) t]T>T = ([(x,x) u]T ) * o si o (id, [(x) t]T)

[(x) fst t]T =df fstT oT [(x)t]T = Tfst o [(x)t ]t

[(x) snd tJT =df sndT oT [(x)t]T = Tsnd o [(x): t]T

[(x) (tQ,ti)]T =df ([(x)tQ]T, [(x)ti]T>T = si* o sr o <[(x)to]T, [(x)ti]T)

[(x) \xt}T =df AT ([(x,x)t]T) = n o A([(x, x )t]T)

[(x) tu]T =df evT oT ([(x)t]T, [(x)u]T>T = ev* o si* o sr o ([(x) t]T, [(x)u]T)

(The notation (x) t denotes a term t with free variables x; we have left out types; for well-typed terms, the interpretation is well-defined.) In the second column, there is the "standard" semantics in terms of the pre-(Cartesian closed) structure of Kl(T). In the third column the same appears spelled out in more primitive terms, after simplifications. Note, e.g., that in the semantics of let, sr is not needed, although the definition of -)T involves it; it gets cancelled out in the simplification.

Constructs specific to particular notions of effect must be interpreted specifically. E.g., for exceptions we can use the coproduct monad T and set

|(x) raise tJT =df raise* o |(x)tJT

As Kl(T) is only pre-(Cartesian closed), we have soundness of typing: x : C h t : A implies |(x) tJT : |CJT —>T |AJT. But of course not all equations of lambda-calculus are validated.

In particular, we have that h t : A implies |tJT : 1 —T |AJT. So a closed term t of a type A denotes an element of T |AJT.

3 Comonads and context-dependent computation

We proceed to an analysis of context-dependent computation.

3.1 Comonads

Basics and first examples

We go straight to the definition and first properties of comonads. Comonads are the dual of monads. A comonad is a functor D : C — C (the underlying functor) together with natural transformations e : D — Idc (the counit), 5 : D — DD (the

comultiplication) satisfying

DA 5a DDA-

DDA D5a DDDA

In other words, a comonad is a comonoid in ([C, C], Idc, *).

Dually to Kleisli categories, a comonad D on a category C induces a category CoKl(D) called the coKleisli category of D. This is defined by:

• an object is an object of C,

• a map of from A to B is a map of C from DA to B,

• idD =df DA -U A,

• if k : A ud B, £ : B ud C, then £ oD k =df DA -U DB -U C where k = DA -U DDA DU DB.

From C there is an identity-on-objects inclusion functor J to CoKl(D), defined on maps by: if f : A u B, then Jf =df DA -U A -U B = DA -DU DB -U B. The functor J has a left adjoint U : CoKl(D) u C given by: UA =df DA and,

if k : A ud B, then Uk =df DA -U DB.

The intuitive basis for the use of coKleisli categories as categories of impure computation should be the following. As before, we think of C as the category of pure functions, but D describes a notion of context. DA is the type of values of a given type A placed into a context. The category CoKl(D), whose maps are maps DA u B of the base category, is the category of context-dependent functions.

The function sa : DA u A is then the identity on A made trivially context-dependent, i.e., turned into a function discarding any given context. The function Jf : DA u B is a general pure function f : A u B regarded as trivially context-dependent in a similar fashion. The function 5a : DA u DDA duplicates the context of a value while k : DA u DB is a context-dependent function k : DA u B extended into one that outputs a value of in a context (so it can be postcomposed with a context-dependent function).

Some computationally meaningful examples with C a Cartesian (closed) category, e.g., Set, are the following.

The product comonad is defined by:

• DA =df A x E, where E is a fixed object of C,

• £A =df A x E -U A,

• 5a =df A x E (A x E) x E.

This is the dual of the exceptions monad. But its use is the same as that of the environment monad: for TA =df E ^ A we have CoKl(D) = Kl(T). Hence the product comonad can be used for dependence on an environment. The important

native operation of this comonad leading to impure computations is ask =df 1 x E E.

The exponent comonad is given by:

• DA =df S ^ A where (S, e, m) is a monoid in C,

• eA =df (S ^ A)--1 (S ^ A) x 1 —— (S ^ A) x S —— A,

• Sa =df A(A(S'a)) : S ^ A — S ^ (S ^ A) where

S'A =df ((S ^ A) x S) x S -- (S ^ A) x (S x S) (S ^ A) x S A.

We come to computational uses soon, but some interesting cases are, e.g., (S,e,m) =df (Nat, 0, +) and (S,e,m) =df (Nat, 0, max). The costate comonad is given by:

• DA =df (S ^ A) x S where S is an object of C,

• £a =df (S ^ A) x S A,

• Sa =df (S ^ A) x S co—iid (S ^ ((S ^ A) x S)) x S.

This comonad arises from the composition in the appropriate order of the adjoint functors S x — ^ S Composition the other way around gives rise to the state

monad T defined by TA = S ^ (A x S). Again we defer the discussion of the computational utility.

The cofree comonad on an endofunctor H on C is given by DA =df vX.A x HX (i.e., the carrier of the final A x H(—)-coalgebra, which is the cofree H-coalgebra on A). The functor DA =df ¡jX.A x HX (i.e., the carrier of the initial A x H(—)-algebra) is also a comonad, the cofree recursive comonad [33] (dual to the free completely iterative monad [2]). The set DA is the set of nonwellfounded resp. wellfounded A-labelled H-branching trees. Think, e.g., of the case HX =df 1 + X x X, leading to binary branching with a termination option. The counit sa : DA — A extracts the root label of a given tree (so the root label is the focus value in a tree and the rest of the tree is its "context"). The comultiplication Sa : DA — DDA replaces the label of every node with the subtree rooted by that node (thus equipping every node label with a local copy of its context).

Comonads for dataflow computation

Next we look at dataflow computation (stream-based computation). We are interested in notions of computation where impure functions from A to B are general, causal or anticausal functions from StrA to StrB where StrA =df vX.A x X is the set of streams with elements from A. The physical intuition here is that of discrete-time signal transformers; streams represent histories of signals. Causality (corresponding to what is physically feasible with signals) means that the present value of the output signal can only depend on the present and past values of the input signal. Anticausality means dependence on the present and future alone.

Streams are in natural bijection with functions from natural numbers: StrA = Nat ^ A. Hence, general stream functions StrA — StrB (as used in dataflow languages like Lucid [3]) are in natural bijection with maps (Nat ^ A) x Nat — B,

i.e., with coKleisli maps of the costate comonad DA =df (S ^ A) x S where S =df Nat. Moreover, the identities and composition of general stream functions and the coKleisli identities and composition agree. So this particular instantiation of the costate comonad describes the notion of context used in general dataflow computation. We call it the streams with a position comonad, since it pairs an input stream of a stream function with a chosen position of interest in the output stream. The important operations supported this comonad are fby and next. The fby ('followed by') operation corresponds to initialized unit delay of the input signal, while next operation corresponds to unit anticipation. In both cases, the stream is shifted but the designated position stays the same.

For clarity, here is the concrete description for the case C = Set:

DA =df (Nat ^ A) x Nat

£a : (Nat ^ A) x Nat m A

(f,n) M fn

5a : (Nat ^ A) x Nat m (Nat ^ ((Nat ^ A) x Nat)) x Nat

(f,n) M (\m.(f,m),n)

fbyA : A x ((Nat ^ A) x Nat) m A

(aoo, (f, 0)) m aoo

(aoo, (f,n + 1)) M fn

nextA : (Nat ^ A) x Nat m A

(f,n) m f (n + 1)

Further, a position in a stream splits it into two parts, a list of elements before the position (the past of the signal) and a stream of all remaining elements (the present and future): (Nat ^ A) x Nat = ListA x StrA. From here it is not a long way to see that the causal stream functions (where the present value of the output signal can depend on the present and past values of the input signal, but not on the future; programs in the Lustre [13] and Lucid Synchrone [29] dataflow languages denote causal stream functions) correspond precisely to the coKleisli maps of the comonad DA =df ListA x A = NEListA = ¡iX.A x (1+X) (this is the cofree recursive monad on H defined by HX =df 1 + X, we call it the nonempty list comonad). And the anticausal ones (where the present of the output signal may only depend on the present and future values of the input signal) correspond to the coKleisli maps of the comonad DA =df StrA = vX.A x X (the cofree comonad on Idc, also equivalent to the exponent comonad DA =df S ^ A with (S, e, m) =df (Nat, 0, +), we call it the stream comonad). Again, in each case the identities and composition of stream functions agree with those of the coKleisli category. Of the operations discussed above, the first comonad supports only unit delay fby, while the second one only supports unit anticipation next.

The concrete description of the nonempty list comonad is this:

DA =df NEList A

sA : NEList A ^ A

(ao,...,an-i,an) ^ an

5A : NEList A ^ NEList (NEList A)

(ao,...,an-i,an) ^ ((ao),..., (ao,...,an-i), (ao,...,an-i,an)) fbyA : A x NEList A ^ A

(aoo, (ao)) ^ aoo

(aoo, (ao,... an+i)) ^ an

The comonad for anticausal dataflow computation is concretely defined by:

DA =df StrA sA : StrA ^ A

(an,an+1,...) ^ an

5a : StrA ^ Str(StrA)

(an,an+1,...) ^ ((an,an+1,...), (an+1,...),...)

nextA : StrA ^ A

(an,an+1,...) ^ an+1

Comonads for tree transformations

A similar example is given by relabelling tree transformations, often specified with attribute grammars [21]. Let H : C^ C. We are interested in relabelling tree functions TreeA ^ Treefi where TreeA =df ¡iX.AxHX is the type of wellfounded A-labelled H-branching trees. (Relabellings are equally plausible for nonwellfounded trees, but we concentrate on the wellfounded case.) At any node of interest in the output tree the label is determined by the label at the same node in the input tree plus maybe some more nodes. In the case of general relabellings, dependence on the labels in all of the remaining tree is allowed. In bottom-up relabellings, only labels at and below the position of interest may influence the result. The idea is again to mark the node of interest.

The comonad for general relabellings (corresponding to general attribute grammars with both synthesized and inherited attributes) is the trees with a position comonad, the comonad structure on the zipper datatype of Huet [16]. This is defined by DA =df Tree'A x A = PathA x TreeA where PathA =df List(A x H'(TreeA)) = ¡J.X.1 + A x H'(TreeA) x X. Here F' denotes the derivative of a functor (container) F; intuitively it is the type of one-hole versions of the container F [23,1]. An element of the type Tree'A x A consists of an A-labelled tree with the label of one node omitted, just to mark this node, along with the omitted label attached separately.

An element of the type PathA x TreeA is an A-labelled tree, split into the path from a node of interest up to the root, together with all side subtrees, and the subtree rooted by this node of interest.

As an example of how the path type is computed for a branching factor H, for HX =df 1 + X x X we have H'X = 2 x X, so Path A = List(A x 2 x TreeA)). An element in this type is a list of triples (the label of my parent, I am the left or right child, the side subtree rooted by my sibling) for every node from the focus node up until the root node (excluded) in a tree to relabel.

The comonad for bottom-up tree relabellings (corresponding to purely synthesized attribute grammars) is the tree comonad, defined by DA =df TreeA (the cofree recursive comonad on H). An element here represents a subtree of a global tree to be relabelled rooted by a node of interest.

(Notice that bottom-up tree relabellings generalize anticausal stream functions.)

The important operations of the comonad are for navigation in the tree: up to the parent of a given node (this is possible in the case of general tree relabellings) and down to the children (possible in both cases).

From streams and trees to containers

It is worth noticing that the tree with a position comonad is in fact a coproduct of costate comonads and the tree comonad is a coproduct of exponent comonads, just as the stream with a position comonad is a costate comonad and the stream comonad an exponent comonad.

The observation is that trees (just as streams) are a special case of containers [1], i.e., set functors FA =df UseS(Ps ^ A) where S is a set (of shapes) and P an assignment of sets (of positions) to shapes.

Shape-preserving functions FA ^ FB are thus families of maps (Ps ^ A ^ Ps ^ B)ses, in other words, maps Uses((Ps ^ A) x Ps) ^ B. The functor DA =df ses((Ps ^ A) x Ps) = F'A x A carries the comonad for the general relabellings.

To speak of abstract bottom-upness, we must confine ourselves to containers with some structure, namely: (1) for any shape s £ S and position p £ Ps, a shape s I p £ S (for the shape of the subcontainer below position p in containers of shape s), (2) for any shape s £ S, a position 0s £ Ps (the root position), (3) for any shape s £ S and positions p £ Ps and p' £ Ps[p, a position p ■ p' £ Ps (the position p' in the subcontainer as one in the global container), such that s I 0s = s, s I (p ■ p') = (s | p) | p', p ■ 0s^p = p, 0s ■ p = p, and (p ■ p') ■ p'' = p ■ (p' ■ p'') — a form of dependent monoid on the family P.

The comonad for bottom-up relabellings is then DA =df Uses(Ps ^ A) = FA.

3.2 Symmetric (semi)monoidal comonads

If C is Cartesian, then the coKleisli category CoKl(D) of a comonad D on C is straightforwardly Cartesian, as J : C ^ CoKl(D) is a right adjoint and preserves limits.

Explicitly, this structure is given by:

Ao xD Ai =df Ao x Ai

fstD =df fst o £ = £ o Dfst

sndD =df snd o £ = £ o Dsnd

{ko,ki)D =df {ko,ki)

But finding a structure to mimic the exponents of C, if C is Cartesian closed, is not so easy. Given that exponents should be internal homsets, it is natural to choose

A ^D B =df DA ^ B It is also unproblematic to match this up with the definition

eyd b =df D((DA ^ B) x A)

D(DA ^ B) x DA —H (DA ^ B) x DA -H B

where sa ,b =df (Dfst,Dsnd) : D(A x B) ^ DA x DB.

But given k : C xD A ^D B, i.e., D(C x A) ^ B, how should we define

AD(k) : DC h DA ^ B, i.e., C

B? It only makes sense to set

AD(k) =df A(k') where k' = DC x DA -h D(C x A)

Using a strength of D (if available), we could use one of the maps

DC x DA —H c x DA -H D(C x A) DC x DA—H DC x A -H D(C x A)

but this gives a solution where the order of two arguments of a binary function is important and the context of the value of one of the arguments is discarded.

The answer lies in symmetric monoidal and semimonoidal comonads. We review the definitions.

A strong [lax] symmetric monoidal functor between symmetric monoidal categories (C,I, ®) and (C',I', <8>') is a functor on F : C h C' together with an isomorphism [map] e : I' h FI and a natural isomorphism [transformation] with components mA , b : FA ®' FB h F (A ® B ) satisfying

FA ®' I' FA ®' FI

F (A ® I )

F urA cfa,fb

FA ®' FB F (A ® B)

F ca,b

mBA F (B ® A)

(FA ®' FB) ®' FCmA'B F (A ® B) ®' FC

afa,fb,fc

FA ®' (FB ®' FC):

id®mB,c

FA®' F(B ® C)

matg,b,c

ma,b®c

F ((A ® B) ® C )

F a a,

F (A ® (B ® C ))

A symmetric monoidal natural transformation between two (strong or lax) symmetric monoidal functors (F, eF, mF), (G, eG, mG) is a natural transformation t : F — G satisfying

f m i t;)

I'^^ FI FA FB-ab F (A ® B)

TI ta®'t

I '-G- GI

GA ®' GB-^- G (A ® B)

A strong [lax] symmetric monoidal comonad on a symmetric monoidal category (C,I,<8>) is a comonad (D,e,S) where the underlying functor D is a strong [lax] symmetric monoidal functor (with preservation of I, ® witnessed by e, m) and the counit and comultiplication e, 5 are symmetric monoidal natural transformations. The latter means that we have

DA ® DB ■mAB D(A ® B)

= I A ® B--

DA ® DB-

sa®sb

£a®b

'DI^TDDI DDA ® DDB

mda,db

D(DA ® DB)

^D(A ® B)

-DD(A ® B)

(Note that Id is canonically symmetric monoidal and that F, G being symmetric monoidal make GF canonically symmetric monoidal, so the definition is meaningful. Note also that the conditions on e are identical to those of an Eilenberg-Moore coalgebra structure on 1.)

Fairly often, as we will see shortly, the full structure of a symmetric monoidal comonad is not achievable, but not necessary either. We speak of a symmetric semimonoidal category, if the unit I is not present (or exists but is not important for us), and of a symmetric semimonoidal functor/comonad, if the unit preservation witness e is not present. We will later (in Sec. 3.4) show that that this imperfection can be avoided by switching to "typed" comonads on presheaf categories.

Let us revisit our examples. The product comonad, given by DA =df A x E with E an object, is lax symmetric monoidal (semimonoidal) as soon as E carries some commutative monoid (semigroup) structure e : 1 ^ E, m : E x E ^ E.

(id , e)

Indeed, we can then choose e =df 1 1 x E, mu ,B

(id , m)

df (A x E) x (B x E) —►

(A x B) x (E x E) (A x B) x E.

The exponent comonad, given by D =df S ^ A with S carrying a monoid structure, is strong symmetric monoidal as witnessed by the isomorphism e =df A(1 x S —1) : 1 = S ^ 1 and natural isomorphism m^ , b =df A(((S ^ A) x (S ^

B)) x S

(evo(fstxid), evo(sndxid))

A x B) : (S ^ A) x (S ^ B) = S ^ (A x B).

The cofree comonad and cofree recursive comonad on any polynomial functor HX = 1 + H'X are lax symmetric semimonoidal: we can choose m to "zip" two trees together, truncating wherever the branchings at a pair of corresponding nodes disagree. In the case of nonempty lists (H'X =df X), this is exactly the customary truncating zipping operation of nonempty lists. Alternatively, a single-node tree can be returned for trees of different shapes, pairing just the values at the roots.

Given a symmetric (semi)monoidal comonad D on a Cartesian closed category C, we can define: if k : C xD A 4D B, then AD(k) =df A(k') where k' = DC x DA —4 D(C x A) -4 B.

How good are the pre-exponents obtained as imitations of exponents?

If D is strong symmetric (semi)monoidal, then A =*D - is right adjoint to - xD A:

C xD A 4D B D(C x A) 4 B DC x DA 4 B DC 4 DA^ B

Hence ^D is a true exponent functor and CoKl(D) is Cartesian closed just as C. If D is only lax symmetric (semi)monoidal however, then the binary operation

^D extends to a functor, but it has few properties of the exponent functor. An intermediate case arises when (e and) m satisfy

DA-Ada

DA x DAmAD(A x A)

where Aa =df {id, id) : A 4 A x A. These conditions are automatic, if D is strong symmetric (semi)monoidal, but not in the lax case. When met, they yield e o D1 = idD1 and m a , b o sa , b = idD(AxB). As a consequence, ^D becomes a weak exponent operation on objects, i.e., we get evD oD (A(k) xD idD) = k.

We note that (!a, A a) (corresponding to the structural rules of weakening and contraction) give a uniform comonoid structure on all objects A of C. Also, the map and natural transformation (Di, s) witness that D is an oplax symmetric monoidal comonad that also respects the uniform comonoid structure (!, A).

3.3 Semantics of context-dependent languages

We are ready to define a general coKleisli semantics of context-dependent languages. We interpret lambda-calculus into the coKleisli category CoKl(D) of a symmetric (semi)monoidal comonad D on a given Cartesian closed category C of pure computations. We do this, as if CoKl(D) was Cartesian closed, even if it may be merely

Cartesian "preclosed" in one of the senses discussed above. We get:

[K D =df an object of CoKl(D) = that object of C

[1 D =df 1D = 1

[A x BJ D =df [A]D xD [B]D = [A]D x [B]D

[A ^ BJ D =df [A]D ^D [B]D = D[A]d ^ [B]D

C D =df [Co]D xD ...xD [Cn-i]D = [Co]D x ... x [Cn-i] D

[(x) Xi\ D =df nD = ni o £

(x) let x ^ t in u\ D =df [(x,x) u]D oD (idD, [(x) t]D>D = [(x,x) u]D o(£, [(x) t ]D>t

[(£)()] D =df !D =!

[(x) fst t\ D =df fstD oD [(x) t]D = fst o [(x) t]D

[(x) snd t] D =df sndD oD [(x) t]D = snd o [(x) t]D

[(x)(to,ti)J D =df ([(x) to]D, [(x) ti]D>D = ([(x) to]D, [(x) ti]D>

[(x) \xt\ D =df AD ([(x,x) t]D) = A([(x,x) t]D o m)

[(x) tu\ D =df evD oD ([(x) t]D, [(x) u]D>D = ev o ([(x) t]D, ([(x) u] D)t>

Any construct specific to a particular notion of context receives a specific interpretation. E.g., for the ask construct of a language for computing with an environment we can use the product comonad and define:

[(x) ask]D =df ask o D!

And for the constructs of a general/causal/anticausal dataflow language we can use the appropriate comonad and define:

[(x) to fby ti]D =df fby o([(x) MD, ([(x) ti)]D)t> [(x) next t]D =df next o ([(x) t]D)t

Again, we have soundness of typing, in the form x : C h t : A implies [(x)t]D : [C]D ^D [A]D, but not all equations of the lambda-calculus are validated.

For a closed term h t : A, soundness of typing says that [t]D : 1 ^D [A]D, i.e., D1 ^ [A]D, so closed terms are evaluated relative to a contextuated value of the unit type.

In case of general or causal stream functions, an element of D1 is a list over 1, i.e., a natural number, for the time elapsed from the beginning of the history at a moment of interest. Of course it identifies a stream position.

If D is strong or lax symmetric monoidal (not just semimonoidal), we have a canonical choice e : 1 4 D1. This happens, for example, in the case of the stream comonad for anticausal dataflow computation. This is adequate as all closed terms in an anticausal language must denote constant streams, with the same value at every position. Indeed, there is no way to identify a position with an anticausal computation on no input.

In what sense is this semantics correct? We could compare the generic coKleisli semantics to some other generic semantics, e.g., an operational semantics, if we had one available. Unfortunately this is not the case: generic operational semantics for context-dependent languages is future work for us.

But we can compare the coKleisli semantics of specific languages to their standard denotational semantics. Here we can observe the following. Standard dataflow languages (Lucid, Lustre/Lucid Synchrone) are first-order and here the coKleisli and standard (stream-function) semantics agree fully. How to combine dataflow constructs and higher-orderness has been unclear; various designs have been proposed, e.g., Colago et al.'s design with two flavors of function spaces [10]. The coKleisli semantics offers a neat design motivated by mathematical considerations, namely imitation of closed structure.

3.4 Precise comonads for dataflow computation and tree transformations

Several of notions of context that we looked at do not correspond to strong symmetric monoidal comonads. Rather, they correspond to lax symmetric semimonoidal comonads, for the reason that m should morally be partial and the total version fails to be an isomorphism and rules out the existence of a cohering e. Here indexing in the form of use of comonads on presheaf categories can help.

Consider the case of causal dataflow. Instead of the lax symmetric monoidal comonad on Set we could work with a more refined strong symmetric monoidal comonad on [N, Set], where N is the set of natural numbers (seen as a discrete category).

We define:

(DA)n =df J] Aj

(eA)n : (DA) n 4 n

(ao,...,an) 4 an

(SA)n : (DA)n 4 nn=o(DA)j

(ao,...,an) 4 ((ao),..., (ao,...,an))

The fby operation can be defined for those A which extend to a functor u 4 Set where u is the poset of natural numbers: we need to be able to delay stream

elements. Constant sets are typical examples.

(fbyA)n : Ao x (DA)n 4 An (aoo, (ao)) 4 aoo

(aoo, (ao,... ,an,an+l)) 1 * An^n+1 an

(By m 4 n we denote the map that is there, if m < n.) All of the above are same definitions as for the causal dataflow comonad defined before, except that we are "typed" by the positions in the single shape that streams can have.

This comonad is unproblematically strong symmetric monoidal in the right way, with e and m defined by

en : 1 4 (D1)n

() 4 ((),...,())

n+1 times

(ma,b)n : (DA)n x (DA)n 4 (DA)n

((a0 ,...,an), (a'o,...,a'n)) 4 ((a0,ao),..., (an,a'n))

A closed term in a causal dataflow language now denotes a natural transformation D1 4 A where 1 = D1. The nth component is thus an element of An, i.e., the term denotes an element of An for any position n. Of course typical base types would be constant: Kn =df K.

A similar treatment is possible for general dataflow computation and for bottom-up and general tree relabelling. In the case of trees, the indexing is by a pair of a shape and position.

4 Discussion

Brookes, Geva and Van Stone's computational comonads

Brookes and Geva's [8] original example of comonadic computation was inten-sional semantics. In its simplest form it is this: As the base category we use the category wCpo of w-cpos and w-continuous functions. The w-cpo DA is given by w-chains of elements of an w-cpo A, partially ordered pointwise. The counit computes the limit of an w-chain. The multiplication sends an w-chain to the w-chain of its prefixes (seen as w-chains by repeating the last element).

Notably, this is very similar in spirit to the comonad D on Set given by nonempty lists, for causal dataflow, except that nonempty lists are finite sequences and they do not have to be chains wrt. some partial order.

The data and laws of computational comonads were slightly different from those of symmetric (semi)monoidal comonads. Most notably, instead of e : 1 4 D1, they had a natural transformation n with components nA : A 4 DA, required to form a uniform Eilenberg-Moore coalgebra structure on all objects A. The natural transformations m and s ("merge" and "split") were governed by laws similar, but

not fully identical or equivalent to those of a lax and oplax symmetric semimonoidal comonad.

Coproducts and recursion

As left adjoints preserve colimits, the Kleisli category of any monad on a co-Cartesian category C inherits the coproducts of C. The coKleisli category of a comonad on a coCartesian category is generally not coCartesian (dually to the case of Kleisli categories and Cartesian structure).

Approximating general recursion (a uniform parameterized fixpoint operation, equivalently a uniform trace operation [14]) of a Cartesian base category in a Kleisli category is a subtle issue that has received considerable attention [12,26,6]. This is an interesting (and non-dual!) problem also in the case of coKleisli categories. Some initial work has been done by N. Frisby in the FP community.

The coKleisli category of the cofree recursive comonad on a functor H, defined by DA =df ¡X.AxHX, always has a partial uniform parameterized fixpoint operation, alternatively a partial uniform trace operation, implementing guarded recursion. This can also be reformulated in terms of recursive coalgebras, dualizing Milius's completely iterative algebras [24]. We recall that cofree recursive comonads describe the context-dependence manifested in bottom-up tree relabellings.

Combining effects and context-dependence

It is feasible that a notion of computation combines both effectfulness and context-dependence. Such combinations can correspond to distributive laws of a comonad over a monad in which case the category of impure functions is the bi-Kleisli category of the distributive law. This design appeared already in the work of Brookes and Van Stone [9]. We have applied it to clocked causal dataflow computation, combining causal dataflow and exceptions [34]. Power and Watanabe [32] have given a definitive account of the mathematics of distributive laws between monads and comonads.

Lawvere theories and arrows/Freyd categories

Lawvere theories [27] and arrows/Freyd categories [17,30] 4 are finer and coarser approaches to effectful computation. Lawvere theories make the effectful operations of a notion of effect explicit. Arrows/Freyd categories, generalizing strong monads/their Kleisli categories, were proposed as an axiomatization of notions of impure computation reaching beyond Moggi-style effects.

Similar treatments of context-dependent computation should also be possible; this is an avenue for investigation.

In the direction of Lawvere theories, we would like to proceed from Power and Shkaravska's [31] analysis of the array comonad (here called the costate comonad) as one generated by a Lawvere cotheory.

4 Jacobs et al. [15,19] have developed a thorough account of the interrelationship of arrows and Freyd categories.

Concerning the other direction, we note that arrows/Freyd categories as such are not a very interesting analysis of context-dependent computation, since any comonad/coKleisli category gives trivially an arrow/Freyd category. While Freyd categories are there to axiomatize the aspects of the Cartesian structure of the base category that must survive in the category of impure computations, for a coKleisli category we know that it is properly Cartesian. The interesting issue is to give a useful axiomatization of the additionally desirable symmetric "preclosed" structure. Freyd categories were not devised for this. Rather, we are after a suitable weakening of the notions of a symmetric closed category a la Eilenberg and Kelly (closed structure without monoidal structure) and of the adjunction in symmetric monoidal closed structure [11]. We have begun studying this matter with J. Adamek and J. Velebil.

5 Conclusions

We have demonstrated that a number of notions of context-dependent computation admit an analysis using symmetric (semi)monoidal comonads, leading to a systematic semantics of corresponding languages, analogous in spirit to Moggi's account of effects in terms of strong monads. Our analysis is not distant from that of Brookes et al., but we have added important new types of examples (notions of dataflow computation, tree labelling) and streamlined the specific data and laws for obtaining an approximation of Cartesian closed structure guided by category-theoretic criteria of canonicity. The resulting picture is quite elegant, especially with the view that the important examples where the basic comonad is only lax symmetric semi-monoidal can be reworked into examples of strong symmetric monoidal comonads using appropriate indexing.

Recursion (both general recursion and guarded recursion) and finer and coarser alternatives to comonads analogous to Lawvere theories and arrows are important special topics that we plan to address elsewhere. Likewise we defer to future research the study of computational uses of comonad resolutions other than the coKleisli resolution and generic operational semantics of context-dependent languages.

Acknowledgement

We are grateful to Thorsten Altenkirch, Bart Jacobs, Paul Levy, Conor McBride, Marino Miculan for useful discussions.

This work was partially supported by the Estonian Science Foundation grant no. 6940 and by the EU FP6 1ST coordination action TYPES.

References

[1] Abbott, M., T. Altenkirch, and N. Ghani, Containers: constructing strictly positive types, Theor. Comput. Sci. 342(1) (2005), pp. 3-27.

[2] Aczel, P., J. Adamek, S. Milius, and J. Velebil, Infinite trees and completely iterative theories: a coalgebraic view, Theor. Comput. Sci. 300(1-3) (2003), pp. 1-45.

[3] Ashcroft, E. A. and W. W. Wadge, "LUCID, the Dataflow Programming Language," Academic Press, 1985.

[4] Benton, N., G. Bierman, V. de Paiva, and M. Hyland, Linear lambda-calculus and categorical models revisited, in: E. Borger et al., eds, "Proc. of 6th Wksh. on Computer Science Logic, CSL '92," Lect. Notes in Comput. Sci. 702, Springer, 1993, pp. 61-84.

[5] Benton, N., J. Hughes, and E. Moggi, Monads and effects, in: G. Barthe, P. Dybjer, L. Pinto, J. Saraiva, eds., "Advanced Lectures from Int. Summer School on Applied Semantics, APPSEM 2ooo," Lect. Notes in Comput. Sci. 2395, Springer, 2oo2, pp. 42-122.

[6] Benton, N. and M. Hyland, Traced premonoidal categories, Theor. Inform. and Appl. 37(4) (2oo3), pp. 273-299.

[7] Bierman, G. and V. de Paiva, On an intuitionistic modal logic, Studia Logica 65(3) (2ooo), pp. 383-416.

[8] Brookes, S. and S. Geva, Computational comonads and intensional semantics, in: M. P. Fourman, P. T. Johnstone, and A. M. Pitts, eds., "Applications of Categories in Computer Science," London Math. Society Lecture Note Series 177, Cambridge Univ. Press, 1992, pp. 1-44.

[9] Brookes, S. and K. Van Stone, "Monads and comonads in intensional semantics," Techn. Report CMU-CS-93-14o, School of Comput. Sci., Carnegie Mellon Univ., 1993, 41 pp.

[10] Colaco, J.-L., A. Girault, G. Hamon, and M. Pouzet, Towards a higher-order synchronous data-flow language, in: "Proc. of 4th ACM Int. Conf. on Embedded Software, EMSOFT'o4," ACM Press, 2oo4, pp. 23o-239.

[11] Eilenberg, S. and G. M. Kelly, Closed categories, in: "Proc. of Conf. on Categorical Algebra (La Jolla, 1965)," Springer, 1966, pp. 421-562.

[12] Erkook, L. and J. Launchbury, Recursive monadic bindings, in: "Proc. of 5th ACM SIGPLAN Int. Conf. on Functional Programming, ICFP 'oo," ACM Press, 2ooo, pp. 174-185.

[13] Halbwachs, N., P. Caspi, P. Raymond, and D. Pilaud, The synchronous data flow programming language LUSTRE, Proc. of IEEE 79(9) (1991), pp. 13o5-132o.

[14] Hasegawa, M, The uniformity principle on traced monoidal categories, in: R. Blute, P. Selinger, eds., "Proc. of 9th Conf. on Category Theory and Computer Science, CTCS 'o2," Electron. Notes in Comput. Sci. 69, Elsevier, 2oo3, pp. 137-155.

[15] Heunen, C. and B. Jacobs, Arrows, like monads, are monoids, in: S. Brookes, M. Mislove, eds., "Proc. of 22nd Ann. Conf. on Mathematical Foundations of Programming Semantics, MFPS XXII," Electron. Notes in Theor. Comput. Sci. 158, Elsevier, 2oo6, pp. 219-236.

[16] Huet, G., The zipper, J. of Funct. Program. 7(5) (1997), pp. 549-554.

[17] Hughes, J., Generalising monads to arrows, Sci. of Comput. Program. 37(1-3) (2ooo), pp. 67-111.

[18] Hyland, M., G. Plotkin, and J. Power, Combining effects: sum and tensor, Theor. Comput. Sci. 357(1-

3) (2oo6), pp. 7o-99.

[19] Jacobs, B. and I. Hasuo, Freyd is Kleisli, for arrows, in: C. McBride, T. Uustalu, eds., "Proc. of Wksh. on Mathematically Structured Functional Programming, MSFP 2oo6," Electron. Wkshs. in Computing, BCS, 2oo6, 13 pp.

[20] Kieburtz, R. B., Codata and comonads in Haskell, unpublished manuscript, 1999.

[21] Knuth, D., Semantics of context-free languages, Math. Syst. Theory 2(2) (1968), pp. 127-145. Corrigendum, ibid., 51(1) (1971), pp. 95-96.

[22] Lewis, J. R., M. B. Shields, E. Meijer, and J. Launchbury, Implicit parameters: dynamic scoping with static types, in: "Proc. of 27th ACM SIGPLAN-SIGACT Symp. on Principles of Programming Languages, POPL 'oo," ACM Press, 2ooo, pp. 1o8-118.

[23] McBride, C., The derivative of a regular type is the type of its one-hole contexts, manuscript, 2ooo.

[24] Milius, S., Completely iterative algebras and completely iterative monads, Inform. and Comput. 196(1) (2oo5), pp. 1-41.

[25] Moggi, E., Notions of computation and monads, Inform. and Comput. 93(1) (1991), pp. 55-92.

[26] Moggi, E. and A. Sabry, An abstract monadic semantics for value recursion, Theor. Inform. and Appl. 38(4) (2oo4), pp. 375-4oo.

[27] Plotkin, G. and J. Power, Semantics for algebraic operations, in: S. Brookes, M. Mislove, eds., "Proc. of 17th Conf. on Mathematical Foundations of Programming Semantics, MFPS-XVII," Electron. Notes in Theor. Comput. Sci. 45, Elsevier, 2001, pp. 332-345.

[28] Plotkin, G. and J. Power, Computational effects and operations: an overview, in: M. H. Escardo, A. Jung, eds., "Proc. of Wksh. on Domains VI," Electron. Notes in Theor. Comput. Sci. 73, Elsevier, 2004, pp. 149-163.

[29] Pouzet, M., Lucid Synchrone, version 3: tutorial and reference manual, unpublished manuscript, 2006.

[30] Power, J. and E. Robinson, Premonoidal categories and notions of computation, Math. Structures in Comput. Sci. 7(5) (1997), pp. 453-468.

[31] Power, J. and O. Shkaravska, From comodels to coalgebras: state and arrays, in: J. Adamek, S. Milius, eds., "Proc. of Wksh. on Coalgebraic Methods in Comput. Sci., CMCS 2004," Electron. Notes in Theor. Comput. Sci. 106, Elsevier, 2004, pp. 297-314.

[32] Power, J. and H. Watanabe, Combining a monad and a comonad, Theor. Comput. Sci. 280(1-2) (2002), pp. 137-162.

[33] Uustalu, T. and V. Vene, The dual of substitution is redecoration, in: K. Hammond, S. Curtis, eds., "Trends in Functional Programming 3," Intellect, 2002, pp. 99-110.

[34] Uustalu, T. and V. Vene, The essence of dataflow programming (full version), in: Z. Horvath, ed., "Revised Selected Lectures from 1st Central-European Functional Programming School, CEFP 2005," Lect. Notes in Comput. Sci. 4164, Springer, 2006, pp. 135-167.

[35] Uustalu, T. and V. Vene. Comonadic functional attribute evaluation, in: M. van Eekelen, ed., "Trends in Functional Programming 6," Intellect, 2007, pp. 145-162.