# Types by Need (Extended Version)

A cornerstone of the theory of lambda-calculus is that intersection types characterise termination properties. They are a flexible tool that can be adapted to various notions of termination, and that also induces adequate denotational models. Since the seminal work of de Carvalho in 2007, it is known that multi types (i.e. non-idempotent intersection types) refine intersection types with quantitative information and a strong connection to linear logic. Typically, type derivations provide bounds for evaluation lengths, and minimal type derivations provide exact bounds. De Carvalho studied call-by-name evaluation, and Kesner used his system to show the termination equivalence of call-by-need and call-by-name. De Carvalho's system, however, cannot provide exact bounds on call-by-need evaluation lengths. In this paper we develop a new multi type system for call-by-need. Our system produces exact bounds and induces a denotational model of call-by-need, providing the first tight quantitative semantics of call-by-need.

## Authors

• 17 publications
• 14 publications
• 3 publications
• ### Types of Fireballs (Extended Version)

The good properties of Plotkin's call-by-value lambda-calculus crucially...
08/30/2018 ∙ by Beniamino Accattoli, et al. ∙ 0

• ### Tight Typings and Split Bounds

Multi types---aka non-idempotent intersection types---have been used to ...
07/06/2018 ∙ by Beniamino Accattoli, et al. ∙ 0

• ### Intersection Types and (Positive) Almost-Sure Termination

Randomized higher-order computation can be seen as being captured by a l...
10/23/2020 ∙ by Ugo Dal Lago, et al. ∙ 0

• ### Resolution as Intersection Subtyping via Modus Ponens

Resolution and subtyping are two common mechanisms in programming langua...
10/13/2020 ∙ by Koar Marntirosian, et al. ∙ 0

• ### Non-idempotent intersection types in logical form

Intersection types are an essential tool in the analysis of operational ...
11/05/2019 ∙ by Thomas Ehrhard, et al. ∙ 0

• ### Natural Deduction and Normalization Proofs for the Intersection Type Discipline

Refining and extending previous work by Retoré, we develop a systematic ...
04/23/2019 ∙ by Federico Aschieri, et al. ∙ 0

• ### A Quantitative Understanding of Pattern Matching

This paper shows that the recent approach to quantitative typing systems...
12/04/2019 ∙ by Sandra Alves, et al. ∙ 0

##### This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

### 1 Introduction

Duplications and erasures have always been considered as key phenomena in the -calculus—the -calculus, where erasures are forbidden, is an example of this. The advent of linear logic [35] gave them a new, prominent logical status. Forbidding erasure and duplication enables single-use resources, i.e. linearity, but limits expressivity, as every computation terminates in linear time. Their controlled reintroduction via the non-linear modality recovers the full expressive power of cut-elimination and allows a fine analysis of resource consumption. Duplication and erasure are therefore the key ingredients for logical expressivity, and—via Curry-Howard—for the expressivity of the -calculus. They are also essential to understand evaluation strategies.

In a -term there can be many -redexes, that is, places where -reduction can be applied. In this sense, the -calculus is non-deterministic. Non-determinism does not affect the result of evaluation, if any, but it affects whether evaluation terminates, and in how many steps. There are two natural deterministic evaluation strategies, call-by-name (shortened to CbN) and call-by-value (CbV), which have dual behaviour with respect to duplication and erasure.

##### Call-by-Name = Silly Duplication + Wise Erasure.

CbN never evaluates arguments of -redexes before the redexes themselves. As a consequence, it never evaluates in subterms that will be erased. This is wise, and makes CbN a normalising strategy, that is, a strategy that reaches a result whenever one exists111If a term admits both converging and diverging evaluation sequences then the diverging sequences occur in erasable subterms of , which is why CbN avoids them.. A second consequence is that if the argument of the redex is duplicated then it may be evaluated more than once. This is silly, as it repeats work already done.

##### Call-by-Value = Wise Duplication + Silly Erasure.

CbV, on the other hand, always evaluates arguments of -redexes before the redexes themselves. Consequently, arguments are not re-evaluated—this is wise with respect to duplication—but they are also evaluated when they are going to be erased. For instance, on , where is the famous looping -term, CbV evaluation diverges (it keeps evaluating ) while CbN converges in one -step (simply erasing ). This CbV treatment of erasure is clearly as silly as the duplicated work of CbN.

##### Call-by-Need = Wise Duplication + Wise Erasure.

It is natural to try to combine the advantages of both CbN and CbV. The strategy that is wise with respect to both duplications and erasures is usually called call-by-need (CbNeed), it was introduced by Wadsworth [54], and dates back to the ’70s. Despite being at the core of Haskell, one of the most-used functional programming languages, and—in its strong variant—being at work in the kernel of Coq as designed by Barras [14], the theory of CbNeed is much less developed than that of CbN or CbV.

