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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
use crate::{
    automaton::{Automata, Automaton, Buildable, FromRawError},
    dfa::{ToDfa, DFA},
    regex::{Operations, Regex, ToRegex},
    utils::*,
};
use std::{
    cmp::{Ordering, Ordering::*, PartialEq, PartialOrd},
    collections::{BTreeSet, HashMap, HashSet, VecDeque},
    fmt::{Debug, Display},
    hash::Hash,
    iter::{repeat, FromIterator},
    ops::{Add, BitOr, Bound::*, Mul, Neg, Not, RangeBounds, Sub},
    str::FromStr,
};

/// <https://en.wikipedia.org/wiki/Nondeterministic_finite_automaton>
#[derive(Debug, Clone)]
pub struct NFA<V: Eq + Hash + Display + Copy + Clone + Debug + Ord> {
    pub(crate) alphabet: HashSet<V>,
    pub(crate) initials: HashSet<usize>,
    pub(crate) finals: HashSet<usize>,
    pub(crate) transitions: Vec<HashMap<V, Vec<usize>>>,
}

/// An interface for structs that can be converted into a NFA.
pub trait ToNfa<V: Eq + Hash + Display + Copy + Clone + Debug + Ord> {
    fn to_nfa(&self) -> NFA<V>;
}

/* IMPLEMENTATION OF NFA */

impl<V: Eq + Hash + Display + Copy + Clone + Debug + Ord> NFA<V> {
    /// Returns an NFA that accepts a word if and only if this word is accepted by both `self` and `other`.
    pub fn intersect(self, other: NFA<V>) -> NFA<V> {
        self.negate().unite(other.negate()).negate().to_nfa()
    }

    /// A contains B if and only if for each `word` w, if B `accepts` w then A `accepts` w.
    pub fn contains(&self, other: &NFA<V>) -> bool {
        self.clone().negate().intersect(other.clone()).is_empty()
    }

    fn small_to_dfa<T: Eq + Hash + Copy + BitOr<Output = T>, C: Fn(usize) -> T>(
        &self,
        zero: T,
        shift: C,
    ) -> DFA<V> {
        let mut map = HashMap::new();
        let mut stack = VecDeque::new();

        let mut dfa = DFA::new_empty(&self.alphabet);

        let i: T = self.initials.iter().fold(zero, |acc, x| acc | shift(*x));
        if self.initials.iter().any(|x| self.finals.contains(x)) {
            dfa.finals.insert(0);
        }

        map.insert(i, 0);
        stack.push_back((i, HashSet::from_iter(self.initials.clone().into_iter())));

        while let Some((elem, iter)) = stack.pop_front() {
            let elem_num = *map.get(&elem).unwrap();
            for v in &self.alphabet {
                let mut it = HashSet::new();
                for state in &iter {
                    if let Some(transitions) = self.transitions[*state].get(&v) {
                        for t in transitions {
                            it.insert(*t);
                        }
                    }
                }
                if it.is_empty() {
                    continue;
                }

                let other = it.iter().fold(zero, |acc, x| acc | shift(*x));
                let entry = map.entry(other);
                let val = entry.or_insert_with(|| {
                    let l = dfa.transitions.len();
                    if it.iter().any(|x| self.finals.contains(x)) {
                        dfa.finals.insert(l);
                    }
                    stack.push_back((other, it));
                    dfa.transitions.push(HashMap::new());
                    l
                });

                dfa.transitions[elem_num].insert(*v, *val);
            }
        }

        dfa
    }

