mocha-model-checking 0.1.0rc1__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.
Files changed (41) hide show
  1. mocha/__init__.py +199 -0
  2. mocha/algorithms/__init__.py +159 -0
  3. mocha/algorithms/automata.py +205 -0
  4. mocha/algorithms/invariance.py +166 -0
  5. mocha/algorithms/ltl2gnba/__init__.py +75 -0
  6. mocha/algorithms/ltl2gnba/acceptance.py +199 -0
  7. mocha/algorithms/ltl2gnba/elementary_set_enumeration.py +360 -0
  8. mocha/algorithms/ltl2gnba/elementary_set_validation.py +312 -0
  9. mocha/algorithms/ltl2gnba/initial_states.py +116 -0
  10. mocha/algorithms/ltl2gnba/pipeline.py +137 -0
  11. mocha/algorithms/ltl2gnba/transitions.py +327 -0
  12. mocha/algorithms/ltl_model_checking.py +313 -0
  13. mocha/algorithms/ltl_satisfiability_validity.py +308 -0
  14. mocha/algorithms/ltl_transformations.py +291 -0
  15. mocha/algorithms/persistence.py +259 -0
  16. mocha/algorithms/reachability.py +143 -0
  17. mocha/algorithms/trace.py +22 -0
  18. mocha/io/__init__.py +20 -0
  19. mocha/io/ltl.py +195 -0
  20. mocha/io/schemas/__init__.py +1 -0
  21. mocha/io/schemas/transition-system.schema.json +95 -0
  22. mocha/io/transition_system.py +136 -0
  23. mocha/logic/__init__.py +5 -0
  24. mocha/logic/_syntax.py +8 -0
  25. mocha/logic/ltl.py +179 -0
  26. mocha/models/__init__.py +37 -0
  27. mocha/models/automaton.py +604 -0
  28. mocha/models/transition_system.py +535 -0
  29. mocha/py.typed +0 -0
  30. mocha/visualisation/__init__.py +55 -0
  31. mocha/visualisation/automaton.py +170 -0
  32. mocha/visualisation/invariance.py +84 -0
  33. mocha/visualisation/overlay.py +24 -0
  34. mocha/visualisation/persistence.py +48 -0
  35. mocha/visualisation/reachability.py +35 -0
  36. mocha/visualisation/transition_system.py +252 -0
  37. mocha_model_checking-0.1.0rc1.dist-info/METADATA +559 -0
  38. mocha_model_checking-0.1.0rc1.dist-info/RECORD +41 -0
  39. mocha_model_checking-0.1.0rc1.dist-info/WHEEL +5 -0
  40. mocha_model_checking-0.1.0rc1.dist-info/licenses/LICENCE +340 -0
  41. mocha_model_checking-0.1.0rc1.dist-info/top_level.txt +1 -0
