Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: FL03 <jo3mccain@icloud.com>
  • Loading branch information
FL03 committed Sep 7, 2024
1 parent 4b08301 commit d197c01
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 145 deletions.
13 changes: 13 additions & 0 deletions rstm/src/exp/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
Appellation: exp <module>
Contrib: FL03 <jo3mccain@icloud.com>
*/
//! # Experimental (exp)
//!
//!
#![allow(unused)]

#[doc(hidden)]
pub mod model;
#[doc(hidden)]
pub mod shift;
101 changes: 45 additions & 56 deletions rstm/src/models/base.rs → rstm/src/exp/model/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
Appellation: tm <module>
Contrib: FL03 <jo3mccain@icloud.com>
*/
use crate::prelude::{Error, Head, StdTape, Symbolic, Tail};
use crate::prelude::{Direction, Error, Head, Rule, StdTape, Symbolic, Tail};
use crate::rules::Ruleset;
use crate::state::{halt::HaltState, State};
use crate::state::{halt::HaltState, RawState, State};

/// # Turing Machine ([StdTm])
///
Expand All @@ -13,55 +13,44 @@ use crate::state::{halt::HaltState, State};
/// Each pre-defined rule maps a head, consisting of a state and symbol, to a new state and symbol along with a direction.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct StdTM<Q = String, S = char> {
pub(crate) program: Ruleset<Q, S>,
pub(crate) state: HaltState<Q>,
pub(crate) tape: StdTape<S>,
pub struct StdTM<Q, A> {
pub(crate) program: Ruleset<Q, A>,
pub(crate) state: State<Option<Q>>,
pub(crate) tape: StdTape<A>,
}

