TuringTape

Struct TuringTape 

Source
pub struct TuringTape<Alphabet> { /* private fields */ }
Expand description

A possibly theorically infinite TuringTape

Implementations§

Source§

impl<Alphabet: Clone> TuringTape<Alphabet>

Source

pub fn new( empty: Alphabet, start: Alphabet, initial: Vec<Alphabet>, ) -> TuringTape<Alphabet>

Initialize a new TuringTape with:

  • empty: The token put at empty tape cells
  • start: The token put in the first cell
  • initial: An vector of tokens to be put after the start token
Examples found in repository?
examples/divisibility.rs (line 103)
73fn main() {
74    let n = std::env::args()
75        .nth(1)
76        .expect("No first argument given. Usage: cargo run --example divisibility -- <int> <int>");
77    let k = std::env::args()
78        .nth(2)
79        .expect("No second argument given. Usage: cargo run --example divisibility -- <int> <int>");
80
81    let n = n
82        .parse::<usize>()
83        .expect("First argument is not a positive integer");
84    let k = k
85        .parse::<usize>()
86        .expect("Second argument is not a positive integer");
87
88    println!("n: {}, k: {}", n, k);
89
90    let mut ns = vec![Alphabet::One; n];
91    let mut ks = vec![Alphabet::One; k];
92
93    let mut delta = vec![Alphabet::Delta];
94
95    let mut initial = Vec::new();
96
97    initial.append(&mut delta.clone());
98    initial.append(&mut ns);
99    initial.append(&mut delta.clone());
100    initial.append(&mut ks);
101    initial.append(&mut delta);
102
103    let tape = TuringTape::new(Alphabet::Delta, Alphabet::StartToken, initial);
104    println!("Tape: {}", tape);
105
106    println!("\n\nRunning...\n\n");
107
108    use States::*;
109    let end_state = tape.run_states(Start, vec![CycleStart, InvalidSyntax, DivByNull, LeftOver]);
110
111    println!("Tape: {}", tape);
112    println!("Endstate: {:?}", end_state);
113}
Source

pub fn get_cursor(&self) -> Alphabet

Fetch the token at the cursor

Source

pub fn set_cursor(&self, value: Alphabet) -> Alphabet

Set the token at the cursor and return the old token

Source

pub fn step_right(&self) -> Alphabet

Make the cursor go one cell to the right

Source

pub fn step_left(&self) -> Alphabet

Make the cursor go one cell to the left

Will panic if one goes off the tape.

Source

pub fn run_states<S: TuringStates<Alphabet> + PartialEq>( &self, start_state: S, end_states: Vec<S>, ) -> S

Runs from start state until one of the end states has been reached. Will return the end state.

Examples found in repository?
examples/divisibility.rs (line 109)
73fn main() {
74    let n = std::env::args()
75        .nth(1)
76        .expect("No first argument given. Usage: cargo run --example divisibility -- <int> <int>");
77    let k = std::env::args()
78        .nth(2)
79        .expect("No second argument given. Usage: cargo run --example divisibility -- <int> <int>");
80
81    let n = n
82        .parse::<usize>()
83        .expect("First argument is not a positive integer");
84    let k = k
85        .parse::<usize>()
86        .expect("Second argument is not a positive integer");
87
88    println!("n: {}, k: {}", n, k);
89
90    let mut ns = vec![Alphabet::One; n];
91    let mut ks = vec![Alphabet::One; k];
92
93    let mut delta = vec![Alphabet::Delta];
94
95    let mut initial = Vec::new();
96
97    initial.append(&mut delta.clone());
98    initial.append(&mut ns);
99    initial.append(&mut delta.clone());
100    initial.append(&mut ks);
101    initial.append(&mut delta);
102
103    let tape = TuringTape::new(Alphabet::Delta, Alphabet::StartToken, initial);
104    println!("Tape: {}", tape);
105
106    println!("\n\nRunning...\n\n");
107
108    use States::*;
109    let end_state = tape.run_states(Start, vec![CycleStart, InvalidSyntax, DivByNull, LeftOver]);
110
111    println!("Tape: {}", tape);
112    println!("Endstate: {:?}", end_state);
113}

Trait Implementations§

Source§

impl<Alphabet: Display + Clone> Display for TuringTape<Alphabet>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Alphabet: Clone> From<TuringTape<Alphabet>> for Vec<Alphabet>

Source§

fn from(tape: TuringTape<Alphabet>) -> Vec<Alphabet>

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<Alphabet> !Freeze for TuringTape<Alphabet>

§

impl<Alphabet> !RefUnwindSafe for TuringTape<Alphabet>

§

impl<Alphabet> !Send for TuringTape<Alphabet>

§

impl<Alphabet> !Sync for TuringTape<Alphabet>

§

impl<Alphabet> Unpin for TuringTape<Alphabet>
where Alphabet: Unpin,

§

impl<Alphabet> !UnwindSafe for TuringTape<Alphabet>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.