rstm_core/traits/
symbols.rs

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
/*
    Appellation: symbolic <module>
    Contrib: FL03 <jo3mccain@icloud.com>
*/

/// [Alphabet] describes a finite set of symbols used to construct a formal language.
///
/// Ideally, the alphabet should be implemented on unit enums since
/// each symbol can be represented as a unique variant and assigned
/// a particular value. The values of the variants may then be used
/// as pointers, specifiying the location of the symbol w.r.t. the
/// alphabet.
pub trait Alphabet {
    type Elem;

    fn as_slice(&self) -> &[Self::Elem];

    fn as_mut_slice(&mut self) -> &mut [Self::Elem];

    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    fn len(&self) -> usize {
        self.as_slice().len()
    }

    fn to_vec(&self) -> Vec<Self::Elem>;
}

/// [Symbolic] is a trait denoting types that can be used as symbols;
/// this is useful for allowing symbols to represented with [char] or
/// be a position on the tape, value mapping for an alphabet,.
pub trait Symbolic
where
    Self: Clone
        + Copy
        + Default
        + Eq
        + Ord
        + PartialEq
        + PartialOrd
        + core::fmt::Debug
        + core::fmt::Display
        + core::hash::Hash
        + Send
        + Sync
        + 'static,
{
}

/*
 ************* Implementations *************
*/
#[cfg(feature = "alloc")]
use alloc::vec::Vec;

impl<S: Symbolic> Alphabet for [S] {
    type Elem = S;

    fn as_slice(&self) -> &[S] {
        self
    }

    fn as_mut_slice(&mut self) -> &mut [S] {
        self
    }

    fn is_empty(&self) -> bool {
        self.is_empty()
    }

    fn len(&self) -> usize {
        self.len()
    }

    fn to_vec(&self) -> Vec<S> {
        self.to_vec()
    }
}

#[cfg(feature = "alloc")]
impl<S: Symbolic> Alphabet for Vec<S> {
    type Elem = S;

    fn as_slice(&self) -> &[S] {
        self.as_slice()
    }

    fn as_mut_slice(&mut self) -> &mut [S] {
        self.as_mut_slice()
    }

    fn is_empty(&self) -> bool {
        self.is_empty()
    }

    fn len(&self) -> usize {
        self.len()
    }

    fn to_vec(&self) -> Vec<S> {
        self.clone()
    }
}

impl<S> Symbolic for S where
    S: Copy
        + Default
        + Eq
        + Ord
        + core::fmt::Debug
        + core::fmt::Display
        + core::hash::Hash
        + Send
        + Sync
        + 'static
{
}