diff --git "a/B9FAT4oBgHgl3EQfsh6e/content/tmp_files/2301.08659v1.pdf.txt" "b/B9FAT4oBgHgl3EQfsh6e/content/tmp_files/2301.08659v1.pdf.txt" new file mode 100644--- /dev/null +++ "b/B9FAT4oBgHgl3EQfsh6e/content/tmp_files/2301.08659v1.pdf.txt" @@ -0,0 +1,2647 @@ +arXiv:2301.08659v1 [cs.LO] 20 Jan 2023 +System F µ +ω with Context-free Session Types⋆ +Diana Costa +, Andreia Mordido +, Diogo Po¸cas +, and Vasco T. Vasconcelos +LASIGE, Faculdade de Ciˆencias, Universidade de Lisboa, Portugal +{dfdcosta,afmordido,dmpocas,vmvasconcelos}@ciencias.ulisboa.pt +Abstract. We study increasingly expressive type systems, from F µ—an +extension of the polymorphic lambda calculus with equirecursive types— +to F µ; +ω —the higher-order polymorphic lambda calculus with equirecur- +sive types and context-free session types. Type equivalence is given by a +standard bisimulation defined over a novel labelled transition system for +types. Our system subsumes the contractive fragment of F µ +ω as studied +in the literature. Decidability results for type equivalence of the various +type languages are obtained from the translation of types into objects +of an appropriate computational model: finite-state automata, simple +grammars and deterministic pushdown automata. We show that type +equivalence is decidable for a significant fragment of the type language. +We further propose a message-passing, concurrent functional language +equipped with the expressive type language and show that it enjoys +preservation and absence of runtime errors for typable processes. +Keywords: System F, Higher-order kinds, Context-free session types +1 +Introduction +Extensions of the λ-calculus to include increasingly sophisticated type struc- +tures have been extensively studied and have led to systems whose importance +is widely recognized: System F [60], System F µ [30], System Fω [36], System +F µ +ω [16]. Ideally, we would like to combine a wishlist of type structures and get +a super-powerful system with vast expressiveness. However, the expressiveness +of types is naturally limited by the universe where they are supposed to live: +programming languages. Expressive type systems pose challenges to compilers +that other (less expressive) types do not even reveal; one such example is type +equivalence checking. +System F can be enriched with different type constructors for specifying +communication protocols. We analyse the impact of combinations of such con- +structors on the type equivalence problem. In order to do so, we extend System +F with session types [42,43,67]. Session types provide for detailed protocol spec- +ifications in the form of types. Traditional recursive session types are limited to +⋆ Support for this research was provided by the Funda¸c˜ao para a Ciˆencia e a Tecnologia +through project SafeSessions, ref. PTDC/CCI-COM/6453/2020, and by the LASIGE +Research Unit, ref. UIDB/00408/2020 and ref. UIDP/00408/2020. + +2 +D. Costa et al. +tail recursion, thus failing to capture all protocols whose traces cannot be charac- +terized by regular languages. Context-free session types overcome this limitation +by extending types with a notion of sequential composition, T ; U [3,68]. The set +of types together with the binary operation ; constitutes a monoid, for which a +new type, Skip, acts as the neutral element and End acts as an absorbing element. +The regular recursive type µ α: s. &{Done: End, More: ?Int; α} describes an +integer stream as seen from the point of view of the consumer. It offers a choice +between Done—after which the channel must be closed, as witnessed by type +End—and More—after which an integer value must be received, followed by the +rest of the stream. Types are categorised by kinds, so that we know that the +recursion variable α is of kind session—denoted by s—and, thus, can be used +with semicolon. Instead, we might want to write a type with a more context-free +flavour. The type µ α: s. &{Leaf : Skip, Node : α; ?Int; α};End describes a proto- +col for the type-safe streaming of integer trees on channels. The continuation +to the Leaf option is Skip, where no communication occurs but the channel is +still open for further composition. The continuation to the Node choice receives +a left subtree, an integer at the root and a right subtree. In either case, once +the whole tree is received, the channel must be closed, as witnessed by the final +End. Beyond first-order context-free session types (where only basic types are +exchanged) [3,68] we may be interested in higher-order session types capable of +exchanging values of complex types [21]. A goal of this paper is the integration +of higher-order context-free session types into system F µ +ω . We want to be able +to abstract the type that is received on a tree channel, which is now possible by +writing λα: t.µ β : s. &{Leaf : Skip, Node : β; ?α; β};End, where t is the kind of +functional types. +A form of abstraction over session types was formerly proposed by Das et +al. [25,26] via (nested) parametric polymorphism. In the notation of Das et al., +we can write a type equation Stream⟨α⟩ .= &{Done: End, More: ?α; Stream⟨α⟩} +that abstracts the type being received on a stream channel. To write the same +type using abstraction, we can think of Stream as a function of its parameter α, +Stream .= λα: t.&{Done: End, More: ?α; Stream α}; we can then rewrite Stream +using the µ-operator, Stream = λα: t.(µ β : s. &{Done: End, More: ?α.β}). Das +et al. proved that parametrized type definitions over regular session types are +strictly more expressive than context-free session types. To some extent, this +analogy guides our approach: if adding abstraction (via parametric polymor- +phism) to regular types leads to nested types, what exactly does it mean to add +abstraction (via a type-level λ-operator) to context-free types? Throughout this +paper we analyse several increments to System F µ that culminate in adding +λ-abstraction to context-free session types. +One of our focuses is necessarily the analysis of the type equivalence problem. +The uncertainty about the decidability of this problem over recursive parametric +types goes back to the 1970s [18,63]. Although the type equivalence problem for +parametric (nested) session types and context-free session types is decidable, +that for the combination of abstractions over context-free types may no longer +be. In fact, this analysis constitutes an interesting journey towards a better + +System F µ +ω with Context-free Session Types +3 +understanding of the role of higher-order polymorphic recursion in presence of +sequential composition, as well as the gains (and losses) resulting from combining +abstraction with arbitrary (rather than tail) recursion. +Ultimately, decidability is not a sufficiently valuable measure regarding a +type system’s practicality. We look for type systems that may be incorporated +into compilers. For that reason, we are interested in algorithms for type equiv- +alence checking. Equivalence in F µ +ω alone is already at least as expressive as +deterministic pushdown automata. If we restrict recursion to the monomorphic +case (requiring recursion variables to denote proper types, that is of kind s or t, +collectively denoted by ∗) we lower the complexity of type equivalence to that +of equivalence for finite-state automata. The extension with context-free session +types is slightly more complex. In order to obtain “good” algorithms, we restrict +the recursion to the monomorphic case, arriving at classes F µ∗ +ω , F µ∗; +ω +. Now the +type equality problem for F µ∗; +ω +translates to the equivalence problem for simple +grammars, which is still decidable [6,33]. Since F µ∗; +ω +subsumes F µ∗ +ω , our proof +of the decidability of type equivalence serves as an alternative to that of Cai et +al. [16] (restricted to contractive types). +Higher-order polymorphism allows for the definition of type operators and +the internalisation of various (session-type) constructs that would otherwise be +offered as built-in constructors. In this way, we are able to internalise basic +session-type constructors such as sequential composition ; and the Dual type op- +erator (which reverses the direction of communication between parties). Duality +is often treated as an external macro. Gay et al. [34] explore different ways of +handling the dual operator, all in a monomorphic setting. In the presence of +polymorphism the dual operator cannot be fully eliminated without introducing +co-variables. Internalisation offers a much cleaner solution. +Due to the presence of sequential composition, regular trees are not a power- +ful enough model for representing types (type TreeC a in Section 2 is an exam- +ple). The main technical challenge when combining System F µ +ω and context-free +session types is making sure that the resulting model can still be represented by +simple grammars, so that type equivalence may be decided by a practical algo- +rithm. The difficulties arise with renaming bound variables. For infinite types, +both renaming with fresh variables and using de Bruijn indices may create an infi- +nite number of distinct variables, which makes the construction of a simple gram- +mar simply impossible. For example, take the type λα: t.µ γ : t. λβ : t.α → γ, +which stands for the infinite type λα: t.λβ : t.α → λβ : t.α → λβ : t... Renam- +ing this type using a fresh variable at each step would result in a type of the form +λυ1 : t.λυ2 : t.υ1 → λυ3 : t.υ1 → λυ4 : t..., requiring infinitely many variables. +Similarly, de Bruijn indices [28] yield a type of the form λtλt1 → λt2 → λt3 → . . . +that requires an infinite number of natural indices. We thus introduce minimal +renaming that uses the least amount of variable names as possible (cf. Gauthier +and Pottier [30]). This ensures that only finitely many terminal symbols are +necessary, allowing for translating types into simple grammars. +Type languages live in term languages and we propose a term language to +consume F µ; +ω types. Based on Almeida et al. [2], we introduce a message-passing + +4 +D. Costa et al. +concurrent programming language. Type checking is decidable if type equivalence +is, and it is, in particular, for F µ∗; +ω +. +The main contributions of this paper are as follows. +– The integration of (higher-order) context-free session types into system F µ +ω , +dubbed F µ; +ω . +– A semantic definition of type equivalence via a labelled transition system. +– The identification of a suitable fragment of System F µ; +ω for which type equiv- +alence is reduced to the bisimilarity of simple grammars. +– A proof that type equivalence on the full System F µ; +ω is at least as hard as +bisimilarity of deterministic pushdown automata. +– The first internalisation of the Dual type operator in a type language. +– A term language to consume F µ; +ω types and an accompanying metatheory. +The type system presented in the paper combines three constructions: se- +quential composition of session types, higher-order kinds via type-level abstrac- +tion and application, and higher-order recursion. Prior to our work there is the +system by Almeida et al. [6] which incorporates sequential composition and (first- +order) recursion, but no higher-order kinds. There is also the system by Cai et +al. [16] which incorporates higher-order kinds and higher-order recursion, but +no sequential composition. Our system is the first to incorporates all three con- +structions. Although some of the results are incremental and generalize results +from the literature, the main technical challenge is understanding the border +past which they don’t hold anymore. For example, “just” including higher-order +kinds into the system by Almeida et al. does not work, since we need to pay +close attention to variable names, making sure that type equivalence is invariant +with respect to alpha-conversion (renaming of bound variables). This required +us to define a novel notion of renaming, inspired by Gauthier and Pottier [30]. +Similarly, “just” including sequential composition into the system of Cai et al. +does not work, since finite-state automata (or regular trees) are not enough to +capture the expressive power of the new type system, even when restricted to +first-order recursion. This required us to look at the more expressive framework +of simple grammars, and introduce a translation from types to words of a simple +grammar. +The rest of the paper is organised as follows. The next section motivates +the type language and introduces the term language with an example. Section 3 +introduces System F µ; +ω , Section 4 discusses type equivalence and Section 5 shows +that type equivalence is decidable for a fragment of the type language. Section 6 +presents the term language and its metatheory. Section 7 discusses related work +and Section 8 concludes the paper with pointers for future work. +2 +Motivation +Our goal is to study type systems that combine equirecursion, higher-order poly- +morphism, and higher-order context-free session types and their incorporation +in programming languages. + +System F µ +ω with Context-free Session Types +5 +�� ::= {} | ⟨⟩ +♯ ::= ? | ! +⊙ ::= & | ⊕ +T ::= T → T | �li : Ti� | ∀α: κ. T | µ α: κ. T | α +(F µ) +T ::= (F µ) | ♯T.T | ⊙{li : Ti} | End +(F µ·) +T ::= (F µ) | ♯T | ⊙{li : Ti} | End | T ; T | Skip +(F µ;) +T ::= (F M) | λα: κ.T | T T +(F M +ω ), +M ::= µ , µ· , µ; +Fig. 1: Six F-systems. +Extensions of F. Figure 1 motivates the construction by proposing six different +type syntaxes, culminating with F µ; +ω . The initial system, F µ, includes well-known +basic type operators [57]: functions T → U, records {li : Ti} and variants ⟨li : Ti⟩. +Type Unit is short for {}, the empty record; we can imagine that Unit stands in +place of an arbitrary scalar type such as Int and Bool. We also include variable +names α, type quantification ∀α: κ. T and recursion µ α: κ. T. In order to control +type formation, all variable bindings must be kinded with some kind κ, even if +for the initial system, F µ, we only use the functional kind t. +We then build on F µ by considering (regular, tail recursive) session types; +we represent the resulting system by F µ·. For example ?Int.!Bool.End is a type +for a channel endpoint that receives an integer, sends a boolean, and terminates. +At this point we introduce a kind s of session types to restrict the ways in +which we can combine session and functional types together. For example, a +well-formed type ?T.U is of kind s and requires U to be also of kind s (whereas +T can be of kind ∗, that is s or t). An example of an infinite session type is +µ α: s. !Int.α that endlessly outputs integer values. For a more elaborate example +consider the type IntStream = µ α: s. &{Done: End, More: ?Int.α} that specifies +a channel endpoint for receiving a (finite or infinite) stream of integer values. +Communication ends after choice Done is selected. +The next step of our construction takes us to context-free session types; the +resulting system is denoted by F µ;. We introduce a new construct for sequen- +tial composition T ; U, and a new type Skip, acting as the neutral element of +sequential composition [68]. The message constructors are now unary (?T and +!T) rather than binary. In System F µ; we distinguish between the traditional +End type and the Skip type. These types have different behaviours: End termi- +nates a channel, while Skip allows for further communication. Type equality is +more subtle for context-free session types, because of the monoidal semantics of +sequential composition. It is derivable from the following axioms: +Skip; T ∼ T +Neutral element +End; T ∼ End +Absorbing element +(T ; U); V ∼ T ; (U; V ) +Associativity +⊙{li : Ti}; U ∼ ⊙{li : Ti; U} +Distributivity +(1) + +6 +D. Costa et al. +F µ +F µ· +F µ; +F µ∗ +ω +F µ∗· +ω +F µ∗; +ω +F µ +ω +F µ· +ω +F µ; +ω +finite-state +automata +simple +grammars +≥ deterministic +pushdown automata +Fig. 2: Relation between the main classes of types in this paper (arrows denote +strict inclusions). +Although the syntax of F µ· is not formally included in the syntax of F µ;, +we can embed recursive session types into context-free session types by mapping +♯T.U into ♯T ; U. It is well-known that context-free session types allow for higher +computational expressivity: while F µ and F µ· can be represented via finite-state +automata, F µ; can only be represented with simple grammars [6,33]. +To finalise our construction, we include type abstraction λα: κ.T and type +application T U. Again, type abstraction binds a variable which must be kinded. +Kinds can now be of higher-order κ ⇒ κ′. For each of the three systems F µ, F µ·, +F µ; we arrive at a higher-order version, respectively F µ +ω , F µ· +ω , F µ; +ω (all of which +we represent as F M +ω ). In System F µ· +ω , for example, we can specify channels for +receiving (finite or infinite) sequences of values of arbitrary (but fixed) types, +Stream = λα: t.(µ β : s. &{Done: End, More: ?α.β}) +where α can be instantiated with the desired type; in particular, Stream Int would +be equivalent to the aforementioned IntStream. +It turns out that the expressive power of general higher-order systems F M +ω +is too large for practical purposes. Even the simplest case F µ +ω is at least as ex- +pressive as deterministic pushdown automata (or equivalently, first-order gram- +mars), for which known equivalence algorithms are notoriously impractical. By +impractical we mean that, although there exists a proof of decidability (due to +S´enizergues [61], later improved by Stirling and Jancar [46,65]), the underlying +algorithm is rather complex. To the best of our knowledge, there is no practical +implementation of an algorithm to decide the equivalence of deterministic push- +down automata. This is essentially due to polymorphic recursion, which can be +encoded by a higher-order µ-operator (we provide an example at the end of Sec- +tion 5). Therefore, it makes sense to restrict the kind κ of the recursion operator +µ α: κ. T. We use the notation µ∗ to mean the subclass of types written using +only ∗-kinded recursion, i.e., µ α: t. T or µ α: s. T. +Figure 2 summarizes the main relations between the classes of types in our +paper. Firstly, we obtain a lattice where the expressive power increases as we +travel down (from functional to session to context-free session types) and right +(from simple polymorphism to higher-order polymorphism with monomorphic + +System F µ +ω with Context-free Session Types +7 +recursion to arbitrary recursion). Four of the classes can be represented using +finite-state automata (up to F µ∗· +ω +). By including sequential composition (F µ; +and F µ∗; +ω +) we are still able to represent types using simple grammars. Once +we allow for arbitrary recursion, the expressiveness of our model requires the +computational power of deterministic pushdown automata. +Programming with F µ; +ω . We now turn our attention to the term language, a mes- +sage passing, concurrent functional language, equipped with context-free session +types. Start with a stream of values of type a. Such a stream, when seen from +the side of the reader, offers two choices: Done and More. In the former case the +interaction is over; in the latter the reader reads a value of type a, as in ?a, and +recurses. This is the stream type we have seen before only that, rather than clos- +ing the channel endpoint (with type End), it terminates with type Skip, so that +it may be sequentially composed with other types. In this informal introduction +to the term language we omit the kinds of type variables. +type +Stream a = &{ Done : Skip , More: ?a ; Stream a} +A fold channel, as seen from the side of the folder, is a type of the following +form. We assume that application binds tighter than semicolon, that is, type +Stream a ; !b ; End is interpreted as (Stream a) ; !b ; End. +type +Fold a b = ?(b → +a → b) ; ?b ; Stream a ; !b ; End +Consumers of this type first receive the folding function, then the starting ele- +ment, then the elements to fold in the form of a stream, and finally output the +result of the fold. The type terminates with End for we do not expect type Fold to +be further composed. Compare Fold with the type for a conventional functional +left fold: (b → a → b) → b → List a →b. +We now develop a function that consumes a Fold channel. Syntax x ⊲ f is for +the inverse function application with low priority, that is x ⊲ f ⊲ g = g (f x). +Recall that Unit is an alternative notation for the empty record type, {}. +foldServer +: ∀a.∀b. Fold a b → +Unit +foldServer +c = let (f, c) = receive c in +let (e, c) = receive c in +foldS f e c +foldS : ∀a.∀b. (b → +a → +b) → b → +Stream a;!b;End +→ +Unit +foldS f e c = match c with +{ Done c → c +⊲ +send e +⊲ +close +, More c → +let (x, c) = receive c in foldS f (f e x) c +} +Function foldServer consumes the initial part of the channel and passes the rest +of the channel to the recursive function foldS that consumes the whole stream +while accumulating the fold value. In the end, when branch Done is selected, +the fold value is written on the channel and the channel closed. In general, the +channel operators—receive, send, select—return the same channel in the form +of a new identifier. It is customary to reuse the identifier name—c in the example, +as in let (f, c)= receive c—since it denotes the same channel. Syntax c ⊲ ... + +8 +D. Costa et al. +hides the continuation channel. The case for the external choice—match—also +returns the continuation (in each branch) so that interaction on the channel +endpoint may proceed. +We may now write different clients for the foldServer. Examples include a +client that generates a stream from a pair of integer values (denoting an inter- +val); another that generates the stream from a list of values; and yet another +that generates the stream from a binary tree. We propose a further client. Con- +sider the type of a channel that exchanges trees in a serialized format [68]. Its +polymorphic version, as seen from the point of view of the reader, is as follows: +type +TreeChannel +a = TreeC a ; End +type +TreeC a = &{ Leaf : Skip , Node : TreeC a;?a;TreeC a} +We transform trees as we read from tree channels into streams. Function +flatten receives a tree channel and a stream channel (as seen from the point of +view of the writer, hence the Dual) and returns the unused part of the stream +channel. +flatten : ∀a.∀c. TreeChannel +a → (Dual +Stream a);c → c +We are now in a position to write a client that checks whether all values in +a tree channel are positive. +allPositive +: TreeChannel +Int +→ +Dual (Fold +Int Bool ) → +Bool +allPositive +t c = +let c = send (λx:Bool .λy:Int. x && y > 0) c in +let c = send +True c in +let c = flatten [Int] [? Bool ;End] t c in +let (x, c) = receive c in +close c; x +The client sends a function and the starting value on the fold channel. Then, +it flattens the given tree t, receives the folded value and closes the channel. +Syntax flatten [Int] [?Bool;End] is for term-level type application. We mean +to flatten a tree of Int values on a stream channel whose continuation is of type +?Bool;End. The continuation channel is bound to c so that we may further receive +the fold value and thereupon close the channel. Syntax e1;e2 is for sequential +composition and abbreviates (λx:Unit.let {} = x in e2)e1 given that {}, the +Unit value, is linear and hence must be consumed. +Finally, a simple application creates a new TreeC channel, passing one end to +a thread that produces a tree channel. It then creates a Fold channel, distributes +one end to a thread foldServer and the other to function allPositive. The fork +primitive receives a suspended computation (a thunk, of the form λx:Unit.e) +and creates a new thread that runs in parallel with that from where the fork +was issued. +system : Bool +system = let (tr , tw) = new TreeC Int in +fork (λ_:Unit . produce tw); +let (fr , fw) = new Fold +Int Bool +in +fork (λ_:Unit . foldServer +fr); + +System F µ +ω with Context-free Session Types +9 +∗ ::= +Kind of proper types +s +session +t +functional +κ ::= +Kind +∗ +kind of proper types +κ ⇒ κ +kind of type operators +T ::= +Type +ι +type constant +α +type variable +λα: κ.T +type-level abstraction +T T +type-level application +Fig. 3: The syntax of types. +ι ::= +Type constant +→ +∗ ⇒ ∗ ⇒ t +arrow +�li� +∗ ⇒ t +record, variant +µκ +(κ ⇒ κ) ⇒ κ +recursive type +∀κ +(κ ⇒ ∗) ⇒ t +universal type +Skip +s +skip +End +s +end +♯ +∗ ⇒ s +input, output +; +s ⇒ s ⇒ s +seq. composition +⊙{li} +s ⇒ s +choice operators +Dual +s ⇒ s +dual operator +Fig. 4: Type constants and kinds. +allPositive +tr fw +3 +Kinds and Types +This section introduces in detail System F µ; +ω , an extension of System F µ +ω incorpo- +rating higher-order context-free session types. The syntax of types is presented +in Fig. 3. A type is either a constant ι (as in Fig. 4), a type variable α, an +abstraction λα: κ.T or an application T U. Besides incorporating the standard +session type constructors as constants, system F µ; +ω also includes Dual as a con- +stant for a type operator mapping a session type to its dual. Note also that +∀α: κ. T is syntactic sugar for ∀κ(λα: κ.T ). Analogously, µ α: κ. T abbreviates +µκ(λα: κ.T). This simplifies our analysis as lambda abstraction becomes the +only binding operator. +A distinction between session and functional types is made resorting to kinds +s and t, respectively. These are the kinds of proper types, ∗; we use the symbol +κ to represent either the kind of a proper type or that of a type operator, of the +form κ ⇒ κ′. A kinding context ∆ stores kinds for type variables using bindings +of the form α: κ. Notation ∆ + α: κ denotes the update of kinding context ∆, +defined as (∆, α: κ) + α: κ′ = ∆, α: κ′ and ∆ + α: κ = ∆, α: κ when α ̸∈ ∆. +To define type formation, we require a few notions. Firstly comes the notion +of renaming, adapted from Gauthier and Pottier [30] and presented in Fig. 5. +Renaming essentially replaces a type T by a minimal alpha-conversion of T. By +alpha-conversion we mean that renameS(T ) renames bound variables in T . By +“minimal” we mean that each bound variable is renamed to its lowest possible +value. We assume at our disposal a countable well-ordered set of type variables +{υ1, . . . , υn, . . .}. In renameS(T), parameter S is a set containing type variables + +10 +D. Costa et al. +Type renaming +renameS(T) +renameS(ι) = ι +renameS(α) = α +renameS(λα: κ.T ) = λυ : κ.renameS(T [υ/α]) +where υ = firstS(λα: κ.T ) +renameS(T U) = renameS∪fv(U)(T ) renameS(U) +Fig. 5: Type renaming. +unavailable for renaming; in the outset of the renaming process S is the empty +set, since all variables are available. In that case the subscript S is often omitted. +The case for lambda abstraction renames the bound variable by the smallest +variable not in the set S ∪ fv(λα: κ.T), which we denote by firstS(λα: κ.T). +Renaming is what allows us to check whether type abstractions λα: κ.T, +λβ : κ.U are equivalent. The types are equivalent if both bound variables α and +β are renamed to the same variable υj. In summary, renaming provides a syntax- +guided approach to the equivalence of lambda-abstractions, where the names of +bound variables should not matter. Our notion of type equivalence preserves +alpha-conversions up to renaming: if T, U are alpha-conversions of one another, +then rename(T) = rename(U) and in particular rename(T) ∼ rename(U). We +will come back to this point after we define type equivalence in Section 4. +We can easily see that renaming uses the minimum amount of variable names +possible; for example, rename(λα: t.λβ : s.β) = λυ1 : t.λυ1 : s.υ1. Notice how +both bound variables α and β are renamed to υ1, the first variable available +for replacement. Also, renaming blatantly violates the Barendregt’s variable +convention [11] used in so many works; for example rename(υ1 (λα: T.α)) = +υ1 (λυ1 : T.υ1), where variable υ1 is both free and bound in the resulting type. +Even if renaming violates the variable convention, substitution can still be per- +formed without resorting to the “on-the-fly” renaming of Curry and Feys [22,40]. +When υ1 ̸= υ2, we have +(λυ1 : κ.λυ2 : κ′.U) T −→ (λυ2 : κ′.U)[T/υ1] = λυ2 : κ′.(U[T/υ1]) +since the renaming rule for application guarantees that υ2 /∈ fv(T). Otherwise +if υ1 = υ2, we have (λυ1 : κ′.U)[T/υ1] = λυ1 : κ′.U. This justifies the inclusion +of set S in the renaming process. From now on, we assume that all types have +gone through the renaming process. +Next comes the notion of type reduction (Fig. 6). Apart from beta reduc- +tion (rule R-β), the definition provides for sequential composition, for unfolding +recursive types and for reducing Dual T types. Note that renaming is further +invoked in rule R-β for beta reduction does not preserve renaming: consider the +renamed type (λυ1 : t.λυ2 : t.υ1 → υ2) Unit. The type resulting from the substi- +tution (λυ2 : t.υ1 → υ2)[Unit/υ1] is λυ2 : t.Unit → υ2 which is not renamed and, +therefore, not equivalent to λυ1 : t.Unit → υ1 according to our rules in Section 4. + +System F µ +ω with Context-free Session Types +11 +Type reduction +T −→ T +R-Seq1 +Skip; T −→ T +R-Seq2 +T −→ V +T ; U −→ V ; U +R-Assoc +(T ; U); V −→ T ; (U; V ) +R-µ +µk T −→ T (µk T ) +R-β +(λα: κ.T) U −→ rename(T [U/α]) +R-TAppL +T −→ U +T V −→ UV +R-D; +Dual (T ; U) −→ Dual T; Dual U +R-DSkip +Dual Skip −→ Skip +R-DEnd +Dual End −→ End +R-D? +Dual (? T ) −→ ! T +R-D! +Dual (! T) −→ ? T +R-D& +Dual (&{li : Ti}) −→ ⊕{li : Dual(Ti)} +R-D⊕ +Dual (⊕{li : Ti}) −→ &{li : Dual(Ti)} +R-DCtx +T −→ U +Dual T −→ Dual U +R-DDVar +Dual (Dual (α T 1 . . . T m)) −→ α T 1 . . . T m +Fig. 6: Type reduction. +Weak head normal form +T whnf +W-Const0 +ι whnf +W-Const1 +ι ̸= ; , µκ , Dual +m ≥ 1 +ι T 1 . . . T m whnf +W-Seq1 +; T whnf +W-Seq2 +T1 whnf +T1 ̸= U; V , Skip +m ≥ 2 +; T1 . . . Tm whnf +W-Var +m ≥ 0 +α T 1 . . . T m whnf +W-Abs +λα: κ.T whnf +W-Dual +T1 whnf +T1 ̸= Skip , End , ♯ U , U; V , ⊙{li : Ui} , Dual (α U1 . . . Un) +m ≥ 2 +Dual T 1 . . . T m whnf +Fig. 7: Weak head normal form. +Thanks to our modified rule R-β, we preserve preservation of renaming under +reductions: if T = rename(T) and T −→ U then U = rename(U). +We also need the notion of weak head normal form, T whnf, borrowed from the +lambda calculus [11,12]. Fig. 7 provides a rule-based characterisation (which can +be used in a compiler). Notice that in rules W-Seq1 and W-Seq2, the sequential +composition operator ; appears in prefix notation. However, for type formation +(kinding), it turns out that ; T1 . . . Tm is a (well formed) type only when m ≤ 2. + +12 +D. Costa et al. +The following result shows that the rule-based characterization is equivalent to +irreducibility. +Lemma 1. +T whnf iff T +̸−→. +Proof. (⇒) Assume that T −→ U for some U. We prove that it is not the case +that T whnf. +– Skip; T −→ T and by inspection of whnf rules, we conclude that Skip; T is +not a weak head normal form. +– T; U −→ V ; U only if T −→ V . However, by hypothesis, given that T re- +duces, T is not a weak head normal form. Therefore, none of the whnf rules +can be applied. So, T; U is not a weak head normal form. +– (T ; U); V −→ T ; (U; V ) and by inspection of whnf rules, we conclude that +(T ; U); V is not a weak head normal form. +– µκ T −→ T (µκ T) and by inspection of whnf rules, we conclude that µk T +is not a weak head normal form. +– (λα: κ.T) U −→ rename(T [U/α]) and by inspection of whnf rules, we con- +clude that (λα: κ.T) U is not a weak head normal form. +– T V −→ U V only if T −→ U. However, by hypothesis, given that T reduces, +T is not a weak head normal form. Therefore, by inspection of whnf rules, +we conclude that T V is not a weak head normal form. +– Cases of T for which Dual T −→ U for some U are automatically excluded +in rule W-Dual, therefore none of those Dual T types is a weak head normal +form. +(⇐) We must investigate all types T such that T +̸−→. We illustrate with a +couple of cases: +– No constant ι reduces, and according to W-Const0, they are all in weak +head normal form. Analogously for ; T, α T1, . . . , Tm(m ≥ 0) and λα: κ.T, +using rules W-Seq1, W-Var and W-Abs, respectively. +– The cases of T for which T; U does not reduce are, according to rule R-Seq2, +those where T does not reduce either. By induction, T whnf. Also, T ̸= +Skip, T1; T2, otherwise the type T; U would reduce via R-Seq1 or R-Assoc, +respectively. This case is covered in W-Seq2 and therefore the type is in weak +head normal form. +– The cases of T for which Dual(T ) does not reduce are all such that T ̸= Skip, +End, T1; T2, ♯T , ⊙{ti : Ti}, Dual(α T1 . . . Tm) and T does not reduce. By +induction, T whnf. Each of those cases for T is covered by W-Dual. +We say that type T normalises to type U, written T ⇓ U, if U whnf and U is +reached from T in a finite number of reduction steps (note that any term which +is already whnf normalises to itself). We write T norm to denote that T ⇓ U for +some U. + +System F µ +ω with Context-free Session Types +13 +Type formation +∆ ⊢ T : κ +K-Const +∆ ⊢ ι : κι +K-Var +α: κ ∈ ∆ +∆ ⊢ α : κ +K-TAbs +∆ + α: κ ⊢ T : κ′ +∆ ⊢ λα: κ.T : κ ⇒ κ′ +K-TApp +∆ ⊢ T : κ ⇒ κ′ +∆ ⊢ U : κ T U norm +∆ ⊢ T U : κ′ +Fig. 8: Type formation. +For example, suppose we want to normalise the type µs T, where T is the +type λυ1 : s.⊕{Done: End, More: !α}; Dual υ1. By computing all reductions from +µsT, we obtain the sequence +µsT −→ T (µsT) −→ ⊕{Done: End, More: !α}; Dual (µsT ) ̸−→ +Finally, we can show that ⊕{Done: End, More: !α}; Dual (µsT ) whnf: +⊕{Done: End, More: !α} whnf +W-Const1 +⊕{Done: End, More: !α}; Dual (µsT ) whnf +W-Seq2 +Hence, we conclude that µs T ⇓ ⊕{Done: End, More: !α}; Dual (µsT ). Similarly, +we can reason that µt (λυ1 : t.υ1), µs (λυ1 : s.Skip; υ1) and µs (λυ1 : s.Dual υ1) +are all examples of non-normalising expressions. +Equipped with normalisation, we can introduce type formation, which we +do via the rules in Fig. 8. Rule K-Const introduces constants as types whose +kinds match those of Fig. 4. Rule K-Var reads the kind of a type variable from +context ∆. An abstraction λα: κ.T is a well-formed type with kind κ ⇒ κ′ if T +is well formed in context ∆ updated with entry α: κ (rule K-TAbs). The update +is necessary since we are dealing with renamed types and the same type variable +may appear with different kinds in nested abstractions. +It is not until we reach rule K-TApp that we find a proviso about the normal- +isation of a type. This is standard and analogous to a condition on contractivity. +The goal is to eliminate types that reduce indefinitely without reaching a weak +head normal form. +Theorem 1. Let ∆ ⊢ T : κ. +Preservation. If T −→ U, then ∆ ⊢ U : κ. +Confluence. If T −→ U and T −→ V , then U −→∗ W and V −→∗ W. +Weak normalisation. T ⇓ U for some U. Furthermore, if T ⇓ V , then U = V . +Proof. Preservation: By rule induction on T −→ U. Let us inspect some cases +of (well-formed) types T that reduce as an illustration: +– Skip; U −→ U; from the assumption that ∆ ⊢ T : κ follows that κ = s and +that ∆ ⊢ U : s. + +14 +D. Costa et al. +– U1; U2 −→ V ; U2 only if U1 −→ V ; from the assumption that ∆ ⊢ T : κ +follows that κ = s, ∆ ⊢ U 1 : s and ∆ ⊢ U 2 : s. By hypothesis, ∆ ⊢ V : s. +Therefore, ∆ ⊢ V ; U2 : s. +– µkU −→ U (µkU); from the assumption that ∆ ⊢ T : κ and since ∆ ⊢ +µκ : (κ ⇒ κ) ⇒ κ, follows that ∆ ⊢ U : κ ⇒ κ. Therefore, ∆ ⊢ U (µκU) : κ. +Confluence: By a case analysis on the various possible reductions from T. +We sketch a simple case. (Skip; T); U −→ T ; U (via R-Seq2) and (Skip; T); U −→ +Skip; (T ; U) (via R-Assoc). Nonetheless, the latter reduces again via R-Seq1: +Skip; (T ; U) −→ T ; U. +Weak normalisation: By a case analysis on T. If T = ι, α or λα: κ.U, +then T whnf by rules W-Const0, W-Var, W-Abs, respectively. Therefore T ⇓ T +according to N-Whnf. If T = U V , then according to K-TApp UV norm, so T ⇓ T ′ +for some T ′ by definition. The second part follows from confluence. +We finally arrive at the main decidability result in this section. In its proof, +we make use of the fact that recursion is restricted to kind ∗ to limit the possible +subexpressions of the form µ∗ U that might appear in the normalisation of T. +Theorem 2 (Decidability of type formation). +∆ ⊢ T : κ is decidable for +types in F µ∗; +ω +. +Proof. In Appendix A. +4 +Type equivalence +This section introduces type bisimulation as our notion of type equivalence. We +define a labelled transition system (LTS) on the space of all types and write +T +a +−→ U to denote that T has a transition by label a to U. The grammar for +labels and the LTS rules are in Fig. 9. +If T is not in weak head normal form, then we must normalise it to some +type U, so that T has the same transitions as U (rule L-Red). Otherwise if +T whnf, then the transitions of T can be immediately derived by looking at the +corresponding rule for T as follows. If T is a variable, use rule L-Var1 (with +m = 0). If T is a constant (other than Skip), use rule L-Const. Note that if T is +a lone Skip, it has no transitions. If T is an abstraction, use rule L-Abs. +If T is an application, then we need to look inside the head. We write T as +T0 T1 . . . Tm with m ≥ 1 where T0 is not an application, and look at T0. If T0 is a +variable, use rules L-Var1 and L-Var2. If T0 is one of the constants →, ∀κ, ⊙{li} +or �li�, use rule L-ConstApp. Note that if T0 is an abstraction or µκ, then T is +not in weak head normal form and it must be normalised. If T0 is ♯, we use rules +L-Msg1 and L-Msg2. If T0 is Dual, then the only way for T to be well-formed +and in weak head normal form is if m = 1 and T1 is α or α U1 . . . Um, in which +case we use rules L-DualVar1 and L-DualVar2. +If T0 is ; , we require an additional case analysis on T1. If m = 1, use rule +L-Seq1. Otherwise m = 2 due to kinding. If T1 is a variable, use rule L-VarSeq1 + +System F µ +ω with Context-free Session Types +15 +a ::= αi | ιi | λα: κ +(i ≥ 0, ι ̸= Skip) +Transition labels +Labelled transition system +T +a +−→ U +L-Red +T −→ U +U +a +−→ V +T +a +−→ V +L-Var1 +m ≥ 0 +α T1 . . . Tm +α0 +−→ Skip +L-Var2 +1 ≤ j ≤ m +α T1 . . . Tm +αj +−→ Tj +L-Const +ι ̸= Skip +ι +ι0 +−→ Skip +L-ConstApp +ι = →, ∀κ, ⊙{ℓi}, �ℓi� +1 ≤ j ≤ m +ι T1 . . . Tm +ιj +−→ Tj +L-Abs +λα: κ.T +λα: κ +−→ T +L-Msg1 +♯T +♯1 +−→ T +L-Msg2 +♯T +♯2 +−→ Skip +L-Seq1 +; T +;1 +−→ T +L-VarSeq1 +m ≥ 0 +(α T1 . . . Tm); U +α0 +−→ U +L-VarSeq2 +1 ≤ j ≤ m +(α T1 . . . Tm); U +αj +−→ Tj +L-MsgSeq1 +♯T ; U +♯1 +−→ T +L-MsgSeq2 +♯T ; U +♯2 +−→ U +L-ChoiceSeq +1 ≤ j ≤ m +⊙{ℓi : Ti}; U +⊙{ℓi}j +−→ Tj; U +L-EndSeq +End; U +End +−→ Skip +L-DualVar1 +Dual (α T1 . . . Tm) +Dual1 +−→ α T1 . . . Tm +L-DualVar2 +Dual (α T1 . . . Tm) +Dual2 +−→ Skip +L-DualSeq1 +(Dual (α T1 . . . Tm)); U +Dual1 +−→ α T1 . . . Tm +L-DualSeq2 +(Dual (α T1 . . . Tm)); U +Dual2 +−→ U +Fig. 9: Labelled transition system for types. +(with m = 0). If T1 is a constant, then it must be of kind s, so it must be either +Skip or End. If T1 is Skip, then T is not in weak normal form and it must be +normalised. If T1 is End, use rule L-EndSeq (End is an absorbing element, so +End; U simply makes a transition to Skip without executing U). Note that T1 +cannot be an abstraction due to kinding. +If T1 is an application, then again we write T1 as U0 U1 . . . Un with n ≥ 1 +where the head U0 is not an application, and look at U0. If U0 is a variable, use +rules L-VarSeq1 and L-VarSeq2. If U0 is a constant, it must be one of ; , µκ, ♯, +⊙{li} or Dual due to kinding. If U0 is ♯, use rules L-MsgSeq1 and L-MsgSeq2. +If U0 is ⊙{li}, use rule L-ChoiceSeq. If U0 is Dual, the only way for T to be +well-formed and in weak head normal form is if n = 1 and U1 is α or α V1 . . . Vℓ, +in which case we use rules L-DualSeq1 and L-DualSeq2. Finally if U0 is ; , µκ +or an abstraction, T is not in weak normal form and it must be normalised. +Let us clarify our LTS rules with an example. Consider the following type +λυ1 : t.µ υ2 : s.⊕{Done: End, More: !υ1}; Dual υ2 and call it T. Clearly T is of + +16 +D. Costa et al. +λυ1 : t.U +U +⊕{Done: End, More: !υ1}; Dual U +End; Dual U +Skip +!υ1; Dual U +υ1 +Skip +Dual U +&{Done: End, More: ?υ1}; Dual (Dual U) +End; Dual (Dual U) +Skip +?υ1; Dual (Dual U) +υ1 +Skip +Dual (Dual U) +λυ1 : t +⊕{Done, More}1 +End +⊕{Done, More}2 +!1 +υ1 +!2 +&{Done, More}1 +End +&{Done, More}2 +?1 +υ1 +?2 +Fig. 10: +The +LTS +for +type +λυ1 : t.U. +Normalisation +T1 +⇓ +T2 +is +represented +as +T1 +⇒ +T2 +and +U +is +a +shorthand +for +type +µs (λυ2 : s.⊕{Done: End, More: !υ1}; Dual υ2). +kind t ⇒ s. For a given functional type υ1, this type specifies a channel that +alternates between: offer a choice and output a value of type υ1; or select a choice +and input a value of type υ1. The polarity is swapped thanks to the application +of constant Dual to the recursion variable υ2. To construct the (fragment of the) +LTS generated by this type, let us first desugar T into λυ1 : t.U where U is the +type µs (λυ2 : s.⊕{Done: End, More: !υ1}; Dual υ2). Notice that U normalises to +⊕{Done: End, More: !υ1}; Dual U. The LTS for the example is sketched in Fig. 10. +In this case, only finitely many types appear. However, more elaborate examples +involving sequential composition or higher-order recursion may lead to an infinite +graph of transitions. +Given the LTS rules, we can define, in the standard way, a notion of bisimula- +tion. A binary relation R on types is called a bisimulation if, for every (T, U) ∈ R +and every transition label a: +1. if T +a +−→ T ′, then there exists U ′ s.t. U +a +−→ U ′ and (T ′, U ′) ∈ R; +2. if U +a +−→ U ′, then there exists T ′ s.t. T +a +−→ T ′ and (T ′, U ′) ∈ R. +We say that types T and U are bisimilar, written T ∼ U, if there exists a +bisimulation R such that (T, U) ∈ R. +Intuitively, a notion of type equivalence must preserve and reflect the syn- +tax of type constructors: for example, a type T → U is equivalent to a type +T ′ → U ′ iff T , T ′ are equivalent and U, U ′ are equivalent. Using the bisim- +ulation technique, we achieve this by considering a labelled transition system +on types: T → U has a transition labelled →1 to T and a transition labelled +→2 to U. In this way, T → U can only be equivalent to another type which +has two transitions with those same labels. For each of the type constructors +( →, ∀κ, !, ?, ⊙{ℓi}, and so on) we have suitable transition rules. Moreover, a +type sometimes needs to be reduced before a type constructor is found at the + +System F µ +ω with Context-free Session Types +17 +root of the syntax tree. If T normalizes to U, then we expect T and U to be +bisimilar, which is achieved thanks to rule L-Red. This handles the various re- +ductions: beta-reductions arising from lambda-abstraction and applications (e.g., +(λα: κ.T) U reduces to [U/α]T), reductions arising from the monoidal structure +of sequential composition (e.g., Skip; T reduces to T), reductions arising from +the internalization of duality as a type constructor (e.g., Dual (!T ) reduces to +?T) and reductions arising from the recursion (e.g., µκ T reduces to T (µκ T)). +Let us look at some examples of bisimilarity. +– Consider the types T = α (!Int) and U = α (!Int); Skip. These are bisimilar, +since they both exhibit a transition with label α1 to !Int (rules L-Var2 and +L-VarSeq2 resp.) and another transition with label α0 to Skip (rules L-Var1 +and L-VarSeq1 resp.). In general, two types α T and α U; V are bisimilar iff +T ∼ U and Skip ∼ V . This justifies our choice for the rules with variables. +– Similar considerations apply to ♯T vs ♯T; Skip and to Dual (α T1 . . . Tm) vs +Dual (α T1 . . . Tm); Skip. +– For internal and external choices, the situation is slightly different. Types +⊙{Go: T ; V , Quit: U; V } and ⊙{Go: T, Quit: U}; V are bisimilar. The for- +mer exhibits transitions ⊙{Go, Quit}1 and ⊙{Go, Quit}2 to T ; V and U; V +resp., due to rule L-ConstApp. The latter exhibits the same transitions, but +due to rule L-ChoiceSeq instead. This is as desired (distributivity of sequen- +tial composition against choice) and justifies our choice for those rules. +– End is bisimilar to any type End; U, due to our choice of rules L-Const and +L-EndSeq. +– Skip is the only well-formed type in weak head normal form that has no +transitions. Hence, if ∆ ⊢ T : κ, we have T ∼ Skip iff T ⇓ Skip. +– Consider the types λα: κ.T and λβ : κ.U. Before generating the LTS, we +must rename them to λυi : κ.[υi/α]T and λυj : κ.[υj/β]U. According to rule +L-Abs, the renamed types are bisimilar only if υi = υj, in which case we check +the equivalence of [υi/α]T and [υj/β]U. Here, the choice of υi depends only +on T (not on U). This allows us to define an LTS for T (independently of U), +and an LTS for U (independently of T), in a way that the types are equivalent +iff the corresponding LTS are bisimilar. Without using renaming, we would +have to somehow “remember” that variables α, β should be “linked”, which +would make the generation of independent LTS impossible. This is the main +technical issue solved by our notion of renaming. +Our notion of type equivalence enjoys natural properties and behaves as +expected with respect to the notions of reduction, normalisation and kinding +from Section 3. We can derive rules for type equivalence, that could be used +to define another coinductive notion of equivalence, via effective syntax-directed +rules. In other words, one could prove that ∼ is the greatest relation satisfying +properties 1-15 below. +Lemma 2. +1. T ∼ α iff T ⇓ α. + +18 +D. Costa et al. +2. T ∼ λα: κ.U iff T ⇓ λα: κ.V for some V s.t. U ∼ V . +3. T ∼ End iff +– T ⇓ End; or +– T ⇓ End; U for some U. +4. Let ι be a constant other than End. Then T ∼ ι iff T ⇓ ι. +5. T ∼ α U1 . . . Um iff: +– T ⇓ α U ′ +1 . . . U ′ +m for some U ′ +1, . . . , U ′ +m s.t. Uj ∼ U ′ +j for each j; or +– T ⇓ (α U ′ +1 . . . U ′ +m); W for some U ′ +1, . . . , U ′ +m, V s.t. Uj ∼ U ′ +j and Skip ∼ V . +6. T ∼ U → V iff T ⇓ U ′ → V ′ for some U ′, V ′ s.t. U ∼ U ′ and V ∼ V ′. +7. T ∼ ∀κU iff T ⇓ ∀κU ′ for some U ′ s.t. U ∼ U ′. +8. T ∼ �ℓ: Uℓ�ℓ∈L iff T ⇓ �ℓ: U ′ +ℓ�ℓ∈L for some U ′ +ℓ s.t. Uℓ ∼ U ′ +ℓ for each ℓ. +9. T ∼ ♯U iff: +– T ⇓ ♯U ′ for some U ′ s.t. U ∼ U ′; or +– T ⇓ ♯U ′; V for some U ′, V s.t. U ∼ U ′ and Skip ∼ V . +10. T ∼ ⊙{ℓ: Uℓ}ℓ∈L iff: +– T ⇓ ⊙{ℓ: U ′ +ℓ}ℓ∈L for some U ′ +ℓ s.t. Uℓ ∼ U ′ +ℓ for each ℓ; or +– T ⇓ ⊙{ℓ: U ′ +ℓ}ℓ∈L; V for some U ′ +ℓ, V s.t. Uℓ ∼ U ′ +ℓ; V for each ℓ. +11. T ∼ End; U iff +– T ⇓ End; or +– T ⇓ End; U′ for some U ′. +12. T ∼ ♯U; V iff: +– T ⇓ ♯U ′ for some U ′ s.t. U ∼ U ′ and V ∼ Skip; or +– T ⇓ ♯U ′; V ′ for some U ′, V ′ s.t. U ∼ U ′ and V ∼ V ′. +13. T ∼ ⊙{ℓ: Uℓ}ℓ∈L; V iff: +– T ⇓ ⊙{ℓ: U ′ +ℓ}ℓ∈L for some U ′ +ℓ s.t. Uℓ; V ∼ U ′ +ℓ for each ℓ; or +– T ⇓ ⊙{ℓ: U ′ +ℓ}ℓ∈L; V ′ for some U ′, V ′ s.t. Uℓ; V ∼ U ′ +ℓ; V for each ℓ. +14. T ∼ Dual (α U1 . . . Um) iff +– T ⇓ Dual (α U ′ +1 . . . U ′ +m) for some U ′ +j s.t. Uj ∼ U ′ +j for each j; or +– T ⇓ Dual (α U ′ +1 . . . U ′ +m); V for some U ′ +j, V s.t. Uj ∼ U ′ +j for each j and +Skip ∼ V . +15. T ∼ Dual (α U1 . . . Um); V iff +– T ⇓ Dual (α U ′ +1 . . . U ′ +m) for some U ′ +j s.t. Uj ∼ U ′ +j for each j and V ∼ Skip; +or +– T ⇓ Dual (α U ′ +1 . . . U ′ +m); V ′ for some U ′ +j, V ′ s.t. Uj ∼ U ′ +j for each j and +V ∼ V ′. +Next, we show that type equivalence is preserved under renaming, reduction +and normalization. +Lemma 3. +1. If T ∼ U, then rename(T ) ∼ rename(U). +2. If T −→ U, then T ∼ U. If T ⇓ U, then T ∼ U. +3. Suppose that T ⇓ T ′ and U ⇓ U ′. Then T ∼ U iff T ′ ∼ U ′. + +System F µ +ω with Context-free Session Types +19 +Proof. Item 1 follows by coinduction on T ∼ U, i.e., we prove that the set +{(rename(T), rename(U)) : T ∼ U} is a bisimulation. The only interesting case +is that in which T = λα: κ.T ′ and U = λβ : κ′.U ′. Since T ∼ U, we get +that α = β, κ = κ′ and T ′ ∼ U ′. It is straightforward to see that T ∼ U +implies fv(T) = fv(U) since free variables are eventually ‘seen’ in the pro- +cess of consuming T, U. Therefore, first(T ) = first(U) = υk for some k, and +rename(T ) = λυk : κ.rename(T ′[υk/α]), rename(U) = λυk : κ.rename(U ′[υk/α]). +Finally, we apply L-Abs to the pair (rename(T), rename(U)), arriving at the pair +(rename(T ′[υk/α]), rename(U ′[υk/α])). This lies in our set since T ′ ∼ U ′ implies +T ′[υk/α] ∼ U ′[υk/α]. +The first part of Item 2 is straightforward: if T −→ U, then by L-Red every +transition T +a +−→ V is matched by a transition U +a +−→ V and vice-versa. To prove +the second part of Item 2, assume that T ⇓ U and consider a finite sequence of +reductions T = T0 −→ T1 −→ . . . −→ Tn = U. At each step of the sequence +we have Tk ∼ Tk+1 and thus (since +∼ +is an equivalence relation) we get +T = T0 ∼ Tn = U. +Item 3 is a direct consequence of Item 2. T ⇓ T ′ and U ⇓ U ′imply T ∼ T ′ +and U ∼ U ′. Since ∼ is an equivalence relation, we get T ∼ U iff T ′ ∼ U ′. +Finally, we show that the axioms for sequential composition in the introduc- +tion (1) are derivable from our notion of bisimulation. +Lemma 4. +1. Skip; T ∼ T (Neutral element). +2. End; T ∼ End (Absorbing element). +3. (T ; U); V ∼ T ; (U; V ) (Associativity). +4. ⊙{li : Ti}; U ∼ ⊙{li : Ti; U} (Distributivity). +Proof. Item 1 follows from the observation that Skip; T −→ T (R-Seq1), to- +gether with Item 2 of Lemma 3. Item 2, follows from the observation that +End; T +End +−→ Skip (L-EndSeq) and End +End +−→ Skip (L-Const). Item 3 follows from +the observation that (T ; U); V −→ T ; (U; V ) (R-Assoc), together with Item 2 +of Lemma 3. Item 4 follows from the observation that ⊙{li : Ti}; U +⊙{li}j +−→ Tj; U +(L-ChoiceSeq) and ⊙{li : Ti; U} +⊙{li}j +−→ Tj; U (L-ConstApp). +5 +Decidability of type equivalence +This section presents results on decidability of type equivalence. Our approach +consists in translating types to objects in some computational model. We look +at finite-state automata (for types in F µ, F µ∗ +ω , F µ·, and F µ∗· +ω +), simple grammars +(for types in F µ; and F µ∗; +ω +) and deterministic pushdown automata (for types in +F µ +ω , F µ· +ω and F µ; +ω ). +We say that a grammar in Greibach normal form is a tuple (T , N, γ, R) +where: T is a set of terminal symbols, denoted by a, b, c; N is a set of nonterminal + +20 +D. Costa et al. +symbols, denoted by X, Y , Z; γ ∈ N ∗ is the starting word; and R ⊆ N ×T ×N ∗ +is a set of productions. A grammar is said to be simple if, for every nonterminal +X and every terminal a, there is at most one production (X, a, δ) ∈ R [51]. +Greek letters γ and δ denote (possibly empty) words of nonterminal symbols. +Productions are written as X +a +−→ δ. We define a notion of bisimulation for +grammars via a labelled transition system. The system comprises a set of states +N ∗ corresponding to words of nonterminal symbols. For each production X +a +−→ +γ and each word of nonterminal symbols δ, we have a labelled transition Xδ +a +−→ +γδ. We let ≈ denote the bisimulation relation for grammars (the definition is +similar to that in Section 4). +For the moment we focus on the class F µ∗; +ω +and we explain how to con- +vert a type T into a simple grammar (TT , NT , word(T), RT ). The conversion is +based on a function word(T) that maps each type T into a word of nonterminal +symbols, while introducing fresh nonterminals and productions. In our construc- +tion, following the approach by Costa et al. [21], we use a nonterminal symbol +with no productions, denoted by ⊥, in order to separate the two descendants +of a send/receive operation such as !T; U. The sequence of nonterminal symbols +word(T) is defined as follows. First consider the cases in which T whnf. +– For any m ≥ 0: word(α T1. . .Tm) = Y for Y a fresh nonterminal symbol +with a production Y +α0 +−→ ε as well as Y +αj +−→ word(Tj)⊥ for each 1 ≤ j ≤ m. +– word(Skip) = ε. +– word(End) = Y for Y a fresh symbol with a single production Y +End +−→ ⊥. +– for any ι ̸= Skip, End: word(ι) = Y for Y a fresh nonterminal symbol with a +single production Y +ι +−→ ε. +– word(λα: κ.T) = Y for Y a fresh symbol with a production Y +λα: κ +−→ word(T). +– for any m ≥ 1 and for ι one of →, ∀κ, ⊙{li}, �li�: word(ι T1 · · · Tm) = Y for a +fresh nonterminal Y with a production Y +ιj +−→ word(Tj) for each 1 ≤ j ≤ m. +– word(♯T ) = Y for Y a fresh symbol with productions Y +♯1 +−→ word(T)⊥ and +Y +♯2 +−→ ε. +– word(; T) = Y for Y a fresh symbol with a production Y +;1 +−→ word(T). +– word(T; U) = word(T) word(U). +– word(Dual (α T1. . .Tm)) = Y for Y a fresh symbol with productions Y +Dual1 +−→ +word(α T1. . .Tm) and Y +Dual2 +−→ ε. +Finally, let us handle the cases where T is not in weak head normal form. +– If T ⇓ Skip, then word(T ) = ε. +– Otherwise if T ⇓ U ̸= Skip, then word(T ) = Y for Y a fresh nonterminal +symbol. Let Zδ = word(U). Then Y has a production Y +a +−→ γδ for each +production Z +a +−→ γ. +In the above construction, we create fresh symbols each time we encounter a +weak head normal form other than Skip. In other words, NT is the set contain- +ing ⊥ and all nonterminals Y created during the computation of word(T). An- +other key insight is that the sequential composition of types is translated into a + +System F µ +ω with Context-free Session Types +21 +concatenation of words: word(T1;T2; . . . ;Tn) = word(T1) word(T2) . . . word(Tn). +This allows our construction to terminate: even if the transitions lead to in- +finitely many types, they are split on the sequential composition operator, and +so we only need to consider finitely many subexpressions. +For the last case in our construction to be well-defined, i.e., when T ⇓ U ̸= +Skip, we require word(U) to be non-empty. Indeed, if Uwhnf, then we can observe +(by inspecting all cases) that word(U) = ε iff U = Skip. +We also need to argue that the construction of word(T) eventually terminates. +For this, we keep track of all types visited during the construction, and we only +add a fresh nonterminal Y to our grammar if the type visited is syntactically +different from all types visited so far. Therefore, we reuse the same symbol Y with +the same productions each time we revisit a type. With all these observations, +we get the following result. +Lemma 5. Suppose that T ∈ F µ∗; +ω +. Then the construction of word(T) termi- +nates producing a simple grammar. +We illustrate the above construction with the polymorphic tree exchanging +example from Section 2, +type +TreeC a = &{ Leaf : Skip , Node : TreeC a; ?a ; TreeC a} +that is written in F µ∗; +ω +as T0 = λυ1 : t.µ υ2 : s. &{Leaf : Skip, Node: υ2; ?υ1; υ2}. +Since T0 is in weak head normal form, word(T0) returns a fresh symbol, which +we call X0. We also have a production X0 +λυ1 : κ +−→ +word(T1), where T1 is the +type µ υ2 : s. &{Leaf : Skip, Node: υ2; ?υ1; υ2}. Since T1 is not in whnf, we must +normalise it, to get T2 = &{Leaf : Skip, Node: T1; ?υ1; T1}. Therefore word(T1) +returns a fresh symbol, which we call X1. To obtain the transitions of X1, we +must first compute word(T2), which is a fresh symbol X2 with transitions X2 +&1 +−→ +word(Skip) and X2 +&2 +−→ word(T1; ?υ1; T1). Thus we also get X1 +&1 +−→ word(Skip) +and X1 +&2 +−→ word(T1; ?υ1; T1). +We have word(Skip) = ε, but we still need to compute word(T1; ?υ1; T1). +This type normalises to T3 = T2; ?υ1; T1 since T1 ⇓ T2. Thus word(T1; ?υ1; T1) +is a fresh symbol X3. To obtain the productions of X3 we must compute +word(T2; ?υ1; T1) = word(T2) word(?υ1) word(T1). At this point we already have +word(T1) = X1 and word(T2) = X2. +We still need to compute word(?υ1), which is a fresh symbol X4 with produc- +tions X4 +?1 +−→ word(υ1)⊥ and X4 +?2 +−→ ε. In turn, word(υ1) is a fresh symbol X5 +with a production X5 +υ1 +−→ ε. Finally, we get word(T2; ?υ1; T1) = X2X4X1, which +means we can write the productions for X3: X3 +&1 +−→ X4X1 and X3 +&2 +−→ X3X4X1. +Putting all this together, we can finally obtain the simple grammar: +X0 +λv1 : t +−→ X1 +X1 +&1 +−→ ε +X1 +&2 +−→ X3 +X2 +&1 +−→ ε +X2 +&2 +−→ X3 +X3 +&1 +−→ X4X1 +X3 +&2 +−→ X3X4X1 +X4 +?1 +−→ X5⊥ +X4 +?2 +−→ ε +X5 +v1 +−→ ε + +22 +D. Costa et al. +Next, we argue that type equivalence (i.e., bisimilarity on types) corresponds +to bisimilarity on the corresponding grammars. This is achieved by the following +lemma, that asserts that the LTS of a type and the LTS of the corresponding +word of nonterminals have exactly the same transitions. +Lemma 6 (Full abstraction). +Let T ∈ F µ∗; +ω +and (TT , NT , word(T), RT ) the +corresponding simple grammar. Suppose also that word(T) ≈ γ. +1. If T +a +−→ U then there exists γ′ such that γ +a +−→ γ′ and word(U) ≈ γ′. +2. If γ +a +−→ γ′ then there exists U such that T +a +−→ U and word(U) ≈ γ′. +Proof. The proof is by coinduction, showing that the relation +R = {(T, γ): word(T) ≈ γ} +is backward closed for the transition relation. This is done by a case analysis on +T. First, assume that T whnf. We show a few cases. +– If T is α T1. . .Tm, then by rules L-Var1 and L-Var2 the LTS at T has transi- +tions T +α0 +−→ Skip and T +αj +−→ Tj for 1 ≤ j ≤ m. Similarly, word(T) = Y for Y +with productions Y +α0 +−→ ε as well as Y +αj +−→ word(Tj)⊥ for 1 ≤ j ≤ m. Since +Y ≈ γ, the LTS at γ has transitions γ +α0 +−→ γ0 and γ +αj +−→ γj for some γ0 s.t. +ε ≈ γ0 and γj s.t. word(Tj)⊥ ≈ γj for 1 ≤ j ≤ m. Since word(Skip) = ε, we +get that (Skip, γ0) ∈ R. Finally, since word(Tj) ≈ word(Tj)⊥, we get that +(Tj, γj) ∈ R for 1 ≤ j ≤ m. +– If T is Skip, then the LTS at T has no transitions. Similarly, word(T ) = ε +has no transitions. Since ε ≈ γ, the LTS at γ has no transitions either. +– If T is U; V , then by rule W-Seq2 U whnf and U is neither Skip nor a +sequential composition. We must perform a second case analysis on U. For +example if U is α U1. . .Um, then by rules L-VarSeq1 and L-VarSeq2 the +LTS at T has transitions T +α0 +−→ V and T +αj +−→ Tj for 1 ≤ j ≤ m. Similarly, +word(T) = word(U)word(V ) = Y word(V ) for Y with productions Y +α0 +−→ ε +as well as Y +αj +−→ word(Uj)⊥ for 1 ≤ j ≤ m. Since Y word(V ) ≈ γ, the LTS +at γ has transitions γ +α0 +−→ γ0 and γ +αj +−→ γj for some γ0 s.t. word(V ) ≈ γ0 +and γj s.t. word(Uj)⊥word(V ) ≈ γj for 1 ≤ j ≤ m. We immediately get +that (V , γ0) ∈ R. Finally, since word(Uj) ≈ word(Uj)⊥word(V ), we get that +(Uj, γj) ∈ R for 1 ≤ j ≤ m. +Next, assume that T is not in whnf. There are two possibilities. +– If T ⇓ Skip, then by rule L-Red the LTS at T has no transitions. Similarly, +word(T) = ε has no transitions. Since ε ≈ γ, the LTS at γ has no transitions +either. +– If T ⇓ U ̸= Skip, then by rule L-Red the LTS at T has a transition T +a +−→ U ′ +iff the LTS at U has a corresponding transition U +a +−→ U ′ (with the same +U ′). Similarly, word(T) = Y for Y with productions Y +a +−→ γ′δ where Zδ = +word(U) and Z has productions Z +a +−→ γ′. Therefore, the LTS at word(T) + +System F µ +ω with Context-free Session Types +23 +has a transition word(T) +a +−→ δ′ iff the LTS at word(U) has a transition +word(U) +a +−→ δ′ (with the same δ′). Hence word(U) ≈ word(T) ≈ γ, so that +(U, γ) ∈ R. Finally, since U whnf the previous case analysis shows that U +and γ have matching transitions. We conclude that T and γ have matching +transitions as well. +As a consequence of the above result, we get soundness and completeness +of the bisimilarity word(T) ≈ word(U) with respect to the bisimilarity T ∼ U. +Indeed by Lemma 6, any sequence of transitions starting from T can be matched +by a sequence of transitions starting from word(T); and similarly for U. Thus +T ∼ U iff word(T) ≈ word(U). +Theorem 3. The type equivalence problem is decidable for types in F µ∗; +ω +. +Proof. Given types T, U in F µ∗; +ω +, an algorithm for deciding T ∼ U is as follows. +First compute word(T) and word(U); this terminates due to Lemma 5. Then +decide whether word(T) ≈ word(U), using any known algorithm for bisimilar- +ity of simple grammars, e.g. Almeida et al. [6] or Burkart et al. [15], whose +time complexity is doubly-exponential. Correctness follows from the discussion +immediately preceding this theorem. +For the remainder of this section, we look at the other classes of types in +Fig. 2 and examine the computation models they correspond to. Since class F µ; +is contained in F µ∗; +ω +, we can express types without λ-abstractions with simple +grammars as well. In this way we recover previous results in the literature [6,21]. +Let us now look at the class F µ∗· +ω +. In this class we do not have Skip nor +sequential composition and message operators are binary (♯T .U) rather than +unary. Since we do not have sequential composition, there is no need to consider +words of nonterminals, and instead it suffices to translate types into single sym- +bols, i.e., states in an automata. Moreover, since there is no recursion beyond +µκ, only finitely many types can be reached from a given T. We could thus adapt +our construction as follows for F µ∗· +ω +. +In the definition of the LTS (Fig. 9): +– discard all rules involving sequential composition; +– discard rules L-Var1 for m > 0 and L-DualVar2 (they were only needed to +distinguish types in sequential composition); +– discard case ι = End in rule L-Const (so that End no longer has transitions); +– replace Skip with End on the right-hand side of rules L-Var1 with m = 0 +and L-Const; +– discard rules L-Msg1 and L-Msg2 and treat ι = ♯ like the other constants in +rule L-ConstApp. +We also replace the construction of word(T ) into a construction of state(T ), +associating to each type T a state in a finite-state automata. For each transition +T +a +−→ U we have the corresponding transition state(T) +a +−→ state(U). Notice +that the resulting automata is deterministic since the original LTS is also deter- +ministic (for each type T and label a, there is at most one transition T +a +−→ U). + +24 +D. Costa et al. +Since bisimilarity of deterministic finite-state automata can be decided in poly- +nomial time [44], we get the following results. +Theorem 4. +1. To each type T in F µ∗· +ω +we can associate a finite-state automata correspond- +ing to the (fragment of the) LTS generated by T. +2. The type equivalence problem is polynomial-time decidable for types in F µ∗· +ω +. +Clearly, Theorem 4 applies to the subclasses of F µ∗· +ω +: F µ, F µ· and F µ∗ +ω . In +this way we recover previous results in the literature [16,21,33]. +Finally, we consider the classes F µ +ω , F µ· +ω and F µ +ω involving arbitrarily-kinded +recursion. We shall show that these classes are already powerful enough to simu- +late deterministic pushdown automata; hence, the type equivalence problem be- +comes impractical (i.e., no practical implementation of an algorithm is known). +We only focus on the simplest case F µ +ω , as the others two classes are even more +expressive. Instead of looking at deterministic pushdown automata, we instead +look at deterministic first-order grammars, which are an equivalent model of +computation [46]. The advantage of considering deterministic first-order gram- +mars is to simplify our construction. We say that a first-order grammar is a +tuple (X, T , N, E, R) where: +– X is a set of variables α, β, . . .; T is a set of terminal symbols a, b, . . .; N is +a set of nonterminal symbols X, Y , . . . +– each nonterminal X has an arity m = arity(X) ∈ N. +– the set E of expressions over X, N is inductively defined by two rules: any +variable α is an expression; if arity(X) = m and E1, . . . , Em are expressions, +then so is X E1 . . . Em. Whenever m = 0, X is called a constant. +– E is an expression over N, called the initial expression. +– R is a set of productions. Each production is a triple (X, a, E), written as +X α1 . . . αm +a +−→ E, where m = arity(X) and the variables in E must be +taken from α1, . . . , αm. +A first-order grammar is deterministic if, for every X and a, there is at most +one production (X, a, E) ∈ R. +Just as a simple grammar defines an LTS over words of nonterminals, a first- +order grammar defines an LTS over the set E0 of closed expressions. For each +production X α1 . . . αm +a +−→ E we have the labelled transition X E1 . . . Em +a +−→ +E[E1/α1, . . . ,Em/αm]. +Let ≈ denote bisimilarity over closed expressions according to a first-order +grammar. We now present a fully abstract (i.e., preserving bisimilarity) trans- +lation of a deterministic first-order grammar into a type in F µ +ω . Each gram- +mar variable α has a corresponding type variable α (of kind t). An expression +X E1 . . . Em is represented as a type application X E1 . . . Em. If X has arity +m and the productions X α1 . . . αm +aj +−→ Ej for a range of j, then we write the +equation specifying X as a record (since the first-order grammar is determinis- +tic, all record labels are distinct, and thus the right-hand side on the equation + +System F µ +ω with Context-free Session Types +25 +specifying X is well-formed). +X .= λα1 : t.. . . λαm : t.{a1 : E1, . . . , am : Em} +This gives rise to a system of equations {Xi .= Ti}, one for each nonterminal Xi, +where the nonterminals may appear in the right-hand sides Ti. Finally, given +an initial expression E, it is standard how to convert it into a µ-type using the +system above. +Using the above translation, we are able to simulate a transition E +aj +−→ F of +the first-order grammar as a transition E +{ai}j +−→ F on the corresponding types. +Therefore, the translation is fully abstract and we get the following result. +Theorem 5. Let E and F be closed expressions on a first-order grammar and +E, F the corresponding types. Then E ≈ F iff E ∼ F. +Let us work on an example to better understand the above translation. Con- +sider the language L3 = {ℓnarna | n ≥ 0} ∪ {ℓnbrnb | n ≥ 0} over the alpha- +bet {a, b, ℓ, r}. L3 is a typical example of a language that cannot be described +with a simple grammar, but can be accepted by a deterministic pushdown au- +tomaton [51]. Consider the first-order grammar with nonterminals X, R, A, B, ⊥, +initial expression X A B, and productions +X α β +ℓ +−→ X (R α) (R β) +X α β +a +−→ α +X α β +b +−→ β +R α +r +−→ α +A +a +−→ ⊥ +B +b +−→ ⊥ +Note that ⊥ is a constant without productions. It is easy to see that the traces +of this first-order grammar correspond exactly to the words in L3. By following +the steps in the above translation, we arrive at the system of equations +X .= λα: t.λβ : t.{ℓ: X(Rα)(Rβ), a: α, b: β} +R .= λα: t.{r: α} +A .= {a: ⊥} +B .= {b: ⊥} +⊥ .= {} +Therefore, the initial expression X A B becomes the type +(µ ξ : t ⇒ t ⇒ t. λα: t.λβ : t.{ℓ: ξ{r: α}{r: β}, a: α, b: β}){a: {}}{b: {}}, +whose transitions simulate the transitions of the first-order grammar. +6 +The term language and its metatheory +This section briefly introduces a concurrent functional language equipped with +F µ∗; +ω +types, together with its metatheory. The results mostly follow from those in +the literature, although explicit recursion at the term level and the unrestricted +bindings in typing contexts are somewhat new in session types. +The syntax of values, terms, processes and call-by-value evaluation contexts +are defined by the grammar in Fig. 11. The same figure introduces types for the + +26 +D. Costa et al. +v ::= c | x | λx: T .t | rec x: T.v | Λα: κ.v | {li = vi} | ⟨l = v⟩ as T +receive[T ] | receive[T ][T] | send[T ] | send[T ] v | send[T ] v[T ] +t ::= v | t t | t[T ] | {li = ti} | let {li = xi} = t in t +⟨l = t⟩ as T | case t of t | match t with t +p ::= ⟨t⟩ | p | p | (νxx)p +E ::= [] | E t | v E | E[T ] | {l1 = v1, . . . , lj = E, . . . , ln = en} +let {l1 = xi} = E in t | ⟨l = E⟩ as T | case E of t | match E with t +c ::= +Term constant +receive +∀α: t. ∀β : s. ?α.β → α ⊗ β +receive on a channel +send +∀α: t. α → ∀β : s. !α.β → β +send on a channel +select lj as ⊕{li : Ti} +⊕{li : Ti} → Tj +internal choice +close +End → Unit +channel close +fork +(Unit → Unit) → Unit +fork a new thread +new +∀α: s. α ⊗ Dual α +channel creation +Fig. 11: Terms, and typed for term constants. +constants. The term language is essentially the polymorphic lambda calculus +with support for session operators, formulated as in Almeida et al. and Cai et +al. [2,16]. From System F it comprises terms and type abstractions, records and +variants, including constructors and destructors in each case. The support for +session operations and concurrency includes channel creation (new), the different +channel operations (receive, send, match, select and close) and thread creation +(fork). We program at the term level and use processes only for the runtime. +Processes include terms as threads, parallel composition and channel creation, +all inspired in the pi-calculus with double binders [73]. +Term and process typing are in Fig. 12. A judgement of the form ∆ | Γ ⊢ t: T +records the fact that term t has type T under contexts ∆ (recording the kinds +of type variables) and Γ (recording types for term variables). The judgement +for processes, Γ ⊢ p, says that p is well-typed under context Γ. The judgement +simplifies that for terms, for processes feature no free type variables and are +assigned no particular type. Here Unit is short for the empty record type {}, and +T ⊗ U is short for the record type {Fst: T, Snd: U}. Once again, the rules are +adapted from the two above cited works. The difference to Cai et al. [16] is that +we work with in a linear setting and hence axioms (T-Const and T-Var) work on +an empty context, and most of the other rules must split the context accordingly. +Rule T-TAbs simplifies that of Cai et al. [16]; we can easily show that the two +rules are interchangeable. We support exponentials [37] for recursive functions, so +that one may write functions that feature more than one recursive call (good for + +System F µ +ω with Context-free Session Types +27 +Term typing +∆ | Γ ⊢ t: T +T-Const +∆ ⊢ Tc : ∗ +∆ | · ⊢ c: Tc +T-Var +∆ | x: T ⊢ x: T +T-TApp +∆ | Γ1 ⊢ t1 : U → T +∆ | Γ2 ⊢ t2 : U +∆ | Γ1, Γ2 ⊢ t1 t2 : T +T-Rec +∆ ⊢ T : ∗ +∆ | Γ, x:ω T → U ⊢ v : T → U +∆ | Γ ⊢ rec x: T → U.v : T → U +T-TAbs +∆, α: κ | Γ ⊢ v : T α +∆ | Γ ⊢ (Λα: κ.v): ∀κ T +T-Record +∆ | Γi ⊢ ti : Ti +∆ | Γ ⊢ {li = ti}: {li : Ti} +T-Proj +∆ | Γ1 ⊢ t1 : {li : Ti} +∆ | Γ2, xi : Ti ⊢ t2 : T +∆ | Γ1, Γ2 ⊢ let {li = xi} = t1 in t2 : T +T-Match +∆ | Γ1 ⊢ t1 : &{li : T i} +∆ | Γ2 ⊢ t2 : {li : Ti → T} +∆ | Γ1, Γ2 ⊢ match t1 with t2 : T +T-Eq +∆ | Γ ⊢ t: U +∆ ⊢ U : ∗ +U ∼ T +∆ | Γ ⊢ t: T +T-Dereliction +∆ | Γ, x: T ⊢ t: U +∆ | Γ, x:ω T ⊢ t: U +T-Weakening +∆ | Γ ⊢ t: U +∆ | Γ, x:ω T ⊢ t: U +T-Contraction +∆ | Γ, y:ω T, z :ω T ⊢ t: U +∆ | Γ, x:ω T ⊢ t[x/y][x/z]: U +Process typing +Γ ⊢ p +ε | Γ ⊢ t: Unit +Γ ⊢ ⟨t⟩ +Γ1 ⊢ p1 +Γ2 ⊢ p2 +Γ1, Γ2 ⊢ p1 | p2 +Γ, x: T, y: Dual T ⊢ p +Γ ⊢ (νxy)p +(T-App, T-Abs: see Cai et al. [16]; T-Case adapt from T-Match) +Fig. 12: Typing. +consuming binary trees, for example) and branches that do not use the recursive +function (for code that is supposed to terminate). Towards this end, we add an +unrestricted binding x:ω T in term variable contexts, an explicit rule for rec (as +opposed to making rec a constant as in Cai et al. [16]) and structural rules for +unrestricted bindings (T-Dereliction, T-Weakening and T-Contraction). Thanks +to the power of System F, most of the session and concurrency operators are +expressed as constants. For example, receive receives a session type !α.β with α, +the payload of the message, an arbitrary type and β, the continuation, a session +type, and returns a pair of the value received and the continuation channel. As +usual ∀α: κ. T abbreviates the type ∀κ (λα: κ.T). The exception is the external +choice (T-Match) which can not be captured by a type (similarly to T-Case) +and hence requires a dedicated typing rule. +Term and process reduction are in Fig. 13. Term reduction comprises the +standard axioms in System F with records, variants and recursion. Evaluation +contexts greatly simplify the structural reduction rules and pave the way to +process reduction. Following Milner [55] we factor out processes by means of a + +28 +D. Costa et al. +Term reduction +t → t +(λx: T.t) v → t[v/x] +(Λα: κ.t)[T ] → t[T /α] +let {li = xi} = {li = vi} in t → t[vi/xi] +case (⟨lj = v⟩ as T ) of {li = ti} → tj v +(rec x: T .v) u → (v[rec x: T.v/x]) u +t1 → t2 +E[t1] → E[t2] +Structural congruence (congruence rules omitted) +p ≡ p +p1 | p2 ≡ p2 | p1 +(p1 | p2) | p3 ≡ p1 | (p2 | p3) +(νxy)p1 | p2 ≡ (νxy)(p1 | p2) +(νwx)(νyz)p ≡ (νyz)(νwx)p +Process reduction +p → p +t1 → t2 +⟨t1⟩ → ⟨t2⟩ +⟨E[fork v]⟩ → ⟨E[{}]⟩ | ⟨v {}⟩ +⟨E[new[T ]]⟩ → (νxy)⟨E[{x,y}]⟩ +(νxy)(⟨E1[receive[T][U] y ]⟩ | ⟨E2[send[V ][W] v x]⟩) → (νxy)(⟨E1[{y, v}]⟩ | ⟨E2[x]⟩) +(νxy)(⟨E1[match y with {li = ti}]⟩ | ⟨E2[(select lj as T ) x]⟩) → (νxy)⟨E1[tj y]⟩ | ⟨E2[x]⟩ +(νxy)(⟨E1[close y]⟩ | ⟨E2[close x]⟩) → ⟨E1[{}]⟩ | ⟨E2[{}]⟩ +p1 → p2 +p1 | q → p2 | q +p1 → p2 +(νxy)p1 → (νxy)p2 +p1 ≡ p2 +p2 → p3 +p3 ≡ p4 +p1 → p4 +Fig. 13: Term and process reduction. +structural congruence relation that accounts for the associative and commuta- +tive nature of parallel composition, scope extrusion and exchanging the order of +channel bindings. The rules closely follow Almeida et al. and Gay and Vasconce- +los [2,35]. One finds axioms for forking new threads, creating new channels, for +communication (receive/send, match/select and close/close), as well as structural +rules to allow reduction underneath parallel composition, channel creation and +structural congruence. +We now address the metatheory of our language, starting with preservation +for both terms and processes. +Theorem 6 (Preservation). +1. If ∆ | Γ ⊢ t: T and t → t′, then ∆ | Γ ⊢ t′ : T. +2. If Γ ⊢ p and p ≡ p′, then Γ ⊢ p′. +3. If Γ ⊢ p and p → p′, then Γ ⊢ p′. + +System F µ +ω with Context-free Session Types +29 +Progress for the term language is assured only when the typing context con- +tains channel endpoints only. When ∆ is understood from the context we write +Γ s to mean that Γ contains only types of kind s, that is ∆ ⊢ T : s for all types +T in Γ. Well typed terms are values, or else they may reduce or are ready to +reduce at the process level. Reduction in the case of session operations—receive, +send, match, select, close—is pending a matching counterpart. +Theorem 7 (Progress for the term language). If ∆ | Γ s ⊢ t: T, then t is +a value, t reduces, or t is stuck in one of the following forms: E[fork v], E[new[T]], +E[receive[T][U] v ], E[send[U] T[v] x], E[match y with {li = ti}], E[(select lj as T) x], +or E[close x]. +In order to state our result on the absence of runtime errors we need a few +notions on the structure of terms and processes; here we follow Almeida et al. [2]. +The subject of an expression e, denoted by subj(e), is x in the following cases. +receive[T][U] x +send[T] v[U] x +match x with t +(select lj as T) x +close x +Two terms e1 and e2 agree on channel xy, notation agreexy(e1, e2), in the +following cases (symmetric forms omitted). +agreexy(receive[T][U] x, send[V ] v[W] y) +agreexy(close x, close y) +agreexy(match x with {li = ti}i∈I, (select lj as T) y) +j ∈ I +A closed process is a runtime error if it is structural congruent to some +process that contains a subexpression or subprocess of one of the following forms. +1. v u where v is not a λ or a rec, v ̸= receive[T][U], send[T] u, send[T] w[U], +select lj as T , close, fork; +2. v[T ] where v in not a Λ, v ̸= receive, receive[U], send, send[U], new; +3. let {li = xi} = v in t and v is not of the form {li = ui}; +4. case v of t and v ̸= ⟨lj = u⟩ as T or t ̸= {li = ui}i∈I with j /∈ I; +5. receive[T][U] v or send[T] u[U] v or match v with t or (select l as T) v or close v +and v is not an endpoint x; +6. ⟨E1[e1]⟩ | ⟨E2[e2]⟩ and subj(e1) = subj(e2); +7. (νxy)(⟨E1[e1]⟩ | ⟨E2[e2]⟩) and subj(e1) += +x and subj(e2) += +y and +¬ agreexy(e1, e2). +The first five cases are standard to system F with records and variants. The +support for session types and concurrency in the first two cases (term and type +application) are derived from the types of values for such operators (Fig. 11). +Item 5 addresses session operators applied to non endpoints. Item 6 is for two +concurrent session operators on the same channel end. Finally, Item 7 is for +mismatches on two session operations on two endpoints for the same channel. +Theorem 8 (Safety). If Γ s ⊢ p, then p is not a runtime error. + +30 +D. Costa et al. +An algorithmic typing system can be easily extracted from the declarative sys- +tem for terms in Fig. 12 via a bidirectional type system. The system, formulated +along the lines of Almeida et al. [2], converts the non-syntax directed judgement +∆ | Γ ⊢ t: T into two functional judgements: type synthesis ∆ | Γ1 ⊢ t ⇒ T | Γ2 +and type check ∆ | Γ1 ⊢ t ⇐ T | Γ2. In the algorithmic type system, context Γ2 +contains the unused part of context Γ1 [73,74,75]. +7 +Related Work +We briefly discuss work that is closer to ours. +Equirecursion in system F. In first investigations on equirecursive types, the no- +tion of type equivalence is often formulated in a coinductive fashion [7,13,20,29,38]. +Two types are equivalent if they unroll into the same infinite tree. Whenever this +unrolling is the only type-level computation, such trees are regular, enabling ef- +ficient decision procedures. Some authors have studied equirecursion together +with other notions of type-level computation. Solomon considers parameterized +type definitions, which correspond to higher-order kinds [63]. These implicitly +correspond to λ-terms, since reduction occurs as types are allowed to call other +types. Some authors consider equirecursion in system Fω, with weaker or stronger +notions of equality [1,14,16,41]. Regarding equirecursion in system F, the model +of Cai et al. [16] is the closest to ours, and indeed our results up to F µ∗· +ω +can +be seen as a generalization of theirs. However, Cai et al. depart from the usual +setting by allowing non-contractive types (which most authors forbid, including +this work), requiring a sort of infinitary lambda calculus. Moreover, this work +further extends additional equivalence properties by including session types with +their distinctive semantics, such as sequential composition and duality. +Session type systems. +Session types were introduced in the 90s by Honda et +al. [42,43,67]. Equirecursion was the first approach used to construct infinite +session types, which often allows type equality to be interpreted according to +a coinductive notion of bisimulation [52]. In this vein, Keizer et al. [48] utilize +coalgebras to represent session types. Since the inception of session types, there +has been an interest in extending the theory to nonregular protocols [58,59,66]. +Context-free session types emerged as a natural extension, as it still allowed +for practical type equality algorithms [4,5,6,21,56,68]. Other approaches that go +beyond regular session types include nested session types [25] as well as 1-counter, +pushdown and 2-counter session types [33]. However, the more expressive notions +are not amenable to practical type equivalence algorithms, just like the higher- +order types present in our system F µ +ω . Polymorphism in session types has also +been a topic of interest, with or without recursion [17,23,24,31,39]. +Dual type operator. +This work is, to the best of our knowledge, the first that +internalises duality as a type constructor. Other settings, such as the language +Alms [72], consider duality for session types as a user-definable, not built in, +type function. Our Dual is a type operator, not a type function. The difference +is that a type function involves a type-level computation, which converges to a + +System F µ +ω with Context-free Session Types +31 +type written without dual. For example, in Alms we would have dual(!Int.End) = +?Int.End (as a type-level computation), both sides being the same type. In our +setting, Dual (!Int; End) is a type on its own, which happens to be equivalent +to ?Int; End. At the same time, our setting allows for types such as Dual α, or +(Dual α); T1; T2, which do not reduce. +Type equivalence algorithms. +Algorithms for deciding the equivalence of types +must inherently be related to the computational power of the corresponding +type system. This has been used implicitly or explicitly to obtain decidability +results. As already explained, if equirecursion is the only type-level computation, +types can be represented as finite-state automata (or equivalently, infinite regular +trees). Although some exponential time algorithms were first proposed [32], it +has been established that the problem can be solved in quadratic time [53], +which is to be expected as it matches the corresponding problem of bisimulation +of finite-state automata [44]; see also Pierce [57]. +The next ‘simplest’ model of computation is that of simple grammars, which +intuitively correspond to deterministic pushdown automata with a single state [33]. +Almeida et al. [6] provided a practical algorithm for checking the bisimilar- +ity of simple grammars. By dropping the determinism assumption, we arrive +at Greibach normal form grammars, which are equivalent to basic process al- +gebras [8,9]. Bisimilarity algorithms have been studied extensively in this set- +ting [15,19,47,49]; presently it is known that the complexity of the problem lies +between EXPTIME and 2-EXPTIME, which does not exclude the possibility of +a polynomial time algorithm for the simpler model of simple grammars. +In this paper we present a reduction from first-order grammars to F µ +ω -types, +showing that the more expressive type systems (F µ +ω , presented here and in Cai et +al. [16], as well as its extensions) are at least as powerful as deterministic push- +down automata. As far as we know, the closest result to ours is by Solomon [63], +which shows conversions between a universe of “context-free types” and deter- +ministic context-free languages. The universe of types studied by Solomon is +different from F µ +ω . With some work we could prove that Solomon’s types can be +embedded into F µ +ω , which would entail our result as a corollary. However, it is +easier and simpler to prove directly the reduction as we did. +The equivalence problem for deterministic pushdown automata was a noto- +rious open problem for a long time, until S´enizergues showed it to be decid- +able [61,62]. Since his proof, many authors have tried to refine the result in an +attempt to arrive at an implementable algorithm [46,64,65]. +Concurrent term languages. The usefulness of a type system is directly related +to its capability to be used in a programming language. Type systems such as +the ones discussed in this work lend themselves quite readily to functional term +languages [45]. For session types, existing term languages are either inspired +in the pi calculus [27,73,69] or in the lambda calculus [35,54,70], or even the +two [71]. The system presented in this paper is linear, meaning that resources +must be used exactly once [50,74]. Some authors go beyond linearity by consid- +ering unrestricted type qualifiers [48,73] or manifest sharing [10]. + +32 +D. Costa et al. +8 +Conclusion and future work +This paper introduces an extension of system F which includes equirecursion, +lambda abstractions, and context-free session types. We present type equivalence +algorithms, and a term language and its metatheory. Although we have defined a +rather general system, it turns out that for practical purposes one must restrict +recursion to µ∗, that is, to type-level monomorphic recursion. In any case, the +main system F µ∗; +ω +is a non-trivial extension of (the contractive fragment of) F µ∗ +ω +(studied by Cai et al. [16]) as well as F µ; (studied by Almeida et al. [21]). +We have only considered polymorphic types of a functional nature: type +∀α: κ. T must always be of kind t. It is worth investigating polymorphism +over session types, as it would allow further additional behaviour. For exam- +ple, we could be interested in streaming values of heterogeneous nature, as in +type µ α: s. &{Done: Skip, More: ∀β : t. ?β; α}. It is however unclear whether +this extension would still allow a translation into a simple grammar. +We proved that the type equivalence problem for systems F µ +ω , F µ· +ω , F µ; +ω is at +least as hard as a non-efficiently-decidable problem. We conjecture that these +systems have the same power as deterministic pushdown automata (and hence, +admit decidable type equivalence), but we do not have a construction to prove +this result. In any case, our proof that the type equivalence problem is at least as +hard as the bisimilarity of deterministic pushdown automata is enough to justify +focus on the significant fragment with restricted recursion. +We study either full recursion (for theoretical results) or recursion limited +to kind ∗ (for algorithmic results). It would be interesting to study in-between +kinds of recursion; the next natural example is µ∗⇒∗. What model of computation +would we arrive at if we consider types written with this recursion operator? We +conjecture that types F µ +ω and F µ· +ω , when restricted to recursion of kind ∗ ⇒ ∗, +would still be expressible as simple grammars, whereas such a restriction in +the more powerful F µ; +ω would take us beyond this model, but perhaps without +reaching the expressivity of deterministic pushdown automata. +References +1. Abel, A.: Type-based termination: a polymorphic lambda-calculus with sized +higher-order types. Ph.D. thesis, Ludwig Maximilians University Munich (2007), +https://d-nb.info/984765581 +2. Almeida, +B., +Mordido, +A., +Thiemann, +P., +Vasconcelos, +V.T.: +Poly- +morphic +context-free +session +types. +CoRR +abs/2106.06658 +(2021). +https://doi.org/10.48550/arXiv.2106.06658 +3. Almeida, B., Mordido, A., Thiemann, P., Vasconcelos, V.T.: Polymorphic lambda +calculus with context-free session types. Information and Computation (2022). +https://doi.org/10.1016/j.ic.2022.104948 +4. Almeida, B., Mordido, A., Vasconcelos, V.T.: FreeST, a programming language +with context-free session types. http://rss.di.fc.ul.pt/tools/freest/ (2019) +5. Almeida, B., Mordido, A., Vasconcelos, V.T.: Freest: Context-free session types +in a functional language. In: PLACES. EPTCS, vol. 291, pp. 12–23 (2019). +https://doi.org/10.4204/EPTCS.291.2 + +System F µ +ω with Context-free Session Types +33 +6. Almeida, B., Mordido, A., Vasconcelos, V.T.: Deciding the bisimilarity of context- +free session types. In: TACAS. LNCS, vol. 12079, pp. 39–56. Springer (2020). +https://doi.org/10.1007/978-3-030-45237-7 3 +7. Amadio, R.M., Cardelli, L.: Subtyping recursive types. In: POPL. pp. 104–118. +ACM Press (1991). https://doi.org/10.1145/99583.99600 +8. Baeten, J.C.M., Bergstra, J.A., Klop, J.W.: Decidability of bisimulation equiva- +lence for processes generating context-free languages. In: PARLE. LNCS, vol. 259, +pp. 94–111. Springer (1987). https://doi.org/10.1007/3-540-17945-3 5 +9. Baeten, J.C.M., Bergstra, J.A., Klop, J.W.: Decidability of bisimulation equiv- +alence for processes generating context-free languages. J. ACM 40(3), 653–682 +(1993). https://doi.org/10.1145/174130.174141 +10. Balzer, S., Pfenning, F.: Manifest sharing with session types. Proc. ACM Program. +Lang. 1(ICFP), 37:1–37:29 (2017). https://doi.org/10.1145/3110281 +11. Barendregt, H.P.: The lambda calculus - its syntax and semantics, Studies in logic +and the foundations of mathematics, vol. 103. North-Holland (1985) +12. Barendregt, H.P.: The type free lambda calculus. In: Studies in Logic and the +Foundations of Mathematics, vol. 90, pp. 1091–1132. Elsevier (1977) +13. Brandt, +M., +Henglein, +F.: +Coinductive +axiomatization +of +recursive +type +equality +and +subtyping. +Fundam. +Informaticae +33(4), +309–338 +(1998). +https://doi.org/10.3233/FI-1998-33401 +14. Bruce, +K.B., +Cardelli, +L., +Pierce, +B.C.: +Comparing +object +encod- +ings. +In: +TACS. +LNCS, +vol. +1281, +pp. +415–438. +Springer +(1997). +https://doi.org/10.1007/BFb0014561 +15. Burkart, O., Caucal, D., Steffen, B.: An elementary bisimulation decision proce- +dure for arbitrary context-free processes. In: MFCS. LNCS, vol. 969, pp. 423–433. +Springer (1995). https://doi.org/10.1007/3-540-60246-1 148 +16. Cai, Y., Giarrusso, P.G., Ostermann, K.: System F-omega with equirecursive +types for datatype-generic programming. In: POPL. pp. 30–43. ACM (2016). +https://doi.org/10.1145/2837614.2837660 +17. Caires, L., P´erez, J.A., Pfenning, F., Toninho, B.: Behavioral polymorphism and +parametricity in session-based communication. In: ESOP. LNCS, vol. 7792, pp. +330–349. Springer (2013). https://doi.org/10.1007/978-3-642-37036-6 19 +18. Cardelli, +L., +Wegner, +P.: +On +understanding +types, +data +abstrac- +tion, +and +polymorphism. +ACM +Comput. +Surv. +17(4), +471–522 +(1985). +https://doi.org/10.1145/6041.6042 +19. Christensen, S., H¨uttel, H., Stirling, C.: Bisimulation equivalence is decid- +able +for +all +context-free +processes. +Inf. Comput. +121(2), +143–148 +(1995). +https://doi.org/10.1006/inco.1995.1129 +20. Colazzo, D., Ghelli, G.: Subtyping recursive types in kernel Fun. In: LICS. pp. 137– +146. IEEE Computer Society (1999). https://doi.org/10.1109/LICS.1999.782605 +21. Costa, D., Mordido, A., Po¸cas, D., Vasconcelos, V.T.: Higher-order context-free +session types in system F. In: PLACES. EPTCS, vol. 356, pp. 24–35 (2022). +https://doi.org/10.4204/EPTCS.356.3 +22. Curry, H.H., Feys, R., Craig, W. (eds.): Combinatory Logic, Volume I. North- +Holland (1958) +23. Dardha, O.: Recursive session types revisited. In: BEAT. EPTCS, vol. 162, pp. +27–34 (2014). https://doi.org/10.4204/EPTCS.162.4 +24. Dardha, O., Giachino, E., Sangiorgi, D.: Session types revisited. Inf. Comput. 256, +253–286 (2017). https://doi.org/10.1016/j.ic.2017.06.002 + +34 +D. Costa et al. +25. Das, +A., +DeYoung, +H., +Mordido, +A., +Pfenning, +F.: +Nested +session +types. +In: +ESOP. +LNCS, +vol. +12648, +pp. +178–206. +Springer +(2021). +https://doi.org/10.1007/978-3-030-72019-3 7 +26. Das, +A., +DeYoung, +H., +Mordido, +A., +Pfenning, +F.: +Nested +session +types. +ACM +Trans. +Program. +Lang. +Syst. +44(3), +19:1–19:45 +(2022). +https://doi.org/10.1145/3539656 +27. Das, A., Pfenning, F.: Rast: A language for resource-aware session types. Log. +Methods Comput. Sci. 18(1) (2022). https://doi.org/10.46298/lmcs-18(1:9)2022 +28. De Bruijn, N.G.: Lambda calculus notation with nameless dummies, a tool for +automatic formula manipulation, with application to the Church-Rosser the- +orem. In: Indagationes Mathematicae. vol. 75, pp. 381–392. Elsevier (1972). +https://doi.org/10.1016/1385-7258(72)90034-0 +29. Gapeyev, V., Levin, M.Y., Pierce, B.C.: Recursive subtyping revealed: functional +pearl. In: ICFP. pp. 221–231. ACM (2000). https://doi.org/10.1145/351240.351261 +30. Gauthier, +N., +Pottier, +F.: +Numbering +matters: +first-order +canonical +forms +for +second-order +recursive +types. +In: +ICFP. +pp. +150–161. +ACM +(2004). +https://doi.org/10.1145/1016850.1016872 +31. Gay, S.J.: Bounded polymorphism in session types. MSCS 18(5), 895–930 (2008). +https://doi.org/10.1017/S0960129508006944 +32. Gay, S.J., Hole, M.: Subtyping for session types in the pi calculus. Acta Informatica +42(2-3), 191–225 (2005). https://doi.org/10.1007/s00236-005-0177-z +33. Gay, S.J., Po¸cas, D., Vasconcelos, V.T.: The different shades of infinite ses- +sion types. In: FoSSaCS. LNCS, vol. 13242, pp. 347–367. Springer (2022). +https://doi.org/10.1007/978-3-030-99253-8 18 +34. Gay, +S.J., +Thiemann, +P., +Vasconcelos, +V.T.: +Duality +of +session +types: +The +final +cut. +In: +PLACES. +EPTCS, +vol. +314, +pp. +23–33 +(2020). +https://doi.org/10.4204/EPTCS.314.3 +35. Gay, S.J., Vasconcelos, V.T.: Linear type theory for asynchronous session types. J. +Funct. Program. 20(1), 19–50 (2010). https://doi.org/10.1017/S0956796809990268 +36. Girard, +J.Y.: +Interpr´etation +fonctionnelle +et +´elimination +des +coupures +de +l’arithm´etique d’ordre sup´erieur. Ph.D. thesis, ´Editeur inconnu (1972) +37. Girard, +J.: +Linear +logic. +Theor. +Comput. +Sci. +50, +1–102 +(1987). +https://doi.org/10.1016/0304-3975(87)90045-4 +38. Glew, N.: A theory of second-order trees. In: ESOP. LNCS, vol. 2305, pp. 147–161. +Springer (2002). https://doi.org/10.1007/3-540-45927-8 11 +39. Griffith, D.E.: Polarized substructural session types. Ph.D. thesis, University of +Illinois at Urbana-Champaign (2016). https://doi.org/10.2172/1562827 +40. Hindley, J.R., Seldin, J.P.: Introduction to Combinators and Lambda-Calculus. +Cambridge University Press (1986) +41. Hinze, R.: Polytypic values possess polykinded types. Sci. Comput. Program. 43(2- +3), 129–159 (2002). https://doi.org/10.1016/S0167-6423(02)00025-4 +42. Honda, K.: Types for dyadic interaction. In: CONCUR. LNCS, vol. 715, pp. 509– +523. Springer (1993). https://doi.org/10.1007/3-540-57208-2 35 +43. Honda, K., Vasconcelos, V.T., Kubo, M.: Language primitives and type discipline +for structured communication-based programming. In: ESOP. LNCS, vol. 1381, +pp. 122–138. Springer (1998). https://doi.org/10.1007/BFb0053567 +44. Hopcroft, J.E., Karp, R.M.: A linear algorithm for testing equivalence of finite +automata. Tech. rep., Cornell University (1971) +45. Im, H., Nakata, K., Park, S.: Contractive signatures with recursive types, type +parameters, and abstract types. In: ICALP. LNCS, vol. 7966, pp. 299–311. Springer +(2013). https://doi.org/10.1007/978-3-642-39212-2 28 + +System F µ +ω with Context-free Session Types +35 +46. Janˇcar, +P.: +Short +decidability +proof +for +DPDA +language +equivalence +via +1st +order +grammar +bisimilarity. +CoRR +abs/1010.4760 +(2010), +http://arxiv.org/abs/1010.4760 +47. Janˇcar, +P.: +Bisimilarity +on +basic +process +algebra +is +in +2-ExpTime +(an +explicit +proof). +Log. +Methods +Comput. +Sci. +9(1) +(2012). +https://doi.org/10.2168/LMCS-9(1:10)2013 +48. Keizer, A.C., Basold, H., P´erez, J.A.: Session coalgebras: A coalgebraic view on +session types and communication protocols. In: ESOP. LNCS, vol. 12648, pp. 375– +403. Springer (2021). https://doi.org/10.1007/978-3-030-72019-3 14 +49. Kiefer, S.: BPA bisimilarity is EXPTIME-hard. Inf. Process. Lett. 113(4), 101–106 +(2013). https://doi.org/10.1016/j.ipl.2012.12.004 +50. Kobayashi, +N., +Pierce, +B.C., +Turner, +D.N.: +Linearity +and +the +pi- +calculus. +ACM +Trans. +Program. +Lang. +Syst. +21(5), +914–947 +(1999). +https://doi.org/10.1145/330249.330251 +51. Korenjak, A.J., Hopcroft, J.E.: Simple deterministic languages. In: SWAT. pp. +36–46. IEEE Computer Society (1966). https://doi.org/10.1109/SWAT.1966.22 +52. Kozen, D., Silva, A.: Practical coinduction. Math. Struct. Comput. Sci. 27(7), +1132–1152 (2017). https://doi.org/10.1017/S0960129515000493 +53. Lange, +J., +Yoshida, +N.: +Characteristic +formulae +for +session +types. +In: +TACAS. +LNCS, +vol. +9636, +pp. +833–850. +Springer +(2016). +https://doi.org/10.1007/978-3-662-49674-9 52 +54. Lindley, S., Morris, J.G.: Talking bananas: structural recursion for session types. +In: ICFP. pp. 434–447. ACM (2016). https://doi.org/10.1145/2951913.2951921 +55. Milner, R.: Functions as processes. Math. Struct. Comput. Sci. 2(2), 119–141 +(1992). https://doi.org/10.1017/S0960129500001407 +56. Padovani, L.: Context-free session type inference. ACM Trans. Program. Lang. +Syst. 41(2), 9:1–9:37 (2019). https://doi.org/10.1145/3229062 +57. Pierce, B.C.: Types and programming languages. MIT Press (2002) +58. Puntigam, F.: Non-regular process types. In: Euro-Par. LNCS, vol. 1685, pp. 1334– +1343. Springer (1999). https://doi.org/10.1007/3-540-48311-X 189 +59. Ravara, A., Vasconcelos, V.T.: Behavioural types for a calculus of concur- +rent objects. In: Euro-Par. LNCS, vol. 1300, pp. 554–561. Springer (1997). +https://doi.org/10.1007/BFb0002782 +60. Reynolds, +J.C.: +Towards +a +theory +of +type +structure. +In: +Program- +ming +Symposium. +LNCS, +vol. +19, +pp. +408–423. +Springer +(1974). +https://doi.org/10.1007/3-540-06859-7 148 +61. S´enizergues, G.: The equivalence problem for deterministic pushdown automata +is decidable. In: ICALP. LNCS, vol. 1256, pp. 671–681. +Springer (1997). +https://doi.org/10.1007/3-540-63165-8 221 +62. S´enizergues, +G.: +L(A) += +L(B)? +decidability +results +from +complete +for- +mal +systems. +In: +ICALP. +LNCS, +vol. +2380, +p. +37. +Springer +(2002). +https://doi.org/10.1007/3-540-45465-9 4 +63. Solomon, M.H.: Type definitions with parameters. In: POPL. pp. 31–38. ACM +Press (1978). https://doi.org/10.1145/512760.512765 +64. Stirling, C.: Decidability of DPDA equivalence. Theor. Comput. Sci. 255(1-2), +1–31 (2001). https://doi.org/10.1016/S0304-3975(00)00389-3 +65. Stirling, C.: Deciding DPDA equivalence is primitive recursive. In: ICALP. +Lecture Notes in Computer Science, vol. 2380, pp. 821–832. Springer (2002). +https://doi.org/10.1007/3-540-45465-9 70 +66. S¨udholt, M.: A model of components with non-regular protocols. In: SC. LNCS, +vol. 3628, pp. 99–113. Springer (2005). https://doi.org/10.1007/11550679 8 + +36 +D. Costa et al. +67. Takeuchi, K., Honda, K., Kubo, M.: An interaction-based language and its +typing system. In: PARLE. LNCS, vol. 817, pp. 398–413. Springer (1994). +https://doi.org/10.1007/3-540-58184-7 118 +68. Thiemann, P., Vasconcelos, V.T.: Context-free session types. In: ICFP. pp. 462– +475. ACM (2016). https://doi.org/10.1145/2951913.2951926 +69. Toninho, +B., +Caires, +L., +Pfenning, +F.: +Dependent +session +types +via +in- +tuitionistic +linear +type +theory. +In: +PPDP. +pp. +161–172. +ACM +(2011). +https://doi.org/10.1145/2003476.2003499 +70. Toninho, B., Caires, L., Pfenning, F.: Higher-order processes, functions, and ses- +sions: A monadic integration. In: ESOP. LNCS, vol. 7792, pp. 350–369. Springer +(2013). https://doi.org/10.1007/978-3-642-37036-6 20 +71. Toninho, B., Yoshida, N.: On polymorphic sessions and functions: A tale of two +(fully abstract) encodings. ACM Trans. Program. Lang. Syst. 43(2), 7:1–7:55 +(2021). https://doi.org/10.1145/3457884 +72. Tov, J.A.: Practical programming with substructural types. Ph.D. thesis, North- +eastern University (2012) +73. Vasconcelos, V.T.: Fundamentals of session types. Inf. Comput. 217, 52–70 (2012). +https://doi.org/10.1016/j.ic.2012.05.002 +74. Walker, D.: Advanced Topics in Types and Programming Languages, chap. Sub- +structural Type Systems, pp. 3–44. The MIT Press (2005) +75. Zalakain, +U., +Dardha, +O.: +π +with +leftovers: +A +mechanisation +in +Agda. +In: +FORTE. +LNCS, +vol. +12719, +pp. +157–174. +Springer +(2021). +https://doi.org/10.1007/978-3-030-78089-0 9 +A +Proof of Theorem 2 +This section is devoted to proving Theorem 2, that kinding is decidable. Since +kinding requires normalisation (due to rule K-TApp), we must first investigate +that problem. +Even without considering sequential composition and recursion, it is well- +known that normalisation may not terminate ((λx.x x) (λx.x x) is the typical +example); in fact it is undecidable whether a type normalises. The standard +approach is to consider kinded types, which are strongly normalising. However, +in our model kinding itself requires normalisation, which leads to a “chicken-and- +egg” situation. The solution is to consider a notion of pre-kinding. We will write +∆ ⊢pre T : κ to mean that T is pre-kinded with kind κ. The rules for pre-kinding +are the same as for kinding, with the exception of rule K-TApp which loses the +normalisation proviso, becoming rule PK-TApp: +K-TApp +∆ ⊢ T : κ ⇒ κ′ +∆ ⊢ U : κ +T U norm +∆ ⊢ T U : κ′ +⇒ +PK-TApp +∆ ⊢pre T : κ ⇒ κ′ +∆ ⊢pre U : κ +∆ ⊢pre T U : κ′ +Lemma 7. ∆ ⊢pre T : κ is decidable (in linear time). +Proof. Pre-kinding uses rule PK-TApp instead of PK-TApp, and therefore does +not require normalisation. Therefore, given ∆ and T a pre-kind can be inferred by +traversing the abstract syntax tree defining T, and using the context ∆ to infer + +System F µ +ω with Context-free Session Types +37 +the kind of variables. This processes requires a single pass and thus terminates +in linear time. Therefore ∆ ⊢pre T : κ is also decidable in linear time. +We can now use pre-kinding to look at normalisation. Recall that, by Fig. 6, +there are essentially four ways to reduce a type T: the usual β-reduction for an +application of a λ-term (R-β); the reduction for the recursion operator (R-µ); +the reductions for sequential composition (R-Seq1, R-Seq2 and R-Assoc); and +the reductions for duals. Let us separate reductions arising from R-µ from the +other rules, i.e., let us use −→µ for reduction under recursion and −→β;D for +the usual β-reduction as well as reductions for sequential composition and duals. +Let us also extend these reductions to functions on an application, i.e., we have +T V −→µ U V , T V −→β;D U V resp. whenever T −→µ U, T −→β;D U resp. In +this way, we have −→ = −→β;D ∪ −→µ. We also extend the notion of weak head +normal form and normalisation into the reductions −→β;D and −→µ, writing +T ⇓β;D U and T ⇓µ U. +Lemma 8 (Normalisation). If ∆ ⊢pre T : κ then there exists a unique U s.t. +T ⇓β;D U. +Proof. Straightforward extension of the normalisation result for simply-typed +lambda calculus [57, Chapter 12]. The fact that we include reduction under +sequential composition and duality does not invalidate the standard proof, since +these reductions simplify the type by attempting to bring a type constructor to +the front; in particular, the depth of the abstract syntax tree defining the type +does not increase along such reductions. +Notice that we cannot extend the above lemma to full β-normalisation since +reduction for the recursion operator may increase the size of the resulting type. +The simplest example is µs (λα: s.α), which is pre-kinded as ⊢pre µs (λα: s.α) : s +but does not normalise: +µs (λα: s.α) −→µ (λα: s.α) (µs (λα: s.α)) −→β;D µs (λα: s.α) −→µ · · · +Theorem 2 (Decidability of type formation). +∆ ⊢ T : κ is decidable for +types in F µ∗; +ω +. +Proof. Let ∆, T, κ be given. We can first determine whether ∆ ⊢pre T : κ +(Lemma 7). If T is not pre-kinded, then it is also not kinded. Otherwise we +need to determine whether T normalises, or equivalently, whether there is an +infinite sequence of reductions T = T0 −→ T1 −→ T2 −→ · · · By Lemma 8, +such a sequence would have to contain an infinite amount of µ-reductions, where +between two µ-reductions there must be a finite number of the other reductions. +In other words, we can construct (any finite prefix of) the sequence +T = T0 ⇓β;D T ′ +0 −→µ T1 ⇓β;D T ′ +1 −→µ T2 ⇓β;D T ′ +2 −→µ · · · +where for each i, T ′ +i is necessarily reached from Ti after finitely many steps. +In the above sequence, the only possible reductions that can be applied to +T ′ +i are µ-reductions, since T ′ +i is whnf with respect to the other reductions. If T ′ +i + +38 +D. Costa et al. +does not have any µ-reduction, the sequence terminates and we can correctly +determine that T normalises. Otherwise if T ′ +i (only) has a µ-reduction, then it +must fit into one of the following cases (this is where we use the restriction to +recursion of kind ∗): +T ′ +i = µ∗ U −→µ U (µ∗ U) +T ′ +i = (µ∗ U); V −→µ (U (µ∗ U)); V +T ′ +i = Dual (µ∗ U) −→µ Dual (U (µ∗ U)) +T ′ +i = (Dual (µ∗ U)); V −→µ (Dual (U (µ∗ U))); V +Note that, in each of the four cases above, the expression µ∗ U reappears after +the µ-reduction (without change). Therefore, the number of different subexpres- +sions µ∗ U that might appear in the sequence of reductions is finite, and we can +detect an infinite sequence by ‘tagging’ the expression µ∗ U and stopping once +µ∗ U reappears. Therefore, we can devise an algorithm for deciding whether T +normalises: follow the sequence of reductions, terminating: as soon as no reduc- +tions are possible, or as soon as we revisit a previously ‘tagged’ µ∗ U. +