Linearity in the Non-deterministicCall-by-Value Setting

Linearity in the non-deterministic call-by-value setting

Abstract

We consider the non-deterministic extension of the call-by-value lambda calculus, which corresponds to the additive fragment of the linear-algebraic lambda-calculus. We define a fine-grained type system, capturing the right linearity present in such formalisms. After proving the subject reduction and the strong normalisation properties, we propose a translation of this calculus into the System with pairs, which corresponds to a non linear fragment of linear logic. The translation provides a deeper understanding of the linearity in our setting.

Introduction

Several non-deterministic extensions of -calculus have been proposed in the literature, e.g. [BoudolIC94, BucciarelliEhrhardManzonettoAPAL12, DezaniciancagliniDeliguoroPipernoTCS96, DezaniciancagliniDeliguoroPipernoSIAM98]. In these approaches, the sometimes called must-convergent parallel composition, is such that if and are two -terms, (also written ) represents the computation that runs either or non-deterministically. Therefore, can run either or , which is exactly what expresses. Extra rewriting rules (or equivalences, depending on the presentation) are set up to account for such an interpretation, e.g. .

This right distributivity can alternatively be seen as the one of the function sum: is defined as . This is the approach of the algebraic lambda-calculi presented in [ArrighiDowekRTA08] and [VauxMSCS09], that were introduced independently but that resulted afterwards to be strongly related [AssafPerdrixDCM11, DiazcaroPerdrixTassonValironHOR10]. In these algebraic calculi, a scalar pondering each ‘choice’ is considered in addition to the sum of terms.

In the call-by-value (or cbv) version of these algebraic/non-deterministic calculi, e.g. [BoudolIC94, DezaniciancagliniDeliguoroPipernoSIAM98, ArrighiDowekRTA08], it is natural to consider also the left distributivity of application over sums: . To our knowledge, this was first observed in [HennessySIAM80]. Indeed, a sum is not a value, in the sense that it represents a non-deterministic choice that remains to be done, and therefore cannot subsitute the argument . In algebraic terms, it means that functions are linear: .

The work we present here is motivated by a better understanding of this linearity, and so our first attempt was to interpret such a cbv calculus in Linear Logic [GirardTCS87] (indeed linear functions can be precisely characterised in this logic). Surprisingly, it appeared that the target calculus was a non linear fragment of the intuitionistic multiplicative exponential Linear Logic (imell), shining a light on the difference between the linearity in these non-deterministic calculi, and the common algebraic notion of linear functions. Since the non linear fragment of imell corresponds to the System  with pairs [Dicosmo95, Sec. 1.5], and this latter might be better known by the reader, we present in this paper a (reversible) translation into the System  with pairs.

Notice also that the left distributivity of application over sum induces a completely different computational behaviour compared to the one in cbn calculi. Consider for instance the term applied to a sum . In the first case, it reduces to and then to , whereas a cbn reduction would lead to and then to . In particular, the cbv algebraic calculus we mentioned above (Lineal, [ArrighiDowekRTA08]) was originally meant to express quantum computing, where a superposition is seen as a quantum superposition. Hence reducing into is considered as the forbidden quantum operation of “cloning” [WoottersZurekNATURE82], while the alternative reduction to is seen as a “copy”, or cnot, a fundamental quantum operation [MonroeMeekhofKingItanoWinelandPRL95].

Outline.

In this paper we propose (in Sec. 1) a type system, called , capturing the linear cbv behaviour of the sum operator that we discussed above. Then we prove its correctness properties, namely subject reduction and strong normalisation in Sec. 2. Its logical interpretation (that is, the translation into System with pairs) is developed in Sec. 3. We conclude with a discussion about the linearity of the call-by-value setting. We leave in the appendices extra examples and some technical details such as auxiliary lemmas.

1 The Calculus

1.1 The Language

We consider the call-by-value -calculus [PlotkinTCS75] extended with a non-deterministic operator in the spirit of the parallel composition from [BucciarelliEhrhardManzonettoAPAL12]. This setting can be seen as the additive fragment of Lineal [ArrighiDowekRTA08]. The set of terms and the set of values are defined by mutual induction as follows (where variables range over a countable set and are denoted by ):

Intuitively denotes the non-deterministic choice between and , and hence, as discussed in the introduction, reduces to the non-deterministic choice . Analogously, in this call-by-value setting, reduces to . The term is introduced to express the impossible computation, and hence always reduces to , while and reduce to , because none of them continue reducing (notice that is not a value), and have an impossible computation on them. Since the operator represents a non deterministic choice, where no one have precedence, terms are considered modulo associativity and commutativity of  (that is an AC-rewrite system [JouannaudKirchnerSIAM86]). Notice that considering either as a sum of functions or as a sum of arguments—depending on its position—is also natural with the previous definitions, where becomes the sum of elements.

