From 25f4896175a619ef31e1e9ec99eb8b83cd1ff6f0 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 31 Dec 2023 04:54:19 +0000 Subject: [PATCH] Deployed c39df7d with MkDocs version: 1.4.2 --- .nojekyll | 0 404.html | 1388 ++++ api/base-exception-classes/index.html | 1972 +++++ api/class-automaton/index.html | 1919 +++++ api/fa/class-dfa/index.html | 5502 ++++++++++++++ api/fa/class-fa/index.html | 1755 +++++ api/fa/class-gnfa/index.html | 1993 +++++ api/fa/class-nfa/index.html | 2959 ++++++++ api/index.html | 1415 ++++ api/pda/class-dpda/index.html | 1816 +++++ api/pda/class-npda/index.html | 1833 +++++ api/pda/class-pda/index.html | 1699 +++++ api/regular-expressions/index.html | 1887 +++++ api/tm/class-dtm/index.html | 1867 +++++ api/tm/class-mntm/index.html | 2012 +++++ api/tm/class-ntm/index.html | 1892 +++++ api/tm/class-tm/index.html | 1534 ++++ api/tm/tm-exception-classes/index.html | 1614 ++++ assets/_mkdocstrings.css | 64 + assets/images/favicon.png | Bin 0 -> 1870 bytes assets/img/favicon.png | Bin 0 -> 719 bytes assets/img/logo.svg | 20 + assets/javascripts/bundle.fac441b0.min.js | 29 + assets/javascripts/bundle.fac441b0.min.js.map | 8 + assets/javascripts/lunr/min/lunr.ar.min.js | 1 + assets/javascripts/lunr/min/lunr.da.min.js | 18 + assets/javascripts/lunr/min/lunr.de.min.js | 18 + assets/javascripts/lunr/min/lunr.du.min.js | 18 + assets/javascripts/lunr/min/lunr.es.min.js | 18 + assets/javascripts/lunr/min/lunr.fi.min.js | 18 + assets/javascripts/lunr/min/lunr.fr.min.js | 18 + assets/javascripts/lunr/min/lunr.hi.min.js | 1 + assets/javascripts/lunr/min/lunr.hu.min.js | 18 + assets/javascripts/lunr/min/lunr.hy.min.js | 1 + assets/javascripts/lunr/min/lunr.it.min.js | 18 + assets/javascripts/lunr/min/lunr.ja.min.js | 1 + assets/javascripts/lunr/min/lunr.jp.min.js | 1 + assets/javascripts/lunr/min/lunr.ko.min.js | 1 + assets/javascripts/lunr/min/lunr.multi.min.js | 1 + assets/javascripts/lunr/min/lunr.nl.min.js | 18 + assets/javascripts/lunr/min/lunr.no.min.js | 18 + assets/javascripts/lunr/min/lunr.pt.min.js | 18 + assets/javascripts/lunr/min/lunr.ro.min.js | 18 + assets/javascripts/lunr/min/lunr.ru.min.js | 18 + .../lunr/min/lunr.stemmer.support.min.js | 1 + assets/javascripts/lunr/min/lunr.sv.min.js | 18 + assets/javascripts/lunr/min/lunr.ta.min.js | 1 + assets/javascripts/lunr/min/lunr.th.min.js | 1 + assets/javascripts/lunr/min/lunr.tr.min.js | 18 + assets/javascripts/lunr/min/lunr.vi.min.js | 1 + assets/javascripts/lunr/min/lunr.zh.min.js | 1 + assets/javascripts/lunr/tinyseg.js | 206 + assets/javascripts/lunr/wordcut.js | 6708 +++++++++++++++++ .../workers/search.208ed371.min.js | 42 + .../workers/search.208ed371.min.js.map | 8 + assets/stylesheets/main.85bb2934.min.css | 1 + assets/stylesheets/main.85bb2934.min.css.map | 1 + assets/stylesheets/palette.a6bdf11c.min.css | 1 + .../stylesheets/palette.a6bdf11c.min.css.map | 1 + characteristics/index.html | 1533 ++++ examples/fa-examples/index.html | 1611 ++++ index.html | 1430 ++++ migration/index.html | 1842 +++++ objects.inv | Bin 0 -> 1155 bytes people/index.html | 1471 ++++ search/search_index.json | 1 + sitemap.xml | 108 + sitemap.xml.gz | Bin 0 -> 352 bytes 68 files changed, 50424 insertions(+) create mode 100644 .nojekyll create mode 100644 404.html create mode 100644 api/base-exception-classes/index.html create mode 100644 api/class-automaton/index.html create mode 100644 api/fa/class-dfa/index.html create mode 100644 api/fa/class-fa/index.html create mode 100644 api/fa/class-gnfa/index.html create mode 100644 api/fa/class-nfa/index.html create mode 100644 api/index.html create mode 100644 api/pda/class-dpda/index.html create mode 100644 api/pda/class-npda/index.html create mode 100644 api/pda/class-pda/index.html create mode 100644 api/regular-expressions/index.html create mode 100644 api/tm/class-dtm/index.html create mode 100644 api/tm/class-mntm/index.html create mode 100644 api/tm/class-ntm/index.html create mode 100644 api/tm/class-tm/index.html create mode 100644 api/tm/tm-exception-classes/index.html create mode 100644 assets/_mkdocstrings.css create mode 100644 assets/images/favicon.png create mode 100644 assets/img/favicon.png create mode 100644 assets/img/logo.svg create mode 100644 assets/javascripts/bundle.fac441b0.min.js create mode 100644 assets/javascripts/bundle.fac441b0.min.js.map create mode 100644 assets/javascripts/lunr/min/lunr.ar.min.js create mode 100644 assets/javascripts/lunr/min/lunr.da.min.js create mode 100644 assets/javascripts/lunr/min/lunr.de.min.js create mode 100644 assets/javascripts/lunr/min/lunr.du.min.js create mode 100644 assets/javascripts/lunr/min/lunr.es.min.js create mode 100644 assets/javascripts/lunr/min/lunr.fi.min.js create mode 100644 assets/javascripts/lunr/min/lunr.fr.min.js create mode 100644 assets/javascripts/lunr/min/lunr.hi.min.js create mode 100644 assets/javascripts/lunr/min/lunr.hu.min.js create mode 100644 assets/javascripts/lunr/min/lunr.hy.min.js create mode 100644 assets/javascripts/lunr/min/lunr.it.min.js create mode 100644 assets/javascripts/lunr/min/lunr.ja.min.js create mode 100644 assets/javascripts/lunr/min/lunr.jp.min.js create mode 100644 assets/javascripts/lunr/min/lunr.ko.min.js create mode 100644 assets/javascripts/lunr/min/lunr.multi.min.js create mode 100644 assets/javascripts/lunr/min/lunr.nl.min.js create mode 100644 assets/javascripts/lunr/min/lunr.no.min.js create mode 100644 assets/javascripts/lunr/min/lunr.pt.min.js create mode 100644 assets/javascripts/lunr/min/lunr.ro.min.js create mode 100644 assets/javascripts/lunr/min/lunr.ru.min.js create mode 100644 assets/javascripts/lunr/min/lunr.stemmer.support.min.js create mode 100644 assets/javascripts/lunr/min/lunr.sv.min.js create mode 100644 assets/javascripts/lunr/min/lunr.ta.min.js create mode 100644 assets/javascripts/lunr/min/lunr.th.min.js create mode 100644 assets/javascripts/lunr/min/lunr.tr.min.js create mode 100644 assets/javascripts/lunr/min/lunr.vi.min.js create mode 100644 assets/javascripts/lunr/min/lunr.zh.min.js create mode 100644 assets/javascripts/lunr/tinyseg.js create mode 100644 assets/javascripts/lunr/wordcut.js create mode 100644 assets/javascripts/workers/search.208ed371.min.js create mode 100644 assets/javascripts/workers/search.208ed371.min.js.map create mode 100644 assets/stylesheets/main.85bb2934.min.css create mode 100644 assets/stylesheets/main.85bb2934.min.css.map create mode 100644 assets/stylesheets/palette.a6bdf11c.min.css create mode 100644 assets/stylesheets/palette.a6bdf11c.min.css.map create mode 100644 characteristics/index.html create mode 100644 examples/fa-examples/index.html create mode 100644 index.html create mode 100644 migration/index.html create mode 100644 objects.inv create mode 100644 people/index.html create mode 100644 search/search_index.json create mode 100644 sitemap.xml create mode 100644 sitemap.xml.gz diff --git a/.nojekyll b/.nojekyll new file mode 100644 index 00000000..e69de29b diff --git a/404.html b/404.html new file mode 100644 index 00000000..896fcfa6 --- /dev/null +++ b/404.html @@ -0,0 +1,1388 @@ + + + +
+ + + + + + + + + + + + + + + +Exception classes shared by all automata.
+ + + +AutomatonException
+
+
+¶
+ Bases: Exception
The base class for all automaton-related errors.
+ + +DiagramException
+
+
+¶
+ Bases: AutomatonException
The diagram cannot be produced
+ + +EmptyLanguageException
+
+
+¶
+ Bases: AutomatonException
The operation cannot be performed because the language is empty
+ + +FinalStateError
+
+
+¶
+ Bases: AutomatonException
A final state fails to meet some required condition.
+ + +InfiniteLanguageException
+
+
+¶
+ Bases: AutomatonException
The operation cannot be performed because the language is infinite
+ + +InitialStateError
+
+
+¶
+ Bases: AutomatonException
The initial state fails to meet some required condition.
+ + +InvalidRegexError
+
+
+¶
+ Bases: RegexException
Regular expression is invalid
+ + +InvalidStateError
+
+
+¶
+ Bases: AutomatonException
A state is not a valid state for this automaton.
+ + +InvalidSymbolError
+
+
+¶
+ Bases: AutomatonException
A symbol is not a valid symbol for this automaton.
+ + +LexerError
+
+
+
+ dataclass
+
+
+¶
+ Bases: RegexException
An exception raised for issues in lexing
+ + + + +MissingStateError
+
+
+¶
+ Bases: AutomatonException
A state is missing from the automaton definition.
+ + +MissingSymbolError
+
+
+¶
+ Bases: AutomatonException
A symbol is missing from the automaton definition.
+ + +RegexException
+
+
+¶
+ Bases: Exception
The base class for all regular expression related errors
+ + +RejectionException
+
+
+¶
+ Bases: AutomatonException
The input was rejected by the automaton.
+ + +SymbolMismatchError
+
+
+¶
+ Bases: AutomatonException
The input symbols between the given automata do not match
+ + +An abstract base class for all automata, including Turing machines.
+ + + + +input_parameters: Dict[str, Any]
+
+
+ property
+
+
+¶Return the public attributes for this automaton.
+ + + +Returns:
+Type | +Description | +
---|---|
+ Dict[str, Any]
+ |
+
+
+
+ A dictionary mapping attribute names to their values. + |
+
accepts_input(input_str)
+
+¶Return True if this automaton accepts the given input.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_str |
+
+ str
+ |
+
+
+
+ The input string to check. + |
+ + required + | +
Returns:
+Type | +Description | +
---|---|
+ bool
+ |
+
+
+
+ True if this automaton accepts the given input; False otherwise. + |
+
copy()
+
+¶Create a deep copy of the automaton.
+ + + +Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ A deep copy of the automaton. + |
+
read_input(input_str)
+
+¶Check if the given string is accepted by this automaton.
+Return the automaton's final configuration if this string is valid.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_str |
+
+ str
+ |
+
+
+
+ The input string to check. + |
+ + required + | +
Returns:
+Type | +Description | +
---|---|
+ AutomatonStateT
+ |
+
+
+
+ The final configuration of the automaton after reading the input. + |
+
read_input_stepwise(input_str)
+
+
+ abstractmethod
+
+
+¶Return a generator that yields each step while reading input.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_str |
+
+ str
+ |
+
+
+
+ The input string to read. + |
+ + required + | +
Yields:
+Type | +Description | +
---|---|
+ Generator[Any, None, None]
+ |
+
+
+
+ A generator that yields the current configuration of the automaton +after each step of reading input. + |
+
Raises:
+Type | +Description | +
---|---|
+ NotImplementedError
+ |
+
+
+
+ If this method has not been implemented by a subclass. + |
+
validate()
+
+
+ abstractmethod
+
+
+¶Raises an exception if this automaton is not internally consistent.
+ + + +Raises:
+Type | +Description | +
---|---|
+ NotImplementedError
+ |
+
+
+
+ If this method has not been implemented by a subclass. + |
+
Classes and methods for working with deterministic finite automata.
+ + + +DFA
+
+
+¶
+ Bases: FA
The DFA
class is a subclass of FA
and represents a deterministic finite
+automaton.
Every DFA can be rendered natively inside of a Jupyter notebook
+(automatically calling show_diagram
without any arguments) if installed
+with the visual
optional dependency.
Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
states |
+
+ AbstractSet[DFAStateT]
+ |
+
+
+
+ Set of the DFA's valid states. + |
+ + required + | +
input_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ Set of the DFA's valid input symbols, each of which is a singleton +string + |
+ + required + | +
transitions |
+
+ Mapping[DFAStateT, Mapping[str, DFAStateT]]
+ |
+
+
+
+ Dict consisting of the transitions for each state. Each key is a +state name, and each value is another dict which maps a symbol +(the key) to a state (the value). + |
+ + required + | +
initial_state |
+
+ DFAStateT
+ |
+
+
+
+ The initial state for this DFA. + |
+ + required + | +
final_states |
+
+ AbstractSet[DFAStateT]
+ |
+
+
+
+ A set of final states for this DFA + |
+ + required + | +
allow_partial |
+
+ bool
+ |
+
+
+
+ By default, each DFA state must have a transition to
+every input symbol; if this parameter is |
+
+ True
+ |
+
cardinality()
+
+¶Returns the cardinality of the language represented by the DFA.
+ + + +Returns:
+Type | +Description | +
---|---|
+ int
+ |
+
+
+
+ The cardinality of the language accepted by self. + |
+
Raises:
+Type | +Description | +
---|---|
+ InfiniteLanguageException
+ |
+
+
+
+ Raised if self accepts an infinite language. + |
+
clear_cache()
+
+¶Resets the word and count caches. +Can be called if too much memory is being used.
+ +complement(*, retain_names=False, minify=True)
+
+¶Creates a DFA which accepts an input if and only if the old one does not. +Minifies by default. Unreachable states are always removed. Partial DFAs +are converted into complete ones.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
retain_names |
+
+ bool
+ |
+
+
+
+ Whether to retain state names through the complement and optional +minify. + |
+
+ False
+ |
+
minify |
+
+ bool
+ |
+
+
+
+ Whether to minify the result of the complement of the input DFA. + |
+
+ True
+ |
+
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ A DFA accepting the complement of the input DFA. State +minimal by default. + |
+
count_mod(input_symbols, k, *, remainders=None, symbols_to_count=None)
+
+
+ classmethod
+
+
+¶Directly computes a DFA that counts given symbols and accepts all strings where +the remainder of division by k is in the set of remainders given. +The default value of remainders is {0} and all symbols are counted by default.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ The set of input symbols to construct the DFA over. + |
+ + required + | +
k |
+
+ int
+ |
+
+
+
+ The number to divide the length by. + |
+ + required + | +
remainders |
+
+ Optional[AbstractSet[int]]
+ |
+
+
+
+ The remainders to accept. If set to None, defaults to {0}. + |
+
+ None
+ |
+
symbols_to_count |
+
+ Optional[AbstractSet[str]]
+ |
+
+
+
+ The input symbols to count towards the length of words to accepts. +If set to None, counts all symbols. + |
+
+ None
+ |
+
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ The DFA accepting the desired language. + |
+
count_words_of_length(k)
+
+¶Returns count of words of length k accepted by the DFA.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
k |
+
+ int
+ |
+
+
+
+ The desired word length. + |
+ + required + | +
Returns:
+Type | +Description | +
---|---|
+ int
+ |
+
+
+
+ The number of words of length k accepted by self. + |
+
difference(other, *, retain_names=False, minify=True)
+
+¶Takes as input two DFAs M1 and M2 which +accept languages L1 and L2 respectively. +Returns a DFA which accepts the difference of L1 and L2.
+Minifies by default. Unreachable states are always removed. +If either input DFA is partial, the result is partial.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
other |
+
+ DFA
+ |
+
+
+
+ The DFA we want to take a difference with. + |
+ + required + | +
retain_names |
+
+ bool
+ |
+
+
+
+ Whether to retain state names through the difference and optional minify. + |
+
+ False
+ |
+
minify |
+
+ bool
+ |
+
+
+
+ Whether to minify the result of the difference of the two DFAs. + |
+
+ True
+ |
+
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ A DFA accepting the difference of the two input DFAs. State minimal by +default. + |
+
empty_language(input_symbols)
+
+
+ classmethod
+
+
+¶Directly computes the minimal DFA rejecting all strings.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ The set of input symbols to construct the DFA over. + |
+ + required + | +
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ The DFA accepting the desired language. + |
+
from_finite_language(input_symbols, language, as_partial=True)
+
+
+ classmethod
+
+
+¶Directly computes the minimal DFA accepting the finite language given as input. +Uses the algorithm described in Finite-State Techniques by Mihov and Schulz, +Chapter 10
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ The set of input symbols to construct the DFA over. + |
+ + required + | +
language |
+
+ AbstractSet[str]
+ |
+
+
+
+ The language to accept. + |
+ + required + | +
as_partial |
+
+ bool
+ |
+
+
+
+ Whether or not to construct this as a partial DFA. + |
+
+ True
+ |
+
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ The DFA accepting the desired language. + |
+
from_nfa(target_nfa, *, retain_names=False, minify=True)
+
+
+ classmethod
+
+
+¶Initialize this DFA as one equivalent to the given NFA. Note +that this usually returns a partial DFA by default.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
target_nfa |
+
+ NFA
+ |
+
+
+
+ The NFA to construct an equivalent DFA for. + |
+ + required + | +
retain_names |
+
+ bool
+ |
+
+
+
+ Whether or not to retain state names during processing. + |
+
+ False
+ |
+
minify |
+
+ bool
+ |
+
+
+
+ Whether or not to minify the DFA resulting from the input NFA. + |
+
+ True
+ |
+
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ The DFA accepting the language of the input NFA. + |
+
from_prefix(input_symbols, prefix, *, contains=True, as_partial=True)
+
+
+ classmethod
+
+
+¶Directly computes the minimal DFA accepting strings with the
+given prefix. If contains
is set to False
then the complement is
+constructed instead.
Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ The set of input symbols to construct the DFA over. + |
+ + required + | +
prefix |
+
+ str
+ |
+
+
+
+ The prefix of strings that are accepted by this DFA. + |
+ + required + | +
contains |
+
+ bool
+ |
+
+
+
+ Whether or not to construct the compliment DFA. + |
+
+ True
+ |
+
as_partial |
+
+ bool
+ |
+
+
+
+ Whether or not to construct this DFA as a partial DFA. + |
+
+ True
+ |
+
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ The DFA accepting the desired language. + |
+
from_subsequence(input_symbols, subsequence, *, contains=True)
+
+
+ classmethod
+
+
+¶Directly computes the minimal DFA recognizing strings containing the
+given subsequence. If contains
is set to False
, then the complement
+is constructed instead.
Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ The set of input symbols to construct the DFA over. + |
+ + required + | +
subsequence |
+
+ str
+ |
+
+
+
+ The target subsequence of strings that are accepted by this DFA. + |
+ + required + | +
contains |
+
+ bool
+ |
+
+
+
+ Whether or to construct the compliment DFA. + |
+
+ True
+ |
+
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ The DFA accepting the desired language. + |
+
from_substring(input_symbols, substring, *, contains=True, must_be_suffix=False)
+
+
+ classmethod
+
+
+¶Directly computes the minimal DFA recognizing strings containing the
+given substring. If contains
is set to False
then the complement
+is constructed instead. If must_be_suffix
is set to True
, then
+the substring must be a suffix instead.
Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ The set of input symbols to construct the DFA over. + |
+ + required + | +
substring |
+
+ str
+ |
+
+
+
+ The substring of strings that are accepted by this DFA. + |
+ + required + | +
contains |
+
+ bool
+ |
+
+
+
+ Whether or to construct the compliment DFA. + |
+
+ True
+ |
+
must_be_suffix |
+
+ bool
+ |
+
+
+
+ Whether or not the target substring must be a suffix. + |
+
+ False
+ |
+
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ The DFA accepting the desired language. + |
+
from_suffix(input_symbols, suffix, *, contains=True)
+
+
+ classmethod
+
+
+¶Directly computes the minimal DFA recognizing strings with the
+given suffix. If contains
is set to False
, then the complement
+is constructed instead.
Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ The set of input symbols to construct the DFA over. + |
+ + required + | +
suffix |
+
+ str
+ |
+
+
+
+ The suffix of strings that are accepted by this DFA. + |
+ + required + | +
contains |
+
+ bool
+ |
+
+
+
+ Whether or not to construct the compliment DFA. + |
+
+ True
+ |
+
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ The DFA accepting the desired language. + |
+
intersection(other, *, retain_names=False, minify=True)
+
+¶Takes as input two DFAs M1 and M2 which +accept languages L1 and L2 respectively. +Returns a DFA which accepts the intersection of L1 and L2.
+Minifies by default. Unreachable states are always removed. +If either input DFA is partial, the result is partial.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
other |
+
+ DFA
+ |
+
+
+
+ The DFA we want to take a intersection with. + |
+ + required + | +
retain_names |
+
+ bool
+ |
+
+
+
+ Whether to retain state names through the intersection and optional minify. + |
+
+ False
+ |
+
minify |
+
+ bool
+ |
+
+
+
+ Whether to minify the result of the intersection of the two DFAs. + |
+
+ True
+ |
+
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ A DFA accepting the intersection of the two input DFAs. State minimal by +default. + |
+
isdisjoint(other)
+
+¶Returns True if the language accepted by self is disjoint from that of other.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
other |
+
+ DFA
+ |
+
+
+
+ The other DFA we are comparing our language against. + |
+ + required + | +
Returns:
+Type | +Description | +
---|---|
+ bool
+ |
+
+
+
+ True if self is disjoint from other, False otherwise. + |
+
isempty()
+
+¶Returns True if the language accepted by self is empty.
+ + + +Returns:
+Type | +Description | +
---|---|
+ bool
+ |
+
+
+
+ True if self accepts the empty language, False otherwise. + |
+
isfinite()
+
+¶Returns True if the language accepted by self is finite.
+ + + +Returns:
+Type | +Description | +
---|---|
+ bool
+ |
+
+
+
+ True if self accepts a finite language, False otherwise. + |
+
issubset(other)
+
+¶Returns True if the language accepted by self is a subset of that of other.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
other |
+
+ DFA
+ |
+
+
+
+ The other DFA we are comparing our language against. + |
+ + required + | +
Returns:
+Type | +Description | +
---|---|
+ bool
+ |
+
+
+
+ True if self is a subset of other, False otherwise. + |
+
issuperset(other)
+
+¶Returns True if the language accepted by self is a superset of that of other.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
other |
+
+ DFA
+ |
+
+
+
+ The other DFA we are comparing our language against. + |
+ + required + | +
Returns:
+Type | +Description | +
---|---|
+ bool
+ |
+
+
+
+ True if self is a superset of other, False otherwise. + |
+
iter_transitions()
+
+¶Iterate over all transitions in the DFA. Each transition is a tuple +of the form (from_state, to_state, symbol).
+ + + +Returns:
+Type | +Description | +
---|---|
+ Generator[Tuple[DFAStateT, DFAStateT, str], None, None]
+ |
+
+
+
+ The desired generator over the DFA transitions. + |
+
maximum_word_length()
+
+¶Returns the length of the longest word in the language accepted by the DFA
+In the case of infinite languages, None
is returned.
Returns:
+Type | +Description | +
---|---|
+ Optional[int]
+ |
+
+
+
+ The length of the longest word accepted by self. None if the language +is infinite. + |
+
Raises:
+Type | +Description | +
---|---|
+ EmptyLanguageException
+ |
+
+
+
+ Raised if self accepts the empty language. + |
+
minify(retain_names=False)
+
+¶Create a minimal DFA which accepts the same inputs as this DFA.
+First, non-reachable states are removed. +Then, indistinguishable states are merged using Hopcroft's Algorithm.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
retain_names |
+
+ bool
+ |
+
+
+
+ Whether to retain original names when merging states. +New names are from 0 to n-1. + |
+
+ False
+ |
+
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ A state-minimal equivalent DFA. May be complete in some cases +if the input is partial. + |
+
minimum_word_length()
+
+¶Returns the length of the shortest word in the language accepted by the DFA.
+ + + +Returns:
+Type | +Description | +
---|---|
+ int
+ |
+
+
+
+ The length of the shortest word accepted by self. + |
+
Raises:
+Type | +Description | +
---|---|
+ EmptyLanguageException
+ |
+
+
+
+ Raised if self accepts an empty language. + |
+
nth_from_end(input_symbols, symbol, n)
+
+
+ classmethod
+
+
+¶Directly computes the minimal DFA which accepts all words whose n
th
+character from the end is symbol
, where n
is a positive integer.
Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ The set of input symbols to construct the DFA over. + |
+ + required + | +
symbol |
+
+ str
+ |
+
+
+
+ The target input symbol. + |
+ + required + | +
n |
+
+ int
+ |
+
+
+
+ The position of the target input symbol. + |
+ + required + | +
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ The DFA accepting the desired language. + |
+
nth_from_start(input_symbols, symbol, n)
+
+
+ classmethod
+
+
+¶Directly computes the minimal DFA which accepts all words whose n
th
+character from the start is symbol
, where n
is a positive integer.
Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ The set of input symbols to construct the DFA over. + |
+ + required + | +
symbol |
+
+ str
+ |
+
+
+
+ The target input symbol. + |
+ + required + | +
n |
+
+ int
+ |
+
+
+
+ The position of the target input symbol. + |
+ + required + | +
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ The DFA accepting the desired language. + |
+
of_length(input_symbols, *, min_length=0, max_length=None, symbols_to_count=None)
+
+
+ classmethod
+
+
+¶Directly computes the minimal DFA recognizing strings whose length is
+between min_length
and max_length
, inclusive. To allow arbitrarily
+long words, the value None
can be passed in for max_length
.
Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ The set of input symbols to construct the DFA over. + |
+ + required + | +
min_length |
+
+ int
+ |
+
+
+
+ The minimum length of strings to be accepted by this DFA. + |
+
+ 0
+ |
+
max_length |
+
+ Optional[int]
+ |
+
+
+
+ The maximum length of strings to be accepted by this DFA. +If set to None, there is no maximum. + |
+
+ None
+ |
+
symbols_to_count |
+
+ Optional[AbstractSet[str]]
+ |
+
+
+
+ The input symbols to count towards the length of words to accepts. +If set to None, counts all symbols. + |
+
+ None
+ |
+
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ The DFA accepting the desired language. + |
+
predecessor(input_str, *, strict=True, key=None)
+
+¶Returns the first string accepted by the DFA that comes before +the input string in lexicographical order.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_str |
+
+ str
+ |
+
+
+
+ The starting input string. + |
+ + required + | +
strict |
+
+ bool
+ |
+
+
+
+ If set to false and input_str is accepted by the DFA, input_str will be +returned. + |
+
+ True
+ |
+
key |
+
+ Optional[Callable]
+ |
+
+
+
+ Function for defining custom lexicographical ordering. Defaults to using +the standard string ordering. + |
+
+ None
+ |
+
Returns:
+Type | +Description | +
---|---|
+ str
+ |
+
+
+
+ The first string accepted by the DFA lexicographically before input_string. + |
+
Raises:
+Type | +Description | +
---|---|
+ InfiniteLanguageException
+ |
+
+
+
+ Raised if the language accepted by self is infinite, as we cannot +generate predecessors in this case. + |
+
predecessors(input_str, *, strict=True, key=None)
+
+¶Generates all strings that come before the input string +in lexicographical order.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_str |
+
+ str
+ |
+
+
+
+ The starting input string. + |
+ + required + | +
strict |
+
+ bool
+ |
+
+
+
+ If set to false and input_str is accepted by the DFA, input_str will be +returned. + |
+
+ True
+ |
+
key |
+
+ Optional[Callable]
+ |
+
+
+
+ Function for defining custom lexicographical ordering. Defaults to using +the standard string ordering. + |
+
+ None
+ |
+
Returns:
+Type | +Description | +
---|---|
+ Generator[str, None, None]
+ |
+
+
+
+ A generator for all strings that come before the input string in +lexicographical order. + |
+
Raises:
+Type | +Description | +
---|---|
+ InfiniteLanguageException
+ |
+
+
+
+ Raised if the language accepted by self is infinite, as we cannot +generate predecessors in this case. + |
+
random_word(k, *, seed=None)
+
+¶Returns a random word of length k accepted by self.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
k |
+
+ int
+ |
+
+
+
+ The length of the desired word. + |
+ + required + | +
seed |
+
+ Optional[int]
+ |
+
+
+
+ The random seed to use for the sampling of the random word. + |
+
+ None
+ |
+
Returns:
+Type | +Description | +
---|---|
+ str
+ |
+
+
+
+ A uniformly random word of length k accepted by the DFA self. + |
+
Raises:
+Type | +Description | +
---|---|
+ ValueError
+ |
+
+
+
+ If this DFA does not accept any words of length k. + |
+
read_input_stepwise(input_str, ignore_rejection=False)
+
+¶Return a generator that yields each step while reading input.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_str |
+
+ str
+ |
+
+
+
+ The input string to read. + |
+ + required + | +
ignore_rejection |
+
+ bool
+ |
+
+
+
+ Whether to throw an exception if the input string is rejected. + |
+
+ False
+ |
+
Yields:
+Type | +Description | +
---|---|
+ Generator[DFAStateT, None, None]
+ |
+
+
+
+ A generator that yields the current configuration of the DFA +after each step of reading input. + |
+
Raises:
+Type | +Description | +
---|---|
+ RejectionException
+ |
+
+
+
+ Raised if this DFA does not accept the input string. + |
+
successor(input_str, *, strict=True, key=None)
+
+¶Returns the first string accepted by the DFA that comes after +the input string in lexicographical order.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_str |
+
+ Optional[str]
+ |
+
+
+
+ The starting input string. If None, will generate all words. + |
+ + required + | +
strict |
+
+ bool
+ |
+
+
+
+ If set to false and input_str is accepted by the DFA, input_str will be +returned. + |
+
+ True
+ |
+
key |
+
+ Optional[Callable]
+ |
+
+
+
+ Function for defining custom lexicographical ordering. Defaults to using +the standard string ordering. + |
+
+ None
+ |
+
Returns:
+Type | +Description | +
---|---|
+ str
+ |
+
+
+
+ The first string accepted by the DFA lexicographically before input_string. + |
+
successors(input_str, *, strict=True, key=None, reverse=False)
+
+¶Generates all strings that come after the input string +in lexicographical order.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_str |
+
+ Optional[str]
+ |
+
+
+
+ The starting input string. If None, will generate all words. + |
+ + required + | +
strict |
+
+ bool
+ |
+
+
+
+ If set to false and input_str is accepted by the DFA, input_str will be +returned. + |
+
+ True
+ |
+
key |
+
+ Optional[Callable]
+ |
+
+
+
+ Function for defining custom lexicographical ordering. Defaults to using +the standard string ordering. + |
+
+ None
+ |
+
reverse |
+
+ bool
+ |
+
+
+
+ If True, then predecessors will be generated instead of successors. + |
+
+ False
+ |
+
Returns:
+Type | +Description | +
---|---|
+ Generator[str, None, None]
+ |
+
+
+
+ A generator for all strings that come after the input string in +lexicographical order. + |
+
symmetric_difference(other, *, retain_names=False, minify=True)
+
+¶Takes as input two DFAs M1 and M2 which +accept languages L1 and L2 respectively. +Returns a DFA which accepts the symmetric difference of L1 and L2.
+Minifies by default. Unreachable states are always removed. +If either input DFA is partial, the result is partial.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
other |
+
+ DFA
+ |
+
+
+
+ The DFA we want to take a symmetric difference with. + |
+ + required + | +
retain_names |
+
+ bool
+ |
+
+
+
+ Whether to retain state names through the symmetric difference and optional +minify. + |
+
+ False
+ |
+
minify |
+
+ bool
+ |
+
+
+
+ Whether to minify the result of the symmetric difference of the two DFAs. + |
+
+ True
+ |
+
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ A DFA accepting the symmetric difference of the two input DFAs. State +minimal by default. + |
+
to_complete(trap_state=None)
+
+¶Creates an equivalent complete DFA with trap_state used as the name +for an added trap state. If trap_state is not passed in, defaults to +the largest negative integer which is not already a state name. +If the DFA is already complete, just returns a copy.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
trap_state |
+
+ Optional[DFAStateT]
+ |
+
+
+
+ Name for custom trap state to be used. + |
+
+ None
+ |
+
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ An equivalent complete DFA. + |
+
to_partial(*, retain_names=False, minify=True)
+
+¶Turns a DFA (complete or not) into a partial DFA. +Removes dead states and trap states (except the initial state) + and all edges leading to them.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
minify |
+
+ bool
+ |
+
+
+
+ Whether to perform a minify operation while converting to +a partial DFA. + |
+
+ True
+ |
+
retain_names |
+
+ bool
+ |
+
+
+
+ Whether to retain state names during minification. + |
+
+ True
+ |
+
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ An equivalent partial DFA. + |
+
union(other, *, retain_names=False, minify=True)
+
+¶Takes as input two DFAs M1 and M2 which +accept languages L1 and L2 respectively. +Returns a DFA which accepts the union of L1 and L2.
+Minifies by default. Unreachable states are always removed. +If either input DFA is partial, the result is partial.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
other |
+
+ DFA
+ |
+
+
+
+ The DFA we want to take a union with. + |
+ + required + | +
retain_names |
+
+ bool
+ |
+
+
+
+ Whether to retain state names through the union and optional minify. + |
+
+ False
+ |
+
minify |
+
+ bool
+ |
+
+
+
+ Whether to minify the result of the union of the two DFAs. + |
+
+ True
+ |
+
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ A DFA accepting the union of the two input DFAs. State minimal by +default. + |
+
universal_language(input_symbols)
+
+
+ classmethod
+
+
+¶Directly computes the minimal DFA accepting all strings.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ The set of input symbols to construct the DFA over. + |
+ + required + | +
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ The DFA accepting the desired language. + |
+
validate()
+
+¶Raises an exception if this automaton is not internally consistent.
+ + + +Raises:
+Type | +Description | +
---|---|
+ InvalidStateError
+ |
+
+
+
+ If this DFA has invalid states in the transition dictionary. + |
+
+ MissingStateError
+ |
+
+
+
+ If this DFA has states missing from the transition dictionary. + |
+
+ InvalidSymbolError
+ |
+
+
+
+ If this DFA has invalid symbols in the transition dictionary. + |
+
+ MissingSymbolError
+ |
+
+
+
+ If this DFA is missing transitions on certain symbols. + |
+
words_of_length(k)
+
+¶Generates all words of length k
in the language accepted by the DFA.
Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
k |
+
+ int
+ |
+
+
+
+ The desired word length. + |
+ + required + | +
Returns:
+Type | +Description | +
---|---|
+ Generator[str, None, None]
+ |
+
+
+
+ A generator for all words of length k accepted by the DFA. + |
+
Classes and methods for working with all finite automata.
+ + + +FA
+
+
+¶
+ Bases: Automaton
The FA
class is an abstract base class from which all finite automata inherit.
+Every subclass of FA can be rendered natively inside of a Jupyter notebook
+(automatically calling show_diagram
without any arguments) if installed with
+the visual
optional dependency.
iter_transitions()
+
+
+ abstractmethod
+
+
+¶Iterate over all transitions in the automaton. Each transition is a tuple +of the form (from_state, to_state, symbol)
+ +show_diagram(input_str=None, path=None, *, layout_method='dot', horizontal=True, reverse_orientation=False, fig_size=None, font_size=14.0, arrow_size=0.85, state_separation=0.5)
+
+¶Generates a diagram of the associated automaton.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_str |
+
+ Optional[str]
+ |
+
+
+
+ String consisting of input symbols. If set, will add processing of +the input string to the diagram. + |
+
+ None
+ |
+
path |
+
+ Union[str, PathLike, None]
+ |
+
+
+
+ Path to output file. If None, the output will not be saved. + |
+
+ None
+ |
+
horizontal |
+
+ bool
+ |
+
+
+
+ Direction of node layout in the output graph. + |
+
+ True
+ |
+
reverse_orientation |
+
+ bool
+ |
+
+
+
+ Reverse direction of node layout in the output graph. + |
+
+ False
+ |
+
fig_size |
+
+ Union[Tuple[float, float], Tuple[float], None]
+ |
+
+
+
+ Figure size. + |
+
+ None
+ |
+
font_size |
+
+ float
+ |
+
+
+
+ Font size in the output graph. + |
+
+ 14.0
+ |
+
arrow_size |
+
+ float
+ |
+
+
+
+ Arrow size in the output graph. + |
+
+ 0.85
+ |
+
state_separation |
+
+ float
+ |
+
+
+
+ Distance between nodes in the output graph. + |
+
+ 0.5
+ |
+
Returns:
+Type | +Description | +
---|---|
+ AGraph
+ |
+
+
+
+ A diagram of the given automaton. + |
+
Classes and methods for working with generalized non-deterministic finite +automata.
+ + + +GNFA
+
+
+¶
+ Bases: FA
The GNFA
class is a subclass of FA
and represents a generalized
+nondeterministic finite automaton.
Its main usage is for conversion of DFAs and NFAs to regular expressions. Note
+that because of this, the GNFA
doesn't support any binary operators or reading
+input (e.g. read_input_stepwise
). Every GNFA can be rendered natively inside of-
+a Jupyter notebook (automatically calling show_diagram
without any arguments)
+if installed with the visual
optional dependency. Note that input_str
+cannot be set as an argument to show_diagram
, as the GNFA
does not read input.
Except for initial_state
and final_state
, one transition goes from every state
+to every other state, and also from each state to itself. To accommodate
+this, transitions can be regular expressions and None
, in addition to
+normal input symbols.
Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
states |
+
+ AbstractSet[GNFAStateT]
+ |
+
+
+
+ Set of the GNFA's valid states. + |
+ + required + | +
input_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ Set of the GNFA's valid input symbols, each of which is a singleton +string. + |
+ + required + | +
transitions |
+
+ Mapping[GNFAStateT, Mapping[GNFAStateT, Optional[str]]]
+ |
+
+
+
+ A dict with the transitions for each state, except This is a subset of the standard regular expressions using this package. + |
+ + required + | +
initial_state |
+
+ GNFAStateT
+ |
+
+
+
+ The initial state for this GNFA. Has transitions going to every +other state, but no transitions coming in from any other state. + |
+ + required + | +
final_state |
+
+ GNFAStateT
+ |
+
+
+
+ A single final state for this GNFA. Has transitions coming in from every
+other state, but no transitions going to any other state.
+Must be different from the |
+ + required + | +
from_dfa(target_dfa)
+
+
+ classmethod
+
+
+¶Initialize this GNFA as one equivalent to the given DFA.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
target_dfa |
+
+ DFA
+ |
+
+
+
+ The DFA to construct an equivalent GNFA for. + |
+ + required + | +
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ The GNFA accepting the language of the input DFA. + |
+
from_nfa(target_nfa)
+
+
+ classmethod
+
+
+¶Initialize this GNFA as one equivalent to the given NFA.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
target_nfa |
+
+ NFA
+ |
+
+
+
+ The NFA to construct an equivalent GNFA for. + |
+ + required + | +
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ The GNFA accepting the language of the input NFA. + |
+
iter_transitions()
+
+¶Iterate over all transitions in the GNFA. Each transition is a tuple +of the form (from_state, to_state, symbol).
+ + + +Returns:
+Type | +Description | +
---|---|
+ Generator[Tuple[GNFAStateT, GNFAStateT, str], None, None]
+ |
+
+
+
+ The desired generator over the GNFA transitions. + |
+
to_regex()
+
+¶Convert GNFA to regular expression.
+ + + +Returns:
+Type | +Description | +
---|---|
+ str
+ |
+
+
+
+ A regular expression equivalent to the input GNFA. + |
+
validate()
+
+¶Raises an exception if this automaton is not internally consistent.
+ + + +Raises:
+Type | +Description | +
---|---|
+ InvalidStateError
+ |
+
+
+
+ If this GNFA has invalid states in the transition dictionary. + |
+
+ MissingStateError
+ |
+
+
+
+ If this GNFA has states missing from the transition dictionary. + |
+
+ InvalidRegexError
+ |
+
+
+
+ If this GNFA has invalid regex in the transition dictionary. + |
+
Classes and methods for working with nondeterministic finite automata.
+ + + +NFA
+
+
+¶
+ Bases: FA
The NFA
class is a subclass of FA
and represents a nondeterministic finite
+automaton.
Every NFA has the same five DFA properties: state
, input_symbols
,
+transitions
, initial_state
, and final_states
. However, the structure of
+the transitions
object has been modified slightly to accommodate the fact that
+a single state can have more than one transition for the same symbol. Therefore,
+instead of mapping a symbol to one end state in each sub-dict, each symbol is
+mapped to a set of end states.
Every NFA can be rendered natively inside of a Jupyter notebook (automatically
+calling show_diagram
without any arguments) if installed with the visual
+optional dependency.
Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
states |
+
+ AbstractSet[NFAStateT]
+ |
+
+
+
+ Set of the NFA's valid states. + |
+ + required + | +
input_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ Set of the NFA's valid input symbols, each of which is a singleton +string. + |
+ + required + | +
transitions |
+
+ Mapping[NFAStateT, Mapping[str, AbstractSet[NFAStateT]]]
+ |
+
+
+
+ Dict consisting of the transitions for each state. Each key is a +state name, and each value is another dict which maps a symbol +(the key) to a set of states (the value). + |
+ + required + | +
initial_state |
+
+ NFAStateT
+ |
+
+
+
+ The initial state for this NFA. + |
+ + required + | +
final_states |
+
+ AbstractSet[NFAStateT]
+ |
+
+
+
+ A set of final states for this NFA. + |
+ + required + | +
concatenate(other)
+
+¶Given two NFAs, M1 and M2, which accept the languages +L1 and L2 respectively, returns an NFA which accepts +the language L1 concatenated with L2.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
other |
+
+ NFA
+ |
+
+
+
+ The NFA we want to concatenate with. + |
+ + required + | +
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ An NFA accepting the language concatenation of the two input NFAs. + |
+
edit_distance(input_symbols, reference_str, max_edit_distance, *, insertion=True, deletion=True, substitution=True)
+
+
+ classmethod
+
+
+¶Constructs the Levenshtein NFA for the given reference_str and given +Levenshtein distance. This NFA recognizes strings within the given +Levenshtein distance (commonly called edit distance) of the +reference_str. Parameters control which error types the NFA will +recognize (insertions, deletions, or substitutions). At least one error +type must be set.
+If insertion and deletion are False and substitution is True, then this +is the same as Hamming distance.
+If insertion and deletion are True and substitution is False, then this +is the same as LCS distance.
+insertion, deletion, and substitution all default to True.
+Code adapted from: +http://blog.notdot.net/2010/07/Damn-Cool-Algorithms-Levenshtein-Automata
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ The set of input symbols to construct the NFA over. + |
+ + required + | +
reference_str |
+
+ str
+ |
+
+
+
+ The reference string the NFA will use to recognize other close strings. + |
+ + required + | +
max_edit_distance |
+
+ int
+ |
+
+
+
+ The maximum edit distance from the reference string this NFA will recognize. +Must be positive. + |
+ + required + | +
insertion |
+
+ bool
+ |
+
+
+
+ Whether to recognize insertion edits relative to the reference string. + |
+
+ True
+ |
+
deletion |
+
+ bool
+ |
+
+
+
+ Whether to recognize deletion edits relative to the reference string. + |
+
+ True
+ |
+
substitution |
+
+ bool
+ |
+
+
+
+ Whether to recognize substitution edits relative to the reference string. + |
+
+ True
+ |
+
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ An NFA accepting all strings within the given edit distance to the reference +string. + |
+
Raises:
+Type | +Description | +
---|---|
+ ValueError
+ |
+
+
+
+ Raised if the max_edit_distance is negative or all of the error flags are +set to False (at least one must be True). + |
+
eliminate_lambda()
+
+¶Returns an equivalent NFA with lambda transitions removed.
+ + + +Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ The equivalent NFA with lambda transitions removed. + |
+
from_dfa(target_dfa)
+
+
+ classmethod
+
+
+¶Initialize this NFA as one equivalent to the given DFA.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
target_dfa |
+
+ DFA
+ |
+
+
+
+ The DFA to construct an equivalent NFA for. + |
+ + required + | +
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ The NFA accepting the language of the input DFA. + |
+
from_regex(regex, *, input_symbols=None)
+
+
+ classmethod
+
+
+¶Initialize this NFA as one equivalent to the given regular expression.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
regex |
+
+ str
+ |
+
+
+
+ The regex to construct an equivalent NFA for. + |
+ + required + | +
input_symbols |
+
+ Optional[AbstractSet[str]]
+ |
+
+
+
+ The set of input symbols to create the NFA over. If not +set, defaults to all ascii letters and digits. + |
+
+ None
+ |
+
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ The NFA accepting the language of the input regex. + |
+
intersection(other)
+
+¶Given two NFAs, M1 and M2, which accept the languages +L1 and L2 respectively, returns an NFA which accepts +the intersection of L1 and L2.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
other |
+
+ NFA
+ |
+
+
+
+ The NFA we want to take an intersection with. + |
+ + required + | +
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ An NFA accepting the intersection of the two input NFAs. + |
+
iter_transitions()
+
+¶Iterate over all transitions in the NFA. Each transition is a tuple +of the form (from_state, to_state, symbol).
+ + + +Returns:
+Type | +Description | +
---|---|
+ Generator[Tuple[NFAStateT, NFAStateT, str], None, None]
+ |
+
+
+
+ The desired generator over the NFA transitions. + |
+
kleene_star()
+
+¶Given an NFA which accepts the language L, returns +an NFA which accepts L repeated 0 or more times.
+ + + +Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ An NFA accepting the finite repetition of the input NFA. + |
+
left_quotient(other)
+
+¶Given two NFAs, M1 and M2, which accept the languages +L1 and L2 respectively, returns an NFA which accepts +the left quotient of L1 with respect to L2 (L2 L1).
+Construction is based off of the one described here: +https://cs.stackexchange.com/a/102043
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
other |
+
+ NFA
+ |
+
+
+
+ The NFA we want to take a left quotient with. + |
+ + required + | +
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ An NFA accepting the left quotient of the two input NFAs. + |
+
option()
+
+¶Given an NFA which accepts the language L, returns +an NFA which accepts L repeated 0 or 1 times.
+ + + +Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ An NFA accepting the optional of the input NFA. + |
+
read_input_stepwise(input_str)
+
+¶Return a generator that yields the configuration of this NFA at each +step while reading input.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_str |
+
+ str
+ |
+
+
+
+ The input string to read. + |
+ + required + | +
Yields:
+Type | +Description | +
---|---|
+ Generator[AbstractSet[NFAStateT], None, None]
+ |
+
+
+
+ A generator that yields the current configuration (a set of states) of +the NFA after each step of reading input. + |
+
Raises:
+Type | +Description | +
---|---|
+ RejectionException
+ |
+
+
+
+ Raised if this NFA does not accept the input string. + |
+
reverse()
+
+¶Given an NFA which accepts the language L, +returns an NFA which accepts the reverse of L.
+ + + +Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ An NFA accepting the reversal of the input NFA. + |
+
right_quotient(other)
+
+¶Given two NFAs, M1 and M2, which accept the languages +L1 and L2 respectively, returns an NFA which accepts +the right quotient of L1 with respect to L2 (L1 / L2).
+Construction is based off of the one described here: +https://cs.stackexchange.com/a/102043
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
other |
+
+ NFA
+ |
+
+
+
+ The NFA we want to take a right quotient with. + |
+ + required + | +
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ An NFA accepting the right quotient of the two input NFAs. + |
+
shuffle_product(other)
+
+¶Given two NFAs, M1 and M2, which accept the languages +L1 and L2 respectively, returns an NFA which accepts +the shuffle of L1 and L2.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
other |
+
+ NFA
+ |
+
+
+
+ The NFA we want to take a shuffle product with. + |
+ + required + | +
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ An NFA accepting the shuffle product of the two input NFAs. + |
+
union(other)
+
+¶Given two NFAs, M1 and M2, which accept the languages +L1 and L2 respectively, returns an NFA which accepts +the union of L1 and L2.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
other |
+
+ NFA
+ |
+
+
+
+ The NFA we want to take a union with. + |
+ + required + | +
Returns:
+Type | +Description | +
---|---|
+ Self
+ |
+
+
+
+ An NFA accepting the union of the two input NFAs. + |
+
validate()
+
+¶Raises an exception if this automaton is not internally consistent.
+ + + +Raises:
+Type | +Description | +
---|---|
+ InvalidStateError
+ |
+
+
+
+ If this NFA has invalid states in the transition dictionary. + |
+
+ MissingStateError
+ |
+
+
+
+ If this NFA has states missing from the transition dictionary. + |
+
+ InvalidSymbolError
+ |
+
+
+
+ If this NFA has invalid symbols in the transition dictionary. + |
+
This is the API documentation generated by the class and method docstrings for the automata
package.
Classes and methods for working with deterministic pushdown automata.
+ + + +DPDA
+
+
+¶
+ Bases: PDA
The DPDA
class is a subclass of PDA
and represents a deterministic pushdown
+automaton.
Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
states |
+
+ AbstractSet[DPDAStateT]
+ |
+
+
+
+ A set of the DPDA's valid states + |
+ + required + | +
input_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ Set of the DPDA's valid input symbols, each of which is a singleton +string. + |
+ + required + | +
stack_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ Set of the DPDA's valid stack symbols, each of which is a singleton +string. + |
+ + required + | +
transitions |
+
+ DPDATransitionsT
+ |
+
+
+
+ A dict consisting of the transitions for each state; see the +example below for the exact syntax + |
+ + required + | +
initial_state |
+
+ DPDAStateT
+ |
+
+
+
+ The name of the initial state for this DPDA. + |
+ + required + | +
initial_stack_symbol |
+
+ str
+ |
+
+
+
+ The name of the initial symbol on the stack for this DPDA. + |
+ + required + | +
final_states |
+
+ AbstractSet[DPDAStateT]
+ |
+
+
+
+ A set of final states for this DPDA. + |
+ + required + | +
acceptance_mode |
+
+ PDAAcceptanceModeT
+ |
+
+
+
+ A string defining whether this DPDA accepts by
+ |
+
+ 'both'
+ |
+
from automata.pda.dpda import DPDA
+# DPDA which which matches zero or more 'a's, followed by the same
+# number of 'b's (accepting by final state)
+dpda = DPDA(
+ states={'q0', 'q1', 'q2', 'q3'},
+ input_symbols={'a', 'b'},
+ stack_symbols={'0', '1'},
+ transitions={
+ 'q0': {
+ 'a': {'0': ('q1', ('1', '0'))} # push '1' to stack
+ },
+ 'q1': {
+ 'a': {'1': ('q1', ('1', '1'))}, # push '1' to stack
+ 'b': {'1': ('q2', '')} # pop from stack
+ },
+ 'q2': {
+ 'b': {'1': ('q2', '')}, # pop from stack
+ '': {'0': ('q3', ('0',))} # no change to stack
+ }
+ },
+ initial_state='q0',
+ initial_stack_symbol='0',
+ final_states={'q3'},
+ acceptance_mode='final_state'
+)
+
read_input_stepwise(input_str)
+
+¶Return a generator that yields the configuration of this DPDA at each +step while reading input.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_str |
+
+ str
+ |
+
+
+
+ The input string to read. + |
+ + required + | +
Yields:
+Type | +Description | +
---|---|
+ Generator[PDAConfiguration, None, None]
+ |
+
+
+
+ A generator that yields the current configuration of +the DPDA after each step of reading input. + |
+
Raises:
+Type | +Description | +
---|---|
+ RejectionException
+ |
+
+
+
+ Raised if this DPDA does not accept the input string. + |
+
Classes and methods for working with nondeterministic pushdown automata.
+ + + +NPDA
+
+
+¶
+ Bases: PDA
The NPDA
class is a subclass of PDA
and represents a nondeterministic
+pushdown automaton.
Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
states |
+
+ AbstractSet[NPDAStateT]
+ |
+
+
+
+ A set of the NPDA's valid states + |
+ + required + | +
input_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ Set of the NPDA's valid input symbols, each of which is a singleton +string. + |
+ + required + | +
stack_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ Set of the NPDA's valid stack symbols, each of which is a singleton +string. + |
+ + required + | +
transitions |
+
+ NPDATransitionsT
+ |
+
+
+
+ A dict consisting of the transitions for each state; see the +example below for the exact syntax + |
+ + required + | +
initial_state |
+
+ NPDAStateT
+ |
+
+
+
+ The name of the initial state for this NPDA. + |
+ + required + | +
initial_stack_symbol |
+
+ str
+ |
+
+
+
+ The name of the initial symbol on the stack for this NPDA. + |
+ + required + | +
final_states |
+
+ AbstractSet[NPDAStateT]
+ |
+
+
+
+ A set of final states for this NPDA. + |
+ + required + | +
acceptance_mode |
+
+ PDAAcceptanceModeT
+ |
+
+
+
+ A string defining whether this NPDA accepts by
+ |
+
+ 'both'
+ |
+
from automata.pda.npda import NPDA
+# NPDA which matches palindromes consisting of 'a's and 'b's
+# (accepting by final state)
+# q0 reads the first half of the word, q1 the other half, q2 accepts.
+# But we have to guess when to switch.
+npda = NPDA(
+ states={'q0', 'q1', 'q2'},
+ input_symbols={'a', 'b'},
+ stack_symbols={'A', 'B', '#'},
+ transitions={
+ 'q0': {
+ '': {
+ '#': {('q2', '#')}, # no change to stack
+ },
+ 'a': {
+ '#': {('q0', ('A', '#'))}, # push 'A' to stack
+ 'A': {
+ ('q0', ('A', 'A')), # push 'A' to stack
+ ('q1', ''), # pop from stack
+ },
+ 'B': {('q0', ('A', 'B'))}, # push 'A' to stack
+ },
+ 'b': {
+ '#': {('q0', ('B', '#'))}, # push 'B' to stack
+ 'A': {('q0', ('B', 'A'))}, # push 'B' to stack
+ 'B': {
+ ('q0', ('B', 'B')), # push 'B' to stack
+ ('q1', ''), # pop from stack
+ },
+ },
+ },
+ 'q1': {
+ '': {'#': {('q2', '#')}}, # push '#' to (currently empty) stack
+ 'a': {'A': {('q1', '')}}, # pop from stack
+ 'b': {'B': {('q1', '')}}, # pop from stack
+ },
+ },
+ initial_state='q0',
+ initial_stack_symbol='#',
+ final_states={'q2'},
+ acceptance_mode='final_state'
+)
+
read_input_stepwise(input_str)
+
+¶Return a generator that yields the configuration of this NPDA at each +step while reading input.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_str |
+
+ str
+ |
+
+
+
+ The input string to read. + |
+ + required + | +
Yields:
+Type | +Description | +
---|---|
+ Generator[Set[PDAConfiguration], None, None]
+ |
+
+
+
+ A generator that yields the current configuration of +the NPDA after each step of reading input. + |
+
Raises:
+Type | +Description | +
---|---|
+ RejectionException
+ |
+
+
+
+ Raised if this NPDA does not accept the input string. + |
+
Classes and methods for working with all pushdown automata.
+ + + +PDA
+
+
+¶
+ Bases: Automaton
An abstract base class for pushdown automata.
+ + + + +iter_transitions()
+
+
+ abstractmethod
+
+
+¶Iterate over all transitions in the automaton. +Each transition is a tuple of the form +(from_state, to_state, (input_symbol, stack_top_symbol, stack_push_symbols))
+ +show_diagram(input_str=None, with_machine=True, with_stack=True, path=None, *, layout_method='dot', horizontal=True, reverse_orientation=False, fig_size=None, font_size=14.0, arrow_size=0.85, state_separation=0.5)
+
+¶Generates the graph associated with the given PDA.
+Args:
+ - input_str (str, optional): String list of input symbols. Defaults to None.
+ - with_machine (bool, optional): Constructs the diagram with states and
+ transitions. Ignored if input_str
is None. Default to True.
+ - with_stack (bool, optional): Constructs the diagram with stack and its
+ operations. Ignored if input_str
is None. Default to True.
+ - path (str or os.PathLike, optional): Path to output file. If
+ None, the output will not be saved.
+ - horizontal (bool, optional): Direction of node layout. Defaults
+ to True.
+ - reverse_orientation (bool, optional): Reverse direction of node
+ layout. Defaults to False.
+ - fig_size (tuple, optional): Figure size. Defaults to None.
+ - font_size (float, optional): Font size. Defaults to 14.0.
+ - arrow_size (float, optional): Arrow head size. Defaults to 0.85.
+ - state_separation (float, optional): Node distance. Defaults to 0.5.
+Returns:
+ AGraph corresponding to the given automaton.
validate()
+
+¶Raises an exception if this automaton is not internally consistent.
+ + + +Raises:
+Type | +Description | +
---|---|
+ InvalidStateError
+ |
+
+
+
+ If this PDA has invalid states in the transition dictionary. + |
+
+ MissingStateError
+ |
+
+
+
+ If this PDA has states missing from the transition dictionary. + |
+
+ InvalidSymbolError
+ |
+
+
+
+ If this PDA has invalid symbols in the transition dictionary. + |
+
+ InvalidAcceptanceModeError
+ |
+
+
+
+ If this PDA has an invalid acceptance mode. + |
+
A set of tools for working with regular expressions. Can recognize regular expressions +over the alphabet of ascii letters (lower and upper case), ascii digits, and subsets of +these.
+A regular expression with the following operations only are supported in this library:
+*
: Kleene star operation, language repeated zero or more times. Ex: a*
,(ab)*
+
: Kleene plus operation, language repeated one or more times. Ex: a+
,(ab)+
?
: Language repeated zero or one time. Ex: a?
abcd
|
: Union. Ex: a|b
&
: Intersection. Ex: a&b
.
: Wildcard. Ex: a.b
^
: Shuffle. Ex: a^b
{}
: Quantifiers expressing finite repetitions. Ex: a{1,2}
,a{3,}
()
: The empty string.(...)
: Grouping.This is similar to the Python re
module, but this library does not support any special
+characters other than those given above. All regular languages can be written with
+these.
isequal(re1, re2, *, input_symbols=None)
+
+¶Whether both regular expressions are equivalent.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
re1 |
+
+ str
+ |
+
+
+
+ The first regular expression as a string. + |
+ + required + | +
re2 |
+
+ str
+ |
+
+
+
+ The second regular expression as a string. + |
+ + required + | +
input_symbols |
+
+ Optional[AbstractSet[str]]
+ |
+
+
+
+ The set of input symbols when doing the comparison. Defaults to +all ascii letters and digits. + |
+
+ None
+ |
+
Returns:
+Type | +Description | +
---|---|
+ bool
+ |
+
+
+
+ Whether the regular expressions are equivalent. + |
+
issubset(re1, re2, *, input_symbols=None)
+
+¶Whether re1 is a subset of re2.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
re1 |
+
+ str
+ |
+
+
+
+ The first regular expression as a string. + |
+ + required + | +
re2 |
+
+ str
+ |
+
+
+
+ The second regular expression as a string. + |
+ + required + | +
input_symbols |
+
+ Optional[AbstractSet[str]]
+ |
+
+
+
+ The set of input symbols when doing the comparison. Defaults to +all ascii letters and digits. + |
+
+ None
+ |
+
Returns:
+Type | +Description | +
---|---|
+ bool
+ |
+
+
+
+ True if re1 is a subset of re2. + |
+
issuperset(re1, re2, *, input_symbols=None)
+
+¶Whether re1 is a superset of re2.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
re1 |
+
+ str
+ |
+
+
+
+ The first regular expression as a string. + |
+ + required + | +
re2 |
+
+ str
+ |
+
+
+
+ The second regular expression as a string. + |
+ + required + | +
input_symbols |
+
+ Optional[AbstractSet[str]]
+ |
+
+
+
+ The set of input symbols when doing the comparison. Defaults to +all ascii letters and digits. + |
+
+ None
+ |
+
Returns:
+Type | +Description | +
---|---|
+ bool
+ |
+
+
+
+ True if re1 is a superset of re2. + |
+
validate(regex)
+
+¶Raises an exception if the input regular expression is invalid.
+ + + +Raises:
+Type | +Description | +
---|---|
+ InvalidRegexError
+ |
+
+
+
+ Raised if the regex given as input is not well defined. + |
+
Classes and methods for working with deterministic Turing machines.
+ + + +DTM
+
+
+¶
+ Bases: TM
The DTM
class is a subclass of TM
and represents a deterministic Turing
+machine.
Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
states |
+
+ AbstractSet[DTMStateT]
+ |
+
+
+
+ A set of the DTM's valid states. + |
+ + required + | +
input_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ Set of the DTM's valid input symbols, each of which is a singleton +string. + |
+ + required + | +
tape_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ Set of the DTM's valid tape symbols, each of which is a singleton +string. + |
+ + required + | +
transitions |
+
+ DTMTransitionsT
+ |
+
+
+
+ Dict consisting of the transitions for each state; each key is a +state name, and each value is a dict which maps a symbol (the key) to +a tuple consisting of the next state, the symbol to write on the +tape, and the direction to move the tape head. + |
+ + required + | +
initial_state |
+
+ DTMStateT
+ |
+
+
+
+ The name of the initial state for this DTM. + |
+ + required + | +
blank_symbol |
+
+ str
+ |
+
+
+
+ A symbol from |
+ + required + | +
final_states |
+
+ AbstractSet[DTMStateT]
+ |
+
+
+
+ A set of final states for this DTM. + |
+ + required + | +
from automata.tm.dtm import DTM
+# DTM which matches all strings beginning with '0's, and followed by
+# the same number of '1's
+dtm = DTM(
+ states={'q0', 'q1', 'q2', 'q3', 'q4'},
+ input_symbols={'0', '1'},
+ tape_symbols={'0', '1', 'x', 'y', '.'},
+ transitions={
+ 'q0': {
+ '0': ('q1', 'x', 'R'),
+ 'y': ('q3', 'y', 'R')
+ },
+ 'q1': {
+ '0': ('q1', '0', 'R'),
+ '1': ('q2', 'y', 'L'),
+ 'y': ('q1', 'y', 'R')
+ },
+ 'q2': {
+ '0': ('q2', '0', 'L'),
+ 'x': ('q0', 'x', 'R'),
+ 'y': ('q2', 'y', 'L')
+ },
+ 'q3': {
+ 'y': ('q3', 'y', 'R'),
+ '.': ('q4', '.', 'R')
+ }
+ },
+ initial_state='q0',
+ blank_symbol='.',
+ final_states={'q4'}
+)
+
read_input_stepwise(input_str)
+
+¶Return a generator that yields the configuration of this DTM at each +step while reading input.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_str |
+
+ str
+ |
+
+
+
+ The input string to read. + |
+ + required + | +
Yields:
+Type | +Description | +
---|---|
+ Generator[TMConfiguration, None, None]
+ |
+
+
+
+ A generator that yields the current configuration of +the DTM after each step of reading input. + |
+
validate()
+
+¶Raises an exception if this automaton is not internally consistent.
+ + + +Raises:
+Type | +Description | +
---|---|
+ InvalidStateError
+ |
+
+
+
+ If this DTM has invalid states in the transition dictionary. + |
+
+ InvalidSymbolError
+ |
+
+
+
+ If this DTM has invalid symbols in the transition dictionary. + |
+
+ InvalidDirectionError
+ |
+
+
+
+ If this DTM has a transition with an invalid direction. + |
+
+ FinalStateError
+ |
+
+
+
+ If this DTM has a transition on any final states. + |
+
Classes and methods for working with multitape nondeterministic Turing +machines.
+ + + +MNTM
+
+
+¶
+ Bases: NTM
The MNTM
class is a subclass of TM
and represents a multitape
+(non)deterministic Turing machine.
Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
states |
+
+ AbstractSet[MNTMStateT]
+ |
+
+
+
+ A set of the MNTM's valid states. + |
+ + required + | +
input_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ Set of the MNTM's valid input symbols, each of which is a singleton +string. + |
+ + required + | +
tape_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ Set of the MNTM's valid tape symbols, each of which is a singleton +string. + |
+ + required + | +
n_tapes |
+
+ int
+ |
+
+
+
+ The number of tapes in this MNTM. + |
+ + required + | +
transitions |
+
+ MNTMTransitionsT
+ |
+
+
+
+ Dict consisting of the transitions for each state; each key is a +state name, and each value is a dict which maps a symbol (the key) to +a list of tuples consisting of the next state, the symbol to write on the +tape, and the direction to move the tape head. + |
+ + required + | +
initial_state |
+
+ MNTMStateT
+ |
+
+
+
+ The name of the initial state for this MNTM. + |
+ + required + | +
blank_symbol |
+
+ str
+ |
+
+
+
+ A symbol from |
+ + required + | +
final_states |
+
+ AbstractSet[MNTMStateT]
+ |
+
+
+
+ A set of final states for this MNTM. + |
+ + required + | +
from automata.tm.mntm import MNTM
+# MNTM which accepts all strings in {0, 1}* and writes all
+# 1's from the first tape (input) to the second tape.
+self.mntm1 = MNTM(
+ states={'q0', 'q1'},
+ input_symbols={'0', '1'},
+ tape_symbols={'0', '1', '#'},
+ n_tapes=2,
+ transitions={
+ 'q0': {
+ ('1', '#'): [('q0', (('1', 'R'), ('1', 'R')))],
+ ('0', '#'): [('q0', (('0', 'R'), ('#', 'N')))],
+ ('#', '#'): [('q1', (('#', 'N'), ('#', 'N')))],
+ }
+ },
+ initial_state='q0',
+ blank_symbol='#',
+ final_states={'q1'},
+)
+
read_input_as_ntm(input_str)
+
+¶Simulates this MNTM as a single-tape Turing machine. +Yields the configuration at each step.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_str |
+
+ str
+ |
+
+
+
+ The input string to read. + |
+ + required + | +
Yields:
+Type | +Description | +
---|---|
+ Generator[AbstractSet[TMConfiguration], None, None]
+ |
+
+
+
+ A generator that yields the current configuration of +the MNTM as a set after each step of reading input. + |
+
Raises:
+Type | +Description | +
---|---|
+ RejectionException
+ |
+
+
+
+ Raised if this MNTM does not accept the input string. + |
+
read_input_stepwise(input_str)
+
+¶Checks if the given string is accepted by this MNTM machine, +using a BFS of every possible configuration from each configuration. +Yields the current configuration of the machine at each step.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_str |
+
+ str
+ |
+
+
+
+ The input string to read. + |
+ + required + | +
Yields:
+Type | +Description | +
---|---|
+ Generator[Set[MTMConfiguration], None, None]
+ |
+
+
+
+ A generator that yields the current configuration of +the DTM after each step of reading input. + |
+
Raises:
+Type | +Description | +
---|---|
+ RejectionException
+ |
+
+
+
+ Raised if this MNTM does not accept the input string. + |
+
validate()
+
+¶Raises an exception if this automaton is not internally consistent.
+ + + +Raises:
+Type | +Description | +
---|---|
+ InvalidStateError
+ |
+
+
+
+ If this MNTM has invalid states in the transition dictionary. + |
+
+ InvalidSymbolError
+ |
+
+
+
+ If this MNTM has invalid symbols in the transition dictionary. + |
+
+ InvalidDirectionError
+ |
+
+
+
+ If this MNTM has a transition with an invalid direction. + |
+
+ FinalStateError
+ |
+
+
+
+ If this MNTM has a transition on any final states. + |
+
+ InconsistentTapesException
+ |
+
+
+
+ If this MNTM has inconsistent tape contents. + |
+
Classes and methods for working with nondeterministic Turing machines.
+ + + +NTM
+
+
+¶
+ Bases: TM
The NTM
class is a subclass of TM
and represents a
+nondeterministic Turing machine.
Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
states |
+
+ AbstractSet[NTMStateT]
+ |
+
+
+
+ A set of the NTM's valid states. + |
+ + required + | +
input_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ Set of the NTM's valid input symbols, each of which is a singleton +string. + |
+ + required + | +
tape_symbols |
+
+ AbstractSet[str]
+ |
+
+
+
+ Set of the NTM's valid tape symbols, each of which is a singleton +string. + |
+ + required + | +
transitions |
+
+ NTMTransitionsT
+ |
+
+
+
+ Dict consisting of the transitions for each state; each key is a +state name, and each value is a dict which maps a symbol (the key) to +a set of tuples consisting of the next state, the symbol to write on the +tape, and the direction to move the tape head. + |
+ + required + | +
initial_state |
+
+ NTMStateT
+ |
+
+
+
+ The name of the initial state for this NTM. + |
+ + required + | +
blank_symbol |
+
+ str
+ |
+
+
+
+ A symbol from |
+ + required + | +
final_states |
+
+ AbstractSet[NTMStateT]
+ |
+
+
+
+ A set of final states for this NTM. + |
+ + required + | +
from automata.tm.ntm import NTM
+# NTM which matches all strings beginning with '0's, and followed by
+# the same number of '1's
+# Note that the nondeterminism is not really used here.
+ntm = NTM(
+ states={'q0', 'q1', 'q2', 'q3', 'q4'},
+ input_symbols={'0', '1'},
+ tape_symbols={'0', '1', 'x', 'y', '.'},
+ transitions={
+ 'q0': {
+ '0': {('q1', 'x', 'R')},
+ 'y': {('q3', 'y', 'R')},
+ },
+ 'q1': {
+ '0': {('q1', '0', 'R')},
+ '1': {('q2', 'y', 'L')},
+ 'y': {('q1', 'y', 'R')},
+ },
+ 'q2': {
+ '0': {('q2', '0', 'L')},
+ 'x': {('q0', 'x', 'R')},
+ 'y': {('q2', 'y', 'L')},
+ },
+ 'q3': {
+ 'y': {('q3', 'y', 'R')},
+ '.': {('q4', '.', 'R')},
+ }
+ },
+ initial_state='q0',
+ blank_symbol='.',
+ final_states={'q4'}
+)
+
read_input_stepwise(input_str)
+
+¶Check if the given string is accepted by this Turing machine.
+Yield the current configurations of the machine at each step.
+ + + +Parameters:
+Name | +Type | +Description | +Default | +
---|---|---|---|
input_str |
+
+ str
+ |
+
+
+
+ The input string to read. + |
+ + required + | +
Yields:
+Type | +Description | +
---|---|
+ Generator[Set[TMConfiguration], None, None]
+ |
+
+
+
+ A generator that yields the current configuration of +the NTM after each step of reading input. + |
+
Raises:
+Type | +Description | +
---|---|
+ RejectionException
+ |
+
+
+
+ Raised if this NTM does not accept the input string. + |
+
validate()
+
+¶Raises an exception if this automaton is not internally consistent.
+ + + +Raises:
+Type | +Description | +
---|---|
+ InvalidStateError
+ |
+
+
+
+ If this NTM has invalid states in the transition dictionary. + |
+
+ InvalidSymbolError
+ |
+
+
+
+ If this NTM has invalid symbols in the transition dictionary. + |
+
+ InvalidDirectionError
+ |
+
+
+
+ If this NTM has a transition with an invalid direction. + |
+
+ FinalStateError
+ |
+
+
+
+ If this NTM has a transition on any final states. + |
+
Exception classes specific to Turing machines.
+ + + +InconsistentTapesException
+
+
+¶
+ Bases: TMException
The number of tapes defined for the multitape Turing machine is +not consistent with the definitions of the transitions.
+ + +InvalidDirectionError
+
+
+¶
+ Bases: TMException
A direction is not a valid direction for this machine.
+ + +MalformedExtendedTapeError
+
+
+¶
+ Bases: TMException
Extended tape for simulating an mntm as a ntm is not valid. +Either there are 2 virtual heads for a virtual tape or the +a head symbol is at the leftmost end of a virtual tape.
+ + +TMException
+
+
+¶
+ Bases: AutomatonException
The base class for all machine-related errors.
+ + +All Automaton instances are fully immutable to protect against common pitfalls, +such as mutating an automaton to an invalid state after it's already been +validated.
+This means that if you wish to make a change to an automaton instance, you must
+retrieve its attributes as a dictionary (using the input_parameters
property),
+make your desired change, then pass those parameters to the relevant
+constructor. For example:
from automata.fa.dfa import DFA
+
+dfa1 = DFA(
+ states={'q0', 'q1', 'q2'},
+ input_symbols={'0', '1'},
+ transitions={
+ 'q0': {'0': 'q0', '1': 'q1'},
+ 'q1': {'0': 'q0', '1': 'q2'},
+ 'q2': {'0': 'q2', '1': 'q1'}
+ },
+ initial_state='q0',
+ final_states={'q1'}
+)
+# If you want to make a change, you must create a new instance; please note
+# that dfa1.input_parameters is always a deep copy of the input parameters for
+# dfa1 (in other words, mutating dfa1.input_parameters will not actually mutate
+# dfa1)
+params = dfa1.input_parameters
+params['final_states'] = {'q2'}
+dfa2 = DFA(**params)
+
Automaton immutability is enforced via a "freeze" step during object +initialization that turns mutable parameters (such as sets or dicts) into their +immutable counterparts (frozensets/frozendicts).
+If your application requires maximum performance, you can disable this
+conversion via the allow_mutable_automata
global configuration option. If
+enabled, the user must ensure that their automaton instances are never modified,
+otherwise correct behavior cannot be guaranteed.
import automata.base.config as global_config
+
+global_config.allow_mutable_automata = True
+# The rest of your code...
+
By default, all Automaton instances are checked for common inconsistencies when
+they are instantiated. If inconsistencies are found, the appropriate exception
+from automata.base.exceptions
is raised.
Because this validation can be performance-intensive for large automaton +instances with many states/transitions, you can disable the automatic validation +using the global configuration feature (introduced in v7):
+import automata.base.config as global_config
+
+global_config.should_validate_automata = False
+
+# The rest of your code...
+
If, at any point, you wish to opt into validation for a specific Automaton instance, you can call the validate
method:
On this page, we give some short examples with discussion for the finite +automata classes and methods.
+The following code snippet creates a DFA
and prints whether it accepts or
+rejects user input.
from automata.fa.dfa import DFA
+
+# DFA which matches all binary strings ending in an odd number of '1's
+my_dfa = DFA(
+ states={'q0', 'q1', 'q2'},
+ input_symbols={'0', '1'},
+ transitions={
+ 'q0': {'0': 'q0', '1': 'q1'},
+ 'q1': {'0': 'q0', '1': 'q2'},
+ 'q2': {'0': 'q2', '1': 'q1'}
+ },
+ initial_state='q0',
+ final_states={'q1'}
+)
+
+try:
+ while True:
+ if my_dfa.accepts_input(input('Please enter your input: ')):
+ print('Accepted')
+ else:
+ print('Rejected')
+except KeyboardInterrupt:
+ print('')
+
The NFA
does not have a built-in method for checking whether it is a subset
+of another NFA
. However, this can be done using existing methods.
# In the following, we have nfa1 and nfa2 and want to determine whether
+# nfa1 is a subset of nfa2.
+
+# If taking the union of nfa2 with nfa1 is equal to nfa2 again,
+# nfa1 didn't accept any strings that nfa2 did not, so it is a subset.
+if (nfa1 | nfa2) == nfa2:
+ print('nfa1 is a subset of nfa2.')
+else:
+ print('nfa1 is not a subset of nfa2.')
+
The following example is inspired by this blog post. +Essentially, we want to determine which strings in a given set are within +the target edit distance to a reference string.
+from automata.fa.dfa import DFA
+from automata.fa.nfa import NFA
+import string
+
+input_symbols = set(string.ascii_lowercase)
+
+# Construct DFA recognizing target words
+target_words = {'these', 'are', 'target', 'words', 'them', 'those'}
+
+target_words_dfa = DFA.from_finite_language(
+ input_symbols,
+ target_words,
+)
+
+# Next, construct NFA recognizing all strings
+# within given edit distance of target word
+reference_string = 'they'
+edit_distance = 2
+
+words_within_edit_distance_dfa = DFA.from_nfa(
+ NFA.edit_distance(
+ input_symbols,
+ reference_string,
+ edit_distance,
+ )
+)
+
+# Take intersection and print results
+found_words_dfa = target_words_dfa & words_within_edit_distance_dfa
+found_words = list(found_words_dfa)
+
+print(
+ f"All words within edit distance {edit_distance} of "
+ f"'{reference_string}': {found_words}"
+)
+
The example below is adapted from the
+visual automata library.
+This function takes in a DFA
or NFA
and returns the
+corresponding transition table.
The start state is prefixed with →
and final states are prefixed
+with *
.
import pandas as pd
+
+def make_table(target_fa) -> pd.DataFrame:
+ initial_state = target_fa.initial_state
+ final_states = target_fa.final_states
+
+ table = {}
+
+ for from_state, to_state, symbol in target_fa.iter_transitions():
+ # Prepare nice string for from_state
+ if isinstance(from_state, frozenset):
+ from_state_str = str(set(from_state))
+ else:
+ from_state_str = str(from_state)
+
+ if from_state in final_states:
+ from_state_str = "*" + from_state_str
+ if from_state == initial_state:
+ from_state_str = "→" + from_state_str
+
+ # Prepare nice string for to_state
+ if isinstance(to_state, frozenset):
+ to_state_str = str(set(to_state))
+ else:
+ to_state_str = str(to_state)
+
+ if to_state in final_states:
+ to_state_str = "*" + to_state_str
+
+ # Prepare nice symbol
+ if symbol == "":
+ symbol = "λ"
+
+ from_state_dict = table.setdefault(from_state_str, dict())
+ from_state_dict.setdefault(symbol, set()).add(to_state_str)
+
+ # Reformat table for singleton sets
+ for symbol_dict in table.values():
+ for symbol in symbol_dict:
+ if len(symbol_dict[symbol]) == 1:
+ symbol_dict[symbol] = symbol_dict[symbol].pop()
+
+
+ df = pd.DataFrame.from_dict(table).fillna("∅").T
+ return df.reindex(sorted(df.columns), axis=1)
+
Copyright 2016-2024 Caleb Evans
+Released under the MIT license
Automata is a Python 3 library implementing structures and algorithms for manipulating finite automata, +pushdown automata, and Turing machines. The algorithms have been optimized and are capable of +processing large inputs. Visualization logic has also been implemented. This package is suitable for +both researchers wishing to manipulate automata and for instructors teaching courses on theoretical +computer science.
+The library requires Python 3.8 or newer.
+You can install the latest version of Automata via pip:
+ +To install the optional visual dependencies, use the visual
extra:
Python 3.7 support has been dropped. Please upgrade to Python 3.8 or later to +use Automata v8.
+Diagrams are no longer being generated using pydot
; this dependency has been
+dropped in favor of using the visual
optional dependency, which will install
+pygraphviz
and coloraide
used for generating figures. You should install
+this optional dependency if you wish to generate figures. This change was to
+allow for native support for displaying finite automaton in Jupyter notebooks.
+The style of the diagrams has been lifted from the visual automata package,
+so you should take a look at the diagrams generated and see if they are still
+satisfactory.
Other new dependencies have been added, but these will be installed automatically +along with v8 of the package.
+There is now greater support for partial DFAs, which included changing the
+DFA.from_nfa()
function to return a partial DFA instead of a complete one.
+To obtain a complete DFA, you must now call DFA.from_nfa().to_complete(trap_state_name)
,
+where trap_state_name
will be used as the name for a trap state if one needs to
+be added.
Type hints have now been added, meaning that code which previously called functions +with incorrect types may not have been flagged. See output from your typechecker +for more information.
+The default set of input symbols for NFA.from_regex
was changed to all ascii letters and digits.
+If needing to use a specific set of input symbols, use the input_symbols
parameter.
All Automaton instances are now fully immutable to protect against common +pitfalls, such as mutating an automaton to an invalid state after it's already +been validated.
+This means that if you wish to make a change to an automaton instance, you must
+retrieve its attributes as a dictionary (using the new input_parameters
+property), make your desired change, then pass those parameters to the relevant
+constructor. For example:
from automata.fa.dfa import DFA
+
+dfa1 = DFA(
+ states={'q0', 'q1', 'q2'},
+ input_symbols={'0', '1'},
+ transitions={
+ 'q0': {'0': 'q0', '1': 'q1'},
+ 'q1': {'0': 'q0', '1': 'q2'},
+ 'q2': {'0': 'q2', '1': 'q1'}
+ },
+ initial_state='q0',
+ final_states={'q1'}
+)
+# You can still copy an automaton just fine
+dfa2 = dfa.copy()
+# If you want to make a change, you must create a new instance; please note
+# that dfa2.input_parameters is always a deep copy of the input parameters for
+# dfa2 (in other words, mutating dfa2.input_parameters will not actually mutate
+# dfa2)
+params = dfa2.input_parameters
+params['final_states'] = {'q2'}
+dfa3 = DFA(**params)
+
The automata.base.regex
module has been renamed to automata.regex.regex
+alongside the other regular expression-related modules.
The default value of the retain_names
parameter for DFA.minify()
has been
+corrected from True
to False
; the API documentation has always stated that
+the default value should be False
, however the default value in the code was
+actually True
; therefore, the code has been updated to match the documentation
+(#59)
+ - Since this code correction may break existing developer code, this is labeled
+ as a backwards-incompatible change rather than just a mere bugfix
Python 3.6 support has been dropped, since it has been end-of-life since +December 2021. Please upgrade to Python 3.7 or later to use Automata v6.
+The networkx package has been added as a required dependency, providing +substantial performance improvements for certain DFA/NFA methods, and also +streamlining the code to improve maintainability.
+Python 3.5 support has been dropped, since it has been end-of-life since +September 2020. Please upgrade to Python 3.6 or later to use Automata v5.
+To support the new graph visualization capabilities, pydot
has been added as a
+project dependency. The pydot
package will be installed when you run pip install automata-lib
.
The only backwards-incompatible change from v3 to v4 is that support for Python +3.4 has been dropped. This is because Python 3.4 has reached end-of-life, and +will no longer receive updates. For more information, please refer to the +Python 3.4.10 release notes.
+There have been no API changes from v3 to v4.
+There have been a number of backwards-incompatible changes from Automata v2 to +v3 to support the new features, including:
+The PDAStack
type is now immutable and hashable; it still represents the
+current stack of a PDA.
Likewise, the TMTape
is now immutable and hashable; it still represents the
+tape of a TM and the current cursor position.
The copy
methods on TMTape
and PDAStack
have been removed, since they are
+now immutable types. This change is similar to how list
has a copy()
method
+but tuple
does not.
DPDA
and NPDA
have a new config option which specifies when to accept. This
+can be either 'empty_stack'
, 'final_state'
or 'both'
. The default is
+'both'
.
There have been a number of backwards-incompatible changes from Automata v1 to +v2 to clean up the API, including:
+The following methods and classes have been renamed for better clarity:
+The automata.shared
package has been renamed to automata.base
.
Before:
+
from automata.shared.automaton import Automaton
+from automata.shared.exceptions import FinalStateError
+
After:
+
from automata.base.automaton import Automaton
+from automata.base.exceptions import FinalStateError
+
The validate_input()
method has been renamed to read_input()
. The
+validate_input(step=True)
form has also been converted to the standalone
+method read_input_stepwise()
.
Before:
+
After:
+
The validate_self()
method has been renamed to validate()
.
Before:
+
After:
+
The top-level *Error
exception classes has been renamed to *Exception
.
Before:
+
from automata.shared.exceptions import AutomatonError
+from automata.shared.exceptions import RejectionError
+from automata.pda.exceptions import PDAError
+from automata.tm.exceptions import TMError
+
After:
+
from automata.base.exceptions import AutomatonException
+from automata.base.exceptions import RejectionException
+from automata.pda.exceptions import PDAException
+from automata.tm.exceptions import TMException
+
In v1, you could copy an automaton (or convert it to another type) by passing it +into the constructor for an Automaton subtype.
+Before:
+
After:
+
Before:
+
After:
+
Before:
+
After:
+
Copyright 2016-2024 Caleb Evans Released under the MIT license
Automata is a Python 3 library implementing structures and algorithms for manipulating finite automata, pushdown automata, and Turing machines. The algorithms have been optimized and are capable of processing large inputs. Visualization logic has also been implemented. This package is suitable for both researchers wishing to manipulate automata and for instructors teaching courses on theoretical computer science.
The library requires Python 3.8 or newer.
"},{"location":"#installing","title":"Installing","text":"You can install the latest version of Automata via pip:
pip install automata-lib\n
To install the optional visual dependencies, use the visual
extra:
pip install 'automata-lib[visual]'\n
"},{"location":"characteristics/","title":"Automaton Characteristics","text":""},{"location":"characteristics/#automaton-instances-are-immutable","title":"Automaton instances are immutable","text":"All Automaton instances are fully immutable to protect against common pitfalls, such as mutating an automaton to an invalid state after it's already been validated.
This means that if you wish to make a change to an automaton instance, you must retrieve its attributes as a dictionary (using the input_parameters
property), make your desired change, then pass those parameters to the relevant constructor. For example:
from automata.fa.dfa import DFA\ndfa1 = DFA(\nstates={'q0', 'q1', 'q2'},\ninput_symbols={'0', '1'},\ntransitions={\n'q0': {'0': 'q0', '1': 'q1'},\n'q1': {'0': 'q0', '1': 'q2'},\n'q2': {'0': 'q2', '1': 'q1'}\n},\ninitial_state='q0',\nfinal_states={'q1'}\n)\n# If you want to make a change, you must create a new instance; please note\n# that dfa1.input_parameters is always a deep copy of the input parameters for\n# dfa1 (in other words, mutating dfa1.input_parameters will not actually mutate\n# dfa1)\nparams = dfa1.input_parameters\nparams['final_states'] = {'q2'}\ndfa2 = DFA(**params)\n
"},{"location":"characteristics/#enabling-mutable-automata","title":"Enabling mutable automata","text":"Automaton immutability is enforced via a \"freeze\" step during object initialization that turns mutable parameters (such as sets or dicts) into their immutable counterparts (frozensets/frozendicts).
If your application requires maximum performance, you can disable this conversion via the allow_mutable_automata
global configuration option. If enabled, the user must ensure that their automaton instances are never modified, otherwise correct behavior cannot be guaranteed.
import automata.base.config as global_config\nglobal_config.allow_mutable_automata = True\n# The rest of your code...\n
"},{"location":"characteristics/#automaton-instances-are-validated-by-default","title":"Automaton instances are validated by default","text":"By default, all Automaton instances are checked for common inconsistencies when they are instantiated. If inconsistencies are found, the appropriate exception from automata.base.exceptions
is raised.
Because this validation can be performance-intensive for large automaton instances with many states/transitions, you can disable the automatic validation using the global configuration feature (introduced in v7):
import automata.base.config as global_config\nglobal_config.should_validate_automata = False\n# The rest of your code...\n
If, at any point, you wish to opt into validation for a specific Automaton instance, you can call the validate
method:
my_automaton.validate()\n
"},{"location":"migration/","title":"Automata Migration Guide","text":""},{"location":"migration/#backwards-incompatible-changes-from-v7-to-v8","title":"Backwards-incompatible changes from v7 to v8","text":""},{"location":"migration/#dependency-changes","title":"Dependency Changes","text":"Python 3.7 support has been dropped. Please upgrade to Python 3.8 or later to use Automata v8.
Diagrams are no longer being generated using pydot
; this dependency has been dropped in favor of using the visual
optional dependency, which will install pygraphviz
and coloraide
used for generating figures. You should install this optional dependency if you wish to generate figures. This change was to allow for native support for displaying finite automaton in Jupyter notebooks. The style of the diagrams has been lifted from the visual automata package, so you should take a look at the diagrams generated and see if they are still satisfactory.
Other new dependencies have been added, but these will be installed automatically along with v8 of the package.
"},{"location":"migration/#greater-support-for-partial-dfas","title":"Greater Support for Partial DFAs","text":"There is now greater support for partial DFAs, which included changing the DFA.from_nfa()
function to return a partial DFA instead of a complete one. To obtain a complete DFA, you must now call DFA.from_nfa().to_complete(trap_state_name)
, where trap_state_name
will be used as the name for a trap state if one needs to be added.
Type hints have now been added, meaning that code which previously called functions with incorrect types may not have been flagged. See output from your typechecker for more information.
"},{"location":"migration/#nfafrom_regex-default-input-symbols","title":"NFA.from_regex default input symbols","text":"The default set of input symbols for NFA.from_regex
was changed to all ascii letters and digits. If needing to use a specific set of input symbols, use the input_symbols
parameter.
All Automaton instances are now fully immutable to protect against common pitfalls, such as mutating an automaton to an invalid state after it's already been validated.
This means that if you wish to make a change to an automaton instance, you must retrieve its attributes as a dictionary (using the new input_parameters
property), make your desired change, then pass those parameters to the relevant constructor. For example:
from automata.fa.dfa import DFA\ndfa1 = DFA(\nstates={'q0', 'q1', 'q2'},\ninput_symbols={'0', '1'},\ntransitions={\n'q0': {'0': 'q0', '1': 'q1'},\n'q1': {'0': 'q0', '1': 'q2'},\n'q2': {'0': 'q2', '1': 'q1'}\n},\ninitial_state='q0',\nfinal_states={'q1'}\n)\n# You can still copy an automaton just fine\ndfa2 = dfa.copy()\n# If you want to make a change, you must create a new instance; please note\n# that dfa2.input_parameters is always a deep copy of the input parameters for\n# dfa2 (in other words, mutating dfa2.input_parameters will not actually mutate\n# dfa2)\nparams = dfa2.input_parameters\nparams['final_states'] = {'q2'}\ndfa3 = DFA(**params)\n
"},{"location":"migration/#renamed-regex-module","title":"Renamed Regex Module","text":"The automata.base.regex
module has been renamed to automata.regex.regex
alongside the other regular expression-related modules.
The default value of the retain_names
parameter for DFA.minify()
has been corrected from True
to False
; the API documentation has always stated that the default value should be False
, however the default value in the code was actually True
; therefore, the code has been updated to match the documentation (#59) - Since this code correction may break existing developer code, this is labeled as a backwards-incompatible change rather than just a mere bugfix
Python 3.6 support has been dropped, since it has been end-of-life since December 2021. Please upgrade to Python 3.7 or later to use Automata v6.
The networkx package has been added as a required dependency, providing substantial performance improvements for certain DFA/NFA methods, and also streamlining the code to improve maintainability.
"},{"location":"migration/#backwards-incompatible-changes-from-v4-to-v5","title":"Backwards-incompatible changes from v4 to v5","text":"Python 3.5 support has been dropped, since it has been end-of-life since September 2020. Please upgrade to Python 3.6 or later to use Automata v5.
To support the new graph visualization capabilities, pydot
has been added as a project dependency. The pydot
package will be installed when you run pip install automata-lib
.
The only backwards-incompatible change from v3 to v4 is that support for Python 3.4 has been dropped. This is because Python 3.4 has reached end-of-life, and will no longer receive updates. For more information, please refer to the Python 3.4.10 release notes.
There have been no API changes from v3 to v4.
"},{"location":"migration/#backwards-incompatible-changes-from-v2-to-v3","title":"Backwards-incompatible changes from v2 to v3","text":"There have been a number of backwards-incompatible changes from Automata v2 to v3 to support the new features, including:
"},{"location":"migration/#some-types-made-immutable","title":"Some types made immutable","text":"The PDAStack
type is now immutable and hashable; it still represents the current stack of a PDA.
Likewise, the TMTape
is now immutable and hashable; it still represents the tape of a TM and the current cursor position.
The copy
methods on TMTape
and PDAStack
have been removed, since they are now immutable types. This change is similar to how list
has a copy()
method but tuple
does not.
DPDA
and NPDA
have a new config option which specifies when to accept. This can be either 'empty_stack'
, 'final_state'
or 'both'
. The default is 'both'
.
There have been a number of backwards-incompatible changes from Automata v1 to v2 to clean up the API, including:
"},{"location":"migration/#renames","title":"Renames","text":"The following methods and classes have been renamed for better clarity:
"},{"location":"migration/#shared-module","title":"Shared module","text":"The automata.shared
package has been renamed to automata.base
.
Before:
from automata.shared.automaton import Automaton\nfrom automata.shared.exceptions import FinalStateError\n
After:
from automata.base.automaton import Automaton\nfrom automata.base.exceptions import FinalStateError\n
"},{"location":"migration/#input-validation-methods","title":"Input validation methods","text":"The validate_input()
method has been renamed to read_input()
. The validate_input(step=True)
form has also been converted to the standalone method read_input_stepwise()
.
Before:
final_state = dfa.validate_input('0011')\nsteps = dfa.validate_input('0011', step=True)\n
After:
final_state = dfa.read_input('0011')\nsteps = dfa.read_input_stepwise('0011')\n
"},{"location":"migration/#automaton-validation-methods","title":"Automaton validation methods","text":"The validate_self()
method has been renamed to validate()
.
Before:
dfa.validate_self()\n
After:
dfa.validate()\n
"},{"location":"migration/#exceptions","title":"Exceptions","text":"The top-level *Error
exception classes has been renamed to *Exception
.
Before:
from automata.shared.exceptions import AutomatonError\nfrom automata.shared.exceptions import RejectionError\nfrom automata.pda.exceptions import PDAError\nfrom automata.tm.exceptions import TMError\n
After:
from automata.base.exceptions import AutomatonException\nfrom automata.base.exceptions import RejectionException\nfrom automata.pda.exceptions import PDAException\nfrom automata.tm.exceptions import TMException\n
"},{"location":"migration/#constructor-polymorphism-removed","title":"Constructor polymorphism removed","text":"In v1, you could copy an automaton (or convert it to another type) by passing it into the constructor for an Automaton subtype.
"},{"location":"migration/#copying-an-automaton","title":"Copying an automaton","text":"Before:
dfa = DFA(dfa)\n
After:
dfa = dfa.copy()\n
"},{"location":"migration/#converting-nfa-to-dfa","title":"Converting NFA to DFA","text":"Before:
dfa = DFA(nfa)\n
After:
dfa = DFA.from_nfa(nfa)\n
"},{"location":"migration/#converting-dfa-to-nfa","title":"Converting DFA to NFA","text":"Before:
nfa = NFA(dfa)\n
After:
nfa = NFA.from_dfa(dfa)\n
"},{"location":"people/","title":"People","text":""},{"location":"people/#maintainers","title":"Maintainers","text":"This is the API documentation generated by the class and method docstrings for the automata
package.
Exception classes shared by all automata.
"},{"location":"api/base-exception-classes/#automata.base.exceptions.AutomatonException","title":"AutomatonException
","text":" Bases: Exception
The base class for all automaton-related errors.
"},{"location":"api/base-exception-classes/#automata.base.exceptions.DiagramException","title":"DiagramException
","text":" Bases: AutomatonException
The diagram cannot be produced
"},{"location":"api/base-exception-classes/#automata.base.exceptions.EmptyLanguageException","title":"EmptyLanguageException
","text":" Bases: AutomatonException
The operation cannot be performed because the language is empty
"},{"location":"api/base-exception-classes/#automata.base.exceptions.FinalStateError","title":"FinalStateError
","text":" Bases: AutomatonException
A final state fails to meet some required condition.
"},{"location":"api/base-exception-classes/#automata.base.exceptions.InfiniteLanguageException","title":"InfiniteLanguageException
","text":" Bases: AutomatonException
The operation cannot be performed because the language is infinite
"},{"location":"api/base-exception-classes/#automata.base.exceptions.InitialStateError","title":"InitialStateError
","text":" Bases: AutomatonException
The initial state fails to meet some required condition.
"},{"location":"api/base-exception-classes/#automata.base.exceptions.InvalidRegexError","title":"InvalidRegexError
","text":" Bases: RegexException
Regular expression is invalid
"},{"location":"api/base-exception-classes/#automata.base.exceptions.InvalidStateError","title":"InvalidStateError
","text":" Bases: AutomatonException
A state is not a valid state for this automaton.
"},{"location":"api/base-exception-classes/#automata.base.exceptions.InvalidSymbolError","title":"InvalidSymbolError
","text":" Bases: AutomatonException
A symbol is not a valid symbol for this automaton.
"},{"location":"api/base-exception-classes/#automata.base.exceptions.LexerError","title":"LexerError
dataclass
","text":" Bases: RegexException
An exception raised for issues in lexing
"},{"location":"api/base-exception-classes/#automata.base.exceptions.MissingStateError","title":"MissingStateError
","text":" Bases: AutomatonException
A state is missing from the automaton definition.
"},{"location":"api/base-exception-classes/#automata.base.exceptions.MissingSymbolError","title":"MissingSymbolError
","text":" Bases: AutomatonException
A symbol is missing from the automaton definition.
"},{"location":"api/base-exception-classes/#automata.base.exceptions.RegexException","title":"RegexException
","text":" Bases: Exception
The base class for all regular expression related errors
"},{"location":"api/base-exception-classes/#automata.base.exceptions.RejectionException","title":"RejectionException
","text":" Bases: AutomatonException
The input was rejected by the automaton.
"},{"location":"api/base-exception-classes/#automata.base.exceptions.SymbolMismatchError","title":"SymbolMismatchError
","text":" Bases: AutomatonException
The input symbols between the given automata do not match
"},{"location":"api/class-automaton/","title":"class Automaton(metaclass=ABCMeta)","text":"An abstract base class for all automata, including Turing machines.
"},{"location":"api/class-automaton/#automata.base.automaton.Automaton.input_parameters","title":"input_parameters: Dict[str, Any]
property
","text":"Return the public attributes for this automaton.
Returns:
Type DescriptionDict[str, Any]
A dictionary mapping attribute names to their values.
"},{"location":"api/class-automaton/#automata.base.automaton.Automaton.accepts_input","title":"accepts_input(input_str)
","text":"Return True if this automaton accepts the given input.
Parameters:
Name Type Description Defaultinput_str
str
The input string to check.
requiredReturns:
Type Descriptionbool
True if this automaton accepts the given input; False otherwise.
"},{"location":"api/class-automaton/#automata.base.automaton.Automaton.copy","title":"copy()
","text":"Create a deep copy of the automaton.
Returns:
Type DescriptionSelf
A deep copy of the automaton.
"},{"location":"api/class-automaton/#automata.base.automaton.Automaton.read_input","title":"read_input(input_str)
","text":"Check if the given string is accepted by this automaton.
Return the automaton's final configuration if this string is valid.
Parameters:
Name Type Description Defaultinput_str
str
The input string to check.
requiredReturns:
Type DescriptionAutomatonStateT
The final configuration of the automaton after reading the input.
"},{"location":"api/class-automaton/#automata.base.automaton.Automaton.read_input_stepwise","title":"read_input_stepwise(input_str)
abstractmethod
","text":"Return a generator that yields each step while reading input.
Parameters:
Name Type Description Defaultinput_str
str
The input string to read.
requiredYields:
Type DescriptionGenerator[Any, None, None]
A generator that yields the current configuration of the automaton after each step of reading input.
Raises:
Type DescriptionNotImplementedError
If this method has not been implemented by a subclass.
"},{"location":"api/class-automaton/#automata.base.automaton.Automaton.validate","title":"validate()
abstractmethod
","text":"Raises an exception if this automaton is not internally consistent.
Raises:
Type DescriptionNotImplementedError
If this method has not been implemented by a subclass.
"},{"location":"api/regular-expressions/","title":"Regular Expressions","text":"A set of tools for working with regular expressions. Can recognize regular expressions over the alphabet of ascii letters (lower and upper case), ascii digits, and subsets of these.
A regular expression with the following operations only are supported in this library:
*
: Kleene star operation, language repeated zero or more times. Ex: a*
,(ab)*
+
: Kleene plus operation, language repeated one or more times. Ex: a+
,(ab)+
?
: Language repeated zero or one time. Ex: a?
abcd
|
: Union. Ex: a|b
&
: Intersection. Ex: a&b
.
: Wildcard. Ex: a.b
^
: Shuffle. Ex: a^b
{}
: Quantifiers expressing finite repetitions. Ex: a{1,2}
,a{3,}
()
: The empty string.(...)
: Grouping.This is similar to the Python re
module, but this library does not support any special characters other than those given above. All regular languages can be written with these.
isequal(re1, re2, *, input_symbols=None)
","text":"Whether both regular expressions are equivalent.
Parameters:
Name Type Description Defaultre1
str
The first regular expression as a string.
requiredre2
str
The second regular expression as a string.
requiredinput_symbols
Optional[AbstractSet[str]]
The set of input symbols when doing the comparison. Defaults to all ascii letters and digits.
None
Returns:
Type Descriptionbool
Whether the regular expressions are equivalent.
"},{"location":"api/regular-expressions/#automata.regex.regex.issubset","title":"issubset(re1, re2, *, input_symbols=None)
","text":"Whether re1 is a subset of re2.
Parameters:
Name Type Description Defaultre1
str
The first regular expression as a string.
requiredre2
str
The second regular expression as a string.
requiredinput_symbols
Optional[AbstractSet[str]]
The set of input symbols when doing the comparison. Defaults to all ascii letters and digits.
None
Returns:
Type Descriptionbool
True if re1 is a subset of re2.
"},{"location":"api/regular-expressions/#automata.regex.regex.issuperset","title":"issuperset(re1, re2, *, input_symbols=None)
","text":"Whether re1 is a superset of re2.
Parameters:
Name Type Description Defaultre1
str
The first regular expression as a string.
requiredre2
str
The second regular expression as a string.
requiredinput_symbols
Optional[AbstractSet[str]]
The set of input symbols when doing the comparison. Defaults to all ascii letters and digits.
None
Returns:
Type Descriptionbool
True if re1 is a superset of re2.
"},{"location":"api/regular-expressions/#automata.regex.regex.validate","title":"validate(regex)
","text":"Raises an exception if the input regular expression is invalid.
Raises:
Type DescriptionInvalidRegexError
Raised if the regex given as input is not well defined.
"},{"location":"api/fa/class-dfa/","title":"class DFA(FA)","text":"Classes and methods for working with deterministic finite automata.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA","title":"DFA
","text":" Bases: FA
The DFA
class is a subclass of FA
and represents a deterministic finite automaton.
Every DFA can be rendered natively inside of a Jupyter notebook (automatically calling show_diagram
without any arguments) if installed with the visual
optional dependency.
Parameters:
Name Type Description Defaultstates
AbstractSet[DFAStateT]
Set of the DFA's valid states.
requiredinput_symbols
AbstractSet[str]
Set of the DFA's valid input symbols, each of which is a singleton string
requiredtransitions
Mapping[DFAStateT, Mapping[str, DFAStateT]]
Dict consisting of the transitions for each state. Each key is a state name, and each value is another dict which maps a symbol (the key) to a state (the value).
requiredinitial_state
DFAStateT
The initial state for this DFA.
requiredfinal_states
AbstractSet[DFAStateT]
A set of final states for this DFA
requiredallow_partial
bool
By default, each DFA state must have a transition to every input symbol; if this parameter is True
, you can disable this characteristic (such that any DFA state can have fewer transitions than input symbols). Note that a DFA must always have every state represented in the transition dictionary, even if there are no transitions on input symbols leaving a state (dictionary is left empty in that case).
True
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.cardinality","title":"cardinality()
","text":"Returns the cardinality of the language represented by the DFA.
Returns:
Type Descriptionint
The cardinality of the language accepted by self.
Raises:
Type DescriptionInfiniteLanguageException
Raised if self accepts an infinite language.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.clear_cache","title":"clear_cache()
","text":"Resets the word and count caches. Can be called if too much memory is being used.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.complement","title":"complement(*, retain_names=False, minify=True)
","text":"Creates a DFA which accepts an input if and only if the old one does not. Minifies by default. Unreachable states are always removed. Partial DFAs are converted into complete ones.
Parameters:
Name Type Description Defaultretain_names
bool
Whether to retain state names through the complement and optional minify.
False
minify
bool
Whether to minify the result of the complement of the input DFA.
True
Returns:
Type DescriptionSelf
A DFA accepting the complement of the input DFA. State minimal by default.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.count_mod","title":"count_mod(input_symbols, k, *, remainders=None, symbols_to_count=None)
classmethod
","text":"Directly computes a DFA that counts given symbols and accepts all strings where the remainder of division by k is in the set of remainders given. The default value of remainders is {0} and all symbols are counted by default.
Parameters:
Name Type Description Defaultinput_symbols
AbstractSet[str]
The set of input symbols to construct the DFA over.
requiredk
int
The number to divide the length by.
requiredremainders
Optional[AbstractSet[int]]
The remainders to accept. If set to None, defaults to {0}.
None
symbols_to_count
Optional[AbstractSet[str]]
The input symbols to count towards the length of words to accepts. If set to None, counts all symbols.
None
Returns:
Type DescriptionSelf
The DFA accepting the desired language.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.count_words_of_length","title":"count_words_of_length(k)
","text":"Returns count of words of length k accepted by the DFA.
Parameters:
Name Type Description Defaultk
int
The desired word length.
requiredReturns:
Type Descriptionint
The number of words of length k accepted by self.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.difference","title":"difference(other, *, retain_names=False, minify=True)
","text":"Takes as input two DFAs M1 and M2 which accept languages L1 and L2 respectively. Returns a DFA which accepts the difference of L1 and L2.
Minifies by default. Unreachable states are always removed. If either input DFA is partial, the result is partial.
Parameters:
Name Type Description Defaultother
DFA
The DFA we want to take a difference with.
requiredretain_names
bool
Whether to retain state names through the difference and optional minify.
False
minify
bool
Whether to minify the result of the difference of the two DFAs.
True
Returns:
Type DescriptionSelf
A DFA accepting the difference of the two input DFAs. State minimal by default.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.empty_language","title":"empty_language(input_symbols)
classmethod
","text":"Directly computes the minimal DFA rejecting all strings.
Parameters:
Name Type Description Defaultinput_symbols
AbstractSet[str]
The set of input symbols to construct the DFA over.
requiredReturns:
Type DescriptionSelf
The DFA accepting the desired language.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.from_finite_language","title":"from_finite_language(input_symbols, language, as_partial=True)
classmethod
","text":"Directly computes the minimal DFA accepting the finite language given as input. Uses the algorithm described in Finite-State Techniques by Mihov and Schulz, Chapter 10
Parameters:
Name Type Description Defaultinput_symbols
AbstractSet[str]
The set of input symbols to construct the DFA over.
requiredlanguage
AbstractSet[str]
The language to accept.
requiredas_partial
bool
Whether or not to construct this as a partial DFA.
True
Returns:
Type DescriptionSelf
The DFA accepting the desired language.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.from_nfa","title":"from_nfa(target_nfa, *, retain_names=False, minify=True)
classmethod
","text":"Initialize this DFA as one equivalent to the given NFA. Note that this usually returns a partial DFA by default.
Parameters:
Name Type Description Defaulttarget_nfa
NFA
The NFA to construct an equivalent DFA for.
requiredretain_names
bool
Whether or not to retain state names during processing.
False
minify
bool
Whether or not to minify the DFA resulting from the input NFA.
True
Returns:
Type DescriptionSelf
The DFA accepting the language of the input NFA.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.from_prefix","title":"from_prefix(input_symbols, prefix, *, contains=True, as_partial=True)
classmethod
","text":"Directly computes the minimal DFA accepting strings with the given prefix. If contains
is set to False
then the complement is constructed instead.
Parameters:
Name Type Description Defaultinput_symbols
AbstractSet[str]
The set of input symbols to construct the DFA over.
requiredprefix
str
The prefix of strings that are accepted by this DFA.
requiredcontains
bool
Whether or not to construct the compliment DFA.
True
as_partial
bool
Whether or not to construct this DFA as a partial DFA.
True
Returns:
Type DescriptionSelf
The DFA accepting the desired language.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.from_subsequence","title":"from_subsequence(input_symbols, subsequence, *, contains=True)
classmethod
","text":"Directly computes the minimal DFA recognizing strings containing the given subsequence. If contains
is set to False
, then the complement is constructed instead.
Parameters:
Name Type Description Defaultinput_symbols
AbstractSet[str]
The set of input symbols to construct the DFA over.
requiredsubsequence
str
The target subsequence of strings that are accepted by this DFA.
requiredcontains
bool
Whether or to construct the compliment DFA.
True
Returns:
Type DescriptionSelf
The DFA accepting the desired language.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.from_substring","title":"from_substring(input_symbols, substring, *, contains=True, must_be_suffix=False)
classmethod
","text":"Directly computes the minimal DFA recognizing strings containing the given substring. If contains
is set to False
then the complement is constructed instead. If must_be_suffix
is set to True
, then the substring must be a suffix instead.
Parameters:
Name Type Description Defaultinput_symbols
AbstractSet[str]
The set of input symbols to construct the DFA over.
requiredsubstring
str
The substring of strings that are accepted by this DFA.
requiredcontains
bool
Whether or to construct the compliment DFA.
True
must_be_suffix
bool
Whether or not the target substring must be a suffix.
False
Returns:
Type DescriptionSelf
The DFA accepting the desired language.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.from_suffix","title":"from_suffix(input_symbols, suffix, *, contains=True)
classmethod
","text":"Directly computes the minimal DFA recognizing strings with the given suffix. If contains
is set to False
, then the complement is constructed instead.
Parameters:
Name Type Description Defaultinput_symbols
AbstractSet[str]
The set of input symbols to construct the DFA over.
requiredsuffix
str
The suffix of strings that are accepted by this DFA.
requiredcontains
bool
Whether or not to construct the compliment DFA.
True
Returns:
Type DescriptionSelf
The DFA accepting the desired language.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.intersection","title":"intersection(other, *, retain_names=False, minify=True)
","text":"Takes as input two DFAs M1 and M2 which accept languages L1 and L2 respectively. Returns a DFA which accepts the intersection of L1 and L2.
Minifies by default. Unreachable states are always removed. If either input DFA is partial, the result is partial.
Parameters:
Name Type Description Defaultother
DFA
The DFA we want to take a intersection with.
requiredretain_names
bool
Whether to retain state names through the intersection and optional minify.
False
minify
bool
Whether to minify the result of the intersection of the two DFAs.
True
Returns:
Type DescriptionSelf
A DFA accepting the intersection of the two input DFAs. State minimal by default.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.isdisjoint","title":"isdisjoint(other)
","text":"Returns True if the language accepted by self is disjoint from that of other.
Parameters:
Name Type Description Defaultother
DFA
The other DFA we are comparing our language against.
requiredReturns:
Type Descriptionbool
True if self is disjoint from other, False otherwise.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.isempty","title":"isempty()
","text":"Returns True if the language accepted by self is empty.
Returns:
Type Descriptionbool
True if self accepts the empty language, False otherwise.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.isfinite","title":"isfinite()
","text":"Returns True if the language accepted by self is finite.
Returns:
Type Descriptionbool
True if self accepts a finite language, False otherwise.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.issubset","title":"issubset(other)
","text":"Returns True if the language accepted by self is a subset of that of other.
Parameters:
Name Type Description Defaultother
DFA
The other DFA we are comparing our language against.
requiredReturns:
Type Descriptionbool
True if self is a subset of other, False otherwise.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.issuperset","title":"issuperset(other)
","text":"Returns True if the language accepted by self is a superset of that of other.
Parameters:
Name Type Description Defaultother
DFA
The other DFA we are comparing our language against.
requiredReturns:
Type Descriptionbool
True if self is a superset of other, False otherwise.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.iter_transitions","title":"iter_transitions()
","text":"Iterate over all transitions in the DFA. Each transition is a tuple of the form (from_state, to_state, symbol).
Returns:
Type DescriptionGenerator[Tuple[DFAStateT, DFAStateT, str], None, None]
The desired generator over the DFA transitions.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.maximum_word_length","title":"maximum_word_length()
","text":"Returns the length of the longest word in the language accepted by the DFA In the case of infinite languages, None
is returned.
Returns:
Type DescriptionOptional[int]
The length of the longest word accepted by self. None if the language is infinite.
Raises:
Type DescriptionEmptyLanguageException
Raised if self accepts the empty language.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.minify","title":"minify(retain_names=False)
","text":"Create a minimal DFA which accepts the same inputs as this DFA.
First, non-reachable states are removed. Then, indistinguishable states are merged using Hopcroft's Algorithm.
Parameters:
Name Type Description Defaultretain_names
bool
Whether to retain original names when merging states. New names are from 0 to n-1.
False
Returns:
Type DescriptionSelf
A state-minimal equivalent DFA. May be complete in some cases if the input is partial.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.minimum_word_length","title":"minimum_word_length()
","text":"Returns the length of the shortest word in the language accepted by the DFA.
Returns:
Type Descriptionint
The length of the shortest word accepted by self.
Raises:
Type DescriptionEmptyLanguageException
Raised if self accepts an empty language.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.nth_from_end","title":"nth_from_end(input_symbols, symbol, n)
classmethod
","text":"Directly computes the minimal DFA which accepts all words whose n
th character from the end is symbol
, where n
is a positive integer.
Parameters:
Name Type Description Defaultinput_symbols
AbstractSet[str]
The set of input symbols to construct the DFA over.
requiredsymbol
str
The target input symbol.
requiredn
int
The position of the target input symbol.
requiredReturns:
Type DescriptionSelf
The DFA accepting the desired language.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.nth_from_start","title":"nth_from_start(input_symbols, symbol, n)
classmethod
","text":"Directly computes the minimal DFA which accepts all words whose n
th character from the start is symbol
, where n
is a positive integer.
Parameters:
Name Type Description Defaultinput_symbols
AbstractSet[str]
The set of input symbols to construct the DFA over.
requiredsymbol
str
The target input symbol.
requiredn
int
The position of the target input symbol.
requiredReturns:
Type DescriptionSelf
The DFA accepting the desired language.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.of_length","title":"of_length(input_symbols, *, min_length=0, max_length=None, symbols_to_count=None)
classmethod
","text":"Directly computes the minimal DFA recognizing strings whose length is between min_length
and max_length
, inclusive. To allow arbitrarily long words, the value None
can be passed in for max_length
.
Parameters:
Name Type Description Defaultinput_symbols
AbstractSet[str]
The set of input symbols to construct the DFA over.
requiredmin_length
int
The minimum length of strings to be accepted by this DFA.
0
max_length
Optional[int]
The maximum length of strings to be accepted by this DFA. If set to None, there is no maximum.
None
symbols_to_count
Optional[AbstractSet[str]]
The input symbols to count towards the length of words to accepts. If set to None, counts all symbols.
None
Returns:
Type DescriptionSelf
The DFA accepting the desired language.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.predecessor","title":"predecessor(input_str, *, strict=True, key=None)
","text":"Returns the first string accepted by the DFA that comes before the input string in lexicographical order.
Parameters:
Name Type Description Defaultinput_str
str
The starting input string.
requiredstrict
bool
If set to false and input_str is accepted by the DFA, input_str will be returned.
True
key
Optional[Callable]
Function for defining custom lexicographical ordering. Defaults to using the standard string ordering.
None
Returns:
Type Descriptionstr
The first string accepted by the DFA lexicographically before input_string.
Raises:
Type DescriptionInfiniteLanguageException
Raised if the language accepted by self is infinite, as we cannot generate predecessors in this case.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.predecessors","title":"predecessors(input_str, *, strict=True, key=None)
","text":"Generates all strings that come before the input string in lexicographical order.
Parameters:
Name Type Description Defaultinput_str
str
The starting input string.
requiredstrict
bool
If set to false and input_str is accepted by the DFA, input_str will be returned.
True
key
Optional[Callable]
Function for defining custom lexicographical ordering. Defaults to using the standard string ordering.
None
Returns:
Type DescriptionGenerator[str, None, None]
A generator for all strings that come before the input string in lexicographical order.
Raises:
Type DescriptionInfiniteLanguageException
Raised if the language accepted by self is infinite, as we cannot generate predecessors in this case.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.random_word","title":"random_word(k, *, seed=None)
","text":"Returns a random word of length k accepted by self.
Parameters:
Name Type Description Defaultk
int
The length of the desired word.
requiredseed
Optional[int]
The random seed to use for the sampling of the random word.
None
Returns:
Type Descriptionstr
A uniformly random word of length k accepted by the DFA self.
Raises:
Type DescriptionValueError
If this DFA does not accept any words of length k.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.read_input_stepwise","title":"read_input_stepwise(input_str, ignore_rejection=False)
","text":"Return a generator that yields each step while reading input.
Parameters:
Name Type Description Defaultinput_str
str
The input string to read.
requiredignore_rejection
bool
Whether to throw an exception if the input string is rejected.
False
Yields:
Type DescriptionGenerator[DFAStateT, None, None]
A generator that yields the current configuration of the DFA after each step of reading input.
Raises:
Type DescriptionRejectionException
Raised if this DFA does not accept the input string.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.successor","title":"successor(input_str, *, strict=True, key=None)
","text":"Returns the first string accepted by the DFA that comes after the input string in lexicographical order.
Parameters:
Name Type Description Defaultinput_str
Optional[str]
The starting input string. If None, will generate all words.
requiredstrict
bool
If set to false and input_str is accepted by the DFA, input_str will be returned.
True
key
Optional[Callable]
Function for defining custom lexicographical ordering. Defaults to using the standard string ordering.
None
Returns:
Type Descriptionstr
The first string accepted by the DFA lexicographically before input_string.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.successors","title":"successors(input_str, *, strict=True, key=None, reverse=False)
","text":"Generates all strings that come after the input string in lexicographical order.
Parameters:
Name Type Description Defaultinput_str
Optional[str]
The starting input string. If None, will generate all words.
requiredstrict
bool
If set to false and input_str is accepted by the DFA, input_str will be returned.
True
key
Optional[Callable]
Function for defining custom lexicographical ordering. Defaults to using the standard string ordering.
None
reverse
bool
If True, then predecessors will be generated instead of successors.
False
Returns:
Type DescriptionGenerator[str, None, None]
A generator for all strings that come after the input string in lexicographical order.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.symmetric_difference","title":"symmetric_difference(other, *, retain_names=False, minify=True)
","text":"Takes as input two DFAs M1 and M2 which accept languages L1 and L2 respectively. Returns a DFA which accepts the symmetric difference of L1 and L2.
Minifies by default. Unreachable states are always removed. If either input DFA is partial, the result is partial.
Parameters:
Name Type Description Defaultother
DFA
The DFA we want to take a symmetric difference with.
requiredretain_names
bool
Whether to retain state names through the symmetric difference and optional minify.
False
minify
bool
Whether to minify the result of the symmetric difference of the two DFAs.
True
Returns:
Type DescriptionSelf
A DFA accepting the symmetric difference of the two input DFAs. State minimal by default.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.to_complete","title":"to_complete(trap_state=None)
","text":"Creates an equivalent complete DFA with trap_state used as the name for an added trap state. If trap_state is not passed in, defaults to the largest negative integer which is not already a state name. If the DFA is already complete, just returns a copy.
Parameters:
Name Type Description Defaulttrap_state
Optional[DFAStateT]
Name for custom trap state to be used.
None
Returns:
Type DescriptionSelf
An equivalent complete DFA.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.to_partial","title":"to_partial(*, retain_names=False, minify=True)
","text":"Turns a DFA (complete or not) into a partial DFA. Removes dead states and trap states (except the initial state) and all edges leading to them.
Parameters:
Name Type Description Defaultminify
bool
Whether to perform a minify operation while converting to a partial DFA.
True
retain_names
bool
Whether to retain state names during minification.
True
Returns:
Type DescriptionSelf
An equivalent partial DFA.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.union","title":"union(other, *, retain_names=False, minify=True)
","text":"Takes as input two DFAs M1 and M2 which accept languages L1 and L2 respectively. Returns a DFA which accepts the union of L1 and L2.
Minifies by default. Unreachable states are always removed. If either input DFA is partial, the result is partial.
Parameters:
Name Type Description Defaultother
DFA
The DFA we want to take a union with.
requiredretain_names
bool
Whether to retain state names through the union and optional minify.
False
minify
bool
Whether to minify the result of the union of the two DFAs.
True
Returns:
Type DescriptionSelf
A DFA accepting the union of the two input DFAs. State minimal by default.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.universal_language","title":"universal_language(input_symbols)
classmethod
","text":"Directly computes the minimal DFA accepting all strings.
Parameters:
Name Type Description Defaultinput_symbols
AbstractSet[str]
The set of input symbols to construct the DFA over.
requiredReturns:
Type DescriptionSelf
The DFA accepting the desired language.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.validate","title":"validate()
","text":"Raises an exception if this automaton is not internally consistent.
Raises:
Type DescriptionInvalidStateError
If this DFA has invalid states in the transition dictionary.
MissingStateError
If this DFA has states missing from the transition dictionary.
InvalidSymbolError
If this DFA has invalid symbols in the transition dictionary.
MissingSymbolError
If this DFA is missing transitions on certain symbols.
"},{"location":"api/fa/class-dfa/#automata.fa.dfa.DFA.words_of_length","title":"words_of_length(k)
","text":"Generates all words of length k
in the language accepted by the DFA.
Parameters:
Name Type Description Defaultk
int
The desired word length.
requiredReturns:
Type DescriptionGenerator[str, None, None]
A generator for all words of length k accepted by the DFA.
"},{"location":"api/fa/class-fa/","title":"class FA(Automaton, metaclass=ABCMeta)","text":"Classes and methods for working with all finite automata.
"},{"location":"api/fa/class-fa/#automata.fa.fa.FA","title":"FA
","text":" Bases: Automaton
The FA
class is an abstract base class from which all finite automata inherit. Every subclass of FA can be rendered natively inside of a Jupyter notebook (automatically calling show_diagram
without any arguments) if installed with the visual
optional dependency.
iter_transitions()
abstractmethod
","text":"Iterate over all transitions in the automaton. Each transition is a tuple of the form (from_state, to_state, symbol)
"},{"location":"api/fa/class-fa/#automata.fa.fa.FA.show_diagram","title":"show_diagram(input_str=None, path=None, *, layout_method='dot', horizontal=True, reverse_orientation=False, fig_size=None, font_size=14.0, arrow_size=0.85, state_separation=0.5)
","text":"Generates a diagram of the associated automaton.
Parameters:
Name Type Description Defaultinput_str
Optional[str]
String consisting of input symbols. If set, will add processing of the input string to the diagram.
None
path
Union[str, PathLike, None]
Path to output file. If None, the output will not be saved.
None
horizontal
bool
Direction of node layout in the output graph.
True
reverse_orientation
bool
Reverse direction of node layout in the output graph.
False
fig_size
Union[Tuple[float, float], Tuple[float], None]
Figure size.
None
font_size
float
Font size in the output graph.
14.0
arrow_size
float
Arrow size in the output graph.
0.85
state_separation
float
Distance between nodes in the output graph.
0.5
Returns:
Type DescriptionAGraph
A diagram of the given automaton.
"},{"location":"api/fa/class-gnfa/","title":"class GNFA(FA)","text":"Classes and methods for working with generalized non-deterministic finite automata.
"},{"location":"api/fa/class-gnfa/#automata.fa.gnfa.GNFA","title":"GNFA
","text":" Bases: FA
The GNFA
class is a subclass of FA
and represents a generalized nondeterministic finite automaton.
Its main usage is for conversion of DFAs and NFAs to regular expressions. Note that because of this, the GNFA
doesn't support any binary operators or reading input (e.g. read_input_stepwise
). Every GNFA can be rendered natively inside of- a Jupyter notebook (automatically calling show_diagram
without any arguments) if installed with the visual
optional dependency. Note that input_str
cannot be set as an argument to show_diagram
, as the GNFA
does not read input.
Except for initial_state
and final_state
, one transition goes from every state to every other state, and also from each state to itself. To accommodate this, transitions can be regular expressions and None
, in addition to normal input symbols.
Parameters:
Name Type Description Defaultstates
AbstractSet[GNFAStateT]
Set of the GNFA's valid states.
requiredinput_symbols
AbstractSet[str]
Set of the GNFA's valid input symbols, each of which is a singleton string.
requiredtransitions
Mapping[GNFAStateT, Mapping[GNFAStateT, Optional[str]]]
A dict with the transitions for each state, except final_state
. Each key is a state name and each value is dict which maps a state (the key) to the transition expression (the value) or None. The transition expression is a regular expression (string) over input_symbols
, and the following symbols only: *
, |
, ?
, ()
.
This is a subset of the standard regular expressions using this package.
requiredinitial_state
GNFAStateT
The initial state for this GNFA. Has transitions going to every other state, but no transitions coming in from any other state.
requiredfinal_state
GNFAStateT
A single final state for this GNFA. Has transitions coming in from every other state, but no transitions going to any other state. Must be different from the initial_state
.
from_dfa(target_dfa)
classmethod
","text":"Initialize this GNFA as one equivalent to the given DFA.
Parameters:
Name Type Description Defaulttarget_dfa
DFA
The DFA to construct an equivalent GNFA for.
requiredReturns:
Type DescriptionSelf
The GNFA accepting the language of the input DFA.
"},{"location":"api/fa/class-gnfa/#automata.fa.gnfa.GNFA.from_nfa","title":"from_nfa(target_nfa)
classmethod
","text":"Initialize this GNFA as one equivalent to the given NFA.
Parameters:
Name Type Description Defaulttarget_nfa
NFA
The NFA to construct an equivalent GNFA for.
requiredReturns:
Type DescriptionSelf
The GNFA accepting the language of the input NFA.
"},{"location":"api/fa/class-gnfa/#automata.fa.gnfa.GNFA.iter_transitions","title":"iter_transitions()
","text":"Iterate over all transitions in the GNFA. Each transition is a tuple of the form (from_state, to_state, symbol).
Returns:
Type DescriptionGenerator[Tuple[GNFAStateT, GNFAStateT, str], None, None]
The desired generator over the GNFA transitions.
"},{"location":"api/fa/class-gnfa/#automata.fa.gnfa.GNFA.to_regex","title":"to_regex()
","text":"Convert GNFA to regular expression.
Returns:
Type Descriptionstr
A regular expression equivalent to the input GNFA.
"},{"location":"api/fa/class-gnfa/#automata.fa.gnfa.GNFA.validate","title":"validate()
","text":"Raises an exception if this automaton is not internally consistent.
Raises:
Type DescriptionInvalidStateError
If this GNFA has invalid states in the transition dictionary.
MissingStateError
If this GNFA has states missing from the transition dictionary.
InvalidRegexError
If this GNFA has invalid regex in the transition dictionary.
"},{"location":"api/fa/class-nfa/","title":"class NFA(FA)","text":"Classes and methods for working with nondeterministic finite automata.
"},{"location":"api/fa/class-nfa/#automata.fa.nfa.NFA","title":"NFA
","text":" Bases: FA
The NFA
class is a subclass of FA
and represents a nondeterministic finite automaton.
Every NFA has the same five DFA properties: state
, input_symbols
, transitions
, initial_state
, and final_states
. However, the structure of the transitions
object has been modified slightly to accommodate the fact that a single state can have more than one transition for the same symbol. Therefore, instead of mapping a symbol to one end state in each sub-dict, each symbol is mapped to a set of end states.
Every NFA can be rendered natively inside of a Jupyter notebook (automatically calling show_diagram
without any arguments) if installed with the visual
optional dependency.
Parameters:
Name Type Description Defaultstates
AbstractSet[NFAStateT]
Set of the NFA's valid states.
requiredinput_symbols
AbstractSet[str]
Set of the NFA's valid input symbols, each of which is a singleton string.
requiredtransitions
Mapping[NFAStateT, Mapping[str, AbstractSet[NFAStateT]]]
Dict consisting of the transitions for each state. Each key is a state name, and each value is another dict which maps a symbol (the key) to a set of states (the value).
requiredinitial_state
NFAStateT
The initial state for this NFA.
requiredfinal_states
AbstractSet[NFAStateT]
A set of final states for this NFA.
required"},{"location":"api/fa/class-nfa/#automata.fa.nfa.NFA.concatenate","title":"concatenate(other)
","text":"Given two NFAs, M1 and M2, which accept the languages L1 and L2 respectively, returns an NFA which accepts the language L1 concatenated with L2.
Parameters:
Name Type Description Defaultother
NFA
The NFA we want to concatenate with.
requiredReturns:
Type DescriptionSelf
An NFA accepting the language concatenation of the two input NFAs.
"},{"location":"api/fa/class-nfa/#automata.fa.nfa.NFA.edit_distance","title":"edit_distance(input_symbols, reference_str, max_edit_distance, *, insertion=True, deletion=True, substitution=True)
classmethod
","text":"Constructs the Levenshtein NFA for the given reference_str and given Levenshtein distance. This NFA recognizes strings within the given Levenshtein distance (commonly called edit distance) of the reference_str. Parameters control which error types the NFA will recognize (insertions, deletions, or substitutions). At least one error type must be set.
If insertion and deletion are False and substitution is True, then this is the same as Hamming distance.
If insertion and deletion are True and substitution is False, then this is the same as LCS distance.
insertion, deletion, and substitution all default to True.
Code adapted from: http://blog.notdot.net/2010/07/Damn-Cool-Algorithms-Levenshtein-Automata
Parameters:
Name Type Description Defaultinput_symbols
AbstractSet[str]
The set of input symbols to construct the NFA over.
requiredreference_str
str
The reference string the NFA will use to recognize other close strings.
requiredmax_edit_distance
int
The maximum edit distance from the reference string this NFA will recognize. Must be positive.
requiredinsertion
bool
Whether to recognize insertion edits relative to the reference string.
True
deletion
bool
Whether to recognize deletion edits relative to the reference string.
True
substitution
bool
Whether to recognize substitution edits relative to the reference string.
True
Returns:
Type DescriptionSelf
An NFA accepting all strings within the given edit distance to the reference string.
Raises:
Type DescriptionValueError
Raised if the max_edit_distance is negative or all of the error flags are set to False (at least one must be True).
"},{"location":"api/fa/class-nfa/#automata.fa.nfa.NFA.eliminate_lambda","title":"eliminate_lambda()
","text":"Returns an equivalent NFA with lambda transitions removed.
Returns:
Type DescriptionSelf
The equivalent NFA with lambda transitions removed.
"},{"location":"api/fa/class-nfa/#automata.fa.nfa.NFA.from_dfa","title":"from_dfa(target_dfa)
classmethod
","text":"Initialize this NFA as one equivalent to the given DFA.
Parameters:
Name Type Description Defaulttarget_dfa
DFA
The DFA to construct an equivalent NFA for.
requiredReturns:
Type DescriptionSelf
The NFA accepting the language of the input DFA.
"},{"location":"api/fa/class-nfa/#automata.fa.nfa.NFA.from_regex","title":"from_regex(regex, *, input_symbols=None)
classmethod
","text":"Initialize this NFA as one equivalent to the given regular expression.
Parameters:
Name Type Description Defaultregex
str
The regex to construct an equivalent NFA for.
requiredinput_symbols
Optional[AbstractSet[str]]
The set of input symbols to create the NFA over. If not set, defaults to all ascii letters and digits.
None
Returns:
Type DescriptionSelf
The NFA accepting the language of the input regex.
"},{"location":"api/fa/class-nfa/#automata.fa.nfa.NFA.intersection","title":"intersection(other)
","text":"Given two NFAs, M1 and M2, which accept the languages L1 and L2 respectively, returns an NFA which accepts the intersection of L1 and L2.
Parameters:
Name Type Description Defaultother
NFA
The NFA we want to take an intersection with.
requiredReturns:
Type DescriptionSelf
An NFA accepting the intersection of the two input NFAs.
"},{"location":"api/fa/class-nfa/#automata.fa.nfa.NFA.iter_transitions","title":"iter_transitions()
","text":"Iterate over all transitions in the NFA. Each transition is a tuple of the form (from_state, to_state, symbol).
Returns:
Type DescriptionGenerator[Tuple[NFAStateT, NFAStateT, str], None, None]
The desired generator over the NFA transitions.
"},{"location":"api/fa/class-nfa/#automata.fa.nfa.NFA.kleene_star","title":"kleene_star()
","text":"Given an NFA which accepts the language L, returns an NFA which accepts L repeated 0 or more times.
Returns:
Type DescriptionSelf
An NFA accepting the finite repetition of the input NFA.
"},{"location":"api/fa/class-nfa/#automata.fa.nfa.NFA.left_quotient","title":"left_quotient(other)
","text":"Given two NFAs, M1 and M2, which accept the languages L1 and L2 respectively, returns an NFA which accepts the left quotient of L1 with respect to L2 (L2 L1).
Construction is based off of the one described here: https://cs.stackexchange.com/a/102043
Parameters:
Name Type Description Defaultother
NFA
The NFA we want to take a left quotient with.
requiredReturns:
Type DescriptionSelf
An NFA accepting the left quotient of the two input NFAs.
"},{"location":"api/fa/class-nfa/#automata.fa.nfa.NFA.option","title":"option()
","text":"Given an NFA which accepts the language L, returns an NFA which accepts L repeated 0 or 1 times.
Returns:
Type DescriptionSelf
An NFA accepting the optional of the input NFA.
"},{"location":"api/fa/class-nfa/#automata.fa.nfa.NFA.read_input_stepwise","title":"read_input_stepwise(input_str)
","text":"Return a generator that yields the configuration of this NFA at each step while reading input.
Parameters:
Name Type Description Defaultinput_str
str
The input string to read.
requiredYields:
Type DescriptionGenerator[AbstractSet[NFAStateT], None, None]
A generator that yields the current configuration (a set of states) of the NFA after each step of reading input.
Raises:
Type DescriptionRejectionException
Raised if this NFA does not accept the input string.
"},{"location":"api/fa/class-nfa/#automata.fa.nfa.NFA.reverse","title":"reverse()
","text":"Given an NFA which accepts the language L, returns an NFA which accepts the reverse of L.
Returns:
Type DescriptionSelf
An NFA accepting the reversal of the input NFA.
"},{"location":"api/fa/class-nfa/#automata.fa.nfa.NFA.right_quotient","title":"right_quotient(other)
","text":"Given two NFAs, M1 and M2, which accept the languages L1 and L2 respectively, returns an NFA which accepts the right quotient of L1 with respect to L2 (L1 / L2).
Construction is based off of the one described here: https://cs.stackexchange.com/a/102043
Parameters:
Name Type Description Defaultother
NFA
The NFA we want to take a right quotient with.
requiredReturns:
Type DescriptionSelf
An NFA accepting the right quotient of the two input NFAs.
"},{"location":"api/fa/class-nfa/#automata.fa.nfa.NFA.shuffle_product","title":"shuffle_product(other)
","text":"Given two NFAs, M1 and M2, which accept the languages L1 and L2 respectively, returns an NFA which accepts the shuffle of L1 and L2.
Parameters:
Name Type Description Defaultother
NFA
The NFA we want to take a shuffle product with.
requiredReturns:
Type DescriptionSelf
An NFA accepting the shuffle product of the two input NFAs.
"},{"location":"api/fa/class-nfa/#automata.fa.nfa.NFA.union","title":"union(other)
","text":"Given two NFAs, M1 and M2, which accept the languages L1 and L2 respectively, returns an NFA which accepts the union of L1 and L2.
Parameters:
Name Type Description Defaultother
NFA
The NFA we want to take a union with.
requiredReturns:
Type DescriptionSelf
An NFA accepting the union of the two input NFAs.
"},{"location":"api/fa/class-nfa/#automata.fa.nfa.NFA.validate","title":"validate()
","text":"Raises an exception if this automaton is not internally consistent.
Raises:
Type DescriptionInvalidStateError
If this NFA has invalid states in the transition dictionary.
MissingStateError
If this NFA has states missing from the transition dictionary.
InvalidSymbolError
If this NFA has invalid symbols in the transition dictionary.
"},{"location":"api/pda/class-dpda/","title":"class DPDA(PDA)","text":"Classes and methods for working with deterministic pushdown automata.
"},{"location":"api/pda/class-dpda/#automata.pda.dpda.DPDA","title":"DPDA
","text":" Bases: PDA
The DPDA
class is a subclass of PDA
and represents a deterministic pushdown automaton.
Parameters:
Name Type Description Defaultstates
AbstractSet[DPDAStateT]
A set of the DPDA's valid states
requiredinput_symbols
AbstractSet[str]
Set of the DPDA's valid input symbols, each of which is a singleton string.
requiredstack_symbols
AbstractSet[str]
Set of the DPDA's valid stack symbols, each of which is a singleton string.
requiredtransitions
DPDATransitionsT
A dict consisting of the transitions for each state; see the example below for the exact syntax
requiredinitial_state
DPDAStateT
The name of the initial state for this DPDA.
requiredinitial_stack_symbol
str
The name of the initial symbol on the stack for this DPDA.
requiredfinal_states
AbstractSet[DPDAStateT]
A set of final states for this DPDA.
requiredacceptance_mode
PDAAcceptanceModeT
A string defining whether this DPDA accepts by 'final_state'
, 'empty_stack'
, or 'both'
.
'both'
"},{"location":"api/pda/class-dpda/#automata.pda.dpda.DPDA--example","title":"Example","text":"from automata.pda.dpda import DPDA\n# DPDA which which matches zero or more 'a's, followed by the same\n# number of 'b's (accepting by final state)\ndpda = DPDA(\n states={'q0', 'q1', 'q2', 'q3'},\n input_symbols={'a', 'b'},\n stack_symbols={'0', '1'},\n transitions={\n 'q0': {\n 'a': {'0': ('q1', ('1', '0'))} # push '1' to stack\n },\n 'q1': {\n 'a': {'1': ('q1', ('1', '1'))}, # push '1' to stack\n 'b': {'1': ('q2', '')} # pop from stack\n },\n 'q2': {\n 'b': {'1': ('q2', '')}, # pop from stack\n '': {'0': ('q3', ('0',))} # no change to stack\n }\n },\n initial_state='q0',\n initial_stack_symbol='0',\n final_states={'q3'},\n acceptance_mode='final_state'\n)\n
"},{"location":"api/pda/class-dpda/#automata.pda.dpda.DPDA.read_input_stepwise","title":"read_input_stepwise(input_str)
","text":"Return a generator that yields the configuration of this DPDA at each step while reading input.
Parameters:
Name Type Description Defaultinput_str
str
The input string to read.
requiredYields:
Type DescriptionGenerator[PDAConfiguration, None, None]
A generator that yields the current configuration of the DPDA after each step of reading input.
Raises:
Type DescriptionRejectionException
Raised if this DPDA does not accept the input string.
"},{"location":"api/pda/class-npda/","title":"class NPDA(PDA)","text":"Classes and methods for working with nondeterministic pushdown automata.
"},{"location":"api/pda/class-npda/#automata.pda.npda.NPDA","title":"NPDA
","text":" Bases: PDA
The NPDA
class is a subclass of PDA
and represents a nondeterministic pushdown automaton.
Parameters:
Name Type Description Defaultstates
AbstractSet[NPDAStateT]
A set of the NPDA's valid states
requiredinput_symbols
AbstractSet[str]
Set of the NPDA's valid input symbols, each of which is a singleton string.
requiredstack_symbols
AbstractSet[str]
Set of the NPDA's valid stack symbols, each of which is a singleton string.
requiredtransitions
NPDATransitionsT
A dict consisting of the transitions for each state; see the example below for the exact syntax
requiredinitial_state
NPDAStateT
The name of the initial state for this NPDA.
requiredinitial_stack_symbol
str
The name of the initial symbol on the stack for this NPDA.
requiredfinal_states
AbstractSet[NPDAStateT]
A set of final states for this NPDA.
requiredacceptance_mode
PDAAcceptanceModeT
A string defining whether this NPDA accepts by 'final_state'
, 'empty_stack'
, or 'both'
.
'both'
"},{"location":"api/pda/class-npda/#automata.pda.npda.NPDA--example","title":"Example","text":"from automata.pda.npda import NPDA\n# NPDA which matches palindromes consisting of 'a's and 'b's\n# (accepting by final state)\n# q0 reads the first half of the word, q1 the other half, q2 accepts.\n# But we have to guess when to switch.\nnpda = NPDA(\n states={'q0', 'q1', 'q2'},\n input_symbols={'a', 'b'},\n stack_symbols={'A', 'B', '#'},\n transitions={\n 'q0': {\n '': {\n '#': {('q2', '#')}, # no change to stack\n },\n 'a': {\n '#': {('q0', ('A', '#'))}, # push 'A' to stack\n 'A': {\n ('q0', ('A', 'A')), # push 'A' to stack\n ('q1', ''), # pop from stack\n },\n 'B': {('q0', ('A', 'B'))}, # push 'A' to stack\n },\n 'b': {\n '#': {('q0', ('B', '#'))}, # push 'B' to stack\n 'A': {('q0', ('B', 'A'))}, # push 'B' to stack\n 'B': {\n ('q0', ('B', 'B')), # push 'B' to stack\n ('q1', ''), # pop from stack\n },\n },\n },\n 'q1': {\n '': {'#': {('q2', '#')}}, # push '#' to (currently empty) stack\n 'a': {'A': {('q1', '')}}, # pop from stack\n 'b': {'B': {('q1', '')}}, # pop from stack\n },\n },\n initial_state='q0',\n initial_stack_symbol='#',\n final_states={'q2'},\n acceptance_mode='final_state'\n)\n
"},{"location":"api/pda/class-npda/#automata.pda.npda.NPDA.read_input_stepwise","title":"read_input_stepwise(input_str)
","text":"Return a generator that yields the configuration of this NPDA at each step while reading input.
Parameters:
Name Type Description Defaultinput_str
str
The input string to read.
requiredYields:
Type DescriptionGenerator[Set[PDAConfiguration], None, None]
A generator that yields the current configuration of the NPDA after each step of reading input.
Raises:
Type DescriptionRejectionException
Raised if this NPDA does not accept the input string.
"},{"location":"api/pda/class-pda/","title":"class PDA(Automaton, metaclass=ABCMeta)","text":"Classes and methods for working with all pushdown automata.
"},{"location":"api/pda/class-pda/#automata.pda.pda.PDA","title":"PDA
","text":" Bases: Automaton
An abstract base class for pushdown automata.
"},{"location":"api/pda/class-pda/#automata.pda.pda.PDA.iter_transitions","title":"iter_transitions()
abstractmethod
","text":"Iterate over all transitions in the automaton. Each transition is a tuple of the form (from_state, to_state, (input_symbol, stack_top_symbol, stack_push_symbols))
"},{"location":"api/pda/class-pda/#automata.pda.pda.PDA.show_diagram","title":"show_diagram(input_str=None, with_machine=True, with_stack=True, path=None, *, layout_method='dot', horizontal=True, reverse_orientation=False, fig_size=None, font_size=14.0, arrow_size=0.85, state_separation=0.5)
","text":"Generates the graph associated with the given PDA. Args: - input_str (str, optional): String list of input symbols. Defaults to None. - with_machine (bool, optional): Constructs the diagram with states and transitions. Ignored if input_str
is None. Default to True. - with_stack (bool, optional): Constructs the diagram with stack and its operations. Ignored if input_str
is None. Default to True. - path (str or os.PathLike, optional): Path to output file. If None, the output will not be saved. - horizontal (bool, optional): Direction of node layout. Defaults to True. - reverse_orientation (bool, optional): Reverse direction of node layout. Defaults to False. - fig_size (tuple, optional): Figure size. Defaults to None. - font_size (float, optional): Font size. Defaults to 14.0. - arrow_size (float, optional): Arrow head size. Defaults to 0.85. - state_separation (float, optional): Node distance. Defaults to 0.5. Returns: AGraph corresponding to the given automaton.
validate()
","text":"Raises an exception if this automaton is not internally consistent.
Raises:
Type DescriptionInvalidStateError
If this PDA has invalid states in the transition dictionary.
MissingStateError
If this PDA has states missing from the transition dictionary.
InvalidSymbolError
If this PDA has invalid symbols in the transition dictionary.
InvalidAcceptanceModeError
If this PDA has an invalid acceptance mode.
"},{"location":"api/tm/class-dtm/","title":"class DTM(TM)","text":"Classes and methods for working with deterministic Turing machines.
"},{"location":"api/tm/class-dtm/#automata.tm.dtm.DTM","title":"DTM
","text":" Bases: TM
The DTM
class is a subclass of TM
and represents a deterministic Turing machine.
Parameters:
Name Type Description Defaultstates
AbstractSet[DTMStateT]
A set of the DTM's valid states.
requiredinput_symbols
AbstractSet[str]
Set of the DTM's valid input symbols, each of which is a singleton string.
requiredtape_symbols
AbstractSet[str]
Set of the DTM's valid tape symbols, each of which is a singleton string.
requiredtransitions
DTMTransitionsT
Dict consisting of the transitions for each state; each key is a state name, and each value is a dict which maps a symbol (the key) to a tuple consisting of the next state, the symbol to write on the tape, and the direction to move the tape head.
requiredinitial_state
DTMStateT
The name of the initial state for this DTM.
requiredblank_symbol
str
A symbol from tape_symbols
to be used as the blank symbol for this DTM.
final_states
AbstractSet[DTMStateT]
A set of final states for this DTM.
required"},{"location":"api/tm/class-dtm/#automata.tm.dtm.DTM--example","title":"Example","text":"from automata.tm.dtm import DTM\n# DTM which matches all strings beginning with '0's, and followed by\n# the same number of '1's\ndtm = DTM(\n states={'q0', 'q1', 'q2', 'q3', 'q4'},\n input_symbols={'0', '1'},\n tape_symbols={'0', '1', 'x', 'y', '.'},\n transitions={\n 'q0': {\n '0': ('q1', 'x', 'R'),\n 'y': ('q3', 'y', 'R')\n },\n 'q1': {\n '0': ('q1', '0', 'R'),\n '1': ('q2', 'y', 'L'),\n 'y': ('q1', 'y', 'R')\n },\n 'q2': {\n '0': ('q2', '0', 'L'),\n 'x': ('q0', 'x', 'R'),\n 'y': ('q2', 'y', 'L')\n },\n 'q3': {\n 'y': ('q3', 'y', 'R'),\n '.': ('q4', '.', 'R')\n }\n },\n initial_state='q0',\n blank_symbol='.',\n final_states={'q4'}\n)\n
"},{"location":"api/tm/class-dtm/#automata.tm.dtm.DTM.read_input_stepwise","title":"read_input_stepwise(input_str)
","text":"Return a generator that yields the configuration of this DTM at each step while reading input.
Parameters:
Name Type Description Defaultinput_str
str
The input string to read.
requiredYields:
Type DescriptionGenerator[TMConfiguration, None, None]
A generator that yields the current configuration of the DTM after each step of reading input.
"},{"location":"api/tm/class-dtm/#automata.tm.dtm.DTM.validate","title":"validate()
","text":"Raises an exception if this automaton is not internally consistent.
Raises:
Type DescriptionInvalidStateError
If this DTM has invalid states in the transition dictionary.
InvalidSymbolError
If this DTM has invalid symbols in the transition dictionary.
InvalidDirectionError
If this DTM has a transition with an invalid direction.
FinalStateError
If this DTM has a transition on any final states.
"},{"location":"api/tm/class-mntm/","title":"class MNTM(TM)","text":"Classes and methods for working with multitape nondeterministic Turing machines.
"},{"location":"api/tm/class-mntm/#automata.tm.mntm.MNTM","title":"MNTM
","text":" Bases: NTM
The MNTM
class is a subclass of TM
and represents a multitape (non)deterministic Turing machine.
Parameters:
Name Type Description Defaultstates
AbstractSet[MNTMStateT]
A set of the MNTM's valid states.
requiredinput_symbols
AbstractSet[str]
Set of the MNTM's valid input symbols, each of which is a singleton string.
requiredtape_symbols
AbstractSet[str]
Set of the MNTM's valid tape symbols, each of which is a singleton string.
requiredn_tapes
int
The number of tapes in this MNTM.
requiredtransitions
MNTMTransitionsT
Dict consisting of the transitions for each state; each key is a state name, and each value is a dict which maps a symbol (the key) to a list of tuples consisting of the next state, the symbol to write on the tape, and the direction to move the tape head.
requiredinitial_state
MNTMStateT
The name of the initial state for this MNTM.
requiredblank_symbol
str
A symbol from tape_symbols
to be used as the blank symbol for this MNTM.
final_states
AbstractSet[MNTMStateT]
A set of final states for this MNTM.
required"},{"location":"api/tm/class-mntm/#automata.tm.mntm.MNTM--example","title":"Example","text":"from automata.tm.mntm import MNTM\n# MNTM which accepts all strings in {0, 1}* and writes all\n# 1's from the first tape (input) to the second tape.\nself.mntm1 = MNTM(\n states={'q0', 'q1'},\n input_symbols={'0', '1'},\n tape_symbols={'0', '1', '#'},\n n_tapes=2,\n transitions={\n 'q0': {\n ('1', '#'): [('q0', (('1', 'R'), ('1', 'R')))],\n ('0', '#'): [('q0', (('0', 'R'), ('#', 'N')))],\n ('#', '#'): [('q1', (('#', 'N'), ('#', 'N')))],\n }\n },\n initial_state='q0',\n blank_symbol='#',\n final_states={'q1'},\n)\n
"},{"location":"api/tm/class-mntm/#automata.tm.mntm.MNTM.read_input_as_ntm","title":"read_input_as_ntm(input_str)
","text":"Simulates this MNTM as a single-tape Turing machine. Yields the configuration at each step.
Parameters:
Name Type Description Defaultinput_str
str
The input string to read.
requiredYields:
Type DescriptionGenerator[AbstractSet[TMConfiguration], None, None]
A generator that yields the current configuration of the MNTM as a set after each step of reading input.
Raises:
Type DescriptionRejectionException
Raised if this MNTM does not accept the input string.
"},{"location":"api/tm/class-mntm/#automata.tm.mntm.MNTM.read_input_stepwise","title":"read_input_stepwise(input_str)
","text":"Checks if the given string is accepted by this MNTM machine, using a BFS of every possible configuration from each configuration. Yields the current configuration of the machine at each step.
Parameters:
Name Type Description Defaultinput_str
str
The input string to read.
requiredYields:
Type DescriptionGenerator[Set[MTMConfiguration], None, None]
A generator that yields the current configuration of the DTM after each step of reading input.
Raises:
Type DescriptionRejectionException
Raised if this MNTM does not accept the input string.
"},{"location":"api/tm/class-mntm/#automata.tm.mntm.MNTM.validate","title":"validate()
","text":"Raises an exception if this automaton is not internally consistent.
Raises:
Type DescriptionInvalidStateError
If this MNTM has invalid states in the transition dictionary.
InvalidSymbolError
If this MNTM has invalid symbols in the transition dictionary.
InvalidDirectionError
If this MNTM has a transition with an invalid direction.
FinalStateError
If this MNTM has a transition on any final states.
InconsistentTapesException
If this MNTM has inconsistent tape contents.
"},{"location":"api/tm/class-ntm/","title":"class NTM(TM)","text":"Classes and methods for working with nondeterministic Turing machines.
"},{"location":"api/tm/class-ntm/#automata.tm.ntm.NTM","title":"NTM
","text":" Bases: TM
The NTM
class is a subclass of TM
and represents a nondeterministic Turing machine.
Parameters:
Name Type Description Defaultstates
AbstractSet[NTMStateT]
A set of the NTM's valid states.
requiredinput_symbols
AbstractSet[str]
Set of the NTM's valid input symbols, each of which is a singleton string.
requiredtape_symbols
AbstractSet[str]
Set of the NTM's valid tape symbols, each of which is a singleton string.
requiredtransitions
NTMTransitionsT
Dict consisting of the transitions for each state; each key is a state name, and each value is a dict which maps a symbol (the key) to a set of tuples consisting of the next state, the symbol to write on the tape, and the direction to move the tape head.
requiredinitial_state
NTMStateT
The name of the initial state for this NTM.
requiredblank_symbol
str
A symbol from tape_symbols
to be used as the blank symbol for this NTM.
final_states
AbstractSet[NTMStateT]
A set of final states for this NTM.
required"},{"location":"api/tm/class-ntm/#automata.tm.ntm.NTM--example","title":"Example","text":"from automata.tm.ntm import NTM\n# NTM which matches all strings beginning with '0's, and followed by\n# the same number of '1's\n# Note that the nondeterminism is not really used here.\nntm = NTM(\n states={'q0', 'q1', 'q2', 'q3', 'q4'},\n input_symbols={'0', '1'},\n tape_symbols={'0', '1', 'x', 'y', '.'},\n transitions={\n 'q0': {\n '0': {('q1', 'x', 'R')},\n 'y': {('q3', 'y', 'R')},\n },\n 'q1': {\n '0': {('q1', '0', 'R')},\n '1': {('q2', 'y', 'L')},\n 'y': {('q1', 'y', 'R')},\n },\n 'q2': {\n '0': {('q2', '0', 'L')},\n 'x': {('q0', 'x', 'R')},\n 'y': {('q2', 'y', 'L')},\n },\n 'q3': {\n 'y': {('q3', 'y', 'R')},\n '.': {('q4', '.', 'R')},\n }\n },\n initial_state='q0',\n blank_symbol='.',\n final_states={'q4'}\n)\n
"},{"location":"api/tm/class-ntm/#automata.tm.ntm.NTM.read_input_stepwise","title":"read_input_stepwise(input_str)
","text":"Check if the given string is accepted by this Turing machine.
Yield the current configurations of the machine at each step.
Parameters:
Name Type Description Defaultinput_str
str
The input string to read.
requiredYields:
Type DescriptionGenerator[Set[TMConfiguration], None, None]
A generator that yields the current configuration of the NTM after each step of reading input.
Raises:
Type DescriptionRejectionException
Raised if this NTM does not accept the input string.
"},{"location":"api/tm/class-ntm/#automata.tm.ntm.NTM.validate","title":"validate()
","text":"Raises an exception if this automaton is not internally consistent.
Raises:
Type DescriptionInvalidStateError
If this NTM has invalid states in the transition dictionary.
InvalidSymbolError
If this NTM has invalid symbols in the transition dictionary.
InvalidDirectionError
If this NTM has a transition with an invalid direction.
FinalStateError
If this NTM has a transition on any final states.
"},{"location":"api/tm/class-tm/","title":"class TM(Automaton, metaclass=ABCMeta)","text":"Classes and methods for working with all Turing machines.
"},{"location":"api/tm/class-tm/#automata.tm.tm.TM","title":"TM
","text":" Bases: Automaton
An abstract base class for Turing machines.
"},{"location":"api/tm/tm-exception-classes/","title":"Turing machine exception classes","text":"Exception classes specific to Turing machines.
"},{"location":"api/tm/tm-exception-classes/#automata.tm.exceptions.InconsistentTapesException","title":"InconsistentTapesException
","text":" Bases: TMException
The number of tapes defined for the multitape Turing machine is not consistent with the definitions of the transitions.
"},{"location":"api/tm/tm-exception-classes/#automata.tm.exceptions.InvalidDirectionError","title":"InvalidDirectionError
","text":" Bases: TMException
A direction is not a valid direction for this machine.
"},{"location":"api/tm/tm-exception-classes/#automata.tm.exceptions.MalformedExtendedTapeError","title":"MalformedExtendedTapeError
","text":" Bases: TMException
Extended tape for simulating an mntm as a ntm is not valid. Either there are 2 virtual heads for a virtual tape or the a head symbol is at the leftmost end of a virtual tape.
"},{"location":"api/tm/tm-exception-classes/#automata.tm.exceptions.TMException","title":"TMException
","text":" Bases: AutomatonException
The base class for all machine-related errors.
"},{"location":"examples/fa-examples/","title":"FA Examples","text":"On this page, we give some short examples with discussion for the finite automata classes and methods.
"},{"location":"examples/fa-examples/#reading-basic-input","title":"Reading basic input","text":"The following code snippet creates a DFA
and prints whether it accepts or rejects user input.
from automata.fa.dfa import DFA\n# DFA which matches all binary strings ending in an odd number of '1's\nmy_dfa = DFA(\nstates={'q0', 'q1', 'q2'},\ninput_symbols={'0', '1'},\ntransitions={\n'q0': {'0': 'q0', '1': 'q1'},\n'q1': {'0': 'q0', '1': 'q2'},\n'q2': {'0': 'q2', '1': 'q1'}\n},\ninitial_state='q0',\nfinal_states={'q1'}\n)\ntry:\nwhile True:\nif my_dfa.accepts_input(input('Please enter your input: ')):\nprint('Accepted')\nelse:\nprint('Rejected')\nexcept KeyboardInterrupt:\nprint('')\n
"},{"location":"examples/fa-examples/#subset-for-nfas","title":"Subset for NFAs","text":"The NFA
does not have a built-in method for checking whether it is a subset of another NFA
. However, this can be done using existing methods.
# In the following, we have nfa1 and nfa2 and want to determine whether\n# nfa1 is a subset of nfa2.\n# If taking the union of nfa2 with nfa1 is equal to nfa2 again,\n# nfa1 didn't accept any strings that nfa2 did not, so it is a subset.\nif (nfa1 | nfa2) == nfa2:\nprint('nfa1 is a subset of nfa2.')\nelse:\nprint('nfa1 is not a subset of nfa2.')\n
"},{"location":"examples/fa-examples/#edit-distance-automaton","title":"Edit distance automaton","text":"The following example is inspired by this blog post. Essentially, we want to determine which strings in a given set are within the target edit distance to a reference string.
from automata.fa.dfa import DFA\nfrom automata.fa.nfa import NFA\nimport string\ninput_symbols = set(string.ascii_lowercase)\n# Construct DFA recognizing target words\ntarget_words = {'these', 'are', 'target', 'words', 'them', 'those'}\ntarget_words_dfa = DFA.from_finite_language(\ninput_symbols,\ntarget_words,\n)\n# Next, construct NFA recognizing all strings\n# within given edit distance of target word\nreference_string = 'they'\nedit_distance = 2\nwords_within_edit_distance_dfa = DFA.from_nfa(\nNFA.edit_distance(\ninput_symbols,\nreference_string,\nedit_distance,\n)\n)\n# Take intersection and print results\nfound_words_dfa = target_words_dfa & words_within_edit_distance_dfa\nfound_words = list(found_words_dfa)\nprint(\nf\"All words within edit distance {edit_distance} of \"\nf\"'{reference_string}': {found_words}\"\n)\n
"},{"location":"examples/fa-examples/#making-a-transition-table","title":"Making a transition table","text":"The example below is adapted from the visual automata library. This function takes in a DFA
or NFA
and returns the corresponding transition table.
The start state is prefixed with \u2192
and final states are prefixed with *
.
import pandas as pd\ndef make_table(target_fa) -> pd.DataFrame:\ninitial_state = target_fa.initial_state\nfinal_states = target_fa.final_states\ntable = {}\nfor from_state, to_state, symbol in target_fa.iter_transitions():\n# Prepare nice string for from_state\nif isinstance(from_state, frozenset):\nfrom_state_str = str(set(from_state))\nelse:\nfrom_state_str = str(from_state)\nif from_state in final_states:\nfrom_state_str = \"*\" + from_state_str\nif from_state == initial_state:\nfrom_state_str = \"\u2192\" + from_state_str\n# Prepare nice string for to_state\nif isinstance(to_state, frozenset):\nto_state_str = str(set(to_state))\nelse:\nto_state_str = str(to_state)\nif to_state in final_states:\nto_state_str = \"*\" + to_state_str\n# Prepare nice symbol\nif symbol == \"\":\nsymbol = \"\u03bb\"\nfrom_state_dict = table.setdefault(from_state_str, dict())\nfrom_state_dict.setdefault(symbol, set()).add(to_state_str)\n# Reformat table for singleton sets\nfor symbol_dict in table.values():\nfor symbol in symbol_dict:\nif len(symbol_dict[symbol]) == 1:\nsymbol_dict[symbol] = symbol_dict[symbol].pop()\ndf = pd.DataFrame.from_dict(table).fillna(\"\u2205\").T\nreturn df.reindex(sorted(df.columns), axis=1)\n
"}]}
\ No newline at end of file
diff --git a/sitemap.xml b/sitemap.xml
new file mode 100644
index 00000000..cd2b7189
--- /dev/null
+++ b/sitemap.xml
@@ -0,0 +1,108 @@
+
+