Introduction
Rust is a modern, flexible, systems-level programming language of growing importance. It is used in many crucial application domains, most notably the Linux kernel [44 ] . This importance and usage increases the need for formal analysis and formal verification of Rust programs, both of which are well supported by Rust’s language design. Accordingly, several verification tools for Rust are being developed [3 , 6 , 12 , 21 , 30 , 43 ] . With its complex ownership model and reference system, Rust provides a powerful yet unusually restrictive type system that ensures memory safety and the absence of data races [42 ] .
The lack of an official, formal, source code level Rust semantics makes validation of these tools problematic. Without such a semantics, interoperability of verification tools is difficult, as tools may not be based on the same, or even compatible, formalisms. Similarly, verification of the Rust compiler needs a formal semantics for each of Rust’s intermediate representations.
The semantics problem is partially addressed by formal models of parts of Rust’s type system or intermediate representations, typically the mid-level intermediate representation (MIR) [13 , 28 , 36 , 39 ] . These semantics provide usable and rigorous formalizations of Rust’s borrow checker and operational semantics of subsets of the language, each offering different trade-offs and specializations. Nevertheless, they are limited in at least two respects. First, being low-level and focused on the MIR, they offer little for reasoning about Rust programs at a level closer to the source code. A high-level representation crucially allows, e.g., comprehension of the complex proof state [21 ] .
Author’s Contact Information: Daniel Drodt, daniel.drodt@tu-darmstadt.de, Technical University of Darmstadt, Darmstadt, Hessen, Germany.
Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components of this work owned by others than the author(s) must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from permissions@acm.org.
© 2026 Copyright held by the owner/author(s). Publication rights licensed to ACM.
ACM 1558-4593/2026/9-ARTZZZ
https://doi.org/XXXXXXX.XXXXXXX Second, no formal semantics for Rust’s concurrent features (“async Rust”) has been developed. As “fearless concurrency” is a major feature and promise of Rust [42 ] , this is an unfortunate limitation. Without formal semantics, it remains difficult to reason about concurrent and parallel Rust programs or to prove them correct, free of deadlocks, and similar properties. Furthermore, it remains elusive to model concrete schedulers or runtimes, which is especially relevant as Rust allows different asynchronous runtimes to be used. Here too, a high-level view seems more promising than semantics of lower-level representations.
As Rust has matured and its type system, borrow checking, and memory model have been examined, refined, and formalized, we argue that a sensible next step is to examine the high-level features, building on these closely-studied foundations.
Hence, we see a crucial gap left open: a formal semantics of Rust suitable for formalizing high-level properties of Rust programs. Ideally, such a semantics should (1) be highly flexible and compositional to accommodate future language extensions; (2) formalize Rust at the source code level (or close to it); (3) allow the formalization of asynchronous Rust code; (4) permit modeling of and reasoning about asynchronous runtimes; and (5) ease deductive verification of Rust programs.
To meet these goals, modularity of the semantics is paramount: Extendability is only feasible if adding a new language feature does not require to significantly rewrite the existing semantics rules. Additionally, a modular semantics eases verification effort, as calculus proof rules are also made modular in the sense that it is possible to extend the calculus with new rules and constructs without revising the rest of the calculus.
However, semantics in the style of structural operational semantics (SOS) struggle with modularity. As Peter Mosses put it, “conventional SOS descriptions of programming languages have quite poor modularity,” which leads to complex semantics, as “[w]hen extending a pure functional language with concurrency primitives and/or references, for instance, the SOS rules for all the functional constructs had to be completely reformulated” [50 ] . While Mosses [50 ] provides a modification of SOS that improves modularity, recent developments in trace-based semantics offer an even more flexible, complete, and modular path [7 , 8 , 32 , 41 ] .
The present paper remedies this situation by providing a comprehensive, formal semantics for most parts of safe Rust’s high-level intermediate representation (HIR) based on the locally abstract, globally concrete (LAGC) semantics framework [17 ] , which was developed with concurrent and parallel programs and deductive verification in mind. LAGC achieves a modular and flexible semantics by separating the local evaluations of expressions from the global state and concerns such as scheduling. This approach is modular in the sense that each syntactic structure—expressions or statements—have one local rule that is independent of the global context. Adding new language features consists largely of formulating new local rules. Furthermore, the approach is well-suited for concurrency by delegating all scheduling concerns to the global layer and building hybrid traces over states and events for each program execution. These traces contain all relevant information of state changes and scheduling relevant events. Combining these elements, LAGC defines the semantics of a specific program as the set of all well-formed traces generated by the global composition rules.
LAGC has been successfully applied to a number of programming languages and settings, such as ProMeLa and active object languages [10 ] like ABS [17 , 18 ] , demonstrating its flexibility. Further examples include Real-Time ABS [62 ] and Timed Rebeca [25 ] . The LAGC approach has also been used, in a slightly different form, in the context of deductive verification [8 ] . These examples demonstrate that LAGC, with its modular approach, separation of local and global context, and hybrid traces, is more suitable than the traditional SOS semantics for purposes of verification and the modeling of concurrent programming languages.
For these reasons, LAGC is a viable semantics framework to achieve the goals outlined above and it allows a semantics of higher-level Rust. Hence, the presented semantics is based on LAGC. Our detailed contributions are:
We provide a semantics for most parts of safe Rust’s HIR in the LAGC framework and demonstrate the semantics’ usability with a number of examples. To demonstrate the applicability of the semantics, we support complex language features and control flows, such as loops with breaks and continues.
We extend this semantics to async Rust, modeling any possible scheduling of async functions. Notably, to our knowledge, this is the first formal semantics of async Rust in the literature.
Based on the asynchronous semantics, we formalize Rust’s most widely-used runtime, the “current thread runtime” of Tokio [64 ] , and prove its fairness. We stress that our approach is straightforwardly adaptable to other runtimes, with the async Rust semantics carrying over.
Finally, we show how our semantics can be used to prove soundness of the calculus for RustyDL, a program logic for Rust [21 ] based on dynamic logic (DL) [53 ] , demonstrating how the modular rules of LAGC ease such proofs. Thereby, we also highlight the interaction of DL and LAGC.
§ 2 introduces the necessary background and definitions on which our semantics is built, as well as the general LAGC approach. We present the semantics for synchronous Rust in § 3 and extend it to async Rust in § 4 . § 5 then builds upon the async semantics to represent one of Tokio’s runtimes in LAGC and proves its fairness guarantees. The calculus of the program logic RustyDL [21 ] is proven sound in § 6 . We present related work in § 7 and conclude the paper in § 8 .
Prerequisites
The goal of the LAGC approach [17 ] is to represent a program run as a trace consisting of states and events and to represent a program’s entire behavior as the set of all possible traces starting from a concrete state. As a principle idea to tackle the complexity of all possible executions, LAGC consists of many local rules for each syntactic structure, which consider abstract states and traces, and global rules , which consider concrete states and traces and are concerned with the correct composition of such traces. Events in traces aid this effort by providing more structural information, e.g., that a function was invoked or a panic occurred.
Building an LAGC semantics consists of four steps: (1) the local evaluation, mapping expressions to (abstract) continuation traces, without knowledge of the surrounding context; (2) a well-formedness predicate, restricting the set of allowed traces and enforcing, e.g., proper ordering of events; (3) trace composition rules, defining how the local traces are stitched together to concrete traces; and (4) global program semantics, defining the initial state and allowed end states. This framework yields a set of concrete traces describing all possible executions for a given program from an initial state and achieves a modular semantics: Each kind of expression has exactly one local rule irrespective of context.
This section gives the necessary background for states, events, and traces, mainly following the definitions in [17 ] , modified to adapt to Rust’s more functional, expression-based structure. This modification in fact simplifies some parts of the LAGC approach.
Values and States
Due to the high-level concerns of our semantics, we do not map Rust constructs to low-level or even machine-level domains; rather, we map to mathematical domains: Rust’s concrete, machine word integer types, such as i32 and u8, are mapped to mathematical integers Z \mathbb{Z} Z , while bool is mapped to boolean values t t \mathrm{tt} tt and f f \mathrm{ff} ff . We proceed similarly with compound types like arrays and structs. This abstraction is not a limitation of the semantics. We ensure the correct behavior of Rust types as part of the evaluation of the relevant expressions.
Definition 2.1 (Starred) Semantic Values. We write V a r Var V a r for the set of program variables with typical elements x x x , y y y , z z z , etc. Further, we define a place p ∈ P l a c e p \in Place p ∈ P l a ce as an abstract representation of memory locations. Such a place is either a program variable x x x , an array field p [ n ] p[n] p [ n ] of another place p p p where n n n is a natural number, or a field access p . i d e n t i f i e r p.identifier p . i d e n t i f i er .
x ∈ V a r : : = i d e n t i f i e r x \in Var ::= identifier x ∈ V a r ::= i d e n t i f i er p ∈ P l a c e : : = x ∣ p [ n ] ∣ p . i d e n t i f i e r p \in Place ::= x \mid p[n] \mid p.identifier p ∈ P l a ce ::= x ∣ p [ n ] ∣ p . i d e n t i f i er n ∈ N : : = 0 ∣ 1 ∣ ⋯ n \in\mathbb{N} ::= 0 \mid1 \mid\cdots n ∈ N ::= 0 ∣ 1 ∣ ⋯ The set of semantic values V a l Val V a l , with typical elements v v v , v 1 v_1 v 1 , v 2 v_2 v 2 , … \ldots … , includes boolean, integer, reference, function, tuple, array, struct, and enum variant values. The latter compound types are defined as their Rust counterparts, where all fields are values. Reference values are either shared (denoted by s s s ) or mutable (denoted by m m m ) and contain a place p ∈ P l a c e p \in Place p ∈ P l a ce and a borrow identifier b ∈ B I d b \in BId b ∈ B I d ; e.g., Ref x [ 1 ] m ( b 0 ) \operatorname{Ref}^{m}_{x[1]}(b_0) Ref x [ 1 ] m ( b 0 ) is a mutable reference of place x [ 1 ] x[1] x [ 1 ] with borrow identifier b 0 b_0 b 0 . Function values are represented as function names.
v ∈ V a l : : = t t ∣ f f ∣ z ∣ Ref p s ( b ) ∣ Ref p m ( b ) ∣ ( v ) ∣ [ v ] ∣ s t r u c t s ∣ e n u m s ∣ f u n c t i o n s v \in Val ::= \mathrm{tt} \mid\mathrm{ff} \mid z \mid\operatorname{Ref}^{s}_{p}(b) \mid\operatorname{Ref}^{m}_{p}(b) \mid(v) \mid[v] \mid structs \mid enums \mid functions v ∈ V a l ::= tt ∣ ff ∣ z ∣ Ref p s ( b ) ∣ Ref p m ( b ) ∣ ( v ) ∣ [ v ] ∣ s t r u c t s ∣ e n u m s ∣ f u n c t i o n s z ∈ Z : : = n ∣ − 1 ∣ − 2 ∣ ⋯ z \in\mathbb{Z} ::= n \mid-1 \mid-2 \mid\cdots z ∈ Z ::= n ∣ − 1 ∣ − 2 ∣ ⋯ The set of starred values S V a l ⊃ V a l SVal \supset Val S V a l ⊃ V a l , with typical element s v sv s v , extends values with the starred value ∗ \ast ∗ , representing unknown, symbolic values.
s v ∈ S V a l : : = v ∣ ∗ sv \in SVal ::= v \mid\ast s v ∈ S V a l ::= v ∣ ∗
Example 2.2. The semantic value to which the Rust expression (true, 4 ∗ 2 4 * 2 4 ∗ 2 , &mut n), where n n n is a program variable of type i32, evaluates is ( t t , 8 , Ref n m ( b 0 ) ) (\mathrm{tt}, 8, \operatorname{Ref}^{m}_{n}(b_0)) ( tt , 8 , Ref n m ( b 0 )) , where b 0 ∈ B I d b_0 \in BId b 0 ∈ B I d is unique.
To associate variables with values, we use a symbolic state σ \sigma σ , modelled as a partial mapping σ : V a r ⇀ S V a l \sigma: Var \rightharpoonup SVal σ : V a r ⇀ S V a l from variables to starred values. We express a modification of a variable x x x with the (starred) value s v sv s v as the state update of σ \sigma σ :
σ [ x ↦ s v ] ( y ) = { s v x = y σ ( y ) x ≠ y \sigma[x \mapsto sv](y) =
\begin{cases}
sv & x = y \\
\sigma(y) & x \ne y
\end{cases} σ [ x ↦ s v ] ( y ) = { s v σ ( y ) x = y x = y
As values can be nested in arrays, tuples, structs, and enums, we define syntactic sugar for updating not just variables but places p ∈ P l a c e p \in Place p ∈ P l a ce . The base case of p = x p = x p = x for some variable x x x is handled by state updates. Updates to an array place p [ n ] p[n] p [ n ] is resolved by updating place p p p with a changed array value.
σ [ p [ n ] ↦ s v ] = σ [ p ↦ [ s v 0 , … , s v n − 1 , s v , s v n + 1 , … , s v m ] ] where n ≤ m ∧ σ ( p ) = [ s v 0 , … , s v m ] \sigma[p[n] \mapsto sv] = \sigma[p \mapsto[sv_0,\ldots,sv_{n-1},sv,sv_{n+1},\ldots,sv_m]] \quad\text{where } n \le m \land\sigma(p) = [sv_0,\ldots,sv_m] σ [ p [ n ] ↦ s v ] = σ [ p ↦ [ s v 0 , … , s v n − 1 , s v , s v n + 1 , … , s v m ]] where n ≤ m ∧ σ ( p ) = [ s v 0 , … , s v m ]
The case for updating field places p . i d e n t i f i e r p.identifier p . i d e n t i f i er is analogous: σ [ p . i d e n t i f i e r ↦ s v ] \sigma[p.identifier \mapsto sv] σ [ p . i d e n t i f i er ↦ s v ] .
The symbolic variables of state σ \sigma σ are defined as sym ( σ ) = { X ∈ V a r ∣ σ ( X ) = ∗ } \operatorname{sym}(\sigma) = \{X \in Var \mid\sigma(X) = \ast\} sym ( σ ) = { X ∈ V a r ∣ σ ( X ) = ∗ } and are used to abstract from concrete evaluations, e.g., for function parameters. We typically denote them with capital letters. If sym ( σ ) = ∅ \operatorname{sym}(\sigma) = \varnothing sym ( σ ) = ∅ , we call σ \sigma σ concrete .
As our semantics may introduce new variables, we require a notion of state extension .
Definition 2.3 (State Extension). Let σ 1 \sigma_1 σ 1 , σ 2 \sigma_2 σ 2 be two states. We say σ 2 \sigma_2 σ 2 extends σ 1 \sigma_1 σ 1 —written as σ 1 ⊆ σ 2 \sigma_1 \subseteq\sigma_2 σ 1 ⊆ σ 2 —iff dom ( σ 1 ) ⊆ dom ( σ 2 ) \operatorname{dom}(\sigma_1) \subseteq\operatorname{dom}(\sigma_2) dom ( σ 1 ) ⊆ dom ( σ 2 ) and ∀ x ∈ dom ( σ 1 ) . σ 1 ( x ) = σ 2 ( x ) \forall x \in\operatorname{dom}(\sigma_1).\,\sigma_1(x) = \sigma_2(x) ∀ x ∈ dom ( σ 1 ) . σ 1 ( x ) = σ 2 ( x ) .
Traces and Events
Traces are sequences of states and events, the latter of which give a structure and additional information to traces. Events cannot change the state; we add the relevant events for Rust as we go along. Like states, traces can be symbolic.
Definition 2.4 (Symbolic Trace). We define symbolic traces inductively by the following rules, where ε \varepsilon ε is the empty trace:
τ : : = ε ∣ τ ↷ t (trace) t : : = σ ∣ e v ( e p ‾ ) (trace element) e p : : = x ∣ v (event parameter) \begin{aligned}
\tau&::= \varepsilon\mid\tau\curvearrowright t && \text{(trace)}\\
t &::= \sigma\mid ev(\overline{ep}) && \text{(trace element)}\\
ep &::= x \mid v && \text{(event parameter)}
\end{aligned} τ t e p ::= ε ∣ τ ↷ t ::= σ ∣ e v ( e p ) ::= x ∣ v (trace) (trace element) (event parameter) where e v ( e p ‾ ) ev(\overline{ep}) e v ( e p ) is an event marker and e p ‾ \overline{ep} e p are the event parameters , i.e., variables or values.
Example 2.5. Let σ 0 : = [ X ↦ ∗ , y ↦ f f ] \sigma_0 := [X \mapsto\ast, y \mapsto\mathsf{ff}] σ 0 := [ X ↦ ∗ , y ↦ ff ] be a symbolic state and e v 0 ev_0 e v 0 an event marker with one parameter. Then, τ 0 : = ε ↷ σ 0 ↷ e v 0 ( X ) ↷ σ 0 [ y ↦ t t ] \tau_0 := \varepsilon\curvearrowright\sigma_0 \curvearrowright ev_0(X) \curvearrowright\sigma_0[y \mapsto\mathsf{tt}] τ 0 := ε ↷ σ 0 ↷ e v 0 ( X ) ↷ σ 0 [ y ↦ tt ] is a symbolic trace, representing an execution starting in (symbolic) state σ 0 \sigma_0 σ 0 , then event e v 0 ( X ) ev_0(X) e v 0 ( X ) , and ending in the updated state where y’s value is changed to false.
We take care that each event in a trace is surrounded on either side by a state. This ensures that that functions over traces like l a s t ( τ ) last(\tau) l a s t ( τ ) and l a s t E v ( τ ) lastEv(\tau) l a s tE v ( τ ) , which evaluate to the last state and event of trace τ \tau τ , respectively, are always defined for non-empty traces.
Often, it is necessary to construct a trace τ \tau τ from two given traces τ 1 , τ 2 \tau_1, \tau_2 τ 1 , τ 2 . We call this process trace composition . We write ⟨ σ ⟩ \langle\sigma\rangle ⟨ σ ⟩ for the singleton trace ε ↷ σ \varepsilon\curvearrowright\sigma ε ↷ σ and τ 1 ⋅ τ 2 \tau_1 \cdot\tau_2 τ 1 ⋅ τ 2 for the concatenation of two symbolic traces τ 1 , τ 2 \tau_1, \tau_2 τ 1 , τ 2 . For example, we write the trace from Example 2.5 as
τ 0 = ⟨ σ 0 ⟩ ↷ e v 0 ( X ) ↷ σ 0 [ y ↦ t t ] . \tau_0 = \langle\sigma_0\rangle\curvearrowright ev_0(X) \curvearrowright\sigma_0[y \mapsto\mathsf{tt}] . τ 0 = ⟨ σ 0 ⟩ ↷ e v 0 ( X ) ↷ σ 0 [ y ↦ tt ] .
Traces τ 1 \tau_1 τ 1 and τ 2 \tau_2 τ 2 , where τ 1 \tau_1 τ 1 ends in the state with which τ 2 \tau_2 τ 2 begins, can be “stitched together” with the semantic chop operation1 :
τ 1 ∗ ∗ τ 2 = τ ⋅ τ 2 where τ 1 = τ ↷ σ and τ 2 = ⟨ σ ⟩ ⋅ τ ′ . \tau_1 \ast\ast\tau_2 = \tau\cdot\tau_2 \text{ where } \tau_1 = \tau\curvearrowright\sigma\text{ and } \tau_2 = \langle\sigma\rangle\cdot\tau'. τ 1 ∗ ∗ τ 2 = τ ⋅ τ 2 where τ 1 = τ ↷ σ and τ 2 = ⟨ σ ⟩ ⋅ τ ′ .
Note that the chop is a partial operation: If τ 1 \tau_1 τ 1 is empty or if the last state of τ 1 \tau_1 τ 1 and first state of τ 2 \tau_2 τ 2 are incompatible, the chop is undefined.
We also require a way to concretize symbolic traces into concrete traces. To this end, we work with concretization mappings ρ \rho ρ .
Definition 2.6 (Concretization Mapping). A concretization mapping ρ : V a r ⇀ V a l \rho: Var \rightharpoonup Val ρ : V a r ⇀ V a l is a partial mapping for state σ \sigma σ if dom ( ρ ) ∩ dom ( σ ) = symb ( σ ) \operatorname{dom}(\rho) \cap\operatorname{dom}(\sigma) = \operatorname{symb}(\sigma) dom ( ρ ) ∩ dom ( σ ) = symb ( σ ) . We denote the concretization of σ \sigma σ with ρ \rho ρ as
ρ ∙ σ = ρ ∪ { x ↦ σ ( x ) ∣ x ∈ dom ( σ ) ∖ dom ( ρ ) } . \rho\bullet\sigma= \rho\cup\{x \mapsto\sigma(x) \mid x \in\operatorname{dom}(\sigma) \setminus\operatorname{dom}(\rho)\}. ρ ∙ σ = ρ ∪ { x ↦ σ ( x ) ∣ x ∈ dom ( σ ) ∖ dom ( ρ )} .
Proposition 2.7. Let σ \sigma σ be a state and ρ \rho ρ a σ \sigma σ concretization mapping. Then ρ ∙ σ \rho\bullet\sigma ρ ∙ σ is a concrete state.
Definition 2.8 (Trace Concretization). We extend the notion of concretization mapping to traces in the natural way: A partial mapping ρ \rho ρ is a concretization mapping for τ \tau τ if it is a concretization mapping for all states in τ \tau τ . The concretization ρ ( τ ) \rho(\tau) ρ ( τ ) of τ \tau τ is defined as:
ρ ( ε ) = ε \rho(\varepsilon) = \varepsilon ρ ( ε ) = ε ρ ( τ ↷ t ) = ρ ( τ ) ↷ ρ ( t ) \rho(\tau\curvearrowright t) = \rho(\tau) \curvearrowright\rho(t) ρ ( τ ↷ t ) = ρ ( τ ) ↷ ρ ( t ) ρ ( σ ) = ρ ∙ σ \rho(\sigma) = \rho\bullet\sigma ρ ( σ ) = ρ ∙ σ ρ ( e v ( e p ‾ ) ) = e v ( ρ ( e p ‾ ) ) \rho(ev(\overline{ep})) = ev(\rho(\overline{ep})) ρ ( e v ( e p )) = e v ( ρ ( e p ))
Example 2.9. The trace τ 0 = ⟨ σ 0 ⟩ ↷ e v 0 ( X ) ↷ σ 0 [ y ↦ t t ] \tau_0 = \langle\sigma_0 \rangle\mathrel{\curvearrowright} ev_0(X) \mathrel{\curvearrowright} \sigma_0[y \mapsto\mathsf{tt}] τ 0 = ⟨ σ 0 ⟩ ↷ e v 0 ( X ) ↷ σ 0 [ y ↦ tt ] in Example 2.5 is symbolic. With ρ 0 : = [ X ↦ 3 ] \rho_0 := [X \mapsto3] ρ 0 := [ X ↦ 3 ] we concretize it to ρ 0 ∙ σ 0 = [ X ↦ 3 , y ↦ f f ] \rho_0 \mathbin{\bullet} \sigma_0 = [X \mapsto3, y \mapsto\mathsf{ff}] ρ 0 ∙ σ 0 = [ X ↦ 3 , y ↦ ff ] , ρ 0 ∙ σ 0 [ y ↦ t t ] = [ X ↦ 3 , y ↦ t t ] = ρ ( σ 0 ) [ y ↦ t t ] \rho_0 \mathbin{\bullet} \sigma_0[y \mapsto\mathsf{tt}] = [X \mapsto3, y \mapsto\mathsf{tt}] = \rho(\sigma_0)[y \mapsto\mathsf{tt}] ρ 0 ∙ σ 0 [ y ↦ tt ] = [ X ↦ 3 , y ↦ tt ] = ρ ( σ 0 ) [ y ↦ tt ] , and ρ ( τ 0 ) : = ⟨ ρ ( σ 0 ) ⟩ ↷ e v 0 ( 3 ) ↷ ρ ( σ 0 ) [ y ↦ t t ] \rho(\tau_0) := \langle\rho(\sigma_0) \rangle\mathrel{\curvearrowright} ev_0(3) \mathrel{\curvearrowright} \rho(\sigma_0)[y \mapsto\mathsf{tt}] ρ ( τ 0 ) := ⟨ ρ ( σ 0 )⟩ ↷ e v 0 ( 3 ) ↷ ρ ( σ 0 ) [ y ↦ tt ] .
Finally, we require continuation markers, as placeholders marking the unevaluated remainder of an expression.
Definition 2.10 (Continuation Marker). Let e e e be a Rust expression. The continuation marker K ( e ) \mathrm{K}(e) K ( e ) states that a trace is extended by the traces resulting from evaluating e e e .
The set of symbolic traces with continuation markers, i.e., all τ ⋅ K ( e ) \tau\cdot\mathrm{K}(e) τ ⋅ K ( e ) , is denoted as C T r \mathrm{CTr} CTr . We call the elements of C T r \mathrm{CTr} CTr “continuation traces.”
Finally, to ease reasoning over traces, they must adhere to a certain structure, e.g., agree on the set of symbolic variables. Additionally, our semantics should not generate any symbolic traces, only concrete ones. Definition 2.11 introduces well-formed and concrete (or “shining”) traces, which our semantics generates.
Definition 2.11 (Well-Formed and Shining Trace). Let τ \tau τ be a symbolic trace and S = ⋃ σ ∈ τ symb ( σ ) S = \bigcup_{\sigma\in\tau} \operatorname{symb}(\sigma) S = ⋃ σ ∈ τ symb ( σ ) . Trace τ \tau τ is well-formed if conditions (1) to (3) hold.
∀ σ ∈ τ . ( dom ( σ ) ∖ symb ( σ ) ) ∩ S = ∅ (1) \forall\sigma\in\tau.\; \left(\operatorname{dom}(\sigma) \setminus\operatorname{symb}(\sigma)\right) \cap S = \varnothing \tag*{(1)} ∀ σ ∈ τ . ( dom ( σ ) ∖ symb ( σ ) ) ∩ S = ∅ (1) ∀ e v ( e ‾ ) ∈ τ . vars ( e ‾ ) ⊆ S (2) \forall ev(\overline{e}) \in\tau.\; \operatorname{vars}(\overline{e}) \subseteq S \tag*{(2)} ∀ e v ( e ) ∈ τ . vars ( e ) ⊆ S (2) ∀ e v ( e ‾ ) , τ 1 , τ 2 , σ 1 , σ 2 . ( τ = τ 1 ↷ σ 1 ↷ e v ( e ‾ ) ↷ σ 2 ⋅ τ 2 ⇒ σ 1 ⊆ σ 2 ) (3) \forall ev(\overline{e}), \tau_1, \tau_2, \sigma_1, \sigma_2.\; \left(\tau= \tau_1 \mathrel{\curvearrowright} \sigma_1 \mathrel{\curvearrowright} ev(\overline{e}) \mathrel{\curvearrowright} \sigma_2 \cdot\tau_2 \mathrel{\Rightarrow} \sigma_1 \subseteq\sigma_2\right) \tag*{(3)} ∀ e v ( e ) , τ 1 , τ 2 , σ 1 , σ 2 . ( τ = τ 1 ↷ σ 1 ↷ e v ( e ) ↷ σ 2 ⋅ τ 2 ⇒ σ 1 ⊆ σ 2 ) (3) Condition (1) ensures that each variable is either symbolic in all states or in no state. Condition (2) holds if all variables in events are symbolic. Finally, condition (3) states that the state succeeding an event is an extension of the state preceding the event. σ 1 ⊆ σ 2 \sigma_1 \subseteq\sigma_2 σ 1 ⊆ σ 2 expresses that σ 2 \sigma_2 σ 2 is an extension of σ 1 \sigma_1 σ 1 (see Definition 2.3 ); hence, events may not alter the state but only introduce new variables to it.
If a well-formed trace contains only concrete states (i.e., S = ∅ S = \varnothing S = ∅ ), we call it concrete or shining. Shining traces are denoted by s h sh s h .
Proposition 2.12. The concretization of a well-formed trace is a well-formed shining trace.
LAGC Semantics for Rust
This section presents an LAGC semantics for synchronous, safe Rust. Concretely, we cover the expressions and statements in Fig. 1. Type, trait, and module definitions are left out, as they have no evaluation and, therefore, require no LAGC treatment. Note that, due to our focus on the HIR, macros are already expanded (though we still denote panic expressions, i.e., forceful errors that stop the program’s execution, as panic!() for readability) and all loops are transformed into the singular loop expression. For clarity, we assume a number of straightforward normalizations of the HIR expressions: (i) all loops, break, and continue expressions have unique labels; (ii) all blocks, and break expressions contain expressions, e.g., break 'l 2 * 3 where l is the label and 2 * 3 is the break’s expression (this is achieved by inserting the unit expression “()” where necessary); (iii) all if expressions have an else branch, and (iv) all functions have at least one explicit return expression.
Observe that most syntactic structures that would be statements in other languages are expressions in Rust, e.g., if and loop expressions. The intuitive semantics of such expressions is explained alongside the formal semantics but we refer to the Rust book [42 ] , the Rust reference [57 ] , and the ferrocene language specification (FLS) [63 ] for details.
The syntax of expressions also contains an additional construct needed to model function calls ergonomically: ?i, where i ∈ C I d i \in\mathit{CId} i ∈ CId is a call identifier , serves as a stand-in for the value that is to be returned by the function call with identifier i i i . The precise, formal semantics are outlined below.
e ∈ E x p : : = v ∣ x ∣ ! e ∣ − e ∣ ∗ e ∣ e o p e ∣ a e = e ∣ [ e ] ∣ ( e ) ∣ e ( e ) ∣ e . i d e n t i f i e r ( e ) ∣ m a t c h e { a r m } ∣ e . i d e n t i f i e r ∣ & e ∣ & m u t e ∣ b r e a k ′ l e ∣ c o n t i n u e ′ l ∣ r e t u r n e ∣ x { i d e n t i f i e r : e } ∣ [ e ; n ] ∣ { s e } ∣ i f e e e l s e e ∣ ′ l : l o o p e ∣ p a n i c ! ( ) ∣ ? i s ∈ S t m t : : = e ∣ l e t p a t = e ; l ∈ L a b e l : : = i d e n t i f i e r i ∈ C I d : : = c a l l i d e n t i f i e r o p ∈ O p : : = + ∣ − ∣ ∗ ∣ … ∣ + = ∣ − = ∣ ∗ = ∣ … a r m ∈ A r m : : = p a t ⇒ e p a t ∈ P a t : : = _ ∣ v ∣ x ∣ x { i d e n t i f i e r : p a t } ∣ x ( p a t ) ∣ ( p a t ) ∣ ( p a t ∣ p a t ) ∣ e . . e ∣ e . . = e ∣ e . . ∣ e . . = ∣ . . e ∣ . . = e a e ∈ A s s i g n E x p : : = p e ∣ _ ∣ [ a e ] ∣ x { i d e n t i f i e r : a e } ∣ ( a e ) ∣ x ( a e ) p e ∈ P l a c e E x p : : = x ∣ p e [ e ] ∣ p e . i d e n t i f i e r ∣ ∗ p e f o o ∈ F n : : = f n i d e n t i f i e r ( x ‾ ) e r ∈ P r o g r a m : : = f o o f n m a i n ( ) e \begin{aligned}
e \in Exp &::= v \mid x \mid!e \mid-e \mid*e \mid e\ op\ e \mid ae = e \mid[e] \mid(e) \mid e(e) \mid e.\mathit{identifier}(e) \\
&\mid\mathit{match}\ e\ \{\mathit{arm}\} \mid e.\mathit{identifier} \mid\&e \mid\&\mathit{mut}\ e \\
&\mid\mathit{break}\ 'l\ e \mid\mathit{continue}'l \mid\mathit{return}\ e \mid x\ \{\mathit{identifier}:e\} \mid[e;n] \mid\{s\ e\} \\
&\mid\mathit{if}\ e\ e\ \mathit{else}\ e \mid'l:\mathit{loop}\ e \mid\mathit{panic!}() \mid?i \\
s \in Stmt &::= e \mid\mathit{let}\ pat = e; \\
l \in Label &::= \mathit{identifier} \\
i \in CId &::= \mathit{callidentifier} \\
op \in Op &::= + \mid- \mid* \mid\ldots\mid+= \mid-= \mid*= \mid\ldots\\
arm \in Arm &::= pat \Rightarrow e \\
pat \in Pat &::= \_ \mid v \mid x \mid x\ \{\mathit{identifier}:pat\} \mid x(pat) \mid(pat) \mid(pat \mid pat) \\
&\mid e..e \mid e..=e \mid e.. \mid e..= \mid..e \mid..=e \\
ae \in AssignExp &::= pe \mid\_ \mid[ae] \mid x\ \{\mathit{identifier}:ae\} \mid(ae) \mid x(ae) \\
pe \in PlaceExp &::= x \mid pe[e] \mid pe.\mathit{identifier} \mid*pe \\
foo \in Fn &::= \mathit{fn}\ \mathit{identifier}(\overline{x})\ e \\
r \in Program &::= foo\ \mathit{fn}\ \mathit{main}()\ e
\end{aligned} e ∈ E x p s ∈ S t m t l ∈ L ab e l i ∈ C I d o p ∈ O p a r m ∈ A r m p a t ∈ P a t a e ∈ A ss i g n E x p p e ∈ P l a ce E x p f oo ∈ F n r ∈ P r o g r am ::= v ∣ x ∣ ! e ∣ − e ∣ ∗ e ∣ e o p e ∣ a e = e ∣ [ e ] ∣ ( e ) ∣ e ( e ) ∣ e . identifier ( e ) ∣ match e { arm } ∣ e . identifier ∣ & e ∣ & mut e ∣ break ′ l e ∣ continue ′ l ∣ return e ∣ x { identifier : e } ∣ [ e ; n ] ∣ { s e } ∣ if e e else e ∣ ′ l : loop e ∣ panic ! ( ) ∣ ? i ::= e ∣ let p a t = e ; ::= identifier ::= callidentifier ::= + ∣ − ∣ ∗ ∣ … ∣ + =∣ − =∣ ∗ =∣ … ::= p a t ⇒ e ::= _ ∣ v ∣ x ∣ x { identifier : p a t } ∣ x ( p a t ) ∣ ( p a t ) ∣ ( p a t ∣ p a t ) ∣ e .. e ∣ e .. = e ∣ e .. ∣ e .. =∣ .. e ∣ .. = e ::= p e ∣ _ ∣ [ a e ] ∣ x { identifier : a e } ∣ ( a e ) ∣ x ( a e ) ::= x ∣ p e [ e ] ∣ p e . identifier ∣ ∗ p e ::= fn identifier ( x ) e ::= f oo fn main ( ) e
fig:1 . The syntax of Rust’s HIR covered in this section.
Some rules below, especially regarding assignments, also consider important subsets of E x p Exp E x p , which we enumerate above: Place expressions (P l a c e E x p ⊂ E x p PlaceExp \subset Exp P l a ce E x p ⊂ E x p ) are expressions that represent a memory location, such as local variables, index expressions, dereferences, and field expressions. Assignee expressions (A s s i g n E x p ⊂ E x p AssignExp \subset Exp A ss i g n E x p ⊂ E x p ) are expressions that can appear on the left-hand side of assignments. Aside from place expressions and the underscore expression (i.e., an assignment to nowhere), these are used for destructuring. If a place expression p e pe p e contains only values in the index and no dereferences, then p e ∈ P l a c e pe \in Place p e ∈ P l a ce . Further, if every place expression in an assignee expression a e ae a e is in P l a c e Place P l a ce , it is an element of the assignee value set A s s i g n V a l ⊂ E x p AssignVal \subset Exp A ss i g nV a l ⊂ E x p .
The considered subset covers almost all of safe Rust, the only limitation being closures, which can be translated to normal functions. Async Rust’s features are discussed in § 4 . We are interested in an operational semantics of Rust, hence we abstract away from memory layout and typing. All expressions and functions in programs must be well-typed and all types must be known.
Example 3.1 (Running Example). We consider a running example in this section which highlights many important Rust features considered here, see Listing 1. It contains three functions: fac, computing the factorial recursively, set_all setting all fields of an array passed via a mutable reference to a given value. The main function calls the other two with example arguments.
Listing 1. A simple Rust program adhering to the simplifications above.
1 fn fac(n: u64) -> u64 {
2 return if n <= 1 { 1 }
3 else {
4 let left = n; let right = fac(n - 1);
5 left * right }; ()
6 }
7 fn set_all(arr: &mut [u64; 3], v: u64) {
8 let mut i = 0;
9 'l: loop { if i == 3 { break 'l () } else {()}
10 *a[i] = v; i += 1 }
11 return (); ()
12 }
13 fn main() {
14 let b = false;
15 let c = !b;
16 let f = fac(4);
17 let arr = [8; 3];
18 set_all(&mut arr, f);
19 return (); ()
20 }
Local Evaluation
We define a local evaluation function val σ : E x p → 2 C T r \operatorname{val}_{\sigma} : \mathit{Exp} \to2^{\mathit{CTr}} val σ : Exp → 2 CTr , mapping Rust expressions to a set of symbolic continuation traces relative to state σ \sigma σ . Each such trace contains state changes, emitted events, and the continuation marker, denoting the expression or value with which we continue. We define one local rule per kind of expression (syntax in Fig. 1). Due to the rich syntax, the number of rules is quite large. Since Rust lacks a full, official specification [34 ] , our local rules follow the Rust reference [57 ] and the unofficial yet “adopted” FLS [63 ] . Observe that for the subset we consider, every program has a deterministic execution.
Values are already fully evaluated. Hence, we define a value’s local evaluation as the empty set.
val σ ( v ) = ∅ (4) \operatorname{val}_{\sigma}(v) = \varnothing \tag*{(4)} val σ ( v ) = ∅ (4)
Reading a local variable produces a set with a single continuation trace, starting with the state σ \sigma σ and a continuation with x x x ’s value in σ \sigma σ .
val σ ( x ) = { ⟨ σ ⟩ ⋅ K ( σ ( x ) ) } (5) \operatorname{val}_{\sigma}(x) = \{ \langle\sigma\rangle\cdot K(\sigma(x)) \} \tag*{(5)} val σ ( x ) = {⟨ σ ⟩ ⋅ K ( σ ( x ))} (5)
Evaluation of Sub-Expressions
A common pattern in the following rules and expressions is that an expression e e e contains sub-expressions, which need to be evaluated before e e e ’s top-level operator can be evaluated. For instance, for the expression ( 5 + 1 ) / 3 (5 + 1) / 3 ( 5 + 1 ) /3 , the division cannot be evaluated until the sub-expression 5 + 1 5 + 1 5 + 1 is fully evaluated. Therefore, we require a way to obtain for a given expression e e e the next sub-expression e ′ e' e ′ to be evaluated. However, in Rust, what this sub-expression is, depends on the context: If we evaluate a [ 1 + 1 ] a[1 + 1] a [ 1 + 1 ] to a value, then “a” is the next sub-expression. However, evaluating it in an assignment context , e.g., a [ 1 + 1 ] = 4 / 2 a[1 + 1] = 4 / 2 a [ 1 + 1 ] = 4/2 , the behavior is different: Because the left-hand side of an assignment is evaluated not to values but to assignee values (AssignVal ), the next sub-expression is 1 + 1 1 + 1 1 + 1 .
For that reason, we also require the context of the expression in addition to the next sub-expression to be evaluated. This allows us to evaluate the next sub-expression and then replace the old sub-expression with the result, leaving the rest of the expression unaltered. For this, we introduce hole expressions . Let HoleExp with typical element h e he h e be defined analogous Exp but with a new base case, a hole ∙ \bullet ∙ , similar to holes in types and terms in functional programming [49 , 67 ] . We further require that ∙ \bullet ∙ occurs exactly once per h e ∈ H o l e E x p he \in\mathit{HoleExp} h e ∈ HoleExp . For example, ∙ / 3 \bullet/ 3 ∙ /3 is a hole expression. This allows us to extract a sub-expression from an expression and substitute it without altering the rest. We denote filling the hole in h e ∈ H o l e E x p he \in\mathit{HoleExp} h e ∈ HoleExp with expression e e e as h e ⟨ ⟨ e ⟩ ⟩ he\langle\langle e\rangle\rangle h e ⟨⟨ e ⟩⟩ . The result is an element of Exp .
We now obtain the next sub-expression with the function n e x t : E x p × 2 E x p → E x p × H o l e E x p next : \mathit{Exp} \times2^{\mathit{Exp}} \to\mathit{Exp} \times\mathit{HoleExp} n e x t : Exp × 2 Exp → Exp × HoleExp , which accurately models Rust’s evaluation order using the sub-expression’s context as second argument. For example, n e x t ( ( 5 + 1 ) / 3 , V a l ) next((5 + 1) / 3, \mathit{Val}) n e x t (( 5 + 1 ) /3 , Val ) is expected to yield ( 5 + 1 , ∙ / 3 ) (5 + 1, \bullet/ 3) ( 5 + 1 , ∙ /3 ) , which allows us to evaluate the next sub-expression 5 + 1 5 + 1 5 + 1 to the value 6 6 6 which can then be inserted into the hole. We get ( ∙ / 3 ) ⟨ ⟨ 6 ⟩ ⟩ = 6 / 3 (\bullet/ 3)\langle\langle6\rangle\rangle= 6 / 3 ( ∙ /3 ) ⟨⟨ 6 ⟩⟩ = 6/3 . The next function must be defined such that for all e , e ′ , h e e, e', he e , e ′ , h e , and M M M such that n e x t ( e , M ) = ( e ′ , h e ) next(e, M) = (e', he) n e x t ( e , M ) = ( e ′ , h e ) , we have h e ⟨ ⟨ e ′ ⟩ ⟩ = e he\langle\langle e'\rangle\rangle= e h e ⟨⟨ e ′ ⟩⟩ = e .
The function n e x t next n e x t is defined recursively and, as mentioned before, it carries the context as its second argument. The recursion stops when reaching an element of this set. Typically it is the set V a l \mathit{Val} Val or A s s i g n V a l \mathit{AssignVal} AssignVal . By default we are the context V a l \mathit{Val} Val . For example, to evaluate the expression a [ 1 + 1 ] = 4 / 2 a[1 + 1] = 4 / 2 a [ 1 + 1 ] = 4/2 , we determine the next sub-expression by n e x t ( a [ 1 + 1 ] = 4 / 2 , V a l ) next(a[1 + 1] = 4 / 2, \mathit{Val}) n e x t ( a [ 1 + 1 ] = 4/2 , Val ) . In this case, first 4 / 2 4 / 2 4/2 is evaluated, then a [ 1 + 1 ] a[1 + 1] a [ 1 + 1 ] . However, when we have a [ 1 + 1 ] = 2 a[1 + 1] = 2 a [ 1 + 1 ] = 2 , we must consider the left-hand side by calling n e x t ( a [ 1 + 1 ] , A s s i g n V a l ) next(a[1 + 1], \mathit{AssignVal}) n e x t ( a [ 1 + 1 ] , AssignVal ) , because do not want to evaluate a [ 1 + 1 ] a[1 + 1] a [ 1 + 1 ] to a value but to get the assignee value (in this case, it is a place) a [ 2 ] a[2] a [ 2 ] . The latter call yields ( 1 + 1 , a [ ∙ ] ) (1 + 1, a[\bullet]) ( 1 + 1 , a [ ∙ ]) .
We need to define n e x t next n e x t for all Rust expressions. For instance, the next sub-expression of the negation expression ! e !e ! e is defined as follows: If e e e also has a further sub-expression that is to evaluated, we return it. Otherwise, if e e e is not in V a l Val V a l , it is the next sub-expression and should be evaluated to a value. We always pass V a l Val V a l to the recursive call, because negations cannot be part of an assignee expression.
next ( ! e , M ) = { ( ! e , ∙ ) if e ∈ V a l ( e ′ , ! h e ) if e ∉ V a l ∧ ( e ′ , h e ) = next ( e , V a l ) \operatorname{next}(!e, M) =
\begin{cases}
(!e, \bullet) & \text{if } e \in Val \\
(e', !he) & \text{if } e \notin Val \land(e', he) = \operatorname{next}(e, Val)
\end{cases} next ( ! e , M ) = { ( ! e , ∙ ) ( e ′ , ! h e ) if e ∈ V a l if e ∈ / V a l ∧ ( e ′ , h e ) = next ( e , V a l )
The expression if e 1 e_1 e 1 e 2 e_2 e 2 else e 3 e_3 e 3 behaves similarly. Because the branches are only conditionally evaluated depending on e 1 e_1 e 1 ’s value, they should not be considered as sub-expressions to be evaluated. Hence, we only consider the condition and obtain the same cases and results as for negation.
next ( if e 1 e 2 else e 3 , M ) = { ( if e 1 e 2 else e 3 , ∙ ) if e 1 ∈ V a l ( e 1 ′ , if h e e 2 else e 3 ) if e 1 ∉ V a l ∧ ( e 1 ′ , h e ) = next ( e 1 , V a l ) \operatorname{next}(\text{if } e_1\ e_2\ \text{else } e_3, M) =
\begin{cases}
(\text{if } e_1\ e_2\ \text{else } e_3, \bullet) & \text{if } e_1 \in Val \\
(e_1', \text{if } he\ e_2\ \text{else } e_3) & \text{if } e_1 \notin Val \land(e_1', he) = \operatorname{next}(e_1, Val)
\end{cases} next ( if e 1 e 2 else e 3 , M ) = { ( if e 1 e 2 else e 3 , ∙ ) ( e 1 ′ , if h e e 2 else e 3 ) if e 1 ∈ V a l if e 1 ∈ / V a l ∧ ( e 1 ′ , h e ) = next ( e 1 , V a l )
An assignment a e = e ae = e a e = e is evaluated by first evaluating the right-hand side e e e to a value and then the left-hand side a e ae a e to an assignee value. Hence, the second case, where e e e is not a value and may have further sub-expressions, behaves as above. If e e e is a value, we treat a e ae a e similarly, but denote that a e ae a e is to be evaluated to an assignee value. Observe that a e ae a e may contain sub-expression that must be evaluated to values, e.g., 1 + 1 1 + 1 1 + 1 in a[1 + 1 1 + 1 1 + 1 ] = 4 / 2 4 / 2 4/2 . Because assignments occur only on the right-hand side, we do not consider M M M .
next ( a e = e , M ) = { ( a e = e , ∙ ) if e ∈ V a l ∧ a e ∈ A s s i g n V a l ( e ′ , a e = h e ) if e ∉ V a l ∧ ( e ′ , h e ) = next ( e , V a l ) ( a e ′ , h e = e ) if e ∈ V a l ∧ a e ∉ A s s i g n V a l ∧ ( a e ′ , h e ) = next ( a e , A s s i g n V a l ) \operatorname{next}(ae = e, M) =
\begin{cases}
(ae = e, \bullet) & \text{if } e \in Val \land ae \in AssignVal \\
(e', ae = he) & \text{if } e \notin Val \land(e', he) = \operatorname{next}(e, Val) \\
(ae', he = e) & \text{if } e \in Val \land ae \notin AssignVal \land(ae', he) = \operatorname{next}(ae, AssignVal)
\end{cases} next ( a e = e , M ) = ⎩ ⎨ ⎧ ( a e = e , ∙ ) ( e ′ , a e = h e ) ( a e ′ , h e = e ) if e ∈ V a l ∧ a e ∈ A ss i g nV a l if e ∈ / V a l ∧ ( e ′ , h e ) = next ( e , V a l ) if e ∈ V a l ∧ a e ∈ / A ss i g nV a l ∧ ( a e ′ , h e ) = next ( a e , A ss i g nV a l )
The second parameter matters when the expression can be an assignee expression, as in that case sub-expressions should not be considered if they are places rather than values. For the index expression e 1 [ e 2 ] e_1[e_2] e 1 [ e 2 ] , if we are in an assignment context, e 1 e_1 e 1 is not a sub-expression if it is a place, e.g., x or x[1 ] . However, the indexing expression e 2 e_2 e 2 must be evaluated to a value.
next ( e 1 [ e 2 ] , M ) = { ( e 1 [ e 2 ] , ∙ ) if e 1 ∈ M ∧ e 2 ∈ V a l ( e 1 ′ , h e [ e 2 ] ) if e 1 ∉ M ∧ ( e 1 ′ , h e ) = next ( e 1 , M ) ( e 1 [ e 2 ′ ] , e 1 [ h e ] ) if e 1 ∈ M ∧ e 2 ∉ V a l ∧ ( e 2 ′ , h e ) = next ( e 2 , V a l ) \operatorname{next}(e_1[e_2], M) =
\begin{cases}
(e_1[e_2], \bullet) & \text{if } e_1 \in M \land e_2 \in Val \\
(e_1', he[e_2]) & \text{if } e_1 \notin M \land(e_1', he) = \operatorname{next}(e_1, M) \\
(e_1[e_2'], e_1[he]) & \text{if } e_1 \in M \land e_2 \notin Val \land(e_2', he) = \operatorname{next}(e_2, Val)
\end{cases} next ( e 1 [ e 2 ] , M ) = ⎩ ⎨ ⎧ ( e 1 [ e 2 ] , ∙ ) ( e 1 ′ , h e [ e 2 ]) ( e 1 [ e 2 ′ ] , e 1 [ h e ]) if e 1 ∈ M ∧ e 2 ∈ V a l if e 1 ∈ / M ∧ ( e 1 ′ , h e ) = next ( e 1 , M ) if e 1 ∈ M ∧ e 2 ∈ / V a l ∧ ( e 2 ′ , h e ) = next ( e 2 , V a l )
We do not enumerate all the other definitions for the remaining expressions here. See Appendix § 10 for the full definitions.
This helper function allows us to express the pattern of evaluating sub-expressions concisely as a local evaluation rule. We evaluate the first sub-expression one step, attach the trace generated by the sub-expression, and rebuild the expression with the sub-expression replaced by its continuation. Rule (6) considers only the cases where next ( e , V a l ) = ( e ′ , h e ) \operatorname{next}(e, Val) = (e', he) next ( e , V a l ) = ( e ′ , h e ) and h e ≠ ∙ he \ne\bullet h e = ∙ ; for all other expressions, the rule is not applicable.
val σ ( e ) = { τ ⋅ K ( h e ⟨ e ′ ′ ⟩ ) ∣ τ ⋅ K ( e ′ ′ ) ∈ val σ ( e ′ ) } where next ( e , V a l ) = ( e ′ , h e ) ∧ h e ≠ ∙ (6) \operatorname{val}_{\sigma}(e) = \{ \tau\cdot K(he\langle e''\rangle) \mid\tau\cdot K(e'') \in\operatorname{val}_{\sigma}(e') \}
\quad\text{where } \operatorname{next}(e, Val) = (e', he) \land he \ne\bullet \tag*{(6)} val σ ( e ) = { τ ⋅ K ( h e ⟨ e ′′ ⟩) ∣ τ ⋅ K ( e ′′ ) ∈ val σ ( e ′ )} where next ( e , V a l ) = ( e ′ , h e ) ∧ h e = ∙ (6)
With this rule in place, the remaining rules can assume that all sub-expressions are (assignment) values, simplifying the presentation.
Arithmetic and Logic Operators
For clarity, we split the rules for operators, which are typically overloaded via traits, into multiple definitions for different types. For bool , negation is defined as:
val σ ( ! v ) = { { ⟨ σ ⟩ ⋅ K ( ff ) } if v = tt { ⟨ σ ⟩ ⋅ K ( tt ) } if v = ff (7) \operatorname{val}_{\sigma}(!v) =
\begin{cases}
\{ \langle\sigma\rangle\cdot K(\text{ff}) \} & \text{if } v = \text{tt} \\
\{ \langle\sigma\rangle\cdot K(\text{tt}) \} & \text{if } v = \text{ff}
\end{cases} \tag*{(7)} val σ ( ! v ) = { {⟨ σ ⟩ ⋅ K ( ff )} {⟨ σ ⟩ ⋅ K ( tt )} if v = tt if v = ff (7)
We present bitwise negation only for the case where e e e is of type u8; all signed and unsigned integer types are analogous. Here, − ^ \mathbin{\hat{-}} − ^ represents the mathematical subtraction operation over Z \mathbb{Z} Z rather than the Rust operator.
val σ ( ! v ) = { ⟨ σ ⟩ ⋅ K ( 2 8 − ^ v − ^ 1 ) } (8) \operatorname{val}_{\sigma}(!v)=\{\langle\sigma\rangle\cdot K(2^{8}\mathbin{\hat{-}}v\mathbin{\hat{-}}1)\} \tag*{(8)} val σ ( ! v ) = {⟨ σ ⟩ ⋅ K ( 2 8 − ^ v − ^ 1 )} (8)
Finally, for all other types, negation is resolved by calling the not function of the Not trait, as in rule (9). The trait is in module core::ops, hence, we add the module to the call. We stress that the rules are modular, as which rule is applied depends strictly on the local expression and its type, not the wider context.
val σ ( ! v ) = val σ ( core::ops::Not::not ( v ) ) (9) \operatorname{val}_{\sigma}(!v)=\operatorname{val}_{\sigma}(\texttt{core::ops::Not::not}(v)) \tag*{(9)} val σ ( ! v ) = val σ ( core::ops::Not::not ( v )) (9)
Example 3.2. Assume the program in Listing 1. Let σ 1 \sigma_1 σ 1 be the state [ b ↦ f f ] [\mathrm{b}\mapsto\mathrm{ff}] [ b ↦ ff ] . The local evaluation of b \mathrm{b} b in σ 1 \sigma_1 σ 1 yields val σ 1 ( b ) = { ⟨ σ 1 ⟩ ⋅ K ( f f ) } \operatorname{val}_{\sigma_1}(\mathrm{b})=\{\langle\sigma_1\rangle\cdot K(\mathrm{ff})\} val σ 1 ( b ) = {⟨ σ 1 ⟩ ⋅ K ( ff )} . Locally evaluating !ff in σ 1 \sigma_1 σ 1 gives { ⟨ σ 1 ⟩ ⋅ K ( t t ) } \{\langle\sigma_1\rangle\cdot K(\mathrm{tt})\} {⟨ σ 1 ⟩ ⋅ K ( tt )} . The evaluation of !b in line 15 in σ 1 \sigma_1 σ 1 is val σ 1 ( ! b ) = { τ ⋅ K ( ! e ) ∣ τ ⋅ K ( e ) ∈ val σ 1 ( b ) } \operatorname{val}_{\sigma_1}(!\mathrm{b})=\{\tau\cdot K(!e)\mid\tau\cdot K(e)\in\operatorname{val}_{\sigma_1}(\mathrm{b})\} val σ 1 ( ! b ) = { τ ⋅ K ( ! e ) ∣ τ ⋅ K ( e ) ∈ val σ 1 ( b )} , which we simplify to { ⟨ σ 1 ⟩ ⋅ K ( ! f f ) } \{\langle\sigma_1\rangle\cdot K(!\mathrm{ff})\} {⟨ σ 1 ⟩ ⋅ K ( ! ff )} .
Sign negation is similar to bitwise negation, in that it is overloaded. We present the version for i8 in rule (10); other integer types are similar. If the result of the negation would be outside the type’s range (in i8’s case, [ − 128 , 127 ] [-128,127] [ − 128 , 127 ] ), a panic occurs.
val σ ( − v ) = { { ⟨ σ ⟩ ⋅ K ( − ^ v ) } if − 128 < v < 128 { ⟨ σ ⟩ ⋅ K ( panic! ( ) ) } if v = − 128 (10) \operatorname{val}_{\sigma}(-v)=
\begin{cases}
\{\langle\sigma\rangle\cdot K(\mathbin{\hat{-}}v)\} & \text{if } -128<v<128\\
\{\langle\sigma\rangle\cdot K(\texttt{panic!}())\} & \text{if } v=-128
\end{cases} \tag*{(10)} val σ ( − v ) = { {⟨ σ ⟩ ⋅ K ( − ^ v )} {⟨ σ ⟩ ⋅ K ( panic! ( ))} if − 128 < v < 128 if v = − 128 (10)
For all other types, the Neg trait’s function is invoked.
val σ ( − v ) = val σ ( core::ops::Neg::neg ( v ) ) (11) \operatorname{val}_{\sigma}(-v)=\operatorname{val}_{\sigma}(\texttt{core::ops::Neg::neg}(v)) \tag*{(11)} val σ ( − v ) = val σ ( core::ops::Neg::neg ( v )) (11)
Addition is handled similarly to negation above: If the sub-values v 1 , v 2 v_1,v_2 v 1 , v 2 can be added without exceeding the bounds of the type, we continue with this value; otherwise we emit a panic. For non-integer types, the Add trait is used. Rule (12) shows the case for u8, where + ^ \mathbin{\hat{+}} + ^ again stands for the mathematical addition over Z \mathbb{Z} Z .
val σ ( v 1 + v 2 ) = { { ⟨ σ ⟩ ⋅ K ( v 1 + ^ v 2 ) } if v 1 + ^ v 2 ≤ 255 { ⟨ σ ⟩ ⋅ K ( panic! ( ) ) } if v 1 + ^ v 2 > 255 (12) \operatorname{val}_{\sigma}(v_1+v_2)=
\begin{cases}
\{\langle\sigma\rangle\cdot K(v_1\mathbin{\hat{+}}v_2)\} & \text{if } v_1\mathbin{\hat{+}}v_2\le255\\
\{\langle\sigma\rangle\cdot K(\texttt{panic!}())\} & \text{if } v_1\mathbin{\hat{+}}v_2>255
\end{cases} \tag*{(12)} val σ ( v 1 + v 2 ) = { {⟨ σ ⟩ ⋅ K ( v 1 + ^ v 2 )} {⟨ σ ⟩ ⋅ K ( panic! ( ))} if v 1 + ^ v 2 ≤ 255 if v 1 + ^ v 2 > 255 (12)
Compound assignment proceeds by invoking the appropriate function of the AddAssign trait. Observe that the left-hand side is a place and the right-hand side a value.
val σ ( p + = v ) = { ⟨ σ ⟩ ⋅ K ( AddAssign::add_assign ( & mut p , v ) ) } (13) \operatorname{val}_{\sigma}(p+=v)=\{\langle\sigma\rangle\cdot K(\texttt{AddAssign::add\_assign}(\&\texttt{mut }p,v))\} \tag*{(13)} val σ ( p + = v ) = {⟨ σ ⟩ ⋅ K ( AddAssign::add_assign ( & mut p , v ))} (13)
References
References are either shared (also called “immutable”) or mutable. As discussed in § 2 , we represent shared references as Ref p s ( b ) \operatorname{Ref}^{s}_{p}(b) Ref p s ( b ) and mutable references as Ref p m ( b ) \operatorname{Ref}^{m}_{p}(b) Ref p m ( b ) , where p p p is the borrowed place and b b b is a borrow identifier.
Rules (14) and (15) describe the evaluation of borrowing from a place p p p to create a shared and mutable reference, respectively. Note that rules (14) and (15) create fresh borrow identifiers, so that two references created by different expressions are distinct.
val σ ( & p ) = { ⟨ σ ⟩ ⋅ K ( Ref p s ( b ) ) ∣ b ∈ BId , b fresh } (14) \operatorname{val}_{\sigma}(\&p)=\{\langle\sigma\rangle\cdot K(\operatorname{Ref}^{s}_{p}(b))\mid b\in\operatorname{BId},b\text{ fresh}\} \tag*{(14)} val σ ( & p ) = {⟨ σ ⟩ ⋅ K ( Ref p s ( b )) ∣ b ∈ BId , b fresh } (14)
val σ ( & mut p ) = { ⟨ σ ⟩ ⋅ K ( Ref p m ( b ) ) ∣ b ∈ BId , b fresh } (15) \operatorname{val}_{\sigma}(\&\texttt{mut }p)=\{\langle\sigma\rangle\cdot K(\operatorname{Ref}^{m}_{p}(b))\mid b\in\operatorname{BId},b\text{ fresh}\} \tag*{(15)} val σ ( & mut p ) = {⟨ σ ⟩ ⋅ K ( Ref p m ( b )) ∣ b ∈ BId , b fresh } (15)
Example 3.3. Continuing the running example, evaluating the mutable reference expression in line 18 in a state σ 2 = [ a r r ↦ [ 8 , 8 , 8 ] ] \sigma_2=[\mathrm{arr}\mapsto[8,8,8]] σ 2 = [ arr ↦ [ 8 , 8 , 8 ]] yields val σ 2 ( & mut a r r ) = { ⟨ σ 2 ⟩ ⋅ K ( Ref a r r m ( b ) ) ∣ b ∈ BId } \operatorname{val}_{\sigma_2}(\&\texttt{mut }\mathrm{arr})=\{\langle\sigma_2\rangle\cdot K(\operatorname{Ref}^{m}_{\mathrm{arr}}(b))\mid b\in\operatorname{BId}\} val σ 2 ( & mut arr ) = {⟨ σ 2 ⟩ ⋅ K ( Ref arr m ( b )) ∣ b ∈ BId } . Observe, that we do not evaluate arr to create the mutable reference: a variable is a fully evaluated place and the reference need not know the borrowed variable’s value. The generated set is infinite if we assume B I d BId B I d to be infinite.
Dereferencing an expression (∗ v *v ∗ v ) has multiple possible evaluations. If v v v is of a pointer type (&T or &mut T), dereferencing evaluates to the referenced value. However, this is only legal if T implements the Copy trait. Rule (16) handles this case.
val σ ( ∗ v ) = { ⟨ σ ⟩ ⋅ K ( σ ( p ) ) } if v = Ref p s ( b ) ∨ v = Ref p m ( b ) (16) \operatorname{val}_{\sigma}(*v) = \left\{ \langle\sigma\rangle\cdot K(\sigma(p)) \right\} \quad\text{if } v = \operatorname{Ref}^{s}_{p}(b) \lor v = \operatorname{Ref}^{m}_{p}(b) \tag*{(16)} val σ ( ∗ v ) = { ⟨ σ ⟩ ⋅ K ( σ ( p )) } if v = Ref p s ( b ) ∨ v = Ref p m ( b ) (16)
If v v v ’s type is not a pointer type, dereferencing is evaluated via the Deref trait. As the trait’s function takes a shared reference, we also add a borrow expression.
val σ ( ∗ v ) = { ⟨ σ ⟩ ⋅ K ( ∗ core::ops::Deref::deref ( & v ) ) } (17) \operatorname{val}_{\sigma}(*v) = \left\{ \langle\sigma\rangle\cdot K(*\operatorname{core::ops::Deref::deref}(\&v)) \right\} \tag*{(17)} val σ ( ∗ v ) = { ⟨ σ ⟩ ⋅ K ( ∗ core::ops::Deref::deref ( & v )) } (17)
Assignments
Assignments encompass a range of possible evaluations depending on the left-hand side (the “assignee expression”). We split them into different rules for clarity. Rule (18) handles the case where we assign to _, i.e., to nowhere. This assignment does not affect the state and evaluates to the unit value.
val σ ( _ = v ) = { ⟨ σ ⟩ ⋅ K ( ( ) ) } (18) \operatorname{val}_{\sigma}(\_ = v) = \left\{ \langle\sigma\rangle\cdot K(()) \right\} \tag*{(18)} val σ ( _ = v ) = { ⟨ σ ⟩ ⋅ K (( )) } (18)
Assigning to a program variable simply updates the state at this variable.
val σ ( x = v ) = { ⟨ σ ⟩ ⇝ σ [ x ↦ v ] ⋅ K ( ( ) ) } (19) \operatorname{val}_{\sigma}(x = v) = \left\{ \langle\sigma\rangle\mathrel{\leadsto} \sigma[x \mapsto v] \cdot K(()) \right\} \tag*{(19)} val σ ( x = v ) = { ⟨ σ ⟩ ⇝ σ [ x ↦ v ] ⋅ K (( )) } (19)
An assignment to the field on an array of type [T; N], where T is the array’s element type and N the array’s size, has two cases: If the index is less than the array’s length, the state is updated at that location; otherwise, a panic occurs. The index must be of type usize, hence it cannot be negative (see rule (20)). Index assignments to other types typically go through the IndexMut trait. In rule (20), v 1 , v 2 ∈ V a l v_1, v_2 \in Val v 1 , v 2 ∈ V a l and p ∈ P l a c e p \in Place p ∈ P l a ce is a place of type [T; N], i.e., a variable of the correct type, a field access like x.f, or an inner array x.f[2 ] . Rule (21) work similarly for struct and tuple fields, except no panic can occur.
val σ ( p [ v 1 ] = v 2 ) = { { ⟨ σ ⟩ ⇝ σ [ p [ n ] ↦ v ] ⋅ K ( ( ) ) } if n < N { ⟨ σ ⟩ ⋅ K ( panic! ( ) ) } otherwise (20) \operatorname{val}_{\sigma}(p[v_1] = v_2) =
\begin{cases}
\left\{ \langle\sigma\rangle\mathrel{\leadsto} \sigma[p[n] \mapsto v] \cdot K(()) \right\} & \text{if } n < N \\
\left\{ \langle\sigma\rangle\cdot K(\operatorname{panic!}()) \right\} & \text{otherwise}
\end{cases} \tag*{(20)} val σ ( p [ v 1 ] = v 2 ) = { { ⟨ σ ⟩ ⇝ σ [ p [ n ] ↦ v ] ⋅ K (( )) } { ⟨ σ ⟩ ⋅ K ( panic! ( )) } if n < N otherwise (20)
val σ ( p . i d e n t = v ) = { ⟨ σ ⟩ ⇝ σ [ p . i d e n t ↦ v ] ⋅ K ( ( ) ) } (21) \operatorname{val}_{\sigma}(p.\mathit{ident} = v) = \left\{ \langle\sigma\rangle\mathrel{\leadsto} \sigma[p.\mathit{ident} \mapsto v] \cdot K(()) \right\} \tag*{(21)} val σ ( p . ident = v ) = { ⟨ σ ⟩ ⇝ σ [ p . ident ↦ v ] ⋅ K (( )) } (21)
The dereferencing expression evaluates a reference to a place p p p which can then be assigned a value by the expression ∗ r = v *r = v ∗ r = v , where r r r is a reference. Because assigning to a dereferenced expression is only permitted when the reference is a mutable reference, we know that r = Ref p m ( b ) r = \operatorname{Ref}^{m}_{p}(b) r = Ref p m ( b ) for some p ∈ P l a c e p \in Place p ∈ P l a ce , b ∈ B I d b \in BId b ∈ B I d . Rule (22) simply updates the corresponding place.
val σ ( ∗ Ref p m ( b ) = v ) = { ⟨ σ ⟩ ⇝ σ [ p ↦ v ] ⋅ K ( ( ) ) } (22) \operatorname{val}_{\sigma}(*\operatorname{Ref}^{m}_{p}(b) = v) = \left\{ \langle\sigma\rangle\mathrel{\leadsto} \sigma[p \mapsto v] \cdot K(()) \right\} \tag*{(22)} val σ ( ∗ Ref p m ( b ) = v ) = { ⟨ σ ⟩ ⇝ σ [ p ↦ v ] ⋅ K (( )) } (22)
Next, we deal with the destructuring assignments , where parts of complex values can be assigned to multiple locations at once. Since the assignment is correctly typed, we know that the value on the right fits the assignee value on the left. Rules (23) to (26) deal with arrays, tuples, structs, and tuple structs, respectively. All are evaluated by introducing a block with assignments to the individual assignee values. Let T a s s i g n = { ⟨ σ ⟩ ⋅ K ( { a v 1 = v 1 , … , a v n = v n } ) } T_{\mathrm{assign}} = \left\{ \langle\sigma\rangle\cdot K(\{a v_1 = v_1, \ldots, a v_n = v_n\}) \right\} T assign = { ⟨ σ ⟩ ⋅ K ({ a v 1 = v 1 , … , a v n = v n }) } in the following, as all these assignments lead to the same set of traces.
val σ ( [ a v 1 , … , a v n ] = [ v 1 , … , v n ] ) = T a s s i g n (23) \operatorname{val}_{\sigma}([a v_1,\ldots,a v_n] = [v_1,\ldots,v_n]) = T_{\mathrm{assign}} \tag*{(23)} val σ ([ a v 1 , … , a v n ] = [ v 1 , … , v n ]) = T assign (23)
val σ ( ( a v 1 , … , a v n ) = ( v 1 , … , v n ) ) = T a s s i g n (24) \operatorname{val}_{\sigma}((a v_1,\ldots,a v_n) = (v_1,\ldots,v_n)) = T_{\mathrm{assign}} \tag*{(24)} val σ (( a v 1 , … , a v n ) = ( v 1 , … , v n )) = T assign (24)
val σ ( x { i d 1 : a v 1 , … , i d n : a v n } = x { i d 1 : v 1 , … , i d n : v n } ) = T a s s i g n (25) \operatorname{val}_{\sigma}(x\{id_1:a v_1,\ldots,id_n:a v_n\} = x\{id_1:v_1,\ldots,id_n:v_n\}) = T_{\mathrm{assign}} \tag*{(25)} val σ ( x { i d 1 : a v 1 , … , i d n : a v n } = x { i d 1 : v 1 , … , i d n : v n }) = T assign (25)
val σ ( x ( a v 1 , … , a v n ) = x ( v 1 , … , v n ) ) = T a s s i g n (26) \operatorname{val}_{\sigma}(x(a v_1,\ldots,a v_n) = x(v_1,\ldots,v_n)) = T_{\mathrm{assign}} \tag*{(26)} val σ ( x ( a v 1 , … , a v n ) = x ( v 1 , … , v n )) = T assign (26)
Example 3.4. We demonstrate the state changes incurred by an assignment by continuing Example 3.2: The assignment c = !b (line 15) in state σ 1 \sigma_1 σ 1 gives us the traces { τ ⋅ K ( c = e ) ∣ τ ⋅ K ( e ) ∈ val σ ( ! b ) } = { ⟨ σ 1 ⟩ ⋅ K ( c = ! f f ) } \{\tau\cdot\mathsf{K}(c = e) \mid\tau\cdot\mathsf{K}(e) \in\operatorname{val}_{\sigma}(!b)\} = \{\langle\sigma_1\rangle\cdot\mathsf{K}(c = !\mathsf{ff})\} { τ ⋅ K ( c = e ) ∣ τ ⋅ K ( e ) ∈ val σ ( ! b )} = {⟨ σ 1 ⟩ ⋅ K ( c = ! ff )} . The assignment c = true, the result of multiple local evaluations, yields: { ⟨ σ 1 ⟩ ⇝ σ 1 [ c ↦ t t ] ⋅ K ( ( ) ) } \{\langle\sigma_1\rangle\rightsquigarrow\sigma_1[c \mapsto\mathsf{tt}] \cdot\mathsf{K}(())\} {⟨ σ 1 ⟩ ⇝ σ 1 [ c ↦ tt ] ⋅ K (( ))} .
Compound Types
Array expressions are evaluated by evaluating their elements left-to-right. This is entirely handled by the rules in § 3.1.1 . The same approach works for tuples, struct, and enum expressions.
An array repeat expression creates an array value with the inner value repeated n n n times.
val σ ( [ v ; n ] ) = { ⟨ σ ⟩ ⋅ K ( [ v , … , v ⏟ n times ] ) } (27) \operatorname{val}_{\sigma}([v;n]) = \{\langle\sigma\rangle\cdot\mathsf{K}([\underbrace{v,\ldots,v}_{n\ \text{times} }])\} \tag*{(27)} val σ ([ v ; n ]) = {⟨ σ ⟩ ⋅ K ([ n times v , … , v ])} (27)
To access a struct field or tuple field, we simply take the field’s value from the state.
val σ ( v . i d e n t ) = { ⟨ σ ⟩ ⋅ K ( v ′ ) } where v = x { i d e n t : v ′ , … } (28) \operatorname{val}_{\sigma}(v.\mathit{ident}) = \{\langle\sigma\rangle\cdot\mathsf{K}(v')\} \quad\text{where } v = x\{\mathit{ident}:v',\ldots\} \tag*{(28)} val σ ( v . ident ) = {⟨ σ ⟩ ⋅ K ( v ′ )} where v = x { ident : v ′ , … } (28)
Accessing the index of an array is similar to a struct field, besides the necessary bounds check: if the index is at least the array’s length, a panic occurs. Since we know that v ′ v' v ′ is of type usize, we need not check for negative values. We only show the case for arrays; all other types are translated to a trait function call.
val σ ( v [ v ′ ] ) = { { ⟨ σ ⟩ ⋅ K ( v v ′ ) } if v = [ v 0 , … , v n ] , v ′ ≤ n { ⟨ σ ⟩ ⋅ K ( panic ! ( ) ) } otherwise (29) \operatorname{val}_{\sigma}(v[v']) =
\begin{cases}
\{\langle\sigma\rangle\cdot\mathsf{K}(v_{v'})\} & \text{if } v = [v_0,\ldots,v_n],v' \le n \\
\{\langle\sigma\rangle\cdot\mathsf{K}(\text{panic}!())\} & \text{otherwise}
\end{cases} \tag*{(29)} val σ ( v [ v ′ ]) = { {⟨ σ ⟩ ⋅ K ( v v ′ )} {⟨ σ ⟩ ⋅ K ( panic ! ( ))} if v = [ v 0 , … , v n ] , v ′ ≤ n otherwise (29)
Basic Control Flow
Evaluating a conditional expression such as if v v v e 1 e_1 e 1 else e 2 e_2 e 2 is straightforward: If condition v v v is the true value, we continue with the then expression e 1 e_1 e 1 ; if v v v is false, we continue with e 2 e_2 e 2 .
val σ ( if v e 1 else e 2 ) = { { ⟨ σ ⟩ ⋅ K ( e 1 ) } if v = t t { ⟨ σ ⟩ ⋅ K ( e 2 ) } if v = f f (30) \operatorname{val}_{\sigma}(\text{if }v\ e_1\ \text{else }e_2) =
\begin{cases}
\{\langle\sigma\rangle\cdot\mathsf{K}(e_1)\} & \text{if } v = \mathsf{tt} \\
\{\langle\sigma\rangle\cdot\mathsf{K}(e_2)\} & \text{if } v = \mathsf{ff}
\end{cases} \tag*{(30)} val σ ( if v e 1 else e 2 ) = { {⟨ σ ⟩ ⋅ K ( e 1 )} {⟨ σ ⟩ ⋅ K ( e 2 )} if v = tt if v = ff (30)
Example 3.5. When evaluating the if in line 2 in a state σ 3 = [ n ↦ 4 ] \sigma_3 = [n \mapsto4] σ 3 = [ n ↦ 4 ] , we evaluate the condition. Once the condition is simplified to f f \mathsf{ff} ff , we have val σ ( if f f { 1 } else { let left = … } ) = { ⟨ σ 3 ⟩ ⋅ K ( { let left = … } ) } \operatorname{val}_{\sigma}(\text{if }\mathsf{ff}\ \{1\}\ \text{else }\{\text{let left} = \ldots\}) = \{\langle\sigma_3\rangle\cdot\mathsf{K}(\{\text{let left} = \ldots\})\} val σ ( if ff { 1 } else { let left = … }) = {⟨ σ 3 ⟩ ⋅ K ({ let left = … })} .
A match expression is evaluated step-wise. First, we consider the first arm. This arm contains a pattern p a t pat p a t and a body e e e . If the pattern’s structure fits the matched value (the scrutinee), we want to continue with this arm. However, pattern matching may introduce new variable bindings: A match arm like x => {x + 2}, where x is some integer variable, does not compare the scrutinee’s value with the value of an existing variable x. Instead, a new variable x is added to the scope and it is bound to the scrutinee’s value.
To accommodate this binding pattern, we use the partial function patMatch σ : P a t × V a l ⇀ S t a t e \operatorname{patMatch}_{\sigma} : \mathit{Pat} \times\mathit{Val} \rightharpoonup\mathit{State} patMatch σ : Pat × Val ⇀ State , which takes a pattern and a value and returns an extension of state σ \sigma σ , that is, new values may be introduced but no existing values changed.
We cover a few important cases of the function patMatch σ \operatorname{patMatch}_{\sigma} patMatch σ below. The wildcard pattern “_” always matches, values (i.e., literal patterns) match if they are exactly the scrutinee’s value, binding patterns (x) introduce new variables, and the alternative pattern considers both sub-patterns and returns the first successful match. The other patterns follow the same approach. Note that we use the symbol “↑” to denote undefinedness.
patMatch σ ( _ , v ) = σ \operatorname{patMatch}_{\sigma}(\_,v)=\sigma patMatch σ ( _ , v ) = σ
patMatch σ ( v 1 , v 2 ) = { σ if v 1 = v 2 ↑ otherwise \operatorname{patMatch}_{\sigma}(v_1,v_2)=
\begin{cases}
\sigma& \text{if } v_1=v_2 \\
\uparrow& \text{otherwise}
\end{cases} patMatch σ ( v 1 , v 2 ) = { σ ↑ if v 1 = v 2 otherwise
patMatch σ ( x , v ) = σ [ x ↦ v ] \operatorname{patMatch}_{\sigma}(x,v)=\sigma[x\mapsto v] patMatch σ ( x , v ) = σ [ x ↦ v ]
patMatch σ ( p a t 1 ∣ p a t 2 , v ) = { σ 1 if patMatch σ ( p a t 1 , v ) = σ 1 σ 2 if patMatch σ ( p a t 1 , v ) ↑ ∧ patMatch σ ( p a t 2 , v ) = σ 2 ↑ otherwise \operatorname{patMatch}_{\sigma}(pat_1\mid pat_2,v)=
\begin{cases}
\sigma_1 & \text{if }\operatorname{patMatch}_{\sigma}(pat_1,v)=\sigma_1 \\
\sigma_2 & \text{if }\operatorname{patMatch}_{\sigma}(pat_1,v)\uparrow\mathbin{\wedge}\operatorname{patMatch}_{\sigma}(pat_2,v)=\sigma_2 \\
\uparrow& \text{otherwise}
\end{cases} patMatch σ ( p a t 1 ∣ p a t 2 , v ) = ⎩ ⎨ ⎧ σ 1 σ 2 ↑ if patMatch σ ( p a t 1 , v ) = σ 1 if patMatch σ ( p a t 1 , v ) ↑ ∧ patMatch σ ( p a t 2 , v ) = σ 2 otherwise
If the first pattern does not match, we remove its arm from the match expression and continue with the rest. The Rust compiler ensures that the match arms are exhaustive, hence we need not cover the case of all arms being removed.
val σ ( m a t c h v { p a t ⇒ e , a r m } ) = { { ⟨ σ ⟩ ↷ σ ′ ⋅ K ( e ) } if patMatch σ ( p a t , v ) = σ ′ { ⟨ σ ⟩ ⋅ K ( m a t c h v { a r m } ) } if patMatch σ ( p a t , v ) ↑ (31) \operatorname{val}_{\sigma}(\mathop{\mathrm{match}}\ v\ \{pat\Rightarrow e,\ arm\})=
\begin{cases}
\{\langle\sigma\rangle\curvearrowright\sigma'\cdot K(e)\} & \text{if }\operatorname{patMatch}_{\sigma}(pat,v)=\sigma' \\
\{\langle\sigma\rangle\cdot K(\mathop{\mathrm{match}}\ v\ \{arm\})\} & \text{if }\operatorname{patMatch}_{\sigma}(pat,v)\uparrow
\end{cases} \tag*{(31)} val σ ( match v { p a t ⇒ e , a r m }) = { {⟨ σ ⟩ ↷ σ ′ ⋅ K ( e )} {⟨ σ ⟩ ⋅ K ( match v { a r m })} if patMatch σ ( p a t , v ) = σ ′ if patMatch σ ( p a t , v ) ↑ (31)
Function and Method Calls
Evaluating functions and methods introduces additionally complexity that must be managed by the semantics. To model the lifecycle of a call—invocation, execution, computation, and reaction to the computation—it is helpful to directly express these points in the semantics and the generated traces. Hence the events we allow in our traces. We make use of four call events in this section, one for each point of the lifecycle of a call. These four events follow the four event semantics of Din et al. [16 ] . Including these events directly in the trace permits easy, declarative restrictions on the order of events to ensure correct evaluation order.
To evaluate calls, we emit an invocation event with the function identifier, a unique but arbitrary call identifier i ∈ C I d i\in CId i ∈ C I d , where C I d CId C I d is the set of call identifiers, and the call arguments. We discuss below, how we ensure uniqueness of the call identifiers. Because the trace composition rules take care of actually executing the called function, the local evaluation rule needs to put a placeholder in the continuation marker that denotes that this expression will evaluate to the value returned by the call with identifier i i i . For that, we introduce the get expression ? i ?i ? i with i ∈ C I d i\in CId i ∈ C I d . This is necessary to mark that this expression cannot be resolved until the called function is completely executed and to access the called function’s return value.
val σ ( v ( v ‾ ) ) = { ⟨ σ ⟩ ↷ invEv ( v , i , v ‾ ) ↷ σ ⋅ K ( ? i ) ∣ i ∈ C I d } (32) \operatorname{val}_{\sigma}(v(\overline{v}))=\{\langle\sigma\rangle\curvearrowright\operatorname{invEv}(v,i,\overline{v})\curvearrowright\sigma\cdot K(?i)\mid i\in CId\} \tag*{(32)} val σ ( v ( v )) = {⟨ σ ⟩ ↷ invEv ( v , i , v ) ↷ σ ⋅ K ( ? i ) ∣ i ∈ C I d } (32)
Method calls, e.g., x . m ( 1 + 1 ) x.m(1+1) x . m ( 1 + 1 ) behave similarly to regular function calls but we make use of the method lookup function method2Fn ( v , m ) \operatorname{method2Fn}(v,m) method2Fn ( v , m ) , which yields the function name for the method m m m on callee v v v . Assume in rule (33) for brevity that f o o = method2Fn ( v , m ) foo=\operatorname{method2Fn}(v,m) f oo = method2Fn ( v , m ) . Observe that the callee is added as the first argument of the call identifier. This matches Rust’s treatment of methods [57 ] .
val σ ( v . m ( v ‾ ) ) = { ⟨ σ ⟩ ↷ invEv ( f o o , i , v , v ‾ ) ↷ σ ⋅ K ( ? i ) ∣ i ∈ C I d } (33) \operatorname{val}_{\sigma}(v.m(\overline{v}))=\{\langle\sigma\rangle\curvearrowright\operatorname{invEv}(foo,i,v,\overline{v})\curvearrowright\sigma\cdot K(?i)\mid i\in CId\} \tag*{(33)} val σ ( v . m ( v )) = {⟨ σ ⟩ ↷ invEv ( f oo , i , v , v ) ↷ σ ⋅ K ( ? i ) ∣ i ∈ C I d } (33)
To evaluate a function’s body, we extend val σ \operatorname{val}_{\sigma} val σ to a function declaration, emitting an invocation reaction event , introducing symbolic variables X ‾ \overline{X} X for the parameter’s values, and continuing with the body where the parameters x ‾ \overline{x} x have been substituted with X ‾ \overline{X} X . We use the notation e [ x ‾ ← X ‾ ] e[\overline{x}\leftarrow\overline{X}] e [ x ← X ] syntactically substituting all x ∈ x ‾ x\in\overline{x} x ∈ x with the corresponding X ∈ X ‾ X\in\overline{X} X ∈ X in e e e .
val σ ( f n f o o ( x ‾ ) e ) = { ⟨ σ ⟩ ↷ invREv ( f o o , i , X ‾ ) ↷ σ [ X ‾ ↦ ∗ ] ⋅ K ( e [ x ‾ ← X ‾ ] ) ∣ i ∈ C I d , X ‾ f r e s h } (34) \operatorname{val}_{\sigma}(\mathop{\mathrm{fn}}\ foo(\overline{x})\ e)=\{\langle\sigma\rangle\curvearrowright\operatorname{invREv}(foo,i,\overline{X})\curvearrowright\sigma[\overline{X}\mapsto*]\cdot K(e[\overline{x}\leftarrow\overline{X}])\mid i\in CId,\overline{X}\ \mathrm{fresh}\} \tag*{(34)} val σ ( fn f oo ( x ) e ) = {⟨ σ ⟩ ↷ invREv ( f oo , i , X ) ↷ σ [ X ↦ ∗ ] ⋅ K ( e [ x ← X ]) ∣ i ∈ C I d , X fresh } (34)
Example 3.6. Consider the call in line 16 and assume that the callee is already evaluated to the function identifier f a c fac f a c . Both callee and argument are already values, hence, we have val σ 1 ( f a c ( 4 ) ) = \operatorname{val}_{\sigma_1}(fac(4))= val σ 1 ( f a c ( 4 )) = { ⟨ σ 1 ⟩ ⇝ invEv ( fac , i , 1 ) ⇝ σ 1 ⋅ K ( ? i ) ∣ i ∈ C I d } \{\langle\sigma_1\rangle\rightsquigarrow\operatorname{invEv}(\operatorname{fac},i,1) \rightsquigarrow\sigma_1 \cdot K(?i) \mid i \in CId\} {⟨ σ 1 ⟩ ⇝ invEv ( fac , i , 1 ) ⇝ σ 1 ⋅ K ( ? i ) ∣ i ∈ C I d } . The get expression ? i ?i ? i creates a computation reaction event and we must ensure, as we do below, that this is only evaluated directly after the function call with id i i i is completed. Evaluation of the body gives us
val σ 1 ( f n f a c ( n ) e ) = { ⟨ σ ⟩ ⇝ invREv ( fac , i , X ) ⇝ σ 1 [ X ↦ ∗ ] ⋅ K ( e [ n ← X ] ) ∣ i ∈ C I d , X fresh } \operatorname{val}_{\sigma_1}(\mathtt{fn\ fac}(n)\ e) = \{\langle\sigma\rangle\rightsquigarrow\operatorname{invREv}(\operatorname{fac},i,X) \rightsquigarrow\sigma_1[X \mapsto*] \cdot K(e[n \leftarrow X]) \mid i \in CId, X\ \text{fresh}\} val σ 1 ( fn fac ( n ) e ) = {⟨ σ ⟩ ⇝ invREv ( fac , i , X ) ⇝ σ 1 [ X ↦ ∗ ] ⋅ K ( e [ n ← X ]) ∣ i ∈ C I d , X fresh }
where e e e is the body of fac in Listing 1.
An expression return v v v emits a return event with the returned value. The return expression evaluates to the unit value, because the returned value is not to be considered for the surrounding context. Instead, the event is used to transmit the return value to the caller.
val σ ( return v ) = { ⟨ σ ⟩ ⇝ retEv ( v ) ⇝ σ ⋅ K ( ( ) ) } (35) \operatorname{val}_{\sigma}(\textbf{return }v) = \{\langle\sigma\rangle\rightsquigarrow\operatorname{retEv}(v) \rightsquigarrow\sigma\cdot K(())\} \tag*{(35)} val σ ( return v ) = {⟨ σ ⟩ ⇝ retEv ( v ) ⇝ σ ⋅ K (( ))} (35)
The get expression emits a computation reaction event and introduces a fresh symbolic variable X X X which represents the value returned by call i i i .
val σ ( ? i ) = { ⟨ σ ⟩ ⇝ compREv ( i , X ) ⇝ σ [ X ↦ ∗ ] ⋅ K ( X ) ∣ X fresh } (36) \operatorname{val}_{\sigma}(?i) = \{\langle\sigma\rangle\rightsquigarrow\operatorname{compREv}(i,X) \rightsquigarrow\sigma[X \mapsto*] \cdot K(X) \mid X\ \text{fresh}\} \tag*{(36)} val σ ( ? i ) = {⟨ σ ⟩ ⇝ compREv ( i , X ) ⇝ σ [ X ↦ ∗ ] ⋅ K ( X ) ∣ X fresh } (36)
Example 3.7. Combining the results of Examples 3.1 to 3.4 and 3.6, we obtain the following traces:
val σ 0 ( b = false ) = { ⟨ σ 0 ⟩ ⇝ σ 1 ⋅ K ( ( ) ) } \operatorname{val}_{\sigma_0}(b = \textbf{false}) = \{\langle\sigma_0\rangle\rightsquigarrow\sigma_1 \cdot K(())\} val σ 0 ( b = false ) = {⟨ σ 0 ⟩ ⇝ σ 1 ⋅ K (( ))}
val σ 1 ( c = ! b ) = { ⟨ σ 1 ⟩ ⋅ K ( c = ff ) } \operatorname{val}_{\sigma_1}(c = {!}b) = \{\langle\sigma_1\rangle\cdot K(c = \textbf{ff})\} val σ 1 ( c = ! b ) = {⟨ σ 1 ⟩ ⋅ K ( c = ff )}
val σ 1 ( c = ! ff ) = { ⟨ σ 1 ⟩ ⋅ K ( c = tt ) } \operatorname{val}_{\sigma_1}(c = {!}\textbf{ff}) = \{\langle\sigma_1\rangle\cdot K(c = \textbf{tt})\} val σ 1 ( c = ! ff ) = {⟨ σ 1 ⟩ ⋅ K ( c = tt )}
val σ 1 ( c = tt ) = { ⟨ σ 1 ⟩ ⇝ σ 2 ⋅ K ( ( ) ) } \operatorname{val}_{\sigma_1}(c = \textbf{tt}) = \{\langle\sigma_1\rangle\rightsquigarrow\sigma_2 \cdot K(())\} val σ 1 ( c = tt ) = {⟨ σ 1 ⟩ ⇝ σ 2 ⋅ K (( ))}
val σ 2 ( f = fac ( 4 ) ) = { ⟨ σ 1 ⟩ ⇝ invEv ( fac , i , 4 ) ⇝ σ 2 ⋅ K ( f = ? i ) ∣ i ∈ C I d } \operatorname{val}_{\sigma_2}(f = \operatorname{fac}(4)) = \{\langle\sigma_1\rangle\rightsquigarrow\operatorname{invEv}(\operatorname{fac},i,4) \rightsquigarrow\sigma_2 \cdot K(f = ?i) \mid i \in CId\} val σ 2 ( f = fac ( 4 )) = {⟨ σ 1 ⟩ ⇝ invEv ( fac , i , 4 ) ⇝ σ 2 ⋅ K ( f = ? i ) ∣ i ∈ C I d }
val σ 3 ( arr = [ 8 ; 3 ] ) = { ⟨ σ 3 ⟩ ⋅ K ( arr = [ 8 , 8 , 8 ] ) } \operatorname{val}_{\sigma_3}(\operatorname{arr} = [8;3]) = \{\langle\sigma_3\rangle\cdot K(\operatorname{arr} = [8,8,8])\} val σ 3 ( arr = [ 8 ; 3 ]) = {⟨ σ 3 ⟩ ⋅ K ( arr = [ 8 , 8 , 8 ])}
val σ 3 ( arr = [ 8 , 8 , 8 ] ) = { ⟨ σ 3 ⟩ ⇝ σ 4 ⋅ K ( ( ) ) } \operatorname{val}_{\sigma_3}(\operatorname{arr} = [8,8,8]) = \{\langle\sigma_3\rangle\rightsquigarrow\sigma_4 \cdot K(())\} val σ 3 ( arr = [ 8 , 8 , 8 ]) = {⟨ σ 3 ⟩ ⇝ σ 4 ⋅ K (( ))}
val σ 4 ( set_all ( & m u t arr , f ) ) = { ⟨ σ 4 ⟩ ⋅ K ( set_all ( Ref arr m ( b ) , f ) ) ∣ b ∈ B I d } \operatorname{val}_{\sigma_4}(\operatorname{set\_all}(\&\mathbf{mut}\ \operatorname{arr},f)) = \{\langle\sigma_4\rangle\cdot K(\operatorname{set\_all}(\operatorname{Ref}_{\operatorname{arr}}^{m}(b),f)) \mid b \in BId\} val σ 4 ( set_all ( & mut arr , f )) = {⟨ σ 4 ⟩ ⋅ K ( set_all ( Ref arr m ( b ) , f )) ∣ b ∈ B I d }
with dom ( σ 0 ) = ∅ \operatorname{dom}(\sigma_0) = \varnothing dom ( σ 0 ) = ∅ , σ 1 = σ 0 [ b ↦ ff ] \sigma_1 = \sigma_0[b \mapsto\textbf{ff}] σ 1 = σ 0 [ b ↦ ff ] , σ 2 = σ 1 [ c ↦ tt ] \sigma_2 = \sigma_1[c \mapsto\textbf{tt}] σ 2 = σ 1 [ c ↦ tt ] , σ 3 = σ 2 [ f ↦ 24 ] \sigma_3 = \sigma_2[f \mapsto24] σ 3 = σ 2 [ f ↦ 24 ] , σ 4 = σ 3 [ arr ↦ [ 8 , 8 , 8 ] ] \sigma_4 = \sigma_3[\operatorname{arr} \mapsto[8,8,8]] σ 4 = σ 3 [ arr ↦ [ 8 , 8 , 8 ]] . Clearly, the resulting trace of this series of computations must be of the form
⟨ σ 0 ⟩ ⇝ σ 1 ⇝ σ 2 ⇝ invEv ( fac , i , 4 ) ⇝ ⋯ ⇝ σ 3 ⇝ σ 4 . \langle\sigma_0\rangle\rightsquigarrow\sigma_1 \rightsquigarrow\sigma_2 \rightsquigarrow\operatorname{invEv}(\operatorname{fac},i,4) \rightsquigarrow\cdots\rightsquigarrow\sigma_3 \rightsquigarrow\sigma_4. ⟨ σ 0 ⟩ ⇝ σ 1 ⇝ σ 2 ⇝ invEv ( fac , i , 4 ) ⇝ ⋯ ⇝ σ 3 ⇝ σ 4 .
What is missing so far is (1) local rules dealing with variable declarations as in Listing 1, not just assignments, (2) local rules dealing with blocks, and (3) a way to compose these individual local rules into one single trace. We deal with the first two items immediately and return to the third one in Section 3.3.
Block Expressions
When a block is fully evaluated and, hence, removed from the continuation, we want to drop all local variables this block introduced, i.e., remove them from the state. This requires bookkeeping of which variables are introduced by a block. Thus, we add a syntactic extension called scoped block expressions : { x ‾ ⊙ s e } \{\overline{x} \mathrel{\odot} s\ e\} { x ⊙ s e } stands for the block expression { s e } \{s\ e\} { s e } , which has so far introduced the local variables x ‾ \overline{x} x . For brevity, we also write { s e } \{s\ e\} { s e } for the scoped block expression { ∅ ⊙ s e } \{\varnothing\mathrel{\odot} s\ e\} { ∅ ⊙ s e } .
Expressions and statements in blocks are also evaluated piecemeal. Declarations with initial value are split into the declaration and the assignment. For simplicity, we only show rules for declaring variables.
val σ ( { x ‾ ⊙ l e t y = e 1 ; s ‾ e 2 } ) = val σ ( { x ‾ ⊙ l e t y ; y = e 1 ; s ‾ e 2 } ) (37) \operatorname{val}_{\sigma}(\{\overline{x} \mathrel{\odot} \mathbf{let}\ y = e_1; \overline{s}\ e_2\}) = \operatorname{val}_{\sigma}(\{\overline{x} \mathrel{\odot} \mathbf{let}\ y; y = e_1; \overline{s}\ e_2\}) \tag*{(37)} val σ ({ x ⊙ let y = e 1 ; s e 2 }) = val σ ({ x ⊙ let y ; y = e 1 ; s e 2 }) (37)
A declaration of program variable y y y is evaluated by adding y y y to the declared variables in the block. Additionally, to avoid name clashes in our state caused by, e.g., evaluating two function bodies in parallel, we rename y y y to a fresh program variable y ′ y' y ′ . As there are potentially infinitely many fresh y ′ y' y ′ , rule (38) may introduce infinitely many traces; which of these is chosen is covered in § 3.7 .
val σ ( { x ≢ let y ; s e ‾ } ) = { ⟨ σ ⟩ ⋅ K ( { x , y ≢ s e ‾ } [ y ← y ′ ] ) ∣ y ′ f r e s h } (38) \operatorname{val}_{\sigma}\left(\left\{x \not\equiv\operatorname{let}\ y;\ \overline{s\ e}\right\}\right)=\left\{\langle\sigma\rangle\cdot K\left(\left\{x,y \not\equiv\overline{s\ e}\right\}[y\leftarrow y']\right)\mid y'\ \mathrm{fresh}\right\} \tag*{(38)} val σ ( { x ≡ let y ; s e } ) = { ⟨ σ ⟩ ⋅ K ( { x , y ≡ s e } [ y ← y ′ ] ) ∣ y ′ fresh } (38)
The first expression in a block is either a value or a complex expression. The latter case is handled by § 3.1.1 . In the former case, we can remove it as in rule (39).
val σ ( { x ≢ v ; s e ‾ } ) = { ⟨ σ ⟩ ⋅ K ( { x ≢ s e ‾ } ) } (39) \operatorname{val}_{\sigma}\left(\left\{x \not\equiv v;\ \overline{s\ e}\right\}\right)=\left\{\langle\sigma\rangle\cdot K\left(\left\{x \not\equiv\overline{s\ e}\right\}\right)\right\} \tag*{(39)} val σ ( { x ≡ v ; s e } ) = { ⟨ σ ⟩ ⋅ K ( { x ≡ s e } ) } (39)
A block containing only the final value v v v is removed, all variables introduced by the block are dropped, i.e., removed from the state, and we continue with v v v only.
val σ ( { x ≢ v } ) = { ⟨ σ ⟩ ⇝ ( σ ∖ x ) ⋅ K ( v ) } (40) \operatorname{val}_{\sigma}\left(\left\{x \not\equiv v\right\}\right)=\left\{\langle\sigma\rangle\mathrel{\leadsto}(\sigma\setminus x)\cdot K(v)\right\} \tag*{(40)} val σ ( { x ≡ v } ) = { ⟨ σ ⟩ ⇝ ( σ ∖ x ) ⋅ K ( v ) } (40)
Panics, in turn, are handled by emitting a panic event and leaving the unit value behind. Abrupt termination is modeled during composition.
val σ ( panic! ( ) ) = { ⟨ σ ⟩ ⇝ panicEv ( ) ⇝ σ ⋅ K ( ( ) ) } (41) \operatorname{val}_{\sigma}\left(\operatorname{panic!}()\right)=\left\{\langle\sigma\rangle\mathrel{\leadsto}\operatorname{panicEv}()\mathrel{\leadsto}\sigma\cdot K(())\right\} \tag*{(41)} val σ ( panic! ( ) ) = { ⟨ σ ⟩ ⇝ panicEv ( ) ⇝ σ ⋅ K (( )) } (41)
Loops
The more complex control flow options of a loop, including break and continue prove to be a challenge, as they require the context of the surrounding loops, which complicates control flow, especially when labeled jumps are involved. We model loops by extending the syntax with loop frames , taking inspiration from loop scopes in deductive program verification [61 ] . These frames permit tracking of the surrounding loop context while leaving the original loop—to which the program might jump again—unaltered. A loop frame lframe ( ‘ l , e ) \operatorname{lframe}(\text{‘}l,e) lframe ( ‘ l , e ) states that we are evaluating the loop with label l l l and that the current expression to be evaluated of the loop’s body is e e e . Once the loop body is fully evaluated, the loop is evaluated from the beginning again. Figure 2 demonstrates the possible relevant control flow options in a loop. Breaks jump out of the loop addressed by its label; continues jump to the next iteration of its corresponding loops. Note that the loop with label 'l1 has an integer value (i, once the corresponding break in line 8 is executed) and the loop 'l2 has the unit value ().
'l1: loop {
'l2: loop {
if i < -3 {
continue 'l2
} else if i < 0 {
continue 'l1
} else if i == 3 {
break 'l1 i
} else if i == 4 {
break 'l2 ()
} else { i += 2 }
}
}
Figure 2. Possible control-flow paths of a loop expression in Rust.
To start a loop’s evaluation, it is translated to a loop frame.
val σ ( ′ l : loop e ) = val σ ( lframe ( ′ l , e ) ) (42) \operatorname{val}_{\sigma}({}'l:\ \operatorname{loop}\ e) = \operatorname{val}_{\sigma}(\operatorname{lframe}({}'l,e)) \tag*{(42)} val σ ( ′ l : loop e ) = val σ ( lframe ( ′ l , e )) (42)
In Rust, break expressions contain an (optional) expression. These expressions mark the value of the loop or block to which the break ’s label refers. A loop expression’s value thus depends on the break expression contained therein.
A break with value v v v in a loop is resolved in one of two ways: If the label matches the active loop frame’s label, we continue with the expression. Otherwise, we replace the entire frame with the break expression. The following rules consider a break expression in the context of a loop frame and a block. The latter may contain additional statements and an expression after the break . These are discarded once the break is evaluated.
val σ ( lframe ( ′ l 1 , { x ↗ break ′ l 2 v ; s ; e } ) ) = { { ⟨ σ ⟩ ⋅ K ( { x ↗ v } ) } l 1 = l 2 { ⟨ σ ⟩ ⋅ K ( { x ↗ break ′ l 2 v } ) } l 1 ≠ l 2 (43) \operatorname{val}_{\sigma}(\operatorname{lframe}({}'l_1,\{x \nearrow\operatorname{break}\ {}'l_2\ v;\ s;\ e\})) =
\begin{cases}
\{\langle\sigma\rangle\cdot K(\{x \nearrow v\})\} & l_1 = l_2\\
\{\langle\sigma\rangle\cdot K(\{x \nearrow\operatorname{break}\ {}'l_2\ v\})\} & l_1 \ne l_2
\end{cases} \tag*{(43)} val σ ( lframe ( ′ l 1 , { x ↗ break ′ l 2 v ; s ; e })) = { {⟨ σ ⟩ ⋅ K ({ x ↗ v })} {⟨ σ ⟩ ⋅ K ({ x ↗ break ′ l 2 v })} l 1 = l 2 l 1 = l 2 (43)
When we encounter a continue expression with label l 2 l_2 l 2 in a loop frame, we evaluate the loop with label l 2 l_2 l 2 , using the function “getLoop,” which maps labels to loops. We maintain the surrounding block to maintain the scope, which preserves proper dropping of the declared variables.
val σ ( lframe ( ′ l 1 , { x ↗ continue ′ l 2 ; s ; e } ) ) = { ⟨ σ ⟩ ⋅ K ( { { x ↗ ( ) } ; getLoop ( ′ l 2 ) } ) } (44) \operatorname{val}_{\sigma}(\operatorname{lframe}({}'l_1,\{x \nearrow\operatorname{continue}\ {}'l_2;\ s;\ e\})) = \{\langle\sigma\rangle\cdot K(\{\{x \nearrow()\};\operatorname{getLoop}({}'l_2)\})\} \tag*{(44)} val σ ( lframe ( ′ l 1 , { x ↗ continue ′ l 2 ; s ; e })) = {⟨ σ ⟩ ⋅ K ({{ x ↗ ( )} ; getLoop ( ′ l 2 )})} (44)
If the loop frame’s expression is not a continue or break, we re-insert the loop of the active label, repeating execution. The value of the loop’s body is ignored, as only break expressions affect the loop’s value.
val σ ( lframe ( ′ l , v ) ) = { ⟨ σ ⟩ ⋅ K ( getLoop ( ′ l ) ) } (45) \operatorname{val}_{\sigma}(\operatorname{lframe}({}'l,v)) = \{\langle\sigma\rangle\cdot K(\operatorname{getLoop}({}'l))\} \tag*{(45)} val σ ( lframe ( ′ l , v )) = {⟨ σ ⟩ ⋅ K ( getLoop ( ′ l ))} (45)
Example 3.8. Evaluating a loop expression ′ l loop { if n = = 0 { break ′ l true ; } else { ( ) } n − = 1 ; } 'l\ \operatorname{loop}\ \{ \operatorname{if}\ n == 0 \{ \operatorname{break}\ 'l\ \operatorname{true}; \} \operatorname{else}\ \{()\}\ n -= 1; \} ′ l loop { if n == 0 { break ′ l true ; } else {( )} n − = 1 ; } in state σ 2 = [ n ↦ 1 ] \sigma_2 = [n \mapsto1] σ 2 = [ n ↦ 1 ] leads to val σ 2 ( ′ l loop … ) = val σ 2 ( lframe ( ′ l , { if … } ) ) \operatorname{val}_{\sigma_2}('l\ \operatorname{loop}\ \ldots) = \operatorname{val}_{\sigma_2}(\operatorname{lframe}('l,\{ \operatorname{if}\ \ldots\})) val σ 2 ( ′ l loop … ) = val σ 2 ( lframe ( ′ l , { if … })) , which we further evaluate to { ⟨ σ 2 ⟩ ⋅ K ( lframe ( ′ l , { if 1 = = 0 … } ) ) } \{\langle\sigma_2\rangle\cdot K(\operatorname{lframe}('l,\{ \operatorname{if}\ 1 == 0\ \ldots\}))\} {⟨ σ 2 ⟩ ⋅ K ( lframe ( ′ l , { if 1 == 0 … }))} . Evaluating val σ 3 ( lframe ( ′ l , { break ′ l true ; } ) ) \operatorname{val}_{\sigma_3}(\operatorname{lframe}('l,\{\operatorname{break}\ 'l\ \operatorname{true};\})) val σ 3 ( lframe ( ′ l , { break ′ l true ; })) , where σ 3 = σ 2 [ n ↦ 0 ] \sigma_3 = \sigma_2[n \mapsto0] σ 3 = σ 2 [ n ↦ 0 ] , yields { ⟨ σ 3 ⟩ ⋅ K ( { true } ) } \{\langle\sigma_3\rangle\cdot K(\{\operatorname{true}\})\} {⟨ σ 3 ⟩ ⋅ K ({ true })} .
Well-Formedness of Traces
Without any restrictions, the traces generated by our semantics do not necessarily describe possible program behavior, as they encompass traces with undesired behavior, e.g., a function body evaluation where the parameters do not match the call arguments. The well-formedness predicate w f wf w f restricts the set of generated traces to ensure a certain ordering and matching between events. This predicate is defined over shining traces and hence defines additional restrictions on top of Definition 2.11. We define the well-formedness predicate recursively: An empty trace is always well-formed, as is the trace ⟨ σ ⟩ \langle\sigma\rangle ⟨ σ ⟩ . States do not influence the well-formedness, and a panic event can only occur as the final event and it is followed by exactly one state.
w f ( ϵ ) = true w f ( ϵ ⇝ σ ) = true w f ( s h ⇝ σ ⇝ τ ) = w f ( s h ⇝ τ ) ∧ p a n i c E v ( ) ∉ s h w f ( s h ⇝ p a n i c E v ( ) ) = w f ( s h ) \begin{aligned}
wf(\epsilon) &= \operatorname{true}\\
wf(\epsilon\leadsto\sigma) &= \operatorname{true}\\
wf(sh \leadsto\sigma\leadsto\tau) &= wf(sh \leadsto\tau) \land panicEv() \notin sh\\
wf(sh \leadsto panicEv()) &= wf(sh)
\end{aligned} w f ( ϵ ) w f ( ϵ ⇝ σ ) w f ( s h ⇝ σ ⇝ τ ) w f ( s h ⇝ p ani c E v ( )) = true = true = w f ( s h ⇝ τ ) ∧ p ani c E v ( ) ∈ / s h = w f ( s h )
Every invocation event must have a unique call identifier. For the invocation reaction, we require that the last event prior is an invocation event with matching function name, call id, and arguments. This requirement also implies that each invocation is reacted to at most once.
w f ( s h ⇝ i n v E v ( f o o , i , v ˉ ) ) = w f ( s h ) ∧ ∄ f o o ′ , v ˉ ′ , i n v E v ( f o o ′ , i , v ˉ ′ ) ∈ s h (46) wf(sh \leadsto invEv(foo,i,\bar{v})) = wf(sh) \land\nexists foo',\bar{v}',\ invEv(foo',i,\bar{v}') \in sh \tag*{(46)} w f ( s h ⇝ in v E v ( f oo , i , v ˉ )) = w f ( s h ) ∧ ∄ f o o ′ , v ˉ ′ , in v E v ( f o o ′ , i , v ˉ ′ ) ∈ s h (46)
w f ( s h ⇝ i n v R E v ( f o o , i , v ˉ ) ) = w f ( s h ) ∧ ( i n v E v ( f o o , i , v ˉ ) = l a s t E v ( s h ) ) (47) wf(sh \leadsto invREv(foo,i,\bar{v})) = wf(sh) \land(invEv(foo,i,\bar{v}) = lastEv(sh)) \tag*{(47)} w f ( s h ⇝ in v R E v ( f oo , i , v ˉ )) = w f ( s h ) ∧ ( in v E v ( f oo , i , v ˉ ) = l a s tE v ( s h )) (47)
No restrictions are placed on the return event, while the computation reaction event—similarly to the invocation’s case—must follow immediately its corresponding return event.
w f ( s h ↷ r e t E v ( v ) ) = w f ( s h ) (48) wf\left(sh \curvearrowright retEv(v)\right) = wf(sh) \tag*{(48)} w f ( s h ↷ r e tE v ( v ) ) = w f ( s h ) (48)
w f ( s h ↷ c o m p R E v ( f o o , v ) ) = w f ( s h ) ∧ ( c o m p E v ( f o o , v ) = l a s t E v ( s h ) ) (49) wf\left(sh \curvearrowright compREv(foo,v)\right) = wf(sh) \land\left(compEv(foo,v) = lastEv(sh)\right) \tag*{(49)} w f ( s h ↷ co m pR E v ( f oo , v ) ) = w f ( s h ) ∧ ( co m pE v ( f oo , v ) = l a s tE v ( s h ) ) (49)
Example 3.9. The trace ⟨ σ 0 ⟩ ↷ p a n i c E v ( ) ↷ σ 0 ↷ i n v E v ( f o o , c i d 0 , 3 ) ↷ σ 0 \langle\sigma_0\rangle\curvearrowright panicEv() \curvearrowright\sigma_0 \curvearrowright invEv(foo,cid_0,3) \curvearrowright\sigma_0 ⟨ σ 0 ⟩ ↷ p ani c E v ( ) ↷ σ 0 ↷ in v E v ( f oo , c i d 0 , 3 ) ↷ σ 0 is not well-formed, as the panic event is succeeded by an invocation event. However, the trace ⟨ σ 0 ⟩ ↷ i n v E v ( f o o , c i d 0 , 3 ) ↷ σ 0 ↷ i n v R E v ( f o o , c i d 0 , 3 ) ↷ σ 0 [ x ↦ 3 ] \langle\sigma_0\rangle\curvearrowright invEv(foo,cid_0,3) \curvearrowright\sigma_0 \curvearrowright invREv(foo,cid_0,3) \curvearrowright\sigma_0[x \mapsto3] ⟨ σ 0 ⟩ ↷ in v E v ( f oo , c i d 0 , 3 ) ↷ σ 0 ↷ in v R E v ( f oo , c i d 0 , 3 ) ↷ σ 0 [ x ↦ 3 ] is well-formed as the invocation reaction event matches the invocation event and follows it immediately.
Trace Composition Rules
So far, our semantics provides the local, symbolic traces for each expression and a definition of “correct” (well-formed) traces. The next step in an LAGC semantics is to compose together the local traces generated in § 3.1 into well-formed, shining traces (see Definition 2.11). We define trace composition rules , which govern how traces are combined.
Trace composition rules are transition rules and define the relation → \to → between two configurations . In general, a configuration consists of a shining trace and some data structures over continuation markers. The former is the trace generated so far, the latter represents in some form the remaining program to evaluate. Thus, the configuration expresses the global state of the program evaluation as well as its history in the form of the trace.
In the synchronous Rust case, we aim to model the call stack created by function and method calls. Hence, we choose as configuration
s h , [ k ‾ ] sh,\left[\overline{k}\right] s h , [ k ]
where k ‾ \overline{k} k are continuations and the left-most continuation is the top element.
All composed traces must be well-formed according to w f wf w f . This restricts both the local traces that may be chosen and the applicability of rules.
Assume an expression e e e and a shining trace s h sh s h . The trace composition rule (50) defines the derivation from the configuration s h , [ K ( e ) , k ‾ ] sh,[K(e),\overline{k}] s h , [ K ( e ) , k ] to s h ∗ ∗ ρ ( τ ) , [ K ( e ′ ) , k ‾ ] sh ** \rho(\tau),[K(e'),\overline{k}] s h ∗ ∗ ρ ( τ ) , [ K ( e ′ ) , k ] , where e e e is the expression in the top continuation marker of the continuation stack. We take the last state of s h sh s h to evaluate e e e one step further. This yields a set of continuation traces, from which we pick one trace, τ ⋅ K ( e ′ ) \tau\cdot K(e') τ ⋅ K ( e ′ ) , arbitrarily. The trace τ \tau τ may be symbolic and we use concretization mapping ρ \rho ρ to concretize it. Then, we use chop to stitch the concretized trace ρ ( τ ) \rho(\tau) ρ ( τ ) to s h sh s h and continue with e ′ e' e ′ in the top continuation marker. The lower levels of the stack are unchanged.
σ = l a s t ( s h ) τ ⋅ K ( e ′ ) ∈ v a l σ ( e ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ ) ) s h , [ K ( e ) , k ‾ ] → s h ∗ ∗ ρ ( τ ) , [ K ( e ′ ) , k ‾ ] (50) \frac{\sigma= last(sh)\qquad\tau\cdot K(e') \in val_{\sigma}(e)\qquad\rho\ \text{concretizes}\ \tau\qquad wf\left(sh ** \rho(\tau)\right)}
{sh,\left[K(e),\overline{k}\right] \to sh ** \rho(\tau),\left[K(e'),\overline{k}\right]} \tag*{(50)} s h , [ K ( e ) , k ] → s h ∗ ∗ ρ ( τ ) , [ K ( e ′ ) , k ] σ = l a s t ( s h ) τ ⋅ K ( e ′ ) ∈ v a l σ ( e ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ ) ) (50)
Example 3.10. We continue the running example. Assume a state σ 0 \sigma_0 σ 0 with d o m ( σ 0 ) = ∅ dom(\sigma_0) = \emptyset d o m ( σ 0 ) = ∅ and let e 0 e_0 e 0 be the main function’s block in Listing 1. Let further σ 1 : = σ 0 [ x ↦ f f ] \sigma_1 := \sigma_0[x \mapsto\mathrm{ff}] σ 1 := σ 0 [ x ↦ ff ] and σ 2 : = σ 1 [ y ↦ t t ] \sigma_2 := \sigma_1[y \mapsto\mathrm{tt}] σ 2 := σ 1 [ y ↦ tt ] . We start in configuration ⟨ σ 0 ⟩ , [ K ( e 0 ) ] \langle\sigma_0\rangle,[K(e_0)] ⟨ σ 0 ⟩ , [ K ( e 0 )] . As the states are concrete, we can use the concretization mapping ρ 0 \rho_0 ρ 0 with d o m ( ρ 0 ) = ∅ dom(\rho_0) = \emptyset d o m ( ρ 0 ) = ∅ throughout. By repeated applications of rule (50) we obtain the following derivation
⟨ σ 0 ⟩ , [ K ( e 0 ) ] → ⟨ σ 0 ⟩ , [ K ( { x ↦ x = f a l s e ; … } ) ] → ⟨ σ 0 ⟩ ↷ σ 1 , [ K ( { x ↦ l e t c = ! x ; … } ) ] → ⟨ σ 0 ⟩ ↷ σ 1 , [ K ( { x , y ↦ y = ! x ; … } ) ] → ⟨ σ 0 ⟩ ↷ σ 1 , [ K ( { x , y ↦ y = ! f f ; … } ) ] → ⟨ σ 0 ⟩ ↷ σ 1 , [ K ( { x , y ↦ y = t t ; … } ) ] → ⟨ σ 0 ⟩ ↷ σ 1 ↷ σ 2 , [ K ( { x , y ↦ l e t f = fac ( 4 ) ; … } ) ] → ⟨ σ 0 ⟩ ↷ σ 1 ↷ σ 2 , [ K ( { x , y ↦ l e t f ; f = fac ( 4 ) ; … } ) ] → ⟨ σ 0 ⟩ ↷ σ 1 ↷ σ 2 , [ K ( { x , y , z ↦ z = fac ( 4 ) ; … } ) ] → ⟨ σ 0 ⟩ ↷ σ 1 ↷ σ 2 ↷ invEv ( fac , i 0 , 4 ) ↷ σ 2 , [ K ( { x , y , z ↦ z = ? i 0 ; … } ) ] \begin{aligned}
\langle\sigma_0\rangle, [K(e_0)] &\to\langle\sigma_0\rangle, [K(\{x \mapsto x = \mathrm{false};\ldots\})] \\
&\to\langle\sigma_0\rangle\curvearrowright\sigma_1, [K(\{x \mapsto\mathbf{let}\ c = {!}x;\ldots\})] \to\langle\sigma_0\rangle\curvearrowright\sigma_1, [K(\{x,y \mapsto y = {!}x;\ldots\})] \\
&\to\langle\sigma_0\rangle\curvearrowright\sigma_1, [K(\{x,y \mapsto y = {!}\mathrm{ff};\ldots\})] \to\langle\sigma_0\rangle\curvearrowright\sigma_1, [K(\{x,y \mapsto y = \mathrm{tt};\ldots\})] \\
&\to\langle\sigma_0\rangle\curvearrowright\sigma_1 \curvearrowright\sigma_2, [K(\{x,y \mapsto\mathbf{let}\ f = \operatorname{fac}(4);\ldots\})] \\
&\to\langle\sigma_0\rangle\curvearrowright\sigma_1 \curvearrowright\sigma_2, [K(\{x,y \mapsto\mathbf{let}\ f; f = \operatorname{fac}(4);\ldots\})] \\
&\to\langle\sigma_0\rangle\curvearrowright\sigma_1 \curvearrowright\sigma_2, [K(\{x,y,z \mapsto z = \operatorname{fac}(4);\ldots\})] \\
&\to\langle\sigma_0\rangle\curvearrowright\sigma_1 \curvearrowright\sigma_2 \curvearrowright\operatorname{invEv}(\operatorname{fac},i_0,4) \curvearrowright\sigma_2, [K(\{x,y,z \mapsto z = ?i_0;\ldots\})]
\end{aligned} ⟨ σ 0 ⟩ , [ K ( e 0 )] → ⟨ σ 0 ⟩ , [ K ({ x ↦ x = false ; … })] → ⟨ σ 0 ⟩ ↷ σ 1 , [ K ({ x ↦ let c = ! x ; … })] → ⟨ σ 0 ⟩ ↷ σ 1 , [ K ({ x , y ↦ y = ! x ; … })] → ⟨ σ 0 ⟩ ↷ σ 1 , [ K ({ x , y ↦ y = ! ff ; … })] → ⟨ σ 0 ⟩ ↷ σ 1 , [ K ({ x , y ↦ y = tt ; … })] → ⟨ σ 0 ⟩ ↷ σ 1 ↷ σ 2 , [ K ({ x , y ↦ let f = fac ( 4 ) ; … })] → ⟨ σ 0 ⟩ ↷ σ 1 ↷ σ 2 , [ K ({ x , y ↦ let f ; f = fac ( 4 ) ; … })] → ⟨ σ 0 ⟩ ↷ σ 1 ↷ σ 2 , [ K ({ x , y , z ↦ z = fac ( 4 ) ; … })] → ⟨ σ 0 ⟩ ↷ σ 1 ↷ σ 2 ↷ invEv ( fac , i 0 , 4 ) ↷ σ 2 , [ K ({ x , y , z ↦ z = ? i 0 ; … })]
It is not possible to apply rule (50) further, as the well-formedness predicate prohibits adding the computation reaction event that evaluation of ? i 0 ?i_0 ? i 0 would yield. Observe that we could have many other derivations similar to the one above, the only difference being that different concrete variable namings and call identifiers are chosen.
Rule (50) ensures that all constructed traces are well-formed and concrete. The composition is also stuck if the last event of s h sh s h is a panicEv event, as we cannot continue past such a fatal error. Similarly, it is not applicable if e e e is a value, because values generate no traces.
Rule (51) executes a function body and pushes the resulting continuation onto the continuation stack. The rule may pick any function from the program at any point, leading to possible spurious function execution. However, the predicate w f wf w f ensures with equation (47) that rule (51) is only applicable when appropriate, i.e., immediately after its corresponding invocation event, and with the correct function. This restriction ensures correct applications of the rule and prohibits spurious function executions.
σ = last ( s h ) f n foo ( x ‾ ) e ∈ F n τ ⋅ K ( e ′ ) ∈ val σ ( f n foo ( x ‾ ) e ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ ) ) s h , [ k ‾ ] → s h ∗ ∗ ρ ( τ ) , [ K ( e ′ ) , k ‾ ] (51) \frac{
\begin{aligned}
\sigma&= \operatorname{last}(sh) \qquad\mathbf{fn}\ \operatorname{foo}(\overline{x})\ e \in Fn \\
\tau\cdot K(e') &\in\operatorname{val}_{\sigma}(\mathbf{fn}\ \operatorname{foo}(\overline{x})\ e) \qquad\rho\ \text{concretizes}\ \tau\qquad wf(sh \mathbin{**} \rho(\tau))
\end{aligned}
}{
sh,\left[\overline{k}\right] \to sh \mathbin{**} \rho(\tau), \left[K(e'),\overline{k}\right]
} \tag*{(51)} s h , [ k ] → s h ∗ ∗ ρ ( τ ) , [ K ( e ′ ) , k ] σ τ ⋅ K ( e ′ ) = last ( s h ) fn foo ( x ) e ∈ F n ∈ val σ ( fn foo ( x ) e ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ )) (51)
Example 3.11. Continuing Example 3.10 we start from configuration ⟨ σ 0 ⟩ ↷ σ 1 ↷ σ 2 ↷ invEv ( fac , i 0 , 4 ) ↷ σ 2 \langle\sigma_0\rangle\curvearrowright\sigma_1 \curvearrowright\sigma_2 \curvearrowright\operatorname{invEv}(\operatorname{fac},i_0,4) \curvearrowright\sigma_2 ⟨ σ 0 ⟩ ↷ σ 1 ↷ σ 2 ↷ invEv ( fac , i 0 , 4 ) ↷ σ 2 , [ K ( { x , y , z ≠ z = ? i 0 ; … } ) ] [K(\{x,y,z \mathrel{\ne} z=?i_0;\ldots\})] [ K ({ x , y , z = z = ? i 0 ; … })] by executing function fac and putting the result to the top of the continuation stack. By rule (51) we derive ⟨ σ 0 ⟩ ↷ σ 1 ↷ σ 2 ↷ invEv ( fac , i 0 , 4 ) ↷ σ 2 ↷ invREv ( fac , i 0 , 4 ) ↷ σ 3 \langle\sigma_0\rangle\curvearrowright\sigma_1 \curvearrowright\sigma_2 \curvearrowright\operatorname{invEv}(\operatorname{fac},i_0,4) \curvearrowright\sigma_2 \curvearrowright\operatorname{invREv}(\operatorname{fac},i_0,4) \curvearrowright\sigma_3 ⟨ σ 0 ⟩ ↷ σ 1 ↷ σ 2 ↷ invEv ( fac , i 0 , 4 ) ↷ σ 2 ↷ invREv ( fac , i 0 , 4 ) ↷ σ 3 , [ K ( { p ≠ r e t u r n i f p ≤ 1 … } ) , K ( { x , y , z ≠ z = ? i 0 ; … } ) ] [K(\{p \mathrel{\ne} \mathbf{return}\ \mathbf{if}\ p \mathrel{\le} 1\ldots\}), K(\{x,y,z \mathrel{\ne} z=?i_0;\ldots\})] [ K ({ p = return if p ≤ 1 … }) , K ({ x , y , z = z = ? i 0 ; … })] where we have σ 3 : = σ 2 [ p ↦ 4 ] \sigma_3 := \sigma_2[p \mapsto4] σ 3 := σ 2 [ p ↦ 4 ] . Note how the trace composition applies the concretization: The symbolic variables introduced by rule (34) are immediately concretized and the well-formedness predicate ensures that it is concretized to the actual call arguments. With rules (50) and (51) we can also evaluate the next call of fac with argument 3 in line 4 but we cannot, as yet, continue after a function returns.
Once a function call completes, we require a rule to pop the top continuation off the continuation stack, effectively returning control to the calling function. Crucially, we cannot do this step once the top continuation marker contains a value, i.e., is fully evaluated, because a return -expression may not be the last or only expression in a function body; it can occur at any point. Because this return emits a return event and immediately stops the function’s evaluation, we want to pop the top continuation marker off the stack at this point.
Observe that, by rules (32) and (51) and equations (46) and (47) , any continuation marker but the top one must contain as the next sub-expression to be evaluated a get expression ? i ?i ? i —i.e., any continuation marker except the top one is waiting on the function call above it to finish. By rule (36) and equation (49) , such a get expression ? i ?i ? i can be evaluated iff the call i i i has just returned. This condition precisely models when we can clean up the continuation marker for the call i i i .
Hence, the structure of rule (52): We remove the top continuation marker K(e e e ) if we can evaluate the continuation marker K(e ′ e' e ′ ) below it and the result is well-formed according to equation (49) . We know that e ′ e' e ′ ’s next sub-expression to be evaluated is a get expression waiting on K(e e e ) to finish. If the result is well-formed, the top continuation marker must have just returned and must therefore be removed from the stack.
σ = last ( s h ) τ ⋅ K ( e ′ ′ ) ∈ val σ ( e ′ ) ρ concretizes τ wf ( s h ∗ ∗ ρ ( τ ) ) s h , [ K ( e ) , K ( e ′ ) , k ˉ ] → s h ∗ ∗ ρ ( τ ) , [ K ( e ′ ′ ) , k ˉ ] (52) \frac{\sigma= \operatorname{last}(sh)\quad\tau\cdot K(e'') \in\operatorname{val}_{\sigma}(e')\quad\rho\text{ concretizes }\tau\quad\operatorname{wf}(sh ** \rho(\tau))}{sh,\left[K(e),K(e'),\bar{k}\right]\to sh ** \rho(\tau),\left[K(e''),\bar{k}\right]} \tag*{(52)} s h , [ K ( e ) , K ( e ′ ) , k ˉ ] → s h ∗ ∗ ρ ( τ ) , [ K ( e ′′ ) , k ˉ ] σ = last ( s h ) τ ⋅ K ( e ′′ ) ∈ val σ ( e ′ ) ρ concretizes τ wf ( s h ∗ ∗ ρ ( τ )) (52)
Example 3.12. We continue Example 3.11. After the call with identifier i 0 i_0 i 0 , we have a configuration of the form ⋯ ⋈ invEv ( fac , i 0 , 4 ) ⋈ ⋯ ⋈ retEv ( 24 ) ⋈ σ 3 , [ K ( ( ) ) , K ( { x , y , z ≢ z = ? i 0 ; … } ) ] \cdots\bowtie\operatorname{invEv}(\operatorname{fac},i_0,4) \bowtie\cdots\bowtie\operatorname{retEv}(24) \bowtie\sigma_3,\left[K(()),K(\{x,y,z \not\equiv z=?i_0;\ldots\})\right] ⋯ ⋈ invEv ( fac , i 0 , 4 ) ⋈ ⋯ ⋈ retEv ( 24 ) ⋈ σ 3 , [ K (( )) , K ({ x , y , z ≡ z = ? i 0 ; … }) ] . Observe that the called function’s continuation marker does not contain the return value; this value is transmitted only via events and is not passed to the surrounding context of the return. Rule (52) allows to remove the stack’s top and continue local evaluation of the main block with rule (36): ⋯ ⋈ retEv ( 24 ) ⋈ σ 3 ⋈ compREv ( i 0 , 24 ) ⋈ σ 3 , [ K ( { x , y , z ≢ z = 24 ; … } ) ] \cdots\bowtie\operatorname{retEv}(24) \bowtie\sigma_3 \bowtie\operatorname{compREv}(i_0,24) \bowtie\sigma_3,\left[K(\{x,y,z \not\equiv z=24;\ldots\})\right] ⋯ ⋈ retEv ( 24 ) ⋈ σ 3 ⋈ compREv ( i 0 , 24 ) ⋈ σ 3 , [ K ({ x , y , z ≡ z = 24 ; … }) ] .
Global Program Semantics
It remains to define the starting point of trace composition and the semantics of a Rust program as a whole. Definition 3.13 further defines a partial semantics, which gives meaning to a Rust expression relative to a state σ \sigma σ . Partial semantics are crucial to the soundness proofs of § 6 .
Definition 3.13 ((Partial) Program Semantics). Let e e e be a Rust expression and σ \sigma σ a concrete state. Assume a maximal sequence of applications of rules (50) to (52)
s h 0 , [ K ( e ) ] → s h 1 , [ k ˉ ] → ⋯ sh_0,[K(e)] \to sh_1,[\bar{k}] \to\cdots s h 0 , [ K ( e )] → s h 1 , [ k ˉ ] → ⋯ with s h 0 = ⟨ σ ⟩ sh_0=\langle\sigma\rangle s h 0 = ⟨ σ ⟩ . If the sequence is finite, it has the form
⟨ σ ⟩ , [ K ( e ) ] → ∗ s h , [ k ˉ ] \langle\sigma\rangle,[K(e)]\to^{*}sh,[\bar{k}] ⟨ σ ⟩ , [ K ( e )] → ∗ s h , [ k ˉ ] with → ∗ \to^{*} → ∗ being the transitive closure of → \to → . If the sequence is infinite, we define s h = lim i → ∞ s h i sh=\lim_{i\to\infty}sh_i s h = lim i → ∞ s h i . We denote the set of all such traces, whether finite or infinite, as Tr ( e , σ ) \operatorname{Tr}(e,\sigma) Tr ( e , σ ) .
For a Rust program r r r with the main function fn main() e e e , we define the set of traces for r r r as Tr ( r ) : = Tr ( main ( ) , σ ) \operatorname{Tr}(r):=\operatorname{Tr}(\operatorname{main}(),\sigma) Tr ( r ) := Tr ( main ( ) , σ ) , where σ \sigma σ is the state with dom ( σ ) = ∅ \operatorname{dom}(\sigma)=\varnothing dom ( σ ) = ∅ .
Example 3.14. Continuing Example 3.2, we start in configuration ⟨ σ 1 ⟩ , K ( ! b ) \langle\sigma_1\rangle,K(!b) ⟨ σ 1 ⟩ , K ( ! b ) . With the local evaluation, we derive ⟨ σ 1 ⟩ , K ( ! f f ) \langle\sigma_1\rangle,K(!ff) ⟨ σ 1 ⟩ , K ( ! f f ) , which is the only possible derivation. We then compose this with the local evaluation of !ff to get ⟨ σ 1 ⟩ , K ( t t ) \langle\sigma_1\rangle,K(tt) ⟨ σ 1 ⟩ , K ( tt ) . This trace is the only element of Tr ( ! b , σ 1 ) \operatorname{Tr}(!b,\sigma_1) Tr ( ! b , σ 1 ) .
Example 3.15. Let x , y x,y x , y be program variables of type u32, b b b a variable of type bool, σ 0 \sigma_0 σ 0 the state that assigns x , y x,y x , y the value 0 and b b b ff. For the Rust program x = if b { y = 3 ; y − 2 } else { y = 4 ; y + 1 } x=\operatorname{if}\ b\ \{y=3;y-2\}\ \operatorname{else}\ \{y=4;y+1\} x = if b { y = 3 ; y − 2 } else { y = 4 ; y + 1 } , we have the following traces:
⟨ σ 0 ⟩ , K ( x = if b { y = 3 ; y − 2 } else { y = 4 ; y + 1 } ) → ⟨ σ 0 ⟩ , K ( x = if f f { y = 3 ; y − 2 } else { y = 4 ; y + 1 } ) → ⟨ σ 0 ⟩ , K ( x = { y = 4 ; y + 1 } ) → ⟨ σ 0 ⟩ ⋈ σ 0 [ y ↦ 4 ] , K ( x = { ( ) ; y + 1 } ) → ⟨ σ 0 ⟩ ⋈ σ 0 [ y ↦ 4 ] , K ( x = { y + 1 } ) → ⟨ σ 0 ⟩ ⋈ σ 0 [ y ↦ 4 ] , K ( x = { 4 + 1 } ) → ⟨ σ 0 ⟩ ⋈ σ 0 [ y ↦ 4 ] , K ( x = { 5 } ) → ⟨ σ 0 ⟩ ⋈ σ 0 [ y ↦ 4 ] , K ( x = 5 ) → ⟨ σ 0 ⟩ ⋈ σ 1 [ y ↦ 4 ] [ x ↦ 5 ] , K ( ( ) ) \begin{aligned}
\langle\sigma_0\rangle,K(x=\operatorname{if}\ b\ \{y=3;y-2\}\ \operatorname{else}\ \{y=4;y+1\}) \\
&\to\langle\sigma_0\rangle,K(x=\operatorname{if}\ ff\ \{y=3;y-2\}\ \operatorname{else}\ \{y=4;y+1\}) \\
&\to\langle\sigma_0\rangle,K(x=\{y=4;y+1\})\to\langle\sigma_0\rangle\bowtie\sigma_0[y\mapsto4],K(x=\{();y+1\}) \\
&\to\langle\sigma_0\rangle\bowtie\sigma_0[y\mapsto4],K(x=\{y+1\})\to\langle\sigma_0\rangle\bowtie\sigma_0[y\mapsto4],K(x=\{4+1\}) \\
&\to\langle\sigma_0\rangle\bowtie\sigma_0[y\mapsto4],K(x=\{5\})\to\langle\sigma_0\rangle\bowtie\sigma_0[y\mapsto4],K(x=5) \\
&\to\langle\sigma_0\rangle\bowtie\sigma_1[y\mapsto4][x\mapsto5],K(())
\end{aligned} ⟨ σ 0 ⟩ , K ( x = if b { y = 3 ; y − 2 } else { y = 4 ; y + 1 }) → ⟨ σ 0 ⟩ , K ( x = if f f { y = 3 ; y − 2 } else { y = 4 ; y + 1 }) → ⟨ σ 0 ⟩ , K ( x = { y = 4 ; y + 1 }) → ⟨ σ 0 ⟩ ⋈ σ 0 [ y ↦ 4 ] , K ( x = {( ) ; y + 1 }) → ⟨ σ 0 ⟩ ⋈ σ 0 [ y ↦ 4 ] , K ( x = { y + 1 }) → ⟨ σ 0 ⟩ ⋈ σ 0 [ y ↦ 4 ] , K ( x = { 4 + 1 }) → ⟨ σ 0 ⟩ ⋈ σ 0 [ y ↦ 4 ] , K ( x = { 5 }) → ⟨ σ 0 ⟩ ⋈ σ 0 [ y ↦ 4 ] , K ( x = 5 ) → ⟨ σ 0 ⟩ ⋈ σ 1 [ y ↦ 4 ] [ x ↦ 5 ] , K (( ))
Example 3.16. Consider the expression e = { let x ; x = 1 ; x } e=\{\operatorname{let}\ x;x=1;x\} e = { let x ; x = 1 ; x } . Assume a concrete state σ 2 \sigma_2 σ 2 ; we start with ⟨ σ 2 ⟩ , K ( { let x ; … } ) \langle\sigma_2\rangle,K(\{\operatorname{let}\ x;\ldots\}) ⟨ σ 2 ⟩ , K ({ let x ; … }) . Per rule (38), the local evaluation yields the set { ⟨ σ 2 ⟩ ⋅ K ( { y ≢ y = 1 ; y } ) ∣ \{ \langle\sigma_2\rangle\cdot K(\{y \not\equiv y=1;y\}) \mid {⟨ σ 2 ⟩ ⋅ K ({ y ≡ y = 1 ; y }) ∣ y y y fresh}. Hence, rule (50) can pick any of these infinite traces to compose. We choose variable z z z as an example, yielding ⟨ σ 2 ⟩ \langle\sigma_2 \rangle ⟨ σ 2 ⟩ , K ( { z ≢ z = 1 ; z } ) \mathrm{K}(\{z \mathrel{\not\equiv} z = 1; z\}) K ({ z ≡ z = 1 ; z }) . Evaluating twice with both cases of rule (39), we have ⟨ σ 2 ⟩ ⇝ σ 2 [ z ↦ 1 ] , K ( { z ≢ z } ) \langle\sigma_2 \rangle\leadsto\sigma_2[z \mapsto1], \mathrm{K}(\{z \mathrel{\not\equiv} z\}) ⟨ σ 2 ⟩ ⇝ σ 2 [ z ↦ 1 ] , K ({ z ≡ z }) . Using rules (5) and (40), we have ⟨ σ 2 ⟩ ⇝ σ 2 [ z ↦ 1 ] , K ( { z ≢ 1 } ) \langle\sigma_2 \rangle\leadsto\sigma_2[z \mapsto1], \mathrm{K}(\{z \mathrel{\not\equiv} 1\}) ⟨ σ 2 ⟩ ⇝ σ 2 [ z ↦ 1 ] , K ({ z ≡ 1 }) , which we finally evaluate with rule (40) to ⟨ σ 2 ⟩ ⇝ σ 2 [ z ↦ 1 ] ⇝ σ 2 , K ( 1 ) \langle\sigma_2 \rangle\leadsto\sigma_2[z \mapsto1] \leadsto\sigma_2, \mathrm{K}(1) ⟨ σ 2 ⟩ ⇝ σ 2 [ z ↦ 1 ] ⇝ σ 2 , K ( 1 ) . The set Tr ( e , σ 2 ) \operatorname{Tr}(e,\sigma_2) Tr ( e , σ 2 ) is the set containing all traces of this form with different instantiations for y y y . The first and final states are the same for all these traces.
Example 3.17. Consider a very simple program like fn main() { panic!(); let x = 1; () }. Evaluating this program in state σ 0 \sigma_0 σ 0 with dom ( σ 0 ) = ∅ \operatorname{dom}(\sigma_0) = \varnothing dom ( σ 0 ) = ∅ , we first evaluate the panic expression and obtain the configuration ⟨ σ 0 ⟩ ⇝ panicEv ( ) ⇝ σ 0 , [ { ( ) ; let x = 1 ; ( ) } ] \langle\sigma_0 \rangle\leadsto\operatorname{panicEv}() \leadsto\sigma_0, [\{(); \texttt{let }x = 1; ()\}] ⟨ σ 0 ⟩ ⇝ panicEv ( ) ⇝ σ 0 , [{( ) ; let x = 1 ; ( )}] . We cannot continue from this configuration, as the well-formedness predicate allows exactly one state after a panic event and nothing else. Hence, this is the maximal application.
Example 3.18. Assume the program fn main() { 'l: loop { () } }, which is obviously non-terminating. Due to our use of chop, where ⟨ σ 0 ⟩ ∗ ∗ ⟨ σ 0 ⟩ = ⟨ σ 0 ⟩ \langle\sigma_0 \rangle** \langle\sigma_0 \rangle= \langle\sigma_0 \rangle ⟨ σ 0 ⟩ ∗ ∗ ⟨ σ 0 ⟩ = ⟨ σ 0 ⟩ , we obtain the following infinite derivation:
σ 0 , [ K ( { ′ l : loop { ( ) } } ) ] → ⟨ σ 0 ⟩ , [ K ( { lframe ( ′ l , { ( ) } ) } ) ] → ⟨ σ 0 ⟩ , [ K ( { lframe ( ′ l , ( ) ) } ) ] → ⟨ σ 0 ⟩ , [ K ( { getLoop ( l ) } ) ] → ⟨ σ 0 ⟩ , [ K ( { ′ l : loop { ( ) } } ) ] → ⋯ \begin{aligned}
\sigma_0, [\mathrm{K}(\{'l:\ \operatorname{loop}\ \{()\}\})] \\
\to\langle\sigma_0 \rangle, [\mathrm{K}(\{\operatorname{lframe}('l,\{()\})\})] \\
\to\langle\sigma_0 \rangle, [\mathrm{K}(\{\operatorname{lframe}('l,())\})] \\
\to\langle\sigma_0 \rangle, [\mathrm{K}(\{\operatorname{getLoop}(l)\})] \\
\to\langle\sigma_0 \rangle, [\mathrm{K}(\{'l:\ \operatorname{loop}\ \{()\}\})] \\
\to\cdots
\end{aligned} σ 0 , [ K ( { ′ l : loop {( )}})] → ⟨ σ 0 ⟩ , [ K ({ lframe ( ′ l , {( )})})] → ⟨ σ 0 ⟩ , [ K ({ lframe ( ′ l , ( ))})] → ⟨ σ 0 ⟩ , [ K ({ getLoop ( l )})] → ⟨ σ 0 ⟩ , [ K ( { ′ l : loop {( )}})] → ⋯ Hence, the traces for the program consist just of a singleton trace: { ⟨ σ 0 ⟩ } \{\langle\sigma_0 \rangle\} {⟨ σ 0 ⟩} . If the loop has an effect on the state, e.g., 'l: loop { x += 1 }, where x x x is originally set to 0, the resulting trace would be an infinite trace of the form ⟨ σ 0 ⟩ ⇝ [ x ↦ 1 ] ⇝ [ x ↦ 2 ] ⇝ ⋯ \langle\sigma_0 \rangle\leadsto[x \mapsto1] \leadsto[x \mapsto2] \leadsto\cdots ⟨ σ 0 ⟩ ⇝ [ x ↦ 1 ] ⇝ [ x ↦ 2 ] ⇝ ⋯ . See [17 ] for a discussion of this modeling of infinite computations.
We want to state some general properties over the set of generated concrete traces for a state and expression Tr ( e , σ ) \operatorname{Tr}(e,\sigma) Tr ( e , σ ) . In particular, we notice that for the synchronous subset of Rust presented in Fig. 1 the generated traces differ only in the names chosen for local variables, e.g., in rule (38), and chosen borrow identifiers. Hence, Definitions 3.20 and 3.22 introduce the notion of relative equality. Intuitively, two states may have different names for some variables but they must agree on a fixed set of variables.
Definition 3.19 (Renaming). Let κ : V a r → V a r \kappa: \mathit{Var} \to\mathit{Var} κ : Var → Var and ι : B I d → B I d \iota: \mathit{BId} \to\mathit{BId} ι : BId → BId be two bijective functions. Then we call κ \kappa κ a variable renaming, ι \iota ι a borrow renaming, and ( κ , ι ) (\kappa,\iota) ( κ , ι ) a renaming. Variable renamings κ \kappa κ are lifted to places in the natural manner:
κ ( p ) = { κ ( x ) if p = x κ ( p ′ ) [ n ] if p = p ′ [ n ] κ ( p ′ ) . i d e n t i f i e r if p = p ′ . i d e n t i f i e r \kappa(p) =
\begin{cases}
\kappa(x) & \text{if } p = x \\
\kappa(p')[n] & \text{if } p = p'[n] \\
\kappa(p').\mathit{identifier} & \text{if } p = p'.\mathit{identifier}
\end{cases} κ ( p ) = ⎩ ⎨ ⎧ κ ( x ) κ ( p ′ ) [ n ] κ ( p ′ ) . identifier if p = x if p = p ′ [ n ] if p = p ′ . identifier A renaming ( κ , ι ) (\kappa,\iota) ( κ , ι ) can be applied on a reference value with the function “rnm” as follows:
rnm ( κ , ι , Ref p s ( b ) ) = Ref κ ( p ) s ( ι ( b ) ) rnm ( κ , ι , Ref p s ( b ) ) = Ref κ ( p ) s ( ι ( b ) ) \begin{aligned}
\operatorname{rnm}(\kappa,\iota,\operatorname{Ref}^{s}_{p}(b)) &= \operatorname{Ref}^{s}_{\kappa(p)}(\iota(b)) \\
\operatorname{rnm}(\kappa,\iota,\operatorname{Ref}^{s}_{p}(b)) &= \operatorname{Ref}^{s}_{\kappa(p)}(\iota(b))
\end{aligned} rnm ( κ , ι , Ref p s ( b )) rnm ( κ , ι , Ref p s ( b )) = Ref κ ( p ) s ( ι ( b )) = Ref κ ( p ) s ( ι ( b )) The “rnm” function is extended to values in the usual manner; every reference contained in a value is renamed as above.
Definition 3.20 (State Equality Modulo Renaming). We define a relation ≃ V \simeq_{V} ≃ V over LAGC states and a set of variables V ⊆ V a r V \subseteq Var V ⊆ V a r . Two states σ 1 , σ 2 \sigma_1, \sigma_2 σ 1 , σ 2 are equal modulo renaming relative to V V V —written as σ 1 ≃ V σ 2 \sigma_1 \simeq_{V} \sigma_2 σ 1 ≃ V σ 2 —iff dom ( σ 1 ) ∩ V = dom ( σ 2 ) ∩ V \operatorname{dom}(\sigma_1) \cap V = \operatorname{dom}(\sigma_2) \cap V dom ( σ 1 ) ∩ V = dom ( σ 2 ) ∩ V , σ 1 ( x ) = σ 2 ( x ) \sigma_1(x) = \sigma_2(x) σ 1 ( x ) = σ 2 ( x ) for all x ∈ dom ( σ 1 ) ∩ V x \in\operatorname{dom}(\sigma_1) \cap V x ∈ dom ( σ 1 ) ∩ V , and there exists a renaming ( κ , ι ) (\kappa, \iota) ( κ , ι ) such that σ 1 ( x ) = rnm ( κ − 1 , ι − 1 , σ 2 ( κ ( x ) ) ) \sigma_1(x) = \operatorname{rnm}(\kappa^{-1}, \iota^{-1}, \sigma_2(\kappa(x))) σ 1 ( x ) = rnm ( κ − 1 , ι − 1 , σ 2 ( κ ( x ))) for all x ∈ dom ( σ 1 ) ∖ V x \in\operatorname{dom}(\sigma_1) \setminus V x ∈ dom ( σ 1 ) ∖ V and σ 2 ( x ) = rnm ( κ , ι , σ 1 ( κ − 1 ( x ) ) ) \sigma_2(x) = \operatorname{rnm}(\kappa, \iota, \sigma_1(\kappa^{-1}(x))) σ 2 ( x ) = rnm ( κ , ι , σ 1 ( κ − 1 ( x ))) for all x ∈ dom ( σ 2 ) ∖ V x \in\operatorname{dom}(\sigma_2) \setminus V x ∈ dom ( σ 2 ) ∖ V .
Proposition 3.21. For every set V ⊆ V a r V \subseteq Var V ⊆ V a r , the relation ≃ V \simeq_{V} ≃ V over LAGC states is an equivalence relation.
Definition 3.22 extends this notion to traces in the natural way.
Definition 3.22 (Trace Equality Modulo Renaming). We extend the relation ≃ V \simeq_{V} ≃ V over concrete traces for the set V ⊆ V a r V \subseteq Var V ⊆ V a r with the following inductive definition:
ε ≃ V ε \varepsilon\simeq_{V} \varepsilon ε ≃ V ε ,
s h 1 ↷ σ 1 ≃ V s h 2 ↷ σ 2 sh_1 \mathbin{\curvearrowright} \sigma_1 \simeq_{V} sh_2 \mathbin{\curvearrowright} \sigma_2 s h 1 ↷ σ 1 ≃ V s h 2 ↷ σ 2 iff s h 1 ≃ V s h 2 sh_1 \simeq_{V} sh_2 s h 1 ≃ V s h 2 and σ 1 ≃ V σ 2 \sigma_1 \simeq_{V} \sigma_2 σ 1 ≃ V σ 2 ,
s h 1 ↷ e v 1 ( v ‾ 1 ) ≃ V s h 2 ↷ e v 2 ( v ‾ 2 ) sh_1 \mathbin{\curvearrowright} ev_1(\overline{v}_1) \simeq_{V} sh_2 \mathbin{\curvearrowright} ev_2(\overline{v}_2) s h 1 ↷ e v 1 ( v 1 ) ≃ V s h 2 ↷ e v 2 ( v 2 ) iff s h 1 ≃ V s h 2 sh_1 \simeq_{V} sh_2 s h 1 ≃ V s h 2 and e v 1 ( v ‾ 1 ) = e v 2 ( v ‾ 2 ) ev_1(\overline{v}_1) = ev_2(\overline{v}_2) e v 1 ( v 1 ) = e v 2 ( v 2 ) .
We again pronounce it as equality modulo renaming relative to V V V .
Lemma 3.23 can now define the determinism of our Rust fragment in terms of generates traces.
Lemma 3.23. For each LAGC state σ \sigma σ and each expression e e e , the following propositions hold:
∣ Tr ( e , σ ) / ≃ dom ( σ ) ∣ = 1 (53) \left|\operatorname{Tr}(e,\sigma)/\simeq_{\operatorname{dom}(\sigma)}\right| = 1 \tag*{(53)} Tr ( e , σ ) / ≃ dom ( σ ) = 1 (53) ( ∀ s h ∈ Tr ( e , σ ) . s h is finite ) ∨ ( ∀ s h ∈ Tr ( e , σ ) . s h is infinite ) (54) \left(\forall sh \in\operatorname{Tr}(e,\sigma). \ sh\text{ is finite}\right) \lor\left(\forall sh \in\operatorname{Tr}(e,\sigma). \ sh\text{ is infinite}\right) \tag*{(54)} ( ∀ s h ∈ Tr ( e , σ ) . s h is finite ) ∨ ( ∀ s h ∈ Tr ( e , σ ) . s h is infinite ) (54) ∀ s h ∈ Tr ( e , σ ) . s h is finite → dom ( σ ) = dom ( last ( s h ) ) (55) \forall sh \in\operatorname{Tr}(e,\sigma). \ sh\text{ is finite} \to\operatorname{dom}(\sigma) = \operatorname{dom}(\operatorname{last}(sh)) \tag*{(55)} ∀ s h ∈ Tr ( e , σ ) . s h is finite → dom ( σ ) = dom ( last ( s h )) (55)
Equation (53) states that all traces generated for e e e starting in σ \sigma σ are equal modulo renaming relative to the variables defined in σ \sigma σ . As a direct consequence, equation (54) states that either all generated traces are finite or all are infinite. Similarly, equation (55) states that for all generated traces, the first and final state (if one exists) share their domain. Together, equations (53) to (55) give us the fact that either all generated traces are infinite or they share the final state.
Asynchronous Rust Semantics
LAGC is suitable for asynchronous and parallel code. Here, we present a semantics for asynchronous constructs in Rust and their use in a concurrent, but not parallel, setting with the most permissive scheduler possible. § 5 shows how these definitions can be used to model a realistic async runtime: Tokio, Rust’s most widely-used async runtime.
Background: Async Rust
Rust’s use of async, await, and futures seems to be similar to other programming languages such as JavaScript and active object languages like ABS [35 ] but it differs in crucial respects.
Asynchronous Rust revolves around the declaration and use of futures , i.e., representations of computations that have yet to be completed. Unlike other languages, Rust does not provide a single type for futures, but instead defines a trait, Future, which can be implemented for many types.
A simplified definition of the Future trait can be seen in Listing 2. It requires an implementor to define the output type which the future is to compute and a poll method, which returns the Poll enum. This type is either Pending or Ready(Output), the latter containing the computed value. Calling poll on a future runs the future until its next suspension point. Such a point may either be the end of the function body or another await expression. In the former case, the function returns Ready with the return value and is finished. In the latter case, we say the future yields (or “suspends”) and returns Pending.
Figure 2 (PDF p. 22). A simplified definition of Rust’s Future trait.
trait Future {
type Output;
fn poll(self) -> Poll<Self::Output>;
}
Hence, a Rust future is a state machine that advances from the initial state to its next yield and after a number of calls may reach its final state and return a value. How the future exactly progresses and when and how it computes its final value is left up to the implementation. Rust merely defines the basic operations and defines suitable abstractions on top of them. Rust futures are passive , that is, they are not automatically scheduled and need to be actively polled to be (partially) executed. On top of these asynchronous primitives, libraries can provide async runtimes, which allow scheduling of futures and can make these scheduled futures behave like “active” futures, as we discuss below.
With an expression f.await, where f is a future, a future is repeatedly polled until it is completed. If the awaited future is still pending, the await expression also suspends. Hence, await provides a suitable abstraction over polling of futures. However, the await expression is only allowed in functions declared with the async keyword, as in Listing 3. Assume that the async function “get” returns the HTML of a URL as a String. Calling “get(url),” hence, returns a future with output type String. In other languages, the created future would automatically be scheduled. In Rust, however, this is not the case; the future remains passive.
Figure 3. An example async function in Rust.
async fn check(url: &str) -> bool {
let f1 = get(url); let f2 = get(url);
let x = f1.await; let y = f2.await;
return x.len() == y.len();
}
Functions marked with async are only syntactic sugar. An asynchronous function like check is actually transformed into a regular function that returns something that implements Rust’s Future trait. The signature of the function then looks as follows
fn check(url: &str) -> impl Future<Output=bool> { ... }
Hence, when the future of function “check” polls “get” and the latter’s future is still pending, “check”’s future also returns Pending. Awaiting futures is the only way to suspend a future.2
With futures as the basic building blocks in place, a number of different schedulers and abstractions can be built. Rather than provide a single runtime for asynchronous programs, Rust leaves runtimes up to libraries and developers. A variety of runtimes have been developed with different features and specializations: Glommio [23 ] is specialized for I/O operations and is based on Linux’s io_uring; smol [60 ] is a small, simple runtime with little overhead. By far the most prevalent runtime library is Tokio [64 ] .
With a runtime set up, futures can be handed to the runtime to be scheduled; typically, this happens by calling the runtime’s “spawn” function. It is the runtime’s responsibility to keep track of these futures and calling poll on scheduled futures to advance them. These “spawned” or “scheduled” futures then are active futures and behave differently. They may be executed whenever the runtime deems fit, even in parallel to other futures. Hence, scheduled futures behave more analogously to futures in active object languages [10 ] . Such spawned futures are often called “handles” (JoinHandle in Tokio’s lingo).
Any non-scheduled future, even when created by a scheduled future, behaves passively, as described above: They only progress when polled and this polling may suspend the scheduled future. We call these non-scheduled futures “local.”
A Rust developer is unlikely to use the Future trait directly and is typically guided by Rust’s design and the existing libraries to use async functions and await expressions as the principal elements. Therefore, our semantics abstracts away from poll calls and instead focuses on awaiting local (or unscheduled) and scheduled futures. This is not due to a fundamental restriction of the approach. Modeling such lower-level steps leads to a somewhat more verbose semantics and may be justified in other contexts. Focusing on await is an important design decision and potential limitation but we believe it to be a sensible abstraction for a high-level semantics, which allows us to achieve the results laid out in the following sections.
Example 4.1 (Running Example). In this section, we demonstrate our semantics with running examples based on Listing 4. It consists of two asynchronous functions, two_power and bar, both taking one parameter of type u32 and returning a value of the same type. The former function recursively computes n ↦ 2 n n \mapsto2^n n ↦ 2 n for n ∈ N n \in\mathbb{N} n ∈ N , the latter computes m ↦ 2 m + 1 m \mapsto2^{m+1} m ↦ 2 m + 1 for m ∈ N m \in\mathbb{N} m ∈ N , using two_power. The main function schedules futures of both functions and awaits them later on, while the functions two_power and bar create and await only local futures.
Listing 4. An asynchronous program.
1 async fn two_power(n: u32) -> u32 {
2 return if n == 0 {
3 1
4 } else {
5 2 * two_power(n - 1).await
6 }; ()
7 }
8 async fn bar(m: u32) -> u32 {
9 return two_power(m).await * 2; ()
10 }
11 fn main() {
12 let f1 = spawn(two_power(1));
13 let f2 = spawn(bar(2));
14 let x = f1.await + f2.await;
15 return (); ()
16 }
Observe that the main function here is only used for ease of presentation: In truth, it must set up the runtime and does not have access to await, as it is not declared as async. But the pattern is not uncommon. See how Tokio, for instance, sets up the tokio::main macro to achieve the same effect [65 ] .
Local Evaluation
We extend the values as defined in Definition 2.1 with future identifiers F I d ⊂ V a l FId \subset Val F I d ⊂ V a l , standing for created futures. The syntax of Fig. 1 is extended with additional syntactic structures: await expressions like e . await e.\texttt{await} e . await and asynchronous functions declared as async fn foo ( x 1 , … , x n ) \texttt{foo}(x_1,\ldots,x_n) foo ( x 1 , … , x n ) , where x 1 , … , x n x_1,\ldots,x_n x 1 , … , x n are the parameters and e e e the function body. From this point on, the evaluation function is tagged with a future identifier F ∈ F I d F \in FId F ∈ F I d : val σ F \operatorname{val}_{\sigma}^{F} val σ F , which denotes the currently active future.
Evaluating a call to async function foo adds a future creation event createEv ( foo , F ′ , v ⃗ ) \operatorname{createEv}(\texttt{foo}, F', \vec{v}) createEv ( foo , F ′ , v ) , denoting that a future of foo is called with parameters v ⃗ \vec{v} v . F ′ F' F ′ is a fresh, symbolic variable standing for the created future. The symbolic variable will be concretized with a concrete future ID during composition.
val σ F ( foo ( v ⃗ ) ) = { ⟨ σ ⟩ ∘ createEv ( foo , F ′ , v ⃗ ) ∘ σ [ F ′ ↦ ∗ ] ⋅ K ( F ′ ) ∣ F ′ fresh } (56) \operatorname{val}_{\sigma}^{F}(\operatorname{foo}(\vec{v})) = \{\langle\sigma\rangle\circ\operatorname{createEv}(\operatorname{foo}, F', \vec{v}) \circ\sigma[F' \mapsto*] \cdot K(F') \mid F'\ \text{fresh}\} \tag*{(56)} val σ F ( foo ( v )) = {⟨ σ ⟩ ∘ createEv ( foo , F ′ , v ) ∘ σ [ F ′ ↦ ∗ ] ⋅ K ( F ′ ) ∣ F ′ fresh } (56)
Example 4.2. Calling two_power in line 12 of Listing 4, creates a future. The evaluation is
val σ 0 f 0 ( two_power ( 1 ) ) = { ⟨ σ 0 ⟩ ↷ createEv ( two_power , F , 1 ) ↷ σ 0 [ F ↦ ∗ ] ⋅ K ( F ) ∣ F fresh } , \operatorname{val}^{f_0}_{\sigma_0}(\operatorname{two\_power}(1)) = \{\langle\sigma_0\rangle\curvearrowright\operatorname{createEv}(\operatorname{two\_power},F,1) \curvearrowright\sigma_0[F \mapsto*]\cdot K(F)\mid F\ \text{fresh}\}, val σ 0 f 0 ( two_power ( 1 )) = {⟨ σ 0 ⟩ ↷ createEv ( two_power , F , 1 ) ↷ σ 0 [ F ↦ ∗ ] ⋅ K ( F ) ∣ F fresh } ,
assuming we are in some state σ 0 \sigma_0 σ 0 and the current future is f 0 f_0 f 0 .
A future is scheduled, or spawned, with the spawn function. We mark this with a spawn event containing the future’s identifier and the symbolic variable H, standing for a handle identifier . The latter is needed, as we formally differentiate between local futures and handles, i.e., wrappers around futures which represent the tasks. The set of all handle identifiers is called H I d ⊂ F I d HId \subset FId H I d ⊂ F I d .
val σ F ( spawn ( v ) ) = { ⟨ σ ⟩ ↷ spawnEv ( v , H ) ↷ σ [ H ↦ ∗ ] ⋅ K ( H ) ∣ H fresh } (57) \operatorname{val}^{F}_{\sigma}(\operatorname{spawn}(v)) = \{\langle\sigma\rangle\curvearrowright\operatorname{spawnEv}(v,H) \curvearrowright\sigma[H \mapsto*]\cdot K(H)\mid H\ \text{fresh}\} \tag*{(57)} val σ F ( spawn ( v )) = {⟨ σ ⟩ ↷ spawnEv ( v , H ) ↷ σ [ H ↦ ∗ ] ⋅ K ( H ) ∣ H fresh } (57)
Example 4.3. The futures created in the main function in Listing 4 are spawned. If we assume that the future created in Example 4.2 is concretized to future f t f_t f t and the future of bar is f b f_b f b , we have the spawn events spawnEv ( f t , h t ) \operatorname{spawnEv}(f_t,h_t) spawnEv ( f t , h t ) and spawnEv ( f b , h b ) \operatorname{spawnEv}(f_b,h_b) spawnEv ( f b , h b ) for some fresh handle identifiers h t h_t h t and h b h_b h b .
Evaluating an async function emits an evaluation event with the function’s name, the currently executing future and fresh symbolic variables X for the call arguments. Then, the function’s body is evaluated with parameters x substituted with X, just as in rule (34).
val σ F ( async fn foo(x) e ) = { ⟨ σ ⟩ ↷ evalEv ( foo , F , X ‾ ) ↷ σ [ X ‾ ↦ ∗ ] ⋅ K ( e [ x ‾ ← X ‾ ] ) ∣ X ‾ fresh } (58) \operatorname{val}^{F}_{\sigma}(\texttt{async fn foo(x) e}) = \{\langle\sigma\rangle\curvearrowright\operatorname{evalEv}(\operatorname{foo},F,\overline{X}) \curvearrowright\sigma[\overline{X} \mapsto*]\cdot K(e[\overline{x}\leftarrow\overline{X}])\mid\overline{X}\ \text{fresh}\} \tag*{(58)} val σ F ( async fn foo(x) e ) = {⟨ σ ⟩ ↷ evalEv ( foo , F , X ) ↷ σ [ X ↦ ∗ ] ⋅ K ( e [ x ← X ]) ∣ X fresh } (58)
Example 4.4. If we evaluate function bar of Listing 4 in state σ 2 \sigma_2 σ 2 and future h b h_b h b , we have the traces { ⟨ σ 2 ⟩ ↷ evalEv ( bar , h b , X ) ↷ σ 2 [ X ↦ ∗ ] ⋅ K ( { return two_power ( X ) . await ∗ 2 ; } ) ∣ X fresh } \{\langle\sigma_2\rangle\curvearrowright\operatorname{evalEv}(\operatorname{bar},h_b,X) \curvearrowright\sigma_2[X\mapsto*]\cdot K(\{\texttt{return two\_power}(X).\texttt{await} * 2;\})\mid X\ \text{fresh}\} {⟨ σ 2 ⟩ ↷ evalEv ( bar , h b , X ) ↷ σ 2 [ X ↦ ∗ ] ⋅ K ({ return two_power ( X ) . await ∗ 2 ; }) ∣ X fresh } .
A return expression now emits a computation event with the active future and the returned value. We replace rule (35) with the following:
val σ F ( return v ) = { ⟨ σ ⟩ ↷ compEv ( F , v ) ↷ σ ⋅ K ( ( ) ) } (59) \operatorname{val}^{F}_{\sigma}(\texttt{return }v) = \{\langle\sigma\rangle\curvearrowright\operatorname{compEv}(F,v) \curvearrowright\sigma\cdot K(())\} \tag*{(59)} val σ F ( return v ) = {⟨ σ ⟩ ↷ compEv ( F , v ) ↷ σ ⋅ K (( ))} (59)
Awaiting a local future emits a local await event localAwEv ( v ) \operatorname{localAwEv}(v) localAwEv ( v ) , where v ∈ F I d v\in FId v ∈ F I d is the awaited future. Since this event starts computation of the function represented by v v v , we model it similarly to a local synchronous call (rule (32)) and leave a “get expression” for the awaited future. This expression can now be applied to call identifiers or local futures. However, if v v v is a handle identifier, i.e., refers to a spawned future, we directly emit a computation reaction event with a symbolic result value. The well-formedness predicate ensures that an await on a spawned future is only executed once this future is completed.
val σ F ( v . await ) = { { ⟨ σ ⟩ ↷ localAwEv ( v ) ↷ σ ⋅ K ( ? v ) } if v ∈ F I d ∖ H I d { ⟨ σ ⟩ ↷ compREv ( v , V ) ↷ σ [ V ↦ ∗ ] ⋅ K ( V ) ∣ V fresh } if v ∈ H I d (60) \operatorname{val}^{F}_{\sigma}(v.\texttt{await}) =
\begin{cases}
\{\langle\sigma\rangle\curvearrowright\operatorname{localAwEv}(v) \curvearrowright\sigma\cdot K(?v)\} & \text{if } v\in FId\setminus HId\\
\{\langle\sigma\rangle\curvearrowright\operatorname{compREv}(v,V) \curvearrowright\sigma[V\mapsto*]\cdot K(V)\mid V\ \text{fresh}\} & \text{if } v\in HId
\end{cases} \tag*{(60)} val σ F ( v . await ) = { {⟨ σ ⟩ ↷ localAwEv ( v ) ↷ σ ⋅ K ( ? v )} {⟨ σ ⟩ ↷ compREv ( v , V ) ↷ σ [ V ↦ ∗ ] ⋅ K ( V ) ∣ V fresh } if v ∈ F I d ∖ H I d if v ∈ H I d (60)
Example 4.5. The futures created by the calls in Listing 4, lines 5 and 9 are local futures, which are not spawned; hence, their await expressions in those same lines “end up” in the first case of rule (60). On the other hand, the awaits in line 14 are handled by rule (60)’s second case, as the futures were spawned and are thus handle identifiers.
Well-Formedness
We adjust the well-formedness predicate as follows: A trace ending in a creation event is well-formed if the preceding trace is well-formed and does not contain a creation event with the same future identifier. The create event cannot create a handle, as those must be created via spawning.
w f ( s h ↷ createEv ( foo , f , v ‾ ) ) = w f ( s h ) ∧ f ∉ H I d ∧ ¬ ∃ foo ′ , v ‾ ′ , createEv ( foo ′ , f , v ‾ ′ ) ∈ s h (61) wf(sh \curvearrowright\operatorname{createEv}(\operatorname{foo},f,\overline{v})) = wf(sh)\land f\notin HId\land\neg\exists\operatorname{foo}',\overline{v}',\operatorname{createEv}(\operatorname{foo}',f,\overline{v}')\in sh \tag*{(61)} w f ( s h ↷ createEv ( foo , f , v )) = w f ( s h ) ∧ f ∈ / H I d ∧ ¬∃ foo ′ , v ′ , createEv ( foo ′ , f , v ′ ) ∈ s h (61)
Spawn events must follow a creation event of the spawned future and this future must not have been spawned or locally awaited already. The created handle identifier must also be unique. A future can either be local or spawned, never both. Hence, no future can have both a local await and a spawn event in the same trace.
w f ( s h ↷ s p a w n E v ( f , h ) ) = w f ( s h ) ∧ ∃ f o o , v ‾ . c r e a t e E v ( f o o , f , v ‾ ) ∈ s h ∧ l o c a l A w E v ( f ) ∉ s h ∧ ∄ h ′ . s p a w n E v ( f , h ′ ) ∈ s h ∧ ∄ f ′ . s p a w n E v ( f ′ , h ) ∈ s h (62) wf(sh \mathrel{\curvearrowright} spawnEv(f,h)) = wf(sh) \land\exists foo,\overline{v}.\ createEv(foo,f,\overline{v}) \in sh \land localAwEv(f) \notin sh
\land\nexists h'.\ spawnEv(f,h') \in sh \land\nexists f'.\ spawnEv(f',h) \in sh \tag*{(62)} w f ( s h ↷ s p a w n E v ( f , h )) = w f ( s h ) ∧ ∃ f oo , v . cr e a t e E v ( f oo , f , v ) ∈ s h ∧ l oc a l A w E v ( f ) ∈ / s h ∧ ∄ h ′ . s p a w n E v ( f , h ′ ) ∈ s h ∧ ∄ f ′ . s p a w n E v ( f ′ , h ) ∈ s h (62)
Local await events are only valid if the future has already been created but not awaited or spawned before:
w f ( s h ↷ l o c a l A w E v ( f ) ) = w f ( s h ) ∧ f ∉ H I d ∧ ∃ f o o , v ‾ . c r e a t e E v ( f o o , f , v ‾ ) ∈ s h ∧ l o c a l A w E v ( f ) ∉ s h ∧ ∄ h . s p a w n E v ( f , h ) ∈ s h wf(sh \mathrel{\curvearrowright} localAwEv(f)) = wf(sh) \land f \notin HId \land\exists foo,\overline{v}.\ createEv(foo,f,\overline{v}) \in sh
\land localAwEv(f) \notin sh \land\nexists h.\ spawnEv(f,h) \in sh w f ( s h ↷ l oc a l A w E v ( f )) = w f ( s h ) ∧ f ∈ / H I d ∧ ∃ f oo , v . cr e a t e E v ( f oo , f , v ) ∈ s h ∧ l oc a l A w E v ( f ) ∈ / s h ∧ ∄ h . s p a w n E v ( f , h ) ∈ s h
An evaluation event can only occur once per future. Additionally, it must follow a local await event or a spawn event. If the future f f f is local, it must have been created before being evaluated and the function name and parameter must match, ensuring a pairing between call and execution. If the future is a spawned future, it must have been spawned from a future f ′ f' f ′ that was created with the correct function name and arguments.
w f ( s h ↷ e v a l E v ( f o o , f , v ‾ ) ) = w f ( s h ) ∧ ∄ f o o ′ , v ‾ ′ . e v a l E v ( f o o ′ , f , v ‾ ′ ) ∈ s h ∧ ( l o c a l A w E v ( f ) ∈ s h ∧ c r e a t e E v ( f o o , f , v ‾ ) ∈ s h ∨ f ∈ H I d ∧ ∃ f ′ . s p a w n E v ( f ′ , f ) ∈ s h ∧ c r e a t e E v ( f o o , f ′ , v ‾ ) ∈ s h ) (63) \begin{aligned}
wf(sh \mathrel{\curvearrowright} evalEv(foo,f,\overline{v})) ={}& wf(sh) \land\nexists foo',\overline{v}'.\ evalEv(foo',f,\overline{v}') \in sh \\
&\land(localAwEv(f) \in sh \land createEv(foo,f,\overline{v}) \in sh \\
&\qquad\lor f \in HId \land\exists f'.\ spawnEv(f',f) \in sh \land createEv(foo,f',\overline{v}) \in sh) \tag*{(63)}
\end{aligned} w f ( s h ↷ e v a l E v ( f oo , f , v )) = w f ( s h ) ∧ ∄ f o o ′ , v ′ . e v a l E v ( f o o ′ , f , v ′ ) ∈ s h ∧ ( l oc a l A w E v ( f ) ∈ s h ∧ cr e a t e E v ( f oo , f , v ) ∈ s h ∨ f ∈ H I d ∧ ∃ f ′ . s p a w n E v ( f ′ , f ) ∈ s h ∧ cr e a t e E v ( f oo , f ′ , v ) ∈ s h ) (63)
We place no restrictions on computation events, as we can only reach a return after evaluating a function, which would already have taken care of the necessary events. Computation reaction events must follow a corresponding computation event, which leads to a case distinction: If the event is over a future, it must follow a computation event of that future. If, however, the event is over a call identifier, it was a synchronous call and, hence, the future in the computation event is the future in which the function was called; the computation reaction event must also directly succeed the computation event. There can be at most one computation reaction event per identifier. In the following, we use f i ∈ C I d ⊎ F I d f_i \in CId \uplus FId f i ∈ C I d ⊎ F I d for a future or call identifier.
w f ( s h ↷ c o m p E v ( f , v ) ) = w f ( s h ) wf(sh \mathrel{\curvearrowright} compEv(f,v)) = wf(sh) w f ( s h ↷ co m pE v ( f , v )) = w f ( s h )
w f ( s h ↷ c o m p R E v ( f i , v ) ) = w f ( s h ) ∧ ∄ v ′ . c o m p R E v ( f i , v ′ ) ∈ s h ∧ ( f i ∈ F I d → c o m p E v ( f i , v ) ∈ s h ) ∧ ( f i ∈ C I d → l a s t E v ( s h ) = c o m p E v ( f i , v ) ) \begin{aligned}
wf(sh \mathrel{\curvearrowright} compREv(f_i,v)) ={}& wf(sh) \land\nexists v'.\ compREv(f_i,v') \in sh \\
&\land(f_i \in FId \to compEv(f_i,v) \in sh) \\
&\land(f_i \in CId \to lastEv(sh) = compEv(f_i,v))
\end{aligned} w f ( s h ↷ co m pR E v ( f i , v )) = w f ( s h ) ∧ ∄ v ′ . co m pR E v ( f i , v ′ ) ∈ s h ∧ ( f i ∈ F I d → co m pE v ( f i , v ) ∈ s h ) ∧ ( f i ∈ C I d → l a s tE v ( s h ) = co m pE v ( f i , v ))
Trace Composition Rules
We define trace composition rules for async Rust that generate all possible schedules of futures or “tasks.” They are defined over configurations consisting of a bag of tasks Q Q Q . A task, thus, consists of a stack of continuation markers as in § 3.7 with two differences: (a) the continuation markers are tagged with future identifiers, e.g., K f 0 ( x + y ) K^{f_0}(x+y) K f 0 ( x + y ) , and (b) the elements of the stack represent both called functions and locally evaluated, unscheduled futures. A configuration is written as s h , ( a c t i v e , Q ) sh,(active,Q) s h , ( a c t i v e , Q ) , where s h sh s h is the current shining trace, a c t i v e active a c t i v e is the active task, i.e., the task currently being executed, or ⊥ \bot ⊥ if there is none, and Q Q Q is the current bag of tasks. This representation ensures that an active task is not prematurely preempted.
The first rule executes the top continuation marker of the active task if possible. It pops the first continuation of the stack, takes one step of the local evaluation, extends the trace by the concretization of the result if it is well-formed, and pushes the new continuation onto the stack. This rule corresponds to rule (50) extended with a bag of stacks. We highlight which parts of the rules are changed.
σ = last ( s h ) τ ⋅ K ( e ′ ) ∈ val σ f ( e ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ ) ) s h , ( [ K f ( e ) , k ‾ ] , Q ) → s h ∗ ∗ ρ ( τ ) , ( [ K f ( e ′ ) , k ‾ ] , Q ) (64) \frac{\sigma=\operatorname{last}(sh)\quad\tau\cdot K(e')\in\operatorname{val}_{\sigma}^{f}(e)\quad\rho\text{ concretizes }\tau\quad wf(sh ** \rho(\tau))}
{sh,\left(\left[K^{f}(e),\overline{k}\right],Q\right)\to sh ** \rho(\tau),\left(\left[K^{f}(e'),\overline{k}\right],Q\right)} \tag*{(64)} s h , ( [ K f ( e ) , k ] , Q ) → s h ∗ ∗ ρ ( τ ) , ( [ K f ( e ′ ) , k ] , Q ) σ = last ( s h ) τ ⋅ K ( e ′ ) ∈ val σ f ( e ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ )) (64)
Example 4.6. We continue our running Example 4.1. Assume we are in a configuration s h 0 ↷ σ 0 , ( [ K h t ( { return if n = = 0 … } ) ] , Q 0 ) sh_0 \curvearrowright\sigma_0,([K^{h_t}(\{\text{return if } n == 0 \ldots\})],Q_0) s h 0 ↷ σ 0 , ([ K h t ({ return if n == 0 … })] , Q 0 ) with σ 0 ( n ) = 1 \sigma_0(n)=1 σ 0 ( n ) = 1 . Then, we are executing function two_power in the active task, Q 0 Q_0 Q 0 must contain the task for main and, possibly, the task for bar on handle h b h_b h b (the latter may already be finished). With rule (64), we execute the body one step further to s h 0 ↷ σ 0 , ( [ K h t ( { return if 1 = = 0 … } ) ] , Q 0 ) sh_0 \curvearrowright\sigma_0,([K^{h_t}(\{\text{return if } 1 == 0 \ldots\})],Q_0) s h 0 ↷ σ 0 , ([ K h t ({ return if 1 == 0 … })] , Q 0 ) . Clearly, this is the semantics we expect, behaving similar to § 3.7 .
Rule (65) extends the active task with a new call context, executing a locally called, synchronous function, just as rule (51) does in the synchronous case. The rules are identical except for the addition of Q Q Q and the tag f f f .
σ = last ( s h ) f ∈ F I d fn foo ( x ‾ ) e ∈ F n τ ⋅ K ( e ′ ) ∈ val σ f ( fn foo ( x ‾ ) e ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ ) ) s h , ( [ k ‾ ] , Q ) → s h ∗ ∗ ρ ( τ ) , ( [ K f ( e ′ ) , k ‾ ] , Q ) (65) \frac{\sigma=\operatorname{last}(sh)\quad f\in FId\quad\text{fn foo}(\overline{x})\ e\in Fn\quad\tau\cdot K(e')\in\operatorname{val}_{\sigma}^{f}(\text{fn foo}(\overline{x})\ e)\quad\rho\text{ concretizes }\tau\quad wf(sh ** \rho(\tau))}
{sh,\left(\left[\overline{k}\right],Q\right)\to sh ** \rho(\tau),\left(\left[K^{f}(e'),\overline{k}\right],Q\right)} \tag*{(65)} s h , ( [ k ] , Q ) → s h ∗ ∗ ρ ( τ ) , ( [ K f ( e ′ ) , k ] , Q ) σ = last ( s h ) f ∈ F I d fn foo ( x ) e ∈ F n τ ⋅ K ( e ′ ) ∈ val σ f ( fn foo ( x ) e ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ )) (65)
A locally awaited future is handled exactly the same as a synchronous in call rule (65), except in this case, an active task exists and the new continuation is pushed on top. Rule (66) has no corresponding rule in § 3.7 .
σ = last ( s h ) f ∈ F I d ∖ H I d async fn foo ( x ‾ ) e ∈ F n τ ⋅ K ( e ′ ) ∈ val σ f ( async fn foo ( x ‾ ) e ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ ) ) s h , ( [ k ‾ ] , Q ) → s h ∗ ∗ ρ ( τ ) , ( [ K f ( e ′ ) , k ‾ ] , Q ) (66) \frac{\sigma=\operatorname{last}(sh)\quad f\in FId\setminus HId\quad\text{async fn foo}(\overline{x})\ e\in Fn\quad\tau\cdot K(e')\in\operatorname{val}_{\sigma}^{f}(\text{async fn foo}(\overline{x})\ e)\quad\rho\text{ concretizes }\tau\quad wf(sh ** \rho(\tau))}
{sh,\left(\left[\overline{k}\right],Q\right)\to sh ** \rho(\tau),\left(\left[K^{f}(e'),\overline{k}\right],Q\right)} \tag*{(66)} s h , ( [ k ] , Q ) → s h ∗ ∗ ρ ( τ ) , ( [ K f ( e ′ ) , k ] , Q ) σ = last ( s h ) f ∈ F I d ∖ H I d async fn foo ( x ) e ∈ F n τ ⋅ K ( e ′ ) ∈ val σ f ( async fn foo ( x ) e ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ )) (66)
Example 4.7. Rule (66) is used, for instance, for the recursive future in two_power: After the local future is awaited, the rule becomes applicable, according to w f wf w f . Like a synchronous function call, the stack is extended with the new future as top and takes the form [ K f 3 ( { return if k = = 0 … } ) , K h t ( ? f 3 ) ] [K^{f_3}(\{\text{return if } k == 0 \ldots\}),K^{h_t}(?f_3)] [ K f 3 ({ return if k == 0 … }) , K h t ( ? f 3 )] .
Similar to rule (52), we pop the top continuation of a stack, returning control to the calling function. As before, we do not require e e e to be a value. Rather, the structure of the continuation stack and the well-formedness predicate ensures that e ′ e' e ′ is a get expression, which can only be executed once the called function, represented by K f ( e ) K^{f}(e) K f ( e ) , returns by the argument for rule (52).
σ = last ( s h ) τ ⋅ K ( e ′ ′ ) ∈ val σ f ′ ( e ′ ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ ) ) s h , ( [ K f ( e ) , K f ′ ( e ′ ) , k ‾ ] , Q ) → s h , ( [ K f ′ ( e ′ ′ ) , k ‾ ] , Q ) (67) \frac{\sigma=\operatorname{last}(sh)\quad\tau\cdot K(e'')\in\operatorname{val}_{\sigma}^{f'}(e')\quad\rho\text{ concretizes }\tau\quad wf(sh ** \rho(\tau))}
{sh,\left(\left[K^{f}(e),K^{f'}(e'),\overline{k}\right],Q\right)\to sh,\left(\left[K^{f'}(e''),\overline{k}\right],Q\right)} \tag*{(67)} s h , ( [ K f ( e ) , K f ′ ( e ′ ) , k ] , Q ) → s h , ( [ K f ′ ( e ′′ ) , k ] , Q ) σ = last ( s h ) τ ⋅ K ( e ′′ ) ∈ val σ f ′ ( e ′ ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ )) (67)
Example 4.8. After the recursive future in Example 4.7 was pushed onto the stack and it was fully evaluated as in Example 4.6, the return emitted a computation event. According to the w f wf w f predicate the this event must follow, which can only be done by the ? f 3 ?f_3 ? f 3 expression in the continuation marker below the top. Thus, only rule (67) is applicable and the stack is simplified to [ K h t ( 1 ) ] [K^{h_t}(1)] [ K h t ( 1 )] .
If the stack is empty, we have no more active task and mark the configuration as idle.
s h , ( [ ] , Q ) → s h , ( I d l e , Q ) (68) sh,([],Q)\to sh,(Idle,Q) \tag*{(68)} s h , ([ ] , Q ) → s h , ( I d l e , Q ) (68)
When the next sub-expression of an expression e e e is an await on a scheduled future that has no corresponding computation event in trace s h sh s h , we call e e e suspended in s h sh s h , written suspended ( e , s h ) \operatorname{suspended}(e, sh) suspended ( e , s h ) . We define this predicate as
suspended ( e , s h ) : = ∃ h ∈ H I d . next ( e , Val ) = ( h . await , … ) ∧ ¬ ∃ v ∈ Val . compEv ( h , v ) ∈ s h . \operatorname{suspended}(e, sh) := \exists h \in HId.\ \operatorname{next}(e, \operatorname{Val}) = (h.\operatorname{await}, \ldots) \land\neg\exists v \in\operatorname{Val}.\ \operatorname{compEv}(h,v) \in sh. suspended ( e , s h ) := ∃ h ∈ H I d . next ( e , Val ) = ( h . await , … ) ∧ ¬∃ v ∈ Val . compEv ( h , v ) ∈ s h .
If the top continuation marker of the active task contains a suspended expression, we no longer have an active task and the suspended task is added to the bag of tasks. This models Rust’s behavior of an await only yielding if the awaited future is not yet ready.
suspended ( e , s h ) s h , ( [ K f ( e ) , k ‾ ] , Q ) → s h , ( Idle , Q ⊎ { [ K f ( e ) , k ‾ ] } ) (69) \frac{\operatorname{suspended}(e, sh)}{sh,\left(\left[\mathrm{K}^{f}(e),\overline{k}\right],Q\right)\to sh,\left(\operatorname{Idle},Q\uplus\left\{\left[\mathrm{K}^{f}(e),\overline{k}\right]\right\}\right)} \tag*{(69)} s h , ( [ K f ( e ) , k ] , Q ) → s h , ( Idle , Q ⊎ { [ K f ( e ) , k ] } ) suspended ( e , s h ) (69)
A scheduled future can be executed if there is no active task. Then an arbitrary function is taken, evaluated, and, if well-formed, appended to the trace. The well-formedness ensures that only scheduled functions are executed. The configuration now has an active task with the new continuation marker as its only element.
σ = last ( s h ) f ∈ H I d async fn foo ( x ‾ ) e ∈ F n τ ⋅ K ( e ′ ) ∈ val σ f ( async fn foo ( x ‾ ) e ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ ) ) s h , ( Idle , Q ) → s h ∗ ∗ ρ ( τ ) , ( [ K f ( e ′ ) ] , Q ) (70) \frac{\begin{gathered}
\sigma=\operatorname{last}(sh)\qquad f\in HId\qquad\text{async fn}~\operatorname{foo}(\overline{x})~e\in Fn\\
\tau\cdot\mathrm{K}(e')\in\operatorname{val}_{\sigma}^{f}(\text{async fn}~\operatorname{foo}(\overline{x})~e)\qquad\rho\ \text{concretizes}\ \tau\qquad wf(sh\mathbin{**}\rho(\tau))
\end{gathered}}{sh,(\operatorname{Idle},Q)\to sh\mathbin{**}\rho(\tau),\left(\left[\mathrm{K}^{f}(e')\right],Q\right)} \tag*{(70)} s h , ( Idle , Q ) → s h ∗ ∗ ρ ( τ ) , ( [ K f ( e ′ ) ] , Q ) σ = last ( s h ) f ∈ H I d async fn foo ( x ) e ∈ F n τ ⋅ K ( e ′ ) ∈ val σ f ( async fn foo ( x ) e ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ )) (70)
Example 4.9. Rule (70) may be applied to functions two_power and bar after futures of them are spawned in lines 12 and 13, as soon as there is no active task. Observe how the well-formedness predicate ensures that their identifiers are unique and that parameters match.
Alternatively, if we are idle, we can pick a task from the bag Q Q Q and add it as the active task. The chosen task is also removed from Q Q Q .
[ K f ( e ) , k ‾ ] ∈ Q s h , ( Idle , Q ) → s h , ( [ K f ( e ) , k ‾ ] , Q ∖ { [ K f ( e ) , k ‾ ] } ) (71) \frac{\left[\mathrm{K}^{f}(e),\overline{k}\right]\in Q}{sh,(\operatorname{Idle},Q)\to sh,\left(\left[\mathrm{K}^{f}(e),\overline{k}\right],Q\setminus\left\{\left[\mathrm{K}^{f}(e),\overline{k}\right]\right\}\right)} \tag*{(71)} s h , ( Idle , Q ) → s h , ( [ K f ( e ) , k ] , Q ∖ { [ K f ( e ) , k ] } ) [ K f ( e ) , k ] ∈ Q (71)
Global Program Semantics
Let r r r be a Rust program with main function fn main() e e e . We extend the global semantics of § 3.8 to our present case:
Definition 4.10 (Asynchronous Program Semantics). Let r r r be a an asynchronous Rust program with the main function fn main() e e e . Assume a maximal sequence of applications of rules (64) to (71)
s h 0 , ( [ K f i n i t ( e ) ] , ∅ ) → s h 1 , ( a c t i v e 1 , Q 1 ) → ⋯ sh_0,\left(\left[\mathrm{K}^{f_{\mathrm{init}}}(e)\right],\varnothing\right)\to sh_1,(active_1,Q_1)\to\cdots s h 0 , ( [ K f init ( e ) ] , ∅ ) → s h 1 , ( a c t i v e 1 , Q 1 ) → ⋯ with s h 0 = ⟨ σ 0 ⟩ sh_0=\langle\sigma_0\rangle s h 0 = ⟨ σ 0 ⟩ , dom ( σ 0 ) = ∅ \operatorname{dom}(\sigma_0)=\varnothing dom ( σ 0 ) = ∅ and f i n i t f_{\mathrm{init}} f init is the initial future identifier. If the sequence is finite, it has the form
⟨ σ ⟩ , ( [ K f i n i t ( e ) ] , ∅ ) → ∗ s h , ( [ k ‾ ] , Q ) \langle\sigma\rangle,\left(\left[\mathrm{K}^{f_{\mathrm{init}}}(e)\right],\varnothing\right)\to^{*}sh,\left(\left[\overline{k}\right],Q\right) ⟨ σ ⟩ , ( [ K f init ( e ) ] , ∅ ) → ∗ s h , ( [ k ] , Q ) If the sequence is infinite, we define s h = lim i → ∞ s h i sh=\lim_{i\to\infty}sh_i s h = lim i → ∞ s h i . We denote the set of all such possibly infinite traces as Tr ( r ) \operatorname{Tr}(r) Tr ( r ) .
This semantics for asynchronous programs now gives meaning to programs with Rust’s async features. It provides a highly non-deterministic scheduler that is maximally permissive: Any possible scheduling of futures in a program r r r is covered by the semantics and included in the set Tr ( r ) \operatorname{Tr}(r) Tr ( r ) . well-formedness restrictions ensure that any trace in Tr ( r ) \operatorname{Tr}(r) Tr ( r ) is valid, i.e., it follows the existing Rust semantics. While typically not desired from an actual runtime, it gives us a base to which we can compare any real-world runtime, as they must produce a subset of the traces in Tr ( r ) \operatorname{Tr}(r) Tr ( r ) .
It remains to model the Tokio runtime and show that they produce fair schedules.
Showing the Fairness of Tokio’s Schedulers
A central property of any runtime (or scheduler) is its fairness : If a future is scheduled, a developer wants guarantees that, under certain conditions, this future is eventually executed. In the case of Rust’s Tokio runtime, its documentation gives informal fairness guarantees [65 ] .
We cover both the formalization of Tokio’s scheduling behavior in terms of composition rules and a formalization of fairness guarantees, including those of the documentation. We also provide a formal proof that these guarantees hold for one of Tokio’s runtimes.
Background: Tokio
The Tokio library provides two runtimes: the current thread runtime , which works on a single thread; and the multi-threaded runtime , which provides concurrency and parallelism. Although the implementations differ slightly, both runtimes are intended to work similarly, except for the number of threads.
The following description is derived partly from Tokio’s documentation [65 ] and partly from analysis of Tokio’s code base [64 ] .
In both runtimes, each thread is associated with a worker , each of which has a local queue of tasks and, potentially, an active task (similar to the notion of active task in § 4.4 ). A task is a future scheduled on the runtime. Each worker also has a tick , a counter that is increased whenever a future is polled. Further, each runtime has a global queue of tasks. Additionally, the runtimes have a parameter for the global queue interval , call it I G ∈ N I_G \in\mathbb{N} I G ∈ N , controlling the interval in which the global queue is considered. This parameter cannot be changed while the runtime is active. To which queue a scheduled task is appended depends on where it is scheduled and the queues’ current state. Tasks can be scheduled either from a task executed by a worker or from outside the runtime, i.e., from parts of the Rust program not controlled by the runtime. If the task is scheduled from outside the runtime or the local queue is full, the task is pushed to the global queue. Otherwise, it is pushed onto the corresponding local queue.
Additionally, the runtimes track I/O timers and events. However, as I/O is more difficult to model, we ignore this feature for now and only model futures.
5.1.1 The Current Thread Runtime. The current thread runtime must decide which task to execute next if the worker is idle, i.e., if it has no active task: If the tick is divisible by I G I_G I G or the local queue is empty, remove the first task of the global queue and execute it. Otherwise, remove and execute the first task of the local queue. Thus, tasks in the local queue are prioritized while the global queue is only queried in periodic intervals.
5.1.2 The Multi-Threaded Runtime. The multi-threaded runtime not only has more workers but also some optimizations and more complex scheduling. Each worker has a last in, first out (LIFO) slot in addition to its queue. This slot contains the last locally scheduled task. The assumption is that the most recently scheduled task is often more pressing. An idle worker considers the following cases to choose its next task: (a) If the tick is divisible by I G I_G I G and the global queue is not empty, execute the first global task; (b) otherwise, if there is a task in the LIFO slot, execute it; (c) otherwise, if the local queue is not empty, execute its first task; (d) otherwise, if the global queue is not empty, take a fixed number of tasks from the global queue, execute the first task, and push the rest to the back of the local queue; (e) otherwise, try to “steal,” i.e., take, another worker’s local tasks.
Case (a) ensures that tasks in the global queue are not ignored and checked in a fixed interval. If a worker is idle, case (d) restores some balance by moving global tasks to the idle worker. Similarly, case (e) balances the load between workers.
Figure 3 shows the structure of both Tokio runtimes. We now model Tokio by extending our LAGC semantics. The local evaluation rules defined in § 4.4 are applicable to both runtimes; only the trace composition and program semantics differ between the two runtimes.
Figure 3 (PDF p. 29). The general setup of Tokio’s runtimes. q G q_G q G is the global queue of tasks. There are n n n workers w i w_i w i , each with a tick t i t_i t i , a local queue q i q_i q i , a (potentially empty) active task a c t i act_i a c t i , and a LIFO slot l i f o i lifo_i l i f o i . Note that w 1 w_1 w 1 has an active task but no task in its LIFO slot (denoted by the dotted border), whereas w n w_n w n has a task in its LIFO slot but no active task (dotted border). The current thread runtime has only one worker and no LIFO slot.
Local Evaluation
As we already defined spawning of tasks with the spawn function in § 4 , we reuse those definitions to define Tokio’s scheduler. No additional local rules are required.
Well-Formedness
We adjust and extend the definition of the w f wf w f -predicate by adding a case for the new schedule event s c h e d E v ( f o o , f ) schedEv(foo, f) sc h e d E v ( f oo , f ) , where foo is an asynchronous function and f ∈ F I d f \in FId f ∈ F I d is a future identifier. It denotes that a task for function foo with future f f f has been scheduled. This event must follow corresponding spawn and create events. This ensures a coupling of creation, spawning, and scheduling. The above requirements also ensure that evaluation is similarly associated.
w f ( s h ↷ s c h e d E v ( f o o , f ) ) = w f ( s h ) ∧ ∃ h ∈ H I d . s p a w n E v ( f , h ) ∈ s h ∧ ∃ v . c r e a t e E v ( f o o , f , v ) ∈ s h ∧ ∄ f o o ′ . s c h e d E v ( f o o ′ , f ) ∈ s h (72) \begin{aligned}
wf(sh \curvearrowright schedEv(foo, f)) &= wf(sh) \land\exists h \in HId.\ spawnEv(f,h) \in sh \\
&\quad\land\exists v.\ createEv(foo,f,v) \in sh \land\nexists foo'.\ schedEv(foo',f) \in sh \tag*{(72)}
\end{aligned} w f ( s h ↷ sc h e d E v ( f oo , f )) = w f ( s h ) ∧ ∃ h ∈ H I d . s p a w n E v ( f , h ) ∈ s h ∧ ∃ v . cr e a t e E v ( f oo , f , v ) ∈ s h ∧ ∄ f o o ′ . sc h e d E v ( f o o ′ , f ) ∈ s h (72)
All other cases are unchanged from § 4.3 .
Modeling the Current Thread Runtime
We model Tokio’s current thread runtime via the trace composition rules below. The general approach follows [24 ] .
We modify the configuration to be of the form s h , ⟨ a c t i v e , t , q G , q ⟩ sh, \langle active, t, q_G, q \rangle s h , ⟨ a c t i v e , t , q G , q ⟩ , where s h sh s h is the current trace, a c t i v e active a c t i v e is the currently active continuation stack or I d l e Idle I d l e if the scheduler is idle, t t t is the tick, q G q_G q G is the global queue, and q q q is the local queue. See Appendix § 11 for an axiomatization of queues. The rules below model the relation → c t \to_{ct} → c t to differentiate it from other possible schedules.
To ensure a deterministic scheduling, which allows us to show fairness, and to accurately model the current thread runtime, we require many composition rules. Figure 4 shows the decision tree of the current thread runtime.
Figure 4. The current thread scheduler’s rule structure. Read left to right.
If the last event was a spawn event, rule (73) appends a schedule event to the trace and, if the result is well-formed, adds a task marker ( f o o , f ) (\mathrm{foo}, f) ( foo , f ) to the end of the local queue.3 The well-formedness requirements above ensure that the schedule event contains the correct function name. We only add this event to ensure that afterwards no spawn event is the last event, guaranteeing that rule (73) is applied at most once per scheduled future. Observe that the function foo is coupled with the future f f f via the create event in equation (72) . We write q ⊕ k q \oplus k q ⊕ k for the result of enqueuing k k k to q q q .
lastEv ( s h ) = spawnEv ( f , h ) async fn foo ( x ‾ ) e ∈ F n s h ′ = s h ↷ schedEv ( f o o , f ) ↷ last ( s h ) wf ( s h ′ ) s h , ⟨ a c t i v e , t , q G , q ⟩ → c t s h ′ , ⟨ a c t i v e , t , q G , q ⊕ ( f o o , f ) ⟩ (73) \begin{aligned}
&\operatorname{lastEv}(sh)=\operatorname{spawnEv}(f,h) \qquad\text{async fn foo}(\overline{x})\ e\in\mathrm{Fn}\\
&sh'=sh\curvearrowright\operatorname{schedEv}(\mathrm{foo},f)\curvearrowright\operatorname{last}(sh) \qquad\operatorname{wf}(sh')\\
&sh,\langle active,t,q_G,q\rangle\to_{\mathrm{ct}}sh',\langle active,t,q_G,q\oplus(\mathrm{foo},f)\rangle \tag*{(73)}
\end{aligned} lastEv ( s h ) = spawnEv ( f , h ) async fn foo ( x ) e ∈ Fn s h ′ = s h ↷ schedEv ( foo , f ) ↷ last ( s h ) wf ( s h ′ ) s h , ⟨ a c t i v e , t , q G , q ⟩ → ct s h ′ , ⟨ a c t i v e , t , q G , q ⊕ ( foo , f )⟩ (73)
Example 5.1. We demonstrate the scheduling rules using Listing 4 (p. 23). The scheduler starts in configuration ⟨ σ 0 ⟩ , ⟨ K f i n i t ( e m a i n ) ⟩ , 0 , queue ( ) , queue ( ) \langle\sigma_0\rangle,\langle K^{\mathrm{finit}}(e_{\mathrm{main}})\rangle,0,\operatorname{queue}(),\operatorname{queue}() ⟨ σ 0 ⟩ , ⟨ K finit ( e main )⟩ , 0 , queue ( ) , queue ( ) , where dom ( σ 0 ) = ∅ \operatorname{dom}(\sigma_0)=\varnothing dom ( σ 0 ) = ∅ and e m a i n e_{\mathrm{main}} e main is the main function’s body.
Assume the body has been executed so that the spawn call in line 12 was just evaluated, emitting a spawn event. Then, rule (73) is the next rule to be applied and we arrive at the configuration
⟨ σ 0 ⟩ ↷ ⋯ ↷ σ 1 ↷ schedEv ( t w o _ p o w e r , f 0 ) ↷ σ 1 , \langle\sigma_0\rangle\curvearrowright\cdots\curvearrowright\sigma_1\curvearrowright\operatorname{schedEv}(\mathrm{two\_power},f_0)\curvearrowright\sigma_1, ⟨ σ 0 ⟩ ↷ ⋯ ↷ σ 1 ↷ schedEv ( two_power , f 0 ) ↷ σ 1 ,
⟨ K f i n i t ( { let f 1 = H ; … } ) ⟩ , 0 , queue ( ) , queue ( ) ⊕ ( t w o _ p o w e r , f 0 ) \langle K^{\mathrm{finit}}(\{\text{let }f_1=H;\ldots\})\rangle,0,\operatorname{queue}(),\operatorname{queue}()\oplus(\mathrm{two\_power},f_0) ⟨ K finit ({ let f 1 = H ; … })⟩ , 0 , queue ( ) , queue ( ) ⊕ ( two_power , f 0 )
We continue by presenting the rules that are analogous to the rules in § 4.4 , namely the rules where there is an active task which is to be executed, possibly causing function calls or awaiting local futures. Since an active task exists, active is a stack of continuation markers. Because the rules are so similar, we highlight which elements of the rule have changed compared to § 4.4 .
If the top continuation marker in the stack contains a non-value expression—which is equivalent to its local evaluation yielding a non-empty set—we evaluate the expression one step, add the resulting trace to the configuration, and replace the stack’s top element with the new continuation marker. Rule (74) adapts rule (64).
∄ f , h . lastEv ( s h ) = spawnEv ( f , h ) ∄ f . lastEv ( s h ) = localAwEv ( f ) σ = last ( s h ) τ ⋅ K ( e ′ ) ∈ val σ f ( e ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ ) ) s h , ⟨ [ K f ( e ) , k ˉ ] , t , q G , q ⟩ → c t s h ∗ ∗ ρ ( τ ) , ⟨ [ K f ( e ′ ) , k ˉ ] , t , q G , q ⟩ (74) \begin{aligned}
&\nexists f,h.\ \operatorname{lastEv}(sh)=\operatorname{spawnEv}(f,h)\quad\nexists f.\ \operatorname{lastEv}(sh)=\operatorname{localAwEv}(f)\\
&\sigma=\operatorname{last}(sh)\quad\tau\cdot K(e^{\prime})\in\operatorname{val}^{f}_{\sigma}(e)\quad\rho\ \text{concretizes}\ \tau\quad wf(sh\mathbin{**}\rho(\tau))\\
&sh,\left\langle\left[K^{f}(e),\bar{k}\right],t,q_G,q\right\rangle\to_{ct}sh\mathbin{**}\rho(\tau),\left\langle\left[K^{f}(e^{\prime}),\bar{k}\right],t,q_G,q\right\rangle \tag*{(74)}
\end{aligned} ∄ f , h . lastEv ( s h ) = spawnEv ( f , h ) ∄ f . lastEv ( s h ) = localAwEv ( f ) σ = last ( s h ) τ ⋅ K ( e ′ ) ∈ val σ f ( e ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ )) s h , ⟨ [ K f ( e ) , k ˉ ] , t , q G , q ⟩ → c t s h ∗ ∗ ρ ( τ ) , ⟨ [ K f ( e ′ ) , k ˉ ] , t , q G , q ⟩ (74)
If the last event was an invocation event, we evaluate the called function and push it onto the active task as in § 3.7 and § 4.4 . We adapt rule (65) to obtain rule (75). Because the last event is an invocation event, it cannot be a spawn event; hence, we need no explicit condition as in the previous rules.
lastEv ( s h ) = invEv ( f o o , … ) σ = last ( s h ) f n f o o ( x ˉ e ) ∈ F n τ ⋅ K ( e ′ ) ∈ val σ f ( e ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ ) ) s h , ⟨ [ k ˉ ] , t , q G , q ⟩ → c t s h ∗ ∗ ρ ( τ ) , ⟨ [ K f ( e ′ ) , k ˉ ] , t , q G , q ⟩ (75) \begin{aligned}
&\operatorname{lastEv}(sh)=\operatorname{invEv}(\mathrm{foo},\ldots)\quad\sigma=\operatorname{last}(sh)\quad\mathrm{fn}\ \mathrm{foo}(\bar{x}\ e)\in Fn\\
&\tau\cdot K(e^{\prime})\in\operatorname{val}^{f}_{\sigma}(e)\quad\rho\ \text{concretizes}\ \tau\quad wf(sh\mathbin{**}\rho(\tau))\\
&sh,\left\langle[\bar{k}],t,q_G,q\right\rangle\to_{ct}sh\mathbin{**}\rho(\tau),\left\langle\left[K^{f}(e^{\prime}),\bar{k}\right],t,q_G,q\right\rangle \tag*{(75)}
\end{aligned} lastEv ( s h ) = invEv ( foo , … ) σ = last ( s h ) fn foo ( x ˉ e ) ∈ F n τ ⋅ K ( e ′ ) ∈ val σ f ( e ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ )) s h , ⟨ [ k ˉ ] , t , q G , q ⟩ → c t s h ∗ ∗ ρ ( τ ) , ⟨ [ K f ( e ′ ) , k ˉ ] , t , q G , q ⟩ (75)
If the last event was a local await event, we evaluate the corresponding function and push it onto the active task. Hence, we model awaiting on local futures within the same task similar to function calls in the synchronous case. Rule (76) is an adaptation of rule (66).
lastEv ( s h ) = localAwEv ( f ) σ = last ( s h ) a s y n c f n f o o ( x ˉ e ) ∈ F n τ ⋅ K ( e ′ ) ∈ val σ f ( e ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ ) ) s h , ⟨ [ k ˉ ] , t , q G , q ⟩ → c t s h ∗ ∗ ρ ( τ ) , ⟨ [ K f ( e ′ ) , k ˉ ] , t , q G , q ⟩ (76) \begin{aligned}
&\operatorname{lastEv}(sh)=\operatorname{localAwEv}(f)\quad\sigma=\operatorname{last}(sh)\quad\mathrm{async\ fn}\ \mathrm{foo}(\bar{x}\ e)\in Fn\\
&\tau\cdot K(e^{\prime})\in\operatorname{val}^{f}_{\sigma}(e)\quad\rho\ \text{concretizes}\ \tau\quad wf(sh\mathbin{**}\rho(\tau))\\
&sh,\left\langle[\bar{k}],t,q_G,q\right\rangle\to_{ct}sh\mathbin{**}\rho(\tau),\left\langle\left[K^{f}(e^{\prime}),\bar{k}\right],t,q_G,q\right\rangle \tag*{(76)}
\end{aligned} lastEv ( s h ) = localAwEv ( f ) σ = last ( s h ) async fn foo ( x ˉ e ) ∈ F n τ ⋅ K ( e ′ ) ∈ val σ f ( e ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ )) s h , ⟨ [ k ˉ ] , t , q G , q ⟩ → c t s h ∗ ∗ ρ ( τ ) , ⟨ [ K f ( e ′ ) , k ˉ ] , t , q G , q ⟩ (76)
Example 5.2. We continue with the running example. The local await in line 9 pushes a new continuation marker for function two_power on the active stack:
s h 1 , ⟨ [ K f 1 ( … f 2 . a w a i t ∗ 2 … ) ] , t 1 , queue ( ) , queue ( ) ⊕ [ K f i n i t ( … ) ] ⟩ → s h 2 , ⟨ ( … ) , K f 2 ( … ) , K f 1 ( … ? f 2 ∗ 2 … ) ⟩ , t 1 , queue ( ) , queue ( ) ⊕ [ K f i n i t ( … ) ] ⟩ sh_1,\left\langle\left[K^{f_1}(\ldots f_2.\mathit{await}*2\ldots)\right],t_1,\operatorname{queue}(),\operatorname{queue}()\oplus\left[K^{\mathit{finit}}(\ldots)\right]\right\rangle
\to sh_2,\left\langle(\ldots),K^{f_2}(\ldots),K^{f_1}(\ldots? f_2*2\ldots)\right\rangle,t_1,\operatorname{queue}(),\operatorname{queue}()\oplus\left[K^{\mathit{finit}}(\ldots)\right]\rangle s h 1 , ⟨ [ K f 1 ( … f 2 . await ∗ 2 … ) ] , t 1 , queue ( ) , queue ( ) ⊕ [ K finit ( … ) ] ⟩ → s h 2 , ⟨ ( … ) , K f 2 ( … ) , K f 1 ( … ? f 2 ∗ 2 … ) ⟩ , t 1 , queue ( ) , queue ( ) ⊕ [ K finit ( … ) ] ⟩
Note that the timer is not increased.
Rule (77), an adaptation of rule (67), removes the top continuation marker of the active task if possible, as this implies that we awaited a local future, which is now fully evaluated, or we fully evaluated a synchronous call.
∄ f , h . lastEv ( s h ) = spawnEv ( f , h ) σ = last ( s h ) τ ⋅ K ( e ′ ′ ) ∈ val σ f ( e ′ ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ ) ) s h , ⟨ [ K f ( e ) , K f ′ ( e ′ ) , k ˉ ] , t , q G , q ⟩ → c t s h , ⟨ [ K f ′ ( e ′ ′ ) , k ˉ ] , t , q G , q ⟩ (77) \begin{aligned}
&\nexists f,h.\ \operatorname{lastEv}(sh)=\operatorname{spawnEv}(f,h)\quad\sigma=\operatorname{last}(sh)\\
&\tau\cdot K(e^{\prime\prime})\in\operatorname{val}^{f}_{\sigma}(e^{\prime})\quad\rho\ \text{concretizes}\ \tau\quad wf(sh\mathbin{**}\rho(\tau))\\
&sh,\left\langle\left[K^{f}(e),K^{f^{\prime}}(e^{\prime}),\bar{k}\right],t,q_G,q\right\rangle\to_{ct}sh,\left\langle\left[K^{f^{\prime}}(e^{\prime\prime}),\bar{k}\right],t,q_G,q\right\rangle \tag*{(77)}
\end{aligned} ∄ f , h . lastEv ( s h ) = spawnEv ( f , h ) σ = last ( s h ) τ ⋅ K ( e ′′ ) ∈ val σ f ( e ′ ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ )) s h , ⟨ [ K f ( e ) , K f ′ ( e ′ ) , k ˉ ] , t , q G , q ⟩ → c t s h , ⟨ [ K f ′ ( e ′′ ) , k ˉ ] , t , q G , q ⟩ (77)
Example 5.3. Continuing Example 5.2, we find that rule (77) can be applied in the execution of Listing 4 when the call to two_power from bar (line 9) is fully computed. Then, the top continuation marker is popped off the stack.
If the active continuation stack is empty, we remove it and make the scheduler idle. Rule (78) adapts rule (68).
∄ f , h . lastEv ( s h ) = spawnEv ( f , h ) s h , ⟨ [ ] , t , q G , q ⟩ → c t s h , ⟨ I d l e , t , q G , q ⟩ (78) \begin{aligned}
&\nexists f,h.\ \operatorname{lastEv}(sh)=\operatorname{spawnEv}(f,h)\\
&sh,\langle[],t,q_G,q\rangle\to_{ct}sh,\langle\mathit{Idle},t,q_G,q\rangle \tag*{(78)}
\end{aligned} ∄ f , h . lastEv ( s h ) = spawnEv ( f , h ) s h , ⟨[ ] , t , q G , q ⟩ → c t s h , ⟨ Idle , t , q G , q ⟩ (78)
When the expression of the active task is suspended in s h sh s h , the worker is made idle and the stack is pushed to the end of the queue. Rule (79) follows rule (69).
∄ f , h . lastEv ( s h ) = spawnEv ( f , h ) suspended ( e , s h ) s h , ⟨ [ K f ( e ) , k ‾ ] , t , q G , q ⟩ → c t s h , ⟨ I d l e , t , q G , q ⊕ [ K f ( e ) , k ‾ ] ⟩ (79) \begin{gathered}
\not\exists f,h.\ \operatorname{lastEv}(sh)=\operatorname{spawnEv}(f,h)\qquad\operatorname{suspended}(e,sh)\\
sh,\left\langle\left[\mathrm{K}^{f}(e),\overline{k}\right],t,q_G,q\right\rangle\to_{ct} sh,\left\langle\mathrm{Idle},t,q_G,q\oplus\left[\mathrm{K}^{f}(e),\overline{k}\right]\right\rangle \tag*{(79)}
\end{gathered} ∃ f , h . lastEv ( s h ) = spawnEv ( f , h ) suspended ( e , s h ) s h , ⟨ [ K f ( e ) , k ] , t , q G , q ⟩ → c t s h , ⟨ Idle , t , q G , q ⊕ [ K f ( e ) , k ] ⟩ (79)
Observe that if rule (79) is applicable, rule (74) cannot be applicable, as the resulting trace cannot be well-formed if the computation event is missing.
Example 5.4. We continue the running example by demonstrating suspension. After the initial task has spawned its sub-tasks (lines 12 and 13) and reached the first await in line 14, it cannot continue until the first task is resolved. Hence, the initial task is suspended and rule (79) is applicable:
s h 3 , ⟨ [ K f i n i t ( … ) ] , t 2 , queue ( ) , queue ( ) ⊕ ( t w o _ p o w e r , f 0 ) ⊕ ( b a r , f 1 ) ⟩ → s h 4 , ⟨ I d l e , t 2 , queue ( ) , queue ( ) ⊕ ( t w o _ p o w e r , f 0 ) ⊕ ( b a r , f 1 ) ⊕ [ K f i n i t ( … ) ] ⟩ sh_3,\left\langle\left[\mathrm{K}^{f_{\mathrm{init}}}(\ldots)\right],t_2,\operatorname{queue}(),\operatorname{queue}()\oplus(\mathrm{two\_power},f_0)\oplus(\mathrm{bar},f_1)\right\rangle
\to sh_4,\left\langle\mathrm{Idle},t_2,\operatorname{queue}(),\operatorname{queue}()\oplus(\mathrm{two\_power},f_0)\oplus(\mathrm{bar},f_1)\oplus\left[\mathrm{K}^{f_{\mathrm{init}}}(\ldots)\right]\right\rangle s h 3 , ⟨ [ K f init ( … ) ] , t 2 , queue ( ) , queue ( ) ⊕ ( two_power , f 0 ) ⊕ ( bar , f 1 ) ⟩ → s h 4 , ⟨ Idle , t 2 , queue ( ) , queue ( ) ⊕ ( two_power , f 0 ) ⊕ ( bar , f 1 ) ⊕ [ K f init ( … ) ] ⟩
Now we model the case of the idle worker, following the descriptions in § 5.1 . If rule (73) is not applicable, the tick is divisible by the global interval, the global queue is not empty (pop is defined), and the configuration is idle, then rule (80) takes the first task of q G q_G q G , advances the tick, and changes the active task to the popped one.
∄ f , h . lastEv ( s h ) = spawnEv ( f , h ) t ≡ 0 ( m o d I G ) pop ( q G ) = ( q G ′ , [ k ] ‾ ) s h , ⟨ I d l e , t , q G , q ⟩ → c t s h , ⟨ [ k ‾ ] , t + 1 , q G ′ , q ⟩ (80) \begin{gathered}
\not\exists f,h.\ \operatorname{lastEv}(sh)=\operatorname{spawnEv}(f,h)\qquad t\equiv0\pmod{I_G}\qquad\operatorname{pop}(q_G)=(q'_G,\overline{[k]})\\
sh,\langle\mathrm{Idle},t,q_G,q\rangle\to_{ct}sh,\left\langle\left[\overline{k}\right],t+1,q'_G,q\right\rangle \tag*{(80)}
\end{gathered} ∃ f , h . lastEv ( s h ) = spawnEv ( f , h ) t ≡ 0 ( mod I G ) pop ( q G ) = ( q G ′ , [ k ] ) s h , ⟨ Idle , t , q G , q ⟩ → c t s h , ⟨ [ k ] , t + 1 , q G ′ , q ⟩ (80)
An analogous rule is required for the case that the first task in q G q_G q G is not a continuation marker but a task marker: Rule (81) evaluates the function of the task marker one step, concretizes it, and makes it the active task.
∄ f , h . lastEv ( s h ) = spawnEv ( f , h ) t ≡ 0 ( m o d I G ) pop ( q G ) = ( q G ′ , ( f o o , f ) ) σ = last ( s h ) lookup ( f o o ) = a s y n c f n f o o ( x ) e τ ⋅ K ( e ′ ) ∈ val σ f ( a s y n c f n f o o ( x ) e ) ρ concretizes τ wf ( s h ∗ ∗ ρ ( τ ) ) s h , ⟨ I d l e , t , q G , q ⟩ → c t s h ∗ ∗ ρ ( τ ) , ⟨ [ K ( e ′ ) ] , t + 1 , q G ′ , q ⟩ (81) \begin{gathered}
\not\exists f,h.\ \operatorname{lastEv}(sh)=\operatorname{spawnEv}(f,h)\qquad t\equiv0\pmod{I_G}\\
\operatorname{pop}(q_G)=(q'_G,(\mathrm{foo},f))\qquad\sigma=\operatorname{last}(sh)\qquad\operatorname{lookup}(\mathrm{foo})=\mathrm{async\ fn\ foo}(x)\ e\\
\tau\cdot\mathrm{K}(e')\in\operatorname{val}^{f}_{\sigma}(\mathrm{async\ fn\ foo}(x)\ e)\qquad\rho\ \operatorname{concretizes}\ \tau\qquad\operatorname{wf}(sh\mathbin{**}\rho(\tau))\\
sh,\langle\mathrm{Idle},t,q_G,q\rangle\to_{ct}sh\mathbin{**}\rho(\tau),\left\langle\left[\mathrm{K}(e')\right],t+1,q'_G,q\right\rangle \tag*{(81)}
\end{gathered} ∃ f , h . lastEv ( s h ) = spawnEv ( f , h ) t ≡ 0 ( mod I G ) pop ( q G ) = ( q G ′ , ( foo , f )) σ = last ( s h ) lookup ( foo ) = async fn foo ( x ) e τ ⋅ K ( e ′ ) ∈ val σ f ( async fn foo ( x ) e ) ρ concretizes τ wf ( s h ∗ ∗ ρ ( τ )) s h , ⟨ Idle , t , q G , q ⟩ → c t s h ∗ ∗ ρ ( τ ) , ⟨ [ K ( e ′ ) ] , t + 1 , q G ′ , q ⟩ (81)
Rule (82) proceeds similarly in the case where the local queue is empty: The first global task is taken and executed.
∄ f , h . lastEv ( s h ) = spawnEv ( f , h ) t ≢ 0 ( m o d I G ) empty ( q ) pop ( q G ) = ( q G ′ , [ k ] ‾ ) s h , ⟨ I d l e , t , q G , q ⟩ → c t s h , ⟨ [ k ‾ ] , t + 1 , q G ′ , q ⟩ (82) \begin{gathered}
\not\exists f,h.\ \operatorname{lastEv}(sh)=\operatorname{spawnEv}(f,h)\qquad t\not\equiv0\pmod{I_G}\qquad\operatorname{empty}(q)\qquad\operatorname{pop}(q_G)=(q'_G,\overline{[k]})\\
sh,\langle\mathrm{Idle},t,q_G,q\rangle\to_{ct}sh,\left\langle\left[\overline{k}\right],t+1,q'_G,q\right\rangle \tag*{(82)}
\end{gathered} ∃ f , h . lastEv ( s h ) = spawnEv ( f , h ) t ≡ 0 ( mod I G ) empty ( q ) pop ( q G ) = ( q G ′ , [ k ] ) s h , ⟨ Idle , t , q G , q ⟩ → c t s h , ⟨ [ k ] , t + 1 , q G ′ , q ⟩ (82)
Otherwise, the first local task is executed per rule (83).
∄ f , h . lastEv ( s h ) = spawnEv ( f , h ) t ≢ 0 ( m o d I G ) pop ( q ) = ( q G ′ , [ k ] ‾ ) s h , ⟨ I d l e , t , q G , q ⟩ → c t s h , ⟨ [ k ‾ ] , t + 1 , q G , q ′ ⟩ (83) \begin{gathered}
\not\exists f,h.\ \operatorname{lastEv}(sh)=\operatorname{spawnEv}(f,h)\qquad t\not\equiv0\pmod{I_G}\qquad\operatorname{pop}(q)=(q'_G,\overline{[k]})\\
sh,\langle\mathrm{Idle},t,q_G,q\rangle\to_{ct}sh,\left\langle\left[\overline{k}\right],t+1,q_G,q'\right\rangle \tag*{(83)}
\end{gathered} ∃ f , h . lastEv ( s h ) = spawnEv ( f , h ) t ≡ 0 ( mod I G ) pop ( q ) = ( q G ′ , [ k ] ) s h , ⟨ Idle , t , q G , q ⟩ → c t s h , ⟨ [ k ] , t + 1 , q G , q ′ ⟩ (83)
Analogous rules to rules (82) and (83), where the task from the queue is not a continuation marker but a task marker, are defined as rules (89) and (90) in Appendix § 12 .
Example 5.5. We continue from Example 5.4: Assume the two tasks in Listing 4 have been spawned (lines 12 and 13) but not yet started and the initial task is suspended while it waits for the spawned tasks. Then, the program is in an idle configuration and we can apply rule (90), yielding the following transition:
s h 5 , ⟨ I d l e , t 3 , queue ( ) , queue ( ) ⊕ ( t w o _ p o w e r , f 0 ) ⊕ ( b a r , f 1 ) ⊕ [ K f i n i t ( … ) ] ⟩ → s h 6 , ⟨ [ K f 0 ( e 2 ) ] , t 3 + 1 , queue ( ) , queue ( ) ⊕ ( b a r , f 1 ) ⊕ [ K f i n i t ( … ) ] ⟩ \begin{aligned}
sh_5,\langle Idle,t_3,\operatorname{queue}(),\operatorname{queue}() \oplus(\mathrm{two\_power},f_0) \oplus(\mathrm{bar},f_1) \oplus[K^{f_{\mathrm{init}}}(\ldots)]\rangle\\
\to sh_6,\langle[K^{f_0}(e_2)],t_3+1,\operatorname{queue}(),\operatorname{queue}() \oplus(\mathrm{bar},f_1) \oplus[K^{f_{\mathrm{init}}}(\ldots)]\rangle
\end{aligned} s h 5 , ⟨ I d l e , t 3 , queue ( ) , queue ( ) ⊕ ( two_power , f 0 ) ⊕ ( bar , f 1 ) ⊕ [ K f init ( … )]⟩ → s h 6 , ⟨[ K f 0 ( e 2 )] , t 3 + 1 , queue ( ) , queue ( ) ⊕ ( bar , f 1 ) ⊕ [ K f init ( … )]⟩
where e 2 e_2 e 2 is the expression left after evaluating the function body of two_power one step and s h 2 sh_2 s h 2 is the extension of s h 1 sh_1 s h 1 with the resulting trace. Note that the time is incremented and the local queue is shortened by one element. The cases for starting function bar and continuing the initial task are similar, though the latter uses rule (83).
The definition of the rules with their restrictive conditions ensures a deterministic scheduler.
Proposition 5.6. Rules (73) to (83), (89) and (90) are mutually exclusive and model a deterministic, well-defined scheduler.
Fairness
When we consider the fairness of the runtime defined above, the question arises, which definition of fairness to examine. Strong fairness, i.e., every expression which is schedulable infinitely often is executed as some point, is too strong for our case. As demonstrated by Hähnle and Henrio [24 ] , weak fairness, i.e., every expression which is always schedulable from some point onwards is eventually executed, is inadequate for cooperative scheduling (e.g., Tokio) and is even inadequate for the more straightforward case of ProMeLa [33 ] . This limitation is easy to see in the runtime above: So long as we have an active task, the composition rules permit no other task to be scheduled, even when it is “ready.” However, weak fairness guarantees only hold for a task that is continuously schedulable. Thus, weak fairness offers no guarantees for the Tokio runtime at all.
To remedy this limitation, quiescent fairness is introduced [24 ] , which is more suitable to cooperative settings. Intuitively, it states that if a task is continuously schedulable when there is no active task, it will be scheduled at some future point. Hence, it is an adaption of weak fairness, as a task need not be schedulable in every state but only on quiescent states , i.e., states with no active task.
If we consider programs with no locally infinite computations , i.e., programs where at any point in time, there will be a quiescent state in the future, we can formally state quiescent fairness of the current thread scheduler. Theorem 5.7 assumes a function “trans,” translating a current thread runtime configuration s h , ⟨ a c t , t , q G , q ⟩ sh,\langle act,t,q_G,q\rangle s h , ⟨ a c t , t , q G , q ⟩ into a configuration s h ′ , ⟨ a c t , Q ⟩ sh',\langle act,Q\rangle s h ′ , ⟨ a c t , Q ⟩ as in § 4.4 . See Appendix § 13 for a formal treatment.
Theorem 5.7 (Quiescent Fairness). Assume a program with no locally infinite computations. Starting from an initial configuration, the current thread scheduler is quiescent fair: If
C 0 → c t C 1 → c t C 2 → c t ⋯ C_0 \to_{ct} C_1 \to_{ct} C_2 \to_{ct} \cdots C 0 → c t C 1 → c t C 2 → c t ⋯ then there is a quiescent fair execution
⟨ σ 0 ⟩ , [ K f i n i t ( e ) ] → ∗ trans ( C 1 ) → ∗ trans ( C 2 ) → ⋯ \langle\sigma_0\rangle,[K^{f_{\mathrm{init}}}(e)] \overset{*}{\to} \operatorname{trans}(C_1) \overset{*}{\to} \operatorname{trans}(C_2) \to\cdots ⟨ σ 0 ⟩ , [ K f init ( e )] → ∗ trans ( C 1 ) → ∗ trans ( C 2 ) → ⋯ producing the same trace (excluding schedEv events).
With the LAGC approach presented in [24 ] , Theorem 5.7 is straightforward (albeit tedious) to prove; see Appendix § 13 . However, the Tokio documentation, informally, claims a different notion of fairness for both runtimes [64 ] : If, for a given Rust program r r r , there is some T max ∈ N T_{\max} \in\mathbb{N} T m a x ∈ N such that at any point the number of tasks in the runtime is less or equal T max T_{\max} T m a x and there is some P max ∈ N P_{\max} \in\mathbb{N} P m a x ∈ N that the evaluation of a poll call on a task in the scheduler finishes within P max P_{\max} P m a x time units, then there is some D max ∈ N D_{\max} \in\mathbb{N} D m a x ∈ N such that a task is scheduled in D max D_{\max} D m a x time units.
Definition 5.8 (Tokio Fairness). Let r r r be an asynchronous Rust program. Assume there are T max , P max ∈ N T_{\max}, P_{\max} \in\mathbb{N} T m a x , P m a x ∈ N such that for all infinite sequences of LAGC rule applications C 0 → C 1 → ⋯ C_0 \to C_1 \to\cdots C 0 → C 1 → ⋯ the following holds
∀ i . numTasks ( Q i ) ≤ T max \forall i.\ \operatorname{numTasks}(Q_i) \le T_{\max} ∀ i . numTasks ( Q i ) ≤ T m a x where Q i Q_i Q i is the set of task stacks in the configuration C i C_i C i and numTasks ( Q ) = ∣ { [ k ˉ ] ∈ Q ∣ [ k ˉ ] ≠ [ ] } ∣ \operatorname{numTasks}(Q) = \left|\left\{\left[\bar{k}\right] \in Q \mid\left[\bar{k}\right] \ne[]\right\}\right| numTasks ( Q ) = { [ k ˉ ] ∈ Q ∣ [ k ˉ ] = [ ] } .
Assume further that all local computations finish in P max P_{\max} P m a x time units. Then the infinite sequence of rule applications C 0 → C 1 → ⋯ C_0 \to C_1 \to\cdots C 0 → C 1 → ⋯ is Tokio fair if there is a D max ∈ N D_{\max} \in\mathbb{N} D m a x ∈ N such that
∀ m , e , f . ( ∀ n ≥ m . quiescent ( trans ( C n ) ) ⇒ K f ( e ) ∈ enabled ( trans ( C n ) ) ) \forall m,e,f.\ \left(\forall n \ge m.\ \operatorname{quiescent}(\operatorname{trans}(C_n)) \Rightarrow K^f(e) \in\operatorname{enabled}(\operatorname{trans}(C_n))\right) ∀ m , e , f . ( ∀ n ≥ m . quiescent ( trans ( C n )) ⇒ K f ( e ) ∈ enabled ( trans ( C n )) ) ⇒ ( ∃ n ′ . m < n ′ ≤ m + D max ∧ K f ( e ) ∈ newConts ( trans ( C n ′ ) , trans ( C n ′ + 1 ) ) ) \Rightarrow\left(\exists n'.\ m < n' \le m + D_{\max} \land K^f(e) \in\operatorname{newConts}(\operatorname{trans}(C_{n'}),\operatorname{trans}(C_{n'+1}))\right) ⇒ ( ∃ n ′ . m < n ′ ≤ m + D m a x ∧ K f ( e ) ∈ newConts ( trans ( C n ′ ) , trans ( C n ′ + 1 )) )
We can show that the current thread runtime is Tokio fair as a result of it being quiescent fair (Theorem 5.9 ). In fact, we show that each quiescent fair scheduler where a scheduling distance with a well-founded order can be defined (as we do in the fairness proof in Appendix § 13 ) is also Tokio fair.
Theorem 5.9. Assume an infinite sequences of LAGC rule applications C 0 → c t C 1 → c t ⋯ C_0 \to_{\mathrm{ct}} C_1 \to_{\mathrm{ct}} \cdots C 0 → ct C 1 → ct ⋯ , then there is a Tokio fair execution ⟨ σ 0 ⟩ \langle\sigma_0 \rangle ⟨ σ 0 ⟩ , { K f i n i t ( e ) } → ∗ trans ( C 1 ) → ⋯ \{K^{f_{\mathrm{init}}}(e)\} \to^* \operatorname{trans}(C_1) \to\cdots { K f init ( e )} → ∗ trans ( C 1 ) → ⋯ producing the same trace (excluding schedEv events).
Proof Sketch. Tokio fairness follows from quiescent fairness and the fact that we can construct a D max ∈ N D_{\max} \in\mathbb{N} D m a x ∈ N from the distance function we used to proof quiescent fairness in Appendix § 13 in combination with T max T_{\max} T m a x and P max P_{\max} P m a x . See Appendix § 14 for a full proof. □
A Comment on the Multi-Thread Runtime
The present paper is focussed on the current thread runtime and we omit a formalization and fairness proof of the multi-tread runtime. While the multi-thread runtime is more widely used, we leave it for future work.
Creating a formalization of the multi-thread runtime consists mainly in creation of new trace composition rules; local evaluation and well-formedness can be left unaltered. Developing these composition rules from the existing rules is a relatively straightforward process. The configuration needs to be extended to support a fixed number of workers, each containing a local queue; the global queue is left as is.
We do not present these composition rules for two reasons. First, for space limitations. Second, because in the multi-threaded runtime, idle workers may steal tasks from other workers to spread the workload more evenly. This stealing procedure is only described conceptually, the concrete algorithm for it is left oblique. Study of the complex code is a time consuming task.
More adaption would be necessary to prove fairness of the multi-threaded runtime. Because multiple workers execute in parallel, an additional counter atop the “local” counter in Appendix § 13 is required to ensure that the composition rules do not “starve” a worker. Additionally, parallelism causes the scheduler to be non-deterministic, which complicates the fairness reasoning. It is possible to overcome these hurdles, but space reasons do not permit an attempt in the present work.
Application: Soundness of a Dynamic Logic Calculus
One promising area of application for our semantics is the soundness of program logic calculi. The development of program logics to provide semantics for a language and proof correctness of programs go back to Hoare [31 ] . Since then, Pratt [53 ] introduced dynamic logic (DL) , an extension of first-order logic, where programs can appear in formulas, e.g., [ x = x + 1 ; ] ( x > 0 ) [x = x + 1;](x > 0) [ x = x + 1 ; ] ( x > 0 ) . DL is closed under all logical operators. Hence, any Hoare triple { ϕ } P { ψ } \{\phi\}P\{\psi\} { ϕ } P { ψ } , where P P P is a program and ϕ , ψ \phi,\psi ϕ , ψ are formulas can be expressed in DL as ϕ → [ P ] ψ \phi\to[P]\psi ϕ → [ P ] ψ . DL is also strictly more expressive than Hoare logic [2 ] . DLs have proven especially helpful for program verification close to the source code level, as there is a reduced mental and semantical gap between the program under verification and the formulas. The DL developed for Java as part of the KeY verification system [1 ] , JavaDL, has been especially successful [5 ] .
As such program logic calculi are designed to be highly modular, i.e., typically have one rule per language construct, a modular semantics is needed to show soundness. So far, the discrepancy between calculus design and semantics design hindered or outright prevented soundness proofs for program logic calculi.
As no suitable semantics was defined, soundness of JavaDL’s calculus rules dealing with programs—mainly its symbolic execution rules—could not be shown, even though it is “[t]he most important property of the JavaDL calculus” [1 ] . Since real-world programming languages rarely have (official) formal semantics and program semantics are typically difficult to relate to the semantics of a logic, resolving this issue has been difficult. In this section, we show how the modular semantics developed in § 3 can be used for soundness proofs of a DL for Rust.
In previous work [21 , 22 ] we presented a DL called “RustyDL” and a calculus to verify Rust programs. This calculus has been implemented in the KeY system. While RustyDL’s calculus rules have been validated by using examples, any formal verification system needs a formal proof of soundness. As most Rust verification tools rely on translations to other languages—e.g., Prusti [3 ] translating to Viper [51 ] —correctness proofs of the tools rely on a semantics of both Rust and the target language and the correct relation between the two. RustyDL on the other hand, can be proven correct merely by showing that all its rules are sound.
In this section we only consider the sequential subset of Rust discussed in § 3 . While DLs for concurrent languages have been proposed [15 , 17 , 19 ] , the non-determinicity of concurrent language features significantly alters the semantics and calculus. § 6.1 and § 6.2 largely recapitulate the results of [21 ] , with minor modifications for brevity and presentation, while § 6.3 contains our new contributions.
RustyDL extends first-order logic with modalities containing Rust program fragments. Hence, formula ϕ : = x ≥ 0 → ⟨ x + = 1 ; ⟩ ( x > 0 ) \phi:= x \ge0 \to\langle x + = 1; \rangle(x > 0) ϕ := x ≥ 0 → ⟨ x + = 1 ; ⟩ ( x > 0 ) expresses that if x x x is greater than or equal to 0 in a state s s s and the program “x += 1;” is executed in s s s , then it terminates in state s ′ s' s ′ , in which x > 0 x > 0 x > 0 holds. We prove such a formula valid, i.e., ⊨ ϕ \models\phi ⊨ ϕ , by deriving the sequent ⟹ ϕ \Longrightarrow\phi ⟹ ϕ in our sequent calculus.
Syntax and Semantics of RustyDL
The set of dynamic logic terms of sort A A A , called D L T r m A \mathrm{DLTrm}_A DLTrm A , is defined as for sorted FOL with an additional base case: x ∈ D L T r m A x \in\mathrm{DLTrm}_A x ∈ DLTrm A for all x ∈ V a r x \in Var x ∈ V a r where x x x ’s type maps to the sort A A A .5 Similarly, the set of dynamic logic formulas D L F m l \mathrm{DLFml} DLFml is similar to FOL extended with ⟨ p ⟩ ϕ \langle p\rangle\phi ⟨ p ⟩ ϕ (“diamond”) and [ p ] ϕ [p]\phi [ p ] ϕ (“box”) where p p p is a program fragment and ϕ ∈ D L F m l \phi\in\mathrm{DLFml} ϕ ∈ DLFml . Note that D L F m l \mathrm{DLFml} DLFml is closed under all logical operatives and nesting of modalities, e.g., ⟨ p 1 ⟩ ( ϕ ∧ ⟨ p 2 ⟩ ψ ) \langle p_1\rangle(\phi\land\langle p_2\rangle\psi) ⟨ p 1 ⟩ ( ϕ ∧ ⟨ p 2 ⟩ ψ ) is a syntactically correct formula.
To manage fine-grained state changes, we add the notion of updates (Definition 6.1) to express uniform, total state changes in our logic. These updates may modify program variables or modify memory locations borrowed by a mutable reference.
Definition 6.1 (Update, Update Application). We define the set U p d \mathrm{Upd} Upd inductively by:
( a : = t ) ∈ U p d (a := t) \in\mathrm{Upd} ( a := t ) ∈ Upd for each program variable a : A a : A a : A and each term t t t of sort A A A ,
( t 1 ∗ → t 2 ) ∈ U p d (t_1 \mathrel{\ast\to} t_2) \in\mathrm{Upd} ( t 1 ∗ → t 2 ) ∈ Upd for each term t 1 t_1 t 1 of sort R e f A m \mathrm{Ref}^{m}_{A} Ref A m and t 2 t_2 t 2 of sort A A A ,
s k i p ∈ U p d \mathrm{skip} \in\mathrm{Upd} skip ∈ Upd ,
( u 1 ∥ u 2 ) ∈ U p d (u_1 \mathbin{\|} u_2) \in\mathrm{Upd} ( u 1 ∥ u 2 ) ∈ Upd for all updates u 1 , u 2 ∈ U p d u_1, u_2 \in\mathrm{Upd} u 1 , u 2 ∈ Upd ,
{ u 1 } u 2 ∈ U p d \{u_1\}u_2 \in\mathrm{Upd} { u 1 } u 2 ∈ Upd for all updates u 1 , u 2 ∈ U p d u_1, u_2 \in\mathrm{Upd} u 1 , u 2 ∈ Upd .
We extend the inductive definition of terms with term { u } t ∈ D L T r m A \{u\}t \in\mathrm{DLTrm}_{A} { u } t ∈ DLTrm A for all updates u ∈ U p d u \in\mathrm{Upd} u ∈ Upd and terms t ∈ D L T r m A t \in\mathrm{DLTrm}_{A} t ∈ DLTrm A . The inductive definition of formulas is extended with formula { u } ϕ \{u\}\phi { u } ϕ for all updates u ∈ U p d u \in\mathrm{Upd} u ∈ Upd and formulas ϕ ∈ D L F m l \phi\in\mathrm{DLFml} ϕ ∈ DLFml . Update applications represent state changes to terms and formulas syntactically and change the state in which a formula or term is evaluated.
Example 6.2. Let a a a , b b b be program variables of type u64 and c c c of type &mut u64. Consider the following examples of updates:
s k i p \mathrm{skip} skip represents the effectless update.
An elementary update a : = 2 ⋅ a a := 2 \cdot a a := 2 ⋅ a expresses a state change where a a a ’s value is doubled. Hence, { a : = 2 ⋅ a } a \{a := 2 \cdot a\}a { a := 2 ⋅ a } a is semantically equivalent to 2 ⋅ a 2 \cdot a 2 ⋅ a .
A parallel update ( a : = b ∥ b : = a ) (a := b \mathbin{\|} b := a) ( a := b ∥ b := a ) represents a state change where the values a a a and b b b are swapped. There is no interference between the updates in a parallel update. If there are multiple updates to the same program variable in one parallel update, only the last one affects the state.
The mutable update c ∗ → a + b c \mathrel{\ast\to} a + b c ∗ → a + b stands for the state change where the variable borrowed by c c c is set to a + b a + b a + b . The value of c c c itself is unchanged.
Example 6.3. Let x : i n t x : \mathrm{int} x : int , y : i n t y : \mathrm{int} y : int , z : R e f i n t m z : \mathrm{Ref}^{m}_{\mathrm{int}} z : Ref int m be program variables. Then the formula
{ x : = y ∥ y : = x ∥ z ∗ → x + 1 } ⟨ x = y + ∗ z ; ⟩ ( x = 2 ⋅ x + 1 ) \{x := y \mathbin{\|} y := x \mathbin{\|} z \mathrel{\ast\to} x + 1\}\langle x = y + \mathord{\ast}z;\rangle(x = 2 \cdot x + 1) { x := y ∥ y := x ∥ z ∗ → x + 1 } ⟨ x = y + ∗ z ; ⟩ ( x = 2 ⋅ x + 1 ) expresses that, in a state where the values of x x x and y y y are swapped and the value referenced by z z z is updated to x x x plus one, the program fragment “x = y + *z;” terminates and afterwards the formula x = 2 ⋅ x + 1 x = 2 \cdot x + 1 x = 2 ⋅ x + 1 holds. The formula is valid if the addition in the program does not overflow.
Mutating updates allow our logic to represent mutable references. Just as in the semantics, mutable references need to contain the borrowed memory location, i.e., the borrowed place. We introduce the sort P l a c e \mathrm{Place} Place for places. For each program variable x x x , its place is represented with the unique constant ⟦ x ⟧ : P l a c e \llbracket x \rrbracket: \mathrm{Place} [ [ x ] ] : Place . With the function m r e f \mathit{mref} mref , mutable references of a place p p p can be represented as a term m r e f ( p ) \mathit{mref}(p) mref ( p ) . If p p p is a place of a lender of sort A A A , then m r e f ( p ) \mathit{mref}(p) mref ( p ) is of sort R e f A m \mathrm{Ref}^{m}_{A} Ref A m .
Formally, we define the semantics of RustyDL using a Kripke structure consisting of states, i.e., first-order models, and a program transition function ρ \rho ρ .
Definition 6.4 (RustyDL Kripke structure). Let r r r be a valid Rust program. A RustyDL Kripke structure for a signature derived from r r r is a tuple
K = ( D , I , S , ρ ) \mathcal{K} = (\mathcal{D}, I, \mathcal{S}, \rho) K = ( D , I , S , ρ ) where
D \mathcal{D} D is the logic’s domain, i.e., a non-empty set. This domain can be split into different sub-domains per sort, e.g., D i n t \mathcal{D}^{\mathrm{int}} D int for integers.
I I I is the interpretation of functions and predicates.
S S S is an infinite set of states s : V a r → D s : \mathit{Var} \to\mathcal{D} s : Var → D .
ρ \rho ρ is a function mapping a program fragment p p p to a partial function S ⇀ S S \rightharpoonup S S ⇀ S with ρ ( p ) ( s 1 ) = s 2 \rho(p)(s_1) = s_2 ρ ( p ) ( s 1 ) = s 2 iff evaluating p p p in state s 1 s_1 s 1 terminates normally in s 2 s_2 s 2 . If p p p does not terminate normally starting in s 1 s_1 s 1 , then ρ ( p ) ( s 1 ) ↑ \rho(p)(s_1)\uparrow ρ ( p ) ( s 1 ) ↑ . We assume Rust programs to be deterministic, which is true of the subset considered in § 3 .
Whereas [21 ] left ρ \rho ρ unspecified due to a lack of a formal semantics, we can now use the LAGC semantics to close this gap. For this, we must clarify the relation between LAGC states σ \sigma σ and RustyDL states s s s . We assume a relation ∼ \sim ∼ between RustyDL states and LAGC states (see Definition F.3 for a formal definition), pronounced “corresponds to,” in the expected manner: integer, boolean, and compound values map to each other in the expected way; references, however, have no borrow identifier on the RustyDL side, hence we obtain a many-to-one mapping.
Proposition 6.5. Let K = ( D , I , S , ρ ) \mathcal{K} = (\mathcal{D}, I, S, \rho) K = ( D , I , S , ρ ) be RustyDL Kripke structure and s ∈ S s \in S s ∈ S a RustyDL state. There exist an infinite number of LAGC states σ \sigma σ such that s ∼ σ s \sim\sigma s ∼ σ . All states in the set { σ ∣ s ∼ σ } \{\sigma\mid s \sim\sigma\} { σ ∣ s ∼ σ } differ only in the borrow identifiers in shared and mutable references.
With this relation, we proceed to define ρ \rho ρ in terms of the generated traces. A panic does not count as normal termination and is therefore excluded from the program transition in Definition 6.6 .
Definition 6.6 (Program Transition Function). Let r r r be a valid Rust program, K = ( D , I , S , ρ ) \mathcal{K} = (\mathcal{D}, I, S, \rho) K = ( D , I , S , ρ ) a Kripke structure for r r r , p p p a program fragment for r r r , s ∈ S s \in S s ∈ S a RustyDL state, and σ \sigma σ an LAGC state such that s ∼ σ s \sim\sigma s ∼ σ . Then the program transition function ρ \rho ρ is defined as
ρ ( p ) ( s ) = { s ′ if ∃ σ ′ . s ′ ∼ σ ′ ∧ ∃ s h . ( s h ⇝ σ ′ ) ∈ Tr ( e , σ ) ∧ lastEv ( s h ) ≠ panicEv ( ) ↑ otherwise \rho(p)(s) =
\begin{cases}
s' & \text{if } \exists\sigma'.\ s' \sim\sigma' \land\exists sh.\ (sh \leadsto\sigma') \in\operatorname{Tr}(e,\sigma) \land\operatorname{lastEv}(sh) \ne\operatorname{panicEv}() \\
\uparrow& \text{otherwise}
\end{cases} ρ ( p ) ( s ) = { s ′ ↑ if ∃ σ ′ . s ′ ∼ σ ′ ∧ ∃ s h . ( s h ⇝ σ ′ ) ∈ Tr ( e , σ ) ∧ lastEv ( s h ) = panicEv ( ) otherwise where e : = { l e t x ; p } e := \{\mathop{\mathrm{let}}\ x;\ p\} e := { let x ; p } and x : = fpv ( p ) x := \operatorname{fpv}(p) x := fpv ( p ) are the free program variables in p p p . In the following, we write K ( p ) \mathcal{K}(p) K ( p ) and Tr ( p , σ ) \operatorname{Tr}(p,\sigma) Tr ( p , σ ) for K ( e ) \mathcal{K}(e) K ( e ) and Tr ( e , σ ) \operatorname{Tr}(e,\sigma) Tr ( e , σ ) , respectively.
By Lemma 3.23, σ ′ \sigma' σ ′ is uniquely identified. Our definition for s ∼ σ s \sim\sigma s ∼ σ ensures that s s s is uniquely identified as well.
We must also fix the semantics of updates, as they are a crucial part of the calculus rules.
Definition 6.7 (Semantics of RustyDL updates). Let C r t Crt C r t be a crate, K = ( D , I , S , ρ ) \mathcal{K} = (\mathcal{D}, I, S, \rho) K = ( D , I , S , ρ ) a Kripke structure for C r t Crt C r t , s , s ′ ∈ S s,s' \in S s , s ′ ∈ S a state, a : A , b : B ∈ V a r a : A, b : B \in\mathit{Var} a : A , b : B ∈ Var program variables, β : V S y m → D \beta: \mathit{VSym} \to\mathcal{D} β : VSym → D a variable assignment, u 1 , u 2 ∈ U p d u_1,u_2 \in\mathit{Upd} u 1 , u 2 ∈ Upd , t , t 2 ∈ D L T r m A t,t_2 \in\mathit{DLTrm}_A t , t 2 ∈ DLTrm A , and t 1 ∈ D L T r m R e f B m t_1 \in\mathit{DLTrm}_{\mathit{Ref}_B^m} t 1 ∈ DLTrm Ref B m . The valuation function val K , s , β : U p d → ( S → S ) \operatorname{val}_{\mathcal{K},s,\beta} : \mathit{Upd} \to(S \to S) val K , s , β : Upd → ( S → S ) is defined as:
val K , s , β ( a : = t ) ( s ′ ) ( b ) = { val K , s , β ( t ) if b = a s ′ ( b ) otherwise \operatorname{val}_{\mathcal{K},s,\beta}(a := t)(s')(b) =
\begin{cases}
\operatorname{val}_{\mathcal{K},s,\beta}(t) & \text{if } b = a \\
s'(b) & \text{otherwise}
\end{cases} val K , s , β ( a := t ) ( s ′ ) ( b ) = { val K , s , β ( t ) s ′ ( b ) if b = a otherwise val K , s , β ( t 1 ∗ → t 2 ) ( s ′ ) ( b ) = { val K , s , β ( t 2 ) if val K , s , β ( t 1 ) = val K , s , β ( mref ( ⟦ b ⟧ ) ) s ′ ( b ) otherwise \operatorname{val}_{\mathcal{K},s,\beta}(t_1 \mathbin{*{\to}} t_2)(s')(b) =
\begin{cases}
\operatorname{val}_{\mathcal{K},s,\beta}(t_2) & \text{if } \operatorname{val}_{\mathcal{K},s,\beta}(t_1) = \operatorname{val}_{\mathcal{K},s,\beta}(\operatorname{mref}(\llbracket b \rrbracket)) \\
s'(b) & \text{otherwise}
\end{cases} val K , s , β ( t 1 ∗ → t 2 ) ( s ′ ) ( b ) = { val K , s , β ( t 2 ) s ′ ( b ) if val K , s , β ( t 1 ) = val K , s , β ( mref ( [ [ b ] ] )) otherwise val K , s , β ( s k i p ) ( s ′ ) = s ′ \operatorname{val}_{\mathcal{K},s,\beta}(\mathop{\mathrm{skip}})(s') = s' val K , s , β ( skip ) ( s ′ ) = s ′ val K , s , β ( u 1 ∥ u 2 ) ( s ′ ) = val K , s , β ( u 2 ) ( val K , s , β ( u 1 ) ( s ′ ) ) \operatorname{val}_{\mathcal{K},s,\beta}(u_1 \mathbin{\|} u_2)(s') = \operatorname{val}_{\mathcal{K},s,\beta}(u_2)(\operatorname{val}_{\mathcal{K},s,\beta}(u_1)(s')) val K , s , β ( u 1 ∥ u 2 ) ( s ′ ) = val K , s , β ( u 2 ) ( val K , s , β ( u 1 ) ( s ′ )) val K , s , β ( { u 1 } u 2 ) = val K , s ′ , β ( u 2 ) where s ′ = val K , s , β ( u 1 ) ( s ) \operatorname{val}_{\mathcal{K},s,\beta}(\{u_1\}u_2) = \operatorname{val}_{\mathcal{K},s',\beta}(u_2)\ \text{where } s' = \operatorname{val}_{\mathcal{K},s,\beta}(u_1)(s) val K , s , β ({ u 1 } u 2 ) = val K , s ′ , β ( u 2 ) where s ′ = val K , s , β ( u 1 ) ( s ) With updates and program transition defined, the semantics of RustyDL terms and formulas is now definable.
Definition 6.8 (Semantics of RustyDL Terms and Formulas ). Let Crt be a valid Rust crate, VSym the set of logic variable symbols, K = ( D , I , S , ρ ) K = (D, \mathcal{I}, S, \rho) K = ( D , I , S , ρ ) a Kripke structure modeling Rust programs, s ∈ S s \in S s ∈ S a state, and β : V S y m → D \beta: \mathrm{VSym} \to D β : VSym → D a variable assignment.
For every RustyDL term t t t we define its evaluation inductively by the evaluation function val K , s , β \operatorname{val}_{K,s,\beta} val K , s , β exactly as in first-order logic, with evaluation of program variables defined as val K , s , β ( a ) = s ( a ) \operatorname{val}_{K,s,\beta}(a) = s(a) val K , s , β ( a ) = s ( a ) for each program variable a a a and val K , s , β ( { u } t ) = val K , s ′ , β ( t ) \operatorname{val}_{K,s,\beta}(\{u\}t) = \operatorname{val}_{K,s',\beta}(t) val K , s , β ({ u } t ) = val K , s ′ , β ( t ) where s ′ = val K , s , β ( u ) ( s ) s' = \operatorname{val}_{K,s,\beta}(u)(s) s ′ = val K , s , β ( u ) ( s ) .
For every RustyDL formula ϕ \phi ϕ we define satisfaction, denoted ( K , s , β ) ⊨ ϕ (K,s,\beta) \models\phi ( K , s , β ) ⊨ ϕ , exactly as in first-order logic with three new cases:
( K , s , β ) ⊨ [ p ] ϕ (K,s,\beta) \models[p]\phi ( K , s , β ) ⊨ [ p ] ϕ iff ρ ( p ) ( s ) ↑ \rho(p)(s)\mathord{\uparrow} ρ ( p ) ( s ) ↑ or ρ ( p ) ( s ) = s ′ \rho(p)(s) = s' ρ ( p ) ( s ) = s ′ and ( K , s ′ , β ) ⊨ ϕ (K,s',\beta) \models\phi ( K , s ′ , β ) ⊨ ϕ ,
( K , s , β ) ⊨ ⟨ p ⟩ ϕ (K,s,\beta) \models\langle p\rangle\phi ( K , s , β ) ⊨ ⟨ p ⟩ ϕ iff ρ ( p ) ( s ) = s ′ \rho(p)(s) = s' ρ ( p ) ( s ) = s ′ exists and ( K , s ′ , β ) ⊨ ϕ (K,s',\beta) \models\phi ( K , s ′ , β ) ⊨ ϕ .
( K , s , β ) ⊨ { u } ϕ (K,s,\beta) \models\{u\}\phi ( K , s , β ) ⊨ { u } ϕ iff ( K , s ′ , β ) ⊨ ϕ (K,s',\beta) \models\phi ( K , s ′ , β ) ⊨ ϕ where s ′ = val K , s , β ( u ) ( s ) s' = \operatorname{val}_{K,s,\beta}(u)(s) s ′ = val K , s , β ( u ) ( s ) .
Calculus Rules
To show the validity of RustyDL formulas, we rely on a sequent calculus over our logic, a standard approach for verification and theorem proving.
Beyond the familiar rules for propositional and first-order logic, as well as rules for reasoning about, e.g., integer and boolean values, the calculus also contains a large number of symbolic execution rules . These allow the unfolding and decomposition of complex expressions and statements into simpler ones until the entire program has been executed and only first-order formulas remain.
To represent the symbolic execution calculus in a compact and understandable manner, a further extension of the Rust syntax is required. In program fragments in rules—and only there—we can use π \pi π and ω \omega ω to represent the inactive prefix and the rest of the program , respectively. The inactive prefix contains any opening braces and similar structures that represent the context; ω \omega ω stands for the rest of the program, including closing braces. Hence, the schematic program fragment π x = 1 ω \pi\ x = 1\ \omega π x = 1 ω matches any program fragment where x = 1 x = 1 x = 1 is the first active expression , e.g., { { x = 1 ; ( ) } x = 2 } \{\{x = 1; ()\} x = 2\} {{ x = 1 ; ( )} x = 2 } . 6
We present these rules in a schematic form with schema variables for different parts of the sequent. In the rule ifElseSplit below, Γ \Gamma Γ , Δ \Delta Δ stand for sets of DL formulas, ϕ \phi ϕ for a DL formula, { U } \{U\} { U } stands for any potential update application (it may also be empty; no update is necessary for the rule to be applicable), s e se se stands for a “simple expression,” i.e., a program variable or literal, { b e 0 } \{be_0\} { b e 0 } , { b e 1 } \{be_1\} { b e 1 } stand for block expressions, and π \pi π and ω \omega ω stand for the inactive program prefix and the program rest, respectively.
Γ , { U } ( s e = = T R U E ) ⟹ { U } ⟨ π { b e 0 } ω ⟩ ϕ , Δ Γ , { U } ( s e = = F A L S E ) ⟹ { U } ⟨ π { b e 1 } ω ⟩ ϕ , Δ ifElseSplit Γ ⟹ { U } ⟨ π if s e { b e 0 } else { b e 1 } ω ⟩ ϕ , Δ \begin{aligned}
&\Gamma, \{U\}(se \mathrel{==} \mathrm{TRUE}) \Longrightarrow\{U\}\langle\pi\ \{be_0\}\ \omega\rangle\phi,\Delta\\
&\Gamma, \{U\}(se \mathrel{==} \mathrm{FALSE}) \Longrightarrow\{U\}\langle\pi\ \{be_1\}\ \omega\rangle\phi,\Delta\\
&\hspace{3em}\text{ifElseSplit}\qquad
\Gamma\Longrightarrow\{U\}\langle\pi\ \textbf{if } se \ \{be_0\}\ \textbf{else }\{be_1\}\ \omega\rangle\phi,\Delta
\end{aligned} Γ , { U } ( se == TRUE ) ⟹ { U } ⟨ π { b e 0 } ω ⟩ ϕ , Δ Γ , { U } ( se == FALSE ) ⟹ { U } ⟨ π { b e 1 } ω ⟩ ϕ , Δ ifElseSplit Γ ⟹ { U } ⟨ π if se { b e 0 } else { b e 1 } ω ⟩ ϕ , Δ
The rule above splits the sequent when an if expression with a side-effect-free condition is encountered into two branches: one where the condition is true and one where it is false.
Crucial rules of our calculus simplify the program in modalities and extract singular effects into updates. For instance, the following rule copy transforms an assignment, where the right-hand side is a simple expression, into an update and leaves behind a unit expression.
Γ ⟹ { U } ⟨ x : = s e ⟩ ⟨ π ( ) ω ⟩ ϕ , Δ s e is a literal or implements Copy copy Γ ⟹ { U } ⟨ π x = s e ω ⟩ ϕ , Δ \begin{aligned}
&\Gamma\Longrightarrow\{U\}\langle x := se\rangle\langle\pi\ ()\ \omega\rangle\phi,\Delta\qquad se\text{ is a literal or implements }\text{Copy} \\
&\hspace{8em}\text{copy}\qquad\Gamma\Longrightarrow\{U\}\langle\pi\ x = se\ \omega\rangle\phi,\Delta
\end{aligned} Γ ⟹ { U } ⟨ x := se ⟩ ⟨ π ( ) ω ⟩ ϕ , Δ se is a literal or implements Copy copy Γ ⟹ { U } ⟨ π x = se ω ⟩ ϕ , Δ
For details on RustyDL and more calculus rules, we refer to [21 , 22 ] .
Showing Soundness
Following the standard definitions of the sequent calculus, we define the soundness of RustyDL rules as follows:
Definition 6.9 (Soundness of Rules). A RustyDL rule with n n n premisses of form Γ i ⟹ Δ i \Gamma_i \mathrel{\Longrightarrow} \Delta_i Γ i ⟹ Δ i (1 ≤ i ≤ n 1 \le i \le n 1 ≤ i ≤ n ) and conclusion Γ ⟹ Δ \Gamma\mathrel{\Longrightarrow} \Delta Γ ⟹ Δ is sound iff ⊨ Γ ⟹ Δ \models\Gamma\mathrel{\Longrightarrow} \Delta ⊨ Γ ⟹ Δ under the assumption that ⊨ Γ i ⟹ Δ i \models\Gamma_i \mathrel{\Longrightarrow} \Delta_i ⊨ Γ i ⟹ Δ i for all 1 ≤ i ≤ n 1 \le i \le n 1 ≤ i ≤ n .
From Definition 6.8 , we conclude that only the final state of a trace is relevant for the RustyDL semantics. Hence, when proving soundness of a rule with a modality, it suffices to show that two sets of traces generated by two modalities agree on some suffix. For this purpose, Definition 6.10 defines the suffix relations of trace sets and Lemma 6.11 relates this to validity in RustyDL.
Definition 6.10 (Trace Suffix). Let T 1 , T 2 T_1, T_2 T 1 , T 2 be sets of traces. We call T 1 T_1 T 1 the suffix set of T 2 T_2 T 2 , denoted as T 1 ≻ T 2 T_1 \succ T_2 T 1 ≻ T 2 , iff there is a finite shining trace s h sh s h such that T 2 = { s h ∗ ∗ s h ′ ∣ s h ′ ∈ T 1 } T_2 = \{sh ** sh' \mid sh' \in T_1\} T 2 = { s h ∗ ∗ s h ′ ∣ s h ′ ∈ T 1 } . We call s h sh s h the prefix .
Lemma 6.11. Let K \mathcal{K} K be a Rust Kripke structure, β \beta β a variable assignment, s 1 , s 2 s_1, s_2 s 1 , s 2 RustyDL states, σ 1 , σ 2 \sigma_1, \sigma_2 σ 1 , σ 2 LAGC states with s 1 ∼ σ 1 s_1 \sim\sigma_1 s 1 ∼ σ 1 and s 2 ∼ σ 2 s_2 \sim\sigma_2 s 2 ∼ σ 2 , ϕ \phi ϕ a RustyDL formula, and p 1 = π e 1 ω p_1 = \pi\ e_1\ \omega p 1 = π e 1 ω , p 2 = π e 2 ω p_2 = \pi\ e_2\ \omega p 2 = π e 2 ω two program fragments. If ( K , s 2 , β ) ⊨ ⟨ p 2 ⟩ ϕ (\mathcal{K}, s_2, \beta) \models\langle p_2 \rangle\phi ( K , s 2 , β ) ⊨ ⟨ p 2 ⟩ ϕ and Tr ( p 2 , σ 2 ) ≻ Tr ( p 1 , σ 1 ) \operatorname{Tr}(p_2, \sigma_2) \succ\operatorname{Tr}(p_1, \sigma_1) Tr ( p 2 , σ 2 ) ≻ Tr ( p 1 , σ 1 ) , then ( K , s 1 , β ) ⊨ ⟨ p 1 ⟩ ϕ (\mathcal{K}, s_1, \beta) \models\langle p_1 \rangle\phi ( K , s 1 , β ) ⊨ ⟨ p 1 ⟩ ϕ .
Proof. We assume ( K , s 2 , β ) ⊨ ⟨ p 2 ⟩ ϕ (\mathcal{K}, s_2, \beta) \models\langle p_2 \rangle\phi ( K , s 2 , β ) ⊨ ⟨ p 2 ⟩ ϕ . By Definitions 6.6 and 6.8 , we know that there is an LAGC state σ \sigma σ and a state s s s such that at least one trace in Tr ( p 2 , σ 2 ) \operatorname{Tr}(p_2, \sigma_2) Tr ( p 2 , σ 2 ) ends in σ \sigma σ and s ∼ σ s \sim\sigma s ∼ σ such that ( K , s , β ) ⊨ ϕ (\mathcal{K}, s, \beta) \models\phi ( K , s , β ) ⊨ ϕ . By Lemma 3.23 , all traces in Tr ( p 2 , σ 2 ) \operatorname{Tr}(p_2, \sigma_2) Tr ( p 2 , σ 2 ) end in σ \sigma σ .
Since Tr ( p 2 , σ 2 ) ≻ Tr ( p 1 , σ 1 ) \operatorname{Tr}(p_2, \sigma_2) \succ\operatorname{Tr}(p_1, \sigma_1) Tr ( p 2 , σ 2 ) ≻ Tr ( p 1 , σ 1 ) , we know that all traces in Tr ( p 1 , σ 1 ) \operatorname{Tr}(p_1, \sigma_1) Tr ( p 1 , σ 1 ) end in σ \sigma σ . Thus, Definition 6.8 gives us ( K , s 1 , β ) ⊨ ⟨ p 1 ⟩ ϕ (\mathcal{K}, s_1, \beta) \models\langle p_1 \rangle\phi ( K , s 1 , β ) ⊨ ⟨ p 1 ⟩ ϕ . □ \square □
We can now begin proving our calculus rules sound.
Theorem 6.12 (Soundness of rule copy). Let Γ , Δ ∈ 2 D L F m l \Gamma, \Delta\in2^{\mathrm{DLFml}} Γ , Δ ∈ 2 DLFml be sets of formulas, U ∈ U p d \mathcal{U} \in\mathrm{Upd} U ∈ Upd be an update, x x x be a program variable whose type is Copy, s e se se be a variable or a literal expression, π x = s e ω \pi\ x = se\ \omega π x = se ω be a program fragment, and ϕ ∈ D L F m l \phi\in\mathrm{DLFml} ϕ ∈ DLFml be a formula. If
⊨ Γ ⟹ { U } { x : = s e } ⟨ π ( ) ω ⟩ ϕ , Δ \models\Gamma\mathrel{\Longrightarrow} \{\mathcal{U}\}\{x := se\}\langle\pi\ ()\ \omega\rangle\phi, \Delta ⊨ Γ ⟹ { U } { x := se } ⟨ π ( ) ω ⟩ ϕ , Δ then the following holds:
⊨ Γ ⟹ { U } ⟨ π x = s e ω ⟩ ϕ , Δ \models\Gamma\mathrel{\Longrightarrow} \{\mathcal{U}\}\langle\pi\ x = se\ \omega\rangle\phi, \Delta ⊨ Γ ⟹ { U } ⟨ π x = se ω ⟩ ϕ , Δ
Proof. We assume ⊨ Γ ⟹ { U } { x : = s e } ⟨ π ( ) ω ⟩ ϕ , Δ \models\Gamma\mathrel{\Longrightarrow} \{\mathcal{U}\}\{x := se\}\langle\pi\ ()\ \omega\rangle\phi, \Delta ⊨ Γ ⟹ { U } { x := se } ⟨ π ( ) ω ⟩ ϕ , Δ . It is sufficient to show
( K , s 1 , β ) ⊨ ⟨ π x = s e ω ⟩ ϕ (\mathcal{K}, s_1, \beta) \models\langle\pi\ x = se\ \omega\rangle\phi ( K , s 1 , β ) ⊨ ⟨ π x = se ω ⟩ ϕ
for all K , s , β \mathcal{K}, s, \beta K , s , β with s 1 = val K , s , β ( U ) ( s ) s_1 = \operatorname{val}_{\mathcal{K},s,\beta}(\mathcal{U})(s) s 1 = val K , s , β ( U ) ( s ) . We infer from the assumption that
( K , s 2 , β ) ⊨ ⟨ π ( ) ω ⟩ ϕ (84) (\mathcal{K}, s_2, \beta) \models\langle\pi\ ()\ \omega\rangle\phi \tag*{(84)} ( K , s 2 , β ) ⊨ ⟨ π ( ) ω ⟩ ϕ (84)
with s 2 = val K , s 1 , β ( x : = s e ) ( s 1 ) s_2 = \operatorname{val}_{\mathcal{K},s_1,\beta}(x := se)(s_1) s 2 = val K , s 1 , β ( x := se ) ( s 1 ) .
Let σ 1 , σ 2 \sigma_1, \sigma_2 σ 1 , σ 2 be LAGC states such that s 1 ∼ σ 1 s_1 \sim\sigma_1 s 1 ∼ σ 1 and s 2 ∼ σ 2 s_2 \sim\sigma_2 s 2 ∼ σ 2 .
We know that x = s e x = se x = se is the active expression. By the construction of the local evaluation rules, we know that it is also the next (sub-)expression to be evaluated and added to the trace s h sh s h using rule (50). Thus we can use the local evaluation. We split over the construction of s e se se . Since it is a simple expression, it is either a value or a variable.
Case 6.1 (s e ∈ V a l se \in\mathit{Val} se ∈ Val ). Then val σ 1 ( x = s e ) = { ( σ 1 ) ⌢ σ 1 [ x ↦ s e ] , K ( ( ) ) } \operatorname{val}_{\sigma_1}(x = se) = \{(\sigma_1) \mathbin{\frown} \sigma_1[x \mapsto se], \mathcal{K}(() )\} val σ 1 ( x = se ) = {( σ 1 ) ⌢ σ 1 [ x ↦ se ] , K (( ))} . Hence the only possible composition is, ⟨ σ 1 , K ( π x = s e ω ) ⟩ → ⟨ σ 1 ⌢ σ 1 [ x ↦ s e ] , K ( π ( ) ω ) ⟩ \langle\sigma_1, \mathcal{K}(\pi\ x = se\ \omega)\rangle\to\langle\sigma_1 \mathbin{\frown} \sigma_1[x \mapsto se], \mathcal{K}(\pi\ ()\ \omega)\rangle ⟨ σ 1 , K ( π x = se ω )⟩ → ⟨ σ 1 ⌢ σ 1 [ x ↦ se ] , K ( π ( ) ω )⟩ . As s 2 = val K , s 1 , β ( x : = s e ) ( s 1 ) s_2 = \operatorname{val}_{\mathcal{K},s_1,\beta}(x := se)(s_1) s 2 = val K , s 1 , β ( x := se ) ( s 1 ) , we know σ 2 = σ 1 [ x ↦ s e ] \sigma_2 = \sigma_1[x \mapsto se] σ 2 = σ 1 [ x ↦ se ] .
Thus, Tr ( π x = s e ω , σ 1 ) = { ⟨ σ 1 ⟩ ⋅ s h ∣ s h ∈ Tr ( π ( ) ω , σ 2 ) } \operatorname{Tr}(\pi\ x = se\ \omega, \sigma_1) = \{\langle\sigma_1\rangle\cdot sh \mid sh \in\operatorname{Tr}(\pi\ ()\ \omega, \sigma_2)\} Tr ( π x = se ω , σ 1 ) = {⟨ σ 1 ⟩ ⋅ s h ∣ s h ∈ Tr ( π ( ) ω , σ 2 )} . Clearly, then, Tr ( π ( ) ω , σ 2 ) ⪯ Tr ( π x = s e ω , σ 1 ) \operatorname{Tr}(\pi\ ()\ \omega, \sigma_2) \preceq\operatorname{Tr}(\pi\ x = se\ \omega, \sigma_1) Tr ( π ( ) ω , σ 2 ) ⪯ Tr ( π x = se ω , σ 1 ) . By Lemma 6.11 and assumption (84), we have ( K , s 1 , β ) ⊨ ⟨ π x = s e ω ⟩ ϕ (\mathcal{K}, s_1, \beta) \models\langle\pi\ x = se\ \omega\rangle\phi ( K , s 1 , β ) ⊨ ⟨ π x = se ω ⟩ ϕ .
Case 6.2 (s e ∈ V a r se \in Var se ∈ V a r ). Let v = s 1 ( s e ) v = s_1(se) v = s 1 ( se ) . Since s e se se is not symbolic (our semantics only generates concrete traces with concrete states), v ∈ V a l v \in Val v ∈ V a l . Then val σ 1 ( s e ) = { ⟨ σ 1 ⟩ ⋅ K ( v ) } \operatorname{val}_{\sigma_1}(se) = \{\langle\sigma_1\rangle\cdot K(v)\} val σ 1 ( se ) = {⟨ σ 1 ⟩ ⋅ K ( v )} and val σ 1 ( x = s e ) = { τ ⋅ K ( x = e ) ∣ τ ⋅ K ( e ) ∈ val σ 1 ( s e ) } = { ⟨ σ 1 ⟩ ⋅ K ( x = v ) } \operatorname{val}_{\sigma_1}(x = se) = \{\tau\cdot K(x = e) \mid\tau\cdot K(e) \in\operatorname{val}_{\sigma_1}(se)\} = \{\langle\sigma_1\rangle\cdot K(x = v)\} val σ 1 ( x = se ) = { τ ⋅ K ( x = e ) ∣ τ ⋅ K ( e ) ∈ val σ 1 ( se )} = {⟨ σ 1 ⟩ ⋅ K ( x = v )} . Further, we have val σ 1 ( x = v ) = { ⟨ σ 1 ⟩ ↷ σ 1 [ x ↦ v ] ⋅ K ( ( ) ) } \operatorname{val}_{\sigma_1}(x = v) = \{\langle\sigma_1\rangle\curvearrowright\sigma_1[x \mapsto v] \cdot K(())\} val σ 1 ( x = v ) = {⟨ σ 1 ⟩ ↷ σ 1 [ x ↦ v ] ⋅ K (( ))} . Hence, the only possible composition is
⟨ σ 1 ⟩ , K ( π x = s e ω ) → ⟨ σ 1 ⟩ , K ( π x = v ω ) → ⟨ σ 1 ⟩ ↷ σ 1 [ x ↦ v ] , K ( π ( ) ω ) \langle\sigma_1\rangle, K(\pi\ x = se\ \omega) \to\langle\sigma_1\rangle, K(\pi\ x = v\ \omega) \to\langle\sigma_1\rangle\curvearrowright\sigma_1[x \mapsto v], K(\pi\ ()\ \omega) ⟨ σ 1 ⟩ , K ( π x = se ω ) → ⟨ σ 1 ⟩ , K ( π x = v ω ) → ⟨ σ 1 ⟩ ↷ σ 1 [ x ↦ v ] , K ( π ( ) ω )
Since s 2 = val K , s 1 , β ( x : = s e ) ( s 1 ) s_2 = \operatorname{val}_{\mathcal{K},s_1,\beta}(x := se)(s_1) s 2 = val K , s 1 , β ( x := se ) ( s 1 ) , we know that σ 2 = σ 1 [ x ↦ v ] \sigma_2 = \sigma_1[x \mapsto v] σ 2 = σ 1 [ x ↦ v ] . Thus, we have Tr ( π x = s e ω , σ 1 ) = { ⟨ σ 1 ⟩ ↷ σ 2 ⋅ s h ∣ s h ∈ Tr ( π ( ) ω , σ 2 ) } \operatorname{Tr}(\pi\ x = se\ \omega, \sigma_1) = \{\langle\sigma_1\rangle\curvearrowright\sigma_2 \cdot sh \mid sh \in\operatorname{Tr}(\pi\ ()\ \omega, \sigma_2)\} Tr ( π x = se ω , σ 1 ) = {⟨ σ 1 ⟩ ↷ σ 2 ⋅ s h ∣ s h ∈ Tr ( π ( ) ω , σ 2 )} which, with Definition 6.6 , assumption (84), and the argument in case 6.1, gives us ( K , s 1 , β ) ⊨ ⟨ π x = s e ω ⟩ ϕ (\mathcal{K}, s_1, \beta) \models\langle\pi\ x = se\ \omega\rangle\phi ( K , s 1 , β ) ⊨ ⟨ π x = se ω ⟩ ϕ . □
The modularity of LAGC thus permits relatively concise and schematic soundness proofs. This ease carries over even to more complex rules, as both the semantics and calculus are on a similar level of modularity and there is, thus, a suitable correlation. We present proofs for additional rules encountered in [21 ] in Appendix § 16 . The rules presented and proven here and in Appendix § 16 present some of the core and most important rules of our calculus. Already, the rules support references and loops. Given the schematic nature of the proofs, proving the remaining calculus rules presents time consuming but straightforward work. These results lead us to postulate Theorem 6.13 with considerable confidence.
Theorem 6.13 (Soundness of RustyDL calculus). The calculus of RustyDL is sound.
Thus, RustyDL is, to our knowledge, the first DL calculus for a real-world programming language, where “[t]he most important property of the [. . .] calculus” [1 ] has been proven.
Related Work
For related work, we first consider other semantics and formalizations of parts of Rust and compare them to LAGC in approach and scope. Then, we consider other applications of the LAGC approach and how they differ from our Rust semantics. Finally, we comment on different LAGC-like applications to deductive verification.
Rust Semantics
Multiple semantics for substantial parts of Rust have been defined in recent years, all with different scopes, focal points, and drawbacks. We present a broad selection here.
In contrast to the approaches discussed below, our semantics targets the high-level HIR representation and abstracts away from memory representations of the data and other low-level concerns indistinguishable for a Rust programmer. Most existing work targets the lower-level MIR; the differences between these two layers are substantial [56 ] , resulting in a significant semantic gap between many existing formalizations and source code level Rust. As a consequence, we also leave other concerns such as type checking and borrow checking aside. Rather than formalizing the borrow checker properties in our semantics—which would be possible—we take Rust’s guarantees for granted and model the operational behavior only. For similar reasons, we only consider safe Rust with its stronger guarantees, whereas other approaches also cover unsafe Rust. Lastly, our semantics is the only one to date with support for async Rust. Hence, a model of a runtime as in § 5 and fairness proofs are not possible in other semantics. Table 1 offers a comprehensive overview of the existing semantics.
Table 1. Comparison of Rust semantics. We denote a feature being supported by •, unsupported by –, and partially supported (e.g., by translation to sum types or lambda functions) by ∘. “BC” stands for the borrow checker being modeled; “Src” stands for source code level.
Jung et al. [38 , 39 ] introduced RustBelt and set out to prove soundness of Rust’s safety guarantees and to formalize the conditions under which a Rust library is a “safe extension” of the language. To accomplish this, the authors define a semantic model of a subset of Rust called λ \lambda λ Rust and implement this model using Iris [40 ] . This approach has been extended to also consider properties other than safety [48 ] . Given their scope, λ \lambda λ Rust is more closely aligned with Rust’s MIR. Furthermore, Jung et al. are concerned with the type system’s semantics, i.e., how types are modeled and inhabited, while we take a more abstract view, focused on the operational semantics of expressions. RustBelt supports unsafe Rust and all relevant data types. As λ \lambda λ Rust is a version of the λ \lambda λ -calculus, the semantics is based on translation. Enums, e.g., are modeled via sum types.
The MiniRust project [36 ] provides a specification for a MIR-like language with the goal of modeling the core of Rust. Though unofficial, it covers a crucial subset of the MIR and serves as a more formal FLS, i.e., a formal specification of Rust at a lower level. Its formalization of references and data types is more explicit than ours in that MiniRust, covering also unsafe Rust, tracks aliases. MiniRust’s big-step operational semantics (BOS) is not presented in a mathematical style like an LAGC semantics but a programmatic style. It supports all data types and expressions, though loops are not explicitly modeled at this level, as they have been translated to basic blocks.
Similar to MiniRust but more comprehensive is the a-mir-formality project [13 ] , which is part of the Rust foundation’s efforts. It also aims to cover the type and borrow checker and the trait system. As it is currently under development, we cannot offer a more detailed comparison at this point.
Ho et al. [28 , 29 ] present a semantics for a Rust subset called low-level borrow calculus (LLBC) in the context of the verification tool Aeneas [30 ] . Their semantics is based on translation to multiple, successively lower-level languages and can serve as model of Rust’s borrow checker. It supports all relevant types and expressions, though as translations to other languages.
Pearce [52 ] offers an SOS-style semantics for a Rust subset, featherweight Rust (FR). This semantics also covers references, ownership, and a version of borrow checking. Of the compound types, only tuples are explicitly covered; others can be translated to suitable representations. Loops are discussed and an approach outlined, but no rules are provided.
There are formal models of Rust ownership and reference system designed for particular perspectives. Jung et al. [37 ] present stacked borrows, a memory model of borrowing in terms of stacks, which has been extended to more permissive tree borrows by Villani et al. [66 ] . Even more encompassing are the formalisations of Rust’s upcoming borrow checker Polonius [14 , 45 , 46 , 47 , 54 ] . As we, at present, offer no model of borrowing, these works can be seen as orthogonal: their semantics can be incorporated into our semantics by using events and the wf predicate. The work of Crichton et al. [9 ] is also relevant, as they provide a conceptual model of ownership and borrowing along with a concise formalization of Polonius.
LAGC Semantics
The LAGC approach has been applied to a number of programming languages. In the original LAGC paper, Din et al. [17 ] defined semantics for a simple While language, for a ProMeLa-like language [33 ] , and for a subset of the asynchronous modeling languages ABS [35 ] and similar active object languages [10 ] . The latter has been expanded by Tarifa to a semantics for Real-Time ABS [62 ] . Other examples include Timed Rebeca by Hähnle et al. [25 ] and the smart contract modeling language SmartML by Reinhold [55 ] . The breadth of these languages demonstrate the flexibility of LAGC and especially its usability for concurrency.
Nevertheless, all prior applications of the LAGC approach were chiefly to small subsets of languages or modeling languages; real-world programming languages were not approached. One result of this limited scope is that expressions had so far been assumed to be side-effect free and pure. We provide the first LAGC semantics that evaluates expressions, not just statements. In fact, this leads to a slight simplification of the approach: path conditions, a central element of symbolic traces in prior semantics, can be dropped entirely.
Another avenue for LAGC research is the fairness of schedulers for programs with cooperative scheduling. Hähnle and Henrio [24 ] formalized a deterministic and quiescent fair scheduler for a language inspired by ABS [35 ] in LAGC and proved its fairness. Our work builds on their approach and adapts it to the Tokio scheduler.
Deductive Verification based on LAGC
Applying LAGC to devise a sound program logic was a core design goal of Din et al. [17 ] and part of their work. However, their logic was limited first by their target languages being modeling languages, as discussed above, and second by the fact that their logics lack implementation. Bubel et al. [8 ] extend this trace-based deductive verification approach by devising a logic and calculus for a more sophisticated setting. It has since been further refined by Hähnle et al. [26 ] and Scaletta [59 ] . An implementation of this calculus has since been developed, though only the sequential parts are supported as yet [58 ] .
Our work is set apart from the existing LAGC verification approaches, since we did not design a program logic and calculus based on a semantics or based on traces, but used a trace-based semantics to validate the soundness of an existing calculus [21 ] . We demonstrate that even when a calculus is independent of an LAGC semantics, soundness proofs follow quite readily from the semantics due to its modular nature. We find that LAGC with its local evaluation rules fits naturally onto a DL calculus, even for complex languages such as Rust.
Somewhat related is the work of Beckert and Bruns [4 ] , where they develop a DL, called dynamic trace logic , over traces and provide a sequent formulas. The approach differs in that Beckert and Bruns are confined to sequential programs and consider only traces over states without events. Their goal was a connection between DL and linear temporal logic (LTL) .
Future Work and Conclusion
Future Work
The original goal of applying LAGC to Rust in order to obtain a semantics with which our dynamic logic calculus can be proven sound has been achieved in § 6 . An avenue for future work is to extend these soundness proofs for the remaining calculus rules, as we present and discuss only the core rules here—though the remaining rules can be proven straightforwardly following the schematic approach demonstrated here. Similarly, we plan to consider completeness of the calculus and demonstrate where incompleteness is still encountered.
As future work, we would like to obtain a mechanization of our complete semantics, the Tokio formalization, and the DL proofs in a proof assistant. § 2 has largely been mechanized in Lean [11 ] , see Drodt [20 ] . Mechanization of § 3 is in progress. We intend to extend this to our other contributions.
Another direction for future work is to provide an LAGC semantics for other parts of Rust not adequately modeled in the present article. For instance, a semantics of MIR might be developed, which more directly expresses Rust’s ownership and reference semantics. Having a semantics both for the HIR and MIR level would permit defining a notion of correct translation from HIR to MIR, against which concrete implementations can be checked. Though we believe it possible even on our chosen level of abstraction, references and borrow checking are more easily modeled on the MIR level. We may also explore integrating the Polonius borrow-checking formalization into LAGC. A formalization of Rust’s reference and ownership properties in terms of well-formedness of traces is not part of our work due to constraints of time and space.
A final avenue for research is to extend the model of Tokio’s current thread runtime to its multi-threaded counterpart and prove its fairness. While modeling the runtime is a manageable task, the fairness proof requires additional proof methods or a more complex proof. Further, extending the formalization to include I/O events presents an interesting challenge. While Din et al. [17 ] consider rudimentary user input, modeling I/O in LAGC has not been attempted at a larger scale.
Conclusion
Any formal semantics of a real-world programming language is necessarily complex and technically challenging, which is exacerbated by considering concurrency and scheduling choices. Rust introduces the additional challenge of multiple language layers (HIR and MIR) and sub-languages (unsafe Rust). Hence, any formalization and semantics of Rust contains abstraction and differing focal points. We choose to present a relatively abstract, high-level semantics both in terms of language layer and representation of states. While properties such as memory layout or type safety are not expressible on this level, we can reason effectively about HIR Rust and asynchronous programs.
We demonstrate the expressive power of the LAGC approach by formalizing the Tokio runtime and proving two notions of fairness for our model. Additionally, we show how soundness of a program logic calculus can be derived from our semantics. Both of these endeavors would be much more complex, if not unfeasible, in a SOS-style semantics or a semantics for Rust’s lower layers. Thereby, we show that the seemingly high upfront complexity of the LAGC approach is offset by the gain in its modular approach.
Our semantics also demonstrate the LAGC approach’s applicability to real-world programming languages, bridging the gap from the academic languages to which it had been confined. The approach is sufficiently flexible and powerful to accurately model a language like Rust which, with its more functional syntax, lays outside the purview of LAGC’s original scope. In fact, we were able to simplify the approach, dropping path conditions entirely, and complete the model with relative ease. It proved to be a natural match for modeling Rust’s async features and runtimes.
Thus, we offer a formal semantics for Rust with many different applications and use cases, as demonstrated above.
Acknowledgments
This work is supported by the German Research Foundation under Grants No. BU 2924/3-1 and HA 2617/9-1.
We would like to thank the following people for their careful and incisive feedback: Reiner Hähnle, Richard Bubel, Marco Scaletta, Lukas Grätz, and Niklas Heidler. We also thank Thanh Long Nguyen for his work on the Lean mechanization.
Additional Definitions of the next Function
First, we show the expressions which have no sub-expression under any circumstances. Those are values, program variables, continue, loop, and panic expressions. For them, the entire expression is considered the sub-expression and the hole expression is the hole ∙ \bullet ∙ .
n e x t ( v , M ) = ( v , ∙ ) \mathit{next}(v,M)=(v,\bullet) next ( v , M ) = ( v , ∙ )
n e x t ( x , M ) = ( x , ∙ ) \mathit{next}(x,M)=(x,\bullet) next ( x , M ) = ( x , ∙ )
n e x t ( continue ’l , M ) = ( continue ’l , ∙ ) \mathit{next}(\text{continue 'l},M)=(\text{continue 'l},\bullet) next ( continue ’l , M ) = ( continue ’l , ∙ )
n e x t ( ’l loop e , M ) = ( ’l loop e , ∙ ) \mathit{next}(\text{'l loop }e,M)=(\text{'l loop }e,\bullet) next ( ’l loop e , M ) = ( ’l loop e , ∙ )
n e x t ( panic! , M ) = ( panic! , ∙ ) \mathit{next}(\text{panic!},M)=(\text{panic!},\bullet) next ( panic! , M ) = ( panic! , ∙ )
Dereferencing expressions have, potentially, one sub-expression, which is always evaluated to a value. Similar definitions holds for match, break, return, and array repeat expressions.
n e x t ( ∗ e , M ) = { ( ∗ e , ∙ ) if e ∈ V a l ( e ′ , ∗ h e ) if e ∉ V a l ∧ ( e ′ , h e ) = n e x t ( e , V a l ) \mathit{next}(*e,M)=
\begin{cases}
(*e,\bullet) & \text{if }e\in Val\\
(e',*he) & \text{if }e\notin Val\land(e',he)=\mathit{next}(e,Val)
\end{cases} next ( ∗ e , M ) = { ( ∗ e , ∙ ) ( e ′ , ∗ h e ) if e ∈ V a l if e ∈ / V a l ∧ ( e ′ , h e ) = next ( e , V a l )
n e x t ( match e { a r m ‾ } , M ) = { ( match e { a r m ‾ } , ∙ ) if e ∈ V a l ( e ′ , match h e { a r m ‾ } ) if e ∉ V a l ∧ ( e ′ , h e ) = n e x t ( e , V a l ) \mathit{next}(\text{match }e\{\overline{arm}\},M)=
\begin{cases}
(\text{match }e\{\overline{arm}\},\bullet) & \text{if }e\in Val\\
(e',\text{match }he\{\overline{arm}\}) & \text{if }e\notin Val\land(e',he)=\mathit{next}(e,Val)
\end{cases} next ( match e { a r m } , M ) = { ( match e { a r m } , ∙ ) ( e ′ , match h e { a r m }) if e ∈ V a l if e ∈ / V a l ∧ ( e ′ , h e ) = next ( e , V a l )
n e x t ( break ’l e , M ) = { ( break ’l e , ∙ ) if e ∈ V a l ( e ′ , break ’l h e ) if e ∉ V a l ∧ ( e ′ , h e ) = n e x t ( e , V a l ) \mathit{next}(\text{break 'l }e,M)=
\begin{cases}
(\text{break 'l }e,\bullet) & \text{if }e\in Val\\
(e',\text{break 'l }he) & \text{if }e\notin Val\land(e',he)=\mathit{next}(e,Val)
\end{cases} next ( break ’l e , M ) = { ( break ’l e , ∙ ) ( e ′ , break ’l h e ) if e ∈ V a l if e ∈ / V a l ∧ ( e ′ , h e ) = next ( e , V a l )
n e x t ( return e , M ) = { ( return e , ∙ ) if e ∈ V a l ( e ′ , return h e ) if e ∉ V a l ∧ ( e ′ , h e ) = n e x t ( e , V a l ) \mathit{next}(\text{return }e,M)=
\begin{cases}
(\text{return }e,\bullet) & \text{if }e\in Val\\
(e',\text{return }he) & \text{if }e\notin Val\land(e',he)=\mathit{next}(e,Val)
\end{cases} next ( return e , M ) = { ( return e , ∙ ) ( e ′ , return h e ) if e ∈ V a l if e ∈ / V a l ∧ ( e ′ , h e ) = next ( e , V a l )
n e x t ( [ e ; n ] , M ) = { ( [ e ; n ] , ∙ ) if e ∈ V a l ( e ′ , [ h e ; n ] ) if e ∉ V a l ∧ ( e ′ , h e ) = n e x t ( e , V a l ) \mathit{next}([e;n],M)=
\begin{cases}
([e;n],\bullet) & \text{if }e\in Val\\
(e',[he;n]) & \text{if }e\notin Val\land(e',he)=\mathit{next}(e,Val)
\end{cases} next ([ e ; n ] , M ) = { ([ e ; n ] , ∙ ) ( e ′ , [ h e ; n ]) if e ∈ V a l if e ∈ / V a l ∧ ( e ′ , h e ) = next ( e , V a l )
Block expressions must be considered depending on their first statement. If it is a let statement, there is no next sub-expression. If it is an expression, but not a break expression with a value or continue expression, it behaves as above. We exclude break and continues here to fit the expected rule shape expected in § 3.5.1 . If there are no statements, it is exactly as in the cases above, as there is only one possible sub-expression.
n e x t ( { let p a t = e 1 ; s ‾ e 2 } , = ) ( { let p a t = e 1 ; s ‾ e 2 } , ∙ ) \mathit{next}(\{\text{let }pat=e_1;\ \overline{s}\ e_2\},=)(\{\text{let }pat=e_1;\ \overline{s}\ e_2\},\bullet) next ({ let p a t = e 1 ; s e 2 } , = ) ({ let p a t = e 1 ; s e 2 } , ∙ )
n e x t ( { e 1 ; s ‾ e 2 } , = ) = { ( { e 1 ; s ‾ e 2 } , ∙ ) if e 1 ∈ V a l ∨ e 1 = break ’l v ∨ e 1 = continue ’l ( e 1 ′ , { h e ; s ‾ e 2 } ) otherwise, where ( e 1 ′ , h e ) = n e x t ( e 1 , V a l ) \mathit{next}(\{e_1;\ \overline{s}\ e_2\},=)=
\begin{cases}
(\{e_1;\ \overline{s}\ e_2\},\bullet) & \text{if }e_1\in Val\lor e_1=\text{break 'l }v\lor e_1=\text{continue 'l}\\
(e_1',\{he;\ \overline{s}\ e_2\}) & \text{otherwise, where }(e_1',he)=\mathit{next}(e_1,Val)
\end{cases} next ({ e 1 ; s e 2 } , = ) = { ({ e 1 ; s e 2 } , ∙ ) ( e 1 ′ , { h e ; s e 2 }) if e 1 ∈ V a l ∨ e 1 = break ’l v ∨ e 1 = continue ’l otherwise, where ( e 1 ′ , h e ) = next ( e 1 , V a l )
n e x t ( { e } , M ) = { ( { e } , ∙ ) if e ∈ V a l ( e ′ , { h e } ) if e ∉ V a l ∧ ( e ′ , h e ) = n e x t ( e , V a l ) \mathit{next}(\{e\},M)=
\begin{cases}
(\{e\},\bullet) & \text{if }e\in Val\\
(e',\{he\}) & \text{if }e\notin Val\land(e',he)=\mathit{next}(e,Val)
\end{cases} next ({ e } , M ) = { ({ e } , ∙ ) ( e ′ , { h e }) if e ∈ V a l if e ∈ / V a l ∧ ( e ′ , h e ) = next ( e , V a l )
Operator expressions have two potential sub-expressions, which are all evaluated to values. We only show the case for addition; other operators are analogous.
n e x t ( e 1 + e 2 , M ) = { ( e 1 + e 2 , ∙ ) if e 1 , e 2 ∈ V a l ( e 1 ′ , h e + e 2 ) if e 1 ∉ V a l ∧ ( e 1 ′ , h e ) = n e x t ( e 1 , V a l ) ( e 2 ′ , e 1 + h e ) if e 1 ∈ V a l ∧ e 2 ∉ V a l ∧ ( e 2 ′ , h e ) = n e x t ( e 2 , V a l ) \mathit{next}(e_1+e_2,M)=
\begin{cases}
(e_1+e_2,\bullet) & \text{if }e_1,e_2\in Val\\
(e_1',he+e_2) & \text{if }e_1\notin Val\land(e_1',he)=\mathit{next}(e_1,Val)\\
(e_2',e_1+he) & \text{if }e_1\in Val\land e_2\notin Val\land(e_2',he)=\mathit{next}(e_2,Val)
\end{cases} next ( e 1 + e 2 , M ) = ⎩ ⎨ ⎧ ( e 1 + e 2 , ∙ ) ( e 1 ′ , h e + e 2 ) ( e 2 ′ , e 1 + h e ) if e 1 , e 2 ∈ V a l if e 1 ∈ / V a l ∧ ( e 1 ′ , h e ) = next ( e 1 , V a l ) if e 1 ∈ V a l ∧ e 2 ∈ / V a l ∧ ( e 2 ′ , h e ) = next ( e 2 , V a l )
Assignment operators behave differently. When e 1 e_1 e 1 , e 2 e_2 e 2 are of integer types, the sub-expressions are evaluated right-to-left; otherwise left-to-right. We always evaluate e 2 e_2 e 2 to a value, but e 1 e_1 e 1 to a place. Here, we show the integer version.
next ( e 1 + = e 2 , M ) = { ( e 1 + = e 2 , ∙ ) if e 1 ∈ Place ∧ e 2 ∈ Val ( e 2 ′ , e 1 + = h e ) if e 2 ∉ Val ∧ ( e 2 ′ , h e ) = next ( e 2 , Val ) ( e 1 ′ , h e + = e 2 ) if e 2 ∈ Val ∧ e 1 ∉ Place ∧ ( e 1 ′ , h e ) = next ( e 1 , Place ) \operatorname{next}(e_1 += e_2, M) =
\begin{cases}
(e_1 += e_2, \bullet) & \text{if } e_1 \in\operatorname{Place} \land e_2 \in\operatorname{Val} \\
(e'_2, e_1 += h e) & \text{if } e_2 \notin\operatorname{Val} \land(e'_2, h e) = \operatorname{next}(e_2, \operatorname{Val}) \\
(e'_1, h e += e_2) & \text{if } e_2 \in\operatorname{Val} \land e_1 \notin\operatorname{Place} \land(e'_1, h e) = \operatorname{next}(e_1, \operatorname{Place})
\end{cases} next ( e 1 + = e 2 , M ) = ⎩ ⎨ ⎧ ( e 1 + = e 2 , ∙ ) ( e 2 ′ , e 1 + = h e ) ( e 1 ′ , h e + = e 2 ) if e 1 ∈ Place ∧ e 2 ∈ Val if e 2 ∈ / Val ∧ ( e 2 ′ , h e ) = next ( e 2 , Val ) if e 2 ∈ Val ∧ e 1 ∈ / Place ∧ ( e 1 ′ , h e ) = next ( e 1 , Place )
Arrays and tuples behave identically: Evaluation is left-to-right and we might be in an assignee or value context. Struct expressions x { i d e n t 1 : e 1 , … , i d e n t n : e n } x\{\mathit{ident}_1 : e_1, \ldots, \mathit{ident}_n : e_n\} x { ident 1 : e 1 , … , ident n : e n } are analogous to arrays and tuples.
next ( [ e 1 , … , e n ] , M ) = { ( [ e 1 , … , e n ] , ∙ ) if e 1 , … , e n ∈ M ( e k ′ , [ e 1 , … , e k − 1 , h e , e k + 1 , … , e n ] ) if e 1 , … , e k − 1 ∈ M ∧ e k ∉ M ∧ ( e k ′ , h e ) = next ( e k , M ) \operatorname{next}([e_1,\ldots,e_n], M) =
\begin{cases}
([e_1,\ldots,e_n], \bullet) & \text{if } e_1,\ldots,e_n \in M \\
(e'_k, [e_1,\ldots,e_{k-1},h e,e_{k+1},\ldots,e_n]) & \text{if } e_1,\ldots,e_{k-1} \in M \land e_k \notin M \\
& \quad\land(e'_k,h e) = \operatorname{next}(e_k,M)
\end{cases} next ([ e 1 , … , e n ] , M ) = ⎩ ⎨ ⎧ ([ e 1 , … , e n ] , ∙ ) ( e k ′ , [ e 1 , … , e k − 1 , h e , e k + 1 , … , e n ]) if e 1 , … , e n ∈ M if e 1 , … , e k − 1 ∈ M ∧ e k ∈ / M ∧ ( e k ′ , h e ) = next ( e k , M )
next ( ( e 1 , … , e n ) , M ) = { ( ( e 1 , … , e n ) , ∙ ) if e 1 , … , e n ∈ M ( e k ′ , ( e 1 , … , e k − 1 , h e , e k + 1 , … , e n ) ) if e 1 , … , e k − 1 ∈ M ∧ e k ∉ M ∧ ( e k ′ , h e ) = next ( e k , M ) \operatorname{next}((e_1,\ldots,e_n), M) =
\begin{cases}
((e_1,\ldots,e_n), \bullet) & \text{if } e_1,\ldots,e_n \in M \\
(e'_k,(e_1,\ldots,e_{k-1},h e,e_{k+1},\ldots,e_n)) & \text{if } e_1,\ldots,e_{k-1} \in M \land e_k \notin M \\
& \quad\land(e'_k,h e) = \operatorname{next}(e_k,M)
\end{cases} next (( e 1 , … , e n ) , M ) = ⎩ ⎨ ⎧ (( e 1 , … , e n ) , ∙ ) ( e k ′ , ( e 1 , … , e k − 1 , h e , e k + 1 , … , e n )) if e 1 , … , e n ∈ M if e 1 , … , e k − 1 ∈ M ∧ e k ∈ / M ∧ ( e k ′ , h e ) = next ( e k , M )
Function calls behave like tuples and array but with an additional expression, the callee, which is evaluated first. Method calls e . i d e n t ( e 1 , … , e n ) e.\mathit{ident}(e_1,\ldots,e_n) e . ident ( e 1 , … , e n ) analogous to function calls and are omitted.
next ( e ( e 1 , … , e n ) , M ) = { ( e ( e 1 , … , e n ) , ∙ ) if e , e 1 , … , e n ∈ Val ( e ′ , h e ( e 1 , … , e n ) ) if e ∉ Val ∧ ( e ′ , h e ) = next ( e , Val ) ( e k ′ , e ( e 1 , … , e k − 1 , h e , e k + 1 , … , e n ) ) if e , e 1 , … , e k − 1 ∈ Val ∧ e k ∉ Val ∧ ( e k ′ , h e ) = next ( e k , Val ) \operatorname{next}(e(e_1,\ldots,e_n), M) =
\begin{cases}
(e(e_1,\ldots,e_n), \bullet) & \text{if } e,e_1,\ldots,e_n \in\operatorname{Val} \\
(e',h e(e_1,\ldots,e_n)) & \text{if } e \notin\operatorname{Val} \land(e',h e)=\operatorname{next}(e,\operatorname{Val}) \\
(e'_k,e(e_1,\ldots,e_{k-1},h e,e_{k+1},\ldots,e_n)) & \text{if } e,e_1,\ldots,e_{k-1} \in\operatorname{Val} \land e_k \notin\operatorname{Val} \\
& \quad\land(e'_k,h e)=\operatorname{next}(e_k,\operatorname{Val})
\end{cases} next ( e ( e 1 , … , e n ) , M ) = ⎩ ⎨ ⎧ ( e ( e 1 , … , e n ) , ∙ ) ( e ′ , h e ( e 1 , … , e n )) ( e k ′ , e ( e 1 , … , e k − 1 , h e , e k + 1 , … , e n )) if e , e 1 , … , e n ∈ Val if e ∈ / Val ∧ ( e ′ , h e ) = next ( e , Val ) if e , e 1 , … , e k − 1 ∈ Val ∧ e k ∈ / Val ∧ ( e k ′ , h e ) = next ( e k , Val )
Field access expressions depend on the context, as they may evaluate their single sub-expression to an assignee value or a value.
next ( e . i d e n t , M ) = { ( e . i d e n t , ∙ ) if e ∈ M ( e ′ , h e . i d e n t ) if e ∉ M ∧ ( e ′ , h e ) = next ( e , M ) \operatorname{next}(e.\mathit{ident}, M) =
\begin{cases}
(e.\mathit{ident}, \bullet) & \text{if } e \in M \\
(e',h e.\mathit{ident}) & \text{if } e \notin M \land(e',h e)=\operatorname{next}(e,M)
\end{cases} next ( e . ident , M ) = { ( e . ident , ∙ ) ( e ′ , h e . ident ) if e ∈ M if e ∈ / M ∧ ( e ′ , h e ) = next ( e , M )
Reference expressions always evaluate their sub-expression to a place.
next ( & e , M ) = { ( & e , ∙ ) if e ∈ Place ( e ′ , & h e ) if e ∉ Place ∧ ( e ′ , h e ) = next ( e , Place ) \operatorname{next}(\&e, M) =
\begin{cases}
(\&e,\bullet) & \text{if } e \in\operatorname{Place} \\
(e',\&h e) & \text{if } e \notin\operatorname{Place} \land(e',h e)=\operatorname{next}(e,\operatorname{Place})
\end{cases} next ( & e , M ) = { ( & e , ∙ ) ( e ′ , & h e ) if e ∈ Place if e ∈ / Place ∧ ( e ′ , h e ) = next ( e , Place )
next ( & mut e , M ) = { ( & mut e , ∙ ) if e ∈ Place ( e ′ , & mut h e ) if e ∉ Place ∧ ( e ′ , h e ) = next ( e , Place ) \operatorname{next}(\&\operatorname{mut} e, M) =
\begin{cases}
(\&\operatorname{mut} e,\bullet) & \text{if } e \in\operatorname{Place} \\
(e',\&\operatorname{mut} h e) & \text{if } e \notin\operatorname{Place} \land(e',h e)=\operatorname{next}(e,\operatorname{Place})
\end{cases} next ( & mut e , M ) = { ( & mut e , ∙ ) ( e ′ , & mut h e ) if e ∈ Place if e ∈ / Place ∧ ( e ′ , h e ) = next ( e , Place )
Axiomatization of Queue
We repeat and adapt the axiomatization for queues found in [24 ] .
Definition B.1 (Queue). We define a queue of type T T T q q q as queues over elements of type T T T . The operations are:
queue() — creates an empty queue
empty ( q ) (q) ( q ) — is true iff q q q is the empty queue
q ⊕ k q \mathbin{\oplus} k q ⊕ k — returns a queue q ′ q' q ′ that is q q q extended with k k k as its last element
pop ( q ) (q) ( q ) — returns a pair ( q ′ , k ) (q', k) ( q ′ , k ) , where k k k is the first element in q q q and q ′ q' q ′ the remaining queue after k k k is removed; the operation is undefined iff q q q is empty
The operations are defined by the following axioms:
empty ( queue ( ) ) (85) \operatorname{empty}(\operatorname{queue}()) \tag*{(85)} empty ( queue ( )) (85) pop ( queue ( ) ) ↑ (86) \operatorname{pop}(\operatorname{queue}()) \mathord{\uparrow} \tag*{(86)} pop ( queue ( )) ↑ (86) ∀ q , k . ¬ empty ( q ⊕ k ) (87) \forall q,k.\ \neg\operatorname{empty}(q \mathbin{\oplus} k) \tag*{(87)} ∀ q , k . ¬ empty ( q ⊕ k ) (87) ∀ q , k , q ′ , k ′ . pop ( q ⊕ k ) = { ( queue ( ) , k ) if empty ( q ) , ( q ′ ⊕ k ) otherwise, where ( q ′ , k ′ ) = pop ( q ) (88) \begin{aligned}
\forall q,k,q',k'.\ \operatorname{pop}(q \mathbin{\oplus} k)
&=
\begin{cases}
(\operatorname{queue}(),k) & \text{if }\operatorname{empty}(q),\\
(q' \mathbin{\oplus} k) & \text{otherwise, where }(q',k')=\operatorname{pop}(q)
\end{cases} \tag*{(88)}
\end{aligned} ∀ q , k , q ′ , k ′ . pop ( q ⊕ k ) = { ( queue ( ) , k ) ( q ′ ⊕ k ) if empty ( q ) , otherwise, where ( q ′ , k ′ ) = pop ( q ) (88) If q q q is a queue of stacks of continuation markers, we also define the set of continuation markers in q q q , K s ( q ) \operatorname{K}_{\mathrm{s}}(q) K s ( q ) , as:
K s ( q ) = { ∅ if empty ( q ) , k ⊎ K s ( q ′ ) where pop ( q ) = ( q ′ , k ˉ ) \operatorname{K}_{\mathrm{s}}(q)=
\begin{cases}
\varnothing& \text{if }\operatorname{empty}(q),\\
k \mathbin{\uplus}\operatorname{K}_{\mathrm{s}}(q') & \text{where }\operatorname{pop}(q)=(q',\bar{k})
\end{cases} K s ( q ) = { ∅ k ⊎ K s ( q ′ ) if empty ( q ) , where pop ( q ) = ( q ′ , k ˉ )
Additional Current Thread Composition Rules
∄ f , h . lastEv ( s h ) = spawnEv ( f , h ) t ≠ 0 ( m o d I G ) empty ( q ) pop ( q G ) = ( q G ′ , ( foo , f ) ) σ = last ( s h ) fn foo ( x ˉ ) e ∈ F n τ ⋅ K ( e ′ ) ∈ val σ f ( fn foo ( x ˉ ) e ) ρ concretizes τ wf ( s h ∗ ∗ ρ ( τ ) ) s h , ⟨ Idle , t , q G , q ⟩ → c t s h , ⟨ [ K f ( e ′ ) ] , t + 1 , q G ′ , q ⟩ (89) \begin{aligned}
&\nexists f,h.\ \operatorname{lastEv}(sh)=\operatorname{spawnEv}(f,h)\qquad t\ne0\pmod{I_G}\\
&\operatorname{empty}(q)\qquad\operatorname{pop}(q_G)=(q'_G,(\operatorname{foo},f))\qquad\sigma=\operatorname{last}(sh)\qquad\operatorname{fn\ foo}(\bar{x})\ e\in Fn\\
&\tau\cdot K(e')\in\operatorname{val}^{f}_{\sigma}(\operatorname{fn\ foo}(\bar{x})\ e)\qquad\rho\ \operatorname{concretizes}\ \tau\qquad\operatorname{wf}(sh\mathbin{**}\rho(\tau))\\
&sh,\langle\operatorname{Idle},t,q_G,q\rangle\mathrel{\to_{\mathrm{ct}}}sh,\left\langle[K^{f}(e')],t+1,q'_G,q\right\rangle \tag*{(89)}
\end{aligned} ∄ f , h . lastEv ( s h ) = spawnEv ( f , h ) t = 0 ( mod I G ) empty ( q ) pop ( q G ) = ( q G ′ , ( foo , f )) σ = last ( s h ) fn foo ( x ˉ ) e ∈ F n τ ⋅ K ( e ′ ) ∈ val σ f ( fn foo ( x ˉ ) e ) ρ concretizes τ wf ( s h ∗ ∗ ρ ( τ )) s h , ⟨ Idle , t , q G , q ⟩ → ct s h , ⟨ [ K f ( e ′ )] , t + 1 , q G ′ , q ⟩ (89)
∄ f , h . lastEv ( s h ) = spawnEv ( f , h ) t ≠ 0 ( m o d I G ) pop ( q ) = ( q ′ , ( foo , f ) ) σ = last ( s h ) fn foo ( x ˉ ) e ∈ F n τ ⋅ K ( e ′ ) ∈ val σ f ( fn foo ( x ˉ ) e ) ρ concretizes τ wf ( s h ∗ ∗ ρ ( τ ) ) s h , ⟨ Idle , t , q G , q ⟩ → c t s h , ⟨ [ K f ( e ′ ) ] , t + 1 , q G , q ′ ⟩ (90) \begin{aligned}
&\nexists f,h.\ \operatorname{lastEv}(sh)=\operatorname{spawnEv}(f,h)\qquad t\ne0\pmod{I_G}\\
&\operatorname{pop}(q)=(q',(\operatorname{foo},f))\qquad\sigma=\operatorname{last}(sh)\qquad\operatorname{fn\ foo}(\bar{x})\ e\in Fn\\
&\tau\cdot K(e')\in\operatorname{val}^{f}_{\sigma}(\operatorname{fn\ foo}(\bar{x})\ e)\qquad\rho\ \operatorname{concretizes}\ \tau\qquad\operatorname{wf}(sh\mathbin{**}\rho(\tau))\\
&sh,\langle\operatorname{Idle},t,q_G,q\rangle\mathrel{\to_{\mathrm{ct}}}sh,\left\langle[K^{f}(e')],t+1,q_G,q'\right\rangle \tag*{(90)}
\end{aligned} ∄ f , h . lastEv ( s h ) = spawnEv ( f , h ) t = 0 ( mod I G ) pop ( q ) = ( q ′ , ( foo , f )) σ = last ( s h ) fn foo ( x ˉ ) e ∈ F n τ ⋅ K ( e ′ ) ∈ val σ f ( fn foo ( x ˉ ) e ) ρ concretizes τ wf ( s h ∗ ∗ ρ ( τ )) s h , ⟨ Idle , t , q G , q ⟩ → ct s h , ⟨ [ K f ( e ′ )] , t + 1 , q G , q ′ ⟩ (90)
Quiescent Fairness of the Current Thread Scheduler
We first define a translation function “trans” mapping current thread configurations to configurations in the semantics of § 4 . Traces and the scheduler’s state can be mapped independently to traces and sets of tasks, respectively. For this, we lift trans to both:
trans ( s h , ⟨ a c t i v e , t , q G , q ⟩ ) = trans ( s h ) , trans ( ⟨ a c t i v e , t , q G , q ⟩ ) \operatorname{trans}(sh,\langle active,t,q_G,q\rangle)=\operatorname{trans}(sh),\operatorname{trans}(\langle active,t,q_G,q\rangle) trans ( s h , ⟨ a c t i v e , t , q G , q ⟩) = trans ( s h ) , trans (⟨ a c t i v e , t , q G , q ⟩)
Traces are “translated” via a projection, by removing the schedule events and the preceding state (as rule (73) repeats this state).
trans ( ε ) = ε \operatorname{trans}(\varepsilon)=\varepsilon trans ( ε ) = ε
trans ( s h ↷ σ ↷ schedEv ( foo , f ) ) = trans ( s h ) \operatorname{trans}(sh\mathbin{\curvearrowright}\sigma\mathbin{\curvearrowright}\operatorname{schedEv}(\operatorname{foo},f))=\operatorname{trans}(sh) trans ( s h ↷ σ ↷ schedEv ( foo , f )) = trans ( s h )
trans ( s h ↷ t ) = trans ( s h ) ↷ t when t ≠ schedEv ( foo , f ) \operatorname{trans}(sh\mathbin{\curvearrowright}t)=\operatorname{trans}(sh)\mathbin{\curvearrowright}t
\quad\text{when }t\ne\operatorname{schedEv}(\operatorname{foo},f) trans ( s h ↷ t ) = trans ( s h ) ↷ t when t = schedEv ( foo , f )
Next, we define how queues of continuation stacks and task markers are mapped to sets of these stacks. The inductive definition is obvious; note that the third case drops task markers.
trans 0 ( q ) = { ∅ if empty ( q ) { [ [ k ] ] } ⊎ trans 0 ( q ′ ) if pop ( q ) = ( q ′ , [ [ k ] ] ) trans 0 ( q ′ ) otherwise, with pop ( q ) = ( q ′ , k ) \operatorname{trans}^{0}(q)=
\begin{cases}
\varnothing& \text{if empty }(q)\\
\left\{\left[\mkern-2mu\left[k\right]\mkern-2mu\right]\right\}\mathbin{\mathbin{\uplus}}\operatorname{trans}^{0}(q') & \text{if }\operatorname{pop}(q)=\left(q',\left[\mkern-2mu\left[k\right]\mkern-2mu\right]\right)\\
\operatorname{trans}^{0}(q') & \text{otherwise, with }\operatorname{pop}(q)=\left(q',k\right)
\end{cases} trans 0 ( q ) = ⎩ ⎨ ⎧ ∅ { [ [ k ] ] } ⊎ trans 0 ( q ′ ) trans 0 ( q ′ ) if empty ( q ) if pop ( q ) = ( q ′ , [ [ k ] ] ) otherwise, with pop ( q ) = ( q ′ , k )
Finally, we translate the scheduler state by combining the global and local queue with the active task:
trans ( ⟨ a c t , t , q G , q ⟩ ) = ( a c t , trans 0 ( q G ) ⊎ trans 0 ( q ) ) \operatorname{trans}(\langle act,t,q_G,q\rangle)=\left(act,\operatorname{trans}^{0}(q_G)\mathbin{\mathbin{\uplus}}\operatorname{trans}^{0}(q)\right) trans (⟨ a c t , t , q G , q ⟩) = ( a c t , trans 0 ( q G ) ⊎ trans 0 ( q ) )
Next, we define a measure of the scheduling distance. We use this in our fairness proof, showing that at every step, an enabled expression is either executed or its distance decreases. Similar to a termination witness in formal verification [1 ] , this distance does not need to be an accurate representation of how many “steps” it takes until an expression is executed. It suffices to be a finitely descending chain of values.
Definition D.1 (Scheduling Distance: Current Thread). Let the configuration C = s h , ⟨ a c t , t , q G , q ⟩ C=sh,\langle act,t,q_G,q\rangle C = s h , ⟨ a c t , t , q G , q ⟩ and c ∈ Z c\in\mathbb{Z} c ∈ Z . We define the scheduling distance d d d of a continuation marker K f ( e ) K^{f}(e) K f ( e ) in C C C as follows:
d ( K f ( e ) , C , c ) = ( pos ( K f ( e ) , C ) , idle ( C ) , unsched ( s h ) , c ) d(K^{f}(e),C,c)=\left(\operatorname{pos}(K^{f}(e),C),\operatorname{idle}(C),\operatorname{unsched}(sh),c\right) d ( K f ( e ) , C , c ) = ( pos ( K f ( e ) , C ) , idle ( C ) , unsched ( s h ) , c ) where pos defines the position of the continuation in the scheduler as:
pos ( K f ( e ) , s h , ⟨ a c t , t , q G , q ⟩ ) = { 1 if a c t = [ k 1 , … , k n ] ∧ K f ( e ) = k 1 ∧ ¬ suspended ( e , s h ) ∣ Ks ( q ) ∣ + ∣ Ks ( q G ) ∣ + 2 if a c t = [ k 1 , … , k n ] ∧ K f ( e ) = k 1 ∧ suspended ( e , s h ) i + ∣ Ks ( q ) ∣ + ∣ Ks ( q G ) ∣ + 2 if a c t = [ k 1 , … , k n ] ∧ K f ( e ) = k i ∧ 1 < i ≤ n 1 + qPos ( K f ( e ) , q ) + ∣ Ks ( q G ) ∣ if a c t ≠ I d l e ∧ K f ( e ) ∈ Ks ( q ) 1 + ∣ Ks ( q ) ∣ + qPos ( K f ( e ) , q G ) if a c t ≠ I d l e ∧ K f ( e ) ∈ Ks ( q G ) qPos ( K f ( e ) , q ) + ∣ Ks ( q G ) ∣ if a c t = I d l e ∧ K f ( e ) ∈ Ks ( q ) ∣ Ks ( q ) ∣ + qPos ( K f ( e ) , q G ) if a c t = I d l e ∧ K f ( e ) ∈ Ks ( q G ) undef if K f ( e ) ∉ Ks ( trans ( ⟨ [ k 1 , … , k n ] , t , q G , q ⟩ ) ) \operatorname{pos}(K^{f}(e),sh,\langle act,t,q_G,q\rangle)=
\begin{cases}
1 & \text{if }act=[k_1,\ldots,k_n]\mathbin{\land}K^{f}(e)=k_1\\
& \mathbin{\land}\neg\operatorname{suspended}(e,sh)\\
\lvert\operatorname{Ks}(q)\rvert+\lvert\operatorname{Ks}(q_G)\rvert+2 & \text{if }act=[k_1,\ldots,k_n]\mathbin{\land}K^{f}(e)=k_1\\
& \mathbin{\land}\operatorname{suspended}(e,sh)\\
i+\lvert\operatorname{Ks}(q)\rvert+\lvert\operatorname{Ks}(q_G)\rvert+2 & \text{if }act=[k_1,\ldots,k_n]\mathbin{\land}K^{f}(e)=k_i\mathbin{\land}1<i\leq n\\
1+\operatorname{qPos}(K^{f}(e),q)+\lvert\operatorname{Ks}(q_G)\rvert& \text{if }act\ne Idle\mathbin{\land}K^{f}(e)\in\operatorname{Ks}(q)\\
1+\lvert\operatorname{Ks}(q)\rvert+\operatorname{qPos}(K^{f}(e),q_G) & \text{if }act\ne Idle\mathbin{\land}K^{f}(e)\in\operatorname{Ks}(q_G)\\
\operatorname{qPos}(K^{f}(e),q)+\lvert\operatorname{Ks}(q_G)\rvert& \text{if }act=Idle\mathbin{\land}K^{f}(e)\in\operatorname{Ks}(q)\\
\lvert\operatorname{Ks}(q)\rvert+\operatorname{qPos}(K^{f}(e),q_G) & \text{if }act=Idle\mathbin{\land}K^{f}(e)\in\operatorname{Ks}(q_G)\\
\textit{undef} & \text{if }K^{f}(e)\notin\operatorname{Ks}\left(\operatorname{trans}(\langle[k_1,\ldots,k_n],t,q_G,q\rangle)\right)
\end{cases} pos ( K f ( e ) , s h , ⟨ a c t , t , q G , q ⟩) = ⎩ ⎨ ⎧ 1 ∣ Ks ( q )∣ + ∣ Ks ( q G )∣ + 2 i + ∣ Ks ( q )∣ + ∣ Ks ( q G )∣ + 2 1 + qPos ( K f ( e ) , q ) + ∣ Ks ( q G )∣ 1 + ∣ Ks ( q )∣ + qPos ( K f ( e ) , q G ) qPos ( K f ( e ) , q ) + ∣ Ks ( q G )∣ ∣ Ks ( q )∣ + qPos ( K f ( e ) , q G ) undef if a c t = [ k 1 , … , k n ] ∧ K f ( e ) = k 1 ∧ ¬ suspended ( e , s h ) if a c t = [ k 1 , … , k n ] ∧ K f ( e ) = k 1 ∧ suspended ( e , s h ) if a c t = [ k 1 , … , k n ] ∧ K f ( e ) = k i ∧ 1 < i ≤ n if a c t = I d l e ∧ K f ( e ) ∈ Ks ( q ) if a c t = I d l e ∧ K f ( e ) ∈ Ks ( q G ) if a c t = I d l e ∧ K f ( e ) ∈ Ks ( q ) if a c t = I d l e ∧ K f ( e ) ∈ Ks ( q G ) if K f ( e ) ∈ / Ks ( trans (⟨[ k 1 , … , k n ] , t , q G , q ⟩) ) Note that this position definition is pessimistic : If a continuation marker is in one queue, it assumes that the entire other queue may be scheduled before it. While unrealistic, our scheduling distance need only be an over approximation of the “real distance.” Further, the distance is only used in our proof for enabled continuation markers, i.e., continuation markers executable in the next step.
A continuation marker’s position in a queue is defined as follows:
qPos ( K f ( e ) , q ) = { i if pop ( q ) = ( q ′ , [ k 1 , … , k n ] ) ∧ K f ( e ) = k i n + qPos ( K f ( e ) , q ′ ) if pop ( q ) = ( q ′ , [ k 1 , … , k n ] ) ∧ K f ( e ) ≠ k 1 , … , k n 1 + qPos ( K f ( e ) , q ′ ) if pop ( q ) = ( q ′ , ( f o o , f ) ) undef if K f ( e ) ∉ Ks ( q ) \operatorname{qPos}(K^{f}(e),q)=
\begin{cases}
i & \text{if }\operatorname{pop}(q)=\left(q',[k_1,\ldots,k_n]\right)\mathbin{\land}K^{f}(e)=k_i\\
n+\operatorname{qPos}(K^{f}(e),q') & \text{if }\operatorname{pop}(q)=\left(q',[k_1,\ldots,k_n]\right)\mathbin{\land}K^{f}(e)\ne k_1,\ldots,k_n\\
1+\operatorname{qPos}(K^{f}(e),q') & \text{if }\operatorname{pop}(q)=\left(q',(\mathrm{foo},f)\right)\\
\textit{undef} & \text{if }K^{f}(e)\notin\operatorname{Ks}(q)
\end{cases} qPos ( K f ( e ) , q ) = ⎩ ⎨ ⎧ i n + qPos ( K f ( e ) , q ′ ) 1 + qPos ( K f ( e ) , q ′ ) undef if pop ( q ) = ( q ′ , [ k 1 , … , k n ] ) ∧ K f ( e ) = k i if pop ( q ) = ( q ′ , [ k 1 , … , k n ] ) ∧ K f ( e ) = k 1 , … , k n if pop ( q ) = ( q ′ , ( foo , f ) ) if K f ( e ) ∈ / Ks ( q )
The function “idle” is 1 if the scheduler is idle and 0 otherwise.
idle ( s h , ⟨ a c t , t , q G , q ⟩ ) = { 1 if a c t = I d l e 0 otherwise \operatorname{idle}(sh,\langle act,t,q_G,q\rangle)=
\begin{cases}
1 & \text{if } act=Idle\\
0 & \text{otherwise}
\end{cases} idle ( s h , ⟨ a c t , t , q G , q ⟩) = { 1 0 if a c t = I d l e otherwise
With “unsched,” we check whether there is an unscheduled spawn events in the trace. It is always either 0 or 1, as there may be at most one unscheduled spawn event in the trace.
unsched ( s h ) = { 1 if ∃ f , h . lastEv ( s h ) = spawnEv ( f , h ) 0 otherwise \operatorname{unsched}(sh)=
\begin{cases}
1 & \text{if } \exists f,h.\ \operatorname{lastEv}(sh)=\operatorname{spawnEv}(f,h)\\
0 & \text{otherwise}
\end{cases} unsched ( s h ) = { 1 0 if ∃ f , h . lastEv ( s h ) = spawnEv ( f , h ) otherwise
We extend the definition to task markers as follows:
qPos ( ( f o o , f ) , q ) = { 1 if pop ( q ) = ( q ′ , ( f o o , f ) ) ∣ k ∣ + qPos ( ( f o o , f ) , q ′ ) if pop ( q ) = ( q ′ , [ k ] ) 1 + qPos ( ( f o o , f ) , q ′ ) if pop ( q ) = ( q ′ , ( f o o ′ , f ′ ) ) ∧ ( f o o ≠ f o o ′ ∨ f ≠ f ′ ) undef if ( f o o , f ) ∉ q \operatorname{qPos}((foo,f),q)=
\begin{cases}
1 & \text{if } \operatorname{pop}(q)=(q',(foo,f))\\
|k|+\operatorname{qPos}((foo,f),q') & \text{if } \operatorname{pop}(q)=(q',[k])\\
1+\operatorname{qPos}((foo,f),q') & \text{if } \operatorname{pop}(q)=(q',(foo',f'))\land(foo\ne foo'\lor f\ne f')\\
\operatorname{undef} & \text{if } (foo,f)\notin q
\end{cases} qPos (( f oo , f ) , q ) = ⎩ ⎨ ⎧ 1 ∣ k ∣ + qPos (( f oo , f ) , q ′ ) 1 + qPos (( f oo , f ) , q ′ ) undef if pop ( q ) = ( q ′ , ( f oo , f )) if pop ( q ) = ( q ′ , [ k ]) if pop ( q ) = ( q ′ , ( f o o ′ , f ′ )) ∧ ( f oo = f o o ′ ∨ f = f ′ ) if ( f oo , f ) ∈ / q
pos ( ( f o o , f ) , s h , ⟨ a c t , t , q G , q ⟩ ) = { 1 + qPos ( ( f o o , f ) , q ) + ∣ Ks ( q G ) ∣ if a c t ≠ I d l e ∧ ( f o o , f ) ∈ q 1 + ∣ Ks ( q ) ∣ + qPos ( ( f o o , f ) , q G ) if a c t ≠ I d l e ∧ ( f o o , f ) ∈ q G qPos ( ( f o o , f ) , q ) + ∣ Ks ( q G ) ∣ if a c t = I d l e ∧ ( f o o , f ) ∈ q ∣ Ks ( q ) ∣ + qPos ( ( f o o , f ) , q G ) if a c t = I d l e ∧ ( f o o , f ) ∈ q G \operatorname{pos}((foo,f),sh,\langle act,t,q_G,q\rangle)=
\begin{cases}
1+\operatorname{qPos}((foo,f),q)+|\operatorname{Ks}(q_G)| & \text{if } act\ne Idle\land(foo,f)\in q\\
1+|\operatorname{Ks}(q)|+\operatorname{qPos}((foo,f),q_G) & \text{if } act\ne Idle\land(foo,f)\in q_G\\
\operatorname{qPos}((foo,f),q)+|\operatorname{Ks}(q_G)| & \text{if } act=Idle\land(foo,f)\in q\\
|\operatorname{Ks}(q)|+\operatorname{qPos}((foo,f),q_G) & \text{if } act=Idle\land(foo,f)\in q_G
\end{cases} pos (( f oo , f ) , s h , ⟨ a c t , t , q G , q ⟩) = ⎩ ⎨ ⎧ 1 + qPos (( f oo , f ) , q ) + ∣ Ks ( q G ) ∣ 1 + ∣ Ks ( q ) ∣ + qPos (( f oo , f ) , q G ) qPos (( f oo , f ) , q ) + ∣ Ks ( q G ) ∣ ∣ Ks ( q ) ∣ + qPos (( f oo , f ) , q G ) if a c t = I d l e ∧ ( f oo , f ) ∈ q if a c t = I d l e ∧ ( f oo , f ) ∈ q G if a c t = I d l e ∧ ( f oo , f ) ∈ q if a c t = I d l e ∧ ( f oo , f ) ∈ q G
We define the order relation > > > over scheduling distances as the canonical ordering of tuples.
As can be seen by the distance definition, d ( K f ( e ) , C , c ) ≥ ( 1 , 0 , 0 , c ) d(K^f(e),C,c)\ge(1,0,0,c) d ( K f ( e ) , C , c ) ≥ ( 1 , 0 , 0 , c ) if K f ( e ) ∈ Ks ( trans ( C ) ) K^f(e)\in\operatorname{Ks}(\operatorname{trans}(C)) K f ( e ) ∈ Ks ( trans ( C )) ; otherwise, the distance is undefined. If d ( K f ( e ) , C , c ) = ( 1 , 0 , 0 , c ) d(K^f(e),C,c)=(1,0,0,c) d ( K f ( e ) , C , c ) = ( 1 , 0 , 0 , c ) and e ∉ V a l e\notin Val e ∈ / V a l , then rule (76) or rule (74) are the next applicable rules. Likewise, if d ( K f ( e ) , C , c ) = ( 1 , 1 , 0 , c ) d(K^f(e),C,c)=(1,1,0,c) d ( K f ( e ) , C , c ) = ( 1 , 1 , 0 , c ) , then the next rule is one of rule (80), rule (82), or rule (83).
The distance of a continuation marker being undefined is not a problem in the following discussion. As we will consider the production of new expressions, we need only consider continuation or task markers that may lead to new expressions via composition rules. These are precisely those markers that have a defined scheduling distance. We never consider the ordering where either component is undefined.
The component c c c in the distance tracks the local computation. Its value is changed depending on the composition rules. We extend → c t \to_{ct} → c t with a new local task counter c c c : C , c → c t C ′ , c ′ C,c\to_{ct}C',c' C , c → c t C ′ , c ′ . Initially, c i n i t = 0 c_{\mathrm{init}}=0 c init = 0 . The scheduler rules are updated to modify c c c in the following ways: Rule (73) does not change c c c , hence, c ′ = c c'=c c ′ = c . Rules (80) to (83), (89) and (90) set c = 0 c=0 c = 0 , as a task is (re-)started. In rules (78) and (79), we leave c c c as is: c ′ = c c'=c c ′ = c . Finally, rules (74) to (77) decrease c c c by one: c ′ = c − 1 c'=c-1 c ′ = c − 1 .
Lemma D.2. Assume an execution s h 0 , ⟨ a c t 0 , t 0 , q G , 0 , q 0 ⟩ → c t + s h 1 , ⟨ a c t 1 , t 1 , q G , 1 , q 1 ⟩ sh_0,\langle act_0,t_0,q_{G,0},q_0\rangle\mathrel{\to_{ct}^{+}}sh_1,\langle act_1,t_1,q_{G,1},q_1\rangle s h 0 , ⟨ a c t 0 , t 0 , q G , 0 , q 0 ⟩ → c t + s h 1 , ⟨ a c t 1 , t 1 , q G , 1 , q 1 ⟩ . Further assume that spawnEv ( f , h ) ∈ s h 1 \operatorname{spawnEv}(f,h)\in sh_1 spawnEv ( f , h ) ∈ s h 1 , lastEv ( s h 1 ) ≠ spawnEv ( f , h ) \operatorname{lastEv}(sh_1)\ne\operatorname{spawnEv}(f,h) lastEv ( s h 1 ) = spawnEv ( f , h ) and createEv ( f o o , f , v ˉ ) ∈ s h 1 \operatorname{createEv}(foo,f,\bar{v})\in sh_1 createEv ( f oo , f , v ˉ ) ∈ s h 1 . Then either ( f o o , h ) ∈ q G , 1 (foo,h)\in q_{G,1} ( f oo , h ) ∈ q G , 1 , ( f o o , h ) ∈ q 1 (foo,h)\in q_1 ( f oo , h ) ∈ q 1 , or evalEv ( f o o , h , v ˉ ) ∈ s h 1 \operatorname{evalEv}(foo,h,\bar{v})\in sh_1 evalEv ( f oo , h , v ˉ ) ∈ s h 1 .
Proof Sketch. This lemma is proven by a straightforward induction over the scheduling rules. Once a future is spawned its corresponding task marker is in a queue until its execution begins, in which case an evaluation event is emitted. □
Naturally, the absence of locally infinite computations preclude infinitely decreasing chains of scheduling distances:
Lemma D.3. If there are no infinite local computations, then there is no infinitely decreasing chain of scheduling distances.
Theorem D.4 (Quiescent Fairness With Counter). Starting from an initial judgement, the current thread scheduler (adjusted with a counter) is quiescent fair if there are no infinite local computations: If C 0 → c t C 1 → c t C 2 ⋯ C_0 \to_{ct} C_1 \to_{ct} C_2 \cdots C 0 → c t C 1 → c t C 2 ⋯ , then there is a quiescent fair execution ⟨ σ 0 ⟩ , ( [ K f i n i t ( e ) ] , ∅ ) → ∗ trans ( C 1 ) → ∗ trans ( C 2 ) → ∗ ⋯ \langle\sigma_0 \rangle, \left([K^{finit}(e)], \emptyset\right) \to^{*} \operatorname{trans}(C_1) \to^{*} \operatorname{trans}(C_2) \to^{*} \cdots ⟨ σ 0 ⟩ , ( [ K f ini t ( e )] , ∅ ) → ∗ trans ( C 1 ) → ∗ trans ( C 2 ) → ∗ ⋯ producing the same trace (modulo schedEv events).
Proof. We must prove two parts: First, for any execution derived by → c t \to_{ct} → c t , there exists an execution using → \to → . Then, we prove the quiescent fairness.
Existence: We proceed by induction over n n n derivation steps with → c t \to_{ct} → c t . As both the semantics using → \to → and → c t \to_{ct} → c t have the same starting point, the base case is free. Assume a derivation in n n n steps for C , c C, c C , c . We apply → c t \to_{ct} → c t once more to get C ′ , c ′ C', c' C ′ , c ′ . By the induction hypothesis, we have a → \to → -execution for trans ( C ) \operatorname{trans}(C) trans ( C ) . We show via a case distinction over the rule applied to get C ′ , c ′ C', c' C ′ , c ′ that a derivation for trans ( C ′ ) \operatorname{trans}(C') trans ( C ′ ) exists. Let in the following C = s h , ⟨ a c t , t , q G , q ⟩ C = sh, \langle act, t, q_G, q \rangle C = s h , ⟨ a c t , t , q G , q ⟩ .
Rule (73): The task marker (foo, f f f ) is pushed to the end of the local queue q q q and appends a schedule event and a repetition of s h sh s h ’s last state to s h sh s h . By the definition of trans, this has no effect on trans ( C ) \operatorname{trans}(C) trans ( C ) . Thus, trans ( C ) \operatorname{trans}(C) trans ( C ) is already the required → \to → -configuration.
Rule (74): Then rule (64) is applicable. The resulting traces are equivalent according to “trans.” Additionally, per definition, trans ( ⟨ [ K f ( e ) , k ‾ ] , t , q G , q ⟩ ) \operatorname{trans}\left(\left\langle[K^{f}(e), \overline{k}], t, q_G, q \right\rangle\right) trans ( ⟨ [ K f ( e ) , k ] , t , q G , q ⟩ ) differs from trans ( ⟨ [ K f ( e ′ ) , k ‾ ] , t , q G , q ⟩ ) \operatorname{trans}\left(\left\langle[K^{f}(e'), \overline{k}], t, q_G, q \right\rangle\right) trans ( ⟨ [ K f ( e ′ ) , k ] , t , q G , q ⟩ ) only by K f ( e ) K^{f}(e) K f ( e ) being replaced by K f ( e ′ ) K^{f}(e') K f ( e ′ ) . This is the same change done by rule (64). Thus, we have a → \to → -derivation.
Rule (75): Then rule (65) is applicable, since its preconditions follow from those of rule (75). The old configuration must be of the form trans ( ⟨ [ k ‾ ] , t , q G , q ⟩ ) = ( [ k ‾ ] , Q ) \operatorname{trans}\left(\left\langle[\overline{k}], t, q_G, q \right\rangle\right) = ([\overline{k}], Q) trans ( ⟨ [ k ] , t , q G , q ⟩ ) = ([ k ] , Q ) . The resulting configuration is:
( [ K f ( e ′ ) , k ‾ ] , Q ) \left([K^{f}(e'), \overline{k}], Q\right) ( [ K f ( e ′ ) , k ] , Q )
This is the same as trans ( ⟨ [ K f ( e ′ ) , k ‾ ] , t , q G , q ⟩ ) \operatorname{trans}\left(\left\langle[K^{f}(e'), \overline{k}], t, q_G, q \right\rangle\right) trans ( ⟨ [ K f ( e ′ ) , k ] , t , q G , q ⟩ ) , as only the active task’s stack is removed and then the same stack with new top element K f ( e ′ ) K^{f}(e') K f ( e ′ ) is added; this is equivalent to the active task [ K f ( e ′ ) , k ‾ ] [K^{f}(e'), \overline{k}] [ K f ( e ′ ) , k ] . Similarly, the traces in the resulting configuration match according to trans.
Rule (76): Then rule (66) is applicable. For the same reasons as in rule (75), the resulting configurations match.
Rule (77): Then rule (67) is applicable. Clearly, trans ( C ′ ) \operatorname{trans}(C') trans ( C ′ ) matches the result of rule (67).
Rule (78): Then rule (68) is applicable. The resulting configurations match per the definition of “trans.”
Rule (79): Then rule (69) is applicable. The resulting configurations clearly agree.
Rule (80) moves the first stack from q G q_G q G to the active slot. Then, rule (71) is applicable. Since s h sh s h in unchanged, the result trans ( C ) = s h , ( [ K f ( e ) , k ‾ ] , Q ∖ { [ K f ( e ) , k ‾ ] } ) \operatorname{trans}(C) = sh, \left([K^{f}(e), \overline{k}], Q \setminus\{[K^{f}(e), \overline{k}]\}\right) trans ( C ) = s h , ( [ K f ( e ) , k ] , Q ∖ {[ K f ( e ) , k ]} ) is the desired → \to → -derivation.
Rule (81): Since the preconditions of rule (81) imply those of rule (70), the latter is applicable on configuration trans ( s h ) , trans ( ⟨ I d l e , t , q G , q ⟩ ) = trans ( s h ) , ( I d l e , Q ) \operatorname{trans}(sh), \operatorname{trans}(\langle Idle, t, q_G, q \rangle) = \operatorname{trans}(sh), (Idle, Q) trans ( s h ) , trans (⟨ I d l e , t , q G , q ⟩) = trans ( s h ) , ( I d l e , Q ) , resulting in configuration trans ( s h ) , ( [ K f ( e ′ ) , Q ) \operatorname{trans}(sh), ([K^{f}(e'), Q) trans ( s h ) , ([ K f ( e ′ ) , Q ) . Since trans 0 ( q G ) = trans 0 ( q G ′ ) \operatorname{trans}^{0}(q_G) = \operatorname{trans}^{0}(q'_G) trans 0 ( q G ) = trans 0 ( q G ′ ) , this is exactly the translation trans ( C ′ ) \operatorname{trans}(C') trans ( C ′ ) .
Rule (82): Same argument as for rule (80).
Rule (89): Same argument as for rule (81). Rule (83): Similar to the cases for rules (80) and (82) but with q q q and q ′ q' q ′ substituted for q G q_G q G and q G ′ q'_G q G ′ , respectively.
Rule (90): Analogous to the cases for rules (81) and (89), except with the observation that trans 0 ( q ) = trans 0 ( q ′ ) \operatorname{trans}^{0}(q)=\operatorname{trans}^{0}(q') trans 0 ( q ) = trans 0 ( q ′ ) .
Mapping: As in [24 ] , we define a mapping between configurations in → \to → and → c t \to_{ct} → c t for a fixed → c t \to_{ct} → c t -execution. Assume such a → c t \to_{ct} → c t -execution
C 0 c t → c t C 1 c t → c t + C n c t → c t ⋯ (91) C^{ct}_0 \mathrel{\to_{ct}} C^{ct}_1 \mathrel{\to^{+}_{ct}} C^{ct}_n \mathrel{\to_{ct}} \cdots \tag*{(91)} C 0 c t → c t C 1 c t → c t + C n c t → c t ⋯ (91)
For most → c t \to_{ct} → c t -applications, there is a direct one-to-one correspondence to a → \to → -step. However, rule (73), which schedules a future, has no equivalent. For this rule, we have no step (i.e., a no-op) on the → \to → -side. Hence the change from at least one step (→ c t + \to^{+}_{ct} → c t + ) to the reflexive-transitive closure (→ ∗ \to^{*} → ∗ ). Then, for the execution (91) there is a → \to → -execution
C 0 ′ → C 1 ′ → ∗ C m ′ → ⋯ (92) C'_0 \to C'_1 \to^{*} C'_m \to\cdots \tag*{(92)} C 0 ′ → C 1 ′ → ∗ C m ′ → ⋯ (92)
Then we can define a mapping π : N → N \pi:\mathbb{N}\to\mathbb{N} π : N → N such that the execution (92) can be written as
trans ( C π ( 0 ) c t ) → trans ( C π ( 1 ) c t ) → + trans ( C π ( m ) c t ) → ⋯ (93) \operatorname{trans}(C^{ct}_{\pi(0)}) \to\operatorname{trans}(C^{ct}_{\pi(1)}) \to^{+} \operatorname{trans}(C^{ct}_{\pi(m)}) \to\cdots \tag*{(93)} trans ( C π ( 0 ) c t ) → trans ( C π ( 1 ) c t ) → + trans ( C π ( m ) c t ) → ⋯ (93)
namely: π ( i ) = i + # ( applications of rule (73) ) \pi(i)=i+\#(\text{applications of rule (73)}) π ( i ) = i + # ( applications of rule (73) ) .
Fairness: Assume a → c t \to_{ct} → c t -execution like (91) and the corresponding → \to → -execution (93) . We need to show that the latter is quiescent fair, i.e.:
∀ m , e , f . ∀ n > m . ( ∃ Q . trans ( ⟨ a c t π ( n ) , t π ( n ) , q G , π ( n ) , q π ( n ) ⟩ ) = ( I d l e , Q ) ⇒ K f ( e ) ∈ enabled ( C π ( n ) ) ) ⇒ ∃ n ′ > m . ( K f ( e ) ∈ K s ( trans ( ⟨ a c t π ( n ′ ) , t π ( n ′ ) , q G , π ( n ′ ) , q π ( n ′ ) ⟩ ) ) ) \begin{aligned}
&\forall m,e,f.\ \forall n>m.\\
&\quad\left(\exists Q.\ \operatorname{trans}\left(\left\langle act_{\pi(n)},t_{\pi(n)},q_{G,\pi(n)},q_{\pi(n)}\right\rangle\right)=(Idle,Q)\Rightarrow K^{f}(e)\in\operatorname{enabled}(C_{\pi(n)})\right)\\
&\quad\Rightarrow\exists n'>m.\ \left(K^{f}(e)\in K_s\left(\operatorname{trans}\left(\left\langle act_{\pi(n')},t_{\pi(n')},q_{G,\pi(n')},q_{\pi(n')}\right\rangle\right)\right)\right)
\end{aligned} ∀ m , e , f . ∀ n > m . ( ∃ Q . trans ( ⟨ a c t π ( n ) , t π ( n ) , q G , π ( n ) , q π ( n ) ⟩ ) = ( I d l e , Q ) ⇒ K f ( e ) ∈ enabled ( C π ( n ) ) ) ⇒ ∃ n ′ > m . ( K f ( e ) ∈ K s ( trans ( ⟨ a c t π ( n ′ ) , t π ( n ′ ) , q G , π ( n ′ ) , q π ( n ′ ) ⟩ ) ) )
Let m , e , f m,e,f m , e , f be arbitrary but fixed and assume that K f ( e ) K^{f}(e) K f ( e ) is enabled in C π ( n ) C_{\pi(n)} C π ( n ) whenever there is no active task in C π ( n ) C_{\pi(n)} C π ( n ) ’s translation. We need to show that eventually, a scheduler rule occurs that produces e e e . We consider each possible rule to yield K f ( e ) ∈ enabled ( C π ( n ) ) K^{f}(e)\in\operatorname{enabled}(C_{\pi(n)}) K f ( e ) ∈ enabled ( C π ( n ) ) . There are three possibilities for add K f ( e ) K^{f}(e) K f ( e ) : (i) it is the consequence of evaluating an expression e ′ e' e ′ in rule (64) or rule (67); (ii) it is the result of evaluating the first step of a scheduled task (rule (70)); or (iii) it is the result of evaluating a synchronous call (rule (65)) or a local future (rule (66)). We show fairness by proving that either e e e is produced, the quiescent fairness precondition does not hold, or the scheduling distance for expression e ′ e' e ′ or ( f o o , f ) (\mathrm{foo},f) ( foo , f ) decreases. By Lemma D.3, there is no infinitely decreasing chain of distances, thus e ′ e' e ′ or ( f o o , f ) (\mathrm{foo},f) ( foo , f ) are eventually scheduled, leading to e e e being produced.
We assume the quiescent fairness precondition; hence, K f ( e ) K^{f}(e) K f ( e ) is reachable. Now there are two possible cases: (case Produce) expression e e e is produced or (case Decrease) the scheduling distance of e ′ e' e ′ or ( f o o , f ) (\mathrm{foo},f) ( foo , f ) decreases.
Rule (64): We restate rule (64) with the translated configuration
trans ( C π ( n ) ) = trans ( s h π ( n ) ) , trans ( ⟨ a c t π ( n ) , t π ( n ) , q G , π ( n ) , q π ( n ) ⟩ ) : \operatorname{trans}(C_{\pi(n)})=\operatorname{trans}(sh_{\pi(n)}),\operatorname{trans}\left(\left\langle act_{\pi(n)},t_{\pi(n)},q_{G,\pi(n)},q_{\pi(n)}\right\rangle\right): trans ( C π ( n ) ) = trans ( s h π ( n ) ) , trans ( ⟨ a c t π ( n ) , t π ( n ) , q G , π ( n ) , q π ( n ) ⟩ ) :
trans ( ⟨ a c t π ( n ) , t π ( n ) , q G , π ( n ) , q π ( n ) ⟩ ) = ( [ K f ( e ′ ) , k ‾ ] , Q ) σ = last ( trans ( s h π ( n ) ) ) τ ⋅ K f ( e ) ∈ val f σ ( e ′ ) ρ concretizes τ w f ( trans ( s h π ( n ) ) ∗ ∗ ρ ( τ ) ) \begin{aligned}
\operatorname{trans}\left(\left\langle act_{\pi(n)},t_{\pi(n)},q_{G,\pi(n)},q_{\pi(n)}\right\rangle\right)
&=\left(\left[K^{f}(e'),\overline{k}\right],Q\right)\\
\sigma&=\operatorname{last}(\operatorname{trans}(sh_{\pi(n)}))\qquad\tau\cdot K^{f}(e)\in\operatorname{val}^{\sigma}_{f}(e')\\
\rho&\text{ concretizes }\tau\qquad wf(\operatorname{trans}(sh_{\pi(n)})**\rho(\tau))
\end{aligned} trans ( ⟨ a c t π ( n ) , t π ( n ) , q G , π ( n ) , q π ( n ) ⟩ ) σ ρ = ( [ K f ( e ′ ) , k ] , Q ) = last ( trans ( s h π ( n ) )) τ ⋅ K f ( e ) ∈ val f σ ( e ′ ) concretizes τ w f ( trans ( s h π ( n ) ) ∗ ∗ ρ ( τ ))
trans ( C π ( n ) ) → trans ( s h π ( n ) ) ∗ ∗ ρ ( τ ) , ( [ K f ( e ) , k ‾ ] , Q ) (94) \operatorname{trans}(C_{\pi(n)})\to\operatorname{trans}(sh_{\pi(n)})**\rho(\tau),\left(\left[K^{f}(e),\overline{k}\right],Q\right) \tag*{(94)} trans ( C π ( n ) ) → trans ( s h π ( n ) ) ∗ ∗ ρ ( τ ) , ( [ K f ( e ) , k ] , Q ) (94)
In this case, because the translated configuration has an active task, so does the configuration C π ( n ) C_{\pi(n)} C π ( n ) and the top of this task, K f ( e ′ ) K^{f}(e') K f ( e ′ ) , can be evaluated yielding a well-formed trace. Hence, only the rules (73) and (74) can be applied; all others have either an idle worker or they cannot evaluate the top of the active task.
If we can apply rule (73), then there must be a spawn event that has not yet been responded too and the distance is of the form ( k , 0 , 1 , c ) (k,0,1,c) ( k , 0 , 1 , c ) , which is reduced by rule (73) to ( k , 0 , 0 , c ) (k,0,0,c) ( k , 0 , 0 , c ) . So we have the case Decrease.
Otherwise, we have d ( K f ( e ′ ) , C π ( n ) , c π ( n ) ) = ( k , 0 , 0 , c π ( n ) ) d(K^{f}(e'), C_{\pi(n)}, c_{\pi(n)}) = (k,0,0,c_{\pi(n)}) d ( K f ( e ′ ) , C π ( n ) , c π ( n ) ) = ( k , 0 , 0 , c π ( n ) ) for some k ≥ 1 k \ge1 k ≥ 1 . Either k = 1 k = 1 k = 1 , which implies that e ′ e' e ′ is the next expression to be executed and is not suspended. Then, clearly, rule (64) is applicable, yielding e ′ e' e ′ (Produce).
If k > 1 k > 1 k > 1 , e ′ e' e ′ is not evaluated by this application of rule (74). But we obtain case Decrease by the observation that d ( K f ( e ′ ) , C π ( n ) + 1 , c π ( n ) + 1 ) = ( k , 0 , 0 , c π ( n ) − 1 ) d(K^{f}(e'), C_{\pi(n)+1}, c_{\pi(n)+1}) = (k,0,0,c_{\pi(n)}-1) d ( K f ( e ′ ) , C π ( n ) + 1 , c π ( n ) + 1 ) = ( k , 0 , 0 , c π ( n ) − 1 ) .
Rule (65) executes a synchronous call. We restate it in the translated setting:
trans ( ⟨ a c t π ( n ) , t π ( n ) , q G , π ( n ) , q π ( n ) ⟩ ) = ( [ k ‾ ] , Q ) σ = last ( s h π ( n ) ) f ∈ F I d lookup ( foo ) = fn foo ( x ‾ ) e τ ⋅ K f ( e ) ∈ val σ f ( fn foo ( x ‾ ) e ′ ) ρ concretizes τ w f ( trans ( s h π ( n ) ) ∗ ∗ ρ ( τ ) ) trans ( C π ( n ) ) → trans ( s h π ( n ) ) ∗ ∗ ρ ( τ ) , ( [ K f ( e ) , k ‾ ] , Q ) (95) \begin{aligned}
\operatorname{trans}(\langle act_{\pi(n)},t_{\pi(n)},q_{G,\pi(n)},q_{\pi(n)}\rangle) &= ([\overline{k}],Q) \qquad\sigma=\operatorname{last}(sh_{\pi(n)}) \qquad f\in FId \\
\operatorname{lookup}(\operatorname{foo}) &= \operatorname{fn}\operatorname{foo}(\overline{x})\ e\ \tau\cdot K^{f}(e)\in\operatorname{val}^{f}_{\sigma}(\operatorname{fn}\operatorname{foo}(\overline{x})\ e') \\
\rho\ \text{concretizes}\ \tau\qquad wf(\operatorname{trans}(sh_{\pi(n)}) ** \rho(\tau)) \\
\operatorname{trans}(C_{\pi(n)}) &\to\operatorname{trans}(sh_{\pi(n)}) ** \rho(\tau),([\ K^{f}(e),\overline{k}],Q) \tag*{(95)}
\end{aligned} trans (⟨ a c t π ( n ) , t π ( n ) , q G , π ( n ) , q π ( n ) ⟩) lookup ( foo ) ρ concretizes τ w f ( trans ( s h π ( n ) ) ∗ ∗ ρ ( τ )) trans ( C π ( n ) ) = ([ k ] , Q ) σ = last ( s h π ( n ) ) f ∈ F I d = fn foo ( x ) e τ ⋅ K f ( e ) ∈ val σ f ( fn foo ( x ) e ′ ) → trans ( s h π ( n ) ) ∗ ∗ ρ ( τ ) , ([ K f ( e ) , k ] , Q ) (95)
By the same argument as for rule (64), only the rules rules (73) and (75) are applicable. The exact same argument further gives us that we either Produce e e e or Decrease the distance.
Rule (66) executes a local future. We restate rule (66) in our translated setting:
trans ( ⟨ a c t π ( n ) , t π ( n ) , q G , π ( n ) , q π ( n ) ⟩ ) = ( [ k ‾ ] , Q ) σ = last ( s h π ( n ) ) f ∈ F I d lookup ( foo ) = async fn foo ( x ‾ ) e τ ⋅ K f ( e ) ∈ val σ f ( async fn foo ( x ‾ ) e ′ ) ρ concretizes τ w f ( trans ( s h π ( n ) ) ∗ ∗ ρ ( τ ) ) trans ( C π ( n ) ) → trans ( s h π ( n ) ) ∗ ∗ ρ ( τ ) , ( [ K f ( e ) , k ‾ ] , Q ) (96) \begin{aligned}
\operatorname{trans}(\langle act_{\pi(n)},t_{\pi(n)},q_{G,\pi(n)},q_{\pi(n)}\rangle) &= ([\overline{k}],Q) \qquad\sigma=\operatorname{last}(sh_{\pi(n)}) \qquad f\in FId \\
\operatorname{lookup}(\operatorname{foo}) &= \operatorname{async\ fn}\operatorname{foo}(\overline{x})\ e\ \tau\cdot K^{f}(e)\in\operatorname{val}^{f}_{\sigma}(\operatorname{async\ fn}\operatorname{foo}(\overline{x})\ e') \\
\rho\ \text{concretizes}\ \tau\qquad wf(\operatorname{trans}(sh_{\pi(n)}) ** \rho(\tau)) \\
\operatorname{trans}(C_{\pi(n)}) &\to\operatorname{trans}(sh_{\pi(n)}) ** \rho(\tau),([\ K^{f}(e),\overline{k}],Q) \tag*{(96)}
\end{aligned} trans (⟨ a c t π ( n ) , t π ( n ) , q G , π ( n ) , q π ( n ) ⟩) lookup ( foo ) ρ concretizes τ w f ( trans ( s h π ( n ) ) ∗ ∗ ρ ( τ )) trans ( C π ( n ) ) = ([ k ] , Q ) σ = last ( s h π ( n ) ) f ∈ F I d = async fn foo ( x ) e τ ⋅ K f ( e ) ∈ val σ f ( async fn foo ( x ) e ′ ) → trans ( s h π ( n ) ) ∗ ∗ ρ ( τ ) , ([ K f ( e ) , k ] , Q ) (96)
We repeat the argument for rule (65) but with rules (73) and (76) applicable.
Rule (67) removes the top continuation of the active task’s stack if its computation is completed. Restated in our terms, the rule goes:
trans ( ⟨ a c t π ( n ) , t π ( n ) , q G , π ( n ) , q π ( n ) ⟩ ) = ( [ K f ( e ′ ′ ) , K f ( e ′ ) , k ‾ ] , Q ) σ = last ( s h π ( n ) ) τ ⋅ K ( e ) ∈ val σ f ( e ′ ) ρ concretizes τ w f ( trans ( s h π ( n ) ) ∗ ∗ ρ ( τ ) ) trans ( C π ( n ) ) → trans ( s h π ( n ) ) , ( [ K f ( e ) , k ‾ ] , Q ) (97) \begin{aligned}
\operatorname{trans}(\langle act_{\pi(n)},t_{\pi(n)},q_{G,\pi(n)},q_{\pi(n)}\rangle) &= ([K^{f}(e''),K^{f}(e'),\overline{k}],Q) \qquad\sigma=\operatorname{last}(sh_{\pi(n)}) \\
\tau\cdot K(e) &\in\operatorname{val}^{f}_{\sigma}(e') \qquad\rho\ \text{concretizes}\ \tau\qquad wf(\operatorname{trans}(sh_{\pi(n)}) ** \rho(\tau)) \\
\operatorname{trans}(C_{\pi(n)}) &\to\operatorname{trans}(sh_{\pi(n)}),([K^{f}(e),\overline{k}],Q) \tag*{(97)}
\end{aligned} trans (⟨ a c t π ( n ) , t π ( n ) , q G , π ( n ) , q π ( n ) ⟩) τ ⋅ K ( e ) trans ( C π ( n ) ) = ([ K f ( e ′′ ) , K f ( e ′ ) , k ] , Q ) σ = last ( s h π ( n ) ) ∈ val σ f ( e ′ ) ρ concretizes τ w f ( trans ( s h π ( n ) ) ∗ ∗ ρ ( τ )) → trans ( s h π ( n ) ) , ([ K f ( e ) , k ] , Q ) (97)
Again, the same argument as for rules (64) to (66) but now rules (73) and (77) can be applied.
Rule (68): Has no effect on the set of enabled continuations. Thus, it cannot have produced e ′ e' e ′ .
Rule (69): Has no effect on the set of enabled continuations. Thus, it cannot have produced e ′ e' e ′ .
Rule (70) executes a spawned future. We restate rule (70) in our translated setting:
trans ( ⟨ a c t π ( n ) , t π ( n ) , q G , π ( n ) , q π ( n ) ⟩ ) = ( Idle , Q ) σ = last ( s h π ( n ) ) f ∈ F I d lookup ( foo ) = async fn foo ( x ‾ ) e τ ⋅ K f ( e ) ∈ val σ f ( async fn foo ( x ‾ ) e ′ ) ρ concretizes τ w f ( trans ( s h π ( n ) ) ∗ ∗ ρ ( τ ) ) trans ( C π ( n ) ) → trans ( s h π ( n ) ) ∗ ∗ ρ ( τ ) , ( [ K f ( e ) ] , Q ) (98) \begin{aligned}
\operatorname{trans}(\langle act_{\pi(n)},t_{\pi(n)},q_{G,\pi(n)},q_{\pi(n)}\rangle) &= (\operatorname{Idle},Q) \qquad\sigma=\operatorname{last}(sh_{\pi(n)}) \qquad f\in FId \\
\operatorname{lookup}(\operatorname{foo}) &= \operatorname{async\ fn}\operatorname{foo}(\overline{x})\ e\ \tau\cdot K^{f}(e)\in\operatorname{val}^{f}_{\sigma}(\operatorname{async\ fn}\operatorname{foo}(\overline{x})\ e') \\
\rho\ \text{concretizes}\ \tau\qquad wf(\operatorname{trans}(sh_{\pi(n)}) ** \rho(\tau)) \\
\operatorname{trans}(C_{\pi(n)}) &\to\operatorname{trans}(sh_{\pi(n)}) ** \rho(\tau),([K^{f}(e)],Q) \tag*{(98)}
\end{aligned} trans (⟨ a c t π ( n ) , t π ( n ) , q G , π ( n ) , q π ( n ) ⟩) lookup ( foo ) ρ concretizes τ w f ( trans ( s h π ( n ) ) ∗ ∗ ρ ( τ )) trans ( C π ( n ) ) = ( Idle , Q ) σ = last ( s h π ( n ) ) f ∈ F I d = async fn foo ( x ) e τ ⋅ K f ( e ) ∈ val σ f ( async fn foo ( x ) e ′ ) → trans ( s h π ( n ) ) ∗ ∗ ρ ( τ ) , ([ K f ( e )] , Q ) (98)
Since the evaluation of async fn foo(\bar{x}) e starts with an evaluation event, well-formedness gives us that a spawn event for f f f and foo exists in trans ( s h π ( n ) ) \operatorname{trans}(sh_{\pi(n)}) trans ( s h π ( n ) ) but no evaluation event for f f f and foo. If lastEv ( trans ( s h π ( n ) ) ) \operatorname{lastEv}(\operatorname{trans}(sh_{\pi(n)})) lastEv ( trans ( s h π ( n ) )) is a spawn event, we apply rule (73) and start our argument on its result.
Lemma D.2 gives us that (foo, f f f ) is in one of the scheduler’s queues and its distance is greater or equal to ( 1 , 1 , 0 , c π ( n ) ) (1, 1, 0, c_{\pi(n)}) ( 1 , 1 , 0 , c π ( n ) ) . We consider each case of the application C π ( n ) → c t C π ( n ) + 1 C_{\pi(n)} \to_{\mathrm{ct}} C_{\pi(n)+1} C π ( n ) → ct C π ( n ) + 1 .
Rule (73) can only be applied to schedule a task with a different future identifier. The distance is, hence, d ( ( f o o , f ) , C π ( n ) , c π ( n ) ) = ( k , i , 1 , c π ( n ) ) d((\mathrm{foo}, f), C_{\pi(n)}, c_{\pi(n)}) = (k, i, 1, c_{\pi(n)}) d (( foo , f ) , C π ( n ) , c π ( n ) ) = ( k , i , 1 , c π ( n ) ) for some k ≥ 1 k \ge1 k ≥ 1 and i ∈ { 0 , 1 } i \in\{0,1\} i ∈ { 0 , 1 } before the step and d ( ( f o o , f ) , C π ( n ) + 1 , c π ( n ) ) = ( k , i , 0 , c π ( n ) ) d((\mathrm{foo}, f), C_{\pi(n)+1}, c_{\pi(n)}) = (k, i, 0, c_{\pi(n)}) d (( foo , f ) , C π ( n ) + 1 , c π ( n ) ) = ( k , i , 0 , c π ( n ) ) afterwards. Thus, the scheduling distance decreases (Decrease).
Rule (74): Again we have case Decrease, as the distance goes from d ( ( f o o , f ) , C π ( n ) , c π ( n ) ) = ( k , 0 , 0 , c π ( n ) ) d((\mathrm{foo}, f), C_{\pi(n)}, c_{\pi(n)}) = (k, 0, 0, c_{\pi(n)}) d (( foo , f ) , C π ( n ) , c π ( n ) ) = ( k , 0 , 0 , c π ( n ) ) , with k > 1 k > 1 k > 1 , to d ( ( f o o , f ) , C π ( n ) + 1 ) = ( k , 0 , 0 , c π ( n ) − 1 ) d((\mathrm{foo}, f), C_{\pi(n)+1}) = (k, 0, 0, c_{\pi(n)} - 1) d (( foo , f ) , C π ( n ) + 1 ) = ( k , 0 , 0 , c π ( n ) − 1 ) .
Rule (75) evaluates a synchronous call. Then, case Decrease holds, as the distance goes from d ( ( f o o , f ) , C π ( n ) , c π ( n ) ) = ( k , 0 , 0 , c π ( n ) ) d((\mathrm{foo}, f), C_{\pi(n)}, c_{\pi(n)}) = (k, 0, 0, c_{\pi(n)}) d (( foo , f ) , C π ( n ) , c π ( n ) ) = ( k , 0 , 0 , c π ( n ) ) , for some k > 1 k > 1 k > 1 , to d ( ( f o o , f ) , C π ( n ) + 1 , c π ( n ) ) = ( k , 0 , 0 , c π ( n ) − 1 ) d((\mathrm{foo}, f), C_{\pi(n)+1}, c_{\pi(n)}) = (k, 0, 0, c_{\pi(n)} - 1) d (( foo , f ) , C π ( n ) + 1 , c π ( n ) ) = ( k , 0 , 0 , c π ( n ) − 1 ) .
Rule (76): Same argument as for rule (75).
Rule (77): Before the application, we have d ( ( f o o , f ) , C π ( n ) , c π ( n ) ) = ( k , 0 , 0 , c π ( n ) ) d((\mathrm{foo}, f), C_{\pi(n)}, c_{\pi(n)}) = (k, 0, 0, c_{\pi(n)}) d (( foo , f ) , C π ( n ) , c π ( n ) ) = ( k , 0 , 0 , c π ( n ) ) for some k > 1 k > 1 k > 1 . Afterwards, d ( ( f o o , f ) , C π ( n ) + 1 , c π ( n ) + 1 ) = ( k , 0 , 0 , c π ( n ) − 1 ) d((\mathrm{foo}, f), C_{\pi(n)+1}, c_{\pi(n)+1}) = (k, 0, 0, c_{\pi(n)} - 1) d (( foo , f ) , C π ( n ) + 1 , c π ( n ) + 1 ) = ( k , 0 , 0 , c π ( n ) − 1 ) , which is a decrease of the scheduling distance (Decrease).
Rule (78): Then the scheduling distance decreases from d ( ( f o o , f ) , C π ( n ) ) = ( k , 0 , 0 , c π ( n ) ) d((\mathrm{foo}, f), C_{\pi(n)}) = (k, 0, 0, c_{\pi(n)}) d (( foo , f ) , C π ( n ) ) = ( k , 0 , 0 , c π ( n ) ) , for some k > 1 k > 1 k > 1 , to d ( ( f o o , f ) , C π ( n ) + 1 ) = ( k − 1 , 1 , 0 , c π ( n ) − 1 ) d((\mathrm{foo}, f), C_{\pi(n)+1}) = (k - 1, 1, 0, c_{\pi(n)} - 1) d (( foo , f ) , C π ( n ) + 1 ) = ( k − 1 , 1 , 0 , c π ( n ) − 1 ) . We have Decrease.
Rule (79): Same argument as for rule (78).
Rule (80): Before the application, we have d ( ( f o o , f ) , C π ( n ) , c π ( n ) ) = ( k , 1 , 0 , c π ( n ) ) d((\mathrm{foo}, f), C_{\pi(n)}, c_{\pi(n)}) = (k, 1, 0, c_{\pi(n)}) d (( foo , f ) , C π ( n ) , c π ( n ) ) = ( k , 1 , 0 , c π ( n ) ) for some k > 1 k > 1 k > 1 . Afterwards, we have d ( ( f o o , f ) , C π ( n ) + 1 , c π ( n ) ) = ( k , 0 , 0 , 0 ) d((\mathrm{foo}, f), C_{\pi(n)+1}, c_{\pi(n)}) = (k, 0, 0, 0) d (( foo , f ) , C π ( n ) + 1 , c π ( n ) ) = ( k , 0 , 0 , 0 ) , i.e., a decrease of the scheduling distance (Decrease).
Rule (81): Either d ( ( f o o , f ) , C π ( n ) , c π ( n ) ) = ( 1 , 1 , 0 , c π ( n ) ) d((\mathrm{foo}, f), C_{\pi(n)}, c_{\pi(n)}) = (1, 1, 0, c_{\pi(n)}) d (( foo , f ) , C π ( n ) , c π ( n ) ) = ( 1 , 1 , 0 , c π ( n ) ) , in which case the correct continuation is produced (Produce), or the distance is ( k , 1 , 0 , c π ( n ) ) (k, 1, 0, c_{\pi(n)}) ( k , 1 , 0 , c π ( n ) ) with k > 1 k > 1 k > 1 and the application reduces the scheduling distance (Decrease) to d ( ( f o o , f ) , C π ( n ) + 1 , c π ( n ) + 1 ) = ( k , 0 , 0 , 0 ) d((\mathrm{foo}, f), C_{\pi(n)+1}, c_{\pi(n)+1}) = (k, 0, 0, 0) d (( foo , f ) , C π ( n ) + 1 , c π ( n ) + 1 ) = ( k , 0 , 0 , 0 ) .
Rule (82): Same argument as for rule (80).
Rule (89): Same argument as for rule (81).
Rule (83): Same as for rules (80) and (82).
Rule (90): Same argument as for rules (81) and (89).
Rule (71): We follow the same argument as for rule (70) but now with a task [ k ˉ ] [\bar{k}] [ k ˉ ] rather than a task marker.
Relation of Quiescent and Tokio Fairness
Proof of Theorem 5.9 . As established in Appendix § 13 , we know that the current thread runtime is quiescent fair. We show that the premisses of Tokio fairness are stronger than those of quiescent fairness and the conclusion follows from that of quiescent fairness. Since the first assumption of Definition 5.8 is absent from the definition of quiescent fairness, we can add it without altering the conclusion.
The second premiss is stronger than the assumption of locally finite computations: For quiescent fairness we only assume that each task completes or suspends in a final amount of steps; we do not require a fixed upper bound for all tasks.
Finally, note that the conclusions of both definitions are similar, except that ∃ n ′ > m . e ∈ newConts ( C n ′ , C n ′ + 1 ) \exists n' > m.\ e \in\operatorname{newConts}(C_{n'}, C_{n'+1}) ∃ n ′ > m . e ∈ newConts ( C n ′ , C n ′ + 1 ) in quiescent fairness is strengthened to ∃ n ′ . m < n ′ ≤ m + D max ∧ e ∈ newConts ( C n ′ , C n ′ + 1 ) \exists n'.\ m < n' \le m + D_{\max} \land e \in\operatorname{newConts}(C_{n'}, C_{n'+1}) ∃ n ′ . m < n ′ ≤ m + D m a x ∧ e ∈ newConts ( C n ′ , C n ′ + 1 ) in Tokio fairness. Hence, we need to show that the n ′ n' n ′ we find for quiescent fairness is always less than m + D max m + D_{\max} m + D m a x . The distance function defined in Appendix § 13 can be used to find D max D_{\max} D m a x .
We note that, because the local computations must complete within P max P_{\max} P m a x steps, we can make all components of the scheduling distance non-negative by adding P max P_{\max} P m a x to the final component. Then, for a scheduling distance ( a , b , c , d ) (a,b,c,d) ( a , b , c , d ) we construct D ∈ N D \in\mathbb{N} D ∈ N by
D : = a ∘ b ∘ c ∘ d ′ D := a \mathbin{\circ} b \mathbin{\circ} c \mathbin{\circ} d' D := a ∘ b ∘ c ∘ d ′
where ∘ \circ ∘ denotes concatenation of digits and d ′ d' d ′ is d + P max d + P_{\max} d + P m a x extended with leading zeroes to the number of digits of P max P_{\max} P m a x . For example, if P max = 100 P_{\max} = 100 P m a x = 100 , the scheduling distance ( 32 , 1 , 0 , − 64 ) (32,1,0,-64) ( 32 , 1 , 0 , − 64 ) is mapped to D 0 = 3210036 D_0 = 3210036 D 0 = 3210036 . As can be easily seen, this mapping preserves the order over distances.
By definition of the scheduling distance (Definition D.1 ), the first component is bounded by the sizes of both queues plus one: 0 ≤ a ≤ ∣ K s ( q ) ∣ + ∣ K s ( q G ) ∣ + 1 0 \leq a \leq|\mathrm{Ks}(q)| + |\mathrm{Ks}(q_G)| + 1 0 ≤ a ≤ ∣ Ks ( q ) ∣ + ∣ Ks ( q G ) ∣ + 1 ; because the number of tasks is less than T max T_{\max} T m a x , we have a ≤ T max + 1 a \leq T_{\max} + 1 a ≤ T m a x + 1 . The second and third components are either 0 or 1; the last component is 0 ≤ d ≤ P max 0 \leq d \leq P_{\max} 0 ≤ d ≤ P m a x . Thus, all distance numbers D D D for all expressions at all points are less than or equal to
D max : = ( T max + 1 ) ∘ 1 ∘ 1 ∘ P max . D_{\max} := (T_{\max} + 1) \mathbin{\circ} 1 \mathbin{\circ} 1 \mathbin{\circ} P_{\max}. D m a x := ( T m a x + 1 ) ∘ 1 ∘ 1 ∘ P m a x .
As the proof above uses no properties of the current thread runtime besides its quiescent fairness and the existence of a scheduling distance, the following corollary follows immediately:
Corollary E.1. A quiescent fair scheduler, where there is a scheduling distance d ( K f ( e ) , C , c ) d(\mathrm{K}^{\mathrm{f}}(e), C, c) d ( K f ( e ) , C , c ) such that, for each continuation marker K f ( e ) \mathrm{K}^{\mathrm{f}}(e) K f ( e ) that is in enabled ( C i ) \operatorname{enabled}(C_i) enabled ( C i ) for some configuration in an execution, the distance for K f ( e ) \mathrm{K}^{\mathrm{f}}(e) K f ( e ) is either decreased by each step or e e e is produced and the distance is bounded by a maximum, is also Tokio fair.
It is unknown whether there exist any quiescent fair schedulers that are not Tokio fair.
Auxiliary Definitions and Theorems for DL
Definition F.1 (Domain). We fix the domain D A \mathcal{D}^{A} D A of each sort A A A as follows:
D b o o l = { t t , f f } \mathcal{D}^{\mathrm{bool}} = \{\mathrm{tt}, \mathrm{ff}\} D bool = { tt , ff } ,
D i n t = Z \mathcal{D}^{\mathrm{int}} = \mathbb{Z} D int = Z ,
D F i e l d \mathcal{D}^{\mathrm{Field}} D Field an infinite set of field identifiers,
D P l a c e A = lim n → ∞ D n P l a c e A \mathcal{D}^{\mathrm{Place}A} = \lim_{n \to\infty} \mathcal{D}^{\mathrm{Place}A}_{n} D Place A = lim n → ∞ D n Place A where
D 0 P l a c e A = { P l a c e ( x ) ∣ x : A ∈ P r o g V S y m } \mathcal{D}^{\mathrm{Place}A}_{0} = \{\mathrm{Place}(x) \mid x : A \in\mathrm{ProgVSym}\} D 0 Place A = { Place ( x ) ∣ x : A ∈ ProgVSym } and
D i + 1 P l a c e A = D i P l a c e A ∪ { A r r P l a c e ( p , n ) ∣ p ∈ D i P l a c e A , n ∈ N } ∪ { F l d P l a c e ( p , f ) ∣ p ∈ D i P l a c e A , f ∈ D F i e l d } \mathcal{D}^{\mathrm{Place}A}_{i+1} = \mathcal{D}^{\mathrm{Place}A}_{i} \cup\{\mathrm{ArrPlace}(p,n) \mid p \in\mathcal{D}^{\mathrm{Place}A}_{i}, n \in\mathbb{N}\} \cup\{\mathrm{FldPlace}(p,f) \mid p \in\mathcal{D}^{\mathrm{Place}A}_{i}, f \in\mathcal{D}^{\mathrm{Field}}\} D i + 1 Place A = D i Place A ∪ { ArrPlace ( p , n ) ∣ p ∈ D i Place A , n ∈ N } ∪ { FldPlace ( p , f ) ∣ p ∈ D i Place A , f ∈ D Field } ,
D R e f A s = { S h a r e d R e f ( a ) ∣ a ∈ D A } \mathcal{D}^{\mathrm{Ref}^{s}_{A}} = \{\mathrm{SharedRef}(a) \mid a \in\mathcal{D}^{A}\} D Ref A s = { SharedRef ( a ) ∣ a ∈ D A } for all sorts A A A ,
D R e f A m = { M u t R e f ( p ) ∣ p ∈ D P l a c e A } \mathcal{D}^{\mathrm{Ref}^{m}_{A}} = \{\mathrm{MutRef}(p) \mid p \in\mathcal{D}^{\mathrm{Place}A}\} D Ref A m = { MutRef ( p ) ∣ p ∈ D Place A } for all sorts A A A ,
D ( A 1 , … , A n ) = D A 1 × ⋯ × D A n \mathcal{D}^{(A_{1},\ldots,A_{n})} = \mathcal{D}^{A_{1}} \times\cdots\times\mathcal{D}^{A_{n}} D ( A 1 , … , A n ) = D A 1 × ⋯ × D A n ,
D [ A ; n ] = { A r r a y ( d n , … , d n ) ∣ d 1 , … , d n ∈ D A } \mathcal{D}^{[A;n]} = \{\mathrm{Array}(d_{n},\ldots,d_{n}) \mid d_{1},\ldots,d_{n} \in\mathcal{D}^{A}\} D [ A ; n ] = { Array ( d n , … , d n ) ∣ d 1 , … , d n ∈ D A } ,
and domains for each datatype declared in the program.
Definition F.2 (Correspondence of RustyDL Domain and LAGC Values). Let D \mathcal{D} D be a RustyDL Domain and σ \sigma σ an LAGC state. We define the binary relation ∼ σ ⊆ D × Val \sim_{\sigma} \subseteq\mathcal{D} \times\operatorname{Val} ∼ σ ⊆ D × Val (pronounced “d d d corresponds to v v v w.r.t. σ \sigma σ ”) inductively as follows:
(1) n ∼ σ n n \sim_{\sigma} n n ∼ σ n for each n ∈ Z n \in\mathbb{Z} n ∈ Z ,
(2) t t ∼ σ t t \mathrm{tt} \sim_{\sigma} \mathrm{tt} tt ∼ σ tt and f f ∼ σ f f \mathrm{ff} \sim_{\sigma} \mathrm{ff} ff ∼ σ ff ,
(3) S h a r e d R e f ( a ) ∼ σ R e f σ s ( b ) \mathrm{SharedRef}(a) \sim_{\sigma} \mathrm{Ref}^{s}_{\sigma}(b) SharedRef ( a ) ∼ σ Ref σ s ( b ) for each x ∈ V a r x \in\mathrm{Var} x ∈ Var and for all b ∈ B I d b \in\mathrm{BId} b ∈ BId , where a ∼ σ σ ( x ) a \sim_{\sigma} \sigma(x) a ∼ σ σ ( x ) ,
(4) M u t R e f ( p ) ∼ σ R e f σ m ( b ) \mathrm{MutRef}(p) \sim_{\sigma} \mathrm{Ref}^{m}_{\sigma}(b) MutRef ( p ) ∼ σ Ref σ m ( b ) for each p ∈ D P l a c e A p \in\mathcal{D}^{\mathrm{Place}A} p ∈ D Place A , b ∈ B I d b \in\mathrm{BId} b ∈ BId , p ′ ∈ P l a c e p' \in\mathrm{Place} p ′ ∈ Place iff p p p and p ′ p' p ′ correspond,
( d 1 , … , d n ) ∼ σ ( v 1 , … , v n ) iff d 1 ∼ σ v 1 , … , d n ∼ σ v n , (99) (d_1,\ldots,d_n) \sim_{\sigma} (v_1,\ldots,v_n) \text{ iff } d_1 \sim_{\sigma} v_1,\ldots,d_n \sim_{\sigma} v_n, \tag*{(99)} ( d 1 , … , d n ) ∼ σ ( v 1 , … , v n ) iff d 1 ∼ σ v 1 , … , d n ∼ σ v n , (99)
Array ( d 1 , … , d n ) ∼ σ [ v 1 , … , v n ] iff d 1 ∼ σ v 1 , … , d n ∼ σ v n . (100) \operatorname{Array}(d_1,\ldots,d_n) \sim_{\sigma} [v_1,\ldots,v_n] \text{ iff } d_1 \sim_{\sigma} v_1,\ldots,d_n \sim_{\sigma} v_n. \tag*{(100)} Array ( d 1 , … , d n ) ∼ σ [ v 1 , … , v n ] iff d 1 ∼ σ v 1 , … , d n ∼ σ v n . (100)
Definition F.3 (Correspondence of RustyDL States and LAGC States). Let K = ( D , I , S , ρ ) \mathcal{K} = (\mathcal{D}, I, S, \rho) K = ( D , I , S , ρ ) be RustyDL Kripke structure, s ∈ S s \in S s ∈ S a RustyDL state, and σ \sigma σ an LAGC state. We define the binary relation ∼ \sim ∼ (pronounced “corresponds”) as follows: s ∼ σ s \sim\sigma s ∼ σ iff for all x : A ∈ ProgVSym x : A \in\operatorname{ProgVSym} x : A ∈ ProgVSym such that x ∈ dom ( σ ) x \in\operatorname{dom}(\sigma) x ∈ dom ( σ ) , we have s ( x ) ∼ σ σ ( x ) s(x) \sim_{\sigma} \sigma(x) s ( x ) ∼ σ σ ( x ) .
We fix some interpretations of functions and relations:
I ( + ) ( n , m ) = n + m I(+)(n,m) = n + m I ( + ) ( n , m ) = n + m with n , m ∈ D i n t n,m \in\mathcal{D}^{\mathit{int}} n , m ∈ D int (similar for all integer functions),
I ( s r e f ) ( d ) = SharedRef ( d ) I(\mathit{sref})(d) = \operatorname{SharedRef}(d) I ( sref ) ( d ) = SharedRef ( d ) with d ∈ D d \in\mathcal{D} d ∈ D ,
I ( m r e f ) ( p ) = MutRef ( p ) I(\mathit{mref})(p) = \operatorname{MutRef}(p) I ( mref ) ( p ) = MutRef ( p ) ,
I ( s ∗ ) ( SharedRef ( d ) ) = d I({}^{*}_{s})(\operatorname{SharedRef}(d)) = d I ( s ∗ ) ( SharedRef ( d )) = d ,
I ( i n U 32 ) = { n ∈ N ∣ 0 ≤ n < 2 32 } I(\mathit{inU32}) = \{n \in\mathbb{N} \mid0 \le n < 2^{32}\} I ( inU32 ) = { n ∈ N ∣ 0 ≤ n < 2 32 } .
Additional Soundness Proofs of Dynamic Logic Calculus Rules
Theorem G.1 (Soundness of rule ifElseSplit). Let Γ , Δ ∈ 2 D L F m l \Gamma, \Delta\in2^{\mathrm{DLFml}} Γ , Δ ∈ 2 DLFml be sets of formulas, U ∈ U p d \mathcal{U} \in\mathrm{Upd} U ∈ Upd be an update, s e se se a literal or a program variable of type bool , { b e 0 } \{be_0\} { b e 0 } and { b e 1 } \{be_1\} { b e 1 } be block expressions, π \pi π if s e se se { b e 0 } \{be_0\} { b e 0 } else { b e 1 } \{be_1\} { b e 1 } ω \omega ω be a program fragment, and ϕ ∈ D L F m l \phi\in\mathrm{DLFml} ϕ ∈ DLFml be a formula. If
⊨ Γ , { U } ( s e = T R U E ) ⟹ { U } ⟨ π { b e 0 } ω ⟩ ϕ , Δ \models\Gamma, \{\mathcal{U}\}(se = \mathrm{TRUE}) \Longrightarrow\{\mathcal{U}\}\langle\pi\ \{be_0\}\ \omega\rangle\phi, \Delta ⊨ Γ , { U } ( se = TRUE ) ⟹ { U } ⟨ π { b e 0 } ω ⟩ ϕ , Δ ⊨ Γ , { U } ( s e = F A L S E ) ⟹ { U } ⟨ π { b e 1 } ω ⟩ ϕ , Δ \models\Gamma, \{\mathcal{U}\}(se = \mathrm{FALSE}) \Longrightarrow\{\mathcal{U}\}\langle\pi\ \{be_1\}\ \omega\rangle\phi, \Delta ⊨ Γ , { U } ( se = FALSE ) ⟹ { U } ⟨ π { b e 1 } ω ⟩ ϕ , Δ then the following holds:
⊨ Γ ⟹ { U } ⟨ π if s e { b e 0 } else { b e 1 } ω ⟩ ϕ , Δ . \models\Gamma\Longrightarrow\{\mathcal{U}\}\langle\pi\ \text{if } se\ \{be_0\}\ \text{else }\{be_1\}\ \omega\rangle\phi, \Delta. ⊨ Γ ⟹ { U } ⟨ π if se { b e 0 } else { b e 1 } ω ⟩ ϕ , Δ.
Proof. We assume
⊨ Γ , { U } ( s e = T R U E ) ⟹ { U } ⟨ π { b e 0 } ω ⟩ ϕ , Δ (101) \models\Gamma, \{\mathcal{U}\}(se = \mathrm{TRUE}) \Longrightarrow\{\mathcal{U}\}\langle\pi\ \{be_0\}\ \omega\rangle\phi, \Delta \tag*{(101)} ⊨ Γ , { U } ( se = TRUE ) ⟹ { U } ⟨ π { b e 0 } ω ⟩ ϕ , Δ (101)
⊨ Γ , { U } ( s e = F A L S E ) ⟹ { U } ⟨ π { b e 1 } ω ⟩ ϕ , Δ (102) \models\Gamma, \{\mathcal{U}\}(se = \mathrm{FALSE}) \Longrightarrow\{\mathcal{U}\}\langle\pi\ \{be_1\}\ \omega\rangle\phi, \Delta \tag*{(102)} ⊨ Γ , { U } ( se = FALSE ) ⟹ { U } ⟨ π { b e 1 } ω ⟩ ϕ , Δ (102)
It is sufficient to show
( K , s 1 , β ) ⊨ ⟨ π if s e { b e 0 } else { b e 1 } ω ⟩ ϕ . (103) (\mathcal{K},s_1,\beta) \models\langle\pi\ \text{if } se\ \{be_0\}\ \text{else }\{be_1\}\ \omega\rangle\phi. \tag*{(103)} ( K , s 1 , β ) ⊨ ⟨ π if se { b e 0 } else { b e 1 } ω ⟩ ϕ . (103)
We infer from the assumptions that
( K , s 1 , β ) ⊨ s e = T R U E → ⟨ π { b e 0 } ω ⟩ ϕ (104) (\mathcal{K},s_1,\beta) \models se = \mathrm{TRUE} \to\langle\pi\ \{be_0\}\ \omega\rangle\phi \tag*{(104)} ( K , s 1 , β ) ⊨ se = TRUE → ⟨ π { b e 0 } ω ⟩ ϕ (104)
( K , s 1 , β ) ⊨ s e = F A L S E → ⟨ π { b e 1 } ω ⟩ ϕ (105) (\mathcal{K},s_1,\beta) \models se = \mathrm{FALSE} \to\langle\pi\ \{be_1\}\ \omega\rangle\phi \tag*{(105)} ( K , s 1 , β ) ⊨ se = FALSE → ⟨ π { b e 1 } ω ⟩ ϕ (105)
We know that if s e se se { b e 0 } \{be_0\} { b e 0 } else { b e 1 } \{be_1\} { b e 1 } is the active expression. By the construction of the local evaluation rules, we know that it is also the next (sub-)expression to be evaluated and added to the trace s h sh s h . Thus, we can use the local evaluation. We split over the structure of s e se se .
Case G.1 (s e ∈ Val se \in\operatorname{Val} se ∈ Val ). Then s e se se is either true or false . We cover the former case as both are analogous.
s e = t r u e se = \mathrm{true} se = true : Then, val σ 1 ( t r u e ) = { ⟨ σ 1 ⟩ ⋅ K ( t t ) } \operatorname{val}_{\sigma_1}(\mathrm{true}) = \{\langle\sigma_1\rangle\cdot K(\mathrm{tt})\} val σ 1 ( true ) = {⟨ σ 1 ⟩ ⋅ K ( tt )} . From that we infer
val σ 1 ( if true { b e 0 } else { b e 1 } ) = { τ ⋅ K ( if e { b e 0 } else { b e 1 } ) ∣ τ ⋅ K ( e ) ∈ val σ 1 ( t r u e ) } = { ⟨ σ 1 ⟩ ⋅ K ( if t t { b e 0 } else { b e 1 } ) } \begin{aligned}
\operatorname{val}_{\sigma_1}(\text{if true }\{be_0\}\text{ else }\{be_1\})
&= \{\tau\cdot K(\text{if }e\ \{be_0\}\text{ else }\{be_1\}) \mid\tau\cdot K(e) \in\operatorname{val}_{\sigma_1}(\mathrm{true})\} \\
&= \{\langle\sigma_1\rangle\cdot K(\text{if }\mathrm{tt}\ \{be_0\}\text{ else }\{be_1\})\}
\end{aligned} val σ 1 ( if true { b e 0 } else { b e 1 }) = { τ ⋅ K ( if e { b e 0 } else { b e 1 }) ∣ τ ⋅ K ( e ) ∈ val σ 1 ( true )} = {⟨ σ 1 ⟩ ⋅ K ( if tt { b e 0 } else { b e 1 })} Further, val σ 1 ( if t t { b e 0 } else { b e 1 } ) = { ⟨ σ 1 ⟩ ⋅ K ( { b e 0 } ) } \operatorname{val}_{\sigma_1}(\text{if }\mathrm{tt}\ \{be_0\}\text{ else }\{be_1\}) = \{\langle\sigma_1\rangle\cdot K(\{be_0\})\} val σ 1 ( if tt { b e 0 } else { b e 1 }) = {⟨ σ 1 ⟩ ⋅ K ({ b e 0 })} . Hence, the only possible composition is
⟨ σ 1 ⟩ , K ( if true { b e 0 } else { b e 1 } ) → ⟨ σ 1 ⟩ , K ( if t t { b e 0 } else { b e 1 } ) → ⟨ σ 1 ⟩ , K ( { b e 0 } ) \langle\sigma_1\rangle, K(\text{if true }\{be_0\}\text{ else }\{be_1\}) \to\langle\sigma_1\rangle, K(\text{if }\mathrm{tt}\ \{be_0\}\text{ else }\{be_1\}) \to\langle\sigma_1\rangle, K(\{be_0\}) ⟨ σ 1 ⟩ , K ( if true { b e 0 } else { b e 1 }) → ⟨ σ 1 ⟩ , K ( if tt { b e 0 } else { b e 1 }) → ⟨ σ 1 ⟩ , K ({ b e 0 })
which ends in the start configuration of Tr ( π { b e 0 } ω , σ 1 ) \operatorname{Tr}(\pi\ \{be_0\}\ \omega,\sigma_1) Tr ( π { b e 0 } ω , σ 1 ) , i.e., we have the relation Tr ( { b e 0 } , σ 1 ) ⪰ Tr ( i f s e { b e 0 } e l s e { b e 1 } , σ 1 ) \operatorname{Tr}(\{be_0\},\sigma_1) \succeq\operatorname{Tr}(\mathop{\mathrm{if}}\ se\ \{be_0\}\ \mathop{\mathrm{else}}\ \{be_1\},\sigma_1) Tr ({ b e 0 } , σ 1 ) ⪰ Tr ( if se { b e 0 } else { b e 1 } , σ 1 ) . Thus, we simplify (103) to
( K , s 1 , β ) ⊨ ⟨ π { b e 0 } ω ⟩ ϕ . (106) (\mathcal{K},s_1,\beta) \models\langle\pi\ \{be_0\}\ \omega\rangle\phi. \tag*{(106)} ( K , s 1 , β ) ⊨ ⟨ π { b e 0 } ω ⟩ ϕ . (106)
Assumption (104) can be simplified, by the definition of the implication, to
( K , s 1 , β ) ⊭ s e ≐ T R U E or ( K , s 1 , β ) ⊨ ⟨ π { b e 0 } ω ⟩ ϕ (107) (\mathcal{K},s_1,\beta) \not\models se \doteq TRUE \quad\text{or} \quad(\mathcal{K},s_1,\beta) \models\langle\pi\ \{be_0\}\ \omega\rangle\phi \tag*{(107)} ( K , s 1 , β ) ⊨ se ≐ T R U E or ( K , s 1 , β ) ⊨ ⟨ π { b e 0 } ω ⟩ ϕ (107)
Since s e = true se = \text{true} se = true , we know that ( K , s 1 , β ) ⊨ s e ≐ T R U E (\mathcal{K},s_1,\beta) \models se \doteq TRUE ( K , s 1 , β ) ⊨ se ≐ T R U E , which means with (107) and Lemma 6.11 that the goal (106) holds.
Case G.2 (s e ∈ V a r se \in Var se ∈ V a r ). Let v = σ 1 ( s e ) v = \sigma_1(se) v = σ 1 ( se ) . Since s e se se is not symbolic, v ∈ V a l v \in Val v ∈ V a l . Then val σ 1 ( s e ) = { ⟨ σ 1 ⟩ ⋅ K ( v ) } \operatorname{val}_{\sigma_1}(se) = \{\langle\sigma_1\rangle\cdot\mathcal{K}(v)\} val σ 1 ( se ) = {⟨ σ 1 ⟩ ⋅ K ( v )} , because s e se se is of type bool \text{bool} bool , which implements Copy \text{Copy} Copy . We present the case for v = t t v = \mathsf{tt} v = tt , as the case v = f f v = \mathsf{ff} v = ff is similar. Then the local evaluation yields
val σ 1 ( i f s e { b e 0 } e l s e { b e 1 } ) = { τ ⋅ K ( i f e { b e 0 } e l s e { b e 1 } ) ∣ τ ⋅ K ( e ) ∈ val σ 1 ( s e ) } = { ⟨ σ 1 ⟩ ⋅ K ( i f t t { b e 0 } e l s e { b e 1 } ) } \operatorname{val}_{\sigma_1}(\mathop{\mathrm{if}}\ se\ \{be_0\}\ \mathop{\mathrm{else}}\ \{be_1\})
= \{\tau\cdot\mathcal{K}(\mathop{\mathrm{if}}\ e\ \{be_0\}\ \mathop{\mathrm{else}}\ \{be_1\}) \mid\tau\cdot\mathcal{K}(e) \in\operatorname{val}_{\sigma_1}(se)\}
= \{\langle\sigma_1\rangle\cdot\mathcal{K}(\mathop{\mathrm{if}}\ \mathsf{tt}\ \{be_0\}\ \mathop{\mathrm{else}}\ \{be_1\})\} val σ 1 ( if se { b e 0 } else { b e 1 }) = { τ ⋅ K ( if e { b e 0 } else { b e 1 }) ∣ τ ⋅ K ( e ) ∈ val σ 1 ( se )} = {⟨ σ 1 ⟩ ⋅ K ( if tt { b e 0 } else { b e 1 })}
As in case G.1, we know that the only possible composition is
⟨ σ 1 ⟩ , K ( i f s e { b e 0 } e l s e { b e 1 } ) → ⟨ σ 1 ⟩ , K ( i f t t { b e 0 } e l s e { b e 1 } ) → ⟨ σ 1 ⟩ , K ( { b e 0 } ) . \langle\sigma_1\rangle,\mathcal{K}(\mathop{\mathrm{if}}\ se\ \{be_0\}\ \mathop{\mathrm{else}}\ \{be_1\}) \to\langle\sigma_1\rangle,\mathcal{K}(\mathop{\mathrm{if}}\ \mathsf{tt}\ \{be_0\}\ \mathop{\mathrm{else}}\ \{be_1\}) \to\langle\sigma_1\rangle,\mathcal{K}(\{be_0\}). ⟨ σ 1 ⟩ , K ( if se { b e 0 } else { b e 1 }) → ⟨ σ 1 ⟩ , K ( if tt { b e 0 } else { b e 1 }) → ⟨ σ 1 ⟩ , K ({ b e 0 }) .
By the same argument as in case G.1, goal (103) holds.
Theorem G.2 (Soundness of Rule move). Let Γ , Δ ∈ 2 D L F m l \Gamma,\Delta\in2^{\mathrm{DLFml}} Γ , Δ ∈ 2 DLFml be sets of formulas, U ∈ U p d \mathcal{U} \in Upd U ∈ U p d be an update, x x x be a program variable whose type is not Copy \text{Copy} Copy and not a mutable reference, y y y be a program variable of the same type as x x x , c c c a constant of y y y ’s type that does not occur in Γ , Δ , ϕ \Gamma,\Delta,\phi Γ , Δ , ϕ , or U \mathcal{U} U , π x = y ω \pi\ x = y\ \omega π x = y ω be a program fragment, and ϕ ∈ D L F m l \phi\in\mathrm{DLFml} ϕ ∈ DLFml be a formula. If
⊨ Γ ⟹ { U } { x : = y ∥ y : = c } ⟨ π ( ) ω ⟩ ϕ , Δ \models\Gamma\Longrightarrow\{\mathcal{U}\}\{x := y \mathbin{\|} y := c\}\langle\pi\ ()\ \omega\rangle\phi,\Delta ⊨ Γ ⟹ { U } { x := y ∥ y := c } ⟨ π ( ) ω ⟩ ϕ , Δ then the following holds:
⊨ Γ ⟹ { U } ⟨ π x = y ω ⟩ ϕ , Δ . \models\Gamma\Longrightarrow\{\mathcal{U}\}\langle\pi\ x = y\ \omega\rangle\phi,\Delta. ⊨ Γ ⟹ { U } ⟨ π x = y ω ⟩ ϕ , Δ. Proof. We assume
⊨ Γ ⟹ { U } { x : = y ∥ y : = c } ⟨ π ( ) ω ⟩ ϕ , Δ \models\Gamma\Longrightarrow\{\mathcal{U}\}\{x := y \mathbin{\|} y := c\}\langle\pi\ ()\ \omega\rangle\phi,\Delta ⊨ Γ ⟹ { U } { x := y ∥ y := c } ⟨ π ( ) ω ⟩ ϕ , Δ It is sufficient to show
( K , s 1 , β ) ⊨ ⟨ π x = y ω ⟩ ϕ . (108) (\mathcal{K},s_1,\beta) \models\langle\pi\ x = y\ \omega\rangle\phi. \tag*{(108)} ( K , s 1 , β ) ⊨ ⟨ π x = y ω ⟩ ϕ . (108) We infer from the assumptions that
( K , s 1 , β ) ⊨ { x : = y ∥ y : = c } ⟨ π ( ) ω ⟩ ϕ (109) (\mathcal{K},s_1,\beta) \models\{x := y \mathbin{\|} y := c\}\langle\pi\ ()\ \omega\rangle\phi \tag*{(109)} ( K , s 1 , β ) ⊨ { x := y ∥ y := c } ⟨ π ( ) ω ⟩ ϕ (109) Let s 2 = val K , s , β ( x : = y ∥ y : = c ) ( s 1 ) s_2 = \operatorname{val}_{\mathcal{K},s,\beta}(x := y \mathbin{\|} y := c)(s_1) s 2 = val K , s , β ( x := y ∥ y := c ) ( s 1 ) . Then we simplify equation (109) to
( K , s 2 , β ) ⊨ ⟨ π ( ) ω ⟩ ϕ (110) (\mathcal{K},s_2,\beta) \models\langle\pi\ ()\ \omega\rangle\phi \tag*{(110)} ( K , s 2 , β ) ⊨ ⟨ π ( ) ω ⟩ ϕ (110) Let v = σ 1 ( y ) v = \sigma_1(y) v = σ 1 ( y ) . Following § 3.1.1 and then rule (5) we have the local evaluation
val σ 1 ( x = y ) = { τ ⋅ K ( x = e ) ∣ τ ⋅ K ( e ) ∈ val σ 1 ( y ) } = { ⟨ σ 1 ⟩ ⋅ K ( x = v ) } (111) \operatorname{val}_{\sigma_1}(x = y) = \{\tau\cdot\mathcal{K}(x = e) \mid\tau\cdot\mathcal{K}(e) \in\operatorname{val}_{\sigma_1}(y)\} = \{\langle\sigma_1\rangle\cdot\mathcal{K}(x = v)\} \tag*{(111)} val σ 1 ( x = y ) = { τ ⋅ K ( x = e ) ∣ τ ⋅ K ( e ) ∈ val σ 1 ( y )} = {⟨ σ 1 ⟩ ⋅ K ( x = v )} (111)
The local evaluation rule (19) further yields val σ 1 ( x = v ) = { ⟨ σ 1 ⟩ ↷ σ 1 [ x ↦ v ] ⋅ K ( ( ) ) } \operatorname{val}_{\sigma_1}(x=v)=\{\langle\sigma_1\rangle\curvearrowright\sigma_1[x\mapsto v]\cdot K(())\} val σ 1 ( x = v ) = {⟨ σ 1 ⟩ ↷ σ 1 [ x ↦ v ] ⋅ K (( ))} . Therefore, the only possible composition is
⟨ σ 1 ⟩ , K ( π x = y ω ) → ⟨ σ 1 ⟩ , K ( π x = v ω ) → ⟨ σ 1 ⟩ ↷ σ 1 [ x ↦ v ] , K ( π ( ) ω ) \langle\sigma_1\rangle,K(\pi\ x=y\ \omega)\to\langle\sigma_1\rangle,K(\pi\ x=v\ \omega)\to\langle\sigma_1\rangle\curvearrowright\sigma_1[x\mapsto v],K(\pi\ ()\ \omega) ⟨ σ 1 ⟩ , K ( π x = y ω ) → ⟨ σ 1 ⟩ , K ( π x = v ω ) → ⟨ σ 1 ⟩ ↷ σ 1 [ x ↦ v ] , K ( π ( ) ω )
Which implies that Tr ( π ( ) ω , σ 1 [ x ↦ v ] ) ≽ Tr ( π x = y ω , σ 1 ) \operatorname{Tr}(\pi\ ()\ \omega,\sigma_1[x\mapsto v])\succcurlyeq\operatorname{Tr}(\pi\ x=y\ \omega,\sigma_1) Tr ( π ( ) ω , σ 1 [ x ↦ v ]) ≽ Tr ( π x = y ω , σ 1 ) . By Lemma 6.11 , we know that goal (106) holds if ( K , s 3 , β ) ⊨ ⟨ π ( ) ω ⟩ ϕ (K,s_3,\beta)\models\langle\pi\ ()\ \omega\rangle\phi ( K , s 3 , β ) ⊨ ⟨ π ( ) ω ⟩ ϕ holds, with s 3 = val K , s 2 , β ( x : = v ) ( s 1 ) s_3=\operatorname{val}_{K,s_2,\beta}(x:=v)(s_1) s 3 = val K , s 2 , β ( x := v ) ( s 1 ) . Assumption (108) gives us a stronger formula than this: It gives us validity of ⟨ π ( ) ω ⟩ ϕ \langle\pi\ ()\ \omega\rangle\phi ⟨ π ( ) ω ⟩ ϕ with no information about y y y 's value. Thus, by (108), we are done. □
Γ , { U } inU32 ( s e 1 + s e 2 ) ⟹ { U } { x : = s e 1 + s e 2 } ⟨ π ( ) ω ⟩ ϕ , Δ Γ , { U } ¬ inU32 ( s e 1 + s e 2 ) ⟹ { U ⟨ π panic! ( ) ω ⟩ ϕ } , Δ Γ ⟹ { U } ⟨ π x = s e 1 + s e 2 ω ⟩ ϕ , Δ \begin{aligned}
\Gamma,\{U\}\operatorname{inU32}(se_1+se_2)&\Longrightarrow\{U\}\{x:=se_1+se_2\}\langle\pi\ ()\ \omega\rangle\phi,\Delta\\
\Gamma,\{U\}\neg\operatorname{inU32}(se_1+se_2)&\Longrightarrow\{U\langle\pi\ \operatorname{panic!}()\ \omega\rangle\phi\},\Delta\\
\Gamma&\Longrightarrow\{U\}\langle\pi\ x=se_1+se_2\ \omega\rangle\phi,\Delta
\end{aligned} Γ , { U } inU32 ( s e 1 + s e 2 ) Γ , { U } ¬ inU32 ( s e 1 + s e 2 ) Γ ⟹ { U } { x := s e 1 + s e 2 } ⟨ π ( ) ω ⟩ ϕ , Δ ⟹ { U ⟨ π panic! ( ) ω ⟩ ϕ } , Δ ⟹ { U } ⟨ π x = s e 1 + s e 2 ω ⟩ ϕ , Δ
Theorem G.3 (Soundness of Rule assignAddU32). Let Γ , Δ ∈ 2 D L F m l \Gamma,\Delta\in2^{\mathrm{DLFml}} Γ , Δ ∈ 2 DLFml be sets of formulas, U ∈ U p d U\in\mathrm{Upd} U ∈ Upd be an update, x x x be a program variable of type u32 , s e 1 , s e 2 se_1,se_2 s e 1 , s e 2 either literals or program variables of type u32 , π x = s e 1 + s e 2 ω \pi\ x=se_1+se_2\ \omega π x = s e 1 + s e 2 ω be a program fragment, and ϕ ∈ D L F m l \phi\in\mathrm{DLFml} ϕ ∈ DLFml be a formula. If
⊨ Γ , { U } inU32 ( s e 1 + s e 2 ) ⟹ { U } { x : = s e 1 + s e 2 } ⟨ π ( ) ω ⟩ ϕ , Δ \models\Gamma,\{U\}\operatorname{inU32}(se_1+se_2)\Longrightarrow\{U\}\{x:=se_1+se_2\}\langle\pi\ ()\ \omega\rangle\phi,\Delta ⊨ Γ , { U } inU32 ( s e 1 + s e 2 ) ⟹ { U } { x := s e 1 + s e 2 } ⟨ π ( ) ω ⟩ ϕ , Δ ⊨ Γ , { U } ¬ inU32 ( s e 1 + s e 2 ) ⟹ { U ⟨ π panic! ( ) ω ⟩ ϕ } , Δ \models\Gamma,\{U\}\neg\operatorname{inU32}(se_1+se_2)\Longrightarrow\{U\langle\pi\ \operatorname{panic!}()\ \omega\rangle\phi\},\Delta ⊨ Γ , { U } ¬ inU32 ( s e 1 + s e 2 ) ⟹ { U ⟨ π panic! ( ) ω ⟩ ϕ } , Δ then the following holds:
⊨ Γ ⟹ { U } ⟨ π x = s e 1 + s e 2 ω ⟩ ϕ , Δ . \models\Gamma\Longrightarrow\{U\}\langle\pi\ x=se_1+se_2\ \omega\rangle\phi,\Delta. ⊨ Γ ⟹ { U } ⟨ π x = s e 1 + s e 2 ω ⟩ ϕ , Δ.
Proof. We only present the case where s e 1 , s e 2 ∈ V a l se_1,se_2\in\mathrm{Val} s e 1 , s e 2 ∈ Val . The other two cases are handled as in the above proofs.
We need to show
( K , s 1 , β ) ⊨ ⟨ π x = s e 1 + s e 2 ω ⟩ ϕ (112) (K,s_1,\beta)\models\langle\pi\ x=se_1+se_2\ \omega\rangle\phi \tag*{(112)} ( K , s 1 , β ) ⊨ ⟨ π x = s e 1 + s e 2 ω ⟩ ϕ (112)
We infer from the assumptions that
( K , s 1 , β ) ⊨ inU32 ( s e 1 + s e 2 ) → { x : = s e 1 + s e 2 } ⟨ π ( ) ω ⟩ ϕ (113) (K,s_1,\beta)\models\operatorname{inU32}(se_1+se_2)\to\{x:=se_1+se_2\}\langle\pi\ ()\ \omega\rangle\phi \tag*{(113)} ( K , s 1 , β ) ⊨ inU32 ( s e 1 + s e 2 ) → { x := s e 1 + s e 2 } ⟨ π ( ) ω ⟩ ϕ (113)
( K , s 1 , β ) ⊨ ¬ inU32 ( s e 1 + s e 2 ) → ⟨ π panic! ( ) ω ⟩ ϕ (114) (K,s_1,\beta)\models\neg\operatorname{inU32}(se_1+se_2)\to\langle\pi\ \operatorname{panic!}()\ \omega\rangle\phi \tag*{(114)} ( K , s 1 , β ) ⊨ ¬ inU32 ( s e 1 + s e 2 ) → ⟨ π panic! ( ) ω ⟩ ϕ (114)
Let σ 1 \sigma_1 σ 1 be an LAGC state such that s 1 ∼ σ 1 s_1\sim\sigma_1 s 1 ∼ σ 1 . Further, let n = val K , s 1 , β ( s e 1 ) + val K , s 1 , β ( s e 2 ) n=\operatorname{val}_{K,s_1,\beta}(se_1)+\operatorname{val}_{K,s_1,\beta}(se_2) n = val K , s 1 , β ( s e 1 ) + val K , s 1 , β ( s e 2 ) . By the semantics if inU32 \operatorname{inU32} inU32 , we know that ( K , s 1 , β ) ⊨ inU32 ( s e 1 + s e 2 ) (K,s_1,\beta)\models\operatorname{inU32}(se_1+se_2) ( K , s 1 , β ) ⊨ inU32 ( s e 1 + s e 2 ) iff 0 ≤ n < 2 32 0\le n<2^{32} 0 ≤ n < 2 32 .
Case G.3 (inU32 ( n ) \operatorname{inU32}(n) inU32 ( n ) holds). Then the first case of rule (12) holds and the evaluation yields
val σ 1 ( s e 1 + s e 2 ) = { ⟨ σ 1 ⟩ ⋅ K ( n ) } \operatorname{val}_{\sigma_1}(se_1+se_2)=\{\langle\sigma_1\rangle\cdot K(n)\} val σ 1 ( s e 1 + s e 2 ) = {⟨ σ 1 ⟩ ⋅ K ( n )}
which makes the local evaluation of the assignment
val σ 1 ( x = s e 1 + s e 2 ) = { ⟨ σ 1 ⟩ ⋅ K ( x = n ) } (115) \operatorname{val}_{\sigma_1}(x=se_1+se_2)=\{\langle\sigma_1\rangle\cdot K(x=n)\} \tag*{(115)} val σ 1 ( x = s e 1 + s e 2 ) = {⟨ σ 1 ⟩ ⋅ K ( x = n )} (115)
val σ 1 ( x = n ) = { ⟨ σ 1 ⟩ ↷ σ 1 [ x ↦ n ] ⋅ K ( ( ) ) } (116) \operatorname{val}_{\sigma_1}(x=n)=\{\langle\sigma_1\rangle\curvearrowright\sigma_1[x\mapsto n]\cdot K(())\} \tag*{(116)} val σ 1 ( x = n ) = {⟨ σ 1 ⟩ ↷ σ 1 [ x ↦ n ] ⋅ K (( ))} (116)
Hence, the only possible composition is
⟨ σ 1 ⟩ , K ( x = s e 1 + s e 2 ) → ⟨ σ 1 ⟩ , K ( x = n ) → ⟨ σ 1 ⟩ ↷ σ 1 [ x ↦ n ] , K ( ( ) ) \langle\sigma_1\rangle,K(x=se_1+se_2)\to\langle\sigma_1\rangle,K(x=n)\to\langle\sigma_1\rangle\curvearrowright\sigma_1[x\mapsto n],K(()) ⟨ σ 1 ⟩ , K ( x = s e 1 + s e 2 ) → ⟨ σ 1 ⟩ , K ( x = n ) → ⟨ σ 1 ⟩ ↷ σ 1 [ x ↦ n ] , K (( ))
Since s 2 ∼ σ 2 s_2\sim\sigma_2 s 2 ∼ σ 2 , Tr ( π ( ) ω , σ 2 ) ≽ Tr ( π x = s e 1 + s e 2 ω , σ 1 ) \operatorname{Tr}(\pi\ ()\ \omega,\sigma_2)\succcurlyeq\operatorname{Tr}(\pi\ x=se_1+se_2\ \omega,\sigma_1) Tr ( π ( ) ω , σ 2 ) ≽ Tr ( π x = s e 1 + s e 2 ω , σ 1 ) . Hence, with Lemma 6.11 , we simplify the goal to
( K , s 2 , β ) ⊨ ⟨ π ( ) ω ⟩ ϕ (117) (K,s_2,\beta)\models\langle\pi\ ()\ \omega\rangle\phi \tag*{(117)} ( K , s 2 , β ) ⊨ ⟨ π ( ) ω ⟩ ϕ (117)
As ( K , s 1 , β ) ⊨ inU32 ( s e 1 + s e 2 ) (K,s_1,\beta)\models\operatorname{inU32}(se_1+se_2) ( K , s 1 , β ) ⊨ inU32 ( s e 1 + s e 2 ) , we can simplify assumption (111) to (115) Case G.4 (inU32(n n n ) does not hold). Then the second case of rule (12) holds and the evaluation yields
val σ 1 ( s e 1 + s e 2 ) = { ⟨ σ 1 ⟩ ⋅ K ( panic! ( ) ) } \operatorname{val}_{\sigma_1}(se_1 + se_2) = \{\langle\sigma_1\rangle\cdot K(\operatorname{panic!}())\} val σ 1 ( s e 1 + s e 2 ) = {⟨ σ 1 ⟩ ⋅ K ( panic! ( ))}
And val σ 1 ( panic! ( ) ) = { ⟨ σ 1 ⟩ ⇝ p a n i c E v ( ) ⇝ σ 1 ⋅ K ( ( ) ) } \operatorname{val}_{\sigma_1}(\operatorname{panic!}()) = \{\langle\sigma_1\rangle\mathrel{\leadsto} panicEv() \mathrel{\leadsto} \sigma_1 \cdot K(())\} val σ 1 ( panic! ( )) = {⟨ σ 1 ⟩ ⇝ p ani c E v ( ) ⇝ σ 1 ⋅ K (( ))} , which means that we have the suffix relation Tr ( π panic! ( ) ω , σ 1 ) ⪰ Tr ( π s e 1 + s e 2 ω ) \operatorname{Tr}(\pi\ \operatorname{panic!}()\ \omega,\sigma_1) \succeq\operatorname{Tr}(\pi\ se_1 + se_2\ \omega) Tr ( π panic! ( ) ω , σ 1 ) ⪰ Tr ( π s e 1 + s e 2 ω ) . With ( K , s 1 , β ) ⊨ ¬ i n U 32 ( n ) (\mathcal{K},s_1,\beta) \models\neg inU32(n) ( K , s 1 , β ) ⊨ ¬ in U 32 ( n ) , we simplify assumption (114) to
( K , s 1 , β ) ⊨ ⟨ π panic! ( ) ω ⟩ ϕ (118) (\mathcal{K},s_1,\beta) \models\langle\pi\ \operatorname{panic!}()\ \omega\rangle\phi \tag*{(118)} ( K , s 1 , β ) ⊨ ⟨ π panic! ( ) ω ⟩ ϕ (118)
which, by Lemma 6.11 , gives us (112) .
Γ ⟹ { U } { x : = s r e f ( y ) } ⟨ π ( ) ω ⟩ ϕ , Δ Γ ⟹ { U } ⟨ π x = & y ω ⟩ ϕ , Δ borrowShared \frac{\Gamma\Longrightarrow\{U\}\{x := sref(y)\}\langle\pi\ ()\ \omega\rangle\phi,\Delta}{\Gamma\Longrightarrow\{U\}\langle\pi\ x = \&y\ \omega\rangle\phi,\Delta}
\qquad\text{borrowShared} Γ ⟹ { U } ⟨ π x = & y ω ⟩ ϕ , Δ Γ ⟹ { U } { x := sr e f ( y )} ⟨ π ( ) ω ⟩ ϕ , Δ borrowShared
Theorem G.4 (Soundness of Rule borrowShared). Let Γ , Δ ∈ 2 D L F m l \Gamma,\Delta\in2^{\mathrm{DLFml}} Γ , Δ ∈ 2 DLFml be sets of formulas, U ∈ U p d U \in\mathrm{Upd} U ∈ Upd be an update, x x x be a program variable of some immutable reference type, y y y a program variable, π x = & y ω \pi\ x = \&y\ \omega π x = & y ω be a program fragment, and ϕ ∈ D L F m l \phi\in\mathrm{DLFml} ϕ ∈ DLFml be a formula. If
⊨ Γ ⟹ { U } { x : = s r e f ( y ) } ⟨ π ( ) ω ⟩ ϕ , Δ \models\Gamma\Longrightarrow\{U\}\{x := sref(y)\}\langle\pi\ ()\ \omega\rangle\phi,\Delta ⊨ Γ ⟹ { U } { x := sr e f ( y )} ⟨ π ( ) ω ⟩ ϕ , Δ then the following holds:
⊨ Γ ⟹ { U } ⟨ π x = & y ω ⟩ ϕ , Δ . \models\Gamma\Longrightarrow\{U\}\langle\pi\ x = \&y\ \omega\rangle\phi,\Delta. ⊨ Γ ⟹ { U } ⟨ π x = & y ω ⟩ ϕ , Δ.
Proof. We assume
⊨ Γ ⟹ { U } { x : = s r e f ( y ) } ⟨ π ( ) ω ⟩ ϕ , Δ (119) \models\Gamma\Longrightarrow\{U\}\{x := sref(y)\}\langle\pi\ ()\ \omega\rangle\phi,\Delta \tag*{(119)} ⊨ Γ ⟹ { U } { x := sr e f ( y )} ⟨ π ( ) ω ⟩ ϕ , Δ (119)
We only need to show
( K , s 1 , β ) ⊨ ⟨ π x = & y ω ⟩ ϕ . (120) (\mathcal{K},s_1,\beta) \models\langle\pi\ x = \&y\ \omega\rangle\phi. \tag*{(120)} ( K , s 1 , β ) ⊨ ⟨ π x = & y ω ⟩ ϕ . (120)
We infer from the assumptions that
( K , s 1 , β ) ⊨ { x : = s r e f ( y ) } ⟨ π ( ) ω ⟩ ϕ (121) (\mathcal{K},s_1,\beta) \models\{x := sref(y)\}\langle\pi\ ()\ \omega\rangle\phi \tag*{(121)} ( K , s 1 , β ) ⊨ { x := sr e f ( y )} ⟨ π ( ) ω ⟩ ϕ (121)
Let s 2 = val K , s 1 , β ( x : = s r e f ( y ) ) ( s 1 ) s_2 = \operatorname{val}_{\mathcal{K},s_1,\beta}(x := sref(y))(s_1) s 2 = val K , s 1 , β ( x := sr e f ( y )) ( s 1 ) . Then we can transform (121) to
( K , s 2 , β ) ⊨ ⟨ π ( ) ω ⟩ ϕ (122) (\mathcal{K},s_2,\beta) \models\langle\pi\ ()\ \omega\rangle\phi \tag*{(122)} ( K , s 2 , β ) ⊨ ⟨ π ( ) ω ⟩ ϕ (122)
We know that x = & y x = \&y x = & y is the active expression. By the construction of the local evaluation rules, we know that it is also a (sub-)expression to be evaluated and added to the trace s h sh s h . Thus we can use the local evaluation.
Let v = σ 1 ( y ) v = \sigma_1(y) v = σ 1 ( y ) . We know val σ 1 ( y ) = { ⟨ σ 1 ⟩ ⋅ K ( v ) } \operatorname{val}_{\sigma_1}(y) = \{\langle\sigma_1\rangle\cdot K(v)\} val σ 1 ( y ) = {⟨ σ 1 ⟩ ⋅ K ( v )} . Further,
val σ 1 ( & y ) = { ⟨ σ 1 ⟩ ⋅ K ( Ref y s ( b ) ) ∣ b ∈ B I d } \operatorname{val}_{\sigma_1}(\&y) = \{\langle\sigma_1\rangle\cdot K(\operatorname{Ref}^{s}_{y}(b)) \mid b \in BId\} val σ 1 ( & y ) = {⟨ σ 1 ⟩ ⋅ K ( Ref y s ( b )) ∣ b ∈ B I d }
val σ 1 ( x = & y ) = { τ ⋅ K ( x = e ) ∣ τ ⋅ K ( e ) ∈ val σ 1 ( & y ) } = { ⟨ σ 1 ⟩ ⋅ K ( x = Ref y s ( b ) ) ∣ b ∈ B I d } \begin{aligned}
\operatorname{val}_{\sigma_1}(x = \&y) &= \{\tau\cdot K(x = e) \mid\tau\cdot K(e) \in\operatorname{val}_{\sigma_1}(\&y)\} \\
&= \{\langle\sigma_1\rangle\cdot K(x = \operatorname{Ref}^{s}_{y}(b)) \mid b \in BId\}
\end{aligned} val σ 1 ( x = & y ) = { τ ⋅ K ( x = e ) ∣ τ ⋅ K ( e ) ∈ val σ 1 ( & y )} = {⟨ σ 1 ⟩ ⋅ K ( x = Ref y s ( b )) ∣ b ∈ B I d }
val σ 1 ( x = Ref y s ( b ) ) = { ⟨ σ 1 ⟩ ⇝ σ 1 [ x ↦ Ref y s ( b ) ] ⋅ K ( ( ) ) ∣ b ∈ B I d } \operatorname{val}_{\sigma_1}(x = \operatorname{Ref}^{s}_{y}(b)) = \{\langle\sigma_1\rangle\mathrel{\leadsto} \sigma_1[x \mapsto\operatorname{Ref}^{s}_{y}(b)] \cdot K(()) \mid b \in BId\} val σ 1 ( x = Ref y s ( b )) = {⟨ σ 1 ⟩ ⇝ σ 1 [ x ↦ Ref y s ( b )] ⋅ K (( )) ∣ b ∈ B I d }
Hence, we know that the only possible compositions starting in state σ 1 \sigma_1 σ 1 have the shape
⟨ σ 1 ⟩ , K ( π x = & y ω ) → ⟨ σ 1 ⟩ , K ( x = Ref y s ( b 1 ) ) → ⟨ σ 1 ⟩ ⇝ σ 1 [ x ↦ Ref y s ( b 1 ) ] , K ( ( ) ) \langle\sigma_1\rangle,K(\pi\ x = \&y\ \omega) \to\langle\sigma_1\rangle,K(x = \operatorname{Ref}^{s}_{y}(b_1)) \to\langle\sigma_1\rangle\mathrel{\leadsto} \sigma_1[x \mapsto\operatorname{Ref}^{s}_{y}(b_1)],K(()) ⟨ σ 1 ⟩ , K ( π x = & y ω ) → ⟨ σ 1 ⟩ , K ( x = Ref y s ( b 1 )) → ⟨ σ 1 ⟩ ⇝ σ 1 [ x ↦ Ref y s ( b 1 )] , K (( ))
for b 1 ∈ B I d b_1 \in BId b 1 ∈ B I d concrete, fresh borrow identifiers.
We know that σ 1 [ x ↦ Ref y s ( b ) ] = σ 2 \sigma_1[x \mapsto\operatorname{Ref}^{s}_{y}(b)] = \sigma_2 σ 1 [ x ↦ Ref y s ( b )] = σ 2 , so we know that the suffix relation Tr ( π ( ) ω , σ 2 ) ⪰ Tr ( π x = & y ω , σ 1 ) \operatorname{Tr}(\pi\ ()\ \omega,\sigma_2) \succeq\operatorname{Tr}(\pi\ x = \&y\ \omega,\sigma_1) Tr ( π ( ) ω , σ 2 ) ⪰ Tr ( π x = & y ω , σ 1 ) . Therefore, by Lemma 6.11 and assumption (122) , we have (120) . □
:::
Γ ⟹ { U } { x : = ∗ s ( y ) } ⟨ π ( ) ω ⟩ ϕ , Δ Γ ⟹ { U } ⟨ π x = ∗ y ω ⟩ ϕ , Δ \begin{array}{c}
\Gamma\Longrightarrow\{U\}\{x := *^{s}(y)\}\langle\pi()\ \omega\rangle\phi,\Delta\\
\hline
\Gamma\Longrightarrow\{U\}\langle\pi\ x = *y\ \omega\rangle\phi,\Delta
\end{array} Γ ⟹ { U } { x := ∗ s ( y )} ⟨ π ( ) ω ⟩ ϕ , Δ Γ ⟹ { U } ⟨ π x = ∗ y ω ⟩ ϕ , Δ
x : A , y : Ref A s , A x : A, y : \operatorname{Ref}^{s}_{A}, A x : A , y : Ref A s , A is Copy
Theorem G.5 (Soundness of Rule derefShared). Let Γ , Δ ∈ 2 D L F m l \Gamma,\Delta\in2^{\mathrm{DLFml}} Γ , Δ ∈ 2 DLFml be sets of formulas, U ∈ U p d U \in\mathrm{Upd} U ∈ Upd be an update, x x x be a program variable of type T \mathrm{T} T , y y y a program variable of type & T \&\mathrm{T} & T , π x = ∗ y ω \pi\ x = *y\ \omega π x = ∗ y ω be a program fragment, and ϕ ∈ D L F m l \phi\in\mathrm{DLFml} ϕ ∈ DLFml be a formula. If
⊨ Γ ⟹ { U } { x : = ∗ s ( y ) } ⟨ π ( ) ω ⟩ ϕ , Δ \models\Gamma\Longrightarrow\{U\}\{x := *^{s}(y)\}\langle\pi()\ \omega\rangle\phi,\Delta ⊨ Γ ⟹ { U } { x := ∗ s ( y )} ⟨ π ( ) ω ⟩ ϕ , Δ then the following holds:
⊨ Γ ⟹ { U } ⟨ π x = ∗ y ω ⟩ ϕ , Δ . \models\Gamma\Longrightarrow\{U\}\langle\pi\ x = *y\ \omega\rangle\phi,\Delta. ⊨ Γ ⟹ { U } ⟨ π x = ∗ y ω ⟩ ϕ , Δ.
Proof. We assume
⊨ Γ ⟹ { U } { x : = ∗ s ( y ) } ⟨ π ( ) ω ⟩ ϕ , Δ (123) \models\Gamma\Longrightarrow\{U\}\{x := *^{s}(y)\}\langle\pi()\ \omega\rangle\phi,\Delta \tag*{(123)} ⊨ Γ ⟹ { U } { x := ∗ s ( y )} ⟨ π ( ) ω ⟩ ϕ , Δ (123)
It is sufficient to show
( K , s 1 , β ) ⊨ ⟨ π x = & y ω ⟩ ϕ . (124) (\mathcal{K},s_{1},\beta) \models\langle\pi\ x = \&y\ \omega\rangle\phi. \tag*{(124)} ( K , s 1 , β ) ⊨ ⟨ π x = & y ω ⟩ ϕ . (124)
We infer from the assumptions that
( K , s 1 , β ) ⊨ { x : = ∗ s ( y ) } ⟨ π ( ) ω ⟩ ϕ (125) (\mathcal{K},s_{1},\beta) \models\{x := *^{s}(y)\}\langle\pi()\ \omega\rangle\phi \tag*{(125)} ( K , s 1 , β ) ⊨ { x := ∗ s ( y )} ⟨ π ( ) ω ⟩ ϕ (125)
Let s 2 = val K , s 1 , β ( x : = ∗ s ( y ) ) ( s 1 ) s_{2} = \operatorname{val}_{\mathcal{K},s_{1},\beta}(x := *^{s}(y))(s_{1}) s 2 = val K , s 1 , β ( x := ∗ s ( y )) ( s 1 ) . Then we can transform (123) to
( K , s 2 , β ) ⊨ ⟨ π ( ) ω ⟩ ϕ (126) (\mathcal{K},s_{2},\beta) \models\langle\pi()\ \omega\rangle\phi \tag*{(126)} ( K , s 2 , β ) ⊨ ⟨ π ( ) ω ⟩ ϕ (126)
We know that x = ∗ y x = *y x = ∗ y is the active expression. By the construction of the local evaluation rules, we know that it is also a (sub-)expression to be evaluated and added to the trace s h sh s h . Thus we can use the local evaluation.
Let σ 1 \sigma_{1} σ 1 be an LAGC state such that s 1 ∼ σ 1 s_{1} \sim\sigma_{1} s 1 ∼ σ 1 , let Ref z s ( b ) = σ 1 ( y ) \operatorname{Ref}^{s}_{z}(b) = \sigma_{1}(y) Ref z s ( b ) = σ 1 ( y ) with some program variable z z z and some borrow Id b b b , and let v : = σ 1 ( z ) v := \sigma_{1}(z) v := σ 1 ( z ) . We know that val σ 1 ( y ) = { ⟨ σ 1 ⟩ ⋅ K ( Ref z s ( b ) ) } \operatorname{val}_{\sigma_{1}}(y) = \{\langle\sigma_{1}\rangle\cdot\mathsf{K}(\operatorname{Ref}^{s}_{z}(b))\} val σ 1 ( y ) = {⟨ σ 1 ⟩ ⋅ K ( Ref z s ( b ))} . Further,
val σ 1 ( ∗ y ) = { τ ⋅ K ( ∗ e ) ∣ τ ⋅ K ( e ) ∈ val σ 1 ( y ) } = { ⟨ σ 1 ⟩ ⋅ K ( ∗ Ref z s ( b ) ) } \begin{aligned}
\operatorname{val}_{\sigma_{1}}(*y) &= \{\tau\cdot\mathsf{K}(*e) \mid\tau\cdot\mathsf{K}(e) \in\operatorname{val}_{\sigma_{1}}(y)\}\\
&= \{\langle\sigma_{1}\rangle\cdot\mathsf{K}(*\operatorname{Ref}^{s}_{z}(b))\}
\end{aligned} val σ 1 ( ∗ y ) = { τ ⋅ K ( ∗ e ) ∣ τ ⋅ K ( e ) ∈ val σ 1 ( y )} = {⟨ σ 1 ⟩ ⋅ K ( ∗ Ref z s ( b ))}
val σ 1 ( ∗ Ref z s ( b ) ) = { ⟨ σ 1 ⟩ ⋅ K ( v ) } \operatorname{val}_{\sigma_{1}}(*\operatorname{Ref}^{s}_{z}(b)) = \{\langle\sigma_{1}\rangle\cdot\mathsf{K}(v)\} val σ 1 ( ∗ Ref z s ( b )) = {⟨ σ 1 ⟩ ⋅ K ( v )}
val σ 1 ( x = ∗ y ) = { τ ⋅ K ( x = e ) ∣ τ ⋅ K ( e ) ∈ val σ 1 ( ∗ y ) } = { ⟨ σ 1 ⟩ ⋅ K ( x = ∗ Ref z s ( b ) ) } \begin{aligned}
\operatorname{val}_{\sigma_{1}}(x = *y) &= \{\tau\cdot\mathsf{K}(x = e) \mid\tau\cdot\mathsf{K}(e) \in\operatorname{val}_{\sigma_{1}}(*y)\}\\
&= \{\langle\sigma_{1}\rangle\cdot\mathsf{K}(x = *\operatorname{Ref}^{s}_{z}(b))\}
\end{aligned} val σ 1 ( x = ∗ y ) = { τ ⋅ K ( x = e ) ∣ τ ⋅ K ( e ) ∈ val σ 1 ( ∗ y )} = {⟨ σ 1 ⟩ ⋅ K ( x = ∗ Ref z s ( b ))}
val σ 1 ( x = ∗ Ref z s ( b ) ) = { τ ⋅ K ( x = e ) ∣ τ ⋅ K ( e ) ∈ val σ 1 ( ∗ Ref z s ( b ) ) } = { ⟨ σ 1 ⟩ ⋅ K ( x = v ) } \begin{aligned}
\operatorname{val}_{\sigma_{1}}(x = *\operatorname{Ref}^{s}_{z}(b)) &= \{\tau\cdot\mathsf{K}(x = e) \mid\tau\cdot\mathsf{K}(e) \in\operatorname{val}_{\sigma_{1}}(*\operatorname{Ref}^{s}_{z}(b))\}\\
&= \{\langle\sigma_{1}\rangle\cdot\mathsf{K}(x = v)\}
\end{aligned} val σ 1 ( x = ∗ Ref z s ( b )) = { τ ⋅ K ( x = e ) ∣ τ ⋅ K ( e ) ∈ val σ 1 ( ∗ Ref z s ( b ))} = {⟨ σ 1 ⟩ ⋅ K ( x = v )}
val σ 1 ( x = v ) = { ⟨ σ 1 ⟩ ↷ σ 1 [ x ↦ v ] ⋅ K ( ( ) ) } \operatorname{val}_{\sigma_{1}}(x = v) = \{\langle\sigma_{1}\rangle\curvearrowright\sigma_{1}[x \mapsto v] \cdot\mathsf{K}(())\} val σ 1 ( x = v ) = {⟨ σ 1 ⟩ ↷ σ 1 [ x ↦ v ] ⋅ K (( ))}
Hence, we know that the only possible composition starting in state σ 1 \sigma_{1} σ 1 is
⟨ σ 1 ⟩ , K ( π x = ∗ y ω ) → ⟨ σ 1 ⟩ , K ( π x = ∗ Ref z s ( b ) ω ) → ⟨ σ 1 ⟩ , K ( π x = v ω ) → ⟨ σ 1 ⟩ ↷ σ 1 [ x ↦ v ] , K ( π ( ) ω ) . \langle\sigma_{1}\rangle,\mathsf{K}(\pi\ x = *y\ \omega) \to\langle\sigma_{1}\rangle,\mathsf{K}(\pi\ x = *\operatorname{Ref}^{s}_{z}(b)\ \omega)
\to\langle\sigma_{1}\rangle,\mathsf{K}(\pi\ x = v\ \omega) \to\langle\sigma_{1}\rangle\curvearrowright\sigma_{1}[x \mapsto v],\mathsf{K}(\pi()\ \omega). ⟨ σ 1 ⟩ , K ( π x = ∗ y ω ) → ⟨ σ 1 ⟩ , K ( π x = ∗ Ref z s ( b ) ω ) → ⟨ σ 1 ⟩ , K ( π x = v ω ) → ⟨ σ 1 ⟩ ↷ σ 1 [ x ↦ v ] , K ( π ( ) ω ) .
Let SharedRef ( w ) = s 1 ( y ) \operatorname{SharedRef}(w) = s_{1}(y) SharedRef ( w ) = s 1 ( y ) . We know that val K , s 1 , β ( ∗ s ( y ) ) = w \operatorname{val}_{\mathcal{K},s_{1},\beta}(*^{s}(y)) = w val K , s 1 , β ( ∗ s ( y )) = w , which implies s 2 ( x ) = w s_{2}(x) = w s 2 ( x ) = w . Since s 1 ∼ σ 1 s_{1} \sim\sigma_{1} s 1 ∼ σ 1 , SharedRef ( w ) \operatorname{SharedRef}(w) SharedRef ( w ) must correspond to Ref z s ( b ) \operatorname{Ref}^{s}_{z}(b) Ref z s ( b ) , which implies w ∼ σ 1 v w \sim_{\sigma_{1}} v w ∼ σ 1 v . Let σ 2 : = σ 1 [ x ↦ v ] \sigma_{2} := \sigma_{1}[x \mapsto v] σ 2 := σ 1 [ x ↦ v ] . Then, s 2 ∼ σ 2 s_{2} \sim\sigma_{2} s 2 ∼ σ 2 .
We know that Tr ( π ( ) ω , σ 2 ) ⊒ Tr ( π x = ∗ y ω , σ 1 ) \operatorname{Tr}(\pi()\ \omega,\sigma_{2}) \sqsupseteq\operatorname{Tr}(\pi\ x = *y\ \omega,\sigma_{1}) Tr ( π ( ) ω , σ 2 ) ⊒ Tr ( π x = ∗ y ω , σ 1 ) . Therefore, by Lemma 6.11 and assumption (124), we have (122). □
derefOfSharedRef
∗ s ( s r e f ( t ) ) ⇝ t *^{s}(sref(t)) \leadsto t ∗ s ( sr e f ( t )) ⇝ t
Theorem G.6 (Soundness and Completeness of Rule derefOfSharedRef). Let K = ( D , I , S , ρ ) \mathcal{K} = (\mathcal{D}, I, S, \rho) K = ( D , I , S , ρ ) be a Kripke structure, s ∈ S s \in S s ∈ S a state, and β \beta β a variable assignment. Then
val K , s , β ( ∗ s ( s r e f ( t ) ) ) = val K , s , β ( t ) \operatorname{val}_{\mathcal{K},s,\beta}(*^{s}(sref(t))) = \operatorname{val}_{\mathcal{K},s,\beta}(t) val K , s , β ( ∗ s ( sr e f ( t ))) = val K , s , β ( t )
Proof.
val K , s , β ( ∗ s ( s r e f ( t ) ) ) = I ( ∗ s ) ( val K , s , β ( s r e f ( t ) ) ) = I ( ∗ s ) ( I ( s r e f ) ( val K , s , β ( t ) ) ) = I ( ∗ s ) ( SharedRef ( val K , s , β ( t ) ) ) = val K , s , β ( t ) \begin{aligned}
\operatorname{val}_{\mathcal{K},s,\beta}(*^{s}(sref(t)))
&= I(*^{s})(\operatorname{val}_{\mathcal{K},s,\beta}(sref(t)))\\
&= I(*^{s})(I(sref)(\operatorname{val}_{\mathcal{K},s,\beta}(t)))\\
&= I(*^{s})(\operatorname{SharedRef}(\operatorname{val}_{\mathcal{K},s,\beta}(t)))\\
&= \operatorname{val}_{\mathcal{K},s,\beta}(t)
\end{aligned} val K , s , β ( ∗ s ( sr e f ( t ))) = I ( ∗ s ) ( val K , s , β ( sr e f ( t ))) = I ( ∗ s ) ( I ( sr e f ) ( val K , s , β ( t ))) = I ( ∗ s ) ( SharedRef ( val K , s , β ( t ))) = val K , s , β ( t )
borrowMut
Γ ⇒ { U } { x : = m r e f ( ∥ Y ∥ ) } ⟨ π ( ) ω ⟩ ϕ , Δ Γ ⇒ { U } ⟨ π x = & m u t y ω ⟩ ϕ , Δ \frac{\Gamma\mathrel{\Rightarrow} \{U\}\{x := mref(\lVert Y\rVert)\}\langle\pi()\ \omega\rangle\phi,\Delta}{\Gamma\mathrel{\Rightarrow} \{U\}\langle\pi\ x = \&mut\ y\ \omega\rangle\phi,\Delta} Γ ⇒ { U } ⟨ π x = & m u t y ω ⟩ ϕ , Δ Γ ⇒ { U } { x := m r e f (∥ Y ∥)} ⟨ π ( ) ω ⟩ ϕ , Δ
Theorem G.7 (Soundness of Rule borrowMut). Let Γ , Δ ∈ 2 D L F m l \Gamma,\Delta\in2^{\mathrm{DLFml}} Γ , Δ ∈ 2 DLFml be sets of formulas, U ∈ U p d U \in\mathrm{Upd} U ∈ Upd be an update, x x x be a program variable of some mutable reference type, y y y a program variable, π x = & m u t y ω \pi\ x = \&mut\ y\ \omega π x = & m u t y ω be a program fragment, and ϕ ∈ D L F m l \phi\in\mathrm{DLFml} ϕ ∈ DLFml be a formula. If
⊨ Γ ⇒ { U } { x : = m r e f ( ∥ Y ∥ ) } ⟨ π ( ) ω ⟩ ϕ , Δ \models\Gamma\mathrel{\Rightarrow} \{U\}\{x := mref(\lVert Y\rVert)\}\langle\pi()\ \omega\rangle\phi,\Delta ⊨ Γ ⇒ { U } { x := m r e f (∥ Y ∥)} ⟨ π ( ) ω ⟩ ϕ , Δ then the following holds:
⊨ Γ ⇒ { U } ⟨ π x = & m u t y ω ⟩ ϕ , Δ . \models\Gamma\mathrel{\Rightarrow} \{U\}\langle\pi\ x = \&mut\ y\ \omega\rangle\phi,\Delta. ⊨ Γ ⇒ { U } ⟨ π x = & m u t y ω ⟩ ϕ , Δ.
Proof. It suffices to show
( K , s 1 , β ) ⊨ ⟨ π x = & m u t y ω ⟩ ϕ . (127) (\mathcal{K},s_1,\beta) \models\langle\pi\ x = \&mut\ y\ \omega\rangle\phi. \tag*{(127)} ( K , s 1 , β ) ⊨ ⟨ π x = & m u t y ω ⟩ ϕ . (127)
We infer from the assumptions that
( K , s 1 , β ) ⊨ { x : = m r e f ( ∥ Y ∥ ) } ⟨ π ( ) ω ⟩ ϕ (128) (\mathcal{K},s_1,\beta) \models\{x := mref(\lVert Y\rVert)\}\langle\pi()\ \omega\rangle\phi \tag*{(128)} ( K , s 1 , β ) ⊨ { x := m r e f (∥ Y ∥)} ⟨ π ( ) ω ⟩ ϕ (128)
Let s 2 = val K , s 1 , β ( x : = m r e f ( ∥ Y ∥ ) ) ( s 1 ) s_2 = \operatorname{val}_{\mathcal{K},s_1,\beta}(x := mref(\lVert Y\rVert))(s_1) s 2 = val K , s 1 , β ( x := m r e f (∥ Y ∥)) ( s 1 ) . Then we can transform (128) to
( K , s 2 , β ) ⊨ ⟨ π ( ) ω ⟩ ϕ (129) (\mathcal{K},s_2,\beta) \models\langle\pi()\ \omega\rangle\phi \tag*{(129)} ( K , s 2 , β ) ⊨ ⟨ π ( ) ω ⟩ ϕ (129)
We know that x = & m u t y x = \&mut\ y x = & m u t y is the active expression. By the construction of the local evaluation rules, we know that it is also a (sub-)expression to be evaluated and added to the trace s h sh s h . Thus we can use the local evaluation.
Let σ 1 \sigma_1 σ 1 be an LAGC state such that s 1 ∼ σ 1 s_1 \sim\sigma_1 s 1 ∼ σ 1 . Let v = σ 1 ( y ) v = \sigma_1(y) v = σ 1 ( y ) . We know val σ 1 ( y ) = { ⟨ σ 1 ⟩ ⋅ K ( v ) } \operatorname{val}_{\sigma_1}(y) = \{\langle\sigma_1\rangle\cdot K(v)\} val σ 1 ( y ) = {⟨ σ 1 ⟩ ⋅ K ( v )} . Further,
val σ 1 ( & m u t y ) = { ⟨ σ 1 ⟩ ⋅ K ( Ref y m ( b ) ) ∣ b ∈ B I d } val σ 1 ( x = & m u t y ) = { τ ⋅ K ( x = e ) ∣ τ ⋅ K ( e ) ∈ val σ 1 ( & m u t y ) } = { ⟨ σ 1 ⟩ ⋅ K ( x = Ref y m ( b ) ) ∣ b ∈ B I d } val σ 1 ( x = Ref y m ( b ) ) = { ⟨ σ 1 ⟩ ↷ σ 1 [ x ↦ Ref y m ( b ) ] ⋅ K ( ( ) ) } \begin{aligned}
\operatorname{val}_{\sigma_1}(\&mut\ y) &= \{\langle\sigma_1\rangle\cdot K(\operatorname{Ref}^{m}_{y}(b)) \mid b \in BId\}\\
\operatorname{val}_{\sigma_1}(x = \&mut\ y) &= \{\tau\cdot K(x = e) \mid\tau\cdot K(e) \in\operatorname{val}_{\sigma_1}(\&mut\ y)\}\\
&= \{\langle\sigma_1\rangle\cdot K(x = \operatorname{Ref}^{m}_{y}(b)) \mid b \in BId\}\\
\operatorname{val}_{\sigma_1}(x = \operatorname{Ref}^{m}_{y}(b)) &= \{\langle\sigma_1\rangle\curvearrowright\sigma_1[x \mapsto\operatorname{Ref}^{m}_{y}(b)] \cdot K(())\}
\end{aligned} val σ 1 ( & m u t y ) val σ 1 ( x = & m u t y ) val σ 1 ( x = Ref y m ( b )) = {⟨ σ 1 ⟩ ⋅ K ( Ref y m ( b )) ∣ b ∈ B I d } = { τ ⋅ K ( x = e ) ∣ τ ⋅ K ( e ) ∈ val σ 1 ( & m u t y )} = {⟨ σ 1 ⟩ ⋅ K ( x = Ref y m ( b )) ∣ b ∈ B I d } = {⟨ σ 1 ⟩ ↷ σ 1 [ x ↦ Ref y m ( b )] ⋅ K (( ))}
Hence, we know that the only possible composition starting in state σ 1 \sigma_1 σ 1 is
⟨ σ 1 ⟩ , K ( π x = & m u t y ω ) → ⟨ σ 1 ⟩ , K ( x = Ref y m ( b ) ) → ⟨ σ 1 ⟩ ↷ σ 1 [ x ↦ Ref y m ( b ) ] , K ( ( ) ) . \langle\sigma_1 \rangle, \mathrm{K}(\pi\ x = \&\mathrm{mut}\ y\ \omega) \to\langle\sigma_1 \rangle, \mathrm{K}(x = \operatorname{Ref}^{m}_{y}(b)) \to\langle\sigma_1 \rangle\curvearrowright\sigma_1[x \mapsto\operatorname{Ref}^{m}_{y}(b)], \mathrm{K}(()) . ⟨ σ 1 ⟩ , K ( π x = & mut y ω ) → ⟨ σ 1 ⟩ , K ( x = Ref y m ( b )) → ⟨ σ 1 ⟩ ↷ σ 1 [ x ↦ Ref y m ( b )] , K (( )) .
Let σ 2 : = σ 1 [ x ↦ Ref y s ( b ) ] \sigma_2 := \sigma_1[x \mapsto\operatorname{Ref}^{s}_{y}(b)] σ 2 := σ 1 [ x ↦ Ref y s ( b )] . Then, Tr ( π ( ) ω , σ 2 ) ⪰ Tr ( π x = & y ω ) , σ 1 \operatorname{Tr}(\pi\ ()\ \omega,\sigma_2) \succeq\operatorname{Tr}(\pi\ x = \&y\ \omega),\sigma_1 Tr ( π ( ) ω , σ 2 ) ⪰ Tr ( π x = & y ω ) , σ 1 . We know s 2 ∼ σ 2 s_2 \sim\sigma_2 s 2 ∼ σ 2 . Therefore, by Lemma 6.11 and assumption (127), we have (125).
Γ ⟹ { U } { x ∗ ↦ s e } ⟨ π ( ) ω ⟩ ϕ , Δ Γ ⟹ { U } ⟨ π ∗ x = s e ω ⟩ ϕ , Δ \frac{\Gamma\Longrightarrow\{ \mathcal{U} \}\{x \mathrel{\ast\mapsto} se\}\langle\pi\ ()\ \omega\rangle\phi,\Delta}{\Gamma\Longrightarrow\{ \mathcal{U} \}\langle\pi\ \ast x = se\ \omega\rangle\phi,\Delta} Γ ⟹ { U } ⟨ π ∗ x = se ω ⟩ ϕ , Δ Γ ⟹ { U } { x ∗ ↦ se } ⟨ π ( ) ω ⟩ ϕ , Δ
Theorem G.8 (Soundness of Rule mutate). Let Γ , Δ ∈ 2 D L F m l \Gamma,\Delta\in2^{\mathrm{DLFml}} Γ , Δ ∈ 2 DLFml be sets of formulas, U ∈ U p d \mathcal{U} \in\mathrm{Upd} U ∈ Upd be an update, x x x be a program variable of type & m u t T \&\mathrm{mut}\ \mathrm{T} & mut T , s e se se a literal or a program variable of type T \mathrm{T} T , π ∗ x = s e ω \pi\ \ast x = se\ \omega π ∗ x = se ω be a program fragment, and ϕ ∈ D L F m l \phi\in\mathrm{DLFml} ϕ ∈ DLFml be a formula. If
⊨ Γ ⟹ { U } { x ∗ ↦ s e } ⟨ π ( ) ω ⟩ ϕ , Δ \models\Gamma\Longrightarrow\{ \mathcal{U} \}\{x \mathrel{\ast\mapsto} se\}\langle\pi\ ()\ \omega\rangle\phi,\Delta ⊨ Γ ⟹ { U } { x ∗ ↦ se } ⟨ π ( ) ω ⟩ ϕ , Δ then the following holds:
⊨ Γ ⟹ { U } ⟨ π ∗ x = s e ω ⟩ ϕ , Δ . \models\Gamma\Longrightarrow\{ \mathcal{U} \}\langle\pi\ \ast x = se\ \omega\rangle\phi,\Delta. ⊨ Γ ⟹ { U } ⟨ π ∗ x = se ω ⟩ ϕ , Δ. Proof. It suffices to show
( K , s 1 , β ) ⊨ ⟨ π ∗ x = s e ω ⟩ ϕ . (130) (\mathcal{K},s_1,\beta) \models\langle\pi\ \ast x = se\ \omega\rangle\phi. \tag*{(130)} ( K , s 1 , β ) ⊨ ⟨ π ∗ x = se ω ⟩ ϕ . (130) We infer from the assumptions that
( K , s 1 , β ) ⊨ { x ∗ ↦ s e } ⟨ π ( ) ω ⟩ ϕ (131) (\mathcal{K},s_1,\beta) \models\{x \mathrel{\ast\mapsto} se\}\langle\pi\ ()\ \omega\rangle\phi \tag*{(131)} ( K , s 1 , β ) ⊨ { x ∗ ↦ se } ⟨ π ( ) ω ⟩ ϕ (131) Let s 2 = val K , s 1 , β ( x ∗ ↦ s e ) ( s 1 ) s_2 = \operatorname{val}_{\mathcal{K},s_1,\beta}(x \mathrel{\ast\mapsto} se)(s_1) s 2 = val K , s 1 , β ( x ∗ ↦ se ) ( s 1 ) . Then we can transform (129) to
( K , s 2 , β ) ⊨ ⟨ π ( ) ω ⟩ ϕ (132) (\mathcal{K},s_2,\beta) \models\langle\pi\ ()\ \omega\rangle\phi \tag*{(132)} ( K , s 2 , β ) ⊨ ⟨ π ( ) ω ⟩ ϕ (132) We know that ∗ x = s e \ast x = se ∗ x = se is the active expression. By the construction of the local evaluation rules, we know that it is also a (sub-)expression to be evaluated and added to the trace s h sh s h . Thus we can use the local evaluation.
We only show the case where s e se se is a value. The case where it is a variable is analogous. Let σ 1 \sigma_1 σ 1 be an LAGC state such that s 1 ∼ σ 1 s_1 \sim\sigma_1 s 1 ∼ σ 1 . Let b b b and y y y be such that σ 1 ( x ) = Ref y m ( b ) \sigma_1(x) = \operatorname{Ref}^{m}_{y}(b) σ 1 ( x ) = Ref y m ( b ) . We know
val σ 1 ( ∗ x ) = { ⟨ σ 1 ⟩ ⋅ K ( y ) } \operatorname{val}_{\sigma_1}(\ast x) = \{\langle\sigma_1 \rangle\cdot\mathrm{K}(y)\} val σ 1 ( ∗ x ) = {⟨ σ 1 ⟩ ⋅ K ( y )} val σ 1 ( ∗ x = s e ) = { τ ⋅ K ( p e = s e ) ∣ τ ⋅ K ( p e ) ∈ val σ 1 ( ∗ x ) } = { ⟨ σ 1 ⟩ ⋅ K ( y = s e ) } \operatorname{val}_{\sigma_1}(\ast x = se) = \{\tau\cdot\mathrm{K}(pe = se) \mid\tau\cdot\mathrm{K}(pe) \in\operatorname{val}_{\sigma_1}(\ast x)\} = \{\langle\sigma_1 \rangle\cdot\mathrm{K}(y = se)\} val σ 1 ( ∗ x = se ) = { τ ⋅ K ( p e = se ) ∣ τ ⋅ K ( p e ) ∈ val σ 1 ( ∗ x )} = {⟨ σ 1 ⟩ ⋅ K ( y = se )} val σ 1 ( y = s e ) = { ⟨ σ 1 ⟩ ↷ σ 1 [ y ↦ s e ] ⋅ K ( ( ) ) } \operatorname{val}_{\sigma_1}(y = se) = \{\langle\sigma_1 \rangle\curvearrowright\sigma_1[y \mapsto se] \cdot\mathrm{K}(())\} val σ 1 ( y = se ) = {⟨ σ 1 ⟩ ↷ σ 1 [ y ↦ se ] ⋅ K (( ))} Therefore, the only possible composition is
⟨ σ 1 ⟩ , K ( ∗ x = s e ) → ⟨ σ 1 ⟩ , K ( y = s e ) → ⟨ σ 1 ⟩ ↷ σ 1 [ y ↦ s e ] , K ( ( ) ) \langle\sigma_1 \rangle, \mathrm{K}(\ast x = se) \to\langle\sigma_1 \rangle,\mathrm{K}(y = se) \to\langle\sigma_1 \rangle\curvearrowright\sigma_1[y \mapsto se],\mathrm{K}(()) ⟨ σ 1 ⟩ , K ( ∗ x = se ) → ⟨ σ 1 ⟩ , K ( y = se ) → ⟨ σ 1 ⟩ ↷ σ 1 [ y ↦ se ] , K (( )) Let σ 2 : = σ 1 [ y ↦ s e ] \sigma_2 := \sigma_1[y \mapsto se] σ 2 := σ 1 [ y ↦ se ] . Since val K , s 1 , β ( x ∗ ↦ s e ) ( s 1 ) ( y ) = s e \operatorname{val}_{\mathcal{K},s_1,\beta}(x \mathrel{\ast\mapsto} se)(s_1)(y) = se val K , s 1 , β ( x ∗ ↦ se ) ( s 1 ) ( y ) = se and is s 1 ( z ) s_1(z) s 1 ( z ) for all program variables other than y y y , we have s 2 ∼ σ 2 s_2 \sim\sigma_2 s 2 ∼ σ 2 .
Then, Tr ( π ( ) ω , σ 2 ) ⪰ Tr ( π ∗ x = s e ω , σ 1 ) \operatorname{Tr}(\pi\ ()\ \omega,\sigma_2) \succeq\operatorname{Tr}(\pi\ \ast x = se\ \omega,\sigma_1) Tr ( π ( ) ω , σ 2 ) ⪰ Tr ( π ∗ x = se ω , σ 1 ) , which with Lemma 6.11 and assumption (130), gives us goal (128). □
Loop Rules
The remaining rules deal with loops using loop scopes [61 ] . The intuitive semantics of a loop scope ↻ x e ↺ \circlearrowright_x e \circlearrowleft ↻ x e ↺ with index variable x x x and body e e e , is that e e e is evaluated. If the evaluation encounters an expression that exits the loop, such as a break or return , x x x is set to true ; the break ’s value becomes the loop scope’s value. Otherwise, x x x is set to false . Thereby, it models exactly one iteration of the loop and x x x models whether we exit the loop.
In terms of LAGC semantics, we define local rules that evaluate the body e e e as for normal blocks, except when the next sub-expression is a relevant expression, i.e., break , continue , or return .
When encountering an expression like break e e e in a loop scope, we have:
val σ ( ↻ x { b r e a k v ; s ˉ e } ↺ ) = { ⟨ σ ⟩ ⋅ K ( { x = t r u e ; e } ) } (133) \operatorname{val}_{\sigma}\left(\circlearrowright_x\{\mathbf{break}\ v;\ \bar{s}\ e\}\circlearrowleft\right)=\{\langle\sigma\rangle\cdot K(\{x=\mathbf{true};\ e\})\} \tag*{(133)} val σ ( ↻ x { break v ; s ˉ e } ↺ ) = {⟨ σ ⟩ ⋅ K ({ x = true ; e })} (133)
The local evaluation for continue sets the value of x x x to false and emits a c o n t i n u e \mathit{continue} continue event.
val σ ( ↻ x { c o n t i n u e ; s ˉ e 2 } ↺ ) = { ⟨ σ ⟩ ⇝ c o n t i n u e E v ( ) ⇝ σ [ x ↦ f a l s e ] ⋅ K ( ( ) ) } (134) \operatorname{val}_{\sigma}\left(\circlearrowright_x\{\mathbf{continue};\ \bar{s}\ e_2\}\circlearrowleft\right)=\{\langle\sigma\rangle\mathrel{\leadsto}\mathit{continueEv}()\mathrel{\leadsto}\sigma[x\mapsto\mathbf{false}]\cdot K(())\} \tag*{(134)} val σ ( ↻ x { continue ; s ˉ e 2 } ↺ ) = {⟨ σ ⟩ ⇝ continueEv ( ) ⇝ σ [ x ↦ false ] ⋅ K (( ))} (134)
We must also adapt the global composition rules to stop execution after a c o n t i n u e \mathit{continue} continue event, as the evaluation should not proceed with the rest of the program after a continue in a loop scope. Rule (133) is the changed version of rule (50). All others are analogous.
σ = last ( s h ) lastEv ( s h ) ≠ p a n i c E v ( ) lastEv ( s h ) ≠ c o n t i n u e E v ( ) τ ⋅ K ( e ′ ) ∈ val σ ( e ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ ) ) s h , K ( e ) → s h ∗ ∗ ρ ( τ ) , K ( e ′ ) (135) \begin{gathered}
\sigma=\operatorname{last}(sh)\qquad\operatorname{lastEv}(sh)\ne\mathit{panicEv}()\qquad\operatorname{lastEv}(sh)\ne\mathit{continueEv}()\\
\tau\cdot K(e')\in\operatorname{val}_{\sigma}(e)\qquad\rho\ \text{concretizes}\ \tau\qquad wf(sh\mathbin{**}\rho(\tau))\\
\hline
sh,K(e)\to sh\mathbin{**}\rho(\tau),K(e') \tag*{(135)}
\end{gathered} σ = last ( s h ) lastEv ( s h ) = panicEv ( ) lastEv ( s h ) = continueEv ( ) τ ⋅ K ( e ′ ) ∈ val σ ( e ) ρ concretizes τ w f ( s h ∗ ∗ ρ ( τ )) s h , K ( e ) → s h ∗ ∗ ρ ( τ ) , K ( e ′ ) (135)
With these adapted rules, we can proceed with proving the relevant RustyDL calculus rules sound.
l o o p S c o p e I n v B o x Γ ⟹ { U } i n v , Δ Γ ⟹ { U } { V } ( i n v → [ π ↻ x e ; c o n t i n u e ↺ ω ] ) ( x ≐ T R U E → ϕ ) ∧ ( x ≐ F A L S E → i n v ) Γ ⟹ { U } [ π l o o p e ω ] ϕ , Δ \begin{gathered}
\mathit{loopScopeInvBox}\\
\Gamma\Longrightarrow\{U\}\mathit{inv},\Delta\\
\Gamma\Longrightarrow\{U\}\{V\}\left(\mathit{inv}\to[\pi\ \circlearrowright_x e;\ \mathbf{continue}\ \circlearrowleft\ \omega]\right)\\
(x\mathrel{\doteq}\mathbf{TRUE}\to\phi)\land(x\mathrel{\doteq}\mathbf{FALSE}\to\mathit{inv})\\
\hline
\Gamma\Longrightarrow\{U\}[\pi\ \mathbf{loop}\ e\ \omega]\phi,\Delta
\end{gathered} loopScopeInvBox Γ ⟹ { U } inv , Δ Γ ⟹ { U } { V } ( inv → [ π ↻ x e ; continue ↺ ω ] ) ( x ≐ TRUE → ϕ ) ∧ ( x ≐ FALSE → inv ) Γ ⟹ { U } [ π loop e ω ] ϕ , Δ
where V V V is an anonymizing update that overwrites every variable written to or mutated in e e e , see [1 , 22 ] for details.
Theorem G.9 (Soundness of Rule loopScopeInvBox). Let Γ , Δ ∈ 2 D L F m l \Gamma,\Delta\in2^{\mathrm{DLFml}} Γ , Δ ∈ 2 DLFml be sets of formulas, U ∈ U p d U\in\mathrm{Upd} U ∈ Upd be an update, x : b o o l x:\mathbf{bool} x : bool be a fresh program variable, π l o o p e ω \pi\ \mathbf{loop}\ e\ \omega π loop e ω be a program fragment, and ϕ , i n v ∈ D L F m l \phi,\mathit{inv}\in\mathrm{DLFml} ϕ , inv ∈ DLFml be formulas, and
V = b 1 : = b 1 ∥ ⋯ ∥ b n : = b n ∥ d 1 ∗ → d 1 ∥ ⋯ ∥ d m ∗ → d m V=b_1:=\mathbf{b}_1\mathbin{\|}\cdots\mathbin{\|}b_n:=\mathbf{b}_n\mathbin{\|}d_1\mathrel{*{\to}}d_1\mathbin{\|}\cdots\mathbin{\|}d_m\mathrel{*{\to}}d_m V = b 1 := b 1 ∥ ⋯ ∥ b n := b n ∥ d 1 ∗ → d 1 ∥ ⋯ ∥ d m ∗ → d m the anonymizing update, where b 1 , … , b n b_1,\ldots,b_n b 1 , … , b n are all variables written to in e e e , d 1 , … , d m d_1,\ldots,d_m d 1 , … , d m are all mutable references mutated in e e e , and b 1 , … , b n , d 1 , … , d m \mathbf{b}_1,\ldots,\mathbf{b}_n,\mathbf{d}_1,\ldots,\mathbf{d}_m b 1 , … , b n , d 1 , … , d m are fresh constants of appropriate sorts. If
⊨ Γ ⟹ { U } i n v , Δ \models\Gamma\Longrightarrow\{U\}\mathit{inv},\Delta ⊨ Γ ⟹ { U } inv , Δ Γ ⟹ { U } { V } ( i n v → [ π ↻ x e ; c o n t i n u e ↺ ω ] ) \Gamma\Longrightarrow\{U\}\{V\}\left(\mathit{inv}\to[\pi\ \circlearrowright_x e;\ \mathbf{continue}\ \circlearrowleft\ \omega]\right) Γ ⟹ { U } { V } ( inv → [ π ↻ x e ; continue ↺ ω ] ) ( x ≐ T R U E → ϕ ) ∧ ( x ≐ F A L S E → i n v ) (x\mathrel{\doteq}\mathbf{TRUE}\to\phi)\land(x\mathrel{\doteq}\mathbf{FALSE}\to\mathit{inv}) ( x ≐ TRUE → ϕ ) ∧ ( x ≐ FALSE → inv ) then the following holds:
⊨ Γ ⟹ { U } [ π l o o p e ω ] ϕ , Δ . \models\Gamma\Longrightarrow\{U\}[\pi\ \mathbf{loop}\ e\ \omega]\phi,\Delta. ⊨ Γ ⟹ { U } [ π loop e ω ] ϕ , Δ.
Proof. We assume
⊨ Γ ⟹ { U } i n v , Δ (136) \models\Gamma\Longrightarrow\{U\}inv, \Delta \tag*{(136)} ⊨ Γ ⟹ { U } in v , Δ (136)
Γ ⟹ { U } { V } ( i n v → [ π ↻ x ; continue ↺ ω ] ⊨ ( x ≐ T R U E → ϕ ) ∧ ( x ≐ F A L S E → i n v ) ) (137) \begin{aligned}
\Gamma&\Longrightarrow\{U\}\{V\}\left(inv \to[\pi\mathbin{\circlearrowright} x; \text{continue} \mathbin{\circlearrowleft} \omega]\right.\\
&\qquad\left.\models(x \mathrel{\doteq} TRUE \to\phi) \land(x \mathrel{\doteq} FALSE \to inv)\right) \tag*{(137)}
\end{aligned} Γ ⟹ { U } { V } ( in v → [ π ↻ x ; continue ↺ ω ] ⊨ ( x ≐ T R U E → ϕ ) ∧ ( x ≐ F A L S E → in v ) ) (137)
Let K = ( D , I , S , ρ ) \mathcal{K}=(\mathcal{D},I,S,\rho) K = ( D , I , S , ρ ) be a random but arbitrary Kripke structure, s ∈ S s \in S s ∈ S a state, and β \beta β a variable assignment. We want to show that
( K , s , β ) ⊨ Γ ⟹ { U } [ π loop e ω ] ϕ , Δ . (\mathcal{K},s,\beta) \models\Gamma\Longrightarrow\{U\}[\pi\ \text{loop}\ e\ \omega]\phi,\Delta. ( K , s , β ) ⊨ Γ ⟹ { U } [ π loop e ω ] ϕ , Δ.
Let s 1 = v a l K , s , β ( U ) ( s ) s_1 = val_{\mathcal{K},s,\beta}(U)(s) s 1 = v a l K , s , β ( U ) ( s ) . Then we need to show
( K , s 1 , β ) ⊨ [ π loop e ω ] ϕ . (138) (\mathcal{K},s_1,\beta) \models[\pi\ \text{loop}\ e\ \omega]\phi. \tag*{(138)} ( K , s 1 , β ) ⊨ [ π loop e ω ] ϕ . (138)
We infer from the assumptions that
( K , s 1 , β ) ⊨ i n v (139) (\mathcal{K},s_1,\beta) \models inv \tag*{(139)} ( K , s 1 , β ) ⊨ in v (139)
( K , s 1 , β ) ⊨ { V } ( i n v → [ π ↻ x ; e ; continue ↺ ω ] ( ( x ≐ T R U E → ϕ ) ∧ ( x ≐ F A L S E → i n v ) ) ) (140) (\mathcal{K},s_1,\beta) \models\{V\}\left(inv \to[\pi\mathbin{\circlearrowright} x;e;\text{continue}\mathbin{\circlearrowleft}\omega]\left((x \mathrel{\doteq} TRUE \to\phi) \land(x \mathrel{\doteq} FALSE \to inv)\right)\right) \tag*{(140)} ( K , s 1 , β ) ⊨ { V } ( in v → [ π ↻ x ; e ; continue ↺ ω ] ( ( x ≐ T R U E → ϕ ) ∧ ( x ≐ F A L S E → in v ) ) ) (140)
Let s 2 = v a l K , s 1 , β ( V ) ( s 1 ) s_2 = val_{\mathcal{K},s_1,\beta}(V)(s_1) s 2 = v a l K , s 1 , β ( V ) ( s 1 ) . Then we can transform (140) to
( K , s 2 , β ) ⊨ ( i n v → [ π ↻ x ; e ; continue ↺ ω ] ( ( x ≐ T R U E → ϕ ) ∧ ( x ≐ F A L S E → i n v ) ) ) (141) (\mathcal{K},s_2,\beta) \models\left(inv \to[\pi\mathbin{\circlearrowright} x;e;\text{continue}\mathbin{\circlearrowleft}\omega]\left((x \mathrel{\doteq} TRUE \to\phi) \land(x \mathrel{\doteq} FALSE \to inv)\right)\right) \tag*{(141)} ( K , s 2 , β ) ⊨ ( in v → [ π ↻ x ; e ; continue ↺ ω ] ( ( x ≐ T R U E → ϕ ) ∧ ( x ≐ F A L S E → in v ) ) ) (141)
If the loop does not terminate when started in s 1 s_1 s 1 , then the above holds trivially. Hence, we assume that the loop is exited after k k k iterations. We proceed with an inductive proof to show that the formula i n v inv in v holds before all iterations. Let s ( i ) s^{(i)} s ( i ) denote the state before the i i i -th iteration.
Case G.5 (Before first iteration (i = 1 i = 1 i = 1 )). Then, s ( i ) = s ( 1 ) = s 1 s^{(i)} = s^{(1)} = s_1 s ( i ) = s ( 1 ) = s 1 . By assumption (139) , we have ( K , s 1 , β ) ⊨ i n v (\mathcal{K},s_1,\beta) \models inv ( K , s 1 , β ) ⊨ in v , hence, the hypothesis holds.
Case G.6 (Induction step: Before iteration i + 1 i+1 i + 1 ). We assume ( K , s ( i ) , β ) ⊨ i n v (\mathcal{K},s^{(i)},\beta) \models inv ( K , s ( i ) , β ) ⊨ in v . We execute the loop body e e e once in state s ( i ) s^{(i)} s ( i ) . Either iteration i i i exits the loop or we continue with the loop’s execution. In the former case, there is no iteration i + 1 i+1 i + 1 and we are done. In the latter case, the semantics of the loop scope and the continue expression (rule (132)) mean that ( K , s ( i + 1 ) , β ) ⊨ i n v (\mathcal{K},s^{(i+1)},\beta) \models inv ( K , s ( i + 1 ) , β ) ⊨ in v holds iff the following holds:
( K , s ( i ) , β ) ⊨ ( i n v → [ π ↻ x ; e ; continue ↺ ω ] ( ( x ≐ T R U E → ϕ ) ∧ ( x ≐ F A L S E → i n v ) ) ) (\mathcal{K},s^{(i)},\beta) \models\left(inv \to[\pi\mathbin{\circlearrowright} x;e;\text{continue}\mathbin{\circlearrowleft}\omega]\left((x \mathrel{\doteq} TRUE \to\phi) \land(x \mathrel{\doteq} FALSE \to inv)\right)\right) ( K , s ( i ) , β ) ⊨ ( in v → [ π ↻ x ; e ; continue ↺ ω ] ( ( x ≐ T R U E → ϕ ) ∧ ( x ≐ F A L S E → in v ) ) )
Any changes the body e e e can affect are to b 1 , … , b n b_1,\ldots,b_n b 1 , … , b n and the places borrowed by d 1 , … , d m d_1,\ldots,d_m d 1 , … , d m . Because these are overwritten by the anonymizing update V V V , and for all other program variables we have s 2 ( y ) = s ( i + 1 ) ( y ) s_2(y)=s^{(i+1)}(y) s 2 ( y ) = s ( i + 1 ) ( y ) , the above follows from assumption (141) . Hence, ( K , s ( i + 1 ) , β ) ⊨ i n v (\mathcal{K},s^{(i+1)},\beta) \models inv ( K , s ( i + 1 ) , β ) ⊨ in v .
Now we know that iteration k k k exits the loop and that ( K , s ( k ) , β ) ⊨ i n v (\mathcal{K},s^{(k)},\beta) \models inv ( K , s ( k ) , β ) ⊨ in v . From this, it follows that
( K , s ( k ) , β ) ⊨ ( i n v → [ π ↻ x ; e ; continue ↺ ω ] ( ( x ≐ T R U E → ϕ ) ∧ ( x ≐ F A L S E → i n v ) ) ) (\mathcal{K},s^{(k)},\beta) \models\left(inv \to[\pi\mathbin{\circlearrowright} x;e;\text{continue}\mathbin{\circlearrowleft}\omega]\left((x \mathrel{\doteq} TRUE \to\phi) \land(x \mathrel{\doteq} FALSE \to inv)\right)\right) ( K , s ( k ) , β ) ⊨ ( in v → [ π ↻ x ; e ; continue ↺ ω ] ( ( x ≐ T R U E → ϕ ) ∧ ( x ≐ F A L S E → in v ) ) )
Because we exit the loop in iteration k k k , we simplify this to
( K , s ( k ) , β ) ⊨ ( i n v → [ π ω ] ( x ≐ T R U E → ϕ ) ) (\mathcal{K},s^{(k)},\beta) \models\left(inv \to[\pi\omega](x \mathrel{\doteq} TRUE \to\phi)\right) ( K , s ( k ) , β ) ⊨ ( in v → [ π ω ] ( x ≐ T R U E → ϕ ) )
which we further simplify to
( K , s ( k ) , β ) ⊨ [ π ω ] ϕ (142) (\mathcal{K},s^{(k)},\beta)\models[\pi\omega]\phi \tag*{(142)} ( K , s ( k ) , β ) ⊨ [ π ω ] ϕ (142)
Because we assume the loop’s termination, we have
Tr ( π ω , σ ( k ) ) ⪰ Tr ( π l o o p e ω , σ 1 ) . \operatorname{Tr}(\pi\omega,\sigma^{(k)})\succeq\operatorname{Tr}(\pi\mathop{\mathrm{loop}}e\omega,\sigma_1). Tr ( π ω , σ ( k ) ) ⪰ Tr ( π loop e ω , σ 1 ) .
By equation (142) and Lemma 6.11 we have goal (138) .
Γ ⟹ { U } ⟨ π { x = t r u e ; s e } ω ⟩ ϕ , Δ Γ ⟹ { U } ⟨ π ↺ x { b r e a k s e ; s ˉ e } ↻ ω ⟩ ϕ , Δ breakValue \frac{\Gamma\Longrightarrow\{U\}\langle\pi\{x=\mathsf{true};se\}\omega\rangle\phi,\Delta}
{\Gamma\Longrightarrow\{U\}\langle\pi\circlearrowleft_x\{\mathsf{break}\ se;\bar{s}\ e\}\circlearrowright\omega\rangle\phi,\Delta}
\qquad\text{breakValue} Γ ⟹ { U } ⟨ π ↺ x { break se ; s ˉ e } ↻ ω ⟩ ϕ , Δ Γ ⟹ { U } ⟨ π { x = true ; se } ω ⟩ ϕ , Δ breakValue
Theorem G.10 (Soundness of Rule breakValue). Let Γ , Δ ∈ 2 D L F m l \Gamma,\Delta\in2^{\mathrm{DLFml}} Γ , Δ ∈ 2 DLFml be sets of formulas, U ∈ U p d U\in\mathrm{Upd} U ∈ Upd be an update, x : b o o l x:\mathsf{bool} x : bool be a program variable, π ↺ x b r e a k s e s ˉ e ↻ ω \pi\circlearrowleft_x\mathsf{break}\ se\bar{s}\ e\circlearrowright\omega π ↺ x break se s ˉ e ↻ ω be a program fragment, s ˉ \bar{s} s ˉ a series of statements, e e e an expression, and ϕ ∈ D L F m l \phi\in\mathrm{DLFml} ϕ ∈ DLFml a formula. If
⊨ Γ ⟹ { U } ⟨ π { x = t r u e ; s e } ω ⟩ ϕ , Δ \models\Gamma\Longrightarrow\{U\}\langle\pi\{x=\mathsf{true};se\}\omega\rangle\phi,\Delta ⊨ Γ ⟹ { U } ⟨ π { x = true ; se } ω ⟩ ϕ , Δ then the following holds:
⊨ Γ ⟹ { U } ⟨ π ↺ x { b r e a k s e ; s ˉ e } ↻ ω ⟩ ϕ , Δ . \models\Gamma\Longrightarrow\{U\}\langle\pi\circlearrowleft_x\{\mathsf{break}\ se;\bar{s}\ e\}\circlearrowright\omega\rangle\phi,\Delta. ⊨ Γ ⟹ { U } ⟨ π ↺ x { break se ; s ˉ e } ↻ ω ⟩ ϕ , Δ.
Proof. We assume
⊨ Γ ⟹ { U } ⟨ π { x = t r u e ; s e } ω ⟩ ϕ , Δ (143) \models\Gamma\Longrightarrow\{U\}\langle\pi\{x=\mathsf{true};se\}\omega\rangle\phi,\Delta \tag*{(143)} ⊨ Γ ⟹ { U } ⟨ π { x = true ; se } ω ⟩ ϕ , Δ (143)
It suffices to show
( K , s 1 , β ) ⊨ ⟨ π ↺ x { b r e a k s e ; s ˉ e } ↻ ω ⟩ ϕ . (144) (\mathcal{K},s_1,\beta)\models\langle\pi\circlearrowleft_x\{\mathsf{break}\ se;\bar{s}\ e\}\circlearrowright\omega\rangle\phi. \tag*{(144)} ( K , s 1 , β ) ⊨ ⟨ π ↺ x { break se ; s ˉ e } ↻ ω ⟩ ϕ . (144)
We infer from the assumption (143) that
( K , s 1 , β ) ⊨ ⟨ π { x = t r u e ; s e } ω ⟩ ϕ (145) (\mathcal{K},s_1,\beta)\models\langle\pi\{x=\mathsf{true};se\}\omega\rangle\phi \tag*{(145)} ( K , s 1 , β ) ⊨ ⟨ π { x = true ; se } ω ⟩ ϕ (145)
We only consider the case where s e ∈ V a l se\in\mathit{Val} se ∈ Val . The case where s e ∈ V a r se\in\mathit{Var} se ∈ Var is analogous. Let σ 1 \sigma_1 σ 1 be an LAGC state with s 1 ∼ σ 1 s_1\sim\sigma_1 s 1 ∼ σ 1 . By rule (131), we have
val σ 1 ( ↺ x { b r e a k s e ; s ˉ e } ↻ ) = { ⟨ σ 1 ⟩ ⋅ K ( { x = t r u e ; s e } ) } (146) \operatorname{val}_{\sigma_1}(\circlearrowleft_x\{\mathsf{break}\ se;\bar{s}\ e\}\circlearrowright)=\{\langle\sigma_1\rangle\cdot\mathcal{K}(\{x=\mathsf{true};se\})\} \tag*{(146)} val σ 1 ( ↺ x { break se ; s ˉ e } ↻ ) = {⟨ σ 1 ⟩ ⋅ K ({ x = true ; se })} (146)
Clearly, then, Tr ( { x = t r u e ; s e } , σ 1 ) ⪰ Tr ( ↺ x { b r e a k s e ; s ˉ e } ↻ , σ 1 ) \operatorname{Tr}(\{x=\mathsf{true};se\},\sigma_1)\succeq\operatorname{Tr}(\circlearrowleft_x\{\mathsf{break}\ se;\bar{s}\ e\}\circlearrowright,\sigma_1) Tr ({ x = true ; se } , σ 1 ) ⪰ Tr ( ↺ x { break se ; s ˉ e } ↻ , σ 1 ) . By Lemma 6.11, this implies that the goal (144) holds.
:::
Γ ⟹ { U } ⟨ x = f a l s e ⟩ ϕ , Δ Γ ⟹ { U } ⟨ π ↺ x { c o n t i n u e s ˉ e } ↻ ω ⟩ ϕ , Δ continue \frac{\Gamma\Longrightarrow\{U\}\langle x=\mathsf{false}\rangle\phi,\Delta}
{\Gamma\Longrightarrow\{U\}\langle\pi\circlearrowleft_x\{\mathsf{continue}\ \bar{s}\ e\}\circlearrowright\omega\rangle\phi,\Delta}
\qquad\text{continue} Γ ⟹ { U } ⟨ π ↺ x { continue s ˉ e } ↻ ω ⟩ ϕ , Δ Γ ⟹ { U } ⟨ x = false ⟩ ϕ , Δ continue
Theorem G.11 (Soundness of Rule continue). Let Γ , Δ ∈ 2 D L F m l \Gamma,\Delta\in2^{\mathrm{DLFml}} Γ , Δ ∈ 2 DLFml be sets of formulas, U ∈ U p d U\in\mathrm{Upd} U ∈ Upd be an update, x : b o o l x:\mathsf{bool} x : bool be a program variable, π ↺ x c o n t i n u e s ˉ e ↻ ω \pi\circlearrowleft_x\mathsf{continue}\ \bar{s}\ e\circlearrowright\omega π ↺ x continue s ˉ e ↻ ω be a program fragment, s ˉ \bar{s} s ˉ a series of statements, e e e an expression, and ϕ ∈ D L F m l \phi\in\mathrm{DLFml} ϕ ∈ DLFml a formula. If
⊨ Γ ⟹ { U } ⟨ x = f a l s e ⟩ ϕ , Δ \models\Gamma\Longrightarrow\{U\}\langle x=\mathsf{false}\rangle\phi,\Delta ⊨ Γ ⟹ { U } ⟨ x = false ⟩ ϕ , Δ then the following holds:
⊨ Γ ⟹ { U } ⟨ π ↺ x { c o n t i n u e s ˉ e } ↻ ω ⟩ ϕ , Δ . \models\Gamma\Longrightarrow\{U\}\langle\pi\circlearrowleft_x\{\mathsf{continue}\ \bar{s}\ e\}\circlearrowright\omega\rangle\phi,\Delta. ⊨ Γ ⟹ { U } ⟨ π ↺ x { continue s ˉ e } ↻ ω ⟩ ϕ , Δ.
Proof. We assume
⊨ Γ ⇒ { U } ⟨ x = f a l s e ⟩ ϕ , Δ (147) \models\Gamma\Rightarrow\{U\}\langle x = \mathrm{false}\rangle\phi, \Delta \tag*{(147)} ⊨ Γ ⇒ { U } ⟨ x = false ⟩ ϕ , Δ (147)
It suffices to show
( K , s 1 , β ) ⊨ ⟨ π ↻ x { c o n t i n u e s e } ↺ ω ⟩ ϕ . (148) (\mathcal{K},s_1,\beta) \models\langle\pi\circlearrowright x\{\mathrm{continue}\ s\ e\}\circlearrowleft\omega\rangle\phi. \tag*{(148)} ( K , s 1 , β ) ⊨ ⟨ π ↻ x { continue s e } ↺ ω ⟩ ϕ . (148)
We infer from the assumption (141) that
( K , s 1 , β ) ⊨ ⟨ x = f a l s e ⟩ ϕ (149) (\mathcal{K},s_1,\beta) \models\langle x = \mathrm{false}\rangle\phi \tag*{(149)} ( K , s 1 , β ) ⊨ ⟨ x = false ⟩ ϕ (149)
Let σ 1 \sigma_1 σ 1 be an LAGC state with s 1 ∼ σ 1 s_1 \sim\sigma_1 s 1 ∼ σ 1 . By rule (132), we have
val σ 1 ( ↻ x { c o n t i n u e s e } ↺ ) = { ⟨ σ 1 ⟩ ↷ continueEv ( ) ↷ σ 1 [ x ↦ f f ] ⋅ K ( ( ) ) } (150) \operatorname{val}_{\sigma_1}(\circlearrowright x\{\mathrm{continue}\ s\ e\}\circlearrowleft) = \{\langle\sigma_1\rangle\curvearrowright\operatorname{continueEv}() \curvearrowright\sigma_1[x \mapsto\mathrm{ff}] \cdot K(())\} \tag*{(150)} val σ 1 ( ↻ x { continue s e } ↺ ) = {⟨ σ 1 ⟩ ↷ continueEv ( ) ↷ σ 1 [ x ↦ ff ] ⋅ K (( ))} (150)
By rule (133), we know that after the continue event, the composition cannot continue. Hence, we infer that all traces in Tr ( ↻ x { c o n t i n u e s e } ↺ , σ 1 ) \operatorname{Tr}(\circlearrowright x\{\mathrm{continue}\ s\ e\}\circlearrowleft,\sigma_1) Tr ( ↻ x { continue s e } ↺ , σ 1 ) are of the form s h ⋅ ⟨ σ 1 ⟩ ↷ continueEv ( ) ↷ σ 1 [ x ↦ f f ] sh \cdot\langle\sigma_1\rangle\curvearrowright\operatorname{continueEv}() \curvearrowright\sigma_1[x \mapsto\mathrm{ff}] s h ⋅ ⟨ σ 1 ⟩ ↷ continueEv ( ) ↷ σ 1 [ x ↦ ff ] for some shining trace s h sh s h . Let σ 2 : = σ 1 [ x ↦ f f ] \sigma_2 := \sigma_1[x \mapsto\mathrm{ff}] σ 2 := σ 1 [ x ↦ ff ] . It is trivial to derive that Tr ( x = f a l s e , σ 1 ) = { ⟨ σ 1 ⟩ ↷ σ 2 } \operatorname{Tr}(x=\mathrm{false},\sigma_1) = \{\langle\sigma_1\rangle\curvearrowright\sigma_2\} Tr ( x = false , σ 1 ) = {⟨ σ 1 ⟩ ↷ σ 2 } . Since all traces in both sets end in state σ 2 \sigma_2 σ 2 , Definition 6.6 implies the goal (148) holds. □ \square □