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
/*
    Appellation: tail <module>
    Contrib: FL03 <jo3mccain@icloud.com>
    Description:
        Turing machines accept instructions in the form of a five-tuple:
            (State, Symbol, State, Symbol, Move)
*/
use super::Move;
use crate::{State, Symbolic};
use contained_core::states::Stateful;
use decanter::prelude::Hashable;
use serde::{Deserialize, Serialize};

#[derive(
    Clone, Debug, Default, Deserialize, Eq, Hash, Hashable, Ord, PartialEq, PartialOrd, Serialize,
)]
pub struct Tail<S: Symbolic = String> {
    state: State,
    symbol: S,
    action: Move,
}

impl<S: Symbolic> Tail<S> {
    pub fn new(state: State, symbol: S, action: Move) -> Self {
        Self {
            state,
            symbol,
            action,
        }
    }
    pub fn action(&self) -> Move {
        self.action
    }
    pub fn state(&self) -> State {
        self.state
    }
    pub fn symbol(&self) -> S {
        self.symbol.clone()
    }
}

impl<S: Symbolic> Stateful<State> for Tail<S> {
    fn state(&self) -> State {
        self.state
    }

    fn update_state(&mut self, state: State) {
        self.state = state;
    }
}

impl<S: Symbolic> std::fmt::Display for Tail<S> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "({}, {}, {})", self.state, self.symbol, self.action)
    }
}

impl<S: Symbolic> From<(State, S, Move)> for Tail<S> {
    fn from(args: (State, S, Move)) -> Self {
        Self::new(args.0, args.1, args.2)
    }
}

impl<S: Symbolic> From<Tail<S>> for (State, S, Move) {
    fn from(tail: Tail<S>) -> (State, S, Move) {
        (tail.state(), tail.symbol(), tail.action())
    }
}