A System F accounting for scalars

The Algebraic lambda-calculus and the Linear-Algebraic lambda-calculus extend the lambda-calculus with the possibility of making arbitrary linear combinations of terms. In this paper we provide a fine-grained, System F-like type system for the linear-algebraic lambda-calculus. We show that this"scalar"type system enjoys both the subject-reduction property and the strong-normalisation property, our main technical results. The latter yields a significant simplification of the linear-algebraic lambda-calculus itself, by removing the need for some restrictions in its reduction rules. But the more important, original feature of this scalar type system is that it keeps track of 'the amount of a type' that is present in each term. As an example of its use, we shown that it can serve as a guarantee that the normal form of a term is barycentric, i.e that its scalars are summing to one.


Introduction
(Linear-)algebraic λ-calculi.A number of recent works seek to endow the λ-calculus with a vector space structure.This agenda has emerged simultaneously in two different contexts.
• The field of Linear Logic considers a logic of resources where the propositions themselves stand for those resources -and hence cannot be discarded nor copied.When seeking to find models of this logic, one obtains a particular family of vector spaces and differentiable functions over these.It is by trying to capture back these mathematical structures into a programming language that Ehrhard and Regnier have defined the differential λ-calculus [ER03], which has an intriguing differential operator as a built-in primitive and and an algebraic module of the λ-calculusterms over natural numbers.Lately Vaux [Vau07,Vau09] has focused his attention on a 'differential λ-calculus without differential operator', extending the algebraic module to positive real numbers.He obtained a confluence result in this case, which stands even in the untyped setting.More recent works on this algebraic λ-calculus tend to consider arbitrary scalars [Ehr10,Tas09].• The field of Quantum Computation postulates that, as computers are physical systems, they may behave accordingto the quantum theory.It proves that, if this is the case, novel, more efficient algorithms are possible [Sho97,Gro96] -which have no classical counterpart.Whilst partly unexplained, it is nevertheless clear that the algorithmic speed-up arises by tapping into the parallelism granted to us 'for free' by the superposition principle; which states that if t and u are possible states of a system, then so is the formal linear combination of them α.t + β.u (with α and β some arbitrary complex numbers, up to a normalizing factor).The idea of a module of λ-terms over an arbitrary scalar field arises quite naturally in this context.This was the motivation behind the linear-algebraic λ-calculus by Dowek and one of the authors [AD08], who obtained a confluence result which holds for arbitrary scalars and again covers the untyped setting.These two languages are rather similar: they both merge higher-order computation, be it terminating or not, in its simplest and most general form (namely the untyped λ-calculus) together with linear algebra in its simplest and most general form also (the axioms of vector spaces).In fact they can simulate each other [DCPTV10].Our starting point is the second one, also referred to as Lineal in this work: because its confluence proof allows arbitrary scalars and because one has to make a choice.Whether the models developed for the first language, and the type systems developed for the second language, carry through to one another via their reciprocal simulations, is a current topic of investigation.
Other motivations to study (linear-)algebraic λ-calculi.The two languages are also reminiscent of other works in the literature: • Algebraic and symbolic computation.The functional style of programming is based on the λ-calculus together with a number of extensions, so as to make everyday programming more accessible.Hence since the birth of functional programmingthere have been several theoretical studies on extensions of the λ-calculus in order to account for basic algebra (see for instance Dougherty's algebraic extension [Dou92] for normalising terms of the λ-calculus) and other basic programming constructs such as pattern-matching [CKL01, AMR09], together with sometimes non-trivial associated type theories [Pet09].Whilst this was not the original motivation behind (linear-)algebraic λ-calculi, they could still be viewed as just an extension of the λ-calculus in order to handle operations over vector spaces and make programmingmore accessible upon them.The main difference in approach is that the λ-calculus is not seen here as a control structure which sits on top of the vector space data structure, controlling which operations to apply and when.Rather, the λ-calculus terms themselves can be summed and weighted, hence they actually are vectors, upon which they can also act.• Parallel and probabilistic computation.The above intertwinings of concepts are essential if seeking to represent parallel or probabilistic computation as it is the computation itself which must be endowed with a vector space structure.The ability to superpose λ-calculus terms in that sense takes us back to Boudol's parallel λ-calculus [Bou94].It may also be viewed as being part of a series of works on probabilistic extensions of calculi, e.g.[BH03,HP00] and [dP95,DHW05,DZ11] for λ-calculus more specifically.
Imposing the existence of a norm in (linear-)algebraic λ-calculi.We do not explore any of the above-mentioned fields of applications specifically in this paper.The theory of vector spaces has many fields of applications, but also many theoretical refinements that must be studied in their own right.We take the view that, in the same way, the theory of vector spaces plus λ-calculus has got theoretical refinements that we must study.Moreover, some of these theoretical refinements are often pre-requisite in order to address other applications, as is notoriously the case with the notion of norm.For instance if we want to be able to interpret a linear combination of terms α i .ti as a probability distribution, we need to make sure that it has norm one.The same is true if we want to interpret α i .ti as quantum superposition, but with a different norm1 .Yet the very definition of a norm is difficult in our context: deciding whether a term terminates is undecidable; but these terms can produce infinities (e.g. a non terminating term which reduces to itself plus 1), hence convergence of the norm is undecidable.Related to this precise topic, Vaux has studied simply typed algebraic λ-calculus, ensuring convergence of the norm [Vau07,Vau09].Tasson has studied model-theoretic properties of the barycentric fragment of this simplytyped algebraic λ-calculus.Ehrhard has proven [Ehr10] the convergence of a Taylor series expansion of algebraic λ-calculus terms, via a System F type system [Gir71,Rey74] .
Therefore, it can be said that standard type systems provide part of the solution: they ensure the convergence of (the existence of the norm of) a term.And indeed it is not so hard to define a simple extension of System F that fits Lineal -just by providing the needed rules to type additions, scalar products and the null vector in some trivial manner (see Definition 5.1).In this paper we do provide a full-blown proof of strong normalisation for this type system.A byproduct of this result is that we are able to lift several conditions that were limiting the reduction rules of Lineal and still prove its confluence.This is because the purpose of these conditions was really to prevent indefinite forms from reducing (e.g.t − t, with t a divergent term).Hence the result together with its byproduct make Lineal into a simpler language.
Imposing a given norm in (linear-)algebraic λ-calculi.Standard type systems only provide a part of the solution; they are unable for instance to impose that well-typed terms have normal form α i .ti with the constraint that α i = 1, a property which is referred to as barycentricity in the previous literature [Tas09].Our main contribution in this paper is to have managed a type system of a novel kind, Scalar, which has the ability to keep track of 'the amount of a type' by summing the amplitudes of its contributing terms and reflecting this amount through scalars within the types.As an example application of the Scalar type system, we have demonstrated that the type system can be used to enforce the barycentric property (see section 6), thereby specializing Lineal into a barycentric, higherorder λ-calculus.
Endowing Lineal with a non-trivial, more informative, fine-grained type system was quite a challenge, as the reader will judge.We believe that fine-grained type theories for these non-deterministic, parallel, probabilistic or quantum extensions of calculi, which capture how many processes are in what class of states (with what weight), constitute truly promising routes towards relevant, less ad-hoc quantitative logics.
Plan.Section 2 presents an overview of the linear-algebraic λ-calculus (Lineal) [AD08].Section 3 presents the Scalar type system with its grammar, equivalences and inference rules.Section 4 shows the subject reduction property giving consistency to the system.Section 5 shows the strong normalisation property for this system, allowing us to lift the above discussed restrictions in the reduction rules.In section 6 we formalize the type system B for barycentric calculi.Section 7 concludes.

Linear-algebraic λ-calculus
Intuitions.As a language of terms, Lineal is just the λ-calculus together with the possibility to make arbitrary linear combinations of terms: if t and r are two terms, α.t + β.r is also a term.Regarding the operational semantics, Lineal merges higher-order computation, be it terminating or not, in its simplest and most general form (the β-reduction of the untyped λ-calculus) together with linear algebra in its simplest and most general form also (the oriented axioms of vector spaces).Care must be taken, however, when merging these two families of reduction rules.
For instance the term (λx xx)(α.t+ β.u) maybe thought of as reducing to (α.t + β.u)(α.t+ β.u) in a call-by-name-oriented view or to α.(tt) + β.(uu) in a call-by-valueoriented view, also compatible with the view that application should be bilinear, just as in linear algebra (cf.Application rules, below).Leaving both options open would break confluence, the second option was chosen, which entails restricting the β-reduction to terms not containing sums or scalars in head position (cf.Beta reduction rule, below).
Instead of introducing vector spaces via an oriented version of their axioms (e.g.α.u + β.u → (α + β).u), one could have decided to perform the β-reduction 'modulo equality in the theory of vector spaces' (e.g.α.u + β.u = (α + β).u).But there is a good reason not to do that.Define a fixed point operator and consider a term b such that Yb is β-equivalent to b + Yb and so on.Modulo equality over vector spaces, the theory would be inconsistent, as the term Yb − Yb would then be equal to 0, but would also reduce to b + Yb − Yb and hence also be equal to b.Instead, this problem can be fixed by restricting rules such as α.u + β.u → (α + β).u to terms that cannot reduce forever (cf.Factorisation rules, below), matching the old intuition that indefinite forms '∞ − ∞' must be left alone.Moreover, oriented axioms of vector spaces define vector spaces and no more than vector spaces, just as well as the original axioms do, as was shown in [AD08].Plus the orientation serves a purpose: it presents the vector in its canonical form.
Definitions.The untyped Lineal calculus, as defined in [AD08], is presented in Figure 1.Terms contain a subclass of base terms, that are the only ones that can be substituted for a variable in a β-reduction step.We use the convention that terms equal up to associativity and commutativity axioms on + (that is an AC-rewrite system).In particular, we have t + u = u + t and t + (u + r) = (t + u) + r.That rewriting modulo equational theory has been extensively studied [JK86,PS81] to a high-level of rigour, and largely discussed in the context of this calculus in [AD08].Scalars (denoted by α, β, γ, . . . ) are members of a commutative ring (S, +, ×, 0).Scalars can be defined as forming a rewrite system by itself [AD08], however for the sake of simplicity, it is enough to consider them as members of a ring.For example, the term 6.t and the term (2 × 3).t are considered to be the same term.Also, α-equivalence is considered implicitly, that is we rename variables when needed to avoid variable capturing.To be fully formal we would need to introduce these three equivalences, however they are standard so we do not mention them explicitly as long as it does not lead to confusion.The parentheses associations are the standard.We add the following conventions: α.β.t stands for α.(β.t) and t 1 . . .
The confluence of this calculus has been formally proven in [AD08].
The set of free variables of a term (notation: FV(t)) is defined as expected.The operation of substitution on terms (notation: t[b/x]) is defined as usual (i.e.taking care of renaming bound variables when needed in order to prevent variable capture), with (α.t Finally, nf(t) denotes the unique normal form of the term t and it is used only when it is clear that t has a unique normal form.As usual, → * is the application of zero or more reduction rules.
Example 2.1.One may be tempted to think λx 2.x ∼ 2.λx x.Consider the following reduction.(λx 2.x + λx x)(3.b 1 + 2.b 2 ) However while these terms behave the same in this case, they are different: the first one is the function (a base term) duplicating its argument, while the second one is twice the identity function.The display below is clarifying:

The Scalar Type System
The grammar of types, cf. Figure 2, defines the set of types (denoted by T, R, S) and its syntactic subclass (denoted by U, V, W ) of what we call unit types.Note that the grammar for unit types does not allow for scalars except to the right of an arrow.More generally, note the novelty of having scalars at the level of types.We will see that these scalars keep track of the sum of the coefficients of the terms contributing to the type.
Type variables are denoted by X, Y , etc. and can only ever be substituted by a unit type.Contexts are denoted by Γ, ∆, etc. and are defined as sets {x : U, . ..},where x is a term Restriction (***) is the one that limits the β-reduction, whereas restrictions (*) and (**) are those that avoid confluence problems related to infinities and indefinite forms, as discussed above.
Figure 1: Syntax and reduction rules of Lineal.
variable appearing only once in the set and U is a unit type.We usually omit the brackets of the set.The substitution of V for X in U is defined as usual and is written In order to avoid capture, we consider that X i cannot appear free in V j , with j < i.Also we assume that free and bound variables of a type are distinct.We also may abuse notation and say X / ∈ S meaning that none of the X i from X are in the set S. We write Γ[U/X] to the substitution of U for X in each type of Γ.Also we write (α.T )[U/X] for α.T [U/X].FV(T ) designates the set of free variables of the type T , defined in the usual manner, and FV(Γ) is the union of the sets of free variables of each type in Γ. Scalars are denoted by α, β, γ . . .and are members of the same commutative ring (S, +, ×) as those of terms.
We also define an equivalence relation upon types as follows: Definition 3.1.For any α, β and T .We define the type equivalence ≡ to be the least congruence such that The complete set of typing rules is shown in Figure 2. Splitting the grammar into general types and unit types is a necessary consequence of the fact that we want scalars in the types to reflect scalars in the terms (e.g.α.λx t should have a type α.U ).Indeed if we did not have the restriction on the left hand side of an arrow being a unit type, i.e.U → T , then we would have types like (α.X) → X, which a priori do not make sense, because Types: R, S, T :: We want the scalars in the types to represent those in the terms, hence the rule s I .The rule + I takes care of sums of terms and the term 0 gets the special type 0 by an axiom.
Finally, let us go back to the application.The standard rule → E from System F needs to be consistent with the extra rules for application that we have on top of β-reduction in Lineal; namely the Application rules: Note that the terms t and r in rules (1) and (2) must now have the same type (up to a scalar) according to the rule + I , so the type of t + r is analogous to the type of α.t in rules (3) and (4).Also, the type for 0 in rules (5) and ( 6) is the same as that of 0.t if we take α = 0 in rules (3) and (4).Thus we can focus our discussion on rules (3) and (4).
By the rule (3), we must have: Γ ⊢ tr : β.T By combining these two we obtain the rule → E presented in Figure 2. Remark 3.2.A good insight into the type system is that, due to the equivalence relation, scalars in types occur only at the top-level and in the target subtypes of arrow types.This fits very well with the idea that in Lineal all term constructs are linear, except for abstraction.With this in mind, the syntax of arrow types could have been restricted to U → α.V , or even written as U → α V instead.In other words, we could get rid of the type equivalences (cf.Definition 3.1) and represent each type equivalence class by just its canonical member.Such a design choice would spare us some lemmas (cf.Section 4), but comes at a price: • Equivalences between 0.T and 0.R or 1.T and T would still need to be enforced through an equivalence relation or some unelegant case distinctions, at least if we want to maintain them.• More generally our aim is to reflect some of the vectorial structure of the terms of Lineal up at the level of types.In that sense the explicit type equivalences we have given provide a good indication that types have the desired structure.

Subject reduction
The following theorem ensures that typing is preserved by reduction.Having such a property is part of the basic requirements for a type system.
Theorem 4.1 (Subject Reduction).For any terms t, t ′ , any context Γ and any type T , if t → t ′ and Γ ⊢ t : T , then Γ ⊢ t ′ : T .
The proof of this theorem is quite long and non-trivial.This is one of the main technical contributions of the paper.4.1.Preliminary lemmas.In order to prove this theorem, we need several auxiliary lemmas giving general properties of our system.We have tried to provide an intuition of every lemma so as to make it easier to follow.Also, we divided them in four groups, reflecting the nature of their statements.Standard and straightforward lemmas are left without proof to improve readability.4.1.1.Lemmas about types.The lemmas in this section are statements about the properties of the types themselves, i.e. the type equivalence relation.
It is not so hard to see that every type is equivalent to unit type multiplied by a scalar.A unit type itself can of course always be multiplied by 1. Lemma 4.2 (α unit).For each type T there exists a unit type U and a scalar α such that T ≡ α.U .This first lemma should not be misinterpreted however: this does not mean to say that any scalar appearing within a type can be factored out of the type.For example even a simple unit type X → α.X is not equivalent to α.(X → X).
The following just says that when two types are equivalent, then the outer left scalars are the same: Lemma 4.3 (Unit does not add scalars).For any unit types U , U ′ and scalars α, β, if Informal proof.Following the grammar of unit types, neither U nor U ′ could contain scalars in this head form but only in the right hand side of an arrow.However, no equivalence rule lets it come out from the right of the arrow and get to the head-form, so if α.U ≡ β.U ′ that means α = β = 0 or U ≡ U ′ and α = β.4.1.2.Relation between types.We define a family of relations between types inspired by [Bar92, def.4.2.1]:Definition 4.4.For any types T, R any context Γ and any term t such that Note that this relation is stable under reduction in the following way: Lemma 4.6 ( -stability).For any types T, R, set of type variables V, terms t and r and context ∈ FV(Γ).We only have to prove that Γ ⊢ r : R is derivable from Γ ⊢ r : T .We proceed now by cases: • R ≡ ∀X.T , then using rules ∀ I and ≡, we can deduce Γ ⊢ r : R. • T ≡ ∀X.S and R ≡ S[U/X], then using rules ∀ E and ≡, we can deduce Γ ⊢ r : R.
The following lemma states that scalars do not interfere with the relation.Lemma 4.7 (Scalars keep order).For any types T , R, any scalar α, context Γ, set of variable types V and term t, if T t V,Γ R then α.T α.t V,Γ α.R.The following lemma states that if two arrow types are ordered, then they are equivalent up to some substitutions.
Lemma 4.8 (Arrows comparison).For any types U, V , T, R, context Γ, set of type variables V and term t, if V → R t V,Γ U → T , then there exist unit types W and variables 4.1.3.Classic lemmas.The lemmas in this section are the classic ones, which appear in most subject reduction proofs.
Proving subject reduction means proving that each reduction rule preserves the type.The way to do this is to go in the opposite direction to the reduction rule, i.e. to study the reduct so as to understand where it may come from, thereby decomposing the redex in its basic constituents.Generation lemmas accomplish that purpose.
We need five generation lemmas: the classical ones, one for applications (Lemma 4.9) and one for abstractions (Lemma 4.11); and three new ones for the algebraic rules, one for products by scalars different than 0 (Lemma 4.12) other for product by 0 (Lemma 4.13) and one for sums (Lemma 4.14).All of them follow by induction on the typing derivation.
Remark 4.15.Note that the assumption of the previous lemma could be weakened as Γ ⊢ t + r : T , since by Lemma 4.2, any type T can be written as α.U for some scalar α and unit type U .
The following lemma is quite standard in proofs of subject reduction for System F -like systems and can be found for example in [Bar92,prop. 4.1.19]and [Kri90, prop.8.2 and 8.5].It ensures that when substituting types for type variables or terms for term variables, in an adequate manner, then the type derived remains valid.
Lemma 4.16 (Substitution lemma).For any term t, any base term b, any types T , U and any context Γ, (1) 4.1.4.Lemmas about the scalars.This section contains the lemmas which make statements about the relative behaviour of the scalars within terms and within types.For example, scalars appearing in the terms are reflected within the types also.This is formalized in the following lemma, which is proved by induction on the typing derivation.
Lemma 4.17 (Scaling unit).For any term t, scalar α, type T and context Γ, if Γ ⊢ α.t : T then there exists U and γ ∈ S such that T ≡ α.γ.U .
Base terms are typed by unit types or its equivalent (proof by induction on the typing derivation).
Lemma 4.18 (Base terms in unit).For any base term b, context Γ and type T , if Γ ⊢ b : T then there exists U such that T ≡ U .
By ax 0 , it is easy to see that 0 has type 0, but also by using equivalences between types we have that ∀X.0 is equivalent to 0. Moreover, for any T , Γ and V with V ∩FV(Γ) = ∅,such that 0 0 V,Γ T , we have T ≡ 0. Then we can state the following lemma (proof by induction on the typing derivation).
The following theorem is an important one.It says that our Scalar type system is polymorphic only in the unit types but not in the general types in the sense that even if it is possible to derive two types for the same term, the outermost-leftmost scalar (i.e. the scalar in the head position) must remain the same.Its proof is not trivial, as it uses several of the previously formulated lemmas.
Proof.Structural induction over t.
Proof.Assume Γ ⊢ t : T , then by Lemma 4.2, T ≡ α.U .Since Γ ⊢ t : 0 ≡ 0.U we obtain by Theorem 4.20 that α = 0. Since 0 has type 0 which is equivalent to 0.U for any U , 0 can still act as argument for an abstraction or even be applied to another term.In either case the result is a term of type 0: Lemma 4.22 (Linearity of 0).For any term t, any context Γ and any type T , (1) If Γ ⊢ 0t : T then T ≡ 0.
Proof.We proceed by checking that every reduction rule preserves the type.We give two cases as example, the full proof can be found in Appendix A.3. rule (t + r)u → tu + ru: Let Γ ⊢ (t + r)u : T ≡ 1.T .Then, by Lemma 4.9, there exist α, β, U, V and T ′ (t+r)u T such that Γ ⊢ λx t : β.(U → T ′ ) and Γ ⊢ b : α.U with α × β = 1.Since b is a base term, by Lemmas 4.18 and 4.3, α = 1 and so β = 1.Then by Lemma 4.11, there exist V , R and

Strong Normalisation, simplified reduction rules and Confluence
The Scalar type system is now proved to have the strong normalisation property, i.e. every typable term is strongly normalising, it cannot reduce forever.In order to show this we first set up another type system, which simply 'forgets' the scalars.Hence this simpler type system is just a straightforward extension of System F , called here λ2 la (Definition 5.1).In the literature surrounding not Lineal but its cousin, the algebraic λ-calculus, one finds such a System F in [Ehr10], which extends the simply typed algebraic λ-calculus of [Vau07,Vau09] -our λ2 la is very similar.Secondly we prove strong normalisation for it (Theorem 5.9).Thirdly we show that every term which has a type in Scalar has a type in λ2 la (Lemma 5.11), which entails strong normalisation in Scalar (Theorem 5.12).
This strong normalisation proof constitutes the second main technical contribution of the paper.The confluence of a simplified version of Lineal is derived from it.
In this section we use the following notation: T(λ2 la ) is the set of types of λ2 la (denoted by A, B, C).Λ is the set of terms of Lineal.Γ t : A says that it is possible to derive the type A ∈ T(λ2 la ) for the term t ∈ Λ in the context Γ under the typing rules of λ2 la .We just use ⊢ for Scalar.In addition, we use Name ⊳ to distinguish the names of the typing rules in λ2 la from those of Scalar.
Definition 5.1.The type grammar of λ2 la is the following: The typing rules of λ2 la are those of System F plus the following rules: In Scalar it is possible to derive ⊢ 3.λx (x + x) : 3.(X → 2.X), while in λ2 la we have ⊢ 3.λx (x + x) : X → X.
In order to prove strong normalisation we extend the proof for λ2.The standard method was invented by Tait [Tai67] for simply typed λ-calculus and generalized to System F by Girard [Gir72].Our presentation follows [Bar92, sec.4.3] (who uses an analogous to Krivine's saturated sets [Kri90,thm. 8.9]).The following definitions are taken from this reference -with slight modifications to handle the extra λ2 la rules.
The strong normalisation property entails that every term is strongly normalising, so first we define the set of strongly normalising terms.
The notion of closure is often captured by the notion of saturated set.We use the notation t = t 1 , . . ., t n with n ≥ 0. Also r t = rt 1 . . .t n where if n = 0 it is just r. where The basic idea is to prove that types correspond to saturated sets.In order to achieve this, we define a valuation from types to SAT (in fact, from type variables to SAT and then, we define a set in SAT by using such a valuation).
(1) A valuation in SAT is a map ξ : V → SAT, where V is the set of type variables.
(3) Given a valuation ξ in SAT, we define for every T ∈ T(λ2 la ) a set [[T ]] ξ ⊆ Λ as follows: Just like in Definition 5.5, we define another valuation, this time from term variables to base terms.We use it to check what happens when we change every free variable of a term for any other base term.The basic idea is the following: we define ρ, ξ t : A to be the property of changing every free term variable in t for another term with the help of the valuation ρ (a base term, since term variables only run over base terms) and still having the resulting term in the set [[A]] ξ .So, we define Γ t : A to be the same property, when the property holds for every pair in Γ and for every valuations ρ and ξ.This is formalised in the following definition (Definition 5.7) and with this definition, we prove that if a term has a type in a valid context, then the property above holds (Theorem 5.8), which yields the strong normalisation theorem (Theorem 5.9) via the concept of saturated set (because saturated sets are subsets of SN).
Definition 5.7.Proof.We proceed by induction on the derivation of Γ t : T .We show one case as an example.The full proof is in Appendix A.5.
Proof.Let Γ t : A. Then by Theorem 5.8, Γ t : A. Define ρ 0 (x) = x for all x and let ξ be a valuation in SAT.Then ρ 0 , ξ Γ (i.e. for all (x : B) ∈ Γ, ρ 0 , ξ x : It is possible to map every type from Scalar to a type in λ2 la as follows.
Definition 5.10.Let (•) ♮ be the following mapping from types in Scalar to T(λ2 la ): where A is an arbitrary fixed type in T(λ2 la ).
We also extend this definition to contexts: The following lemma ensures that if it is possible to give a type to a term in Scalar then it is possible to give to the term the mapped type in λ2 la .Example 5.2 may help to see this.
Theorem 5.12 (Strong normalisation).If Γ ⊢ t : T then t is strongly normalising.Proof.By Lemma 5.11, Γ ♮ t : T ♮ , then by Theorem 5.9, t is strongly normalising.Taking up again the example of Section 2, terms like Y are simply not allowed in this typed setting, since Theorem 5.12 ensures that all the typable terms have a normal form.So we do not have infinities and hence the intuitive reasons for having restrictions ( * ) on the Factorisation rules of the linear-algebraic calculus (cf. the reduction rules in Section 2) have now vanished.If we drop them, the example becomes as follows.
Example 5.13.Consider some arbitrary typable and hence normalising term t.Then α.t − α.t can be reduced by a factorisation rule into (α − α).t.This reduces in one step to 0, without the need to reduce t.
It turns out that, in general, for typable terms we can indeed drop the restrictions (*) and (**) without breaking the confluence of Lineal.These restrictions were there only due to the impossibility of checking for the normalisation property in the untyped setting.Having this property, the confluence of the system follows naturally.
Theorem 5.14 (Confluence).Let t be a term of Lineal, as it appears in Figure 1, but without restrictions (*) and (**).If t is typable in Scalar, t → * u and t → * r, then there exists a term t ′ such that u → * t ′ and r → * t ′ .
Proof.First let us introduce some notation.Let → β be a β-reduction and → a any reduction from Figure 1 but the β-reduction, without restrictions (*) and (**).Moreover, → * β and → * a are the application of zero or more of such rules, as usual.
The proof follows in several steps.(1) First we prove local confluence for the algebraic fragment, i.e. if t → a u and t → a r, then there exists a term t ′ such that u → * a t ′ and r → * a t ′ .(2) Then we prove local confluence for the β-reduction, i.e. if t → β u and t → β r, then there exists a term t ′ such that u → * β t ′ and r → * β t ′ .(3) Finally, we prove that algebraic rules and β-reduction commutes, i.e. if t → a u and t → β r, then there exists a term t ′ such that u → * t ′ and r → * t ′ , where → * is a reduction sequence of zero or more steps involving any rules.This prove the local confluence of the system.The well known Newman's lemma states that local confluence plus strong normalisation implies confluence (cf.for example [TeR03, thm.1.2.1]).This is how the proofs of the steps look like.(1) Valiron did a semi-automatized proof in the interactive theorem prover Coq [Coq09].
The interested reader can find the proof in [Val11].An easier-to-read explanation of it appears in [ADCV11].
(2) The local confluence of the β-reduction follows from [Dou92] and a trivial extension of the confluence of lambda-calculus.
Note that the proofs of subject reduction (Theorem 4.1) and strong normalisation (Theorem 5.12) have been done in the general case, without restrictions (*) and (**), so they are still valid for the simplified calculus.
Having dropped restrictions (*) and (**) is an important simplification of the linearalgebraic λ-calculus, which becomes really just an oriented version of the axioms of vector spaces [AD04] together with the call-by-value strategy (i.e.restriction (***) remains, of course, to make all functions remain linear in their arguments, in the sense of linear-algebra).

Barycentric λ-calculus
By slightly modifying our system, the Scalar type system may be used in order to specialize Lineal into a higher-order barycentric calculus.In order to illustrate this point, let us consider the following type judgement, which can be obtained from Scalar : where B stands for ∀X.X → X → X.Note that the type B has true, false and linear combinations of them with scalars summing to one, as members.In this example the type system provides a guarantee that the function is barycentric and that if it receives a barycentric argument, it preserves this property.For example, if we apply such a function to 1 2 .(true+ false) we obtain: .false.
Although this seems feasible, we do not develop a full-blown barycentric higher-order λcalculus and associated properties in this paper.We just show that the Scalar type system accomplishes part of the job by checking for the barycentric property, i.e. checking that the normal form of a term has amplitudes summing to one.A barycentric λ-calculus fragment of the algebraic λ-calculus has already been studied for its own sake [Tas09], however in this work the calculus was endowed with a simple type system, not one that would recognize barycentric terms amongst other terms.
To this end let us define a type system with the rules and grammar of Scalar, but where the valid types are the classic ones (i.e.types exempt of any scalar, which we have referred to as T(λ2 la ) in Definition 5.1), whilst all the other types are just intermediate types: Definition 6.1.We define the type system B for the barycentric calculus to be the Scalar type system with the following restrictions: • Contexts are sets of tuples (x : A), with A ∈ T(λ2 la ), • Type variables run over T(λ2 la ) instead of unit types, i.e. the rule ∀ E accepts only A ∈ T(λ2 la ), • The final sequent has type in T(λ2 la ).Note that the type derivation may have, as intermediate sequents, sequents that do not have their type in T(λ2 la ) but in Scalar.
Example 6.2.The sequent y : X ⊢ 2.(λx 1 2 .x)y: X is valid in B, even if intermediate sequents do not have their types restricted to T(λ2 la ): x)y : X In order to show that this type system does the job, let us define the weight function: Definition 6.3.Let ω : Λ → R be a function defined inductively by: where b is a base term.
Example 6.4.Continuing Example 6.2, note that 2.(λx 1 2 .x)y→ * y.The weight function cannot check the barycentric property on the first term (indeed ω(2.(λx 1 2 .x)y)= 2), even when this term reduces to a barycentric term.For this matters, we use the type system B to check this property, as stated by the following theorem.Theorem 6.5 (Normal-form of terms in B have weight 1).Let Γ ⊢ t : A with A ∈ T(λ2 la ), then ω(nf(t)) = 1.
We need the following three lemmas in order to prove this theorem.The first lemma says that an application in normal form can only happen when a variable is in head position.Lemma 6.6.If t 1 t 2 is in normal form, then t 1 = x r.
The following lemma says that if there is an application in normal form (which by the previous lemma has to have the form x r), then it cannot be typed in the type system B with a type having a scalar at the right of an arrow, the only possible scalar have to be in the head position of the type.Lemma 6.7.Γ ⊢ x r : T and x r is in normal form, then there exist A ∈ T(λ2 la ) and α ∈ S such that T ≡ α.A.
Proof.Induction on the derivation of Γ ⊢ x r : T .cf. Appendix A.8.
The last lemma shows that scalars cannot be removed by subsumption.Lemma 6.8.Let A ∈ T(λ2 la ).For any T, V, Γ, t, if α.T t V,Γ α.A, then T ≡ B with B ∈ T(λ2 la ).
Proof.Case analysis.cf.Appendix A.9 Using these lemmas, the proof of Theorem 6.5 goes as follows.
Proof.Instead, we prove the more general case: If Γ ⊢ t : α.A then ω(nf(t)) = α, by structural induction on nf(t).We take Γ ⊢ nf(t) : α.A, which is true by Theorem 4.1.We show two cases as an example.The full proof is in Appendix A.10.
w, whose weight is equal to one.So, a priori this ω function cannot tell us that this term yields a barycentric term.However the fact that has type A in T(λ2 la ), according to the Theorem 6.5, anticipates this result.
• One might think that unit types are just as good as barycentric types B for the sake of obtaining Theorem 6.5, via a combination of Subject-reduction and the uniqueness of scalars property (cf.Theorem 4.20).This is not quite the case, here is a counter-example: x : U → 2.U, y : U ⊢ x 1 2 .y: U but ω(x 1 2 .y)= 1 2 But the more significant difference between unit types and B is one of composability: the application of a term of unit type to another is not necessarily of unit type; whereas barycentric types on the other hand are preserved under application.Thus terms in B are not only barycentric; they can also be viewed as barycentric-preserving functions.

Summary of contributions
In summary, we have defined a System F -like type system for an extension of Lineal, a λ-calculus which allows making arbitrary linear combinations of λ-calculus terms α.t + β.u.The Scalar type system is fine-grained in that it keeps track of the 'amount of a type', i.e. the scalar of the type of a term is the sum of the scalars of the types of the contributing subterms.

Our main technical contributions were:
• A proof of the subject reduction property of the Scalar type system (Theorem 4.1).This came out after having proven a set of lemmas related to the equivalence relation intrinsic to the types and another set of lemmas explaining how the scalars within the types are related to the scalars within the terms.Once all of the important properties were known, we were able to use them to decompose and recompose any term before and after applying a reduction rule, so as to show that every reduction rule preserves the types.• A proof of the strong normalisation property of the Scalar type system (Theorem 5.12).
The technique used to prove the strong normalisation property was by proving that such property would hold for a simpler system and then to show the correspondence between the two systems.• A proof that under strong normalisation, most of the conditions upon the Lineal reduction rules can be lifted (e.g.allowing the factorisation not only of closed normal terms but of any term) without jeopardizing confluence, thereby simplifying the Lineal language.• A proof that the Scalar type system can be used to check that a term has the barycentric property, i.e. that the amplitudes of its normal form are summing to one.Arguably a denotational semantics approach might have led to less syntactic proofs of the properties of the type system, sustained by the guiding intuition about an underlying mathematical space.On the other hand, the complexity of the proofs in this paper is largely due to the large number of rules (16 rules plus associativity and commutativity of +).Moreover the issue of models of (linear-)algebraic λ-calculus is a challenging, active topic of the current research.We know of the categorical model of simply typed Lineal [Val10] and the finiteness space model of simply typed algebraic λ-calculus [Ehr05,Tas09].Moreover, even if both calculi simulate each other [DCPTV10], it is not clear whether the translation applies at the level of models.Hence known models are intricate and tend not to cover the set of terms under consideration in this paper.Note also that since the models of untyped λ-calculus are uncountable, the models of (linear-)algebraic λ-calculus are likely to be vector spaces of uncountable dimensions.These are fascinating, open questions.(1) Γ, x : U ⊢ t : T → I Γ ⊢ λx t : U → T This is the trivial case.Note that for each V ⊆ FV(Γ), one has U → T ≻ λx t V,Γ U → T .
(2) Γ ⊢ λx t : ∀X.T ∀ E Γ ⊢ λx t : T [V /X] By the induction hypothesis there exist U , R and V such that Γ, x : U ⊢ t : R and U → R λx t V,Γ ∀X.T ≻ λx t X,Γ T [V /X].(Observe that since X is bounded in ∀X.T , any variable in F V (Γ) is distinct from it).
(4) Γ ⊢ λx t : T T ≡ R ≡ Γ ⊢ λx t : R By the induction hypothesis there exist U , S and V such that Γ, x : U ⊢ t : S and U → S λx t V,Γ T ≡ R.
x n is the set of free variables in t. • Let ρ be a valuation in Λ b and ξ a valuation in SAT.Then − ρ, ξ satisfies t : A, notation ρ, ξ t : A, if and only if [[t]] ρ ∈ [[A]] ξ .− ρ, ξ Γ if and only if ρ, ξ x : A for all x : A in Γ − Γ t : A if and only if for every ρ, ξ, ρ, ξ Γ implies ρ, ξ t : A.