safe recursion revisited i: categorical semantics for lower complexity

27
Theoretical Computer Science 515 (2014) 19–45 Contents lists available at ScienceDirect Theoretical Computer Science www.elsevier.com/locate/tcs Safe recursion revisited I: Categorical semantics for lower complexity Mike Burrell a , Robin Cockett b,, Brian Redmond b a Computer Science Department, University of Western Ontario, London, Ontario, Canada b Department of Computer Science, University of Calgary, Calgary, Alberta, Canada article info abstract Article history: Received 27 May 2010 Received in revised form 6 September 2013 Accepted 30 September 2013 Communicated by D. Sannella Keywords: Categorical semantics Implicit computational complexity Safe recursion The objective of this paper is to prove that the initial Pola setting, with both inductive and coinductive data, is sound for polynomial size (PSIZE). Explicitly this means all programs written in Pola have their output size bounded by a polynomial in their input size. The paper describes the polarized categorical semantics for Pola and establishes the result by providing categorical models for various fragments of Pola which have explicit size bound information built into the maps. To obtain PSIZE soundness for Pola with just inductive data, the semantics in sized sets suffices. Sized sets are sets equipped with a “size map” which associates to each element a size. Size is usually just a natural number but, more generally, the size could be an element of a size rig. A polarized category consists of an opponent and a player category joined by a module. Sized sets can be used to create a polarized category by letting the opponent and module maps be bounded by polynomials while the player category consists of maps bounded by a constant. This gives a Pola setting with inductive data and immediately establishes the PSIZE soundness of the initial Pola setting. The main technical difficulty of the paper is to provide a semantics which correctly models coinductive data as well. For this “amortized” sets are introduced: these are set in which a higher-order size function is associated to each element, which given a size returns a size. This is amortized as one is concerned with the asymptotic behavior of these functions. While amortized sets have coinductive data they are not affine closed: a final step, using equivalence relations, is required to obtain a model which includes this aspect of Pola structure. While PSIZE by itself is a very weak bound it is a crucial step in establishing polynomial space (PSPACE) and polynomial time (PTIME) bounds for these settings. © 2013 Elsevier B.V. All rights reserved. 1. Introduction It is well-known that having a (strong) natural number object in a monoidal category immediately delivers all primitive recursive functions [19]. Therefore, to obtain settings which realize lower complexities something quite drastic has to be done. An attractive feature, however, of a natural number object is that, as initial data, it arrives packaged with a universal property which determines the equality of maps involving that type. In the initial model of a doctrine involving such a type, This work was partially supported by the National Science and Engineering Research Council (NSERC) of Canada. In addition Brian Redmond was partially supported by a post-doctoral grant from the Pacific Institute of Mathematical Sciences (PIMS), Calgary. * Corresponding author. 0304-3975/$ – see front matter © 2013 Elsevier B.V. All rights reserved. http://dx.doi.org/10.1016/j.tcs.2013.09.034

Upload: brian

Post on 30-Dec-2016

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Safe recursion revisited I: Categorical semantics for lower complexity

Theoretical Computer Science 515 (2014) 19–45

Contents lists available at ScienceDirect

Theoretical Computer Science

www.elsevier.com/locate/tcs

Safe recursion revisited I: Categorical semanticsfor lower complexity ✩

Mike Burrell a, Robin Cockett b,∗, Brian Redmond b

a Computer Science Department, University of Western Ontario, London, Ontario, Canadab Department of Computer Science, University of Calgary, Calgary, Alberta, Canada

a r t i c l e i n f o a b s t r a c t

Article history:Received 27 May 2010Received in revised form 6 September 2013Accepted 30 September 2013Communicated by D. Sannella

Keywords:Categorical semanticsImplicit computational complexitySafe recursion

The objective of this paper is to prove that the initial Pola setting, with both inductive andcoinductive data, is sound for polynomial size (PSIZE). Explicitly this means all programswritten in Pola have their output size bounded by a polynomial in their input size. Thepaper describes the polarized categorical semantics for Pola and establishes the result byproviding categorical models for various fragments of Pola which have explicit size boundinformation built into the maps.To obtain PSIZE soundness for Pola with just inductive data, the semantics in sized setssuffices. Sized sets are sets equipped with a “size map” which associates to each element asize. Size is usually just a natural number but, more generally, the size could be an elementof a size rig. A polarized category consists of an opponent and a player category joined bya module. Sized sets can be used to create a polarized category by letting the opponentand module maps be bounded by polynomials while the player category consists of mapsbounded by a constant. This gives a Pola setting with inductive data and immediatelyestablishes the PSIZE soundness of the initial Pola setting.The main technical difficulty of the paper is to provide a semantics which correctly modelscoinductive data as well. For this “amortized” sets are introduced: these are set in which ahigher-order size function is associated to each element, which given a size returns a size.This is amortized as one is concerned with the asymptotic behavior of these functions.While amortized sets have coinductive data they are not affine closed: a final step, usingequivalence relations, is required to obtain a model which includes this aspect of Polastructure.While PSIZE by itself is a very weak bound it is a crucial step in establishing polynomialspace (PSPACE) and polynomial time (PTIME) bounds for these settings.

© 2013 Elsevier B.V. All rights reserved.

1. Introduction

It is well-known that having a (strong) natural number object in a monoidal category immediately delivers all primitiverecursive functions [19]. Therefore, to obtain settings which realize lower complexities something quite drastic has to bedone.

An attractive feature, however, of a natural number object is that, as initial data, it arrives packaged with a universalproperty which determines the equality of maps involving that type. In the initial model of a doctrine involving such a type,

✩ This work was partially supported by the National Science and Engineering Research Council (NSERC) of Canada. In addition Brian Redmond waspartially supported by a post-doctoral grant from the Pacific Institute of Mathematical Sciences (PIMS), Calgary.

* Corresponding author.

0304-3975/$ – see front matter © 2013 Elsevier B.V. All rights reserved.http://dx.doi.org/10.1016/j.tcs.2013.09.034

Page 2: Safe recursion revisited I: Categorical semantics for lower complexity

20 M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45

these equalities become the basis for generating all the equality judgments. As computation is often viewed as arising frominitial settings, this native notion of equality is of considerable interest.

In dealing with settings with complexity below primitive recursive – here referred to as lower complexity settings – ourinterest is not merely in the presence or absence of maps but also in the notion of equality which they support. Therefore,we would like data, even in these settings, to possess a universal property and whence a native notion of equality.

The paper starts by reviewing the categorical semantics underlying these lower complexity settings: these are Pola (shortfor polarized) categories. These setting were described in [5] and the reader is encouraged to consult that paper should moredetails be required. Pola categories are strong polarized categories which have products, coproducts, and are affine closed.When, in addition, inductive and coinductive data is present, we refer to these categories as Pola settings.

Here we describe the (strong) universal and couniversal properties expected of data in Pola categories. The basic universalproperty of inductive data was presented already in [5], however, to this we add here the possibility of a player strength.This allows us to capture both the systems of Bellantoni and Cook [2] and Leivant [17], while also utilizing affine structurefollowing Hofmann [11].

The category of R-sized sets (see Section 4.1) was used in [5] to give a model for Pola categories with inductive data.However, R-sized sets do not model coinductive data. Section 4.2 provide an elaboration of R-sized sets, called the categoryof amortized sets, which supports both polarized inductive and coinductive data. However, the category of amortized setsis only weakly affine closed. In order to obtain a full Pola category with both polarized inductive and coinductive data, inSection 4.3, a further formal construction is performed to produce the category of amortized equivalences. The category ofamortized sets is significant as it makes precise the sense in which maps involving infinite structures can be polynomiallysize bounded – i.e. lie in PSIZE.

Bounding the size of the output in terms of the size of the input is a crucial step in establishing stronger bounds onspace and on time.

These developments grew from the realization that the system of Bellantoni and Cook [2] for describing PTIME couldbe viewed as a proof theory for a polarized logic. Polarities were introduced by Girard [8] to classify the behavior of thelogical connectives in his “constructive” classical logic LC – an idea directly related to Andreoli’s notion of focusing [1].Olivier Laurent [15] further developed these ideas and quickly realized that there was a compelling connection to games[16]: these and further references to related developments are described in [9].

The general categorical proof theory for polarized logics and games is described in [6] and uses the notion of a polarizedcategory. A polarized category is simply a module,1 however, viewed as a categorical structure in its own right. Polarizationis produced by the separation between the category which is the domain of the module (the “opponent” world) and thecategory which is the codomain (the “player world”). While Bellantoni and Cook used the terms “normal” and “safe” (re-spectively) for the two worlds of computation – rather than the game theory inspired terminology used here – it was clearthat they were employing the technique of polarizing to achieve complexity separation for computations.

Bellantoni and Cook’s system of safe recursion, which only considered binary natural numbers, was a simplificationof a system developed by Leivant [17] which had more general inductive data and infinitely many tiers (although twosufficed). Leivant’s extension to general inductive data involved some subtle issues which have been the subject of morerecent developments, see [14]. These systems supported only products in their “safe” worlds and used a polarized primitiverecursion principle.

The categorical doctrines we present here uses a further crucial idea introduced by Hofmann [11]. He realized that it wasadvantageous to assume that the player (or safe) world was affine closed as the higher-order structure allows a significantincrease in expressive power which is important in programming applications.

It is interesting to note that because Bellantoni and Cook’s system has such limited data and uses “recursion on notation”,Cobham’s theorem can actually be used to pass from a polynomial bound on size – as, for example, provided in the lastsection of this paper – to a bound on time. For the more complex Pola based systems, which support more general dataand a more sophisticated recursion scheme, this passage is no longer valid. This means, one must work harder to securethese properties (a discussion of these matters is planned for a sequel to this paper).

Section 2 introduces Pola categories. Section 3 introduces the universal and couniversal property of respectively inductiveand coinductive data in Pola categories. Section 4 supplies three examples of Pola categories of increasing sophistication. Thesimplest is the Pola category of R-sized sets which has all inductive data. Next comes the category of amortized sets whichhas inductive and coinductive data, but is not affine closed. Finally, the category of amortized equivalences has polynomiallysize bounded maps, is affine closed, and has both inductive and coinductive data.

2. Pola categories

The basic categorical structure employed in this paper is that of a Pola category. This is a polarized category, X, in thesense of [6] consisting of an “opponent” Cartesian category, Xo , a “player” category Xp , and a connecting module Xo ×Xp

−→ Xp . A module will usually be viewed in this paper as being a bipartite category: that is, as a category whose objects

1 A module M : X −→ Y is variously called a profunctor, a distributor, a bimodule: it is equivalently a functor M : Xop ×Y −→ Set or a “bipartite” categoryconsisting of the categories X and Y and in addition “cross-maps” running from the objects of X to objects of Y – but not in the reverse direction.

Page 3: Safe recursion revisited I: Categorical semantics for lower complexity

M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45 21

are separated into two classes, A and B – thought of as opponent and player worlds – which has cross-maps running fromobjects in A to objects in B but not in the reverse direction. This section describes the structure of Pola categories in stages:for some more details see [5].

The first requirement of a Pola category is that it is strong. Strong polarized categories already have a number of impor-tant links to other categorical structures: of particular importance in this development is the link to bundle fibrations. Theselinks act as a guide to how the various structural properties should behave. The structure of a Pola category is largely de-termined by the structure of the player category (in opponent context) which is required to have products, coproducts, andto be affine closed. Furthermore, in a Pola category there must be a tight connection between the player and the opponentcategory determined by a lifting – this equivalently means the corresponding fibration admits comprehension.

2.1. Polarized strong categories

A polarized strong category, X, consists of a Cartesian category.2 Xo , the opponent category, and a category, Xp , the playercategory, and a module M:

M :Xo ×Xp −→Xp

equipped with a “strong” composition and “strong” identities for the module maps:

(X1, Y1)f−→ Y2 (X2, Y2)

g−→ Y3

(X1 × X2, Y1)f ; g−−−→ Y3 (1, Y )

ιY−−→ Y

which satisfy:

• Strong identities are natural: ιY y = (1, y)ιY ′ , for any y : Y −→ Y ′ in Xp ;• The strong composition preserves the basic module structure:(

f ; f ′)y = f ; ( f ′ y)

(x1 × x2, y)( f1; f2) = ((x1, y) f1

); (x2,1) f2

