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 11, 2024
1 parent 387cc4e commit f80fed8
Show file tree
Hide file tree
Showing 20 changed files with 276 additions and 87 deletions.
27 changes: 25 additions & 2 deletions core/src/actors/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
Appellation: actor <module>
Contrib: FL03 <jo3mccain@icloud.com>
*/
use crate::rules::Directive;
use super::Executor;
use crate::rules::{Directive, Program};
use crate::{Head, State, Symbolic};

/// [Actor] aptly describe the `TMH` model
Expand All @@ -20,6 +21,17 @@ impl<Q, S> Actor<Q, S> {
}
}

pub fn from_state(State(state): State<Q>) -> Self {
Self::new(State(state), Vec::new())
}

pub fn from_tape(tape: impl IntoIterator<Item = S>) -> Self
where
Q: Default,
{
Self::new(State::default(), tape)
}

pub const fn head(&self) -> &Head<Q, usize> {
&self.ptr
}
Expand All @@ -41,11 +53,22 @@ impl<Q, S> Actor<Q, S> {
}
}

pub fn run(self, program: Program<Q, S>) -> Executor<Q, S>
where
Q: Clone + Default + PartialEq + 'static,
S: Symbolic,
{
Executor::from_actor(self).with_program(program)
}

pub fn is_empty(&self) -> bool {
self.alpha.is_empty()
}

pub fn is_halted(&self) -> bool where Q: 'static {
pub fn is_halted(&self) -> bool
where
Q: 'static,
{
self.ptr.state.is_halt()
}

Expand Down
9 changes: 9 additions & 0 deletions core/src/actors/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ pub struct Executor<Q, S> {
}

