Scholarly article on topic 'Arrows, like Monads, are Monoids'

Arrows, like Monads, are Monoids Academic research paper on "Computer and information sciences"

CC BY-NC-ND
0
0
Share paper
Keywords
{Arrow / Bifunctor / Monad / Monoid / "Freyd category"}

Abstract of research paper on Computer and information sciences, author of scientific article — Chris Heunen, Bart Jacobs

Abstract Monads are by now well-established as programming construct in functional languages. Recently, the notion of “Arrow” was introduced by Hughes as an extension, not with one, but with two type parameters. At first, these Arrows may look somewhat arbitrary. Here we show that they are categorically fairly civilised, by showing that they correspond to monoids in suitable subcategories of bifunctors C op × C → C . This shows that, at a suitable level of abstraction, arrows are like monads — which are monoids in categories of functors C → C . Freyd categories have been introduced by Power and Robinson to model computational effects, well before Hughes' Arrows appeared. It is often claimed (informally) that Arrows are simply Freyd categories. We shall make this claim precise by showing how monoids in categories of bifunctors exactly correspond to Freyd categories.

Academic research paper on topic "Arrows, like Monads, are Monoids"

Electronic Notes in Theoretical Computer

www.elsevier.com/locate/entcs

Arrows, like Monads, are Monoids

Chris Heunen and Bart Jacobs1

Institute for Computing and Information Sciences Radboud University, Nijmegen, the Netherlands, Email: {c.heunen,b.jacobs}@cs.ru.nl

Abstract

Monads are by now well-established as programming construct in functional languages. Recently, the notion of "Arrow" was introduced by Hughes as an extension, not with one, but with two type parameters. At first, these Arrows may look somewhat arbitrary. Here we show that they are categorically fairly civilised, by showing that they correspond to monoids in suitable subcategories of bifunctors Cop x C ^ C. This shows that, at a suitable level of abstraction, arrows are like monads — which are monoids in categories of functors C ^ C.

Freyd categories have been introduced by Power and Robinson to model computational effects, well before Hughes' Arrows appeared. It is often claimed (informally) that Arrows are simply Freyd categories. We shall make this claim precise by showing how monoids in categories of bifunctors exactly correspond to Freyd categories.

Keywords: Arrow, Bifunctor, Monad, Monoid, Freyd category.

1 Introduction

The main result of this article can be expressed in one sentence: Hughes' Arrows are monoids in categories of bifunctors Cop x C ^ C. In fact, this entire paper is just an explanation of this one-line summary.

There are several reasons why this result might appeal.

• Arrows have been introduced by Hughes [11,19] in the context of functional programming, to the rather applied end of fitting certain parsers into a general interface (see [10] for more applications). The notion comes without much motivation, and looks somewhat arbitrary. It is then reassuring —

1 Also part-time at Technical University Eindhoven, the Netherlands.

Available online at www.sciencedirect.com

DIRECT'

SCIENCE ^ I

Electronic Notes in Theoretical Computer Science 158 (2006) 219—

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

and maybe even surprising — that this notion appears as very natural in a completely different (categorical) setting.

• Arrows are meant as extensions of Monads — which are well-established in functional programming [18,30]. Indeed, for a monad M, the mapping (X,Y) a M(Y)X is a well-known instance of an Arrow. But the intended analogy between Monads and Arrows has not yet been further substantiated.

It is a standard category theory textbook result [17, VII.3] that a monad is an example of a "monoid in a category", namely in a category C a C of endofunctors on a category C (see below for more details). Our main observation is that Arrows are also monoids, also in a category of functors, not of the form C a C, but Cop x C a C.

• When we make the minor change of considering monoids in categories of bifunctors Cop x C a Sets — with Sets instead of C as codomain — we can establish a one-to-one correspondence with Freyd categories from [23]. Hence the monoid description gives a different view on the subject, namely one in which the emphasis lies on the fact that an Arrow is a mapping from both input and output types to a type of computations, with suitable composition operations. The perspective of Freyd categories puts more emphasis on the (slightly tricky) premonoidal aspects involved.

After introducing Arrows in Section 2, their structure is analysed categorically in Section 3. It turns out that the elaboration of the main result does require some work. The most technical part is the construction of suitable monoidal (tensor) infrastructure in categories of functors Cop x C a C, which is described in Subsection 3.2. This finally gives rise to our model of Arrows as monoids in Section 4.

We start with a gradual introduction of the various notions involved. In doing so we assume a basic level of familiarity with categorical notions and techniques.

