2045 \lmcsheadingLABEL:LastPageOct. 05, 2023Oct. 07, 2024
[a] [c] [b]
Fair Asynchronous Session Subtyping
Abstract.
Session types are widely used as abstractions of asynchronous message passing systems. Refinement for such abstractions is crucial as it allows improvements of a given component without compromising its compatibility with the rest of the system. In the context of session types, the most general notion of refinement is asynchronous session subtyping, which allows message emissions to be anticipated w.r.t. a bounded amount of message consumptions. In this paper we investigate the possibility to anticipate emissions w.r.t. an unbounded amount of consumptions: to this aim we propose to consider fair compliance over asynchronous session types and fair refinement as the relation that preserves it. This allows us to propose a novel variant of session subtyping that leverages the notion of controllability from service contract theory and that is a sound characterisation of fair refinement. In addition, we show that both fair refinement and our novel subtyping are undecidable. We also present a sound algorithm which deals with examples that feature potentially unbounded buffering. Finally, we present an implementation of our algorithm and an empirical evaluation of it on synthetic benchmarks.
1. Introduction
The coordination of software components via message-passing techniques is becoming increasingly popular in modern programming languages and development methodologies based on actors and microservices, e.g., Rust, Go, and the Twelve-Factor App methodology [twelvefactor]. Often the communication between two concurrent or distributed components takes place over point-to-point fifo channels.
Abstract models such as communicating finite-state machines [BZ83] and asynchronous session types [HYC16] are essential to reason about the correctness of such systems in a rigorous way. In particular these models are important to reason about mathematically grounded techniques to improve concurrent and distributed systems in a compositional way. The key question is whether a component can be refined independently of the others, without compromising the correctness of the whole system. In the theory of session types, the most general notion of refinement is the asynchronous session subtyping [ESOP09, CDY2014, MariangiolaPreciness], which leverages asynchrony by allowing the refined component to anticipate message emissions, but only under certain conditions. Notably asynchronous session subtyping rules out candidate subtypes that occur naturally in communication protocols where, e.g., two parties simultaneously send each other a finite but unspecified amount of messages before removing them from their buffers.
We illustrate this key limitation of the asynchronous session subtyping with Figure 1, which depicts possible communication protocols between a spacecraft and a ground station that communicate via two unbounded asynchronous channels (one in each direction). For convenience, the protocols are represented as session types (bottom) and equivalent communicating finite-state machines (top). Consider and first. Session type is the abstraction of the spacecraft. It may send a finite but unspecified number of telemetries (), followed by a message — this phase of the protocol typically models a for loop and its exit. In the second phase, the spacecraft receives a number of telecommands (), followed by a message . Session type is the abstraction of the ground station. It is the dual of , written , as required in standard binary session types without subtyping. Since and are dual of each other, the theory of session types guarantees that they form a correct composition, namely no communication errors can be generated and the communication protocol can always terminate successfully, with empty queues.
However, it is clear that this protocol is not efficient: the communication is half-duplex, i.e., it is never the case that more than one party is sending at any given time. Using full-duplex communication is crucial in distributed systems with intermittent connectivity, e.g., in this case ground stations are not always visible from low orbit satellites.
The abstraction of a more efficient ground station is given by type , which sends telecommands before receiving telemetries. In this way and interact in a symmetric manner: they first send all of their messages and then consume the messages sent from the other partner. No communication error can occur, and the communication protocol can always terminate successfully, with empty queues. Unfortunately is not an asynchronous subtype of according to earlier definitions of session subtyping [ESOP09, MariangiolaPreciness, CDY2014]. Hence they cannot formally guarantee that is a safe replacement for . Note that the composition of and is not existentially bounded, hence it cannot be verified by techniques based on communicating finite-state machines [LangeY19, BouajjaniEJQ18, GenestKM06, GenestKM07].
= | ||
= | ||
= |
Technically speaking, previous asynchronous session subtyping relations do not capture our spacecraft example due to the notion of correct composition that they consider. For instance, the notion of correct composition considered in [MariangiolaPreciness] imposes that all sent messages are guaranteed to be consumed along all possible computations of the receiver. Following this approach the above type is not a correct refinement of because can start by performing infinitely many outputs without consuming any incoming message.
The alternative notion of correct composition that we consider is weaker in that we do not impose a sent message to be consumed along all possible paths of the receiver, but we only require that, for all possible computation of the receiver either the message has been already consumed or there exists a continuation of the computation in which the message will be consumed. More precisely, our notion of correctness is as follows: given the composition of two session types, for every computation there always exists a continuation of such computation reaching successful termination (with empty queues). This is a reasonable assumption, e.g., for programs that can conceptually run indefinitely but must account for graceful termination (e.g., to release acquired resources).
According to this notion of correct composition, and are correct partners in that for every reachable state, we can always find a way to terminate successfully the interaction. This way to termination can be selected by exiting from the initial loops of outputs of both and . The theory that we will develop will allow us to conclude that is a correct refinement of for every possible partner, not only for the partner .
The use of this notion of correct composition is new in the context of asynchronous session types, but it has been already considered in several related contexts. First of all, we observe that according to the terminology in [GlabbeekH19], our notion of correctness coincides with imposing that successful termination is a liveness-property which holds under the assumption of full fairness. For this reason, we name fair compliance our notion of correct composition. Fair compliance has been already considered in the context of synchronous session types [Padovani16, CicconeP22], in the definition of should testing [RV07] where “every reachable state is required to be on a path to success”, and applied also to behavioural contracts [BravettiZ08, wsfm08].
Given our notion of fair compliance defined on an operational model for asynchronous session types, we define fair refinement the refinement relation that preserves it. Then, we propose a novel variant of session subtyping called fair asynchronous session subtyping, that leverages the notion of controllability from service contract theory, and which is a sound characterisation of fair refinement. We show that both fair refinement and fair asynchronous session subtyping are undecidable, but give a sound algorithm for the latter. Our algorithm covers session types that exhibit complex behaviours (including the spacecraft example and variants). Our algorithm has been implemented in a tool available online [tool].
Structure of the paper
The rest of this paper is structured as follows. In § 2 we recall syntax and semantics of asynchronous session types, we define fair compliance and the corresponding fair refinement. In § 3 we introduce fair asynchronous subtyping, the first relation of its kind to deal with examples such as those in Figure 1. In § 4 we propose a sound algorithm for subtyping that supports examples with unbounded accumulations, including the ones discussed in this paper. In § 5 we discuss the implementation of this algorithm. In § 6 we present an evaluation of our implementation on generated session types. Finally, in § 7 we discuss related and future work. The paper includes also an the appendix containing details of proofs that are not necessary in order to understand the main results that we have proved and the corresponding proof techniques.
This paper is based on the conference publication [BravettiLZ21]. The main novelties w.r.t. [BravettiLZ21] are: the inclusion of all the proofs of our results, a completely new empirical evaluation of the implementation of our algorithm for checking fair asynchronous session subtyping (see § 6), an enriched and more comprehensive related work section.
2. Fair Refinement for Asynchronous Session Types
In this section we first recall the syntax of two-party session types, their reduction semantics, and a notion of compliance centred on the successful termination of interactions. We define our notion of refinement based on this compliance and show that it is generally undecidable whether a type is a refinement of another.
2.1. Preliminaries: Binary Session Types
Syntax
The formal syntax of two-party session types is given below. We follow the simplified notation used in, e.g., [BravettiCZ17, BCZ18], without dedicated constructs for sending an output/receiving an input. Additionally we abstract away from message payloads since they are orthogonal to the results of this paper.
[Session Types] Given a set of labels , ranged over by , the syntax of two-party session types is given by the following grammar:
Output selection represents a guarded internal choice, specifying that a label is sent over a channel, then continuation is executed. Input branching represents a guarded external choice, specifying a protocol that waits for messages. If message is received, continuation takes place. In selections and branchings each branch is tagged by a label , taken from a global set of labels . In each selection/branching, these labels are assumed to be pairwise distinct. In what follows, we leave implicit the index set in input branchings and output selections when it is clear from the context. Types and denote standard recursion constructs. We assume recursion to be guarded in session types, i.e., in , the recursion variable occurs within the scope of a selection or branching. Session types are closed, i.e., all recursion variables occur under the scope of a corresponding binder . Terms of the session syntax that are not closed are dubbed (session) terms. Type denotes the end of the interactions.
The dual of session type , written , is inductively defined as follows: , , , , and .
2.2. Asynchronous Fair Refinement
We now define our notion of fair refinement. We first define a reduction semantics formalizing the interaction between two binary session types assuming asynchronous communication via FIFO buffers. Then we formalize the notion of successful final configuration; intuitively a configuration is successful if both communicating types have completed their send/receive operations and the buffers are empty. Compliance is then defined as follows: two session types are compliant if, for every reachable configuration (according to the reduction semantics), the interaction can continue to reach a successful configuration. Finally, we say that a type refines another type if it can safely replace , i.e., if is compliant with a type then also is compliant with .
In the definition of the reduction semantics for types we need some auxiliary notation. Hereafter, we let range over words in , write for the empty word, and write for the concatenation of words and , where each word may contain zero or more labels. Also, we write for where every free occurrence of is replaced by .
We give an asynchronous semantics of session types via transition systems whose states are configurations of the form: where and are session types equipped with two sequences and of incoming messages (representing unbounded buffers). We use , , etc. to range over configurations.
In this paper, we use explicit unfoldings of session types, as defined below. {defi}[Unfolding] Given session type , we define :
Definition 2.2 is standard — an equivalent function is used in the first session subtyping [GH05]. Notice that unfolds all the recursive definitions in front of , and it is well defined for session types with guarded recursion (c.f. assumptions in Section 2.1).
[Transition Relation] The transition relation over configurations is the minimal relation satisfying the rules below (plus symmetric ones):
-
(1)
if then ;
-
(2)
if then ;
-
(3)
if then .
We write for the reflexive and transitive closure of the relation. Intuitively a configuration reduces to configuration when either (1) a type outputs a message , which is added at the end of its partner’s queue; (2) a type consumes an expected message from the head of its queue; or (3) the unfolding of a type can execute one of the transitions above.
Next, we define successful configurations as those configurations where both types have terminated (reaching ) and both queues are empty. We use this to give our definition of compliance which holds when it is possible to reach a successful configuration from all reachable configurations. {defi}[Successful Configuration] The notion of successful configuration is formalised by a predicate defined as follows:
[Compliance] Given a configuration we say that it is a correct composition if, whenever , there exists a configuration such that and .
Two session types and are compliant if is a correct composition.
Observe that our definition of compliance is stronger than what is generally considered in the literature on session types, e.g., [LangeY19, LY17, DY13], where two types are deemed compliant if all messages that are sent are eventually received, and each non-terminated type can always eventually make a move. Compliance is analogous to the notion of correct session in [Padovani16] but in an asynchronous setting.
A consequence of Definition 2.2 is that it is generally not the case that a session type is compliant with its dual , as we show in the example below. {exa} The session type and its dual are not compliant. Indeed, when sends label , the configuration is no longer reachable.
We introduce a notion of refinement that preserves compliance. This follows previous work done in the context of behavioural contracts [BravettiZ08] and synchronous multi-party session types [Padovani16]. The key difference with these works is that we are considering asynchronous communication based on (unbounded) fifo queues. Asynchrony makes fair refinement undecidable, as we show below.
[Refinement] A session type refines , written , if for every s.t. and are compliant then and are also compliant. In contrast to traditional (synchronous and asynchronous) subtyping for session types [GH05, MariangiolaPreciness, ESOP09], this refinement is not covariant on outputs, i.e., it does not always allow a refined type to have output selections with less labels.111The synchronous subtyping in [GH05] follows a channel-oriented approach; hence it has the opposite direction and is contravariant on outputs.
Let and . We have that is a synchronous (and asynchronous) subtype of . However is not a refinement of . In particular, the type is compliant with but not with , since does not terminate.
2.3. Undecidability of Fair Refinement
Next, we show that the refinement relation is generally undecidable. The proof of undecidability exploits results from the tradition of computability theory, i.e., Turing completeness of queue machines. The crux of the proof is to reduce the problem of checking the reachability of a given state in a queue machine to the problem of checking the refinement between two session types.
Preliminaries
Below we consider only state reachability in queue machines, and not the typical notion of the language recognised by a queue machine (see, e.g., [BravettiCZ17] for a formalisation of queue machines). Hence, we use a simplified formalisation, where no input string is considered.
[Queue Machine] A queue machine is defined by a five-tuple where:
-
•
is a finite set of states;
-
•
is a finite set denoting the queue alphabet (ranged over by );
-
•
is the initial queue symbol;
-
•
is the start state;
-
•
is the transition function ( is the set of sequences of symbols in ).
Considering a queue machine , a configuration of is an ordered pair where is its current state and is the queue. The starting configuration is , consisting of the start state and the initial queue symbol .
Next, we define the transition relation (), leading a configuration to another, and the related notion of state reachability. {defi}[State Reachability] Given a machine , the transition relation over configurations is defined as follows. For , , and , we have whenever . Let be the reflexive and transitive closure of .
A target state is reachable in if there is s.t. .
Since queue machines can deterministically encode Turing machines (see, e.g., [BravettiCZ17]), checking state reachability for queue machines is undecidable.
To prove the undecidability of fair refinement, we consider an arbitrary queue machine , and a target state for which we define two session types and such that if and only if state is reachable in . Hereafter, we use convenient notations for denoting output selections and input branchings. Instead of using labels indexed on an indexing set , as in the input branching syntax , we also use explicitly distinct labels, as in (we use the same notation for output selections). We also use the union operator to combine disjoint sets of labels, for instance, instead of writing , we use the notation (we use the same notation for input branchings).
We start by defining the type .222In the definition of the type , as well as in the definition , we make the non restrictive assumption that the set of labels of the Definition 2.1 of the syntax of session types includes the symbols in the considered queue machine alphabet plus the additional symbol . This type reproduces the finite control of the queue machine , with a couple of differences: () it initialises the queue with symbol , and () the state produces the additional ending symbol to communicate the end of the computation, then it consumes all symbols in the queue and successfully terminates when is read from the queue. In this way, the queue is empty when the type successfully terminates.
[Finite Control Encoding] Let be a queue machine, , and be the additional ending symbol; we define as follows:
where, given and , is defined as follows:
while
We now define the type , that repeatedly behaves like a producer/consumer for all the symbols of the queue alphabet plus the ending symbol , with the difference that after producing and consuming the ending symbol , the type becomes .
[Producer/consumer] Let be a queue machine and be the ending symbol. We define as
While and may appear unrelated, we have that under some conditions holds. Namely, if and only if is reachable in . To prove this, we first characterize the set of types that are compliant with . This set consists of types that have the same behaviour (according to type bisimilarity) of , i.e., the dual of . The type , instead of being a producer/consumer, is a consumer/producer which sends the messages it receives back to the partner. This simulates a FIFO queue that receives messages and sends messages in the same order of reception. Hence, the finite control encoding , when combined with such consumer/producer (i.e. any type having the same behaviour of ), faithfully reproduces the same behaviour of the encoded queue machine. A successful configuration can be reached only if the type modeling the finite control terminates, and this is possible only if the final state is reached.
As mentioned above, the proof relies on the notion of type bisimilarity.
[Type bisimilarity]
A relation on session types is a bisimulation whenever implies:
-
(1)
if then ;
-
(2)
if then with ;
-
(3)
if then with ;
-
(4)
if then .
is bisimilar to , written , if there is a bisimulation such that .
Session type bisimilarity will be used only in the proof of undecidability of refinement and will not be involved in further developments in the remainder of the paper. Namely, we need bisimilarity in Lemma 3 to characterise the session types that are compliant with . Notice also that the relation is symmetric, i.e., if then also . In fact, the first three items of the above Definition simply check whether the l.h.s. and the r.h.s. terms are either both or have the same branching structure (i.e., the same set of labels) up-to unfolding of the r.h.s. But the same effect of unfolding on the r.h.s. can be obtained on the l.h.s. by (possibly repeated) application of the fourth item of the above definition.
In the proof of undecidability of refinement we need a result about bisimilar session types, i.e., bisimilarity preserves compliance. Namely, we have that is compliant with if and only if is compliant with assuming and . This is an immediate corollary of the following Lemma (which directly follows from the bisimilarity of the considered types and ).
Lemma 1.
Consider the configuration and the session type s.t. . We have that:
-
•
if and only if ;
-
•
if and only if there exists s.t. .
Corollary 2.
Consider two pairs of bisimilar session types: and . We have that is compliant with if and only if is compliant with . Moreover, we have that if and only if .
As informally mentioned above, type bisimilarity allows us to characterize the set of types that are compliant with a producer/consumer type , for some queue machine and additional ending symbol . This result is formalized by the following Lemma (proof in Appendix A.1).
Lemma 3.
Let be a queue machine and the additional ending symbol. Posing , for every session type with input/output labels in we have that is compliant with if and only if .
The type behaves like a FIFO queue, which simply returns the messages it has received from the partner (in the same order). Hence a type simulating the finite control , for the same queue machine and additional ending symbol as above, turns out to be compliant with if and only if the final state is reachable in (remember that only the encoding of allows to reach ). This result is formalized in the next theorem (proof in Appendix A.1).
Theorem 4.
Let be a queue machine, , the additional ending symbol. Posing and , we have that is compliant with if and only if is reachable in .
Notice that the above theorem formalizes a reduction from the reachability problem in queue machines to the verification of compliance between session types. Hence, we can already conclude that the compliance relation is undecidable.
We now combine Corollary 2, Lemma 3 and Theorem 4 to prove the undecidability of refinement. Consider the two above types and . By Lemma 3 we have that is compliant only with and its bisimilar types. Given that bisimulation preserves compliance (Corollary 2) we have that refines if and only if it is compliant with . But the latter holds if and only if is reachable in (Theorem 4). In this way we reduce the reachability problem in queue machines to the verification of refinement between session types. We formally state this result in the theorem below (proof in Appendix A.1).
Theorem 5.
Let be a queue machine, , the additional ending symbol. Posing and , we have that if and only if is reachable in .
As a direct consequence of the above theorem and the undecidability of reachability in queue machines, we can conclude that refinement (Definition 2.2) is also undecidable.
Corollary 6.
Given two session types and , it is in general undecidable to check whether holds.
2.4. Controllability and its Decidability
Given a notion of compliance, controllability amounts to checking the existence of a compliant partner (see, e.g., [Loh08, Wei08, BZ09a]). In our setting, a session type is controllable if there exists another session type with which it is compliant.
Checking for controllability algorithmically is not trivial as it requires to consider infinitely many potential partners. For the synchronous case, an algorithmic characterisation was studied in [Padovani16]. In the asynchronous case, the problem is even harder because each of the infinitely many potential partners may generate an infinite state computation (due to unbounded buffers): specifically this reflects in the proof of its algorithmic characterisation. The main contribution of this subsection is, thus, to give an algorithmic characterisation of controllability in the asynchronous setting that is proven to be sound and complete. Doing this is important because controllability is an essential ingredient for defining fair asynchronous subtyping, see Section 3.
[Characterisation of Controllability, ] We preliminarly define judgement for session types having single input choices, i.e. such that all their input branches include just one possible choice. is defined inductively as follows: {mathpar} \inferrule end ok
end∈T T{} ok μt.T ok
T ok &{l:T} ok
∀i ∈I . T_i ok ⊕{l_i:T_i}_i∈I ok where holds if occurs in .
We now define predicate over arbitrary session types as follows. holds true if and only if there exists such that:
-
(1)
is obtained from by syntactically replacing every input choice occurring in with a term (with ). Formally this is denoted by , where (standing for “single input choices”) is defined as the smallest relation over session types such that: {mathpar} \inferrule end sin end
\inferrulet sin t
\inferruleT sin T’ μt.T sin μt.T’
\inferruleT_j sin T’_j j ∈I &{l_i:T_i}_i∈I sin &{l_j:T’_j}
\inferrule∀i ∈I . T_i sin T’_i ⊕{l_i:T_i}_i∈I sin ⊕{l_i:T’_i}_i∈I In the following we use to denote the set of single input choice types such that .
-
(2)
holds true.
A type such that is indeed controllable, in that , the dual of type considered above, is compliant with (the predicate in the premise of the rule for recursion guarantees that a successful configuration is always reachable while looping). Moreover the above definition naturally yields a simple algorithm that decides whether or not holds for a type , i.e., we first pick a single branch for each input prefix syntactically occurring in (there are finitely many of them) and then we inductively check if holds.
does not hold because it is not possible to construct a as specified in Definition 2 for which holds. In this case we have just two possible types that can be obtained by input choice replacement: and . For the former does not hold because there is no in the body of ; for the latter, instead, does not hold because there is no in the body of .
As a result of Theorem 7 (below), there is no session type that is compliant with . Hence is not controllable.
The following theorem shows that the judgement , as defined above, precisely characterises controllability (i.e., the existence of a compliant type). Its proof is rather complex (it requires introducing significant auxiliary technical machinery) and can be found in Appendix A.2.
Theorem 7.
holds if and only if there exists a session type such that and are compliant.
Sketch of the proof. The proof relies on expressing session types via a set of equations, where each of the variables is mapped to an equation. In essence, from controllable we show that there exists a compliant type by considering the type (in equation set notation), where is the type with single input branches obtained from by input choice replacement. The more difficult part of the proof is the opposite implication, where from the existence of any compliant we show that is controllable. This amounts to show that it is possible to build from the transition system of the correct composition (in equation set notation), which is, in general, infinite state. ∎
3. Fair Asynchronous Session Subtyping
In this section, we present our novel variant of asynchronous subtyping which we call fair asynchronous subtyping.
First, we need to define a distinctive notion of unfolding. As anticipated in the introduction (see the discussion about Figure 1), our subtyping will identify the type as a subtype of , with
Following the approach taken in other definitions of asynchronous subtyping [MY15, MariangiolaPreciness, CDY2014], our definition will require to decompose the candidate supertype ( in our case) as an input context, with holes filled with subtypes starting with output selections. Notice that the subterm of which starts with an output selection is not a correct subtype because it contains the free occurrence of the recursive variable . Our distinctive notion of unfolding, will replace such free variable with its definition. More precisely, we define the function to unfold type by replacing recursion variables with their corresponding definitions only if they are guarded by an output selection. In the definition, we use the predicate which holds if all instances of variable are output selection guarded, i.e., occurs free in only inside subterms .
[Selective Unfolding]
Given a term , we define
where, is obtained from by replacing the free occurrences of that are inside a subterm of by .
Consider the type , then we have
i.e., the type is only unfolded within output selection sub-terms.
Note that is used to identify where unfolding must take
place, e.g.,
.
The last auxiliary notation required to define our notion of subtyping is that of input contexts, which are used to record inputs that may be delayed in a candidate super-type. In contrast to previous works on asynchronous subtyping, these input contexts may include recursive constructs. {defi}[Input Context] An input context is a session type with several holes defined by the syntax:
where the holes , with , of an input context are assumed to be pairwise distinct. We assume that recursion is guarded, i.e., in an input context , the recursion variable must occur within a subterm .
We write for the set of hole indices in . Given a type for each , we write for the type obtained by filling each hole in with the corresponding .
In contrast to previous works [MariangiolaPreciness, ESOP09, CDY2014, BravettiCZ17, sefm19, BCLYZ19], these input contexts may contain recursive constructs. This is crucial to deal with examples such as Figure 1.
We are now ready to define the fair asynchronous subtyping relation, written . The rationale behind asynchronous session subtyping is that under asynchronous communication it is unobservable whether or not an output is anticipated before an input, as long as this output is executed along all branches of the candidate super-type. Besides the usage of our new recursive input contexts the definition of fair asynchronous subtyping differs from those in [MariangiolaPreciness, ESOP09, CDY2014, BravettiCZ17, sefm19, BCLYZ19] in that controllability plays a fundamental role: the subtype is not required to mimic supertype inputs leading to uncontrollable behaviours.
[Fair Asynchronous Subtyping, ]
A relation on session types is a controllable subtyping relation whenever
implies:
-
(1)
if then ;
-
(2)
if then ;
-
(3)
if then , , and , where ;
-
(4)
if then and .
is a controllable subtype of if there is a controllable subtyping relation s.t. .
is a fair asynchronous subtype of , written , whenever: controllable implies that is a controllable subtype of . Notice that the top-level check for controllability in the above definition is consistent with the inner controllability checks performed in Case .
Subtyping simulation game
Session type is a fair asynchronous subtype of if is not controllable or if is a controllable subtype of . Intuitively, the above co-inductive definition says that it is possible to play a simulation game between a subtype and its supertype as follows. Case (1) says that if is the type, then must also be . Case (2) says that if is recursively defined, then is replaced by the unfolding of its definition, is left unchanged and the simulation game continues. Case (3) says that if is an input branching, then the sub-terms in that are controllable can reply by inputting at most some of the labels in the branching (contravariance of inputs), and the simulation game continues (see Example 3). Case (4) says that if is an output selection, then can reply by outputting all the labels in the selection, possibly after executing some inputs, after which the simulation game continues. We comment further on Case (4) with Example 3.
Consider and . We have . Once branch , that is uncontrollable, is removed from , we can apply contravariance for input branching. We have in Definition 3.
Consider and from Figure 1. For the pair , we apply Case (4) of Definition 3 for which we compute
with . Observe that contains a recursive sub-term, such contexts are not allowed in previous works [MariangiolaPreciness, ESOP09, CDY2014].
The use of selective unfolding makes it possible to express in terms of a recursive input context with holes filled by types (i.e., closed terms) that start with an output prefix. Indeed selective unfolding does not unfold the recursion variable (not guarded by an output selection), which becomes part of the input context . Instead it unfolds the recursion variable (which is guarded by an output selection) so that the term that fills the hole, which is required to start with an output prefix, is a closed term.
Case (4) of Definition 3 requires us to check that the following pairs are in the relation: () and () . Observe that . Hence, we have with
and is a controllable subtyping relation.
We show that fair asynchronous subtyping is sound w.r.t. fair refinement. In fact, fair asynchronous subtyping can be seen as a sound coinductive characterisation of fair refinement. Namely this result gives an operational justification to the syntactical definition of fair asynchronous session subtyping. Note that is not complete w.r.t. , see Example 3.
The proof of soundness of fair asynchronous subtyping w.r.t. fair refinement is rather complex and can be found in Appendix A.3, here we report the two main results and a sketch of their proofs.
Proposition 8.
Given two session types and , if then, for every , , and such that is a correct composition, there exist , , , and such that and .
Sketch of the proof. Given that is a correct composition, there exist , , , and such that and . The thesis is proved by induction on the length of this sequence of transitions.
If the length is 0, then , that implies , that also implies (because ), from which we have .
If the length is greater than 0, we proceed by case analysis on the first possible transition .
If the transition is inferred by it is sufficient to observe that and , and then apply the inductive hypothesis because is a correct composition in that it is reachable from a correct composition.
We now consider that the transition is inferred by .
There are three possible cases:
-
(1)
,
-
(2)
and starts with an input branching (i.e., ),
-
(3)
and starts with an output branching (i.e., ).
In the first two cases we have that the above initial transition is , for some . Given that , it is possible to show that , that , and also . Then we can apply the inductive hypothesis because and is a correct composition.
In the third case, given that , and is controllable, we have that , and with , for every . We first observe that the sequence of transitions , with , includes at least one output selection executed by one of the output selections filling the holes in . This label is the first one emitted by the l.h.s. type after it has executed input branchings in . We have that the same sequence of transitions, excluding the output of , can be executed from the configuration . Such a sequence is , with ; notice that it is shorter than the above one. We now consider . We can now apply the inductive hypothesis on the shorter sequence , because (and because it is possible to prove that is also a correct composition, see Proposition 22 in Appendix A.3). ∎
Theorem 9.
Given two session types and , if then .
Sketch of the proof. If is not controllable, then the thesis trivially holds because for every .
Consider now controllable. The thesis is proved by showing that if then, for every , , and such that is a correct composition, we have that the following holds:
if then there exists such that and is a correct composition.
The above implies the thesis because, given and the correct composition , if there exists a computation , we can apply the above result on each step of the computation to prove that there exists such that and is a correct composition. Then, by Proposition 8, we have that there exist , , , and such that and . ∎
Let and . We have , but is not a fair asynchronous subtype of since , i.e., covariance of outputs is not allowed.
3.1. Undecidability of fair asynchronous session subtyping
In this section we address the problem of checking fair asynchronous session subtyping, and we show that it is actually undecidable. We have already proved that the fair refinement relation is undecidable (Corollary 6) and that the fair asynchronous subtyping relation is a subset of the refinement relation (Theorem 9). From these results we cannot immediately conclude that fair asynchronous subtyping is also undecidable; hence we need a specific proof for this additional undecidability result. The approach we take has some commonalities with the one adopted in Section 2.3, as we also proceed by reduction from undecidability properties in queue machines. Nevertheless, there are several relevant differences. First, we consider termination in queue machines instead of state reachability. Then we need to slightly modify the encodings of both the finite control and of the queue of the considered machine. And finally, the proof of correctness of the encoding is significantly different as subtyping is defined on the syntax of types, while refinement is defined on the operational semantics of (the parallel composition of) session types.
As anticipated above, we reduce the problem of checking the (non)termination of a queue machine to the problem of checking subtyping between two session types. In Definition 2.3 we have defined denoting computation steps of a queue machine. We have that one queue machine terminates if and only if there exists a configuration with empty queue that is reachable from the initial configuration, i.e., . This holds because the transition function is total in queue machines, hence if the queue is not empty there is always a possible transition. In case the queue machine does not terminate, we have that implies the existence of an additional computation step .
Given a queue machine and an additional ending symbol , we now define the types and in such a way that does not terminate if and only if . The encodings and are similar to the corresponding encodings and defined in Definitions 2.3 and 2.3, but with the following differences:
-
•
there is no specific target state ;
-
•
the encoding starts with an input branching with only one branch labeled with the initial queue symbol and continuation corresponding to the producer/consumer as defined in Definition 2.3;
-
•
in order to be a potential subtype of , all of the output selections in must have branchings for all of the symbols in (because these are the labels in the output selection in the potential supertype); among all of these branchings only one will be consistent with the encoding of the finite control, while the continuations in the other branchings are guaranteed to be always good subtypes (this is guaranteed by a type that nondeterministically produces symbols, and that after producing the ending symbol it is able to recursively consume all possible symbols in , and then become after consuming the ending symbol ).
[New Finite Control Encoding] Let be a queue machine and let be the additional ending symbol. We define as follows:
with, given and , is defined as follows:
where
with and .
[New Producer/consumer] Let be a queue machine and be the ending symbol. We define as
with as defined in Definition 2.3.
We now prove that the above two types and are such that if and only if the machine does not terminate. We report a sketch of the proof, the details are in Appendix A.4.
Theorem 10.
Given a queue machine and the ending symbol , consider and . We have that if and only if does not terminate.
Sketch of the proof. The only-if part is proved by considering the contrapositive statement, that is, if the queue machine terminates then . If the queue machine terminates, we have that . Consider now the pair of types with and . If, by contradiction, , since is controllable (it is compliant, e.g., with its dual) we have that by Definition 3 there exists a fair asynchronous subtyping relation such that . By applying the definition of fair asynchronous subtyping relation we have that will have to include other pairs of types corresponding with configurations reachable in the queue machine . The types represent the corresponding state , while the types represent the corresponding queue . Consider now the pair of types corresponding with the final configuration : starts with an input branching (representing the willingness to consume one symbol from the queue) while starts with an output selection (in fact, the representation of the queue starts with a sequence of input branchings, one for each symbol in the queue, followed by an output selection and, given that it represents the empty queue, the initial sequence of input branching is absent). Summarising, we have that , starts with an input branching, and with an output selection: hence there is a pair in which does not satisfy the item for input selection in Definition 3, thus contradicting the initial assumption about being a fair asynchronous subtyping relation.
The if part is proved by showing that if the queue machine does not terminate then there exists a fair asynchronous subtyping relation that contains the pair , hence . There are two kinds of pairs in : (i) the pairs discussed in the above only-if part of the proof that corresponds to the path in the subtyping simulation game that reproduces the computation of the queue machine , and (ii) other pairs corresponding to alternative paths. Here, we only comment the new pairs of kind (ii). The l.h.s. types in these pairs are generated by considering the alternative branches in the types in Definition 3.1, namely those involving the types denoted with and . These types are of two kinds: (a) they are able to recursively perform all possible outputs until the label is selected (type ), or (b) they are able to recursively perform all possible inputs until the label is selected (type ). All of these pairs satisfy the constraints in Definition 3 (under the assumption that also a final pair belongs to ). Summarising, there exists a fair asynchronous subtyping relation such that in that this is the first pair of the kind (i) above. Hence we can conclude that . ∎
As a direct consequence of the above theorem and the undecidability of termination in queue machines, we can conclude that fair asynchronous subtyping (Definition 3) is also undecidable.
Corollary 11.
Given two session types and , it is in general undecidable to check whether .
4. A Sound Algorithm for Fair Asynchronous Subtyping
We propose an algorithm which soundly verifies whether a session type is a fair asynchronous subtype of another. The algorithm relies on building a tree whose nodes are labelled by configurations of the simulation game induced by Definition 3. The algorithm analyses the tree to identify witness subtrees which contain input contexts that are growing following a recognisable pattern.
Recall the satellite communication example (Figure 1). The spacecraft with protocol may be a replacement for an older generation of spacecraft which follows the more complicated protocol , see Figure 3. Type notably allows the reception of telecommands to be interleaved with the emission of telemetries. The new spacecraft may safely replace the old one because .
However, checking leads to an infinite accumulation of input contexts, hence it requires to consider infinitely many pairs of session types. E.g., after selects the output label twice, the subtyping simulation game considers the pair , where is given in Figure 3. The pairs generated for this example illustrate a common recognisable pattern where some branches grow infinitely (the -branch), while others stay stable throughout the derivation (the -branch). The crux of our algorithm is to use a finite parametric characterisation of the infinitely many pairs occurring in the check of .
The simulation tree for , written , is the labelled tree representing the simulation game for , i.e., is a tuple where is its set of nodes, is its root, is its transition relation, and is its labelling function, such that . We omit the formal definition of , as it is straightforward from Definition 3 following the subtyping simulation game discussed after that definition. We give an example below.
Notice that the simulation tree is defined only when is controllable, since holds without needing to play the subtyping simulation game if is not controllable. We say that a branch of is successful if it is infinite or if it finishes in a leaf labelled by . All other branches are unsuccessful. Under the assumption that is controllable, we have that all branches of are successful if and only if . As a consequence checking whether all branches of are successful is generally undecidable. It is possible to identify a branch as successful if it visits finitely many pairs (or node labels), see Example 3; but in general a branch may generate infinitely many pairs, see Examples 4 and 4.
|
In order to support types that generate unbounded accumulation, we characterise finite subtrees — called witness subtrees, see Definition 4 — such that all the branches that traverse these finite subtrees are guaranteed to be successful.
Notation
We give a few auxiliary definitions and notations. Hereafter and range over extended input contexts, i.e., input contexts that may contain distinct holes with the same index. These are needed to deal with unfoldings of input contexts, see Example 4.
The set of reductions of an input context is the minimal set s.t. {enumerate*}[label=()]
;
if then and
if then . Notice that due to unfolding (item 4), the reductions of an input context may contain extended input contexts. Moreover, given a reduction of , we have that .
Consider the following extended input contexts: {mathpar} A_1 = μt . &{ l_1 : [ ]^1, l_2 : &{ l_3 : t } }
A_2 = &{ l_3 : μt . &{ l_1 : [ ]^1, l_2 : &{ l_3 : t } } }
unfold(A_1) = &{ l_1 : [ ]^1, l_2 : &{ l_3 : μt . &{ l_1 : [ ]^1, l_2 : &{ l_3 : t } } } } Context is a reduction of , i.e., one can reach from , by unfolding and executing the input . Context is also a reduction of . Observe that contains two distinct holes indexed by .
Given an extended context and a set of hole indices such that , we use the following shorthands. Given a type for each , we write for the extended context obtained by replacing each hole in by . Also, given an extended context we write for the extended context obtained by replacing each hole in by . When , we often omit and write, e.g., and .
Using the above notation and posing , we can rewrite (Figure 3) as .
Consider the session type below
Posing we have . Assuming and , we can rewrite as .
Figure 4 shows the partial simulation tree for , from Figures 1 and 3 (ignore the dashed edges for now). Notice how the branch leading to the top part of the tree visits only finitely many node labels (see dotted box), however the bottom part of the tree generates infinitely many labels, see the path along the transitions in the dashed box.
Witness subtrees
Next, we define witness trees which are finite subtrees of a simulation tree which we prove to be successful. The role of the witness subtree is to identify branches that satisfy a certain accumulation pattern. It detects an input context whose holes fall in two categories: () growing holes (indexed by indices in below) which lead to an infinite growth and () constant holes (indexed by indices in below) which stay stable throughout the simulation game. The definition of witness trees relies on the notion of ancestor of a node , which is a node (different from ) on the path from the root to . We illustrate witness trees with Figure 4 and Example 4. {defi}[Witness Tree] A finite tree is a witness tree for , such that , with and , if all the following conditions are satisfied:
-
(1)
for all either or
, where is a reduction of , and it holds that-
•
implies that is a leaf and
-
•
if and is not a leaf then starts with an output selection;
-
•
- (2)
Intuitively Condition (1) says that a witness subtree consists of nodes that are labelled by pairs where contains a fixed context (or a reduction/repetition thereof) whose holes are partitioned in growing holes () and constant holes (). Whenever all growing holes have been removed from a pair (by reduction of the context) then this means that the pair is labelling a leaf of the tree. In addition, if the initial input is limited to only one instance of , the l.h.s. type starts with an output selection so that this input cannot be consumed in the subtyping simulation game.
Condition 2 says that all leaves of the tree must validate certain conditions from which we can infer that their continuations in the full simulation tree lead to successful branches. Leaves satisfying Condition (2a) straightforwardly lead to successful branches as the subtyping simulation game, starting from the corresponding pair, has been already checked starting from its ancestor having the same label. Leaves satisfying Condition (2b) lead to an infinite but regular “increase” of the types in -indexed holes — following the same pattern of accumulation from their ancestor. The next two kinds of leaves must additionally satisfy the subtyping relation — using witness trees inductively or based on the fact they generate finitely many labels. Leaves satisfying Condition (2c) lead to regular “decrease” of the types in -indexed holes — following the same pattern of reduction from their ancestor. Leaves satisfying Condition (2d) use only constant -indexed holes because, by reduction of the context , the growing holes containing the accumulation have been removed.
Remark 12.
In the tree of Figure 4 we highlight two subtrees. The subtree in the dotted box is not a witness subtree because it does not validate Condition (1) of Definition 4, i.e., there is an intermediary node with a label in which the r.h.s type does not contain .
The subtree in the dashed box is a witness subtree with 3 leaves, where the dashed edges represent the ancestor relation, , and . We comment on the leaves clockwise, starting from , which satisfies Condition (2d). The next leaf satisfies condition (2c), while the final leaf satisfies Condition (2b).
Algorithm
Given two session types and we first check whether is uncontrollable. If this is the case we immediately conclude that . Otherwise, we proceed in four steps.
-
S1
We compute a finite fragment of , stopping whenever () we encounter a leaf (successful or not), () we encounter a node that has an ancestor as defined in Definition 4 (Conditions (2a), (2b), and (2c)), () or the length of the path from the root of to the current node exceeds a bound set to two times the depth of the AST of . This bound allows the algorithm to explore paths that will traverse the super-type at least twice. We have empirically confirmed that it is sufficient for all examples mentioned in Section 5.
-
S2
We remove subtrees from the tree produced in S1 corresponding to successful branches of the simulation game which contain finitely many labels. Concretely, we remove each subtree whose each leaf is either successful or has an ancestor such that is in the same subtree and .
-
S3
We extract subtrees from the tree produced in S2 that are potential candidates to be subsequently checked. The extraction of these finite candidate subtrees is done by identifying the forest of subtrees rooted in ancestor nodes which do not have ancestors themselves.
-
S4
We check that each of the candidate subtrees from S3 is a witness tree.
If an unsuccessful leaf is found in S1, then the considered session types are not related. In S1, if the generation of the subtree reached the bound before reaching an ancestor or a leaf, then the algorithm is unable to give a decisive verdict, i.e., the result is unknown. Otherwise, if all checks in S4 succeed then the session types are in the fair asynchronous subtyping relation. In all other cases, the result is unknown because a candidate subtree is not a witness.
We illustrate the algorithm above with the tree in Figure 4. After S1, we obtain the whole tree in the figure (11 nodes). After S2, all nodes in the dotted boxed are removed. After S3 we obtain the (unique) candidate subtree contained in the dashed box. This subtree is identified as a witness subtree in S4, hence we have .
Soundness of the algorithm
The soundness of our algorithm w.r.t. fair asynchronous session subtyping relies on proving that given a witness tree such that , then . We formalize this in Theorem 15 further down below.
The definition of witness tree consider nestings of input contexts . In the proof of Theorem 15 we need the notation , to generalize to nestings of input contexts with parametric depth, defined as follows:
-
•
is
-
•
is , when .
Given a witness tree for , we define a family of isomorphic trees with labels in which the r.h.s. type has incrementally increased nestings of the input context in the growing holes.
[-th Witness Tree]Given a witness tree for , and , we inductively define as follows:
-
•
;
-
•
for , given we define with , , , and
if .
We now present a preliminary Lemma stating that, given a witness subtree of a simulation tree, all the trees in the family faithfully represent the subtyping simulation game (proof in Appendix A.5).
Lemma 13.
Consider a witness tree contained in a simulation tree. For every , we have that in is compatible with the subtyping simulation game, i.e., is present in if and only if there exists a simulation tree including with and .
We now move to a proposition stating that, given a witness subtree of a simulation tree, we have that all branches in the simulation tree that traverse follows paths also present in the family of trees or in simulation trees where is a leaf of for which we know that (proof in Appendix A.5). In the statement of this proposition we use to denote the reflexive and transitive closure of .
Proposition 14.
Let and be two session types with . If contains a witness tree with root , then for every node such that we have that is a label present either in , for some , or in with .
We can now present the main result needed to prove the soundness of our algorithm.
Theorem 15.
Let and be session types s.t. . If contains a witness subtree with root then for every node s.t. , either is a successful leaf, or there exists s.t. .
In the light of this last theorem, we can finally conclude that if the candidate subtrees of identified with the steps S1-3 explained above are also witness subtrees (check done in the step S4), then we have .
5. Implementation
To evaluate our algorithm, we have produced a Haskell implementation of it, which is available on GitHub [tool]. It implements a version of the algorithm presented in Section 4, which internally represents session types as automata (LTS) (see, e.g., [BravettiZ21]). In this context it is also natural to use bisimulation in place of the syntactic equality for session types. These design choices helped us to concretise an implementation of the algorithm in Section 4 and allowed us to implement an optimisation which minimises the input types. We comment on this below.
Using automata internally makes it easier to identify candidate input contexts as we can keep track of states that correspond to the input context computed when applying Case (4) of Definition 3. In particular, we augment each local state in the automata representation of the candidate supertype with two counters: the -counter keeps track of how many times a state has been used in an input context; the -counter keeps track of how many times a state has occurred within a hole of an input context. We illustrate this with Figure 5 which depicts the internal data structures our tool manipulates when checking from Figures 1 and 3. The state indices of the automata in Figure 5 correspond to the ones in Figure 1 (2nd column) and Figure 3 (3rd column).
The first row of Figure 5 represents the root of the simulation tree, where both session types are in their respective initial state and no transition has been executed. We use state labels of the form where is the original identity of the state, is the value of the -counter, and is the value of the -counter. The second row depicts the configuration after firing transition , via Case (4) of Definition 3. While the candidate subtype remains in state (due to a self-loop) the candidate supertype is unfolded with (Definition 3). The resulting automaton contains an additional state and two transitions. All previously existing states have their -counter incremented, while the new state has its -counter incremented. The third row of the figure shows the configuration after firing transition , using Case (4) of Definition 3 again. In this step, another copy of state is added. Its -counter is set to since this state has been used in a context twice; and the -counters of all other states are incremented.
Using this representation, we construct a candidate input context by building a tree whose root is a state such that . The nodes of the tree are taken from the states reachable from , stopping when a state such that is found. A leaf becomes a hole of the input context. The hole is a constant () hole when , and growing () otherwise. Given this strategy and the configurations in Figure 5, we successfully identify the context with and .
Thanks to our automata representation, it is also possible to minimise (up-to bisimulation) each session-type automaton before performing Steps S1-S4. Concretely our tool accepts an optional command-line flag that turns on the minimisation of each session type after it has been transformed into an automaton. We discuss the benefits of this optimisation in the next section.
Last transition | State of | Representation of |
---|---|---|
We have run our tool on a dozen of examples handcrafted to test the limits of our algorithm (inc. the examples discussed in this paper), as well as on the 174 tests taken from [BCLYZ19]. All of these tests terminate under a second.
Additionally, for debugging and illustration purposes, the tool can optionally generate graphical representations of the subtyping simulation game and of witness trees.
6. Empirical Evaluation on Synthetic Benchmarks
To evaluate the cost of our algorithm and its implementation, wrt. runtime and memory usage, we have performed an empirical evaluation based on a family of pairs of sub/supertype of increasing sizes. We perform our evaluation with and without our minimisation-based optimisation and discuss the results.
Experimental setup
The family of types we consider is based on variants from our spacecraft example: the subtype is based on variants of in Figure 1, while the supertype is based on variants of in Figure 3. The shape and size of each variant is determined by three parameters which respectively affect the number of choices in branches (branching width), the number of inputs that can be accumulated in the supertype (input depth), and the number of choices in selections (selection width).
Given values , , and for each of these parameters, we generate a subtyping problem as described in Figure 6. We assume that , , and — the branching/selection parameters need to provide at least one branch, while input depth could be zero (no anticipation). Each test applies our algorithm to verify that is a fair asynchronous subtype of (by construction the test always succeeds).
We describe Figure 6 in more details. The subtype only depends on two parameters: branching width () and selection width (). It is similar to in Figure 1 except that it can send (resp. receive) different telemetry (resp. telecommand) messages. It is a recursive type that immediately chooses between sending one of the telemetries () then recurse, or send a termination signal (). In the latter case, the behaviour continues with , i.e., another recursive definition followed by a branching construct where the type expects to receive either one of the telecommands () then recurse, or receive the termination signal .
The supertype depends on three parameters: branching width (), input depth (), and selection width (). This type is similar to in Figure 3 but can send (resp. receive) different telemetry (resp. telecommand) messages and allows the reception of telecommands to precede the emission of a telemetry message. relies on four additional definitions. encodes the sequence of inputs that can precede the emission of telemetries. performs the selections that precede the final series of inputs in . performs the final series of outputs.
Figure 7 gives a graphical representation of the session-type automata generated by the definitions in Figure 6 after minimisation up to bisimulation. The figure shows a subtype (left) that can send four different messages (, then can receive two different messages (). The state labels correspond to the ones of in Figure 1.
The supertype (right) is more complex. It can also send four different messages (, and receive two different messages (). Additionally, it may postpone the emission of telemetries and receive up to telecommands first (). The state labels correspond to the ones of in Figure 3. Note that because of minimisation the two final states of are merged into their counterpart in Figure 7. Since the emission of in is further postponed compared to , we also obtain several variants of state , labelled by and highlighted in gray in Figure 7.
Experimental results
Figures 10, 10, and 10 give the results of running the implementation of our algorithm on increasingly large instances of the subtyping problem . Each figure shows the runtime (larger data points in blue, left y-axis) and peak memory usage (smaller data points in red, right y-axis) for each instance of the problem. Each figure includes two x-axes: the bottom one represents the number of transitions in the automata representation of the candidate supertype (which we consider a good measure of the size of the subtyping problem); the top one represents the value of the variable parameter for each experiment (e.g., branching with). Plots on the left show the result without minimisation, plots on the right show results using minimisation up to bisimulation. Each figure depicts 20 data points unless our implementation timed out (more than 300 seconds). The yellow curve highlights the runtime trend. It is computed using SciPy’s curve_fit function.
All the benchmarks in this paper were run on a MacBook Pro with an Intel i5 CPU with 16GB RAM running macOS 13.4. The time was measured by taking the difference between the system clock before and after our tool was invoked. The memory usage refers to the maximum resident set size as reported by the /usr/bin/time -l command. Each test was ran 3 times, the plots report the average time (resp. memory) measurements. All our test data and infrastructure are available on our GitHub repository [tool].
Figure 10 shows the result of checking , with (branching width) increasing by step of , from to . The left-hand side plot shows that the tool quickly runs out of resource without optimisation: only terminate in reasonable time. While the asymptotic cost of the algorithm with minimised automata is still exponential, the tool can deal with much larger input using this optimisation as show on the right.
Figure 10 shows the result of checking , with (input depth) increasing by step of , from to ( data points). Observe that minimisation nearly halves the number of transitions in the candidate supertypes. As a consequence, the version of the tool that minimises its input before applying the subtyping algorithm runs much faster and uses much less memory than its non-optimised counterpart.
Figure 10 shows the result of checking , with (selection width) increasing by step of , from to ( data points). In this case minimisation has a lesser effect on the number of transitions in the candidate supertypes, but it has still a significant effect on runtime, e.g., the largest problem takes 20s on the minimised automata and 37s on the non-minimised ones.
7. Related and Future Work
Related work
The relationship between refinement and subtyping in the context of synchronous session types has been thoroughly investigated both for binary and multiparty session types. For instance, Bernardi and Hennessy [BernardiH16] establish a correspondence between binary session subtyping and an observational preorder on session types interpreted as contracts. A similar result has been obtained in the context of multiparty session types by Severi and Dezani-Ciancaglini [SeveriD19], where the subtyping is dubbed structural preorder, while the refinement is named observational preorder. Concerning asynchronous communication we can mention previous works on refinement for asynchronous communication by some of the authors of this paper. The work in [wsfm08] also considers fair compliance, however here we consider binary (instead of multiparty) communication and we use a unique input queue for all incoming messages instead of distinct named input channels. Moreover, in the present paper we provide a sound characterisation of fair refinement using coinductive subtyping and provide a sound algorithm and its implementation. In [sefm19, BravettiZ21] the asynchronous subtyping of [MY15] is used to characterise refinement for a notion of correct composition based on the impossibility to reach a deadlock, instead of the possibility to reach a final successful configuration as done in the present paper. The refinement from [sefm19] does not support examples such as those in Figure 1.
Concerning fairness in the context of session types, Padovani studied a notion of fair subtyping for synchronous multi-party session types in [Padovani16]. This work notably considers the notion of viability which corresponds, in the synchronous multiparty setting, to our notion of controllability. We use the term controllability instead of viability following the tradition of service contract theories like those based on Petri nets [Loh08, Wei08] or process calculi [BZ09a]. Compared to [Padovani16], asynchronous communication makes it much more involved to prove soundness and completeness of the decidable characterisation of controllability, as we do in this paper. Indeed in the asynchronous case, transition systems arising from the communication of two types are, in general, infinite state (due to unbounded queues), while they are always finite state in the synchronous case. Fair refinement in [Padovani16] is characterised by defining a coinductive relation on normal form of types, obtained by removing inputs leading to uncontrollable continuations. Instead of using normal forms, we remove these inputs during the asynchronous subtyping check. A limited form of variance on output is also admitted in [Padovani16]. Covariance between the outputs of a subtype and those of a supertype is possible when the additional branches in the supertype are not needed to have compliance with potential partners. In [Padovani16] this check is made possible by exploiting a difference operation [Padovani16, Definition 3.15] on types, which synthesises a new type representing branches of one type that are absent in the other. We observe that the same approach cannot work to introduce variance on outputs in an asynchronous setting. Indeed the interplay between output anticipation and recursion could generate differences in the branches of a subtype and a supertype that cannot be statically represented by a (finite) session type.
Padovani also studied an alternative notion of fair synchronous subtyping in [Padovani13]. Although the contribution of that paper refers to session types, the formal framework therein seems to deviate from the usual session type approach. In particular, it considers shared channel communication instead of binary channels: when a partner emits a message, it is possible to have a race among several potential receivers for consuming it. As a consequence of this alternative semantics, the subtyping in [Padovani13] does not admit variance on input. Another difference with respect to session type literature is the notion of success among interacting sessions: a composition of session is successful if at least one participant reaches an internal successful state. This approach has commonalities with testing [DH84], where only the test composed with the system under test is expected to succeed, but differs from the typical notion of success considered for session types. In [Barbanerad10, BernardiH16] (resp. [MariangiolaPreciness]) it was proved that the Gay-Hole synchronous session subtyping (resp. orphan message free asynchronous subtyping) coincides with refinement induced by a successful termination notion requiring interacting processes to be both in the state (with empty buffers, in the asynchronous case).
More recently, van Glabbeek et al. [GlabbeekHH21] introduce a type system for multiparty sessions that assumes fairness. Nevertheless, the notion of fairness used in that paper is different with respect to the notion considered by Padovani [Padovani16] (in the synchronous case) and in this paper (in the asynchronous case). In fact, in [GlabbeekHH21] weak fairness is considered, consisting of a minimal fairness assumption that “guarantees only that concurrent transitions cannot prevent each other from happening”. On the other hand, Padovani [Padovani16] and ourselves consider a stronger notion of fairness, namely, according to the terminology in [GlabbeekH19], we consider the composition of two session types correct if their successful termination is a liveness property which holds under the assumption of full fairness. In [GlabbeekH19] it is proved that, for finite state transition systems, full fairness collapses to strong fairness of transitions, i.e., a transition which is (relentlessly) enabled infinitely many times during a computation, it is also executed infinitely often in such computation. Session types are finite states, but we consider asynchronous communication via unbounded FIFO buffers, hence our transition system (Definition 2.2) describing the composition of two session types is not finite because buffers can store an unbounded amount of messages. On the contrary, in the context of synchronous communication the transition system describing the composition of two session types is finite state, hence the above correspondence result between full fairness and strong fairness applies. A strong fair session subtyping has been recently used in a type system that guarantees fair termination of sessions for a -calculus like language with binary sessions [CicconeP22]. The subtype defined in that paper differs from previous strong fair subtypings because it also deals with higher-order types (useful to type process languages including primitives for session creation and delegation) and because it is only sound but not complete w.r.t. fair session type refinement. More precisely, it is complete only for bounded processes and it does not capture subtypes like those discussed in Example 3, where the supertype has an uncontrollable (infinite) branch.
Several variants of asynchronous session subtyping have been proposed in [ESOP09, MariangiolaPreciness, CDY2014, MY15, GhilezanPPSY21] and further studied in our earlier work [BravettiCZ17, BravettiCLYZ21, sefm19, BCLYZ19]. All these variants have been shown to be undecidable [BCZ18, LY17, BravettiCZ17]. Moreover, all these subtyping relations are (implicitly) based on an unfair notion of compliance. Some of these papers consider binary session types [MariangiolaPreciness, CDY2014, MY15] as we do in this paper. An interesting technical difference with these papers is that they use finite input contexts (i.e. without recursion) while we also consider infinite input contexts which may contain recursion — this is necessary to obtain and (see Figures 1 and 3). Moreover, the papers [MariangiolaPreciness, CDY2014] impose additional constraints in the definition of asynchronous subtyping to guarantee absence of orphan-messages. Such constraints require the subtype not to have output loops whenever an output anticipation is performed, thus guaranteeing that at least one input is performed in all possible paths. In this paper, absence of orphan messages between compatible types is guaranteed as successful termination is enforced under the assumption of full-fairness. Notice that not imposing this orphan-message-free constraint is consistent with our recursive input contexts that allows for input loops in the supertype whenever an output anticipation is performed. The other papers [ESOP09, GhilezanPPSY21] consider asynchronous subtyping for multiparty session types. In the binary case, a subtype can only anticipate (under some specific conditions) outputs w.r.t input. In the multiparty context additional differences are allowed, for instance, a subtype can anticipate also an input w.r.t. other inputs of messages coming from other partners. Intuitively, this is possible because in the considered operational model messages coming from different partners are stored in distinct message queues. A difference between [ESOP09] and [GhilezanPPSY21] is that the former concentrates on deadlock freedom, while the latter considers also orphan message freedom. Notably, the subtyping in [GhilezanPPSY21] is proved to be precise (i.e. sound and complete), w.r.t. a notion of refinement that preserves orphan message freedom, deadlocks, and starvation, for a -calculus like language with multiparty sessions.
In [BCLYZ19, BravettiCLYZ21], we proposed a sound algorithm for the (unfair) asynchronous subtyping in [MariangiolaPreciness]. The sound algorithm that we present in this paper substantially differs from that of [BCLYZ19, BravettiCLYZ21]. Here we use witness trees that take under consideration both increasing and decreasing of accumulated input. In [BCLYZ19, BravettiCLYZ21], instead, only regular growing accumulation is considered. It is worth mentioning that in the context of multiparty session types there exist alternative sound (but not complete) algorithmic approaches. In particular, in [DagninoGD23] a multiparty approach is adopted: they study properties of networks of communicating end-point types instead of studying a subtyping relation on binary session types in isolation, as we do in this paper. A first phase of their algorithm infers global types from networks, and a second phase checks the well formedness of the inferred global types. Using techniques similar to ours (i.e. reduction from queue machines) well formedness is proved to be undecidable, but a sound algorithmic characterisation is proposed which is based on the notion of balancing. The authors of that paper show that, following their approach, one of the examples not captured by the algorithm in [BCLYZ19, BravettiCLYZ21] can be managed.
Finally, we mention work about refinement/subtyping in the context of asynchronous multiparty sessions, where the use of global types allows for the definition of decidable type systems. More precisely, both Castellani et al. [CastellaniDG21] and Li et al. [LiSW24] study a notion of refinement for (asynchronous) multiparty session types that ensures that the implementation of a given role can be replaced by another in the context of a specific global type. This means that the relation considers not only the component being refined, but also the other components of the system. Unlike most subtyping relation for asynchronous session types, this relation is decidable — this is notably due to the relation being restricted to the specific context of a given global type.
Future work
In future work, we will investigate the possibility to characterize a notion of fair asynchronous session subtyping which is complete with respect to our notion of fair refinement, in particular, we are interested in a less restrictive subtyping which includes also some form of output variance. We also plan to lift our study of fairness from binary to multiparty session types; in fact, the notions of fair compliance and refinement extend naturally to several partners. Finally, we will investigate a more refined termination condition for our algorithm using ideas from [BravettiCLYZ21, Theorem 3.8]. In particular, we plan to identify conditions similar to those in Definition 4 such that it is always guaranteed to find, during the computation of each branch of the simulation tree, a node with an ancestor satisfying such conditions. Then, the initial phase of the algorithm dedicated to the identification of the candidate subtrees can terminate when such nodes are detected, and the subsequent phase will continue to check whether such candidate subtrees are also witness subtrees.
Acknowledgments
We thank the anonymous reviewers for their valuable feedback and insightful suggestions, which have improved the quality of this work.
References
- [Ada17] Adam Wiggins. The Twelve Factor methodology. https://12factor.net, 2017.
- [BCL+19] Mario Bravetti, Marco Carbone, Julien Lange, Nobuko Yoshida, and Gianluigi Zavattaro. A sound algorithm for asynchronous session subtyping. In CONCUR, volume 140 of LIPIcs, pages 38:1–38:16. Schloss Dagstuhl - Leibniz-Zentrum für Informatik, 2019.
- [BCL+21] Mario Bravetti, Marco Carbone, Julien Lange, Nobuko Yoshida, and Gianluigi Zavattaro. A sound algorithm for asynchronous session subtyping and its implementation. Log. Methods Comput. Sci., 17(1), 2021. URL: https://lmcs.episciences.org/7238.
- [BCZ17] Mario Bravetti, Marco Carbone, and Gianluigi Zavattaro. Undecidability of asynchronous session subtyping. Inf. Comput., 256:300–320, 2017.
- [BCZ18] Mario Bravetti, Marco Carbone, and Gianluigi Zavattaro. On the boundary between decidability and undecidability of asynchronous session subtyping. Theor. Comput. Sci., 722:19–51, 2018.
- [Bd10] Franco Barbanera and Ugo de’Liguoro. Two notions of sub-behaviour for session-based client/server systems. In PPDP’10, pages 155–164. ACM, 2010.
- [BEJQ18] Ahmed Bouajjani, Constantin Enea, Kailiang Ji, and Shaz Qadeer. On the completeness of verifying message passing programs under bounded asynchrony. In CAV (2), volume 10982 of Lecture Notes in Computer Science, pages 372–391. Springer, 2018.
- [BH16] Giovanni Tito Bernardi and Matthew Hennessy. Modelling session types using contracts. Mathematical Structures in Computer Science, 26(3):510–560, 2016.
- [BLZ21] Mario Bravetti, Julien Lange, and Gianluigi Zavattaro. Fair refinement for asynchronous session types. In Stefan Kiefer and Christine Tasson, editors, Proc. FOSSACS 2021, volume 12650 of Lecture Notes in Computer Science, pages 144–163. Springer, 2021. doi:10.1007/978-3-030-71995-1\_8.
- [BZ83] Daniel Brand and Pitro Zafiropulo. On communicating finite-state machines. J. ACM, 30(2):323–342, 1983.
- [BZ08a] Mario Bravetti and Gianluigi Zavattaro. Contract Compliance and Choreography Conformance in the Presence of Message Queues. In WS-FM’08, volume 5387 of Lecture Notes in Computer Science, pages 37–54. Springer, 2008.
- [BZ08b] Mario Bravetti and Gianluigi Zavattaro. A foundational theory of contracts for multi-party service composition. Fundam. Inform., 89(4):451–478, 2008. URL: http://content.iospress.com/articles/fundamenta-informaticae/fi89-4-05.
- [BZ09] Mario Bravetti and Gianluigi Zavattaro. A theory of contracts for strong service compliance. Math. Struct. Comput. Sci., 19(3):601–638, 2009. doi:10.1017/S0960129509007658.
- [BZ19] Mario Bravetti and Gianluigi Zavattaro. Relating session types and behavioural contracts: The asynchronous case. In SEFM, volume 11724 of Lecture Notes in Computer Science, pages 29–47. Springer, 2019.
- [BZ21] Mario Bravetti and Gianluigi Zavattaro. Asynchronous session subtyping as communicating automata refinement. Softw. Syst. Model., 20(2):311–333, 2021. doi:10.1007/s10270-020-00838-x.
- [CDCY14] Tzu-Chun Chen, Mariangiola Dezani-Ciancaglini, and Nobuko Yoshida. On the preciseness of subtyping in session types. In PPDP 2014, pages 146–135. ACM Press, 2014.
- [CDG21] Ilaria Castellani, Mariangiola Dezani-Ciancaglini, and Paola Giannini. Global types and event structure semantics for asynchronous multiparty sessions. CoRR, abs/2102.00865, 2021. URL: https://arxiv.org/abs/2102.00865, arXiv:2102.00865.
- [CDSY17] Tzu-Chun Chen, Mariangiola Dezani-Ciancaglini, Alceste Scalas, and Nobuko Yoshida. On the preciseness of subtyping in session types. Logical Methods in Computer Science, 13(2), 2017.
- [CP22] Luca Ciccone and Luca Padovani. Fair termination of binary sessions. Proc. ACM Program. Lang., 6(POPL):1–30, 2022. doi:10.1145/3498666.
- [DGD23] Francesco Dagnino, Paola Giannini, and Mariangiola Dezani-Ciancaglini. Deconfined global types for asynchronous sessions. Log. Methods Comput. Sci., 19(1), 2023. doi:10.46298/LMCS-19(1:3)2023.
- [DY13] Pierre-Malo Deniélou and Nobuko Yoshida. Multiparty compatibility in communicating automata: Characterisation and synthesis of global session types. In Proc. ICALP 2013, volume 7966 of Lecture Notes in Computer Science, pages 174–186, 2013. doi:10.1007/978-3-642-39212-2\_18.
- [GH05] Simon J. Gay and Malcolm Hole. Subtyping for session types in the pi calculus. Acta Inf., 42(2-3):191–225, 2005. doi:10.1007/s00236-005-0177-z.
- [GKM06] Blaise Genest, Dietrich Kuske, and Anca Muscholl. A Kleene theorem and model checking algorithms for existentially bounded communicating automata. Inf. Comput., 204(6):920–956, 2006. doi:10.1016/j.ic.2006.01.005.
- [GKM07] Blaise Genest, Dietrich Kuske, and Anca Muscholl. On communicating automata with bounded channels. Fundam. Inform., 80(1-3):147–167, 2007. URL: http://content.iospress.com/articles/fundamenta-informaticae/fi80-1-3-09.
- [GPP+21] Silvia Ghilezan, Jovanka Pantovic, Ivan Prokic, Alceste Scalas, and Nobuko Yoshida. Precise subtyping for asynchronous multiparty sessions. Proc. ACM Program. Lang., 5(POPL):1–28, 2021. doi:10.1145/3434297.
- [HYC16] Kohei Honda, Nobuko Yoshida, and Marco Carbone. Multiparty asynchronous session types. J. ACM, 63(1):9, 2016. doi:10.1145/2827695.
- [Loh08] Niels Lohmann. Why does my service have no partners? In WS-FM, volume 5387 of Lecture Notes in Computer Science, pages 191–206. Springer, 2008.
- [LSW24] Elaine Li, Felix Stutz, and Thomas Wies. Deciding subtyping for asynchronous multiparty sessions. In ESOP (1), volume 14576 of Lecture Notes in Computer Science, pages 176–205. Springer, 2024.
- [LY17] Julien Lange and Nobuko Yoshida. On the undecidability of asynchronous session subtyping. In FOSSACS’17, volume 10203 of Lecture Notes in Computer Science, pages 441–457, 2017.
- [LY19] Julien Lange and Nobuko Yoshida. Verifying asynchronous interactions via communicating session automata. In CAV (1), volume 11561 of Lecture Notes in Computer Science, pages 97–117. Springer, 2019.
- [MY15] Dimitris Mostrous and Nobuko Yoshida. Session typing and asynchronous subtyping for the higher-order -calculus. Inf. Comput., 241:227–263, 2015. doi:10.1016/j.ic.2015.02.002.
- [MYH09] Dimitris Mostrous, Nobuko Yoshida, and Kohei Honda. Global principal typing in partially commutative asynchronous sessions. In ESOP, volume 5502 of Lecture Notes in Computer Science, pages 316–332. Springer, 2009.
- [NH84] Rocco De Nicola and Matthew Hennessy. Testing Equivalences for Processes. Theoretical Computer Science, 34:83–133, 1984.
- [Pad13] Luca Padovani. Fair subtyping for open session types. In ICALP, volume 7966 of Lecture Notes in Computer Science, pages 373–384. Springer, 2013.
- [Pad16] Luca Padovani. Fair subtyping for multi-party session types. Math. Struct. Comput. Sci., 26(3):424–464, 2016.
- [RV07] Arend Rensink and Walter Vogler. Fair testing. Inf. Comput., 205(2):125–198, 2007. doi:10.1016/j.ic.2006.06.002.
- [SD19] Paula Severi and Mariangiola Dezani-Ciancaglini. Observational equivalence for multiparty sessions. Fundam. Informaticae, 170(1-3):267–305, 2019. doi:10.3233/FI-2019-1863.
- [The20] The Authors. Fair refinement for asynchronous session types. https://github.com/julien-lange/fair-asynchronous-subtyping, 2020.
- [vGH19] Rob van Glabbeek and Peter Höfner. Progress, justness, and fairness. ACM Comput. Surv., 52(4):69:1–69:38, 2019.
- [vGHH21] Rob van Glabbeek, Peter Höfner, and Ross Horne. Assuming just enough fairness to make session types complete for lock-freedom. In 36th Annual ACM/IEEE Symposium on Logic in Computer Science, LICS 2021, Rome, Italy, June 29 - July 2, 2021, pages 1–13. IEEE, 2021. doi:10.1109/LICS52264.2021.9470531.
- [Wei08] Daniela Weinberg. Efficient controllability analysis of open nets. In WS-FM, volume 5387 of Lecture Notes in Computer Science, pages 224–239. Springer, 2008.
Appendix A Proofs
A.1. Undecidability of Fair Refinement
Let and ; we have that if and only if is reachable in . To prove this, we first characterize the set of types that are compliant with .
See 3
Proof A.1.
Let .
We first prove the if part. Let be a session type with input/output labels in s.t. . We now prove that is compliant with . It is trivial to see that is compliant with ; this holds because in the configuration the two parties alternate inputs and outputs in such a way that their buffers have maximal length 1, and moreover the possibility to successfully terminate by selecting the ending label is never disallowed. By Corollary 2 we have that also all types are compliant with .
We now move to the only-if part. Let be a session type with input/output labels in s.t. is compliant with , i.e., is a correct composition. We have that cannot start with an output selection; in fact, if, for instance, it starts with an output selection and it selects any label , the type can select a branch with a different label , thus blocking. The initial input branching of must have branchings labeled with all the symbols in plus the ending symbol , in that these are the labels that can be initially selected by . In each continuation of , the unfolding of the type should start with an output selection, otherwise the entire system is blocked in that the continuation of after the initial output selection starts with an input branching. Moreover, given that these input branchings of the continuation of have only the initially selected label, the output selection in the continuation of can have only such label. After each of these output selections of the continuation of , the same reasoning can be applied, excluding the case in which the label was initially selected. In this case, the continuation of should be such that its unfolding is . This because, the continuation of becomes after executing the input branching labeled with . These constraints that we have just proved holding for the type guarantee that .
In order to prove the undecidability of refinement, we first show that is compliant with if and only if is reachable in .
See 4
Proof A.2.
Consider the queue machine , the types and and the initial configuration . The first transition is .
We now define a partial mapping function from configurations (reachable from the initial configuration ) to configurations in the queue machine computation:
-
•
where
-
–
if starts with an input branching, or if starts with an output selection with unique label ,
-
–
the operator stands for concatenation, and
-
–
and is the reverse of .
-
–
Notice that is defined and it coincides with the initial configuration of the queue computation . In the following we use the following notation:
-
•
if
-
–
and
-
–
all intermediary traversed configurations are not in the domain of the partial mapping function .
-
–
Given that, excluding the final state , for each state of the queue machine reproduces the dequeue/enqueue actions of state and is a simple forwarder that repeatedly produces and consumes the same labels, we have that given we have if and only if with and .
We now prove the only-if part of the theorem. Assume that is compliant with . This means that there exists a computation leading to the final successful configuration. The unique occurrence of is inside the type , hence we have thus implying that state is reachable in .
We now prove the if part. Assume that is reachable in . Consider . There are two possible cases: either (i) it is possible to extend the sequence of transitions as follows , for some state , (ii) or during the sequence of transitions a configuration is traversed in which the l.h.s. type is .
In the first case (i), we have that ; moreover, in this computation of the queue machine the state is not traversed. This means that such a queue machine computation can be extended to reach , hence the sequence of transitions can be additionally extended to reach a configuration where the l.h.s. type is . From such a configuration, we have that there are only finitely many transitions leading to the final successful configuration (in this final transitions both the queues are emptied and both types become ).
In the second case (ii), we have that a configuration whose l.h.s. type is . As just observed, this means that the configuration is an intermediary configuration in the final sequence of transitions leading to the final successful configuration (in which both the queues are emptied and both types are ).
By combining Theorem 4 with Lemma 3, we can finally prove that our encoding of queue machines into session types correctly reduces state reachability into refinement.
See 5
Proof A.3.
We first prove the only-if part. Let . By Lemma 3 we have that is compliant with . Given that , also is compliant with . By Theorem 4 this implies that is reachable in .
We now prove the if part. Assume that is reachable in . As discussed in Section 2 (see footnote 2) our encoding of queue machines assumes that the set of labels in the Definition 2.1 of session types includes the symbols in the queue machine alphabet plus the symbol . We now consider a queue machine obtained by replacing the queue alphabet with a richer alphabet such that , and by extending with a new transition relation which includes also the additional queue symbols in its domain. The behaviour of on these additional symbols is irrelevant because these symbols will never be placed in the queue, given that the input alphabet is still . We have that is reachable in , simply because reproduces the same computations of . By Theorem 4 we have that is compliant with . By Corollary 2 we have that is compliant with all such that . Under the assumption that , by Lemma 3 we have that the set of types such that precisely corresponds with the types with which is compliant. We have observed that is compliant with all such , hence we can conclude that .
A.2. Controllability Characterisation
In this section we will prove the following theorem about controllability characterisation.
See 7
We start by introducing some notions and definitions that will be needed in the proof.
First of all we present an equivalent definition, based on purely structural induction, of the predicate introduced in Definition 2 characterizing session type controllability. {defi} Given a session type , we define the judgment inductively as follows: {mathpar} \inferrule t ok
end ok
end∈ T ∨∃t’ : t’ ≠ t ∧t’ ∈ free(T)
T ok
μt.T ok
\inferrule
T ok
&{l:T} ok
∀i ∈I . T_i ok ⊕{l_i:T_i}_i∈I ok where is the set of variables occurring free in .
In the following we will use a reformulation of session types in terms of equation sets. In equation set notations we will use terms that have the same syntax as those used to denote session types, excluding the recursion operator. Notice that in such notations we consider possibly open terms (i.e. such that is not empty). Session types are, thus, denoted by , with being a set of variables that includes all variables in and also in for all .
Formally, given a session type (we assume with loss of generality that each of its recursions uses a variable with a different name) we consider its equivalent equation set notation , defined as follows:
-
•
is the set of variable names used in the recursions of
-
•
is the only term without recursion operators satisfying: there exists a set of terms , one for each variable , such that
-
•
each , with , is the only term without recursion operators satisfying: there exists a set of variables and a set of terms , one for each variable , such that with occurring in .
[Unfolding]Given session type in equation set notation we define its unfolding as follows:
Notice that unfolding is well defined because we consider session types with guarded recursion in equation set notation.
The transition relation for configurations , with , for , being session types in equation set notation, is defined as in Definition 2.2 by using the above definition of unfolding (and by assuming that the equational part is copied, for both and , after every transition).
Given and session types, it obviously holds (by standard arguments) that the transition system of is bisimilar to that of , hence that: and are compliant if and only if and are compliant.
We now define predicate for session types in equation set notation. is defined as in Definition 2, by assuming that predicate is, instead, defined as follows. if there exists an indexing (total order) on the variables of such that and, for all , with , it, holds:
Moreover, as in Definition 2, in order to establish of a session type input prefix replacement must preliminarily be performed, so to obtain session types where and in both term and all terms , with , all input prefixes have a single label.
Proposition 16.
if and only if .
Proof A.4.
We first show that implies . Given obtained by input prefix replacement from (so to have input prefixes with single choices) that satisfies the predicate, we correspondingly consider , which is an input prefix replacement of . is an immediate consequence of by considering the indexing of variable names used in the recursions of obtained as follows. We incrementally assign indexes to variables (starting from ) according to a depth-first visit of the syntax tree of as follows. When we are at a node, we have two cases. Either has already an assigned index (not possibile at the beginning) or not. In the latter case: we consider all operators occurring in , if any, that syntactically include or variable such that and we assign an index to all such (incrementing the last assigned index) in increasing order from the innermost to the outermost; then we assign an index to (incrementing the last assigned index). Finally, in both cases, we visit all the descendants (with no other recursion node in-between) of the node, if any.
We now show that implies . Given obtained by input prefix replacement from that satisfies the predicate, we correspondingly consider the only term which is an input prefix replacement of such that . We show that (Definition A.2 above) by structural induction:
-
•
For the base cases and we have nothing to show.
-
•
and are a direct consequence of the induction hypothesis, i.e. and , respectively.
-
•
is a direct consequence of the induction hypothesis and of the fact that: . The latter is shown as follows. From we know that there exists a variable indexing such that, for all it, holds: . So, given index such that , we have to show: . What we know is that , so there are two cases:
-
(1)
Either it holds and we are done (with ).
-
(2)
Or , for some , is a subterm of . In this case we show that: . To do this we consider index and the defining term in its equation: we know that . Now again we have the same two cases, considering index instead of and term instead of term . Notice that we cannot proceed like this forever because the syntax of is finite, hence case must eventually apply. Moreover when this happens, we are sure that the variable that we detect is different from (i.e. ) because the indexing of the variables that we consider are always strictly smaller than .
-
(1)
We are now in a position to prove the desired theorem. We prove implications in the two opposite directions one at a time.
Theorem 17.
If there exists a session type such that and are compliant then .
Proof A.5.
Since and are compliant, as observed above, we have also that and are compliant. Therefore (the transition system of) configuration is a correct composition according to Definition 2.2.
We now show that : by Proposition 16 this implies that . In order to do this we need to enrich the transition system representation of the behaviour of configurations . We assume the transition relation defined in Definition 2.2 to be enriched as follows: transitions originated from outputs of (rule of Definition 2.2) are assumed to be decorated with the label of the performed output (denoted by ), while transitions originated from inputs of (rule of Definition 2.2) are assumed to be decorated with the label of the performed input (denoted by ). Notice that, in case of transitions originated from inputs or outputs of no decoration is added to transitions . Moreover, rule (about recursion unfolding) of Definition 2.2 is assumed to just copy the decoration labeling the transition (if there is any).
We now consider such an enriched transition system over configurations . We use to range over these configurations. We say that a configuration exposes variable if . Moreover, we denote transition systems paths starting from a given configuration , i.e. finite sequences of transitions (where decorations can be in case of non decorated transitions), by means of strings (strings over pairs with being a decoration or and a configuration).
Assuming , we now construct an indexing on the variables in the subset of , which includes variables such that: a configuration that exposes is reachable from the initial configuration . We proceed as follows. If , then we consider any reachable configuration that exposes some variable . Since is a correct composition, the configuration must reach a configuration such that . We consider the path from to and the last configuration of such a path that exposes a variable. We denote such a variable with , the configuration that exposes it with , and the path (string) from that leads to (part of the path from to considered above) with . In any subsequent -th step, with , we consider the set . If , then we consider any reachable configuration that exposes some variable . Since is a correct composition, the configuration must reach a configuration such that . We consider the path from to and the first configuration of such a path that either exposes a variable in or is such that . Again we consider the path from to and the last configuration of such a path that: is different from and exposes a variable (such a variable must exist, because exposes a variable, and belong to because of the way we have selected ). We denote such a variable with , the configuration that exposes it with , and the path (string) from that leads to (part of the path from to considered above) with .
We now consider terms for each variable . We build terms inductively by taking , where , with being either a or (that represents being outside the path), is defined as follows.
-
•
-
•
-
•
-
•
where and, for all , : with -
•
-
•
-
•
-
•
if has some transition
where is any such that -
•
if has some transition
where, for all , -
•
if and has neither nor transitions
where is the first configuration having some transition or some transition in the path from to a configuration such that (such a path must exist because is a correct composition)
where we use to represent the empty string.
We also take .
We now have that is a session type in equation notation: must include all variables in and also in for all because, otherwise, a configuration exposing the variable that is not included in would have been reachable from the initial configuration (which contradicts the definition of ). Moreover, due to the way is defined, is obtained from by performing input replacement that yields input prefixes with single inputs. Finally, being the last configuration exposing a variable inside a path ending with a configuration that either exposes a variable in (and not having previous configurations exposing such variables) or is such that , each of the satisfies the constraint .
Theorem 18.
If then there exists a session type such that and are compliant.
Proof A.6.
If then . That is, there exists an input prefix replacement that yields a session type such that (and in both term and all terms , with , all input prefixes have a single label) and that satisfies the predicate, i.e. there exists an indexing of the variables, such that: . We assume set to be minimal, i.e. to not include any defined but unused variable name and we take to be the unique session type such that .
In the following we will consider configurations that are reachable from the initial configuration . We say that any such configuration exposes variable if . Now, given any configuration reachable from the initial configuration , we have that is such that:
-
•
-
•
There exists a configuration , which is reached from with the transitions originated by performing either the non-empty sequence of inputs in the lefthand type or the non-empty sequence of inputs in the righthand type, such that , with .
This property of is, indeed, an invariant property of all configurations reachable from the initial configuration in that: it is satisfied by itself and it is preserved both by transitions originated from outputs of the lefthand or righthand type (which, for a configuration satisfying the above property, can be done only if its own queue is empty, and have the effect of enqueuing in the righthand or lefthand type, respectively, a symbol that it can then, dually, dequeue with an input) and by transitions originated from inputs of the lefthand or righthand type (which just make the already existing input transition sequence to shorter).
We now notice that it is possible to reach, from , by performing outputs of the lefthand or righthand type immediately followed by inputs dually executed by the righthand or lefthand type, respectively: either a configuration such that (in case ), or a configuration exposing an indexed variable . In the latter case, we can, similarly, reach: either a configuration such that (in case ), or a configuration exposing an indexed variable with . In the latter case, we repeat, again, the same step: we are guaranteed to eventually meet the case in which a configuration is reached in that variable indexes strictly decrease at each step. We thus have that and are compliant, hence and are compliant.
A.3. Soundness of Fair Asynchronous Subtyping w.r.t. Fair Refinement
Lemma 19.
Consider the session type . Let and , for every . If is a correct composition then one of the following holds:
-
•
does not contain any input branching and , for every ;
-
•
contains an input branching and (for every ) and have at least one outgoing transition.
For every possible transition we have that one of the following holds:-
(1)
does not consume the label and there exist , , (for every , ), , and s.t. and
; -
(2)
consumes the label , hence , and s.t. and , where are the labels in one of the paths to in .
For every possible transition we have that there exist , , (for every , ), , and s.t.
and
. -
(1)
Lemma 20.
Consider and with , where are the labels in one of the paths to in . We have that if is a correct composition, then also is a correct composition.
Proof A.7.
By contraposition, assume is not a correct composition. This implies the existence of , from which it is not possible to reach a successful configuration, such that . If the labels were not consumed, we extend to by allowing the l.h.s. type to consume all the labels . We have that also from is not possible to reach a successful configuration. We now reorder the transitions in such that in the initial steps the l.h.s. type consumes the labels . After these transitions the configuration is reached. This implies that also , but this is not possible because is a correct composition and from no successful configuration can be reached.
Lemma 21.
Consider the session type . Let and , for every . If is a correct composition then, for every , there exists such that and .
Proof A.8.
Given that is a correct composition, we know that there exists s.t. and . During this sequence of transitions, the input context will become without input branchings, because a configuration that contains one type with an input branching is not successful. In other terms there exist a prefix of the sequence of transitions, at the end of which the input context becomes without input branchings. We proceed by induction on the length of such a prefix. If the length is zero, we can apply the first item of Lemma 19 to conclude that , for every , hence also can reach a successful configuration. In the inductive step, we consider the first transition of , we apply the last item of Lemma 19 to show that also , for every , can perform a transition such that it is possible to apply again the hypothesis on the reached configurations. This is possible because if is correct, also the configurations it can reach are correct.
Proposition 22.
Consider the session type . If is a correct composition then, for every , we have that also is a correct composition.
Proof A.9.
By contraposition, assume s.t. is not a correct composition. This means the existence of such that cannot reach a successful configuration. By induction on the length of this sequence of transition we show that, differently from what assumed, can reach a successful configuration. If the length is 0, we simply apply Lemma 21 to show that can reach a successful configuration. If the length is not 0, we consider two possible cases: (i) the initial transition of of consumes the label from the the queue of the r.h.s. type or (ii) it does not. In case (i) we use the corresponding item 2 in Lemma 19 to see that we can apply Lemma 20 on and , in order to conclude that is a correct composition. Given that we can conclude that can reach a successful configuration. In case (ii) we use the corresponding item 1 in Lemma 19 to conclude that we can apply again the inductive hypothesis on the shortest sequence of transitions . This is possible because has a corresponding transition to , such that and still satisfies the assumption in the statement of the Lemma. In particular is a correct composition because also is a correct composition.
Lemma 23.
If is a correct composition then is controllable.
Proof A.10.
We show the existence of a type such that is a correct composition.
Consider a type defined as follows. Assume and . The type initially performs outputs with single output labels , , , respectively. After such outputs, it becomes like , with the difference that along all of its paths, the initial input branchings are replaced by one of its continuation as follows: the -th input branching is replaced by its continuation in the branch labeled with .
We now show by contraposition that is a correct composition. If is not correct, then there exists such that from it is not possible to reach a successful configuration. It is not restrictive to assume that during the r.h.s. type has produced the queue (in fact, if it has not produced them, we continue the computation performing them). We can also assume that outputs in , corresponding to outputs in along an initial path with less than inputs have been all performed (also in this case, if these outputs were not performed, we continue the computation executing them). We have that also can perform a computation . Given that is a correct composition, we have that from will be possible to reach a successful configuration, thus contradicting the above assumption.
See 8
Proof A.11.
Given that is a correct composition, there exist , , , and such that and . We proceed by induction on the length of this sequence of transition.
If the length is 0, then , that implies , that also implies (because ), from which we have .
If the length is greater than 0, we proceed by case analysis on the possible first transition .
If the transition is inferred by it is sufficient to observe that and , and then apply the inductive hypothesis because is a correct composition in that it is reachable from a correct composition.
We now consider that the transition is inferred by .
We first discuss the case in which .
In this case, the above transition is
, for
some .
Given that , and is controllable by Lemma 23,
we have
with , for every . This ensures that
.
Then we can apply the inductive hypothesis because
and
is a correct composition.
We now discuss the case in which . There are two possible subcases: (i) also starts with an input branching, i.e., , or (ii) starts with an output selection, i.e., .
In case (i), the above transition is , for some . Given that , and is controllable by Lemma 23, we have , , and , where . Given that is a correct composition and , also the latter configuration is a correct composition. By Lemma 23 we have that is controllable. This implies that , hence also . This ensures that . Then we can apply the inductive hypothesis because and is a correct composition.
In case (ii), given that , and is controllable, we have that , and with , for every . We first observe that the sequence of transitions , with , includes at least one output selection executed by one of the output selections filling the holes in . This label is the first one emitted by the l.h.s. type after it has executed input branchings in . We have that the same sequence of transitions, excluding the output of , can be executed from the configuration . Such a sequence is , with ; notice that it is shorter than the above one. We now consider . We can now apply the inductive hypothesis on the shorter sequence , because and by Proposition 22 is a correct composition.
See 9
Proof A.12.
If is not controllable, then the thesis trivially holds because for every .
We now consider controllable, and we prove the thesis by showing that if then, for every , , and such that is a correct composition, we have that the following holds:
-
•
if then there exists such that and is a correct composition.
The above implies the thesis because, given and the correct composition , if there exists a computation , we can apply the above result on each step of the computation to prove that there exists such that and is a correct composition. Then, by Proposition 8, we have that there exist , , , and such that and .
We now prove the above result. The transition can be of four possible kinds:
-
(1)
the consumption of a message from the r.h.s. queue, i.e. ;
-
(2)
the insertion of a new message in the l.h.s. queue, i.e. ;
-
(3)
the consumption of a message from the l.h.s. queue, i.e. ;
-
(4)
the insertion of a new message in the r.h.s. queue, i.e. .
In the first two cases, we simply observe that there exists also (resp. ), that , and also (resp. ) is a correct composition because reachable from the correct composition (resp. ).
In the third case we have that starts with an input branching. Given that , and is controllable, also must start with an input branching, i.e. . By definition of we have that , , and , where . Given that is a correct composition, there exists s.t. and . The former configuration is a correct composition, hence also the latter is such. This implies, by Lemma 23, that is controllable, hence and also . Thus, we have , with . We conclude this case by observing again that is a correct composition in that reachable from the correct composition .
In the fourth and last case, we have that starts with an output selection, and is the continuation in the branch with label . Given that , and is controllable, we have , and , for every and some such that . It remains to show that is a correct composition, but this follows from Proposition 22 and the fact that , with for some , is a correct composition. In fact and is a correct composition.
A.4. Undecidability of Fair Asynchronous Subtyping
See 10
Proof A.13.
We first consider the only-if part, proving the contrapositive statement, that is, if the queue machine terminates then . If the queue machine terminates, we have that . Consider now the pair of types with and . If, by contradiction, , since is controllable (it is compliant, e.g., with its dual) we have that by Definition 3 there exists a fair asynchronous subtyping relation such that . We now show that, by definition of fair asynchronous subtyping relation, will have to include other pairs of types corresponding with configurations reachable in the queue machine . Consider the type :
assuming and
It starts with an input branching, with labels for each queue alphabet symbol including the initial queue symbol . Then it has a sequence of output selections, including the sequence of symbols to be emitted by the queue machine after having consumed . Consider now the type :
It starts with an input branching with only label , followed by an output selection on all symbols, including label having continuation . The latter ensures that is controllable. If we consider the constraints imposed by the Definition 3 on fair asynchronous subtyping relations, we can conclude that should contain a pair of types where is the type corresponding to the new state of the queue machine (reached after the above sequence of output selections to be emitted by the queue machine after having consumed ) and is like , with the difference that before the output selection there is a sequence of input branchings, each one with only one label, corresponding with the sequence of symbols in the queue after the first computation step. This reasoning can be repeatedly applied to prove that should also contain other pairs of types , one for each configuration reachable in the queue machine . Consider now the pair corresponding to the terminating configuration . The type , as all the types representing states in the queue machine, starts with an input branching. The type , on the other hand, represents the empty queue, so it is , i.e. it is like but without input branchings before the output selection. This means that does not satisfy the item for input selection in Definition 3. Hence cannot be a fair asynchronous subtyping, but this contradicts the above initial assumption about being a fair asynchronous session subtyping.
We now move to the if part. Assume that the queue machine does not terminate. We show that there exists a fair asynchronous subtyping relation that contains the pair , hence . There are two kinds of pairs in : (i) the pairs discussed in the above only-if part of the proof that corresponds to the path in the subtyping simulation game that reproduces the computation of the queue machine , and (ii) other pairs corresponding to alternative paths. The pairs of types (i) satisfy the constraints imposed by Definition 3 because output selections of the l.h.s. type can always be mimicked by the r.h.s. type (that always include an output selection after a sequence of input branchings with only one label), and input branchings can always be mimicked by the r.h.s. type because under the assumption that the queue machine does not terminate, the queue is always non-empty during the computation. Also the pairs of type (ii) satisfy the constraints imposed by Definition 3. In fact, these pairs are generated considering the alternative branches in the l.h.s. types in Definition 3.1, namely, the branches corresponding with the labels and in the definition, that we report here for reader convenience:
with and . The l.h.s. type in the pairs associated with these branches, are of two kinds: (a) they are able to recursively perform all possible outputs until the label is selected (type ), or (b) they are able to recursively perform all possible inputs until the label is selected (type ). In the first case (a), the constraints in Definition 3 are satisfied because the r.h.s. type is always able to mimick output selections (see the above observation). In the second case (b), we have that the output has been previously selected by the last pair of kind (a) considered. Hence, the r.h.s. type is a sequence of input branchings, with only one label, where all inputs excluding the last one are different from , and the last one, having label , has continuation . This guarantees that all these pairs satisfy the constraints in Definition 3, under the assumption that also a final pair belongs to . We the conclude by observing that we have proved the existence of a fair session subtyping relation such that (in that this is the first pair of the kind (i) above), hence we have that .
A.5. Soundness of the Algorithm w.r.t. Fair Asynchronous Subtyping
See 13
Proof A.14.
We proceed by induction. If , the thesis directly follows from the fact that is contained in a simulation tree.
If , by inductive hypothesis we have that the thesis holds for . We prove that the thesis holds also for showing that there exists a simulation tree including with labeled with if and only if there exists a simulation tree including with labeled with . The proof is by case analysis, considering the three possible steps in the subtyping simulation game at the basis of the definition of .
If starts with a recursive definition, the thesis trivially holds because simply modify the l.h.s. type by unfolding its initial recursion and leaves the r.h.s. type unchanged.
If starts with an input branching, by Definition 3 we have that the r.h.s. type contains an entire context in its growing holes. We initially consider with labeled with . This means that by applying ) to the r.h.s. type we obtain an input context starting with an input branching satisfying the constraints imposed by Definition 3. The step of the subtyping simulation game corresponding to selects a branch of the input branching such that its continuation is controllable. Now consider with label . The application of ) modifies the outer context in the same way thus obtaining a type starting with the same input branching, simply with an additional nesting of in the holes in . The continuation is also controllable because it is an input contexts with the set of indexed holes, hence the same set of types and . Hence it is possible to apply a corresponding step in the subtyping simulation game with labeled with . Notice that the same reasoning can be applied assuming that with labeled with to prove that there exists also the corresponding step in the subtyping simulation game . In this case we use the assumption that in the growing holes of the r.h.s. type of the label of we have an entire context , thus guaranteeing the presence of the same in all the continuations of the initial input branching present in the outer context.
If starts with an output selection, we initially consider with labeled with . This means that by applying to the r.h.s. type we obtain an input context filled with types starting with output selections satisfying the constraints imposed by Definition 3. Notice that the application of to the outer input context does not remove holes, but at most replicates some of them. Moreover, the application of applies to the innermost types and by unfolding the variables inside outputs replacing them with their definitions (already present in and given that these are closed terms). The considered step in the subtyping simulation game modifies (the unfoldings of) and by resolving initial output selections, thus obtaining and . Now consider with label . What we have just observed about the step of subtyping simulation game, holds also for this new pair of types. The application of respectively modifies the outer input context and the inner types and in the same way, and also the same resolution of the initial output selections in and is possible. Hence there exists with labeled with . Notice that the same reasoning can be applied assuming that with labeled with to prove that there exists also the corresponding step in the subtyping simulation game .
See 14
Proof A.15.
We proceed by induction on the length of .
If the length is 0, then is the root of hence its label is obviously in .
If the length is greater than 1, consider . By inductive hypothesis we have that is a label present either in , for some , or in with .
We start from the latter case, i.e., there exists in such that . We have that there exists in s.t. .
We now consider the former case, i.e., there exists one node in , for some , labeled with . Let be such node. There are two possibilities, either (i) the node is a leaf in , or (ii) it is not a leaf. In the case (ii) we have that contains , with labeled with . If is a leaf, we consider the four kinds of leaves separately.
If is a leaf of type 2a, then there exists an ancestor of in with the same label . Given that the ancestor is not a leaf, contains , with labeled with .
If is a leaf of type 2b in , we have . The node has an ancestor in s.t. . Consider now the corresponding node in . We have that is labeled with . Given that is not a leaf, contains , with labeled with .
If is a leaf of type 2c in , we have . We have two cases. If , by definition of witness tree, . The node has the same label as the root of . Hence such a simulation tree includes a transition from its root to a node labeled with . If the node has an ancestor in such that . Consider now the corresponding node in . We have that is labeled with . Given that is not a leaf, contains , with labeled with .
If corresponds to leaf of type 2d in , we have that the label of is the same as the label in the corresponding node in , i.e. . In fact labels of the leaves of type 2d in do not change when moving to . This because the input context does not include growing holes. By definition of witness tree we have that . The node has the same label as the root of . Hence such a simulation tree includes a transition from its root to a node labeled with .
See 15
Proof A.16.
Let be the witness subtree with root in . By Proposition 14 we have that is a label present either in , for some , or in with . In the latter case the thesis trivially holds because all nodes in are either successful or there exists . In the former case there are two cases: either there exists an intermediary node (non-leaf) in one , for some , labeled with is an intermediary, or such a node can be only in leaf positions. In the first case the thesis trivially holds because all intermediary nodes have successors. The second case can occur only for leaves of type 2c in , or corresponding to leaves of type 2d in . Both cases imply that with . Hence has the same label as the root of and, as above, the thesis trivially holds because all nodes in are either successful or there exists .