    # A Linear Algebra Approach to Linear Metatheory

Linear typed λ-calculi are more delicate than their simply typed siblings when it comes to metatheoretic results like preservation of typing under renaming and substitution. Tracking the usage of variables in contexts places more constraints on how variables may be renamed or substituted. We present a methodology based on linear algebra over semirings, extending McBride's kits and traversals approach for the metatheory of syntax with binding to linear usage-annotated terms. Our approach is readily formalisable, and we have done so in Agda.

## Authors

##### 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

The basic metatheoretic results for typed -calculi, such as preservation of typing under renaming, weakening, substitution and so on, are crucial but quite boring to prove. In calculi with substructural typing disciplines and modalities, it can also be quite easy to break these properties [Wad92, BBPH93]. It is desirable therefore to use a proof assistant to prove these properties. This has the double benefit of both confidence in the results and in focusing on the essential properties required to obtain them.

Mechanisation of the metatheory of substructural -calculi has not received the same level of attention as intuitionistic typing. “Straightforward” translations from paper presentations to formal presentations make metatheory difficult, due to incompatibilies between the standard de Bruijn representation of binding and the splitting of contexts. For formalisations of linear sequent calculi, sticking to the paper presentation using lists and permutations is common [PW99, XORN17, Lau18], but explicit permutations make the resulting encodings difficult to use. Multisets for contexts are more convenient [CLR19], but do not work well for Curry-Howard uses, as noted by Laurent. For natural deduction, Allais [All18] uses an I/O model to track usage of variables, Rouvoet et al. [RBPKV20] use a co-de Bruijn representation to distribute variables between subterms, and Crary uses mutually defined typing and linearity judgements with HOAS [Cra10].

In this paper, we adapt the generic kits and traversals technique for proving admissibility of renaming and substitution due to McBride [McB05] to a linear typed

-calculus where variables are annotated with values from a skew semiring denoting those variables’

usage by terms. Our calculus, , is a prototypical example of a linear “quantitative” or “coeffect” calculus in the style of [RP10, BGMZ14, GS14, POM14, OLE19]. The key advantages of over the formalisations listed above are that the shape of typing contexts is maintained, so de Bruijn indices behave the same as in non-substructural calculi, and by selecting different semirings, we obtain from well known systems, including Barber’s Dual Intuitionistic Linear Logic [Bar96] and Pfenning and Davis’ S4 modal type theory [PD99].

McBride’s kits and traversals technique isolates properties required to form binding-respecting traversals of simply typed -terms, so that renaming and substitution arise as specific instantiations. Benton, Hur, Kennedy, and McBride [BHKM12] implement the technique in Coq and extend it to polymorphic terms. Allais et al. [AAC20] generalise to a wider class of syntax with binding and show that more general notions of semantics can be handled.

To adapt kits and traversals to linear usage-annotated terms requires us to not only respect the binding structure, but to also respect the usage annotations. For instance, the usages associated with a term being substituted in must be correctly distributed across all the occurrences of that term in the result. To aid us in tracking usages correctly, we employ the linear algebra of vectors and matrices induced by the skew semiring we are using. Usage annotations on contexts are vectors, usage-preserving maps of contexts are matrices, and the linearity properties of the maps induced by matrices are exactly the lemmas we need for showing that traversals (and hence renaming, subusaging, and substitution) preserve typing and usages.

The paper proceeds as follows. In section 2, we specify our requirements on the set of annotations that will track usage of variables. A consequence of our formalisation is that we learn that we only need skew semirings, a weaker structure than the partially ordered semirings usually used. In section 3, we use these annotations to define the system in an intrinsically typed style. Then, in section 4, we prove that admits renaming, subusaging, and substitution by our extension of McBride’s kits and traversals technique. We conclude in section 5 with some directions for future work.

The Agda formalisation of this work can be found at https://github.com/laMudri/generic-lr/tree/master/src/Specific. It contains our formalisation of vectors and matrices (approx. 790 lines) and the definition of and proofs of renaming and substitution (approx. 530 lines).

## 2 Skew semirings

