rstm_core/
rule.rs

1/*
2    Appellation: rule <module>
3    Created At: 2025.08.30:01:04:10
4    Contrib: @FL03
5*/
6mod impl_learned_rule;
7
8mod impl_rule;
9mod impl_rule_builder;
10mod impl_rule_ext;
11
12use crate::{Direction, Head, Tail};
13use rstm_state::{RawState, State};
14
15/// The [`Rule`] implementation is a concrete representation of a single instruction, or rule,
16/// within a given Turing machine program. It encapsulates the necessary components to define
17/// the behavior of the Turing machine when it encounters a specific state and symbol.
18///
19/// **Note**: The inner fields are flattened for serialization purposes when using `serde`;
20/// this means that the fields of the [`Head`] and [`Tail`] structs will be serialized as if they
21/// were direct fields of the `Rule` struct itself.
22#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Ord, PartialOrd)]
23#[cfg_attr(
24    feature = "serde",
25    derive(serde::Deserialize, serde::Serialize),
26    serde(rename_all = "camelCase")
27)]
28pub struct Rule<Q = String, A = char>
29where
30    Q: RawState,
31{
32    #[cfg_attr(feature = "serde", serde(flatten))]
33    pub head: Head<Q, A>,
34    #[cfg_attr(feature = "serde", serde(flatten))]
35    pub tail: Tail<Q, A>,
36}
37
38/// A [`LearnedRule`] is an extension of the basic [`Rule`] structure, incorporating a
39/// confidence metric to quantify the reliability or certainty of the rule within the scope of
40/// a learning context. This is particularly useful in scenarios where rules are derived from
41/// data or experience, allowing for a more nuanced application of rules based on their
42/// confidence levels.
43#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
44#[cfg_attr(
45    feature = "serde",
46    derive(serde::Deserialize, serde::Serialize),
47    serde(rename_all = "camelCase")
48)]
49pub struct LearnedRule<C = f32, Q = usize, S = usize>
50where
51    Q: RawState,
52{
53    pub confidence: C,
54    #[cfg_attr(feature = "serde", serde(flatten))]
55    pub rule: Rule<Q, S>,
56}
57
58#[derive(Default)]
59pub struct RuleBuilder<Q, S> {
60    direction: Direction,
61    state: Option<State<Q>>,
62    symbol: Option<S>,
63    next_state: Option<State<Q>>,
64    write_symbol: Option<S>,
65}