One of the reasons for this is that it cannot be defined inside the -calculus without some hacking. Manageable presentations of CbNeed indeed require first-class sharing and micro-step operational semantics where variable occurrences are replaced one at a time (when needed), and not all at once as in the -calculus. Another reason is the less natural logical interpretation.

##### Linear Logic, Names, Values, and Needs.

CbN and CbV have neat interpretations in linear logic. They correspond to two different representations of intuitionistic logic in linear logic, based on two different representations of implication222The CbN translation maps to , while the CbV maps it to , or equivalently to ..

The logical interpretation of CbNeed—studied by Maraist et al. in [44]—is less neat than those of CbN and CbV. Within linear logic, CbNeed is usually understood as corresponding to the CbV representation where erasures are generalised to all terms, not only those under the scope of a modality. So, it is seen as a sort of affine CbV. Such an interpretation however is unusual, because it does not match exactly with cut-elimination in linear logic, as for CbN and CbV.

##### Call-by-Need, Abstractly.

The main theorem of the theory of CbNeed is that it is termination equivalent to CbN, that is, on a fixed term, CbNeed evaluation terminates if and only if CbN evaluation terminates, and, moreover, they essentially produce the same result (up to some technical details that are irrelevant here). This is due to the fact that both strategies avoid silly divergent sequences such as that of . Termination equivalence is an abstract theorem stating that CbNeed erases as wisely as CbN. Curiously, in the literature there are no abstract theorems reflecting the dual fact that CbNeed duplicates as wisely as CbV—we provide one, as a side contribution of this paper.

##### Call-by-Need and Denotational Semantics.

CbNeed is then usually considered as a CbV optimisation of CbN. In particular, every denotational model of CbN is also a model of CbNeed, and adequacy—that is the fact that the denotation of is not degenerated if and only if terminates—transfers from CbN to CbNeed.

Denotational semantics is invariant by evaluation, and so is insensitive to evaluation lengths by definition. It then seems that denotational semantics cannot distinguish between CbN and CbNeed. The aim of this paper is, somewhat counter-intuitively, to separate CbN and CbNeed semantically. We develop a type system whose type judgements induce a model—this is typical of intersection type systems—and whose type derivations provide exact bounds for CbNeed evaluation—this is usually obtained via non-idempotent intersection types. Unsurprisingly, the design of the type system requires a delicate mix of erasure and duplication and builds on the linear logic understanding of CbN and CbV.

##### Multi Types.

Our typing framework is given by multi types, which is an alternative name for non-idempotent intersection types333The new terminology is due to the fact that a non-idempotent intersection can be seen as a multi-set .. Multi types characterise termination properties exactly as intersection types, having moreover the advantages that they are closely related to (the relational semantics of) linear logic, their type derivations provide quantitative information about evaluation lengths, and the proof techniques are simpler—no need for the reducibility method.

The seminal work of de Carvalho [20] (appeared in 2007 but unpublished until 2018) showed how to use multi types to obtain exact bounds on evaluation lengths in CbN. Ehrhard adapted multi types to CbV [29], and very recently Accattoli and Guerrieri adapted de Carvalho’s study of exact bounds to Ehrhard’s system and CbV evaluation [8]. Kesner used de Carvalho’s CbN multi types to obtain a simple proof that CbNeed is termination equivalent with respect to CbN [37] (first proved with other techniques by Maraist, Odersky, and Wadler [45] and Ariola and Felleisen [10] in the nineties), and then Kesner and coauthors continued exploring the theory of CbNeed via CbN multi types [12, 39, 13].

Kesner’s use of CbN multi types to study CbNeed is qualitative, as it deals with termination and not with exact bounds. For a quantitative study of CbNeed, de Carvalho’s CbN system cannot really be informative: CbN multi types provide bounds for CbNeed which cannot be exact because they already provide exact bounds for CbN, which generally takes more steps than CbNeed.

##### Multi Types by Need.

In this paper we provide the first multi type system characterising CbNeed termination and whose minimal type derivations provide exact bounds for CbNeed evaluation lengths. The design of the type system is delicate, as we explain in Sect. 6. One of the key points is that, in contrast to Ehrhard’s system for CbV [29], multi types for CbNeed cannot be directly extracted by the relational semantics of linear logic, given that CbNeed does not have a clean representation in it. A by-product of our work is a new denotational semantics of CbNeed, the first one to precisely reflect its quantitative properties.

Beyond the result itself, the paper tries to stress how the key ingredients of our type system are taken from those for CbN and CbV and combined together. To this aim, we first present multi types for CbN and CbV, and only then we proceed to build the CbNeed system and prove its properties.

