dandy 0.1.10

Implementation of DFAs, NFAs and regular expressions together with a file format
Documentation
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
use crate::dfa::{Dfa, DfaState};
use crate::nfa::{Nfa, NfaState};
use crate::*;
use ::regex::Regex as LibRegex;
use proptest::prelude::*;
use rand::prelude::*;
use std::collections::HashSet;
use std::ops::RangeInclusive;
use std::rc::Rc;

struct MultipleCounterIter {
    state: Vec<usize>,
    len: usize,
    max_number: usize,
    has_returned: bool,
    is_finished: bool,
}

impl MultipleCounterIter {
    fn new(length: usize, max: usize) -> Self {
        MultipleCounterIter {
            state: vec![0; length],
            len: 0,
            max_number: max,
            has_returned: false,
            is_finished: false,
        }
    }
}

impl Iterator for MultipleCounterIter {
    type Item = Vec<usize>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.is_finished {
            return None;
        }

        if !self.has_returned {
            self.has_returned = true;
            return Some(vec![]);
        }

        for i in (0..self.len).rev() {
            self.state[i] += 1;
            if self.state[i] > self.max_number {
                self.state[i] = 0;
            } else {
                return Some(self.state[0..self.len].to_vec());
            }
        }

        self.len += 1;

        if self.len > self.state.len() {
            self.is_finished = true;
            None
        } else {
            Some(self.state[0..self.len].to_vec())
        }
    }
}

#[test]
fn test_subset_construction() {
    let dfa_source = include_str!("../tests/test_files/eq_to_nfa1.dfa");
    let parsed_dfa = parser::dfa(dfa_source).unwrap();
    let dfa: Dfa = parsed_dfa.try_into().unwrap();

    let nfa_source = include_str!("../tests/test_files/nfa1.nfa");
    let parsed_nfa = parser::nfa(nfa_source).unwrap();
    let nfa: Nfa = parsed_nfa.try_into().unwrap();

    let converted = nfa.to_dfa();
    assert!(dfa.equivalent_to(&converted));
}

