diff --git a/core/src/actors/actor.rs b/core/src/actors/actor.rs index 34d5ad1..c6a5409 100644 --- a/core/src/actors/actor.rs +++ b/core/src/actors/actor.rs @@ -3,7 +3,7 @@ Contrib: FL03 */ use super::Executor; -use crate::rules::Ruleset; +use crate::rules::RuleSet; use crate::{Direction, Error, Head, State}; /// An [Actor] is an implementation of a Turing machine with a moving head (TMH). @@ -71,7 +71,7 @@ impl Actor { &self.tape } /// Returns a mutable reference of the tape as a slice - pub fn tape_mu(&mut self) -> &mut [A] { + pub fn tape_mut(&mut self) -> &mut [A] { &mut self.tape } /// Returns an immutable reference to the head of the tape @@ -104,7 +104,7 @@ impl Actor { } /// Executes the given program; the method is lazy, meaning it will not compute immediately /// but will return an [Executor] that is better suited for managing the runtime. - pub fn execute(self, program: Ruleset) -> Executor { + pub fn execute(self, program: RuleSet) -> Executor { Executor::new(self, program) } /// Checks if the tape is empty diff --git a/core/src/actors/exec.rs b/core/src/actors/exec.rs index f3cce45..c9128ce 100644 --- a/core/src/actors/exec.rs +++ b/core/src/actors/exec.rs @@ -3,7 +3,7 @@ Contrib: FL03 */ use super::Actor; -use crate::{Error, Head, Ruleset, State, Symbolic}; +use crate::{Error, Head, RuleSet, State, Symbolic}; /// # [Executor] /// @@ -14,13 +14,13 @@ pub struct Executor { /// the actor that will be executing the program pub(crate) actor: Actor, /// the program being executed - pub(crate) program: Ruleset, + pub(crate) program: RuleSet, /// the number of steps taken by the actor pub(crate) steps: usize, } impl Executor { - pub(crate) fn new(actor: Actor, program: Ruleset) -> Self { + pub(crate) fn new(actor: Actor, program: RuleSet) -> Self { Self { actor, program, @@ -34,7 +34,7 @@ impl Executor { { Self { actor, - program: Ruleset { + program: RuleSet { initial_state: Default::default(), rules: Vec::new(), }, @@ -42,7 +42,7 @@ impl Executor { } } /// Load a program into the executor - pub fn load(self, program: Ruleset) -> Self { + pub fn load(self, program: RuleSet) -> Self { Executor { program, ..self } } @@ -50,6 +50,10 @@ impl Executor { &self.actor } + pub fn steps(&self) -> usize { + self.steps + } + pub fn current_state(&self) -> State<&'_ Q> { self.actor.state() } @@ -58,6 +62,14 @@ impl Executor { self.actor.read() } + /// Reads the current symbol at the head of the tape + pub fn read_or_default(&self) -> Head<&Q, &S> { + self.actor.read().unwrap_or_else(|_| Head { + state: self.actor.state(), + symbol: &S::default(), + }) + } + #[cfg_attr( feature = "tracing", tracing::instrument(skip_all, name = "run", target = "actor") diff --git a/core/src/actors/mod.rs b/core/src/actors/mod.rs index 9c03694..435f140 100644 --- a/core/src/actors/mod.rs +++ b/core/src/actors/mod.rs @@ -19,17 +19,17 @@ pub(crate) mod prelude { use crate::{Direction, Error, Head, Rule, State, Tail}; -/// [Handle] describes the step-by-step execution of a program; the trait is generalized -/// with the introduction of a single generic parameter, [Args], capable of sufficiently +/// [Handle] describes the step-by-step execution of a program; the trait is generalized +/// with the introduction of a single generic parameter, `T`, capable of sufficiently /// representing any possible object that may be passed to the [handle] method. -/// -/// This notion is particularly useful as it allows us to define the process using an actor, -/// before generically implementing the [Engine] trait for the [Executor] struct. Doing so -/// allows for further abstraction by considering the -pub trait Handle { +/// +/// This notion is particularly useful as it allows us to define the process using an actor, +/// before generically implementing the [Engine] trait for the [Executor] struct. Doing so +/// allows for further abstraction by considering the +pub trait Handle { type Output; - fn handle(&mut self, args: Args) -> Self::Output; + fn handle(&mut self, args: T) -> Self::Output; } #[doc(hidden)] diff --git a/core/src/lib.rs b/core/src/lib.rs index 9cebb5f..bc9cb4d 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -25,9 +25,9 @@ extern crate alloc; #[doc(inline)] pub use self::{ - actors::Actor, + actors::{Actor, Executor, Handle}, error::Error, - rules::{Rule, Ruleset}, + rules::{Rule, RuleSet}, state::State, traits::prelude::*, types::prelude::*, diff --git a/core/src/mem/memory.rs b/core/src/mem/memory.rs new file mode 100644 index 0000000..8317c93 --- /dev/null +++ b/core/src/mem/memory.rs @@ -0,0 +1,215 @@ +/* + Appellation: store + Contrib: FL03 +*/ + +/// [RawMemory] is a trait that provides a common interface for memory storage. +pub trait RawMemory { + type Elem; + + private!(); + + fn is_empty(&self) -> bool { + self.len() == 0 + } + + fn len(&self) -> usize; +} + +pub trait Memory: RawMemory { + fn to_vec(&self) -> Vec + where + Self::Elem: Clone; +} + +pub trait MemoryMut: Memory { + fn clear(&mut self); + + fn insert(&mut self, index: usize, elem: Self::Elem); + + fn remove(&mut self, index: usize) -> Option; +} + +/// [Sequential] extends the base trait [RawMemory] to provide sequential access to memory. +pub trait SeqMemory: Memory { + fn as_ptr(&self) -> *const Self::Elem; + + fn as_mut_ptr(&mut self) -> *mut Self::Elem; + + fn as_slice(&self) -> &[Self::Elem]; + + fn as_mut_slice(&mut self) -> &mut [Self::Elem]; + + fn get(&self, index: I) -> Option<&I::Output> + where + I: core::slice::SliceIndex<[Self::Elem]>; + + fn get_mut(&mut self, index: I) -> Option<&mut I::Output> + where + I: core::slice::SliceIndex<[Self::Elem]>; +} + +#[doc(hidden)] +pub trait Fetch { + type Output; + + fn fetch(&self, index: K) -> Option<&Self::Output>; + + fn fetch_mut(&mut self, index: K) -> Option<&mut Self::Output>; +} + +/* + ************* Implementations ************* +*/ +impl Fetch for [T] +where + I: core::slice::SliceIndex<[T]>, + I::Output: Sized, +{ + type Output = I::Output; + + fn fetch(&self, index: I) -> Option<&Self::Output> { + <[T]>::get(self, index) + } + + fn fetch_mut(&mut self, index: I) -> Option<&mut Self::Output> { + <[T]>::get_mut(self, index) + } +} + +impl RawMemory for [T] { + type Elem = T; + + seal!(); + + fn is_empty(&self) -> bool { + <[T]>::is_empty(self) + } + + fn len(&self) -> usize { + <[T]>::len(self) + } +} + +impl Memory for [T] { + fn to_vec(&self) -> Vec + where + T: Clone, + { + <[T]>::to_vec(self) + } +} + +impl SeqMemory for [T] { + fn as_ptr(&self) -> *const T { + <[T]>::as_ptr(self) + } + + fn as_mut_ptr(&mut self) -> *mut T { + <[T]>::as_mut_ptr(self) + } + + fn as_slice(&self) -> &[T] { + self + } + + fn as_mut_slice(&mut self) -> &mut [T] { + self + } + + fn get(&self, index: I) -> Option<&I::Output> + where + I: core::slice::SliceIndex<[T]>, + { + <[T]>::get(self, index) + } + + fn get_mut(&mut self, index: I) -> Option<&mut I::Output> + where + I: core::slice::SliceIndex<[T]>, + { + <[T]>::get_mut(self, index) + } +} + +#[cfg(feature = "alloc")] +mod impl_alloc { + use alloc::vec::Vec; + + impl super::RawMemory for Vec { + type Elem = T; + + seal!(); + + fn is_empty(&self) -> bool { + Vec::is_empty(self) + } + + fn len(&self) -> usize { + Vec::len(self) + } + } + + impl super::Memory for Vec { + fn to_vec(&self) -> Vec + where + T: Clone, + { + self.clone() + } + } + + impl super::SeqMemory for Vec + where + Vec: AsRef<[T]>, + { + fn as_ptr(&self) -> *const T { + Vec::as_ptr(self) + } + + fn as_mut_ptr(&mut self) -> *mut T { + Vec::as_mut_ptr(self) + } + + fn as_slice(&self) -> &[T] { + Vec::as_slice(self) + } + + fn as_mut_slice(&mut self) -> &mut [T] { + Vec::as_mut_slice(self) + } + + fn get(&self, index: I) -> Option<&I::Output> + where + I: core::slice::SliceIndex<[T]>, + { + <[T]>::get(self, index) + } + + fn get_mut(&mut self, index: I) -> Option<&mut I::Output> + where + I: core::slice::SliceIndex<[T]>, + { + <[T]>::get_mut(self, index) + } + } +} + +#[cfg(feature = "std")] +mod impl_std { + use std::collections::HashMap; + + impl super::RawMemory for HashMap { + type Elem = V; + + seal!(); + + fn is_empty(&self) -> bool { + HashMap::is_empty(self) + } + + fn len(&self) -> usize { + HashMap::len(self) + } + } +} diff --git a/core/src/mem/mod.rs b/core/src/mem/mod.rs index 74573bd..8736ecc 100644 --- a/core/src/mem/mod.rs +++ b/core/src/mem/mod.rs @@ -6,11 +6,15 @@ //! //! #[doc(inline)] -pub use self::store::*; +pub use self::memory::*; +pub mod memory; +#[doc(hidden)] pub mod store; pub mod tape; pub(crate) mod prelude { pub use super::tape::prelude::*; } + +pub(crate) type Cell = crate::Head; diff --git a/core/src/mem/store.rs b/core/src/mem/store.rs index 8317c93..e0f76f4 100644 --- a/core/src/mem/store.rs +++ b/core/src/mem/store.rs @@ -1,215 +1,40 @@ /* - Appellation: store + Appellation: container Contrib: FL03 */ - -/// [RawMemory] is a trait that provides a common interface for memory storage. -pub trait RawMemory { - type Elem; - - private!(); - - fn is_empty(&self) -> bool { - self.len() == 0 - } - - fn len(&self) -> usize; -} - -pub trait Memory: RawMemory { - fn to_vec(&self) -> Vec - where - Self::Elem: Clone; -} - -pub trait MemoryMut: Memory { - fn clear(&mut self); - - fn insert(&mut self, index: usize, elem: Self::Elem); - - fn remove(&mut self, index: usize) -> Option; -} - -/// [Sequential] extends the base trait [RawMemory] to provide sequential access to memory. -pub trait SeqMemory: Memory { - fn as_ptr(&self) -> *const Self::Elem; - - fn as_mut_ptr(&mut self) -> *mut Self::Elem; - - fn as_slice(&self) -> &[Self::Elem]; - - fn as_mut_slice(&mut self) -> &mut [Self::Elem]; - - fn get(&self, index: I) -> Option<&I::Output> - where - I: core::slice::SliceIndex<[Self::Elem]>; - - fn get_mut(&mut self, index: I) -> Option<&mut I::Output> - where - I: core::slice::SliceIndex<[Self::Elem]>; -} +use crate::{Rule, State}; #[doc(hidden)] -pub trait Fetch { - type Output; +pub trait GetRule { + fn get(&self, state: State<&Q>, symbol: &S) -> Option<&Rule>; - fn fetch(&self, index: K) -> Option<&Self::Output>; + fn get_mut(&mut self, state: State<&Q>, symbol: &S) -> Option<&mut Rule>; +} - fn fetch_mut(&mut self, index: K) -> Option<&mut Self::Output>; +pub trait RawContainer { + type Elem; } /* ************* Implementations ************* */ -impl Fetch for [T] -where - I: core::slice::SliceIndex<[T]>, - I::Output: Sized, -{ - type Output = I::Output; - - fn fetch(&self, index: I) -> Option<&Self::Output> { - <[T]>::get(self, index) - } - - fn fetch_mut(&mut self, index: I) -> Option<&mut Self::Output> { - <[T]>::get_mut(self, index) - } -} -impl RawMemory for [T] { +impl RawContainer for Vec { type Elem = T; - - seal!(); - - fn is_empty(&self) -> bool { - <[T]>::is_empty(self) - } - - fn len(&self) -> usize { - <[T]>::len(self) - } } -impl Memory for [T] { - fn to_vec(&self) -> Vec - where - T: Clone, - { - <[T]>::to_vec(self) - } +impl RawContainer for Box<[T]> { + type Elem = T; } -impl SeqMemory for [T] { - fn as_ptr(&self) -> *const T { - <[T]>::as_ptr(self) - } - - fn as_mut_ptr(&mut self) -> *mut T { - <[T]>::as_mut_ptr(self) - } - - fn as_slice(&self) -> &[T] { - self - } - - fn as_mut_slice(&mut self) -> &mut [T] { - self - } - - fn get(&self, index: I) -> Option<&I::Output> - where - I: core::slice::SliceIndex<[T]>, - { - <[T]>::get(self, index) - } - - fn get_mut(&mut self, index: I) -> Option<&mut I::Output> - where - I: core::slice::SliceIndex<[T]>, - { - <[T]>::get_mut(self, index) - } +impl RawContainer for [T] { + type Elem = T; } -#[cfg(feature = "alloc")] -mod impl_alloc { - use alloc::vec::Vec; - - impl super::RawMemory for Vec { - type Elem = T; - - seal!(); - - fn is_empty(&self) -> bool { - Vec::is_empty(self) - } - - fn len(&self) -> usize { - Vec::len(self) - } - } - - impl super::Memory for Vec { - fn to_vec(&self) -> Vec - where - T: Clone, - { - self.clone() - } - } - - impl super::SeqMemory for Vec - where - Vec: AsRef<[T]>, - { - fn as_ptr(&self) -> *const T { - Vec::as_ptr(self) - } - - fn as_mut_ptr(&mut self) -> *mut T { - Vec::as_mut_ptr(self) - } - - fn as_slice(&self) -> &[T] { - Vec::as_slice(self) - } - - fn as_mut_slice(&mut self) -> &mut [T] { - Vec::as_mut_slice(self) - } - - fn get(&self, index: I) -> Option<&I::Output> - where - I: core::slice::SliceIndex<[T]>, - { - <[T]>::get(self, index) - } - - fn get_mut(&mut self, index: I) -> Option<&mut I::Output> - where - I: core::slice::SliceIndex<[T]>, - { - <[T]>::get_mut(self, index) - } - } +impl RawContainer for &mut [T] { + type Elem = T; } -#[cfg(feature = "std")] -mod impl_std { - use std::collections::HashMap; - - impl super::RawMemory for HashMap { - type Elem = V; - - seal!(); - - fn is_empty(&self) -> bool { - HashMap::is_empty(self) - } - - fn len(&self) -> usize { - HashMap::len(self) - } - } +impl RawContainer for &mut [T; N] { + type Elem = T; } diff --git a/core/src/rules/builders/rule.rs b/core/src/rules/builders/rule.rs deleted file mode 100644 index 91f58c7..0000000 --- a/core/src/rules/builders/rule.rs +++ /dev/null @@ -1,83 +0,0 @@ -/* - Appellation: rule - Contrib: FL03 -*/ -use crate::rules::Rule; -use crate::{Direction, State}; - -#[derive(Default)] -pub struct RuleBuilder { - direction: Direction, - state: Option>, - symbol: Option, - next_state: Option>, - write_symbol: Option, -} - -impl RuleBuilder { - pub fn new() -> Self { - Self { - direction: Direction::Right, - state: None, - symbol: None, - next_state: None, - write_symbol: None, - } - } - - pub fn direction(self, direction: Direction) -> Self { - Self { direction, ..self } - } - - pub fn left(self) -> Self { - self.direction(Direction::Left) - } - - pub fn state(self, state: State) -> Self { - Self { - state: Some(state), - ..self - } - } - - pub fn symbol(self, symbol: S) -> Self { - Self { - symbol: Some(symbol), - ..self - } - } - - pub fn next_state(self, State(state): State) -> Self { - Self { - next_state: Some(State(state)), - ..self - } - } - - pub fn write_symbol(self, write_symbol: S) -> Self { - Self { - write_symbol: Some(write_symbol), - ..self - } - } - - pub fn build(self) -> Rule { - Rule { - head: crate::Head { - state: self.state.expect("state is required"), - symbol: self.symbol.expect("symbol is required"), - }, - tail: crate::Tail { - direction: self.direction, - state: self.next_state.expect("next_state is required"), - symbol: self.write_symbol.expect("write_symbol is required"), - }, - } - } -} - -impl From> for Rule { - fn from(builder: RuleBuilder) -> Self { - builder.build() - } -} diff --git a/core/src/rules/impls/impl_rule.rs b/core/src/rules/impls/impl_rule.rs new file mode 100644 index 0000000..2955c05 --- /dev/null +++ b/core/src/rules/impls/impl_rule.rs @@ -0,0 +1,96 @@ +/* + Appellation: impl_rule + Contrib: FL03 +*/ +use crate::rule::Rule; +use crate::types::{Head, Tail}; + +impl core::convert::AsRef> for Rule { + fn as_ref(&self) -> &Head { + self.head() + } +} + +impl core::convert::AsRef> for Rule { + fn as_ref(&self) -> &Tail { + self.tail() + } +} + +impl core::convert::AsMut> for Rule { + fn as_mut(&mut self) -> &mut Head { + self.head_mut() + } +} + +impl core::convert::AsMut> for Rule { + fn as_mut(&mut self) -> &mut Tail { + self.tail_mut() + } +} + +impl core::borrow::Borrow> for Rule { + fn borrow(&self) -> &Head { + self.head() + } +} + +impl core::borrow::Borrow> for Rule { + fn borrow(&self) -> &Tail { + self.tail() + } +} + +impl core::borrow::BorrowMut> for Rule { + fn borrow_mut(&mut self) -> &mut Head { + self.head_mut() + } +} + +impl core::borrow::BorrowMut> for Rule { + fn borrow_mut(&mut self) -> &mut Tail { + self.tail_mut() + } +} + +impl PartialEq<(Head, Tail)> for Rule +where + Q: PartialEq, + S: PartialEq, +{ + fn eq(&self, other: &(Head, Tail)) -> bool { + self.head == other.0 && self.tail == other.1 + } +} + +impl PartialEq> for Rule +where + Q: PartialEq, + S: PartialEq, +{ + fn eq(&self, other: &Head) -> bool { + &self.head == other + } +} + +impl PartialEq> for Rule +where + Q: PartialEq, + S: PartialEq, +{ + fn eq(&self, other: &Tail) -> bool { + &self.tail == other + } +} + +impl From<(Head, Tail)> for Rule { + fn from((head, tail): (Head, Tail)) -> Self { + Self { head, tail } + } +} + +impl From> for (Head, Tail) { + fn from(rule: Rule) -> Self { + (rule.head, rule.tail) + } +} diff --git a/core/src/rules/impls/impl_rule_repr.rs b/core/src/rules/impls/impl_rule_repr.rs new file mode 100644 index 0000000..db22d36 --- /dev/null +++ b/core/src/rules/impls/impl_rule_repr.rs @@ -0,0 +1,29 @@ +/* + Appellation: impl_rule_repr + Contrib: FL03 +*/ +use crate::rule::Rule; + +impl<'a, 'b, Q, A> Rule<&'a Q, &'b A> { + pub fn cloned(&self) -> Rule + where + Q: Clone, + A: Clone, + { + Rule { + head: self.head.cloned(), + tail: self.tail.cloned(), + } + } + + pub fn copied(&self) -> Rule + where + Q: Copy, + A: Copy, + { + Rule { + head: self.head.copied(), + tail: self.tail.copied(), + } + } +} diff --git a/core/src/rules/mod.rs b/core/src/rules/mod.rs index 51d66a2..3d9729f 100644 --- a/core/src/rules/mod.rs +++ b/core/src/rules/mod.rs @@ -3,22 +3,26 @@ Contrib: FL03 */ #[doc(inline)] -pub use self::{builders::RuleBuilder, program::Ruleset, rule::Rule, ruleset::RuleMap}; +pub use self::{ + rule::{Rule, RuleBuilder}, + rule_map::RuleMap, + rule_set::RuleSet, +}; -pub(crate) mod program; pub(crate) mod rule; -pub mod ruleset; -#[doc(hidden)] -mod builders { - pub use self::rule::RuleBuilder; +pub mod rule_map; +pub mod rule_set; - mod rule; +#[doc(hidden)] +mod impls { + pub mod impl_rule; + pub mod impl_rule_repr; } pub(crate) mod prelude { - pub use super::program::Ruleset; pub use super::rule::Rule; + pub use super::rule_set::RuleSet; pub use super::{Directive, Scope, Transition}; } diff --git a/core/src/rules/rule.rs b/core/src/rules/rule.rs index 37c8530..fa2eb53 100644 --- a/core/src/rules/rule.rs +++ b/core/src/rules/rule.rs @@ -2,8 +2,7 @@ Appellation: instruction Contrib: FL03 */ -use super::RuleBuilder; - +pub use self::builder::RuleBuilder; use crate::{Head, State, Tail}; #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Ord, PartialOrd)] @@ -75,121 +74,84 @@ impl Rule { } } -impl<'a, Q, A> Rule<&'a Q, &'a A> { - pub fn cloned(&self) -> Rule - where - Q: Clone, - A: Clone, - { - Rule { - head: self.head.cloned(), - tail: self.tail.cloned(), - } - } - - pub fn copied(&self) -> Rule - where - Q: Copy, - A: Copy, - { - Rule { - head: self.head.copied(), - tail: self.tail.copied(), - } - } -} - -mod impls { - use super::Rule; - use crate::{Head, Tail}; - - impl core::convert::AsRef> for Rule { - fn as_ref(&self) -> &Head { - self.head() - } - } - - impl core::convert::AsRef> for Rule { - fn as_ref(&self) -> &Tail { - self.tail() - } - } - - impl core::convert::AsMut> for Rule { - fn as_mut(&mut self) -> &mut Head { - self.head_mut() - } - } - - impl core::convert::AsMut> for Rule { - fn as_mut(&mut self) -> &mut Tail { - self.tail_mut() - } - } - - impl core::borrow::Borrow> for Rule { - fn borrow(&self) -> &Head { - self.head() +mod builder { + use crate::rules::Rule; + use crate::{Direction, State}; + + #[derive(Default)] + pub struct RuleBuilder { + direction: Direction, + state: Option>, + symbol: Option, + next_state: Option>, + write_symbol: Option, + } + + impl RuleBuilder { + pub fn new() -> Self { + Self { + direction: Direction::Right, + state: None, + symbol: None, + next_state: None, + write_symbol: None, + } } - } - impl core::borrow::Borrow> for Rule { - fn borrow(&self) -> &Tail { - self.tail() + pub fn direction(self, direction: Direction) -> Self { + Self { direction, ..self } } - } - impl core::borrow::BorrowMut> for Rule { - fn borrow_mut(&mut self) -> &mut Head { - self.head_mut() + pub fn left(self) -> Self { + self.direction(Direction::Left) } - } - impl core::borrow::BorrowMut> for Rule { - fn borrow_mut(&mut self) -> &mut Tail { - self.tail_mut() + pub fn state(self, state: State) -> Self { + Self { + state: Some(state), + ..self + } } - } - impl PartialEq<(Head, Tail)> for Rule - where - Q: PartialEq, - S: PartialEq, - { - fn eq(&self, other: &(Head, Tail)) -> bool { - self.head == other.0 && self.tail == other.1 + pub fn symbol(self, symbol: S) -> Self { + Self { + symbol: Some(symbol), + ..self + } } - } - impl PartialEq> for Rule - where - Q: PartialEq, - S: PartialEq, - { - fn eq(&self, other: &Head) -> bool { - &self.head == other + pub fn next_state(self, State(state): State) -> Self { + Self { + next_state: Some(State(state)), + ..self + } } - } - impl PartialEq> for Rule - where - Q: PartialEq, - S: PartialEq, - { - fn eq(&self, other: &Tail) -> bool { - &self.tail == other + pub fn write_symbol(self, write_symbol: S) -> Self { + Self { + write_symbol: Some(write_symbol), + ..self + } } - } - impl From<(Head, Tail)> for Rule { - fn from((head, tail): (Head, Tail)) -> Self { - Self { head, tail } + pub fn build(self) -> Rule { + Rule { + head: crate::Head { + state: self.state.expect("state is required"), + symbol: self.symbol.expect("symbol is required"), + }, + tail: crate::Tail { + direction: self.direction, + state: self.next_state.expect("next_state is required"), + symbol: self.write_symbol.expect("write_symbol is required"), + }, + } } } - impl From> for (Head, Tail) { - fn from(rule: Rule) -> Self { - (rule.head, rule.tail) + impl From> for Rule { + fn from(builder: RuleBuilder) -> Self { + builder.build() } } } diff --git a/core/src/rules/ruleset.rs b/core/src/rules/rule_map.rs similarity index 100% rename from core/src/rules/ruleset.rs rename to core/src/rules/rule_map.rs diff --git a/core/src/rules/program.rs b/core/src/rules/rule_set.rs similarity index 86% rename from core/src/rules/program.rs rename to core/src/rules/rule_set.rs index 798e2b2..4112d09 100644 --- a/core/src/rules/program.rs +++ b/core/src/rules/rule_set.rs @@ -7,16 +7,16 @@ use super::Rule; use crate::{Head, State, Tail}; use alloc::vec::{self, Vec}; -type RuleVec = Vec>; +type Rules = Vec>; #[derive(Clone, Debug, Default)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] -pub struct Ruleset { +pub struct RuleSet { pub(crate) initial_state: Option>, - pub(crate) rules: RuleVec, + pub(crate) rules: Rules, } -impl Ruleset { +impl RuleSet { pub fn new() -> Self { Self { initial_state: None, @@ -62,11 +62,11 @@ impl Ruleset { self.initial_state.as_ref().map(|state| state.to_ref()) } /// Returns a reference to the instructions. - pub const fn instructions(&self) -> &RuleVec { + pub const fn instructions(&self) -> &Rules { &self.rules } /// Returns a mutable reference to the instructions. - pub fn instructions_mut(&mut self) -> &mut RuleVec { + pub fn instructions_mut(&mut self) -> &mut Rules { &mut self.rules } /// Returns an iterator over the elements. @@ -143,19 +143,19 @@ impl Ruleset { } } -impl AsRef<[Rule]> for Ruleset { +impl AsRef<[Rule]> for RuleSet { fn as_ref(&self) -> &[Rule] { &self.rules } } -impl AsMut<[Rule]> for Ruleset { +impl AsMut<[Rule]> for RuleSet { fn as_mut(&mut self) -> &mut [Rule] { &mut self.rules } } -impl core::ops::Deref for Ruleset { +impl core::ops::Deref for RuleSet { type Target = [Rule]; fn deref(&self) -> &Self::Target { @@ -163,13 +163,13 @@ impl core::ops::Deref for Ruleset { } } -impl core::ops::DerefMut for Ruleset { +impl core::ops::DerefMut for RuleSet { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.rules } } -impl core::ops::Index> for Ruleset +impl core::ops::Index> for RuleSet where Q: PartialEq, S: PartialEq, @@ -181,7 +181,7 @@ where } } -impl From>> for Ruleset { +impl From>> for RuleSet { fn from(instructions: Vec>) -> Self { Self { initial_state: Some(State::default()), @@ -190,25 +190,25 @@ impl From>> for Ruleset { } } -impl Extend> for Ruleset { +impl Extend> for RuleSet { fn extend>>(&mut self, iter: I) { self.rules.extend(iter) } } -impl FromIterator> for Ruleset +impl FromIterator> for RuleSet where Q: Default, { fn from_iter>>(iter: I) -> Self { Self { initial_state: Some(State::default()), - rules: RuleVec::from_iter(iter), + rules: Rules::from_iter(iter), } } } -impl IntoIterator for Ruleset { +impl IntoIterator for RuleSet { type Item = Rule; type IntoIter = vec::IntoIter; diff --git a/core/src/traits/apply.rs b/core/src/traits/apply.rs index e5ca4df..92db0bd 100644 --- a/core/src/traits/apply.rs +++ b/core/src/traits/apply.rs @@ -41,4 +41,3 @@ impl Apply for Option { self.map(f) } } - diff --git a/core/src/traits/io.rs b/core/src/traits/io.rs deleted file mode 100644 index 9e1a52f..0000000 --- a/core/src/traits/io.rs +++ /dev/null @@ -1,59 +0,0 @@ -/* - Appellation: io - Contrib: FL03 -*/ - -pub trait RawBuf { - type Elem; - - /// Returns an immutable reference to the buffer as a slice - fn as_slice(&self) -> &[Self::Elem]; - /// Returns a mutable reference to the buffer as a slice - fn as_mut_slice(&mut self) -> &mut [Self::Elem]; -} - -/// The `Read` trait provides a way to read bytes from a source -pub trait Read -where - B: RawBuf, -{ - type Output; - - fn read(&mut self, buf: &mut B) -> Self::Output; -} - -pub trait Write -where - B: RawBuf, -{ - type Output; - - fn write(&mut self, buf: &mut B) -> Self::Output; -} - -/* - ************* Implementations ************* -*/ -impl RawBuf for [T] { - type Elem = T; - - fn as_slice(&self) -> &[Self::Elem] { - self - } - - fn as_mut_slice(&mut self) -> &mut [Self::Elem] { - self - } -} - -impl RawBuf for Vec { - type Elem = T; - - fn as_slice(&self) -> &[Self::Elem] { - self - } - - fn as_mut_slice(&mut self) -> &mut [Self::Elem] { - self - } -} diff --git a/core/src/traits/mod.rs b/core/src/traits/mod.rs index 55350fe..7db0635 100644 --- a/core/src/traits/mod.rs +++ b/core/src/traits/mod.rs @@ -11,10 +11,6 @@ mod increment; mod indexing; mod symbols; -#[doc(hidden)] -pub mod io; -#[doc(hidden)] -pub mod store; #[doc(hidden)] pub mod transform; diff --git a/core/src/traits/store.rs b/core/src/traits/store.rs deleted file mode 100644 index e0f76f4..0000000 --- a/core/src/traits/store.rs +++ /dev/null @@ -1,40 +0,0 @@ -/* - Appellation: container - Contrib: FL03 -*/ -use crate::{Rule, State}; - -#[doc(hidden)] -pub trait GetRule { - fn get(&self, state: State<&Q>, symbol: &S) -> Option<&Rule>; - - fn get_mut(&mut self, state: State<&Q>, symbol: &S) -> Option<&mut Rule>; -} - -pub trait RawContainer { - type Elem; -} - -/* - ************* Implementations ************* -*/ - -impl RawContainer for Vec { - type Elem = T; -} - -impl RawContainer for Box<[T]> { - type Elem = T; -} - -impl RawContainer for [T] { - type Elem = T; -} - -impl RawContainer for &mut [T] { - type Elem = T; -} - -impl RawContainer for &mut [T; N] { - type Elem = T; -} diff --git a/core/src/traits/symbols.rs b/core/src/traits/symbols.rs index 33c3d28..f8c4555 100644 --- a/core/src/traits/symbols.rs +++ b/core/src/traits/symbols.rs @@ -25,7 +25,7 @@ pub trait Alphabet { self.as_slice().len() } - fn to_vec(&self) -> Vec; + fn to_vec(&self) -> Vec<(K, Self::Elem)>; } /// [Symbolic] is a trait denoting types that can be used as symbols; diff --git a/core/src/traits/transform.rs b/core/src/traits/transform.rs index 42ddf7a..19b617f 100644 --- a/core/src/traits/transform.rs +++ b/core/src/traits/transform.rs @@ -4,14 +4,6 @@ */ use crate::{Direction, Head, State}; -/// [Handle] is a generic trait describing objects capable of handling some input and producing -/// some output. -pub trait Handle { - type Output; - - fn handle(&mut self, args: T) -> Self::Output; -} - /// [TM] pub trait TM { type Idx: Copy + core::ops::Add; @@ -35,24 +27,14 @@ pub trait TM { } } -pub trait Driver { - type Head; - - fn read(&self) -> Option; - - fn scope(&self) -> Head<&'_ Q, &'_ A>; - - fn write(&mut self, symbol: A) -> Option; -} - -pub trait Read { +pub trait Reader { type Output; fn read(&self) -> Self::Output; } -pub trait Write { +pub trait WriteMut { type Output; - fn write(&self) -> Self::Output; + fn write(&mut self, data: T) -> Option; } diff --git a/core/tests/actor.rs b/core/tests/actor.rs index 79047b0..3ab59ec 100644 --- a/core/tests/actor.rs +++ b/core/tests/actor.rs @@ -5,7 +5,7 @@ extern crate rstm_core as rstm; use rstm::actors::Actor; -use rstm::rules::{Rule, Ruleset}; +use rstm::rules::{Rule, RuleSet}; use rstm::{ruleset, State}; lazy_static::lazy_static! { @@ -25,7 +25,7 @@ fn busy_beaver() { let initial_state = State(0_isize); let input = [0_usize; 10]; - let program = Ruleset::from_iter(*RULES); + let program = RuleSet::from_iter(*RULES); let actor = Actor::from_state(initial_state).with_alpha(input); let mut rt = actor.execute(program); diff --git a/core/tests/rules.rs b/core/tests/rules.rs index 4fa7fe6..33046aa 100644 --- a/core/tests/rules.rs +++ b/core/tests/rules.rs @@ -4,7 +4,7 @@ */ extern crate rstm_core as rstm; -use rstm::rules::Ruleset; +use rstm::rules::RuleSet; use rstm::{ruleset, Direction, Head, State, Tail}; #[test] @@ -20,7 +20,7 @@ fn ruleset() { // validate the number of rules within the ruleset assert_eq!(rules.len(), 6); // create a new program using the ruleset - let program = Ruleset::from_iter(rules); + let program = RuleSet::from_iter(rules); // validate the number of rules within the program assert_eq!(rules.len(), program.len()); // create a new head for a rule within the program diff --git a/rstm/examples/actor.rs b/rstm/examples/actor.rs index 7acfc33..5be4e3f 100644 --- a/rstm/examples/actor.rs +++ b/rstm/examples/actor.rs @@ -4,7 +4,7 @@ */ extern crate rstm; -use rstm::{ruleset, Actor, Ruleset, State}; +use rstm::{ruleset, Actor, RuleSet, State}; fn main() -> Result<(), Box> { _tracing("debug"); @@ -22,7 +22,7 @@ fn main() -> Result<(), Box> { (-1, 1) -> Left(1, 1), ]; // create a new program from the ruleset - let program = Ruleset::from_iter(rules); + let program = RuleSet::from_iter(rules); // create a new instance of the machine let tm = dbg!(Actor::new(alpha, initial_state, 0)); tm.execute(program).run()?; diff --git a/rstm/src/exp/model/base.rs b/rstm/src/exp/model/base.rs index fcc6f03..8716be5 100644 --- a/rstm/src/exp/model/base.rs +++ b/rstm/src/exp/model/base.rs @@ -3,7 +3,7 @@ Contrib: FL03 */ use crate::prelude::{Direction, Error, Head, Rule, StdTape, Symbolic, Tail}; -use crate::rules::Ruleset; +use crate::rules::RuleSet; use crate::state::{halt::HaltState, RawState, State}; /// # Turing Machine ([StdTm]) @@ -14,7 +14,7 @@ use crate::state::{halt::HaltState, RawState, State}; #[derive(Clone, Debug)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct StdTM { - pub(crate) program: Ruleset, + pub(crate) program: RuleSet, pub(crate) state: State>, pub(crate) tape: StdTape, } @@ -25,13 +25,13 @@ impl StdTM { A: Default, { StdTM { - program: Ruleset::from_iter(rules), + program: RuleSet::from_iter(rules), state: State::none(), tape, } } /// Returns an immutable reference to the [program](Program) - pub const fn program(&self) -> &Ruleset { + pub const fn program(&self) -> &RuleSet { &self.program } /// Creates a new instance of a [head](Head) from references to the current state and symbol; diff --git a/rstm/src/exp/model/tmh.rs b/rstm/src/exp/model/tmh.rs index cb76610..d74c89a 100644 --- a/rstm/src/exp/model/tmh.rs +++ b/rstm/src/exp/model/tmh.rs @@ -5,7 +5,7 @@ #![cfg(feature = "std")] use crate::rules::Rule; use crate::state::RawState; -use crate::{Direction, Error, Head, Ruleset, State}; +use crate::{Direction, Error, Head, RuleSet, State}; use std::collections::hash_map::{self, HashMap}; @@ -13,7 +13,7 @@ pub struct TMH where Q: RawState, { - pub(crate) program: Ruleset, + pub(crate) program: RuleSet, pub(crate) scope: Head, // Head, pub(crate) tape: HashMap, } @@ -24,7 +24,7 @@ where { pub fn new(initial_state: State) -> Self { Self { - program: Ruleset::new(), + program: RuleSet::new(), scope: Head { state: initial_state, symbol: 0, @@ -41,7 +41,7 @@ where ..self } } - pub fn with_program(self, program: Ruleset) -> Self { + pub fn with_program(self, program: RuleSet) -> Self { Self { program, ..self } }