f ; (x, y) f ′ = (1 × x,1)(( f y); f ′)

• The strong identities are identities for the strong composition:

(π1,1) f = ιY ; f : (1 × X, Y ) −→ Y ′

(π0,1) f = f ; ιY ′ : (X × 1, Y ) −→ Y ′

• Strong composition is associative:

(a×,1)(( f1; f2); f3

) = f1; ( f2; f3) : (X1 × (X2 × X3), Y) −→ Y ′

There is an important alternative way, due to Richard Wood [22], of regarding such a category as an enrichment of theplayer category Xp in SetX

opo . The enrichment works as follows:

Xp(A, B) : Xopo −→ Set;

X

x

X ′

�→M((X ′, A), B)

M((x,1A),1B )

h

M((X, A), B) (x,1)h

For this to work it suffices that Xo have a tensor, then the tensor on SetXopo with respect to which this enrichment works

is the Day tensor product extending the tensor on Xo to SetXopo . In our case, as the tensor on Xo is the product, the

extension to SetXopo is just the product. Given such an enrichment, one can extract a module M : Xo ×Xp −→ Xp by setting

M((X, A), B) = Xp(A, B)(X).There is also an important connection to fibrations: to access this we consider a fixed opponent world X and a varying

player world Y,Y′, . . . . To facilitate this we shall refer to a polarized strong category with opponent world X as an X-strongcategory. From the enriched perspective we are simply fixing the enrichment. An X-strong functor F : Y −→ Y′ betweenX-strong categories will then be an ordinary functor F : Y−→ Y′ and a morphism, also labeled F , on cross-maps such that:

(X, Y )f−→ Y ′

(X, F (Y ))F ( f )−−−→ F (Y ′)

2 Here Cartesian means having finite products. We shall also assume that all categories are locally small – that is, that all hom-objects are in fact sets.

Page 4: Safe recursion revisited I: Categorical semantics for lower complexity

22 M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45

which preserve the basic module structure (x, F (y))F (h)F (y′) = F ((x, y)hy′) and preserves the strong composition andidentities:

• F (ιY ) = ιF (Y );• F ( f1; f2) = F ( f1); F ( f2).

Clearly the composite of two X-strong functors is again an X-strong functor. An X-strong transformation between strongfunctors is an ordinary transformation between the ordinary functors α : F −→ F ′ such that for cross-maps h we have(1,α)F ′(h) = F (h)α. We now have:

Proposition 2.1. X-strong categories, functors, and transformations form a 2-category, written Str(X).

From the enriched perspective these are just enriched functors and natural transformations.Given an X-strong category Y a fibration3 bun(Y) : Y −→ X can be constructed, which we refer to as the bundle fibration.

The total category Y of this fibration is defined as follows:

Objects: pairs of objects (X, Y ) ∈X×Y.Maps: a map from (X1, Y1) to (X2, Y2) is a pair (x,h), where x : X1 −→ X2 in X and h : (X1, Y1) −→ Y2 is a module map.Composition: let (x,h) : (X1, Y1) −→ (X2, Y2) and (x′,h′) : (X2, Y2) −→ (X3, Y3); then composition is defined by (xx′, (�,1)

(h; (x,1)h′)) : (X1, Y1) −→ (X3, Y3).Identities: (1X , (!X ,1)ιY ) : (X, Y ) −→ (X, Y ).

It is not hard to check that Y is a category and moreover gives rise to a fibration which has a cleavage:

p : Y −→X; (x, y) �→ x

For each map x : X −→ X ′ and object (X ′, Y ′) over X ′ , the Cartesian lifting is x∗ = (x, (!X ,1)ιY ′ ) : (X, Y ′) −→ (X ′, Y ′).Letting CFib(X) denote the category whose objects are fibrations with cleavage over X and whose maps are functors

over X which preserve the cleavage then, when F is an X-strong functor, we may define a morphism of (cleaved) fibrationsby bun(F )(x, ιY ) = (x, F (ιY )) = (x, ιF (Y )). We observe:

Proposition 2.2. bun : Str(X) −→ CFib(X) is a 2-functor which, moreover, preserves products.

A Cartesian category X canonically provides an X-strong category by defining the cross-maps as:

X1 × Xh−→ X2

(X, X1)h−→ X2

Note that this makes f ; g = a×( f × 1)g . An X-strong functor F then becomes a strong functor in the usual sense [13,7,22].Given any fibration with cleavage over a Cartesian category X, the fiber over 1 naturally forms an X-strong category and

this gives a functor in the reverse direction:

Proposition 2.3. There is a 2-functor pol : CFib(X) −→ Str(X).

Proof. (Sketch) Let p : Y −→ X be a fibration with cleavage. Then we may build an X-strong category on the fiber over 1where the cross-maps (X, Y ) −→ Y ′ are the maps !∗X (Y ) −→ Y ′ in Y. The strong identity maps are then identity maps in the

fiber over 1. The strong composition of (X1, Y ) =!∗X1(Y )

f−→ Y ′ and (X2, Y ′) =!∗X2(Y ′) g−→ Y ′′ is given by lifting the first map

to the map (X1 × X2, Y )f−→ (X2, Y ′) (as illustrated below) and composing with g:

3 This is not the usual Grothendieck fibration from the module but uses the extra composition ‘;’ of an X-strong category in an essential way.

Page 5: Safe recursion revisited I: Categorical semantics for lower complexity

M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45 23

(X1 × X2, Y )

f

!∗π0

(X1, Y )

f

!∗ (1, Y )

(X2, Y ′)

g

!∗ (1, Y ′)

X1 × X2

π1

!π0

(1, Y ′′)

X1 ! 1

X2

!

A morphism of fibrations F : (p : Y −→ X) −→ (q : Y′ −→ X) consists of a functor F : Y −→ Y′ such that p = F ;q whichpreserves the cleavage. The restriction to the fiber over 1 then defines an X-strong functor between the induced X-strongcategories. Similarly a natural transformation between a morphism of fibrations induces an X-strong transformation. �Proposition 2.4. The above functors form a Galois adjunction4 bun � pol : Str(X) −→ CFib(X).

Proof. (Sketch) The unit of the adjunction carries Y to the fiber over 1 in bun(Y):

η : Y −→ pol(bun(Y));Y

f

Y ′�→

(1, Y )

(11,ιY f )

(1, Y ′)

We need to show the following universal property:

H

pol(bun(Y))

pol(H�)

pol(A)

To do this we indicate how H� is defined on (x, f ) ∈ bun(Y) using the lifting property (dotted maps) of the fibration A andthe definition (X, A) :=!∗X (Y ) ∈ p−1(X) as above:

(X, Y )

H( f )

H�(x, f )

H( f )(X, Y ′)

(1,x)

!∗

(X ′, Y ′)

!∗(1, Y ′)

X

!

x

1X

!x X ′

!1

This is clearly unique as the liftings are unique.

4 This is an adjunction (η, ε) : F � G : X −→ Y with the additional property that F (η) is an isomorphism – or equivalently G(ε) is an isomorphism. Anysuch adjunction can be factorized as a reflection followed by a coreflection.

Page 6: Safe recursion revisited I: Categorical semantics for lower complexity

24 M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45

To show this is a Galois adjunction it suffices to check that bun(η) is an isomorphism: however, this morphism offibration is determined by its effect on the fiber over 1 and these fibers are the same category. �

The fact that there is a Galois adjunction between Str(X) and CFib(X) means that one can identify a common fullsubcategory: the subcategory of CFib(X) corresponds to “bundle fibrations” while the full subcategory of Str(X) correspondsmore prosaically to the X-strong categories in which Y is already the fiber over 1 in bun(Y): we shall call strong polarizedcategories which satisfy this bundled. In particular, we shall demand that all Pola categories are bundled in this sense: thus,all Pola categories player maps X −→ Y and maps in context (1, X) −→ Y coincide.

2.2. Adding structure to strong polarized categories

Pola categories are X-strong categories which have more structure: the player category, in particular, is assumed tobe affine5 closed and to have products and coproducts. This section describes how this structure is defined for X-strongcategories.

An X-strong category Y is affine in case Y is a symmetric monoidal closed category, with tensor ⊗ and unit the finalobject, 1, and this structure extends to the module. It is affine closed in case it is affine and A ⊗ _ has a right adjoint A � _for each A ∈ Y.

(X, Y1)f−→ Y ′

1 (X, Y2)g−→ Y ′

2

(X, Y1 ⊗ Y2)f ⊗ g−−−−→ Y ′

1 ⊗ Y ′2

(X, Z ⊗ Y1)f−→ Y2

(X, Z)cur( f )−−−−→ Y1 � Y2

These must satisfy the equations:

• The tensor product is an X-strong bifunctor: ( f ⊗ g); ( f ′ ⊗ g′) = f ; f ′ ⊗ g; g′ , ιY1 ⊗ ιY2 = ιY1⊗Y2 . The monoidal naturalisomorphism a⊗, uL⊗, uR⊗, c⊗ are X-strong natural transformations: e.g. (1,a⊗)(( f ⊗ g) ⊗ h) = ( f ⊗ (g ⊗ h))a⊗ , etc.;

• The tensor product must behave well with the module structure: (x, y1 ⊗ y2)( f ⊗ g) = (x, y1) f ⊗ (x, y1)g and ( f ⊗g)(y1 ⊗ y2) = f y1 ⊗ gy2;

• For the closed structure:(cur( f ) ⊗ (!X ,1)ιA

)ev = f

(x,1)cur( f ) = cur((x,1) f

)h; cur( f ) = cur

((h ⊗ (!,1)i

); f)

cur((1,ev)ιB

) = ιA�B

Remark 2.5.

(i) From the enriched perspective this is just demanding the affine structure is suitably enriched.(ii) It is reasonable to wonder why the affine tensor is not assumed to coincide with the product. In the presence of

inductive data (see Section 3.2), assuming closedness and this identification of product and tensor, immediately takesone out polynomial of lower complexity and it is not too hard to see, in fact, into ELEMENTARY time. On the other hand,when the affine structure is distinct from the product it is possible to assume closedness while remaining in polynomialcomplexity as shall be seen later in this paper. Affine closedness is important as it adds significant expressive power.

An X-strong category Y has products in case Y has products and this structure extends to cross-maps:

(X, Y )f−→ Y1 (X, Y )

g−→ Y2

(X, Y )〈 f , g〉−−−−→ Y1 × Y2 (X, Y )

!X,Y−−−→ 1

These must satisfy the following equations:

• 〈 f , g〉π0 = f , 〈 f , g〉π1 = g , and 〈hπ0,hπ1〉 = h.• The terminal object satisfies: for any cross-map f =!X,Y : (X, Y ) −→ 1.

An X-strong category Y has coproducts in case the categories X and Y have coproducts which are distributive with respectto the product in X and with respect to the (affine) tensor in Y (this latter is forced if Y is affine closed). This means there

5 Affine in the sense that it is a symmetric monoidal closed category in which the tensor unit is a final object.

Page 7: Safe recursion revisited I: Categorical semantics for lower complexity

M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45 25

is a (strength) map d : Z × (Y1 + Y2) −→ Z × Y1 + Z × Y2 which is inverse to the natural map in the reverse direction. Inaddition we require that the coproducts work across the module in both the possible ways:

(X1, Y )h1−−→ Y ′ (X2, Y )

h2−−→ Y ′

(X1 + X2, Y ) −−−−−→〈h1|h2〉o

Y ′(X, Y1)

h1−−→ Y ′ (X, Y2)h2−−→ Y ′

(X, Y1 + Y2) −−−−−→〈h1|h2〉p

Y ′

Remark 2.6. Note that we have not demanded that the products of Y distribute over the coproducts. In the presence ofinductive data, when higher-order types distribute over coproducts, this actually introduces more arithmetic power into thesystem. In particular, when products distribute, in this manner, over coproducts this makes the setting include PSPACE. Thisis due to an observation of Hofmann [12] and is related to the observations in [18].

The examples considered in this paper, see Section 4, do satisfy this distributive law because they are settings which aresize bounded (as opposed to time bounded).

Products and coproducts can, of course, be defined at the 2-categorical level by demanding left and right adjoints to thediagonal X-strong functor: the above is an explicit equivalent presentation. This formulation, however, has the advantagethat it is transported by any 2-functor which preserves products: notably bun is of this form. This leads to the followingfibrational statement:

Proposition 2.7. Let Y be an affine closed X-strong category with products and coproducts. Then the corresponding fibration p : Y−→ X is fibered affine closed and has fibered products and coproducts. This means that each of the fibers is an affine closed categorypossessing products and coproducts, and this structure is preserved (on the nose) by the re-indexing functors.

When the fibers have products then the total category Y itself has products. However, this does not work for coproducts:in particular, objects from different fibers may not have coproducts in the total category.

2.3. Lifting

A strong polarized category has a “lifting” when there is a canonical way to interpret cross-maps as opponent maps.From the fibrational perspective this demands that the bundle fibration has “comprehension”. Lifting plays a crucial role indefining the inductive and coinductive properties of data in low complexity settings.

We say that the module has a lift if for each Y ∈Y there is an object ↑(Y ) ∈ X and a module map(↑(Y ),1) εY−−→ Y

such that whenever (X,1)h−→ Y is a module map there is a unique map h : X −→ ↑(Y ) making

(X,1)

(h,1)

h Y

(↑(Y ),1)

εY

commute. The combinator is an operation which takes certain cross-maps to X-maps. We can define an operation in theother direction � by g� = (g,1)εY . Then the following equations are easy consequences of the definition:(

x�) = x(

h)� = h

We also have ((x,1)hy) = xh(ε y) .This allows one to define a lift combinator, which we shall use in the term logic:

(X, Y ⊗ Y ′) f−→ Y ′′

(X × ↑(Y ), Y ′) −−→f ↑

Y ′′

as:

(↑(Y ),1)εY−−→ Y (↑(Y ), Y ′) (!,1)ιY ′−−−−−→ Y ′

(↑(Y ), Y ′)εY ,Y ′−−−→ Y ⊗ Y ′ (X, Y ⊗ Y ′) f−→ Y ′′

(↑(Y ) × X, Y ′)εY ,Y ′ ; f−−−−−→ Y ′′

(X × ↑(Y ), Y ′) −−→↑

Y ′′

f
Page 8: Safe recursion revisited I: Categorical semantics for lower complexity

26 M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45

with εY ,Y ′ = (1, u−1L )(eY ⊗ (!,1)ιY ′ ) and f ↑ = (c×,1)(εY ,Y ′ ; f ).

Clearly X provides an X-strong category with a lift. It is straightforward to check:

Proposition 2.8. Lifting defines an X-strong functor ↑(−) : Y −→ X defined by Y �→ ↑(Y ) and y : Y1 → Y2 �→ (εY1 y) which isiso-monoidal6 for both the product and the affine tensor.

Proof. Because the product and tensor are affine we expect comonoidal maps ↑(Y1 ⊗ Y2) −→ ↑(Y1) × ↑(Y2) and ↑(Y1 × Y2)

−→ ↑(Y1) × ↑(Y2). The maps in the reverse direction are given by:

Y1 ⊗ Y2 −→ Y1 ⊗ Y2

(1, Y1 ⊗ Y2) −→ Y1 ⊗ Y2

(↑(Y1), Y2) −→ Y1 ⊗ Y2

(↑(Y1) × ↑(Y2),1) −→ Y1 ⊗ Y2

↑(Y1) × ↑(Y2) −→ ↑(Y1 ⊗ Y2)

Y1 −→ Y1

(1, Y1) −→ Y1

(↑(Y1),1) −→ Y1

(↑(Y1) × ↑(Y2),1) −→ Y1

Y2 −→ Y2

(1, Y2) −→ Y2

(↑(Y2),1) −→ Y2

(↑(Y1) × ↑(Y2),1) −→ Y2

(↑(Y1) × ↑(Y2),1) −→ Y1 × Y2

↑(Y1) × ↑(Y2) −→ ↑(Y1 × Y2)

Moreover, as 1 is terminal there is a unique map !↑(1) : ↑(1) −→ 1 which is inverse to the map (ι1) : 1 −→ ↑(1). �

It is also important for our purpose that the lift preserves the coproduct structure and we shall simply demand that thisis the case.

Recall that a fibration p : Y −→ X, which has a terminal object functor T : X −→ Y – which picks out the terminal objectin each fiber – admits comprehension if this functor has a right adjoint [10].

Proposition 2.9. If an X-strong category Y has a lift, then the corresponding fibration p : Y−→ X admits comprehension in the abovesense.

Proof. Let Y be an X-strong category with a lift operator. Then there is a functor R : Y−→X defined by R(X, Y ) = X ×↑(Y )

and R(x,h) = 〈π0x, (h;ε)〉. We claim that this is right adjoint to the terminal object functor T : X −→ Y, defined by T (X) =(X,1) and T (x) = (x, !). I.e. that there is a bijection:

(X,1) −→ (X ′, Y )

X −→ X ′ × ↑(Y )

The unit and counit of the adjunction are:

ηX = 〈1X , !X 〉 : X → X × ↑(1)

εX,Y = (π0, (π1,1)εY

) : (X × ↑(Y ),1) −→ (X, Y )

These are certainly natural transformations: it remains to verify the adjunction equations which is left to the reader. �It is often convenient to view the lift as being the identity on types: we shall say that the lift is silent in this case. Given

a strong polarized category with a lift one can always extract (couniversally) from it a strong polarized category with asilent lift. This shows that strong polarized categories with silent lifts form a coreflexive subcategory of all strong polarizedcategories with a lift (and functors which preserve lift).

Proposition 2.10. The full subcategory of strong polarized categories with silent lifts is a coreflexive subcategory of the category ofstrong polarized categories with a lift.

Proof. For any strong polarized category P we must construct a strong polarized category with a trivial lift S(P) and afunctor ε : S(P) −→ P such that any functor from a strong polarized category with a trivial lift factorizes uniquely through thiscategory. If P :X×Y −→ X then we set S(P) : Y×Y−→Y where the o-category has homsets S(P)o(Y1, Y2) = X(↑(Y1),↑(Y2))

and the op-map Sop((Y0, Y1), Y2) = Pop((↑(Y0), Y1), Y2) while the p-maps are the same: composition is then as in P. Thefunctor ε then carries o-objects to their lifted counterpart.

That this has the desired couniversal property is straightforward to check. �Having a silent lift greatly simplifies notation as lifting can be implicitly managed by position.We have now collected all the structural components required to define the key categorical structure of this paper:

6 These are often called strong monoidal, however, the reader will appreciate that we have quite a few “strong” notions in this paper already!

Page 9: Safe recursion revisited I: Categorical semantics for lower complexity

M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45 27

Definition 2.11. A Pola category is a strong polarized category which is bundled, has a silent lift, has (finite) products andcoproducts, and is affine closed. A Pola category is said to be distributive when product distribute over coproducts in theplayer category (in every opponent context).

3. Data in Pola categories

Pola categories can support both “polarized” inductive and coinductive data: it is the presence of such data which deliverscomputational power to the setting. This section introduces the basic properties of polarized data. A striking peculiarity ofdata in Pola categories is that the data itself does not, in general, supply material from which one can build further data.This is because neither inductive nor coinductive data will, in general, organize itself into a functor let alone the moredemanding notion of a polarized operator – for which these data types are fixed points.

The section starts with a discussion of polarized operators and, in particular of the “basic operators” present in all Polacategories. A slightly subtle issue concerns the “strength” of a polarized operator. All polarized operators are “strong” overthe opponent context, however, some are also strong over a player context. Inductive data built from a polarized operatorwhich is strong, in this latter sense, satisfies a universal property which uses this strength.

The section introduces recursion principles for inductive data and coinductive data. The basic form is obtained by “po-larizing” the notion of an inductive fixed point or initial algebra in context. This can be strengthened to a more powerful“primitive recursive” notion which allows access to the state of the data. The latter is the fixed point version of the primitiverecursion commonly described in the literature.

In a standard setting these recursion principles are easily seen to be equivalent. In this polarized setting, the standardproof that the basic inductive fixed point principle is equivalent to its primitive recursive version7 cannot be used. In fact,while the recursion principles are still equivalent, the proof is along rather different lines and relies on the affine closedstructure.

The situation for coinductive data is much simpler: the asymmetry of the situation does not permit the complex interac-tions with strength and closure which gave rise to the variations for inductive data. Thus, in this case it is simply a matterof showing how the basic coinductive fixed point property is “polarized” and how this translates into a recursion principle.

3.1. Polarized operators

A polarized operator F on a strong Pola category consists of a pair of strong functors F p : Yn −→ Y, and Fo : Xn −→ X anda map of cross-maps Fop :

(X1,1)f1−−→ Y1 · · · (Xn,1)

fn−−→ Yn

(Fo(X1, . . . , Xn),1)Fop( f1, . . . , fn)−−−−−−−−−→ F p(Y1, . . . , Yn)

satisfying various natural conditions. In the unary case these are:

• Fo and F p are X-strong functors;• Fop((x,1)h) = (Fo(x),1)Fop(h) and Fop(hy) = Fop(h)F p(y);• Lifting must be preserved by the operator in the sense that there is a strong natural isomorphism γ F such that:

Y

⇓ γ FF p

↑X

Fo

Y ↑ X

In Pola categories, where the lift is trivial, we shall demand that γ F is the identity map and, thus, F p and Fo can bewritten as just F .

The basic strength of a polarized operator may be displayed by the following applications of the operator:

(X, Y )f−→ Z

(X, F (Y )) −−−−→F X

p ( f )F (Z)

(X × Y ,1)g−→ Z

(X × F (Y ),1) −−−−→F X

o (g)F (Z)

7 Recall this standard proof uses a “state” which is a product in which one component is the data type itself: a trick which originates in Kleene’s methodof encoding the predecessor function.

Page 10: Safe recursion revisited I: Categorical semantics for lower complexity

28 M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45

For example, the affine tensor paired with the product is a polarized operator and this strength becomes:

(X, Y0)f0−−→ Y ′

0 (X, Y1)f1−−→ Y ′

1

(X, Y0 ⊗ Y1) −−−−−→f0 ⊗ f1

Y ′0 ⊗ Y ′

1

(X × Y0,1)g0−−→ Y ′

0 (X × Y1,1)g1−−→ Y ′

1

(X × (Y0 × Y1),1) −−−−−→〈g0, g1〉 Y ′0 × Y ′

1

Certain polarized operators are always present in a Pola category:

Proposition 3.1. In any Pola category:

(i) For any object A in Y, the constant functors K Ap : Y0 −→ Y and K A

o : X0 −→ X, defined by K Ap (Y ) = A and K A

o (X) = ↑(A), form a

polarized operator. In this case K Aop(∗) = (!↑(A),1)εA .

(ii) The product in Y and the product in X form a polarized operator.(iii) The coproduct in Y and coproduct in X form a polarized operator (this is why we required lifting to preserve coproducts).(iv) The tensor product in Y and the product in X form a polarized operator.

Polarized operators compose as operations on a polarized strong category. Thus, further examples can be generated fromthese basic examples. In a Pola category the polarized operators generated by constants, +, ×,and ⊗ will be called the basicpolarized operators. When the product distributes over the coproduct (which is true for the examples considered in thispaper) all the basic polarized operators can be expressed as a sum of polarized operators in × and ⊗.

Polarized operators can also be (player) strong as the player category Y is a category with products. A polarized operatorwhich is (player) strong in this sense is called a strong polarized operator. Notice that constants and products are alwaysstrong polarized operators. When products distribute over coproducts then the coproduct polarized operator also is strong.However, the tensor product will – almost always – not be strong.

A strong polarized operator has the applications:

(X, Z × Y )f−→ Z ′

(X, F p(Z) × Y ) −−−−−→F X,Y

p ( f )F p(Z ′)

strongp

(X × Z , Y )f−→ Z ′

(X × Fo(Z), Y ) −−−−−→F X,Y

o ( f )F p(Z ′)

strongo

Proposition 3.2. In any distributive Pola category the polarized operators corresponding to constants, products, and coproducts arealways strong.

Proof. The strong polarized operator corresponding to the product has the following applications:

(X, Z0 × Y )f0−−→ Z ′

0

(X, (Z0 × Z1) × Y )(1,π0 × 1) f0−−−−−−−−→ Z ′

0