Let us first recall that the Monad construct in functional programming languages corresponds directly with a strong monad in category theory [30]. To fix notation, we shortly recall that a monad on a category C consists of an endofunctor M : C a C, a unit natural transformation n: 1 " M, and a multiplication natural transformation fi: M2 a M satisfying familiar equations [17].

Usually, a monoid is described as a set M together with an associative binary operation m : M x M a M with a unit e G M. Category theory provides an abstract framework to work in different "universes". A basic illustration of this abstraction is that the notion of monoid can also be formulated in an arbitrary category with suitable structure: for instance a monoid in a category C

with Cartesian products (x, 1) is an object M of C, together with morphisms m: M x M a M and e: 1 a M that make certain diagrams commute, corresponding to the monoid equations. The unit equations m(x, e) = x = m(e, x), for instance, are:

This formulation leads to monoids in universes of for instance topological spaces or dcpo's. The carrier M then has suitable structure, that is preserved by the operations.

Even stronger, we do not even need Cartesian products (x, 1) for this formulation: monoidal structure (tensors) (0, I) suffices, because we don't need productions or diagonals. Section VII.3 of [17] lists several examples of familiar notions (such as groups and rings) that appear in this way as monoid in a category.

One example is a monad (M, n, f) on a category C. The category C a C of endofunctors (functors from C to itself, with natural transformations between them) carries a rather trivial monoidal structure given by functor composition: F 0 G = F o G, with identity functor I as obvious unit. Indeed, a monad can be described as a pair of maps (M 0 M) —A M A— I satisfying (precisely) the monoid equations. A forteriori, a strong monad on C is (precisely) a monoid in the category of strong functors C a C, with natural transformations that commute with strength.

Likewise, we will argue that monoids in suitable subcategories of bifunctors Cop x C a C model Arrows. The most technical part is the construction of a tensor in this category that has exponentiation as unit. In doing so we shall give a simplification of one of the Arrow operations (namely first) introduced by Hughes, see Proposition 3.4.

Arrows have been studied categorically before: [26] use Freyd categories (from [23]) as models of Arrows. We shall elaborate on the relation with our work in Section 5. The occurrence of the name Freyd is also very appropriate in our setting, because we encounter several issues, like dinaturality and small completeness, on which he has worked [8,2,9]. Our view of Arrows as monoids also provides another approach to Freyd categories. Section 5 concludes by proving that both models, monoids and Freyd categories, coincide in the relevant case.

222 C. Heunen, B. Jacobs / Electronic Notes in Theoretical Computer Science 158 (2006) 219-236

2 Arrows in functional languages

This section introduces Arrows and their use in functional programming languages. We briefly consider monads first, since this construction from category theory historically paved the way for Arrows.

2.1 Monads

A major reason for the initial reluctance in the adoption of functional programming languages is the need to pass state data around explicitly, even through functions that do even not use it. Monadic programming [30] provides an answer to this inconvenience. Through the use of a monad one can encapsulate the changes to the state data, the "side-effects", without explicitly carrying states around. Monads can efficiently structure functional programs while improving genericity. This mechanism is even deemed important enough to be incorporated into Haskell syntax [21]. A monad in Haskell is defined as a type class:

class Monad M where return :: X a M X (>>=) :: M X a (X a M Y) a M Y

To ensure the desired behaviour, the programmer herself should prove certain monad laws about the operations return and >>=. These boil down to the axioms that M be a strong monad, in the categorical sense.

In effect, monads are functional combinators. They enable the combination of functions very generally, without many assumptions on the precise functions to combine. However, these restrictions are strict enough to exclude certain classes of libraries from implementation with a monadic interface, most notably efficient parser combinators [27,14].

2.2 Arrows

Arrows [11,20] are even more general functional combinators, and can be seen as a generalisation of monads, as we will see in example 2.2. An Arrow in Haskell is a type constructor class of the form:

class Arrow A where

arr :: (X a Y) a A X Y (>>>) :: A X Y a A Y Z a A X Z first :: A X Y a A (X,Z) (Y, Z)

Analogous to monads, an Arrow must furthermore satisfy the following arrow laws, the proof of which is up to the programmer:

(a >>> b) >>> c = a >>> (b >>> c), (1)

arr (g 0 f) = arr f » arr g, (2)

arr id » a = a = a » arr id, (3)

first a » arr n = arr n » a, (4)

first a >>> arr (id x f) = arr (id x f) >>> first a, (5)

first a » arr a = arr a » first (first a), (6)

first (arr f) = arr (f x id), (7)

