Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: Joe McCain III <jo3mccain@icloud.com>
  • Loading branch information
FL03 committed Jul 29, 2024
1 parent 541f47c commit 5028018
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 45 deletions.
14 changes: 14 additions & 0 deletions core/src/rules/parts/head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ impl<Q, S> Head<Q, S> {
self.symbol = symbol;
}
}

pub fn to_ref<'a>(&'a self) -> Head<&'a Q, &'a S> {
Head {
state: self.state.to_ref(),
symbol: &self.symbol,
}
}

pub fn to_mut<'a>(&'a mut self) -> Head<&'a mut Q, &'a mut S> {
Head {
state: self.state.to_mut(),
symbol: &mut self.symbol,
}
}
}

impl<'a, Q, S> Head<&'a Q, &'a S> {
Expand Down
86 changes: 77 additions & 9 deletions core/src/rules/parts/tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@ impl<Q, S> Tail<Q, S> {
pub fn as_tuple(&self) -> (Direction, &State<Q>, &S) {
(self.direction, &self.state, &self.symbol)
}
/// Returns an instance of the [head](Head) where each element within
/// the created instance is an immutable reference
pub fn as_head(&self) -> Head<&'_ Q, &'_ S> {
super::Head::new(self.state.to_ref(), &self.symbol)
}
/// Consumes the tail and returns a new instance of the [Head]
pub fn into_head(self) -> Head<Q, S> {
super::Head::new(self.state, self.symbol)
}
/// Consumes the tail and returns the direction, state, and symbol as a 3-tuple
pub fn into_tuple(self) -> (Direction, State<Q>, S) {
(self.direction, self.state, self.symbol)
Expand All @@ -63,6 +54,83 @@ impl<Q, S> Tail<Q, S> {
pub const fn write_symbol(&self) -> &S {
&self.symbol
}
/// Consumes the tail and returns a new instance of the [Head]
pub fn into_head(self) -> Head<Q, S> {
super::Head::new(self.state, self.symbol)
}
/// Returns an instance of the [head](Head) where each element within
/// the created instance is an immutable reference
pub fn to_head_ref<'a>(&'a self) -> Head<&'a Q, &'a S> {
super::Head::new(self.state.to_ref(), &self.symbol)
}

pub fn to_ref(&self) -> Tail<&'_ Q, &'_ S> {
Tail {
direction: self.direction,
state: self.state.to_ref(),
symbol: &self.symbol,
}
}

pub fn to_mut(&mut self) -> Tail<&'_ mut Q, &'_ mut S> {
Tail {
direction: self.direction,
state: self.state.to_mut(),
symbol: &mut self.symbol,
}
}

pub fn into_owned(self) -> Tail<Q, S>
where
Q: Clone,
S: Clone,
{
Tail {
direction: self.direction,
state: self.state,
symbol: self.symbol,
}
}

pub fn to_owned(&self) -> Tail<Q, S>
where
Q: Clone,
S: Clone,
{
Tail {
direction: self.direction,
state: self.state.to_owned(),
symbol: self.symbol.clone(),
}
}
}

impl<'a, Q, S> Tail<&'a Q, &'a S> {
pub fn cloned(&self) -> Tail<Q, S>
where
Q: Clone,
S: Clone,
{
Tail {
direction: self.direction,
state: self.state.cloned(),
symbol: self.symbol.clone(),
}
}
}

impl<'a, Q, S> Tail<&'a mut Q, &'a mut S> {
pub fn cloned(&self) -> Tail<Q, S>
where
Q: Clone,
S: Clone,
{
Tail {
direction: self.direction,
state: self.state.cloned(),
symbol: self.symbol.clone(),
}
}
}

mod builder {
Expand Down
12 changes: 10 additions & 2 deletions core/src/rules/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,29 @@ impl<Q, S> Program<Q, S> {
self.ruleset.get(idx)
}
/// Returns a collection of tails for a given head.
pub fn get_head(&self, head: &Head<Q, S>) -> Vec<&Tail<Q, S>>
pub fn get_head(&self, head: &Head<Q, S>) -> Vec<Tail<&'_ Q, &'_ S>>
where
Q: PartialEq,
S: PartialEq,
{
self.iter()
.filter_map(|i| {
if i.head() == head {
Some(i.tail())
Some(i.tail().to_ref())
} else {
None
}
})
.collect()
}

pub fn find_head(&self, head: Head<&'_ Q, &'_ S>) -> Option<&Tail<Q, S>>
where
Q: PartialEq,
S: PartialEq,
{
self.iter().find(|i| i.head().to_ref() == head).map(|i| i.tail())
}
}

impl<Q: Default, S> From<RuleSet<Q, S>> for Program<Q, S> {
Expand Down
3 changes: 2 additions & 1 deletion core/src/tape/tape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use core::cell::Cell;
/// This is done to quantify the impact of operations whose directions are defined to
/// be [Direction::Stay]. Moving left and right within a linear space speaks directly
/// to a translation or shift in space, however, staying in place does not result in
/// any movement, shift, or translation within space.
/// any movement, shift, or translation within space. That being said, staying still
/// is an operation that does result in some change in-time.
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct StdTape<S = char> {
Expand Down
45 changes: 12 additions & 33 deletions rstm/src/turing/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,37 +73,14 @@ impl<Q, S> TM<Q, S> {
pub fn tape_mut(&mut self) -> &mut StdTape<S> {
&mut self.tape
}
}

// #[cfg(feature = "std")]
impl<Q, S> TM<Q, S>
where
Q: Clone + PartialEq,
S: Symbolic,
{
#[cfg_attr(
feature = "tracing",
tracing::instrument(skip_all, name = "step", target = "fsm")
)]
pub fn step_inplace(&mut self) -> Result<Head<Q, S>, Error> {
#[cfg(feature = "tracing")]
tracing::info!("Stepping...");
let prog = self.program().clone();
// Create a new head from the current state and symbol
let head = self.head().cloned();
// Get the first instruction for the current head
if let Some(&tail) = prog.get_head(&head).first() {
let nxt = self.tape.update_inplace(tail.clone());
self.set_state(nxt);
return Ok(tail.as_head().cloned());
}
Err(Error::state_not_found())
}
/// Runs the program until the
///
/// The program will continue to run until the current state is a halt state.
#[cfg_attr(
feature = "tracing",
tracing::instrument(skip_all, name = "run", target = "fsm")
)]
pub fn run(mut self) -> Result<(), Error> {
pub fn run(mut self) -> Result<(), Error> where Q: Clone + PartialEq, S: Symbolic {
#[cfg(feature = "tracing")]
tracing::info!("Running the program...");
loop {
Expand Down Expand Up @@ -131,18 +108,20 @@ where
{
type Item = Head<Q, S>;

#[cfg_attr(
feature = "tracing",
tracing::instrument(skip_all, name = "step", target = "fsm")
)]
fn next(&mut self) -> Option<Self::Item> {
#[cfg(feature = "tracing")]
tracing::info!("Stepping...");
let prog = self.program().clone();
// Create a new head from the current state and symbol
let head = self.head().cloned();
// Get the first instruction for the current head
if let Some(&tail) = prog.get_head(&head).first() {
let nxt = self.tape_mut().update_inplace(tail.clone());
self.set_state(nxt);
return Some(tail.as_head().cloned());
if let Some(&tail) = self.program.get_head(&head).first() {
self.state = self.tape.update_inplace(tail.cloned());
return Some(tail.cloned().into_head());
}
self.step_inplace().ok()
None
}
}

0 comments on commit 5028018

Please sign in to comment.