    fn big_to_dfa(&self) -> DFA<V> {
        let mut map: HashMap<BTreeSet<usize>, usize> = HashMap::new();
        let mut stack = VecDeque::new();

        let mut dfa = DFA::new_empty(&self.alphabet);

        let initial: BTreeSet<usize> = self.initials.iter().copied().collect();
        map.insert(initial.clone(), 0);
        stack.push_back(initial);

        if self.initials.iter().any(|x| self.finals.contains(x)) {
            dfa.finals.insert(0);
        }

        while let Some(set) = stack.pop_front() {
            let num = *map.get(&set).unwrap();
            for v in &self.alphabet {
                let mut it = HashSet::new();
                for s in &set {
                    if let Some(transitions) = self.transitions[*s].get(&v) {
                        for t in transitions {
                            it.insert(*t);
                        }
                    }
                }
                if it.is_empty() {
                    continue;
                }

                let other = it.iter().fold(BTreeSet::new(), |mut acc, x| {
                    acc.insert(*x);
                    acc
                });
                if !map.contains_key(&other) {
                    let l = dfa.transitions.len();
                    map.insert(other.clone(), l);
                    if it.iter().any(|x| self.finals.contains(x)) {
                        dfa.finals.insert(l);
                    }
                    stack.push_back(other.clone());
                    dfa.transitions.push(HashMap::new());
                }
                dfa.transitions[num].insert(*v, *map.get(&other).unwrap());
            }
        }

        dfa
    }

    /// Returns a string containing the dot description of the automaton
    pub fn to_dot(&self) -> String {
        let mut ret = String::new();
        ret.push_str("digraph {");

        if !self.finals.is_empty() {
            ret.push_str("    node [shape = doublecircle];");
            for e in &self.finals {
                ret.push_str(&format!(" S_{}", e));
            }
            ret.push_str(";");
        }

        if !self.initials.is_empty() {
            ret.push_str("    node [shape = point];");
            for e in &self.initials {
                ret.push_str(&format!(" I_{}", e));
            }
            ret.push_str(";");
        }

        ret.push_str("    node [shape = circle];");
        let mut tmp_map = HashMap::new();
        for (i, map) in self.transitions.iter().enumerate() {
            if map.is_empty() {
                ret.push_str(&format!("    S_{};", i));
            }
            for (k, v) in map {
                for e in v {
                    tmp_map.entry(e).or_insert_with(Vec::new).push(k);
                }
            }
            for (e, v) in tmp_map.drain() {
                let mut vs = v.into_iter().fold(String::new(), |mut acc, x| {
                    acc.push_str(&x.to_string());
                    acc.push_str(", ");
                    acc
                });
                vs.pop();
                vs.pop();
                ret.push_str(&format!("    S_{} -> S_{} [label = \"{}\"];", i, e, vs));
            }
        }

        for e in &self.initials {
            ret.push_str(&format!("    I_{} -> S_{};", e, e));
        }

        ret.push_str("}");

        ret
    }

    /// Returns an empty NFA.
    pub fn new_empty(alphabet: HashSet<V>) -> NFA<V> {
        NFA {
            alphabet,
            initials: HashSet::new(),
            finals: HashSet::new(),
            transitions: Vec::new(),
        }
    }

    /// Returns a full NFA.
    pub fn new_full(alphabet: HashSet<V>) -> NFA<V> {
        NFA {
            transitions: vec![alphabet.iter().map(|v| (*v, vec![0])).collect()],
            alphabet,
            initials: (0..=0).collect(),
            finals: (0..=0).collect(),
        }
    }

    /// Returns a NFA that accepts all words of the given length.
    pub fn new_length(alphabet: HashSet<V>, len: usize) -> NFA<V> {
        let mut transitions: Vec<_> = repeat(HashMap::new()).take(len).collect();
        for (i, map) in transitions.iter_mut().enumerate() {
            for v in &alphabet {
                map.insert(*v, vec![i + 1]);
            }
        }

        transitions.push(HashMap::new());

        NFA {
            alphabet,
            initials: (0..=0).collect(),
            finals: (len..=len).collect(),
            transitions,
        }
    }

    /// Returns a NFA that accepts only the given word.
    pub fn new_matching(alphabet: HashSet<V>, word: &[V]) -> NFA<V> {
        let l = word.len();
        let mut nfa = NFA {
            alphabet,
            initials: (0..=0).collect(),
            finals: (l..=l).collect(),
            transitions: repeat(HashMap::new()).take(l + 1).collect(),
        };

        for (i, l) in word.iter().enumerate() {
            nfa.transitions[i].insert(*l, vec![i + 1]);
        }

        nfa
    }