We shall use skew semirings where other authors have previously used partially ordered semirings (see, for example, the Granule definition of a resource algebra [OLE19]). Elements of a skew semiring are used as usage annotations, and describe how values are used in a program. In the syntax for , each assumption will have a usage annotation, describing how that assumption can be used in the derivation. Addition describes how to combine multiple usages of an assumption, and multiplication describes the action our graded -modality can have. The ordering describes the specificness of annotations. If , can be the annotation for a variable wherever can be. We can read this relation as “” — where we demand that a variable be used according to , it is also fine to use it if it is annotated .

Skew semirings are a generalisation of partially ordered semirings, which are in turn a generalisation of commutative semirings. As such, readers unfamiliar with the more general structures may wish to think in terms of the more specific structures. Our formalisation was essential for noticing and sticking to this level of generality.

###### Definition 2.1.

A (left) skew monoid is a structure such that forms a partial order, is monotonic with respect to , and the following laws hold. 1x x x x1 (xy)z x(yz)

###### Remark 2.2.

A commutative skew monoid is just a commutative monoid.

Skew-monoidal categories are due to Szlachányi [Szl12], and the notion introduced here of a skew monoid is a decategorification of the notion of skew-monoidal category.

###### Definition 2.3.

A (left) skew semiring is a structure such that forms a partial order, and are monotonic with respect to , forms a commutative monoid, forms a skew monoid, and we have the following distributivity laws. 0z 0 (x + y)z xz + yz 0 x0 xy + xz x(y + z)

###### Example 2.4.

In light of the above remark, most “skew” semirings are actually just partially ordered semirings. An example that yields a system equivalent to Barber’s DILL is the semiring of “unused”, “unrestricted”, and “linear”, respectively. See [OLE19] for more examples.

We will only speak of left skew semirings, and thus generally omit the word “left”. A mnemonic for (left) skew semirings is “multiplication respects operators on the left from left to right, and respects operators on the right from right to left”. One may also describe multiplication as “respecting” and “corespecting” operators on the left and right, respectively.

