Assume, guarantee or repair: a regular framework for non regular properties

We present Assume-Guarantee-Repair (AGR)—a novel framework which verifies that a program satisfies a set of properties and also repairs the program in case the verification fails. We consider communicating programs—these are simple C-like programs, extended with synchronous actions over communication channels. Our method, which consists of a learning-based approach to assume–guarantee reasoning, performs verification and repair simultaneously: in every iteration, AGR either makes another step towards proving that the (current) system satisfies the required properties, or alters the system in a way that brings it closer to satisfying the properties. To handle infinite-state systems we build finite abstractions, for which we check the satisfaction of complex properties that contain first-order constraints, using both syntactic and semantic-aware methods. We implemented AGR and evaluated it on various communication protocols. Our experiments present compact proofs of correctness and quick repairs.


Introduction
Verification of large-scale systems is a main challenge in the field of formal verification. Often, the verification process of such systems does not scale well. Compositional verification aims to address this challenge by breaking up the verification of a large system into the verification of its smaller components which can be checked separately. The results of the verification can be composed back to conclude the correctness of Assume-Guarantee-Repair the entire system. This, however, is not always possible, since the correctness of a component often depends on the behavior of its environment.
The Assume-Guarantee (AG) style compositional verification [33,39] suggests a solution to this problem. The simplest AG rule checks if a system composed of M 1 and M 2 satisfies a property P by checking that M 1 under assumption A satisfies P and that any system containing M 2 as a component satisfies A.
In this work, we present Assume-Guarantee-Repair (AGR)a fully automated framework which applies the AG rule, and while seeking a suitable assumption A, incrementally repairs the given program in case the verification fails. Our framework is inspired by [37], which presented a learning-based method to finding a suitable assumption A, using the L * [4] algorithm for learning regular languages. However, unlike previous work, AGR not only performs verification but also repair.
Our AGR framework handles communicating programs, which are commonly used for modeling concurrent systems. These are infinite-state C-like programs, extended with the ability to synchronously read and write messages over communication channels. We model such programs as finite-state automata over an action alphabet, which reflects the program statements. The automata representation is similar in nature to that of control-flow graphs. Its advantage, however, is in the ability to exploit an automata-learning algorithm such as L * . The accepting states in the automaton representation model points of interest in the program, to which the specification can relate.
We have implemented a tool for AGR and evaluated it on examples modeling communication protocols of various sizes and with various types of errors. Our experiments show that for most examples, AGR converges and finds a repair after a few (2)(3)(4)(5) iterations of verify-repair. Moreover, our tool generates assumptions that are significantly smaller then the (possibly repaired) M 2 , thus constructing a compact and efficient proof of correctness.

Contributions
To summarize, the main contributions of this paper are: 1. A learning-based Assume-Guarantee algorithm for infinite-state communicating programs, which manages overcoming the difficulties such programs present. In particular, our algorithm overcomes the inherent irregularity of the first-order constraints in these programs, and offers syntactic solutions to the semantic problems they impose. 2. An Assume-Guarantee-Repair algorithm, in which the Assume-Guarantee and the Repair procedures intertwine to produce a repaired program which, due to our construction, maintains many of the "good" behaviors of the original program. Moreover, in case the original program satisfies the property, our algorithm is guaranteed to terminate and return this conclusion. 3. An incremental learning algorithm that uses query results from previous iterations in learning a new language with a richer alphabet. 4. A novel use of abduction to repair communicating programs over first order constraints. 5. An implementation of our algorithm, demonstrating the effectiveness of our framework.

Contribution over conference version
Preliminary results of this work were published in [17]. This paper extends the results of [17] by the following new contributions.
-A formal definition of the weakest assumption for communicating programs, and a study of special cases for which the weakest assumption is regular (Sect. 7). -A full description of the implementation of the teacher in the context of communicating programs (Sect. 8.1 and Algorithm 1). -Convergence of the syntactic repair-characterizing cases in which this repair does not terminate (Sect. 8.4.1).

Paper organization
The rest of the paper is organized as follows. In the next section, we give a high-level overview of AGR, highlighting the salient features of our approach. Section 3 describes related work. The following three sections set up the background necessary for understanding our approach. Section 4 gives preliminary definitions, Sect. 5 describes communicating programs and their properties (expressed as regular languages), and Sect. 6 proves properties of traces that we use later when proving soundness and completeness of AGR. Section 7 describes and analyzes the assume-guarantee rule used by AGR, while Sect. 8 describes in detail the AGR approach. Finally, Sect. 9 describes our experimental evaluation and Sect. 10 concludes the paper. The program M 1 and the specification P able x pw over channel enc, and get Enc?x pw2 reads a value to x pw2 on channel get Enc). The specification P 1 is modeled as an automaton that does not contain assignment actions. It may contain communication actions in order to specify behavioral requirements, as well as constraints over the variables of both system components, that express requirements on their values in various points in the runs.
Consider, for example, the program M 1 and the specification P seen in Fig. 2, and the program M 2 of Fig. 1. M 2 reads a password on channel read to the variable x pw , and once it is long enough (has at least four digits), it sends the value of x pw to M 1 through channel enc. M 1 reads this value to variable y pw and then applies a simple function that changes its value, and sends the changed variable back to M 2 . The property P reasons about the parallel run of the two programs. The pair (getEnc!y pw , getEnc?x pw2 ) denotes a synchronization of M 1 and M 2 on channel getEnc. P makes sure that the parallel run of M 1 and M 2 always reads a value and then encrypts it-a temporal requirement. In addition, it makes sure that the value after encryption is different from the original value, and that there is no overflow-both are semantic requirements over the program variables. That is, P expresses temporal requirements that contain first order constraints. In case one of the requirements does not hold, P reaches the state r 4 which is an error state. Note that P here is not complete, for simplicity of presentation (see Definition 8 for a formal definition of a complete program).
The L * algorithm aims at learning a regular language U . Its entities consist of a teacher-an oracle who answers membership queries ("is the word w in U ?") and equivalence 1 Throughout the paper we use property and specification interchangeably. queries ("is A an automaton whose language is U ?"), and a learner, who iteratively constructs a finite deterministic automaton A for U by submitting a sequence of membership and equivalence queries to the teacher.
In using the L * algorithm for learning an assumption A for the AG-rule, membership queries are answered according to the satisfaction of the specification P: If M 1 ||t satisfies P, then the trace t in hand should be in A. Otherwise, t should not be in A. Once the learner constructs a stable system A, it submits an equivalence query. The teacher then checks whether A is a suitable assumption, that is, whether M 1 ||A satisfies P, and whether the language of M 2 is contained in the language of A. According to the results, the process either continues or halts with an answer to the verification problem. The learning procedure aims at learning the weakest assumption A w , which contains all the traces that in parallel with M 1 satisfy P . The key observation that guarantees termination in [37] is that the components in this procedure-M 1 ,M 2 , P and A w are all regular.

Our setting
Our setting is more complicated, since the traces in the components-both the programs and the specification contain constraints, which are to be checked semantically and not syntactically. These constraints may cause some traces to become infeasible. For example, if a trace contains an assignment x := 3 followed by a constraint x ≥ 4 (modeling an "if" statement), then this trace does not contribute any concrete runs, and therefore does not affect the system behavior. Thus, we must add feasibility checks to the process.
Constraints in the specification also pose a difficulty, as satisfiability of a specification is determined by the semantics of the constraints and not only by the language syntax, and so there is more here to check than standard language containment. Moreover, in our setting A w above may no longer be regular, see Lemma 5. However, our method manages overcoming this problem in a way that still guarantees termination in case the verification succeeds, and progress, otherwise. In addition, we characterize special cases in which the weakest assumption is in fact regular.
As we have described above, not only do we construct a learning-based method for the AG-rule for communicating programs, but we also repair the programs in case the verification fails. An AG-rule can either conclude that M 1 ||M 2 satisfies P, or return a real, non-spurious counterexample of a computation t of M 1 ||M 2 that violates P. In our case, instead of returning t, we repair M 2 in a way that eliminates the counterexample t. Our repair is both syntactic and semantic, where for semantic repair we use abduction [38] to infer a new constraint which makes the counterexample t infeasible.
Consider again M 1 and P of Fig. 2 and M 2 of Fig. 1. The composition M 1 ||M 2 does not satisfy P. For example, if the initial value of x pw is 2 63 , then after encryption the value of y pw is 2 64 , violating P. Our algorithm finds a bad trace during the AG stage which captures this bad behavior, and the abduction in the repair stage finds a constraint that eliminates it: x pw < 2 63 , and inserts this constraint to M 2 .
Following this step we now have an updated M 2 , and we continue with applying the AG-rule again, using information we have gathered in the previous steps. In addition to removing the error trace, we update the alphabet of M 2 with the new constraint. Continuing our example, in the following iteration AGR will verify that the repaired M 2 together with M 1 satisfy P, and terminate.
Thus, AGR operates in a verify-repair loop, where each iteration runs a learning-based process to determine whether the (current) system satisfies P, and if not, eliminates bad behaviors from M 2 while enriching the set of constraints derived from these bad behaviors, which often leads to a quicker convergence. In case the current system does satisfy P, we return the repaired M 2 together with an assumption A that abstracts M 2 and acts as a smaller proof for the correctness of the system. The original motivation for using the AG rule for verification is to find small proofs for the correctness of M 1 ||M 2 P, without the need to compute the whole composition of M 1 ||M 2 , but using the smaller assumption A. In our case, we use the same reasoning, but we do not only prove correctness or provide a counterexample, we also repair M 2 . Thus, we keep learning abstractions to the repaired M 2 , and, as we later show, rely on information from previous iterations in order to learn an abstraction for the next iteration. Our algorithm produces both a repaired system M 2 such that M 1 ||M 2 P; and an assumption A that serves as an abstraction for M 2 and allows to prove the correctness of the repaired system with respect to P, without the need to compute the whole composition of M 1 ||M 2 .
The assumption A can later be used for the verification of different components other than M 2 (or the repaired M 2 ). If some M 2 is such that M 2 ⊆ A (that is, A is an abstraction of M 2 ), then from M 1 ||A P we conclude that M 1 ||M 2 P. That is, given M 2 and the small A, all we need to check is that A is indeed an abstraction of M 2 , instead of computing the composition of the whole system and specification.

