diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ad089e..1fa89ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,34 @@ brute-force reference or a closed-form value and has a regression test. (new module `sofic.shifts.canonical_presentations`) present a sofic shift by the canonical NFAs of its factor language, trimmed to the essential states. +### Distromaton, xor automata, and the universal automaton + +- New module `sofic.automata.canonical.closures` completes the canonical NFAs of Myers, Adámek, Milius & Urbat + (CMCS 2014) and Zetzsche, van Heerdt, Sammartino & Silva (MFPS 2021): `Distromaton` (join-irreducible + intersections of residuals), `minimal_xor_automaton`, `xor_caba_automaton` (the new acceptor of Zetzsche et al., + on a basis of the Boolean closure read as a GF(2) space), `UniversalAutomaton` (Lombardy & Sakarovitch), + `minimal_structured_dfa` (the minimal DFA whose states form a semilattice, distributive lattice, Boolean algebra + or GF(2) space), and `is_intersection_closed` (Myers et al.'s criterion for the canonical RFSA to be a + state-minimal NFA). Every closure element is stored exactly as a set of atoms, i.e. of states of the minimal + DFA of the reversed language, so derivatives are preimages and all constructions are exact. +- New `sofic.automata.xor.XorAutomaton`: NFA-shaped automata that accept by parity of accepting paths, with the xor + subset construction (`determinize`), `reverse` and serialization. It is not a `LabeledAutomaton`, whose language + operations assume NFA acceptance. +- Tests reproduce the papers' examples ((a+b)\*a and (a+b)\*b(a+b)^n), check the distromaton against Myers et al.'s + second construction (the reverse minimal DFA ordered by inclusion), the minimal xor automaton's size against the + GF(2) rank of a brute-force Hankel matrix, and Corollaries 3.21 and 4.6. Two corrections to the papers: the + xor-CABA example of Zetzsche et al. (Fig. 10) uses four linearly dependent vectors where a basis has three, and + Myers et al.'s remark that {aa} is not intersection-closed contradicts their Example 4.3 (singletons are). +- Shifts: `distromaton_presentation`, `universal_presentation` (also `SoficShift` methods), and + `follower_sets_intersection_closed`, which certifies that `canonical_rfsa_presentation` has the fewest states of + any presentation (brute-force checked against every smaller labeled graph). Canonical presentations now trim the + automaton before building the graph, which the new automata need (their `Σ*` and `∅` states) and which leaves + the existing presentations unchanged. +- Generators: `canonical_rfsa_hmm` and `maximized_prime_atomaton_hmm` are now documented as size-minimal among + generators whose state morphs are mixtures of causal-state morphs, the stochastic counterpart of the canonical + RFSA's minimality, with proof; the minimal xor automaton is cross-referenced as the GF(2) counterpart of + `minimal_quasi_realization`. + ### Generator verification - `GenerativeModelResult` records the returned generator's word-process deviation from the source diff --git a/docs/automata/automata.rst b/docs/automata/automata.rst index 7c38409..655e490 100644 --- a/docs/automata/automata.rst +++ b/docs/automata/automata.rst @@ -44,6 +44,7 @@ Canonical structures rfsa atomaton + canonical_closures observation_table learning wheeler diff --git a/docs/automata/canonical_closures.rst b/docs/automata/canonical_closures.rst new file mode 100644 index 0000000..eec36c6 --- /dev/null +++ b/docs/automata/canonical_closures.rst @@ -0,0 +1,114 @@ +.. canonical_closures.rst +.. py:module:: sofic.automata.canonical.closures + +************************************************** +Distromaton, xor automata, and universal automaton +************************************************** + +A regular language has no unique state-minimal NFA, but it has several *canonical* ones. Myers, Adámek, Milius and +Urbat :cite:`Myers2014,Myers2015` obtain them all by one recipe: close the residuals :math:`u^{-1}L` under the +operations of an algebraic variety, which gives the minimal deterministic automaton of :math:`L` whose states form +an algebra of that variety (:func:`minimal_structured_dfa`), and take a minimal set of generators of that algebra +as the states of an NFA. Zetzsche, van Heerdt, Sammartino and Silva :cite:`Zetzsche2021` recast the second step as +generators for algebras over a monad and let the algebra of the deterministic automaton differ from the side effect +of the succinct one through distributive law homomorphisms, which brings in the átomaton and a new acceptor, the +minimal xor-CABA automaton. + +========================== ========================== ====================== =========================================== +algebra of the DFA closure of the residuals states of the NFA canonical automaton +========================== ========================== ====================== =========================================== +join-semilattice unions join-irreducibles canonical RFSA (*jiromaton*), :doc:`rfsa` +distributive lattice unions, intersections join-irreducibles :class:`Distromaton` +Boolean algebra Boolean operations atoms átomaton, :doc:`atomaton` +GF(2) vector space symmetric difference a basis :func:`minimal_xor_automaton` +Boolean algebra as GF(2) Boolean operations a basis :func:`xor_caba_automaton` +========================== ========================== ====================== =========================================== + +The universal automaton :cite:`LombardySakarovitch2008` (:class:`UniversalAutomaton`) keeps *every* intersection +of residuals; the distromaton is its subautomaton on the join-irreducible ones. + +All constructions here are exact. Every language in these closures is a union of atoms of :math:`L`, and the atoms +are the states of the minimal complete DFA :math:`D` of :math:`L^R` :cite:`BrzozowskiTamm2014`, so a language is +stored as a set of states of :math:`D`: the residual :math:`u^{-1}L` is :math:`\{q : \delta(q, u^R) \in F\}`, the +derivative :math:`a^{-1}K` is the preimage :math:`\{q : \delta(q, a) \in K\}`, :math:`\varepsilon \in K` iff the +start state is in :math:`K`, and the lattice and vector-space operations are set operations. + +Distromaton +=========== + +The distromaton's states are the intersections of residuals that are not unions of smaller intersections; a state +:math:`K` is initial iff :math:`K \subseteq L`, accepting iff :math:`\varepsilon \in K`, and +:math:`K \xrightarrow{a} K'` iff :math:`K' \subseteq a^{-1}K` :cite:`Myers2014`. There is one state per atom +(the intersection of the residuals containing it), so the distromaton and the átomaton have the same number of +states once the átomaton counts the negative atom (:cite:`Myers2014`, Corollary 3.21). Their transition structures +differ: the distromaton is the reverse of the minimal DFA of :math:`L^R` *ordered by inclusion*, with +:math:`z \xrightarrow{a} z'` whenever :math:`\delta(z', a)` includes :math:`z`, where the átomaton requires +equality. + +Xor automata +============ + +A :class:`~sofic.automata.xor.XorAutomaton` is shaped like an NFA but accepts a word when it labels an odd number of +accepting paths :cite:`VuilleminGama2010`; it is a weighted automaton over GF(2), and +:meth:`~sofic.automata.xor.XorAutomaton.determinize` is the xor subset construction. The minimal xor automaton has +a basis of the span of the residuals as states. It is minimal among all xor automata and never larger than the +minimal DFA, but canonical only up to the basis; :func:`minimal_xor_automaton` picks the residuals greedily in +shortlex order of their access words. It is the GF(2) instance of the minimal linear representation that +:func:`~sofic.generators.minimal_quasi_realization.minimal_quasi_realization` computes over the reals. + +The minimal xor-CABA automaton :cite:`Zetzsche2021` uses a basis of the whole Boolean closure, read as a GF(2) +space, so it has one state per atom. With ``basis="residuals"`` the basis extends that of the minimal xor +automaton, which is then its accessible part; with ``basis="atoms"`` it is the átomaton plus the negative atom, +because the átomaton is co-deterministic with one accepting state and so has at most one accepting path per word. + +Minimality and state-minimal NFAs +================================= + +Each canonical automaton is size-minimal in a class of acceptors (:cite:`Myers2014`, Section 4; +:cite:`Zetzsche2021`, Section 6): + +======================== =========================================================================== +automaton has the fewest states among +======================== =========================================================================== +canonical RFSA NFAs whose states accept unions of residuals +átomaton NFAs whose accepted languages (over all initial sets) are closed under complement +distromaton NFAs whose accepted languages are closed under intersection +minimal xor automaton all xor automata +minimal xor-CABA xor automata whose accepted languages have equal GF(2) and Boolean closures +======================== =========================================================================== + +When :func:`is_intersection_closed` holds -- every intersection of two residuals is a union of residuals -- the +canonical RFSA is a state-minimal NFA outright, and the distromaton and átomaton have at most one more state +(:cite:`Myers2014`, Theorem 4.4 and Corollary 4.6). For sofic shifts this certifies that the canonical RFSA +presentation is a smallest presentation +(:func:`~sofic.shifts.canonical_presentations.follower_sets_intersection_closed`). + +.. code-block:: python + + from sofic.automata.canonical.closures import ( + Distromaton, minimal_xor_automaton, xor_caba_automaton, is_intersection_closed, minimal_structured_dfa, + ) + + Distromaton.from_language(nfa) + xor = minimal_xor_automaton(nfa) # an XorAutomaton + xor.recognizes(word) # odd number of accepting paths + xor.determinize() # DFA of the same language + minimal_structured_dfa(nfa, "lattice") # the distributive-lattice DFA the distromaton is read from + is_intersection_closed(nfa) # then the canonical RFSA is a state-minimal NFA + +API +=== + +.. autoclass:: Distromaton + :members: from_language +.. autofunction:: distromaton_from_language +.. autoclass:: UniversalAutomaton + :members: from_language +.. autofunction:: universal_automaton_from_language +.. autofunction:: minimal_xor_automaton +.. autofunction:: xor_caba_automaton +.. autofunction:: minimal_structured_dfa +.. autofunction:: is_intersection_closed + +.. autoclass:: sofic.automata.xor.XorAutomaton + :members: from_language, recognizes, run, step, determinize, minimize, reverse diff --git a/docs/automata/rfsa.rst b/docs/automata/rfsa.rst index 3f23c22..ffd096c 100644 --- a/docs/automata/rfsa.rst +++ b/docs/automata/rfsa.rst @@ -17,7 +17,11 @@ states the primes containing the empty word, and a transition :math:`p \xrightarrow{a} p'` whenever :math:`L_{p'} \subseteq a^{-1} L_p` :cite:`Denis2002`. It is never larger than the minimal DFA and can be exponentially smaller: for :math:`\Sigma^* a \Sigma^n` the minimal DFA has -:math:`2^{n+1}` states and the canonical RFSA :math:`n + 2`. +:math:`2^{n+1}` states and the canonical RFSA :math:`n + 2`. Myers et al. call it +the *jiromaton* :cite:`Myers2014`: its states are the join-irreducibles of the +semilattice of unions of residuals, and it has the fewest states among NFAs whose +states accept unions of residuals. The other canonical NFAs of that construction +are in :doc:`canonical_closures`. Reversing a canonical RFSA gives the maximized prime átomaton of the reversed language (:meth:`CanonicalRFSA.dual`; see :doc:`atomaton`). NL\* learns the diff --git a/docs/generators/canonical_generators.rst b/docs/generators/canonical_generators.rst index 4157ecd..d3880b6 100644 --- a/docs/generators/canonical_generators.rst +++ b/docs/generators/canonical_generators.rst @@ -43,6 +43,30 @@ known. The topological counterparts for sofic shifts are in :mod:`sofic.shifts.canonical_presentations`. +Minimality within a class +------------------------- + +The canonical RFSA is size-minimal among NFAs whose states accept unions of residuals (:cite:`Myers2014`, +Corollary 4.11; :cite:`Zetzsche2021`, Corollary 34). The analogue, with mixtures in place of unions, holds for +:func:`canonical_rfsa_hmm`: a generator of the process in which the future morph +:math:`h_g(w) = \Pr(w \mid G_0 = g)` of every state of positive stationary probability is a mixture of +causal-state morphs has at least as many states as :func:`canonical_rfsa_hmm`. By the Markov property + +.. math:: + + \Pr(\cdot \mid \text{past}) = \sum_g \Pr(G_0 = g \mid \text{past}) \, h_g, + +so every causal-state morph lies in :math:`\operatorname{conv}\{h_g\}`. The hypothesis gives the reverse +inclusion, so the two polytopes coincide and each vertex is one of the :math:`h_g`; :func:`canonical_rfsa_hmm` +has one state per vertex and belongs to the class. Reversing time gives the same statement for +:func:`maximized_prime_atomaton_hmm`, with past morphs and reverse causal states. Generators outside the class can +be smaller. The argument is not from the literature. + +The other canonical NFAs of :doc:`/automata/canonical_closures` have no analogue here: the distromaton and the +universal automaton are built from intersections of residuals, and mixtures of morphs have no meet. The minimal +xor automaton is a minimal linear representation over GF(2); its counterpart over the reals is +:func:`~sofic.generators.minimal_quasi_realization.minimal_quasi_realization`. + .. autofunction:: canonical_rfsa_hmm .. autofunction:: maximized_prime_atomaton_hmm .. autofunction:: atomaton_hmm diff --git a/docs/generators/minimal_quasi_realization.rst b/docs/generators/minimal_quasi_realization.rst index 4145bf4..c4346fb 100644 --- a/docs/generators/minimal_quasi_realization.rst +++ b/docs/generators/minimal_quasi_realization.rst @@ -28,7 +28,9 @@ over all pairs of words :math:`u, v`, and is the dimension of Upper's generalized-state space :cite:`Upper1997`. It is a lower bound on the number of states of every HMM presentation of the process, including the ε-machine, and can be strictly smaller than all of them: the minimal representation is in -general only a quasi-realization with signed entries. +general only a quasi-realization with signed entries. The same minimization over +the two-element field instead of the reals gives the minimal xor automaton of a +regular language (:func:`~sofic.automata.canonical.closures.minimal_xor_automaton`). :func:`minimal_quasi_realization` grows bases :math:`R` (rows) and :math:`O` (columns) of the reachable and observable spaces breadth-first over words, diff --git a/docs/references.bib b/docs/references.bib index 8283f1d..a044405 100644 --- a/docs/references.bib +++ b/docs/references.bib @@ -1797,3 +1797,49 @@ @article{Myers2015 year = {2015}, doi = {10.1016/j.tcs.2015.03.035}, } + +@inproceedings{Myers2014, + author = {Myers, Robert S. R. and Ad{\'a}mek, Ji{\v r}{\'\i} and Milius, Stefan and Urbat, Henning}, + title = {Canonical Nondeterministic Automata}, + booktitle = {Coalgebraic Methods in Computer Science (CMCS 2014)}, + editor = {Bonsangue, Marcello M.}, + series = {Lecture Notes in Computer Science}, + volume = {8446}, + pages = {189--210}, + publisher = {Springer}, + year = {2014}, + doi = {10.1007/978-3-662-44124-4_11}, +} + +@inproceedings{Zetzsche2021, + author = {Zetzsche, Stefan and van Heerdt, Gerco and Sammartino, Matteo and Silva, Alexandra}, + title = {Canonical Automata via Distributive Law Homomorphisms}, + booktitle = {Proceedings of the 37th Conference on Mathematical Foundations of Programming Semantics (MFPS 2021)}, + editor = {Sokolova, Ana}, + series = {Electronic Proceedings in Theoretical Computer Science}, + volume = {351}, + pages = {296--313}, + year = {2021}, + doi = {10.4204/EPTCS.351.18}, +} + +@techreport{VuilleminGama2010, + author = {Vuillemin, Jean and Gama, Nicolas}, + title = {Efficient Equivalence and Minimization for Non Deterministic Xor Automata}, + institution = {LIENS, {\'E}cole Normale Sup{\'e}rieure}, + type = {Research report}, + year = {2010}, + url = {https://hal.inria.fr/inria-00487031}, +} + +@incollection{LombardySakarovitch2008, + author = {Lombardy, Sylvain and Sakarovitch, Jacques}, + title = {The Universal Automaton}, + booktitle = {Logic and Automata: History and Perspectives}, + editor = {Flum, J{\"o}rg and Gr{\"a}del, Erich and Wilke, Thomas}, + series = {Texts in Logic and Games}, + volume = {2}, + pages = {457--504}, + publisher = {Amsterdam University Press}, + year = {2008}, +} diff --git a/docs/shifts/canonical_presentations.rst b/docs/shifts/canonical_presentations.rst index ce770ea..c627fba 100644 --- a/docs/shifts/canonical_presentations.rst +++ b/docs/shifts/canonical_presentations.rst @@ -4,11 +4,19 @@ Canonical NFA presentations .. currentmodule:: sofic.shifts.canonical_presentations A sofic shift is determined by its factor language, which is factorial and extendable :cite:`LindMarcus1995`. -Any automaton for that language with every state accepting presents the shift once trimmed to the states on -bi-infinite paths, so the canonical NFAs of the factor language -- the átomaton :cite:`BrzozowskiTamm2014`, the -canonical residual finite-state automaton :cite:`Denis2002` and the maximized prime átomaton -:cite:`MaarandTamm2022` -- give canonical, generally non-resolving presentations beside the Fischer and Krieger -covers. The residual-side states are follower sets that are not unions of other follower sets. +Any automaton for that language presents the shift once trimmed and restricted to the states on bi-infinite paths, +so the canonical NFAs of the factor language -- the átomaton :cite:`BrzozowskiTamm2014`, the canonical residual +finite-state automaton :cite:`Denis2002`, the maximized prime átomaton :cite:`MaarandTamm2022`, the distromaton +:cite:`Myers2014` and the universal automaton :cite:`LombardySakarovitch2008` -- give canonical, generally +non-resolving presentations beside the Fischer and Krieger covers. The residual-side states are follower sets that +are not unions of other follower sets; the distromaton's are intersections of follower sets that are not unions of +smaller intersections. The minimal xor automaton has no counterpart, since it accepts by parity of paths. + +When every intersection of two follower sets is a union of follower sets +(:func:`follower_sets_intersection_closed`), the canonical RFSA of the factor language is a state-minimal NFA +(:cite:`Myers2014`, Theorem 4.4). A smallest presentation is essential, and an essential presentation is an NFA for +the factor language with every state initial and accepting, so the canonical RFSA presentation is then a +presentation with the fewest states. The golden mean shift is an example. .. code-block:: python @@ -16,6 +24,9 @@ covers. The residual-side states are follower sets that are not unions of other shift.canonical_rfsa_presentation() shift.maximized_prime_atomaton_presentation() shift.atomaton_presentation() + shift.distromaton_presentation() + shift.universal_presentation() + follower_sets_intersection_closed(shift) # then canonical_rfsa_presentation is smallest The stochastic analogues, which replace unions of residuals by mixtures of future morphs, are in :mod:`sofic.generators.canonical_generators`. @@ -23,4 +34,7 @@ The stochastic analogues, which replace unions of residuals by mixtures of futur .. autofunction:: canonical_rfsa_presentation .. autofunction:: maximized_prime_atomaton_presentation .. autofunction:: atomaton_presentation +.. autofunction:: distromaton_presentation +.. autofunction:: universal_presentation +.. autofunction:: follower_sets_intersection_closed .. autofunction:: factor_language_nfa diff --git a/sofic/automata/__init__.py b/sofic/automata/__init__.py index cbaa2d1..2f781af 100644 --- a/sofic/automata/__init__.py +++ b/sofic/automata/__init__.py @@ -12,6 +12,7 @@ from sofic.automata.base import LabeledAutomaton from sofic.automata.buchi import BuchiAutomaton from sofic.automata.canonical.atomaton import Atomaton, AtomicAutomaton, MaximizedPrimeAtomaton +from sofic.automata.canonical.closures import Distromaton, UniversalAutomaton from sofic.automata.canonical.rfsa import CanonicalRFSA, ResidualFiniteStateAutomaton from sofic.automata.dfa import DFA from sofic.automata.enumeration.icdfa import ( @@ -117,6 +118,7 @@ wheeler_state_index, ) from sofic.automata.wheeler_index import WheelerIndex, wheeler_index +from sofic.automata.xor import XorAutomaton __all__ = [ "Atomaton", @@ -140,6 +142,7 @@ "CanonicalRFSA", "DFA", "DeterministicVisiblyPushdownAutomaton", + "Distromaton", "DyckAlphabet", "ERROR_STATE", "ERROR_SYMBOL", @@ -162,11 +165,13 @@ "SubsequentialTransducer", "Transducer", "UnifilarAutomaton", + "UniversalAutomaton", "WeightedFiniteStateTransducer", "WheelerError", "WheelerIndex", "WheelerOrder", "VisiblyPushdownAutomaton", + "XorAutomaton", "automaton_to_regex", "colex_width", "generator_product", diff --git a/sofic/automata/canonical/__init__.py b/sofic/automata/canonical/__init__.py index 7b3bb5a..1d945cb 100644 --- a/sofic/automata/canonical/__init__.py +++ b/sofic/automata/canonical/__init__.py @@ -1,4 +1,4 @@ -"""Canonical residual automata, atomata, and their duality.""" +"""Canonical residual automata, atomata, their duality, and the other canonical NFAs of closures of residuals.""" from sofic.automata.canonical.atomaton import ( Atomaton, @@ -7,6 +7,16 @@ atomic_states, is_atomic, ) +from sofic.automata.canonical.closures import ( + Distromaton, + UniversalAutomaton, + distromaton_from_language, + is_intersection_closed, + minimal_structured_dfa, + minimal_xor_automaton, + universal_automaton_from_language, + xor_caba_automaton, +) from sofic.automata.canonical.dual import dual_atomaton_from_rfsa, dual_rfsa_from_atomaton from sofic.automata.canonical.residual import ( ResidualTable, @@ -24,18 +34,26 @@ "Atomaton", "AtomicAutomaton", "CanonicalRFSA", + "Distromaton", "MaximizedPrimeAtomaton", "ResidualFiniteStateAutomaton", "ResidualTable", + "UniversalAutomaton", "atomaton_from_language", "atomic_states", "canonical_rfsa_from_language", + "distromaton_from_language", "dual_atomaton_from_rfsa", "dual_rfsa_from_atomaton", "is_atomic", + "is_intersection_closed", "maximized_prime_atomaton_from_language", + "minimal_structured_dfa", + "minimal_xor_automaton", "observation_to_atomaton", "observation_to_canonical_rfsa", "observation_to_maximized_prime_atomaton", "observation_to_minimal_dfa", + "universal_automaton_from_language", + "xor_caba_automaton", ] diff --git a/sofic/automata/canonical/closures.py b/sofic/automata/canonical/closures.py new file mode 100644 index 0000000..8bc0468 --- /dev/null +++ b/sofic/automata/canonical/closures.py @@ -0,0 +1,396 @@ +r"""Canonical NFAs from closures of the residuals under algebraic operations. + +Myers, Adámek, Milius and Urbat :cite:`Myers2014,Myers2015` build canonical NFAs for a regular language +:math:`L` in two steps. First close the residuals (left quotients) :math:`u^{-1}L` under the operations of a +locally finite variety; with the derivatives :math:`K \mapsto a^{-1}K` as transitions this is the minimal +deterministic automaton of :math:`L` whose states form an algebra of that variety +(:func:`minimal_structured_dfa`). Then take a minimal set of generators of that algebra as the states of an +NFA, with :math:`K \xrightarrow{a} K'` whenever :math:`K'` occurs in the decomposition of :math:`a^{-1}K`. +Zetzsche, van Heerdt, Sammartino and Silva :cite:`Zetzsche2021` recast the second step as generators for +algebras over a monad, related by distributive law homomorphisms, so that the algebra of the deterministic +automaton and the side effect of the succinct one may differ: + +========================= ======================== ====================== ===================================== +algebra of the DFA closure of the residuals states of the NFA canonical automaton +========================= ======================== ====================== ===================================== +join-semilattice unions join-irreducibles canonical RFSA (*jiromaton*) +distributive lattice unions, intersections join-irreducibles distromaton (:class:`Distromaton`) +Boolean algebra Boolean operations atoms átomaton +GF(2) vector space symmetric difference a basis minimal xor automaton +Boolean algebra as GF(2) Boolean operations a basis minimal xor-CABA automaton +========================= ======================== ====================== ===================================== + +The universal automaton :cite:`LombardySakarovitch2008` uses every intersection of residuals rather than a +minimal generating set (:class:`UniversalAutomaton`). + +Exact representation. Every language in these closures is a union of atoms of :math:`L`, and the atoms are in +bijection with the states of the minimal complete DFA :math:`D` of the reversed language :math:`L^R` +:cite:`BrzozowskiTamm2014`: the atom :math:`A_q` is the reverse of the set of words leading to :math:`q`. So a +language :math:`K` of the closure is stored exactly as the set of states :math:`q` with :math:`A_q \subseteq K`. +Writing :math:`\delta`, :math:`F` and :math:`s_0` for the transition function, accepting states and start state +of :math:`D`, a word :math:`w` lies in :math:`A_q` iff :math:`\delta(s_0, w^R) = q`, from which + +* the residual :math:`u^{-1}L` is :math:`\{q : \delta(q, u^R) \in F\}`, and :math:`L` itself is :math:`F`; +* the derivative :math:`a^{-1}K` is the preimage :math:`\{q : \delta(q, a) \in K\}`; +* :math:`\varepsilon \in K` iff :math:`s_0 \in K`; +* union, intersection, complement and symmetric difference are the set operations. + +Every construction below is exact and determinizes the reversed language once. +""" + +from __future__ import annotations + +from collections.abc import Hashable, Iterable, Sequence +from dataclasses import dataclass +from typing import TYPE_CHECKING, Any, Literal + +from sofic.automata.canonical.residual import ResidualTable, _language_automaton +from sofic.automata.dfa import DFA +from sofic.automata.nfa import NFA +from sofic.automata.xor import XorAutomaton +from sofic.states import sequential_labels + +if TYPE_CHECKING: + from sofic.automata.base import LabeledAutomaton + from sofic.automata.languages.base import RegularLanguage + +__all__ = [ + "Distromaton", + "UniversalAutomaton", + "Variety", + "distromaton_from_language", + "is_intersection_closed", + "minimal_structured_dfa", + "minimal_xor_automaton", + "universal_automaton_from_language", + "xor_caba_automaton", +] + +Variety = Literal["semilattice", "lattice", "boolean", "gf2"] +_Set = frozenset[Hashable] + + +@dataclass(frozen=True) +class _AtomAlgebra: + """Languages of the Boolean closure of the residuals of ``L``, as sets of atoms (see the module docstring).""" + + alphabet: tuple[Any, ...] + atoms: tuple[Hashable, ...] + start: Hashable + language: _Set + delta: dict[tuple[Hashable, Any], Hashable] + + @classmethod + def of(cls, language: Any) -> _AtomAlgebra: + aut = language.determinize() if isinstance(language, XorAutomaton) else _language_automaton(language) + table = ResidualTable.from_automaton(aut.reverse()) + return cls( + alphabet=table.alphabet, + atoms=tuple(table._bfs_order()), + start=table.start, + language=table.accepting, + delta=table.delta, + ) + + @property + def everything(self) -> _Set: + return frozenset(self.atoms) + + def derivative(self, language: _Set, symbol: Any) -> _Set: + return frozenset(q for q in self.atoms if self.delta[(q, symbol)] in language) + + def has_epsilon(self, language: _Set) -> bool: + return self.start in language + + def residuals(self) -> tuple[_Set, ...]: + """The distinct residuals, in shortlex order of their shortest access words.""" + order = [self.language] + seen = {self.language} + for residual in order: + for symbol in self.alphabet: + target = self.derivative(residual, symbol) + if target not in seen: + seen.add(target) + order.append(target) + return tuple(order) + + def meets(self) -> tuple[_Set, ...]: + """Every intersection of residuals, the empty intersection being :math:`\\Sigma^*`.""" + return _close(self.residuals(), frozenset.__and__, start=self.everything) + + def closure(self, variety: Variety) -> tuple[_Set, ...]: + if variety == "boolean": + subsets: list[_Set] = [frozenset()] + for atom in self.atoms: + subsets += [s | {atom} for s in subsets] + return tuple(subsets) + if variety == "gf2": + return _close(self.residuals(), frozenset.__xor__) + if variety == "semilattice": + return _close(self.residuals(), frozenset.__or__) + if variety == "lattice": + return _close(self.meets(), frozenset.__or__) + raise ValueError(f"unknown variety {variety!r}; expected 'semilattice', 'lattice', 'boolean' or 'gf2'") + + +def _close(generators: Iterable[_Set], operation: Any, start: _Set = frozenset()) -> tuple[_Set, ...]: + """Every combination of a subfamily of ``generators`` under an associative, commutative ``operation``. + + ``start`` is the combination of the empty subfamily (the identity of ``operation``). + """ + elements = [start] + seen = {start} + for generator in generators: + for combined in [operation(e, generator) for e in elements]: + if combined not in seen: + seen.add(combined) + elements.append(combined) + return tuple(elements) + + +def _nfa_on(cls: type[NFA], algebra: _AtomAlgebra, generators: Sequence[_Set]) -> Any: + """The succinct NFA of a union-generating set (Proposition 14 of :cite:`Zetzsche2021`). + + Every derivative of a generator, and :math:`L` itself, must be the union of the generators it contains. + """ + name = dict(zip(generators, sequential_labels(len(generators)), strict=True)) + nfa = cls( + input_alphabet=frozenset(algebra.alphabet), + initial_states=frozenset(name[g] for g in generators if g <= algebra.language), + accepting_states=frozenset(name[g] for g in generators if algebra.has_epsilon(g)), + ) + for g in generators: + nfa.graph.add_state(name[g]) + for g in generators: + for symbol in algebra.alphabet: + successor = algebra.derivative(g, symbol) + for h in generators: + if h <= successor: + nfa.add_transition(name[g], name[h], symbol) + return nfa + + +class _GF2Basis: + """Incremental Gaussian elimination over GF(2) on sets of atoms, encoded as bitmasks.""" + + def __init__(self, atoms: Sequence[Hashable]) -> None: + self._bit = {atom: 1 << i for i, atom in enumerate(atoms)} + self._rows: dict[int, tuple[int, int]] = {} # leading bit -> (row, mask of the basis vectors it sums) + self.vectors: list[_Set] = [] + + def _reduce(self, vector: _Set) -> tuple[int, int]: + row = sum(self._bit[q] for q in vector) + combination = 0 + while row and (lead := row.bit_length() - 1) in self._rows: + pivot, pivot_combination = self._rows[lead] + row ^= pivot + combination ^= pivot_combination + return row, combination + + def add(self, vector: _Set) -> bool: + """Append ``vector`` to the basis if it is independent of the vectors so far.""" + row, combination = self._reduce(vector) + if not row: + return False + self._rows[row.bit_length() - 1] = (row, combination ^ (1 << len(self.vectors))) + self.vectors.append(vector) + return True + + def coordinates(self, vector: _Set) -> list[int]: + """Indices of the basis vectors whose symmetric difference is ``vector``.""" + row, combination = self._reduce(vector) + if row: + raise ValueError("vector is not in the span of the basis") + return [i for i in range(len(self.vectors)) if combination >> i & 1] + + +def _xor_on(algebra: _AtomAlgebra, basis: _GF2Basis) -> XorAutomaton: + names = sequential_labels(len(basis.vectors)) + xor = XorAutomaton( + input_alphabet=frozenset(algebra.alphabet), + initial_states=frozenset(names[i] for i in basis.coordinates(algebra.language)), + accepting_states=frozenset(n for n, v in zip(names, basis.vectors, strict=True) if algebra.has_epsilon(v)), + ) + for name in names: + xor.graph.add_state(name) + for name, vector in zip(names, basis.vectors, strict=True): + for symbol in algebra.alphabet: + for j in basis.coordinates(algebra.derivative(vector, symbol)): + xor.add_transition(name, names[j], symbol) + return xor + + +class Distromaton(NFA): + r"""Distromaton of a regular language :cite:`Myers2014`. + + Its states are the join-irreducible languages of the distributive lattice generated by the residuals; the + lattice is ordered by inclusion, which makes the distromaton a nondeterministic automaton over a poset. As an + NFA it is state-minimal among NFAs whose accepted languages (over all sets of initial states) are closed under + intersection (:cite:`Myers2014`, Theorem 4.13; :cite:`Zetzsche2021`, Corollary 37). + """ + + @classmethod + def from_language(cls, language: RegularLanguage | LabeledAutomaton | XorAutomaton, **kwargs: Any) -> Distromaton: + return distromaton_from_language(language) + + +class UniversalAutomaton(NFA): + r"""Universal automaton of a regular language :cite:`LombardySakarovitch2008`. + + Its states are all intersections of residuals (the empty intersection is :math:`\Sigma^*`), with + :math:`K \xrightarrow{a} K'` iff :math:`K' \subseteq a^{-1}K`. Every NFA for the language whose states are + all accessible and co-accessible maps into it, so it contains a homomorphic image of every state-minimal NFA. + """ + + @classmethod + def from_language( + cls, language: RegularLanguage | LabeledAutomaton | XorAutomaton, **kwargs: Any + ) -> UniversalAutomaton: + return universal_automaton_from_language(language) + + +def distromaton_from_language(language: RegularLanguage | LabeledAutomaton | XorAutomaton) -> Distromaton: + r"""Build the distromaton of a regular language :cite:`Myers2014,Zetzsche2021`. + + States are the join-irreducibles of the lattice generated by the residuals under union and intersection + (together with :math:`\emptyset` and :math:`\Sigma^*`): the intersections of residuals that are not unions of + smaller intersections. A state :math:`K` is initial iff :math:`K \subseteq L`, accepting iff + :math:`\varepsilon \in K`, and :math:`K \xrightarrow{a} K'` iff :math:`K' \subseteq a^{-1}K`. + + In a finite distributive lattice of sets the join-irreducibles are the smallest members containing each point. + Here the points are the atoms, so the state of atom :math:`A` is the intersection of the residuals that + contain it (:math:`\Sigma^*` for the negative atom). There is one state per atom, *including* the negative + atom when it is non-empty, so the distromaton has as many states as the minimal complete DFA of :math:`L^R` + (:cite:`Myers2014`, Corollary 3.21) -- one more than :class:`~sofic.automata.canonical.atomaton.Atomaton`, + which drops the negative atom, whenever that atom is non-empty. The state :math:`\Sigma^*` is then not + accessible, but it belongs to the canonical automaton and is kept. + """ + algebra = _AtomAlgebra.of(language) + residuals = algebra.residuals() + states: dict[_Set, None] = {} + for atom in algebra.atoms: + meet = algebra.everything + for residual in residuals: + if atom in residual: + meet &= residual + states[meet] = None + return _nfa_on(Distromaton, algebra, list(states)) + + +def universal_automaton_from_language( + language: RegularLanguage | LabeledAutomaton | XorAutomaton, +) -> UniversalAutomaton: + r"""Build the universal automaton of a regular language :cite:`LombardySakarovitch2008`. + + States are all intersections of residuals, as in :cite:`Myers2014`: :math:`K` is initial iff + :math:`K \subseteq L`, accepting iff :math:`\varepsilon \in K`, and :math:`K \xrightarrow{a} K'` iff + :math:`K' \subseteq a^{-1}K`. The states correspond to the maximal factorizations :math:`(X, K)` of + :math:`L`, :math:`XK \subseteq L`. The empty intersection :math:`\Sigma^*` and, when it occurs, :math:`\emptyset` + are kept, so the result need not be trim. The distromaton is the subautomaton on the join-irreducible + intersections. The number of intersections can be exponential in the number of residuals. + """ + algebra = _AtomAlgebra.of(language) + return _nfa_on(UniversalAutomaton, algebra, algebra.meets()) + + +def minimal_xor_automaton(language: RegularLanguage | LabeledAutomaton | XorAutomaton) -> XorAutomaton: + r"""Build the minimal xor automaton of a regular language :cite:`VuilleminGama2010,Myers2014`. + + Its states form a basis of the GF(2) vector space spanned by the residuals under symmetric difference, and + it accepts by parity (see :class:`~sofic.automata.xor.XorAutomaton`). Its size, the rank over GF(2) of the + Hankel matrix :math:`[uv \in L]_{u, v}`, is minimal among all xor automata for :math:`L` (:cite:`Myers2014`, + Theorem 4.10; :cite:`Zetzsche2021`, Corollary 35) and at most the size of the minimal DFA. + + The automaton is canonical only up to the choice of basis. Here the basis is chosen greedily among the + residuals in shortlex order of their access words, so every state accepts a residual and :math:`L` itself is + the single initial state (unless :math:`L = \emptyset`). This is the GF(2) instance of the minimal linear + representation that :func:`~sofic.generators.minimal_quasi_realization.minimal_quasi_realization` computes + over the reals. + """ + algebra = _AtomAlgebra.of(language) + basis = _GF2Basis(algebra.atoms) + for residual in algebra.residuals(): + basis.add(residual) + return _xor_on(algebra, basis) + + +def xor_caba_automaton( + language: RegularLanguage | LabeledAutomaton | XorAutomaton, + *, + basis: Literal["residuals", "atoms"] = "residuals", +) -> XorAutomaton: + r"""Build a minimal xor-CABA automaton of a regular language :cite:`Zetzsche2021`. + + The Boolean closure of the residuals, read as a GF(2) vector space under symmetric difference, has the atoms + as a basis, so its dimension is the number of atoms (including the negative atom when non-empty). A + *minimal xor-CABA automaton* is the xor automaton on a basis of that space (:cite:`Zetzsche2021`, + Definition 28); it is size-minimal among xor automata whose accepted languages have the same GF(2) closure + as Boolean closure (:cite:`Zetzsche2021`, Corollary 38). It stands to the minimal xor automaton as the + átomaton stands to the canonical RFSA. + + ``basis`` chooses the basis: + + * ``"residuals"`` (default) extends the basis of :func:`minimal_xor_automaton` by atoms in order. The + residual span is closed under derivatives, so the states accessible from the initial states are exactly + the minimal xor automaton. + * ``"atoms"`` uses the atoms. The result is then the átomaton together with the negative atom: atoms are + disjoint, so derivatives decompose as in the átomaton, and the átomaton is co-deterministic with a single + accepting state, so every word has at most one accepting path and parity acceptance agrees with NFA + acceptance. + """ + algebra = _AtomAlgebra.of(language) + gf2 = _GF2Basis(algebra.atoms) + if basis == "residuals": + for residual in algebra.residuals(): + gf2.add(residual) + elif basis != "atoms": + raise ValueError(f"unknown basis {basis!r}; expected 'residuals' or 'atoms'") + for atom in algebra.atoms: + gf2.add(frozenset({atom})) + return _xor_on(algebra, gf2) + + +def minimal_structured_dfa(language: RegularLanguage | LabeledAutomaton | XorAutomaton, variety: Variety) -> DFA: + r"""The minimal deterministic automaton of a language whose states form an algebra in ``variety``. + + Construction 2.15 of :cite:`Myers2014`: the states are the closure of the residuals under the operations of + the variety -- unions (``"semilattice"``), unions and intersections with :math:`\emptyset` and + :math:`\Sigma^*` (``"lattice"``), Boolean operations (``"boolean"``), or symmetric difference (``"gf2"``) -- + with :math:`K \xrightarrow{a} a^{-1}K`, initial state :math:`L` and accepting states the languages containing + :math:`\varepsilon`. It is complete. It is the minimal pointed bialgebra of :cite:`Zetzsche2021`, from which + the canonical NFAs of the module docstring are read off by choosing generators. States are labeled + sequentially. The closure can be exponentially larger than the minimal DFA (the Boolean closure always has + :math:`2^{\#\text{atoms}}` elements). + """ + algebra = _AtomAlgebra.of(language) + elements = algebra.closure(variety) + name = dict(zip(elements, sequential_labels(len(elements)), strict=True)) + dfa = DFA( + input_alphabet=frozenset(algebra.alphabet), + initial_states=frozenset({name[algebra.language]}), + accepting_states=frozenset(name[e] for e in elements if algebra.has_epsilon(e)), + ) + for element in elements: + dfa.graph.add_state(name[element]) + for element in elements: + for symbol in algebra.alphabet: + dfa.add_transition(name[element], name[algebra.derivative(element, symbol)], symbol) + return dfa + + +def is_intersection_closed(language: RegularLanguage | LabeledAutomaton | XorAutomaton) -> bool: + r"""Whether every intersection of two residuals of the language is a union of residuals :cite:`Myers2014`. + + For such languages the canonical RFSA is a state-minimal NFA, and the distromaton and the átomaton (counting + the negative atom) have at most one more state (:cite:`Myers2014`, Theorem 4.4 and Corollary 4.6). Every + singleton :math:`\{w\}` is intersection-closed (:cite:`Myers2014`, Example 4.3), since its distinct residuals + are singletons or empty and so pairwise disjoint; the remark after Theorem 4.4 there, that :math:`\{aa\}` is not, conflicts with + that example and with the definition. + """ + algebra = _AtomAlgebra.of(language) + residuals = algebra.residuals() + return all( + frozenset().union(*(r for r in residuals if r <= left & right)) == left & right + for i, left in enumerate(residuals) + for right in residuals[i + 1 :] + ) diff --git a/sofic/automata/xor.py b/sofic/automata/xor.py new file mode 100644 index 0000000..63c459d --- /dev/null +++ b/sofic/automata/xor.py @@ -0,0 +1,119 @@ +r"""Xor automata: NFAs read with GF(2)-weighted acceptance. + +A xor automaton has the shape of an NFA without ε-moves, but a word is accepted +when it labels an *odd* number of accepting paths :cite:`VuilleminGama2010`. It +is a weighted automaton over the two-element field: the reachable "state set" +after a word is a vector over GF(2), and transitions act on it linearly, by +symmetric difference of successor sets. Parallel duplicate transitions count +once, since the transitions form a relation. + +Because the acceptance condition differs from an NFA's, :class:`XorAutomaton` +is deliberately not a :class:`~sofic.automata.base.LabeledAutomaton`: language +operations go through :meth:`XorAutomaton.determinize`. +""" + +from __future__ import annotations + +from collections import deque +from collections.abc import Hashable, Sequence +from typing import TYPE_CHECKING, Any + +from sofic.base import StateMachine +from sofic.graph import ATTR_SYMBOL, EPSILON + +if TYPE_CHECKING: + from sofic.automata.dfa import DFA + + +class XorAutomaton(StateMachine): + """Finite automaton accepting the words with an odd number of accepting paths :cite:`VuilleminGama2010`.""" + + input_alphabet: frozenset[Any] + initial_states: frozenset[Hashable] + accepting_states: frozenset[Hashable] + + def __init__( + self, + input_alphabet: frozenset[Any] | None = None, + initial_states: frozenset[Hashable] | None = None, + accepting_states: frozenset[Hashable] | None = None, + **kwargs: Any, + ) -> None: + super().__init__(**kwargs) + self.input_alphabet = input_alphabet if input_alphabet is not None else frozenset() + self.initial_states = initial_states if initial_states is not None else frozenset() + self.accepting_states = accepting_states if accepting_states is not None else frozenset() + + def validate(self) -> None: + for state in self.initial_states | self.accepting_states: + self._require(self.graph.has_state(state), f"missing state {state!r}") + for transition in self.transitions(): + symbol = transition.data.get(ATTR_SYMBOL) + self._require(symbol is not None and symbol is not EPSILON, "xor automata have no ε-transitions") + self._require(symbol in self.input_alphabet, f"symbol {symbol!r} not in input alphabet") + + @classmethod + def from_language(cls, language: Any, **kwargs: Any) -> XorAutomaton: + """Return the minimal xor automaton; see :func:`~sofic.automata.canonical.closures.minimal_xor_automaton`.""" + from sofic.automata.canonical.closures import minimal_xor_automaton + + return minimal_xor_automaton(language) + + def add_transition(self, source: Hashable, target: Hashable, symbol: Any, **attrs: Any) -> int: + return self.graph.add_transition(source, target, **{ATTR_SYMBOL: symbol, **attrs}) + + def delta(self, state: Hashable, symbol: Any) -> set[Hashable]: + return {t.target for t in self.graph.out_transitions(state) if t.data.get(ATTR_SYMBOL) == symbol} + + def step(self, states: frozenset[Hashable], symbol: Any) -> frozenset[Hashable]: + """Return the GF(2) state vector after reading ``symbol`` from the vector ``states``.""" + result: set[Hashable] = set() + for state in states: + result ^= self.delta(state, symbol) + return frozenset(result) + + def run(self, word: Sequence[Any]) -> frozenset[Hashable]: + """Return the states reached from the initial states by an odd number of paths labeled ``word``.""" + current = frozenset(self.initial_states) + for symbol in word: + current = self.step(current, symbol) + return current + + def recognizes(self, word: Sequence[Any]) -> bool: + """Return whether ``word`` labels an odd number of accepting paths.""" + return len(self.run(word) & self.accepting_states) % 2 == 1 + + def determinize(self, *, alphabet: frozenset[Any] | None = None) -> DFA: + """Return the complete DFA on the reachable GF(2) state vectors (the xor subset construction).""" + from sofic.automata.dfa import DFA + + symbols = sorted(self.input_alphabet if alphabet is None else alphabet, key=repr) + start = frozenset(self.initial_states) + dfa = DFA(input_alphabet=frozenset(symbols), initial_states=frozenset({start})) + dfa.graph.add_state(start) + seen = {start} + queue = deque([start]) + while queue: + current = queue.popleft() + for symbol in symbols: + target = self.step(current, symbol) + if target not in seen: + seen.add(target) + dfa.graph.add_state(target) + queue.append(target) + dfa.add_transition(current, target, symbol) + dfa.accepting_states = frozenset(s for s in seen if len(s & self.accepting_states) % 2 == 1) + return dfa + + def minimize(self, *, alphabet: frozenset[Any] | None = None) -> DFA: + """Return the minimal DFA of the accepted language.""" + return self.determinize(alphabet=alphabet).minimize() + + def reverse(self) -> XorAutomaton: + """Return the xor automaton of the reversed language; reversal preserves the number of paths.""" + return XorAutomaton( + input_alphabet=self.input_alphabet, + initial_states=frozenset(self.accepting_states), + accepting_states=frozenset(self.initial_states), + graph=self.graph.reverse(), + ) diff --git a/sofic/generators/canonical_generators.py b/sofic/generators/canonical_generators.py index 43bc234..ab89f1d 100644 --- a/sofic/generators/canonical_generators.py +++ b/sofic/generators/canonical_generators.py @@ -26,8 +26,24 @@ reversed process, time-reversed with its stationary law, with at most :math:`|S^-|` states. These are generators, so their state entropies bound the generative complexity :math:`C_g` of -:cite:`LohrAy2009` from above; no minimality is claimed. Identifying them with the canonical NFAs is an analogy -(extreme points of a cone in place of prime residuals) and is not from the literature. +:cite:`LohrAy2009` from above. Identifying them with the canonical NFAs is an analogy (extreme points of a cone in +place of prime residuals) and is not from the literature. + +They are size-minimal within a class, as the canonical RFSA is size-minimal among NFAs whose states accept unions +of residuals (:cite:`Myers2014`, Corollary 4.11; :cite:`Zetzsche2021`, Corollary 34), with mixtures in place of +unions: *a generator of the process in which the future morph* :math:`h_g(w) = \Pr(w \mid G_0 = g)` *of every +state of positive stationary probability is a mixture of causal-state morphs has at least as many states as* +:func:`canonical_rfsa_hmm`. Proof: by the Markov property :math:`\Pr(\cdot \mid \text{past}) = \sum_g \Pr(G_0 = g +\mid \text{past})\, h_g`, so every causal-state morph lies in :math:`\operatorname{conv}\{h_g\}`; the hypothesis +gives the reverse inclusion, so the two polytopes coincide and each of their vertices is one of the :math:`h_g`. +:func:`canonical_rfsa_hmm` has one state per vertex and belongs to the class. Reversing time gives the same +statement for :func:`maximized_prime_atomaton_hmm`, with past morphs and reverse causal states. Generators outside +the class can be smaller. This argument is not from the literature. + +The other canonical NFAs of :mod:`sofic.automata.canonical.closures` have no analogue here. The distromaton and +the universal automaton are built from intersections of residuals, and mixtures of morphs have no meet. The +minimal xor automaton is a minimal linear representation over GF(2), whose counterpart over the reals is +:func:`~sofic.generators.minimal_quasi_realization.minimal_quasi_realization`. """ from __future__ import annotations diff --git a/sofic/serialization.py b/sofic/serialization.py index 04e950e..51595bb 100644 --- a/sofic/serialization.py +++ b/sofic/serialization.py @@ -365,6 +365,7 @@ def _spec(cls: type[StateMachine], fields: tuple[str, ...], builder: str = "defa def _specs() -> tuple[_ModelSpec, ...]: from sofic.automata.buchi import BuchiAutomaton from sofic.automata.canonical.atomaton import Atomaton, AtomicAutomaton, MaximizedPrimeAtomaton + from sofic.automata.canonical.closures import Distromaton, UniversalAutomaton from sofic.automata.canonical.rfsa import CanonicalRFSA, ResidualFiniteStateAutomaton from sofic.automata.dfa import DFA from sofic.automata.nfa import NFA @@ -380,6 +381,7 @@ def _specs() -> tuple[_ModelSpec, ...]: SingleEntryVisiblyPushdownAutomaton, VisiblyPushdownAutomaton, ) + from sofic.automata.xor import XorAutomaton from sofic.generators.base import HiddenMarkovModel, QuasiStochasticModel, StochasticModel from sofic.generators.bidirectional_epsilon_machine import BidirectionalEpsilonMachine from sofic.generators.epsilon_machine import EpsilonMachine @@ -459,6 +461,9 @@ def _specs() -> tuple[_ModelSpec, ...]: _spec(MaximizedPrimeAtomaton, labeled), _spec(ResidualFiniteStateAutomaton, labeled), _spec(CanonicalRFSA, labeled), + _spec(Distromaton, labeled), + _spec(UniversalAutomaton, labeled), + _spec(XorAutomaton, labeled), _spec(MealyMachine, transducer), _spec(MooreMachine, transducer), _spec(EpsilonTransducer, epsilon_transducer), diff --git a/sofic/shifts/__init__.py b/sofic/shifts/__init__.py index 73ad93d..4264986 100644 --- a/sofic/shifts/__init__.py +++ b/sofic/shifts/__init__.py @@ -5,8 +5,11 @@ from sofic.shifts.canonical_presentations import ( atomaton_presentation, canonical_rfsa_presentation, + distromaton_presentation, factor_language_nfa, + follower_sets_intersection_closed, maximized_prime_atomaton_presentation, + universal_presentation, ) from sofic.shifts.covers import ( LeftFischerCover, @@ -60,8 +63,11 @@ __all__ = [ "atomaton_presentation", "canonical_rfsa_presentation", + "distromaton_presentation", "factor_language_nfa", + "follower_sets_intersection_closed", "maximized_prime_atomaton_presentation", + "universal_presentation", "BowenFranksGroup", "bowen_franks_group", "in_amalgamate", diff --git a/sofic/shifts/canonical_presentations.py b/sofic/shifts/canonical_presentations.py index b19d4a2..91b1906 100644 --- a/sofic/shifts/canonical_presentations.py +++ b/sofic/shifts/canonical_presentations.py @@ -1,13 +1,19 @@ r"""Canonical NFAs of a sofic shift's language, as presentations of the shift. A sofic shift :math:`X` is determined by its factor language :math:`L(X)`, which is factorial and extendable -:cite:`LindMarcus1995`. Any automaton for :math:`L(X)` whose states are all accepting presents :math:`X` once -trimmed to the states on bi-infinite paths: every path reads a word of :math:`L(X)` (residuals of a factorial -language are factorial), and every word of :math:`L(X)` labels a path. The canonical NFAs of :math:`L(X)` -- -the átomaton :cite:`BrzozowskiTamm2014`, the canonical residual finite-state automaton :cite:`Denis2002` and the -maximized prime átomaton :cite:`MaarandTamm2022` -- therefore give canonical, generally non-resolving -presentations, beside the right-resolving Fischer and Krieger covers. On the residual side the states are -follower sets that are not unions of other follower sets. +:cite:`LindMarcus1995`. Any automaton for :math:`L(X)` presents :math:`X` once trimmed (accessible and +co-accessible states) and restricted to the states on bi-infinite paths: every path between trim states reads a +word of :math:`L(X)` (it extends to an accepted word, and :math:`L(X)` is factorial), and every word of +:math:`L(X)` labels a path. The canonical NFAs of :math:`L(X)` -- the átomaton :cite:`BrzozowskiTamm2014`, the +canonical residual finite-state automaton :cite:`Denis2002`, the maximized prime átomaton +:cite:`MaarandTamm2022`, the distromaton :cite:`Myers2014` and the universal automaton +:cite:`LombardySakarovitch2008` -- therefore give canonical, generally non-resolving presentations, beside the +right-resolving Fischer and Krieger covers. On the residual side the states are follower sets that are not unions +of other follower sets; the distromaton's states are the intersections of follower sets that are not unions of +smaller intersections, and the universal automaton's are all intersections of follower sets. + +The minimal xor automaton of :mod:`sofic.automata.canonical.closures` has no counterpart here: it accepts by +parity of paths, and a labeled graph presents the words with *some* path. These are the topological shadows of the stochastic analogues in :mod:`sofic.generators.canonical_generators`, which replace unions of residuals by mixtures of future morphs. @@ -26,8 +32,11 @@ __all__ = [ "atomaton_presentation", "canonical_rfsa_presentation", + "distromaton_presentation", "factor_language_nfa", + "follower_sets_intersection_closed", "maximized_prime_atomaton_presentation", + "universal_presentation", ] @@ -46,8 +55,12 @@ def factor_language_nfa(shift: SoficShift) -> NFA: def _presentation(shift: SoficShift, automaton: Any) -> SoficShift: + from sofic.automata.algorithms import trim from sofic.shifts.sofic import SoficShift + # Untrimmed states can read words outside L(X): the distromaton's inaccessible state Sigma*, and the universal + # automaton's state for an empty residual, which is accessible but has a loop on every symbol. + automaton = trim(automaton) graph = TransitionGraph() for state in automaton.states(): graph.add_state(state) @@ -75,3 +88,37 @@ def atomaton_presentation(shift: SoficShift) -> SoficShift: from sofic.automata.canonical.residual import atomaton_from_language return _presentation(shift, atomaton_from_language(factor_language_nfa(shift))) + + +def distromaton_presentation(shift: SoficShift) -> SoficShift: + """Presentation of ``shift`` by the distromaton of its factor language (join-irreducible meets of follower sets).""" + from sofic.automata.canonical.closures import distromaton_from_language + + return _presentation(shift, distromaton_from_language(factor_language_nfa(shift))) + + +def universal_presentation(shift: SoficShift) -> SoficShift: + """Presentation of ``shift`` by the universal automaton of its factor language (all meets of follower sets). + + Every essential presentation of ``shift`` is an NFA for :math:`L(X)` with all states initial and accepting, so + it maps into the universal automaton :cite:`LombardySakarovitch2008`. The number of states can be exponential + in the number of follower sets. + """ + from sofic.automata.canonical.closures import universal_automaton_from_language + + return _presentation(shift, universal_automaton_from_language(factor_language_nfa(shift))) + + +def follower_sets_intersection_closed(shift: SoficShift) -> bool: + r"""Whether every intersection of two follower sets of ``shift`` is a union of follower sets. + + Follower sets are the residuals of the factor language, so this is + :func:`~sofic.automata.canonical.closures.is_intersection_closed` applied to :math:`L(X)`. When it holds the + canonical RFSA of :math:`L(X)` is a state-minimal NFA (:cite:`Myers2014`, Theorem 4.4). A presentation with + the fewest states is essential, and an essential presentation with every state initial and accepting is an NFA + for :math:`L(X)`, so :func:`canonical_rfsa_presentation` then has the fewest states of any presentation of + ``shift``. + """ + from sofic.automata.canonical.closures import is_intersection_closed + + return is_intersection_closed(factor_language_nfa(shift)) diff --git a/sofic/shifts/sofic.py b/sofic/shifts/sofic.py index 0bf2b91..e4e77ad 100644 --- a/sofic/shifts/sofic.py +++ b/sofic/shifts/sofic.py @@ -103,6 +103,24 @@ def atomaton_presentation(self) -> SoficShift: return atomaton_presentation(self) + def distromaton_presentation(self) -> SoficShift: + """Presentation by the distromaton of the factor language. + + See :func:`~sofic.shifts.canonical_presentations.distromaton_presentation`. + """ + from sofic.shifts.canonical_presentations import distromaton_presentation + + return distromaton_presentation(self) + + def universal_presentation(self) -> SoficShift: + """Presentation by the universal automaton of the factor language. + + See :func:`~sofic.shifts.canonical_presentations.universal_presentation`. + """ + from sofic.shifts.canonical_presentations import universal_presentation + + return universal_presentation(self) + def krohn_rhodes_complexity(self) -> ComplexityBounds: """Krohn-Rhodes complexity bounds of the factor language's syntactic monoid. diff --git a/tests/test_canonical_closures.py b/tests/test_canonical_closures.py new file mode 100644 index 0000000..6cb10b4 --- /dev/null +++ b/tests/test_canonical_closures.py @@ -0,0 +1,234 @@ +"""Distromaton, xor automata, universal automaton, and minimal structured DFAs (Myers et al. 2014; Zetzsche et al. 2021).""" + +from itertools import product + +import pytest +from hypothesis import given, settings + +from sofic.automata.algorithms import equivalent +from sofic.automata.canonical import Atomaton, CanonicalRFSA +from sofic.automata.canonical.closures import ( + Distromaton, + distromaton_from_language, + is_intersection_closed, + minimal_structured_dfa, + minimal_xor_automaton, + universal_automaton_from_language, + xor_caba_automaton, +) +from sofic.automata.canonical.residual import ResidualTable +from sofic.automata.dfa import DFA +from sofic.automata.nfa import NFA +from sofic.automata.xor import XorAutomaton +from sofic.exceptions import SoficValidationError +from sofic.graph import ATTR_SYMBOL, EPSILON +from sofic.testing.strategies import dfas + + +def _size(aut) -> int: + return len(list(aut.states())) + + +def _nfa(alphabet: str, edges, initial, accepting) -> NFA: + nfa = NFA( + input_alphabet=frozenset(alphabet), initial_states=frozenset(initial), accepting_states=frozenset(accepting) + ) + for source, target, _ in edges: + nfa.graph.add_state(source) + nfa.graph.add_state(target) + for state in (*initial, *accepting): + nfa.graph.add_state(state) + for source, target, symbol in edges: + nfa.add_transition(source, target, symbol) + return nfa + + +def _ends_in_a() -> NFA: + """(a + b)* a, the running example of Zetzsche et al. (2021).""" + return _nfa("ab", [(0, 0, "a"), (0, 0, "b"), (0, 1, "a")], [0], [1]) + + +def _b_then_n_symbols(n: int) -> NFA: + """(a + b)* b (a + b)^n, Example 1.1 of Myers et al. (2014).""" + edges = [(0, 0, "a"), (0, 0, "b"), (0, 1, "b")] + edges += [(i, i + 1, s) for i in range(1, n + 1) for s in "ab"] + return _nfa("ab", edges, [0], [n + 1]) + + +def _words(alphabet, max_length: int): + for length in range(max_length + 1): + yield from product(sorted(alphabet), repeat=length) + + +def _gf2_rank(rows: list[int]) -> int: + pivots: dict[int, int] = {} + for row in rows: + while row and (lead := row.bit_length() - 1) in pivots: + row ^= pivots[lead] + if row: + pivots[row.bit_length() - 1] = row + return len(pivots) + + +def _hankel_gf2_rank(dfa: DFA) -> int: + """GF(2) rank of [uv in L] over all u, v shorter than the minimal DFA. + + A non-zero GF(2) combination of residuals lies in a derivative-closed space of dimension d <= n, so it contains + a word shorter than n; rows indexed by words shorter than n reach every residual. + """ + n = _size(dfa.minimize()) + columns = list(_words(dfa.input_alphabet, n - 1)) + rows = [sum(1 << j for j, v in enumerate(columns) if dfa.recognizes(u + v)) for u in columns] + return _gf2_rank(rows) + + +def _atom_count(dfa: DFA) -> int: + """States of the minimal complete DFA of the reverse language: the atoms, including the negative atom.""" + return len(ResidualTable.from_automaton(dfa.reverse())._bfs_order()) + + +def _myers_distromaton_counts(language) -> tuple[int, int, int, int]: + """The distromaton read off the minimal DFA of the reverse, ordered by inclusion (Myers et al. 2014, Sect. 3.3). + + States are those of the DFA; z -a-> z' iff delta(z', a) includes z; initial = accepting states of the DFA; + accepting = states included in the start state. + """ + table = ResidualTable.from_automaton(language.reverse()) + states = table._bfs_order() + edges = sum(table.includes(z, table.delta[(w, a)]) for z in states for w in states for a in table.alphabet) + accepting = sum(table.includes(z, table.start) for z in states) + return len(states), edges, len(table.accepting), accepting + + +# --- worked examples --------------------------------------------------------------------------------------------- + + +def test_running_example_of_zetzsche_et_al(): + language = _ends_in_a() + distromaton = Distromaton.from_language(language) + assert (_size(distromaton), len(distromaton.initial_states), len(distromaton.accepting_states)) == ( + 3, + 1, + 2, + ) # Fig. 8b + assert _size(minimal_xor_automaton(language)) == 2 # Fig. 7b + sizes = {v: _size(minimal_structured_dfa(language, v)) for v in ("semilattice", "lattice", "boolean", "gf2")} + assert sizes == {"semilattice": 3, "lattice": 4, "boolean": 8, "gf2": 4} # Figs. 5a, 8a, 3, 7a + + +def test_xor_caba_automaton_of_the_running_example_has_one_state_per_atom(): + """Three atoms, so three states. Fig. 10 of Zetzsche et al. (2021) uses {4, 6, 7, 8}, which is a generator but + not a basis: in the notation of their Fig. 3, 4 xor 6 xor 8 is the empty language.""" + language = _ends_in_a() + for basis in ("residuals", "atoms"): + xor = xor_caba_automaton(language, basis=basis) + assert _size(xor) == 3 + assert equivalent(xor.determinize(), language) + + +@pytest.mark.parametrize("n", [1, 2, 3]) +def test_example_1_1_of_myers_et_al(n): + language = _b_then_n_symbols(n) + assert _size(language.minimize()) == 2 ** (n + 1) + assert _size(CanonicalRFSA.from_language(language)) == n + 2 + assert _size(minimal_xor_automaton(language)) == n + 2 + # the distromaton is the jiromaton plus a final sink state Sigma* (Example 3.22) + distromaton = distromaton_from_language(language) + assert _size(distromaton) == n + 3 + assert is_intersection_closed(language) # Example 4.3(1) + for aut in (distromaton, universal_automaton_from_language(language)): + assert equivalent(aut, language) + + +def test_intersection_closed_examples(): + aa = _nfa("a", [(0, 1, "a"), (1, 2, "a")], [0], [2]) + assert is_intersection_closed(aa) # a singleton, Example 4.3(2) of Myers et al. (2014) + # {ab, ac, bc, bd}: the residuals {b, c} and {c, d} meet in {c}, which is not a union of residuals + edges = [(0, 1, "a"), (0, 2, "b"), (1, 3, "b"), (1, 3, "c"), (2, 3, "c"), (2, 3, "d")] + assert not is_intersection_closed(_nfa("abcd", edges, [0], [3])) + + +def test_xor_automaton_counts_paths_mod_two(): + xor = XorAutomaton( + input_alphabet=frozenset("ab"), initial_states=frozenset({0, 1}), accepting_states=frozenset({2}) + ) + for state in range(3): + xor.graph.add_state(state) + xor.add_transition(0, 2, "a") + xor.add_transition(1, 2, "a") + xor.add_transition(0, 2, "b") + xor.validate() + assert not xor.recognizes(("a",)) # two accepting paths + assert xor.recognizes(("b",)) # one + assert equivalent(xor.reverse().determinize(), xor.determinize().reverse()) + + +def test_xor_automaton_rejects_epsilon_moves(): + xor = XorAutomaton(input_alphabet=frozenset("a"), initial_states=frozenset({0})) + xor.graph.add_state(0) + xor.graph.add_transition(0, 0, **{ATTR_SYMBOL: EPSILON}) + with pytest.raises(SoficValidationError, match="no ε-transitions"): + xor.validate() + + +def test_xor_automaton_input_and_round_trip(): + xor = XorAutomaton.from_language(_ends_in_a()) + assert _size(minimal_xor_automaton(xor)) == _size(xor) + from sofic.serialization import model_from_dict, model_to_dict + + clone = model_from_dict(model_to_dict(xor)) + assert type(clone) is XorAutomaton + assert equivalent(clone.determinize(), xor.determinize()) + + +# --- properties -------------------------------------------------------------------------------------------------- + + +@settings(max_examples=60, deadline=None, derandomize=True) +@given(dfa=dfas(max_states=4)) +def test_canonical_automata_accept_the_language(dfa): + for aut in (distromaton_from_language(dfa), universal_automaton_from_language(dfa)): + assert equivalent(aut, dfa) + for xor in (minimal_xor_automaton(dfa), xor_caba_automaton(dfa), xor_caba_automaton(dfa, basis="atoms")): + xor.validate() + assert equivalent(xor.determinize(), dfa) + small = _atom_count(dfa) <= 6 + for variety in ("semilattice", "lattice", "boolean", "gf2") if small else ("semilattice", "lattice", "gf2"): + structured = minimal_structured_dfa(dfa, variety) + assert equivalent(structured, dfa) + assert _size(structured.minimize()) == _size(dfa.minimize()) + + +@settings(max_examples=60, deadline=None, derandomize=True) +@given(dfa=dfas(max_states=4)) +def test_distromaton_matches_the_reverse_dfa_construction(dfa): + """Two constructions of Myers et al. (2014): join-irreducible meets, and the ordered reverse minimal DFA.""" + distromaton = distromaton_from_language(dfa) + counts = ( + _size(distromaton), + len(list(distromaton.transitions())), + len(distromaton.initial_states), + len(distromaton.accepting_states), + ) + assert counts == _myers_distromaton_counts(dfa) + + +@settings(max_examples=60, deadline=None, derandomize=True) +@given(dfa=dfas(max_states=4)) +def test_sizes_of_the_canonical_automata(dfa): + atoms = _atom_count(dfa) + distromaton = distromaton_from_language(dfa) + rfsa = CanonicalRFSA.from_language(dfa) + assert _size(distromaton) == atoms # Corollary 3.21 + assert _size(Atomaton.from_language(dfa)) in (atoms, atoms - 1) # the átomaton drops the negative atom + assert _size(minimal_xor_automaton(dfa)) == _hankel_gf2_rank(dfa) <= _size(dfa.minimize()) + caba = xor_caba_automaton(dfa) + assert _size(caba) == atoms + reached, frontier = set(caba.initial_states), set(caba.initial_states) + while frontier: + frontier = {t.target for s in frontier for t in caba.graph.out_transitions(s)} - reached + reached |= frontier + assert len(reached) == _size(minimal_xor_automaton(dfa)) + assert _size(distromaton) <= _size(universal_automaton_from_language(dfa)) + if is_intersection_closed(dfa): + assert _size(distromaton) <= _size(rfsa) + 1 # Corollary 4.6 diff --git a/tests/test_canonical_generators.py b/tests/test_canonical_generators.py index 8136bab..3aa8138 100644 --- a/tests/test_canonical_generators.py +++ b/tests/test_canonical_generators.py @@ -16,9 +16,13 @@ from sofic.shifts.canonical_presentations import ( atomaton_presentation, canonical_rfsa_presentation, + distromaton_presentation, factor_language_nfa, + follower_sets_intersection_closed, maximized_prime_atomaton_presentation, + universal_presentation, ) +from sofic.shifts.sofic import SoficShift from sofic.testing.strategies import sofic_shifts @@ -94,14 +98,54 @@ def test_generator_search_starts_from_the_prime_atomaton_analogue() -> None: @pytest.mark.parametrize( - "present", [canonical_rfsa_presentation, maximized_prime_atomaton_presentation, atomaton_presentation] + "present", + [ + canonical_rfsa_presentation, + maximized_prime_atomaton_presentation, + atomaton_presentation, + distromaton_presentation, + universal_presentation, + ], ) @settings(max_examples=25, deadline=None, derandomize=True) @given(shift=sofic_shifts(max_states=3)) def test_canonical_presentations_present_the_same_shift(present, shift) -> None: presented = present(shift) assert equivalent(factor_language_nfa(shift), factor_language_nfa(presented)) - assert len(list(presented.states())) <= 2 ** len(list(shift.trim_transient().states())) + if present is not universal_presentation: + assert len(list(presented.states())) <= 2 ** len(list(shift.trim_transient().states())) + + +def _presents(shift: SoficShift, states: int, edges) -> bool: + candidate = SoficShift(symbol_alphabet=shift.symbol_alphabet) + for state in range(states): + candidate.graph.add_state(state) + for source, target, symbol in edges: + candidate.add_transition(source, target, symbol) + return equivalent(factor_language_nfa(shift), factor_language_nfa(candidate)) + + +@settings(max_examples=40, deadline=None, derandomize=True) +@given(shift=sofic_shifts(max_states=3)) +def test_intersection_closed_follower_sets_make_the_rfsa_presentation_minimal(shift) -> None: + """Every labeled graph with fewer states than the canonical RFSA presentation presents another shift.""" + if not follower_sets_intersection_closed(shift): + return + k = len(list(canonical_rfsa_presentation(shift).states())) + if not 2 <= k <= 3: + return + m = k - 1 + possible = [(s, t, a) for s in range(m) for t in range(m) for a in sorted(shift.symbol_alphabet)] + for mask in range(1 << len(possible)): + assert not _presents(shift, m, [e for i, e in enumerate(possible) if mask >> i & 1]) + + +def test_follower_sets_of_the_golden_mean_shift_are_intersection_closed() -> None: + shift = golden_mean(0.4).to_sofic_shift() + assert follower_sets_intersection_closed(shift) + assert len(list(canonical_rfsa_presentation(shift).states())) == 2 + assert equivalent(factor_language_nfa(shift), factor_language_nfa(shift.distromaton_presentation())) + assert equivalent(factor_language_nfa(shift), factor_language_nfa(shift.universal_presentation())) def test_shift_methods_and_supports() -> None: