TYPE SYSTEMS FOR

POLYNOMIAL-TIME COMPUTATION

Vom Fachbereich Mathematik der Technischen Universität Darmstadt

angenommene

HABILITATIONSSCHRIFT

von

Martin Hofmann, PhD

aus Erlangen

Referenten:

Prof. Dr. Th. Streicher (Darmstadt) Prof. Dr. K. Keimel (Darmstadt) Prof. Dr. D. Basin (Freiburg) Prof. Ph. Scott, PhD (Ottawa)

Tag der Einreichung:  30. Oktober 1998
Tag des wissenschaftlichen Gesprächs: 12. Februar 1999

Abstract

This thesis introduces and studies a typed lambda calculus with higher-order primitive recursion over inductive datatypes which has the property that all definable number-theoretic functions are polynomial time computable. This is achieved by imposing type-theoretic restrictions on the way results of recursive calls can be used.

The main technical result is the proof of the characteristic property of this system. It proceeds by exhibiting a category-theoretic model in which all morphisms are polynomial time computable by construction.

The second more subtle goal of the thesis is to illustrate the usefulness of this semantic technique as a means for guiding the development of syntactic systems, in particular typed lambda calculi, and to study their meta-theoretic properties.

Minor results are a type checking algorithm for the developed typed lambda calculus and the construction of combinatory algebras consisting of polynomial time algorithms in the style of the first Kleene algebra.

Contents

1 Introduction 2

1.1 Primitive recursion with higher-order functions . . . . . . . . . . . . . . . . 3 1.1.1 Safe recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.1.2 Linearity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.1.3 Modal and linear function spaces . . . . . . . . . . . . . . . . . . . 6

1.2 Statement of soundness results . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.3 Primitive recursion with inductive datatypes . . . . . . . . . . . . . . . . . 9 1.4 Main result and proof idea . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 1.5 Expressivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.6 Previous and related work . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

2 Background 16 2.1 Complexity theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.2 Bounded recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.3 Safe recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.4 Tiered recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.5 Higher-order functions and the

typed lambda calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.5.1 Set-theoretic interpretation . . . . . . . . . . . . . . . . . . . . . . . 26 2.5.2 The system PV^{ω} . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

2.6 Background on categories . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.6.1 Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.6.2 Terminal object and cartesian products . . . . . . . . . . . . . . . . 29 2.6.3 Functors and natural transformations . . . . . . . . . . . . . . . . . 30 2.6.4 Canonical isomorphisms and object equality . . . . . . . . . . . . . 31 2.6.5 Subcategories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 2.6.6 Global elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 2.6.7 Function spaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 2.6.8 Interpretation of the typed lambda calculus . . . . . . . . . . . . . 33 2.6.9 Logical relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 2.6.10 Presheaves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 2.6.11 Adjoint functors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 2.6.12 Comonads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 2.7 Affine linear categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

2.7.1 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 2.7.2 Wiring maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 2.7.3 Indexed tensor product . . . . . . . . . . . . . . . . . . . . . . . . . 53 2.7.4 Linear function spaces . . . . . . . . . . . . . . . . . . . . . . . . . 53 2.7.5 Comonads in ALC . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 2.7.6 ALC and the Yoneda embedding . . . . . . . . . . . . . . . . . . . 55

3 The type system SLR 61 3.1 Types and subtyping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 3.2 Terms and typing rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

3.2.1 Contexts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 3.2.2 Typing rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 3.2.3 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

3.3 Syntactic metatheory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 3.4 Comparison with other systems . . . . . . . . . . . . . . . . . . . . . . . . 77 3.5 Set-theoretic semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 3.6 Reduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 3.7 Alternative syntax with modal types . . . . . . . . . . . . . . . . . . . . . 80 3.8 Other variations of SLR . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

4 Semantics of SLR 85

4.1 A BCK-algebra of PTIME-functions . . . . . . . . . . . . . . . . . . . . . . 86 4.1.1 A new length measure . . . . . . . . . . . . . . . . . . . . . . . . . 87 4.1.2 Construction of H_{p} . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 4.1.3 Truth values and numerals . . . . . . . . . . . . . . . . . . . . . . . 92

4.2 Realisability sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

4.2.1 Natural numbers and other datatypes . . . . . . . . . . . . . . . . . 95 4.3 Interpreting recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

4.3.1 Leivant trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 4.4 Recursion operators as higher-typed constants . . . . . . . . . . . . . . . . 104

4.4.1 Polynomial-time functions via a comonad . . . . . . . . . . . . . . . 106 4.4.2 Interpretation of SLR . . . . . . . . . . . . . . . . . . . . . . . . . . 109 4.4.3 Main result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 4.4.4 Interpretation of λ^{□!} . . . . . . . . . . . . . . . . . . . . . . . . . . 114

4.5 Duplicable numerals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 4.6 Computational interpretation . . . . . . . . . . . . . . . . . . . . . . . . . 121 4.7 More BCK-algebras . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122

4.7.1 Linear lambda terms . . . . . . . . . . . . . . . . . . . . . . . . . . 123 4.7.2 A BCK-algebra of all PTIME-functions . . . . . . . . . . . . . . . . 123

5 Conclusions and further work 125

Acknowledgements

This thesis would not have come into existence without the help of several people to all of which I should like to express my thanks here.

My advisors Klaus Keimel and Thomas Streicher and my referees David Basin and Philip Scott read and examined this thesis and provided helpful comments.

Invitations to seminars or workshops by Carsten Butz, Neil Jones, Ulrich Kohlenbach, Helmut Schwichtenberg and Daniel Leivant allowed me to present preliminary stages of this work and to obtain valuable feedback.

Benjamin Pierce introduced me to implementing type checkers in ML and allowed me to use his F_{≤}^{ω}-implementation as a basis for the typechecker for the systems described in this thesis.

Peter Lietz deserves special thanks for proofreading large parts of this thesis and spotting several typos and ambiguities.

Finally, the following people provided stimulating input to my research through discussion: Samson Abramsky, Thorsten Altenkirch, Stephen Bellantoni, Peter Clote, John Longley, Karl-Heinz Niggl, Gordon Plotkin, John Power, Helmut Schwichtenberg.

Chapter 1

Introduction

The aim of this thesis is twofold. Firstly, we develop type-theoretic syntactic restrictions of primitive recursion on inductively defined data structures which ensure polynomial time complexity.

The second more subtle goal is to illustrate the usefulness of category-theoretic semantics as a technique for guiding the precise formulation of syntactic systems and to establish metatheoretic properties.

The main technical result of the thesis is the proof that all first-order functions definable in a certain typed lambda calculus are polynomial time computable. This proof proceeds by exhibiting a category-theoretic model of the calculus in which all denotations of first-order functions are polynomial time computable by construction.

In retrospect, the semantic method may seem complicated as it involves quite a bit of technical machinery and it might well be that sooner or later a more direct syntactic proof will appear.

To appreciate the semantic approach the reader should bear in mind the following points.

– The apparent overhead of the semantic approach is largely due to the need of developing several general concepts such as affine linear categories, comonads, functor categories, logical relations, realisability sets. Once these concepts are available the actual proofs are quite short and perspicuous as they emphasize the invariants needed to establish the desired property. The abovementioned general concepts we develop in Chapter 2 and Section 4.2 of Chapter 4 are with very few exceptions independent of the particular application and can be used in other situations.

– The syntax has been developed hand in hand with the semantics and has been guided by the semantic intuition. In the author’s opinion the type system described in Chapter 3 is very clear, almost free from ad-hoc constructions, and easily extensible. These (obviously subjective) properties are largely due to the semantic intuition for the syntactic concepts such as modal and linear function spaces.

1.1 Primitive recursion with higher-order functions

The starting point of this thesis is an analysis of higher-order primitive recursion on natural numbers and other inductive datatypes, in particular of the situations which lead to superpolynomial growth and/or runtime.

First, we recall that the size of natural numbers is defined as the length of their binary representation, i.e., |x| = ⌈log_{2}(x + 1)⌉^{1}.

This means that ordinary primitive recursion which expresses a value f(x) in terms of f(x − 1) will almost always lead to exponential (in |x|) runtime of f.

In order to define polynomial time functions on natural numbers one should use recursion on notation as given by the following schema:

Definition 1.1.1 Let A be a set, g ∈ A, and h ∈ ℕ×A ^{}A. The function f : ℕ ^{}A is defined from g, h by recursion on notation, if

\begin{matrix}f(0) = g \\ f(x) = h(x, f(⌊\frac{x}{2}⌋)), when x > 0 \end{matrix}

The set A is called the result type, the function h is called the step function of the recursive definition

The number of recursive unfoldings needed to evaluate f(x) is precisely |x|, thus linear in |x|. Nevertheless, as we will now show, recursion on notation leads beyond polynomial time. Consider the following definition of a function sq : ℕ ^{}ℕ.

sq(0) = 1

sq(x) = 4 · sq(⌊\frac{x}{2}⌋), when x > 0

This defines a function of quadratic growth, more precisely sq(x) = [x]^{2} where [x] =_{def} 2^{|x|} is the least power of two greater than x. From sq we can then define an exponentially growing function exp : ℕ ^{}ℕ by

exp(0) = 2

exp(x) = sq(exp(⌊\frac{x}{2}⌋)), when x > 0

Indeed, exp(x) = 2^{[x]}.

Once we have exponentiation we can define ordinary primitive recursion in terms of recursion on notation so that we are back to square one, as it were.

One can rule out the definition of such fast-growing functions by requiring an a priori bound on the function to be defined. This leads to Cobham’s recursion-theoretic characterisation of the polynomial time computable functions which we discuss later in Section 2.2. Our aim in this thesis is, however, not to require explicit bounds be it on size or on runtime, but to ensure polynomial time by syntactic type-theoretic restrictions. For first-order functions such syntactic restrictions exist in the form of tiered and safe recursion.

1More generally, we write |x⃗| for (|x_{1}|, …, |x_{n}|) when x⃗ = (x_{1}, …, x_{n}).

1.1.1 Safe recursion

Leivant [25] and Bellantoni-Cook [3] have carefully analysed recursion on notation schemes and concluded that the deeper reason for the definability of exponentiation is that in the recursive equation for exp(x) we apply a recursively defined function, namely sq to the result of a recursive call, namely exp(⌊\frac{x}{2}⌋). Their systems of tiered [25], respectively safe [3] recursion consist of formal syntactic restrictions which effectively rule out such application of recursively defined functions thereby ensuring that the definable functions are exactly those computable in polynomial time. For reasons that become clear later we call this a modality restriction.

In a nutshell the idea behind safe recursion is that variables are grouped into two zones usually separated by a semicolon like so: f(x⃗; y⃗). The variables before the semicolon are called normal whereas those after the semicolon are called safe. The intended invariant is that such a function is polynomial-time computable and moreover satisfies a growth restriction of the form

|f(x⃗; y⃗)| ≤ p(|x⃗|) + max(|y⃗|)

for some polynomial p. The syntactic restrictions are now set up in such a way that this invariant is maintained. In particular one imposes that recursion on notation is always over a normal variable, but that results of recursive calls must be accessed via safe variables only; in other words from g(u⃗; v⃗) and h(x, u⃗; y, v⃗) we may form f(x, u⃗; v⃗) by f(0, u⃗; v⃗) = g(u⃗; v⃗) and f(x, u⃗; v⃗) = h(x, u⃗; f(⌊\frac{x}{2}⌋ , u⃗; v⃗), v⃗), but such recursion is not allowed if y also is a normal variable.

The function sq(x) = [x]^{2} can now be defined from g(; ) = 1 and h(x; y) = 4y (Multiplication by two, hence by four is considered as a basic function of a safe argument). For exp(x) we would have to use h(x; y) = sq(y; ) which is not allowed as y is a safe variable in the left-hand side, but normal in the right-hand side. More details will be given in Section 2.3. Tiered recursion will be described in Section 2.4.

1.1.2 Linearity

These systems were designed for a first-order result type A = ℕ. With a functional result type, e.g., A = ℕ→ℕ (the set of functions from ℕ to ℕ), new phenomena occur which again allow for the definition of non-polynomial time computable functions. Consider the following recursion on notation with result type A = ℕ→ℕ.

\begin{matrix}f(0) = λy∈ ℕ.2y \\ f(x) = f(⌊\frac{x}{2}⌋) ∘ f(⌊\frac{x}{2}⌋) \end{matrix}

Here λy∈ ℕ.2y denotes the function ℕ∋x ↦ 2x and ∘ is composition of functions in the applicative order. In other words, we have

\begin{matrix}f(0, y) = 2y \\ f(x, y) = f(⌊\frac{x}{2}⌋ , f(⌊\frac{x}{2}⌋ , y)) \end{matrix}

Notice that this recursion must be considered “safe” as no previously recursively defined function is applied to results of recursive calls.

Yet, induction on x shows that f(x, y) = exp(x) · y.

We claim that this is due to the fact that f(⌊\frac{x}{2}⌋) is called twice in the course of the computation of f(x) and we will show that if we allow at most one “use” of the result of a recursive call then higher result types do not lead beyond polynomial time. This restriction will be called a linearity restriction.

One may think that in the above example the nested application of f(⌊\frac{x}{2}⌋) in the step

function is the culprit and that several independent usages of f(⌊\frac{x}{2}⌋) might be harmless. However, by adapting an example from [28] one obtains a definition of an evaluation function for quantified boolean formulas which is complete for polynomial space and thus (commonly believed not to be) polynomial time computable.

Let us now give an example of a useful recursive definition which does obey the linearity restriction.

Consider an addition function add : ℕ ^{}A where A = ℕ^{3}→ℕ specified by

add(l, x, y, c) = x + y + (c mod 2) provided |l| ≥ max(|x|, |y|)

Such a function admits a definition by recursion on notation with result type A = ℕ^{3}→ℕ using only case distinction on parity and binary successor functions S_{0}(x) = 2x, S_{1}(x) = 2x + 1 and some self-explanatory abbreviations:

add(0) = λ(x, y, c)∈ ℕ^{3}.c mod 2
add(x) = λ(x, y, c)∈ ℕ^{3}.let carry = (x ∧ (y ∨ c))((¬x) ∧ (y ∧ c)) in

if x ⊕ y ⊕ c = 0

then

S_{0}(add(⌊\frac{x}{2}⌋)(⌊\frac{x}{2}⌋ , ⌊\frac{y}{2} ⌋ , carry)) else

S_{1}(add(⌊\frac{x}{2}⌋)(⌊\frac{x}{2}⌋ , ⌊\frac{y}{2} ⌋ , carry))

This definition obeys the following two restrictions. For one thing, no recursively defined function is applied to the result of a recursive call; for another the step function h : ℕ × A ^{}A uses its second argument only once. Notice that the argument add(⌊\frac{x}{2}⌋) literally appears twice and even gets applied to different values each time. However this does not count, since these branches are within different branches of a case distinction. We shall see below how this is expressed syntactically.

The above definition can be seen as a recursion on notation with first-order result type A = ℕ and substitution of parameters. Indeed, “linear” recursion with functional result type A = ℕ^{k}→ℕ can always be reduced to recursion with parameter substitution using a semantic technique described in [18, 20]. However, as we shall describe, linearity is more subtle in the presence of other inductive datatypes such as binary trees.

1.1.3 Modal and linear function spaces

One of the main contributions of this work will be the definition and analysis of a formal system in which these criteria, viz. linearity and modality, have a well-defined syntactical status. This system takes the form of a typed lambda calculus (a formal system for defining higher-order functions) with three kinds of function spaces:

– Modal function space, written □A→B: A function of this type is allowed to use its argument as input to a recursively defined function and also an arbitrary number of times. In particular the type of functions defined by recursion on notation with result type A will be □N→A^{2}.

– Linear function space, written A⊸B: A function of this type is neither allowed to recurse on its argument nor to use it more than once. The application functional h(f) = f(0) is in (N→A)⊸A or (□N→A)⊸A. Also basic functions such as S_{0}(x) = 2x and S_{1}(x) = 2x + 1 are in N⊸N.

– Nonlinear function space, written A→B: A function of this type is allowed to use its argument an arbitrary number of times without, however, recursing on it. The ordinary function space is included in the modal one and contains the linear function space. A typical inhabitant of this function space which is not linear is the composition functional h(f) = f ∘ f which is in A→A when A is of the form B⊸B, B→B, or □B→B.

We will sometimes adopt the convention that a function on natural numbers is always linear, even if it uses its argument more than once, i.e., we may identify N→A and N⊸A.

The scheme of recursion on notation is then subject to the following three restrictions:

– The step function must be of type □N→A⊸A, i.e., f(⌊\frac{x}{2}⌋) must be used at most once in the definition of f(x) and in particular must not appear as a subterm of a term to be recursed on; the recursion variable x itself may be used more than once and even recursed on.

– The type of a function defined by recursion on notation is □N→A.

– The result type must be built up from N and ⊸ (and a few more constructors to be introduced later); in particular it must neither contain modal nor ordinary function space. Such types will be called safe types.

The first restriction rules out both nonlinear and recursive use of results of recursive calls.

The second restriction is a consequence of the first one; recursively defined functions get modal type thus (by the first restriction) cannot be used within step functions.

2We will write N for the type expression denoting the set ℕ of natural numbers. This distinction between type expression and denoted set becomes important when we consider other interpretations.

The third restriction is necessary since otherwise the first restriction could be overcome by a kind of tail recursion as in

\begin{matrix}f(0) = λy∈ N.y \\ f(x) = λy∈ N.f(⌊\frac{x}{2}⌋ , sq(y)) \end{matrix}

Here, the step function

h(x, u) = λy∈ N.u(sq(y)))

has the type □N→(□N→N)⊸(\underline{□}N→N) where the underlined □ comes from the fact that y is used as argument to a recursively defined function, namely sq.

If result type A = □N→N would be allowed then this would be a legal recursion on notation; however, f(x, y) = y^{[x]}.

Syntactically, these restrictions are enforced by representing recursion on notation as a family of higher-order constants

rec^{N}[A] : N⊸(□N→A⊸A)→□N→A

for each allowed result type A. In an application rec^{N}[A] g h the first argument g is the base case and the second one h is the step function. Notice that the passage from step function to the recursively defined function is nonlinear. Were it linear one could circumvent the syntactic restrictions by some encoding.

The main contribution of this thesis is an appropriate formalisation and semantic interpretation of these three function space which ensures that the ensuing recursion patterns allow the definition of polynomial time computable functions only. The proof of this soundness property proceeds by interpretation in a model which at the same time provides semantic intuition for the three function spaces.

1.1.3.1 Role of the nonlinear function space

Several people have asked why we need the intermediate nonlinear function space A→B and whether it wouldn’t be simpler to have just A⊸B and □A→B. The answer is that the nonlinear function space allows for finer typings and higher expressivity, but can be left out if the user does not feel comfortable with it.

The main purpose of the nonlinear function space is that it allows one to postulate the type equality N→A = N⊸A which permits multiple use of ground type variables. Without nonlinear function space this would have to be formulated as an ad-hoc condition. Nonlinear function space also allows for a slightly more generous typing of recursion constants, cf. Remark 3.2.1.

1.2 Statement of soundness results

Due to the presence of higher-order functions and different function spaces it might not be immediately clear how to express formally that all definable function are polynomial time computable and indeed there are different inequivalent such formalisations. The approach we take is based on a set-theoretic interpretation of our systems which interprets all three function spaces as set-theoretic function space and expressions as elements of the denotation of their type. In particular, recursion on notation will be interpreted as in Def. 1.1.1. In other words, this set-theoretic interpretation simply consists of forgetting about linearity and modality and treating functional expressions as the functions they intuitively denote. The soundness property is then stated as follows

Principle 1.2.1 (Soundness, first version) The set-theoretic interpretation of a term of type □N→N is always a polynomial time computable function.

In favour of this definition we put forward the following reasons.

– Eventually we are only interested in evaluating programs sending integers to integers; higher-order functions only appear as subterms of first-order programs.

– If in our system it was possible to define a non-polynomial time computable (in some sense) higher-order function then in many cases it would be possible to construct from it a non-polynomial time computable function of type □N→N in contradiction to the soundness principle.

Crucial to this approach is that higher-order functions are viewed as auxiliary concepts needed to structure, modularise, and simplify eventually first-order programs. If we allow for programs with functional input as might occur e.g., in exact real number computation then a more fine-grained approach could be necessary. It is known, see e.g. [8] that there are several non-equivalent notions of second-order polynomial-time computability which are equivalent as far as the effect on first-order functions is concerned.

Again in support of our view, one could say that even programs computing with exact real numbers will eventually accept environment requests such as keyboard inputs and produce answers such as screen outputs so that in essence a first-order function is computed the complexity of which can be captured by our approach.

Next, we should explain the prominent status of polynomial time in our approach. For one thing, polynomial time is generally considered as the appropriate mathematical formalisation of “feasible computation”. As any formalisation it has its limits and indeed, as is well-known, there are several obviously feasible algorithms which exhibit superpolynomial runtime in the worst case (simplex algorithm, ML type inference) and there are even undecidable problems which can nevertheless be decided quickly in all interesting cases (subtyping in F_{≤} [36]). On the other hand, in some cases even quadratic runtime must be considered unfeasible. But it seems difficult to give a better formalisation of feasible computation without getting lost in details.^{3}

Secondly, the methods we use are to a large extent independent of the particular complexity class and could well be applied to higher-order extensions of recursion schemata capturing other classes as can be found e.g. in [7]. The advantage of polynomial time as opposed to smaller classes is that it is relatively robust with respect to choice of encoding, machine model, etc. and thus allows one to largely abstract away from these issues.

3Recently, some people have proposed to identify “feasibility” with the probabilistic complexity class BP P. We will not consider this.

Finally, one might object against the platonic existence statement contained in the soundness principle. What does it help if we know that the function computed by our program is in principle polynomial time computable when our very program computes it in, say, exponential time.

At this point a finer look at the proofs of the soundness property is needed. As will turn out, these proofs are entirely constructive, so that it is possible to extract an algorithm, viz., a compiler, which effectively transforms a program of type □N→N into an extensionally equal polynomial time algorithm. Due to the fact that already the expansion of higherorder definitions takes superpolynomial time [40] we cannot expect that this compilation process itself runs in polynomial time.

In current work, which is, however, not reported in this thesis we implement this translation and explore to what extent it gives efficiency gains compared to usual compiler techniques which do not take into account the extra information given by the linear/modal typing.

So, summing up, we will actually establish the following refined version of the soundness principle.

Principle 1.2.2 (Soundness, final version) The set-theoretic interpretation of a term of type □N→N is always a polynomial time computable function and a polynomialtime algorithm witnessing this can be effectively obtained from such term.

1.3 Primitive recursion with inductive datatypes

The formulation of the soundness principle is sufficiently flexible to encompass data structures other than the natural numbers such as lists or trees. All we ask is that their addition should not affect the complexity of definable functions of type □N→N.

Inductive datatypes can be used in two different ways. For one thing, they can be part of result types, for another we can define functions on inductive datatypes by recursion along their inductive definition.

As prototypical examples we will in this thesis use lists and binary labelled trees, both over some parameter type which undergoes the same restriction as the result type of recursion on notation. Other inductive types can be added by following this pattern provided their constructors do not contain functional arguments. Inductive types which do not satisfy this restriction and more generally mixed-variance recursive types such as D ≅ N⊸D can also be added, but no recursor will be available for those. The details of these extensions are not contained in this thesis and will be presented elsewhere.

Lists over type A come with two primitive constructor functions nil : L(A), denoting the empty list and cons : A⊸L(A)⊸L(A), where cons(a, l) is obtained from l by prefixing a.

Trees T(A) over type A are constructed by way of the functions leaf : A⊸T(A) and node : A⊸T(A)⊸T(A).

Notice the linear typing of these operations.

If we were to encode lists or trees as natural numbers using some (recursively defined) pairing function then we would obtain the weaker typings

cons : A⊸□L(A)→L(A)
node : A⊸□T(A)→□T(A)→T(A)

which prevents the use of cons and node in step functions and thus the definition of almost any reasonable function involving lists or trees.

This also suggests that (unlike e.g., in Gödel’s system T) lists and trees can not be obtained as a definitional extension from the subsystem with natural numbers and functions between them.

Definition 1.3.1 A function f on lists yielding values in B is defined from h_{nil} and h_{cons} by list recursion with result type B and step function h_{cons} if

f(nil) = h_{nil}
f(cons(a, l)) = h_{cons}(a, l, f(l))

In our system the step function is required to be of type □(A)→□(L(A))→B⊸B and the resulting function f will be of type □L(A)→B.

Definition 1.3.2 A function f on trees yielding values in B is defined from h_{leaf} and h_{node} by tree recursion with result type B and step function h_{node} if

f(leaf(a)) = h_{leaf}(a)
f(node(a, l, r)) = h_{node}(a, l, r, f(l), f(r))

In our system the step function is required to be of type

□(A)→□(T(A))→□(T(A))→B⊸B⊸B

and the resulting function f will be of type □T(A)→B.

Here is an example of a function involving recursion on notation with result type L(A). Let a : A be a parameter and define f_{a} : □N→L(A) by

\begin{matrix}f_{a}(0) = nil \\ f_{a}(x) = cons(a, f_{a}(⌊\frac{x}{2}⌋)) \end{matrix}

We get f_{a} : □N→L(A) and λa: A.f_{a} : A→□N→L(A). Giving the stronger type A⊸□N→ L(A) would be incorrect as a is effectively used more than once.

Indeed, together with constructions for decomposing lists which we introduce later we are able to define a function g : L(N⊸N)⊸N⊸N which composes the first two entries of a list, i.e., g(cons(u, cons(v, l))) = u ∘ v, which together with the (illegal) typing of f above would allow us to define exp : □N→N using recursion on notation with result type N⊸N.

As an example of list recursion, we can define concatenation as a function of type □L(A)→ L(A)⊸L(A) by

append(nil, l) = l

append(cons(a, l′), l) = cons(a, append(l′, l))

Here l is a parameter of type L(A), the result type is L(A) and the step function is h_{cons}(a, l′, k) = cons(a, k) which admits the type □L(A)→□A→L(A)⊸L(A). In fact, it even has the stronger type L(A)⊸A⊸L(A)⊸L(A). We remark at this point that through the use of subtyping we do not need to commit ourselves to a particular type when defining a function. Within certain limits the appropriate one can be inferred whenever a term is used.

Binary trees as result type provide another justification for the linearity restriction in step functions. The function dup(x) : leaf(0, x, x) takes a tree x: T(N) and constructs a new tree labelled with 0 and having two copies of x as immediate successors. This function dup gets the type T(N)→T(N) rather than T(N)⊸T(N) because its argument is used twice. This thwarts the following tentative definition of a function f : □N→T(N):

\begin{matrix}f(0) = leaf(0) \\ f(x) = dup(f(⌊\frac{x}{2}⌋)) \end{matrix}

This function constructs a tree of size 2^{|n|} thus exhibits exponential growth. In Leivant’s system [25], which does not provide linearity, such function is nevertheless allowed, which he justifies by measuring trees by their depth rather than by their number of nodes. Caseiro [5], from where this example is taken, develops semantic criteria which rule out such functions. Loc. cit. does not contain linear types, however.