From a skew semiring , we form finite vectors, which we notate as , and matrices, which we notate as . In Agda, we represent vectors in as functions , where is the type of valid indexes in an -tuple, and matrices in as functions . Whereas elements of describe how individual variables are used, elements of describe how all of the variables in an -length context are used. We call such vectors usage contexts, and take them to be row vectors. Matrices in will be used to describe how usage contexts are transformed by renaming and substitution in section 4. We define , and on vectors and matrices pointwise. Basis vectors (used to represent usage contexts for individual variables), identity matrices , matrix multiplication , and matrix reindexing are defined as follows: ⟨- |: Idx n →Rn⟨i |j := {1,if i = j0,otherwise I : Rm ×mIij := ⟨i |j * : Rm ×n×Rn ×oRm ×o(MN)ik := ∑j MijNjk –×- : Rm’ ×n’×(Idx m →Idx m’) ×(Idx n →Idx n’) →Rm ×n(Mf ×g)i,j := Mf i,g j

We define vector-matrix multiplication by treating vectors as -height matrices. We will sometimes silently cast between the types and , particularly when using the reindexing operation.

## 3 Syntax

We present the syntax of as an intrinsically typed syntax, as it is in our Agda formalisation. Intrinisic typing means that we define well-typed terms as inhabitants of an inductive family indexed by typing contexts , usage contexts , and types . Typing contexts are lists of types. Usage contexts are vectors of elements of some fixed skew semiring , with the same number of elements as the typing context they are paired with. To highlight how usage annotations are used in the syntax, we write all elements of , and vectors and matrices thereof, in green.

The types of are given by the grammar: . We have a base type , function types

, tensor product types

with unit , sum types with unit , “with” product types with unit , and an exponential indexed by a usage .

We distinguish between plain variables, values of type , which are indices into a context with a specified type, and usage-checked variables, values of type which are pairs of a plain variable and proof that . The force of the latter condition is that the selected variable must have a usage annotation in , and all other variables must have a usage annotation .

The constructors for our intrinsically typed terms are presented in Figure 1. In keeping with our intrinsic typing methodology, terms of are presented as constructors of the inductive family , hence the notation instead of the more usual . Our Agda formalisation uses de Bruijn indices to represent variables, but we have annotated the rules with variable names for ease of reading. Ignoring the usages, the typing rules all look like their simply typed counterparts; the only difference between the and

products being their presentation in terms of pattern matching and projections, respectively. Thus the addition of usage contexts and constraints on them refines the usual simple typing to be usage constrained. For instance, in the

-I rule, the usage context on the conclusion is constrained to be able to supply the sum of the usage contexts of the premises. If we instantiate to be the semiring, then we obtain a system that is equivalent to Barber’s DILL [Bar96].

## 4 Metatheory

McBride defines kits [McB05, BHKM12], which provide a general method for giving admissible rules that are usually proven by induction on the derivation. To produce a kit, we give an indexed family and explain how to inject variables, extract terms, and weaken by new variables coming from binders. In return, given a type-preserving map from variables in one context to -stuff in another (an environment), we get a type-preserving function between terms in these contexts. Such a function is the intrinsic typing equivalent of an admissible rule.

To make the kit-based approach work in our usage-constrained setting, we make modifications to both kits and environments. Kits need not support arbitrary weakening, but only weakening by the introduction of -use variables. The family must also respect of usage contexts. Environments are equipped with a matrix mapping input usages to output usages.

We prove simultaneous substitution using renaming. We take both renaming and substitution as corollaries of the traversal principle (Theorem 4.3) yielded from kits and environments.

Definition of kits, environments and traversal are in the module Specific.Syntax.Traversal.

### 4.1 Kits, Environments, and Traversals

A kit is a structure on -like relations , intuitively giving a way in which lives between the usage-checked variable judgement and the typing judgement . The components and are basically unchanged from McBride’s original kits. The component only differs in that new variables are given annotation , which intuitively marks them as weakenable. The requirement is new, and allows us to fix up usage contexts via skew algebraic reasoning.

###### Definition 4.1 (Kit).

For any , let denote the type of kits. A kit comprises the following functions for all , , , , and . psh : P Q QΓ A PΓ A vr : PΓ -A PΓ A
tm :
PΓ A
PΓ A wk : PΓ A PΓ, 0Δ A

An inhabitant of is described as stuff in of type .

#### Environments

In simple intuitionistic type theory, an environment is a type-preserving function from variables in the old context to stuff in the new context : an inhabitant of . The traversal function turns such an environment into a map between terms, .

For , we want maps of usaged terms . We can see that an environment of type would be insufficient — can only be inhabited when is compatible with a basis vector, so our environment would be trivial in more general cases. Instead, we care about non-usage-checked variables .

Our understanding of an environment is that it should simultaneously map all of the usage-checked variables in to stuff in in a way that preserves usage. As such, we want to map each variable not to -stuff in , but rather -stuff in , where is some fragment of . Precisely, when weighted by , we want these to sum to , so as to provide “enough” usage to cover all of the variables . When we collect all of the into a matrix , we notice that the condition just described is stated succinctly via a vector-matrix multiplication . This culminates to give us the following:

###### Definition 4.2 (Env).

For any , , , , and , where and have lengths and respectively, let denote the type of environments. An environment comprises a pair of a matrix and a mapping of variables , such that .

Our main result is the following, which we will instantiate to prove admissibility of renaming (4.6), subusaging (4.7), and substitution (4.9). The proof is in subsection 4.4.

###### Theorem 4.3 (traversal, trav).

Given a kit on and an environment , we get a function .

### 4.2 Renaming

We now show how to use traversals to prove that renaming (including weakening) and subusaging are admissible. This subsection corresponds to the Agda module Specific.Syntax.Renaming.

###### Definition 4.4 (LVar-kit).

Let be defined with the following fields.

:

The only occurrence of the usage context in the definition of is to the left of a . Applying transitivity in this place gets us the required term.

.

.

:

A basis vector extended by s is still a basis vector: if that we have for some , we also have .

Environments for renamings are special in that the matrix can be calculated from the action of the renaming on non-usage-checked variables.

###### Lemma 4.5 (ren-env).

Given a type-preserving mapping of plain variables such that