Related work
Assume-guarantee style compositional verification [33,39] has been extensively studied. The assumptions necessary for compositional verification were first produced manually, limiting the practicality of the method. More recent works [7,10,19,21] proposed techniques for automatic assumption generation using learning and abstraction refinement techniques, making assume-guarantee verification more appealing. In [7,37] alphabet refinement has been suggested as an optimization, to reduce the alphabet of the generated assumptions, and consequently their sizes. This optimization can easily be incorporated into our framework as well. Other learning-based approaches for automating assumption generation have been described in [8,9,23]. All these works address non-circular rules and are limited to finite state systems. Automatic assumption generation for circular rules is presented in [13,14], using compositional rules similar to the ones studied in [30,34]. Our approach is based on a noncircular rule but it targets complex, infinite-state concurrent systems, and addresses not only verification but also repair. The compositional framework presented in [27] addresses L * -based compositional verification and synthesis but it only targets finite state systems.
Several works use abduction. The work in [26] addresses automatic synthesis of circular compositional proofs based on logical abduction; however the focus of that work is sequential programs, while here we target concurrent programs. A sequential setting is also considered in [2], where abduction is used for automatically generating a program environment. Our computation of abduction is similar to that of [2]. However, we require our constraints to be over a predefined set of variables, while they look for a minimal set. Moreover, our goal is to repair a faulty program.
The approach presented in [45] aims to compute the interface of an infinite-state component. Similar to our work, the approach works with both over-and under-approximations but it only analyzes one component at a time. Furthermore, the component is restricted to be deterministic (necessary for the permissiveness check). In contrast we use both components of a system to compute the necessary assumptions, and as a result they can be much smaller than in [45]. Furthermore, we do not restrict the components to be deterministic and, more importantly, we also address the system repair in case of dissatisfaction.
Many works study the learnability of symbolic automata, e.g. [5,15,29,44]. The work of [24] studies alphabet refinement for learning symbolic automata. However, all of these are restricted to the abstraction of single transitions, where valuations of variables (states) and their update via program statements are not considered. We, on the other hand, model program statements and program states.
There is extensive research on automated program repair. Examples for testing-based approaches are [22,31,32,35,40], whereas in this work we repair with respect to a formal specification rather than a test-suit. In the context of formal repair with respect to a specification, [41] lays foundations of diagnosis of errors in multi-component systems, with respect to logical specifications. Their setting relies on the logical representation of the system and the specification, and is concerned with finding all reasons to the violation of the property. In our work we are indeed looking for error traces that violate the specification, but our specifications allow us to reason also about the temporal behavior of the program due to its representation as an automaton. [25] presents an algorithm to repair finite-state programs with respect to a temporal specification, where we are concerned with infinite-state programs that admit some finite representation. [42,43] present automated repair algorithms using SAT based techniques for specifications given as assertions. Note that while we are aiming at repairing the system, our work is not restricted only to repair, but also for finding a small proof of correctness, in the form of the assumption A, that abstracts (the repaired) M 2 and allows a smaller and more efficient composition of the multiple components.

Regular languages
In this section we define the notions of finite automata and regular languages. We then give a high-level description of the automata learning algorithm L * .
where all sets are finite and nonempty, and: 1. Q is the set of states and q 0 ∈ Q is the initial state. 2. α is the set of alphabet of M. 3. δ ⊆ Q × α × Q is the transition relation. We sometimes also refer to it as a function δ : Q × α → 2 Q . 4. F ⊆ Q is the set of accepting states.
A Finite automaton is deterministic (DFA) if for every q ∈ Q and every a ∈ α, |δ(q, a)| ≤ 1. It is also complete if for every q ∈ Q and every a ∈ α, |δ(q, a)| = 1.
Let w = a 1 , . . . , a k be a word over alphabet α. We say that M accepts w if there exists r 0 , . . . , r k ∈ Q such that r 0 = q 0 , r k ∈ F and for every i,

Learning regular languages
In active automata learning we are interested in learning a DFA A for some regular language U , by issuing queries to some knowledgeable teacher. L * [4] is an algorithm for active automata learning, using membership and equivalence queries. Its entities consist of a teacher-an oracle who answers membership queries (mqs) "is the word w in U ?", and equivalence queries (eqs) "is A an automaton whose language is U ?"; and a learner, who iteratively constructs a finite deterministic automaton A for U by submitting a sequence of membership and equivalence queries to the teacher.
The learner presents its queries to the teacher following the instructions of the L * algorithm. In particular, membership queries present words in increasing length and in alphabetical order. Based on its queries, the learner maintains an observation table T , which accumulates all words learned so far, together with an indication of whether each word belongs to U or not. If w is in T and is indicated to be in U we call w a positive example, denoted by +w, and if it is indicated to not be in U we call it a negative example, denoted by w.
When the observation table is "stable", the learner generalizes the table and constructs a conjectured automaton A , which is sent via equivalence query to the teacher. The teacher either determines that L(A ) = U, in which case L * terminates; or returns a counterexample in the form of a word which is in the symmetric difference between L(A ) and U . The learner updates its observation table accordingly and continues with its construction.
If the teacher answers according to a regular language U , then the L * algorithm is guaranteed to terminate and learn a DFA for U , in polynomial time. In the rest of the paper we rely on the correctness of L * and the fact that the learner queries words in increasing order.
For more details regarding the L * algorithm, we refer the reader to [4].

Assume-guarantee reasoning
For large systems, composed of two components (or more), Assume-Guarantee (AG) proof rule [39] is highly effective. Given a system composed of components M 1 and M 2 and a property P, the AG-rule concludes that M 1 ||M 2 | P provided that an assumption A is found, such that M 1 ||A | P and the behaviors of M 2 are contained in the behaviors of A. For M 1 , M 2 , P and A that are all Label Transition Systems (LTSs), an automated AG rule has been suggested in [10]. There, the assumption A is learned using the L * algorithm. Often, the learned A is much smaller (in terms of states and transitions) than M 2 . Thus, an efficient composition proof rule is obtained, for proving that M 1 ||M 2 | P.
In using the L * algorithm for learning an assumption A for the AG-rule, membership queries are answered according to the satisfaction of the specification P: If M 1 ||t satisfies P, then the trace t in hand should be in A. Otherwise, t should not be in A. Once the learner constructs a stable system A, it submits an equivalence query. The teacher then checks whether A is a suitable assumption, that is, whether M 1 ||A satisfies P, and whether the language of M 2 is contained in the language of A. According to the results, the process either continues or halts with an answer to the verification problem. The learning procedure aims at learning the weakest assumption A w , which contains all the traces that in parallel with M 1 satisfy P . The key observation that guarantees termination in [37] is that the components in this procedure-M 1 ,M 2 , P and A w -are all regular.
It worth noticing that often A, learned by L * , is much smaller than the weakest assumption and than M 2 .

Communicating programs and regular properties
In this section we present the notion of communicating programs. These are C-like programs, extended with the ability to synchronously read and write messages over communication channels. We model such programs as automata over an action alphabet that reflects the program statements. The alphabet includes constraints, which are quantifier-free firstorder formulas, representing the conditions in if and while statements. It also includes assignment statements and read and write communication actions. The automata representation is similar in nature to that of control-flow graphs. Its advantage, however, is in the ability to exploit an automatalearning algorithm such as L * for its verification [4]. We first formally define the alphabet over which communicating programs are defined. Let G be a finite set of communication channels. Let X be a finite set of variables (whose ordered vector isx) and D be a (possibly infinite) data domain. For simplicity, we assume that all variables are defined over D. The elements of D are also used as constants in arithmetic expressions and constraints.
Definition 2 An action alphabet over G and X is α = G ∪ E ∪ C where: g?x is a read action of a value to the variable x through channel g. g!x is a write action of the value of x on channel g.
We use g * x to indicate some action, either read or write, through g. -The pairs (g?x 1 , g!x 2 ) and (g!x 1 , g?x 2 ) represent a synchronization of two programs on read-write actions over channel g (defined later).
2. E ⊆ { x := e : e ∈ E, x ∈ X } is a finite set of assignment statements, where E is a set of expressions over X ∪D. For an expression e, we denote by e[x ←d] the expression over D in which the variablesx ⊆ X are assigned with the values ofd ⊆ D. 3. C is a finite set of constraints over X ∪ D.

Definition 3 A communicating program (or, a program) is
1. Q is a finite set of states and q 0 ∈ Q is the initial state. 2. X is a finite set of variables that range over D.
We sometimes also refer to it as a function δ : Q × α → 2 Q . 5. F ⊆ Q is the set of accepting states.
The words that are read along a communicating program are a symbolic representation of the program behaviors. We refer to such a word as a trace. Each such trace induces concrete executions of the program, which are formed by concrete assignments to the program variables in a way that conforms with the actions along the word.
Although communicating programs are an extension of finite automata, we investigate them from a different perspective. Usually, an automaton takes as input a word w and checks whether w is in the language of the automaton. In this work we like to think of the automaton as the generator of the behavior, as it describes the program. Therefore, we begin with a run of the program, and induce traces from the run, and not the other way around. We now formally define these notions.

Definition 4 A run in a program automaton M is a finite sequence of states and actions
The induced trace of r is the sequence t = (a 1 , . . . , a n ) of the actions in r . If q n is accepting, then t is an accepted trace of M.
From now on we assume that every trace we discuss is induced by some run. We turn to define the concrete executions of the program.
Intuitively, x is arbitrarily assigned by the read action, and the rest of the variables are unchanged.

If a i is an assignment
That is, the effect of a synchronous communication on a channel is that of an assignment.
5. If a i does not involve a read or an assignment, then β i = β i−1 . 6. Finally, if a i is a constraint in C, then β i (x) a i (and since a i does not change the variable assignments, then β i−1 (x) a i holds as well).
We say that t is feasible if there exists an execution of t.
The symbolic language of M, denoted T (M), is the set of all accepted traces induced by runs of M. The concrete language of M, denoted L(M), is the set of all executions of accepted traces in T (M). We will mostly be interested in feasible traces, which represent (concrete) executions of the program.

Parallel composition
We now describe and define the parallel composition of two communicating programs, and the way in which they communicate.
Let G 1 , G 2 be the sets of communication channels occurring in actions of M 1 , M 2 , respectively. We assume that X 1 ∩ X 2 = ∅.
The interface alphabet α I of M 1 and M 2 consists of all communication actions on channels that are common to both components. That is, In parallel composition, the two components synchronize on their communication interface only when one component writes data through a channel, and the other reads it through the same channel. The two components cannot synchronize if both are trying to read or both are trying to write. We distinguish between communication of the two components with each other (on their common channels), and their communication with their environment. In the former case, the components must "wait" for each other in order to progress together. In the latter case, the communication actions of the two components interleave asynchronously.

Definition 6
The parallel composition of M 1 and M 2 , denoted M 1 ||M 2 , is the program M = Q, x, α, δ, q 0 , F , defined as follows.
where Q 1 and Q 2 are new copies of Q 1 and Q 2 , respectively. The initial state is q 0 = (q 1 0 , q 2 0 ).
That is, the alphabet includes pairs of read-write communication actions on channels that are common to M 1 and M 2 . It also includes individual actions of M 1 and M 2 , which are not communications on common channels. 4. δ is defined as follows.
That is, when a communication is performed synchronously in both components, the data is transformed through the channel from the writing component to the reading component. As a result, the values of x 1 and x 2 equalize. This is enforced in M by adding a transition labeled by the constraint x 1 = x 2 that immediately follows the synchronous communication. 4 That is, on actions that are not in the interface alphabet, the two components interleave. 5. F = F 1 × F 2 Definition 7 For a trace t, we define M t to be the communicating program which only follows the trace t, and has no other transitions. Then, we define M||t to be the composition M||M t (and similarly for t||M).

Regular properties and their satisfaction
The specifications we consider are also given as some variation of communicating programs. We now define the syntax and semantics of the properties that we consider as specifications. These are properties that can be represented as finite automata, hence the name regular. However, the alphabet of such automata includes communication actions and first-order constraints over program variables. Thus, such automata are suitable for specifying the desired and undesired behaviors of communicating programs over time.
In order to define our properties, we first need the notion of a deterministic and complete program. The definition is somewhat different from the standard definition for finite automata, since it takes the semantic meaning of constraints into account. Intuitively, in a deterministic and complete program, every concrete execution has exactly one trace that induces it.
Definition 8 A communicating program over alphabet α is deterministic and complete if for every state q and for every action a ∈ α the following hold: 1. Syntactic determinism and completeness. There is exactly one state q such that q, a, q is in δ. 5 2. Semantic determinism. If q, c 1 , q and q, c 2 , q are in δ for constraints c 1 , c 2 ∈ C such that c 1 = c 2 and q = q , then c 1 ∧ c 2 ≡ f alse. 3. Semantic completeness. Let C q be the set of all constraints on transitions leaving q. Then ( c∈C q c) ≡ true .
A property is a deterministic and complete program with no assignment actions, whose language defines the set of desired and undesired behaviors over the alphabet α P.
A trace is accepted by a property P if it reaches a state in F, the set of accepting states of P. Otherwise, it reaches a state in Q \ F, and is rejected by P.
Next, we define the satisfaction relation between a program and a property. Intuitively, a program M satisfies a property P (denoted M P) if all executions induced by 5 In our examples we sometimes omit the actions that lead to a rejecting sink for the sake of clarity. accepted traces of M reach an accepting state in P. Thus, the accepted behaviors of M are also accepted by P. A property P specifies the behavior of a program M by referring to communication actions of M and imposing constraints over the variables of M. Thus, the set of variables of P is identical to that of M. Let G be the set of communication actions of M. Then, α P includes a subset of G as well as constraints over the variables of M. The interface of M and P, which consists of the communication actions that occur in P, is defined as α I = G ∩ α P.
In order to capture the satisfaction relation between M and P, we define a conjunctive composition between M and P, denoted M × P. In conjunctive composition, the two components synchronize on their common communication actions when both read or both write through the same communication channel. They interleave on constraints and on actions of α M that are not in α P.
That is, the alphabet includes communication actions on channels common to M and P. It also includes individual actions of M and P.
Note that communication actions of the form (g * x, g * y) can only appear if M is itself a parallel composition of two programs. 4. δ is defined as follows.
That is, on actions that are not common communication actions to M and P, the two components interleave.
Note that accepted traces in M × P are those that are accepted in M and rejected in P. Such traces are called error traces and their corresponding executions are called error executions. Intuitively, an error execution is an execution along M Thus, a feasible error trace in M × P is an evidence to M P, since it indicates the existence of an execution that violates P.

Example 2
Consider the program M, the property P and the partial construction of M × P presented in Fig. 4. The property P requires every verified password y to be of length at least 4. It is easy to see that M P, since the trace t = (password?y, y > 0, verify!y, y < 1000) is a feasible error trace in M × P.

Traces in the composed system
Before we discuss our framework for compositional verification and repair of communicating systems, we prove some properties of traces in the composed system. We later use these properties in order to prove that our framework is sound and complete (Sect. 7.1), and to prove the correctness and termination of our algorithm (Sects. 8.3 and 8.5).

