rstm_core/traits/
instruction.rs

1/*
2    Appellation: instruction <module>
3    Created At: 2025.09.04:18:27:25
4    Contrib: @FL03
5*/
6use crate::{Direction, Head, Rule, Symbol, Tail};
7use rstm_state::{RawState, State};
8
9/// The [`Scope`] trait establishes a common interface for all _head_-like objects;
10pub trait Scope<Q, S>
11where
12    Q: RawState,
13{
14    /// returns an immutable reference to the current state
15    fn current_state(&self) -> &State<Q>;
16
17    fn current_symbol(&self) -> &S;
18}
19
20/// The [`Directive`] trait is used to define the expected behaviors of all _tail_-like objects
21/// within the system
22pub trait Directive<Q, S>
23where
24    Q: RawState,
25{
26    fn direction(&self) -> Direction;
27
28    fn next_state(&self) -> &State<Q>;
29
30    fn next_symbol(&self) -> &S;
31}
32
33/// The [`Instruction`] trait defines the expected behaviors of a particular rule within a
34/// Turing machine program.
35pub trait Instruction<Q, S>
36where
37    Q: RawState,
38{
39    /// returns a copy of the direction of the head
40    fn direction(&self) -> Direction;
41    /// returns a reference to the current state of the Turing machine
42    fn current_state(&self) -> &State<Q>;
43    /// returns a reference to the next state of the Turing machine
44    fn next_state(&self) -> &State<Q>;
45    /// returns a reference to the current symbol under the head
46    fn symbol(&self) -> &S;
47    /// returns a reference to the symbol to be written by the head
48    fn write_symbol(&self) -> &S;
49    /// returns an instance of [`Head`] containing references to the current state and symbol
50    fn head(&self) -> Head<&Q, &S> {
51        Head {
52            state: self.current_state().view(),
53            symbol: self.symbol(),
54        }
55    }
56    /// returns an instance of [`Tail`] containing references to the next state and symbol
57    fn tail(&self) -> Tail<&Q, &S> {
58        Tail {
59            direction: self.direction(),
60            next_state: self.next_state().view(),
61            write_symbol: self.write_symbol(),
62        }
63    }
64    /// returns an instance of [`Rule`] containing references to the states and symbols within
65    fn as_rule(&self) -> Rule<&Q, &S> {
66        Rule {
67            head: self.head(),
68            tail: self.tail(),
69        }
70    }
71}
72
73/*
74 ************* Implementations *************
75*/
76impl<A, Q, S> Instruction<Q, S> for A
77where
78    A: Scope<Q, S> + Directive<Q, S>,
79    Q: RawState,
80    S: Symbol,
81{
82    fn direction(&self) -> Direction {
83        self.direction()
84    }
85
86    fn current_state(&self) -> &State<Q> {
87        self.current_state()
88    }
89
90    fn next_state(&self) -> &State<Q> {
91        self.next_state()
92    }
93
94    fn symbol(&self) -> &S {
95        self.current_symbol()
96    }
97
98    fn write_symbol(&self) -> &S {
99        self.next_symbol()
100    }
101}
102
103impl<Q, S> Scope<Q, S> for (State<Q>, S)
104where
105    Q: RawState,
106{
107    fn current_state(&self) -> &State<Q> {
108        &self.0
109    }
110
111    fn current_symbol(&self) -> &S {
112        &self.1
113    }
114}
115
116impl<Q, S> Scope<Q, S> for Head<Q, S>
117where
118    Q: RawState,
119{
120    fn current_state(&self) -> &State<Q> {
121        self.state()
122    }
123
124    fn current_symbol(&self) -> &S {
125        &self.symbol
126    }
127}
128
129impl<Q, S> Scope<Q, S> for Rule<Q, S>
130where
131    Q: RawState,
132{
133    fn current_state(&self) -> &State<Q> {
134        self.state()
135    }
136
137    fn current_symbol(&self) -> &S {
138        self.symbol()
139    }
140}
141
142impl<Q, S> Directive<Q, S> for (Direction, State<Q>, S)
143where
144    Q: RawState,
145{
146    fn direction(&self) -> Direction {
147        self.0
148    }
149
150    fn next_state(&self) -> &State<Q> {
151        &self.1
152    }
153
154    fn next_symbol(&self) -> &S {
155        &self.2
156    }
157}
158
159impl<Q, S> Directive<Q, S> for Tail<Q, S>
160where
161    Q: RawState,
162{
163    fn direction(&self) -> Direction {
164        self.direction
165    }
166
167    fn next_state(&self) -> &State<Q> {
168        self.state()
169    }
170
171    fn next_symbol(&self) -> &S {
172        self.symbol()
173    }
174}
175
176impl<Q, S> Directive<Q, S> for Rule<Q, S>
177where
178    Q: RawState,
179{
180    fn direction(&self) -> Direction {
181        self.direction()
182    }
183
184    fn next_state(&self) -> &State<Q> {
185        self.tail().state()
186    }
187
188    fn next_symbol(&self) -> &S {
189        self.write_symbol()
190    }
191}