The -conversion and the set () of free variables of are defined as usual (cf. [Barendregt84, Sec. 2.1]). We say that a term is closed whenever . Given a term and a value , we denote by to the term obtained by simultaneously substituting for all the free occurrences of in , taking care to rename bound variables when needed in order to prevent variable capture. Hereafter, terms are considered up to -conversion. The five rewrite rules plus the -reduction are summarised as follows.

Distributivity rules: Zero rules: -reduction:
, , , .
, ,

Our objective is to define a type system, capturing as much as possible the behaviour of . Roughly speaking, we want a system where, if has type and has type , then has type . So the natural typing rule for such a construction is “ and entails ”. We also want a special type distinguishing the impossible computation , which we call . Due to the associative and commutative nature of , we consider an equivalence between types taking into account its commutative nature. Hence if is a type, is an equivalent type. Also the neutrality of with respect to is captured by an equivalence between and . Finally, as usual the arrow type characterises the functions taking an argument in and returning an element of . However, notice that the type captures a behaviour that is not appearing in our setting: there is no function taking a non-deterministic superposition as argument. Indeed, if has type and type , any function distributes as , so needs to be characterised by a function taking both and , but not simultaneously. In order to capture such a behaviour, we introduce a unit type (i.e. an atomic type with respect to ), capturing elements which are not sums of elements, and hence the arrow types have the shape , where the different arguments to which the function can be applied, are captured by polymorphic types with variables ranging on unit types. For example, the previous term can have type , where if is applied to the above discussed of type , it reduces to of type .

To take into account the above discussion, the grammar of the  type system is defined by mutual induction as follows (where type variables range over a countable set and are denoted by ):

Contexts are denoted by and are defined as sets of pairs , where each term variable appears at most once. The substitution of  by in  is defined analogously to the substitution in terms, and is written . We also use the vectorial notation  for if and . To avoid capture, we consider that cannot appear free in , with . Free and bound variables of a type are assumed distinct.

The above discussed equivalence relation  on types, is defined as the least congruence such that:

Within this equivalence, it is consistent to use the following notation:

Notation:  ;  if .

Remark 1

Every type is equivalent to a sum of unit types.

Returning to the previous example, reduces to and its type have to be an arrow with a polymorphic unit type at the left. Such a type must allow to be converted into both the type of and the type of . Hence, consider and to be the respective types of and , we need to be of type for some and where and for some unit types and . That is, we need that if has such a type, then has type and type . We can express this with the following rule

 \prooftreeΓ⊢t:∀X.(U→S)Γ⊢v1+v2:U[W1/X]+U[W2/X]\justifiesΓ⊢t(v1+v2):S[W1/X]+S[W2/X]\endprooftree

In the same way, for the right distributivity, if and are two functions of types and respectively, then the application needs and to be the type of . Therefore, the polymorphism plays a role again, and if has type and has type such that and also equal to the type of , then has a type. It can be expressed by

 \prooftreeΓ⊢t+u:∀X.(U→S)+∀X.(V→R)Γ⊢v:U[W1/X]=V[W2/X]\justifiesΓ⊢(t+u)v:S[W1/X]+R[W2/X]\endprooftree

Notice that when combining both cases, for example in , we need the type of to be an arrow accepting both the type of and the type of as arguments, and the same happens with the type of . So, the combined rule is

 \prooftreeΓ⊢t+u:∀X.(U→S)+∀X.(U→R)Γ⊢v1+v2:U[V/X]+U[W/X]\justifiesΓ⊢(t+u)(v1+v2):S[V/X]+R[W/X]\endprooftree

The arrow elimination has become also a forall elimination. For the general case however it is not enough with the previous rule. We must consider bigger sums, which are not typable with such a rule, as well as arrows with more than one , e.g. , where has the correct type. Since it is under a sum, and the elimination must be done simultaneously in all the members of the sum, it is not possible with a traditional forall elimination.

The generalised arrow elimination as well as the rest of the typing rules are summarised in Fig. 1. Rules for the universal quantifier, axiom and introduction of arrow are the usual ones. As discussed before, any sum of typable terms can be typed using rule . Notice that there is no elimination rule for  since the actual non-deterministic choice step (which eliminates one branch) is not considered here. For similar calculi where the elimination is present in the operational semantics, see e.g. [BucciarelliEhrhardManzonettoAPAL12, DiazcaroDowek12]. Finally, a rule assigns equivalent types to the same terms.