first (a >>> b) = first a » first b, (8)

In these equations we use:

Xi-^-Xi x X x (Y x Z)-2-*(X x Y) x Z,

for the familiar product maps fst, snd and assoc. Also, arr (id x f) is sometimes written as second (arr f), where

second a = arr 7 » first a » arr y,

and y : X x Y Y x X is the well-known swap-map.

Throughout this article we use a, b, c for arrows, f, g for functions, and X, Y, Z for types, wherever possible.

The arrow laws might appear arbitrary, and [11] does not derive them systematically. However, we shall show they are quite natural from a suitable categorical perspective.

It turns out that the Arrow interface is general enough to allow most known libraries that are incompatible with a monadic interface. Applications can be found on the website [10], for example in the aforementioned parser combinators, in reactive programming [15] and in user interfaces [6].

Example 2.1 (Pure functions) The first example of Arrows that comes to mind is, naturally, ordinary functions. In Haskell this is written as follows.

instance Arrow where

arr f = f

f >>> g = g ◦ f

first f = f x id

One sees at a glance that this satisfies the arrow laws (1)-(8).

To distinguish between normal functions and functions as Arrows, we also call the former pure functions.

Example 2.2 (Kleisli Arrows from monads) In category theory, given a monad one can construct the Kleisli category of free algebras. Likewise, given a monad in a functional programming language, we can cast it as an Arrow. This standard example of an Arrow is already present in [11].

newtype Kleisli M X Y = K (X a M Y)

instance Monad M ^ Arrow (Kleisli M) where arr f = K (return o f)

K f >>> K g = K (XX. f X >>= g)

first (K f) = K (X(X,Z).f X >>= XY. return (Y, Z))

The arrow laws (1)-(8) (for K) follow readily from the monad laws (for M). Intuitively we can think of a Kleisli Arrow as a computation that allows for monadic behaviour in its codomain (i.e. in its output).

Further examples of Arrows will appear in Section 4, in categorical language.

3 Analysing Arrows

In this section we shall formulate several results to make the underlying categorical structure of Arrows in Haskell explicit. We also provide an alternative for the 'first' operations in terms of what we call 'internal strength' (in Proposition 3.4). The main outcome of this section is a reformulation of an Arrow as a monoid.

We fix a category T with types as objects and terms as morphisms, such as for example the category Hask of Haskell types and functions. We assume that T is Cartesian closed, and carries a binary operation A—, —) on objects/types that satisfies the arrow laws (1)-(8) for given collections of maps arr, » and first. We shall identify the categorical structure involved in a series of results.

Lemma 3.1 The operation A(—, —) extends to a functor Top x T a T, with action on maps f: X' a X and g: Y a Y' given by:

A(f, g) = Xh. arr(f) >>> h >>> arr(g) : A(X, Y) a A(X', Y').

C. Heunen, B. Jacobs /Electronic Notes in Theoretical Computer Science 158 (2006) 219-236 225

This functor is strong in its second argument, and costrong in its first, via

st2 = A(z, h). arr(\x. (z, x)) » second(h) : Z x A(X, Y) a A(X, Z x Y), costv, = A(z, h). arr(\f. f (z)) >>> h

: Z x A(X,Y) a A(Xz,Y).

Proof. It is easy to check that A preserves identities and composition, and also that the strength and costrength maps are natural. These maps furthermore satisfy the following equations, which will come in useful:

A(id,n2) o st2 = ^2, A (id, a) o st2 o (id x st2) = st2 o a,

cost2 o Ah. (z, h) = A(Af. f (z), id), A(/3, id) o cost2 o (id x cost2) = cost2 o a,

where f3: X^ -A(XYis the familiar canonical isomorphism. □

Notice that we could just as well have used sti = A(id,Y) o st2 o y : A(X,Y) x Z a A(X,Y x Z), which then satisfies similar equations, like n1 = st1 o A(id, n1).

Lemma 3.2 The maps arr: YX a A(X,Y) form a natural transformation (+)(_) A A(-, +) from exponents to arrows.

Similarly, the maps first: A(X, Y) a A(X x Z,Y x Z) are natural in X, Y. This may be formulated as: first yields a natural transformation (first) from A to the functor Ax given by (X, Y) a f]z A(X x Z,Y x Z).

Proof. This follows from easy calculations: for maps f: X' a X, g: Y a Y' in T and h: YX we have:

(A(f, g) o arr) (h) = arr(f) >>> arr(h) >>> arr(g)