Using tree recursion we can define a function z : □T(N)→N⊸N such that z(t, x) = 2^{#(t)} · x where #(t) is the number of leaves in t:

z(leaf(a)) = λx.2x
z(node(a, l, r)) = λx.z(l)(z(r)(x))

We have z(f(x), 1) = 2^{[x]} which is not polynomial time computable and shows why f must be rejected. In Leivant’s system this is not possible as it does not allow for functional result types.

1.4 Main result and proof idea

The main result of this thesis is the proof that the above-described modal linear lambda calculus (or rather a suitable formalisation thereof) is sound in the sense spelled out in Section 1.2 above.

The idea behind this proof is to show that a function

f : □A_{1}→ … □A_{m}→B_{1}⊸ … B_{n}⊸C

where the types A_{i}, B_{j}, C are “safe” in the sense of Section 1.1.3 is polynomial time computable and satisfies a growth restriction of the form

ℓ(f(x⃗; y⃗)) ≤ p(ℓ(|x⃗|)) + ∑_{i=0}^{n} ℓ(y_{i})

where p is a polynomial depending on f and ℓ is an appropriate length measure which on integers agrees with the usual length in binary. There is an obvious formal similarity to the Bellantoni-Cook invariant used to establish soundness of safe recursion mentioned in Section 1.1.1. The main difference is the replacement of maximum by summation. This is what accounts for linearity on the semantic side. For one thing the constructor for trees will in general satisfy ℓ(node(a, l, r)) = ℓ(a) + ℓ(l) + ℓ(r) + O(1), but not ℓ(node(a, l, r)) = max(ℓ(a), ℓ(l), ℓ(r)) + O(1). For another, we can adopt the view that the length of a function f : A ^{}B is the amount by which it extends the length of its input, i.e.,

ℓ(f) = max_{a∈A}(ℓ(f(a)) −^{.} ℓ(a))

where a ranges over the possible inputs to f. (We may put ℓ(f) = ∞ if the maximum doesn’t exist.) Then the length of a composition g ∘ f will be less or equal to ℓ(f)+ℓ(g), but in general not be bounded by max(ℓ(f), ℓ(g)). Thus, if we want to define step functions involving composition and, more generally, linear lambda calculus, then we are lead to replace maximisation by summation in the Bellantoni-Cook invariant.

This invariant also shows the necessity of linearity: Suppose that ℓ(f(x, y)) ≤ ℓ(x) + ℓ(y) + c for some constant c. If we form g(x) = f(x, x) then we can in general not find a constant d so that ℓ(g(x)) ≤ ℓ(x) + d. Thus, the above invariant is not maintained under identification of arguments.

Most of the technical work described in this thesis can be seen as an appropriate formalisation of this rather intuitive idea. The necessary steps are the following.

– We must find an appropriate notion of polynomial time computability for higherorder linear functions and also an appropriate length measure for those. This will be done using an applicative structure of certain length bounded polynomial time algorithms. The main result in this part is that these algorithms can be organised into a so-called BCK-algebra which via the “modest-set construction” provides an appropriate algorithmic interpretation for the linear lambda calculus fragment of our system. This interpretation fleshes out the above-mentioned “polysum” invariant.

– We must give meaning to types other than those of the form □A⃗→B⃗⊸C, in particular the nonlinear function spaces and iterated modal function space like in e.g., □(□N⊸N)⊸N. This is done by a general category-theoretic technique derived from the Yoneda embedding. The results we develop here are formulated and proved in a sufficiently abstract way so that they become independent of the intended application and could be used in other situations as well.

– We must formally define the syntax and typing rules for modal linear type lambda calculus, define its interpretation in the category-theoretic semantics and relate it to its set-theoretic standard interpretation to deduce the main result. Here our rationale was that the terms should not contain any term-formers or constants referring to linearity or modality. The user should rather write down terms which look like ordinary terms in simply-typed lambda calculus—the “aspect”, i.e., modal or linear function space will be inferred automatically according to the usages of abstracted variables.

In our opinion such inference mechanism simplifies the use of the system and indeed experiments with a prototype implementation are promising in this respect.

The category-theoretic semantics which gives the desired soundness proof is, however, within a wide range, independent of the particular syntax chosen. We will exploit this fact by defining another calculus which has only a single function space: A⊸B and two unary type formers □(−) and !(−) which allow the definition of modal function space as □(A)⊸B and of nonlinear function space as !(A)⊸B. In this calculus we do need extra constants referring, e.g., to necessitation.

The category-theoretic semantics can be used without any changes to interpret this alternative calculus thus giving a soundness result for it as well.

1.5 Expressivity

Another possible criticism against the soundness principle is that it does not say anything about the expressivity of our systems; indeed, it could be trivially met by a programming language which does not define any function at all or only constant functions.

Of course, soundness is only a minimal sanity condition and we have to argue for expressivity separately, notably by giving examples and by providing as many accepted programming language constructs as possible.

The systems presented in this thesis will enjoy the property that all polynomial time computable functions are in fact definable by terms of type □N→N. However, such completeness property does not say very much about the expressivity of a system from a programming language point of view as the proof of such property can be (and usually is) conducted by an encoding of Turing machine computations. Indeed, the implementation guaranteed by such completeness theorems usually proceed by showing that functions of arbitrary polynomial growth can be represented and that a function step : □N→N⊸N with the property that step(n, c) is the next configuration of a Turing machine configuration c provided |c| ≤ |n|. Given these ingredients any polynomial time computable function can be defined by iterating step sufficiently many times.

Clearly, this is practically unsatisfactory; not only because it involves rather heavy encoding; this in fact could be overcome using appropriate abbreviations; but more seriously because it places the burden of finding an appropriate polynomial runtime bound on the programmer rather than on the type system.

Admittedly, one cannot expect much more from a completeness theorem which should encompass all polynomial time computable functions even those which are polynomial time computable for some deep mathematical reason rather than something superficial that could be detected automatically by a type system. However, in order that our systems can claim to be of relevance for practical programming we must provide constructs which allow the programmer to code in an uncontrived way as close as possible to what he or she is used to.

The addition of higher-order functions as well as inductive datatypes are a step in this direction on the theoretical side. On the practical side, we have developed an implementation which allows one to carry out somewhat larger examples.

1.6 Previous and related work

The system presented here is closely related to the one developed independently by Bellantoni-Niggl-Schwichtenberg [2]. Their system also uses modal and linear types and boasts safe recursion with higher result type. At present, it is based entirely on integers and the only type formers are the function spaces □A→B (in their notation !A⊸B) and A⊸B.

Another important difference is the setup of the type system as a whole. Whereas the present system enjoys linear time decidability of all judgements, well-typedness in [2] is intertwined with untyped reduction and consequently as complex as the latter. We also believe that due to subtyping and modality inference the present system is somewhat easier to use in practice than the other one.

The main difference between the two approaches lies, however, in the soundness proof. Whereas op. cit. is based on a syntactical analysis of a normalisation procedure the present proof is based on an interpretation of the calculus in a semantic model. It is at this point not clear whether this syntactic method can be extended to the more general syntax studied here, e.g., general inductive types, cartesian products, and polymorphism.

Another related system is Girard-Asperti’s [13, 1] Light Linear Logic (LLL). Like [2] this system is a linearly typed lambda calculus admitting a polynomial time normalisation procedure. Although it can be shown that all polynomial time functions are expressible in LLL, the pragmatics, i.e., expressibility of particular algorithms, is unexplored, and superficial evidence suggests that the system would need to be improved in this direction so as to compete with our calculus. A more detailed comparison between the available programming patterns in either system would be very desirable, but must at present await further research.

The systems of tiered recursion studied by Leivant and Marion [26, 27, 28] also use restrictions of primitive recursion in order to achieve complexity effects. One difference is that the use of modality is replaced by the use of several copies of the base types (“tiers”). Another difference is that linearity and the ensuing recursion patterns with higher-result type have not been studied in the context of the Leivant-Marion work. Accordingly, functional result types in these systems lead to higher complexity classes [28]. We show in Section 2.4 that safe recursion can be seen as a “high-level language” for tiered recursion, i.e., that there is a procedure which annotates an SLR-term (without higher-order result types) with tiers so that it becomes typable in Leivant-Marion’s system. It therefore seems plausible that by adding linearity to their system one could achieve higher-order result types and conversely that the characterisations in [28] could be transported to the realm of safe recursion.

Finally, we mention Caseiro’s systems [5]. She studies first-order extensions of safe recursion with inductive datatypes and develops criteria which apply to recursion patterns presently not allowed in SLR like the one used in the direct definition of insertion sort. Unfortunately, these criteria are rather complicated, partly semantical, and do not readily apply to higher-order functions. We hope that by further elaborating the techniques presented in this paper it will be possible to give a type-theoretic account of Caseiro’s work which would constitute a further step towards a polynomial time functional programming language. In [21] we have carried out a step in this direction.

Superficially related are the works by Otto [31] and Goerdt [14]. Otto uses categorytheoretic methods to account for the syntax of safe recursion, whereas we use these tools to provide a semantical proof of soundness.

Goerdt’s approach guarantees polynomial time complexity of functions definable in a system like Gödel’s T without any syntactic restrictions by interpreting it in a finite model. In other words, there is an a priori maximum number N for intermediate results. The successor functions are interpreted as S_{i}(x) = min(N, 2x + i) so that the bound N is never exceeded. The price to be paid is that when reasoning about programs written in this system one always has to take into account the possibility of cut-off due to overflow.

Background

In this chapter we develop the necessary technical background in the fields of computability and complexity theory, lambda calculus and type systems, and category theory. Most of the material is well-established or part of the folklore in the field. To the author’s knowledge, are the following parts original:

– The use of presheaf semantics to establish conservativity of a higher-order extension of a first order theory with binding operators such as F, see also [15],

– The comonad semantics of modal lambda calculus and the relationship between S4 modal operators and safe recursion, see also [18],

– The notion of well-pointed affine linear category, the use of extensional presheaves in this context, in particular the definition of the !-operator in Ext(ℂ).

– The back-and-forth translation between safe recursion and tiered recursion.

The description is meant to be understandable for readers without prior knowledge of category theory and type systems. The reader who is familiar with these subjects might nevertheless want to at least skim the material in order to understand the notation and the new results mentioned above.

2.1 Complexity theory

For integer x ∈ ℕ we write |x| = ⌈log_{2}(x + 1)⌉ for the length of x in binary notation. For example, we have |0| = 0, |3| = 2, |2^{t}| = t+1. If x⃗ = (x_{1}, …, x_{n}) then |x⃗| = (|x_{1}|, …, |x_{n}|).

We have |x| + |y| − 1 ≤ |xy| ≤ |x| + |y| and max(|x|, |y|) ≤ |x + y| ≤ max(|x|, |y|) + 1. We write ||x|| for the length of the length of x, i.e., ||x|| = |a| when a = |x|. By “polynomial” we will always mean “polynomial with positive integer coefficients”. We sometimes use Knuth’s O-notation. If f : X ^{}ℕ is an ℕ-valued function

(typically X = ℕ^{k}) then O(f) is the set of all those functions g : X ^{}ℕ for which there exist constants c, d such that g(a) ≤ cf(a) + d. It is customary to write g = O(f) rather than g ∈ O(f) in this case.

We must fix some universal machine model allowing us to formalise runtimes and to encode algorithms as natural numbers. Since we are only interested in polynomial time we need not be very precise about this machine model. For the sake of definiteness we take ordinary one-tape Turing machines. In order to write down particular algorithms we use a simple imperative language on integer variables which in addition to the usual control structures has the following two statements: read x⃗ reads the input (presented e.g. as the content of the tape upon initialisation) and stores it in the variables x⃗. The statement write e outputs the value of expression e and terminates the whole program. All intermediate computations are carried out by way of evaluating expressions and assigning to integer variables. These statements occur each exactly once in an algorithm.

Let f, T : ℕ^{n}→ℕ be functions. We say that f has time complexity T if there exists a Turing machine A such that for each x⃗ = (x_{1}, …, x_{n}) the machine A started on input x⃗ (coded in binary with blanks as separators) terminates after not more than T (|x⃗|) steps with output f(x⃗) (also coded in binary).

Since we need at least some time to write the output the time complexity also bounds the size of the result:

|f(x⃗)| ≤ T (|x⃗|)

A function f : ℕ^{n}→ℕ is polynomial time computable (f ∈ PTIME) if f has time complexity p for some n-variate polynomial p.

Lemma 2.1.1 (Pairing function) There exist injections num : ℕ ^{}ℕ, ⟨−, −⟩ : ℕ × ℕ ^{}ℕ with disjoint images such that num(x), ⟨x, y⟩ as well as the functions .1 and .2, getnum, isnum, and ispair defined by

⟨x, y⟩.1 = x ⟨x, y⟩.2 = y z.1 = z.2 = 0, otherwise ispair(⟨x, y⟩) = 1 ispair(z) = 0, otherwise getnum(num(x)) = x getnum(z) = 0, otherwise isnum(num(x)) = 1 isnum(z) = 0

are computable in linear time and such that moreover we have

\begin{matrix}|⟨x, y⟩| ≤ |x| + |y| + 2||y|| + 3 \\ |num(x)| ≤ |x| + 1 \end{matrix}

Proof. Let F (x) be the function which writes out the binary representation of x using 00 for 0 and 01 for 1, i.e., formally, F (x) = ∑_{i=0}^{n} 4^{i}c_{i} when x = ∑_{i=0}^{n} 2^{i}c_{i}.

We now define ⟨x, y⟩ as x y 1 1 F (|y|) 0 where is juxtaposition of bit sequences, i.e., x y = x · 2^{|y|} + y. We define num(x) as x 1 = 2x + 1.

In order to decode z = ⟨x, y⟩ we strip off the least significant bit (which indicates that we have a pair), then continue reading the binary representation until we encounter the first two consecutive ones. What we’ve read so far is interpreted as the length of y. Reading this far further ahead gives us the value of y. The remaining bits correspond to x.

□

Assume some reasonable coding of Turing machines and configurations as natural numbers using the above pairing function. For Turing machine e and input x⃗ we let init(e, x⃗) denote the initial configuration of Turing machine e applied to input x⃗. For configuration c ∈ ℕ (which includes the contents of the tapes as well as the machine itself) we let step(c) denote the configuration obtained from c by performing a single computation step. We let term(c) = 0 if c is a terminated configuration and term(c) = 1 otherwise. We may assume that term(c) = 0 implies step(c) = c. Finally, we let out(c) be the output contained in a terminated configuration c. We may assume that term(c) = 1 implies out(c) = 0. It is intuitively clear that these basic functions are computable in linear time as they only involve case distinctions and simple manipulations of bitstrings; see [7] for a formal proof.

Suppose that f : ℕ^{n}→ℕ has time complexity T and that e is a program (Turing machine) for f. We write

F (i, x⃗) = step^{i}(init(e, x⃗))

for the configuration reached after i steps on input x⃗. We have

f(x⃗) = F (T (|x⃗|), x⃗)

and furthermore, if T is a polynomial then

∀i.|F (i, x⃗)| ≤ p(|x⃗|)

for some polynomial p ≥ T. “Morally”, we should have |F (i, x⃗)| ≤ T (|x⃗|) because the Turing machine cannot use more than T (|x⃗|) storage bits. However, since the program is part of the configuration and due to the slight overhead involved with the use of pairing functions the configuration may get slightly larger.

2.2 Bounded recursion

The above algebraisation of Turing machine computations allows one to characterise polynomial time computability in terms of a restricted pattern of recursion.

Definition 2.2.1 Let f : ℕ^{n}→ℕ and g_{i} : ℕ^{k}→ℕ (for i = 1 … n) be functions. We say that h : ℕ^{k}→ℕ is defined from f and g⃗ by composition if

h(x⃗) = f(g_{1}(x⃗), g_{2}(x⃗), …, g_{k}(x⃗))

Definition 2.2.2 Let g : ℕ^{n}→ℕ, h_{0}, h_{1} : ℕ^{n+2}→ℕ, k : ℕ^{n+1}→ℕ be functions. We say that f : ℕ^{n+1}→ℕ is defined from g, h_{0}, h_{1}, k by bounded recursion on notation if

f(0, y⃗) = g(y⃗)
f(2x, y⃗) = h_{0}(x, f(x, y⃗), y⃗), if x > 0
f(2x + 1, y⃗) = h_{1}(x, f(x, y⃗), y⃗)

and, moreover, f(x, y⃗) ≤ k(x, y⃗) for all x, y⃗.

Definition 2.2.3 (Cobham’s function algebra) The functions

S_{0}, S_{1} : ℕ→ℕ (successor functions)
π_{i}^{n} : ℕ^{n}→ℕ (projections)
# : ℕ^{2}→ℕ (smash)

are defined by

S_{0}(x) = 2x
S_{1}(x) = 2x + 1
π_{i}^{n}(x_{1}, …, x_{n}) = x_{i}
x#y = 2^{|x|·|y|}

The class F is the smallest class of functions containing the above basic functions and closed under composition and bounded recursion on notation.

Here is a definition of the function conc(x, y) = y x = y · 2^{|x|} + x:

conc(0, y) = y
conc(S_{0}(x), y) = S_{0}(conc(x, y))
conc(S_{1}(x), y) = S_{1}(conc(x, y))

Now, textrmconc(x, y) ≤ 2^{|x|} · 2^{|y|} + 2^{|x|} ≤ 2^{|x|}(2^{|y|} + 1) ≤ 2^{|x|+|y|+1} ≤ S_{0}(x#y). Hence, conc ∈ F.

More generally, notice that if |f(x)| ≤ |x|^{k} then f(x) ≤ x# … #x (k times) so that every polynomial time computable function is bounded by a function in F.

We remark that we can replace bounded recursion on notation by a scheme like in Def. 1.1.1:

f(0, y⃗) = g(y⃗)
f(x, y⃗) = h(x, f(⌊\frac{x}{2}⌋ , y⃗), y⃗), if x > 0
provided f(x, y⃗) ≤ k(x, y⃗)

if we add to the basic functions a conditional cond : ℕ^{3}→ℕ defined by

cond(0, y, z) = y
cond(x + 1, y, z) = z

and functions for quotient and remainder of division by two: ⌊\frac{x}{2}⌋, parity(x).

Furthermore, we can “algebraicise” bounded recursion on notation, i.e., get rid of the proof obligation f(x, y⃗) ≤ k(x, y⃗) by using the following scheme:

\begin{matrix}f(x, y⃗) = g(y⃗) \\ f(x, y⃗) = min(h(x, f(⌊\frac{x}{2}⌋ , y⃗), y⃗) , k(x, x⃗)), if x > 0 \end{matrix}

Theorem 2.2.4 (Cobham) A function f : ℕ^{n}→ℕ is polynomial time computable iff f ∈ F.

Proof. The direction F ⊆ PTIME goes by induction on the definition of f. The basic functions are clearly in PTIME and the class PTIME is closed under composition. It remains to verify closure of PTIME under the scheme of bounded recursion on notation.

Let bit(i, x) be the ith most significant bit of x, i.e. formally, bit(i, x) = ⌊\frac{x}{2^{|x|−i}} ⌋ mod 2. Suppose that g, h_{0}, h_{1}, k are polynomial time computable witnessed by polynomials

p, q_{0}, q_{1}, r. The following algorithm obviously computes the function f defined from g, h_{0}, h_{1}, k by bounded recursion on notation.

read x, y⃗

f := g(y⃗); x′ := 0

for i = 1 to |x| do begin if bit(i, x) = 0

then f := h_{0}(x′, f, y⃗); x′ := S_{0}(x′) else f := h_{1}(x′, f, y⃗); x′ := S_{1}(x′)

end write f

The result now follows using the invariant f = f(x′, y⃗) and x′ = ⌊x/2^{|x|−i}⌋.

It remains to estimate the running time. The loop is executed |x|-times and the runtime of each round can be bounded by

q(|x|, r(|x|, |y⃗|), |y⃗|) + l(|x|, r(|y⃗|), |y⃗|)

where q = max(q_{0}, q_{1}) and l is a linear term accounting for shuffling around intermediate results. Notice that we use in an essential way that all the intermediate results f are bounded by k(x, y⃗).

The proof of the converse direction PTIME ⊆ F is based on the representation of PTIME-functions using init, step, out. We first have to show that these functions are in F. Intuitively, this follows from the fact that they basically involve case distinctions and bit manipulations; for the (technical details) we refer to [7]. Next, recall that for every polynomial p there exists a function k built up from # and composition such that

|k(x⃗)| ≥ p(|x⃗|)

For example |x# … #x| ≥ |x|^{n}.

Therefore, if f : ℕ^{n}→ℕ is in PTIME by program e then we can find k ∈ F such that

\begin{matrix}f(x⃗) = out(F (|k(x⃗)|, x⃗)) \\ F (i, x⃗) ≤ k(x⃗) \end{matrix}

where

F (i, x⃗) = step^{i}(init(e, x⃗))

The result follows since G(z, x⃗) = F (|z|, x⃗) admits a definition by bounded recursion on notation from init, step, and k. □

We have thus obtained a recursion-theoretic characterisation of the polynomial time computation. The disadvantage of F is that resource bounds are built into the definition by way of the bounding function k and that functions of arbitrary polynomial growth rate are “hardwired” by way of the basic #-function. Indeed, if we add exponentiation as basic function to F then we obtain the Kalmar elementary functions. So, one could say that the scheme of bounded recursion on notation is not “intrinsically polynomial time” and does not really provide any insight into the nature of feasible computation.

2.3 Safe recursion

Let us now study Bellantoni and Cook’s system of safe recursion in some more detail. As said in the Introduction the main idea behind safe recursion is to forbid recursion on results of recursive calls.

This is achieved by dividing the variables of a function f into two zones separated by a semicolon: f(x⃗; y⃗). The x⃗-variables are called normal; the y⃗-variables are called safe. Both range over natural numbers as before. The intended invariant is that f recurses on its normal variables and applies only basic functions to its safe arguments.

Formally, one should think of such functions as triples (m, n, f) where m, n ∈ ℕ and f : ℕ^{m} × ℕ^{n}→ℕ.

In order that this invariant be maintained we must never substitute a term depending on a safe variable for a normal variable. Formally, this is done by restricting the scheme of composition

Definition 2.3.1 Let f(x⃗; y⃗), g_{i}(u⃗; ), and h_{j}(u⃗; v⃗) be functions of appropriate arity. We say that k(u⃗; v⃗) is defined from f, g⃗, h⃗ by safe composition if

k(u⃗; v⃗) = f(g⃗(u⃗; ); h⃗(u⃗; v⃗))

Now we can restrict recursion on notation in such a way that results of recursive calls may be used via safe variables only.

Definition 2.3.2 A function f(x, y⃗; z⃗) is defined from g(x⃗; y⃗) and h(x, y⃗; u, z⃗) by safe recursion on notation if

\begin{matrix}f(0, y⃗; z⃗) = g(y⃗; z⃗) \\ f(x, y⃗; z⃗) = h(x, y⃗; f(⌊\frac{x}{2}⌋ , y⃗; z⃗), z⃗), if x > 0 \end{matrix}

Definition 2.3.3 (Bellantoni-Cook) The class B is the least set of functions (or triples (m, n, f) for pedants) closed under safe composition and safe recursion on notation and containing the following basic functions:

– projections (of both safe and normal variables),

– the constant 0,

– the successor functions S_{0}(; y) = 2y, S_{1}(; y) = 2y + 1,

– division by two div(; y) = ⌊\frac{y}{2} ⌋,

– the parity function parity(; y) = y (mod 2),

– the conditional cond(; x, y, z) defined by

cond(; 0, y, z) = y
cond(; x + 1, y, z) = z

all in safe arguments as indicated.

Here is a definition of the function conc(x; y) = 2^{|x|} · y + x with x normal and y safe as indicated:

conc(0; y) = y

conc(x; y) = cond(; parity(; x), S_{0}(; conc(⌊\frac{x}{2}⌋; y)) S_{1}(; conc(⌊\frac{x}{2}⌋; y)))

We can now form sq(x; ) = conc(x; x), but the definition of exp

exp(0; ) = 1

exp(x; ) = sq(exp(⌊\frac{x}{2}⌋; ); )

is illegal because the result of the recursive call exp(⌊\frac{x}{2}⌋; ) is placed into a normal position rather than a safe one as prescribed. However, finite iterations of sq such as sq(sq(sq(x; ); ); ) are allowed by safe composition.

Since the second argument of conc is safe, we can use it to further process the result of a recursive call:

\begin{matrix}f(0; ) = \\ f(x, y; ) = conc(y; f(x, y; )) \end{matrix}

This function has a similar growth rate as the smash function #.

Similarly, we can define the smash function itself using an auxiliary function pad(x; y) = 2^{|x|} · y.

The following soundness theorem for B contains its proof in its statement.

Theorem 2.3.4 If f(x⃗; y⃗) is definable in B then f is in PTIME and, moreover,

|f(x⃗; y⃗)| ≤ p(|x⃗|) + max(|y⃗|)

for some polynomial p.

Proof. Direct induction over derivations. □

The converse also holds, i.e., all PTIME-functions admit a definition in B. We refer to [7] for the (surprisingly technical) proof. See, however, Section 3.2.3 where we prove this result for the system with higher result types.

2.4 Tiered recursion

Leivant and Marion [25, 27] have proposed another resource-free characterisation of polynomial time based on a countable number of copies of the natural numbers called tiers. A term of tier k may be substituted for a variable of tier l provided that k ≥ l.

In a recursion on notation we require that the (result) tier of the function to be defined be lower than the tier of the variable recursed on.

The restriction on recursions does not apply if f(x) is not actually used in the course of the computation of f(S_{0}(x)) or f(S_{1}(x)), in other words if recursion on notation is used merely for a generalised case distinction. The main result is that all polynomial time functions are definable from the functions 0, S_{0}(x), S_{1}(x), and projections available at all tiers.

The reader may wish to check that the definition of sq(x) = [x]^{2} by recursion on notation in the introduction is legal in tiered recursion when x is of tier 1 and sq(x) is of tier 0. It is also easy to see that the definition of exp(x) admits no annotation with tiers.

Leivant and Marion prove the main result directly using a machine model; we will give here an alternative proof by relating tiered recursion to safe recursion. The aim of this exercise is to pinpoint the difference between the two systems and in particular to falsify the somewhat common belief that the two levels safe and normal correspond exactly to the tiers. This (wrong) belief appears all the more plausible as Leivant and Marion actually show that two tiers suffice to define all polynomial time computable functions.

Let us temporarily use the letter L for Leivant and Marion’s system.

Proposition 2.4.1 If f(x⃗; y⃗; z⃗) is definable in L of tier k and with variables x⃗ of tier greater than k, with variables y⃗ of tier equal to k, and with variables z⃗ of tier lower than k, then f(x⃗; y⃗; z⃗) does not depend on the z⃗-variables and admits a definition in B with variables x⃗ normal and variables y⃗ safe. Moreover, this definition of f in B is obtained in a compositional way following the definition of f in L.

Proof. Direct by induction on the definition of f in L. Flat recursion can be simulated in B using conditional, division by two, and parity. □

The following version of the converse is also proved directly by induction.

Proposition 2.4.2 If f(x⃗; y⃗) is definable in B then for every tier k there exists a tier l ≥ k such that f(x⃗; y⃗) of tier k is definable in L of tier k with variables x⃗ of tier l and variables y⃗ of tier k.

We see that safe recursion can be viewed as a high-level language for L or a “tier-inference system”.

2.5 Higher-order functions and the typed lambda calculus

A higher-order function is a function which can take besides natural numbers also functions as arguments. A typical higher-order function is evaluation:

ev(f, x) = f(x)

Another one is iteration:

it(f, u) = f^{|u|}(0)

Iteration is definable schematically in B in the following sense.

If f(x⃗; y⃗, z) is definable then so is g(x⃗, u; y⃗) given by

\begin{matrix}g(x⃗, 0; y⃗) = 0 \\ g(x⃗, u; y⃗) = f(x⃗; y⃗, g(x⃗; ⌊\frac{u}{2}⌋ , y⃗)) \end{matrix}

The typed lambda calculus has been invented to facilitate the formulation of such schemata. It uses type expressions built up from N and → to describe the “arity” of a higher-order function.

A, B ::= N | A→B

Terms are built up from variables and basic functions by application and functional abstraction:

e ::= x  variable
| c  constant
| (e_{1} e_{2}) application
| λx: A.e abstraction

Application can also be written as e_{1}(e_{2}). Iterated application (e e_{1} e_{2} … e_{n}) (association to the left) can also be written as e(e_{2}, e_{3}, …, e_{n}) or e(e⃗). The variable x in an abstraction λx: A.e is bound. We identify terms up to renaming of bound variables, i.e., λx: A.xy is considered equal to λz: A.zy, but different from λx: A.xu.

If A⃗ = (A_{1}, …, A_{n}) then A⃗→B stands for A_{1}→A_{2}→ … →A_{n}→B. The type A^{n}→B is defined recursively by A^{0}→B = B and A^{n+1}→B = A→A^{n}→B (association to the right).

A type of the form N⃗→N is called a first-order type. All the other types are higherorder. A term of first order type all whose variables are of base type is called first-order. Note that by iterated abstraction a first-order term can be transformed into a closed term of first-order type.

We fix an assignment of types to constants. For example, we could have 0 : N and S_{0}, S_{1} : N→N to mean that 0 is an integer constant and S_{0}, S_{1} are unary functions on integers.

Well-formed terms are assigned types relative to a typing of the variables: a context. Such a context is a set Γ = {x_{1}: A_{n}, …, x_{n}: A_{n}} of bindings x: A such that the x_{i} are pairwise distinct. We write Γ, x: A for Γ ∪ {x: A} if x is not mentioned in Γ. We write dom(Γ) for the set of variables bound in Γ and Γ(x) = τ if x: τ ∈ Γ.

The typing judgement Γ ⊢ e : A read term e is well-typed of type A in context Γ is then inductively defined by the following rules.

\frac{x: A ∈ Γ}{Γ ⊢ x : A}  (T-Var)

\frac{c: A}{Γ ⊢ c : A}  (T-Const)

\frac{Γ ⊢ e_{1} : A→B  Γ ⊢ e_{2} : A}{Γ ⊢ e_{1} e_{2} : B}  (T-Arr-E)

\frac{Γ, x: A ⊢ e : B}{Γ ⊢ λx: A.e : A→B}  (T-Arr-I)

The following two rules are easily seen to be admissible:

\frac{Γ ⊢ e : A Γ, Δ well-formed}{Γ, Δ ⊢ e : A}  (T-Weak)

\frac{Γ, x: A ⊢ e : B  Γ ⊢ e′ : A}{Γ ⊢ e[e′/x] : B}  (T-Subst)

In the latter rule e[e′/x] denotes the capture-free substitution of e′ for x in e. It is obtained by first renaming all bound variables in e so as to become different from free variables in e′ and then literally replacing every occurrence of x in e by e′.

If e is closed, i.e., contains no free variables then we write e : A instead of Γ ⊢ e : A or ∅ ⊢ e : A. The typing rules also make sense if instead of just N we have several basic types. They can also be generalised to encompass other type formers such as cartesian products, lists, etc. We shall encounter such later on.

2.5.1 Set-theoretic interpretation

The typed lambda calculus has an intended set-theoretic interpretation: To each type A we assign a set [[A]] by

\begin{matrix}[[N]] = ℕ \\ [[A→B]] = [[A]]→[[B]] \end{matrix}

where [[A]]→[[B]] is the set of all functions from [[A]] to [[B]]. An environment η for a context Γ is an assignment η(x) ∈ [[A]] for each binding x: A in Γ. If Γ ⊢ e : A and η is an environment for Γ then the meaning [[e]]η is given by

[[x]]η = η(x)
[[e_{1} e_{2}]]η = [[e_{1}]]η([[e_{2}]]η)
[[λx: A.e]]η(v) = [[e]]η[x ↦ v]

For example, the meaning of the term λf: (N→N)→N.f(λx: N.x) is the function which takes a function f : (ℕ→ℕ)→ℕ and applies it to the identity function.

2.5.2 The system PV^{ω}

The system PV^{ω} [9] is a higher-order extension of Cobham’s function algebra F. It is defined as the typed lambda calculus over base type N and the following constants:

i. The constant zero: 0 : o.

ii. The two successor functions: s_{0}, s_{1} : o→o.

iii. Integer division by two (“mixfix notation”): ⌊\frac{x}{2}⌋.

iv. The (infix) functions chop, pad, and smash : − . , ⊞, # : o→o→o. v. The ternary conditional Cond : o→o→o→o.

vi. The bounded recursor rec : o→(o→o→o)→(o→o)→o→o

The set-theoretic meaning of these functions is given as follows.

[[0]] = 0, [[s_{0}]](x) = 2x, [[s_{1}]](x) = 2x + 1, [[_{⌊\overline{2}⌋}]](x) = ⌊x/2⌋,
[[#]](x)(y) = 2^{|x|·|y|}, [[⊞]](x)(y) = x · 2^{|y|}, [[− . ]](x)(y) = ⌊x/2^{|y|}⌋,

[[Cond]](x)(y)(z) = \{ \begin{matrix}y, if x = 0 \\ z, otherwise \end{matrix}

Finally, if g ∈ [[N]], h ∈ [[N→N→N]], and k ∈ [[N→N]] then f(x) = [[rec]](g, h, k, x) is the function defined by

\begin{matrix}f(0) = min(k(0), g) \\ f(x) = min(k(x), h(x, f(⌊\frac{x}{2}⌋))) \end{matrix}

It is easy to see by induction that whenever a function f(x_{1}, …, x_{n}) can be defined in F then there exists a term e : N^{n}→N such that f(x⃗) = [[e]](x⃗).

The advantage of using PV^{ω} as opposed to plain F is that it allows for a direct definition of higher-order functions.

For example, we can define the functional ∃ : (N→N)→N which maps f : N→N and x to 0 iff f(i) = 0 for some i ≤ |x|.

∃ = λf: N→N.λx: N.rec(f(0), λx: N.λe: N.Cond(e, 0, f(|x|)))

The main result about PV^{ω} is that its first-order section is contained in PTIME:

Theorem 2.5.1 If e : N^{n}→N in PV^{ω} then [[e]] : ℕ^{n}→ℕ is a PTIME-function.

This result is due to Cook and Urquhart [9] who introduced PV^{ω} in order to define a functional interpretation for Buss’ systems of Bounded Arithmetic—subsystems of Peano arithmetic in which all provably total functions are PTIME [4].

Their proof uses a translation of normal forms of PV^{ω} terms back into F. We will later on describe a semantic proof which does not make use of normalisation. In doing so we illustrate one of the central concepts used in the soundness proof which forms the heart of this thesis.

2.6 Background on categories

The key technique in this thesis is to establish properties the set-theoretic interpretation of some typed lambda calculus by relating it to another “non-standard” interpretation. Extensive use will be made of categories both to define the appropriate notion of model and to construct the particular models needed to establish the desired results. In order to make this thesis self-contained we review the required category theory from scratch starting with the most basic definitions. The reader with prior knowledge might skim the first few sections taking a closer look only from Section 2.6.4 onwards.

2.6.1 Categories

A category ℂ is given by a collection |ℂ| of objects and for any two objects A, B a set ℂ(A, B) of morphisms. We may write ℂ for |ℂ| and f : A ^{}B for f : ℂ(A, B). For each object A there is an identity morphism id_{A} : A ^{}A. We may also use the notations id or A for id_{A}. For any three objects A, B, C there is a composition comp_{A,B,C} : ℂ(B, C) × ℂ(A, B) ^{}ℂ(A, C). If f : B ^{}C and g : A ^{}B then we write f ∘ g for comp_{A,B,C}(f, g). Composition and identities are related by the following equations:

\begin{matrix}f ∘ id = f = id ∘ f \\ f ∘(g ∘ h) = (f ∘ g) ∘ h \end{matrix}

A morphism f : A ^{}B is an isomorphism if there exists g : B ^{}A such that g ∘ f = id and f ∘ g = id. Two objects A, B are isomorphic, written A ≅ B if there exists an isomorphism f : A ^{}B.

A morphism f : A ^{}B is a monomorphism if f ∘ u = f ∘ v implies u = v for all u, v : X ^{}A.

A category ℂ is called small if its collection of objects |C| forms a set.

2.6.1.1 Examples

The category Sets has the collection of sets as objects; a morphism from set X to Y is a function from X to Y.

A partially ordered set P can be viewed as a category with the elements of P as objects and P (x, y) = {⋆} if x ≤ y and P (x, y) = ∅, otherwise.

A typed lambda calculus gives rise to a category 𝕊 with its typing contexts as objects in the following way. A substitution from context Γ to context Δ is a function σ assigning to each x ∈ dom(Δ) a term σ(x) such that Γ ⊢ σ(x) : Δ(x). The application e[σ] of a substitution σ to a term e with free variables among the domain of σ is defined as the homomorphic extension of x[σ] = σ(x) with the understanding that the bound variables in e are chosen different from the free variables in σ.

We have the derived rule

\frac{Δ ⊢ e : A σ : Γ ^{}Δ}{Γ ^{}e[σ] : A}  (T-Subst’)

The identity substitution is defined by id_{Γ}(x) = x for x ∈ dom(Γ). If σ : Γ ^{}Δ and τ : Δ ^{}Θ then τ ∘ σ : Γ ^{}Θ is defined by (τ ∘ σ)(x) = τ(x)[σ]. Now, x[τ ∘ σ] = τ(x)[σ] = x[τ][σ] by definition, hence e[τ ∘ σ] = e[τ][σ] for all terms e by homomorphic extension. Similarly, e[id] = e for all e. From this, it follows that contexts and substitutions form indeed a category and in particular, that composition of substitutions is associative.

We emphasise that associativity holds with respect to definitional equality of terms, i.e., up to renaming of bound variables, and that no further equations such as (λx: A.e)e′ = e[e′/x] are needed. We also notice that the set of terms of type A in context Γ is in 1-1 correspondence with substitutions from Γ to the context {x: A} where x is an arbitrary variable.

The category ℙ of PTIME-functions is defined as follows. An object of ℙ is a nonnegative integer (thought of as an arity); a morphism from m to n is a PTIME-function from ℕ^{m} to ℕ^{n}. Composition in ℙ is ordinary composition of functions.

All these categories except Sets are small.

2.6.2 Terminal object and cartesian products

An object ⊤ in a category ℂ is terminal if for every X ∈ ℂ there is a unique morphism ⟨⟩_{X} : X ^{}⊤ called the terminal projection. Any two terminal objects are isomorphic and so one often speaks of the terminal object. The one element set {⟨⟩} is a terminal object in Sets. The empty context is the terminal object in the category of substitutions. Finally, 0 is a terminal object in the category ℙ of PTIME-functions.

A cartesian product of two objects A, B ∈ ℂ is an object A × B and morphisms π : A × B ^{}A and π′ : A × B ^{}B such that for any pair of morphisms f : C ^{}A and g : C ^{}B there is a unique morphism ⟨f, g⟩ : C ^{}A × B satisfying π ∘⟨f, g⟩ = f and π′ ∘⟨f, g⟩ = g. More generally, if I is a set and (A_{i})_{i∈I} is an I-indexed family of objects of ℂ then a cartesian product of the family is given by a object ∏_{i∈I} A_{i} and a family of morphisms π_{i} : ∏_{i} A_{i} ^{}A_{i} such that for every family f_{i} : C ^{}A_{i} there is a unique map ⟨f_{i}|i∈ I⟩ : C ^{}∏_{i∈I} A_{i} such that π_{i} ∘⟨f_{i}|i∈ I⟩ = f_{i}.

Notice that uniqueness of ⟨f_{i}|i∈ I⟩ implies the following equations:

\begin{matrix}⟨f_{i}|i∈ I⟩ ∘ h = ⟨f_{i} ∘ h|i∈ I⟩ \\ ⟨π_{i}|i∈ I⟩ = id \end{matrix}

which in turn imply uniqueness.

Like a terminal object, a cartesian product of two objects is unique up to isomorphism so that existence of cartesian products is a property of a category rather than extra structure.

In Sets cartesian products always exist and are given as sets of pairs in the case of binary cartesian products and as the set of choice functions in the more general case. In the category of substitutions, binary products are given by Γ × Δ = Γ ∪ Δ′ where Δ′ is obtained from Δ by renaming the variables in dom(Δ) so as to become different from those in dom(Γ). In ℙ binary cartesian products are given by addition of natural numbers.

The categories 𝕊 and ℙ also have indexed cartesian products for finite index set. In general, a category which has binary cartesian products also has cartesian products of finite families.

2.6.3 Functors and natural transformations

A functor F : ℂ ^{}𝔻 from category ℂ to category 𝔻 is given by a function F : |ℂ| ^{}|𝔻| and for any two objects A, B a function

F : ℂ(A, B) ^{}𝔻(F (A), F (B))

such that the following equations called functor laws or functoriality are satisfied whenever they make sense.

\begin{matrix}F (id) = id \\ F (f ∘ g) = F (f) ∘ F (g) \end{matrix}

Functors can be composed in the obvious way and for every category we have an identity functor.

2.6.3.1 Opposite and product category

The opposite category ℂ^{op} of a category ℂ is given by |ℂ^{op}| = |ℂ| and ℂ^{op}(X, Y) = ℂ(Y, X). The product category ℂ×𝔻 is given by |ℂ×𝔻| = |ℂ|×|𝔻| and (ℂ×𝔻)((X_{0}, X_{1}), (Y_{0}, Y_{1})) = ℂ(X_{0}, Y_{0}) × 𝔻(X_{1}, Y_{1}).

A functor F : ℂ^{op} ^{}𝔻 is called a contravariant functor from ℂ to 𝔻. More explicitly, its morphism part sends a morphism f ∈ ℂ(X, Y) to a morphism F (f) ∈ 𝔻(F (Y), F (X)). A functor F : ℂ×ℂ ^{}𝔻 is called a bifunctor from ℂ to 𝔻. The purpose of opposite and product category is to subsume ordinary, contravariant, and bifunctors under one single notion.

2.6.3.2 Natural transformation

Let F, G : ℂ ^{}𝔻 be functors from category ℂ to category 𝔻. A natural transformation α from F to G written α : F ^{}G is a |ℂ|-indexed family of morphisms α_{X} ∈ 𝔻(F X, GX) such that the following equation called naturality is satisfied for all u ∈ ℂ(X, Y).

α_{Y} ∘ F u = Gu ∘ α_{X}

For every functor F : ℂ ^{}𝔻 there is an identity natural transformation id : F ^{}F given by id_{X} = id_{F X} and two natural transformations α : F ^{}G and β : G ^{}H can be composed by (β ∘ α)_{X} = β_{X} ∘ α_{X}. If the category ℂ is small the natural transformations from ℂ to 𝔻 form a set so that we obtain a category 𝔻^{ℂ}: the category of functors from ℂ to 𝔻 with natural transformations as morphisms.

A natural isomorphism is a natural transformation α : F ^{}G such that every morphism α_{X} is an isomorphism. It is easy to see that in this case the family of inverses (α_{X})^{−1} forms itself a natural transformation from G to F so that a natural isomorphism can equivalently be defined as an isomorphism in a functor category.

A functor F is full, resp. faithful if for each X, Y the function F : ℂ(X, Y)→𝔻(F X, F Y) is surjective, resp. injective.

2.6.3.3 Examples

The functor Γ : ℙ ^{}Sets defined by Γ(n) = ℕ^{n} and Γ(u) = u is faithful, but not full.

The set-theoretic interpretation of a typed lambda calculus defines a functor I : 𝕊 ^{}Sets where I(Γ) is the set of environments for Γ and for substitution σ : Γ ^{}Δ we have

I(σ)(η)(x) = [[σ(x)]]η

The functor I is neither full nor faithful.

2.6.4 Canonical isomorphisms and object equality

This functor preserves cartesian products and terminal objects in the sense that I(Γ × Δ) and I(Γ) × I(Δ) are isomorphic sets. An element of the former is a function defined on the disjoint union of dom(Γ) and dom(Δ); an element of the latter is a pair of a function on dom(Γ) and a function on dom(Δ). Furthermore, the isomorphism between the two is canonical in the sense that the following two diagrams, where i denotes the isomorphism, commute whenever they make sense.

[figure: I(Γ); I(π); I(Γ × Δ); I(π′); I(Δ); I(Γ) × I(Δ); i; π′; π]

It greatly simplifies notation and reasoning if one treats such a canonical isomorphism as an identity, i.e., assumes that I(Γ × Δ) and I(Γ) × I(Δ) are equal. A technical development based on this simplification should be understood as an abbreviation for a more detailed development in which the isomorphisms are filled in. This filling in of isomorphisms is possible whenever the assumed equality is used in a “reasonable way”, which intuitively means that identity of objects is used only insofar as to make certain morphisms composable.

In the particular example at hand canonicity of the isomorphism means that when making the simplifying identification between I(Γ × Δ) and I(Γ) × I(Δ) we can at the same time identify I(π) and π as well as I(π′) and π′, which now formally have equal source and target.

We reiterate the point that treating an isomorphism as an identity does not mean that the to isomorphic objects get actually identified, but that canonical isomorphisms do not show up in the notation.

There are ways by which this convention and its soundness can be formalised and established, for details see [23, 22].

2.6.5 Subcategories

A category ℂ is a subcategory of 𝔻 if |ℂ| ⊆ |𝔻| and ℂ(X, Y) ⊆ 𝔻(X, Y) and composition and identities are the same in both categories. The category ℂ is a full subcategory if ℂ(X, Y) = 𝔻(X, Y). If F : ℂ ^{}𝔻 is a faithful functor then we can identify ℂ with the subcategory of 𝔻 consisting of the objects of the form F X and of morphisms of the form F f. If F is full and faithful then the thus obtained subcategory is full. Again, such identification should be understood as a means for simplification of notation and reasoning not necessarily as an actual identification.

2.6.6 Global elements

Let ℂ be a category with terminal object. A global element of object X is a morphism u : ⊤ ^{}X. We also use the notation u : X to mean that u is a global element of X. The set of global elements of X is denoted G(X). This assignment extends to a functor G : ℂ ^{}Sets by G(f)(u) = f ∘ u.

We may use the notation f(u) instead of G(f)(u) or f ∘ u if f : A ^{}B and u : A thus emphasising the view of morphisms as functions on global elements.

A category is called well-pointed if this functor is faithful, i.e., a morphism is uniquely determined by its functional action on global elements. The category Sets is obviously well-pointed and so are categories of algebraic structures. Syntactical categories like 𝕊 are in general not well-pointed and neither are most functor categories.

If a category ℂ has terminal objects and cartesian products then G(⊤) ≅ {⟨⟩} and G(A × B) ≅ G(A) × G(B) and these isomorphisms are again canonical allowing us to view them as identities. So, a global element of a cartesian product can be seen as a pair of global elements.

2.6.7 Function spaces

2.6.7.1 Cartesian product as a functor

Let ℂ be a category and A ∈ ℂ be such that for every X ∈ ℂ the product X × A exists. In this case X ↦ X × A defines a functor − × A from ℂ to ℂ with morphism part given by f × A = ⟨f ∘ π, π′⟩. Similarly, we have a functor A × − : ℂ→ℂ. If all binary cartesian products exists then × defines a bifunctor on ℂ given on objects by (A, B) ↦ A × B and on morphisms by (f, g) ↦ ⟨f ∘ π, g ∘ π′⟩.

Now let again A be such that all products X × A exist. If B is another object then the function space or exponential of B by A is given by an object B^{A} (also written A→B), a morphism ev : B^{A} × A ^{}B—the evaluation map—and for every morphism f : X ×A ^{}B a unique morphism curry(f) : X ^{}B^{A} called the currying or exponential transpose of f such that ev ∘(curry(f) × A) = f.

Instead of uniqueness we can also postulate the following additional equations which in turn are consequences of uniqueness:

curry(f) ∘ h = curry(f ∘(h × A)) curry(ev) = id

Currying and evaluation establish a natural bijection

ℂ(X × A, B) ≅ ℂ(X, B^{A})

2.6.7.2 Examples

In Sets the function space B^{A} is the set of a all functions from A to B.

If we factor the terms of a typed lambda calculus by βη-equality, i.e. the congruence generated by the following two equations

(λx: A.e)e′ = e[e′/x]
(λx: A.ex) = e, if x is not free in e

then a context f : A→B is the exponential ({y: B})^{{x:A}} in 𝕊 with evaluation map given by ev(y) = fx and currying given by abstraction.

A category is called cartesian closed if it has a terminal object, all binary cartesian products and all function spaces. The category Sets is cartesian closed and so is 𝕊 but only in a slightly contrived way.

2.6.7.3 Global elements and function spaces

Applying G to the evaluation map ev : B^{A} × A ^{}B gives rise to a function

G(ev) : G(B^{A}) × G(A) ^{}G(B)

We will abbreviate G(ev)(u, a) by u(a) thus viewing a global element of a function space as an actual function.

We also note that in view of ℂ(⊤, A→B) ≅ ℂ(⊤ × A, B) ≅ ℂ(A, B) global elements of function space A→B are in 1-1 correspondence with morphisms from A to B.

2.6.8 Interpretation of the typed lambda calculus

Let ℂ be a category with terminal object and a distinguished subcollection T ⊆ ℂ of objects called type objects such that all cartesian products of the form X × A for A ∈ T and all exponentials of the form B^{A} for A, B ∈ T exist. Then all products of finite families of type objects exist as well via the encoding

∏_{i∈{i_{1},...,i_{n}}} A_{i} = (… (⊤ × A_{i_{1}}) × …) × A_{i_{n}}

Notice here that we always have ⊤ × X ≅ X.

Now suppose that we are given a typed lambda calculus with some base types and constants with associated types. For every base type B we assume a type object [[B]]. This extends to an assignment of type objects to all types by [[A_{1}→A_{2}]] = [[A_{1}]]→[[A_{2}]]. Furthermore, for every constant c of type A we assume a global element [[c]] : [[A]].

A context Γ now gets interpreted as the dom(Γ)-indexed cartesian product of the family ([[Γ(x)]])_{x∈dom(Γ)}. A term Γ ⊢ e : A gets interpreted as a morphism [[Γ ⊢ e : A]] : [[Γ]] ^{}[[A]]. The definition of this interpretation is by induction on typing derivations; the defining clauses are as follows.

[[Γ ⊢ x : Γ(x)]]  = π_{x}
[[Γ ⊢ c : A]]  = [[c]] ∘⟨⟩_{[[Γ]]}
[[Γ ⊢ e_{1}e_{2} : B]]  = ev ∘⟨[[Γ ⊢ e_{1} : A→B]], [[Γ ⊢ e_{2} : A]]⟩
[[Γ ⊢ λx: A.e : A→B]] = curry([[Γ, x: A ⊢ e : B]])

In the last clause we have identified the dom(Γ) ∪ {x}-indexed product [[Γ, x: A]] with the binary product [[Γ]] × [[A]]. This is legitimate as these are canonically isomorphic in the sense of Section 2.6.4. If we wanted to avoid a treatment of canonical isomorphism as identity we would have to rewrite the clause into something like

[[Γ ⊢ λx: A.e : A→B]] = curry(i ∘[[Γ, x: A ⊢ e : B]])

where i : [[Γ, x: A]] ^{}[[Γ]] × [[A]] is the isomorphism defined by

i = ⟨⟨π_{y}|y∈ dom(Γ)⟩, π_{x}⟩

One can now show that this interpretation is sound for βη-equality in the sense that if Γ ⊢ e_{1} : A and Γ ⊢ e_{2} : A are βη-equal then [[Γ ⊢ e_{1} : A]] = [[Γ ⊢ e_{2} : A]].

Example. The set-theoretic interpretation of the typed lambda calculus forms an instance of this generic interpretation. If we use the category-theoretic framework then the interpretation of a term will be a function from [[Γ]] to [[A]]. However, [[Γ]] is isomorphic to the set of environments on Γ so the “typing” agrees with the previously defined set-theoretic interpretation.

2.6.8.1 Global elements and category-theoretic semantics

The identification between G(A × B) and G(A) × G(B) generalises to indexed cartesian products thus allowing us to view a global element of [[Γ]] as a function η defined on dom(Γ) and such that η(x) : [[Γ(x)]].

If Γ ⊢ e : A then

G([[Γ ⊢ e : A]]) : G([[Γ]]) ^{}G([[A]])

is a function sending such environments to global elements of A.

If the category ℂ is well-pointed then [[Γ ⊢ e : A]] is uniquely determined by the function G([[Γ ⊢ e : A]]) which maps environments (elements of G([[Γ]]) to values in G([[A]]).

The difference to set-theoretic interpretation is that not all functions are ℂ-morphisms, thus, by appropriately choosing the category ℂ one can maintain additional invariants.

2.6.9 Logical relations

Logical relations form a tool with which to relate two different interpretations of a typed lambda calculus typically with the aim of using one interpretation in order to prove something about the other one.

The following definition is not the most general one possible, but sufficient for our purposes. Let ℂ, 𝔻 be two categories with enough structure to interpret a typed lambda calculus. We denote the respective interpretations by [[−]]^{ℂ} and [[−]]^{𝔻}.

In the following definition we make use of the simplifying notation for application of global elements of function spaces introduced in Section 2.6.7.3

Definition 2.6.1 (Statman, Plotkin) A type-indexed family of relations

R_{A} ⊆ G([[A]]^{ℂ}) × G([[A]]^{𝔻})

is called a logical relation, if for all types A and B and u : [[A→B]]^{ℂ} and v : [[A→B]]^{𝔻} we have

uR_{A→B}v ⟺ ∀a: [[A]]^{ℂ}.∀b: [[B]]^{𝔻}.aR_{A}b ⇒ u(a)R_{B}v(b)

So, a logical relation is determined by its restriction to base types. Let R be a logical relation.

For environments η: [[Γ]]^{ℂ} and ρ: [[Γ]]^{𝔻} we define

ηR_{Γ}ρ ⟺ ∀x∈ dom(Γ).η(x)R_{[[Γ]](x)}ρ(x)

Theorem 2.6.2 Suppose that [[c]]^{ℂ}R_{A}[[c]]^{𝔻} for every constant c : A. Then for every term Γ ⊢ e : A and environments η ∈ G([[Γ]]^{ℂ}) and ρ ∈ G([[Γ]]^{𝔻}), we have

ηR_{Γ}ρ ⟹ [[Γ ⊢ e : A]]^{ℂ}(η) R_{A} [[Γ ⊢ e : A]]^{𝔻}(ρ)

Proof. By induction on the derivation of Γ ⊢ e : A. The case T-Const follows from the assumption; if e = x and Γ ⊢ e : A by T-Var then [[Γ ⊢ x : A]](η) = η(x) and again the conclusion is part of the assumption.

Now suppose that e = e_{1}e_{2} where Γ ⊢ e_{1} : A→B and Γ ⊢ e_{2} : A. If ηR_{Γ}ρ then, inductively,

[[Γ ⊢ e_{1}]]^{ℂ}(η)R_{A→B}[[Γ ⊢ e_{1}]]^{𝔻}(ρ)

and

[[Γ ⊢ e_{1}]]^{ℂ}(η)R_{A}[[Γ ⊢ e_{2}]]^{𝔻}(ρ)

Hence

[[Γ ⊢ e_{1}e_{2}]]^{ℂ}(η)R_{B}[[Γ ⊢ e_{1}e_{2}]]^{𝔻}(ρ)

by definition of R_{A→B}.

Finally, suppose that e = λx: A.e_{1} where Γ, x: A ⊢ e_{1} : B. In order to show that

[[Γ ⊢ e : A]]^{ℂ}(η)R_{A→B}[[Γ ⊢ e : A]]^{𝔻}(ρ)

we assume u : [[A]]^{ℂ} and v : [[A]]^{𝔻} with uR_{A}v. Pairing these with η and ρ, respectively, yields environments ⟨η, u⟩ : [[Γ, x: A]]^{ℂ} and ⟨ρ, v⟩ : [[Γ, x: A]]^{𝔻}. The induction hypothesis then yields

[[Γ, x: A ⊢ e : B]]^{ℂ}(⟨η, u⟩)R_{B}[[Γ, x: A ⊢ e : B]]^{ℂ}(⟨ρ, v⟩)

But

[[Γ ⊢ λx: A.e : A→B]]^{ℂ}(η)(u) = [[Γ, x: A ⊢ e : B]]^{ℂ}(⟨η, u⟩)

and similarly for 𝔻 by the defining equation for function spaces, hence the result. □

2.6.10 Presheaves

A presheaf over a category ℂ is a functor from ℂ^{op} to Sets. More elementarily, this means that F consists of a |ℂ|-indexed family of sets (F X)_{X∈ℂ} and for every morphism f : X ^{}Y a “reindexing function” F f : F Y ^{}F X such that

\begin{matrix}F (id_{X})(x) = x \\ F (f ∘ g)(x) = F (g)(F (f)(x)) \end{matrix}

We will usually write F_{X} for F X and F_{f}(x) for F (f)(x). If the presheaf under consideration is clear from the context we may also write x[f] for F_{f}(x). We then have x[id] = x and x[f][g] = x[f ∘ g]. The attention of the reader is drawn to the formal similarity of the morphism part of a presheaf with substitution.

Notice that a natural transformation from F to G is given by a |ℂ|-indexed family of functions α_{X} : F_{X} ^{}G_{X} such that for every u ∈ ℂ(X, Y) and f ∈ F_{Y} we have

(α_{Y} (f))[u] = α_{X}(f[u])

Under the abovementioned analogy with explicit substitution this equation corresponds to compatibility of a term former (here α) with substitution.

If ℂ is a small category then the presheaves form a category which we denote by ℂ̂ rather than Sets^{ℂ^{op}}.

For every object X ∈ ℂ we have the representable presheaf Y(X) defined by Y(X)_{Y} = ℂ(Y, X) and Y(X)_{f}(u) = u ∘ f. This extends to a functor Y : ℂ ^{}ℂ̂ by Y(t)_{Z}(u) = t ∘ u whenever t : X ^{}Y and u ∈ Y(X)_{Z} = ℂ(Z, X).

This functor is called Yoneda embedding. The Yoneda Lemma states that this functor is full and faithful. Indeed, we can recover t from Y(t) by t = Y(t)_{X}(id_{X}) giving faithfulness, and if α : Y(X) ^{}Y(Y) is a natural transformation then α = Y(t) for t =_{def} α_{X}(id_{X}) hence fullness.

It is convenient to identify a category ℂ with a subcategory of ℂ̂ thus writing X instead of Y(X).

2.6.10.1 Examples

In the category 𝕊̂ we have for every type A the presheaf Tm(A) given by

\begin{matrix}Tm(A)_{Γ} = \{e | Γ ⊢ e : A\} \\ Tm(A)_{σ}(e) = e[σ] \end{matrix}

This presheaf is isomorphic to the representable presheaf Y({x: A}).

In ℙ̂ we have the presheaf N of PTIME-functions given by

N_{n} = {f | f : ℕ^{n} ^{}ℕ and f ∈ PTIME}
N_{u}(f) = f ∘ u

This presheaf is isomorphic to the representable presheaf Y(1).

Thus, by the Yoneda Lemma the ℙ̂-morphisms from N to N are in 1-1 correspondence with the PTIME-functions.

Lemma 2.6.3 If F is a presheaf then the set of morphisms ℂ̂(Y(X), F) is naturally isomorphic to the set F_{X}, that is F is isomorphic to the presheaf ℂ̂(Y(−), F).

Proof. If m : Y(X) ^{}F then m̂ =_{def} m_{X}(id_{X}) ∈ F_{X}. Conversely, if f ∈ F_{X} then we define f̌ : Y(X) ^{}F by f̌_{Y} (u) = f[u]. We have ˇ m̂_{Y} (u) = m_{X}(id_{X})[u] = m_{Y} (Y(X)_{u}(id_{X})) = m_{Y} (id_{X} ∘ u) = m_{Y} (u). Conversely, ˆ f̌ = f[id_{X}] = f. For naturality, we have to check that for v : X′ ^{}X we have ˇ (f[v])_{X′}(u) = f̌_{X}(v ∘ u) which is direct from the definitions. □

2.6.10.2 Cartesian closure of ℂ̂

Every presheaf category ℂ̂ is cartesian closed. The terminal object is given by the constant presheaf defined by ⊤_{X} = {⟨⟩}, ⟨⟩[u] = ⟨⟩. If ℂ has a terminal object then Y(⊤) ≅ ⊤ and it is convenient to treat this isomorphism as an identity.

Cartesian product F × G is given pointwise by (F × G)_{X} = F_{X} × G_{X} and ((F × G)_{u}(f, g) = (F_{u}(f), G_{u}(g).

If ℂ has cartesian products then Y(X × Y) ≅ Y(X) × Y(Y) so that when viewing ℂ as a subcategory of ℂ̂ we do not need to make a distinction as to whether we form cartesian products in ℂ or in ℂ̂.

More generally, ℂ̂ has indexed products as follows. If (F_{i})_{i∈I} is a family of presheaves then the product ∏_{i∈I} F_{i} ∈ ℂ̂ is defined pointwise by

(∏_{i∈I} F_{i})_{X} = ∏_{i∈I}(F_{i})_{X}

and analogously on morphisms.

If f_{i} : C ^{}F_{i} then

⟨f_{i}|i∈ I⟩_{X} = ⟨(f_{i})_{X}|i∈ I⟩

and

(π_{i})_{X} = π_{i}

where the notation on the right-hand side refers to set-theoretic indexed cartesian products.

For the function space we start from the observation that if ℂ̂ has function spaces then we must necessarily have

(F →G)_{X}
≅ ℂ̂(Y(X), F →G)  by Lemma 2.6.3
≅ ℂ̂(Y(X) × F, G)

which suggests to define (F →G)_{X} as the set of natural transformations from Y(X) × F to G. More elementarily, an element φ of (F →G)_{X} assigns to each Y ∈ ℂ, v : Y ^{}X and f ∈ F_{Y} and element φ(v, f) ∈ G_{Y} in such a way that if u : Z ^{}Y then

φ(v, f)[u] = φ(v ∘ u, f[u])

If w : X′ ^{}X we define φ[w] ∈ (F →G)′_{X} by φ[w](v, f) = φ(w ∘ v, f).

The evaluation map is given by

ev_{X}(φ, f) = φ(id_{X}, f)

If h : L × F ^{}G then curry(h) : L ^{}F →G is defined by

curry(h)_{X}(l)(u, f) = h_{Y} (l[u], f)

when l ∈ L_{X}, u : Y ^{}X, f ∈ F_{Y}. The straightforward verifications are left to the reader.

We draw the attention of the reader to the similarity of function space in functor categories and implication in Kripke semantics. Indeed, Kripke semantics of intuitionistic propositional calculus can be seen as interpretation in the presheaf category 𝕎̂ where 𝕎 is the poset of worlds viewed as a category. Every formula φ gets assigned a presheaf [[φ]] which has the property that [[φ]]_{w} has at most one element. The defining clauses are [[φ ∧ ψ]] = [[φ]] × [[ψ]] and [[φ ⊃ ψ]] = [[φ]]→[[ψ]]. One may write w ⊩ φ if [[φ]]_{w} ≠ ∅ and then recovers the usual rules for interpretation in Kripke models.

The following characterisation of function spaces involving representable presheaves is crucial for the application of presheaves to lambda calculi with recursion operators.

Proposition 2.6.4 Let U ∈ ℂ and assume that ℂ has cartesian products of the form − × U. Then for any presheaf F ∈ ℂ̂ the function space U→F (= Y(U)→F) is isomorphic to the presheaf F_{−×U} defined by (F_{−×U})_{X} = F_{X×U} and (F_{−×U})_{u} = F_{u×U}.

Proof.

(U→F)_{X}
≅ ℂ̂(X, U→F)  Lemma 2.6.3
≅ ℂ̂(X × U, F)  Property of function space
≅ F_{X×U}  Lemma 2.6.3
□

We can now define evaluation and currying directly on F_{−×U} thus allowing us to treat the above isomorphism as an identity. Evaluation ev : F_{−×U} × U ^{}F is given by

ev_{X}(f, u) = f[⟨id_{X}, u⟩]

when f ∈ F_{X×U}, u : X ^{}U. If h : L × U ^{}F then curry(h) : L ^{}F_{−×U} is given by

curry(h)_{X}(l) = h_{X×U}(l[π], π′)

If F is representable itself, i.e., F = V for V ∈ ℂ then we have (U→V)_{X} = V_{X×U} = ℂ(X × U, V). If the exponential U→V exists in ℂ then (U→V)_{X} ≅ ℂ(X, U→V) = Y(U→V)_{X} so the Yoneda embedding preserves existing exponentials and therefore, when writing U→V it does not make a difference whether we understand the function space in ℂ or in ℂ̂.

The functor category ℂ̂ therefore can be seen as a generic extension of a category ℂ with all previously lacking function spaces. It is, however, not a free extension, as ℂ̂ extends ℂ with much more structure than merely function spaces.

2.6.10.3 Examples

In 𝕊̂ the exponential Tm(A)→Tm(B) can be given by

(Tm(A)→Tm(B))_{Γ} = Tm(B)_{Γ,x:A}

Therefore, we have a global element in 𝕊̂

lambda : ∏_{A,B types}(Tm(A)→Tm(B))→Tm(A→B)

which performs functional abstraction. Similarly, we have

app : ∏_{A} ∏_{B}(Tm(A→B))→Tm(A)→Tm(B)

This can be used to give a semantic proof of adequacy of representations of formal systems using higher-order abstract syntax used in the context of Logical Frameworks.

We further remark that if 𝕊 is understood modulo βη-equality then {x: A→B} is the exponential of {x: A} and {x: B} in 𝕊, so in this case Tm(A)→Tm(B) is isomorphic to Tm(A→B).

Recall that N ∈ ℙ̂ was defined as the representable presheaf Y(1). The function space N→N (which is not representable) is given by

(ℕ→ℕ)_{n} = ℕ_{n+1} = {n + 1-ary PTIME-functions}

2.6.10.4 Presheaves and global elements

Suppose that ℂ has a terminal object ⊤. Then by Lemma 2.6.3 the set G(F) of global elements of a presheaf F is isomorphic to the set F_{⊤}. More generally, the global sections functor G : ℂ̂ ^{}Sets is isomorphic to the functor which applies a presheaf F to ⊤. This means that the following diagram commutes for every m ∈ ℂ̂(F, G):

[figure: G(F); ≅; F_{⊤}; G(G); G(m); ≅; G_{⊤}; m_{⊤}]

In view of this we will henceforth notationally identify G(F) with F_{⊤} whenever ℂ has a terminal object.

This has the following useful consequence. If f ∈ ℂ̂(Y(X), Y(Y)) then by the Yoneda-Lemma there exists g ∈ ℂ(X, Y) with f = Y(g), namely g = f_{X}(id_{X}). Now, G(Y(X)) = ℂ(⊤, X) = G(X) hence G(Y(g)) = G(g) and so G(f) = G(g).

2.6.10.5 Extensional presheaves

If the underlying category is well-pointed (cf. Section 2.6.6) then it is possible to define a well-pointed subcategory of ℂ̂ which contains all the representable presheaves and is closed under product and function space.

Definition 2.6.5 Let ℂ be a well-pointed category. A presheaf F ∈ ℂ̂ is called extensional if for each X ∈ ℂ the function η_{X} : F_{X} ^{}F_{⊤}^{G(X)} sending f ∈ F_{X} to G(X) ∋ x ↦ f[x] ∈ F_{⊤} is injective.

In other words, a presheaf F is extensional if for each f_{1}, f_{2} ∈ F_{X} we have f_{1} = f_{2} if and only if f_{1}[x] = f_{2}[x] for all global elements x : X.

Lemma 2.6.6 Let ℂ be a well-pointed category. i. Every representable presheaf Y(X) for X ∈ ℂ is extensional.

ii. Every constant presheaf is extensional, in particular ⊤ is.

iii. If F_{1}, F_{2} are extensional so are F_{1} × F_{2}.

iv. If G is extensional so is F →G.

Proof. Let X, Y be objects of ℂ. An element f ∈ Y(Y)_{X} is a ℂ-morphism from X to Y. As ℂ is well-pointed such morphism is uniquely determined by its action on global elements hence Y(Y) is extensional. If F_{1}, F_{2} are extensional then obviously F_{1} × F_{2} are extensional as products are taken pointwise in ℂ̂.

Now suppose that G is extensional and F is arbitrary and let μ, μ′ ∈ (F →G)_{X}. Assume furthermore that for each x ∈ G(X) we have (F →G)_{x}(μ) = (F →G)_{x}(μ′), i.e., if f ∈ G(F) then μ_{⊤}(x, f) = μ′_{⊤}(x, f). We must show that μ = μ′. To see this, pick Y ∈ ℂ and u ∈ ℂ(Y, X) and f ∈ F_{Y}. Now both μ_{Y} (u, f) and μ′_{Y} (u, f) are elements of G_{Y} thus to show that they are equal it suffices to show that G_{y}(μ_{Y} (u, f)) = G_{y}(μ′_{Y} (u, f)) for each y ∈ G(Y) as G is extensional. By naturality we have G_{y}(μ_{Y} (u, f)) = μ_{⊤}(u ∘ y, F_{y}(f)). By assumption the latter term equals μ′_{⊤}(u ∘ y, F_{y}(f)) hence the result. □

Let us write Ext(ℂ) for the full subcategory (of ℂ̂) of extensional presheaves.

Lemma 2.6.7 Ext(ℂ) is a well-pointed category.

Proof. Let F, G ∈ Ext(ℂ) and u, v : F ^{}G be natural transformations such that u(f) = v(f) for each f ∈ G(G) ≅ G_{⊤}. We must show that u = v, i.e., u_{X}(m) = v_{X}(m) for each X ∈ ℂ and m ∈ F_{X}. Since G is extensional this would follow from u_{X}(m)[g] = v_{X}(m)[g] for every g ∈ G(G). But by naturality and assumption on u, v we have

u_{X}(m)[g] = u_{⊤}(m ∘ g) = v_{⊤}(m ∘ g) = v_{X}(m)[g]
□

Remark 2.6.8 In view of this fact extensional presheaves admit a more concrete description as sets with additional structure. Namely, an extensional presheaf can equivalently be given as a set |A| together with a |ℂ|-indexed family of functions A_{X} ⊆ |A|^{G(X)} such that whenever u ∈ ℂ(Y, X) and a ∈ A_{X} then a ∘ G(u) ∈ A_{Y}. A morphism between two such objects A and B is a function f : |A| ^{}|B| such that for each a ∈ A_{X} we have f ∘ a ∈ B_{X}. These objects form a category equivalent to Ext(ℂ). This category has also been introduced in [12] and implicitly in [39].

We have now assembled enough technical machinery to give a semantic proof of Theorem 2.5.1 2.6.10.6 Interpretation of PV^{ω} in ℙ̂

We interpret the base type as the representable presheaf N = Y(1). Function types are interpreted as the corresponding function spaces in ℙ̂.

Next, we need to assign a global element [[c]] : [[A]] to each constant c: A.

The first-order constants are interpreted by applying the Yoneda embedding to their set-theoretic meanings. For example, we define [[S_{0}]] ∈ ℙ̂(⊤, O→O) ≅ ℙ̂(O, O) as Y(λx.2x). That is, [[s_{0}]]_{n}(f) = λx⃗.2f(x⃗).

For the recursor rec : A where

A = o→(o→o→o)→(o→o)→o→o

we first note that in view of Prop. 2.6.4

[[A]] ≅ ℂ̂(N × N _{+2} × N _{+1}, N _{+1})

Treating this isomorphism as an identity we can define [[rec]] directly as follows:

[[rec]]_{n}(g, h, k) = f

where g ∈ N_{n}, h ∈ N_{n+2}, k ∈ N_{n+1} are arbitrary and f ∈ N_{n+1} is defined from g, h, k by bounded recursion on notation according to Def. 2.2.1.

Naturality amounts to the fact that bounded recursion on notation commutes with substitution, i.e., if f is obtained from g, h, k by bounded recursion on notation as above and s ∈ ℙ(m, n) then f ∘ s can be obtained by bounded recursion on notation from g ∘ s, h ∘(s + 2), k ∘(s + 1). Here, by s + k ∈ ℙ(m + k, n + k) we mean the product functor − × k applied to s.

Remark 2.6.9 Since ℙ is a well-pointed category it makes sense to form the subcategory Ext(ℙ) of extensional presheaves and in view of Lemma 2.6.6 the interpretation restricts to Ext(ℙ).

Now, if t : N→N then [[t]] : [[N]]→[[N]] and by the Yoneda-Lemma the function G([[t]]) : G(N) ^{}G(N) is a PTIME-function hence by the Yoneda Lemma [[t]] is a PTIMEfunction. It remains to show that it agrees with the set-theoretic interpretation of t. In order to show this we consider the logical relation between the set-theoretic interpretation and the interpretation in ℙ̂ generated by

xR_{N}y ⟺ x = y

Notice that this definition makes sense as N_{⊤} = N_{0} ≅ ℕ.

Suppose that u ∈ [[N^{n}→N]]^{Sets}, i.e., u : ℕ^{n} ^{}ℕ and u′ : [[N^{n}→N]]_{ℙ}, i.e., u′ ∈ ℙ̂(N^{n}, N) ≅ ℙ(n, 1) then by definition of a logical relation u and u′ are related in R_{N^{n}→N} if they send related elements to related elements, which means in this particular case that u and u′ are equal functions. Therefore, if u ∈ [[N^{n}→N]]^{Sets} is related to some global element of [[N^{n}→N]]_{ℙ} then it must be a polynomial time computable function.

The promised result that set-theoretic meanings of first-order terms are polynomial time computable therefore is a consequence of Theorem 2.6.2 if we can show that the respective meanings of every constant are related.

For first-order constants this is immediate from the above unfolding of R at first-order types.

For the recursor assume that

\begin{matrix}g R_{N} g′ \\ h R_{N→N→N} h′ \\ k R_{N→N} k′ \end{matrix}

After unfolding the definitions this gives that g, g′ and h, h′ and k, k′ are extensionally equal functions. Therefore, [[rec]]^{Sets}(g, h, k) and [[rec]]_{ℙ}(g′, h′, k′) are also equal and hence related.

2.6.10.7 Syntactic version

The content of this section is independent of the rest of this thesis.

Cook and Urquhart actually show a stronger result, namely that every first-order PV^{ω} term is provably equal (w.r.t. some equational theory) to a term in a first-order system PV which consists of terms for all function definitions in F and an equational logic based on their defining axioms. It is possible to give a semantic proof of this by replacing ℙ with a category having tuples of PV-terms modulo provable equality as morphisms. The interpretation of PV^{ω} in ℙ̂𝕍 then assigns to every first-order term of PV^{ω} a PV-term.

The proof then goes through in essentially the same way; however, such a syntactic category is not well-pointed (this would mean that extensionally equal function expressions would be provably equal), so we cannot restrict our interpretation to extensional presheaves.

We also note that, as before, the logical relation only establishes that the resulting PV-term is extensionally equal to the PV^{ω}-term to start off with. If we want to strengthen this to PV^{ω}-provable equality one must use a more general kind of logical relation. We omit the details as we do not consider these syntactic versions later.

2.6.11 Adjoint functors

The reader who is unfamiliar with adjoint functors and does not want to invest energy could skip the following section and ignore later references to the concept. We include it because in our opinion it substantially clarifies some of the later definitions; e.g., linear function space and comonads.

Let F : ℂ ^{}𝔻 and G : 𝔻 ^{}ℂ. We say that F is left adjoint to G, written F ⊣ G (alternatively G is right adjoint to F, written G ⊢ F) if for each X ∈ ℂ and Y ∈ 𝔻 we have a bijective correspondence

φ_{X,Y} : ℂ(F X, Y) ≅ 𝔻(X, GY)

which is natural in X, Y in the sense that if u ∈ ℂ(X, X′) and v ∈ 𝔻(Y, Y′) then

φ_{X′,Y′}(v ∘ f ∘ F u) = Gv ∘ φ_{X,Y} (f) ∘ u

The application of φ or φ^{−1} to a morphism f is called the transpose of f (along the adjunction).

We have a natural transformation ε : F ∘ G ^{}id given by ε_{X} = φ_{GX,X}^{−1}(id_{GX}) called the counit of the adjunction.

Another way of defining adjunction consists of postulating the counit ε and in exchange dropping the requirement that φ be bijective. The map φ and the counit ε are then required to satisfy the equations ε_{Y} ∘ F φ_{X,Y} (u) = u and φ_{X,F X}(ε_{X}) = id_{GX}. An inverse to φ can then be defined by

φ_{X,Y}^{−1} (u) = ε_{Y} ∘ F u

Similarly, we have a natural transformation η : id ^{}G ∘ F called the unit of the adjunction. It is given by η_{X} = φ_{X,F X}(id_{F X}). There are alternative presentations of adjunctions taking φ^{−1} and η or η and ε as primitives.

2.6.11.1 Examples

Let ℂ be a category with terminal object. The constant presheaf functor ∇ : Sets ^{}ℂ̂ given by ∇S_{X} = S and s[u] = s is left adjoint to the global sections functor G : ℂ̂ ^{}Sets. Indeed, if m : ∇S ^{}F then by naturality, we have

m_{X}(s) = m_{X}(∇S_{⟨⟩_{X}}(s)) = F_{⟨⟩_{X}}(m_{⊤}(s))

So m is uniquely determined by the function m_{⊤} : S ^{}F_{⊤}. The unit of the adjunction η_{S} : S ^{}(∇S)_{⊤} is the identity function; the counit ε_{F} : ∇F_{⊤} ^{}F is given by (ε_{F})_{X}(f) = f[⟨⟩_{X}].

The global sections functor G : ℂ̂ ^{}Sets also has a right adjoint Δ : Sets ^{}ℂ̂ given by Δ(S)_{X} = S^{G(X)} where the latter G is the global sections functor from ℂ to Sets. Since Δ(S) is always extensional it restricts to an adjunction between Sets and Ext(ℂ). The unit η : F ^{}Δ(G(F)) of the latter adjunction is the natural transformation sending f ∈ F_{X} to G(X) ∋ x ↦ f[x] ∈ G(F). By definition of “extensional” it is a monomorphism and so it follows from a Theorem in [29] that G is faithful thus giving another proof of Lemma 2.6.7 above.

If ℂ has a product functor − × A and all exponentials of the form −^{A} exist in ℂ, then the assignment X ↦ X^{A} extends to a functor on ℂ acting on morphisms by

u^{A} =_{def} curry(u ∘ ev) : X^{A} ^{}Y^{A}

whenever u : X ^{}Y.

This functor is right adjoint to the product functor − × A in view of the bijective correspondence ℂ(X × A, Y) ≅ ℂ(X, Y^{A}).

2.6.12 Comonads

Comonads form the semantic analogue of S4-modal operators which we will need in order to interpret safe recursion. They are the dual notion of the more familiar monads [29, 30, 24] in the sense that a comonad on a category ℂ is a monad on ℂ^{op} and so the general theorems on monads carry over to comonads mutatis mutandis. We will nevertheless develop the needed material from scratch.

Definition 2.6.10 Let ℂ be a category. A comonad on ℂ is a functor □ : ℂ ^{}ℂ together with two natural transformations ε_{X} : □(X) ^{}X—the counit—, and ν_{X} : □(X) ^{}□(□(X))—the comultiplication satisfying the following equations:

[figure: □X; ν_{X}; □□X; □X; ε_{□X}; id_{□X}; □X; ν_{X}; □□X; □X; □ε_{X}; id_{□X}; □X; ν_{X}; □□X; □□X; ν_{X}; ν_{□X}; □□□X; □ν_{X}]

The reader familiar with monads might wish to note that the comonads of interest to us will not have a strength, i.e., the morphism part of the functor □ is in general not applicable in the presence of parameters.

Proposition 2.6.11 If G : ℂ ^{}𝔻 and F : 𝔻 ^{}ℂ with G ⊢ F is a pair of adjoint functors then F ∘ G : ℂ ^{}ℂ forms a comonad with the counit taken from the adjunction and comultiplication defined from the unit η by

ν_{X} = F (η_{GX})

Every comonad arises in this way albeit not necessarily in a unique way.

Proof. By dualising the corresponding proof for monads, see [24]. □

2.6.12.1 Kleisli triples

Let (□, ε, ν) be a comonad on category ℂ.

If f : □A ^{}B then we can define f^{□} : □A ^{}□B by

f^{□} = □(f) ∘ ν_{A}

We call f^{□} the Kleisli-lifting of f. This lifting which is reminiscent of the S4 necessitation rule

\frac{□Γ ⊢ φ}{□Γ ⊢ □φ}  (Nec)

satisfies the following equations:

[figure: □A; f^{□}; □B; B; ε_{B}; f; □A; □A; ε_{A}^{□}; = id_{□A}; □A; f^{□}; □B; □C; g^{□}; (g ∘ f^{□})^{□}]

From ε and f ↦ f^{□} we can define comultiplication by ν_{X} = (id_{□X})^{□} and even the morphism part of □ by □(f) = (f ∘ ε)^{□}. The equations relating ε and ν, naturality of ε (i.e. f ∘ ε = ε ∘ □(f)), and the functor laws can be proved from the above equations for f ↦ f^{□} so that we obtain a more economical definition of comonads starting from a mapping □ on objects, a family of maps ε, and an operation f ↦ f^{□} on morphisms subject to the above three equations.

The corresponding concept for monads was called Kleisli-triple by Moggi.

2.6.12.2 Preservation of products

Now assume that ℂ has terminal object and cartesian products. We always have the maps

⟨⟩_{□⊤} : □⊤ ^{}⊤

and

⟨□π, □π′⟩ : □(A × B) ^{}□A × □B

The comonad □ preserves terminal objects and cartesian products if these maps are isomorphisms. In this case, we can identify □⊤ and ⊤ as well as □(A × B) and □A × □B.

2.6.12.3 Examples

If ℂ is a category with terminal object then the adjunction G ⊢ ∇ defines a comonad on ℂ̂ given explicitly by (□F)_{X} = F_{⊤}. In the particular case of ℂ = 𝕊 we have that □Tm(A) is the constant presheaf of closed terms of type A, and □(Tm(A)→Tm(B)) is the constant presheaf Tm(B)_{x:A}, i.e., the set of terms of type B with a distinguished free variable of type A.

This can be used to interpret the induction principles for higher-order abstract syntax studied in [11].

Let 𝔹 be the category having pairs of natural numbers as objects. A morphism from (m, n) to (m′, n′) is a pair (f, g) where f ∈ ℙ(m, m′) and g ∈ ℙ(m + m′, n′) and in addition

max |g(x⃗; y⃗)| ≤ p(|x⃗|) + max(|y⃗|)

for some polynomial p. This category arises from the “polymax-bounded” functions used in the soundness proof for safe recursion (Thm. 2.3.4). Indeed, safe recursion can be recovered as the following schema in 𝔹: If g : (m, n) ^{}(0, 1) and h : (m+1, n+1) ^{}(0, 1) then f : ℕ^{m+1} × ℕ^{n} ^{}ℕ defined from g, h by safe recursion is a 𝔹-morphism from (m + 1, n) to (0, 1).

Using 𝔹̂ we can turn this pattern into a single higher-order constant as follows.

The category 𝔹 has a terminal object ⊤ = (0, 0) and cartesian products given on objects by componentwise addition. In the functor category we have the representable presheaf N = Y(0, 1) given concretely by

N_{(m,n)} = {f : ℕ^{m} × ℕ^{n} ^{}ℕ | f ∈ PTIME ∧ |f(x⃗; y⃗)| ≤ p(|x⃗|) + max(|y⃗|)}

In 𝔹̂ we have a comonad □ given by □F_{(m,n)} = F_{(m,0)}. The counit is given by (ε_{F})_{(m,n)} = F_{π} where π : (m, n) ^{}(m, 0) is the projection. The comultiplication is the identity as □^{2} = □.

This comonad arises from an adjunction between 𝔹̂ and ℙ̂ one direction of which is (on objects) the projection (m, n) ↦ m and the other one sends m to (m, 0).

Now,

□N_{(m,n)} = N_{(m,0)} = 𝔹((m, n) , (1, 0))

So □N ≅ Y(1, 0) and by the Yoneda Lemma we know that the set of 𝔹-morphisms from (m, n) to (0, 1) are in 1-1 correspondence with the 𝔹̂-morphisms from □N^{m} × N^{n} to N.

Lemma 2.6.3 gives for arbitrary presheaf F the characterisations

\begin{matrix}(N→F)_{(m,n)} ≅ F_{(m,n+1)} \\ (□N→F)_{(m,n)} ≅ F_{(m+1,n)} \end{matrix}

Safe recursion thus takes the form of a single higher-order constant

rec : N→(□N→N→N)→□N→N

In the next section we show how to interpret in 𝔹̂ a modal lambda calculus which allows to formulate a higher-order extension of safe recursion analogous to system PV^{ω}.

2.6.12.4 Interpretation of modal lambda calculus

A cartesian closed category with a product preserving comonad can be used to interpret the modal lambda calculus by Pfenning and Davies [33].^{1}

In addition to the constructs of typed lambda calculus it has a unary type former □:

A ::= … | □A

1We will later on use other formulations of modal lambda calculus. In using Pfenning’s calculus here we illustrate the flexibility of the comonad concept and provide a link between our subsequent formulations of modal lambda calculus and existing work.

The grammar for terms is extended by the following clauses:

e ::= … | box(e) | let e_{1}=box(x) in e_{2}

Contexts are built out of two kinds of binding: x: A (ordinary binding) and x :^{□} A (modal binding)^{2}. A context with modal bindings only is called modal.

The typing rules for the lambda calculus fragment are as before with the understanding that rule T-Var applies to both modally and ordinarily bound variables and that rule T-Arr-E requires an ordinarily bound variable. The rules for the newly introduced constructs are

\frac{Γ ⊢ e : A Γ modal}{Γ ⊢ box(e) : □A}  (T-Box)

\frac{Γ ⊢ e_{1} : □A Γ, x :^{□} A ⊢ e_{2} : B}{Γ ⊢ let e_{1}=box(x) in e_{2} : B}  (T-Let-Box)

This system can be modelled in a category with product preserving comonad as follows: types are interpreted as usual with the extra clause

[[□A]] = □[[A]]

A context Γ gets interpreted as ∏_{x∈dom(Γ)} F_{x}([[A]]) where F_{x} is the identity if x is ordinarily bound and F_{x} = □ if x is modally bound in Γ.

As before, a typing judgement Γ ⊢ e : A gets interpreted as a morphism

[[Γ ⊢ e : A]] : [[Γ]] ^{}[[A]]

The defining clauses for the functional fragment are as before.

Since □ preserves products, the meaning of a modal context is of the form □(X) thus allowing us to interpret rule T-Box by Kleisli lifting, i.e.,

[[Γ ⊢ box(e)]] = [[Γ ⊢ e]]^{□} (= □([[Γ ⊢ e]]) ∘ ν)

Finally, the rule T-Let-Box is interpreted using composition as follows.

[[Γ ⊢ let e_{1}=□(x) in e_{2} : B]] = [[Γ, x :^{□} A ⊢ e_{2} : B]] ∘⟨id_{[[Γ]]}, [[Γ ⊢ e_{1} : □A]]⟩

In order to get a higher-order version of safe recursion we add constants of appropriate type for the basic functions, e.g., S_{0}, S_{1} : N→N and a constant

rec : N→(□N→N→N)→□N→N

2Pfenning and Davies use two zoned contexts of the form Γ; Δ where Γ records the modal bindings and Δ records the ordinary bindings.

We can embed Bellantoni-Cook’s system into this calculus by translating a function f(x⃗; y⃗) into a closed term of type □N^{m}→N^{n}→N. Moreover, functionals like the iteration functional it(f, u) = f^{|u|}(0) from Section 2.5 can now be defined as single constants rather than schemata in B:

it =_{def} λf: N→N.λu: □N.rec 0 (λx: □N.λy: N.f y) u

The resulting calculus can now be interpreted in 𝔹̂ as sketched above using the Yoneda Lemma to interpret first-order constants and the above reasoning in order to interpret the recursor rec. The thus obtained interpretation has the property that, again by the Yoneda Lemma, the meaning of a first-order function is a morphism in 𝔹—hence PTIME. In order to show that the thus associated PTIME-function is the “correct one”, we may relate this interpretation to the set-theoretic one by extending the logical relation used for PV^{ω} by the clause

x R_{□A} y ⟺ x R_{A} y

Notice here that G(□F) = G(F). By following the pattern of the corresponding proof for PV^{ω} it then follows that the set-theoretic meanings of first-order functions in the modal lambda calculus with constants for safe recursion are PTIME.

Like in the case of PV^{ω} the category 𝔹 can be replaced by a category whose morphisms are terms denoting definitions in the function algebra B. More precisely, a morphism from (m, n) to (m′, n′) would consist of an m′-tuple of terms f_{i}(x⃗; ) in m normal variables x⃗ together with an n′-tuple of terms g_{j}(x⃗; y⃗) in m normal variables x⃗ and n safe variables y⃗.

The semantics then shows that for every term t : □N→N in modal lambda calculus we can find a term t̂(x; ) with the same meaning as t.

One should notice, however, that such a category is no longer well-pointed which means that the definition of the logical relation must be made in a slightly more complicated fashion. Details of this approach will not be discussed in this thesis.

2.7 Affine linear categories

Modelling contexts as cartesian products means that (semantically) a variable can be used as many times as desired: in every category with cartesian products we can define diagonal morphisms δ : [[x: A]] ^{}[[x_{1}: A, x_{2}: A]], namely δ = ⟨id, id⟩.

If we want to semantically exploit linearity restrictions we must generalise cartesian products in such a way that diagonal maps do not necessarily exist anymore. This can be done and leads to the notion of symmetric monoidal category [29]. This concept, although well established, is somewhat complicated due to the fact that the associated structure is not determined by universal properties as in the case of cartesian product and thus a number of coherence conditions are required.

Fortunately, all the examples of interest in the present context enjoy an extra property which allows us to avoid coherence. Rather than defining symmetric monoidal categories and afterwards isolating a special case we will define the latter directly.

Definition 2.7.1 An affine linear category (ALC) is given by the following data:

– a category ℂ,

– for any two objects A, B ∈ ℂ an object A ⊗ B, called tensor product, and morphisms π : A ⊗ B ^{}A and π′ : A ⊗ B ^{}B, called projections, which are jointly monomorphic in the following sense. If f, g : C ^{}A ⊗ B and π ∘ f = π ∘ g and π′ ∘ f = π′ ∘ g then f = g,

– for any two maps f : A ^{}A′ and g : B ^{}B′ a map f ⊗ g : A ⊗ B ^{}A′ ⊗ B′ such that π ∘(f ⊗ g) = f ∘ π and π′ ∘(f ⊗ g) = g ∘ π′,

– an isomorphism α : A⊗(B ⊗C) ^{}(A⊗B)⊗C such that π ∘ π ∘ α = π, π′ ∘ π ∘ α = π ∘ π′, π′ ∘ α = π′ ∘ π′,

– an isomorphism γ : A ⊗ B ^{}B ⊗ A such that π ∘ γ = π′ and π′ ∘ γ = π,

– a terminal object ⊤ such that π : A ⊗ ⊤ ^{}A and π′ : ⊤ ⊗ A ^{}A are isomorphisms

This definition warrants some explanation. First, and most importantly, we note that in view of the requirement on the projections the other constructions f ⊗ g, α, and γ, are uniquely determined by their defining equations.

Next we note that if ℂ happens to have cartesian products then for every pair of objects A, B we have a monomorphism

⟨π, π′⟩ : A ⊗ B ^{}A × B

These leads to the intuition that the tensor product can be seen as an extra property on pairs: given f : C ^{}A and g : C ^{}B then it may or may not be the case that ⟨f, g⟩ factors through A ⊗ B. The definition of ALC states in this sense that certain definable maps involving cartesian products factor through tensor products, for example the associativity map ⟨⟨π ∘ π, π′ ∘ π⟩, π′⟩ : (A × B) × C ^{}A × (B × C) does.

We notice that global elements always factor through the tensor product: if a ∈ G(A) and b ∈ G(B) then a ⊗ b : ⊤ ⊗ ⊤ ^{}A ⊗ B and thus (a ⊗ b) ∘⟨⟩^{−1} ∈ G(A ⊗ B) where we have used the fact that the unique map π = π′ = ⟨⟩ : ⊤ ⊗ ⊤ ^{}⊤ is required to be an isomorphism. Now π ∘(a ⊗ b) ∘⟨⟩^{−1} = a ∘ π ∘⟨⟩^{−1} = a and similarly for b.

What we have essentially used here is the existence of a diagonal map δ : ⊤ ^{}⊤⊗⊤ satisfying π ∘ δ = π′ ∘ δ = id. This motivates the following definition.

Definition 2.7.2 An object D in an ALC is called duplicable if there exists a (uniquely determined) morphism δ : D ^{}D ⊗ D such that π ∘ δ = π′ ∘ δ = id.

We write ℂ_{dup} for the full subcategory consisting of the duplicable objects.

Lemma 2.7.3 If ℂ is an ALC then ⊤ ∈ ℂ_{dup} and whenever A, B ∈ ℂ_{dup} so is A ⊗ B. Moreover, A ⊗ B is a cartesian product in ℂ_{dup}.

Proof. The diagonal for ⊤ is obtained as a special case of the isomorphism X ≅ X ⊗ ⊤. The diagonal δ^{A⊗B} for A ⊗ B is obtained from δ^{A} : A ^{}A ⊗ A and δ^{B} : B ^{}B ⊗ B as

A ⊗ B ^{δ^{A}⊗δ^{B}}^{}(A ⊗ A) ⊗ (B ⊗ B) ^{w}^{}(A ⊗ B) ⊗ (A ⊗ B)

where w is a wiring map. □

The above discussion of global elements generalises to the following lemma.

Lemma 2.7.4 If D is duplicable and f : D ^{}A, g : D ^{}B then there exists a unique map h = (f ⊗ g) ∘ δ : D ^{}A ⊗ B such that π ∘ h = f and π′ ∘ h = g.

For the reader familiar with symmetric monoidal categories (SMC), see [29] we remark that every ALC forms an SMC, but not vice versa.

2.7.1 Examples

Of course, every category with cartesian products and terminal object forms an ALC. A more interesting example which forms the intuitive basis of the central result in this thesis is constructed as follows.

Let us define the category 𝕄 of length spaces (for lack of better name) as follows. A length space is a pair A = (|A|, ℓ_{A}) where |A| is a set and ℓ_{A} : |A| ^{}ℕ is a mapping assigning a natural number—the length—to each element of |A|. A morphism from length space A to B is a function f : |A| ^{}|B| such that

ℓ_{B}(f(a)) ≤ c + ℓ_{A}(a)

for some “witnessing” constant c independent of a. Composition is ordinary composition of functions. The tensor product is given by |A⊗B| = |A|×|B| and ℓ_{A⊗B}(a, b) = ℓ_{A}(a)+ℓ_{B}(b). The tensor product of two morphisms f : A ^{}A′ and g : B ^{}B′ is given as in Sets by (f ⊗g)(a, b) = (f(a), g(a)). If c_{f} and c_{g} are constants witnessing that f, g are morphisms then we have

ℓ_{A′⊗B′}((f ⊗ g)(a, b)) ≤ c_{f} + c_{g} + ℓ_{A}(a) + ℓ_{B}(b) = ℓ_{A⊗B}(a, b) + c_{1} + c_{2}

So, f ⊗ g is a morphism witnessed by c_{1} + c_{2}. Similarly, the set-theoretic isomorphisms witnessing associativity, etc., of cartesian product are morphisms in 𝕄.

The terminal object ⊤ is given by |⊤| = {⟨⟩} and ℓ_{⊤}(⟨⟩) = 0. Clearly, A ⊗ ⊤ ≅ A is isomorphic to A by the set-theoretic isomorphism between |A| × ⊤ and |A| so that we can conclude that 𝕄 is an ALC.

A length space X is duplicable iff there exists a constant c such that ℓ_{X}(x) ≤ c for all x ∈ |X|. Namely, in this case ℓ_{X⊗X}(x, x) = 2ℓ_{X}(x) ≤ c + ℓ_{X}(x) so the diagonal map is a morphism from X to X ⊗ X.

On the other hand, the object N with |N| = ℕ and ℓ_{N}(x) = |x| is not duplicable.

The category 𝕄 has the subcategory 𝕄_{s} of strict morphisms which satisfy the stronger requirement

ℓ_{B}(f(a)) ≤ ℓ_{A}(a)

The tensor product on 𝕄 restricts to 𝕄_{s} in the sense that the required isomorphisms α, γ etc. are strict and that tensor product of strict morphisms is strict. In ℕ^{s} the only duplicable objects are those for which ℓ_{X}(x) = 0.

Both 𝕄 and 𝕄_{s} also have a cartesian product given by |A × B| = |A| × |B| and ℓ_{A×B}(a, b) = max(ℓ_{A}(a), ℓ_{B}(b)).

The pairing of morphisms f : C ^{}A and g : C ^{}B is given as in Sets by ⟨f, g⟩(c) = (f(c), g(c)) and we have

ℓ_{A×B}(f(c), g(c)) = max(ℓ_{A}(f(c)), ℓ_{B}(g(c))) ≤ max(c_{f}, c_{g}) + ℓ_{C}(c)

Notice that ⟨f, g⟩ is not an 𝕄-morphism from C to A ⊗ B unless C is duplicable.

2.7.2 Wiring maps

The morphisms α, γ and the constructor ⊗ yield intuitively linear maps. One might ask whether these primitives are sufficient; they are indeed in the following sense.

A morphism in an ALC constructed by composition, tensoring (⊗) from the isomorphisms α, γ, etc., projections, identities will be called a wiring map (MacLane calls the corresponding notion for SMC canonical maps).

Suppose that object A can be decomposed into a term built up via ⊤ and ⊗ from objects A_{1}, …, A_{n} in this order, e.g., n = 4 and A = ((A_{1} ⊗A_{2})⊗(⊤⊗A_{3}))⊗A_{4}. Suppose further that ι is an injection from m elements into n elements and that B is built up from A_{ι(1)}, …, A_{ι(m)} in this order, e.g., B = A_{4} ⊗ (A_{3} ⊗ A_{2}). Then there exists a wiring map w_{ι} : A ^{}B which “sends A_{i} in A to A_{i} in B” in the sense that it satisfies the obvious specification in terms of projections. In the example the wiring map is given by

((A_{1} ⊗ A_{2}) ⊗ (⊤ ⊗ A_{3})) ⊗ A_{4}
γ  A_{4} ⊗ ((A_{1} ⊗ A_{2}) ⊗ (⊤ ⊗ A_{3}))
^{id_{A_{4}}⊗(π′⊗π′)} A_{4} ⊗ (A_{2} ⊗ A_{3})
^{id_{A_{4}}⊗γ}  A_{4} ⊗ (A_{3} ⊗ A_{2})

and it is uniquely determined by the specification π ∘ w = π′, π ∘ π′ ∘ w = π′ ∘ π′ ∘ π, π′ ∘ π′ ∘ w = π′ ∘ π ∘ π.

It should be clear from this example how these wiring maps are defined and specified in general using an appropriate inductive definition.

2.7.3 Indexed tensor product

Recall that in a category with cartesian products we can define all finite indexed cartesian products using some arbitrary enumeration of the index set. A similar thing can be done in ALC. Suppose that (A_{i})_{i∈I} is a finite family of objects of an ALC ℂ. We define the tensor product ⨂_{i∈I} A_{i} by

⨂_{i∈I} A_{i} = (… (A_{i_{1}} ⊗ A_{i_{2}}) …) ⊗ A_{i_{n}}

where I = {i_{1}, …, i_{n}} is some arbitrary enumeration of the elements of the index set I. Associativity and symmetry show that this tensor product is independent up to isomorphism of the chosen enumeration. We can define projections π_{i} : ⨂_{i∈I} A_{i} ^{}A_{i} and wiring maps in the obvious sense.

2.7.4 Linear function spaces

In this section we define an analogue of function spaces for ALC. Let A, B be objects in an ALC ℂ. The linear function space or linear exponential of B by A is given by an object A⊸B, a morphism ev : (A⊸B) ⊗ A ^{}B—the evaluation map—and for every morphism f : X ⊗ A ^{}B a unique morphism curry(f) : X ^{}A⊸B called the currying or exponential transpose of f such that ev ∘(curry(f) ⊗ id_{A}) = f.

Again, instead of uniqueness we can postulate the following additional equations which in turn are consequences of uniqueness:

curry(f) ∘ h = curry(f ∘(h ⊗ id)) curry(ev) = id

Currying and evaluation establish a natural bijection

ℂ(X ⊗ A, B) ≅ ℂ(X, A⊸B)

An ALC in which all linear function spaces exist will be called affine linear closed category (ALCC). We remark that the analogue for SMC is called symmetric monoidal closed category. In an ALCC the mapping X ↦ A⊸X extends to a functor on ℂ which is right adjoint to the tensor product functor ⊗ A.

2.7.4.1 Examples

If tensor product is given by cartesian product then ordinary function spaces in the sense of Section 2.6.7 are the linear function spaces. In 𝕄 the function space A⊸B is given by

\begin{matrix}|A⊸B| = 𝕄(A, B) \\ ℓ_{A⊸B}(f) = min\{c | ∀a∈ |A|.ℓ_{B}(f(a)) ≤ c + ℓ_{A}(a)\} = max_{a∈|A|} ℓ_{B}(f(a)) −^{.} ℓ_{A}(a) \end{matrix}

Evaluation sends (f, a) ∈ |(A⊸B) ⊗ A| to f(a) ∈ B and we have

ℓ_{B}(f(a)) ≤ ℓ_{A⊸B}(f) + ℓ_{A}(a) = ℓ_{(A⊸B)⊗A}(f, a)

so evaluation is actually a map in 𝕄_{s}. Conversely, if f : X ⊗ A ^{}B then curry(f)(x) = f_{x} where f_{x}(a) = f(x, a). We have ℓ_{B}(f_{x}(a)) ≤ ℓ_{X}(x)+ℓ_{A}(a)+c for some c, so f_{x} ∈ |A⊸B| and moreover ℓ_{A⊸B}(f_{x}) ≤ c + ℓ_{X}(x), so curry(f) ∈ 𝕄(X, A⊸B).

We notice that if f is an 𝕄_{s}-morphism then so is curry(f). This—together with the fact that evaluation is a 𝕄_{s}-morphism—implies that 𝕄_{s} also has linear function spaces and that they agree with those in 𝕄. Somewhat astonishingly we do not have |A⊸B| = 𝕄_{s}(A, B) in 𝕄_{s}.

2.7.4.2 Affine linear lambda calculus

In the same way as the typed lambda calculus provides an “internal language” for categories with function spaces we can devise a linear typed lambda calculus which admits a natural interpretation in an ALC with enough linear function spaces.

The types of linear lambda calculus^{3} are the same as those of ordinary typed lambda calculus except that we write A⊸B instead of A→B to emphasize the linear character. The typing rules T-Var, T-Const, and T-Arr-I remain unchanged. Rule T-Arr-E which corresponds to application is replaced by the following weaker rule:

\frac{Γ ⊢ e_{1} : A⊸B  Δ ⊢ e_{2} : A  dom(Γ) ∩ dom(Δ) = ∅}{Γ, Δ ⊢ e_{1}e_{2} : B}

(T-Arr-E-Lin)

Induction on derivations shows that if Γ ⊢ e : A then every variable x ∈ dom(Γ) appears at most once in e. So, for example, λf: A⊸A⊸A.λx: A.λy: A.fyx is well typed (of type (A⊸A⊸A)⊸ A⊸A⊸A), but

λf: A⊸A⊸A.λx: A.fxx

is ill-typed as is

λf: A⊸A.λx: A.f(fx)

The linear lambda calculus admits an interpretation in an ALC with a distinguished class of objects closed under linear function space and containing interpretations for base types. Types are interpreted by the obvious clause [[A⊸B]] = [[A]]⊸[[B]]. A context Γ is interpreted as a dom(Γ)-index tensor product:

[[Γ]] = ⨂_{x∈dom(Γ)}[[Γ(x)]]

A variable is interpreted by the corresponding projection; functional abstraction is interpreted by currying. For linear application assume that Γ ⊢ e_{1} : A⊸B and Δ ⊢ e_{2} : A. Then

[[Γ, Δ, ⊢ e_{1}e_{2} : B]]

3We henceforth omit the attribute “affine” in this context.

is given by

[[Γ, Δ]] ^{w}^{}[[Γ]] ⊗ [[Δ]] ^{f_{1}⊗f_{2}}^{}[[A⊸B]] ⊗ [[A]] ^{ev}^{}[[B]]

where w is a wiring map and f_{1}, f_{2} are the interpretations of e_{1}, e_{2}, respectively.

2.7.5 Comonads in ALC

A comonad □ : ℂ ^{}ℂ is said to preserve tensor product and terminal object if the morphisms ⟨⟩ : □⊤ ^{}⊤ is an isomorphism and there exists a (uniquely determined) isomorphism φ : □(X ⊗ Y) ^{}□X ⊗ □Y such that π ∘ φ = □π and π′ ∘ φ = □π′. If this is the case, we can notationally identify □(X ⊗ Y) with □X ⊗ □Y and □⊤ with ⊤.

2.7.6 ALC and the Yoneda embedding

In this section we show how to add linear function spaces and also a comonad ! giving duplicability to an arbitrary well-pointed ALC. This is one of the main tools for our soundness proof for the calculus SLR. We emphasise at this point that the technique described in this section is independent of this intended application and could be applied in various other situations.

Recall that the Yoneda embedding Y : ℂ ^{}ℂ̂ preserves cartesian products and also existing function spaces in a category ℂ with cartesian products. In this way, functor categories provide a way to generically add lacking function spaces to a category with cartesian products.

A similar effect can be achieved for the linear world using an appropriate tensor product construction for presheaves. For SMC this has been done by Day [10]. However, even if ℂ is an ALC then the presheaf category ℂ̂ equipped with Day’s tensor product will in general fail to be an ALC because the projections (which in this case can be defined) fail to be jointly monomorphic. However, if the underlying ALC ℂ is well-pointed then we can define a tensor product on the full subcategory Ext(ℂ) of extensional presheaves making it an ALCC in such a way that the Yoneda embedding preserves tensor products and existing linear function spaces. The tensor product we define agrees in fact with Day’s tensor product which can also be defined for Ext(ℂ). In this particular case there is, however, a more concrete description which in particular avoids the notion of “end” [29]. We will work exclusively with this more concrete description.

Assume for the rest of this section that ℂ is a well-pointed ALC.

2.7.6.1 Tensor product of extensional presheaves

For extensional presheaves A, B ∈ Ext(ℂ) the tensor product A ⊗ B ∈ Ext(ℂ) is defined as follows. For X ∈ ℂ the set (A ⊗ B)_{X} consists of those pairs (a, b) ∈ A_{X} × B_{X} for which there exist objects U, V ∈ ℂ, a map t : X ^{}U ⊗ V and elements ā ∈ A_{U}, b̄ ∈ B_{V} such that a = ā[π ∘ t] and b = b̄[π′ ∘ t].

(a, b) ∈ A_{X} × B_{X}  ā ∈ A_{U}  b̄ ∈ A_{V}
X  U  V

[figure: U ⊗ V; π′; π; t]

In this case we say that t, ā, b̄ witness that (a, b) ∈ (A⊗B)_{X}. We may also use the notation (a, b|t, ā, b̄) for such a pair with the understanding that t, ā, b̄ are not part of the element but merely required to exist.

If u : Y ^{}X and (a, b) ∈ (A ⊗ B)_{X} witnessed by t, ā, b̄ then we define (A ⊗ B)_{u}(a, b) as (a[u], b[u]) which can be witnessed by (t ∘ u, ā, b̄). Using the above notation we can write more compactly

(A ⊗ B)_{u}(a, b|t, ā, t̄) =_{def} (a[u], b[u]|t ∘ u, ā, b̄)

which is a valid definition as the right hand side does not depend on the choice of the witnesses t, ā, b̄.

Since the thus defined presheaf A ⊗ B is a sub-presheaf of the cartesian product A × B it is clearly extensional, i.e., lies in Ext(ℂ).

The projections are defined in the obvious way by π_{X}(a, b) = a and π′_{X}(a, b) = b which are clearly jointly monomorphic.

If f : A ^{}A′ and g : B ^{}B′ then f ⊗ g : A ⊗ B ^{}B ⊗ B′ is given by

(f ⊗ g)_{X}(a, b|t, ā, b̄) =_{def} (f(a), g(b)|t, f(ā), g(b̄))

Associativity is defined by α(a, (b, c)) = ((a, b), c). If (a, (b, c)) is witnessed by t : X ^{}U⊗ V and ā ∈ A_{U} and t′ : V ^{}W ⊗ Z and b̄ ∈ B_{W} and c̄ ∈ C_{Z} then we have

s =_{def} α ∘(id ⊗ t′) ∘ t : X ^{}(U ⊗ W) ⊗ Z

and we obtain a witness for ((a, b), c) as s, id_{U⊗W} , ā, b̄, c̄. Similarly, the other isomorphisms are defined and it follows that Ext(ℂ) with the above tensor product is an ALC.

Let us see how the Yoneda embedding preserves the tensor product. Suppose that (a, b|t, ā, b̄) ∈ (Y(A) ⊗ Y(B))_{X}, i.e., a : X ^{}A, b : X ^{}B, t : X ^{}U ⊗ V, ā : U ^{}A, b̄: V ^{}B, a = ā ∘ π ∘ t, b = b̄ ∘ π′ ∘ t.

Then

h =_{def} (ā ⊗ b̄) ∘ t : X ^{}A ⊗ B

is an element of Y(A ⊗ B) and it is independent of the witness as π ∘ h = ā ∘ π ∘ t = a and similarly π′ ∘ h = b. Conversely, if h ∈ Y(A⊗B)_{X} then (π ∘ h, π′ ∘ h|h, id, id) ∈ Y(A)⊗Y(B) and it is readily seen that these constructions yield a natural isomorphism ι : Y(A) ⊗ Y(B) ≅ Y(A ⊗ B) which is canonical in the sense that Y(π) ∘ ι = π and Y(π′) ∘ ι = π′ which allows us to treat it as an identity.

Lemma 2.7.5 Let A, B ∈ Ext(ℂ). If D ∈ ℂ_{dup} then (A ⊗ B)_{D} = (A × B)_{D}.

Proof. Any element (a, b) ∈ (A × B)_{D} can be witnessed by δ_{D}, a, b. □

2.7.6.2 Linear function space of extensional presheaves

Proposition 2.7.6 The ALC Ext(ℂ) has all linear function spaces and the Yoneda embedding Y : ℂ ^{}Ext(ℂ) preserves existing linear function spaces up to canonical isomorphism.

Proof. We only give the constructions leaving most of the routine verifications to the reader.

The linear function space A⊸B of extensional presheaves is defined similarly to the ordinary function space:

(A⊸B)_{X} =_{def} Ext(ℂ)(Y(X) ⊗ A, B)

If f : C ⊗ A ^{}B then curry(f) : C ^{}A⊸B is defined as follows. Suppose that X ∈ ℂ, c ∈ C_{X}, Y ∈ ℂ, and (u, a) ∈ (Y(X) ⊗ A)_{Y}. The latter means that u : Y ^{}X, a ∈ A_{Y} and that there exist t : Y ^{}U ⊗ V, ū : U ^{}X, ā : A_{V} such that a = ā[π′ ∘ t] and u = ū ∘ π ∘ t. We define

curry(f)_{X}(c)_{Y} (u, a) =_{def} f_{Y} (c[u], a)

The argument to f_{Y} can be witnessed by t, c[ū], ā.

Next, we define the evaluation map ev : (A⊸B) ⊗ A ^{}B. Suppose we are given X ∈ ℂ, f ∈ (A⊸B)_{X}, a ∈ A_{X}, and witnesses t : X ^{}U ⊗ V, f̄ ∈ (A⊸B)_{U}, ā ∈ A_{V}. This means that whenever (x, a′) ∈ (Y(X) ⊗ A)_{Y}, i.e., x : Y ^{}X and a′ ∈ A_{Y} plus appropriate witnesses, then f_{Y} (x, a′) = f̄_{Y} (π ∘ t ∘ x, a′).

We might be tempted to define

ev_{X}(f, a) =_{def} f_{X}(id_{X}, a)

However, this does not work since (id_{X}, a) need not be an element of (Y(X) ⊗ A)_{X}. The only possible witness would use t and ā, but then we would have to give x̄ : U ^{}X such that x̄ ∘ π ∘ t = id_{X} and there is in general no reason as to why such x̄ should exist.

Instead, we define

ev_{X}(f, a) =_{def} f̄_{X}(π ∘ t, a)

and now the argument can be witnessed by t, id_{U}, ā. But since we have used part of the witness in the definition we must show that this is at all well-defined, i.e., does not depend on the witness chosen. This is where extensionality comes in. If x ∈ G(X) then

(ev_{X}(f, a))[x]
= f̄_{X}(π ∘ t, a)[x]
= f̄_{⊤}(π ∘ t ∘ x, a[x])  naturality of f
= f_{⊤}(x, a[x])

where the last step follows from the assumption f̄[π ∘ t] = f using the fact that (x, a[x]) ∈ (Y(X) ⊗ A)_{⊤} by Lemma 2.7.5 e.g. by δ_{⊤}, x, a. So, the restriction of ev_{X}(f, a) along a global element does not depend on the chosen witness and so ev_{X}(f, a) is well-defined by extensionality of B. □

Proposition 2.7.7 If A, B ∈ ℂ and A⊸B exists in ℂ then Y(A⊸B) is canonically isomorphic to Y(A)⊸Y(B).

Proof. By Lemma 2.6.3

Y(A⊸B)_{X}
≅ ℂ(X , A⊸B)
≅ ℂ(X ⊗ A , B)
≅ Ext(ℂ)(Y(X ⊗ A) , Y(B))  since Y is full and faithful
≅ Ext(ℂ)(Y(X) ⊗ Y(A), Y(B))
≅ (Y(A)⊸Y(B))_{X}
□

Finally, we have an affine linear analogue to Prop. 2.6.4.

Proposition 2.7.8 Let U ∈ ℂ. For any presheaf F ∈ Ext(ℂ) the linear function space U⊸F (= Y(U)⊸F) is isomorphic to the presheaf F_{−⊗U} obtained by precomposing F with the functor X ↦ X ⊗ U.

Proof. Like the proof of Prop. 2.6.4 with × replaced by ⊗ and → replaced by ⊸. □

2.7.6.3 Duplication in Ext(ℂ)

Obviously, if D ∈ ℂ_{dup} then Y(D) is duplicable in Ext(ℂ) since Y(δ_{D}) gives the required diagonal.

In Ext(ℂ) we can associate to any presheaf a “best approximating” duplicable presheaf.

Proposition 2.7.9 There exists a comonad ! : Ext(ℂ) ^{}Ext(ℂ)_{dup} ⊆ Ext(ℂ) such that

– the counit written derelict : ! A ^{}A is an inclusion,

– if D ∈ ℂ_{dup} then ! Y(D) = Y(D),

– ! ⊤ = ⊤,

– ! preserves ⊗ up to equality, in fact both !(A ⊗ B) and ! A ⊗! B are equal to !(A × B),

– G(! A) = G(A).

Proof. The set (! A)_{X} consists of those elements a ∈ A_{X} for which there exists a witness consisting of a duplicable object D ∈ ℂ_{dup}, a map t : X ^{}D and ā ∈ A_{D} such that a = ā[t]. In particular, if A = Y(A) is representable then ! A_{X} consists of those maps into A which factor through a duplicable object. We remark that such factorisation is implicit in the notion of “affinations” in [2].

The morphism part of ! A is inherited from A.

The counit derelict : ! A ^{}A is simply the inclusion map ! A_{X} ⊆ A_{X}.

The diagonal δ : ! A ^{}! A ⊗ ! A is obtained as follows. If X ∈ ℂ and a ∈ (! A)_{X} witnessed by t : X ^{}D and ā ∈ A_{D} where D ∈ ℂ_{dup} and a = ā[t] then (a, a) ∈ ! A ⊗! A as witnessed by δ_{D} ∘ t : X ^{}D ⊗ D and ā, ā.

[figure: a∈ A_{X}; A_{t}; A_{D} ∋ ā; X; t; D; δ; D ⊗ D]

To see that ! ⊤ = ⊤ we notice that ⟨⟩ ∈ ! ⊤_{X} is witnessed by ⟨⟩ : X ^{}⊤ and ⟨⟩ ∈ ⊤_{⊤}.

For preservation of ⊗ suppose that A, B ∈ Ext(ℂ). If (a, b) ∈ !(A × B)_{X} witnessed by t : X ^{}D and ā ∈ A_{D}, b̄ ∈ B_{D} with a = ā[t] and b = b̄[t] then (a, b) ∈ (! A ⊗! B)_{X} can be witnessed by δ ∘ t : X ^{}D ⊗ D, id_{D}, id_{D}, ā, b̄.

Similarly, (a, b) ∈ !(A ⊗ B)_{X} can be witnessed by t : X ^{}D and δ : D ^{}D ⊗ D and ā, b̄.

So !(A × B) ⊆ ! A ⊗! B and !(A × B) ⊆ !(A ⊗ B).

For the inclusion ! A ⊗! B ⊆ !(A × B) assume that (a, b) ∈ (! A ⊗! B)_{X} witnessed by t : X ^{}U ⊗ V and s_{1} : U ^{}D_{1} and s_{2} : V ^{}D_{2} and ā ∈ A_{D_{1}}, b̄ ∈ B_{D_{2}}. Then, since D = D_{1} ⊗ D_{2} is duplicable, (a, b) ∈ !(A × B)_{X} can be witnessed by (s_{1} ⊗ s_{2}) ∘ t and ā[π] ∈ A_{D}, b̄[π′] ∈ B_{D}.

\begin{matrix}(a, b) ∈ A_{X} × B_{X} & ā[s_{1}] ∈ A_{U}, b̄[s_{2}] ∈ B_{V} & ā ∈ A_{D_{1}}, b̄ ∈ B_{D_{2}} \\ X & \overline{t ^{}} & U ⊗ V & \overline{s_{1} ⊗ s_{2} ^{}} & D_{1} ⊗ D_{2} \end{matrix}

Finally, !(A ⊗ B) ⊆ !(A × B) is obvious by applying ! to the inclusion A ⊗ B ⊆ A × B.

The last part G(A) = G(! A) follows from the fact that a global element a : ⊤ ^{}A

□

The type system SLR

In this chapter we define and study the syntax and set-theoretic semantics of a lambda calculus with modal and linear function spaces. Apart from the definition itself the main result proved in this chapter is decidability of type checking by a syntax-directed procedure.

This result does not follow trivially by inverting the typing rules as we have only one abstraction construct for the different function spaces. That is to say, a functional abstraction λx: A.e can receive any of the three types A⊸B, A→B, □A→B according to how the variable x is used in the body of e.

We also describe an alternative system which boasts modal types □(A) and !(A) and in which the function spaces can be defined as □(A)⊸B and !(A)⊸B. This system is slightly more expressive but does not provide any modality inference.

3.1 Types and subtyping

The type expressions of the calculus SLR are given by the following grammar.

A, B ::= | N  natural numbers
| X  type variable
| L(A)  lists over A
| T(A) binary trees labelled over A
| ∀X.A polymorphic type
| A⊸B linear function space
| □A→B nonlinear, modal function space
| A→B nonlinear, nonmodal function space
| A × B cartesian product
| A ⊗ B tensor product

The type formers × and ⊗ bind stronger than ⊸, →, □ − →−. All these type formers associate to the right so A⊸B⊸C reads A⊸(B⊸C).

The type former ∀X.A binds a free type variable X in A. A variable which is not bound by a ∀-quantifier is free. For example, the variables X, Y are free in ∀Z∀U.(X×Z)⊸U⊸Y, whereas Z, U are bound. Types are understood as equivalence classes modulo renaming of bound variables, e.g., ∀X.X⊸N ⊗ X = ∀Y.Y ⊸N ⊗ Y. If X is a free variable in the type expression B and A is another type then the capture free substitution of A for X in B written B[A/X] is defined by first choosing a representative for B all of whose bound variables are different from the free variables in A and then replacing all occurrences of X in B by A. For example

(∀X.Y ⊸X)[X ⊗ N / Y] = ∀X′.(X ⊗ N)⊸X′

We will now introduce a generic notation for the three function spaces which on the one hand facilitates the formulation of the typing rules and on the other hand allows us to introduce further modalities without having to change the typing rules and the type checking algorithm.

Definition 3.1.1 An aspect is a pair (l, m) where l ∈ {linear, nonlinear} and m ∈ {nonmodal, modal} with the exception (linear, modal) which is not an aspect.

The aspects are ordered componentwise by nonlinear <: linear and modal <: nonmodal.

Now we use the following generic notations for the three function spaces.

A ⟶^{a} B is A⊸B when a = (linear, nonmodal)
A ⟶^{a} B is A→B when a = (nonlinear, nonmodal)
A ⟶^{a} B is □A→B when a = (nonlinear, modal)

Our applications do not use the fourth theoretically possible aspect (linear, modal) because (semantically) modal use of a variable always implies nonlinear use. The typing rules, however, make perfect sense with modal, linear function space and it may be that subsequent use will be found for this aspect.

The ordering a <: a′ should be read as “a offers more capabilities than a′”. So a variable which admits nonlinear use is worth more than a variable restricted to linear use, hence nonlinear <: linear; a variable allowing for modal use, i.e., being recursed over, is worth more than a variable which may not be used in this way. Since the aspects refer to the argument positions, the corresponding function spaces are ordered in the opposite direction. A function of type □A→B places a stronger requirement on its input than a function of type A→B, so we have a subtyping A→B <: □A→B.

Definition 3.1.2 The subtyping relation between types is defined inductively by the following rules.

\frac{A′ <: A  B <: B′  a′ <: a}{A ⟶^{a} B <: A′ ⟶^{a′} B′}  (S-Arr)

\frac{A <: A′  B <: B′}{A × B <: A′ × B′}  (S-Prod)

\frac{A <: A′  B <: B′}{A ⊗ B <: A′ ⊗ B′}  (S-Tens)

\frac{A <: B}{∀X.A <: ∀X.B}  (S-All)

\frac{A <: B}{N→A <: N⊸B}  (S-Ax)

A <: A  (S-Refl)

Notice the contravariance of the first rule w.r.t. the ordering of aspects so that, e.g., A⊸B <: A→B and A→B <: □A→B.

The subtyping rule for function types contains (using S-Refl) the special cases A⊸B <: A→B <: □A→B and A⊸B <: □A⊸B <: □A→B.

The axiom S-Ax is optional. It expresses that variables of type N can be duplicated without sacrificing linearity. We give one interpretation which validates it and another which does not, but in exchange allows for stronger basic functions such as multiplication as a basic function of type N⊸N⊸N.

Remark 3.1.3 We remark at this point that without axiom S-Ax the distinction between A→B and □A→B is immaterial because we could then identify the two function spaces while preserving typability. Obviously, such identification would be unsound with S-Ax as we would then identify □N→A with N⊸A.

The following properties of subtyping are direct.

Proposition 3.1.4 – The following rule of transitivity is admissible

\frac{A <: B  B <: C}{A <: C}  (S-Trans)

– Subtyping is decidable by a syntax directed procedure.

3.2 Terms and typing rules

The expressions of SLR are given by the grammar

e ::= x  (variable)
| (e_{1} e_{2})  (application)
| λx: A.e  (abstraction)
| ΛX.e  (type abstraction)
| e[A]  (type application)
| ⟨e_{1}, e_{2}⟩  (pairing w.r.t. ×)
| e.1  first projection
| e.2  second projection
| e_{1} ⊗ e_{2}  (pairing w.r.t. ⊗)
| let e_{1}=x ⊗ y in e_{2} (⊗-elimination)
| let e_{1}=x in y  untyped abbreviation
| c  (constants)

Here x ranges over a countable set of variables and c ranges over the following set of constants with types as indicated:

0 : N S_{0}, S_{1} : N⊸N nil : ∀A.L(A) cons : ∀A.A⊸L(A)⊸L(A) leaf : ∀A.A⊸T(A) node : ∀A.A⊸T(A)⊸T(A) rec^{N} : ∀X.X⊸(□N→X⊸X)→□N→X case^{N} : ∀X.(X × (N⊸X) × (N⊸X))⊸N rec^{L} : ∀A.∀X.X⊸(□A→□L(A)→□L(A)→X⊸X)→□L(A)→X case^{L} : ∀A.∀X.X × (A⊸L(A)⊸X)⊸L(A)⊸X rec^{T} : ∀A.∀X.(□A→X)→(□A→□T(A)→□T(A)→X⊸X⊸X)→□T(A)→X case^{T} : ∀A∀X.((A⊸X) × (A⊸T(A)⊸T(A)⊸X))⊸T(A)⊸X

Remark 3.2.1 We remark that without nonlinear function space we would have to give the following slightly weaker types to the recursors.

rec^{N} : ∀X.X⊸□(□N→X⊸X)→□N⊸X rec^{L} : ∀A.∀X.X⊸□(□A→□L(A)→□L(A)→X⊸X)→□L(A)→X rec^{T} : ∀A.∀X.(□A→X)→□(□A→□T(A)→□T(A)→X⊸X⊸X)→□T(A)→X

This typing rules out step functions containing safe parameters of type N whereas such are allowed with the finer typing involving nonlinear function space using rule S-Ax. As said before, without rule S-Ax the distinction between the two function spaces can be given up.

3.2.1 Contexts

A typing judgement e : A read “e has type A” is made relative to a typing context which records typing and aspect assumptions for variables. For example, if e has type B in a context in which variable x has type A and linear, nonmodal aspect then λx: A.e has type A⊸B in the context with the assumption on x removed. This typically happens if x appears at most once in e and not as a subterm of an argument to a nonlinear function. If e can be given type B only under the stronger assumption that x has modal, nonlinear aspect then the weaker type □A→B will be assigned to the abstraction λx: A.e. Typically, this happens if x appears as a subterm of a term of type N, L(A), T(A) which is recursed on, i.e., appears as last argument to a recursor.

Definition 3.2.2 A context is a partial function from term variables to pairs of aspects and types. It is typically written as a list of bindings of the form x:^{a} A. If Γ is a context we write dom(Γ) for the set of variables bound in Γ. If x:^{a} A ∈ Γ then we write Γ(x) for A and Γ((x)) for the aspect a.

A context Γ is nonlinear if all its bindings are of nonlinear aspect.

Two contexts Γ, Δ are disjoint if the sets dom(Γ) and dom(Δ) are disjoint. If Γ and Δ are disjoint we write Γ, Δ for the union of Γ and Δ.

Notation. We write xˆ:A for the binding x:^{a} A where a is (linear, nonmodal), i.e., the maximal aspect. For type A and aspect a we define an aspect adj(a, A) as follows:

adj((nonlinear, nonmodal), N) = (linear, nonmodal) adj(a, A) = a in all other cases

This notation allows us to subsume rules S-Arr and S-Ax under the following more general rule.

\frac{A′ <: A B <: B′ a′ <: adj(a, A)}{A ⟶^{a} B <: A′ ⟶^{a′} B′}  (S-Arr-Ax)

For context Γ and aspect a we write Γ <: a to mean that Γ((x)) <: a for every variable x ∈ dom(Γ).

3.2.2 Typing rules

Definition 3.2.3 The subset of safe types is defined by the following grammar.

A, B ::= N | L(A) | T(A) | X | A⊸B | A × B | A ⊗ B

The safe types are those which may appear as result types of primitive recursive definitions. By definition, type variables range over safe types; accordingly polymorphic application will be restricted to safe types. This means that since our constructor functions for lists and trees are polymorphic we can only form lists and trees over safe type of entries. Semantically, we could justify lists and trees over non safe type of entries, but these would not be allowed as result types of recursive definitions.

The typing relation Γ ⊢ e : A between contexts, expressions, and types is defined inductively by the following rules. We suppose that all contexts, types, and terms occurring in such a rule are well-formed; in particular, if Γ, Δ or similar appears as a premise or conclusion of a rule then Γ and Δ must be disjoint for the rule to be applicable. The typing rules described here are the affine ones from [18].

\frac{x ∈ dom(Γ)}{Γ ⊢ x : Γ(x)}  (T-Var)

\frac{Γ ⊢ e : A A <: B}{Γ ⊢ e : B}  (T-Sub)

\frac{Γ, x:^{a} A ⊢ e : B}{Γ ⊢ λx: A.e : A ⟶^{a} B}  (T-Arr-I)

\frac{Γ, Δ_{1} ⊢ e_{1} : A ⟶^{a} B Γ, Δ_{2} ⊢ e_{2} : A Γ nonlinear Γ, Δ_{2} <: a}{Γ, Δ_{1}, Δ_{2} ⊢ (e_{1} e_{2}) : B}

(T-Arr-E)

\frac{Γ ⊢ e : A X not free in Γ}{Γ ⊢ ΛX.e : ∀X.A}  (T-All-I)

\frac{Γ ⊢ e : ∀X.A S safe}{Γ ⊢ e[S] : A[S/X]}  (T-All-E)

\frac{Γ ⊢ e_{1} : A_{1} Γ ⊢ e_{2} : A_{2}}{Γ ⊢ ⟨e_{1}, e_{2}⟩ : A_{1} × A_{2}}  (T-Prod-I)

\frac{Γ ⊢ e : A_{1} × A_{2} i ∈ \{1, 2\}}{Γ ⊢ e.i : A_{i}}  (T-Prod-E)

\frac{Γ, Δ_{1} ⊢ e_{1} : A_{1} Γ, Δ_{2} ⊢ e_{2} : A_{2} Γ nonlinear}{Γ, Δ_{1}, Δ_{2} ⊢ e_{1} ⊗ e_{2} : A_{1} ⊗ A_{2}}  (T-Tens-I)

\frac{Γ, Δ_{1} ⊢ e_{1} : A_{1} ⊗ A_{2} Γ, Δ_{2}, x:^{a_{1}} A_{1}, y :^{a_{2}} A_{2} ⊢ e_{2} : B Γ nonlinear Δ_{1} ≤ a_{1} ∧ a_{2}}{Γ, Δ_{1}, Δ_{2} ⊢ let e_{1}=x ⊗ y in e_{2} : B}  (T-Tens-E)

\frac{Γ, Δ_{1} ⊢ e_{1} : A Γ, Δ_{2}, x:^{a} A ⊢ e_{2} : B  Γ nonlinear Δ_{1} ≤ a}{Γ, Δ_{1}, Δ_{2} ⊢ let e_{1}=x in e_{2} : B} (T-Let)

\frac{c : A}{Γ ⊢ c : A}  (T-Const)

3.2.3 Examples

The function sq. We have y: N, q: N ⊢ S_{0}q : N by rules T-Var, T-Const, and T-Arr-E with a = (nonmodal, linear), Γ = Δ_{1} = ∅, Δ_{2} = y: N, q: N. Similarly, we get y: N, q: N ⊢ S_{0}(S_{0} q) : N. Therefore, t =_{def} λy: N.λq: N.S_{0}(S_{0} q) : N⊸N⊸N, and t : □N→N→N by T-Sub. Now x: □N ⊢ rec^{N} x 1 t : N by T-Const and three instances of T-Arr-E; the first with a = (modal, nonlinear). Finally, T-Arr-I gives sq =_{def} λx: N.rec^{N} x 1 t : □N→N.

Now, x: □N ⊢ sq x : N and thus x: □N ⊢ sq (sq x)N by rule T-Arr-E with a = (modal, nonlinear).

However, as expected, we cannot define an exponentially growing function by

exp =_{def} λx: N.rec^{N} 1 (λz: N.sq) x

as typechecking exp in the empty context fails. The reason is that the principal type of the step function λz: N.sq is N⊸□N→N which is a subtype of □N→□N→N, but not of □N→N⊸N as would be required in order to typecheck the application.

Iterator from recursor We can define

it^{N} =_{def} ΛX.λg: X.λh: X⊸X.λn: N.rec^{N} g (λx: N.λy: X.h x)

and we obtain the typing

it^{N} : ∀X.X⊸(X⊸X)→□N→X

If-then-else We can define test for zero by

ifz_{1} =_{def} ΛX.λn: N.λx: X.λy: X.case^{N}[X]⟨x, ⟨λn: N.y, λn: N.y⟩⟩

and we obtain the typing

ifz_{1} : ∀X.N⊸X⊸X⊸X

Notice that although y appears twice in the body of the case-expression it is counted linearly according to rule T-Prod-E. This does not happen with our newly defined if-expression: The function

F =_{def} λf: N⊸N.λx: N.ifz_{1}[N] x (f 0) (f 1)

gets the type

F : (N⊸N)→N⊸N

according to rule T-Arr-E. Without expanding the definition of ifz_{1} and merely looking at its type the system does not recognise that in fact f will be applied at most once. If we desire such behaviour we should use the cartesian product in the typing of if-then-else:

ifz_{2} = ΛX.λn: N.λp: X × X.case^{N}[X]⟨p.1, ⟨λn: N.p.2, λn: N.p.2⟩⟩ :
∀X.N⊸(X × X)⊸X

Then we obtain the more refined typing for F:

F =_{def} λf: N⊸N.λx: N.ifz_{2}[N] x ⟨f 0, f 1⟩ : (N⊸N)⊸N⊸N

We emphasise how the presence of polymorphism and cartesian product types enables such general definitions. In the earlier system described in [18] only ifz_{1} could be formulated and only for fixed result type X.

We also remark that by rule T-All-E the result types of case distinctions are bound to be safe although this is not strictly necessary from the point of view of soundness. Indeed, case distinction for arbitrary result type can be defined schematically; for example, if A = □N→N then we can define

ifz^{A} = λn: N.λp: A × A.λm: N.ifz_{2}[N] n ⟨p.1 n, p.2 n⟩ : N⊸(A × A)⊸A

There would be no principal obstacle against introducing a second kind of type variables ranging over arbitrary types, but we have refrained from doing so in order to keep the syntax manageable.

Ordinary safe recursion. Let us see how ordinary safe recursion can be recovered in SLR. Suppose we are given g : □N^{m}→N^{n}⊸N and h : □N^{m+1}→N^{n+1}⊸N and we wish to define f : □N^{m+1}→N^{n}⊸N such that, semantically,

\begin{matrix}f(0, x⃗; y⃗) = g(x⃗; y⃗) \\ f(x, x⃗; y⃗) = h(x, x⃗; f(⌊\frac{x}{2}⌋ , x⃗; y⃗), y⃗) \end{matrix}

Let Γ be the context which binds the x-variables to N with aspect (modal, nonlinear) and the y-variables to N with aspect (nonmodal, nonlinear) and write

base =_{def} g(x⃗; y⃗)
step =_{def} λx: N.λy: N.h(x, x⃗; y, y⃗)

for base case and step function of the above definition. We have

Γ ⊢ base : N

Γ ⊢ step : □N→N⊸N

Since Γ is a nonlinear context, the side conditions to rule T-Arr-E are satisfied in the application rec^{N}[N] base step and we have

Γ ⊢ rec^{N}[N] base step : □N→N

Finally, rule T-Arr-I gives

∅ ⊢ λx: N.λx⃗: N^{n}.λy⃗: N.rec^{N}[N] base step : □N^{m+1}→N^{n}→N

and we finally get the desired typing □N^{m+1}→N^{n}⊸N using S-Ax and T-Sub. We remark that the type inference algorithm implicit in the proof of Theorem 3.3.7 below performs all these steps automatically, and in the implementation we merely type in the term and its type will be computed.

Bounded recursion on notation With higher result type it is relatively straightforward to translate bounded recursion on notation into safe recursion and thus to show that all PTIME-functions are definable in SLR.

We have the following correspondence [3].

Proposition 3.2.4 If f(x⃗) is definable in F then we can find terms u_{f} : □N^{n}→N and v_{f} : □N→N^{n}⊸N such that (in the set-theoretic semantics)

v_{f}(t; x⃗) = f(x⃗)

whenever t ≥ u_{f}(x⃗; ).

Proof. By induction on a definition of f in F. The crucial step is bounded recursion on notation. Suppose that

\begin{matrix}f(0, x⃗) = g(x⃗) \\ f(x, x⃗) = h(x, x⃗, f(⌊\frac{x}{2}⌋ , x⃗)) \end{matrix}

and that f(x, x⃗) ≤ k(x, x⃗).

By the induction hypothesis we have

\begin{matrix}f(0, x⃗) = v_{g}(t; x⃗) \\ f(x, x⃗) = v_{h}(t; x, f(⌊\frac{x}{2}⌋ , x⃗), x⃗) \end{matrix}

provided that

t ≥ max(u_{g}(x⃗), u_{h}(x, v_{k}(u_{k}(x, x⃗; ); x, x⃗), x⃗; ))

Now define v_{f} by safe recursion with result type N⊸N as

v_{f}(0; x, x⃗) = 0
v_{f}(t; x, x⃗) = if x=0
then v_{g}(t; x⃗)
else v_{h}(t; x, v_{f}(⌊\frac{t}{2}⌋; ⌊\frac{x}{2}⌋ , x⃗), x⃗)

Induction then shows that f(x, x⃗) = v_{f}(t; x, x⃗) whenever

t ≥ max(u_{g}(x⃗), u_{h}(x, v_{k}(u_{k}(x, x⃗; ); x, x⃗), x⃗; )) + |x|

Defining v_{f} sufficiently large so as to majorise the right hand side then yields the result. □

We remark that the above definition would not have been possible without rule S-Ax. If we wanted to show that even the system without duplication at ground type defines all PTIME-functions it seems that we would have to go down one level of abstraction and encode polynomially bounded Turing machines directly. The details remain unexplored.

Tree recursion Here is an iterator for trees which does not provide access to the recursion variable itself but only to results of recursive calls.

it^{T} =_{def} ΛA.ΛX.λg: □A→X.λh: □A→X⊸X⊸X.
rec^{T}[A][X] g (λa: A.λl: T(A).λr: T(A).h a) :
∀A.∀X.(□A→X)→(□A→X⊸X⊸X)→□T(A)→X

Here is a definition of a function of type ∀A.□T(A)→T(A) which hereditarily swaps left and right subtrees:

treerev =_{def} ΛA.It^{T}[A][T(A)] (leaf[A]) (λa: A.λl: T(A).λr: T(A).node[A] a r l)

We have

treerev(leaf[A] a) = leaf[A] a

treerev(node[A] a l r) = node[A] a (treerev r) (treerev l)

Insertion sort Finally, we give a sugared version of the familiar insertion sort algorithm. Suppose that we have a function ≤: □N→N⊸N. We define an insertion function insert : □L(N)→□N→ L(N)⊸L(N) such that insert(l, a, l′) inserts a into l′ in the correct place assuming that l is longer than l′ and that l′ is already sorted. The extra parameter l is used to “drive” the recursion enabling us to use l′ in a linear way:

insert([ ], a, l′) = [ ]
insert(x :: y, a, a′ :: l′) = if a ≤ a′
then a :: a′ :: l
else a′ :: insert(y, a, l′)

This definition can be formalised using the higher-typed recursion operator

rec^{L}[N][L(N)⊸L(N)]

The sorting function of type □L(N)→L(N) is then defined by

sort([ ]) = [ ]

sort(a :: l) = insert(l, a, sort(l))

Here rec^{L}[N][L(N)] has been used.

The correctness of this code hinges on the fact that sort(l) is not longer than l, hence the particular instance of insert behaves correctly.

The usual recursive definition of insert without extra parameter would yield a function of type □L(N)→□N→L(N) which cannot be iterated due to its modal type.

This use of extra parameters to drive recursions is intrinsic to the pattern of safe recursion and already appears in Bellantoni-Cook’s proof that all polynomial time functions are definable in their first-order system. The inconvenience caused by this necessity is somewhat palliated by the presence of higher result types as can be seen from the above definition of insert which would be difficult to define with rec^{L}[N][L(N)] alone.

Splitting and quicksort. In order to illustrate the use of tensor products we show how to split a list in two components. Let test : N⊸N be a variable. We define a function

split : □N→L(N)⊸L(N) ⊗ L(N)

such that when

split(n, l) = (yes, no)

then yes is a list containing those elements a of l for which test(a) = 0 and no contains the other ones provided that the length of l is smaller or equal to |n|. Otherwise the behaviour of split is undefined. The formal definition of split is as follows.

split = λtest: N⊸N.it^{N}[L(N)⊸L(N) ⊗ L(N)] (λl: L(N).nil[N] ⊗ nil[N]) (λspl: L(N)⊸L(N) ⊗ L(N).λl: L(N).

case^{L}[L(N) ⊗ L(N)] (nil[N] ⊗ nil[N], λhd: N.λtl: L(N).

let spl l′=yes ⊗ no in

ifz[L(N) ⊗ L(N)] (test x) (cons[N] x yes ⊗ no, yes ⊗ cons[N] x no))) l

From this splitting function one can define a sorting function based on the quicksort algorithm. It seems difficult to do this without using the tensor product.

3.3 Syntactic metatheory

The expression e_{1}[e_{2}/x] denotes the capture-free substitution of e_{2} for x in e_{1}. The proofs of the following two propositions are straightforward inductions on derivations. Proposition 3.3.1 The following rules are admissible.

\frac{Γ ⊢ e : A}{Γ, Δ ⊢ e : A}  (T-Weak)
\frac{Γ, x:^{a} A ⊢ e : C  B <: A  b ≤ a}{Γ, x:^{b} B ⊢ e : C}  (T-Weak’)

\frac{Γ, x:^{a} A ⊢ e : C  x not free in e}{Γ ⊢ e : C}  (T-Strength)
\frac{Γ, Δ_{1}, x:^{a} A ⊢ e_{1} : B Γ, Δ_{2} ⊢ e_{2} : B  Γ nonlinear  Γ, Δ_{2} <: a}{Γ, Δ_{1}, Δ_{2} ⊢ e_{1}[e_{2}/x] : B}

(T-Subst)

Proposition 3.3.2 (Generation of typing) i. If Γ ⊢ x : A then x ∈ dom(Γ) and

Γ(x) <: A. ii. If Γ ⊢ λx: A.e : C then Γ, x:^{a} A ⊢ e : B for some a and B such that A ⟶^{a} B <: C. iii. If Γ ⊢ (e_{1} e_{2}) : B then we can find a decomposition Γ = Γ′, Δ_{1}, Δ_{2}, a type A and

an aspect a such that Γ′, Δ_{1} ⊢ e_{1} : A ⟶^{a} B and Γ′, Δ_{2} ⊢ e_{2} : A and Γ′ is nonlinear. x:^{a′} X in Γ′, Δ_{1} then a′ <: adj(a, A).

iv. If Γ ⊢ ΛX.e : B then B = ∀X.C for some X not free in Γ and Γ ⊢ e : C. v. If Γ ⊢ e[S] : C then there exists S, B with S safe such that Γ ⊢ e : ∀X.B and

B[S/X] <: C and X not free in Γ. vi. If Γ ⊢ ⟨e_{1}, e_{2}⟩ : C then C = C_{1} × C_{2} and Γ ⊢ e_{i} : C_{i}. vii. If Γ ⊢ e.i : C_{i} for i = 1 or i = 2 then we can find C_{3−i} such that Γ ⊢ e : C_{1} × C_{2}. viii. If Γ ⊢ e_{1} ⊗ e_{2} : C_{1} ⊗ C_{2} then we can find a decomposition Γ = Γ′, Δ_{1}, Δ_{2} with Γ′

nonlinear and Γ′, Δ_{i} ⊢ e_{i} : C_{i}. ix. If Γ ⊢ let e_{1}=x ⊗ y in e_{2} : C then we can find a decomposition Γ = Γ′, Δ_{1}, Δ_{2} with Γ′

nonlinear and types A_{1}, A_{2} such that Γ′, Δ_{1} ⊢ e_{1} : A_{1} ⊗A_{2} and Γ′, Δ_{2}, x:^{a_{1}} A_{1}, x:^{a_{2}} A_{2} ⊢

e_{2} : C and Δ_{1}(x) ≤ a_{1}, Δ_{1}(x) ≤ (a_{2}) for each x ∈ dom Δ_{1}.

x. If Γ ⊢ let e_{1}=x in e_{2} : C then we can find a decomposition Γ = Γ′, Δ_{1}, Δ_{2} with Γ′ nonlinear and a types A such that Γ′, Δ_{1} ⊢ e_{1} : A and Γ′, Δ_{2}, x:^{a} A ⊢ e_{2} : C and Δ_{1}(x) ≤ a for each x ∈ dom Δ_{1}.

xi. If Γ ⊢ c : A then τ(c) <: A.

Let Γ, Δ be contexts. We say that Δ is a subcontext of Γ if Δ(x) = Γ(x) for each x ∈ dom(Δ). Notice that we do not require Γ((x)) = Δ((x)).

Definition 3.3.3 Let Δ, Δ′ be contexts. We write Δ ≪ Δ′ to mean that Δ((x)) ≤ Δ′((x)) for each x : dom(Δ′).

The following is immediate.

Proposition 3.3.4 If Δ ≪ Δ′ and Δ′ ⊢ e : A then Δ ⊢ e : A.

Definition 3.3.5 Let Γ be a context and e be an expression. Say that typechecking e under Γ fails if Δ ⊬ e : A for all subcontexts Δ of Γ and types A. Say that (Δ, A) is a principal solution for the typechecking problem (Γ, e) if

– Δ is a subcontext of Γ

– Δ ⊢ e : A

– whenever Δ′ ⊢ e : A′ and Δ′ is a subcontext of Γ then Δ′ ≪ Δ.

In other words, typechecking (Γ, e) fails if we cannot assign a type to e even if we allow to omit variables from Γ and to assign arbitrary aspects to the leftover bindings. A principal solution (Δ, A) is obtained by omitting from Γ as many variables as possible and to relax the required bindings as little as possible so as to obtain a type for e. One might think that the thus obtained type isn’t necessarily the best possible or in other words that one could perhaps obtain a smaller type by further relaxing the context. Fortunately, our type system is such that this cannot happen. The reason is that relaxing the context (by omitting variables or relaxing aspects) can only help to typecheck a term at all, but not to decrease the type of an already typable term.

Notice that the aspects in Γ do not affect the solvability of a typechecking problem (Γ, e). In fact, we only use Γ to ascribe types to the variables.

Lemma 3.3.6 Suppose that (Γ, x: A , e) has a principal solution (Δ, C) and assume that B <: A.

If x ∉ dom(Δ) then (Γ, x: B , e) has principal solution (Δ, C). If Δ = Δ′, x:^{a} A then (Γ, x: B , e) has principal solution (Θ, x:^{b} B , D) for some D <: C, Δ′ ≪ Θ and a ≤ b.

Proof. The first case is direct from T-Strength. For the second case we first notice that by T-Weak’ and T-Strength the problem (Γ, x: B , e) must have a solution of the form (Θ, x:^{b} B , D). The assumption together with rule Weak’ gives Δ′, x:^{a} B ⊢ e : C. Principality then yields Δ′ ≪ Θ and a ≤ b and C ≤ D. □

Theorem 3.3.7 Let Γ be a context and e an expression. Either typechecking e under Γ fails or there exists a principal solution for the typechecking problem (Γ, e). Moreover, there exists a syntax-directed procedure which decides whether a principal solution exists and computes it in the affirmative case.

Proof. By induction on the structure of e.

Case e = c. The principal solution is (∅, τ(c)). Clearly, ∅ ⊢ c : τ(c) . If Γ ⊢ c : A then generation of typing yields τ(c) <: A and Γ nonlinear. Thus Γ ≪ ∅.

Case e = x. If x ∉ dom(Γ) then typechecking x under Γ obviously fails. Otherwise, we claim that ({xˆ:Γ(x)}, Γ(x)) is a principal solution. To see this, assume Δ ⊢ x : A for some subcontext Δ of Γ. Then by generation of typing we must have Δ(x) <: A and Δ = Δ′, x:^{a} X. Now, since Δ is a subcontext of Γ we must have X = Δ(x) = Γ(x) ≤ A, hence Δ ≪ {xˆ:Γ(x)}.

Case e = λx: A.e′. We may assume w.l.o.g. that x is not in dom(Γ). If Δ ⊢ e : C for some subcontext Δ of Γ then by generation of typing we have Δ, x:^{a} A ⊢ e′ : B for some B,a and A ⟶^{a} B <: C. Therefore, typechecking (Γ, e) fails if typechecking (Γ, x: A , e′) fails. Assume that this is not the case and let (Δ, B) be a principal solution of (Γ, x: A, e′). We have two cases to distinguish.

i. x ∉ dom(Δ). Then (Δ, A⊸B) is a principal solution.

ii. Δ = Δ_{1}, x:^{a} A where x ∉ dom(Δ_{1}). Then (Δ_{1}, A ⟶^{a} B) is a principal solution.

In case i we have Δ, x: A ⊢ e′ : B by T-Weak and thus Δ ⊢ e : A⊸B. In case ii Δ_{1} ⊢ e : A ⟶^{a} B is immediate from T-Arr-I. For principality assume that Δ′ ⊢ e : C for some subcontext Δ′ of Γ. By the above analysis we get Δ′, x:^{a′} A ⊢ e′ : B′ for some a′, B′ with A ⟶^{a′} B′ <: U. The induction hypothesis gives us Δ′, x:^{a′} A ≪ Δ and B <: B′. If x is not in dom(Δ) then we also have Δ′ ≪ Δ. Thus, A⊸B <: A⟶^{a′}B′ <: C. If Δ = Δ_{1}, x:^{a} A then we get Δ_{1} ≪ Δ and a′ ≤ a, hence A ⟶^{a} B <: A ⟶^{a′} B′ <: C.

Case e = (e_{1} e_{2}). By generation of typing we know that if either (Γ, e_{1}) or (Γ, e_{2}) fails then the problem (Γ, e_{1} e_{2}) fails. So assume that (Δ_{1}, A_{1}) and (Δ_{2}, A_{2}) are principal solutions to these latter problems. Generation of typing gives us that A_{1} = B ⟶^{a} C for some B, C, a. If A_{2} is not a subtype of B then typechecking e_{1} e_{2} obviously fails.

We claim that the principal solution is (Δ, C) where Δ is the largest (w.r.t. ≪ ) context satisfying the following requirements.

i. dom(Δ) = dom(Δ_{1}) ∪ dom(Δ_{2})

ii. x ∈ dom(Δ_{1}) implies Δ((x)) ≤ Δ_{1}((x))

iii. x ∈ dom(Δ_{2}) implies Δ((x)) ≤ Δ_{2}((x))

iv. x ∈ dom(Δ_{2}) implies Δ((x)) ≤ adj(B, a)

v. x ∈ dom(Δ_{1}) ∩ dom(Δ_{2}) implies that Δ((x)) is nonlinear

To see that Δ ⊢ e : C we decompose Δ as Θ, Λ_{1}, Λ_{2} where Λ_{1} is Δ restricted to dom(Δ_{1}) \ dom(Δ_{2}) and Λ_{2} is Δ restricted to dom(Δ_{2}) \ dom(Δ_{1}) and Θ is the rest, i.e., Δ restricted to dom(Δ_{1}) ∩ dom(Δ_{2}). From (v) we know that Θ is nonlinear. Furthermore, if x:^{a′} X in Θ, Λ_{2} then a′ ≤ adj(a, B) by (iv) so rule T-Arr-E yields Δ ⊢ e : C.

For principality assume that Δ′ ⊢ e : C′. Generation of typing yields a decomposition Δ′ = Θ, Λ_{1}, Λ_{2} where Θ is nonlinear and Θ, Λ_{1} ⊢ e_{1} : B′ ⟶^{a′} C′ and Θ, Λ_{2} ⊢ e_{2} : B′ and y :^{a′′} Y ∈ Θ, Λ_{2} implies a′′ ≤ a′. The induction hypothesis gives Θ, Λ_{1} ≪ Δ_{1} and Θ, Λ_{2} ≪ Δ_{2} and B ⟶^{a} C <: B′ ⟶^{a′} C′ and B <: B′. By generation of subtyping we get a′ ≤ adj(a, B) and C <: C′.

So to show Δ′ ≪ Δ it is enough to show that Δ′ restricted to dom(Δ) meets requirements (ii–v) above.

From the induction hypothesis we get Δ′((x)) ≤ Δ_{1}((x)) if x ∈ dom(Δ_{1}) and Δ′((x)) ≤ Δ_{2}((x)) if x ∈ dom(Δ_{2}). Furthermore, if x ∈ dom(Δ_{2}) then Δ′((x)) = Θ, Λ_{2}((x)) ≤ a′ ≤ a. Finally, x ∈ dom(Δ_{1}) ∩ dom(Δ_{2}) implies x : dom(Θ) as Λ_{1}, Λ_{2} are disjoint. Hence Δ′((x)) = Θ((x)) is nonlinear. Thus Δ′ ≪ Δ by definition of Δ.

Case e = ΛX.e′ . By bound renaming we may assume w.l.o.g. that type variable X does not occur in Γ. Let (Δ, A) be a principal solution of problem (Γ, e′). By assumption and the fact that Δ is a subcontext of Γ we know that X does not occur in Δ and so Δ ⊢ e : ∀X.A. Principality follows directly from the induction hypothesis and Prop. 3.3.2.

Case e = e′[S]. If S is not safe then there is no solution. For (Γ, e) to be solvable it is necessary that (Γ, e′) has a principal solution of the form (Δ, ∀X.A). In this case, (Δ, A[S/X]) is a principal solution for (Γ, e). Indeed, if Δ′ ⊢ e : C then by generation of typing we must have Δ′ ⊢ e′ : ∀X.B and B[S/X] <: C. The induction hypothesis gives Δ ≪ Δ′ and ∀X.A <: ∀X.B. Therefore, A[S/X] <: B[S/X] <: C.

Case e = (e_{1}, e_{2}). Let (Δ_{1}, A_{1}) and (Δ_{2}, A_{2}) be the principal solutions of (Γ, e_{1}) and (Γ, e_{2}) respectively. If none exist then typechecking (Γ, e) fails. Otherwise, we claim that the principal solution is (Δ, A_{1} × A_{2}) where Δ is the greatest lower bound w.r.t. ≪ of Δ_{1} and Δ_{2}.

It is clear from the definition that Δ ⊢ (e_{1}, e_{2}) : A_{1} × A_{2}. On the other hand, if Δ′ ⊢ e : A′ then A′ = A′_{1} × A′_{2} and Δ′ ⊢ e_{i} : A′_{i}. The induction hypothesis yields Δ′ ≪ Δ_{1} and Δ′ ≪ Δ_{2}. Hence, Δ′ ≪ Δ by construction of Δ. The i.h. also yields A_{i} <: A′_{i} hence A_{1} × A_{2} <: A′ by rule S-Prod.

Case e = e′.i. For (Γ, e) to typecheck it is necessary that (Γ, e′.i) has a principal solution of the form (Δ, A_{1} × A_{2}). In this case (Δ, A_{i}) is a principal solution for (Γ, e).

Case e = e_{1}⊗e_{2}. Let (Δ_{i}, A_{i}) be principal solutions for (Γ, e_{i}). If none exist typechecking fails. We claim that the principal solution for (Γ, e) is given by (Δ, A_{1} ⊗ A_{2}) where Δ is the largest w.r.t. ≪ subcontext of Γ satisfying

i. dom(Δ) = dom(D_{1}) ∪ dom(Δ_{2}),

ii. Δ((x)) ≤ Δ_{i}((x)) for i = 1, 2 and x ∈ dom(Δ_{i}),

iii. Δ((x)) nonlinear when x ∈ dom(Δ_{1}) ∩ dom(Δ_{2}).

It is clear from Prop. 3.3.4 and T-Tens-I that Δ ⊢ e_{1} ⊗ e_{2} : A_{1} ⊗ A_{2}. Conversely, if Δ′ ⊢ e_{1} ⊗ e_{2} : A′_{1} ⊗ A′_{2} then Δ′ = Θ, Λ_{1}, Λ_{2} where Θ is nonlinear and Θ, Λ_{i} ⊢ e_{i} : A′_{i}. The induction hypothesis gives A_{i} ≪ A′_{i} and Θ, Λ_{i} ≪ Δ_{i}. So, Δ′ meets requirements i–iii and hence Δ′ ≪ Δ.

Case e = let e_{1}=x in e_{2}. We may assume that x ∉ dom(Γ). If (Γ, e) has a solution

at all then generation of typing gives us Θ^{0}, Λ_{1}^{0} ⊢ e_{1} : A^{0} and Θ^{0}, Λ_{2}^{0}, x:^{a^{0}} A^{0} ⊢ e_{2} : B^{0} for subcontexts Θ^{0}, Λ_{1}^{0}, Λ_{2}^{0} of Γ and arbitrary types and aspect A^{0}, B^{0}, a^{0}. Therefore, the typechecking problem (Γ, e_{1}) must have a solution, say (Δ_{1}, A) where A ≤ A^{0} and Θ^{0}, Λ_{1}^{0} ≪

Δ_{1}. Rule Weak’ then gives Θ^{0}, Λ_{2}^{0}, x:^{a^{0}} A ⊢ e_{2} : B^{0}, so (Γ, x: A , e_{2}) must also have a principal solution, say (Δ_{2}, C). We claim that the principal solution of (Γ, e) is (Δ, C) where Δ is the largest w.r.t. ≪ subcontext of Γ satisfying the following requirements.

i. dom(Δ) = dom(Δ_{1}) ∪ dom(Δ_{2}) \ {x},

ii. Δ((y)) nonlinear for y ∈ dom(Δ_{1}) ∩ dom(Δ_{2})

iii. Δ((y)) ≤ Δ_{1}((y)) for all y ∈ dom(Δ_{1}),

iv. Δ((y)) ≤ Δ_{2}((y)) for all y ∈ dom(Δ_{2}) \ {x},

v. if x ∈ dom(Δ_{2}) then Δ((y)) ≤ Δ_{2}((x)) for all y ∈ dom(Δ_{2}) \ {x}.

It follows from Weak and Subst that Δ ⊢ e : C. Principality is immediate from the analysis above and the induction hypothesis.

Case e = let e_{1}=x_{1} ⊗ x_{2} in e_{2}. Analogous to the previous case. □

Corollary 3.3.8 Given Γ, e it is decidable whether there exists a type A such that Γ ⊢ e : A.

Proof. Let (Δ, A) be the principal solution of the problem (Γ, e). If none exists then clearly no such A can exist. Otherwise check whether Γ ≪ Δ. If yes then by context subsumption we also have Γ ⊢ e : A; otherwise Γ ⊢ e : B is impossible by principality.

□

Corollary 3.3.9 Let Γ be a context and e be a term. Either e is not typable in Γ or there exists a typing Γ ⊢ e : A such that whenever Γ ⊢ e : A′ then A <: A′.

Proof. Let (Δ, A) be the principal solution of (Γ, e). If Γ ≪ Δ then A has the required property, otherwise e is not typable. □

3.4 Comparison with other systems

The modal part of SLR builds upon the modal lambda calculi developed by Pfenning et al. [11, 34]. The new feature of SLR as compared to these latter systems is the absence of special term formers related to modality. Aspects of functions are inferred automatically; explicit coercions are avoided by way of a subtyping.

The idea of restricting modalities to the argument position of function types arises Pfenning and Cervesato’s Linear Logical Framework [6] and also in Plotkin’s formulation of linear system F [37]. These systems do not use subtyping and have two kinds of abstraction and application for linear and nonlinear function spaces, respectively.

A problem we have not studied for SLR is automatic inference of type annotations to functional abstractions and polymorphic applications. Concerning annotations in abstractions, an obstacle against a straightforward generalisation of Hindley-Milner type inference to a system like SLR is that untyped terms need not have principal types. For example, the term e = λx.λf.fx can be given any of the following three incomparable type schemes:

e : A⊸(A⊸B)⊸B
e : A→(A⊸B)⊸B
e : □A→(□A→B)⊸B

These three are subsumed under the following general pattern:

e : A ⟶^{a} (A⟶^{a})⊸B

where a is an arbitrary aspect. For purely linear lambda calculus Wadler et. al. [42, 41] have described an inference algorithm which assigns type schemes containing variable aspects (called use variables in loc. cit.). It could be possible to extend their algorithm to SLR; the details have not been explored.

Automatic inference of type applications would for example allow one to omit type arguments to constructor functions like cons, leaf and to the recursors as well as derived patterns like iterators.

Inference of polymorphic type applications has been shown to be undecidable by Pfenning [32] for system F. However, since the polymorphism in SLR is predicative (type arguments to polymorphic function cannot contain polymorphic types as those are not safe.) Pfenning’s result does not apply directly and there is still some hope.

Another very promising approach has recently been put forward by Pierce and Turner [35]. In their system the user is required to give the type of a term to be checked and also to give the type of all local definitions, i.e., to write let x=e_{1} : A in e_{2}. In exchange, type annotations to functional abstractions and type applications can be omitted.

3.5 Set-theoretic semantics

The calculus SLR has an intended set-theoretic interpretation which in particular associates a function ℕ ^{}ℕ to a closed term of type □N→N. The central result of this thesis is that all these functions are computable in polynomial time.

The purpose of this set-theoretic interpretation is merely to specify the meaning of the terms. In order to effectively compute these meanings one has two choices.

Either one views the terms as ordinary functional programs by disregarding modality and linearity information and evaluates them using standard evaluation techniques for functional programs. This is what we have done in our prototype implementation. Notice that we will not prove that such evaluation can be performed in polynomial time and, at least in general this will not be true since already expansion of higher-order definitions (i.e., βη-reduction in simply-typed lambda calculus) takes superexponential time.

A more ambitious and arguably more efficient way of computing meanings would be to extract a compiler from the soundness proof to be given and use the latter to generate PTIME code from first-order terms in SLR. A practical implementation of this procedure is intended, but no concrete results are available at the time of writing.

Notation. If A, B are sets we write A × B and A→B for their cartesian product and function space. If A is a set let L(A) stand for the set of finite lists over A constructed by nil and cons. If A is a set let T(A) be the set of binary A-labelled trees over A inductively defined by leaf(a) ∈ T(A) when a ∈ A and node(a, l, r) ∈ T(A) when a ∈ A and l, r ∈ T(A). Let U be a set which contains ℕ and is closed under ×, →, L, T.

If η is a partial function from type variables to U and A is a type then we define a set [[A]]η by

[[X]]η = η(X) [[N]]η = ℕ [[L(A)]]η = L([[A]]η) [[T(A)]]η = T([[A]]η)

[[A ⟶^{a} B]]η = [[A]]η→[[B]]η

[[∀X.A]]η = ∏_{B∈U}[[A]]η[X ↦ B]

[[A × B]]η = [[A ⊗ B]]η = [[A]]η × [[B]]η

If A is a closed type then [[A]]η is independent of η and we thus write [[A]] in this case. Notice that the interpretation of a safe type always lies in U. Also notice that if A <: B then [[A]]η = [[B]]η.

To each constant c : A we associate an element [[c]] ∈ [[A]] by the clauses given in the introduction.

The interpretation of terms is w.r.t. an partial function η which maps type variables to elements of U and term variables to arbitrary values:

[[x]]η  = η(x)
[[λx: A.e]]  = λv∈ [[A]].[[e]]η[x↦v]
[[e_{1} e_{2}]]η  = [[e_{1}]]η([[e_{2}]]η)
[[ΛX.e]]η  = λA∈ U.[[e]]η[X ↦ A]
[[e[A]]]η  = [[e]]η([[A]]η)
[[⟨e_{1}, e_{2}⟩]]η  = ([[e_{1}]]η, [[e_{2}]]η)
[[e.i]]η  = v_{i} where [[e]]η = (v_{1}, v_{2})
[[e_{1} ⊗ e_{2}]]η  = ([[e_{1}]]η, [[e_{2}]]η)
[[let e_{1} = x ⊗ y in e_{2}]]η = [[e_{2}]]η[x ↦ v_{1}, y ↦ v_{2}] where [[e_{1}]]η = (v_{1}, v_{2})
[[c]]η  = [[c]]

The purpose of this set-theoretic semantics is to specify the meaning of SLR terms. It allows us to do without any notion of term rewriting or evaluation. Of course, by directing the defining equations of the recursors one obtains a normalising rewrite system which computes the set-theoretic meaning of first-order functions. However, there is no reason why such a rewrite system should terminate in polynomial time. In order to obtain polynomial time algorithms from SLR-terms one must rather study the soundness proof we give and from it extract a compiler which transforms SLR-programs of first-order type into polynomial time algorithms. That this is possible in principle follows from the fact that our soundness proof is constructive; a practical implementation, however, must await further work.

3.6 Reduction

We do not define a reduction relation for SLR-terms but will rather specify their meaning by a set-theoretic model. Nevertheless, it seems worth discussing possible reduction rules for SLR. If the rule S-Ax is omitted from SLR then the obvious reduction relation based on βreduction for function spaces ((λx: A.e)e′ ⇝ e[e′/x]) and rules like let e_{1}⊗e_{2}=x_{1}⊗x_{2} in e_{3} ⇝ e_{3}[e_{1}/x_{1}, e_{2}/x_{2}] satisfies the subject reduction property, i.e., if e ⇝ e′ and Γ ⊢ e : A then Γ ⊢ e′ : A. This follows directly from T-Subst. However, with rule S-Ax subject reduction fails for the following reason: If g : N⊸N⊸N is a variable then

e = λx: N.gxx

gets the type N→N by rule T-Arr-I and hence also the type N⊸N by rule S-Ax and T-Sub. Therefore, the term

e′ = λf: N⊸N.e(f0)

gets the type (N⊸N)⊸N by T-Arr-E and T-Arr-I. However, the reduct

e′′ = λf: N⊸N.g(f0)(f0)

gets the type (N⊸N)→N because f appears twice in the body of the abstraction. Yet, (N⊸N)→N is not a subtype of (N⊸N)⊸N.

A type system closed under reduction would necessarily have to be as complex as normalisation of SLR terms, thus basically require to run programs in order to type check them. The reason is as follows. Let e_{1}, e_{2} be two term of type N containing a free variable f : N⊸N, i.e., f: N⊸N ⊢ e_{1}, e_{2} : N. As before, let g: N⊸N⊸N be a variable and consider the term

e = λf: N⊸N.ge_{1}e_{2}

If there exists a term e_{3} such that e_{3} reduces to both e_{1} and e_{2} then

λf: N⊸N.(λx: N.gxx)e_{3} ⇝^{∗} e

so e would have to be given type (N⊸N)⊸N. Otherwise, it should get type (N⊸N)→N. Whether such common ancestor e_{3} exists can be decided by normalising e assuming that reduction is confluent.

This is exactly what Bellantoni, Niggl, Schwichtenberg do in order to enforce that their type system be closed under reduction. They use the term affination for the collection of several occurrences of a variable stemming from the same subterm of type N. The reader may wish to compare this with the definition of the comonad ! in 2.7.6.3.

Since we do not rely on reduction in the proof of soundness we do not need this.

3.7 Alternative syntax with modal types

It is sometimes convenient to have a unary type former □(−) and to define the modal function space from the linear one as

□A→B =_{def} □(A)⊸B

Similarly, following Girard, we may introduce a modality !(−) for duplication and define the nonlinear function space as

A→B =_{def} !(A)⊸B

The reason why we have not done this in the official version is that we were aiming for a system which does not use any new term formers or constants in conjunction with the modalities and can automatically infer the best possible typing of a given term. This is not possible with unary modalities as shown by the following example: The term

e =_{def} λf: A⊸B.λx: A.fx

can be given any of the following incomparable types

□(A⊸B)⊸□A⊸□B
!(A⊸B)⊸! A⊸! B
(A⊸! B)⊸A⊸! B
(A⊸□B)⊸A⊸□B

and a few more. So, a flexible yet relatively simple type system like the one for SLR does not seem possible for such a system. It could be that a system with “aspect variables” in the style of Hindley-Milner type variables could lead to a viable solution; again details remain to be studied.

If we are more modest and refrain from all inference then we can have a very simple system with modalities and single linear function space. Such system will fail to have the subject reduction property, but since our semantic soundness proof does not need any notion of reduction on terms this need not concern us.

The types of this system, to be called λ^{□!} for the moment are given by the grammar

A ::= X | A_{1}⊸A_{2} | !(A) | □(A) | ∀X.A | A_{1} ⊗ A_{2} | A_{1} × A_{2} | N | L(A) | T(A)

The terms are those of SLR and in addition we have new term formers referring to the modalities:

e ::= … | □(e) | !(e) | unbox(e) | derelict(e)

as well as a constant

δ : N⊸!(N)

allowing us to duplicate terms of ground type.

Contexts are sets of bindings x: A with disjoint variables like in the simply-typed lambda calculus. There are no aspects and no subtyping.

A context Γ is called modal if Γ(x) is of the form □(A) for every variable x ∈ dom(Γ). It is called nonlinear if Γ(x) is of the form □(A) or !(A) in this case. So, a modal context is in particular nonlinear.

The constants of this system are the same as the ones for SLR with their types amended according to the above definition.

The typing rules are as follows.

\frac{x ∈ dom(Γ)}{Γ ⊢ x : Γ(x)}  (T-Var)

\frac{Γ ⊢ e : A Γ modal}{Γ, Δ ⊢ □(e) : □(A)}  (T-Box-I)

\frac{Γ ⊢ e : □(A)}{Γ ⊢ unbox(e) : !(A)}  (T-Box-E)

\frac{Γ ⊢ e : !(A)}{Γ ⊢ derelict(e) : A}  (T-Bang-E)

\frac{Γ ⊢ e : A Γ nonlinear}{Γ ⊢ !(e) : !(A)}  (T-Bang-I)

\frac{Γ, x: A ⊢ e : B}{Γ ⊢ λx: A.e : A⊸B}  (T-Arr-I)

\frac{Γ, Δ_{1} ⊢ e_{1} : A ⟶^{a} B  Γ, Δ_{2} ⊢ e_{2} : A  Γ nonlinear}{Γ, Δ_{1}, Δ_{2} ⊢ (e_{1} e_{2}) : B} (T-Arr-E)

\frac{Γ ⊢ e : A X not free in Γ}{Γ ⊢ ΛX.e : ∀X.A}  (T-All-I)

\frac{Γ ⊢ e : ∀X.A S safe}{Γ ⊢ e[S] : A[S/X]}  (T-All-E)

\frac{Γ ⊢ e_{1} : A_{1} Γ ⊢ e_{2} : A_{2}}{Γ ⊢ ⟨e_{1}, e_{2}⟩ : A_{1} × A_{2}}  (T-Prod-I)

\frac{Γ ⊢ e : A_{1} × A_{2} i ∈ \{1, 2\}}{Γ ⊢ e.i : A_{i}}  (T-Prod-E)

\frac{Γ, Δ_{1} ⊢ e_{1} : A_{1} Γ, Δ_{2} ⊢ e_{2} : A_{2} Γ nonlinear}{Γ, Δ_{1}, Δ_{2} ⊢ e_{1} ⊗ e_{2} : A_{1} ⊗ A_{2}}  (T-Tens-I)

\frac{Γ, Δ_{1} ⊢ e_{1} : A_{1} ⊗ A_{2} Γ, Δ_{2}, x: A_{1}, x: A_{2} ⊢ e_{2} : B Γ nonlinear}{Γ, Δ_{1}, Δ_{2} ⊢ let e_{1}=x ⊗ y in e_{2} : B}

(T-Tens-E)

\frac{Γ, Δ_{1} ⊢ e_{1} : A Γ, Δ_{2}, x: A ⊢ e_{2} : B  Γ nonlinear}{Γ, Δ_{1}, Δ_{2} ⊢ let e_{1}=x in e_{2} : B}  (T-Let)

\frac{c : A}{Γ ⊢ c : A}  (T-Const)

We will show later in Section 4.4.2 how our semantic soundness proof also covers this system with only superficial amendments.

As said before, the disadvantage of this system is that due to the presence of the extra term formers our programs will be more verbose than those in SLR. For example, the squaring function will be defined as

sq =_{def} λx: □(N).rec^{N}[N] 0 !(λx: □(N).λy: N.S_{0}(S_{0}(y))) : □(N)⊸N

If we want to apply squaring to a constant, say 2 : N then we have to write

sq(□(2)) : N

If we want to apply squaring again, then we have to do this each time:

sq(□(sq(□(2))))

We notice that typing in this system is not closed under well-typed substitution, i.e., an analogue of rule T-Subst is not admissible. The reason is that rule T-Box-I does not obviously commute with substitutions; an explicit counterexample and a detailed discussion can be found in [34].

3.7.0.1 Failure of subject reduction for λ^{□!}

We remark that typing in system λ^{□!} is not stable under substitution thus is not preserved by untyped reduction. To see this, consider the context Δ =_{def} f : X⊸□(Y), x : X and define d =_{def} λy: □(Y).□(y). We have

Δ ⊢ d : □(Y)⊸□(□(Y))

by rule T-Box-I with Γ = ∅. We also have Δ ⊢ (f x) : □(Y) by rule T-Arr-E. Therefore,

Δ ⊢ d(f x) : □(□(Y))

However,

Δ ⊬ □(f x) : □(□(Y))

because the side condition to rule T-Box-I is not satisfied in this case.

It should be possible to construct a system along the lines of Pfenning and Wong’s systems [34], see also Section 2.6.12.4. Since our soundness proof does not rely on reduction we have not explored this avenue.

3.8 Other variations of SLR

For expository reasons we have defined SLR with specific base types and constants built in. It should be clear, however, that the meta-theoretic development is independent of the choice of constants and also relatively flexible w.r.t. to the addition of new types. Other base types can be straightforwardly added via type variables. If one wants to add a new type former with associated operations on terms one has to give subtyping and typing rules and slightly extend some of the proofs.

Another line of extension is the addition of aspects. Indeed, the typing rules and proofs have been formulated in such a way that the aspects can be replaced by an arbitrary ∧- semilattice A together with a monotone function into the two-element poset nonlinear ≤ linear. We will briefly discuss an application of this in Chapter 5.

Semantics of SLR

This chapter constitutes the heart of this thesis. It is devoted to the construction of models for SLR which enable us to deduce that all definable functions are polynomial time computable.

In Section 4.1 we review the concept of a BCK-algebra which corresponds to affine linear lambda calculus in the same way as combinatory or SK -algebra corresponds to ordinary lambda calculus.

The main result of this section is the construction of a particular BCK-algebra consisting of PTIME algorithm satisfying a certain linear growth restriction.

In Section 4.2 we study the category H of modest realisability sets over this algebra. The absence of diagonalisation or an S-combinator in H is reflected by the fact that this category is not cartesian closed but rather affine linear closed in the sense of Section 2.7.

This category serves as interpretation of safe types and functions between them. In particular, we show how to interpret in H natural numbers and trees along with their constructor functions and operators for case distinction.

Next, we define a notion of PTIME-function between such realisability sets which generalises the morphisms in H. Every morphism is a PTIME-function but not vice versa. We then identify recursion patterns for natural numbers as operators on PTIME-functions. E.g., if h is a PTIME-function from P ⊗ N to A⊸A then so will be rec(h) defined by rec(h)(p, 0, a) = a and rec(h)(p, x, a) = h(p, x, rec(h)(p, ⌊\frac{x}{2}⌋ , a)).

In order to obtain a fully-fledged model of SLR we group the PTIME-functions together with the H-morphisms to form a new category H^{□} in which objects are pairs X = (X_{0}, X_{1}) of H-sets and where a morphism from (X_{0}, X_{1}) to (Y_{0}, Y_{1}) is a pair f = (f_{0}, f_{1}) of PTIMEfunctions (in a suitably defined sense) f_{0} : X_{0} ^{}Y_{0} and f_{1} : X_{0} ^{}(X_{1}⊸Y_{1}) where X_{1}⊸Y_{1} is linear function space in H.

The two-zoned structure of this category is reminiscent of the two zones in Bellantoni-Cook’s original first-order function algebra and also of the structure of the category used in [15] to model a higher-type extension thereof.

Like H this category is a well-pointed ALC. However, linear function spaces only exist between safe objects, i.e., those of the from (⊤, X).

The desired model of SLR is then obtained as the functor category Ext(H^{□}) of extensional presheaves over H^{□}. This category supports all linear function spaces as well as modalities □ and ! referring to modality and nonlinearity. On representable presheaves we have □(A_{0}, A_{1}) = (A_{0} ⊗ A_{1}, ⊤).

The category H has the property that only finite objects are duplicable; in particular the object of integers is not. Thus, it provides a model only for SLR without rule S-Ax; in exchange it supports stronger basic functions such as multiplication as a constant of type N⊸N⊸N.

To accommodate the equation N⊸A = N→A we construct another BCK-algebra M which has the property that the ensuing category of realisability sets does have a duplicable object of integers. The previous setup has been sufficiently modular so that no further work beyond the construction of this algebra needs to be done.

Finally, by using a logical relation we conclude the desired result that all functions definable in SLR are polynomial time computable.

4.1 A BCK -algebra of PTIME -functions

It is well-known that the concept of functional abstraction found in typed and untyped lambda calculi can be captured algebraically using the notion of combinatory algebra. Such a combinatory algebra is given by a set A, a binary operation written as juxtaposition for application associating to the left and constants S, K ∈ A such that the following equations are valid.

\begin{matrix}Sxyz = (xz)(yz) \\ Kxy = x \end{matrix}

The main result about combinatory algebra is that if t is a term built up using application from variables and the constants S, K then whenever x is a variable we can effectively produce a term λx.t which does not contain x and which has the property that (λx.t)x = t is a logical consequence of the above two equations. The definition of λx.t is by induction on the structure of t the crucial step being λx.t_{1}t_{2} = S(λx.t_{1})(λx.t_{2}).

A similar algebraisation also exists for the (affine) linear lambda calculus in the form of BCK-algebras.

Definition 4.1.1 A BCK-algebra is given by a set A, a binary operation (written as juxtaposition) associating to the left and three constants B, C, K ∈ A such that the following equations are valid.

Kxy = x
Bxyz = x(yz)
Cxyz = xzy

To rule out the trivial example we additionally require that |A| > 1.

An identity combinator I with Ix = x can be defined as I = CKK^{1}. Notice that since x ↦ Kx is injective but not onto every BCK-algebra must be infinite.

Lemma 4.1.2 Let A be a BCK-algebra and t be a term in the language of BCK-algebras and containing constants from A. If free variable x appears at most once in t then we can find a term λx.t not containing x such that for every other term s the equation (λx.t)s = t[s/x] is valid in A, i.e., all ground instances of the equation hold in A.

Proof. By induction on the structure of t. If t does not contain x then we put λx.t = Kt. If t = x then λx.t = I. If t = t_{1}t_{2} and x does not appear in t_{1} then λx.t = Bt_{1}(λx.t_{2}). If t = t_{1}t_{2} and x does not appear in t_{2} then λx.t = C(λx.t_{1})t_{2}. □

The reason why BCK-algebras are interesting in the context of polynomial time computation is that all functions which are computable in time O(|x|^{p}) for a fixed p and which are bounded by a linear function with unit slope can be organised as a BCK-algebra as we will now show.

4.1.1 A new length measure

Before giving the construction we must overcome the messy technical problem that there does not exist an injective function ⟨·, ·⟩ : ℕ^{2} ^{}ℕ such that

|⟨x, y⟩| ≤ |x| + |y| + c

for a fixed constant c.^{2}

In order to circumvent the technical difficulties arising from this we will use another length measure defined using the pairing function from Lemma 2.1.1. Recall that in this lemma we defined a pairing function ⟨−, −⟩ : ℕ × ℕ ^{}ℕ such that |⟨x, y⟩| = |x| + |y| + 2||y|| + 3 and an injection num : ℕ ^{}ℕ such that |num(x)| = |x| + 1, as well as projections and characteristic functions for the images of the two functions.

Definition 4.1.3 The length function ℓ(x) is defined recursively by

ℓ(num(x)) = |x| + 1
ℓ(⟨x, y⟩) = ℓ(x) + ℓ(y) + 3
ℓ(x) = |x|, otherwise

We have ℓ(0) = 0.

Lemma 4.1.4 The following inequalities hold for every x ∈ ℕ.

|x| ≥ ℓ(x) ≥ |x|/(1 + ||x||)

1Thanks to Andrzej Filinski for pointing this out to me.

2Thanks to John Longley for a short proof of this.

Proof. By course-of-values induction on x. If x is not of the form ⟨u, v⟩ then the result is direct. So assume the latter and that the inequalities have been established for u and v.

Now, |⟨u, v⟩| ≥ |u| + |v| + 3 ≥^{IH} ℓ(u) + ℓ(v) + 3 = ℓ(⟨u, v⟩) so the first inequality holds. For the second one we calculate as follows.

|⟨u, v⟩|
= |u| + |v| + 2||v|| + 3
≤ ℓ(u)(1 + ||u||) + ℓ(v)(1 + ||v||) + 2||v|| + 3  IH
≤ (ℓ(u) + ℓ(v))(1 + ||⟨u, v⟩||) + 2||⟨u, v⟩|| + 3
≤ (ℓ(u) + ℓ(v) + 3)(1 + ||⟨u, v⟩||)
□

It follows from the first inequality that if a function f : ℕ ^{}ℕ is computable in time O(ℓ(x)^{n}) then it is in particular computable in time O(|x|^{n}). Conversely, if f : ℕ ^{}ℕ is computable in time O(|x|^{n}) then the function λx.f(num(x)) is computable in time O(ℓ(x)^{n}).

The second inequality shows that, in this case f itself is computable in time O(ℓ(x)^{n+1}) as |x|/(1 + ||x||) ≥ |x|^{1−1/n} for large x. so |x| = O(ℓ(x)^{1+ε}) for each ε > 0.

4.1.2 Construction of H_{p}

Recall the notation concerning Turing machine computations from Section 2.1 Let p > 2 be a fixed integer.

Definition 4.1.5 A computation {e}(x) is called short (w.r.t. p) if it terminates in not more than d(ℓ(e) + ℓ(x))^{p} steps where d = ℓ(e) + ℓ(x) − ℓ({e}(x)).

An algorithm e is called short if {e}(x) is short for all x.

The difference d between ℓ(e)+ℓ(x) and ℓ({e}(x)) is called the defect of computation {e}(x). Notice that if {e}(x) is short then it must have nonzero defect so ℓ({e}(x)) < ℓ(e) + ℓ(x) for every x. Also notice that if f is computable in time O(ℓ(x)^{p}) and ℓ(f(x)) = ℓ(x)+O(1) then by padding (inserting comments) we can obtain a short algorithm e for f.

Proposition 4.1.6 There exists a function app : ℕ × ℕ ^{}ℕ and a constant γ such that

– app(e, x) is computable in time d(ℓ(e)+ℓ(x)+γ)^{p} where d = ℓ(e)+ℓ(x)−ℓ(app(e, x))

– If {e}(x) is short then {e}(x) = app(e, x).

Proof. The following algorithm satisfies the required specification:

read e, x

conf := init(e, x)

while t < (ℓ(e) + ℓ(x))^{p+1} and term(conf ) ≠ 0 do begin

conf := step(conf) t := t + 1 end r := out(conf) d := ⌈t/(ℓ(e) + ℓ(x))^{p}⌉ if term(conf) = 0 and ℓ(r) + d ≤ ℓ(e) + ℓ(x)

then write r else write 0

The algorithm performs at most d(ℓ(e) + ℓ(x))^{p} simulation steps where d = ℓ(e) + ℓ(x) − ℓ(app(e, x)) regardless of which branch of the conditional is taken. If the computation {e}(x) is short then the dedicated time suffices to finish it so that {e}(x) = app(e, x) in this case. The total running time of app(e, x) consists of the simulation steps plus a certain number of steps needed for initialisation, some arithmetic, and moving around intermediate results. The number of these steps is linear in the binary length of the input, thus quadratic in ℓ(e) + ℓ(x) by Lemma 4.1.4 and thus can be accounted for by an appropriate choice of the constant γ in view of p > 2. □

We will henceforth write app(e, x) as ex where appropriate.

Before embarking on the proof that the above defined application function induces a BCK-algebra structure on the natural numbers we will try to motivate the notion of short computation and in particular the role of the defect.

The starting point is that we want to construct an untyped universe of such computations which can later on serve as step functions in safe recursions. Certainly, these algorithms should themselves be polynomial time computable. Moreover, in order that their use as step functions does not lead beyond polynomial time we must require a growth restriction of the form ℓ(f(x)) = ℓ(x) + O(1). Remember that if, e.g., ℓ(f(x)) = 2ℓ(x) then ℓ(f^{|y|}(x)) = 2^{|y|}ℓ(x), thus we quit polynomial time.

Next, in order that application itself be polynomial time computable we must restrict to algorithms running in time O(ℓ(x)^{p}) for some fixed p.

Next, we have to look at the coefficient of the leading term of the polynomial governing the runtime. If our algorithms have running time dℓ(x)^{p} + O(ℓ(x)^{p−1}) for arbitrary d then the application runs in time O(ℓ(x)^{p+1}) thus, again, application is not among the algorithms considered and accordingly, no higher-order functions are possible. If we also bound the coefficient of the leading term and only consider algorithms running in dℓ(x)^{p} + O(ℓ(x)^{p−1}) for fixed d and p then once again we lose closure under composition, as in order to evaluate f(g(x)) we must evaluate both f and g requiring time 2dℓ(x)^{p} + O(ℓ(x)^{p−1}). The solution is to couple runtime and output size via the defect so that if u := g(x) is large (which would mean that the second computation f(u) runs longer) then this is made up for by a shorter runtime of g(x). We shall now see formally that this works and in particular that a B-combinator is definable.

Lemma 4.1.7 (Parametrisation) For every e there exists an algorithm e′ such that e⟨x, y⟩ = e′xy.

Proof. Let e_{0} be the following algorithm:

read x

write “read y; write app(e, ⟨‘x, y⟩)”

Here ‘x refers to the actual value of x rather than the name x itself. Now, assuming that e_{0} has been reasonably encoded, we have ℓ({e_{0}}(x)) ≤ ℓ(e) + ℓ(x) + c_{0} where c_{0} is some fixed constant. Note that we use here the fact that ℓ(⟨u, v⟩) = ℓ(u) + ℓ(v) + 3 so it is possible to “hardwire” both e and x without sacrificing essentially more than ℓ(e) + ℓ(x) in length. By padding e_{0} we obtain an algorithm e_{1} with the same behaviour as e_{0} and such that ℓ({e_{1}}(x)) < ℓ(e_{1}) + ℓ(x). Since {e}(x) terminates in time linear in |x|, thus quadratic in ℓ(x), we can—by further padding e_{1}—obtain an algorithm e_{2} such that e_{2}x = {e_{0}}(x).

Now, by construction, we have {e_{2}x}(y) = e⟨x, y⟩ and the computation {e_{2}x}(y) takes less than d(ℓ(e) + ℓ(x) + ℓ(y) + 3 + γ)^{p} steps where γ is the constant from Prop. 4.1.6 and d = ℓ(e) + ℓ(x) + ℓ(y) + 3 − ℓ(e⟨x, y⟩). Therefore, by further padding e_{2} to make up for γ + 3 we obtain the desired algorithm e′. □

Theorem 4.1.8 The set of natural numbers together with the above application function app is a BCK-algebra.

Proof. The combinator K is obtained by parametrising the (linear time computable) left inverse to the pairing function.

For the composition combinator B we start with the following three-input algorithm:

B_{0} ≡ read w write app(w.1.1, app(w.1.2, w.2))

We have {B_{0}}(⟨⟨x, y⟩, z⟩) = x(yz) and the time t_{tot} needed to evaluate {B_{0}}(⟨⟨x, y⟩, z⟩) is less than t_{1} + t_{2} + t_{b} where

u = yz
w = x(yz)
d_{1} = ℓ(y) + ℓ(z) − ℓ(u)
d_{2} = ℓ(x) + ℓ(u) − ℓ(w)
t_{1} = d_{1}(ℓ(y) + ℓ(z) + γ)^{p}
t_{2} = d_{2}(ℓ(x) + ℓ(u) + γ)^{p}

and where t_{b}—the time needed for shuffling around intermediate results—is linear in |x| + |y| + |z| + |u| + |w| thus O((ℓ(x) + ℓ(y) + ℓ(z))^{2}) where we have used the inequalities ℓ(u) ≤ ℓ(y) + ℓ(z) and ℓ(w) ≤ ℓ(x) + ℓ(y) + ℓ(z) to get rid of the u and w.

This means that we can find a constant c_{2} such that

t_{tot} ≤ (d_{1} + d_{2})(ℓ(x) + ℓ(y) + ℓ(z) + c_{2})^{p}

Here we have used the fact that ℓ(u) ≤ ℓ(y) + ℓ(z).

Now the defect of the computation {B_{0}}(⟨⟨x, y⟩, z⟩) equals ℓ(B_{0}) + ℓ(x) + ℓ(y) + ℓ(z) + 6 − ℓ(w) = ℓ(B_{0}) + 6 + d_{1} + d_{2}. Therefore, by choosing ℓ(B_{0}) large enough we obtain

app(B_{0}, ⟨⟨x, y⟩, z⟩) = {B_{0}}(⟨⟨x, y⟩, z⟩) = x(yz)

The desired algorithm B is then obtained by applying Lemma 4.1.7 twice.

Notice, that the existence of the B combinator hinges on the fact that the time of a computation decreases as the size of the output goes up. Had we not imposed the dependency of running time on output size via the defect it would not have been possible to define the B combinator.

Let us finally define the C combinator. We start with the following algorithm

C_{0} ≡ read w write app(app(w.1.1, w.2), w.1.2)

Clearly,

{C_{0}}(⟨⟨x, y⟩, z⟩) = xzy

The total time t_{tot} needed for this computation is bounded by t_{1} + t_{2} + t_{b} where

u = xz
w = uy
d_{1} = ℓ(x) + ℓ(z) − ℓ(u)
d_{2} = ℓ(u) + ℓ(y) − ℓ(w)
t_{1} = d_{1}(ℓ(x) + ℓ(z) + γ)^{p}
t_{2} = d_{2}(ℓ(u) + ℓ(y) + γ)^{p}

and, again, t_{b} is O((ℓ(x) + ℓ(y) + ℓ(z))^{2}). Therefore, we can find a constant c_{0} such that

t_{tot} ≤ (d_{1} + d_{2})(ℓ(x) + ℓ(y) + ℓ(z) + c_{0})^{p}

The defect of the computation {C_{0}}(⟨⟨x, y⟩, z⟩) is

d = ℓ(C_{0}) + 6 + ℓ(x) + ℓ(y) + ℓ(z) − ℓ(w) = ℓ(C_{0}) + 6 + d_{1} + d_{2}

Therefore, assuming w.l.o.g. that ℓ(C_{0}) ≥ c_{0} we obtain

app(C_{0}, ⟨⟨x, y⟩, z⟩) = {C_{0}}(⟨⟨x, y⟩, z⟩) = xzy

The desired combinator C is again obtained by applying Lemma 4.1.7 twice. □

Definition 4.1.9 The BCK-algebra thus constructed will be called H_{p}.

Abbreviations. Let H be a BCK-algebra. In view of Lemma 4.1.2 we will freely use linear lambda terms involving constants from H in order to denote particular elements of H. Moreover, we write λx_{1}x_{2} … x_{n}.t for λx_{1}.λx_{2}. . . . .λx_{n}.t. We write T for the pairing combinator λxyf.fxy and P_{1}, P_{2} for the projections λp.p(λxy.x) and λp.p(λxy.y). Note that P_{i}(T t_{1}t_{2}) = t_{i}.

It is in general not a good idea, to use projections in order to decompose a variable meant to encode a pair. The reason is that in order to maintain linearity we can use either P_{1} or P_{2}, but not both. The correct way to decompose a pair is to apply it to a function of two arguments which are then bound to the components of a pair. Suppose, for example, that u, v ∈ H and that we want to define an element u ⊗ v ∈ H such that (u ⊗ v)(T xy) = T (ux)(vy). Writing

(u ⊗ v) =_{def} λp.T (u(P_{1} p))(v(P_{2} p))

does not work since the λ-abstraction is not defined because p occurs twice in its body. We can, however, achieve the desired effect by putting

(u ⊗ v) =_{def} λp(λxy.T (ux)(vy))

4.1.3 Truth values and numerals

In every BCK-algebra truth values and numerals can be encoded. In concrete examples it is, however, often convenient to use other representations for these basic datatypes than the canonical ones which is why we give them the status of extra structure.

Definition 4.1.10 A BCK-algebra H supports truth values and natural numbers if there are distinguished elements tt, ff, D, S_{0}, S_{1}, G and an injection num : ℕ→H such that the following equations are satisfied.

D tt x y = x D ff x y = y S_{0} num(x) = num(2x) S_{1} num(x) = num(2x + 1) G num(0) = T tt (T tt tt) G num(2(x + 1)) = T ff (T tt num(x + 1)) G num(2x + 1) = T ff (T ff num(x))

If φ is an informal statement let [φ] be tt if φ is true and ff otherwise. We have

P_{1}(G num(n)) = [n=0]
P_{1}(P_{2}(G num(n))) = [n is even]
P_{2}(P_{2}(G num(n + 1))) = num(⌊\frac{n+1}{2}⌋)

Proposition 4.1.11 Every BCK-algebra supports truth values and natural numbers.

Proof. Define tt =_{def} λxy.x and ff =_{def} λxy.y and D =_{def} λtxy.txy. This accounts for the truth values. The injection num is defined recursively by num(0) = T tt(T tt tt), num(2(x + 1)) = T ff(T tt num(x)), num(2x + 1) = T ff(T ff num(x)). Now put

S_{0} = λx.x(λty.D t
num(0)  case x = 0
(T ff(T tt (T ff y)) case x ≠ 0
S_{1} = λx.T ff(T ff x)
G = I
□

Proposition 4.1.12 The algebras H_{p} support natural numbers and truth values with the settings tt = 1, ff = 0, and num defined as in Lemma 2.1.1, i.e., num(x) = 2x + 1.

Proof. The missing constants are obtained by parametrisation from the obvious algorithms computing them. □

4.2 Realisability sets

In this section we define and explore an analogue of the category of modest sets introduced by Moggi and others based on a BCK-algebra supporting truth values and natural numbers. We refer to, e.g., [17] for an introduction to modest sets and realisability. We shall see that due to the absence of an S-combinator hence of diagonalisation, the thus obtained category of modest sets is not cartesian closed. It is, however, an affine linear category w.r.t. to a natural tensor product based on the pairing function and it also has cartesian products, which, however, lack right adjoints, i.e., function spaces.

Unless stated otherwise let H be an arbitrary BCK-algebra supporting truth values and natural numbers. For a concrete example the reader may of course think of H_{p} for H.

Definition 4.2.1 An H-set is a pair X = (|X|, ⊩_{X}) where |X| is a set and ⊩_{X}⊆ H × X is a relation such that

i. ∀x∈X.∃n∈H.n ⊩_{X} x.

ii. ∀x, y∈X.∀n∈H.n ⊩_{X} x ∧ n ⊩_{X} y ⇒ x = y

A morphism from H-set X to H-set Y is a function f : |X| ^{}|Y | such that there exists an element e ∈ H with

∀x∈X.∀t∈H.t ⊩_{X} x ⇒ e t ⊩_{Y} f(x)

We write e ⊩_{X⊸Y} f in this case.

Remark 4.2.2 We remark in the classical case of realisability sets over a partial combinatory algebra H the term H-set is used for objects which only satisfy requirement i) above. Those which also satisfy ii) are called modest H-sets. Since we do not need this generalisation we do not use the attribute “modest”. The requirement i) that every element have a realiser is needed in order that the category of H-set is well-pointed which will allow us to use extensional presheaves.

If f : |X| ^{}|Y | is a set-theoretic function then we say that f is realised by e, if e ⊩_{X⊸Y} f. So an H-set morphism from X to Y is a function that can be realised.

We will sometimes write X instead of |X| and ⊩ instead of ⊩_{X}.

Definition and Theorem 4.2.1 The H-sets together with their morphisms form a wellpointed affine linear category H with the following settings.

– Identities and composition are given by set-theoretic identity and composition which are realised by virtue of the I and B combinators.

– The tensor product of H-sets X, Y is given by

|X ⊗ Y | = |X| × |Y | (set-theoretic cartesian product) t ⊩_{X⊗Y} (x, y) ⟺ ∃u, v.t = T uv ∧ u ⊩ x ∧ v ⊩ y

– The projections are given by π(a, b) = a and π′(a, b) = b.

– The terminal object is ⊤ = {⟨⟩} with tt ⊩_{⊤} ⟨⟩.

Proof. The projections are obviously jointly monic and the defining equations for the associated morphisms and operators imply that those are defined as in the category of sets, e.g., associativity is given by α(x, (y, z)) = ((x, y), z). Therefore, all that remains to be shown is that the projections as well as these associated morphisms are realisable.

The projections are realised by P_{1} and P_{2}.

If f : X_{1} ^{}Y_{1} and g : X_{2} ^{}Y_{2} then (f ⊗ g)(x_{1}, x_{2}) equals (f(x_{1}), g(x_{2})) and this can be realised by λp.p(λt_{1}t_{2}.T (dt_{1})(et_{2})) when d ⊩ f and e ⊩ g.

Symmetry (X ⊗ Y ≅ Y ⊗ X) is realised by λp.p(λxy.T yx).

Associativity (X ⊗ (Y ⊗ Z) ≅ (X ⊗ Y) ⊗ Z) is realised by λp.p(λxv.v(λyz.T (T xy)z)). The unique map ⟨⟩ : X ^{}⊤ is realised by λx.tt. The isomorphism X ≅ X⊗⊤ is realised by λx.T xtt; its inverse is realised by λt.t(λxy.x).

Similarly, X ≅ ⊤ ⊗ X.

Finally, if x ∈ |X| then the function f_{x} : |⊤| ^{}|X| is a morphism realised, e.g., by Ke where e is a realiser for x. So all elements of |X| appear as global elements and it follows that H is a well-pointed. Notice that this hinges on the requirement that every element have a realiser. □

Proposition 4.2.3 In H all linear function spaces exist and are given as follows. If X, Y ∈ H then X⊸Y has as underlying set the set of morphisms from X to Y. The realisability relation ⊩_{X⊸Y} is as defined above in Def. 4.2.1, i.e.,

e ⊩_{X⊸Y} f ⟺ ∀t, x.t ⊩_{X} x ⇒ et ⊩_{Y} f(x)

The application map ev : (X⊸Y) ⊗ X ^{}Y is defined by ev(f, x) = f(x).

Proof. Application is realised by λz.z(λfx.fx). If f : Z ⊗X ^{}Y is realised by e then for each z ∈ Z the function x ↦ f(z, x) is realised by λv.e(λk.kuv) when u is a realiser for z. Therefore, we have a function from Z to (X⊸Y). This function itself is realised by λuv.e(λk.kuv). □

Proposition 4.2.4 The category H has cartesian products given by

\begin{matrix}|X × Y | = |X| × |Y | \\ e ⊩_{X×Y} (x, y) ⟺ e tt ⊩ x ∧ e ff ⊩ y \end{matrix}

Proof. Projections X × Y ^{}X and X × Y ^{}Y are realised by λe.e tt and λe.e ff, respectively. If f : Z ^{}X and g : Z ^{}Y are realised by d and e then the “targettupled” function ⟨f, g⟩ : Z ^{}X × Y defined by ⟨f, g⟩(c) = (f(c), g(c)) is realised by λxt.(Dtde)x. □

4.2.1 Natural numbers and other datatypes

Definition 4.2.5 The H-set of natural numbers N has ℕ as underlying set and realising relation defined by num(n) ⊩_{N} n.

Lemma 4.2.6 Suppose that H = H_{p}. The H-morphisms from N ⊗ ⋯ ⊗ N (n factors) are the functions f : ℕ^{n} ^{}ℕ which are computable in time O((|x_{1}| + ⋯ + |x_{n}|)^{p}) and moreover satisfy |f(x⃗)| = |x_{1}| + ⋯ + |x_{n}| + O(1).

Proof. Direct from Proposition 4.1.6 and the discussion following Lemma 4.1.4. □

Proposition 4.2.7 (Constructors and case distinction) Let X be an H-set. The functions

0 : |⊤| ^{}|N|
S_{0} : |N| ^{}|N|
S_{1} : |N| ^{}|N|
case^{N} : |C × ((N⊸C) × (N⊸C))| ^{}|N⊸C|

with 0(⟨⟩) = 0, S_{0}(x) = 2x, S_{1}(x) = 2x+1, and case^{N}(g, h_{0}, h_{1})(0) = g, case^{N}(g, h_{0}, h_{1})(2x+ i) = h_{i}(x), otherwise, are morphisms in H.

Proof. Obvious for 0, S_{0}, S_{1}. A realiser for case^{N} is obtained as follows.

λp.λx.G x(λuv.Du
(ptt)  case x=0
(v(λqy  q=[x is even], y = ⌊\frac{x}{2}⌋
.pff q y))
□

Definition 4.2.8 Let A be an H-set. We define T(A) ∈ H by |T(A)| = T(|A|), i.e., the set of |A|-labelled binary trees. The realisability relation ⊩_{T(A)} is defined inductively by

– If x ⊩ a then T tt x ⊩ leaf(x).

– If x ⊩ a and y ⊩ l and z ⊩ r then T ff (T a(T lr)) ⊩ node(a, l, r).

Proposition 4.2.9 The constructors leaf and node give rise to morphisms of the following type

leaf_{A} : A ^{}T(A)
node_{A} : A ⊗ T(A) ⊗ T(A) ^{}T(A)

Furthermore, the function

case_{X}^{T(A)} : (A⊸X) × (A ⊗ T(A) ⊗ T(A)⊸X)⊸T(A)⊸X

given by

case_{X}^{T(A)}(h_{leaf}, h_{node})(leaf(a)) = h_{leaf}(a)
case_{X}^{T(A)}(h_{node}, h_{node})(leaf(a)) = h_{node}(a, l, r)

is realisable.

Proof. Analogous to Prop. 4.2.7 □

Similarly, we can define H-sets of lists and other inductively defined data types (see also Section 4.3.1).

4.3 Interpreting recursion

In general (e.g. when H = H_{p}) the category H itself does not contain all PTIME-functions and thus does not allow us to represent patterns of safe recursion on notation.

In order to achieve this we introduce a notion of polynomial time computable function between H-sets which strictly contains the H-set morphisms. Safe recursion then takes the form of an operator on such polynomial time computable functions with the proviso that the step functions be H-set morphisms. In order to give safe recursion the shape of a higher-typed constants we move to extensional presheaves over a category obtained by integrating the polynomial time computable functions with the H-set morphisms.

In order to be able to define these polynomial time computable functions we need to restrict our attention to BCK-algebras in which application is polynomial time computable. In addition we need to impose further technical conditions all of which are met by the algebras H_{p}. The reason for introducing another level of abstraction and not working directly with the H_{p} is that we will later on instantiate these results with two other BCKalgebras.

Definition 4.3.1 A BCK-algebra H supporting truth values and natural numbers is called polynomial time computable (polynomial or PTIME for short) if its carrier set can be identified with a subset of strings so that it is amenable to algorithmic manipulation and there exists a function ℓ : H ^{}ℕ such that

– there exists an algorithm f and a polynomial p such that for each x, y ∈ H the computation f(x, y) terminates after not more than p(ℓ(x) + ℓ(y)) steps with result xy,

– for each x, y ∈ H the following inequalities hold:

ℓ(xy) ≤ ℓ(x) + ℓ(y)
ℓ(x) ≤ |x|
|Kx| > |x|
|Bx| > |x|
|Cx| > |x|
|Bxy| > |x| + |y|
|Cxy| > |x| + |y|
|num(n)| ≥ |n|

The first two inequalities are abstracted from the particular example H_{p}. They are needed in order to show that iterations of functions represented in H are polynomial time computable.

The other inequalities are sanity conditions ensuring that the partial applications of B, C, K copy their input into the result and do not reduce the size by some sort of information compression.

We summarise a few basic facts about polynomial BCK-algebra in the following lemma:

Lemma 4.3.2 Let H be a polynomial time computable BCK-algebra.

i. The BCK-algebras H_{p} are polynomial time computable;

ii. For each x, y ∈ H we have |T xy| > |x| + |y|;

iii. The requirement |num(n)| ≥ |n| is satisfied for the canonical encoding (Section 4.1.11) of natural numbers in BCK-algebra;

iv. ℓ(num(n)) = O(|n|), hence Θ(|n|);

v. There exists a PTIME-function getnum such that getnum(num(n)) = n.

Proof. Part i is obvious from the definition of the H_{p}. Part ii follows from T xy = C(CIx)y, hence |T xy| > |CIx| + |y| > |CI| + |x| + |y| ≥ |x| + |y|. For iii we use ii and induction over n. Part iv follows from the existence of constructor functions S_{0} and S_{1} together with the inequation ℓ(xy) ≤ ℓ(x) + ℓ(y).

For part v we note that the function getnum admits a recursive definition in terms of G. The total number of unfoldings of the recursive definition can be a priori bounded by |x|.

□

We assume henceforth that our generic BCK-algebra H is polynomial.

Definition 4.3.3 Let X, Y be H-sets. A PTIME-function from X to Y is a function f : |X| ^{}|Y | such that there exists a one-argument algorithm g and a polynomial p such that whenever e ⊩_{X} x then the computation g(e) terminates after not more than p(|e|) steps and g(e) ⊩_{Y} f(x).

We use the notation f : X ^{PTIME}^{}Y to indicate that f is a PTIME-function from X to Y

An algorithm g together with polynomial p as in the above definition will often be called a realiser for PTIME-function f. If g is a realiser with polynomial p then ℓ(g(e)) ≤ p(|e|) by ℓ(x) ≤ |x|.

A realiser for a PTIME-function f from X ⊗ Y to Z can equivalently be given as a two-argument algorithm g together with a two-variable polynomial p such that d ⊩ x and e ⊩ y implies that {g}(x, y) terminates in not more than p(|x|, |y|) steps and yields a realiser for f(x, y).

Notice that every morphism of H-sets is a PTIME-function between H-sets, but not vice versa. The PTIME-functions with ordinary composition clearly form a category but this fact will not be needed.

Also notice that by the estimates of |num(n)| in Lemma 4.3.2 a map f : N ^{PTIME}^{}N is the same as a polynomial time computable function on the integers in the usual complexitytheoretic sense.

If A_{1}, …, A_{m}, B_{1}, …, B_{n}, C are H-sets then a map

f : A_{1} ⊗ … ⊗ A_{m} ^{PTIME}^{}(B_{1} ⊗ … ⊗ B_{n})⊸C

can be viewed as an m + n-ary map with the first m inputs “normal” in the sense that the result depends polynomially on their size and the second n inputs “safe” in the sense that the size of the result is majorised by the sum of their sizes plus a constant independent of their sizes. More, formally, if k, q is a realiser for such map f then we have

ℓ(f(x⃗; y⃗)) ≤ q(ℓ(x⃗)) + ∑_{i=1}^{n} ℓ(y_{i})

In this way the polynomial time functions between H-sets generalise Bellantoni’s “polymaxbounded”-functions to linearity and higher types. We see that the maximum operation is now replaced by summation which exploits the greater generality offered by linearity.

Theorem 4.3.4 (Safe recursion on notation) Let P, X be H-sets. If

h : P ⊗ N ^{PTIME}^{}X⊸X

is a PTIME-function as indicated then so is the function f : |P ⊗ N| ^{}|X⊸X| defined by

\begin{matrix}f(p, 0)(g) = g \\ f(p, x)(g) = h(p, x)(f(p, ⌊x/2⌋)(g)) & when x > 0 \end{matrix}

Proof. Let k be a realiser for h, i.e. an algorithm such that t ⊩_{P} p implies that k(t, num(n)) terminates in not more than than q(|t|, |num(n)|) steps for some fixed polynomial q. In view of ℓ(x) ≤ |x| this implies ℓ(k(t, num(n))) ≤ q(|t|, |num(n)|).

In order to realise rec^{N}(h) we consider the recursive algorithm g : H ×H ^{}H defined by

g(t, x) = if (P_{1}(G x)) = tt
then λv.v  (Case x = 0)
else λv.k(t, x) (g(t, div2 x) v)

where

div2 = λx.P_{2}(P_{2}(G x))

It is clear from the definition that g is a realiser for g provided we can show that it has polynomial runtime for inputs of the form t ∈ dom(P) and x = num(n) for n ∈ ℕ. Notice that for other inputs g(t, x) may diverge even if k is assumed total. For example x could be such that div2 x = x.

The length of g(t, x) satisfies the following estimate.

ℓ(g(t, num(0))) ≤ c
ℓ(g(t, num(n))) ≤ c + q(|t|, |num(n)|) + ℓ(g(t, num(⌊\frac{n}{2}⌋))), if n > 0

Therefore,

ℓ(g(t, num(n))) ≤ |n| · q(|t|, |num(n)|) + c

In order to estimate the time T (t, x) needed to compute g(t, x) we first note that, in fact, g can be written as

g(t, x) = if(P_{1}(G x)) = tt
then I  (Case x = 0)
else B k(t, x) (g(t, div2 x) v)

so that

T (t, num(0)) ≤ c
T (t, num(n)) ≤ q(|t|, |num(n)|) + T (t, num(⌊\frac{n}{2}⌋))
+p′(q(|t|, |num(n)|) + ℓ(g(t, num(⌊\frac{n}{2}⌋)))), if n > 0

Here c is a constant and p′ is a polynomial obtained from enlarging the polynomial p witnessing that application in H is polynomial time w.r.t. ℓ a little bit so as to account for bookkeeping.

Hence, by induction on n:

T (t, num(n))
≤ c + |n| · (q(|t|, |num(n)|)+
+p′(q(|t|, |num(n)|) + |n| · q(|t|, |num(n)|) + c))
≤ c + |num(n)|(q(|t|, |num(n)|)+
+p′(q(|t|, |num(n)|) + num(n) · q(|t|, |num(n)|) + c))

which is polynomial in |t|, |num(n)| as required. □

Corollary 4.3.5 (Duplicable safe parameters) If P, D, X are H-sets and D is duplicable and h : P ⊗ N ^{PTIME}^{}D ⊗ X⊸X is a PTIME-function then the function f : |P ⊗ N| ^{}|D ⊗ X⊸X| defined by

\begin{matrix}f(p, 0)(d, g) = g \\ f(p, x)(d, g) = h(p, x)(d, f(p, ⌊\frac{x}{2}⌋)(d, g)) \end{matrix}

is a PTIME-function.

Proof. Define Y = D⊸X and h′ : P ⊗ N ^{PTIME}^{}Y ⊸Y by

h′(p, n)(u)(d) = let (d_{1}, d_{2})=δ(d) in h(p, x)(d_{1}, u(d_{2}))

where δ is the diagonal morphism for D. This definition can be formalised as a composition of h with a H-map from D ⊗ X⊸X to Y ⊸Y definable in the language of ALCC using δ and therefore yields a PTIME-function without further proof.

Applying Theorem 4.3.4 yields f′ : P ⊗ N ^{PTIME}^{}Y ⊸Y satisfying the corresponding recurrence. The desired f is obtained from f′ by

f(p, x)(d, g) = (f′(p, x)(λdg.g)) d g

Again, this can be seen as composition of f with a map from (Y ⊸Y) to D⊸X⊸X. □

Theorem 4.3.6 (Safe tree recursion) Let P, X ∈ H and

h_{leaf} : P ⊗ A ^{PTIME}^{}X
h_{node} : P ⊗ A ⊗ T(A) ⊗ T(A) ^{PTIME}^{}X ⊗ X⊸X

be PTIME-functions as indicated. then the function f : |P ⊗ T(A)| ^{}|X| defined by

f(p, leaf(a)) = h_{leaf}(p, a)
f(p, node(a, l, r)) = h_{node}(p, a, l, r)(f(p, l), f(p, r))

is a PTIME-function from P ⊗ T(A) to X.

Proof. Let k_{leaf} and k_{node} be realisers for h_{leaf} and h_{node} viewed as binary, resp. quaternary PTIME-functions with witnessing polynomials q_{leaf}(t, a) and q_{node}(t, a, l, r). In order to realise f we recursively define a function g : H × H ^{}H by

g(t, x) = D x_{1}
(k_{leaf}(t, x_{2}))
(k_{node}(t, x_{21}, x_{22}, x_{23})(T f(z_{0}, x_{22}) f(z_{0}, x_{23})))
where x_{1} = P_{1}x, x_{2} = P_{2}x, x_{21} = P_{1}(P_{2}x), x_{22} = P_{1}(P_{2}(P_{2}x)), x_{23} = P_{2}(P_{2}(P_{2}x))

Again, it is clear that if this algorithm runs sufficiently fast then it will realise the above function on trees. It thus remains to show that if t ∈ dom(P) and x ∈ dom(T(A)) then f(t, x) is computable in polynomial time.

Now, we have

ℓ(f(t, T tt a)) ≤ q_{leaf}(|t|, |a|)
ℓ(f(t, T ff(T a(T lr)))) ≤ c + q_{node}(|t|, |a|, |l|, |r|) + ℓ(f(t, l)) + ℓ(f(t, r))

where c is a constant accounting for the ℓ-length of the function combining the results of the recursive calls.

Therefore, if x ∈ dom(T(A)) then using |T xy| > |x| + |y| we obtain

ℓ(f(t, x)) ≤ |x| · q(|t|, |x|)

where q(|t|, |x|) majorise q_{leaf}(|t|, |a|) and q_{node}(|t|, |x|, |x|, |x|) + c.

Now it follows that the runtime of f(t, x) can be estimated by

T (t, T tt a) ≤ p′(q(|t|, |a|)) ≤ p′(|t|, |T tta|)
T (t, T ff x) ≤ q(|t|, |x|) + T (t, l) + T (t, r)+
p′(q(|t|, |x|) + |l|q(|t|, |l|) + |r|q(|t|, |r|))
≤ T (t, l) + T (t, r) + p′(|t|, |x|)

where x = T ff(T a(T lr)) and p′ is a suitably large polynomial. Therefore, (always under the assumption that x ∈ dom(T(A)))

T (t, x) ≤ |x| · p′(|t|, |x|)
□

Corollary 4.3.7 Let P, X, D ∈ H with D duplicable and suppose that

h_{leaf} : P ⊗ A ^{PTIME}^{}D⊸X
h_{node} : P ⊗ A ⊗ T(A) ⊗ T(A) ^{PTIME}^{}D ⊗ X ⊗ X⊸X

be PTIME-functions as indicated. then the function f : |P ⊗ T(A)| ^{}|D⊸X| defined by

f(p, leaf(a))(d) = h_{leaf}(f, a)(d)
f(p, node(a, l, r))(d) = h_{node}(p, a, l, r)(d, f(p, l)(d), f(p, r)(d))

is a PTIME-function from P ⊗ T(A) to X.

Proof. Analogous to the proof of Cor. 4.3.5 using Thm. 4.3.6 with result type D⊸X.

□

Again, we omit the treatment of lists as it is analogous to the previously treated cases.

4.3.1 Leivant trees

Let us finally consider the “Leivant trees” mentioned in the introduction. For H-set A we define T′(A) by |T′(A)| = |T(A)| and

– If x ⊩ a then T tt x ⊩ leaf(x).

– If x ⊩ a and ytt ⊩ l and yff ⊩ r then T ff (T ay) ⊩ node(a, l, r).

It is then clear that the constructors give rise to morphisms

leaf′ : A ^{}T′(A)
node′ : A ⊸ (T′(A) × T′(A)) ^{}T′(A)

and that we can define an operator for case distinction

case^{T′} : (A⊸X) × (A⊸(T′(A) × T′(A))⊸X) ⊸ T′(A)⊸X

Notice that due to the cartesian product in the second branch of a case distinction on Leivant trees we can either refer to the left subtree or to the right subtree, but not to both.

Unfortunately, we did not manage to show that recursion on Leivant trees is possible in quite the same generality as for the ordinary trees. What we can offer is the following iteration principle which does not provide access to the recursion variable.

Proposition 4.3.8 Let P, X ∈ H and

h_{leaf} : P ^{PTIME}^{}A⊸X
h_{node} : P ^{PTIME}^{}A⊸(X × X)⊸X

be PTIME-functions. Then the function f : |P ⊗ T′(A)| ^{}|X| defined by

f(p, leaf(a)) = h_{leaf}(p)(a)
f(p, node(a, l, r)) = h_{node}(p)(a)(f(p, l), f(p, r))

is a PTIME-function.

Proof. We first note that using safe recursion on notation (Thm. 4.3.4) with result type T′(A)⊸X and case^{T′} we can define a PTIME-function f′ : P ⊗ N ^{PTIME}^{}T′(A)⊸X with the property that

f′(p, n)(t) = f(p, t)

provided that |n| exceeds the depth of t.

Now notice that by Prop. 4.1.6 we have ℓ(et) ≤ e + max(ℓ(ff), ℓ(tt)) for t ∈ {tt, ff}. We may thus assume without essential loss of generality that if t ⊩_{T′(A)} node(a, l, r) then

ℓ(t) > max(ℓ(P_{2}(P_{2} t) tt), ℓ(P_{2}(P_{2} t) ff))

and thus it follows by (external) tree induction that if e ⊩_{T′(A)} t then ℓ(e), hence in particular |e| is an upper bound on the depth of t. Thus, if k′(p, n) is a realiser for f′ then

k(p, t) = k′(p, num(t))t

is a realiser for f. □

4.4 Recursion operators as higher-typed constants

In this section we show how to embed the category of H-sets as well as the PTIME-functions between them into a single functor category in which the recursion patterns identified in the preceding four propositions take the form of higher-order constants involving modalities.

The strategy is to first combine H-set morphisms and PTIME-functions into a single category H^{□} which is structurally similar to the category B of “polymax-bounded” functions described in Section 2.6.12.3

Definition 4.4.1 The category H^{□} has as objects pairs X = (X_{0}, X_{1}) where both X_{0} and X_{1} are H-sets. A morphism from X to Y consists of a PTIME-function f_{0} : X_{0} ^{}Y_{0} and a PTIME-function f_{1} : X_{0} ^{}(X_{1}⊸Y_{1}). The identity morphism is given by the identity function at X_{0} and the constant function yielding the identity morphism at X_{1}. The composition of (f_{0}, f_{1}) and (g_{0}, g_{1}) is given by g_{0} ∘ f_{0} and x ↦ g_{1}(g_{0}(x)) ∘ f_{0}(x).

Proposition 4.4.2 The following data endow H^{□} with the structure of a well-pointed ALC.

The tensor product of X = (X_{0}, X_{1}) and Y = (Y_{0}, Y_{1}) is given by

(X_{0}, X_{1}) ⊗ (Y_{0}, Y_{1}) = (X_{0} ⊗ Y_{0}, X_{1} ⊗ Y_{1})

The first projection π : X ⊗ Y ^{}X is given by the obvious canonical maps

π_{0} : X_{0} ⊗ Y_{0} ^{PTIME}^{}X_{0}
π_{1} : X_{0} ⊗ Y_{0} ^{PTIME}^{}(X_{1} ⊗ Y_{1})⊸X_{1}

The second projection is defined analogously.

The terminal object is given by ⊤ = (⊤, ⊤).

Proof. Routine verification. □

The category H^{□} also has cartesian products given by (X_{0}, X_{1}) × (Y_{0}, Y_{1}) = (X_{0} × Y_{0}, X_{1} × Y_{1}) where × is the cartesian product in H.

An object of the form (X_{0}, ⊤) is called normal; an object of the form (⊤, X_{1}) is called safe.

We notice that an H^{□}-map from a safe object to a normal one must be constant.

The category H can be embedded fully and faithfully into H^{□} via X ↦ (⊤, X) and H(X, Y) ∋ f ↦ (id_{⊤}, f̂) where f̂: ⊤ ^{}X⊸Y is obtained as the transpose of

⊤ ⊗ X ^{≅}^{}X ^{f}^{}Y

This embedding preserves tensor product and cartesian product up to equality and we will therefore treat it as an inclusion thus identifying H with the full subcategory of H^{□} consisting of the safe objects.

Proposition 4.4.3 If A ∈ H^{□} is arbitrary and B is normal then

A ⊗ B ≅ A × B

Proof. Suppose that f : Z ^{}A and g : Z ^{}B. This means that we have PTIMEfunctions f_{0} : Z_{0} ^{PTIME}^{}A_{0} and f_{1} : Z_{0} ^{PTIME}^{}(Z_{1}⊸A_{1}), as well as g_{0} : Z_{0} ^{PTIME}^{}B as indicated. The component g_{1} : Z_{0} ^{PTIME}^{}Z_{1}⊸⊤ is trivial. Now the function sending z ∈ |Z_{0}| to (f_{0}(z), g_{0}(z)) ∈ |A_{0} ⊗ B_{0}| is a PTIME-function, too. Together with f_{1} it furnishes the desired H^{□}-morphism ⟨f, g⟩ : Z ^{}A ⊗ B. □

Lemma 4.4.4 An object D = (D_{0}, D_{1}) is duplicable in H^{□} if either D_{0} is empty or D_{1} is duplicable in H.

Proof. Immediate calculation. □

Proposition 4.4.5 The category H^{□} has linear exponentials X⊸Y if X and Y are safe. In this case X⊸Y is also safe and explicitly given by (⊤, X_{1}⊸Y_{1}).

Proof. If X, Y are safe and P = (P_{0}, P_{1}) is arbitrary then a H^{□} morphism from P ⊗X to Y is given by a PTIME-function from P_{0} to (P_{1} ⊗X)⊸Y. But (P_{1} ⊗X)⊸Y is isomorphic to P_{1}⊸(X⊸Y) whence we obtain a PTIME-function from P_{0} to P_{1}⊸X⊸Y which gives a H^{□}-morphism from P to X⊸Y. Inverting this process gives the other direction of the required natural isomorphism. □

We note that H^{□} is not an ALCC; in particular the linear function space (N, ⊤)⊸(⊤, N) does not exist in H^{□}. Suppose for a contradiction that A = (A_{0}, A_{1}) was such function space. Then, in particular, we would have an evaluation map ev : A_{0} ⊗ N ^{PTIME}^{}A_{1}⊸N which has the property that for every “true” PTIME-function f there exist elements a_{0} ∈ |A_{0}| and a_{1} ∈ |A_{1}| such that f(x) = ev(a_{0}, x)(a_{1}). But this would mean that ev is a universal polynomial time computable function which is impossible by diagonalisation.

The lacking function spaces can be added to H^{□} by moving to the functor category Ext(H^{□}) described in Chapter 2. In order that this functor category exists we must make sure that the category H^{□} is small.

This can be achieved by requiring that the underlying sets of H-sets be taken from a suitably chosen universe U closed under all set-theoretic operations required to form the H-sets of interest. Note that such universe can be defined by a simple inductive process and in particular no “large cardinal assumption” is needed for its existence.

It now follows from the results presented in Section 2.7.6 that Ext(H^{□}) is an ALCC and that the Yoneda embedding Y : H^{□} ^{}Ext(H^{□}) preserves the ALC structure as well as existing linear function spaces and cartesian products. In particular, the linear function spaces between safe objects are preserved by the embedding.

Moreover, Ext(H^{□}) supports a comonad ! with the property that whenever D ∈ H^{□} is duplicable then ! D = D in Ext(H^{□}) and for arbitrary presheaf F ∈ Ext(H^{□}) the presheaf ! F is duplicable.

4.4.1 Polynomial-time functions via a comonad

In this section we identify a comonad □ on Ext(H^{□}) which has the property that if X is safe then □(X) ≅ (X, ⊤) so that by Prop. 2.7.8 we have □(X)⊸F_{(Y_{0},Y_{1})} ≅ F_{(Y_{0}⊗X,Y_{1})}.

For presheaf F we define □F by

□F_{(Z_{0},Z_{1})} = F_{(Z_{0},⊤)}

Notice that if X is safe then □(Y(X)) ≅ Y(□X): At argument Z both presheaves consist of PTIME-functions from Z_{0} to X.

If Z ∈ H^{□} write Z_{0} for the normal object (Z_{0}, ⊤) and p_{Z} : Y ^{}Z_{0} for the obvious projection arising from Z ≅ Z_{0} ⊗ Z_{1}. The counit unbox_{F} : □F ^{}F is then defined by (unbox_{F})_{Z} = F_{p_{Z}}.

Like in Section 2.6.12.3 we define the comultiplication as the identity in view of □□F = □F.

If f : □F ^{}G then the lifted morphism f^{□} : □F ^{}□G is given concretely by f_{(Z_{0},Z_{1})}^{□} = f_{(Z_{0},⊤)}.

Proposition 4.4.6 For presheaves F, G we have F ⊗ □G = F × □G and □F ⊸G = □F →G where ×, → are cartesian product and ordinary function space of presheaves. Moreover, □(F ⊗ G) = □F ⊗ □G = □F × □G.

Proof. Suppose that X, Y ∈ H^{□}, write Y_{0} for the normal object (Y_{0}, ⊤). Suppose, furthermore, that f ∈ F_{X} and g ∈ □G_{Y}. We have (f, g) ∈ (F ⊗ □G)_{(X,Y)} by

(X, Y) ^{}X × Y_{0} ^{Prop 4.4.3}^{}X ⊗ Y_{0}

and f̄ = f, ḡ = g.

The equality of the function spaces then is a direct consequence:

□F ⊸G_{(X,Y)}

= Ext(H^{□})((X, Y) ⊗ □F, G)
= Ext(H^{□})((X, Y) × □F, G)
= □F →G_{(X,Y)}

The last part is similar to the first. □

Proposition 4.4.7 Let F ∈ Ext(H^{□}). We have ! □F = □! F = □F.

Proof. Clearly, □! F ⊆ □F and ! □F ⊆ □F. To show □F ⊆ ! □F we notice that whenever X = (X_{0}, X_{1}) ∈ H^{□} then D = (X_{0}, ⊤) is duplicable and so, if f ∈ □F_{X} then f ∈ ! □F_{X} can be witnessed by the projection X ^{}D and f itself. Since D is normal this also shows the other inclusion. □

Finally, we notice that G(□F) ≅ G(F) as G(□F) ≅ F_{⊤} = F_{(⊤,⊤)} = □F_{(⊤,⊤)} ≅ G(□F). Also recall from Prop. 2.7.9 that G(! F) = G(F). We will now see how the recursion patterns defined in Theorems 4.3.4 and 4.3.6 can be lifted to Ext(H^{□}).

Theorem 4.4.8 Let X be a safe presheaf. There exists a global element

rec^{N} : !(□N⊸X⊸X)⊸□N⊸X⊸X

such that the following equation holds for global elements h : □N⊸X⊸X, g : X, x ∈ G(N) \ {0}.

rec^{N} h 0 g = g
rec^{N} h x g = h x (rec^{N} h ⌊\frac{x}{2}⌋ g)

Proof. We must define a natural transformation from !(□N⊸X⊸X) to □N ⊸ X ⊸ X.

Suppose that Z ∈ H^{□} and assume

h ∈ !(□N⊸X⊸X)_{Z}

witnessed by t : Z ^{}D and h̄ ∈ (□N⊸X⊸X)_{D} where D = (D_{0}, D_{1}) is duplicable. Now, in view of Prop. 2.7.8 we have

\begin{matrix}(□N⊸X⊸X)_{Z} ≅ H^{□}((Z_{0} ⊗ N, Z_{1}) , (⊤, X⊸X)) \\ (□N⊸X⊸X)_{D} ≅ H^{□}((D_{0} ⊗ N, D_{1}) , (⊤, X⊸X)) \end{matrix}

h̄ : D_{0} ⊗ N ^{PTIME}^{}(D_{1} ⊗ X)⊸X

and

h : Z_{0} ⊗ N ^{PTIME}^{}(Z_{1} ⊗ X)⊸X

and t_{0} : Z_{0} ^{PTIME}^{}D_{0}, t_{1} : Z_{0} ^{PTIME}^{}Z_{1}⊸D_{1} and

h(z_{0}, x)(z_{1}, g) = h̄(t_{0}(z_{0}), x)(t_{1}(z_{0}, z_{1}), g)

We must define (again, neglecting isomorphism) a function

f = rec_{Z}^{N}(h) : Z_{0} ⊗ N ^{PTIME}^{}Z_{1}⊸X⊸X

such that

\begin{matrix}f(0)(g) = g \\ f(x)(g) = h(z_{0}, x)(f(⌊\frac{x}{2}⌋)(g)) \end{matrix}

Since D = (D_{0}, D_{1}) is duplicable we know by Lemma 4.4.4 that either D_{0} is empty or D_{1} is a duplicable H-set. In the former case, Z_{0} must also be empty and so f will be the empty function. Otherwise, Corollary 4.3.5 gives us a function f′ : D_{0} ⊗ N ^{PTIME}^{}D_{1}⊸X⊸X such that

\begin{matrix}f′(d_{0}, 0)(d_{1}, g) = g \\ f′(d_{0}, x)(d_{1}, g) = h̄(d_{0}, x)(f′(d_{0}, ⌊\frac{x}{2}⌋)(g)) \end{matrix}

Now we define

f(z_{0}, x) = λz_{1}g.f′(t_{0}(z_{0}), x) (t_{1}(z_{0}) z_{1}) g

and the desired equations follow by induction on x. Since the thus defined f is uniquely determined by the recursive equations it does not depend on the witness h̄.

Naturality of the assignment f ↦ rec^{N}(h) means that recursive definition respects substitution of parameters and is also readily established by induction on x. □

Similarly, we can lift tree recursion to Ext(H^{□}):

Theorem 4.4.9 Let A, X be safe presheaves. There exists a global element

rec^{T(A)} : !(□A⊸X)⊸!(□A⊸□T(A)⊸□T(A)⊸X⊸X⊸X)⊸□T(A)⊸X

such that the following equations are valid for appropriate global elements.

rec^{T(A)}(g, h, leaf(a)) = g
rec^{T(A)}(g, h, node(a, l, r)) = h(a, l, r, rec^{T(A)}(g, h, l), rec^{T(A)}(g, h, r))

Proof. Analogous to the previous one, this time using Cor. 4.3.7. □

In this way, other recursion patterns we might be interested in can also be lifted to Ext(H^{□}).

4.4.2 Interpretation of SLR

We are now ready to define an interpretation of SLR without rule S-Ax in Ext(ℂ). We show later in Section 4.5 how to encompass that rule if so desired.

To each aspect a we associate a functor

F_{a}(X) = X, if a = (nonmodal, linear) F_{a}(X) = ! X, if a = (nonmodal, nonlinear) F_{a}(X) = □X, if a = (modal, nonlinear)

We also define a natural transformation ε_{a} : F_{a} ^{}Id as either the identity, derelict, or unbox.

If f : F_{a}(X) ^{}Y then we write f^{a} : F_{a}(X) ^{}F_{a}(X) for the Kleisli lifting of f w.r.t. F_{a} according to Section 2.6.12.1. If a <: a′ then we define a natural transformation ι_{a,a′} : F_{a}(X) ^{}F_{a′}(X) by

ι_{a,a′} = ε_{a}  if a = (nonmodal, linear)
ι_{a,a′} = id  if a = a′
ι_{a,a′} = !(unbox) = unbox^{!} if a = (modal, nonlinear), a′ = (nonmodal, nonlinear)

Let η be a partial function mapping type variables to objects of H and A be a type. The presheaf [[A]]η is defined by

[[X]]η = η(X) [[N]]η = N [[L(A)]]η = L([[A]]η) [[T(A)]]η = T([[A]]η)

[[A ⟶^{a} B]]η = F_{a}([[A]]η)⊸[[B]]η

[[∀X.A]]η = ∏_{B∈H}[[A]]η[X ↦ B] [[A × B]]η = [[A]]η × [[B]]η [[A ⊗ B]]η = [[A]]η ⊗ [[B]]η

Here ∏_{B∈H} is a |H|-indexed cartesian product of presheaves, see Section 2.6.10.2.

We notice that if A is safe then [[A]]η is a safe object so that the defining clauses for L(A) and T(A) make sense.

A context Γ = x_{1} :^{a_{1}} A_{1}, …, x_{n} :^{a_{n}} A_{n} gets interpreted as the tensor product

[[Γ]]η =_{def} F_{a_{1}}([[A_{1}]]η) ⊗ ⋯ ⊗ F_{a_{n}}([[A_{n}]]η)

A derivation of a judgement Γ ⊢ e : A gets interpreted as a morphism

[[Γ ⊢ e : A]]η : [[Γ]]η ^{}[[A]]η

Before actually defining this interpretation let us warn the reader that we will not prove that the interpretation is independent of the chosen typing derivation. Neither will we prove that it enjoys one or the other substitution property and neither will we prove that it validates whatsoever equational theory between terms. We are confident that such properties could be established if so desired, but they are not needed for the present development.

4.4.2.1 Constants and variables

A variable gets interpreted as the corresponding projection morphism possibly followed by a counit ε_{a}. For example, if a = (modal, nonlinear) then Γ, x:^{a} A ⊢ x : A gets interpreted as the morphism

[[Γ, x:^{a} A]]η = [[Γ]]η ⊗ □[[A]]η ^{projection}^{}□[[A]]η ^{unbox}^{}[[A]]η

The first order constants such as S_{0}, S_{1}, node, etc. get interpreted by applying the Yoneda embedding to their interpretations in H^{□}. The recursors are interpreted as the terminal projection [[Γ]] ^{}⊤ followed by the global elements defined in Theorems 4.4.8, 4.4.9.

4.4.2.2 Application and abstraction

It follows from Propositions 2.7.9, 4.4.7, and Lemma 2.7.3 that [[Γ]]η is duplicable whenever Γ is nonlinear.

More generally, if Γ is nonlinear and Δ_{1}, Δ_{2} are arbitrary as in rules T-Arr-E and T-Tens-E then we can define a map

v_{Γ,Δ_{1},Δ_{2}} : [[Γ, Δ_{1}, Δ_{2}]]η ^{}[[Γ, Δ_{1}]]η ⊗ [[Γ, Δ_{2}]]η

as

v_{Γ,Δ_{1},Δ_{2}} =_{def} w_{1} ∘(δ ⊗ ([[Δ_{1}]]η ⊗ [[Δ_{2}]]η)) ∘ w_{2}

where δ is the diagonal on [[Γ]]η and w_{1}, w_{2} are wiring maps.

Suppose now that Γ, x:^{a} A ⊢ e : B and let f : [[Γ]]η ⊗ F_{a}([[A]]η) ^{}[[B]]η be the interpretation of e. The currying or exponential transpose of this morphism yields a map [[Γ]] ^{}F_{a}([[A]]η)⊸[[B]]η which serves as the interpretation of λx: A.e : A ⟶^{a} B.

Now suppose that

Γ, Δ_{1} ⊢ e_{1} : A ⟶^{a} B

Γ, Δ_{2} ⊢ e_{2} : A Γ, Δ_{2} <: a Γ nonlinear

as in the premises to rule T-Arr-E and let

\begin{matrix}f_{1} : [[Γ]]η ⊗ [[Δ_{1}]]η ^{}F_{a}([[A]]η)⊸[[B]]η \\ f_{2} : [[Γ]]η ⊗ [[Δ_{2}]]η ^{}[[A]]η \end{matrix}

be the interpretations of e_{1} and e_{2}. The side condition on the aspects in Γ, Δ_{2} together with the fact that □ commutes with ⊗ allows us to “raise” f_{2} to a morphism f^{□} : [[Γ]]η ⊗ [[Δ_{2}]]η ^{}F_{a}([[A]]η).

Now the interpretation of e_{1}e_{2} is obtained as

ev ∘(f_{1} ⊗ f_{2}^{□}) ∘ v_{Γ,Δ_{1},Δ_{2}}

where

ev : (F_{a}([[A]]η)⊸[[B]]η) ⊗ [[A]]η ^{}[[B]]η

is the evaluation morphism and v_{Γ,Δ_{1},Δ_{2}} is defined as described above using the fact that Γ is nonlinear.

4.4.2.3 Polymorphic abstraction and application

Suppose that Γ ⊢ e : A and that X does not occur in Γ. Then the interpretation of Γ ⊢ ΛX.e : ∀X.A is defined by

[[Γ ⊢ ΛX.e : ∀X.A]]η = ⟨[[Γ ⊢ e : A]]η[X ↦ B] | B∈ H⟩

If Γ ⊢ e : ∀X.A and B is safe then we define

[[Γ ⊢ e[B] : A[B/X]]]η = π_{[[B]]η} ∘ [[Γ ⊢ e : ∀X.A]]η

4.4.2.4 Cartesian products

Suppose that Γ ⊢ e_{1} : A_{1}, Γ ⊢ e_{2} : A_{2}. Then we define

[[Γ ⊢ ⟨e_{1}, e_{2}⟩ : A_{1} × A_{2}]]η = ⟨[[Γ ⊢ e_{1} : A_{1}]]η, [[Γ ⊢ e_{2} : A_{2}]]η⟩

where ⟨−, −⟩ is the pairing operation associated with cartesian products in H^{□}. If Γ ⊢ e : A_{1} × A_{2} then we define

[[Γ ⊢ e.i : A_{i}]]η = π_{i} ∘ [[Γ ⊢ e : A_{1} × A_{2}]]η

where π_{i} : [[A_{1} × A_{2}]]η ^{}[[A_{i}]]η is the projection morphism.

4.4.2.5 Tensor products

Suppose that Γ, Δ_{i} ⊢ e_{i} : A_{i} and that Γ is nonlinear as in the premise to rule T-Tens-I. Using the diagonal on [[Γ]]η we can define a map

v : [[Γ, Δ_{1}, Δ_{2}]]η ^{}[[Γ, Δ_{1}]]η ⊗ [[Γ, Δ_{2}]]η

as in the previous case. Then we define,

[[Γ, Δ_{1}, Δ_{2} ⊢ e_{1} ⊗ e_{2} : A_{1} ⊗ A_{2}]]η = ([[Γ, Δ_{1} ⊢ e_{1} : A_{1}]]η ⊗ [[Γ, Δ_{2} ⊢ e_{2} : A_{2}]]η) ∘ v

Now suppose that

Γ, Δ_{1} ⊢ e_{1} : A_{1} ⊗ A_{2}

Γ, Δ_{2}, x:^{a_{1}} A_{1}, x:^{a_{2}} A_{2} ⊢ e_{2} : B

Γ, Δ_{1} <: a_{1} ∧ a_{2} Γ nonlinear

as in the premise to T-Tens-E.

Let f_{1} : [[Γ, Δ_{⊢}e_{1}]]η ^{}[[A_{1} ⊗ A_{2}]] and f_{2} : [[Γ, Δ_{2}, x:^{a_{1}} A_{1}, x:^{a_{2}} A_{2}]] ^{}[[B]] be the interpretations of e_{1} and e_{2}.

Since Γ, Δ_{1} <: a_{1} ∧ a_{2} we can raise f_{1} to form a morphism

f_{1}^{a_{1}∧a_{2}} : [[Γ, Δ_{1}]] ^{}F_{a}([[A_{1}]]) ⊗ F_{a}([[A_{2}]])

from which we obtain

g =_{def} (ι_{a_{1}∧a_{2},a_{2}} ⊗ ι_{a_{1}∧a_{2},a_{2}}) ∘ f_{1}^{a_{1}∧a_{2}} : [[Γ, Δ_{1}]] ^{}F_{a_{1}}([[A_{1}]]) ⊗ F_{a_{2}}([[A_{2}]])

We then define the interpretation of Γ, Δ_{1}, Δ_{2} ⊢ let e_{1} = x ⊗ y in e_{2} : B as

f_{2} ∘([[Γ, Δ_{2}]] ⊗ g) ∘ v_{Γ,Δ_{1},Δ_{2}}

where v_{Γ,Δ_{1},Δ_{2}} is as above in Section 4.4.2.2

4.4.2.6 Subtyping and subsumption

By induction on the definition of subtyping we define a coercion map ι_{A,B} : [[A]]η ^{}[[B]]η when A <: B using identity, composition, the mappings ι_{a,a′}, and functoriality of the semantic type formers ⊸, ⊗, ×, ∏.

If Γ ⊢ e : B was obtained from Γ ⊢ e : A and A <: B by rule T-Sub then we define

[[Γ ⊢ e : B]]η = ι_{A,B} ∘ [[Γ ⊢ e : A]]η

4.4.3 Main result

Let us temporarily write [[−]]^{S} for the set-theoretic interpretation and [[−]]^{F} for the interpretation in Ext(H^{□}).

If f ∈ Ext(H^{□})(□N, N) then it follows from the Yoneda Lemma and the discussion in Section 2.6.10.4 that G(f) : ℕ ≅ G(□N) ^{}G(N) ≅ ℕ is a polynomial time computable function. This means that if e : □N→N is a closed term then G([[f]]^{F}) is a PTIME-function and the same goes for functions with several arguments. It remains to show that the thus obtained function coincides with the intended set-theoretic meaning. To do this, we use again a logical relation.

Let η, ζ be partial functions mapping type variables to sets in U and safe presheaves, respectively. Let ρ be a partial function which assigns to each type variable X a binary relation ρ(X) ⊆ η(X) × G(ζ(X)). We define a relation R_{η,ζ,ρ}(A) ⊆ [[A]]^{S}η × G([[A]]^{F} ζ) by

x R_{η,ζ,ρ}(X) y ⟺ x ρ(X) y
x R_{η,ζ,ρ}(N) y ⟺ x = y
u R_{η,ζ,ρ}(A ⟶^{a} B) v ⟺
∀x∈ [[A]]^{S}η.∀y∈ G([[A]]^{F} ρ).x R_{η,ζ,ρ}(A) y ⇒ u(x) R_{η,ζ,ρ}(B) v(y)
u R_{η,ζ,ρ}(∀X.A) v ⟺
∀U, V ∈ H.∀R ⊆ |U| × |V |.π_{U}(u) R_{η[X↦U],ζ[X↦V],ρ[X↦R]}(A) π_{V} (v)
(u_{1}, u_{2}) R_{η,ζ,ρ}(A_{1} × A_{2}) (v_{1}, v_{2}) ⟺ u_{i} R_{η,ζ,ρ}(A_{i}) v_{i} i = 1, 2
(u_{1}, u_{2}) R_{η,ζ,ρ}(A_{1} ⊗ A_{2}) (v_{1}, v_{2}) ⟺ u_{i} R_{η,ζ,ρ}(A_{i}) v_{i} i = 1, 2

The relations R_{η,ζ,ρ}(L(A)) and R_{η,ζ,ρ}(T(A)) are defined inductively by

nil R_{η,ζ,ρ}(L(A)) nil always
cons(a, l) R_{η,ζ,ρ} cons(a′, l′) ⟺
a R_{η,ζ,ρ}(A) a′ ∧ l R_{η,ζ,ρ}(L(A)) l′
leaf(a) R_{η,ζ,ρ}(T(A)) leaf(a′) ⟺ a R_{η,ζ,ρ}(A) a′
node(a, l, r) R_{η,ζ,ρ}(T(A)) node(a′, l′, r′) ⟺
a R_{η,ζ,ρ}(A) a′ ∧ l R_{η,ζ,ρ}(T(A)) l′ ∧ r R_{η,ζ,ρ}(T(A)) r′

Now, a direct inspection of the definitions, and a reasoning similar to the one in Section 2.6.10.6 shows that we have

[[c]]^{S}R_{A}G([[c]]^{F})

for every SLR-constant c : A.

Next we show by induction on subtyping derivations that whenever A <: B then

∀x∈ [[A]]^{S}η.∀y∈ G([[A]]^{F} ρ).x R_{η,ζ,ρ}(A) y ⇒ x R_{η,ζ,ρ}(B) ι_{A,B}(y)

Notice here that [[A]]^{S} = [[B]]^{S} whenever A <: B. Finally, by induction on typing derivations we prove the following extension of Theorem 2.6.2

Proposition 4.4.10 Suppose that Γ ⊢ e : A and that η, ζ, ρ are assignments as above. Suppose, further that γ^{S} ∈ G([[Γ]]^{S}) and γ^{F} ∈ G([[Γ]]^{F}) are such that γ^{S}(x) R_{η,ζ,ρ}(Γ(x)) γ^{F} (x) for each x ∈ dom(Γ). Then

[[Γ ⊢ e : A]]η^{S}(γ^{S}) R_{η,ζ,ρ}(A) [[Γ ⊢ e : A]]ρ^{F}

Corollary 4.4.11 The set-theoretic interpretation of a closed term f : □N→N is a polynomial time computable function.

Proof. By specialising the above proposition to Γ = ∅, A = □N→N and expanding the definitions. □

4.4.4 Interpretation of λ^{□!}

We can use Ext(H^{□}) also in order to give meaning to the alternative system λ^{□!} thus providing a proof that also the definable functions of the latter system are PTIME. Spelling this out in detail would be a rather boring exercise in typesetting so we will only set out a few of the important points. Types are interpreted as objects of Ext(H^{□}). The new type formers □,! are interpreted by the eponymous comonads on Ext(H^{□}). The associated operations on morphisms provide meaning for the term formers associated with the modalities. The defining clauses for cartesian and tensor product follow the interpretation of SLR.

It seems plausible that any other reasonable formulation of modal/linear lambda calculus including the one in [2] can be interpreted in Ext(H^{□}) in a similar fashion.

4.5 Duplicable numerals

The algebra H has the disadvantage that even values of type N may not be duplicated, i.e, there does not exist an element δ ∈ H such that δnum(x) = λf.fnum(x)num(x). Indeed, assuming such diagonal element would contradict Theorem 4.3.4 for the following reason. Multiplication is easily seen to be a morphism from N ⊗ N to N. Using the hypothetical δ we could realise the diagonal function from N to N ⊗ N and thus by composition the squaring function would be a morphism from N to N. Iterating it using safe recursion on notation would allow us to define a function of exponential growth.

In Bellantoni-Cook’s original system and in SLR duplication of values of ground type is, however, permitted and sound. The reason is that multiplication is not among the basic functions of these systems and as an invariant it is maintained that a function depending on several safe arguments of ground type is bounded by the maximum of these arguments plus a constant. Obviously, multiplication does not have this property.

In order to obtain an analogue of the algebra H we need to get a handle on the maximum of the lengths of the two components of a pair. This motivates the following definitions. First, like in Lemma 2.1.1 we fix disjoint injections

num : ℕ ^{}ℕ
pad : ℕ ^{}ℕ
⟨·, ·⟩ : ℕ × ℕ→ℕ

together with test functions isnum, ispad, ispair and inverses getnum, getpad, .1, .2 computable in linear time and satisfying specifications and size restrictions analogous to the ones in Lemma 2.1.1. We need two bits now to distinguish the ranges of the injections, so we will have |num(x)| = |pad(x)| = 2 and |⟨u, v⟩| = |u| + |v| + 2||v|| + 4. (Recall that two bits are needed to separate the |v| block from u, v.)

Definition 4.5.1 (Linear length, maximum length) The length functions ℓ_{lin}(x) (linear length) and ℓ_{max}(x) (maximum length) are defined recursively as follows.

ℓ_{lin}(num(x)) = 1
ℓ_{max}(num(x)) = |x|
ℓ_{lin}(pad(x)) = |x| + 1
ℓ_{max}(pad(x)) = 0
ℓ_{lin}(⟨x, y⟩) = 4 + ℓ_{lin}(x) + ℓ_{lin}(y)
ℓ_{max}(⟨x, y⟩) = max(ℓ_{max}(x), ℓ_{max}(y))
ℓ_{lin}(x) = |x|, otherwise
ℓ_{max}(x) = 0, otherwise

We may assume that ℓ_{lin}(0) = ℓ_{max}(0) = 0.

Lemma 4.5.2 The following inequalities hold for every x ∈ ℕ.

|x| ≥ ℓ_{lin}(x) + ℓ_{max}(x)
|x| ≤ ℓ_{lin}(x)(1 + ||x|| + ℓ_{max}(x))

Proof. By course of values induction on x. If x = num(y) then

|x| = |y| + 2 ≥ 1 + |y| = ℓ_{lin}(x) + ℓ_{max}(x)

and

|x| = |y| + 2 ≤ 1 · (1 + 2 + ℓ_{max}(x)) ≤ ℓ_{lin}(x)(1 + ||x|| + ℓ_{max}(x))

since ||2|| = 2.

If x = pad(y) then

|x| = |y| + 2 ≥ |y| + 1 = ℓ_{lin}(x) + ℓ_{max}(x)

and

|x| = |y| + 2 ≤ (|y| + 1)(1 + 2) ≤ ℓ_{lin}(x)(1 + ||x|| + ℓ_{max}(x))

The interesting case is when x = ⟨u, v⟩. In this case, we have

|⟨u, v⟩| ≥ |u| + |v| + 4 ≥^{IH} ℓ_{lin}(u) + ℓ_{lin}(v) + ℓ_{max}(u) + ℓ_{max}(v) + 4 ≥
≥ ℓ_{lin}(⟨u, v⟩) + ℓ_{max}(⟨u, v⟩)

thus establishing the first inequality. For the second we calculate as follows.

ℓ_{lin}(⟨u, v⟩)(1 + ||⟨u, v⟩|| + ℓ_{max}(⟨u, v⟩)
= (ℓ_{lin}(u) + ℓ_{lin}(v) + 4)(1 + ||⟨u, v⟩|| + max(ℓ_{max}(u), ℓ_{max}(v)))
≥ ℓ_{lin}(u)(1 + ||u|| + ℓ_{max}(u)) + ℓ_{lin}(v)(1 + ||v|| + ℓ_{max}(v)) + 4(1 + ||⟨u, v⟩||)
≥^{IH} |u| + |v| + 2||v|| + 4
= |⟨u, v⟩|

In the remaining case we have

|x| ≥ |x| + 0 = ℓ_{lin}(x) + ℓ_{max}(x)

and

|x| ≤ |x|(1 + ||x||) = ℓ_{lin}(x)(1 + ||x|| + ℓ_{max}(x))
□

Corollary 4.5.3 There exists a quadratic polynomial p such that |x| ≤ p(ℓ_{lin}(x)+ℓ_{max}(x)).

Proof. Writing u for \sqrt{|x|} we obtain for

u^{2} ≤ ℓ_{lin}(x)(1 + u + ℓ_{max}(x))

for x large enough from ||x|| ≤ \sqrt{|x|}. This gives

u ≤ ℓ_{lin}(x) + \sqrt{ℓ_{lin}(x)^{2} + 4ℓ_{lin}(x)(1 + ℓ_{max}(x))} ≤ ℓ_{lin}(x) + (ℓ_{lin}(x) + 2 + 2ℓ_{max}(x))

hence the result by squaring. □

Accordingly, any function computable in time O(|x|) is computable in time O((ℓ_{lin}(x) + ℓ_{max}(x))^{2}).

We assume an encoding of computations such that for each algorithm e and integer N ≥ ℓ_{lin}(e) we can find an algorithm e′ such that the runtime of {e′}(x) is not greater than |N| plus the time needed to compute {e}(x) and such that ℓ_{lin}(e′) ≥ N. Similarly, we assume that we can arbitrarily increase the maximum length of an algorithm. That this is in principle possible hinges on the two injections num and pad.

Let p > 2 be a fixed integer.

Definition 4.5.4 A computation {e}(x) is called short (w.r.t. p) if it needs less than d(ℓ_{lin}(e) + ℓ_{lin}(x) + max(ℓ_{max}(e), ℓ_{max}(x)))^{p} steps where d = ℓ_{lin}(e) + ℓ_{lin}(x) − ℓ_{lin}({e}(x)) and in addition ℓ_{max}({e}(x)) ≤ d+max(ℓ_{max}(e), ℓ_{max}(x)). The difference d between ℓ_{lin}(e)+ ℓ_{lin}(x) and ℓ_{lin}({e}x) is again called the defect of the computation {e}(x).

An algorithm e is called short if {e}(x) is short for all x.

For what follows it is useful to recall the following basic rules of “maxplus-arithmetic”. – x + max(y, z) = max(x + y, x + z)

– more generally, f(max(x, y)) = max(f(x), f(y)) whenever f is monotone.

– max(x, y) ≤ z ⟺ x ≤ z ∧ y ≤ z.

– max(x, y) ≤ x + y.

– max(x, y + z) ≤ y + max(x, z). The following shows that maximum-bounded number-theoretic functions are computable by short algorithms. Lemma 4.5.5 If f : ℕ^{n}→ℕ is computable in time O(max(|x_{1}|, …, |x_{n}|)^{p}) and

|f(x_{1}, …, x_{n})| = max(|x_{1}|, …, |x_{n}|) + O(1)

then there exists a short algorithm e such that

f(x_{1}, …, x_{n}) = {e}(⟨num(x_{1}), ⟨num(x_{2}), …, num(x_{n})⟩ … ⟩)

Proof. Immediate from the definitions. □

Proposition 4.5.6 There exists a function app : ℕ × ℕ ^{}ℕ and a constant γ such that

– app(e, x) is computable in time d(ℓ_{lin}(e) + ℓ_{lin}(x) + γ + max(ℓ_{max}(e), ℓ_{max}(x)))^{p} where d = ℓ_{lin}(e) + ℓ_{lin}(x) − ℓ_{lin}(app(e, x)).

– ℓ_{max}(app(e, x)) ≤ d + max(ℓ_{max}(e), ℓ_{max}(x)))

– app(e, x) = {e}(x) for every short computation {e}(x).

Proof. The following algorithm meets the required specification

read e, x; conf := init(e, x); t := 0; T := (ℓ_{lin}(e) + ℓ_{lin}(x) + max(ℓ_{max}(e) + ℓ_{max}(x)))^{p} while t ≤ (ℓ_{lin}(e) + ℓ_{lin}(x))T and term(conf ) ≠ 0 do begin

conf := step(conf);

t := t + 1; end r := out(conf); d := ℓ_{lin}(e) + ℓ_{lin}(x) − ℓ_{lin}(r); if

term(conf) = 0 and

t ≤ dT and

ℓ_{max}(r) ≤ d + max(ℓ_{max}(e), ℓ_{max}(x)) then write r else write 0

The verification runs analogous to the one in Prop. 4.1.6. □

Again, we will abbreviate app(e, x) by e · x or ex.

Lemma 4.5.7 (Parametrisation) For every e there exists an algorithm e′ such that e⟨x, y⟩ = e′xy.

Proof. Let e_{0} be the following algorithm:

read x

write “read y; write app(e, ⟨‘x, y⟩)”

Now, ℓ_{lin}({e_{0}}(x)) ≤ ℓ_{lin}(e) + ℓ_{lin}(x) + c and ℓ_{max}({e_{0}}(x)) ≤ max(ℓ_{max}(e), ℓ_{max}(x)) where c is some fixed constant.

Sufficient padding of e_{0} thus yields the desired algorithm e′. □

Theorem 4.5.8 The set of natural numbers together with the above application function app is a BCK-algebra.

Proof. The K-combinator is straightforward. For the composition combinator B we look again at the algorithm from the proof of Theorem 4.1.8.

B_{0} ≡ read w write app(w.1.1, app(w.1.2, w.2))

We have {B_{0}}(⟨⟨x, y⟩, z⟩) = x(yz) and the time t_{tot} needed to perform this computation is less than t_{1} + t_{2} + t_{b} where

u = yz
w = x(yz)
d_{1} = ℓ_{lin}(y) + ℓ_{lin}(z) − ℓ_{lin}(u)
d_{2} = ℓ_{lin}(x) + ℓ_{lin}(u) − ℓ_{lin}(w)
t_{1} = d_{1}(ℓ_{lin}(y) + ℓ_{lin}(z) + γ + max(ℓ_{max}(y), ℓ_{max}(z)))^{p}
t_{2} = d_{2}(ℓ_{lin}(x) + ℓ_{lin}(u) + γ + max(ℓ_{max}(x), ℓ_{max}(u)))^{p}

and where t_{b}—the time needed for shuffling around intermediate results—is linear in |x| + |y|+|z|+|u|+|w| and thus quadratic in ℓ_{lin}(x)+ℓ_{lin}(y)+ℓ_{lin}(z)+max(ℓ_{max}(x), ℓ_{max}(y), ℓ_{max}(z)). Now

ℓ_{max}(u) ≤ d_{1} + max(ℓ_{max}(y), ℓ_{max}(z))

So,

t_{2} ≤ d_{2}(ℓ_{lin}(x) + ℓ_{lin}(u) + γ+
max(ℓ_{max}(x), (ℓ_{lin}(y) + ℓ_{lin}(z) − ℓ_{lin}(u)) + max(ℓ_{max}(y), ℓ_{max}(z))))^{p}
≤ d_{2}(ℓ_{lin}(x) + ℓ_{lin}(y) + ℓ_{lin}(z) + γ + max(ℓ_{max}(x), ℓ_{max}(y), ℓ_{max}(z)))^{p}

This means that we can find a constant c such that

t_{tot} ≤ (d_{1} + d_{2})(ℓ_{lin}(x) + ℓ_{lin}(y) + ℓ_{lin}(z) + c + max(ℓ_{max}(x), ℓ_{max}(y), ℓ_{max}(z)))^{p}

Now the defect of the computation {B_{0}}(⟨⟨x, y⟩, z⟩) equals ℓ_{lin}(B_{0}) + 4 + ℓ_{lin}(x) + ℓ_{lin}(y) + ℓ_{lin}(z) − ℓ_{lin}(w) = ℓ_{lin}(B_{0}) + 4 + d_{1} + d_{2}.

Moreover,

ℓ_{max}(w)

≤ d_{2} + max(ℓ_{max}(x), ℓ_{max}(u))
≤ d_{2} + max(ℓ_{max}(x), d_{1} + max(ℓ_{max}(y), ℓ_{max}(z)))
≤ d_{2} + d_{1} + max(ℓ_{max}(x), ℓ_{max}(y), ℓ_{max}(z))

Therefore, by choosing ℓ_{lin}(B_{0}) large enough we obtain

app(B_{0}, ⟨⟨x, y⟩, z⟩) = {B_{0}}(⟨⟨x, y⟩, z⟩) = x(yz)

The desired algorithm B is then obtained by applying Lemma 4.5.7 twice.

Notice that the coupling of defect and maximum length is essential for the definability of composition.

We come to the twisting combinator C. Again, we start with the algorithm

C_{0} ≡ read w write app(app(w.1.1, w.2), w.1.2)

Clearly,

{C_{0}}(⟨⟨x, y⟩, z⟩) = xzy

The total time t_{tot} needed for this computation is bounded by t_{1} + t_{2} + t_{b} where

u = xz
w = uy
d_{1} = ℓ_{lin}(x) + ℓ_{lin}(z) − ℓ_{lin}(u)
d_{2} = ℓ_{lin}(u) + ℓ_{lin}(y) − ℓ_{lin}(w)
t_{1} = d_{1}(ℓ_{lin}(x) + ℓ_{lin}(z) + γ + max(ℓ_{max}(x), ℓ_{max}(z))^{p})
t_{2} = d_{2}(ℓ_{lin}(u) + ℓ_{lin}(y) + γ + max(ℓ_{max}(u), ℓ_{max}(y))^{p})

and the time t_{b} needed for administration is

O((ℓ_{lin}(x) + ℓ_{lin}(y) + ℓ_{lin}(z) + max(ℓ_{max}(x), ℓ_{max}(y), ℓ_{max}(z)))^{2})

Now,

t_{2} ≤ d_{2}(ℓ_{lin}(u) + ℓ_{lin}(y) + γ+
max(ℓ_{lin}(x) + ℓ_{lin}(z) − ℓ_{lin}(u) + max(ℓ_{max}(x), ℓ_{max}(z)), ℓ_{max}(y)))^{p}
≤ d_{2}(ℓ_{lin}(x) + ℓ_{lin}(y) + ℓ_{lin}(z) + max(ℓ_{max}(x), ℓ_{max}(y), ℓ_{max}(z)))^{p}

and we can find a constant c such that

t_{tot} ≤ (d_{1} + d_{2})(ℓ_{lin}(x) + ℓ_{lin}(y) + ℓ_{lin}(z) + max(ℓ_{max}(x), ℓ_{max}(y), ℓ_{max}(z)))

The defect of the computation {C_{0}}(⟨⟨x, y⟩, z⟩) is

d = ℓ_{lin}(C_{0}) + 8 + ℓ_{lin}(x) + ℓ_{lin}(y) + ℓ_{lin}(z) − ℓ_{lin}(w) = ℓ_{lin}(C_{0}) + 8 + d_{1} + d_{2}

Therefore, by choosing ℓ_{lin}(C_{0}) large enough we obtain the desired combinator as in the proof of Theorem 4.1.8. □

The thus obtained BCK-algebra will henceforth be called M_{p}. The next theorem shows that all the previous machinery about H^{□} can be applied to M_{p} as well.

Theorem 4.5.9 The BCK-algebra M_{p} is polynomial with respect to the length measure ℓ(x) = ℓ_{lin}(x) + ℓ_{max}(x).

Proof. It is clear that xy is computable in time polynomial in ℓ(x)+ℓ(y). For the estimate ℓ(xy) ≤ ℓ(x) + ℓ(y) we calculate as follows: If x, y ∈ M_{p} then ℓ_{lin}(xy) ≤ ℓ_{lin}(x) + ℓ_{lin}(y) and ℓ_{max}(xy) ≤ d + max(ℓ_{max}(x), ℓ_{max}(y) where d = ℓ_{lin}(x) + ℓ_{lin}(y) − ℓ_{lin}(xy). Thus, ℓ(xy) = ℓ_{lin}(xy) + ℓ_{max}(xy) ≤ ℓ_{lin}(x) + ℓ_{lin}(y) − d + d + max(ℓ_{max}(x), ℓ_{max}(y)) ≤ ℓ(x) + ℓ(y).

The other estimates involving | − | are direct from the definition. □

The following shows that in the category of M_{p}-sets natural numbers are duplicable.

Proposition 4.5.10 Let A be an M_{p}-set such that there exists k ∈ ℕ with ℓ_{lin}(x) ≤ k for all x ∈ dom(A). Then A is duplicable, i.e., the diagonal function δ_{A} : A ^{}A⊗A defined by δ_{A}(x) = (x, x) is realisable.

Proof. Consider the following algorithm

dup ≡ read x write λf.f x x

It is clear that if this algorithm is short then it is a realiser for the diagonal function δ : N ^{}N ⊗ N. Its running time is linear thus at most quadratic in ℓ_{lin}(x) + ℓ_{max}(x). In view of the characterisation in Lemma 4.5.5 it thus remains to show that it meets the growth restrictions

ℓ_{lin}(dup(x)) = ℓ_{lin}(x) + O(1)
ℓ_{max}(dup(x)) = ℓ_{max}(x) + O(1)

If x ∈ dom(A) then ℓ_{lin}(x) ≤ k by assumption, so

ℓ_{lin}(dup(x)) = ℓ_{lin}(λf.fxx) = ℓ_{lin}(x) + ℓ_{lin}(x) + O(1) = O(1)

Next,

ℓ_{max}(dup(x)) = ℓ_{max}(λf.fxx) = max(ℓ_{max}(x), ℓ_{max}(x)) + O(1) = ℓ_{max}(x) + O(1)
□

It now follows from Prop. 2.7.9 that N = ! N in the category Ext(H^{□}) constructed over H = M_{p} thus providing an interpretation of SLR with the axiom SLR and hence a soundness proof for the latter system.

4.6 Computational interpretation

The interpretation in Ext(H^{□}) although maybe complicated is entirely constructive and can be formalised for instance in extensional Martin-Löf type theory with quotient types, see [16]. Such formalisation gives rise to an algorithm which computes a polynomial time algorithm for every term of type □N→N. The efficiency of such compilation algorithm and, more importantly, of the produced polynomial time algorithms hinges on the form of the soundness proof. A detailed analysis falls outside the scope of this thesis and must await further investigation.

We would like at this point, however, to comment on the apparent overhead involved with the runtime monitoring in the application function of the BCK-algebras involved. Intuitively, explicit runtime bounds could be omitted since we know anyway that all welltyped programs terminate within the allocated time. However, in doing so we change the behaviour of those programs which contain as a subcomputation an application which returns zero because of runtime exhaustion. In principle this might affect the results; in order to show that this is not so one can replace the category H by a category in which runtime bounds are built into the definition of morphisms.

Definition 4.6.1 Let p > 2. The category H′ has as objects pairs X = (|X|, ⊩_{X}) where |X| is a set and ⊩⊆ ℕ × |X| is a surjective relation. A morphism from X to Y is a function f : |X| ^{}|Y | such that there exists an algorithm e with the property that whenever t ⊩_{X} x then {e}(x) terminates with a result y such that y ⊩_{Y} f(x) and moreover the runtime of {e}(x) is less than (ℓ(e) + ℓ(x) − ℓ(y))(ℓ(e) + ℓ(x))^{p}.

An analogous definition can be given relative to M_{p}.

One can now show that H′ has essentially the same category-theoretic properties as H, but no explicit runtime computations appear in H. The big disadvantage of H′ as opposed to H is that we have to carry out calculations on the level of algorithms for every single construction in H′, whereas with H these calculations can be concentrated in the proof that H_{p} forms a polynomial time BCK-algebra. After that all the verifications can be carried out on the higher level of abstraction given by untyped linear lambda calculus.

4.7 More BCK -algebras

In this section we describe two more BCK-algebras. The first one is constructed from linear lambda terms rather than Turing machines and provides an alternative proof of the main result. The second has the property that exactly the PTIME-functions are represented in it as algorithms sending numerals to numerals. We do not have at present have a particular application for this algebra.

Finally, we mention that there exists a BCK-algebra similar to H_{p} but more general as in the ensuing category of realisability sets iteration patterns are available which allow one to define certain recursive functions without moving to the modal function space. In particular the insertion sort algorithm mentioned in Section 3.2.3 admits a natural representation in that category.

The details of this construction are worked out separately in [19].

4.7.1 Linear lambda terms

Rather than using H_{p} we can also work with untyped linear lambda terms directly. In order to represent numbers and booleans we augment these by constants tt, ff, D, num(n), for n ∈ ℕ and constants for linearly bounded polynomial time numerical functions such as S_{0}, S_{1}, test for zero, etc. such lambda terms can be evaluated by performing beta reductions and reducing fully applied function constants according to their specification, i.e., if f is a constant for the function f : ℕ→ℕ then we evaluate fnum(n) in one step to num(f(n)). By appropriately defining the length of all constants we can achieve that reduction steps do not increase the length which shows that evaluation to normal form can be done in polynomial time.

If we want to have duplicable numerals this syntactic approach becomes more difficult. It appears that by using a shared environment for N-valued variables similar to [2] one can do it, but details have not been worked out.

4.7.2 A BCK -algebra of all PTIME -functions

Next, there exists a BCK-algebra P in which all polynomial time functions and not only the linearly bounded ones are representable. Such algebra cannot be polynomial time computable in the sense of Definition 4.3.1, because if all polynomial time functions are represented then application cannot be polynomial time computable. However, there is an injection num : ℕ ^{}P such that for each e ∈ H the function n ↦ e num(x) is polynomial time computable.

Recall the coding functions ⟨−, −⟩ and num from Lemma 2.1.1 and the length function ℓ.

The carrier of P is the set of integers of the form ⟨n, e⟩ where n ≥ 1. The idea is that e is (a Gödel number of) an algorithm and that n is an “exponent” controlling the runtime of e.

The application function is given as follows.

⟨m, e⟩⟨n, x⟩ = \{ \begin{matrix}⟨k, y⟩, & if\{e\}(⟨m, x⟩) = ⟨k, y⟩ \\ & and this computation takes ≤ (ℓ(e) + ℓ(x))^{mn} steps \\ & and k ≤ mn and y ≤ (ℓ(e) + ℓ(x))^{mn/k} \\ ⟨1, 0⟩, & otherwise \end{matrix}

Using an algorithm similar to the one for H_{p} it follows that there is a constant γ such that whenever find that ⟨m, e⟩⟨n, x⟩ = ⟨k, y⟩ then this application is computable in time (ℓ(e) + ℓ(x) + γ)^{mn/k}.

Proposition 4.7.1 (Composition) For ⟨m, e⟩ ∈ P and ⟨m′, e′⟩ ∈ P there exists ⟨r, f⟩ ∈ P such that

⟨m, e⟩(⟨m′, e′⟩⟨n, x⟩) = ⟨r, f⟩⟨n, x⟩

for all ⟨n, x⟩ ∈ P. Moreover, r ≤ 2mm′ and ℓ(f) = ℓ(e) + ℓ(e′) + O(1).

Proof. Suppose that ⟨e′, m′⟩⟨n, x⟩ = ⟨k, y⟩. We have k ≤ m′n and y ≤ (ℓ(e′)+ℓ(x))^{mn/k}. The time needed for this computation is T_{1} = (ℓ(e′) + ℓ(x) + γ)^{m′n}.

Now, the time needed to compute ⟨m, e⟩⟨k, y⟩ is T_{2} = (ℓ(e) + ℓ(y))^{mk} ≤ (ℓ(e) + (ℓ(e′) + ℓ(x))^{m′n/k} + γ)^{mk} ≤ (ℓ(e) + ℓ(e′) + ℓ(x) + γ)^{mm′n}. Let ⟨q, z⟩ be the result of this computation. We have q ≤ mk ≤ mm′n and also ℓ(z) ≤ (ℓ(e) + ℓ(y))^{mk/q} ≤ (ℓ(e) + (ℓ(e′) + ℓ(x))^{m′n/k})^{mk/q} ≤ (ℓ(e) + ℓ(e′) + ℓ(x))^{mm′n/q}. The total time to compute ⟨m, e⟩(⟨m′, e′⟩⟨n, x⟩) is therefore less than (ℓ(e)+ℓ(e′)+ℓ(x)+γ)^{2mm′k} (the factor 2 is overly generous, but never mind), so we can find ⟨r, f⟩ computing the composition of ⟨m, e⟩ and ⟨m′, e′⟩ such that r ≤ 2mm′ and ℓ(f) = ℓ(e) + ℓ(e′) + O(1). □

Notice how the tradeoff between exponent and size in the definition of application allows us to cancel the intermediate exponent q and thus to define composition.

Using an analogue of Lemma 4.1.7 (parametrisation) we can now show that P forms a BCK-algebra.

Numerical values are embedded into P by num′(x) = ⟨1, num(x)⟩. Let us write NUM ⊆ P for the image of this embedding. The application of ⟨m, e⟩ to ⟨1, num(x)⟩ takes time (ℓ(e) + |x| + 1)^{m} thus is polynomial in |x|. On the other hand, every PTIME-function on integers is representable in P by appropriately padding a PTIME algorithm for it.

At present, we do not have an application for this algebra P, but it is interesting to note that it is indeed possible to organise all PTIME-functions into a BCK-algebra.

Conclusions and further work

The ultimate goal behind this work is the development of a type system for functional programs which identifies polynomial time algorithms by using type-theoretic generalisations of primitive recursion.

In this thesis we have shown how to extend Bellantoni-Cook’s notion of safe recursion to higher-order functions and inductively defined data structures. On the technical side we have developed a semantic method for proving correctness of such systems.

Like many model constructions in the field of type theory this semantic method can be seen as a distillation of the invariants needed to prove soundness by induction on the structure of typing derivations. The advantage is that it is now rather easy to extend the type system by other constructs as long as they can be interpreted in the model or—to reiterate this distillation metaphor—as long as the distilled invariants suffice for the desired extension.

Examples of such “cheap” extensions would be record types, coproduct types, variant types. Arbitrary inductive types can also be added if one merely aims for interpreting constructors and case distinction. If recursors are also desired then as we have seen in the case of the Leivant trees a more careful case-to-case analysis is necessary.

A number of other extensions have been considered and did not make it into the thesis for various reasons mostly for lack of time and space. One such further topic is the definition of a higher-order intuitionistic logic over SLR. As is well known, every presheaf category is a model of such logic and so is in particular Ĥ^{□}. As shown in [15] it is necessary to restrict to the category of sheaves for a certain Grothendieck topology in order to validate decidability of atomic formulas. Alternatively, if one restricts attention to first-order logic, one can use a functional interpretation of proofs as SLR-terms and thereby avoid the use of sheaves.

In such a logic based on SLR^{1} one would have a linear implication and modalities !, □ both on propositions and on individuals. Alternatively, one would have three kinds of implications and quantifiers. The induction schema would take the form

φ(0)⊸!(∀x: □N.φ(⌊\frac{x}{2}⌋)⊸φ(x))⊸∀x: □N.φ(x)

1We remark that in a recent talk S. Bellantoni has presented such a logic based on the system in [2].

Here φ must not contain the modalities ! and □ neither in formulas nor in quantifiers.

The interpretation of this logic would then show that Skolem functions of Π_{2}^{0}-statements are polynomial time computable functions. Using the Friedman-Dragalin translation one could even extend this to a classical variant of the logic.

So far this would merely be an exercise in spelling out definitions and writing down proof rules. The challenge would be to explore the strength of such logic, to see whether it is practically more convenient to use than e.g. Bounded Arithmetic, and to solve metatheoretic questions which go beyond the provability of Π_{2}^{0} statements.

The linearity restriction in the inductive step has some intuitive appeal since it is met by most of the so to speak “obvious” proofs by induction which use the induction hypothesis at most once. If more than one use is made of the induction hypothesis then this is often emphasised as something unusual and “ingenious”. The modality restriction is less natural and could be perhaps be avoided in a logic based on Caseiro’s LIN system.

Another strand of further development would be the extension with further modalities corresponding to the levels of the Grzegorczyk hierarchy [38]. As mentioned in Section 3.8 the SLR type system can easily be extended to this. Some work on extending the semantics will be necessary though. Similarly, one might consider an adaptation of the results to complexity classes below polynomial time. As far as recursion with basic result type is concerned this could be easily achieved using recursion-theoretic characterisations of such classes as can be found in [7]. For higher result types the construction of appropriate BCK-algebras would be required and it is not clear at present to what extent such is possible.

We emphasize that the restriction to extensional presheaves and accordingly the simplified construction of tensor product of presheaves which has been made in order to simplify the presentation precludes the use of non-well-pointed semantics. If such would be needed in the future then Day’s original tensor product [10] and an appropriate definition of the !-modality must be employed. Indeed, the semantics given in [19] is not well-pointed, so in order to apply the described methods to that case one would have to follow that avenue.

On the practical side we would like to see an integration of SLR with a full scale programming language so that also non-polynomial time algorithms could be written down albeit with a different type. Partial evaluation (based on our semantic soundness proof) could then be used to compile a program into PTIME-code once all its arguments on which the runtime depends superpolynomially are known.

Bibliography

[1] Andrea Asperti. Light affine logic. In Proc. Symp. Logic in Comp. Sci. (LICS). IEEE, 1998.

[2] S. Bellantoni, K.-H. Niggl, and H. Schwichtenberg. Ramification, Modality, and Linearity in Higher Type Recursion. in preparation, 1998.

[3] Stephen Bellantoni and Stephen Cook. New recursion-theoretic characterization of the polytime functions. Computational Complexity, 2:97–110, 1992.

[4] Samuel R. Buss. Bounded Arithmetic. Bibliopolis, 1986.

[5] Vuokko-Helena Caseiro. Equations for Defining Poly-time Functions. PhD thesis, University of Oslo, 1997. Available by ftp from ftp.ifi.uio.no/pub/vuokko/0adm.ps.

[6] Iliano Cervesato and Frank Pfenning. A Linear Logical Framework. In Symposium on Logic in Computer Science (LICS’96), pages 264–275. IEEE, 1996.

[7] Peter Clote. Computation models and function algebras. available electronically under http://thelonius.tcs.informatik.uni-muenchen.de/∼clote/Survey.ps.gz, 1996.

[8] S. Cook and B. Kapron. Characterisations of the basic feasible functionals at all finite types. In S. Buss and P. Scott, editors, Feasible Mathematics, pages 154–159. Birkhäuser, 1990.

[9] S. Cook and A. Urquhart. Functional interpretations of feasibly constructive arithmetic. Annals of Pure and Applied Logic, 63:103–200, 1993.

[10] Brian J. Day. An embedding theorem for closed categories. In Category Seminar Sydney 1972–73, LNM 420. Springer Verlag, 1974.

[11] J. Despeyroux, F. Pfenning, and C. Schürmann. Primitive Recursion for Higher-Order Abstract Syntax. Technical Report CMU-CS-96-172, Carnegie Mellon University, 1996.

[12] Thomas Ehrhard and Loïc Colson. On strong stability and higher-order sequentiality. In Proc. 9th Symp. Logic in Comp. Sci. (LICS), Paris, France, pages 103–109. IEEE, 1994.

[13] Jean-Yves Girard.  Light Linear Logic.  Manuscript. Available via

http://hypatia.dcs.qmw.ac.uk/, 1995.

[14] Andreas Goerdt. Characterizing complexity classes by higher type primitive recursive definitions. Theoretical Computer Science, 100:45–66, 1992.

[15] Martin Hofmann. An application of category-theoretic semantics to the characterisation of complexity classes using higher-order function algebras. Bulletin of Symbolic Logic, 3(4):469–485, 1997.

[16] Martin Hofmann. Extensional constructs in intensional type theory. BCS distinguished dissertation series. Springer, 1997. 214 pp.

[17] Martin Hofmann. Syntax and semantics of dependent types. In A. M. Pitts and P. Dybjer, editors, Semantics and Logics of Computation, Publications of the Newton Institute, pages 79–130. Cambridge University Press, 1997.

[18] Martin Hofmann. A mixed modal/linear lambda calculus with applications to Bellantoni-Cook safe recursion. In Proceedings of CSL ’97, Aarhus. Springer LNCS 1414, pages 275–294, 1998.

[19] Martin Hofmann. Linear types and non-size-increasing polynomial time computation. Draft. Available from www.dcs.ed.ac.uk/home/mxh/papers/icfp.ps.gz, October 1998.

[20] Martin Hofmann. More results on modal/linear lambda calculus. Submitted. Available under www.dcs.ed.ac.uk/home/mxh, 1998.

[21] Martin Hofmann. Linear types and non size-increasing polynomial time computation. In Logic in Computer Science (LICS). IEEE, Computer Society Press, 1999. to appear.

[22] Martin Hofmann and Thomas Streicher. The groupoid interpretation of type theory. In Giovanni Sambin and Jan Smith, editors, Proceedings of the meeting Twenty-five years of constructive type theory, Venice, 1995. Oxford University Press, 1998. Extended abstract in Proc. LICS ’94.

[23] G.M. Kelly, editor. Category Seminar Sydney 1972/73, volume 420 of Lecture Notes in Mathematics. Springer, Berlin, Heidelberg, New York, 1973.

[24] Joachim Lambek and Philip Scott. Introduction to Higher-Order Categorical Logic. Cambridge University Press, 1986.

[25] Daniel Leivant. Stratified Functional Programs and Computational Complexity. In Proc. 20th IEEE Symp. on Principles of Programming Languages, 1993.

[26] Daniel Leivant and Jean-Yves Marion. Lambda calculus characterisations of polytime. Fundamentae Informaticae, 19:167–184, 1993.

[27] Daniel Leivant and Jean-Yves Marion. Ramified Recurrence and Computational Complexity II: Substitution and Poly-space. In Jerzy Tiuryn and Leszek Pacholski, editors, Proc. CSL ’94, Kazimierz, Poland, Springer LNCS, Vol. 933, pages 4486–500, 1995.

[28] Daniel Leivant and Jean-Yves Marion. Ramified Recurrence and Computational Complexity IV: Predicative functionals and Poly-space. Manuscript, 1997.

[29] Saunders Mac Lane. Categories for the Working Mathematician. Springer, 1971.

[30] Eugenio Moggi. Notions of computation and monads. Information and Computation, 93:55–92, 1991.

[31] Jim Otto. Complexity Doctrines. PhD thesis, McGill University, 1995.

[32] Frank Pfenning. On the undecidability of partial polymorphic type reconstruction. Fundamenta Informaticae, 19, 1993.

[33] Frank Pfenning and Rowan Davies. A modal analysis of staged computation. In Proc. POPL ’96, Florida. IEEE, 1996.

[34] Frank Pfenning and Hao-Chi Wong. On a modal lambda calculus for S4. In Proceedings of the 11th Conference on Mathematical Foundations of Programming Semantics (MFPS), New Orleans, Louisiana. Electronic Notes in Theoretical Computer Science, Volume 1, Elsevier, 1995.

[35] Benjamin Pierce and David Turner. Local type inference. In Conference record of the 25th ACM Symposium on Principles of Programming Languages (POPL), 1998.

[36] Benjamin C. Pierce. Bounded quantification is undecidable. Information and Computation, 112(1):131–165, July 1994.

[37] Gordon Plotkin. Type theory and recursion. Invited talk at the 8th Annual IEEE Symposium on Logic in Computer Science, Montreal, Canada, 1993.

[38] H. E. Rose. Subrecursion. Clarendon Press, Oxford, 1984.

[39] Vladimir Sazonov and A. Voronkov. A construction of typed lambda models related to feasible computability. In Georg Gottlob, Alexander Leitsch, and Daniele Mundici, editors, Proceedings of the Third Kurt Gödel Colloquium, KGC’93. (Lecture Notes in Computer Science; vol. 713), pages 301–313. Springer, 1993.

[40] R. Statman. The typed lambda calculus is not elementary recursive. Theoretical Computer Science, 9, 1979.

[41] David N. Turner, Philip Wadler, and Christian Mossin. Once upon a type. In 7’th International Conference on Functional Programming and Computer Architecture, San Diego, California, June 1995.

[42] Philip Wadler. Is there a use for linear logic? In ACM Conference on Partial Evaluation and Semantics-Based Program Manipulation, New Haven, Connecticut, June 1991.