Along the way, we also prove the missing fundamental property of CbNeed, that is, that it duplicates as efficiently as CbV. The result is obtained by dualising Kesner’s approach [37], showing that the CbV multi type system is correct also with respect to CbNeed evaluation, that is, its bounds are also valid with respect to CbNeed evaluation lengths. Careful: the CbV system is correct but of course not complete with respect to CbNeed, because CbNeed may normalise when CbV diverges. The proof of the result is straightforward, because of our presentations of (CbN,) CbV and CbNeed. We adopt a liberal, non-deterministic formulation of CbV, and assuming that garbage collection is always postponed. These two ingredients turn CbNeed into a fragment of CbV, obtaining the new fundamental result as a corollary of correctness of CbV multi types for CbV evaluation.

##### Technical Development.

The paper is extremely uniform, technically speaking. The three evaluations are presented as strategies of Accattoli and Kesner’s Linear Substitution Calculus (shortened to LSC) [1, 6], a calculus with a simple but expressive form of explicit sharing. The LSC is strongly related to linear logic [2], and provides a neat and manageable presentation of CbNeed, introduced by Accattoli, Barenbaum, and Mazza in [3], and further developed by various authors in [9, 37, 12, 4, 5, 39, 13]. Our type systems count evaluation steps by annotating typing rules in the exact same way, and the proofs of correctness and completeness all follow the exact same structure. While the results for CbN are very minor variations with respect to those in the literature [20, 7], those for CbV are the first ones with respect to a presentation of CbV with sharing.

As it is standard for CbNeed, we restrict our study to closed terms and weak evaluation (that is, out of abstractions). The main consequence of this fact is that normal forms are particularly simple (sometimes called answers in the literature). Compared with other recent works dealing with exact bounds such as Accattoli, Graham-Lengrand, and Kesner’s [7] and Accattoli and Guerrieri’s [8] the main difference is that the size of normal forms is not taken into account by type derivations. This is because of the simple notions of normal forms in the closed and weak case, and not because the type systems are not accurate.

Call-by-need was introduced by Wadsworth [54] in the ’70s. In the ’90s, it was first reformulated as operational semantics by Launchbury [43], Maraist, Odersky, and Wadler [45], and Ariola and Felleisen [10], and then implemented by Sestoft [52] and further studied by Kutzner and Schmidt-Schauß [42]. More recent papers are Garcia, Lumsdaine, and Sabry’s [31], Ariola, Herbelin, and Saurin’s [11], Chang and Felleisen’s [23], Danvy and Zerny’s [26], Downen et al.’s [28], Pédrot and Saurin’s [50], and Balabonski et al.’s [12].

##### Related work about Multi Types.

Intersection types are a standard tool to study -calculi—see Coppo and Dezani [24, 25], Pottinger [51], and Krivine [41]. Non-idempotent intersection types, i.e. multi types, were first considered by Gardner [32], and then by Kfoury [40], Neergaard and Mairson [47], and de Carvalho [20]—a survey is Bucciarelli, Kesner, and Ventura’s [18].

Many recent works rely on multi types or relational semantics to study properties of programs and proofs. Beyond the cited ones, Diaz-Caro, Manzonetto, and Pagani’s [27], Carraro and Guerrieri’s [19], Ehrhard and Guerrieri’s [30], and Guerrieri’s [36] deal with CbV, while Bernadet and Lengrand’s [15], de Carvalho, Pagani, and Tortora de Falco’s [22] provide exact bounds. Further related work is by Bucciarelli, Ehrhard, and Manzonetto [16], de Carvalho and Tortora de Falco [21], Tsukada and Ong [53], Kesner and Vial [38], Piccolo, Paolini and Ronchi Della Rocca [49], Ong [48], Mazza, Pellissier, and Vial [46], Bucciarelli, Kesner and Ronchi Della Rocca [17]—this list is not exhaustive.

This is the long version (with all proofs) of a paper accepted to ESOP 2019.

### 2 Closed λ-Calculi

In this section we define the CbN, CbV, and CbNeed evaluation strategies. We present them in the context of the Accattoli and Kesner’s linear substitution calculus (LSC) [1, 6]. We mainly follow the uniform presentation of these strategies given by Accattoli, Barenbaum, and Mazza [3]. The only difference is that we adopt a non-deterministic presentation of CbV, subsuming both the left-to-right and the right-to-left strategies in [3], that makes our results slightly more general. Such a non-determinism is harmless: not only CbV evaluation is confluent, it even has the diamond property, so that all evaluations have the same length.

##### Terms and Contexts.

