Electronic Notes in Theoretical Computer Science 69 (2003)

URL: http://www.elsevier.nl/locate/entcs/volume69.html 24 pages

Adjunction Models For Call-By-Push-Value

With Stacks

Paul Blain Levy

PPS, Université Denis Diderot, Case 7014, 2 Place Jussieu 75251 PARIS Cedex 05, France Paul.Levy@pps.jussieu.fr

Abstract

Call-by-push-value (CBPV) is a new paradigm, which has been claimed to provide the semantic primitives from which call-by-value and call-by-name are built. We present its operational semantics in the form of a Felleisen-Friedman style CK-machine, and see how this machine suggests a new term judgement of stacks. When augmented with this judgement, CBPV has an elegant categorical semantics based on adjunctions.

We describe this categorical semantics incrementally. First, we introduce locally indexed categories and the opGrothendieck construction, and use these to give the basic structure for interpreting the 3 judgements: values, stacks and computations. Then we look at the universal property required to interpret each type constructor. We define a model to be a strong adjunction with countable coproducts, countable products and exponentials.

We justify this definition in two ways. First, we see that it has a wide range of instances: we give examples for divergence, storage, erratic choice, continuations etc., in each case decomposing Moggi's strong monad into a strong adjunction.

For the second justification, we start by giving equational laws for CBPV+stacks. This requires some additional pattern-matching constructs, but they do not affect the set of computations. We then show that the categories of theories and of models are equivalent.

1 Introduction

1.1 Background

Moggi [17] introduced the use of a strong monad T on a cartesian category C to model call-by-value languages. As noted by [5] and others, this structure can also be used to interpret call-by-name, where a type denotes a T-algebra.

Based on these ideas, the call-by-push-value (CBPV) paradigm was introduced, subsuming call-by-value and call-by-name. Two key type constructors

©2003 Published by Elsevier Science B. V.

in CBPV are U and F, and their composite UF corresponds to Moggi's type constructor T. This immediately prompts the question: surely CBPV decomposes Moggi's monad into an adjunction? Now, this is certainly the case for all of the concrete models studied. For example the storage model decomposes Moggi's S ^ (S x —) monad into S ^ — and S x —, whilst the continuations model decomposes Moggi's (--> R) ^ R monad into--> R and--> R.

However, the syntax of CBPV does not confirm this analysis. CBPV has two judgements, values and computations, and the former give us a value category C, but the other category required for an adjunction is absent. Thus, we are left with a "not-quite-adjunction" which can be formulated in several ways, none of them elegant [15].

But fortunately, recent work on CK-machine semantics (a form of operational semantics [4]) for CBPV has brought to light a new judgement: that of stacks. (Independently, a stack judgement with some similar rules was introduced in [3], in the setting of call-by-name and call-by-value with control effects.) The categorical semantics of CBPV+stacks is precisely the elegant adjunction structure noticed in each of the concrete models. The purpose of this paper is to present this adjunction semantics.

1.2 Adjunctions: A Discussion

Let C and D be categories; we will underline objects of D. It is well known that the notion of adjunction from C to D has numerous equivalent definitions. One of these requires functors U and F and an isomorphism

C(X, UY) = V(FX, Y) natural in X and Y (1)

Alternatively F can be specified on objects only and naturality in X removed. (This is equivalent to the first definition by the parametrized representability theorem.) But can we give a definition where both U and F are specified on objects only? Here is one way.

Let us say, in an adjunction, that an oblique rnorphisrn from X to Y is a C-morphism from X to UY_ or a 2?-morphism from FX to Y_; it hardly matters which, since they correspond via 1. Clearly an oblique morphism can be composed with a C-morphism on the left, or with a D-morphism on the right; it straddles the two categories, so to speak. Let us write 0(X,Y) for the oblique morphisms from X to Y_. Now an adjunction from C to V can be specified by a functor O : C°P xD ^ Set and isomorphisms

C(X, UY) = 0(X,Y) natural in X (2)

0(X, Y) ^ V{FX, Y) natural in Y (3)

Again the equivalence to the earlier definition follows from parametrized rep-resentability. To see the benefit of this definition, fix a set R and consider the adjunction

Set(X, Y ^ R) = Setop (X ^ R, Y) (4)

We can decompose this isomorphism quite naturally by setting O(X, Y) to be

Set(X x Y,R).

This is essentially what is happening in CBPV+stacks: we have 3 judgements, denoting C-morphisms (values), oblique morphisms (computations) and V-morphisms (stacks). But the above account is overly simplistic, for, as we shall see, we want V to be locally indexed by C.

1.3 Element Style vs. Naturality Style

Universal properties in category theory can usually be defined either in terms of elements or in terms of naturality of isomorphisms. Here is a well-known example. A product for a family of objects {Ai]i^I in a category C consists of an object V—the vertex—together with either of the following:

• for each i E I, a morphism V n> Ai, such that the function

C(X, V) n^i C(x, Ai) for all X (5)