(X, Z1 × Y )f1−−→ Z ′

1

(X, (Z0 × Z1) × Y )(1,π1 × 1) f1−−−−−−−−→ Z ′

1

(X, (Z0 × Z1) × Y ) −−−−−−−−−−−−−−−−−→〈(1,π0 × 1) f0, (1,π1 × 1) f1〉

Z ′0 × Z ′

1

(X × Z0, Y )f0−−→ Z ′

0

(X × (Z0 × Z1), Y )(1 × π0,1) f0−−−−−−−−→ Z ′

0

(X × Z1, Y )f1−−→ Z ′

1

(X × (Z0 × Z1), Y )(1 × π1,1) f1−−−−−−−−→ Z ′

1

(X × (Z0 × Z1), Y ) −−−−−−−−−−−−−−−−−→〈(1 × π0,1) f0, (1 × π1,1) f1〉

Z ′0 × Z ′

1

Assuming products distribute over coproducts, the strong applications for the coproduct as a polarized operator are:

(X, Y0 × Z)f0−−→ Y ′

0

(X, Y0 × Z)f0σ0−−−→ Y ′

0 + Y ′1

(X, Y1 × Z)f1−−→ Y ′

1

(X, Y1 × Z)f1σ1−−−→ Y ′

0 + Y ′1

(X, Y0 × Z + Y1 × Z)〈 f0σ0 | f1σ1〉p−−−−−−−−−→ Y ′

0 + Y ′1

(X, (Y0 + Y1) × Z) −−−−−−−−−−−−→(1,d )〈 f σ | f σ 〉

Y ′0 + Y ′

1

L 0 0 1 1 p
Page 11: Safe recursion revisited I: Categorical semantics for lower complexity

M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45 29

Datatype Constructors

Nat = μx.1 + x 〈Zero | Succ〉 : 1 + Nat −→ NatBNat = μx.1 + x + x 〈End | B0 | B1〉 : 1 + BNat + BNat −→ BNatL(A) = μx.1 + A ⊗ x 〈Nil | Cons〉 : 1 + A ⊗ L(A) −→ L(A)

Bool = 1 + 1 〈True | False〉 : 1 + 1 −→ BoolSF(A) = A + 1 〈SS | FF〉 : A + 1 −→ SF(A)

T(A) = μx.A + X ⊗ X 〈Leaf | Node〉 : A + T(A) ⊗ T(A) −→ T(A)

T′(A) = μx.A + X × X 〈Leaf′ | Node′〉 : A + T′(A) × T′(A) −→ T′(A)

Fk = 1 + · · · + 1︸ ︷︷ ︸k

〈S1 | · · · | Sk〉 : 1 + · · · + 1︸ ︷︷ ︸k

−→ Fk

Fig. 1. Inductive data types.

(X × Y0, Z)f0−−→ Y ′

0

(X × Y0, Z)f0σ0−−−→ Y ′

0 + Y ′1

(X × Y1, Z)f1−−→ Y ′

1

(X × Y1, Z)f1σ1−−−→ Y ′

0 + Y ′1

(X × Y0 + X × Y1, Z)〈 f0σ0 | f1σ1〉o−−−−−−−−−→ Y ′

0 + Y ′1

(X × (Y0 + Y1), Z) −−−−−−−−−−−−→(dR ,1)〈 f0σ0 | f1σ1〉o

Y ′0 + Y ′

1 �

3.2. Inductive data

If F is a strong polarized operator on a Pola category an inductive fixed point for F is an object μx.F (x) with a playerisomorphism:

F(μx.F (x)

) ConsF−−−−→ μx.F (x)

such that given any player F -algebra in context g : (X, F (A) × Y ) −→ A there is a unique map fold(g) : (X × μx.F (x), Y ) −→ Asuch that the following diagram commutes:

(X × F (μx.F (x)), Y )

(π0,〈F X,Yo fold(g),(!,1)ι〉)

(1×↑(Cons),1)(X × μx.F (x), Y )

fold(g)

(X, F (A) × Y ) g A

If F is not a strong polarized operator then Y must be set to 1. The inductive (fixed point) recursion principle is the uniqueexistence of the fold map in the above square: it is the most basic recursion principle we will consider. Note that thisprinciple already uses the strength of the polarized operator in a non-trivial manner.

Fig. 1 gives a number of basic examples of inductive data types. Consider, for example, the data type of unary naturalnumbers, Nat. The polarized operator is F (X) = 1 + X and the constructor is

〈Zero | Succ〉 : 1 + Nat −→ Nat

where, as usual, for each coproduct component we provide a named constructor. The type of the constructor in the definitionis the type (of the copairing components) of a general algebra map with no context. In lower complexity settings the binarynatural numbers BNat have an important traditional role: they are not isomorphic to the unary numbers Nat and permitmore arithmetic operations. Data types need not be recursive: they can simply be used to name certain key types: forexample the data types Bool, Fk , and SF(A) name the Booleans, the k element finite set, and the exception type.

An important data type is the list type, L(A) = μx.1 + A ⊗ x. We also introduce two sorts of trees: the first, T(A), whichwe refer to as a Hofmann tree, has the branches at its nodes tensored, while the second, T′(A), which we refer to as a Leivanttree, has its branches paired with a product.

Page 12: Safe recursion revisited I: Categorical semantics for lower complexity

30 M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45

An apparently more powerful recursion principle is the primitive recursion principle: it states that given an algebra map:g : (X × F (μx.F (X)), F (A) × Y ) −→ A there is a unique fold∗(g) : (X × F (μx.F (X)), Y ) −→ A such that

(X × F (μx.F (x)), Y )

(1,〈F X,Yo (fold∗(g)),(!,1)ιY 〉)

(1×↑(Cons),1)(X × μx.F (x), Y )

fold∗(g)

(X × F (μx.F (x)), F (A) × Y ) g A

Note here that one is allowed to use in the opponent position a copy of the state of the data over which one is recursing:this is the recursion principle used in [2] and [18].

Our first observation is:

Proposition 3.3. In any distributive Pola category, which has inductive data for all basic polarized operators, this data satisfies thefixed point recursion principle if and only if it satisfies the primitive recursion principle.

The importance of this result is that it shows that it suffices to prove the basic fixed point recursion principle holdsfor inductive data in order to obtain the apparently stronger primitive recursive principle which is commonly used in theliterature. The result tells one something about the expressive power of the system. In particular, we claim (and this is themain subject of the second paper) that a polar category with inductive data can express all PSPACE programs. In this light,it should be no surprise that this slightly strengthened recursion principle can also be expressed. We shall not explicitly usethe result in this paper so we give only a very brief discussion of its proof in order to indicate why it is more complex thanmight be expected.

Proof. (Sketch) We note that in the primitive recursive scheme we make available at each step a copy of a subtree of thedata type corresponding to the point in the computation we have reached. However, this is determined if we can provide apath to the occurrence of that subtree and the original tree. Any recursion over the subtree can be simulated by a recursionover the whole tree in which the path to the occurrence of the subtree is also provided.

As the original tree is in the opponent context it is freely available inside the player computation in context. The problemis to make sure that the path is also available whenever the subtree is used: the path is passed as an inherited argumentso is tensored. The path can be propagated over coproducts and products as tensor is strong over these:

X ⊗ (A × B) −→ (X ⊗ A) × (X ⊗ B), X ⊗ (A + B) −→ X ⊗ A + X ⊗ B

However, the tensor is not strong over itself so in general there is no map

X ⊗ (A ⊗ B) −→ (X ⊗ A) ⊗ (X ⊗ B)

as this requires the ability to duplicate X . However, we can duplicate paths as we may use the power of the original datatype (i.e. a recursion with subject the original data) to duplicate the path: after all the path is a path of that data.

This means we may propagate a path and the original data type to any point in the calculation which allows us tosimulate primitive recursion. �3.3. Coinductive data

If G is a polarized operator on a Pola category a coinductive fixed point for G is an object νz.G(z) with a player isomor-phism:

dest : νz.G(z) −→ G(νz.G(z)

)such that given any G-coalgebra in context g : (X, B) −→ G(B) there is a unique map unfold(g) : (X × B,1) −→ νz.G(z) makingthe following diagram commute:

(X × B,1)

unfold(g)

(〈π0,↑(g↑)〉,1)(X × G(B),1)

G Xo (unfold(g))

νz.G(z) G(νz.G(z))

dest
Page 13: Safe recursion revisited I: Categorical semantics for lower complexity

M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45 31

The coinductive (fixed point) recursion principle is the unique existence of the unfold map in the above square. Notice that thestrength of G (if it has one) is not used at all.

As mentioned in the introduction, a Pola category in which all basic polarized operators have inductive and coinductivefixed points is called a Pola setting.

4. Examples of Pola settings

This section introduces two key examples of Pola categories: R-sized sets and R-amortized sets. R-sized sets, has asobjects, sets in which every element has an associated size, where this “size” is just an element of the rig R. Cross-mapsare then set maps which increase the player size by a factor bounded by a polynomial in the sizes of the opponent context.When the rig is well-complete, that is has non-empty infima, this Pola category also has inductive data for all basic polarizedfunctors. This delivers a semantic proof that the size complexity of maps in the initial Pola category with inductive datais polynomially bounded – which, as discussed in the introduction, is a first step in showing polynomial space and timebounds.

The category of R-sized sets does not have coinductive data and so provides no guidance as to what should be meant byasserting that a map, which for example manipulates infinite lists, has a size complexity which is “polynomially bounded”.In order to make these notions more precise, the more sophisticated category of R-amortized sets is introduced. To definethis category we use power polynomials and techniques which have also been used in [3] to bound the complexity ofhigher-order linear systems. When the rig, R, is the natural numbers, the resulting setting does have both inductive andcoinductive data. Furthermore, it provides a precise interpretation of the sense in which functions involving coinductivetypes are polynomially bounded (see particularly Remark 4.11).

Unfortunately, the category of R-amortized sets is not quite a Pola category: it fails to be closed with respect to theaffine structure. Significantly, however, when R is the natural numbers (R = N), it is weakly affine closed. To obtain asetting which is a Pola category and has both coinductive and inductive data it is necessary to make one further formal stepto the category of R-amortized equivalences. This setting is then a Pola category with, when R = N, both inductive andcoinductive data.

4.1. The category of R-sized sets

Let R = (R,+, ·,0,1,�) be a size rig: that is an ordered (commutative) rig R with bottom element 0 (so that 0 � r forall r ∈ R), and order-preserving operations. The canonical rig to have in mind is the natural numbers N but R�0 and Q�0are also examples.

The category of R-sized sets with polynomially bounded maps, denoted R-Seto , is constructed as follows:

Objects: R-sized sets A = (U (A), |_|A), where U (A) is the underlying set and |_|A : U (A) −→ R is the size function. Oftenwe shall identify the underlying set and the object. We think of R-sized sets as sets, A, each of whose elementsa ∈ A has an assigned R-size, |a|A .

Maps: A map f : A −→ B is a map between the underlying sets f : U (A) −→ U (B) such that there exists a polynomialp ∈R[x] such that for all a ∈ U (A), | f (a)|B � p(|a|A):

U (A)

�|_|A

fU (B)

|_|B

R p R

We say p is a polynomial bound for f .Composition: The usual (set) function composition. The composite is bounded by substituting the bounding polynomials.

Identities are the identity functions with the identity as a polynomial bound.

It is clear that this forms a category. One way to define a product for R-sized sets with polynomially bounded maps isas A × B where U (A × B) = U (A) × U (B) and |(a,b)|A×B = |a|A + |b|B . The projection maps, π0 and π1, are then boundedby the identity polynomial. Given f : C −→ A and g : C −→ B , the tuple map 〈 f , g〉 is bounded by p + q, where p is a boundfor f and q is a bound for g . The terminal object is the R-sized set 1 with U (1) = {()} and |()|1 = 0.

The category of R-sized sets and constant bounded maps, denoted R-Setp , is the subcategory of R-Seto consisting ofR-sized sets (so the objects are unchanged) and functions f : A −→ B such that there exists a constant c ∈ R such that forall a ∈ A, | f (a)|B � |a|A + c.

Proposition 4.1. R-Set is a strong polarized category which is bundled.