The set of terms of the LSC is given by the following grammar, where is called an explicit substitution (shortened to ES), that is a more compact notation for :

 \textscLSCTermst,s \Coloneqqx∣v∣ts∣t[x←s] \textscLSCValuesv \Coloneqqλx.t

The set of free variables of a term is defined as expected, in particular, . A term is closed if , open otherwise. As usual, terms are identified up to -equivalence.

Contexts are terms with exactly one occurrence of the hole , an additional constant. We shall use many different contexts. The most general ones are weak contexts (i.e. not under abstractions). The (evaluation) contexts , and —used to define CbN, CbV and CbNeed evaluation strategies, respectively—are special cases of weak contexts (in fact, CbV contexts coincide with weak contexts, the consequences of that are discussed on p. 1). To define evaluation strategies, substitution contexts (i.e. lists of explicit substitutions) also play a role.

 Weak contexts W \Coloneqq⟨⋅⟩∣Wt∣W[x←t]∣tW∣t[x←W] Substitution contexts S \Coloneqq⟨⋅⟩∣S[x←t] CbN contexts C \Coloneqq⟨⋅⟩∣Ct∣C[x←t] CbV contexts V \ColoneqqW CbNeed contexts E \Coloneqq⟨⋅⟩∣Et∣E[x←t]∣E⟨⟨x⟩⟩[x←E′]

We write for the term obtained by replacing the hole in context by the term . This plugging operation, as usual with contexts, can capture variables—for instance . We write when we want to stress that the context does not capture the free variables of .

##### Micro-step semantics.

The rewriting rules decompose the usual small-step semantics for -calculi, by substituting one variable occurrence at the time, and only when such an occurrence is in evaluation position. We emphasise this fact saying that we adopt a micro-step semantics. We now give the definitions, examples of evaluation sequences follow right next.

Formally, a micro-step semantics is defined by first giving its root-steps and then taking the closure of root-steps under suitable contexts.

 Multiplicative root-step S⟨λx.t⟩s ↦mS⟨t[x←s]⟩ Exponential CbN root-step C⟨⟨x⟩⟩[x←t] ↦ecbnC⟨⟨t⟩⟩[x←t] Exponential CbV root-step V⟨⟨x⟩⟩[x←S⟨v⟩] ↦ecbvS⟨V⟨⟨v⟩⟩[x←v]⟩ Exponential CbNeed root-step E⟨⟨x⟩⟩[x←S⟨v⟩] ↦eneedS⟨E⟨⟨v⟩⟩[x←v]⟩

where, in the root-step (resp. ; ), if for some , then (resp. ; ) and are disjoint. This condition can always be fulfilled by -equivalence.

The evaluation strategies for CbN, for CbV, and for CbNeed, are defined as the closure of root-steps under CbN, CbV and CbNeed evaluation contexts, respectively (so, all evaluation strategies do not reduce under abstractions, since all such contexts are weak):

 CbNCbVCbNeed →mcbn\coloneqqC⟨↦m⟩ →ecbn\coloneqqC⟨↦ecbn⟩ →cbn\coloneqqC⟨↦m∪↦ecbn⟩ →mcbv\coloneqqV⟨↦m⟩ →ecbv\coloneqqV⟨↦ecbv⟩ →cbv\coloneqqV⟨↦m∪↦ecbv⟩ →mneed\coloneqqE⟨↦m⟩ →eneed\coloneqqE⟨↦eneed⟩ →need\coloneqqE⟨↦m∪↦eneed⟩

where the notation means that, given a root-step , the evaluation is defined as follows: if and only if there are terms and and a context such that and and .

Note that evaluations , and can equivalently be defined as , and , respectively.

Given an evaluation sequence we note with the length of , and with and the number of multiplicative and exponential steps in , respectively—and similarly for and .

##### Erasing Steps.

The reader may be surprised by our evaluation strategies, as none of them includes erasing steps, despite the absolute relevance of erasures pointed out in the introduction. There are no contradictions: in the LSC—in contrast to the -calculus—erasing steps can always be postponed, and so they are often simply omitted. This is actually close to programming language practice, as the garbage collector acts asynchronously with respect to the evaluation flow. For the sake of clarity let us spell out the erasing rules—they shall nonetheless be ignored in the rest of the paper. In CbN and CbNeed every term is erasable, so the root erasing step takes the following form

 t[x←s]↦gct if x∉fv(t) and it is then closed by weak evaluation contexts. In CbV only values are erasable; so, the root erasing step in CbV is: t[x←S⟨v⟩]↦gcS⟨t⟩ if x∉fv(t)

and it is then closed by weak evaluation contexts.

###### Example 1

