eryon_actors/engine/mod.rs
1/*
2 Appellation: engines <module>
3 Contrib: @FL03
4*/
5//! This module works to implement various engines compatible with the [Plant](crate::plant::Plant).
6//!
7//! For the moment, the module contains two engines:
8//!
9//! - [NeuralEngine]: A neural Turing machine
10//! - [WolframUTM]: A Wolfram [2, 3] universal Turing machine
11//!
12//! While the Wolfram [2, 3] UTM is used for testing and validation purposes, the NeuralTM is
13//! used in production applications. All engines are required to implement a set of traits that
14//!
15//! The Wolfram UTM is primarily used for testing and validation purposes, while the NeuralTM
16//! is used for more practical applications. That being said, the architecture of the NeuralTM
17//! is consistent with that of the WolframUTM as we rely on a 2-simplex to define the so-called
18//! _headspace_ of the engines.
19#[doc(inline)]
20pub use self::{neural::NeuralEngine, wolfram::WolframUTM};
21
22pub mod neural;
23pub mod wolfram;
24
25pub(crate) mod prelude {
26 pub use super::neural::prelude::*;
27 pub use super::wolfram::*;
28 pub use super::{ComputationalEngine, RawEngine};
29}
30
31use crate::traits::{Alphabet, RawStore};
32use eryon::{Head, RawState, State};
33
34pub trait RawEngine: Send + Sync + core::fmt::Debug {
35 type Store: RawStore;
36
37 private!();
38}
39
40pub trait Engine: RawEngine
41where
42 Self: Clone + Default,
43{
44 private!();
45}
46/// A compute-oriented engine abstracted for use within the [Plant](crate::plant::Plant).
47pub trait ComputationalEngine<Q, S>: RawEngine
48where
49 Q: RawState,
50 S: Alphabet,
51 Self: Sized,
52 Self::Store: RawStore<Elem = S::Elem>,
53{
54 fn new(alphabet: S, initial_state: State<Q>) -> Self;
55 /// returns an immutable reference to the alphabet
56 fn alphabet(&self) -> &S;
57 /// returns a mutable reference to the alphabet
58 fn alphabet_mut(&mut self) -> &mut S;
59 /// returns an instance of the head with immutable references to the state and symbol
60 fn head(&self) -> Head<&Q, &S::Elem>;
61 /// returns an instance of the head with mutable references to the state and symbol
62 fn head_mut(&mut self) -> Head<&mut Q, &mut S::Elem>;
63 /// returns a copy of the machine's current position
64 fn position(&self) -> usize;
65 /// returns a mutable reference to the machine's current position
66 fn position_mut(&mut self) -> &mut usize;
67 /// returns an instance of the state with an immutable inner value
68 fn state(&self) -> State<&Q>;
69 /// returns an instance of the state with a mutable inner value
70 fn state_mut(&mut self) -> State<&mut Q>;
71 /// returns an immutable reference to the tape
72 fn tape(&self) -> &Self::Store;
73 /// returns a mutable reference to the tape
74 fn tape_mut(&mut self) -> &mut Self::Store;
75 /// preform a single step of the turing machine
76 fn step(&mut self) -> Result<(), crate::ActorError>;
77 /// sets the alphabet of the turing machine
78 fn set_alphabet(&mut self, alphabet: S);
79 /// sets the position of the head
80 fn set_position(&mut self, position: usize);
81 /// sets the state of the head
82 fn set_state(&mut self, state: State<Q>);
83 /// sets the tape of the turing machine
84 fn set_tape<I>(&mut self, tape: I)
85 where
86 I: IntoIterator<Item = S::Elem>;
87 /// restores the engine from a snapshot
88 fn restore_from_snapshot(&mut self, snapshot: crate::Snapshot<Q, S, Self::Store>)
89 where
90 Self::Store: IntoIterator<Item = S::Elem>,
91 {
92 snapshot.restore_engine(self);
93 }
94 /// returns a snapshot of the engine
95 fn snapshot(&self) -> crate::SnapshotRef<'_, Q, S, Self::Store> {
96 crate::SnapshotRef::from(self)
97 }
98}