Proof. It remains to describe the module structure. A cross-map (A, B) −→ C is a function f : U (A) × U (B) −→ U (C) suchthat there exists a polynomial p ∈R[x] with, for all (a,b) ∈ U (A) × U (B), | f (a,b)|C � p(|a|A) + |b|B .

Page 14: Safe recursion revisited I: Categorical semantics for lower complexity

32 M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45

The strong composition is defined as follows: given f : (A1, B1) −→ B2 and g : (A2, B2) −→ C bounded by p and q, respec-tively, their composite is defined by ( f ; g)((a1,a2),b1) = g(a2, f (a1,b1)) and is bounded by p + q as:∣∣( f ; g)

((a1,a2),b1

)∣∣C = ∣∣g

(a2, f (a1,b1)

)∣∣C � q

(|a2|A2

) + ∣∣ f (a1,b1)∣∣

B2

� q(|a2|A2

) + p(|a1|A1

) + |b1|B1

� (p + q)(∣∣(a1,a2)

∣∣A1×A2

) + |b1|B1

The identity cross-maps ιA : (1, A) −→ A are given by second projection and are bounded by the constant 0. This is clearlybundled. �

We observe in addition:

Lemma 4.2. The polarized strong category R-Set is distributive: in fact, it has coproducts which are extensive.

Proof. We require that both the category R-Setp and the category R-Seto have coproducts and that the coproduct actacross the module maps in two different ways. The coproduct of R-sized sets is A + B with U (A + B) = U (A) + U (B)

where, for a ∈ U (A), |a|A+B = |a|A + 1 and for b ∈ U (B), |b|A+B = |b|B + 1. The injections are the usual set-theoretic onesand are clearly constant bounded (by 1). Consider the copairing in the player position: given f : (X, A) −→ C and g : (X, B)

−→ C , constant bounded by p and q, respectively, then it is easy to see that the copairing map 〈 f |g〉 : (X, A + B) −→ C isbounded by p + q. The coproducts are extensive as they are inherited from sets. �

In order to obtain the additional structure on R-sized sets to make a Pola category it is necessary to assume that thesize rig R has a further property. A size rig is well-complete when it has infima for all non-empty sets,

∧i∈I xi , and these

are preserved, in each argument, by the operations.

Lemma 4.3. Let R be a well-complete size rig then:

(i) R has maxima for all finite sets, max(x1, . . . , xn);(ii) max(a + x1,a + x2, . . . ,a + xn)� a + max(x1, . . . , xn);

(iii) max(a · x1,a · x2, . . . ,a · xn) � a · max(x1, . . . , xn);(iv) R has an element x . y such that y + z � x if and only if z � x . y.

Proof.

(i) Define max(x1, . . . , xn) = ∧{z | z � xi, i = 1, . . . ,n} this is a non-empty set as x1 + · · · + xn is clearly in it. Note thatmax() = 0.

(ii) We have

a + max(x1, . . . , xn) = a +∧{

z′ ∣∣ x1 � z′, . . . , xn � z′}=

∧{a + z′ ∣∣ x1 � z′, . . . , xn � z′}

�∧

{z | a + x1 � z, . . . ,a + xn � z}= max(a + x1, . . . ,a + xn)

where the inequality is because {a + z′ | x1 � z′, . . . , xn � z′} ⊆ {z | a + x1 � z, . . . ,a + xn � z}.(iii) Similar to (ii).(iv) Define x . y = ∧{z | y + z � x}. This set is non-empty as it contains x. If y + z � x then by definition z � x . y. For the

converse it suffices to show that y + (x . y)� x:

y + (x.

y) = y +∧

{z | y + z � x} =∧

{y + z | y + z � x}�∧

{v | v � x} � x �Both N and R�0 are examples of well-complete size rigs, however, Q�0 is not. We shall be assuming that R is a

well-complete size rig from now on. Our first observation which uses maxima is:

Lemma 4.4. When R is a well-complete size rig then R-Set has products.

Proof. We already have seen that the opponent category has products, however, this definition does not give products inthe player category. Define a new product of R-sized sets by A × B having U (A × B) = U (A) × U (B) as before but now set

Page 15: Safe recursion revisited I: Categorical semantics for lower complexity

M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45 33

|(a;b)|A×B = max(|a|A, |b|B) (note we use semicolon to distinguish this tupling). The tuple of the maps f : (X, A) −→ B andg : (X, A) −→ C , bounded by respectively | f (x,a)|B � |a|A + p(|x|X ) and | f (x,a)|B � |a|A + q(|x|X ), is 〈 f , g〉 : (X, A) −→ B × C ,and this is bounded by:

∣∣( f (x,a); g(x,a))∣∣

B×C = max(∣∣ f (x,a)

∣∣B ,

∣∣g(x,a)∣∣C

)� max

(|a|A + p(|x|X

), |a|A + q

(|x|X))

� |a|A + max(

p(|x|X

),q

(|x|X))

Projections are given by the usual projection functions and are bounded by the constant 0. The uniqueness of tupling forthis product is delivered by the underlying interpretation in Set. �Proposition 4.5. When R is a well-complete size rig then R-Set is a Pola category.

Proof. Lifting is clearly the identity on R-sized sets.It remains to define the affine structure. Define A ⊗ B to have U (A ⊗ B) = U (A)× U (B) and |(a,b)|A⊗B = |a|A +|b|B . The

tensor product of maps f : (X, A1) −→ B1 and g : (X, A2) −→ B2 has ( f ⊗ g)(x, (a1,a2)) = ( f (x,a1), g(x,a2)) and is boundedby the sum of the constant bounds for f and g . The tensor is affine as the tensor unit is the terminal object. In R-Seto wealready know this is a product.

The internal hom A � B is defined by setting U (A � B) to be the set of constant R-sized maps from A to B , and

| f |A�B =∧{

c∣∣ ∀a ∈ A.

∣∣ f (a)∣∣

B � |a|A + c}

The evaluation map evX : (X, A ⊗ (A � B)) −→ B is given by evX (x, (a, f )) = f (a) and is bounded as | f (a)|B � |a|A +| f |A�B .Given a constant bounded map g : (X, A ⊗ C) −→ B so that |g(x, (a, c))|B � |a|A + |c|C + p(|x|X ), then cur(g)(x, c) =

λa.g(x, (a, c)) is bounded as:

∣∣cur(g)(x, c)∣∣

A�B =∧{

k∣∣ ∀a ∈ A.

∣∣cur(g)(x, c)(a)∣∣

B � |a|A + k}

=∧{

k∣∣ ∀a ∈ A.

∣∣g(x, (a, c)

)∣∣B � |a|A + k

}� |c| + p

(|x|X) �

Notice the diagonal map � : A −→ A ⊗ A is not in general bounded by a constant, so the tensor product and the Cartesianproduct are distinct in R-Setp . However, the two are definitely isomorphic in R-Seto as the diagonal is bounded by thepolynomial 2x ∈R[x].

R-sized sets has inductive data types:

Proposition 4.6. The Pola category R-Set has inductive data for all the basic polarized operators.

Proof. Every polarized operator in this setting (as products and tensors distribute over coproducts) may be expressed in“disjunctive form” as F (Z) = F1(Z) + · · · + Fk(Z) where each Fi is generated by constants, products, and tensors. Further-more, if F is a strong combinator then each Fi must be generated by just products and constants.

Such a functor always has a fixed point in Set, F @, which is the free algebra generated by the constructors consi .Furthermore, F @ can be turned into an R-sized set by defining |_|F @ : F @ −→R inductively:

∣∣consi(d)∣∣

F @ = 1 + |d|F @∣∣(d1; . . . ;dn)∣∣

F @ = max(|d1|F @ , . . . , |dn|F @

)∣∣(d1, . . . ,dn)

∣∣F @ =

n∑i=1

|di|F @

|a|F @ = |a|A (where a ∈ A is constant)

Each constructor increases the size of its input by 1, so they are certainly maps in the player category. Note that theisomorphism cons of Section 3.2 is the cotuple of these constructors, and so it too is bounded by a constant size increase.The inverse map is size decreasing and so is also in R-Setp . Thus, it is a fixed point in R-Setp .

Page 16: Safe recursion revisited I: Categorical semantics for lower complexity

34 M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45

To bound the maps produced by the recursion principles it is convenient to use the inductive fixed point recursionprinciple (initial algebra principle). It thus suffices to show we can polynomially bound the map fold defined recursively by:

(X × F (μx.F (x)), Y )(1×↑(consF ),1)

(π0,〈F X,Yop (fold),ιY 〉)

(X × μx.F (x), Y )

fold

(X, F (B) × Y ) g B

Clearly this depends on g which, being in the p-world, is constant bounded by a factor dependent on the o-context:∣∣g(x|z; y)∣∣ � max

(|z|, |y|) + P g(|x|)

Without loss of generality we may assume P g(|x|)� 1.We shall deal separately first with the case when F is a strong polarized operator. In that case the constructors take the

general form of a constructor applied to a product tuple, consi(a; t1; . . . ; tn), in which all but one argument can be assumedto recursively expect a term of the data type itself. As an inductive hypothesis we shall suppose that∣∣fold(x, w|y)

∣∣ � |w| · P g(|x|) + |y|

where |w| � 1 as it must have at least one constructor. It suffices to show that, assuming this is true of the recursivearguments of consi(a; t1; . . . ; tn), that it is true for the whole element (the base cases are when there are no recursivesubterms, i.e. n = 0).∣∣fold

(x, consi(a; t1; . . . ; tn)

∣∣y)∣∣

= ∣∣g(x∣∣a; fold(x, t1|y); . . . ; fold(x, tn|y); y

)∣∣� max

(|a|, ∣∣fold(x, t1|y)∣∣, . . . , ∣∣fold(x, tn|y)

∣∣, |y|) + P g(|x|)

� max(|a|, |t1| · P g

(|x|) + |y|, . . . , |tn| · P g(|x|) + |y|, |y|) + P g

(|x|)� max

(|a|, |t1| · P g(|x|), . . . , |tn| · P g

(|x|)) + |y| + P g(|x|)

� max(|a|, |t1|, . . . , |tn|) · P g

(|x|) + P g(|x|) + |y|

= (max

(|a|, |t1|, . . . , |tn|) + 1) · P g

(|x|) + |y|= ∣∣consi(a; t1; . . . ; tn)

∣∣ · P g(|x|) + |y|

In the case when the polarized operator is not strong this means that the operator uses tensor tupling as well as producttupling. Thus a typical term of the data type takes the form consi(W(a1, . . . ,ar, t1, . . . , tn)) where W is a word in productand tensor tuples, ai are constants and t j are the recursive occurrences of the data type. This word produces a calculation|W| in terms of max and + which produces the size of the term:∣∣consi

(W(a1, . . . ,ar, t1, . . . , tn)

)∣∣ = 1 + |W|(|a1|, . . . , |ar |, |t1|, . . . , |tn|)Observe that |W|(x1 · z, . . . , xn · z) � |W|(x1, . . . , xn) · z as both addition and max has this property: this allows a similarargument to the above.

We start with the inductive hypothesis that |fold(x, w)| � |w| · P g(|x|) then we have:∣∣fold(x, consi

(W(a1, . . . ,ar, t1, . . . , tn)

))∣∣= ∣∣g

(x∣∣W(

a1, . . . ,ar, fold(x, t1), . . . , fold(x, tn)))∣∣

� |W|(|a1|, . . . , |ar |,∣∣fold(x, t1)

∣∣, . . . , ∣∣fold(x, tn)∣∣) + P g(x)

� |W|(|a1|, . . . , |ar |, |t1| · P g(|x|), . . . , |tn| · P g

(|x|)) + P g(x)

� |W|(|a1|, . . . , |ar |, |t1|, . . . , |tn|) · P g(|x|) + P g(x)

�(1 + |W|(|a1|, . . . , |ar |, |t1|, . . . , |tn|

)) · P g(|x|)

= ∣∣consi(W(a1, . . . ,ar, t1, . . . , tn)

)∣∣ · P g(|x|) �

Remark 4.7. The argument that the fold map is polynomially bounded for a strong polarized operator is slightly differentfrom that of a general polarized operator which is not strong. The strong polarized operators were implicit in both [18] and[2]. The argument for the size bound in those works is essentially the argument presented here. Polarized operators whichuse the affine structure where fundamental in [11] and the argument for this size bound follows the intuition laid out byHofmann as discussed in the introduction.