A good example to observe the differences between CbN, CbV, and CbNeed is given by the term where is the identity combinator. In CbN, it evaluates with 5 multiplicative steps and 5 exponential steps, as follows:

 t →mcbn(λy.xx)[x←II](II) →mcbn(xx)[y←II][x←II] →ecbn((II)x)[y←II][x←II] →mcbn(z[z←I]x)[y←II][x←II] →ecbn(I[z←I]x)[y←II][x←II] →mcbnw[w←x][z←I][y←II][x←II] →ecbnx[w←x][z←I][y←II][x←II] →ecbn(II)[w←x][z←I][y←II][x←II] →mcbnx′[x′←I][w←x][z←I][y←II][x←II] →ecbnI[x′←I][w←x][z←I][y←II][x←II]

In CbV, evaluates with 5 multiplicative steps and 5 exponential steps, for instance from right to left, as follows:

 t →mcbv(λx.λy.xx)(II)(z[z←I]) →ecbv(λx.λy.xx)(II)(I[z←I]) →mcbv(λx.λy.xx)(w[w←I])(I[z←I]) →ecbv(λx.λy.xx)(I[w←I])(I[z←I]) →mcbv(λy.xx)[x←I[w←I]](I[z←I]) →mcbv(xx)[y←I[z←I]][x←I[w←I]] →ecbv(xI)[y←I[z←I]][x←I][w←I] →ecbv(II)[y←I[z←I]][x←I][w←I] →mcbvx′[x′←I][y←I[z←I]][x←I][w←I] →ecbvI[x′←I][y←I[z←I]][x←I][w←I]

Note that the fact that CbN and CbV take the same number of steps is by chance, as they reduce different redexes: CbN never reduce the unneeded redex associated to , but it reduces twice the needed redex associated to , while CbV reduces both, but each one only once.

In CbNeed, evaluates in 4 multiplicative steps and 4 exponential steps.

 t →mneed(λy.xx)[x←II](II) →mneed(xx)[y←II][x←II] →mneed(xx)[y←II][x←z[z←I]] →eneed(xx)[y←II][x←I[z←I]] →eneed(Ix)[y←II][x←I][z←I] →mneed(w[w←x])[y←II][x←I][z←I] →eneedw[w←I][y←II][x←I][z←I] →eneedI[w←I][y←II][x←I][z←I]
##### CbV Diamond Property.

CbV contexts coincide with weak ones. As a consequence, our presentation of CbV is non-deterministic, as for instance one can have

 x[x←I](y[y←I])mcbv← (II)(y[y←I]) →ecbv(II)(I[y←I])

but it is easily seen that diagrams can be closed in exactly one step (if the two reducts are different). For instance,

 x[x←I](y[y←I]) →ecbvx[x←I](I[y←I])mcbv← (II)(I[y←I])

Moreover, the kind of steps is preserved, as the example illustrates. This is an instance of the strong form of confluence called diamond property. A consequence is that either all evaluation sequences normalise or all diverge, and if they normalise they have all the same length and the same number of steps of each kind. Roughly, the diamond property is a form of relaxed determinism. In particular, it makes sense to talk about the number of multiplicative / exponential steps to normal form, independently of the evaluation sequence. The proof of the property is an omitted routine check of diagrams.

##### Normal Forms.

We use two predicates to characterise normal forms, one for both CbN and CbNeed normal forms, for which ES can contain whatever term, and one for CbV normal forms, where ES can only contain normal terms:

 \infernormal(λx.t) \infernormal(t[x←s])normal(t) \infernormalcbv(λx.t) \infernormalcbv(t[x←s])normalcbv(t)normalcbv(s)
###### Proposition 1 (Syntactic characterisation of closed normal forms)

Let be a closed term.

1. CbN and CbNeed: For , is -normal if and only if .

2. CbV: is cbv-normal if and only if .

The simple structure of normal forms is the main point where the restriction to closed calculi plays a role in this paper.

From the syntactic characterization of normal forms (Proposition 1) it follows immediately that among closed terms, CbN and CbNeed normal forms coincide, while CbV normal forms are a subset of them. Such a subset is proper since the closed term (where and ) is CbN normal but not CbV normal (and it cannot normalise in CbV).

### 3 Preliminaries About Multi Types

In this section we define basic notions about multi types, type contexts, and (type) judgements that are shared by the three typing systems of the paper.

##### Multi-Sets.

The type systems are based on two layers of types, defined in a mutually recursive way, linear types and finite multi-sets of linear types. The intuition is that a linear type corresponds to a single use of a term, and that an argument is typed with a multi-set of linear types if it is going to end up (at most) times in evaluation position, with respect to the strategy associated with the type system. The three systems differ on the definition of linear types, that is therefore not specified here, while all adopt the same notion of finite multi-set of linear types (named multi type), that we now introduce:

 Multi types M,N \Coloneqq[Li]i∈J  (J a finite set)