    /// Returns a NFA that accepts only the empty word.
    pub fn new_empty_word(alphabet: HashSet<V>) -> NFA<V> {
        NFA {
            alphabet,
            initials: (0..=0).collect(),
            finals: (0..=0).collect(),
            transitions: vec![HashMap::new()],
        }
    }

    /// Returns an automaton built from the raw arguments.
    pub fn from_raw(
        alphabet: HashSet<V>,
        initials: HashSet<usize>,
        finals: HashSet<usize>,
        transitions: Vec<HashMap<V, Vec<usize>>>,
    ) -> Result<Self, FromRawError<V>> {
        let len = transitions.len();

        if let Some(state) = initials.iter().find(|&&state| state >= len) {
            return Err(FromRawError::InvalidInitial(*state));
        }

        if let Some(state) = finals.iter().find(|&&state| state >= len) {
            return Err(FromRawError::InvalidFinal(*state));
        }

        for (state, map) in transitions.iter().enumerate() {
            if let Some(&letter) = map.keys().find(|&x| !alphabet.contains(x)) {
                return Err(FromRawError::UnknownLetter(letter));
            }

            for (&letter, destinations) in map {
                if let Some(&destination) = destinations.iter().find(|&&x| x >= len) {
                    return Err(FromRawError::InvalidTransition(state, letter, destination));
                }
            }
        }

        Ok(NFA {
            alphabet,
            initials,
            finals,
            transitions,
        })
    }
}

impl<V: Eq + Hash + Display + Copy + Clone + Debug + Ord> ToDfa<V> for NFA<V> {
    fn to_dfa(&self) -> DFA<V> {
        if self.is_empty() {
            DFA::new_empty(&self.alphabet)
        } else if self.transitions.len() < 32 {
            self.small_to_dfa(0 as u32, |x| 1 << x)
        } else if self.transitions.len() < 64 {
            self.small_to_dfa(0 as u64, |x| 1 << x)
        } else if self.transitions.len() < 128 {
            self.small_to_dfa(0 as u128, |x| 1 << x)
        } else {
            self.big_to_dfa()
        }
    }
}

impl<V: Eq + Hash + Display + Copy + Clone + Debug + Ord> ToNfa<V> for NFA<V> {
    fn to_nfa(&self) -> NFA<V> {
        self.clone()
    }
}

impl<V: Eq + Hash + Display + Copy + Clone + Debug + Ord> ToRegex<V> for NFA<V> {
    fn to_regex(&self) -> Regex<V> {
        let n = self.transitions.len();
        if n == 0 {
            return Regex {
                alphabet: self.alphabet.clone(),
                regex: Operations::Empty,
            };
        }

        let mut mat1: Vec<Vec<Operations<V>>> = repeat(repeat(Operations::Empty).take(n).collect())
            .take(n)
            .collect();
        let mut mat2: Vec<Vec<Operations<V>>> = mat1.clone();

        for (i, m) in self.transitions.iter().enumerate() {
            mat1[i][i] = Operations::Epsilon;
            for (k, v) in m {
                for &j in v {
                    mat1[i][j] += Operations::Letter(*k);
                }
            }
        }

        for k in 0..n {
            for i in 0..n {
                for j in 0..n {
                    mat2[i][j] = mat1[i][j].clone()
                        + mat1[i][k].clone()
                            * Operations::Repeat(Box::new(mat1[k][k].clone()), 0, None)
                            * mat1[k][j].clone();
                }
            }
            std::mem::swap(&mut mat1, &mut mat2);
        }

        let mut res = Operations::Empty;
        for &st in &self.initials {
            for &en in &self.finals {
                res += mat1[st][en].clone();
            }
        }

        Regex {
            alphabet: self.alphabet.clone(),
            regex: res,
        }
    }
}

impl<V: Eq + Hash + Display + Copy + Clone + Debug + Ord> Automata<V> for NFA<V> {
    fn run(&self, v: &[V]) -> bool {
        if self.initials.is_empty() {
            return false;
        }

        let mut actuals = self.initials.clone();
        let mut next = HashSet::new();

        for l in v {
            for st in &actuals {
                if let Some(tr) = self.transitions[*st].get(l) {
                    for t in tr {
                        next.insert(*t);
                    }
                }
            }

            std::mem::swap(&mut actuals, &mut next);
            if actuals.is_empty() {
                return false;
            }
            next.clear();
        }

        actuals.iter().any(|x| self.finals.contains(x))
    }