impl<Q, S> Executor<Q, S> {
pub fn from_actor(actor: Actor<Q, S>) -> Self
where
Q: Default,
{
Self {
actor,
program: Program::new(),
}
}

pub fn with_program(self, program: Program<Q, S>) -> Self {
Executor { program, ..self }
Expand Down
1 change: 0 additions & 1 deletion core/src/macros/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ macro_rules! unit_impl_fmt {
$(impl_fmt!(@impl $trait::<$T>($($fmt)*));)*
};
}

8 changes: 7 additions & 1 deletion core/src/rules/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl<Q, S> Program<Q, S> {
pub fn iter_mut(&mut self) -> core::slice::IterMut<Instruction<Q, S>> {
self.ruleset.iter_mut()
}

pub fn get(&self, State(state): State<&Q>, symbol: &S) -> Option<&Tail<Q, S>>
where
Q: PartialEq,
Expand Down Expand Up @@ -184,6 +184,12 @@ impl<Q: Default, S> From<RuleSet<Q, S>> for Program<Q, S> {
}
}

impl<Q, S> Extend<Instruction<Q, S>> for Program<Q, S> {
fn extend<I: IntoIterator<Item = Instruction<Q, S>>>(&mut self, iter: I) {
self.ruleset.extend(iter)
}
}

impl<Q, S> FromIterator<Instruction<Q, S>> for Program<Q, S>
where
Q: Default,
Expand Down
13 changes: 12 additions & 1 deletion core/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,29 @@ pub type AnyState = State<Box<dyn std::any::Any + Send + Sync>>;
#[doc(hidden)]
pub trait RawState {
type Ctx;
}

pub trait Stated<Q>: RawState<Ctx = Q> {
fn cloned(&self) -> Self
where
Q: Clone;
fn copied(&self) -> Self
where
Q: Copy;
}

#[doc(hidden)]
pub trait Stateful<Q> {
type State: RawState<Ctx = Q>;
}


/*
************* Implementations *************
*/
impl<Q> RawState for Halt<Q> {
type Ctx = Q;
}

impl<Q> RawState for State<Q> {
type Ctx = Q;
}
Expand Down
10 changes: 8 additions & 2 deletions core/src/state/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ impl<Q> State<Q> {
{
core::any::TypeId::of::<Q>()
}
pub fn is_halt(&self) -> bool where Q: 'static {
pub fn is_halt(&self) -> bool
where
Q: 'static,
{
core::any::TypeId::of::<Self>() == core::any::TypeId::of::<State<Halt<Q>>>()
}
}
Expand All @@ -107,7 +110,10 @@ impl<Q> State<Halt<Q>> {
pub fn halt(Halt(inner): Halt<Q>) -> Self {
Self(Halt(inner))
}


pub fn unhalt(self) -> State<Q> {
State(self.0.into_inner())
}
}

impl<'a, Q> State<&'a Q> {
Expand Down
61 changes: 42 additions & 19 deletions core/src/state/states/halting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,69 @@
Appellation: halting <module>
Contrib: FL03 <jo3mccain@icloud.com>
*/
use crate::state::{RawState, State, Stateful};
use crate::state::{RawState, State};

#[doc(hidden)]
pub trait Halting {
pub trait Haltable<Q = String> {
const HALT: bool = true;

type State: RawState<Ctx = Q>;

private!();

}

#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Halt<Q>(pub Q);

pub struct Halt<T>(pub T);

impl<T> Halt<T> {
pub fn new(halt: T) -> Self {
impl<Q> Halt<Q> {
pub fn new(halt: Q) -> Self {
Self(halt)
}
pub fn into_inner(self) -> T {

pub fn into_inner(self) -> Q {
self.0
}
pub fn as_ref(&self) -> &T {
&self.0

pub fn as_ref<'a>(&'a self) -> Halt<&'a Q> {

Check warning

Code scanning / clippy

the following explicit lifetimes could be elided: 'a Warning

the following explicit lifetimes could be elided: 'a

Check warning

Code scanning / clippy

the following explicit lifetimes could be elided: 'a Warning

the following explicit lifetimes could be elided: 'a

Check warning

Code scanning / clippy

the following explicit lifetimes could be elided: 'a Warning

the following explicit lifetimes could be elided: 'a
Halt(&self.0)
}
pub fn as_mut(&mut self) -> &mut T {
&mut self.0

pub fn as_mut<'a>(&'a mut self) -> Halt<&'a mut Q> {

Check warning

Code scanning / clippy

the following explicit lifetimes could be elided: 'a Warning

the following explicit lifetimes could be elided: 'a

Check warning

Code scanning / clippy

the following explicit lifetimes could be elided: 'a Warning

the following explicit lifetimes could be elided: 'a

Check warning

Code scanning / clippy

the following explicit lifetimes could be elided: 'a Warning

the following explicit lifetimes could be elided: 'a
Halt(&mut self.0)
}
}

impl<Q> RawState for Halt<Q> {
type Ctx = Q;
pub fn as_state<'a>(&'a self) -> State<Halt<&'a Q>> {

Check warning

Code scanning / clippy

the following explicit lifetimes could be elided: 'a Warning

the following explicit lifetimes could be elided: 'a

Check warning

Code scanning / clippy

the following explicit lifetimes could be elided: 'a Warning

the following explicit lifetimes could be elided: 'a

Check warning

Code scanning / clippy

the following explicit lifetimes could be elided: 'a Warning

the following explicit lifetimes could be elided: 'a
State(Halt(&self.0))
}

pub fn cloned(&self) -> Self
where
Q: Clone,
{
Self(self.0.clone())
}

pub fn copied(&self) -> Self
where
Q: Copy,
{
Self(self.0)
}
}

impl<T> Stateful<T> for Halt<T> {
type State = Halt<T>;
impl<Q> From<State<Q>> for Halt<Q> {
fn from(State(state): State<Q>) -> Self {
Self(state)
}
}

impl<T> Halting for Halt<T> {
impl<Q> Haltable<Q> for Halt<Q> {
type State = State<Q>;
seal!();
}

impl<T> Halting for State<Halt<T>> {
impl<Q> Haltable<Q> for State<Halt<Q>> {
type State = State<Q>;
seal!();
}
48 changes: 48 additions & 0 deletions core/src/tape/cursor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Appellation: cursor <module>
Contrib: FL03 <jo3mccain@icloud.com>
*/

/// [Cursor] contains contextual information about the position of the head of a tape.
///
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Cursor<T = char> {
pub(crate) pos: usize,
pub(crate) ptr: *mut T,
pub(crate) step: usize,
}

impl<T> Cursor<T> {
pub fn new(ptr: *mut T) -> Self {
Self {
pos: 0,
ptr,
step: 0,
}
}

pub fn from_vec(vec: &mut Vec<T>) -> Self {
Self::new(vec.as_mut_ptr())
}

pub fn pointer(&self) -> *mut T {
self.ptr
}

pub fn position(&self) -> usize {
self.pos
}

pub fn step(&self) -> usize {
self.step
}

pub fn update(&mut self, cursor: usize) {
self.pos = cursor;
self.next();
}

fn next(&mut self) {
self.step += 1;
}
}
10 changes: 10 additions & 0 deletions core/src/tape/ds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Appellation: ds <module>
Contrib: FL03 <jo3mccain@icloud.com>
*/
#![allow(unused)]
/// [DoubleTape] preserves the input, recording all actions taken on a seperate tape.
pub struct DoubleTape<I = char, O = char> {
pub(crate) inputs: Vec<I>,
pub(crate) output: Vec<O>,
}
29 changes: 0 additions & 29 deletions core/src/tape/entry.rs

This file was deleted.

10 changes: 0 additions & 10 deletions core/src/tape/iter.rs

This file was deleted.

13 changes: 10 additions & 3 deletions core/src/tape/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ pub use self::tape::Tape;
pub(crate) mod tape;

#[doc(hidden)]
pub mod entry;
pub mod cursor;
#[doc(hidden)]
pub mod iter;
pub mod ds;

pub(crate) mod prelude {
pub use super::tape::Tape;
}

/// A trait for tape-like structures.
#[doc(hidden)]
pub trait RawTape {
type Elem;

private!();

fn as_slice(&self) -> &[Self::Elem];
}

Expand All @@ -33,6 +36,8 @@ pub trait RawTape {
impl<T> RawTape for [T] {
type Elem = T;

seal!();

fn as_slice(&self) -> &[Self::Elem] {
&self

Check warning

Code scanning / clippy

this expression creates a reference which is immediately dereferenced by the compiler Warning

this expression creates a reference which is immediately dereferenced by the compiler
}
Expand All @@ -41,7 +46,9 @@ impl<T> RawTape for [T] {
impl<T> RawTape for Vec<T> {
type Elem = T;

seal!();

fn as_slice(&self) -> &[Self::Elem] {
&self
Vec::as_slice(self)
}
}
Loading

0 comments on commit f80fed8

Please sign in to comment.