Anatomy of a product

In this notebook, we write a Python function that constructs the product of two automata.

This is obviously not a new feature: Spot can already make a product of two automata using its product() function.
For instance:

The builtin spot.product() function produces an automaton whose language is the intersection of the two input languages. It does so by building an automaton that keeps track of the runs in the two input automata. The states are labeled by pairs of input states so that we can more easily follow what is going on, but those labels are purely cosmetic. The acceptance condition is the conjunction of the two acceptance condition, but the acceptance sets of one input automaton have been shifted to not conflict with the other automaton.

In fact, that automaton printer has an option to shift those sets in its output, and this is perfect for illustrating products. For instance a.show('+3') will display a1 with all its acceptance sets shifted by 3.

Let's define a function for displaying the three automata involved in a product, using this shift option so we can follow what is going on with the acceptance sets.

Building a product

Let's now rewrite product() in Python. We will do that in three steps.

First attempt

First, we build a product without taking care of the acceptance sets. We just want to get the general shape of the algorithm.

We will build an automaton of type twa_graph, i.e., an automaton represented explicitely using a graph. In those automata, states are numbered by integers, starting from 0. (Those states can also be given a different name, which is why the the product() shows us something that appears to be labeled by pairs, but the real identifier of each state is an integer.)

We will use a dictionary to keep track of the association between a pair (ls,rs) of input states, and its number in the output.

Besides the obvious lack of acceptance condition (which defaults to t) and acceptance sets, there is a less obvious problem: we never declared the set of atomic propositions used by the result automaton. This as two consequences:

These two issues are fixed by either calling p1.register_ap(...) for each atomic proposition, or in our case p1.copy_ap_of(...) to copy the atomic propositions of each input automaton.

Second attempt: a working product

This fixes the list of atomtic propositions, as discussed above, and also sets the correct acceptance condition. The set_acceptance method takes two arguments: a number of sets, and an acceptance function. In our case, both of these arguments are readily computed from the number of states and acceptance functions of the input automata.

Third attempt: a more usable product

We could stop with the previous function: the result is a correct product from a theoretical point of view. However our function is still inferior to spot.product() in a couple of points:

The former point could be addressed by calling set_state_names() and passing an array of strings: if a state number is smaller than the size of that array, then the string at that position will be displayed instead of the state number in the dot output. However we can do even better by using set_product_states() and passing an array of pairs of states. Besides the output routines, some algorithms actually retrieve this vector of pair of states to work on the product.

Regarding the latter point, consider for instance the deterministic nature of these automata. In Spot an automaton is deterministic if it is both existential (no universal branching) and universal (no non-deterministic branching). In our case we will restrict the algorithm to existantial input (by asserting is_existential() on both operands), so we can consider that the prop_universal() property is an indication of determinism:

Because a1 and a2 are deterministic, their product is necessarily deterministic. This is a property that the spot.product() algorithm will preserve, but that our version does not yet preserve. We can fix that by adding

if left.prop_universal() and right.prop_universal():
    result.prop_universal(True)

at the end of our function. Note that this is not the same as

result.prop_universal(left.prop_universal() and right.prop_universal())

because the results the prop_*() family of functions take and return instances of spot.trival values. These spot.trival, can, as their name implies, take one amongst three values representing yes, no, and maybe. yes and no should be used when we actually know that the automaton is deterministic or not (not deterministic meaning that there actually exists some non determinitic state in the automaton), and maybe when we do not know.

The one-liner above is wrong for two reasons:

The reason maybe and no is equal to maybe is that Python evaluate it like no if maybe else maybe, but when a trival is evaluated in a Boolean context (as in if maybe) the result is True only if the trival is equal to yes.

So our

if left.prop_universal() and right.prop_universal():
    result.prop_universal(True)

is OK because the if body will only be entered of both input automata are known to be deterministic.

However the question is in fact more general than just determinism: the product of two weak automata is weak, the product of two stutter-invariant automata is stutter-invariant, etc. So when writing an algorithm one should consider which of the property bits are naturally preserved by the algorithm, and set the relevant bits: this can save time later if the resulting automaton is used as input for another algorithm.

For development, it is useful to know that we can force the automaton printer to show the real state numbers (not the pairs) by passing option 1, and that we can retrieve the associated pairs ourselves. Note that the pairs also appear as tooltips when we mouse over the states.

Timings

As an indication of how slow it is to implement such an algorithm using the Python bindings of Spot, consider the following comparison:

Depending on the machine where this notebook has been run, using the C++ version of the product can be 1 to 2 orders of magnitude faster. This is due to all the type conversions (converting Python types to C++ types) that occurs everytime a function/method of Spot is called from Python. When calling high-level C++ functions (such as spot.product()) from Python, the overhead is negligible because most of the time is spent on the C++ side, actually executing the function. However when calling low-level functions (such as new_edge(), new_state(), out()) most of the time is spent converting the arguments from Python to C++ and the results from C++ to Python.

Despite that speed difference, Python can be useful to prototype an algorithm before implementing it in C++.

The case of weak automata

Finally, note that spot.product() actually does a bit more: it has some specializations for when one of the argument of the product is marked as weak (as indicated by prop_weak()). In this case, the resulting acceptance condition can be simplified a bit.