= arr(g o h o f) = arr(g^(h)) = (arr o gf^ (h),

226 and

(Ax (f,g) o (first))(h) = (A(f x id,g x id) o ^)z((first(h)))

= (A(f x id,g x id)(first(h))) = (arr(f x id) >>> first(h) >>> arr(g x id))

= (first(arr(f )) >>> first(h) >>> first(arr(g)))

= (first(arr(f ) >>> h >>> arr(g))) = (first(A(f,g)(h))) = ((first) oA(f,g))(h).

Lemma 3.3 The maps »: A(X,P) x A(P,Y) a A(X,Y) are natural in X,Y, and dinatural in P. The latter means (see [17, IX.4] or [2]) that for each map f: P a Q the following diagram commutes.

A(X, P) x A(P, Y) idxA(f,id" V

A(X, Y)

A(X, P) x A(Q, Y)

A(id,f )xi

A(X,Y )

-A(X,Q)xA(Q,Y)^tA(X,Y)

Proof. We shall only do dinaturality: for a : A(X, P) and b : A(Q,Y),

(>>> o id x A(f, id)) (a, b) = a >>> A(f, id)(b)

= a >>> arr(f ) >>> b = A (id, f)(a) >>> b = (>>> o A(id,f) x id)(a,b).

Intuitively, dinaturality in P signifies that this middle parameter 'is not really important'; it could just have well been another one, as long as it is the same across the second argument of the first factor, and the first argument of the second. This suggests that ^^ should really be seen as an operation from a tensor product. This will be elaborated in Section 3.2.

3.1 The operation first

We now prove that the maps 'first' satisfying (4)-(8) can be simplified into maps called 'ist', for internal strength.

Proposition 3.4 The maps first : A(X,Y ) a A(X x Z,Y x Z ) satisfying equations (4)-(8) correspond to "internal strength" mapps ist : A(X,Y ) a

C. Heunen, B. Jacobs /Electronic Notes in Theoretical Computer Science 158 (2006) 219-236 227

A(X,Y x X) which are natural in Y and dinatural in X, and satisfy

ist(arr(f)) = arr((f, id)), (9)

ist(a) >>> arr(n1) = a, (10)

ist(a >» b) = ist(a) >>> ist(arr(n1) >» b) >>> arr(id x n2), (11)

ist(ist(a)) = ist(a) >>> arr((id,n2)). (12)

The alternative formulation in terms of internal strength 'ist' in this result is convenient because it has only two parameters - instead of three for 'first' - and its (di)naturality is clearly described. The proof of the equivalence of 'first' and 'ist' involves many basic calculations, of which we only present a few exemplaric cases.

Proof. Given the maps 'first' satisfying (4)-(8), we define internal strength on a : A(X, Y) as:

ist(a) = arr(A) >>> first(a),

where A = (id,id). One then checks naturality in Y, dinaturality in X, and (9)-(12). The (di)naturality equations can be formulated as:

ist(a) » arr(g x id) = ist(a » arr(g)) (13)

arr(f) >>> ist(a) = ist(arr(f) >>> a) >>> arr(id x f). (14)

As illustration we check equation (10):

ist(a) >>> arr(n1) = arr(A) >>> first(a) >>> arr(n1)

= arr(A) >>> arr(n1) » a

= arr(n1 o A) >>> a = arr(id) >>> a

Conversely, given internal strength 'ist' satisfying (9)-(12), we define:

first(a) = ist(arr(n1) >>> a) >>> arr(id x n2),

where n1: X x Z a X and id x n2: Y x (X x Z) a Y x Z. This yields a natural operation, in the sense that:

arr(f x id) >» first(a) >>> arr(g x id) = first(f >>> a >>> g).

We shall prove equation (8) in detail, and leave the rest to the interested

reader.

>>> first b

ist(arr(^i) >> a) >>> arr(id x n2) >>> ist(arr(ni) >> b)

>>arr(id x n2) ist(arr(n1) » a) >>> ist(arr(id x n2) >>> arr(n1) » b)

>>>arr(id x (id x n2)) >>> arr(id x n2) ist(arr(n1) >» a) >>> ist(arr(n1) >» b)

>>>arr(id x n2) >> arr(id x n2) ist(arr(n1) >» a >>> b) >>> arr(id x n2)

first (a >» b). □

3.2 Monoidal structure

Recall from the introduction that describing an Arrow (A, arr, >>>) as a monoid in a category of bifunctors Cop x C a C requires a monoidal structure (®, I) on such bifunctors, so that the Arrow A appears as monoid of the form:

AoA^^A^^I

The naturality arr: (+)(~-> A A observed in lemma 3.2 suggests to take exponentiation as unit I = (+)(~>> of the intended monoidal structure. The big question then is: what is

We now sketch the main idea, still in the category T of types and terms. Composition >> is given by a collection of maps:

A(X, P) x A(P, Y)-^^ A(X, Y)

which can be combined by a coproduct on the left-hand-side:

( n A(X, P) x A(p Y) ]-Y)

\P eT /

This is a coproduct over all objects/types in T, and thus size aspects become relevant. But such coproducts (or sums) can be taken in polymorphic type theories, so for the time being we simply proceed and postpone issues of size

until the next section.

Once we have this (big) coproduct, it becomes "natural" to take the dinat-urality observed in lemma 3.3 into account. This is done by using a suitable

first a

(dinat) (= (11)

C. Heunen, B. Jacobs /Electronic Notes in Theoretical Computer Science 158 (2006) 219—236

coequaliser c to form the required tensor ® as in:

U A(X,Pi) x P2Pl x A(P2,Y)

VPiP2ét ,

—^ .4(X, P) x A(P, Y)

d'2 pet J

'A(X,Y ) A

(A ® A)(X,Y)

The composition map » then appears as a map (A & A) a A by construction. The two maps d1,d2 capture dinaturality via the composites:

■A(X,P1 x Pp)

A(id,evo7)

PpxA(P2,Y)-

A(A(evo7),id)

A(X,P2), ■A(PUY).

in which strength and costrength play a crucial role.

Let us make sure that exponentiation I = (—)(-) is indeed a unit for this tensor. We concentrate on the required isomorphism p: A A A 0 I. It is obtained for each pair X, Y of objects as a composite

A(X,Y )

Xa. ky (a,idy )

U A(X, P) x Y

■(A ® I)(X, Y),

where ky is the F-injection into the coproduct. The inverse of p is obtained in:

A(X, P) x Yp

— (A ® I)(X, Y)

A(X,Y)

where e is the cotuple of maps A(X, P) x YP a A(X, P x YP) a A(X, Y) given as A(id, ev o y) o st1. One can then check that p and p-1 are indeed each other's inverses.

Finally, we have to check that the monoid equations hold for the span (A 0 A) —a A a— I. We shall do one of the equations, namely

id&arr

230 C. Heunen, B. Jacobs / Electronic Notes in Theoretical Computer Science 158 (2006) 219—236

which for a: A(X,Y) becomes

a i-(a, id) i-(a, arr(id))

a >>> arr(id).

Hence commutation of this diagram amounts to arrow law (3), which states a >>> arr(id) = a.

Likewise, the associativity isomorphism a : (A & B) & C a A & (B & C) is given by associativity (A x B) x C a A x (B x C) via the coproduct. The required coherence properties, like a o a = (id & a) o a o (a & id), are proven by using the arrow laws, notably (6). Strength, costrength en internal strength on A & A are inherited from A, again via the coproduct and the equaliser.

Summarising, in a type theoretic setting we have sketched that an Arrow is the same thing as a monoid in the category of bifunctors Top x T a T with strength, costrength, and internal strength.

4 Arrows, categorically

In the previous section we have reformulated an Arrow A in Haskell as a monoid A & A a A a I in a category of bifunctors acting on types and terms. The most complicated ingredient was the tensor product &. It was constructed like the tensor product of profunctors (or distributors) generalising relation composition [16], see also [4].

One other complication involves size. The big coproduct in the previous section uses all objects of the category T as indices. This requires T to be both small and (co)complete. At this stage it is important to recall the basic result of Freyd [8, Chapter 3, Exercise D] that there are no categories that are both small and complete, except preorders (see also [13, 8.3.2]). However, small complete internal categories do exist [12], and can indeed be used as models for polymorphic type theory. Working in such a universe is very similar to working in a polymorphic type theory as we have done in the previous section.

Thus it becomes natural to consider the more general setting of bifunctors Cop x C a B, where C is B-enriched, so that size issues are separated. So far we have considered B = C, for C Cartesian closed (and thus enriched over itself). Another obvious case is to take B = Sets and C small. The situation then reduces to profunctors, the monoidal structure of which is well-known, see [4, Section 7.8] or [22, Section 6.5]. In the remainder of this paper we shall focus on this (profunctor) situation, and take the following as categorical formalisation of the notion of Arrow, as originally formulated [11] in the language of Haskell.

Definition 4.1 Let C be a small category. An Arrow over C is a monoid in the category of profunctors Cop xC a Sets, with its usual monoidal structure, that carry an internal strength.

The restriction to small categories is maybe a bit too strong. It is needed to construct the usual tensor via a big coproduct over objects of C in the category Sets, like in the previous section. We can also formulate the composition operation » of an Arrow A via collections of maps A(X, P) x A(P, Y) —y A(X, Y) — which are natural in X, Y and dinatural in P — and satisfy the equations (1)-(8). In this manner we can relax the restriction to locally small categories.

In the remainder of this section we shall sketch how to understand the earlier examples 2.1 and 2.2 in this setting, and extend them a bit. We therefor fix a locally small category C.

The Hom-bifunctor Cop x C — Sets given by (X,Y) — HomC (X,Y) is perhaps the most obvious example of an Arrow — like the pure functions of example 2.1.

If M: C — C is a strong monad, then the mapping (X, Y) — HomC(X, MY) is also an Arrow — namely the Kleisli Arrow as described in example 2.2.

That Kleisli Arrows have monadic behaviour in their codomain leads us to consider Arrows behaving monadically in their domain. In functional language: Kleisli Arrows take care of bookkeeping in the output, so why not build Arrows that take care of bookkeeping in the input [29,28]? Since an Arrow Cop x C — Sets is contravariant in its 'domain', we must start from a monad on Cop. That is, if N is a comonad, then we obtain an Arrow via (X, Y) — HomC (NX, Y).

Even more generally, as in [29], [24] or even [5], we can try to build Arrows behaving monadically in both their domain and codomain at once. Given a strong monad M, a comonad N, and a distributive law A: NM — MN between them we obtain an Arrow A via A(X,Y) = HomC(NX, MY). The unit of the monoid A is readily defined using the (co)units of M and N, and the internal strength ist : HomC(NX, MY) — HomC(NX, M(Y x X)) is easily given by ist(/) = st^ o (e, /), where e is the counit of N. The distributive law is needed to give the multiplication/composition a » b of a E HomC(NX, MY) and b E HomC(NY, MZ):

NX —^ N2X NMY MNY M2 Z —^ MZ.

232 C. Heunen, B. Jacobs / Electronic Notes in Theoretical Computer Science 158 (2006) 219-236

5 Arrows as Freyd categories

One could question our approach to Arrows in the previous sections because of the oft-heard statement "Arrows are Freyd categories" [19]: Freyd categories are claimed to already provide categorical semantics for Arrows. However, this claim has always remained very informal. We think our approach is closer to the original functional intuitions underlying Arrows in Haskell, because:

• the description of Arrows as bifunctors emphasises that they are binary operations on types;

• the precise formulation of the premonoidal structure of Freyd categories is rather delicate, and distracts from the essence of the structure of Arrows: all this structure corresponds only to 'first'.

But more interestingly, our alternative formalisation of Arrows as monoids makes it possible to give precise mathematical meaning to the statement "Arrows are Freyd categories". That is the aim of the current section. We shall compare this result with the situation for monads 2 .

Let us first recall what a Freyd category is. Therefor we need the notion of a premonoidal category, which we intuitively think of as a monoidal category in which the tensor need not be a bifunctor, though it is functorial in each variable separately.

Definition 5.1 A binoidal category is a category D, with for every object X two functors (-) x X : D a D and X x (-) : D a D such that X x Y = X x Y. Hence we write X K Y = X x Y = X x Y. A morphism f is called central if for each g, both:

• (f x id) o (id x g) = (id x g) o (f x id), and

• (id x f) o (g x id) = (g x id) o (id x f).

For such a central f it makes sense to write f Kg or g K f for these composites.

Definition 5.2 A symmetric premonoidal category is a binoidal category D together with an object I E D and natural isomorphisms with central components a : (X K Y) K Z a X K (Y K Z), A : I K X a X, p : X K I a X and Y : X K Y a Y K X that obey the familiar coherence properties for monoidal categories.

The non-bifunctoriality reflects the order of side-effects when we think of D as a category of 'computations'. When we include a category C of 'values', we arrive at the notion of a Freyd category [23,25,26].

2 As suggested by John Power.

Definition 5.3 A Freyd category consists of a symmetric premonoidal category D together with a category C with finite products, and an identity-on-objects functor J : C — D that preserves all structure: J(X x Y) = X K Y, J (a) = a, J (A) = A, etc. A Freyd category C — D is called (locally) small if the category D is (locally) small.

The comparison between monoids and Freyd categories begins with the well-known (and easily seen) fact that the following are in one-to-one correspondence:

• Monoids in the category of functors C — C,

• Monads M on C, and

• Identity-on-objects functors J : C — D that have a right adjoint,

The functor J arises from M by the Kleisli construction, while J gives a monad M induced by the adjunction.

This can be generalised to the following equivalence.

Theorem 5.4 For a locally small category C with finite products, there is a one-to-one correspondence between

(i) Arrows A over C, that is (cf. definition 4-1), monoids A in the category of profunctors Cop x C — Set that are internally strong.

(ii) Locally small Freyd categories C — D.

Proof. Suppose we are given a monoid A : Cop x C — Set with », arr, and ist, satisfying the by now familiar properties of an Arrow. Put D = Ca, the "Kleisli category" of A,3 with objects X E C and a E A(X, Y) as morphisms a : X — Y. Then D is symmetric premonoidal by defining I = 1 E D and X K Y = X x Y. The premonoidal tensor K extends to a functor (on morphisms) by virtue of the provided ist (or equivalently first, see proposition 3.4, and second), since every morphism a E A(X,Y) yields a K Z = firsts (a) : X K Z — Y K Z and Z K a = second^ (a) : Z K X — Z K Y. The implication (i) ^ (ii) is completed by defining J : C — D to act as the identity on objects, and as arr on morphisms.

Conversely, suppose given a Freyd category J : C — D. We then define A : Cop x C — Set by A(X,Y) = HomD(X,Y). This A is made into a monoid in the category of profunctors Cop x C — Set by taking the unit arr = J : HomC(X, Y) — HomD(X, Y), and taking as multiplication » the composition A(X, P) x A(P, Y) — A(X, Y) in D. Furthermore we can define

istx.y : A(X, Y) — A(X, Y x X) by istx,y (/) = (/ k X) o J ((id, id)).

3 The reason for calling Ca the Kleisli category will be presented elsewhere.

Naturality of ist in Y is obvious, dinaturality in X boils down to the fact that the diagram

HomD(X,y)^iHomD(X,y x X) (-)oJ(g^ ^-YJCs) )◦(-)

HomD(XV r)

HomD(X',y)-^Homj>(X\Y k X')

HomD(X, Y x X')

-)oJ (g)

commutes for every morphism g : X a X' in C. The crux here is that it need only commute for morphisms g of C, i.e. morphisms of D of the form J(g) (cf. equations (13) and (14)), which are central. Since one also readily checks (9)—(12), we see that (ii) implies (i). □

Another, similar, equivalence that comes to mind is that of

• Monoids in the category of strong functors C a C (and natural transformations commuting with strength),

• Strong monads on C, and

• Freyd categories C a D with a right adjoint.

However, there seems to be no meaningful 'Arrow-analogue' of this correspondence, because Arrows are automatically (co)strong (cf. lemma 3.1).

Conclusion and future work

This paper contains two reformulations of the notion of Arrow, introduced in the context of functional programming by Hughes: one minor and one major. The minor reformulation concerns an easier alternative to the 'first' operation. The major reformulation is the description of an Arrow as a monoid in a category of bifunctors.

We have used the latter reformulation to justify the informal claim that Arrows are Freyd categories. On Sets the two semantics, Freyd categories and internally strong monoids, coincide. However, the monoid approach generalises in a different direction than the Freyd category approach.

A (minor) topic left open is how the monoidal structure on the category of bifunctors Cop x C a C relates to the monoidal structure on the category of functors C a C.

In the end the question comes up: does this help functional programmers in any way? At this stage we have no such claim. But one does notice several variations and extensions of Arrows appearing, such as Biarrows [1], or

a need for recursion schemes, and thus the need for a foundation, of monads/Arrows [7,3,19]. Our categorical reformulation as monoids might give guidance for the proper formulation of such variations.

Acknowledgement

We are indebted to the anonymous reviewers for their comments and suggestions, and to John Power for pushing us to make the connection between monoids and Freyd categories explicit.

References

[1] Alimarine, A., S. Smetsers, A. van Weelden, M. van Eekelen and R. Plasmeijer, There and back again: arrows for invertible programming, in: Proceedings of the 2005 ACM SIGPLAN workshop on Haskell (2005), pp. 86-97, (ISBN 1-59593-071-X).

[2] Bainbridge, E., P. Freyd, A. Scedrov and P. Scott, Functorial polymorphism, Theoretical Computer Science 70(1) (1990), pp. 35-64, corrigendum in Theor. Comp. Sci. 71(3):431, 1990.

[3] Benton, N. and M. Hyland, Traced premonoidal categories, Theoretical Informatics and Applications 37 (2003), pp. 273-299.

[4] Borceux, F., "Handbook of Categorical Algebra," Encyclopedia of Mathematics and its applications 1, Cambridge University Press, 1994.

[5] Cockett, J., J. Koslowski, R. Seely and R. Wood, Modules, Theory and Applications of Categories 11 (2003), pp. 375-396.

[6] Courtney, A., "Modeling User Interfaces in a Functional Language," Ph.D. thesis, Yale University (2004).

[7] Erkok, L. and J. Launchbury, A recursive do for haskell, in: Haskell Workshop, 2002, pp. 29-37.

[8] Freyd, P., "Abelian Categories: An Introduction to the Theory of Functor," Harper and Row, New York, 1964.

[9] Freyd, P., Structural polymorphism, Theoretical Computer Science 115 (1993), pp. 107-129.

10] Haskell.org, http://www.haskell.org/arrows.

11] Hughes, J., Generalising monads to arrows, Science of Computer Programming 37 (2000), pp. 67-111.

12] Hyland, J., A small complete category, Annals of Pure & Applied Logic 40 (1988), pp. 135-165.