mocha/__init__.py ADDED
@@ -0,0 +1,199 @@
1
+ """Public API for the Model Checking Assistant."""
2
+
3
+ from .algorithms import (
4
+ AlgorithmTrace,
5
+ InvariantResult,
6
+ InvariantStep,
7
+ LTLRewriteStep,
8
+ PersistenceResult,
9
+ PersistenceStep,
10
+ ReachabilityResult,
11
+ ReachabilityStep,
12
+ TransitionSystemOverlay,
13
+ check_invariant,
14
+ check_persistence,
15
+ compute_reachable_states,
16
+ reduce_to_reachable,
17
+ cycle_check,
18
+ gnba_to_nba,
19
+ normalize_ltl,
20
+ to_release_pnf,
21
+ to_weak_until_pnf,
22
+ trace_invariant,
23
+ trace_normalize_ltl,
24
+ trace_persistence,
25
+ trace_reachability,
26
+ trace_release_pnf,
27
+ trace_weak_until_pnf,
28
+ transition_system_automaton_product,
29
+ )
30
+ from .io import (
31
+ LTLParseError,
32
+ from_dict,
33
+ load_transition_system,
34
+ parse_ltl,
35
+ save_transition_system,
36
+ to_dict,
37
+ validate_dict,
38
+ )
39
+ from .algorithms import (
40
+ AcceptanceCondition,
41
+ AcceptanceDecision,
42
+ AcceptanceResult,
43
+ AcceptanceStep,
44
+ compute_acceptance_sets,
45
+ trace_acceptance_sets,
46
+ InitialStateStep,
47
+ InitialStatesResult,
48
+ compute_initial_states,
49
+ trace_initial_states,
50
+ LTLToGNBAResult,
51
+ LTLToGNBAStep,
52
+ ltl_to_gnba,
53
+ trace_ltl_to_gnba,
54
+ TransitionRestriction,
55
+ TransitionRelationResult,
56
+ TransitionRelationStep,
57
+ TransitionRelationTable,
58
+ compute_transition_relation,
59
+ trace_transition_relation,
60
+ ElementarySetEnumeration,
61
+ ElementarySetResult,
62
+ ElementarySetViolation,
63
+ ElementaryTable,
64
+ ElementaryTableChange,
65
+ ElementaryTableRow,
66
+ ElementaryTableStep,
67
+ check_elementary_set,
68
+ ElementarySetCollectionResult,
69
+ check_elementary_set_collection,
70
+ validate_elementary_set_collection,
71
+ enumerate_elementary_sets,
72
+ trace_elementary_sets,
73
+ LTLSatisfiabilityResult,
74
+ LTLSatisfiabilityStep,
75
+ LTLValidityResult,
76
+ LTLValidityStep,
77
+ check_ltl_satisfiability,
78
+ check_ltl_validity,
79
+ trace_ltl_satisfiability,
80
+ trace_ltl_validity,
81
+ LTLModelCheckingResult,
82
+ LTLModelCheckingStep,
83
+ check_ltl,
84
+ trace_check_ltl,
85
+ )
86
+
87
+ from .logic import ltl
88
+ from .models import (
89
+ Action,
90
+ AtomicProposition,
91
+ Automaton,
92
+ AutomatonState,
93
+ AutomatonTransition,
94
+ FiniteAutomaton,
95
+ GNBA,
96
+ GeneralizedNondeterministicBuchiAutomaton,
97
+ NBA,
98
+ NondeterministicBuchiAutomaton,
99
+ State,
100
+ Symbol,
101
+ Transition,
102
+ TransitionSystem,
103
+ )
104
+
105
+
106
+ __all__ = [
107
+ "Action",
108
+ "AlgorithmTrace",
109
+ "AtomicProposition",
110
+ "Automaton",
111
+ "AutomatonState",
112
+ "AutomatonTransition",
113
+ "FiniteAutomaton",
114
+ "GNBA",
115
+ "GeneralizedNondeterministicBuchiAutomaton",
116
+ "InvariantResult",
117
+ "InvariantStep",
118
+ "LTLParseError",
119
+ "LTLRewriteStep",
120
+ "NBA",
121
+ "NondeterministicBuchiAutomaton",
122
+ "PersistenceResult",
123
+ "PersistenceStep",
124
+ "ReachabilityResult",
125
+ "ReachabilityStep",
126
+ "State",
127
+ "Symbol",
128
+ "Transition",
129
+ "TransitionSystem",
130
+ "TransitionSystemOverlay",
131
+ "check_invariant",
132
+ "check_persistence",
133
+ "compute_reachable_states",
134
+ "reduce_to_reachable",
135
+ "cycle_check",
136
+ "from_dict",
137
+ "gnba_to_nba",
138
+ "load_transition_system",
139
+ "ltl",
140
+ "normalize_ltl",
141
+ "parse_ltl",
142
+ "save_transition_system",
143
+ "to_dict",
144
+ "to_release_pnf",
145
+ "to_weak_until_pnf",
146
+ "trace_invariant",
147
+ "trace_normalize_ltl",
148
+ "trace_persistence",
149
+ "trace_reachability",
150
+ "trace_release_pnf",
151
+ "trace_weak_until_pnf",
152
+ "AcceptanceCondition",
153
+ "AcceptanceDecision",
154
+ "AcceptanceResult",
155
+ "AcceptanceStep",
156
+ "compute_acceptance_sets",
157
+ "trace_acceptance_sets",
158
+ "InitialStateStep",
159
+ "InitialStatesResult",
160
+ "compute_initial_states",
161
+ "trace_initial_states",
162
+ "LTLToGNBAResult",
163
+ "LTLToGNBAStep",
164
+ "ltl_to_gnba",
165
+ "trace_ltl_to_gnba",
166
+ "TransitionRestriction",
167
+ "TransitionRelationResult",
168
+ "TransitionRelationStep",
169
+ "TransitionRelationTable",
170
+ "compute_transition_relation",
171
+ "trace_transition_relation",
172
+ "ElementarySetEnumeration",
173
+ "ElementarySetResult",
174
+ "ElementarySetViolation",
175
+ "ElementaryTable",
176
+ "ElementaryTableChange",
177
+ "ElementaryTableRow",
178
+ "ElementaryTableStep",
179
+ "check_elementary_set",
180
+ "ElementarySetCollectionResult",
181
+ "check_elementary_set_collection",
182
+ "validate_elementary_set_collection",
183
+ "enumerate_elementary_sets",
184
+ "trace_elementary_sets",
185
+ "LTLSatisfiabilityResult",
186
+ "LTLSatisfiabilityStep",
187
+ "LTLValidityResult",
188
+ "LTLValidityStep",
189
+ "check_ltl_satisfiability",
190
+ "check_ltl_validity",
191
+ "trace_ltl_satisfiability",
192
+ "trace_ltl_validity",
193
+ "LTLModelCheckingResult",
194
+ "LTLModelCheckingStep",
195
+ "check_ltl",
196
+ "trace_check_ltl",
197
+ "transition_system_automaton_product",
198
+ "validate_dict",
199
+ ]
@@ -0,0 +1,159 @@
1
+ """Algorithms and traces for model checking and formula transformations."""
2
+
3
+ from mocha.visualisation.overlay import TransitionSystemOverlay
4
+ from .automata import gnba_to_nba, transition_system_automaton_product
5
+ from .invariance import (
6
+ InvariantResult,
7
+ InvariantStep,
8
+ check_invariant,
9
+ trace_invariant,
10
+ )
11
+ from .ltl_transformations import (
12
+ LTLRewriteStep,
13
+ normalize_ltl,
14
+ to_release_pnf,
15
+ to_weak_until_pnf,
16
+ trace_normalize_ltl,
17
+ trace_release_pnf,
18
+ trace_weak_until_pnf,
19
+ )
20
+ from .persistence import (
21
+ PersistenceResult,
22
+ PersistenceStep,
23
+ check_persistence,
24
+ cycle_check,
25
+ trace_persistence,
26
+ )
27
+ from .reachability import (
28
+ ReachabilityResult,
29
+ ReachabilityStep,
30
+ compute_reachable_states,
31
+ reduce_to_reachable,
32
+ trace_reachability,
33
+ )
34
+ from .ltl2gnba import (
35
+ AcceptanceCondition,
36
+ AcceptanceDecision,
37
+ AcceptanceResult,
38
+ AcceptanceStep,
39
+ compute_acceptance_sets,
40
+ trace_acceptance_sets,
41
+ InitialStateStep,
42
+ InitialStatesResult,
43
+ compute_initial_states,
44
+ trace_initial_states,
45
+ LTLToGNBAResult,
46
+ LTLToGNBAStep,
47
+ ltl_to_gnba,
48
+ trace_ltl_to_gnba,
49
+ TransitionRestriction,
50
+ TransitionRelationResult,
51
+ TransitionRelationStep,
52
+ TransitionRelationTable,
53
+ compute_transition_relation,
54
+ trace_transition_relation,
55
+ ElementarySetEnumeration,
56
+ ElementarySetResult,
57
+ ElementarySetViolation,
58
+ ElementaryTable,
59
+ ElementaryTableChange,
60
+ ElementaryTableRow,
61
+ ElementaryTableStep,
62
+ check_elementary_set,
63
+ ElementarySetCollectionResult,
64
+ check_elementary_set_collection,
65
+ validate_elementary_set_collection,
66
+ enumerate_elementary_sets,
67
+ trace_elementary_sets,
68
+ )
69
+ from .ltl_satisfiability_validity import (
70
+ LTLSatisfiabilityResult,
71
+ LTLSatisfiabilityStep,
72
+ LTLValidityResult,
73
+ LTLValidityStep,
74
+ check_ltl_satisfiability,
75
+ check_ltl_validity,
76
+ trace_ltl_satisfiability,
77
+ trace_ltl_validity,
78
+ )
79
+ from .ltl_model_checking import (
80
+ LTLModelCheckingResult,
81
+ LTLModelCheckingStep,
82
+ check_ltl,
83
+ trace_check_ltl,
84
+ )
85
+
86
+ from .trace import AlgorithmTrace
87
+
88
+ __all__ = [
89
+ "AlgorithmTrace",
90
+ "InvariantResult",
91
+ "InvariantStep",
92
+ "LTLRewriteStep",
93
+ "PersistenceResult",
94
+ "PersistenceStep",
95
+ "ReachabilityResult",
96
+ "ReachabilityStep",
97
+ "TransitionSystemOverlay",
98
+ "check_invariant",
99
+ "check_persistence",
100
+ "compute_reachable_states",
101
+ "reduce_to_reachable",
102
+ "cycle_check",
103
+ "gnba_to_nba",
104
+ "normalize_ltl",
105
+ "to_release_pnf",
106
+ "to_weak_until_pnf",
107
+ "trace_invariant",
108
+ "trace_normalize_ltl",
109
+ "trace_persistence",
110
+ "trace_reachability",
111
+ "trace_release_pnf",
112
+ "trace_weak_until_pnf",
113
+ "AcceptanceCondition",
114
+ "AcceptanceDecision",
115
+ "AcceptanceResult",
116
+ "AcceptanceStep",
117
+ "compute_acceptance_sets",
118
+ "trace_acceptance_sets",
119
+ "InitialStateStep",
120
+ "InitialStatesResult",
121
+ "compute_initial_states",
122
+ "trace_initial_states",
123
+ "LTLToGNBAResult",
124
+ "LTLToGNBAStep",
125
+ "ltl_to_gnba",
126
+ "trace_ltl_to_gnba",
127
+ "TransitionRestriction",
128
+ "TransitionRelationResult",
129
+ "TransitionRelationStep",
130
+ "TransitionRelationTable",
131
+ "compute_transition_relation",
132
+ "trace_transition_relation",
133
+ "ElementarySetEnumeration",
134
+ "ElementarySetResult",
135
+ "ElementarySetViolation",
136
+ "ElementaryTable",
137
+ "ElementaryTableChange",
138
+ "ElementaryTableRow",
139
+ "ElementaryTableStep",
140
+ "check_elementary_set",
141
+ "ElementarySetCollectionResult",
142
+ "check_elementary_set_collection",
143
+ "validate_elementary_set_collection",
144
+ "enumerate_elementary_sets",
145
+ "trace_elementary_sets",
146
+ "LTLSatisfiabilityResult",
147
+ "LTLSatisfiabilityStep",
148
+ "LTLValidityResult",
149
+ "LTLValidityStep",
150
+ "check_ltl_satisfiability",
151
+ "check_ltl_validity",
152
+ "trace_ltl_satisfiability",
153
+ "trace_ltl_validity",
154
+ "LTLModelCheckingResult",
155
+ "LTLModelCheckingStep",
156
+ "check_ltl",
157
+ "trace_check_ltl",
158
+ "transition_system_automaton_product",
159
+ ]
@@ -0,0 +1,205 @@
1
+ """Algorithms for transforming and analysing automata."""
2
+
3
+ from itertools import combinations
4
+ from typing import Hashable, TypeVar
5
+
6
+ from mocha.models.automaton import FiniteAutomaton, GNBA, NBA
7
+ from mocha.models.transition_system import TransitionSystem
8
+
9
+ AutomatonState = TypeVar("AutomatonState", bound=Hashable)
10
+ Symbol = TypeVar("Symbol", bound=Hashable)
11
+ State = TypeVar("State", bound=Hashable)
12
+ Action = TypeVar("Action", bound=Hashable)
13
+ AtomicProposition = TypeVar("AtomicProposition", bound=Hashable)
14
+
15
+ ProductState = tuple[State, AutomatonState]
16
+
17
+
18
+ def gnba_to_nba(
19
+ gnba: GNBA[AutomatonState, Symbol],
20
+ ) -> NBA[tuple[AutomatonState, int], Symbol]:
21
+ """Construct an NBA equivalent to ``gnba``.
22
+
23
+ The construction augments every GNBA state with a round-robin counter and
24
+ chooses an arbitrary enumeration
25
+ ``𝓕 = {F₁, …, Fₖ}``. Counter ``i`` means that the run is waiting to visit
26
+ ``Fᵢ``. A transition out of a state in ``Fᵢ`` advances the counter to the
27
+ next acceptance set. The NBA accepts states whose counter is one and whose
28
+ GNBA component belongs to ``F₁``; visiting them infinitely often requires
29
+ every acceptance set to be visited infinitely often. Any enumeration of
30
+ ``𝓕`` produces an equivalent NBA.
31
+
32
+ When ``𝓕`` is empty, its generalized Büchi condition is vacuously true.
33
+ The result therefore has one counter value and marks every state as
34
+ accepting.
35
+
36
+ Args:
37
+ gnba: The generalized nondeterministic Büchi automaton to convert.
38
+
39
+ Returns:
40
+ An equivalent nondeterministic Büchi automaton. Its states are pairs
41
+ ``(state, index)`` where ``index`` identifies the next acceptance set.
42
+ """
43
+ accepting_sets = tuple(gnba.accepting_sets)
44
+ condition_count = len(accepting_sets)
45
+ counter_count = max(condition_count, 1)
46
+
47
+ states = {
48
+ (state, index)
49
+ for state in gnba.states
50
+ for index in range(1, counter_count + 1)
51
+ }
52
+ initial_states = {
53
+ (state, 1)
54
+ for state in gnba.initial_states
55
+ }
56
+
57
+ transitions: set[
58
+ tuple[tuple[AutomatonState, int], Symbol, tuple[AutomatonState, int]]
59
+ ] = set()
60
+ for transition in gnba.transitions():
61
+ for index in range(1, counter_count + 1):
62
+ if condition_count and transition.source in accepting_sets[index - 1]:
63
+ next_index = index % condition_count + 1
64
+ else:
65
+ next_index = index
66
+
67
+ transitions.add(
68
+ (
69
+ (transition.source, index),
70
+ transition.symbol,
71
+ (transition.target, next_index),
72
+ )
73
+ )
74
+
75
+ if condition_count:
76
+ accepting_states = {
77
+ (state, 1)
78
+ for state in accepting_sets[0]
79
+ }
80
+ else:
81
+ accepting_states = states
82
+
83
+ return NBA.from_components(
84
+ states=states,
85
+ alphabet=gnba.alphabet,
86
+ transitions=transitions,
87
+ initial_states=initial_states,
88
+ accepting_states=accepting_states,
89
+ )
90
+
91
+
92
+ def _valuations(
93
+ propositions: frozenset[AtomicProposition],
94
+ ) -> frozenset[frozenset[AtomicProposition]]:
95
+ """Return the powerset ``2^propositions`` as state-label valuations."""
96
+ proposition_list = tuple(propositions)
97
+ return frozenset(
98
+ frozenset(subset)
99
+ for size in range(len(proposition_list) + 1)
100
+ for subset in combinations(proposition_list, size)
101
+ )
102
+
103
+
104
+ def transition_system_automaton_product(
105
+ ts: TransitionSystem[State, Action, AtomicProposition],
106
+ automaton: (
107
+ FiniteAutomaton[AutomatonState, frozenset[AtomicProposition]]
108
+ | NBA[AutomatonState, frozenset[AtomicProposition]]
109
+ ),
110
+ ) -> TransitionSystem[
111
+ ProductState[State, AutomatonState],
112
+ Action,
113
+ AutomatonState,
114
+ ]:
115
+ r"""Construct the standard product of ``ts`` and a finite automaton or NBA.
116
+
117
+ The automaton alphabet must be exactly ``2^AP``, where ``AP`` is the
118
+ transition system's atomic-proposition set. Product states are ``(s, q)``.
119
+ To align a run with its trace, initial states consume ``L(s)`` from an
120
+ automaton initial state, and a product transition consumes the label of its
121
+ target system state:
122
+
123
+ .. math::
124
+
125
+ (s, q) \xrightarrow{a} (s', q')
126
+ \quad\text{iff}\quad
127
+ s \xrightarrow{a} s'
128
+ \ \text{and}\ q \xrightarrow{L(s')} q'.
129
+
130
+ The product has atomic-proposition set ``Q`` and labels every product state
131
+ with its automaton component: ``L'(s, q) = {q}``. For an NBA, a product
132
+ state ``(s, q)`` is accepting exactly when ``q`` is a member of
133
+ ``automaton.accepting_states``.
134
+
135
+ Args:
136
+ ts: The labelled transition system.
137
+ automaton: A finite automaton or NBA over the valuations ``2^AP``.
138
+
139
+ Returns:
140
+ The product transition system over ``S × Q``, labelled by ``Q``.
141
+
142
+ Raises:
143
+ ValueError: If the automaton alphabet differs from ``2^AP``.
144
+ """
145
+ expected_alphabet = _valuations(ts.atomic_propositions)
146
+ if automaton.alphabet != expected_alphabet:
147
+ raise ValueError(
148
+ "Automaton alphabet must equal the transition system's "
149
+ f"valuation set 2^AP: expected {expected_alphabet!r}, "
150
+ f"got {automaton.alphabet!r}"
151
+ )
152
+
153
+ product_states = {
154
+ (state, automaton_state)
155
+ for state in ts.states
156
+ for automaton_state in automaton.states
157
+ }
158
+ initial_states = {
159
+ (state, automaton_state)
160
+ for state in ts.initial_states
161
+ for initial_automaton_state in automaton.initial_states
162
+ for automaton_state in automaton.post(
163
+ initial_automaton_state,
164
+ ts.labels(state),
165
+ )
166
+ }
167
+ transitions: set[
168
+ tuple[
169
+ ProductState[State, AutomatonState],
170
+ Action,
171
+ ProductState[State, AutomatonState],
172
+ ]
173
+ ] = set()
174
+
175
+ for transition in ts.transitions():
176
+ for automaton_state in automaton.states:
177
+ for next_automaton_state in automaton.post(
178
+ automaton_state,
179
+ ts.labels(transition.target),
180
+ ):
181
+ transitions.add(
182
+ (
183
+ (transition.source, automaton_state),
184
+ transition.action,
185
+ (transition.target, next_automaton_state),
186
+ )
187
+ )
188
+
189
+ return TransitionSystem.from_components(
190
+ states=product_states,
191
+ actions=ts.actions,
192
+ transitions=transitions,
193
+ initial_states=initial_states,
194
+ atomic_propositions=automaton.states,
195
+ labels={
196
+ (state, automaton_state): {automaton_state}
197
+ for state, automaton_state in product_states
198
+ },
199
+ )
200
+
201
+
202
+ __all__ = [
203
+ "gnba_to_nba",
204
+ "transition_system_automaton_product",
205
+ ]