Skip to main content

rstm_core/programs/
program_base.rs

1/*
2    Appellation: program_base <module>
3    Created At: 2026.01.11:12:34:11
4    Contrib: @FL03
5*/
6use super::RawRuleset;
7use crate::rules::Instruction;
8use rstm_state::{RawState, State};
9
10/// The [`ProgramBase`] struct is used to define a generic program capable of being executed by
11/// a Turing machine or similar computational model. It consists of an optional initial state,
12/// a set of rules (or instructions) used to indicate how the machine should *respond* under
13/// different *circumstances*, and a marker to associate the generic parameters with the struct.
14#[derive(Clone, Copy, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
15#[cfg_attr(
16    feature = "serde",
17    derive(serde::Serialize, serde::Deserialize),
18    serde(deny_unknown_fields, rename_all = "snake_case")
19)]
20#[repr(C)]
21pub struct ProgramBase<R: ?Sized, Q, A, I = <R as RawRuleset<Q, A>>::Rule>
22where
23    Q: RawState,
24    R: RawRuleset<Q, A, Rule = I>,
25    I: Instruction<Q, A>,
26{
27    pub(crate) initial_state: Option<State<Q>>,
28    pub(crate) _marker: core::marker::PhantomData<(I, Q, A)>,
29    pub(crate) rules: R,
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35    use crate::rules::{Direction, Rule};
36
37    #[test]
38    fn test_program_base() {
39        let rule = Rule::from_parts(0, 'a', Direction::Right, 1, 'b');
40
41        let program = ProgramBase {
42            initial_state: Some(State::new(0)),
43            _marker: core::marker::PhantomData,
44            rules: [rule],
45        };
46        assert_eq!(program.initial_state, Some(State::new(0)));
47    }
48}