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 Aug 21, 2024
1 parent ff97712 commit 9755844
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 39 deletions.
38 changes: 13 additions & 25 deletions core/src/actors/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use crate::rules::{Directive, Program};
use crate::{Head, State};

/// An [Actor] describes a Turing machine with a moving head (TMH).
#[derive(Clone)]
///
/// [Actor]'s abstractly define actionable surfaces capable of executing a [Program].
#[derive(Clone, Default, Eq, Hash, PartialEq, PartialOrd)]
pub struct Actor<Q, S> {
/// the input alphabet
pub(crate) alpha: Vec<S>,
Expand Down Expand Up @@ -67,6 +69,8 @@ impl<Q, S> Actor<Q, S> {
D: Directive<Q, S>,
S: Clone,
{
#[cfg(feature = "tracing")]
tracing::debug!("Stepping the tape...");
self.write(rule.value().clone());
self.head.shift_inplace(rule.direction());
self.read()
Expand Down Expand Up @@ -119,24 +123,6 @@ impl<Q, S> Actor<Q, S> {
self.alpha.push(value);
}
}

pub fn print(&self) -> String
where
S: core::fmt::Display,
{
self.alpha
.iter()
.enumerate()
.map(|(i, c)| {
let c = c.to_string();
if i == self.cursor() {
format!("[{c}]")
} else {
format!("{c}")
}
})
.collect::<String>()
}
}

impl<Q, S> core::fmt::Debug for Actor<Q, S>
Expand All @@ -146,9 +132,10 @@ where
{
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
for (i, c) in self.alpha.iter().enumerate() {
match c {
b if i == self.cursor() => write!(f, "[{:?}, {b:?}]", self.head.state)?,
_ => write!(f, "{c:?}")?,
if i == self.cursor() {
write!(f, "[{c:?}]")?;
} else {
write!(f, "{c:?}")?;
}
}
Ok(())
Expand All @@ -162,9 +149,10 @@ where
{
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
for (i, c) in self.alpha.iter().enumerate() {
match c {
b if i == self.cursor() => write!(f, "[{}, {b}]", self.head.state)?,
_ => write!(f, "{c}")?,
if i == self.cursor() {
write!(f, "[{c}]")?;
} else {
write!(f, "{c}")?;
}
}
Ok(())
Expand Down
84 changes: 73 additions & 11 deletions core/src/types/head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@
*/
use crate::state::State;

/// The head of a turing machine generally speaks to the current state and symbol of the
/// machine w.r.t. the [tape](crate::Tape).
/// The [Head] struct represent the state and symbol of an actor at a given moment in time.
/// With respect to a Turing machine, the head defines the current state and symbol of the
/// machine. When associated with a direction the head becomes a tail, instructing the machine
/// to move, write, and transition to a new state.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize), serde(rename_all = "lowercase"))]
pub struct Head<Q = String, S = char> {
#[cfg_attr(
feature = "serde",
serde(
flatten,
alias = "state",
alias = "current_state",
alias = "head_state"
alias = "current_state"
)
)]
pub state: State<Q>,
#[cfg_attr(
feature = "serde",
serde(flatten, alias = "symbol", alias = "current_symbol", alias = "value")
serde(flatten, alias = "current_symbol")
)]
pub symbol: S,
}
Expand All @@ -45,7 +44,6 @@ impl<Q, S> Head<Q, S> {
pub fn as_mut_tuple(&mut self) -> (&mut State<Q>, &mut S) {
(&mut self.state, &mut self.symbol)
}

/// Updates the current [state](State)
pub fn set_state(&mut self, state: State<Q>) {
self.state = state;
Expand Down Expand Up @@ -79,7 +77,7 @@ impl<Q, S> Head<Q, S> {
self.symbol = symbol;
}
}

/// Converts the current head into a new head with immutable references to the current state and symbol
pub fn to_ref<'a>(&'a self) -> Head<&Q, &S>
where
Q: 'a,
Expand All @@ -90,7 +88,7 @@ impl<Q, S> Head<Q, S> {
symbol: &self.symbol,
}
}

/// Converts the current head into a new head with mutable references to the current state and symbol
pub fn to_mut<'a>(&'a mut self) -> Head<&mut Q, &mut S>
where
Q: 'a,
Expand All @@ -104,6 +102,10 @@ impl<Q, S> Head<Q, S> {
}

impl<Q> Head<Q, usize> {
pub fn read_tape<'a, S>(&self, tape: &'a [S]) -> Option<&'a S> {
tape.get(self.symbol)
}

pub fn shift(self, direction: crate::Direction) -> Self {
Self {
symbol: direction.apply(self.symbol),
Expand All @@ -127,8 +129,68 @@ impl<'a, Q, S> Head<&'a Q, &'a S> {
symbol: self.symbol.clone(),
}
}

pub fn copied(&self) -> Head<Q, S>
where
Q: Copy,
S: Copy,
{
Head {
state: self.state.copied(),
symbol: self.symbol.clone(),

Check warning

Code scanning / clippy

using clone on type S which implements the Copy trait Warning

using clone on type S which implements the Copy trait
}
}
}

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

pub fn copied(&self) -> Head<Q, S>
where
Q: Copy,
S: Copy,
{
Head {
state: self.state.copied(),
symbol: *self.symbol,
}
}
}

impl<'a, Q, S> Head<*const Q, *const S> {

Check warning

Code scanning / clippy

this lifetime isn't used in the impl Warning

this lifetime isn't used in the impl
pub fn cloned(&self) -> Head<*const Q, *const S>
where
Q: Clone,
S: Clone,
{
Head {
state: self.state.clone(),

Check warning

Code scanning / clippy

using clone on type State<*const Q> which implements the Copy trait Warning

using clone on type State<*const Q> which implements the Copy trait
symbol: self.symbol.clone(),

Check warning

Code scanning / clippy

using clone on type *const S which implements the Copy trait Warning

using clone on type \*const S which implements the Copy trait
}
}

pub unsafe fn copied(&self) -> Head<Q, S>
where
Q: Copy,
S: Copy,

Check warning

Code scanning / clippy

unsafe function's docs miss # Safety section Warning

unsafe function's docs miss # Safety section
{
Head {
state: State(*self.state.0),
symbol: *self.symbol,
}
}
}


impl<Q, S> From<(Q, S)> for Head<Q, S> {
fn from((state, symbol): (Q, S)) -> Self {
Self::new(State(state), symbol)
Expand Down
6 changes: 3 additions & 3 deletions rstm/src/turing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use self::{model::Turing, state::TMS};
pub(crate) mod model;
pub(crate) mod state;

use crate::prelude::{Error, Head, Symbolic, Tape};
use crate::prelude::{Error, Head, Symbolic, Tail, Tape};
use crate::rules::Program;
use crate::state::State;

Expand Down Expand Up @@ -113,8 +113,8 @@ impl<Q, S> TM<Q, S> {
return None;
}
// Get the first instruction for the current head
if let Some(tail) = self.program.get_head_ref(self.read()?) {
self.state = self.tape.apply_inplace(tail.cloned());
if let Some(Tail { direction, state, symbol }) = self.program.get_head_ref(self.read()?).map(|tail| tail.cloned()) {
self.state = self.tape.update_inplace(direction, state, symbol);
return self.read();
}
unreachable!("No instruction found for the current head")
Expand Down

0 comments on commit 9755844

Please sign in to comment.