13] Jacobs, B., "Categorical Logic and Type Theory," North Holland, Amsterdam, 1999.

14] Jansson, P. and J. Jeuring, Polytypic data conversion programs, Science of Computer Programming 43 (2002), pp. 35-75.

15] Jeuring, J. and S. P. Jones, editors, "Arrows, Robots and Functional Reactive Programming," Lect. Notes Comp. Sci. 2638, Springer, 2003.

16] Lawvere, F., Metric spaces, generalized logic, and closed categories, Seminario Matematico e Fisico. Rendiconti di Milano 43 (1973), pp. 135-166, reprint available via www.tac.mta.ca/tac/reprints.

17] Mac Lane, S., "Categories for the Working Mathematician," Springer, 1971.

[18] Moggi, E., Computational lambda-calculus and monads, Logic in Computer Science (1989).

[19] Paterson, R., A new notation for arrows, in: International Conference on Functional Programming (2001), pp. 229-240.

[20] Paterson, R., Arrows and computation, in: J. Gibbons and O. de Moor, editors, The Fun of Programming, Palgrave, 2003 pp. 201-222, http://www.soi.city.ac.uk/~ross/papers/fop.html.

[21] Peyton Jones, S. and J. Hughes, Report on the programming language haskell 98, a non-strict purely functional programming language, Technical report, www.haskell.org/onlinereport (1999).