proptest! {
    /// Tests that a DFA can be turned into a table with dfa.to_table() and then be
    /// parsed to the *very same* DFA again (not just equivalent)
    #[test]
    fn dfa_table_reparse(dfa in dfa(50, 50)) {
        let parsed_dfa: Dfa = parser::dfa(&dfa.to_table()).unwrap().try_into().unwrap();
        assert_eq!(dfa, parsed_dfa);
    }

    /// Tests that a DFA can be minimized and is then still equivalent to the original DFA
    #[test]
    fn dfa_minimize_eq(dfa in dfa(25, 25)) { // This size is adequate, larger size takes too long time
        let mut minimized_dfa = dfa.clone();
        minimized_dfa.minimize();
        assert!(minimized_dfa.equivalent_to(&dfa), "Minimized DFA should be equivalent to original");
        assert!(dfa.equivalent_to(&minimized_dfa), "Original DFA should be equivalent to original");
    }

    /// Tests that a DFA can be turned into an NFA and then turned back again to a DFA
    /// while still being equivalent to the original DFA
    #[test]
    fn dfa_to_nfa_to_dfa(dfa in dfa(50, 50)) {
        let converted = dfa.clone().to_nfa().to_dfa();
        assert!(dfa.equivalent_to(&converted), "DFA should be equivalent to DFA->NFA->DFA");
        assert!(converted.equivalent_to(&dfa), "DFA->NFA->DFA should be equivalent to DFA");
    }


    /// Tests that a NFA can be turned into a table with dfa.to_table() and then be
    /// parsed to the *very same* DFA again (not just equivalent)
    #[test]
    fn nfa_table_reparse(nfa in nfa(50, 50)) {
        let parsed_nfa: Nfa = parser::nfa(&nfa.to_table()).unwrap().try_into().unwrap();
        assert_eq!(nfa, parsed_nfa);
    }

    /// Tests that a NFA can be turned into an DFA and then turned back again to a NFA
    /// while still being equivalent to the original NFA
    #[test]
    fn nfa_to_dfa_to_nfa(nfa in nfa(25, 25)) {
        let converted = nfa.to_dfa().to_nfa();
        assert!(nfa.equivalent_to(&converted), "NFA should be equivalent to NFA->DFA->NFA");
        assert!(converted.equivalent_to(&nfa), "NFA->DFA->NFA should be equivalent to NFA");
    }

    #[test]
    fn dfa_binary_ops(
        dfa1 in fixed_alphabet_dfa(20, 'a'..='f', ('a'..='f').count()),
        dfa2 in fixed_alphabet_dfa(20, 'a'..='f', ('a'..='f').count()),
        tests in prop::collection::vec("[a-f]+", 100)
    ) {
        let intersection = dfa1.intersection(&dfa2).unwrap();
        let union = dfa1.union(&dfa2).unwrap();
        let difference = dfa1.difference(&dfa2).unwrap();
        let symmetric_difference = dfa1.symmetric_difference(&dfa2).unwrap();
        for test in tests.iter() {
            let r1 = dfa1.accepts_graphemes(test);
            let r2 = dfa2.accepts_graphemes(test);
            assert_eq!(intersection.accepts_graphemes(test), r1 && r2);
            assert_eq!(union.accepts_graphemes(test), r1 || r2);
            assert_eq!(difference.accepts_graphemes(test), r1 && !r2);
            assert_eq!(symmetric_difference.accepts_graphemes(test), r1 != r2);
        }
    }

    #[test]
    fn nfa_binary_ops(
        // This takes a really long time to run, so we reduce the size of NFAs tested and amount of test cases
        nfa1 in fixed_alphabet_nfa(8, 'a'..='f', ('a'..='f').count()),
        nfa2 in fixed_alphabet_nfa(8, 'a'..='f', ('a'..='f').count()),
        tests in prop::collection::vec("[a-f]+", 50)
    ) {
        let intersection = nfa1.intersection(&nfa2).unwrap();
        let union = nfa1.clone().union(nfa2.clone()).unwrap();
        for test in tests.iter() {
            let r1 = nfa1.accepts_graphemes(test);
            let r2 = nfa2.accepts_graphemes(test);
            assert_eq!(intersection.accepts_graphemes(test), r1 && r2);
            assert_eq!(union.accepts_graphemes(test), r1 || r2);
        }
    }

    #[test]
    fn dfa_self_union(dfa in fixed_alphabet_dfa(20, 'a'..='z', ('a'..='z').count())) {
        let union = dfa.union(&dfa).unwrap();
        assert!(union.equivalent_to(&dfa));
    }

    #[test]
    fn dfa_self_intersection(dfa in fixed_alphabet_dfa(20, 'a'..='z', ('a'..='z').count())) {
        let intersection = dfa.intersection(&dfa).unwrap();
        assert!(intersection.equivalent_to(&dfa));
    }

    #[test]
    fn dfa_inversion_tautologies(
        dfa in fixed_alphabet_dfa(20, 'a'..='f', ('a'..='f').count()),
        tests in prop::collection::vec("[a-f]+", 100)
    ) {
        // dfa OR !dfa should accept everything
        // dfa AND !dfa should accept nothing
        let inv_dfa = {
            let mut dfa = dfa.clone();
            dfa.invert();
            dfa
        };
        let union = dfa.union(&inv_dfa).unwrap();
        let intersection = dfa.intersection(&inv_dfa).unwrap();
        assert!(union.has_reachable_accepting_state());
        assert!(!intersection.has_reachable_accepting_state());
        tests.iter().for_each(|test| {
            assert!(union.accepts_graphemes(test));
            assert!(!intersection.accepts_graphemes(test));
        });
    }

    #[test]
    fn nfa_remove_epsilon_transitions(
        nfa in nfa(25, 25)
    ) {
        let mut no_eps = nfa.clone();
        no_eps.remove_epsilon_moves();
        assert!(nfa.equivalent_to(&no_eps));
        assert!(no_eps.states().iter().all(|s| s.epsilon_transitions.is_empty()));
        assert!(!no_eps.has_epsilon_moves());
    }

    #[test]
    fn nfa_remove_unreachable_states(
        nfa in nfa(25, 25)
    ) {
        let mut no_unr_states = nfa.clone();
        no_unr_states.remove_unreachable_states();
        assert!(nfa.equivalent_to(&no_unr_states));
    }

    #[test]
    fn nfa_words(
        dfa in fixed_alphabet_dfa(25, 'a'..='f', ('a'..='f').count())
    ) {
        let nfa = dfa.to_nfa();
        let mut no_eps = nfa.clone();
        no_eps.remove_epsilon_moves();

        let inverse = {
            let mut dfa = nfa.to_dfa();
            dfa.minimize();
            dfa.invert();
            let mut nfa = dfa.to_nfa();
            nfa.remove_epsilon_moves();
            nfa
        };

        no_eps.words().take(100).for_each(|word|{
            assert!(nfa.accepts_graphemes(&word));
            assert!(!inverse.accepts_graphemes(&word));
        });

        inverse.words().take(100).for_each(|word| {
            assert!(!nfa.accepts_graphemes(&word));
            assert!(inverse.accepts_graphemes(&word));
        });

        // This checks that merging both iter_nfa and iter_inv gives all words
        // and in lexicographic order. It only checks for words up to length 3
        // due to exponential growth. This also checks that the iterators doesn't
        // "skip" words or generates duplicate words since all words should be in
        // exactly one of the iterators.
        let mut iter = MultipleCounterIter::new(3, nfa.alphabet().len() - 1);
        let mut iter_nfa = nfa.word_component_indices();
        let mut next_nfa = iter_nfa.next();
        let mut iter_inv = inverse.word_component_indices();
        let mut next_inv = iter_inv.next();
        while let Some(word) = iter.next() {
            if next_nfa.as_ref().map_or(false, |w| w == &word) {
                next_nfa = iter_nfa.next();
            } else if next_inv.as_ref().map_or(false, |w| w == &word) {
                next_inv = iter_inv.next();
            } else {
                panic!("Missed component sequence {word:?}");
            }
        }
    }

    #[test]
    fn regex(
        regex_str in random_regex("[a-z]"),
        tests in prop::collection::vec("[a-z]+", 20)
    ) {
        let regex = parser::regex(&regex_str).unwrap();
        let mut dfa = regex.to_nfa().to_dfa();
        dfa.minimize();
        let lib_regex = LibRegex::new(&format!("^({regex_str})$")).unwrap();

        let accepted_chars = regex_str.chars().collect::<HashSet<_>>();

        tests.iter().for_each(|test|{
            // Need to filter string since it can't use characters not in the regex itself
            // due to the DFA alphabet
            let s = test.chars().filter(|c| accepted_chars.contains(c)).collect::<String>();
            assert_eq!(dfa.accepts_graphemes(&s), lib_regex.is_match(&s));
        })
    }

    #[test]
    fn regex_parse(regex_str in random_regex("[a-∅]")) {
        let parse1 = parser::regex(&regex_str).unwrap();
        let stringified = parse1.to_string();
        let parse2 = parser::regex(&stringified).unwrap();
        assert!(parse1.to_nfa().equivalent_to(&parse2.to_nfa()));
    }
}