where denotes the multi-set constructor. The empty multi-set (the multi type obtained for ) is called empty (multi) type and denoted by the special symbol . An example of multi-set is , that contains two occurrences of and one occurrence of . Multi-set union is noted .

##### Type Contexts.

A type context is a map from variables to multi types such that only finitely many variables are not mapped to . The domain of is the set . The type context is empty if .

Multi-set union is extended to type contexts point-wise, i.e.  maps each variable to . This notion is extended to several contexts as expected, so that denotes a finite union of contexts—when the notation is to be understood as the empty context. We write for only if . More generally, we write if the intersection between the domains of and is empty.

The restricted context with respect to the variable , written is defined by and if .

##### Judgements.

Type judgements are of the form or , where the indices and are natural numbers whose intended meaning is that evaluates to normal form in multiplicative steps and exponential steps, with respect to the evaluation strategy associated with the type system.

To make clear in which type systems the judgement is derived, we write if is a derivation in the CbN system ending in the judgement , and similarly for CbV and CbNeed.

### 4 Types by Name

In this section we introduce the CbN multi type system, together with intuitions about multi types. We also prove that derivations provide exact bounds on CbN evaluation sequences, and define the induced denotational model.

##### CbN Types.

The system is essentially a reformulation of de Carvalho’s system [20], itself being a type-based presentation of the relational model of the CbN -calculus induced by relational model of linear logic via the CbN translation of -calculus into linear logic. Definitions:

• CbN linear types are given by the following grammar:

 CbN linear types L,L′ \Coloneqqnormal∣M⊸L

Multi(-sets) types are defined as in Sect. 3, relatively to CbN linear types. Note the linear constant (used to type abstractions, which are normal terms): it plays a crucial role in our quantitative analysis of CbN evaluation.

• The CbN typing rules are in Fig. 1.

• The rule: it has as many premises as the elements in the (possibly empty) set of indices . When , the rule has no premises, and it types with the empty multi type . The rule is needed to derive the right premises of the rules and , that have a multi type on their right-hand side. Essentially, it corresponds to the promotion rule of linear logic, that, in the CbN representation of the -calculus, is indeed used for typing the right subterm of applications and the content of explicit substitutions.

• The size of a derivation is the sum of the indices. A quick look to the typing rules shows that indices on typing judgements are not needed, as can be recovered as the number of rules, and as the number of rules. It is however handy to note them explicitly.

##### Subtleties and easy facts.

Let us overview some facts about our presentation of the type system.

1. Introduction and destruction of multi-sets: multi-set are introduced on the right by the rule and on the left by . Moreover, on the left they are summed by and .

2. Vacuous abstractions: we rely on the convention that the abstraction rule can always abstract a variable not explicitly occurring in the context. Indeed, if , then is equal to since .

3. Relevance: No weakening is allowed in axioms. An easy induction on type derivations shows that

###### Lemma 1 (Type contexts and variable occurrences for CbN)

Let be a derivation. If then .

Lemma 1 implies that derivations of closed terms have empty type context. Note that there can be free variables of not in : the ones only occurring in subterms not touched by the evaluation strategy.

##### Key Ingredients.

Two key points of the CbN system that play a role in the design of the CbNeed one in Sect. 6 are:

1. Erasable terms and : the empty multi type is the type of erasable terms. Indeed, abstractions that erase their argument—whose paradigmatic example is —can only be typed with , because of . Note that in CbN every term—even diverging ones—can be typed with by rule (taking 0 premises), because, correctly, in CbN every term can be erased.

2. Adequacy and linear types: all CbN typing rules but assign linear types. And is used only as right premise of the rules and , to derive . It is with respect to linear types, in fact, that the adequacy of the system is going to be proved: a term is CbN normalising if and only if it is typable with a linear type, given by Theorem 4.1 and Theorem 4.2 below.

##### Tight derivations.

A term may have several derivations, indexed by different pairs . They always provide upper bounds on CbN evaluation lengths. The interesting aspect of our type systems, however, is that there is a simple description of a class of derivations that provide exact bounds for these quantities, as we shall show. Their definition relies on the type constant.

###### Definition 1 (Tight derivations for CbN)

A derivation is tight if and is empty.

###### Example 2

Let us return to the term used in Example 1 for explaining the difference in reduction lengths among the different strategies. We now give a derivation for it in the CbN type system.