    fn is_complete(&self) -> bool {
        if self.initials.is_empty() {
            return false;
        }

        for m in &self.transitions {
            for v in &self.alphabet {
                if match m.get(v) {
                    None => true,
                    Some(v) => v.is_empty(),
                } {
                    return false;
                }
            }
        }
        true
    }

    fn is_reachable(&self) -> bool {
        let mut acc: HashSet<usize> = self.initials.clone().into_iter().collect();
        let mut stack: Vec<usize> = self.initials.iter().cloned().collect();
        while let Some(e) = stack.pop() {
            for v in self.transitions[e].values() {
                for t in v {
                    if !acc.contains(t) {
                        acc.insert(*t);
                        stack.push(*t);
                    }
                }
            }
        }
        acc.len() == self.transitions.len()
    }

    fn is_coreachable(&self) -> bool {
        self.clone().reverse().is_reachable()
    }

    fn is_trimmed(&self) -> bool {
        self.is_reachable() && self.is_coreachable()
    }

    fn is_empty(&self) -> bool {
        if !self.initials.is_disjoint(&self.finals) {
            return false;
        }

        let mut acc: HashSet<usize> = self.initials.clone().into_iter().collect();
        let mut stack: Vec<usize> = self.initials.clone().into_iter().collect();

        while let Some(e) = stack.pop() {
            for v in self.transitions[e].values() {
                for t in v {
                    if self.finals.contains(t) {
                        return false;
                    }
                    if !acc.contains(t) {
                        acc.insert(*t);
                        stack.push(*t);
                    }
                }
            }
        }
        true
    }

    fn is_full(&self) -> bool {
        if self.initials.is_disjoint(&self.finals) {
            return false;
        }

        let mut acc: HashSet<usize> = self.initials.clone().into_iter().collect();
        let mut stack: Vec<usize> = self.initials.clone().into_iter().collect();

        while let Some(e) = stack.pop() {
            for v in self.transitions[e].values() {
                for t in v {
                    if !self.finals.contains(t) {
                        return false;
                    }
                    if !acc.contains(t) {
                        acc.insert(*t);
                        stack.push(*t);
                    }
                }
            }
        }
        true
    }

    fn negate(self) -> NFA<V> {
        self.to_dfa().negate().to_nfa()
    }

    fn complete(mut self) -> NFA<V> {
        if self.is_complete() {
            return self;
        }

        let l = self.transitions.len();
        self.transitions.push(HashMap::new());
        for m in &mut self.transitions {
            for v in &self.alphabet {
                let t = m.entry(*v).or_insert_with(Vec::new);
                if t.is_empty() {
                    t.push(l);
                }
            }
        }

        if self.initials.is_empty() {
            self.initials.insert(l);
        }

        self
    }

    fn make_reachable(mut self) -> NFA<V> {
        let mut acc: HashSet<usize> = self.initials.clone().into_iter().collect();
        let mut stack: Vec<usize> = self.initials.iter().cloned().collect();
        while let Some(e) = stack.pop() {
            for v in self.transitions[e].values() {
                for t in v {
                    if !acc.contains(t) {
                        acc.insert(*t);
                        stack.push(*t);
                    }
                }
            }
        }

        let mut map = HashMap::new();
        let mut ind = 0;
        let l = self.transitions.len();
        for i in 0..l {
            if acc.contains(&i) {
                map.insert(i, ind);
                self.transitions.swap(i, ind);
                ind += 1;
            }
        }
        self.transitions.truncate(ind);

        self.finals = self
            .finals
            .iter()
            .filter(|x| acc.contains(&x))
            .map(|x| *map.get(x).unwrap())
            .collect();
        // no need to filter the initials since they are reachable
        self.initials = self.initials.iter().map(|x| *map.get(x).unwrap()).collect();
        for m in &mut self.transitions {
            for v in m.values_mut() {
                for t in v {
                    *t = *map.get(t).unwrap();
                }
            }
        }

        self
    }

