Skip to main content

rstm_state/traits/
halting.rs

1/*
2    appellation: halted <module>
3    authors: @FL03
4*/
5use crate::RawState;
6use crate::state::State;
7
8/// The [`Halting`] trait establishes an interface for determining whether a given state
9/// is in a halted condition. This trait is essential for Turing machine simulations,
10/// as it allows for the identification of states that signify the end of computation.
11pub trait Halting {
12    /// returns true if the current state is considered to be _halted_, otherwise false.
13    fn is_halted(&self) -> bool;
14}
15
16/*
17 ************* Implementations *************
18*/
19use crate::Halt;
20
21impl<Q> Halting for State<Q>
22where
23    Q: Halting,
24{
25    fn is_halted(&self) -> bool {
26        self.get().is_halted()
27    }
28}
29
30impl<Q, H> Halting for Halt<Q, H>
31where
32    Q: RawState,
33    H: RawState,
34{
35    fn is_halted(&self) -> bool {
36        matches!(self, &Halt::Halt(_))
37    }
38}
39
40impl<Q> Halting for Option<Q> {
41    fn is_halted(&self) -> bool {
42        self.is_none()
43    }
44}
45
46macro_rules! impl_is_halted {
47    ($($tag:ident {$($T:ty),* $(,)?}),* $(,)?) => {
48        $(impl_is_halted! { @impl #[$tag] Halting for $($T),* })*
49    };
50    (@impl #[unsigned] $trait:ident for $($T:ty),*) => {
51        $(impl $trait for $T {
52            fn is_halted(&self) -> bool {
53                self == &<$T>::MAX
54            }
55        })*
56    };
57    (@impl #[signed] $trait:ident for $($T:ty),*) => {
58        $(impl $trait for $T {
59            fn is_halted(&self) -> bool {
60                self.abs() == <$T>::MAX
61            }
62        })*
63    };
64    (@impl #[float] $trait:ident for $($T:ty),*) => {
65        $(impl $trait for $T {
66            fn is_halted(&self) -> bool {
67                self.is_nan() || self.is_infinite()
68            }
69        })*
70    };
71}
72
73impl_is_halted! {
74    unsigned { u8, u16, u32, u64, u128, usize },
75    signed { i8, i16, i32, i64, i128, isize },
76    float { f32, f64 }
77}