impl<Q, S> StdTM<Q, S> {
pub fn new(program: Ruleset<Q, S>, tape: StdTape<S>) -> Self
impl<Q, A> StdTM<Q, A> {
pub fn new(rules: impl IntoIterator<Item = Rule<Q, A>>, tape: StdTape<A>) -> Self
where
Q: Clone + Default,
S: Default,
A: Default,
{
let state = program
.initial_state()
.map(|q| q.cloned())
.unwrap_or_default();
StdTM {
program,
state: HaltState::state(state),
program: Ruleset::from_iter(rules),
state: State::none(),
tape,
}
}
/// Returns an immutable reference to the [program](Program)
pub const fn program(&self) -> &Ruleset<Q, S> {
pub const fn program(&self) -> &Ruleset<Q, A> {
&self.program
}
/// Creates a new instance of a [head](Head) from references to the current state and symbol;
pub fn read(&self) -> Option<Head<&'_ Q, &'_ S>> {
pub fn read(&self) -> Option<Head<&'_ Option<Q>, &'_ A>> {
self.tape()
.read()
.ok()
.map(|symbol| Head::new(self.state(), symbol))
}
/// Returns an instance of the [state](State) with an immutable
/// reference to the internal data
pub fn state(&self) -> State<&'_ Q> {
self.state.as_state()
}
/// Returns an instance of the [state](State) with a mutable
/// reference to the internal data
pub fn state_mut(&mut self) -> State<&'_ mut Q> {
self.state.as_mut_state()

pub fn state(&self) -> State<&Option<Q>> {
self.state.to_ref()
}
/// Returns an immutable reference to the [tape](StdTape)
pub const fn tape(&self) -> &StdTape<S> {
pub const fn tape(&self) -> &StdTape<A> {
&self.tape
}
/// Returns a mutable reference to the [tape](StdTape)
pub fn tape_mut(&mut self) -> &mut StdTape<S> {
pub fn tape_mut(&mut self) -> &mut StdTape<A> {
&mut self.tape
}
/// Runs the program until the
Expand All @@ -74,7 +63,7 @@ impl<Q, S> StdTM<Q, S> {
pub fn execute(mut self) -> Result<(), Error>
where
Q: Clone + PartialEq + 'static,
S: Symbolic,
A: Symbolic,
{
#[cfg(feature = "tracing")]
tracing::trace!("Executing the program...");
Expand All @@ -96,41 +85,41 @@ impl<Q, S> StdTM<Q, S> {
}
}

fn handle(&mut self, direction: Direction, state: State<Option<Q>>, symbol: A) {
self.tape.update(direction, symbol);
self.state = state.into();

Check warning

Code scanning / clippy

useless conversion to the same type: rstm_core::State<std::option::Option<Q>> Warning

useless conversion to the same type: rstm_core::State<std::option::Option<Q>>
}

#[cfg_attr(
feature = "tracing",
tracing::instrument(skip_all, name = "process", target = "turing")
)]
fn process(&mut self) -> Option<Head<&'_ Q, &'_ S>>
fn process(&mut self) -> Option<Head<&'_ Q, &'_ A>>
where
Q: Clone + PartialEq,
S: Clone + PartialEq,
A: Clone + PartialEq,
{
#[cfg(feature = "tracing")]
tracing::trace!("Processing the current instruction...");
// Get the first instruction for the current head
if let Some(Tail {
direction,
state,
symbol,
}) = self.program.get_ref(self.read()?)
{
//
self.tape.update(direction, symbol.clone());
self.state = state.cloned().into();
return Some(Head::new(state, symbol));
let csymbol = self.tape.read().ok()?;
if let Some(cstate) = self.state().get() {
if let Some(Tail {
direction,
state,
symbol,
}) = self.program.get(State(cstate), csymbol)
{
//
self.tape.update(*direction, symbol.clone());
self.state = state.clone().map(Some);
return Some(Head::new(State(state), symbol));
}
unreachable!("No instruction found for the current head")
} else {
#[cfg(feature = "tracing")]
tracing::trace!("Head is halted, terminating execution...");
return None;
}
unreachable!("No instruction found for the current head")
}
}

impl<Q, S> core::iter::Iterator for StdTM<Q, S>
where
Q: Clone + PartialEq + 'static,
S: Clone + PartialEq,
{
type Item = Head<Q, S>;

fn next(&mut self) -> Option<Self::Item> {
self.process().map(|i| i.cloned())

}
}
5 changes: 3 additions & 2 deletions rstm/src/models/mod.rs → rstm/src/exp/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
Contrib: FL03 <jo3mccain@icloud.com>
*/
#[doc(inline)]
pub use self::base::StdTM;
pub use self::{base::StdTM, tmh::TMH};

pub mod base;
pub mod engine;
pub mod tmh;

pub(crate) mod prelude {
pub use super::base::StdTM;
pub use super::tmh::TMH;
}
File renamed without changes.
10 changes: 10 additions & 0 deletions rstm/src/exp/shift/direction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Appellation: direction <module>
Contrib: FL03 <jo3mccain@icloud.com>
*/
use crate::prelude::Direction;

pub struct LinearShift<T: ?Sized> {
pub(crate) direction: Direction,
pub(crate) value: T,
}
8 changes: 7 additions & 1 deletion rstm/src/shift/mod.rs → rstm/src/exp/shift/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Contrib: FL03 <jo3mccain@icloud.com>
*/
#[doc(inline)]
pub use self::direction::{LinearShift, ShiftDirection};
pub use self::direction::*;

pub(crate) mod direction;

Expand All @@ -20,3 +20,9 @@ pub trait Shift<T> {

fn shift(&self, step: T) -> Self::Output;
}

pub trait ApplyOnce<T, F> where F: FnOnce(T) -> Self::Output {
type Output;

fn apply(&self, f: F, args: T) -> F::Output;
}
13 changes: 5 additions & 8 deletions rstm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,19 @@
//! The crate is designed to be flexible and easy to use while preserving the abstract nature
//! of the models.
// #![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "std"), no_std)]
#![crate_name = "rstm"]
#![crate_type = "lib"]

#[cfg(feature = "alloc")]
extern crate alloc;

#[doc(inline)]
pub use self::models::StdTM;
#[doc(inline)]
pub use rstm_core::*;

#[doc(hidden)]
pub mod models;
#[doc(hidden)]
pub mod shift;
pub mod exp;

pub mod prelude {
pub use super::models::prelude::*;

pub use rstm_core::prelude::*;
}
78 changes: 0 additions & 78 deletions rstm/src/shift/direction.rs

This file was deleted.

0 comments on commit d197c01

Please sign in to comment.