mocha-model-checking 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mocha/__init__.py +199 -0
- mocha/algorithms/__init__.py +159 -0
- mocha/algorithms/automata.py +205 -0
- mocha/algorithms/invariance.py +166 -0
- mocha/algorithms/ltl2gnba/__init__.py +75 -0
- mocha/algorithms/ltl2gnba/acceptance.py +199 -0
- mocha/algorithms/ltl2gnba/elementary_set_enumeration.py +360 -0
- mocha/algorithms/ltl2gnba/elementary_set_validation.py +312 -0
- mocha/algorithms/ltl2gnba/initial_states.py +116 -0
- mocha/algorithms/ltl2gnba/pipeline.py +137 -0
- mocha/algorithms/ltl2gnba/transitions.py +327 -0
- mocha/algorithms/ltl_model_checking.py +313 -0
- mocha/algorithms/ltl_satisfiability_validity.py +308 -0
- mocha/algorithms/ltl_transformations.py +291 -0
- mocha/algorithms/persistence.py +259 -0
- mocha/algorithms/reachability.py +143 -0
- mocha/algorithms/trace.py +22 -0
- mocha/io/__init__.py +20 -0
- mocha/io/ltl.py +195 -0
- mocha/io/schemas/__init__.py +1 -0
- mocha/io/schemas/transition-system.schema.json +95 -0
- mocha/io/transition_system.py +136 -0
- mocha/logic/__init__.py +5 -0
- mocha/logic/_syntax.py +8 -0
- mocha/logic/ltl.py +179 -0
- mocha/models/__init__.py +37 -0
- mocha/models/automaton.py +604 -0
- mocha/models/transition_system.py +535 -0
- mocha/py.typed +0 -0
- mocha/visualisation/__init__.py +55 -0
- mocha/visualisation/automaton.py +170 -0
- mocha/visualisation/invariance.py +84 -0
- mocha/visualisation/overlay.py +24 -0
- mocha/visualisation/persistence.py +48 -0
- mocha/visualisation/reachability.py +35 -0
- mocha/visualisation/transition_system.py +252 -0
- mocha_model_checking-0.1.0.dist-info/METADATA +587 -0
- mocha_model_checking-0.1.0.dist-info/RECORD +41 -0
- mocha_model_checking-0.1.0.dist-info/WHEEL +5 -0
- mocha_model_checking-0.1.0.dist-info/licenses/LICENCE +340 -0
- mocha_model_checking-0.1.0.dist-info/top_level.txt +1 -0
mocha/models/__init__.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""Domain models used by the Model Checking Assistant."""
|
|
2
|
+
|
|
3
|
+
from .automaton import (
|
|
4
|
+
Automaton,
|
|
5
|
+
AutomatonState,
|
|
6
|
+
AutomatonTransition,
|
|
7
|
+
FiniteAutomaton,
|
|
8
|
+
GNBA,
|
|
9
|
+
GeneralizedNondeterministicBuchiAutomaton,
|
|
10
|
+
NBA,
|
|
11
|
+
NondeterministicBuchiAutomaton,
|
|
12
|
+
Symbol,
|
|
13
|
+
)
|
|
14
|
+
from .transition_system import (
|
|
15
|
+
Action,
|
|
16
|
+
AtomicProposition,
|
|
17
|
+
State,
|
|
18
|
+
Transition,
|
|
19
|
+
TransitionSystem,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
__all__ = [
|
|
23
|
+
"Action",
|
|
24
|
+
"AtomicProposition",
|
|
25
|
+
"Automaton",
|
|
26
|
+
"AutomatonState",
|
|
27
|
+
"AutomatonTransition",
|
|
28
|
+
"FiniteAutomaton",
|
|
29
|
+
"GNBA",
|
|
30
|
+
"GeneralizedNondeterministicBuchiAutomaton",
|
|
31
|
+
"NBA",
|
|
32
|
+
"NondeterministicBuchiAutomaton",
|
|
33
|
+
"State",
|
|
34
|
+
"Symbol",
|
|
35
|
+
"Transition",
|
|
36
|
+
"TransitionSystem",
|
|
37
|
+
]
|
|
@@ -0,0 +1,604 @@
|
|
|
1
|
+
"""Typed representations of finite and omega (infinite word) automata.
|
|
2
|
+
|
|
3
|
+
The shared automaton structure is the tuple ``(Q, Σ, δ, I)``: states, alphabet,
|
|
4
|
+
transition relation, and initial states. Acceptance conditions belong to the
|
|
5
|
+
specialized classes: ``F ⊆ Q`` for finite automata and nondeterministic Büchi
|
|
6
|
+
automata, and ``𝓕 ⊆ 2^Q`` for generalized nondeterministic Büchi automata.
|
|
7
|
+
"""
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from collections.abc import Iterable, Iterator
|
|
11
|
+
from typing import Generic, Hashable, NamedTuple, Self, TypeVar
|
|
12
|
+
|
|
13
|
+
AutomatonState = TypeVar("AutomatonState", bound=Hashable)
|
|
14
|
+
Symbol = TypeVar("Symbol", bound=Hashable)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class AutomatonTransition(NamedTuple, Generic[AutomatonState, Symbol]):
|
|
18
|
+
"""A member of an automaton transition relation.
|
|
19
|
+
|
|
20
|
+
The tuple represents the transition ``source --symbol--> target``.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
source: AutomatonState
|
|
24
|
+
symbol: Symbol
|
|
25
|
+
target: AutomatonState
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class Automaton(Generic[AutomatonState, Symbol]):
|
|
29
|
+
"""A mutable automaton graph without an acceptance condition.
|
|
30
|
+
|
|
31
|
+
The automaton stores the shared components ``(Q, Σ, δ, I)``. Its transition
|
|
32
|
+
relation may be nondeterministic, and it may have multiple initial states.
|
|
33
|
+
Collection properties return immutable ``frozenset`` snapshots; use the
|
|
34
|
+
supplied mutation methods to change the automaton. Subclasses add an
|
|
35
|
+
acceptance condition and its semantics.
|
|
36
|
+
|
|
37
|
+
``None`` is reserved as the unfiltered-symbol sentinel in :meth:`post` and
|
|
38
|
+
:meth:`pre`, and therefore cannot be an alphabet symbol.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
def __init__(self) -> None:
|
|
42
|
+
"""Create an automaton with empty components."""
|
|
43
|
+
self._states: set[AutomatonState] = set()
|
|
44
|
+
self._alphabet: set[Symbol] = set()
|
|
45
|
+
self._transitions: dict[
|
|
46
|
+
AutomatonState,
|
|
47
|
+
dict[Symbol, set[AutomatonState]],
|
|
48
|
+
] = {}
|
|
49
|
+
self._initial_states: set[AutomatonState] = set()
|
|
50
|
+
|
|
51
|
+
@staticmethod
|
|
52
|
+
def _validate_symbol(symbol: Symbol) -> None:
|
|
53
|
+
"""Reject the sentinel used for unfiltered transition queries."""
|
|
54
|
+
if symbol is None:
|
|
55
|
+
raise ValueError("None cannot be used as an alphabet symbol")
|
|
56
|
+
|
|
57
|
+
@property
|
|
58
|
+
def states(self) -> frozenset[AutomatonState]:
|
|
59
|
+
"""Return the state set ``Q``."""
|
|
60
|
+
return frozenset(self._states)
|
|
61
|
+
|
|
62
|
+
@property
|
|
63
|
+
def alphabet(self) -> frozenset[Symbol]:
|
|
64
|
+
"""Return the input alphabet ``Σ``."""
|
|
65
|
+
return frozenset(self._alphabet)
|
|
66
|
+
|
|
67
|
+
@property
|
|
68
|
+
def initial_states(self) -> frozenset[AutomatonState]:
|
|
69
|
+
"""Return the initial-state set ``I``."""
|
|
70
|
+
return frozenset(self._initial_states)
|
|
71
|
+
|
|
72
|
+
def add_state(
|
|
73
|
+
self,
|
|
74
|
+
state: AutomatonState,
|
|
75
|
+
*,
|
|
76
|
+
initial: bool = False,
|
|
77
|
+
) -> None:
|
|
78
|
+
"""Add a state with optional initial status.
|
|
79
|
+
|
|
80
|
+
Raises:
|
|
81
|
+
ValueError: If ``state`` is already present.
|
|
82
|
+
"""
|
|
83
|
+
if state in self._states:
|
|
84
|
+
raise ValueError(f"State {state!r} already exists")
|
|
85
|
+
|
|
86
|
+
self._states.add(state)
|
|
87
|
+
self._transitions[state] = {}
|
|
88
|
+
|
|
89
|
+
if initial:
|
|
90
|
+
self._initial_states.add(state)
|
|
91
|
+
|
|
92
|
+
def add_symbol(self, symbol: Symbol) -> None:
|
|
93
|
+
"""Add ``symbol`` to the alphabet without adding a transition."""
|
|
94
|
+
self._validate_symbol(symbol)
|
|
95
|
+
self._alphabet.add(symbol)
|
|
96
|
+
|
|
97
|
+
def add_transition(
|
|
98
|
+
self,
|
|
99
|
+
source: AutomatonState,
|
|
100
|
+
symbol: Symbol,
|
|
101
|
+
target: AutomatonState,
|
|
102
|
+
) -> None:
|
|
103
|
+
"""Add ``source --symbol--> target`` to the transition relation.
|
|
104
|
+
|
|
105
|
+
Endpoints must already be states, and the symbol must be registered
|
|
106
|
+
in the alphabet with :meth:`add_symbol`.
|
|
107
|
+
|
|
108
|
+
Raises:
|
|
109
|
+
ValueError: If either endpoint or the symbol is unknown, or
|
|
110
|
+
``symbol`` is ``None``.
|
|
111
|
+
"""
|
|
112
|
+
if source not in self._states:
|
|
113
|
+
raise ValueError(f"Unknown source state {source!r}")
|
|
114
|
+
if target not in self._states:
|
|
115
|
+
raise ValueError(f"Unknown target state {target!r}")
|
|
116
|
+
|
|
117
|
+
self._validate_symbol(symbol)
|
|
118
|
+
if symbol not in self._alphabet:
|
|
119
|
+
raise ValueError(f"Unknown alphabet symbol {symbol!r}")
|
|
120
|
+
self._transitions[source].setdefault(symbol, set()).add(target)
|
|
121
|
+
|
|
122
|
+
def set_initial(
|
|
123
|
+
self,
|
|
124
|
+
state: AutomatonState,
|
|
125
|
+
initial: bool = True,
|
|
126
|
+
) -> None:
|
|
127
|
+
"""Set or clear membership of ``state`` in ``I``.
|
|
128
|
+
|
|
129
|
+
Raises:
|
|
130
|
+
ValueError: If ``state`` is unknown.
|
|
131
|
+
"""
|
|
132
|
+
if state not in self._states:
|
|
133
|
+
raise ValueError(f"Unknown state {state!r}")
|
|
134
|
+
|
|
135
|
+
if initial:
|
|
136
|
+
self._initial_states.add(state)
|
|
137
|
+
else:
|
|
138
|
+
self._initial_states.discard(state)
|
|
139
|
+
|
|
140
|
+
def post(
|
|
141
|
+
self,
|
|
142
|
+
state: AutomatonState,
|
|
143
|
+
symbol: Symbol | None = None,
|
|
144
|
+
) -> frozenset[AutomatonState]:
|
|
145
|
+
"""Return successors of ``state``, optionally after ``symbol``.
|
|
146
|
+
|
|
147
|
+
Raises:
|
|
148
|
+
ValueError: If ``state`` is unknown.
|
|
149
|
+
"""
|
|
150
|
+
if state not in self._states:
|
|
151
|
+
raise ValueError(f"Unknown state {state!r}")
|
|
152
|
+
|
|
153
|
+
if symbol is not None:
|
|
154
|
+
return frozenset(self._transitions[state].get(symbol, ()))
|
|
155
|
+
|
|
156
|
+
successors: set[AutomatonState] = set()
|
|
157
|
+
for targets in self._transitions[state].values():
|
|
158
|
+
successors.update(targets)
|
|
159
|
+
return frozenset(successors)
|
|
160
|
+
|
|
161
|
+
def pre(
|
|
162
|
+
self,
|
|
163
|
+
state: AutomatonState,
|
|
164
|
+
symbol: Symbol | None = None,
|
|
165
|
+
) -> frozenset[AutomatonState]:
|
|
166
|
+
"""Return predecessors of ``state``, optionally before ``symbol``.
|
|
167
|
+
|
|
168
|
+
Raises:
|
|
169
|
+
ValueError: If ``state`` is unknown.
|
|
170
|
+
"""
|
|
171
|
+
if state not in self._states:
|
|
172
|
+
raise ValueError(f"Unknown state {state!r}")
|
|
173
|
+
|
|
174
|
+
predecessors: set[AutomatonState] = set()
|
|
175
|
+
for source, transitions_by_symbol in self._transitions.items():
|
|
176
|
+
if symbol is None:
|
|
177
|
+
if any(
|
|
178
|
+
state in targets
|
|
179
|
+
for targets in transitions_by_symbol.values()
|
|
180
|
+
):
|
|
181
|
+
predecessors.add(source)
|
|
182
|
+
elif state in transitions_by_symbol.get(symbol, ()):
|
|
183
|
+
predecessors.add(source)
|
|
184
|
+
|
|
185
|
+
return frozenset(predecessors)
|
|
186
|
+
|
|
187
|
+
def transitions(
|
|
188
|
+
self,
|
|
189
|
+
) -> Iterator[AutomatonTransition[AutomatonState, Symbol]]:
|
|
190
|
+
"""Iterate over each distinct member of ``δ``."""
|
|
191
|
+
for source, transitions_by_symbol in self._transitions.items():
|
|
192
|
+
for symbol, targets in transitions_by_symbol.items():
|
|
193
|
+
for target in targets:
|
|
194
|
+
yield AutomatonTransition(source, symbol, target)
|
|
195
|
+
|
|
196
|
+
def has_transition(
|
|
197
|
+
self,
|
|
198
|
+
source: AutomatonState,
|
|
199
|
+
symbol: Symbol,
|
|
200
|
+
target: AutomatonState,
|
|
201
|
+
) -> bool:
|
|
202
|
+
"""Return whether ``source --symbol--> target`` belongs to ``δ``."""
|
|
203
|
+
return (
|
|
204
|
+
source in self._transitions
|
|
205
|
+
and target in self._transitions[source].get(symbol, ())
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
@property
|
|
209
|
+
def is_deterministic(self) -> bool:
|
|
210
|
+
"""Return whether the automaton is structurally deterministic.
|
|
211
|
+
|
|
212
|
+
There must be at most one initial state and at most one successor for
|
|
213
|
+
every state-symbol pair, including those at unreachable states.
|
|
214
|
+
Missing transitions are allowed; :attr:`is_complete` checks that
|
|
215
|
+
every state-symbol pair has a successor.
|
|
216
|
+
"""
|
|
217
|
+
return (
|
|
218
|
+
len(self._initial_states) <= 1
|
|
219
|
+
and all(
|
|
220
|
+
len(targets) <= 1
|
|
221
|
+
for transitions_by_symbol in self._transitions.values()
|
|
222
|
+
for targets in transitions_by_symbol.values()
|
|
223
|
+
)
|
|
224
|
+
)
|
|
225
|
+
|
|
226
|
+
@property
|
|
227
|
+
def is_complete(self) -> bool:
|
|
228
|
+
"""Return whether every state-symbol pair has at least one successor.
|
|
229
|
+
|
|
230
|
+
All states are checked, including unreachable ones. Completeness is
|
|
231
|
+
independent of determinism, initial states, and acceptance conditions.
|
|
232
|
+
With no states or no alphabet symbols, the condition is vacuously true.
|
|
233
|
+
"""
|
|
234
|
+
return all(
|
|
235
|
+
transitions_by_symbol.get(symbol)
|
|
236
|
+
for transitions_by_symbol in self._transitions.values()
|
|
237
|
+
for symbol in self._alphabet
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
def remove_transition(
|
|
241
|
+
self,
|
|
242
|
+
source: AutomatonState,
|
|
243
|
+
symbol: Symbol,
|
|
244
|
+
target: AutomatonState,
|
|
245
|
+
) -> None:
|
|
246
|
+
"""Remove an existing transition from ``δ``.
|
|
247
|
+
|
|
248
|
+
The symbol remains in ``Σ`` even if this removes its final transition.
|
|
249
|
+
|
|
250
|
+
Raises:
|
|
251
|
+
ValueError: If an endpoint is unknown or the transition is absent.
|
|
252
|
+
"""
|
|
253
|
+
if source not in self._states:
|
|
254
|
+
raise ValueError(f"Unknown source state {source!r}")
|
|
255
|
+
if target not in self._states:
|
|
256
|
+
raise ValueError(f"Unknown target state {target!r}")
|
|
257
|
+
|
|
258
|
+
targets = self._transitions[source].get(symbol)
|
|
259
|
+
if targets is None or target not in targets:
|
|
260
|
+
raise ValueError(
|
|
261
|
+
f"Transition ({source!r}, {symbol!r}, {target!r}) does not exist"
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
targets.remove(target)
|
|
265
|
+
if not targets:
|
|
266
|
+
del self._transitions[source][symbol]
|
|
267
|
+
|
|
268
|
+
def remove_state(self, state: AutomatonState) -> None:
|
|
269
|
+
"""Remove ``state`` and all incident transitions.
|
|
270
|
+
|
|
271
|
+
Its initial membership is also removed; alphabet symbols remain
|
|
272
|
+
declared.
|
|
273
|
+
|
|
274
|
+
Raises:
|
|
275
|
+
ValueError: If ``state`` is unknown.
|
|
276
|
+
"""
|
|
277
|
+
if state not in self._states:
|
|
278
|
+
raise ValueError(f"Unknown state {state!r}")
|
|
279
|
+
|
|
280
|
+
for source in self._states:
|
|
281
|
+
if source == state:
|
|
282
|
+
continue
|
|
283
|
+
|
|
284
|
+
empty_symbols: list[Symbol] = []
|
|
285
|
+
for symbol, targets in self._transitions[source].items():
|
|
286
|
+
targets.discard(state)
|
|
287
|
+
if not targets:
|
|
288
|
+
empty_symbols.append(symbol)
|
|
289
|
+
for symbol in empty_symbols:
|
|
290
|
+
del self._transitions[source][symbol]
|
|
291
|
+
|
|
292
|
+
del self._transitions[state]
|
|
293
|
+
self._states.remove(state)
|
|
294
|
+
self._initial_states.discard(state)
|
|
295
|
+
|
|
296
|
+
def remove_symbol(self, symbol: Symbol) -> None:
|
|
297
|
+
"""Remove a symbol from ``Σ`` and all transitions labelled by it.
|
|
298
|
+
|
|
299
|
+
Raises:
|
|
300
|
+
ValueError: If ``symbol`` is unknown.
|
|
301
|
+
"""
|
|
302
|
+
if symbol not in self._alphabet:
|
|
303
|
+
raise ValueError(f"Unknown alphabet symbol {symbol!r}")
|
|
304
|
+
|
|
305
|
+
for transitions_by_symbol in self._transitions.values():
|
|
306
|
+
transitions_by_symbol.pop(symbol, None)
|
|
307
|
+
self._alphabet.remove(symbol)
|
|
308
|
+
|
|
309
|
+
@classmethod
|
|
310
|
+
def from_components(
|
|
311
|
+
cls,
|
|
312
|
+
*,
|
|
313
|
+
states: Iterable[AutomatonState],
|
|
314
|
+
alphabet: Iterable[Symbol],
|
|
315
|
+
transitions: Iterable[tuple[AutomatonState, Symbol, AutomatonState]],
|
|
316
|
+
initial_states: Iterable[AutomatonState] = (),
|
|
317
|
+
) -> Self:
|
|
318
|
+
"""Construct an automaton from the explicit tuple components.
|
|
319
|
+
|
|
320
|
+
The declared states and alphabet form ``Q`` and ``Σ``. Initial states,
|
|
321
|
+
transition endpoints, and transition symbols must be declared; this
|
|
322
|
+
constructor never infers omitted components.
|
|
323
|
+
|
|
324
|
+
Raises:
|
|
325
|
+
ValueError: If a component refers to an undeclared state or symbol,
|
|
326
|
+
or if ``None`` is supplied as an alphabet symbol.
|
|
327
|
+
"""
|
|
328
|
+
states = set(states)
|
|
329
|
+
alphabet = set(alphabet)
|
|
330
|
+
transitions = set(transitions)
|
|
331
|
+
initial_states = set(initial_states)
|
|
332
|
+
|
|
333
|
+
if None in alphabet:
|
|
334
|
+
raise ValueError("None cannot be used as an alphabet symbol")
|
|
335
|
+
|
|
336
|
+
if not initial_states <= states:
|
|
337
|
+
raise ValueError(
|
|
338
|
+
f"Unknown initial states: {initial_states - states!r}"
|
|
339
|
+
)
|
|
340
|
+
for source, symbol, target in transitions:
|
|
341
|
+
if source not in states:
|
|
342
|
+
raise ValueError(f"Unknown transition source: {source!r}")
|
|
343
|
+
if target not in states:
|
|
344
|
+
raise ValueError(f"Unknown transition target: {target!r}")
|
|
345
|
+
if symbol is None:
|
|
346
|
+
raise ValueError("None cannot be used as an alphabet symbol")
|
|
347
|
+
if symbol not in alphabet:
|
|
348
|
+
raise ValueError(f"Unknown transition symbol: {symbol!r}")
|
|
349
|
+
|
|
350
|
+
automaton = cls()
|
|
351
|
+
for state in states:
|
|
352
|
+
automaton.add_state(
|
|
353
|
+
state,
|
|
354
|
+
initial=state in initial_states,
|
|
355
|
+
)
|
|
356
|
+
for symbol in alphabet:
|
|
357
|
+
automaton.add_symbol(symbol)
|
|
358
|
+
for source, symbol, target in transitions:
|
|
359
|
+
automaton.add_transition(source, symbol, target)
|
|
360
|
+
|
|
361
|
+
return automaton
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
class _StateAcceptanceAutomaton(Automaton[AutomatonState, Symbol]):
|
|
365
|
+
"""Shared storage for automata whose condition is ``F ⊆ Q``."""
|
|
366
|
+
|
|
367
|
+
def __init__(self) -> None:
|
|
368
|
+
super().__init__()
|
|
369
|
+
self._accepting_states: set[AutomatonState] = set()
|
|
370
|
+
|
|
371
|
+
@property
|
|
372
|
+
def accepting_states(self) -> frozenset[AutomatonState]:
|
|
373
|
+
"""Return the accepting-state set ``F``."""
|
|
374
|
+
return frozenset(self._accepting_states)
|
|
375
|
+
|
|
376
|
+
def add_state(
|
|
377
|
+
self,
|
|
378
|
+
state: AutomatonState,
|
|
379
|
+
*,
|
|
380
|
+
initial: bool = False,
|
|
381
|
+
accepting: bool = False,
|
|
382
|
+
) -> None:
|
|
383
|
+
"""Add a state with optional initial and accepting status."""
|
|
384
|
+
super().add_state(state, initial=initial)
|
|
385
|
+
if accepting:
|
|
386
|
+
self._accepting_states.add(state)
|
|
387
|
+
|
|
388
|
+
def set_accepting(
|
|
389
|
+
self,
|
|
390
|
+
state: AutomatonState,
|
|
391
|
+
accepting: bool = True,
|
|
392
|
+
) -> None:
|
|
393
|
+
"""Set or clear membership of ``state`` in ``F``.
|
|
394
|
+
|
|
395
|
+
Raises:
|
|
396
|
+
ValueError: If ``state`` is unknown.
|
|
397
|
+
"""
|
|
398
|
+
if state not in self._states:
|
|
399
|
+
raise ValueError(f"Unknown state {state!r}")
|
|
400
|
+
|
|
401
|
+
if accepting:
|
|
402
|
+
self._accepting_states.add(state)
|
|
403
|
+
else:
|
|
404
|
+
self._accepting_states.discard(state)
|
|
405
|
+
|
|
406
|
+
def remove_state(self, state: AutomatonState) -> None:
|
|
407
|
+
"""Remove a state and its membership in ``F``."""
|
|
408
|
+
super().remove_state(state)
|
|
409
|
+
self._accepting_states.discard(state)
|
|
410
|
+
|
|
411
|
+
@classmethod
|
|
412
|
+
def from_components(
|
|
413
|
+
cls,
|
|
414
|
+
*,
|
|
415
|
+
states: Iterable[AutomatonState],
|
|
416
|
+
alphabet: Iterable[Symbol],
|
|
417
|
+
transitions: Iterable[tuple[AutomatonState, Symbol, AutomatonState]],
|
|
418
|
+
initial_states: Iterable[AutomatonState] = (),
|
|
419
|
+
accepting_states: Iterable[AutomatonState] = (),
|
|
420
|
+
) -> Self:
|
|
421
|
+
"""Construct an automaton with a state-based acceptance condition.
|
|
422
|
+
|
|
423
|
+
An empty ``accepting_states`` set is valid and represents the empty
|
|
424
|
+
language for both finite and Büchi automata.
|
|
425
|
+
"""
|
|
426
|
+
states = set(states)
|
|
427
|
+
alphabet = set(alphabet)
|
|
428
|
+
transitions = set(transitions)
|
|
429
|
+
initial_states = set(initial_states)
|
|
430
|
+
accepting_states = set(accepting_states)
|
|
431
|
+
|
|
432
|
+
if not accepting_states <= states:
|
|
433
|
+
raise ValueError(
|
|
434
|
+
f"Unknown accepting states: {accepting_states - states!r}"
|
|
435
|
+
)
|
|
436
|
+
|
|
437
|
+
automaton = super().from_components(
|
|
438
|
+
states=states,
|
|
439
|
+
alphabet=alphabet,
|
|
440
|
+
transitions=transitions,
|
|
441
|
+
initial_states=initial_states,
|
|
442
|
+
)
|
|
443
|
+
automaton._accepting_states = accepting_states
|
|
444
|
+
return automaton
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
class FiniteAutomaton(_StateAcceptanceAutomaton[AutomatonState, Symbol]):
|
|
448
|
+
"""An automaton with non-deterministic finite-word acceptance semantics.
|
|
449
|
+
|
|
450
|
+
A word is accepted exactly when at least one run starts in ``I``, consumes
|
|
451
|
+
every symbol in the word through ``δ``, and ends in ``F``. This models an
|
|
452
|
+
NFA directly; :attr:`is_deterministic` identifies the DFA subset.
|
|
453
|
+
An empty ``F`` is allowed and rejects every word, including the empty word.
|
|
454
|
+
"""
|
|
455
|
+
|
|
456
|
+
def accepts(self, word: Iterable[Symbol]) -> bool:
|
|
457
|
+
"""Return whether the automaton accepts a finite input word.
|
|
458
|
+
|
|
459
|
+
Symbols outside ``Σ`` cause rejection because they are not words over
|
|
460
|
+
the automaton's alphabet. The empty word is accepted precisely when an
|
|
461
|
+
initial state is accepting.
|
|
462
|
+
"""
|
|
463
|
+
current_states = set(self.initial_states)
|
|
464
|
+
|
|
465
|
+
for symbol in word:
|
|
466
|
+
if symbol not in self._alphabet:
|
|
467
|
+
return False
|
|
468
|
+
|
|
469
|
+
next_states: set[AutomatonState] = set()
|
|
470
|
+
for state in current_states:
|
|
471
|
+
next_states.update(self.post(state, symbol))
|
|
472
|
+
current_states = next_states
|
|
473
|
+
|
|
474
|
+
if not current_states:
|
|
475
|
+
return False
|
|
476
|
+
|
|
477
|
+
return bool(current_states & self._accepting_states)
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
class NBA(_StateAcceptanceAutomaton[AutomatonState, Symbol]):
|
|
481
|
+
"""A nondeterministic Büchi automaton over infinite words.
|
|
482
|
+
|
|
483
|
+
An NBA accepts an infinite word when at least one run starts in ``I`` and
|
|
484
|
+
visits states in ``F`` infinitely often. The common automaton components
|
|
485
|
+
retain the standard tuple ``(Q, Σ, δ, I, F)``; only the interpretation of
|
|
486
|
+
``F`` differs from :class:`FiniteAutomaton`.
|
|
487
|
+
An empty ``F`` is allowed and rejects every infinite word.
|
|
488
|
+
|
|
489
|
+
This class represents the acceptance condition only. Algorithms decide
|
|
490
|
+
language acceptance and model checking questions outside the domain model.
|
|
491
|
+
"""
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
class GNBA(Automaton[AutomatonState, Symbol]):
|
|
495
|
+
"""A generalized nondeterministic Büchi automaton.
|
|
496
|
+
|
|
497
|
+
A GNBA is the tuple ``(Q, Σ, δ, I, 𝓕)``. Its acceptance condition is a
|
|
498
|
+
finite family ``𝓕`` of state sets. An infinite run is accepting when it
|
|
499
|
+
visits every member of ``𝓕`` infinitely often. This class stores that
|
|
500
|
+
condition; it intentionally does not implement an acceptance algorithm.
|
|
501
|
+
"""
|
|
502
|
+
|
|
503
|
+
def __init__(self) -> None:
|
|
504
|
+
"""Create a GNBA with empty graph components and acceptance family."""
|
|
505
|
+
super().__init__()
|
|
506
|
+
self._accepting_sets: set[frozenset[AutomatonState]] = set()
|
|
507
|
+
|
|
508
|
+
@property
|
|
509
|
+
def accepting_sets(self) -> frozenset[frozenset[AutomatonState]]:
|
|
510
|
+
"""Return the generalized Büchi acceptance condition ``𝓕 ⊆ 2^Q``."""
|
|
511
|
+
return frozenset(self._accepting_sets)
|
|
512
|
+
|
|
513
|
+
def add_accepting_set(
|
|
514
|
+
self,
|
|
515
|
+
states: Iterable[AutomatonState],
|
|
516
|
+
) -> None:
|
|
517
|
+
"""Add a state set to the generalized acceptance condition.
|
|
518
|
+
|
|
519
|
+
Empty sets are allowed: they represent an unsatisfiable acceptance
|
|
520
|
+
requirement. Duplicate sets do not change the condition.
|
|
521
|
+
|
|
522
|
+
Raises:
|
|
523
|
+
ValueError: If a supplied state is unknown.
|
|
524
|
+
"""
|
|
525
|
+
accepting_set = frozenset(states)
|
|
526
|
+
unknown_states = accepting_set - self._states
|
|
527
|
+
if unknown_states:
|
|
528
|
+
raise ValueError(f"Unknown accepting states: {unknown_states!r}")
|
|
529
|
+
|
|
530
|
+
self._accepting_sets.add(accepting_set)
|
|
531
|
+
|
|
532
|
+
def remove_accepting_set(
|
|
533
|
+
self,
|
|
534
|
+
states: Iterable[AutomatonState],
|
|
535
|
+
) -> None:
|
|
536
|
+
"""Remove an existing member of the generalized acceptance condition.
|
|
537
|
+
|
|
538
|
+
Raises:
|
|
539
|
+
ValueError: If the supplied state set is not a member of ``𝓕``.
|
|
540
|
+
"""
|
|
541
|
+
accepting_set = frozenset(states)
|
|
542
|
+
if accepting_set not in self._accepting_sets:
|
|
543
|
+
raise ValueError(f"Unknown accepting set: {accepting_set!r}")
|
|
544
|
+
|
|
545
|
+
self._accepting_sets.remove(accepting_set)
|
|
546
|
+
|
|
547
|
+
def remove_state(self, state: AutomatonState) -> None:
|
|
548
|
+
"""Remove a state and delete it from every member of ``𝓕``."""
|
|
549
|
+
super().remove_state(state)
|
|
550
|
+
self._accepting_sets = {
|
|
551
|
+
accepting_set - {state}
|
|
552
|
+
for accepting_set in self._accepting_sets
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
@classmethod
|
|
556
|
+
def from_components(
|
|
557
|
+
cls,
|
|
558
|
+
*,
|
|
559
|
+
states: Iterable[AutomatonState],
|
|
560
|
+
alphabet: Iterable[Symbol],
|
|
561
|
+
transitions: Iterable[tuple[AutomatonState, Symbol, AutomatonState]],
|
|
562
|
+
initial_states: Iterable[AutomatonState] = (),
|
|
563
|
+
accepting_sets: Iterable[Iterable[AutomatonState]] = (),
|
|
564
|
+
) -> Self:
|
|
565
|
+
"""Construct a GNBA from its explicit tuple components.
|
|
566
|
+
|
|
567
|
+
Every state in each acceptance set must be declared in ``Q``. The
|
|
568
|
+
empty set is a valid member of ``𝓕`` and makes acceptance impossible.
|
|
569
|
+
|
|
570
|
+
Raises:
|
|
571
|
+
ValueError: If any tuple component refers to an undeclared state
|
|
572
|
+
or symbol.
|
|
573
|
+
"""
|
|
574
|
+
accepting_sets = tuple(
|
|
575
|
+
frozenset(accepting_set)
|
|
576
|
+
for accepting_set in accepting_sets
|
|
577
|
+
)
|
|
578
|
+
automaton = super().from_components(
|
|
579
|
+
states=states,
|
|
580
|
+
alphabet=alphabet,
|
|
581
|
+
transitions=transitions,
|
|
582
|
+
initial_states=initial_states,
|
|
583
|
+
)
|
|
584
|
+
for accepting_set in accepting_sets:
|
|
585
|
+
automaton.add_accepting_set(accepting_set)
|
|
586
|
+
|
|
587
|
+
return automaton
|
|
588
|
+
|
|
589
|
+
|
|
590
|
+
NondeterministicBuchiAutomaton = NBA
|
|
591
|
+
GeneralizedNondeterministicBuchiAutomaton = GNBA
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
__all__ = [
|
|
595
|
+
"Automaton",
|
|
596
|
+
"AutomatonState",
|
|
597
|
+
"AutomatonTransition",
|
|
598
|
+
"FiniteAutomaton",
|
|
599
|
+
"GNBA",
|
|
600
|
+
"GeneralizedNondeterministicBuchiAutomaton",
|
|
601
|
+
"NBA",
|
|
602
|
+
"NondeterministicBuchiAutomaton",
|
|
603
|
+
"Symbol",
|
|
604
|
+
]
|