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
//! Example automaton that recognizes the context-free language a^n b^n.
//!
//! This module provides a concrete example of how the deterministic automata framework
//! can handle languages beyond regular languages by using states that carry additional
//! data (counters). The [`CounterAutomatonBlueprint`] demonstrates recognition of the
//! context-free language a^n b^n, which cannot be recognized by traditional finite
//! state automata.
//!
//! # The Language a^n b^n
//!
//! The language a^n b^n consists of strings with:
//! - Zero or more occurrences of symbol 'a'
//! - Followed by exactly the same number of occurrences of symbol 'b'
//! - For any n ≥ 0
//!
//! Examples of strings in this language:
//! - `""` (empty string, n=0)
//! - `"ab"` (n=1)
//! - `"aabb"` (n=2)
//! - `"aaabbb"` (n=3)
//!
//! Examples of strings NOT in this language:
//! - `"a"` (unbalanced)
//! - `"ba"` (wrong order)
//! - `"aab"` (unequal counts)
//! - `"abab"` (interleaved)
//!
//! # Key Insight: Beyond Regular Languages
//!
//! This example is significant because it demonstrates how the framework can recognize
//! languages that are provably impossible for traditional finite state automata to
//! handle. The key insight is that states can carry arbitrary data - in this case,
//! a counter that tracks the balance between 'a' and 'b' symbols.
//!
//! # State Machine Design
//!
//! The automaton uses three types of states:
//! - [`CounterState::Start(n)`] - Reading 'a' symbols, counter tracks how many seen
//! - [`CounterState::End(n)`] - Reading 'b' symbols, counter tracks how many more needed
//! - [`CounterState::Reject`] - Invalid input detected
//!
//! The state space is theoretically infinite (counters can grow arbitrarily large),
//! but the automaton remains deterministic and efficiently processable.
//!
//! # Framework Benefits
//!
//! This example showcases several advantages of the framework:
//! - **Expressiveness**: Can handle non-regular languages
//! - **Determinism**: No backtracking or ambiguity in state transitions
//! - **Composability**: Can be combined with other automata using product operations
//! - **Type Safety**: Counter overflow could be caught at runtime depending on build configuration
use crate::;
/// A blueprint for an automaton that recognizes the language a^n b^n.
///
/// This automaton accepts strings consisting of n occurrences of a first symbol
/// followed by exactly n occurrences of a second symbol, for any n ≥ 0.
/// It demonstrates how the framework can handle context-free languages using
/// states that carry counter information.
/// The state type for the counter automaton.
///
/// This enum represents the different phases of processing input in the a^n b^n
/// language recognizer, with states carrying counter information.