Page 17: Safe recursion revisited I: Categorical semantics for lower complexity

M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45 35

4.2. The category of R-amortized sets

The polarized category of R-sized sets while having inductive data unfortunately lacks coinductive data. This lack isdirectly connected to the question of how the “size” of coinductive data is measured and, in order to understand this, it isnecessary to consider a more sophisticated setting. The setting we introduce in this section contain R-sized sets but alsocontain much more. Crucially, it embodies the description of how the “size” of coinductive data is measured in terms ofamortizing functions. It transpires that this setting is not quite a Pola category as it is not affine closed and this leads to thedevelopment in the next section in which this defect is addressed.

An R-amortized set, where R is a well-complete size rig, X = (U (X),‖_‖X ), is a set U (X) together with an “amortizedsize”, that is a function ‖_‖X : U (X) −→R⇒R, which associates to each element an order preserving endomorphism of R.We shall adopt a special notation denoting ‖x‖X • r, that is the amortizing function of x applied to r, simply by xr : inparticular we shall often identify an element in an amortized set with its amortizing function omitting mention of ‖_‖X .

Remark 4.8. Eventually we shall focus on the special case when R= N, in that case we note:

(1) When a map is infima preserving it certainly is monotone: if the domain is well-ordered, which is so for the rigof natural numbers, then every set contains its infima and so order preserving functions from the natural numbersautomatically preserve non-empty infima!

(2) The intuition behind an N-amortized set is that any computation, whose result is a coinductive data type, will beevaluated (lazily) to a head normal form. However, this form can always be expanded by forcing the evaluation of its“closures”: this is called “poking”. Clearly, the more that the result is poked, the more that is revealed of the result. The“amortized size” of the computation is the relationship between the amount of poking and the revealed size. Thus, thevalue xn may be thought of as the “size” of the result given that we have forced the evaluation by n pokes.

An ordered rig generates a Cartesian closed theory P(R), which is a “left rig category”: its objects are products ofhigher-order types in R (such as R ⇒ R, (R ⇒ R) ⇒ R), etc.) with maps obtained by freely adding Cartesian closedstructure to the Lawvere theory of the rig (see further discussion in Remark 4.9 below). One may view the maps of P(R) asλ-terms built on top of the syntax of polynomials. We refer to these terms as power-polynomials: they are generated freelyfrom the rig operations, λ-abstraction and application. The homsets of this category all have a canonical rig structure (andorder) which is determined by the rig. Thus, R has, by assumption, a rig structure but then R ⇒ R does by defining itpointwise, and whence R⇒ (R⇒R) does, etc. Composition in this category, however, only preserves this rig structure onthe left, that is f (h + g) = f h + f g and f (h · g) = ( f h) · ( f g).

A power-polynomial of type (R⇒R) ⇒ (R⇒R) can be applied, to an amortized function to generate a new amortizedfunction. They can thus be used to bound the changes in amortized size caused by maps in the category of amortized sets.

As for R-sized sets, we define two categories: the category of R-amortized sets with power-polynomial bounded maps,(AR)o , and the category of R-amortized sets with constant bounded maps, (AR)p . The category of R-amortized sets withpower-polynomial bounded maps, (AR)o , is defined as follows:

Objects: R-amortized sets.Maps: Set maps f : X −→ Y between the underlying sets such that there is a power-polynomial ϕ(a : R ⇒ R,b : R) : R

such that

f (x)r � ϕ(‖x‖, r

)The power-polynomials are to be thought of as bounding the size of f (x)r by bounding both how much x can be poked

and the resulting increase in size based on what is exposed. They can become quite complicated: for example we mighthave

ϕ(‖x‖, r

) = (‖x‖ • (r + P )) · (‖x‖ • ((‖x‖ • (r + P )

) + K)) + K ′ = xr+P · xxr+P +K + K ′

where note the double subscription. However, it is worth keeping in mind that if all the functions substituted into a powerpolynomial are polynomials then any resulting function (between first order-types) will just be a polynomial! This is crucialto the semantics we are developing.

Remark 4.9. That no more than polynomials are produced by power-polynomials is not so obvious, however, it is true ingreat generality. Given any Cartesian category such as the theory of rigs which we may call Poly then the power-polynomialsare obtained by freely adding higher-order types to obtain P(Poly). We wish to know whether the inclusion I : Poly −→P(Poly) is full and faithful. To see it is faithful is easy: we may use the Yoneda embedding

Y : Poly −→ SetPolyop

Page 18: Safe recursion revisited I: Categorical semantics for lower complexity

36 M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45

which provides a full and faithful embedding of Poly into a Cartesian closed category which may be extended to P(Poly) –as P(Poly) is freely generated from Poly, to give the diagram:

Poly

Y

IP(Poly)

Y�

SetPolyop

as the Yoneda functor is faithful I must be as well. This does not show that it is full: to see this requires the cut-eliminationtheorem. However, as we are only considering the evaluation of the power-polynomials, this sophistication is not needed:we may view the power polynomials as being the image of Y� in SetPolyop

.

This is a category: the identity map, 1X : X −→ X has ϕ(‖x‖, r) = xr . The composite f g has bound given by the followingcalculation:

g(

f (x))

r � ϕg(∥∥ f (x)

∥∥, r)� ϕg

(λs.ϕ f

(‖x‖, s), r

)where the bounding expression on the right is still of the correct form.

Furthermore, this category has products. The final object is the one element set 1 = {()} with ()r = 0 for every r: thismakes the final map ! : X −→ 1 bounded as, for example, ()r = 0 � xr . The binary product of two R-amortized sets withrespect to polynomially bounded maps may be defined in various ways. For example, take the usual set theoretic Cartesianproduct, X × Y , and define (x; y)r = max(xr, yr) then the projections are clearly bounded as xr � (x; y)r = max(xr, yr) andif f : Z −→ X and g : Z −→ Y are suitably bounded maps then 〈 f , g〉 the pairing map is also bounded as:(

f (z); g(z))

r = max(

f (z)r, g(z)r)

� max(ϕ f

(‖z‖, r),ϕg

(‖z‖, r))

� (ϕ f + ϕg)(‖z‖, r

)where we again use the fact that addition of polynomials always exceeds the maximum.

Lemma 4.10. (AR)o is a lextensive category into which R-sized sets with polynomial bounded maps embeds.

Proof. Coproducts are given by disjoint union of sets as expected. Only finite coproducts are available in general as the newbounds for copairing maps are obtained by taking the maximum of the old bounds.

R-sized sets embeds in this as the amortized sets which have a constant size xr = xs for all r, s ∈N. �Remark 4.11. It is instructive to consider what the elements of the category (AR)o are. An element is a map e : 1 −→ X ,which picks out a bounded element of U (X); that is an element such that e()r � ϕ(‖()‖, r) for some power-polynomial ϕ .However, because ()n = 0 for every n ∈R this reduces to a straight polynomial in r. Thus, e()r � p(r) meaning that the sizeof e poked r times is bounded in a polynomial in r.

The category of R-amortized sets with constant bounded maps, (AR)p , is defined as follows:

Objects: R-amortized sets.Maps: Set maps f : X −→ Y between the underlying sets such that there is a P , K ∈R with f (x)r � xr+P + K .

In this category a map f is allowed to poke its domain a constant number, P , of times more than it is itself poked. Theresult must then have a size which is at most a constant K greater than the size revealed by this poking of its argument.

This is a category: identity maps are clearly constant bounded, the composition f g of two constant bounded maps isitself constant bounded as

g(

f (x))

r � f (x)r+P g + K g � xr+P g+P f + K g + K f

Furthermore the category has products given by X × Y , where note that we use the semicolon as the separator for thesepairs as we shall shortly introduce some further pairing operations, with (x; y)r = max(xr, yr) as above. The calculation forthe pairing map becomes:(

f (z); g(z))

r = max(

f (z)r, g(z)r)

� max(zr+P f + K f , zr+P g + K g)

� zr+P +P g + K f + K g

f
Page 19: Safe recursion revisited I: Categorical semantics for lower complexity

M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45 37

which is a constant bounded map. As for polynomial bounded maps, the disjoint union gives coproducts so the category isextensive.

In addition this category has (various) affine tensor products which are distinct from the product, although their under-lying sets are the same. These tensor products makes (AR)p into an affine category (as their unit is the final object) invarious ways. We shall focus on the following tensor: define X ⊗ Y to have its underlying set the usual Cartesian product,where now we use the comma to separate pairs, such that (x, y)r = xr + yr . Intuitively to poke a tensor product r times wenow are allowed to poke both components that many times.

We must verify that this is a tensor product with unit the final object: all the coherence diagram follow once it is verifiedthat the appropriate set coherence maps for the product are constant bounded. That this is the case is very straightforwardto check. We shall illustrate, in detail, why this tensor is a functor leaving the rest to the reader. For this, suppose f : X−→ X ′ and g : Y −→ Y ′ are constant bounded then we must show that f ⊗ g is constant bounded:(

f (x), g(y))

r = f (x)r + g(x)r

� xr+P f + K f + yr+P g + K g

� xr+P f +P g + yr+P f +P g + K f + K g

= (x, y)r+P f +P g + K f + K g

Thus, (AR)p is an extensive affine category in which also this tensor distributes over the coproduct (as its underlyinginterpretation in sets does).

In fact, (AR)p has more than one affine tensor. To illustrate some of the highly separated structure of the category(AR)p we shall explore another possible tensor. Define X ⊕ Y to again have underlying set X × Y , where this time wedenote the pairs using a wriggle for the separator, define the amortizing function by:

(x � y)n = max{xr + ys | r + s = n}Now it is not immediately clear that this is well-defined as the maximum of general sets is not necessarily defined. However,in this case we have:

Lemma 4.12. In (AR)p , ⊕ is an affine tensor and there are natural transformations (which are not natural isomorphisms) X ⊗ Y−→ X ⊕ Y −→ X × Y which underlies to the identity map in sets.

Proof. To establish that (x � y)r exists it suffices to exhibit an element greater than all the elements in the set as thenone can take the infimum of all the elements above all the elements in this set. Of course, such an element is (x, y)r asn � n + m = r so that, because growth is monotone, xn � xr and, similarly, ym � yr . But this means xn + ym � xr + yr .

It is straightforward to check that ⊕ is an affine tensor on (AR)p . We show that it is a functor leaving the coherencesfor the reader to check:(

f (x) � g(y))

r = max{

f (x)n + g(x)m∣∣n + m = r

}� max{xn+P f + K f + ym+P g + K g |n + m = r}� max{xn+P f + ym+P g |n + m = r} + K f + K g

� max{xn + ym|n + m = r + P f + P g} + K f + K g

= (x � y)r+P f +P g + K f + K g

The inequality above makes the identity map in sets into a natural (monoidal) transformation X ⊗ Y −→ X ⊕ Y . It is nota natural isomorphism as to be so would require

(x, x)r = xr + xr � max{xn + xm|n + m = r + P } + K

Consider, an element x such at xr = r then this requires constants P and K such that 2r � r + P + K which is impossible.There is also a natural map X ⊕ Y −→ X × Y which underlies to the identity between sets as

(x; y)r = max(xr, yr) � max(xr + y0, x0 + yr) � max{xn + ym|n + m = r}Furthermore, this does not have a constant bounded inverse even for R-sized sets seen as amortized sets. �Remark 4.13.

(1) The series of natural transformations X ⊗ Y −→ X ⊕ Y −→ X × Y are not natural isomorphisms in (AR)p . It is interestingto observe, however, that for R-sized sets, regarded as constant amortized sets, the first two tensors are equivalent. Theseparating counter-example in the lemma relies fundamentally on having a non-trivial growth function for amortization.Thus, this indicates something of the richer structure of R-amortized sets when compared to R-sized sets.

Page 20: Safe recursion revisited I: Categorical semantics for lower complexity

38 M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45

(2) The intuition of the tensor, ⊕, is as follows: both components are available for being amortized, thus poking this tensorn times involves sharing the pokes between the components. That is one can poke one component r times and then theother object s times so long as the total amount of poking r + s is bounded by n. This is an attractive intuition, however,we shall use ⊗ in what follows because it is simpler to form a (weak) affine closed setting based on this tensor.