First, let us shorten to . Then, we define as the following derivation for the subterm of :

 \infer[fun]⊢(1,2)λx.λy.xx:[n,[n]⊸n]⊸(0⊸n)\infer[fun]x:[n,[n]⊸n]⊢(1,2)λy.xx:0⊸n\infer[app]x:[n,[n]⊸n]⊢(1,2)xx:n\infer[ax]x:[[n]⊸n]⊢(0,1)x:[n]⊸n\infer[many]x:[n]⊢(0,1)x:[n]\infer[ax]x:[n]⊢(0,1)x:n

Now, we need two derivations for , one of type , given by as follows

 \infer[app]⊢(1,1)II:n\infer[fun]⊢(0,1)λz.z:[n]⊸n\infer[ax]z:[n]⊢(0,1)z:n\infer[many]⊢(0,0)λw.w:[n]\infer[normal]⊢(0,0)λw.w:n

and one of type , given by as follows

 [app ] ⊢^(1,2) II  : [ n ] ⊸n [fun ] ⊢^(0,1) λz.z  : [ [ n ] ⊸n ] ⊸([ n ] ⊸n) [ax ] z: [ [ n ] ⊸n ] ⊢^(0,1) z  : [ n ] ⊸n  [many ] ⊢^(0,1) λw.w  : [ [ n ] ⊸n ] [fun ] ⊢^(0,1) λw.w  : [ n ] ⊸n [ax ] w: [ n ] ⊢^(0,1) w  : n

Finally, we put , and together in the following derivation for , where and

[separation=1em, label separation=0.3em, rule margin=0.5ex]  _^(1,2) s  : [ n, n^[ n ] ] (0 n) ⊢^(1,1) II  : n ⊢^(1,2) II  : n^[ n ] 2[] ⊢^(2,3) II  : [ n, n^[ n ] ] 2[] ⊢^(4,5) s(II)  : 0n 0[] ⊢^(0,0) II  : 0 2[] ⊢^(5,5) (s(II))(II)  : n

Note that that is a tight derivation and the indices correspond exactly to the number of -steps and -steps, respectively, from to its cbn-normal form, as shown in Example 1. Theorem 4.1 below shows that this is not by chance: tight derivations are minimal and provide exact bounds to evaluation lengths.

The next two subsections prove the two halves of the properties of the CbN type system, namely correctness and completeness.

#### 4.1 CbN Correctness

Correctness is the fact that every typable term is CbN normalising. In our setting it comes with additional quantitative information: the indices and of a derivation provide bounds for the length of the CbN evaluation of , that are exact when the derivation is tight.

The proof technique is standard. Moreover, the correctness theorems for CbV and CbNeed in the next sections follow exactly the same structure. The proof relies on a quantitative subject reduction property showing that decreases by exactly one at each -step, and similarly for and -steps. In turn, subject reduction relies on a linear substitution lemma. Last, correctness for tight derivations requires a further property of normal forms.

Let us point out that correctness is stated with respect to closed terms only, but the auxiliary results have to deal with open terms, since they are proved by inductions (over predicates defined by induction) over the structure of terms.

##### Linear Substitution.

The linear substitution lemma states that substituting over a variable occurrence as in the exponential rule consumes exactly one linear type and decreases of one the exponential index .

###### Lemma 2 (CbN linear substitution)

If then there is a splitting such that for every derivation there is a derivation .

The proof is by induction over CbN evaluation contexts.

##### Quantitative Subject Reduction.

A key point of multi types is that the size of type derivations shrinks after every evaluation step, which is what allows to bound evaluation lengths. Remarkably, the size (defined as the sum of the indices) shrinks by exactly 1 at every evaluation step.

###### Proposition 2 (Quantitative subject reduction for CbN)

Let be a derivation.

1. Multiplicative: if then and there exists a derivation .

2. Exponential: if then and there exists a derivation .

The proof is by induction on and , using the linear substitution lemma for the root exponential step.

##### Tightness and Normal Forms.

Since the indices are always non-negative, quantitative subject reduction (Proposition 2) implies that they bound evaluation lengths. The bound is not necessarily exact, as derivations of normal forms can have strictly positive indices. If they are tight, however, they are indexed by , as we now show. The proof of this fact (by induction on the predicate ) requires a slightly different statement, for the induction to go through.

###### Proposition 3 (normal typing of normal forms for CbN)

Let be such that , and be a derivation. Then is empty, and so is tight, and .

##### The Tight Correctness Theorem.

The theorem is then proved by a straightforward induction on the evaluation length relying on quantitative subject reduction (Proposition 2) for the inductive case, and the properties of tight typings for normal forms (Proposition 3) for the base case.

###### Theorem 4.1 (CbN tight correctness)

Let be a closed term. If then there is such that , , , . Moreover, if is tight then and .

Note that Theorem 4.1 implicitly states that tight derivations have minimal size among derivations.

#### 4.2 CbN Completeness