prop_compose! {
    fn fixed_alphabet_nfa(max_states: usize, alphabet: RangeInclusive<char>, alphabet_size: usize)
        (num_states in 1..max_states)
        (
            states in state_names(num_states),
            initial_state in 0..num_states,
            accepting_states in prop::collection::vec(any::<bool>(), num_states..=num_states),
            transitions in prop::collection::vec(nfa_transitions(num_states, alphabet_size), num_states..=num_states),
            epsilon_transitions in prop::collection::vec(epsilon_transitions(num_states), num_states..=num_states),
        )
    -> Nfa {
        let states = states.into_iter().zip(
            accepting_states.into_iter().zip(
                transitions.into_iter().zip(
                    epsilon_transitions.into_iter()
                )
            )
        ).enumerate().map(|(idx, (state_name, (accepting, (transitions, epsilon_transitions))))|
            NfaState {
                name: Rc::from(state_name.as_str()),
                initial: idx == initial_state,
                accepting,
                transitions,
                epsilon_transitions
            }
        ).collect();

        let mut alphabet: Vec<Rc<str>> = alphabet.clone().map(|c| Rc::from(c.to_string())).collect();
        alphabet.shuffle(&mut thread_rng());
        let alphabet = Rc::from(alphabet);

        Nfa {
            alphabet,
            states,
            initial_state
        }
    }
}

prop_compose! {
    fn nfa(max_states: usize, max_alphabet_size: usize)
        (num_states in 1..max_states, alphabet_size in 1..max_alphabet_size)
        (
            states in state_names(num_states),
            alphabet in alphabet_elems(alphabet_size),
            initial_state in 0..num_states,
            accepting_states in prop::collection::vec(any::<bool>(), num_states..=num_states),
            epsilon_transitions in prop::collection::vec(epsilon_transitions(num_states), num_states..=num_states),
            transitions in prop::collection::vec(nfa_transitions(num_states, alphabet_size), num_states..=num_states)
        )
    -> Nfa {
        let states = states.into_iter().zip(
            accepting_states.into_iter().zip(
                transitions.into_iter().zip(
                    epsilon_transitions.into_iter()
                )
            )
        ).enumerate().map(|(idx, (state_name, (accepting, (transitions, epsilon_transitions))))|
            NfaState {
                name: Rc::from(state_name.as_str()),
                initial: idx == initial_state,
                accepting,
                epsilon_transitions,
                transitions
            }
        ).collect();

        Nfa {
            alphabet: alphabet.iter().map(|entry| Rc::from(entry.as_str())).collect(),
            states,
            initial_state
        }
    }
}