f i—► Xi-(f; is an isomorphism

• an isomorphism

C(X,V) = nieI C(X, Ai) natural in X (6)

The equivalence of these two definitions follows from the Yoneda Lemma.

For the universal properties we will treat in this paper, we will give definitions in element-style only, and leave the naturality-style formulation to future work.

2 Review of Call-By-Push-Value

There are two variants of CBPV: finitary and infinitely wide. In this paper we treat infinitely wide CBPV; the finitary case is treated by substituting "finite" for "countable" throughout. (The reverse substitution would not work, because for both variants contexts are finite, and hence the value category requires only finite products.)

CBPV has two disjoint classes of terms: values and computations. It likewise has two disjoint classes of types: a value has a value type, while a computation has a computation type. For clarity, we underline computation types. The types are given by

value types A ::= UB\ Y,ieiAi I 1 I A x A

computation types B_v.= FA | B_i | A ^ B_

where I can be any countable set (finite, in finitary CBPV). The meaning of F and U is as follows. A computation of type FA produces a value of type A. A value of type UB_ is a thunk of a computation of type B_, i.e. the computation is frozen into a value so that it can be passed around. When later required, it can be forced i.e. executed.

Unlike in call-by-value, a function in CBPV is a computation, and hence a function type is a computation type. We will discuss this further in Sect. 3.

Like in call-by-value, an identifier in CBPV can be bound only to a value, so it must have value type. We accordingly define a context r to be a sequence

x0 : ■■ ■ xn-1 : An-1

of identifiers with associated value types. We often omit the identifiers and write just A0,■■■, An-1. We write r hv V : A to mean that V is a value of type A, and we write r bc M : B_ to mean that M is a computation of type

The terms of CBPV are given in Fig. 1; the symbol i represents any particular element of I. We explain some of the less familiar constructs. M to x, N is the sequenced computation that first executes M, and when this produces a value x proceeds to execute N. This was written in Moggi's syntax using let, but we reserve let for ordinary binding. The keyword pm stands for "pattern-match" , and the symbol ' represents application in reverse order. Because we think of ieI as the type of functions taking each i E I to a computation of type we have made its syntax similar to that of —

Following Lawvere [12], we say that a context morphism q from r = A0,■■■, Am-1 to A = Bo^^, Bn-1 is a sequence of values V0,---, Vn-1 where r hv Vi : Bi. As usual, such a morphism induces (by induction [6]) a substitution function q* from values A hv V : C to values r hv V : C and from computations A bc M : B_ to T bc M : B_. We define identity and composite context morphisms in the usual way.

3 The CK-Machine and the Stack Judgement

There are several ways of presenting operational semantics for computations of CBPV. In [14] it is presented in big-step form, but here we use a CK-machine, in the style of [4]. We also generalize from closed computations to computations on a fixed context r. The machine is presented in Fig. 2, with types written explicitly. (We will explain below why we write for certain transitions, the so-called "reversible" ones.) A configuration of the machine, with types, has the form

r I M B K c {7)

where r bc M : B_ is the computation we are currently evaluating and K is the stack (perhaps better known as an evaluation context, another concept that appeared in [4]). Notice that T and C remain fixed throughout execution.

To begin with, we place the computation we wish to evaluate alongside the empty stack, and we apply transitions until we reach a terminal configuration. When the inside has the form M to x^ N, we must first evaluate M, during which time we have no need of N. So we move the context [•] to x^ N onto

Tbv V : A rx : Abc M : B

r, x : A, r' bv x :A r bc let V be x. M : B

r bv V : A r bc M : FA r, x : A bc N: B

r bc produce V : FA T bc M to x. N : B

T bc M : B T bv V : UB

T bv thunk M : [JB T bc force V : B

r bv V : Ai Mi ■R

r bv (i, V) : E^Ai r bv V : A r bv V' : A'

r bv (V, V') : Ax A' ••• h hc Mj : B^j ■■■teI

' A{----i.M;-----) : \\: ,11:.

r, x : A bc M : B

r hc pm 7 as {...,(i,x).Mi}...} :B

ThvV:AxA' r,x : A,y : A' bc M : B r bc pm V as (x,y).M : B

r hc M : n^

r bc ilM : B.

r hv F : A r hc M : A ^ B

r bc Xx.M : A -»■ B

r bc V 'M : B

Fig. 1. Terms of Call-By-Push-Value

the stack, and proceed to evaluate M. Once we have evaluated M to the form produce V, we remove that context from the stack and proceed to evaluate N[V/x]. Similarly, to evaluate V'M we leave the operand V on the stack while we evaluate M.

Classifying a function as a computation is a novelty of CBPV and, at first sight, seems counterintuitive. But the CK-machine provides an explanation. For it treats Ax as an instruction "pop x", and it treats V' as an instruction "push V". Thus a computation of type A B_ pops a value of type A and proceeds as a computation of type B. Similarly, a computation of type Yliei—i pops a tag i E I and proceeds as a computation of type

In order to characterize the well-typed configurations, we require a judgement for stacks, of the form r|5 bk K : C_. Thus a configuration (7) will be well-typed precisely when T hc M : B_ and T\B_hk K : C_.

By inspecting Fig. 2, we can see that the typing rules for this judgment should be as given in Fig. 3. This ensures that the well-typed configurations are precisely those obtainable from an initial configuration.

There are some evident operations on stacks.

(i) Given a context-morphism q from T to A and a stack A\B_ bk K : C we can substitute q in L to give a stack bk q*K : C.

Initial configuration for evaluation of T bc M : C_

Transitions

let V be x. M

M [V/x]

M to x. N

produce V

N [V/x]

force thunk M

pm (V, V') as (x, y).M M [V/x, V '/y]

îlM M

V lM M

M [V/x]

FA [•] to x. N :: K

FA [•] to x. N :: K

pm (Î,V) as {...,(t,x).Mt,...} B M-t[V/x] B

Bx n xjBt

n %eiR% Mt

K î :: K

î :: K

V :: K

V :: K

ç ç ç ç

Fig. 2. CK-Machine For CBPV, With Types

r,x:AhcM:B Y\BV~WK:C T\C hk nil : C T\FA hk [•] to x. M :: /\ : (_

r|5j bk K : C T \~v V : A T\BhkK:C

Fin K-.C T\A B_\~k V :: K : C

Fig. 3. Typing rules for stacks

(ii) Given a computation r bc M : B_ and a stack hk K : C, we can dismantle K onto M by forming the configuration M, K and applying the "reversible" transitions (designated as in Fig. 2) in reverse—this is clearly deterministic—until we reach M', nil, whereupon we cannot continue. We write M • K for M' and we have r bc M • K : C_. Dismantling can be defined by induction on K, as shown in Fig. 7. (If we represent a stack K as an evaluation context £'[•], then M • K is just E[M ].)

(iii) Given two stacks r|i? bk K : C and r|(7 l~k L : D, we can concatenate K and L to give r|i? bk K-W-L : D_, defined by induction on K in Fig. 7. (If we represent K and L as evaluation contexts E[•] and E'[•] respectively, then K-+L is represented as E'[E[-]].)

Lemma 3.1 Substitution, dismantling and concatenation satisfy the following properties.

nil-+K = K p*nil = nil

K-+nil = K p*(K+L) = (p*K); (p*L)

(K; L); L' = K; (L; L') p*(M • K) = (p*M) • (p*K)

M • nil = M id*P = P

M • (K+K') = (M • K) • K' p*q*P = (p; q)*P

We use the term hypercongruence for a congruence on terms-in-context that is closed under substitution, dismantling and concatenation.

4 Basic Structure

4.1 Interpreting Values

Like Moggi, we interpret values in a cartesian category C, called the "value category". As usual, the products in C are used for interpreting both x and context extension (comma). Although there is no value in the language x : A x A' bv V : A, there will be after we extend the syntax in Sect. 6.

We will occasionally write composition in C (in diagrammatic order) as f *g rather than as f; g. This has the effect of shortening certain definitions, e.g. Def. 5.1(coproducts).

4.2 Interpreting Stacks

If values are interpreted in the cartesian category C, stacks will be interpreted in a locally C-indexed category D. This can be defined in various ways:

• as a strict C-indexed category (i.e. functor C°P —► Cat) in which all the fibres DX have the same set of objects ob D and all the reindexing functors Df are identity-on-objects

• as a [C°P, Set]-enriched category

• using the following concrete description. Definition 4.1 A locally C-indexed category consists of

• a set ob D, whose elements we call D-objects and we underline (except if ob D = ob C)

• for each object X e ob C and each pair of objects Y_, Z_ e ob V, a small set

DX(Y_,Z), an element of which vje call a D-morphism and write Y X> Z

• for each object X e ob C and each object Y_ e ob V, an identity rnorphisrn Y Y

• for each morphism Y f> Z and each morphism Z—W, a composite

morphism Y W

for each D-morphism Y f> Z and each C-morphism X'—^ X , a rein-dexed D-morphism Y kXf> Z

such that

id; f = f k*id = id idf = f f; id = f k*(f; g) = (kf); (k*g) (l; k)f = l*(kf)

(f; g);h = f; (g;h)

It is easy to see that this is natural for interpreting stacks: a computation type will denote an object of V and a stack r|i? bk K : C will denote a V-morphism over [r] from [5] to [C]. Then identity morphisms in T> interpret nil, composition interprets concatenation of stacks, and reindexing interprets substitution.

Before proceeding further, we develop some theory of locally indexed categories. Firstly, it is clear that they form a 2-category, and we have operations —°P and x. The most important example of a locally C-indexed category is called self C, and given by

ob self C = ob C self Ca(B,C) = C(A x B,C)

with the evident composition and reindexing. This is used in the following result, which is mentioned in [17].

Proposition 4.2 A strong monad on C corresponds to a monad on self C.

As with ordinary categories, we wish to speak of the "homset functor" associated with D, and we do this as follows.

Definition 4.3 (i) Let D be a locally C-indexed category. We write opGroth D for the ordinary category where

• an object is a pair xY where X e ob C and Y e ob V;

• a rnorphisrn from xY to x'Z in opGroth V consists of a pair kh where X'—^X in C and Y~^Z in V

• the identity on rX is given by ¡¿id;

• the composite of rX—^pY—^vZ is l;k((l*f);g)■

(ii) For a locally D-indexed category, we write Hom^, or D for short, for the functor

opGroth(D°P x D)-^Set

x (Y,Z)'-- Dx (Y, Z)

k (f, h) i-- Xg.(f; (k*g); h)

4.3 Interpreting Computations

If we are interpreting values in a cartesian category C, and stacks in a locally C-indexed category D, then we will interpret computations in a functor O : opGroth D —► Set. This can be described in concrete terms.

Proposition 4.4 A functor O : opGroth D —► Set is given by

• for each X e ob C and Y e ob V, a small set OxY, an element of which we call an O-morphism over X to Y and write > aX > Y

• for each X'—^X and > X > Y a reindexed O-morphism >-~xah^Y

• for each > ax > Y and Y X> — a composite O-morphism > 9Xh> Yl satisfying identity, associativity and reindexing laws:

g;id = g id*g = g k*(g;h) = (k*g);(k*h)

g;(h; h') = (g; h); h' (k; l)*g = k*(l*g) where g is an oblique morphism.

Our intention is that a computation r bc M : B_ will denote an (9-morphism over [r] to {BJ and that q*M will denote [g]*[M] while the dismantling M»K will denote [M]; {Kj.

4.4 Examples

We summarize the above discussion as follows.

Definition 4.5 A CBPV pre-structure consists of

• a cartesian category C

• a locally C-indexed category D

• a functor O : opGroth D —► Set.

Here are some examples of CBPV pre-structures.

trivial Given a cartesian category C, set D to be self C and set OXY to be C (X,Y).

Scott Let C be Cpo, let a D-object be a pointed cpo and a D-morphism over X from Y_ to Z_ be a right-strict continuous function from X x Y to Z, and let OxY_ be continuous functions from X to Y_.

monad Given a strong monad T on a cartesian category C, we obtain a pre-

structure (C, CT, OT), where a CT-object is a T-algebra, a CT-morphism over

X from (Y, 9) to (Z, 0) is a C-morphism X x Y->- Z satisfying

X x TY ^Qt (X x YTZ

X x Y-j-^ Z

and let OX (Y, 9) be C(X,Y).

storage Given a set S, let C be Set, let D be self Set and let OXY be Set(S x X,Y).

erratic choice Let C be Set, let D be Rel i.e. an object is a set and a morphism over X from Y to Z is a relation from X x Y to Z, and let an O-morphism over X to Y be a relation from X to Y.

continuations Given a set R, let C be Set, let D be (self Set)op and let OXY be Set(X x Y, R).

5 CBPV Adjunction Models

5.1 Universal Properties

So far, the only type constructors we can interpret are 1 and x. The rest are interpreted using the following universal properties. In the terminology of Sect. 1.3 they are defined in element style.

Definition 5.1 In a CBPV pre-structure (C, D, O),

UB a right adjunctive for a V-object B_ is a C-object V and an O-morphism > f°rce> B, such that the functions

C(X,V)—>OxB for all X (8)

f ——> f *force

are isomorphisms

FA a left adjunctive for a C-object A is a V-object V and an O-morphism produce v , such that the functions

Vx(V,Y)-hi-

(nX,AProduce); (n*X)Ah)

for all X,Y

are isomorphisms

Ai a coproduct for a family {Ai}ieI of C-objects is a C-object V and, for each i E I, a C-morphism Ai > V , such that the functions

C (X x V,Y) — OxxvY- -

VxMY,Z)~

UieiC(X x Ai,Y) rlieiOxxAX n ^VxHAtGLtZ) \i.((X x \m)*f)

for all X,Y (10)

for all X,Y_ (11)

forallX,Y,Z (12)

are isomorphisms

YiieiB_i a product for a family {Bijiei of V-objects is a V-object V_ and, for

each i E I, a D-morphism V—Bi

OxV^Y[ieIOxBl VxiY^^li^VxiY.Bd h ——> Ai.(h; ()*ni)

such that the functions

for all X for all X,Y

are isomorphisms

an exponential from a C-object A to a V-object B is a V-object V and a D-morphism V eJ> B , such that the functions

OxV^OxxaB for all X (15)

Vx(Y,V)^Vxxa(Y,B) for all X,Y_ (16)

h —^ (nX,Ah); (nX,Aev) are isomorphisms.

Definition 5.2 A strong adjunction is a CBPV pre-structure (C, D, O) with all right adjunctives and left adjunctives—we say that it goes from C to D.

As a variation of the characterization of adjunctions in Sect. 1.2, we have

Proposition 5.3 [15] A strong adjunction from C to D is equivalent to an adjunction from self C to D.

Thus a strong adjunction from C gives rise to a monad on self C i.e. a strong monad on C; hence the word "strong".

Definition 5.4 A CBPV adjunction model is a strong adjunction (C, D, O) with all countable coproducts, countable products and exponentials. We write U,F,^ etc. for the operations on objects.

We write the universal elements as foreetc. and the inverses to the various functions required to be isomorphisms as q11 — etc. To reduce clutter,

we frequently omit the objects and just write force, q^ etc.

It is obvious how to define the interpretation of CBPV in such a structure. We mention that part of Def. 5.4 is redundant, notably the requirement for (11),(13),(15) to be isomorphisms, but the requirement for (12) to be an isomorphism does not appear to be redundant. Warning The definition of CBPV adjunction model in [15] mistakenly omitted this requirement.

Proposition 5.5 Every CBPV adjunction model (C, D, O,...) is isomorphic to one in which the thunk/force isomorphism (8) is the identity.

Proof. Leave the value category and stack category unchanged, and set O' to be the functor taking XY_ to C(X, UY_).

Despite this result, there are many semantics, such as storage, continuation and game models, which it is more computationally natural to present in a form where (8) is not the identity.

5.2 Examples

We first require the following, adapted from [1,2].

Definition 5.6 A distributive coproduct for a family of objects {Ai]i^I in

a cartesian category C is an object V and a C-morphism Ai > V for each i E I, such that the functions

C(X x V, Y) ^ nieIC(X x Ai,Y) for all X, Y (17)

f Xi.((X x ini); f)

are isomorphisms. (More abstractly, it is a coproduct in self C.) A cartesian category with all countable distributive coproducts is called a countably distributive category.

Any distributive coproduct is a coproduct in C (and conversely if C is a ccc).

We now look again at the examples of CBPV pre-structures described in Sect. 4.4 to see when they have all the required universal elements.

trivial The pre-structure given by a cartesian category C is a CBPV model iff C is countably bicartesian closed, i.e. a ccc with all countable coproducts and products. Such models are called trivial and they interpret CBPV with no computational effects at all. Indeed, "CBPV adjunction model" can be seen as a generalization of "countably bicartesian closed category" to accommodate computational effects.

Scott This is clearly a CBPV adjunction model.

monad The pre-structure given by a strong monad T on a cartesian category C is a CBPV model iff

• C is countably distributive

• C has a product for every countable family of T-algebra carriers

• C has an exponential from every object to every T-algebra carrier.

Notice that these conditions imply that C has all Kleisli exponentials and all countable products of Kleisli exponentials.

storage This pre-structure is clearly a CBPV adjunction model, see below. More generally, given a CBPV model (C, V, O,...) and a C-object S, we obtain another CBPV model by setting C' to be C, setting V to be V, and setting 0'XY_ to be OsxxY-

erratic choice This pre-structure is clearly a CBPV adjunction model, see below.

continuations This pre-structure is clearly a CBPV adjunction model, see below. More generally, given a CBPV model (C, V, O,...) and a V-object R, we obtain another CBPV model by setting С to be С, setting V to be (self C)op, and setting Q'XY to be 0XxYR-

The connectives in these examples are given in the following table:

model Siei 1 X и F EU/ -

Scott Siei 1 X — —±. EU/ -

storage Siei 1 X S ^ — SX Пде! -

storage, general Siei 1 X U(S - ) F(S x —) Пде! -

erratic choice Siei 1 X P — ieI X

continuations Siei 1 X — - R — - R Siei X

continuations, general Siei 1 X U (-- >R) и (-- R) Siei X

Notice the we recover Moggi's strong monad in each case.

6 The Equational Theory

6.1 Complex Values and Stacks

Since we want the term model of CBPV+stacks to be an adjunction model, we require additional syntax—otherwise we cannot even form a projection x : A x A' hv V : A to make a cartesian category of values. The rules for the formation of "complex" values and stacks using binding and pattern-matching are given in Fig. 4. We shall see in Sect. 6.2 that these new constructs can always be eliminated from a computation. For example, produce (pm V as (x,y).W) can be simplified into pm V as (x, y).produce W.

We explain the where construct as follows. In any stack K to B_ there is a unique occurrence of nil, and we can "bind" it to a stack L from B_, giving the concatenated stack K-W-L. A stack V from A B_ is typically of the form V :: L, where V is a value of type A and L is a stack from so it can be pattern-matched as x : nil in K.

We mention that omitting the let construct and the K where nil is L construct would not affect the theory/model equivalence of Sect. 7.2; they are only a convenience. The pattern-match constructs, on the other hand, are essential.

Complex Values

r hv V : A r, x : A hv W : B T bv let V be x. W : B

rhvy:E^» ••• T^-.A^Wr.B ...i€I

rbv pm V as {...,(«, x).Wi,...} : B r hv V : A x A' r, x : A, y : A' hv W : B r hv pm V as {x,y).W : B

Complex Stacks

ThvV:A T,x.A\BhkK : C T\B hk let V be x. K : C

rhvF: J2teIAt • • • r,x : Aj\B hk Kt : C ■ ■ ■ i€l T\B hk pm V as {. .., (i, x).Kh ...} : C

YV~V V ■. Ax A1 r,x: A,y : A'\Bhk K : C T\B hk pm V as (x,y).K : C

Y\CY-kK-.B Y\BV-kL-.D r|C[ bk K where nil is L : D

••• r|£7 hk Kj : B_j •••ief r|n i€lBjhkL:D

T\C hk {... , Ki where i :: nil,...} is L : D

r,x: A\CV-kK-.B T\A B_\~k L : D r|C bk K where x :: nil is L : D

Fig. 4. Complex Values and Stacks 6.2 Properties of Equational Theory

The equational theory is the least congruence on terms-in-context containing the laws in Fig. 5. To reduce clutter, we omit the assumptions necessary to make each equation well-typed, and we employ the bound/unbound convention: when, in an equation—such as the ^-law M = Ax.(x 'M)—the term

r bc M : B_ occurs both in the scope of an x-binder and not in the scope of an x-binder, we assume x E r. We do not write the weakening explicitly.

Lemma 6.1 Provable equality is a hypercongruence.

Proof. Straightforward induction for each of substitution, dismantling and concatenation. □

Proposition 6.2 For any computation T \-c M : B_, we can obtain a computation T bc M' : B_ that does not use the complex value constructs of Sect. 6, and a proof that M = M' in the equational theory. Similarly, we can eliminate these constructs from any closed value bv V : A.

Prop. 6.2, which does not involve stacks, is proved in [15]. One noteworthy equation of Fig. 5 is the ^-law

M • K = M to x. ((produce x) • K) (18)

This is equivalent to the two equations

M = M to x. produce x (19)

(M tox. N) • K = M tox. (N • K) (20)

(20) makes it clear why a stack denotes an algebra homomorphism in the monad models of CBPV. As instances of (20) we have many familiar CBPV equations:

(M to x. N) to y. P = M to x. (N to y. P) i.(M to x. Ni),...} = M to x. X{..., i.Ni,...} \y.(M to x. N) = M to x. Xy.N

In [15] rather complex lemmas were required to prove these valid in the categorical semantics. It is an advantage of working with stacks that these equations become straightforward.

7 General Theories

7.1 Signatures

We define a sequent to be a judgment without a term; thus a sequent is of the form r bv B or T bc B_ or bk K : 5'. Here T is a finite sequence A0,..., Ar-1 of value types, with no associated identifiers.

In order to follow the approach of [11], we need the facility to add primitive operations to CBPV. Each operation has a "sorting" which is a sequent1. For example, the sorting of + is nat, nat bv nat. Applied to two values of type nat, it makes a value of type nat. An operation div that produces a

1 In [11], the only primitive operations required are constants, because in simply typed A-calculus every sequent is equivalent to a closed sequent, but in CBPV that is not true for value sequents or stack sequents.

/ -laws

let V be x. Q = Q[V/x]

K where nil is L = K -+L

pm (1,V) as {...,(i, x).Qi,...} = Qi[V/x]

pm (V, V') as (x, y).Q = Q[V/x,V '/y]

force thunk M =M

(produce V) to x. M = M [V/x]

1\{...,i.Mi,...} = M-t

{...,Ki where i :: nil,. ..} is 1 :: L = Ki+L

V' Ax.M = M [V/x]

K where x :: nil is V :: L = K [V/ x] -+L

^-laws

Q[V/z] = pm V as {...,(i, x).Q[(^ x)/z],...}

Q[V/z] = pm V as (x, y).Q[(x, y)/z]

V= thunk force V

M • K = M to x. ((produce x) • K)

K+L = [•] to x. ((produce x) • K) :: L

M= A{...,i.i' M,...}

K+L = {.. . , (K+i :: nil) where i :: nil,. ..} :

M= Ax. (x' M)

K+L = (K-+x :: nil) where x :: nil is L

Fig. 5. Equational laws for CBPV + stacks

natural number, or—if the divisor is zero—raises an error, has the sorting nat, nat hc Fnat.

More distinctively, we could have stack-like operations that build a computation M into a computation f (V0,..., Vr-1\M) whose execution begins by executing M, and so the context f (V0,..., VT-i\[•]) is placed on to the stack for future use.

r | /0V--,K-i|M) B! K c

r I M B C

A signature is a collection of primitive operations; more formally, a function from sequents to sets. The associated rules are shown in Fig. 6. We have, as an instance of (20), the equation

f (Vo,..., Vr-i\M to x.N) = M to x. f (Vo,..., Vr-i\N)

r hv Vo : Ao ••• r hv i : Ar_ i

r hv f (Vo,...,Vr-i): B " v "

r hv Vo : Ao ••• r hv Vr-i : Ar-i

rhc /(F0,...,K-i) :B

feSiAo,...^^ hc B)

r hv Vb : A • • • T hv K-i : A-i T \~c M : B

---feS (A0,..., A^IB bk B')

r l~c /(Vo,..., Vr-i\M) : J? ~J

T hv : • • • T hv K-i : A-i T\B' \~k K : C r| R hk/(F0,...,K-i|H)::X:C:

Fig. 6. Rules For A Signature S

7.2 Theory/Model Equivalence

We are now in a position to state a theory/model equivalence theorem. Our formulation of this theorem (though not the proof) follows [11], in particular the use of structure preservation on the nose. For the purposes of this section, we insist on defining a cartesian category to be a category with distinguished terminal object and binary products. We cannot define it to be a category with distinguished n-ary products for all n E N, because the operations on objects and on types must be identical for the on-the-nose approach to work. This is a flaw, pervasive in categorical semantics, and we leave its rectification to future work.

We begin our account by defining an on-the-nose morphism from CBPV adjunction model (C, D, O,...) to CBPV adjunction model (C', D', O',...) in the obvious way. We then define Adj to be the category of CBPV adjunction models and on-the-nose morphisms.

A theory for CBPV+stacks consists of

• a type structure t, i.e. two (not necessarily small) sets valtypes t and comptypes t, equipped with a binary operation x on valtypes t, and similarly with operations for all the other connectives—note that t is not required to be freely generated

• a T-signature S, i.e. a function from sequents in t to sets

• a hypercongruence = on the terms-in-context in t generated from S according to Fig. 1, 4 and 6, containing all the equations of Fig. 5.

For a given T-signature S, we write law for the least congruence on terms-in-context using symbols in S that contains all the equations of Fig. 5. This is a hypercongruence (a generalization of Lemma 6.1).

Given a theory (t, S, =), we write S= (A0,..., An-i hv B) for the set of values A0,..., An-i hv V : B using the symbols of S, quotiented by the con-

K M • K K^L

nil M L

[•] to x. N :: K (.M to x. N)»K [•] to X. N :: (K-H-L)

i:: K (ilM) • K iv. (K-H-L)

V :: K (ViM)»K V :: (K-ti-L)

let V be x. K let V be x. (M • K) let V be x. (K-H-L)

pm V as {... , (i,x).Ki,...} pm V as {...,(i,x).(M.X,),...} pmV as {... ,(i,x).(Ki^L),...}

pm V as (x, y).K pm V as (x, y).(M • K) pm V as (x,y).(K-H-L)

K where nil is K' (M • K) • K' K where nil is (K'-H~L)

{... ,Ki where i :: nil,...} is K' (\{...,t.(M.Kt),...}).K' {...,Ki where i :: nil,...} is (K'-H~L)

K where x :: nil is K' (\x.(M • K)) • K' K where x :: nil is (K'~H~L)

f(V0,...,Vr-1\M)»K f(V{3,...,K-i|[-]):: (KHVL)

Fig. 7. Dismantling and Concatenation gruence =. We write similarly the quotient sets of computations and stacks.

Remark 7.1 It is clear that if (t,S, =) is a theory then S= is another t-signature.

Remark 7.2 An alternative formulation is followed in [8,13], avoiding the

let x be V. P (id, [V])*[P]

produce V [ V ] *prod

M to x. N [M]; qF [N]

force V V] * force

thunk M q^ M ]

Xx.M q- [M ]

V 'M [M]; [V]*ev

qn XI. {Mil

tlM {M ];() * n

(V, v) (V ] '])

pm V as (x, y). P ((id, ([V]; n)), ([V']; n'))*{P]

(i, v ) [V]; in,

pm V as {..., (1, x).Pi,. ..} (id,[V]) *qS XI. {Pi

nil id

[•] to x. M :: K (qF [M]); [K]

i :: K (()*n,); [K]

V :: K ([V] *ev); [K]

K where nil is L K]; [L]

{...,Ki where I :: nil, . ..} is L (qOXI.[Ki]);[L]

K where x :: nil is L (q- K ]);[L]

f (Vo,..., V—1) ([Vo] ,...,[Vr—i]) *f

f (Vo,...,Vr-i\M) [M ];( [Vo ] ,...,[Vr—i]) * f

f (V0,..., Vr—1 \[•]) :: K ([Vo],...,[Vr—i])*f; [K]

Fig. 8. Categorical semantics

notion of theory. We observe that S ^ S|aw extends to a monad TT on the category ST of r-signatures. We then define a direct model to be a type structure t together with an algebra for the monad TT. This notion is equivalent to that of theory.

morphism is the equivalence class of

[V]*[P] P[V/xo]

[M];[K] M • K

id nil

[K ];[L] K+L

n pmx0 as (x1; x2). x1

n1 pmx0 as (xi, x2). x2

([V], [V ']) (V,V)

force force x0

q^ [M] thunk M

produce produce x0

qF[M] [•] to xi. (M[(xo, xi)/xo]) :: nil

in» (Î, xo)

qsXi.[Pi] pm xo as {..., (xi, (i,xa)).Pi[(xi,x2)/xo],...} î :: nil

qH \i.[Mi] \{...,i.Mi,...}

qHAi.[Ki] {...,Ki where i :: nil,...} is nil

ev x0 :: nil

q^[M] Xxi.M [(xo, xi)/xo ]

q^[K] K[(xo,x1)/xo] where x1 :: nil is nil

Fig. 9. Classifying Model Of A Theory

An on-the-nose morphism F from a theory (t, S, =) to a theory (t', S', =')

provides

• a function from value types of t to value types of t', and similarly for computation types, preserving all connectives

• a function from SL(A0,..., Am— 1 hv B) to SL (FA0,..., FAm—1 hv FB), for each A0,... ,Am—1 and B, and similarly for computations and stacks, preserving (up to =') all the term constructors, as well as substitution, dismantling and concatenation.

We write Th for the category of theories and on-the-nose morphisms, and can

now formulate our main result.

Proposition 7.3 The categories Adj and Th are equivalent.

Proof. Let A = (C, D, O,...) be an adjunction model. We define its internal language LA to be the following theory. The type structure t is given by ob C and ob D. The signature S is given by

S(Ao,...,Ar-i hv B)=C(Ao x •••x Ar-i,B) S(Ao, . . . , Ar_ 1 b° R) = OAox-xAr-iM.

S(A0,..., Ar_,\R hk C)=VAox...xAr-M,C)

We next interpret the terms generated by S in A following Fig. 8, so that a stack A0,..., An_i |R bk C denotes a D-morphism over A0 x • • • x An_i from R to C, and similarly for values and computations. We state rather pedantically that this n-ary product in C is defined by left association, so the product of the singleton sequence A is 1 x A.

Next, we set = to be the kernel of [—]. We prove that M • K denotes [M]; [K] and similarly for substitution and concatenation. These are all straightforward inductions (the proof for substitution requires us to prove that all the isomorphisms in Def. 5.1 are natural in X). Consequently, = is a hypercongruence, as required. Showing that it satisfies all the required equational laws is straightforward.

In the opposite direction, given a theory L = (t, S, =) we define its classifying model CL to be the following adjunction model. The objects are just the types of t, and the operations on objects given by the type structure. The homsets are defined by

C(A, B) = S= (A hv B) OaR = S= (A bc R) Ca(R,C_) = S=(A\R bk C)

All the categorical operations are defined in Fig. 9, and proving the equations of an adjunction model is trivial, using Lemma 3.1 generalized to terms generated by S.

Next we have to show these two operations L and C to be inverse up to on-the-nose isomorphism. Given a model A = (C, D, O,...), write (t, S, =) for its internal language LA. We want to construct an on-the-nose isomorphism a a from A to the classifying category of (t,S, =). These two models have the same objects and operations on objects, and we set aA to be identity on objects. On homsets it is given by

C(A, B) = S=(A hv B) OaR ^ S=[A hc B)

VA{B,C) ^ S={A\B_ hk C)

iAi p 1 - [P ]

where we write iA for the isomorphism from A to 1 x A.

On the other hand, given a theory L = (t, S, =), we want to construct an on-the-nose isomorphism f3L from L to LCL = (t,S', ='). The two theories have the same type structure, and we set f3L to be identity on objects. On homsets it is given by

S=(Aq ,An-1 hv B) = Sl (Ao,An-1 hv B) S=(A0,An_! hc B) = SL(A0,An_! hc B)

[MI^UJ^^MKxo,...,^-!)] S=(A0,A^B hk C) = SL(A0,A^B hk C)

where we write jAo,..,An-1 for the context morphism from A0 x ••• x An-1 to A0,..., An-1. The inverse of f3A is defined by induction on terms:

[k*P ] - [[P ](vo,...,vn-i)] [P-1M • k*K] - [[K](Vo,Vn-i\M] kK-+@-1L - [[K](Vo,Vn-1\[^]) :: L]

where r hv Vi : Ai for i = 0,...,n — 1 and k is the context morphism (using symbols from S) from r to A0 x ••• x An-1 given by (fi-1V0,... ,P-1V^n-1). The other clauses are trivial, because f3—l must preserve all structure.

Checking that all the isomorphisms are indeed structure-preserving and the composites are identity is straightforward and tedious. It is obvious how to extend L and C to functors between Adj and Th, and it is then easily seen that a and f3 are natural. □

8 Call-By-Value is Kleisli, Call-By-Name is co-Kleisli

Given a CBPV adjunction model, we can form Kleisli and co-Kleisli adjunctions and obtain fully faithful comparison functors.

Kleisli

self C" >'

\ / \ / /x / \

co-Kleisli

For the Scott model, the Kleisli category (over 1) provides the partial maps model for CBV [18], while the co-Kleisli category (over 1) is pointed cpos and continuous maps—the standard model for CBN. This can be understood in terms of the translations [14] from CBV and CBN into CBPV. The details of the translations are not important here, just the following properties.

• A CBV term is translated into a CBPV computation of the form A0,..., An-i hc M : FB. Hence it is interpreted in the Kleisli category. In fact we have a strong adjunction between C and CUF, in the terminology of [16] this is a strong K-category.

• A CBN term is translated into a CBPV computation of the form UA0)..., UAn_1 bc M : B_. Hence it is interpreted in the co-Kleisli category, in the fibre over 1.

Notice that in a continuation model, the duality between the Kleisli and co-Kleisli categories [19,20] is a consequence of the duality between C and Di.

9 Further Work

Whilst we have attached names to the various adjunction models such as "storage", "erratic choice" and so forth, these names need to be justified and explained, especially as regards the D-morphisms. It remains also to describe more advanced models such as possible world and game models. Game models without a bracketing condition [9] are known to be continuation models [10], and hence must give adjunction models; but for well-bracketed game models [7] the situation is less immediate.

An interesting question is whether the universal properties of Sect. 5.1 can be described in naturality style rather than element style, by means of a Yoneda Lemma. A difficulty here is the requirement for (12) to be an isomorphism.

References

[1] Carboni, A., S. Lack and R. F. C. Walters, Introduction to extensive and distributive categories, Journal of Pure and Applied Algebra 84 (1993), pp. 145158.

[2] Cockett, J. R. B., Introduction to distributive categories, Mathematical Structures in Computer Science 3 (1993), pp. 277-307.

[3] Curien, P.-L. and H. Herbelin, The duality of computation., in: Proceedings of the A CM Sigplan International Conference on Functional Programming (ICFP-00), ACM Sigplan Notices 35.9 (2000), pp. 233-243.

[4] Felleisen, M. and D. P. Friedman, Control operators, the SECD-machine, and the X-calculus, in: M. Wirsing, editor, Formal Description of Programming Concepts, North-Holland, 1986 pp. 193-217.

[5] Filinski, A., "Controlling Effects," Ph.D. thesis, School of Computer Science, Carnegie Mellon University, Pittsburgh, Pennsylvania (1996).

[6] Fiore, M., G. D. Plotkin and D.Turi, Abstract syntax and variable binding, in: G. Longo, editor, Proceedings of the 14th Annual Symposium on Logic in Computer Science (LICS'99) (1999), pp. 193-202.

[7] Hyland, J. M. E. and C.-H. L. Ong, On full abstraction for PCF: I, II, and III, Information and Computation 163 (2000), pp. 285-408.

[8] Jeffrey, A., A fully abstract semantics for a higher-order functional language with nondeterministic computation, TCS: Theoretical Computer Science 228 (1999).

[9] Laird, J., Full abstraction for functional languages with control, in: Proceedings, Twelfth Annual IEEE Symposium on Logic in Computer Science, IEEE Computer Society Press, Warsaw, Poland, 1997, pp. 58-67.

[10] Laird, J., "A Semantic Analysis of Control," Ph.D. thesis, University of Edinburgh (1998).

[11] Lambek, J. and P. Scott, "Introduction to Higher Order Categorical Logic," Cambridge University Press, Cambridge, 1986.

[12] Lawvere, F. W., "Functional Semantics of Algebraic Theories," Ph.D. thesis, Columbia University (1963).

[13] Levy, P. B., X-calculus and cartesian closed categories (1996), essay for Part III of the Mathematical Tripos, Cambridge University, manuscript.

[14] Levy, P. B., Call-by-push-value: a subsuming paradigm (extended abstract), in: J.-Y. Girard, editor, Typed Lambda-Calculi and Applications, LNCS 1581 (1999), pp. 228-242.

[15] Levy, P. B., "Call-by-push-value," Ph.D. thesis, Queen Mary, University of London (2001).

[16] Levy, P. B., H. Thielecke and A. J. Power, Modelling environments in call-by-value programming languages, submitted.

[17] Moggi, E., Notions of computation and monads, Information and Computation 93 (1991), pp. 55-92.

[18] Plotkin, G. D., Lectures on predomains and partial functions (1985), course notes, Center for the Study of Language and Information, Stanford.

[19] Selinger, P., Control categories and duality: On the categorical semantics of the lambda-mu calculus, Mathematical Structures in Computer Science 11 (2001), pp. 207-260.

[20] Streicher, T. and B. Reus, Classical logic, continuation semantics and abstract machines, Journal of Functional Programming 8 (1998), pp. 543-572.