[22] Poigne, A., Basic category theory, in: S. Abramsky, D. M. Gabbay and T. S. E. Maibaum, editors, Handbook of Logic in Computer Science: Background - Mathematical Structures (Volume 1), Clarendon Press, Oxford, 1992 pp. 413-640.

[23] Power, J. and E. Robinson, Premonoidal categories and notions of computation, Mathematical Structures in Computer Science 7 (1997).

[24] Power, J. and E. Robinson, Modularity and dyads, Mathematical Foundations of Programming Semantics XV 20, 1999, pp. 467-480.

[25] Power, J. and H. Thielecke, Environments, continuation semantics and indexed categories, in: M. Abadi and T. Ito, editors, Theoretical Aspects of Computer Software, number 1281 in Lect. Notes Comp. Sci. (1997), pp. 391-414.

[26] Power, J. and H. Thielecke, Closed Freyd- and K-categories, in: J. Wiedermann, P. van Emde Boas and M. Nielsen, editors, International Colloquium on Automata, Languages and Programming, number 1644 in Lect. Notes Comp. Sci. (1999), pp. 625-634.

[27] Swierstra, S. and L. Duponcheel, Deterministic, error-correcting combinator parsers, in: J. Launchbury, E. Meijer and T. Sheard, editors, Advanced Functional Programming, Lect. Notes Comp. Sci. 1129 (1996), pp. 184-207.

[28] Uustalu, T. and V. Vene, Signals and comonads, Journ. of Universal Comp. Sci. 11(7) (2005), pp. 1310-1326.

[29] Uustalu, T. and V. Vene, The essence of dataflow programming, Revised Lectures from Central European Functional Programming School (Budapest, 2005), to appear, http://www.cs.ioc.ee/~tarmo/papers/.

[30] Wadler, P., Monads for functional programming, in: Program Design Calculi: Proceedings of the 1992 Marktoberdorf International Summer School (1993).