Definition 11
Let t be a trace over alphabet α, and let α ⊆ α. We denote by t ↓ α the restriction of t on α , which is the trace obtained from t by omitting all letters in t that are not in α . If α contains a communication action a = (g * x, g * y) and we have g * x ∈ α then the restriction t ↓ α includes the corresponding communication, g * x, and similarly for g * y ∈ α .
For traces in the conjunctive and parallel compositions, we have the following two lemmas.
Lemma 1 Let M be a communicating program and P be a property, and let t be a trace of M × P. Then t ↓ α M is a trace of M.
We prove Lemma 1. The proof of Lemma 2 is similar, with the special care of communications actions, and is provided in the full version of this paper [18].
We first observe the following. If (a 1 , · · · , a k ) is a trace of M × P such that ∀i : a i / ∈ α M, and q = (q M , q 0 P ) is the state in M × P before reading a 1 , then ∀i ≥ 1 : ∃q i P : , that is, when reading a trace that does not contain letters from α M, the program M × P only advances on the P component. This is true since by the We now inductively prove that (c i 1 , · · · , c i j ) is a trace of M for every 1 ≤ j ≤ n. In particular, for j = n this means that t M is a trace of M.
Let j := 1 and denote k : We now discuss the feasibility of traces in the composed system. Proof Let t ∈ T (M × P) be a feasible trace. Then, there exists an execution u on t. Denote t = (b 1 , · · · , b n ) and u = (β 0 , b 1 , β 1 , · · · , b n , β n ). We inductively construct an execution e on t ↓ α M . The existence of such an execution e proves that t ↓ α M is feasible.
Let t ↓ α M = (c 1 , · · · , c k ). We set e = (γ 0 , c 1 , γ 1 , · · · , c k , γ k ) where γ 0 , . . . , γ k are defined as follows. Note that for each i < l < i is holds that b l is a constraint. Indeed, by the definition of conjunctive composition (Definition 9), if b l is not a constraint, then b l ∈ α M. But in that case, b l must synchronize with some alphabet letter in t ↓ α M , contradicting the fact that i is the minimal index for which b i = c j . Thus, since u is an execution, and for all i < l < i : b l is a constraint, it holds that ∀i ≤ l < i : β i = β l . In particular, β i −1 = β i = γ j−1 . Now, since b i = c j , we can assign γ j to be the same as β i and result in a valid assignment. Thus, e is a valid execution on t ↓ α M , making t ↓ α M feasible, as needed. The proof of Lemma 4 is different from the proof of Lemma 3, since here we can no longer use the exact same assignments as the ones of the run on M 1 ||M 2 . In the case of M × P, the set of variables of M × P is equal to that of M, and the two runs only differ on the constraints that are added to the trace of M × P. In M 1 ||M 2 , on the other hand, M 1 and M 2 are defined over two disjoint sets of variables. Nevertheless, The proof is similar to the proof of Lemma 3, and is provided in the full version [18].

The assume-guarantee rule for communicating systems
Let M 1 and M 2 be two programs, and let P be a property. The classical Assume-Guarantee (AG) proof rule [39] assures that if we find an assumption A (in our case, a communicating program) such that M 1 ||A P and M 2 A both hold, then M 1 ||M 2 P holds as well. For labeled transition systems over a finite alphabet (LTSs) [10], the AG-rule is guaranteed to either prove correctness or return a real (nonspurious) counterexample. The work in [10] relies on the L * Algorithm [4] for learning an assumption A for the AG-rule. In particular, L * aims at learning A w , the weakest assumption for which M 1 ||A w P. A crucial point of this method is the fact that A w is regular [20], and thus can be learned by L * . For communicating programs, this is not the case, as we show in Lemma 5.

Definition 12 (Weakest Assumption)
Let P be a property and let M 1 and M 2 be two programs. The weakest assumption A w with respect to M 1 , M 2 and P has the language L(A w ) = {w ∈ (α M 2 ) * : M 1 ||w P}. That is, A w is the set of all words over the alphabet of M 2 that together with M 1 satisfy P.