To obtain a strong polarized category we define the module maps (AR)o × (AR)p −→ (AR)p : a cross-map h : (X, Y ) −→ Zis a set map from h : X × Y −→ Z such that there is a power-polynomial ϕ and a constant P with

h(x, y)r � ϕ(‖x‖, r

) + yr+P

It is clear that this gives a module with a well-defined composition.

Proposition 4.14. The module (AN)o × (AN)p −→ (AN)p gives a strong polarized affine category (with respect to ⊗), with productsand extensive coproducts, which is bundled. Furthermore, when R= N it is weakly closed (with respect to ⊗).

Proof. The remaining issue is the weak affine closed structure: for this we shall assume R = N. We shall, for notationalsimplicity, ignore the context which does not add any conceptual complications. We define the internal hom A �w B toconsist of pairs ( f , P ) where f : A −→ B is a set map and P ∈ R such that for all n ∈ R there is a k ∈ R such that for alla ∈ A f (a)n � an+P + k:

A �w B = {( f , P ) : A −→ B

∣∣ ∀n : R.∃k : R.∀a : A. f (a)n � an+P + k}

Note that k depends on n : R but not on a : A. For a given set map there is no necessity for such a P to exist. However, if( f , P ) is in this set then for every P ′ � P the pair ( f , P ′) will also be in this set. When R= N, as N is well-ordered, therewould be a least P such that this condition is true.

The amortization function for ( f , P ) is defined as:

( f , P )n ={

0 n < P

inf{k : R|∀m.P + m � n ⇒ ∀a : A. f (a)m � aP+m + k} n � P

For this to be well-defined we must be assured that there is actually a k with the desired property so that the set isnon-empty. This follows as there are only finitely many m ∈ N, with P + m � n, and, by assumption, for each such m thereis a k making f (a)m � aP+m + k for every a. This means that the maximum of these k will inhabit the set.

Notice that if ( f , P ), ( f , P ′) ∈ A �w B with P < P ′ then ( f , P ′)n � ( f , P )n so the more delay one allows (i.e the largerP is) the smaller the amortized cost becomes.

The evaluation map then has

evw(a, ( f , P )

)n = f (a)n � an+P + ( f , P )n+P = (a, f )n+P

and so is certainly bounded.Consider now a constant bounded map g : A ⊗ C −→ B with say:

g(a, c)n � (a, c)n+Q + K = an+Q + cn+Q + K

We show that(curw(g), Q

) : C −→ A �w B; c �→ (curw(g)(c), Q

)where curw(g)(c) = λa.g(a, c), is well-defined and bounded. It is well-defined as

g(a, c)n � an+Q + cn+Q + K ⇒ (curw(g)(c), Q

)n+Q � cn+Q + K

so that (curw(g)(c), Q )n+Q is defined at each n, thus (curw(g)(c), Q ) ∈ A �w B for each c. This also has tells us we havethe required bound, (curw(g)(c), Q )n � cn + K : above we have seen the bound held when n � Q and for n < Q , as we set(curw(g), Q )n = 0, it always holds. �

This higher-order structure makes explicit the “look ahead” required by a function: the cost of having to do so makesthe structure weak. In the next section we shall show how this weakness can be removed to provide a full Pola setting.Meanwhile we describe the main reason for moving to amortized sets: namely that these sets support both inductive and –in the case where R = N – coinductive data. In both cases we build the appropriate fixed point in sets and provide it withan amortized cost and then prove that the fold or unfold map is suitably bounded.

Proposition 4.15. AR has inductive data for all basic polarized operators.

Page 21: Safe recursion revisited I: Categorical semantics for lower complexity

M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45 39

Proof. Inductive data follows the same pattern as for R-sized sets:

consi(d)r = 1 + dr

(d1; . . . ;dn)r = max((d1)r, . . . , (dn)r

)(d1, . . . ,dn)r =

n∑i=1

(di)r

ar = ar (when a is atomic)

There are two cases as before to handle: when the polarized operator is strong and when it is not. Below we give theproof for when it is not strong leaving the rest to the reader. Suppose g : (X, F (B)) −→ B has g(x, t)r � ϕ(‖x‖, r) + tr+P thenwe inductively assume fold(x, t)r � ϕ(‖x‖, r + t0 · P ) · tr+t0·P 8 and, letting t′ := consi(W(a1, . . . ,as, t1, . . . , tm)), now calculate:

fold(x, t′)

r

= fold(x, consi

(W(a1, . . . ,as, t1, . . . , tm)

))r

= g(x∣∣W(

a1, . . . ,as, fold(x, t1), . . . , fold(x, tm)))

r

� ϕ(‖x‖, r

) + |W|((a1)r+P , . . . , fold(x, tm)r+P)

� ϕ(‖x‖, r

) + |W|((a1)r+P , . . . ,ϕ(‖x‖, r + P + (tm)0 · P

) · (tm)r+P+(tm)0·P)

� ϕ(‖x‖, r

) + |W|((a1)r+P , . . . ,ϕ(‖x‖, r + (

1 + (tm)0) · P

) · (tn)r+(1+(tm)0)·P)

� ϕ(‖x‖, r

) + |W|((a1)r+P , . . . ,ϕ(‖x‖, r + (

t′)0 · P

) · (tn)r+(t′)0·P)

� ϕ(‖x‖, r

) + ϕ(‖x‖, r + (

t′)0 · P

) · |W|((a1)r+P , . . . , (tn)r+(t′)0·P)

� ϕ(‖x‖, r + (

t′)0 · P

) · (1 +W((a1)r+P , . . . , (tn)

)r+(t′)0·P

)� ϕ

(‖x‖, r + (t′)

0 · P) · (t′)

r+(t′)0·P �We shall now specialize to the case R= N in order to describe coinductive data. Recall for coinductive data the solution

in sets is a set of infinite trees. Each tree in t ∈ νx.G(x) can be unwrapped one step to a tree

dest(t) = σi(Wi(a1, . . . ,as, t1, . . . , tn)

) ∈ G(νx.G(x)

)where the prefix Wi(a1, . . . ,as, x1, . . . , xn) is from the type G(X). We define the amortized cost of such a prefixed term by:

[σi

(Wi(a1, . . . ,as, t1, . . . , tn)

)]r = 1 + [

Wi(a1, . . . ,as, t1, . . . , tn)]

r[(W1(a11, . . . , t1m1); . . . ;Wn(an1, . . . , tnmn )

)]r = max

i=1,...,nWi(ai1, . . . , timi )r

[(W1(a11, . . . , t1m1), . . . ,Wn(an1, . . . , tnmn )

)]r =

n∑i=1

[Wi(ai1, . . . , timi )

]r

Then we define for t ∈ νx.G(x) the amortized cost to be:

tn ={

0 when n = 0

[dest(t)]n−1 when n > 0

Notice the way this definition works: to calculate the r size of a coinductive data type involves unwinding it recursively rtimes: at the rth stage all the recursive subtrees in νz.G(z) are evaluated at 0 and so have zero cost. This has the crucialconsequence that the (r + 1)-size of these trees can be inductively defined in terms of the r-size using a single unwindingstep.

Theorem 4.16. The strong polarized category of N-amortized sets, AN , has both inductive and coinductive data for all basic polarizedoperators.

8 The reader may reasonably wonder why t0 suddenly appears this formula! In fact, t0 is being used to bound the number of constructors – for which itis a bound albeit a very poor one.

Page 22: Safe recursion revisited I: Categorical semantics for lower complexity

40 M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45

Proof. We start by observing that the destruction for coinductive data is constant bounded as for any element in thecoinductive type t0 = 0 and tr+1 := dest(t)r so dest(t)r � tr+1 always (so a “look ahead” of 1 is required to account for onelevel less of destruction). The inverse of destruction clearly does not increase size so is also constant bounded.

Suppose now we are given a player coalgebra in context, g : (X, B) −→ G(B), then this has a power-polynomial bound ofthe form g(x|b)r � ϕg(‖x‖, r) + br+P where without loss we can assume P > 0. We need to provide a power-polynomialbound for the unfold in the following couniversal diagram:

X × B

unfold

〈π0,↑(g)〉X × G(B)

G Xop(unfold)

νz.G(z)dest

G(νz.G(z))

We shall use the fact that unfold = 〈π0,↑(g)〉 G Xop(unfold) dest−1 to effect a proof by induction. Our hypothesis is that

unfold(b, x)n � n · ϕg(‖x‖,n · P ) + bn·P Note that when n = 0 it is trivially true as unfold(x,b)0 = 0. We suppose it true for nthen we have:

unfold(x,b)n+1

= dest−1(G Xop(unfold)

(⟨π0,↑(g)

⟩(x,b)

))n+1

= G Xop(unfold)

(⟨π0,↑(g)

⟩(x,b)

)n

= G Xop(unfold)

(x, g(x,b)

)n

= W(a1, . . . ,as,unfold(x,b1), . . . ,unfold(x,bm)

)n

where g(x,b) := W(a1, . . . ,as,b1, . . . ,bm)

= |W|((a1)n, . . . , (as)n,unfold(x,b1)n, . . . ,unfold(x,bm)n)

� |W|((a1)n, . . . , (as)n,n · ϕg(‖x‖,n · P

) + (b1)n·P , . . . ,n · ϕg(‖x‖,n · P

) + (bm)n·P)

� n · ϕg(‖x‖,n · P

) + |W|((a1)n, . . . , (as)n, (b1)n·P , . . . , (bm)n·P)

� n · ϕg(‖x‖,n · P

) +W(a1, . . . ,as,b1, . . . ,bm)n·P recall P > 0

= n · ϕg(‖x‖,n · P

) + g(x,b)n·P� n · ϕg

(‖x‖,n · P) + ϕg

(‖x‖,n · P) + bn·P+P

= (n + 1) · ϕg(‖x‖,n · P

) + b(n+1)·P� (n + 1) · ϕg

(‖x‖, (n + 1) · P) + b(n+1)·P �

4.3. Amortized equivalences

It is clear that we should like, for (AR)p , to be able say that in the amortized set A �w B that the elements ( f , P )

and ( f , P ′) are somehow equivalent. To show that this is possible we will move to the slightly more sophisticated polarizedsetting of amortized equivalences, EQ(AR), in which the objects consist of R-amortized sets together with an equivalencerelation on the elements, (A,≡A). We may think of these objects as a “syntactic” set A which has an associated con-crete amortized size but which, in fact, represents a semantic notion, for which certain of the syntactic elements must beconsidered equivalent.

An R-amortized equivalence A= (A,≡A) consists of an amortized set A with an equivalence relation on the underlyingset, ≡A⊆ U (A) × U (A). The equivalence relation ≡A induces a quotient map hA : U (A) −→ U (A)/ ≡A which shall be used inthe description of the morphisms.

We define EQ(AR)o (and EQ(AR)p respectively) by:

Objects: X = (X,≡X ) where X ∈ AR and ≡X⊆ U (X) × U (X) is an equivalence relation.Maps: f :X −→ Y is a set map f : U (X) −→ U (Y )/ ≡Y such that:

(a) There is a f ′ : X −→ Y in (AR)o (respectively (AR)p), called a tracing of f , such that U ( f ′)hY = f :

U (X)

fhX

U ( f ′)U (Y )

hY

U (X)/ ≡X f /≡ U (Y )/ ≡Y

Page 23: Safe recursion revisited I: Categorical semantics for lower complexity

M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45 41

(b) Whenever, x ≡X x′ then f (x) = f (x′) (so the lower dotted map f / ≡ exists and makes the diagram commute).Identity: hX :X −→X where

U (X)

hXhX

U (1X )U (X)

hX

U (X)/ ≡X 1≡XU (X)/ ≡X

Composition: f g := U ( f ′)g where f ′ is a tracing of f . It is straightforward to show this is well-defined.

Cross maps are defined by (X ,Y)g−→Z similarly to the maps above as set maps U (X) × U (Y ) −→ U (Z)/ ≡Z such that:

(a) There is an g′ : (X, Y ) −→ Z in A which is a tracing of g in the sense that U (g′)hY = g:

U (X) × U (Y )

ghX ×hY

U (g′)U (Z)

hZ

U (X)/ ≡X ×U (Y )/ ≡Y U (Z)/ ≡Z

(b) Whenever, x ≡X x′ and y ≡Y y′ then g(x, y) = g(x′, y′).

The structure of products, coproducts, and tensor then follow the expected pattern:

A+ B = (A + B,≡A � ≡B)

A× B = (A × B,≡A × ≡B)

A⊗ B = (A ⊗ B,≡A × ≡B)

where (x, y) ≡A × ≡B (x′, y′) ⇔ x ≡A x′&y ≡B y′ . We then have:

Proposition 4.17. EQ(AR) is a Pola category.

Proof. It remains to check that this setting is affine closed. We shall do the proof without a context: the proof with acontext is similar.

A⊗ B

1⊗cur(g)

g C

A⊗ (A� B)

ev

First define:

A � B = {( f , P ) ∈ A �w B

∣∣ a ≡A a′ ⇒ hB(

f (a)) = hB

(f(a′))}

and say ( f , P ) ≡A�B (g, Q ) ⇔ U ( f )hB = U (g)hB . We then define the evaluation to be:

A⊗ (A� B)ev−−→ B = A ⊗ (A � B) ⊆ A ⊗ (A �w B)

U (evw )−−−−−→ BhB−−→ B/ ≡B

Note that this map is defined using a tracing so automatically satisfies the first condition. It remains to show that if a ≡A a′and f ≡A�B f ′ then ev(a, f ) = ev(a′, f ′):

ev(a, f ) = hB(U (evw)(a, f )

) = hB(

f (a)) = hB

(f (a′)

) = hB(

f ′(a′)) = ev(a′, f ′).

Now define cur(g) := U (curw(g′))hA�B where g′ is a trace of g: this is clearly traced and will make the couniversal diagramabove commute if it is well-defined.

To check this we suppose that g′ and g′′ are both tracings of g then

hA�B(U

(curw

(g′))(c)

) = hA�B(λa.g′(a, c)

)= λa.hB

(g′(a, c)

) = λa.hB(

g′′(a, c))

= hA�B(λa.g′′(a, c)

) = hA�B(U

(curw

(g′′))(c)

) �Now we turn to the issue of whether EQ(AR) has data:

Page 24: Safe recursion revisited I: Categorical semantics for lower complexity

42 M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45

Proposition 4.18. When AR has inductive data then EQ(AR) also has inductive data.

We shall use the following somewhat technical lemma:

Lemma 4.19. In any category with an (E,M)-factorization system, in which the E-maps are epic, any natural transformation, α : G−→ G ′ whose components αX : G(X) −→ G ′(X) lie in E and which is between endofunctors which preserve E-maps, induces an E-mapα : μx.G(x) −→ μx.G ′(x) (whenever these inductive types exist).

Proof. We define α by:

G(μx.G(x))

G(α)

cons μx.G(x)

α

G(μx.G ′(x)) α G ′(μx.G ′(x))cons′ μx.G ′(x)

If α is not epic there are distinct f , g : μx.G ′(X) −→ Z such that α f = αg . Now if α is factorized as μx.G(x)e−→ V

m−−→μx.G ′(x) then we have:

G(μx.G(x))

G(e)

cons μx.G(x)

eG(V )

α

G ′(V )

G ′(m)

c V

m

G ′(μx.G ′(x))cons′ μx.G ′(x)

where the cross-map is an isomorphism and turns V in to a G ′ fixed point. Furthermore, c : G ′(V ) −→ V is an initialG ′-algebra as any G ′-algebra g : G ′(A) −→ A induces a G-algebra αA g : G(A) −→ A and whence a unique comparison fromμx.G(x) which map factors through V as:

G(μx.G(x))

G(α)

G(e)α

cons μx.G(x)

e

αG ′(V )

G ′(m)

c V

m

G(μx.G ′(x))

G(fold)

α G ′(μx.G ′(x))

G(fold)

cons′ μx.G ′(x)

fold

G(A) α G ′(A) g A

This gives a comparison map from the fixed point on V which is unique as e is epic, so m is an isomorphism making α anE-map. �

Proof. (of 4.18) A polarized operator G on EQ(AR) is a composite of sum, product, tensor, and constant functors. Thiswe may view as a “pure” functor in sums, products, and tensors which has certain arguments filled in with constants:

Page 25: Safe recursion revisited I: Categorical semantics for lower complexity

M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45 43

G(X ) := G(A1, . . . ,Am,X ). Because these functors on the underlying sets (with tensor translated as product) preserveregular epics, this gives an epic natural transformation hG given by the composite

U (G(X)) = U (G(A1, . . . , An, X)) = G ′(U (A1), . . . , U (An), U (X))

G ′(hA1,...,hAn ,1)

G ′(U (X)) = G ′(U (A1)/ ≡, . . . , U (An)/ ≡, U (X))

G ′(hX )

G ′(U (X)/ ≡)

where G ′(V ) := G ′(U (A1)/ ≡, . . . , U (An)/ ≡, V ). Thus G(X ) is determined by equivalence on G ′(U (A1), . . . , U (An), U (X)).An algebra g : G(C) −→ C will then be given by a traced map:

U (G(A1, .., An, C))

hG g

U (g′)U (C)

hC

G ′(U (A1)/ ≡, .., U (An)/ ≡, U (C)/ ≡) U (C)/ ≡

For the tracing map g′ we certainly have a unique map fold(g′) : μx.G(A1, .., An, x) −→ C in amortized sets. By Lemma 4.19there is then an epic map

h : μx.G ′(U (A1), . . . , U (An), x) −→ μx.G ′(U (A1)/ ≡, . . . , U (An)/ ≡, x

)

whose equivalence relation must be forced on the fixed point μx.G ′(A1, . . . , An, x). This map followed by the ev-ident fold map in sets gives a unique amortized equivalence map establishing the required universal property for(μx.G(A1, .., An, x),≡h). �

For coinductive data we have:

Proposition 4.20. When AR has coinductive data then EQ(AR) has coinductive data.

Proof. Every polarized functor G ′ = G/ ≡G on EQ(AR) is, as before, traced from a polarized functor G on AR with, inSet, an epic transformation hG : G(A) −→ G ′ = G/ ≡G (A). The coinductive data generated by νx.G/ ≡G (x) is then νx.G(x)factored by the equivalence relation generated by the induced map νx.G(x) −→ νx.G ′(x) generated from the diagram

νx.G(x)

h

dest G(νx.G(x))

hG

G ′(νx.G(x))

G ′(h)

Vd

m

G ′(V )

G ′(m)

νx.G ′(x) ′ G ′(νx.G ′(x))

dest
Page 26: Safe recursion revisited I: Categorical semantics for lower complexity

44 M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45

which makes V into a fixed point. Any G/ ≡G -coalgebra in EQ(AR) must be traced from G so is also a G-coalgebra butthis immediately gives the required lifting.

A

hA

g′G(A)

hG

G ′(A)

G ′(hA)

A/ ≡ A G ′(A/ ≡)A

νx.G(x)

h

dest G(νx.G(x))

G(h)hG

V

m

d G ′(V )

G ′(m)

νx.G ′(x) dest G ′(νx.G ′(x))

Note in this proof V is not required to be a greatest fixed point: it suffices to demonstrate that the map from theG ′/ ≡-coalgebra factorizes through V . �

This establishes:

Theorem 4.21. EQ(AR) is a Pola setting.

As the maps in this Pola setting are by construction PSIZE bounded and the initial Pola setting has a unique (to equiva-lence) interpretation in this setting we have:

Theorem 4.22. The initial Pola setting is sound for PSIZE computations.

5. Conclusions

The main contribution of this paper is to provide categorical models – amortized sets and equivalences – of lowercomplexity settings which support both inductive and coinductive data. These provides PSIZE bounds for the initial Polasetting with inductive and coinductive data and is, thus, the first step in establishing the more exacting PSPACE bounds(and PTIME bounds) which, together with the expression of other complexity classes, is the subject of future work.

This paper makes no attempt to explore detailed complexity issues surrounding the coinductive data. However, it shouldbe noted that the power of the coinductive data types we have introduced is highly constrained as they live in the playerworld which is essentially constant time. This constant time performance, however, may be a feature which is of practicalinterest in real-time applications.

There are now many implicit systems for PTIME and PSPACE: in this work we have developed a categorical frameworkwhich seems not only to capture the essence of many previous systems but also carries its own notion of provable equality.Furthermore, a prototype implementation of a programming language [4], called Pola, based on these ideas has been devel-oped. In order to implement such a programming language it was necessary to develop a type system for Pola categorieswhich supported type inference. The presence of affine types and products in the player world led us to deploy a polymor-phic bunched type system, in which a “circular” recursion scheme [20,21], was used to write programs employing Pola data.This appears to be a very natural and expressive type theory for this family of implicit complexity settings.

Acknowledgements

The authors would like to acknowledge the hard work both of the editors and the referees whose suggestions andcomments where invaluable in bringing this paper to its current form.

References

[1] J.-M. Andreoli, Focussing and proof construction, Ann. Pure Appl. Log. 107 (1) (2001) 131–163.

Page 27: Safe recursion revisited I: Categorical semantics for lower complexity

M. Burrell et al. / Theoretical Computer Science 515 (2014) 19–45 45

[2] S. Bellantoni, S. Cook, A new recursion-theoretic characterization of the polytime functions, Comput. Complex. 2 (1992) 97–110.[3] A. Brunel, K. Terui, Church ⇒ Scott = Ptime: an application of resource sensitive realizability, in: Dice 2010, in: Electron. Proc. Theor. Comput. Sci.,

vol. 23, 2010, pp. 31–46.[4] M.J. Burrell, Pola project page, http://projects.wizardlike.ca/projects/pola.[5] J.R.B. Cockett, B. Redmond, A categorical setting for lower complexity, Electron. Notes Theor. Comput. Sci. 265 (2010) 277–300.[6] J.R.B. Cockett, R.A.G. Seely, Polarized category theory, modules and game semantics, Theory Appl. Categ. 18 (2007) 4–101.[7] J.R.B. Cockett, D. Spencer, Strong categorical datatypes I, in: International Meeting on Category Theory 1991, in: Canad. Math. Soc. Conf. Proc., vol. 13,

AMS, 1992, pp. 141–169.[8] J.-Y. Girard, A new constructive logic: classical logic, Math. Struct. Comput. Sci. 1 (3) (1991) 255–296.[9] M. Hamano, P. Scott, A categorical semantics for Polarized MALL, Ann. Pure Appl. Log. 145 (2007) 276–313.

[10] C. Hermida, B. Jacobs, Structural induction and coinduction in a fibrational setting, Inf. Comput. 145 (2) (1998) 107–152.[11] M. Hofmann, Type systems for polynomial-time computation, Habilitation thesis, University of Darmstadt, 1999.[12] M. Hofmann, Linear types and non-size-increasing polynomial time computation, Inf. Comput. 183 (1) (2003) 57–85.[13] A. Kock, Strong Functors and Monoidal Monads, Arch. Math. 23 (1972) 113–120.[14] U. Dal Lago, S. Martini, M. Zorzi, General ramified recurrence is sound for polynomial time, in: Dice 2010, in: Electron. Proc. Theor. Comput. Sci.,

vol. 23, 2010, pp. 47–62.[15] O. Laurent, Étude de la polarisation en logique, Université Aix-Marseille II, Thèse de Doctorat, 2002.[16] O. Laurent, Polarized games, Ann. Pure Appl. Log. 130 (1–3) (2004) 79–123.[17] D. Leivant, Stratified functional programs and computational complexity, in: Proc. 20th IEEE Symp. on Principles of Programming Languages, 1993,

pp. 325–333.[18] D. Leivant, J.-Y. Marion, Ramified recurrence and computational complexity II: Substitution and poly-space, in: Proc. CSL’94, in: Lect. Notes Comput.

Sci., vol. 933, Springer, 1994, pp. 486–500.[19] R. Paré, L. Román, Monoidal categories with natural numbers object, Stud. Log. 48 (3) (1989) 361–376.[20] L. Santocanale, A calculus of circular proofs and its categorical semantics, in: Proc. FOSSACS 2002, in: Lect. Notes Comput. Sci., vol. 2303, Springer,

2002, pp. 257–371.[21] V. Vene, Categorical programming with inductive and coinductive types, PhD thesis, University of Tartu, 2000.[22] R.J. Wood, Indicial methods for relative categories, PhD thesis, Dalhousie University, 1976.