prop_compose! {
    fn fixed_alphabet_dfa(max_states: usize, alphabet: RangeInclusive<char>, alphabet_size: usize)
        (num_states in 1..max_states)
        (
            states in state_names(num_states),
            initial_state in 0..num_states,
            accepting_states in prop::collection::vec(any::<bool>(), num_states..=num_states),
            transitions in prop::collection::vec(dfa_transitions(num_states, alphabet_size), num_states..=num_states)
        )
    -> Dfa {
        let states = states.into_iter().zip(
            accepting_states.into_iter().zip(
                transitions.into_iter()
            )
        ).enumerate().map(|(idx, (state_name, (accepting, transitions)))|
            DfaState {
                name: Rc::from(state_name.as_str()),
                initial: idx == initial_state,
                accepting,
                transitions
            }
        ).collect();

        let mut alphabet: Vec<Rc<str>> = alphabet.clone().map(|c| Rc::from(c.to_string())).collect();
        alphabet.shuffle(&mut thread_rng());
        let alphabet = Rc::from(alphabet);

        Dfa {
            alphabet,
            states,
            initial_state
        }
    }
}

prop_compose! {
    fn dfa(max_states: usize, max_alphabet_size: usize)
        (num_states in 1..max_states, alphabet_size in 1..max_alphabet_size)
        (
            states in state_names(num_states),
            alphabet in alphabet_elems(alphabet_size),
            initial_state in 0..num_states,
            accepting_states in prop::collection::vec(any::<bool>(), num_states..=num_states),
            transitions in prop::collection::vec(dfa_transitions(num_states, alphabet_size), num_states..=num_states)
        )
    -> Dfa {
        let states = states.into_iter().zip(
            accepting_states.into_iter().zip(
                transitions.into_iter()
            )
        ).enumerate().map(|(idx, (state_name, (accepting, transitions)))|
            DfaState {
                name: Rc::from(state_name.as_str()),
                initial: idx == initial_state,
                accepting,
                transitions
            }
        ).collect();

        Dfa {
            alphabet: alphabet.iter().map(|entry| Rc::from(entry.as_str())).collect(),
            states,
            initial_state
        }
    }
}

prop_compose! {
    fn dfa_transitions(states: usize, alphabet_size: usize)
        (transitions in prop::collection::vec(0..states, alphabet_size..=alphabet_size))
    -> Vec<usize> {
        transitions
    }
}

prop_compose! {
    fn epsilon_transitions(states: usize)
        (transitions in prop::collection::vec(any::<bool>(), states..=states))
    -> Vec<usize> {
        let mut rng = thread_rng();
        let mut transitions: Vec<_> = transitions.into_iter()
            .enumerate()
            .filter_map(|(idx, b)| b.then_some(idx))
            .collect();
        transitions.shuffle(&mut rng);
        transitions
    }
}

prop_compose! {
    fn nfa_transitions(states: usize, alphabet_size: usize)
        (transitions in prop::collection::vec(
            // This is a bytevec saying for each state if it has a transition there or not
            // HashMap would be a better fit but maybe too much rejections?
            prop::collection::vec(any::<bool>(), states..=states),
            alphabet_size..=alphabet_size
        ))
    -> Vec<Vec<usize>> {
        let mut rng = thread_rng();
        transitions.into_iter()
            .map(|row| {
                let mut row: Vec<usize> = row.into_iter()
                    .enumerate()
                    .filter_map(|(idx, b)| b.then_some(idx))
                    .collect();
                row.as_mut_slice().shuffle(&mut rng);
                row
            })
            .collect()
    }
}

prop_compose! {
    fn state_names(count: usize)
        (names in filtered_set(count, r"[^\s#{}]+", &["ε", "eps", "", "->", "*"]))
    -> HashSet<String> {
        names
    }
}

prop_compose! {
    fn simple_alphabet(count: usize)
        (names in filtered_set(std::cmp::max(count, 4), "[a-e]", &[]))
    -> HashSet<String> {
        names
    }
}

prop_compose! {
    fn alphabet_elems(count: usize)
        (names in filtered_set(count, r"[^\s#{}]+", &["ε", "eps", "", "->", "*"]))
    -> HashSet<String> {
        names
    }
}

prop_compose! {
    fn filtered_set(count: usize, regex: &'static str, deny: &'static [&'static str])
        (names in prop::collection::hash_set(
            regex.prop_filter( // No whitespace
                "name should not be reserved",
                |s| !deny.contains(&s.as_str()) && !s.contains(|c: char| c.is_whitespace())
            ),
            count..=count
        ))
    -> HashSet<String> {
        names
    }
}

fn random_regex(base: &'static str) -> impl Strategy<Value = String> {
    base.prop_recursive(20, 1024, 20, |inner| {
        prop_oneof![
            10 => prop::collection::vec(inner.clone(), 1..20)
                .prop_map(|vec| format!("({})", vec.join(""))),
            10 => prop::collection::vec(inner.clone(), 1..20).prop_map(|vec| vec.join("|")),
            3 => inner.clone().prop_map(|r| format!("({r})*")),
            3 => inner.clone().prop_map(|r| format!("({r})+")),
        ]
    })
}