Lemma 5 For infinite-state communicating programs, the weakest assumption A w is not always regular.
Proof Consider the programs M 1 and M 2 , and the property P of Fig. 5. Let α M 2 = {x := 0, y := 0, x := x + 1, y := y + 1, sync}. Note that in order to satisfy P, after the sync action, a trace t must pass the test x = y. Also note that the weakest assumption A w does not depend on the behavior of M 2 , but only on its alphabet. Assume by way of contradiction that L(A w ) is a regular language, and consider the language L = {x := 0} · {y := 0} · {x := x + 1, y := y + 1} * · {sync} By closure properties of regular languages, it holds that L is a regular language, and thus following our assumption, we have that L ∩ L(A w ) is regular as an intersection of two regular languages. However L ∩L(A w ) is the set of all words that after the initialization {x := 0}{y := 0}, contain equally many actions of the form x := x + 1 and y := y + 1. That is where L eq = {u ∈ {x := x + 1, y := y + 1} * : To cope with this difficulty, we change the focus of learning. Instead of learning the (possibly) non-regular language of A w , we learn T (M 2 ), the set of accepted traces of M 2 . This language is guaranteed to be regular, as it is represented by the automaton M 2 .

Soundness and completeness of the assume-guarantee rule for communicating systems
Since we have changed the goal of learning, we first show that in the setting of communicating systems, the assumeguarantee rule is sound and complete.

Weakest assumption: special cases
As we have proven in Lemma 5, in the context of communicating systems, the weakest assumption is not always regular, and so we changed our learning goal to the language of M 2 , that is, to T (M 2 ). We now consider special cases for which the weakest assumption is guaranteed to be regular, and can therefore be used as a target for the learning process. This may result in the generation of a more general assumption.
We first show that if all components are constraints-free, then the weakest assumption is guaranteed to be regular. Intuitively, this is since when there are no constraints in the composed system (that includes also the specification), all traces are feasible. Thus we can reduce the problem of finding a weakest assumption for communicating programs to finding the weakest assumption for finite state automata.

Lemma 6
Let P be a property and M 1 and M 2 be communicating programs such that α P, α M 1 and α M 2 do not contain constraints. Then, the weakest assumption A w with respect to M 1 , M 2 and P, is regular.
Proof We construct a communicating system A over α M 2 , based on M 1 × P, as follows. A state in A is accepting if its M 1 component is rejecting, or its P component is accepting. We add self loops to all states, labeled by the alphabet of M 2 that does not synchronize with M 1 and P. We replace the alphabet of M 1 and P that is not also in M 2 with ε-transitions (and leave the rest unchanged). Finally, we determinize the result. We therefore have that t is an accepting trace of A iff all the states that A reaches when reading t are either not accepting in M 1 , or accepting (hence, not rejecting) in P. For the complete details, see the full version [18]. Let L(A) be the set of all traces of A.
Note that in case that there are no constraints, all traces are feasible, that is, L(A) = T (A). To show the correctness of Claim 1, we state the following claim, which can be proved by induction on the trace t. See [18] for the full proof.
Claim 2 For a trace t ∈ (α M 2 ) * , the program A reaches the same set of states S when reading t, as the set of states that M 1 × P reaches given t.
Claim 1 then follows from the definition of the set of accepting states of A. Then, to conclude the proof, we have that L(A w ) = T (A), and therefore is regular.
In [20] the authors prove that the weakest assumption is regular for the setting of LTSs, which are prefix-closed finite-state automata. Their proof relies on the fact that LTSs are prefix closed, and they construct the weakest assumption accordingly. Our proof holds for general communicating programs, and not only for prefix-closed ones. We can therefore extend the result of [20] to the case of general finite-state automata. Corollary 1 follows from the fact that we can refer to finite-state automata as communicating programs without constraints; when applying the composition operators || and ×, we relax the requirements of synchronization on read-write channels, to requiring synchronization on mutual alphabet letters. A 1 , A 2 and A p be finite-state automata such that αA p ⊆ (αA 1 ∪αA 2 ). Then, the weakest assumption A w for A 1 , A 2 and A p is regular.

Corollary 1 Let
We now consider a more general case for which we can find regular weakest assumptions. To this end, we define a refined notion of the weakest assumption, in which we do not care what the behavior of A w is, for traces that are not feasible. We call it the semantic weakest assumption. In the following, for a trace t ∈ (α M 2 ) * we denote S t := T ((M 1 ||t) × P).

Definition 13 (Semantic weakest assumption)
Let P be a property and let M 1 and M 2 be two communicating programs. A semantic weakest assumption A s with respect to M 1 , M 2 and P has the following property: ∀t ∈ (α M 2 ) * such that S t contains feasible traces, it holds that t ∈ T (A s ) iff M 1 ||t P.
That is, a semantic weakest assumption A s contains all words over the alphabet of M 2 that together with M 1 satisfy P non-vacuously. Note that there can be more than one such assumption, as we do not define its behavior for sets S t that include no infeasible traces. Proof We show that the assumption A from the proof of Lemma 6 is such a semantic weakest assumption. We prove that under the terms of Lemma 7, for every trace t ∈ (α M 2 ) * such that S t contains feasible traces, it holds that t ∈ T (A) iff M 1 ||t P.
Let t ∈ T (A). Then, according to the construction of A, and similar to the proof of Lemma 6, all paths in (M 1 ||t) × P reach an accepting state, and thus M 1 ||t P.
For the other direction, let t ∈ (α M 2 ) * such that M 1 ||t P. According to the terms of the lemma, it holds that either all traces in S t are not feasible, or that all of them are feasible. If the former holds, then we have no requirement on t.
If the latter holds, then, since M 1 ||t P, then all paths in (M 1 ||t) × P reach an accepting state, and due to the construction of A, it holds that t ∈ T (A).
Note that we restrict the traces in S t , since if S t contains both feasible and infeasible traces, then we can no longer guarantee that all paths of (M 1 ||t) × P reach an accepting state, even if M 1 ||t P. For general communicating programs, for which this restriction does not hold, we can use the proof as in Lemma 5 to show that there are cases in which there is no regular semantic weakest assumption, that is, every semantic weakest assumption is not regular.
As a special case of Lemma 7 we have the following.

Corollary 2
In case the behavior of (M 1 ||M 2 ) × P is deterministic in the sense that for every trace t ∈ (α M 2 ) * there is only one corresponding trace t ∈ (M 1 ||M 2 ) × P such that t ↓ α M 2 = t, then there exists a regular semantic weakest assumption.

The assume-guarantee-repair (AGR) framework
In this section we discuss our Assume-Guarantee-Repair (AGR) framework for communicating programs. The framework consists of a learning-based Assume-Guarantee algorithm, called AG L * , and a repair procedure, which are tightly joined.
Recall that the goal of L * in our case is to learn T (M 2 ), though we might terminate earlier if we find a suitable assumption for the AG rule: The nature of AG L * is such that the assumptions it learns before it reaches M 2 may contain the traces of M 2 and more, but still be represented by a smaller automaton. Therefore, similarly to [10], AG L * often terminates with an assumption A that is much smaller than M 2 . Indeed, our tool often produces very small assumptions (see Sect. 9).
As mentioned before, not only do we determine whether M 1 ||M 2 P, but we also repair the program in case it violates the specification. When M 1 ||M 2 P, the AG L * algorithm finds an error trace t as a witness for the violation. In this case, we initiate the repair procedure, which eliminates t from M 2 . repair applies abduction in order to learn a new constraint which, when added to t, makes the counterexample infeasible. 8 The new constraint enriches the alphabet in a way which may eliminate additional counterexamples from M 2 , by making them infeasible. We elaborate on our use of abduction in Sect. 8.3. The removal of t and the addition of the new constraint result in a new goal M 2 for AG L * to learn. Then, AG L * continues to search for a new assumption A that allows to verify M 1 ||M 2 P.
An important feature of our AGR algorithm is its incrementality. When learning an assumption A for M 2 we can use the information gathered in previous mqs and eqs, since the answer for them has not been changed (see Theorem 2 in Sect. 8.2). This allows the learning of M 2 to start from the point where the previous learning has left off, resulting in a more efficient algorithm.
As opposed to the case where M 1 ||M 2 P, we cannot guarantee the termination of the repair process in case M 1 ||M 2 P. This is because we are only guaranteed to remove one (bad) trace and add one (infeasible) trace in every iteration (although in practice, every iteration may remove a larger set of traces). Thus, we may never converge to a repaired system. Nevertheless, in case of property violation, our algorithm always finds an error trace, thus a progress towards a "less erroneous" program is guaranteed.
It should be noted that the AG L * part of our AGR algorithm deviates from the AG-rule of [10] in two important ways. First, since the goal of our learning is T (M 2 ) rather than L(A w ), our membership queries are different in type and order. Second, in order to identify real error traces and send them to repair as early as possible, we add additional queries to the membership phase that reveal such traces. We then send them to repair without ever passing through equivalence queries, which improves the overall efficiency. Indeed, our experiments include several cases in which all repairs were invoked from the membership phase. In these cases, AGR ran an equivalence query only when it has already successfully repaired M 2 , and terminated.

MQs & EQs and the implementation of the teacher
As our algorithm heavily relies on the L * algorithm, we begin with a description of membership and equivalence queries, both from the side of the learner and the teacher. When using L * in verification, the implementation of the teacher plays a major role. We therefore describe the queries issued by the learner, and for each query, we elaborate on how exactly the query is answered by the teacher. The pseudo-code for the teacher is given in Algorithm 1.

Membership queries
As a mq, the learner asks whether a given trace t is in the language of some suitable assumption A. The order of mqs is as in the L * algorithm, that is, we traverse traces in alphabetical order and increasing length. The teacher answers the mq as follows.
, check if t is an error trace, and if so, turn directly to repair. That is -If M 1 ||t P, pause learning and turn to repair.
-If M 1 ||t P, answer the mq with yes.
Note that not every membership query is answered immediately, since the learning process may pause for repairing the system. If a repair was issued on a trace t, then after repair, the teacher answers no on the mq on t, but it may provide additional information as we describe in Sect. 8.2.

Equivalence queries
As an eq, the learner asks, given assumption A, whether A is a suitable assumption to verify the correctness of the system. An eq is issued once the learner was able to construct an automaton that is consistent with all previous mqs, as done in L * algorithm. The teacher answers an eq as follows. If t is not a real error trace, return no to the eq , along with a trace to be eliminated from A.
Note that, as with mqs, not every eq is immediately answered. In case of a violation, the algorithm first repairs the system, and only then the teacher returns no to the eq (as the queried A contained a real error trace). Also as in mqs, the repair stage might provide the learner with additional information (Sect. 8.2). Algorithm 1 below presents the pseudo-code of the teacher when answering the two types of queries. While M 1 and P remain unchanged, M 2 is iteratively repaired (if needed). The teacher therefore considers at iteration i, the repaired program M i 2 .

Algorithm 1
The AG L * Teacher if M 1 ||t 2 P then 4: let t ∈ (M 1 ||t 2 ) × P be an error trace t is a cex 5: proving M 1 ||M i 2 P 6: return repairt 7: else return yes M 1 ||t 2 P 8: else return no t 2 / ∈ T (M i 2 ) 9: function eq-oracle (candidate assumption A i j ) 10: if M 1 ||A i j P then 11: if T (M i 2 ) ⊆ T (A i j ) then return yes 12: else return not A

The assume-guarantee-repair (AGR) algorithm
We now describe our AGR algorithm in more detail (see Algorithm 2). Figure 6 describes the flow of the algorithm. AGR comprises two main parts, namely AG L * and repair.
The input to AGR are the components M 1 and M 2 , and the property P. While M 1 and P stay unchanged during AGR, M 2 keeps being updated as long as the algorithm recognizes that it needs repair.
The algorithm works in iterations, where in every iteration the next updated M i 2 is calculated, starting with iteration i = 0, where M 0 2 = M 2 . An iteration starts with the membership phase in line 4 of Algorithm 2, and ends either when AG L * successfully terminates (Algorithm 2 line 12) or when procedure repair is called (Algorithm 2 lines 7 and 15). When a new system M i 2 is constructed, AG L * does not start from scratch. The information that has been learned in previous iterations is still valid for M i 2 . The new iteration is given additional new trace(s) that have been added or removed from the previous M i 2 (Algorithm 2 lines 8, 16). AG L * consists of two phases: membership, and equivalence. In the membership phase (lines 4-9 of Algorithm 2), the algorithm issues mqs as calls to the function mqoracle of Algorithm 1. If, during the membership phase, we encounter a trace t 2 ∈ M i 2 that in parallel with M 1 does not satisfy P, then t 2 is a bad behavior of M 2 , and repair is invoked. To this end, we enhance the answers of the teacher not only to yes and no, but also to repair. This holds also for eqs.
hold. In this case, AGR terminates and returns M i 2 as a successful repair of M 2 . If M 1 ||A i j P, then a counterexample t is returned, that is composed of bad traces in M 1 , A i j , and P. If the bad trace t 2 , the restriction of t to the alphabet of A i j , is also in M i 2 , then t 2 is a bad behavior of M i 2 , and here too repair is invoked. Otherwise, AGR updated the L * table, returns to the membership phase, and continues to learn A i j . As we have described, repair is called when a bad trace t is found in (M 1 ||M i 2 ) × P and should be removed. If t contains no constraints then its sequence of actions is illegal and its restriction t 2 ∈ T (M i 2 ) should be removed from M i 2 . In this case, repair returns to AG L * and updates the learning goal to be T (M i+1 along with the answer "t 2 " that indicates that t 2 should not be a part of the learned assumption. In Sect. 8.4 we discuss different methods for removing t 2 from M i 2 . 9 The more interesting case is when t contains constraints. In this case, we not only remove the matching t 2 from M i 2 , but also add a new constraint c to the alphabet, which causes t 2 to be infeasible. This way we eliminate t 2 , and may also eliminate a family of bad traces that violate the property in the same manner-adding a new constraint can only cause the removal of additional error traces, and cannot add traces to the system. We deduce c using abduction, as we describe in Sect. 8.3. As before, repair returns to AG L * with a new goal to be learned, but now also with an extended alphabet. In addition, we are provided with information about two traces: while L * learner did not converge to a candidate assumption do issue mqs 5:

Algorithm 2 AGR
let t 2 ∈ (α M i 2 ) * chosen according to the L * learner 6: if mq-oracle (t 2 ) = repairt then 7: add -t 2 to the L * table and in case of abduction, add +t 2 to the L * table 9: else add t 2 to the L * table according to the answer of the mq 10: let A i j be the candidate assumption generated by the L * learner issue eq 11: if eq-oracle (A i j ) = yes then 12: return M 1 ||M i 2 P together with the assumption A i j 13: else 14: if eq-oracle (A i j ) = repair -t then 15: add -t 2 to the L * table and in case of abduction, add +t 2 to the L * table 17: else eq-oracle (A i j ) = no ± t 18: let t 2 be the cex from the eq 19: add t 2 to the L * table according to the answer of the eq after issuing an eq, the algorithm returns to the mqs phase 20: if t does not contain constraints then 24: set M i+1 returnt 2 , + t 2 t 2 that should not be included in the new assumption, and (t 2 · c) that should be included.

Incremental learning
One of the advantages of AGR is that it is incremental, in the sense that answers to membership queries from previous iterations remain unchanged for the repaired system. Formally, we have the following. From Theorem 2 it follows that, in particular, T i is consistent with (1) traces that are removed between M i 2 and M i+1 2 ; and (2) traces that are learned using abduction and added to M i+1 2 .

Theorem 2 Assume that T i is the L *
Proof Traces are added to the L * table in three scenarios: during mqs; as counterexamples to eqs; and while repairing the system, removing error traces and adding traces learned by abduction. The difference between M i 2 and M i+1 2 is only due to the repair part, that is, in error traces that are removed and traces that were added due to abduction. The two components agree on all other traces, and so T i is consistent with all traces in M i+1 2 that are not part of repair.
2 ) be an error trace that was removed during repair. We consider the two cases-in which repair is invoked during a mq, or during an eq.
In the former case, this is the first time the L * learner queries t 2 , as the L * learner does not issue mqs on traces that are already in the table. Therefore, t 2 is not part of T i yet, and there is no inconsistency.
If t 2 is an error trace that is found during an eq, then M 1 ||t 2 P (Algorithm 1, line 16). Since whether M 1 ||t 2 P only depends on M 1 , P and the specific trace t 2 , and not on M i 2 , and since M 1 and P remain constant, it holds that t 2 was an error trace also in previous iterations. Therefore, t 2 cannot appear in T i as a positive trace. This concludes the case of traces that are removed from M i 2 during repair. Now, let t 2 be a positive trace added to M i+1 2 using abduction, that is t 2 = t 2 · c for t 2 that was removed by repair. If the constraint c is a new alphabet letter, then in particular t 2 is not over the alphabet of M i 2 and cannot appear in T i . We now consider the case in which the added constraint c is already a part of α M i 2 . If the error trace t 2 was found during a mq, then, since the L * learner issues queries in an increasing-length order, it holds that t 2 was not queried in a mq in previous iterations as |t 2 | < |t 2 |. Moreover, it cannot be the case that t 2 was a negative counterexample to an eq since these are only derived from error traces (Algorithm 1, lines [16][17][18][19], and t 2 is not an error trace. The same argument holds also if t 2 was found during an eq.
We are left to show that if t 2 was found during an eq, it cannot be the case that t 2 was previously queried during a mq. In this case it holds that the L * learner did not issue a mq on t 2 . Since the L * learner issues queries in increasing length, and since |t 2 | < |t 2 |, it holds that t 2 was not previously queried.

Semantic repair by abduction
We now describe our repair of M i 2 , in case the error trace t contains constraints (Algorithm 2 line 27). Error traces with no constraints are removed from M i 2 syntactically (Algorithm 2 lines 24-26), while in abduction we semantically eliminate t by making it infeasible. The new constraint is then added to the alphabet of M i 2 and may eliminate additional error traces. Note that the constraints added by abduction can only restrict the behavior of M 2 , making more traces infeasible. Therefore, we do not add counterexamples to M 2 .
The process of inferring new constraints from known facts about the program is called abduction [12]. We now describe how we apply it. Given a trace t, let ϕ t be the first-order formula (a conjunction of constraints), which constitutes the SSA representation of t [3]. In order to make t infeasible, we look for a formula ψ such that ψ ∧ ϕ t → false. 10 Example 4 Consider the component M 2 of Fig. 1 and the component M 1 and specification P of Fig. 2 from Sect. 1. The following t is an error trace in (M 1 ||M 2 ) × P: t = (read?x pw , 999 < x pw , (enc?y pw , enc!x pw ), y pw = 2 · y pw , (getEnc!y pw , getEnc?x pw2 ), x pw = x pw2 , y pw ≥ 2 64 ) due to the execution (read?2 63 , 999 < 2 63 , (enc?2 63 , enc!2 63 ), y pw = 2 · 2 63 , 10 Usually, in abduction, we look for ψ such that ψ ∧ ϕ t is not a contradiction. In our case, however, since ϕ t is a violation of the specification, we want to infer a formula that makes ϕ t unsatisfiable.
We then look for a constraint ψ that will make the sequence t, and in particular the violation of the last constraint y pw ≥ 2 64 , infeasible.
Note that t ∈ T (M 1 ||M i 2 ) × P, and so it includes variables both from X 1 , the set of variables of M 1 , and from X 2 , the set of variables of M i 2 . Since we wish to repair M i 2 , the learned ψ is over the variables of X 2 only. In Example 4, this corresponds to learning a formula over x pw .
The formula ψ ∧ ϕ t → false is equivalent to ψ → (ϕ t → false). Then, ψ = ∀x ∈ X 1 : (ϕ t → false) = ∀x ∈ X 1 (¬ϕ t ), is such a desired constraint: ψ makes t infeasible and is defined only over the variables of X 2 . We now use quantifier elimination [46] to produce a quantifier-free formula over X 2 . Computing ψ is similar to the abduction suggested in [12], but the focus here is on finding a formula over X 2 rather than over any minimal set of variables as in [12]; in addition, in [12] they look for ψ such that ϕ t ∧ ψ is not a contradiction, while we specifically look for ψ that blocks ϕ t . We use Z3 [11] to apply quantifier elimination and to generate the new constraint.

Example 5
For t of Example 4, the process described above results in the constraint ψ = x pw < 2 63 . Note that while ψ blocks the erroneous behavior of t, it allows all executions of t in which x pw is assigned with smaller values than 2 63 . In addition, it does not only block the one execution in which x pw = 2 63 , but the set of all erroneous executions in (M 1 ||M 2 ) × P of the example.
After generating ψ(X 2 ), we add it to the alphabet of M i 2 (line 29 of Algorithm 2). In addition, we produce a new trace t 2 = t 2 · ψ(X 2 ). The trace t 2 is returned as the output of the abduction.
We now turn to prove that by making t 2 infeasible, we eliminate the error trace t.

Lemma 8 Let t = (t 1 ||t 2 ) × t P . If t 2 is infeasible, then t is infeasible as well.
Proof This is due to the fact that t P can only restrict the behaviors of t 1 and t 2 , thus if t 2 is infeasible, t cannot be made feasible. Formally, Lemma 8 follows from Lemma 3 and Lemma 4 given in Sect. 6. By Lemma 3, if t = (t 1 ||t 2 ) × t P is feasible, then t 1 ||t 2 is a feasible trace of M 1 ||M 2 . By Lemma 4, if t 1 ||t 2 is feasible, then t 2 is feasible as well. Therefore, if t 2 is infeasible, then t is infeasible, proving Lemma 8.
In order to add t 2 · ψ(X 2 ) to M i 2 while removing t 2 , we split the state q that t 2 reaches in M i 2 into two states q and q , and add a transition labeled ψ(X 2 ) from q to q , where only q is now accepting (see Fig. 7). Thus, we eliminate the Fig. 7 Adding the constraint ψ(X 2 ) to block the error trace t 2 . Note that ψ is also added to traces other than t 2 , for example to t . Then, ψ(X 2 ) blocks assignments of t that violate P in the same way as t 2 , but it allows for other assignments of t to hold repair now returns to AG L * with the negative example-t 2 and the positive example +t 2 to add to the L * table T i+1 of the next iteration, in order to learn an assumption for the repaired component M i+1 2 (which includes t 2 but not t 2 ). Figure 8 presents the repaired component M 1 2 we generate given M 2 of Fig. 1 and M 1 and P of Fig. 2. As there is only one error trace (that induces many erroneous executions), the repaired component is achieved after one iteration. The new constraint, ψ = x pw < 2 63 , is added at the end of the trace t 2 = t ↓ α M i 2 for t of Example 4. Intuitively, this constraint is equivalent to adding an assume statement in the program.

Syntactic removal of error traces
Recall that the goal of repair is to remove a bad trace t 2 from M 2 once it is found by AG L * . If t 2 contains constraints, we remove it by using abduction as described in Sect. 8.3. Otherwise, we can remove t 2 by constructing a system whose language is T (M 2 ) \ {t 2 }. We call this the exact method for repair. However, removing a single trace at a time may lead to slow convergence, and to an exponential blow-up in the sizes of the repaired systems. Moreover, as we have discussed, in some cases there are infinitely many such traces, in which case AGR may never terminate.
For faster convergence, we have implemented two additional heuristics, namely approximate and aggressive. These heuristics may remove more than a single trace at a time, while keeping the size of the systems small. While "good" traces may be removed as well, the correctness of the repair is maintained, since no bad traces are added. Moreover, an error trace is likely to be in an erroneous part of the system, and in these cases our heuristics manage to remove a set of error traces in a single step.
We survey the three methods.
-Exact. To eliminate only t 2 from M 2 , we construct the program M t 2 that accepts only t 2 , and complement it to constructM t 2 that accepts all traces except for t 2 . Finally, we intersectM t 2 with M 2 . This way we only eliminate t 2 , and not other (possibly good) traces. On the other hand, this method converges slowly in case there are many error traces, or does not converge at all if there are infinitely many error traces. -Approximate Similarly to our repair via abduction in Sect. 8.3, we prevent the last transition that t 2 takes from reaching an accepting state. Let q be the state that M 2 reaches when reading t 2 . We mark q as a non-accepting state, and add an accepting state q , to which all in-going transitions to q are diverted, except for the last transition on t 2 . This way, some traces that lead to q are preserved by reaching q instead, and the traces that share the last transition of t 2 are eliminated along with t 2 . As we have argued, these transitions may also be erroneous. -Aggressive In this simple method, we remove q, the state that M 2 reaches when reading t 2 , from the set of accepting states. This way we eliminate t 2 along with all other traces that lead to q. In case that every accepting state is reached by some error trace, this repair might result in an empty language, creating a trivial repair. However, our experiments show that in most cases, this method quickly leads to a non-trivial repair.

Towards convergence of syntactic repair
As discussed above, the exact repair may not terminate in case of infinitely many traces that introduce the same error. Indeed, we now claim that when provided with long-enough counterexamples, we can conclude that the exact repair will not converge, and justifiably turn to the other repair types.
In the following, we claim that once a long-enough error trace is found, it is induced from some run in the underlying automaton, that contains a cycle. Then, all similar traces that follow the same sequence of states are also error traces, and the cycle induces infinitely many error traces that cannot be removed together using the exact repair method. We now formalise this intuition.
We make use of the pumping lemma for regular languages, as stated in the following claim.
Claim Let L be a regular language and let A be a finite state automaton for L, with n states. Let z ∈ L such that |z| > n. Then, we can write z = uvw such that for every i ≥ 0, it holds that uv i w ∈ L.
Lemma 9 Let t ∈ T ((M 1 ||M 2 ) × P) be an error trace without constraints. Let N be the number of states in (M 1 ||M 2 ) ×P. Then, if |t| > N , it induces an error trace t 2 ∈ T (M 2 ), such that we can write t = uvw for |v| > 0 and for every i it holds that t 2 i := uv i w also corresponds to an error trace.
Proof Denote M = (M 1 ||M 2 )× P. Let t ∈ T (M) be an error trace such that |t| > N . Then, the run of M on t contains a cycle. Since M is the composition of three components, there is a state ( p, q, r ) of M that appears more than once on the run of M on t. Let us denote such that the run of M on t visits the state ( p, q, r ) when reading t[ j] and t [k]. Consider the partition t = u · v · w where Then, using the argument of the pumping lemma, it holds that t i := uv i w is a trace of M, that reaches the same state as t, for every i ≥ 0. Now, note that for every trace t that does not contain constraints, and for every system M , it holds that t is a trace of M iff t is a feasible trace of M . In particular, since t does not contain constraints, then t i is a feasible trace of M for every i, and therefore is an error trace for every i. In particular, it holds that t 2 i = t i ↓ α M 2 is an error trace in M 2 and should be eliminated, for every i. Lemma 9 proves that once a long-enough error trace t is detected, in case that t does not contain constraints, then it induces infinitely many error traces. Thus, the exact repair process will never terminate. Note that in this scenario, the approximate repair can fix the system, as it diverts all traces of the same nature to a non-accepting state.
We remark that the same reasoning cannot be applied to traces with constraints. where (x := x + 1) 90 means repeating the letter (x := x + 1) for 90 times.
The trace t is of length 93, and it is an error trace in the system (M 1 ||M 2 ) × P, which is of size at most 9. 11 Then, 11 In fact, when composing the systems, the resulting system is of size 5. even-though |t| > 9, we cannot decompose it as stated in the claim. This, since after applying x := x + 1 more than 100 times, it will no longer be an error trace of the system.
Following the discussion in Sect. 7.2, note that if all the three components-M 1 , M 2 , and P do not contain constraints, then only syntactic queries are needed, and only syntactic repair is applied.

Correctness and termination
For this discussion, we assume a sound and complete teacher who can answer the membership and equivalence queries in AG L * , which require verifying communicating programs and properties with first-order constraints. Our implementation uses Z3 [11] in order to answer satisfiability queries issued in the learning process. The soundness and completeness of Z3 depend on the underlying theory (induced by the program statements we allow). For first-order linear arithmetic over the reals, as we consider in this work, this is indeed the case. However, our method can be applied to all theories for which there exists a sound solver. 12 As we have discussed earlier, AGR is not guaranteed to terminate, due to its repair part. There are indeed cases for which the repair stage may be called infinitely many times. However, in case that no repair is needed, or if a repaired system is obtained after finitely many calls to repair , then AGR is guaranteed to terminate with a correct answer.
To see why, consider a repaired system M i 2 for which M 1 ||M i 2 P. Since the goal of AG L * is to learn T (M i 2 ), which is (syntactically) regular, this stage will terminate at the latest when AG L * learns exactly T (M i 2 ) (it may terminate sooner if a smaller appropriate assumption is found). Notice that, in particular, if M 1 ||M 2 P, then AGR terminates with a correct answer in the first iteration of the verify-repair loop.
repair is only invoked when a (real) error trace t 2 is found in M i 2 , in which case a new system M i+1 2 , that does not include t 2 , is produced by repair. If M 1 ||M i 2 P, then an error trace is guaranteed to be found by AG L * either in the membership or equivalence phase. Therefore, also in case that M 1 ||M i 2 violates P, the iteration is guaranteed to terminate.
In particular, since every iteration of AGR finds and removes an error trace t 2 , and no new erroneous traces are introduced in the updated system, then in case that M 2 has finitely many error traces, AGR is guaranteed to terminate with a repaired system, which is correct with respect to P. 13 To conclude the above discussion, Theorem 3 formally states the correctness and termination of the AGR algorithm. Recall that in Algorithm 2 we set M 0 2 := M 2 and that M i 2 is the repaired component after i iterations of repair. The proof of Theorem 3 follows from Lemmas 10, 11, and 12, given below.

Lemma 10
Every iteration i of the AGR algorithm terminates. In addition, unless repair is invoked, answers to mqs and eqs are consistent with T (M i 2 ). 14 That is, whenever the AG L * teacher (Algorithm 1) returns yes for a mq on t 2 or +t 2 as a counterexample for an eq, then indeed t 2 ∈ T (M i 2 ); and whenever the AG L * teacher returns no for a mq or t 2 as a counterexample for an eq, then indeed t 2 / ∈ T (M i 2 ).
Proof Consistency of mqs with M i 2 is straight forward as the mq-oracle (Algorithm 1) answers mqs according to membership in T (M i 2 ), or invokes repair. The same holds for eqs -in case repair is not invoked, eq-oracle returns 13 Note that finitely many error traces might induce infinitely many erroneous executions, that are all eliminated together when we eliminate t 2 . 14 If repair is invoked then, as we remove the trace from M i 2 , the answer will not be consistent with M i 2 , but it will be consistent with M i+1 2 .
the counterexample +t 2 (Algorithm 1 line 14) if it is in M i 2 but not part of the assumption, and it returns t 2 (Algorithm 1 line 19) if it is in the assumption but not in M i 2 . If repair was not invoked, we have that since both types of queries are consistent with T (M i 2 ), which is a regular language, the current iteration terminates due to the correctness of L * . If repair was invoked, then obviously the iteration terminates, by calling repair. Also note that repair is only called when a real error is found, that is, a trace t 2 ∈ T (M i 2 ) such that M 1 ||t 2 P. , and from the correctness of L * algorithm we conclude that the algorithm will eventually learn T (M i 2 ). Note that in case that M 1 ||M 2 P and that AGR learned , then the the eq-oracle returns yes (Algorithm 1 line 11) and the algorithm terminates with the assumption A i as a proof of correctness (Algorithm 2 line 12).
Assume that M 1 ||M i 2 P. Then there exists an error trace t ∈ (M 1 ||M i 2 ) × P. From Lemmas 3, 4 it holds that In particular, it holds that t is an error trace of (M 1 ||t 2 ) × P. Thus, M 1 ||t 2 P. Since AGR converges towards T (M i 2 ) (by Lemma 10), either t 2 shows up as an mq, and the mq-oracle indicates that repair is needed (Algorithm 1 lines 2-6); Or AGR comes up with a candidate assumption and issues an eq on it. There, again, t 2 (or some other error trace) will come up as an error trace t 2 ∈ T (M i 2 ), and the eq-oracle will indicate that repair is needed (Algorithm 1 lines [15][16][17][18]. Note that although each phase converges towards T (M i 2 ), it may terminate earlier. We show that in case that the algorithm terminates before finding T (M i 2 ), it returns the correct answer.

P.
Proof Item 1. Assume AGR returns an assumption A. Then there exists i such that T (M i 2 ) ⊆ T (A) and M 1 ||A P, since this is the only scenario in which an assumption A is returned (Algorithm 1 lines 10-11 and Algorithm 2 lines [11][12]. From the soundness of the AG rule for communicating systems (Theorem 1) it holds that M 1 ||M i 2 P. Item 2. Assume now that a phase i of AGR ends with finding an error trace t (and a call to repair). We prove that M 1 ||M i 2 P. First note that AGR may output such a trace both while making a mq and while making an eq. If t was found during a mq (Algorithm 1 lines 3-6), then there exists t 2 ∈ T (M i 2 ) such that M 1 ||t 2 P, and t ∈ (M 1 ||t 2 ) × P. Since t 2 ∈ T (M i 2 ), it holds that t is also an error trace of (M 1 ||M i 2 ) × P, proving M 1 ||M i 2 P. If t was found during an eq (Algorithm 1 lines 15-18), then t is an error trace in . This makes t an error trace of (M 1 ||M i 2 )× P as well, thus M 1 ||M i 2 P . This concludes the proof.
The proof of Theorem 3 follows almost directly from the lemmas above.

Proof of Theorem 3
Lemma 11 states that if M 1 ||M i 2 P then AGR terminates with the correct answer. This implies item 1 and item 2.
In addition, Lemma 11 states that if M 1 ||M i 2 P then AGR finds an error trace witnessing the violation. Once such an error trace is found, repair is invoked. Since repair eliminates at least one error trace, the system post repair contains fewer error traces, and item 4 follows.
Lemma 12 states that if an iteration i of AGR ends with an error trace, then M 1 ||M i 2 P. This implies item 3.

Experimental results
We implemented our AGR framework in Java, integrating the L * learner implementation from the LTSA tool [28]. We used Z3 [11] to implement calls to the teacher while answering the satisfaction queries in Algorithm 1, and for abduction in repair. Table 1 displays results of running AGR on various examples, varying in their sizes, types of errors-semantic and syntactic, and the number of errors. Additional results are available in [16]. The examples are available on [1]. The iterations column indicates the number of iterations of the verify-repair loop, until a repaired M 2 is achieved. Examples with no errors were verified in the first iteration, and are indicated by verification. We experimented with the three repair methods described in Sect. 8.4. Figure 10 presents comparisons between the three methods in terms of run-time and the size of the repair and assumptions. The graphs are given in logarithmic scale.
Most of our examples model multi-client-server communication protocols, with varying sizes. Our tool managed to repair all those examples that were flawed.
As can be seen in Table 1, our tool successfully generates assumptions that are significantly smaller than the repaired and the original M 2 .
For the examples that needed repair, in most cases our tool needed 2-5 iterations of verify-repair in order to successfully construct a repaired component. Interestingly, in example #15 the aggressive method converged slower than the approximate method. This is due to the structure of M 2 , in which different error traces lead to different states. Marking these states as non-accepting removed each trace separately. However, some of these traces have a common transition, and preventing this transition from reaching an accepting state, as done in the approximate method, managed removing several error traces in a single repair.
Example #22 models a simple structure in which, due to a loop in M 2 , the same alphabet sequence can generate infinitely many error traces. The exact repair method timed out, since it attempted removing one error trace at a time. On the other hand, the aggressive method removed all accepting states, creating an empty program-a trivial (yet valid) repair. In contrast, the approximate method created a valid, non-trivial repair.

Example 8
As long as the system needs repair, no assumption can be learned. When we reach a correct M 2 , we are usually able to find a smaller assumption that proves the correctness of M 1 ||M 2 with respect to P. Our tool preforms the best on examples in the spirit of M 1 and P of Fig. 11 and M 2 of Fig. 12. There, the specification P allows all traces in which first M 2 acts on one of its channels (G 1 , G 2 or G 3 ), and then M 1 acts on its channel (C). The program M 2 in Fig. 12 is more restrictive than P requires-once the variable x is read through some channel, M 2 continues to use only this channel. M 1 ||M 2 P due to the restriction on their synchronization using sync 1 and sync 2 . We are then able to learn the assumption A of Fig. 13, which is much smaller than M 2 , and allows proving the correctness of M 1 ||M 2 with respect to P.  Fig. 8. In this case, we learn an assumption with 5 states, that is the result of merging states q 1 and q 2 in M 1 2 . This is since we answer queries according to M 1 2 , which has a more unique structure than the structure of M 2 of Fig. 12. This demonstrates the behavior of example #19 in Table 1.

Conclusion and future work
We have presented the model of communicating programs, which is able to capture program behavior and synchronization between the system components, while exploiting a finite automata representation in order to apply automata learning. We then presented AGR, which offers a new take on the learning-based approach to assume-guarantee verification, by managing to cope with complex properties and by also repairing infinite-state programs.
Our experimental results show that AGR can produce very succinct proofs, and can repair flawed communicating programs efficiently. AGR leverages the finite automata-like representation of the systems in order to apply the L * algorithm and to learn small proofs of correctness.
We prove that in general, the weakest assumption that is often used for compositional verification, is not regular for the case of communicating programs, and we come up with a new goal for the learning process. In addition, we find types of communicating programs for which the weakest assumption is regular. We leave finding the full characterization of programs for which the weakest assumption is regular for future work.
In this work, we repair the system by eliminating error traces, and locate new constraints learned using abduction, at the end of the error trace, in order to make it infeasible. A possible extension of this process is to wisely locate constraints over the error trace. Intuitively, we would like the constraint to be as "close" as possible to the error location. However, this is not a trivial task, as the error can be the result of multiple actions of the two communicating programs. Another extension to the repair process is changing the program behavior, rather than blocking it. Examples for such a mutation-based approach to program repair are [6,36,42].
For syntactic repair, we characterize cases in which the repair process does not converge. This may happen also in the case of semantic repair, in which infinitely many new constraints are learned and the repair process does not terminate. As future work, we intend to incorporate invariant generation, according to reoccurring error traces, in order to help the convergence of the semantic repair process.
Funding Open Access funding enabled and organized by Projekt DEAL.
Open Access This article is licensed under a Creative Commons Attribution 4.0 International License, which permits use, sharing, adaptation, distribution and reproduction in any medium or format, as long as you give appropriate credit to the original author(s) and the source, provide a link to the Creative Commons licence, and indicate if changes were made. The images or other third party material in this article are included in the article's Creative Commons licence, unless indicated otherwise in a credit line to the material. If material is not included in the article's Creative Commons licence and your intended use is not permitted by statutory regulation or exceeds the permitted use, you will need to obtain permission directly from the copyright holder. To view a copy of this licence, visit http://creativecomm ons.org/licenses/by/4.0/.