Skip to main content

rstm_core/rules/traits/
rulespace.rs

1/*
2    Appellation: rulespace <module>
3    Created At: 2025.12.16:17:22:50
4    Contrib: @FL03
5*/
6use crate::{Head, Rule};
7use rspace_traits::RawSpace;
8use rstm_state::{RawState, State};
9
10/// A [`HeadSpace`] is a particular _kind_ of space that deals with the _heads_ of rules.
11pub trait HeadSpace<Q, S>: RawSpace<Elem = Head<Q, S>> {}
12/// If a _headspace_, or configuration space, defines all possible configurations of a system,
13/// then a rulespace defines how those configurations can change or evolve over time. In other
14/// words, if a c-space is a disconnected point cloud, then a rulespace is a set of rules that
15/// connect those points together, defining morphisms between them.
16pub trait RuleSpace<Q, S>
17where
18    Q: RawState,
19{
20    type Space<_Q, _S>: ?Sized + HeadSpace<_Q, _S>;
21
22    /// Retrieve the underlying configuration space associated with this rulespace.
23    fn get_rule(&self, state: &Q, symbol: &S) -> Option<&Rule<Q, S>>;
24}
25
26/*
27 ************* Implementations *************
28*/
29
30impl<Q, S> HeadSpace<Q, S> for [Head<Q, S>] {}
31
32impl<Q, S> RuleSpace<Q, S> for [Rule<Q, S>]
33where
34    Q: RawState + PartialEq,
35    S: PartialEq,
36{
37    type Space<_Q, _S> = dyn HeadSpace<_Q, _S>;
38
39    fn get_rule(&self, state: &Q, symbol: &S) -> Option<&Rule<Q, S>> {
40        self.iter().find(|&Rule { head, .. }| {
41            head.state().view() == State(state) && head.symbol() == symbol
42        })
43    }
44}
45
46#[cfg(feature = "alloc")]
47mod impl_alloc {
48    use super::{HeadSpace, RuleSpace};
49    use crate::{Head, Rule};
50    use alloc::vec::Vec;
51    use rstm_state::{RawState, State};
52
53    impl<Q, A> HeadSpace<Q, A> for Vec<Head<Q, A>> {}
54
55    impl<Q, A> RuleSpace<Q, A> for Vec<Rule<Q, A>>
56    where
57        Q: RawState + PartialEq,
58        A: PartialEq,
59    {
60        type Space<_Q, _S> = dyn HeadSpace<_Q, _S>;
61
62        fn get_rule(&self, state: &Q, symbol: &A) -> Option<&Rule<Q, A>> {
63            self.iter().find(|&Rule { head, .. }| {
64                head.state().view() == State(state) && head.symbol() == symbol
65            })
66        }
67    }
68}