    fn make_coreachable(self) -> NFA<V> {
        self.reverse().make_reachable().reverse()
    }

    fn trim(self) -> NFA<V> {
        self.make_reachable().make_coreachable()
    }

    fn reverse(mut self) -> NFA<V> {
        let mut transitions: Vec<_> = repeat(HashMap::new())
            .take(self.transitions.len())
            .collect();

        for i in 0..self.transitions.len() {
            for (k, v) in &self.transitions[i] {
                for e in v {
                    transitions[*e].entry(*k).or_insert_with(Vec::new).push(i);
                }
            }
        }

        self.transitions = transitions;
        std::mem::swap(&mut self.initials, &mut self.finals);
        self
    }
}

impl<V: Eq + Hash + Display + Copy + Clone + Debug + Ord> Buildable<V> for NFA<V> {
    fn unite(mut self, other: NFA<V>) -> NFA<V> {
        let NFA {
            alphabet,
            initials,
            finals,
            transitions,
        } = other;

        let l = self.transitions.len();

        append_hashset(&mut self.alphabet, alphabet);
        append_shift_hashset(&mut self.initials, initials, l);
        append_shift_hashset(&mut self.finals, finals, l);
        append_shift_transitions(&mut self.transitions, transitions);

        self
    }

    fn concatenate(mut self, mut other: NFA<V>) -> NFA<V> {
        let l = self.transitions.len();
        shift_fnda(&mut other, l);
        let NFA {
            alphabet,
            initials,
            finals,
            mut transitions,
        } = other;

        append_hashset(&mut self.alphabet, alphabet);

        for e in &initials {
            for (v, t) in &mut transitions[e - l] {
                // e - l because of the shift above
                for f in &self.finals {
                    self.transitions[*f]
                        .entry(*v)
                        .or_insert_with(Vec::new)
                        .append(&mut t.clone());
                }
            }
        }

        if finals.is_disjoint(&initials) {
            self.finals = finals;
        } else {
            append_hashset(&mut self.finals, finals);
        }
        self.transitions.append(&mut transitions);

        self
    }

    fn kleene(mut self) -> NFA<V> {
        let l = self.transitions.len();
        let mut map = HashMap::new();

        for i in &self.initials {
            for (k, v) in &self.transitions[*i] {
                let set = &mut map.entry(*k).or_insert_with(HashSet::new);
                for x in v {
                    set.insert(*x);
                }
            }
        }

        for i in &self.finals {
            for (k, v) in &map {
                let mut set: HashSet<usize> = self.transitions[*i]
                    .entry(*k)
                    .or_insert_with(Vec::new)
                    .drain(..)
                    .collect();
                for x in v {
                    set.insert(*x);
                }
                self.transitions[*i].insert(*k, set.into_iter().collect());
            }
        }

        self.transitions.push(
            map.into_iter()
                .map(|(k, v)| (k, v.into_iter().collect()))
                .collect(),
        );
        self.initials.clear();
        self.initials.insert(l);
        self.finals.insert(l);

        self
    }

    fn at_most(mut self, u: usize) -> NFA<V> {
        if !self.initials.iter().any(|x| self.finals.contains(x)) {
            let l = self.transitions.len();
            self.initials.insert(l);
            self.finals.insert(l);
            self.transitions.push(HashMap::new());
        }

        (0..u).fold(NFA::new_empty_word(self.alphabet.clone()), |acc, _| {
            acc.concatenate(self.clone())
        })
    }

    fn at_least(self, u: usize) -> NFA<V> {
        (0..u)
            .fold(NFA::new_empty_word(self.alphabet.clone()), |acc, _| {
                acc.concatenate(self.clone())
            })
            .concatenate(self.kleene())
    }

