1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//! Dynamic dispatch for automaton blueprints with heterogeneous state types.
//!
//! This module provides dyn-compatible traits that enable runtime polymorphism over
//! automata with different internal state structures while working within the same
//! formal language context (same alphabet, state classification, and error handling).
//!
//! # The State Type Erasure Problem
//!
//! The `State` associated type is inherently part of each automaton's structural
//! definition - different automaton implementations use fundamentally different
//! state representations (integers, enums, complex structs, etc.). This makes
//! the main automaton traits incompatible with `dyn` trait objects.
//!
//! However, when working with formal languages, we typically operate within
//! consistent contexts where the `Alphabet`, `StateSort`, and `ErrorType` remain
//! the same across different automaton implementations.
//!
//! # Solution: State-Focused Erasure
//!
//! This module provides companion traits that erase only the `State` type through
//! dynamic dispatch, while keeping other types as concrete generic parameters:
//!
//! - **[`ErasedAutomatonBlueprint`]**: Dyn-compatible blueprint trait
//! - **[`ErasedAutomaton`]**: Dyn-compatible runtime automaton trait
//! - **Universal Coverage**: All mutation automata (including deterministic ones) work seamlessly
//!
//! # Example: Heterogeneous State Types in Same Language Context
//!
//! ```
//! use deterministic_automata::{BasicStateSort, DeterministicAutomatonBlueprint, MutationAutomatonBlueprint, DynamicAutomatonBlueprint};
//!
//! # struct CounterAutomaton;
//! # impl DeterministicAutomatonBlueprint for CounterAutomaton {
//! # type State = i32; type Alphabet = char; type StateSort = BasicStateSort; type ErrorType = String;
//! # fn initial_state(&self) -> Self::State { 0 }
//! # fn state_sort_map(&self, _: &Self::State) -> Result<Self::StateSort, Self::ErrorType> { Ok(BasicStateSort::Accept) }
//! # fn transition_map(&self, state: &Self::State, _: &Self::Alphabet) -> Result<Self::State, Self::ErrorType> { Ok(*state) }
//! # }
//! #
//! # #[derive(Clone)] enum PatternState { Start, Found }
//! # struct PatternAutomaton;
//! # impl DeterministicAutomatonBlueprint for PatternAutomaton {
//! # type State = PatternState; type Alphabet = char; type StateSort = BasicStateSort; type ErrorType = String;
//! # fn initial_state(&self) -> Self::State { PatternState::Start }
//! # fn state_sort_map(&self, _: &Self::State) -> Result<Self::StateSort, Self::ErrorType> { Ok(BasicStateSort::Accept) }
//! # fn transition_map(&self, state: &Self::State, _: &Self::Alphabet) -> Result<Self::State, Self::ErrorType> { Ok(state.clone()) }
//! # }
//!
//! let counter = CounterAutomaton; // Uses i32 state for counting
//! let pattern = PatternAutomaton; // Uses enum state for pattern matching
//!
//! // Same language context: char alphabet, BasicStateSort classification, String errors
//! let automata: Vec<&DynamicAutomatonBlueprint<char, BasicStateSort, String>> = vec![
//! &counter, // Different state structure, same language interface
//! &pattern, // Different state structure, same language interface
//! ];
//!
//! // Dynamic dispatch over different state implementations
//! for automaton in automata {
//! let result = automaton.characterise(&['a', 'b']);
//! // Each uses its own state structure internally
//! }
//! ```
use crate::;
/// A dyn-compatible blueprint for defining automata with erased state types.
///
/// This trait enables dynamic dispatch over automata with different internal state
/// structures while maintaining compile-time knowledge of alphabet, state classification,
/// and error types. It serves as a companion to the main automaton blueprint traits,
/// solving the dyn compatibility problem by using generic type parameters instead
/// of associated types.
///
/// # Associated Types
///
/// * `Alphabet` - The type of input symbols. Must support equality comparison.
/// * `StateSort` - The classification type for states (e.g., Accept/Reject).
/// * `ErrorType` - The type used for error handling when states are invalid.
///
/// # Required Methods
///
/// * [`automaton`](Self::automaton) - Creates a runtime automaton instance with erased state
/// * [`characterise`](Self::characterise) - Processes an entire input sequence
///
/// # Universal Implementation
///
/// All types implementing [`MutationAutomatonBlueprint`] (including deterministic
/// automata via blanket implementation) automatically implement this trait,
/// providing seamless interoperability between different automaton paradigms.
/// A dyn-compatible runtime instance of an automaton with erased state type.
///
/// This trait represents an automaton in execution, maintaining the current state
/// internally while providing methods to process input symbols one at a time.
/// The specific state type is erased, enabling dynamic dispatch over automata
/// with different state representations.
///
/// # Associated Types
///
/// * `Alphabet` - The type of input symbols that the automaton processes.
/// * `StateSort` - The classification type for states (e.g., Accept/Reject).
/// * `ErrorType` - The type used for error handling when states are invalid.
///
/// # Required Methods
///
/// * [`update_state`](Self::update_state) - Processes a single input symbol
/// * [`current_state_sort`](Self::current_state_sort) - Returns the current state classification
///
/// # Provided Methods
///
/// * [`update_sort_state`](Self::update_sort_state) - Updates state and returns classification in one call
pub type DynamicAutomatonBlueprint<Alphabet,StateSort,ErrorType> = dyn ;
pub type DynamicAutomaton<'a,Alphabet,StateSort,ErrorType> = dyn ;