Completeness is the fact that every normalising term has a (tight) type derivation. As for correctness, the completeness theorem is always obtained via three intermediate steps, dual to those for correctness.

##### Normal Forms.

The first step is to prove (by induction on the predicate ) that every normal form is typable, and is actually typable with a tight derivation.

###### Proposition 4 (Normal forms are tightly typable for CbN)

Let be such that . Then there is tight derivation .

##### Linear Removal.

In order to prove subject expansion, we have to first show that typability can also be pulled back along substitutions, via a linear removal lemma dual to the linear substitution lemma.

###### Lemma 3 (Linear removal for CbN)

Let , where . Then there exist

• a linear type ,

• a derivation , and

• a derivation

such that

• Type contexts: .

• Indices: .

##### Quantitative Subject Expansion.

This property is the dual of subject reduction.

###### Proposition 5 (Quantitative subject expansion for CbN)

Let be a derivation.

1. Multiplicative: if then there is a derivation .

2. Exponential: if then there is a derivation .

The proof is by induction on and , using the linear removal lemma for the root exponential step.

##### The Tight Completeness Theorem.

The theorem is proved by a straightforward induction on the evaluation length relying on quantitative subject expansion (Proposition 5) in the inductive case, and the existence of tight typings for normal forms (Proposition 4) in the base case.

###### Theorem 4.2 (CbN tight completeness)

Let be a closed term. If and then there is a tight derivation .

##### Back to Erasing Steps.

Our system can be easily adapted to measure also garbage collection steps (the CbN erasing rule is just before Example 1, page 1). First, a new, third index on judgements is necessary. Second, one needs to distinguish the erasing and non-erasing cases of the the and rules, discriminated by the type. For instance, the rules are (the rules are similar):

 \infer[ES]Γ⊎Π⊢(m+m′,e+e′,g+g′)t[x←s]:LΓ,x:M⊢(m,e,g)t:LΠ⊢(m′,e′,g′)s:MM≠0

The index bounds to the number of erasing steps. In the closed case, however, the bound cannot be, in general, exact. Variables typed with by do not exactly match variables not appearing in the typed term (that is the condition triggering the erasing step), because a variable typed with may appear in the body of abstractions typed with the normal rule, as such bodies are not typed.

It is reasonable to assume that exact bounds for erasing steps can only by provided by a type system characterising strong evaluation, whose typing rules have to inspect abstraction bodies. These erasing typing rules are nonetheless going to play a role in the design of the CbNeed system in Sect. 6.

#### 4.3 CbN Model

The idea to build the denotational model from the multi type system is that the interpretation (or semantics) of a term is simply the set of its type assignments, i.e. the set of its derivable types together with their type contexts. More precisely, let be a term and (with ) be pairwise distinct variables. If , we say that the list is suitable for . If is suitable for , the (relational) semantics of for is

 [[t]]CbN→x\coloneqq{((M1,…,Mn),L)∣∃Φ▹cbn x1:M1,…,xn:Mn ⊢(m,e)t:L}.

Subject reduction (Proposition 2) and expansion (Proposition 5) guarantee that the semantics of (for any term , possibly open) is invariant by CbN evaluation. Correctness (Theorem 4.1) and completeness (Theorem 4.2) guarantee that, given a closed term , its interpretation is non-empty if and only if is CbN normalisable, that is, they imply that relational semantics is adequate.

In fact, adequacy also holds with respect to open terms. The issue in that case is that the characterisation of tight derivations is more involved, see Accattoli, Graham-Lengrand and Kesner’s [7]. Said differently, weaker correctness and completeness theorems without exact bounds also hold in the open case. The same is true for the CbV and CbNeed systems of the next sections.

### 5 Types by Value

Here we introduce Ehrhard’s CbV multi type system [29] adapted to our presentation of CbV in the LSC, and prove its properties. The system is similar, and yet in many aspects dual, to the CbN one, in particular the grammar of types is different. Linear types for CbV are defined by:

 CbV linear types L,L′ \ColoneqqM⊸N

Multi(-sets) types are defined as in Sect. 3, relatively to CbV linear types. Note that linear types now have a multi type both as source and as target, and that the constant is absent—in CbV, its role is played by .

The typing rules are in Fig. 2. It is a type-based presentation of the relational model of the CbV -calculus induced by relational model of linear logic via the CbV translation of -calculus into linear logic. Some remarks:

• Right-hand types: all rules but assign a multi type to the term on the right-hand side, and not a linear type as in CbN.

• Abstractions and : the rule has a restricted form with respect to the CbN one, it can only be applied to abstractions, that in turn are the only terms that can be typed with a linear type.

• Indices: note as the indices are however incremented (on and ) and summed (in and ) exactly as in the CbN system.