Example 1

Let , , , , and . Then

 \prooftreeΓ⊢λx.t+λy.u:∀X.(U→T)+∀X.(U→R)Γ⊢v1+v2:V1+V2\justifiesΓ⊢(λx.t+λy.u)(v1+v2):T[W1/X]+T[W2/X]+R[W1/X]+R[W2/X]\using→E\endprooftree

Notice that this term reduces to .

Example 2

Let and . Then the term , which reduces to , can be typed in the following way:

 \prooftreeΓ⊢λx.x:∀X.X→XΓ⊢v1+v2:U+V\justifiesΓ⊢(λx.x)(v1+v2):U+V\using→E\endprooftree

Notice that without the simultaneous forall/arrow elimination, it is not possible to type such a term.

2 Main Properties

The  type system is consistent, in the sense that typing is preserved by reduction (Theorem 2.1). Moreover, only terms with no infinite reduction are typable (Theorem 2.2).

The preservation of types by reduction, or subject reduction property, is proved by adapting the proof of Barendregt [Barendregt92, Section 4.2] for the System : we first define a binary relation  on types, and then prove the usual generation and substitution lemmas (cf. Appendix A for more details).

Theorem 2.1 (Subject Reduction)

For any terms , any context  and any type , if then .

We also prove the strong normalisation property (i.e. no typable term has an infinite reduction) by adapting the standard method of reducibility candidates [Girard89, Chap. 14] to the  type system. The idea is to interpret types by reducibility candidates, which are sets of strongly normalising terms. Then we show that as soon as a term has a type, it is in its interpretation, and thereby is strongly normalising.

We define here candidates as sets of closed terms. The set of all the closed terms is writen , and  denotes the set of strongly normalising closed terms. In the following, we write for the set of reducts in one step of a term  (with any of the six rules given in Sec. 1.1), and for the set of its reducts in any number of steps (including itself). Both notations are naturally extended to sets of terms. A term is a pseudo value when it is an abstraction or a sum of them: A term that is not a pseudo value is said to be neutral, and we denote by  the set of closed neutral terms.

Definition 1

A set  is a reducibility candidate if it satisfies the three following conditions: Strong normalisation: . Stability under reduction: . Stability under neutral expansion: If , then implies .

We denote the reducibility candidates by , and the set of all the reducibility candidates by . Note that  is in . In addition, the term  is a neutral term with no reduct, so it is in every reducibility candidate by . Hence every reducibility candidate is non-empty.

Let be the closure of a set of terms  by . It can be defined inductively as follows: If , then , and if and , then .

We can actually use this closure operator to define reducibility candidates:

Lemma 1

If , then .

In order to interpret types with reducibility candidates, we define the operators ‘arrow’, ‘plus’ and ‘intersection’ in : Let . We define: and where .

Proposition 1

Let . Then both  and  are reducibility candidates. Moreover, if is a family of , then  is a reducibility candidate.

The operator  is commutative and associative on terms, and hence so is the operator  defined on sets of terms. Therefore,  is commutative and associative on reducibility candidates. In addition, (a reducibility candidate according to Lemma 1) is neutral with respect to . Lemma 2 formalises these properties.

Lemma 2

Let . Then , and .

Type variables are interpreted using valuations, i.e. partial functions from type variables to reducibility candidates: The interpretation of a type  in a valuation  (that is defined for each free type variable of ) is given by

 \llbracketX\rrbracket‘r = ‘r(X)\llbracket¯¯¯0\rrbracket‘r = ¯¯¯∅\llbracketU→T\rrbracket‘r = \llbracketU\rrbracket‘r→\llbracketT\rrbracket‘r\llbracketT+R\rrbracket‘r = \llbracketT\rrbracket‘r∓\llbracketR\rrbracket‘r\llbracket‘AX.T\rrbracket‘r = ⋂A‘:RC\llbracketT\rrbracket‘r,X↦A =

Lemma 1 and Proposition 1 ensure that each type is interpreted by a reducibility candidate. Furthermore, Lemma 2 entails that this interpretation is well defined with respect to the type equivalences.

Lemma 3

For any types , and any valuation , if then .

We show that this interpretation complies with typing judgements. Reducibility candidates deal with closed terms, whereas proving the adequacy lemma by induction requires the use of open terms with some assumptions on their free variables (which are ensured by the context). Therefore we use substitutions  to close terms:

 ‘s:=∅|x↦u;‘st∅=t,tx↦u;‘s=t{u/x}‘s.