    fn repeat<R: RangeBounds<usize>>(self, r: R) -> NFA<V> {
        let start = match r.start_bound() {
            Included(&a) => a,
            Excluded(&a) => a + 1,
            Unbounded => 0,
        };

        let end = match r.end_bound() {
            Included(&a) => Some(a),
            Excluded(&a) => Some(a - 1),
            Unbounded => None,
        };

        if let Some(end) = end {
            if end < start {
                return NFA::new_empty(self.alphabet);
            }
        }

        if let Some(end) = end {
            (0..start)
                .fold(NFA::new_empty_word(self.alphabet.clone()), |acc, _| {
                    acc.concatenate(self.clone())
                })
                .concatenate(self.at_most(end - start))
        } else {
            self.at_least(start)
        }
    }
}

impl<V: Eq + Hash + Display + Copy + Clone + Debug + Ord> PartialEq<NFA<V>> for NFA<V> {
    fn eq(&self, other: &NFA<V>) -> bool {
        self.le(other) && self.ge(other)
    }
}

impl<V: Eq + Hash + Display + Copy + Clone + Debug + Ord> PartialEq<DFA<V>> for NFA<V> {
    fn eq(&self, other: &DFA<V>) -> bool {
        self.eq(&other.to_nfa())
    }
}

impl<V: Eq + Hash + Display + Copy + Clone + Debug + Ord> PartialEq<Regex<V>> for NFA<V> {
    fn eq(&self, other: &Regex<V>) -> bool {
        self.eq(&other.to_nfa())
    }
}

impl<V: Eq + Hash + Display + Copy + Clone + Debug + Ord> PartialEq<Automaton<V>> for NFA<V> {
    fn eq(&self, other: &Automaton<V>) -> bool {
        match other {
            Automaton::DFA(v) => self.eq(&*v),
            Automaton::NFA(v) => self.eq(&*v),
            Automaton::REG(v) => self.eq(&*v),
        }
    }
}

impl<V: Eq + Hash + Display + Copy + Clone + Debug + Ord> PartialOrd for NFA<V> {
    fn partial_cmp(&self, other: &NFA<V>) -> Option<Ordering> {
        match (self.ge(&other), self.le(&other)) {
            (true, true) => Some(Equal),
            (true, false) => Some(Greater),
            (false, true) => Some(Less),
            (false, false) => None,
        }
    }

    fn lt(&self, other: &NFA<V>) -> bool {
        other.contains(&self) && !self.contains(&other)
    }

    fn le(&self, other: &NFA<V>) -> bool {
        other.contains(&self)
    }

    fn gt(&self, other: &NFA<V>) -> bool {
        self.contains(&other) && !other.contains(&self)
    }

    fn ge(&self, other: &NFA<V>) -> bool {
        self.contains(&other)
    }
}

impl FromStr for NFA<char> {
    type Err = String;

    fn from_str(s: &str) -> Result<NFA<char>, Self::Err> {
        s.parse::<Regex<char>>().map(|x| x.to_nfa())
    }
}

/// The multiplication of A and B is A.concatenate(B)
impl<V: Eq + Hash + Display + Copy + Clone + Debug + Ord> Mul for NFA<V> {
    type Output = Self;

    fn mul(self, other: NFA<V>) -> NFA<V> {
        self.concatenate(other)
    }
}

/// The negation of A is A.negate().
impl<V: Eq + Hash + Display + Copy + Clone + Debug + Ord> Neg for NFA<V> {
    type Output = Self;

    fn neg(self) -> NFA<V> {
        self.negate()
    }
}

/// The opposite of A is A.reverse().
impl<V: Eq + Hash + Display + Copy + Clone + Debug + Ord> Not for NFA<V> {
    type Output = Self;

    fn not(self) -> NFA<V> {
        self.reverse()
    }
}

/// The substraction of A and B is an automaton that accepts a word if and only if A accepts it and B doesn't.
impl<V: Eq + Hash + Display + Copy + Clone + Debug + Ord> Sub for NFA<V> {
    type Output = Self;

    fn sub(self, other: NFA<V>) -> NFA<V> {
        self.intersect(other.negate())
    }
}

/// The addition fo A and B is an automaton that accepts a word if and only if A or B accept it.
impl<V: Eq + Hash + Display + Copy + Clone + Debug + Ord> Add for NFA<V> {
    type Output = Self;

    fn add(self, other: NFA<V>) -> NFA<V> {
        self.unite(other)
    }
}