Given a context , we say that a substitution  satisfies  for the valuation  (notation: ) when  implies . A typing judgement is said to be valid (notation ) if for every valuation , and for every substitution  satisfying  for , we have .

Every derivable typing judgement is valid: for each , each term  and each type , we have that implies .

This immediately provides the strong normalisation result:

Theorem 2.2 (Strong normalisation)

Every typable term in  is strongly normalising.

Proof

If a term  is typable by a type , then the adequacy lemma ensures that . As a reducibility candidate,  is included in , and thus  is strongly normalising. ∎

3 Logical Interpretation

In this section, we interpret the  type system into System with pairs ( for short). Sum types are interpreted with Cartesian products. Since this product is neither associative nor commutative in , we first consider  without type equivalences. This involves a slightly modified but equivalent type system, that we call . We then translate every term of  into a term of . Finally, we show that our translation is correct with respect to typing in  (Theorem 3.2) and reduction (Theorem 3.3).

The system  is defined with the same grammar of types as , and the same rules , , , , and . There is no type equivalence, and thereby no commutativity nor associativity for sums (also is not neutral for sums). Hence rule , has to be precised. To specify what an -ary sum is, we introduce the structure of trees for types.

Example 3

In , the type is no longer equivalent to . We can represent the first one by the labelled tree on the right.

To formalise , we use the standard representation of binary trees, with some special leaves  (which can be labelled by a unit type):

Each leaf is denoted by the finite word on the alphabet (for left and right) representing the path from the root of the tree. For instance, the type is obtained using the labelling , with the tree of the left.

We say that a labelling function  (formally, a partial function from to unit types) labels a tree  when each of its leaves  is in the domain of . In this case, we write the type of  obtained by labelling  with . Notice that conversely, for any type , there exists a unique tree  and a labelling function  such that . The tree composition consists in “branching” to each leaf  of  (cf. Example 6 in Appendix B.1). By extending the definition of labelling functions to functions from leaves to types, we have , where  denotes a -leaf of , and  a -leaf of . Then the rule for the arrow elimination in  is:

 \prooftree‘G⊢t:T[w↦∀→X.(U→Tw)]‘G⊢u:T′[v↦U[→Vv/→X]]\justifies‘G⊢tu:T‘oT′[wv↦Tw[→Vv/→X]]\using→E′\endprooftree

where is a word whose prefix  represents a leaf of  (cf. Example 7).

is derivable in  if and only if there is a type  such that is derivable in .

Translation into the System F with Pairs.

We recall the syntax of  [Dicosmo95]:

 Terms:t,u:=x | ‘lx.t | tu | ⋆ | ⟨t,u⟩ | ‘pl(t) | ‘pr(t)Types:A,B:=X | A"=>"B | ‘AX.A | 1 | A‘∗B

(reduction and typing rules are well known, cf. Fig. 2 on Appendix B).

In the same way than for the types, we define a term of  with a tree (whose binary nodes  are seen as pairs) and a partial function  from to -terms. We write  for  (with ). Remark that if and is a -leaf of , then  is a subterm of  that can be obtained by reducing , where  is the mirror word of  (cf.  Example 4).

Example 4 (Representation of Fp-terms with trees)

Let . Then  (where  is the tree on the right) and  reduces from .

Every type  is interpreted by a type of .

 |X|=X,|¯¯¯0|=1,|‘AX.U|=‘AX.|U|,|U→T|=|U|"=>"|T|,|T+R|=|T|‘∗|R|.

Then any term  typable with a derivation  is interpreted by a -term :

If , then .

If , then .

If , then .

If , then .

If ,

then .

If , then .

If , then .

This interpretation is in fact a direct translation of sums by pairs at each step of the derivation, except for the application: informally, all the distributivity redexes are reduced before the translation of a term , which requires to ‘know’ the sum structure of  and . This structure is actually given by their type, and that is why we can only interpret typed terms.

Example 5

If  has type and  has type , then we see them as terms of shape and respectively (the reducibility model of section 2 ensures that they actually reduce to terms of this shape). Indeed, the translation of  reduces to the translation of :

,
where , , , and

Theorem 3.1 (Correction with respect to typing)

If a judgement  is derivable in  with derivation , then .

The technical details for its proof are given in Appendix B.2. In Appendix B.3 it is given a theorem showing that the translation is not trivial since it is reversible.

To return back to , observe that if , their translations are equivalent in  (in the sense that there exists two terms establishing an isomorphism between them), and conclude with Proposition 3.

Theorem 3.2

If a judgement  is derivable in , then there is a term  of  such that

To some extent, the translation from  to  is also correct with respect to reduction (technical details for its proof in Appendix B.4).

Theorem 3.3 (Correction with respect to reduction)

Let be derivable (by ) in , and . If the reduction is not due to rule , then there is ’ deriving , and

Notice that the associativity and commutativity of types have their analogous in the term equivalences. However, the equivalence has its analogous with a reduction rule, . Since  has no equivalences, this reduction rule is not correct in the translation. However, if  is derivable by  in , then there is some  such that , where and are the terms establishing the isomorphism between and in .

Conclusion

In this paper we considered an extension to call-by-value lambda calculus with a non-deterministic (or algebraic) operator , and we mimiced its behaviour at the level of types. As we discussed in the introduction, this operator behaves like the algebraic sum with linear functions: . However, our system is simulated by System  with pairs, which corresponds to the non linear fragment of imell.

This puts in the foreground the deep difference between the linearity in the algebraic sense (the one of Linear Logic), and the linearity of  (which is the same, for instance, as Lineal [ArrighiDowekRTA08]). In the first case, a function is linear if it does not duplicate its argument  (that is, –or – will not appear during the computation), whereas in  a linear behaviour is achieved by banning sum terms substitutions: while computing , the argument will never be duplicated even if  is not linear in . We can only duplicate values (that intuitively correspond to constants in the algebraic setting, so their duplication does not break linearity). Actually, in , the application is always distributed over the sum before performing the -reduction, and these both reductions do not interact. This is what our translation shows: all distributivity rules are simulated during the translation (of the application), and then the -reduction is simulated in System , without paying any attention to the linearity.

As mentioned in the introduction, Lineal was meant for quantum computing and forcing the left distributivity is useful to prevent cloning. Moreover, it makes perfectly sense to consider any function as linear in this setting, since every quantum operator is given by a matrix, and thereby is linear. A cbv reduction for this kind of calculus is thus entirely appropriate.

Acknowledgements.

We would like to thank Olivier Laurent for the useful advice he gave us about the interpretation we present in this paper, as well as Pablo Arrighi for the fruitful discussions about Lineal and its linearity.

Appendix A Formalisation of the Proof of Subject Reduction

The preservation of types by reduction, or subject reduction property, is proved by adapting the proof of Barendregt [Barendregt92, Section 4.2] for the Sytem : we first define a binary relation  on types, and then we give the usual generation and substitution lemmas. Finally, we give a needed property (Lemma 8) for the typing of and values.

Definition 2 (Relation ≼ on types)
• Given two types  and , we write if either

• or

• and for some type .

• We write the reflexive (with respect to ) transitive closure of .

The following property says that if two arrow types are related by , then they are equivalent up to substitutions.

Lemma 4 (Arrow comparison)

For any unit types , and types , , if , then there exist such that .

As a pruned version of a subtyping system, we can prove the subsumption rule:

Lemma 5 (≼-subsumption)

For any context , any term and any unit types , such that and no free type variable in  occurs in , if then .

Generation lemmas allows to study the conclusion of a derivation so as to understand where it may come from, thereby decomposing the term in its basic constituents.

Lemma 6 (Generation lemmas)

For any context , any terms ,, and any type ,

1. implies and for some integers , , some types , and some unit types  such that .

2. implies for some types , such that .

3. implies and with for some types ,  such that .

The following lemma is standard in proofs of subject reduction, and can be found for example in [Barendregt92, Prop. 4.1.19] and [Krivine90, Props. 8.2 and 8.5]. It ensures than by substituting type variables for types or term variables for terms in an adequate manner, the type derived is still valid.

Lemma 7 (Substitution)

For any , , , and ,

1. implies .

2. If , and , then .

Finally we need a property showing that is only typed by and its equivalent types, and values are always typed by unit types or equivalent.

Lemma 8 (Typing 0 and values)
1. For any , if  then .

2. For any value  (i.e. a variable or an abstraction), if then  is necessarily equivalent to a unit type.

Using all the previous lemmas, the proof of subject reduction is made by induction on typing derivation.

Appendix B Formalisation of the Translation into System F

b.1 Some Examples

Example 6 (Tree composition)

Let  = and  ’= .   Then =

Example 7 (Arrow elimination rule in Addstr)

The following derivation is correct:

 ‘G⊢t:(‘A→X.(U→T1)+‘A→X.(U→T2))+¯¯¯0‘G⊢u:U[→V/→X]+¯¯¯0‘G⊢tu:((T1[→V/→X]+¯¯¯0)+(T2[→V/→X