Configuration

Struct Configuration 

Source
pub struct Configuration<S: Symbol> {
    pub state: State,
    /* private fields */
}
Expand description

Configuration is a struct that represents the state of a Turing machine. Machines do not implement their state as a part of themselves; instead, machines mutate configurations according to their program.

Fields§

§state: State

Configuration State is used by crate::TuringMachine and cannot be changed by self-methods.

Implementations§

Source§

impl<S: Symbol> Configuration<S>

Source

pub fn new(tape: Tape<S>, index: usize, state: State) -> Result<Self, String>

Constructs a new Configuration from the Tape, the index usize and the State.

Returns a new [Ok(Configuration)] if the index is within the bounds of the Tape, otherwise an [Err(String)] with diagnostic information.

Source

pub fn new_nrm(tape: Tape<S>) -> Result<Self, String>

Constructs a new Configuration from the Tape, the index 0 and the state 1. This configuration named normal or nrm.

Returns a new [Ok(Configuration)] if the Tape is not empty otherwise an [Err(String)] with diagnostic information.

Examples found in repository?
examples/hyper_machine.rs (line 82)
11fn main() -> Result<(), String> {
12    use nrm_machines::*;
13
14    let stand = new_stand_machine();
15    let zerofy = new_zerofy_machine();
16    let l_shift = new_left_shift_machine();
17    let r_shift = new_right_shift_machine();
18    let trans = new_trans_machine();
19
20    let mut program = Program::new(
21        vec![
22            stand.clone(),
23            zerofy.clone(),
24            l_shift.clone(),
25            r_shift.clone(),
26            trans.clone(),
27        ],
28        State(9),
29    );
30    // This is simplest implementation of `change choose second to choose third` machine
31    program.extend([
32        // Find l_shift
33        (1, r_shift.clone(), 1, r_shift.clone(), Move::Right),
34        (1, trans.clone(), 1, trans.clone(), Move::Right),
35        (1, zerofy.clone(), 1, zerofy.clone(), Move::Right),
36        (1, l_shift.clone(), 2, stand.clone(), Move::Left),
37        // Clear until r_shift
38        (2, zerofy.clone(), 2, stand.clone(), Move::Left),
39        (2, trans.clone(), 2, stand.clone(), Move::Left),
40        (2, r_shift.clone(), 3, r_shift.clone(), Move::Right),
41        //
42        // Set second r_shift
43        (3, stand.clone(), 4, r_shift.clone(), Move::Right),
44        // Set first trans
45        (4, stand.clone(), 5, trans.clone(), Move::Right),
46        // Set first zerofy
47        (5, stand.clone(), 6, zerofy.clone(), Move::Right),
48        // Set first l_shift
49        (6, stand.clone(), 7, l_shift.clone(), Move::Right),
50        // Set second trans
51        (7, stand.clone(), 8, trans.clone(), Move::Right),
52        // Set second zerofy
53        (8, stand.clone(), 9, zerofy.clone(), Move::Right),
54        // Set second l_shift and stop execution
55        (9, stand.clone(), 0, l_shift.clone(), Move::None),
56    ])?;
57
58    let hyper_machine = Classic::new(program, stand.clone())?;
59    let choose_second = Tape::new([
60        r_shift.clone(),
61        trans.clone(),
62        zerofy.clone(),
63        l_shift.clone(),
64    ]);
65    let result_choose_third = hyper_machine.translate_nrm(choose_second)?;
66
67    let expected_choose_third = Tape::new([
68        r_shift.clone(),
69        r_shift.clone(),
70        trans.clone(),
71        zerofy.clone(),
72        l_shift.clone(),
73        trans.clone(),
74        zerofy.clone(),
75        l_shift.clone(),
76    ]);
77
78    assert_eq!(expected_choose_third, result_choose_third);
79    println!("If you're reading this, hyper machine successful transform choose second machine");
80
81    let tape = Tape::from("0101101110");
82    let mut conf = Configuration::new_nrm(tape.clone())?;
83    for machine in result_choose_third.as_vec() {
84        conf = machine.execute(conf).unwrap();
85        conf.state = State(1)
86    }
87    println!(
88        "Choose third machine translate {} into {}",
89        String::from_iter(tape.as_vec()),
90        String::from_iter(conf.tape().as_vec())
91    );
92
93    Ok(())
94}
Source

pub fn new_std(tape: Tape<S>) -> Result<Self, String>

Constructs a new Configuration from the Tape, the index tape.len() - 1 and the state: 1. This configuration named standart or std.

Returns a new [Ok(Configuration)] if the Tape is not empty otherwise an [Err(String)] with diagnostic information.

Source

pub fn destruct(self) -> (Tape<S>, usize, State)

Destructs Configuration into (Tape<S>, usize, State). May be used only with owned values.

Source

pub fn tape(&self) -> &Tape<S>

Returns the Tape reference of the Configuration.

Zero cost method.

Examples found in repository?
examples/hyper_machine.rs (line 90)
11fn main() -> Result<(), String> {
12    use nrm_machines::*;
13
14    let stand = new_stand_machine();
15    let zerofy = new_zerofy_machine();
16    let l_shift = new_left_shift_machine();
17    let r_shift = new_right_shift_machine();
18    let trans = new_trans_machine();
19
20    let mut program = Program::new(
21        vec![
22            stand.clone(),
23            zerofy.clone(),
24            l_shift.clone(),
25            r_shift.clone(),
26            trans.clone(),
27        ],
28        State(9),
29    );
30    // This is simplest implementation of `change choose second to choose third` machine
31    program.extend([
32        // Find l_shift
33        (1, r_shift.clone(), 1, r_shift.clone(), Move::Right),
34        (1, trans.clone(), 1, trans.clone(), Move::Right),
35        (1, zerofy.clone(), 1, zerofy.clone(), Move::Right),
36        (1, l_shift.clone(), 2, stand.clone(), Move::Left),
37        // Clear until r_shift
38        (2, zerofy.clone(), 2, stand.clone(), Move::Left),
39        (2, trans.clone(), 2, stand.clone(), Move::Left),
40        (2, r_shift.clone(), 3, r_shift.clone(), Move::Right),
41        //
42        // Set second r_shift
43        (3, stand.clone(), 4, r_shift.clone(), Move::Right),
44        // Set first trans
45        (4, stand.clone(), 5, trans.clone(), Move::Right),
46        // Set first zerofy
47        (5, stand.clone(), 6, zerofy.clone(), Move::Right),
48        // Set first l_shift
49        (6, stand.clone(), 7, l_shift.clone(), Move::Right),
50        // Set second trans
51        (7, stand.clone(), 8, trans.clone(), Move::Right),
52        // Set second zerofy
53        (8, stand.clone(), 9, zerofy.clone(), Move::Right),
54        // Set second l_shift and stop execution
55        (9, stand.clone(), 0, l_shift.clone(), Move::None),
56    ])?;
57
58    let hyper_machine = Classic::new(program, stand.clone())?;
59    let choose_second = Tape::new([
60        r_shift.clone(),
61        trans.clone(),
62        zerofy.clone(),
63        l_shift.clone(),
64    ]);
65    let result_choose_third = hyper_machine.translate_nrm(choose_second)?;
66
67    let expected_choose_third = Tape::new([
68        r_shift.clone(),
69        r_shift.clone(),
70        trans.clone(),
71        zerofy.clone(),
72        l_shift.clone(),
73        trans.clone(),
74        zerofy.clone(),
75        l_shift.clone(),
76    ]);
77
78    assert_eq!(expected_choose_third, result_choose_third);
79    println!("If you're reading this, hyper machine successful transform choose second machine");
80
81    let tape = Tape::from("0101101110");
82    let mut conf = Configuration::new_nrm(tape.clone())?;
83    for machine in result_choose_third.as_vec() {
84        conf = machine.execute(conf).unwrap();
85        conf.state = State(1)
86    }
87    println!(
88        "Choose third machine translate {} into {}",
89        String::from_iter(tape.as_vec()),
90        String::from_iter(conf.tape().as_vec())
91    );
92
93    Ok(())
94}
Source

pub fn into_tape(self) -> Tape<S>

Returns the Tape copy of the Configuration.

Source

pub fn get_symbol(&self) -> &S

Returns the current symbol reference. This reference is always exists.

§Panics

Configuration could panic only if source code is broken - this would be a bug. Configuration controls its inner index so it always valid.

So you could open an issue on GitHub.

Source

pub fn index(&self) -> usize

Returns the current Tape index. This value is always in tape bounds.

Source

pub fn is_empty(&self) -> bool

Returns true if the Tape is not empty, otherwise false.

Note that Turing Tape cannot be empty but this method can return false (because Tape type is based on the Vec type).

Source

pub fn len(&self) -> usize

Returns the Tape length.

Source

pub fn set_symbol(&mut self, symbol: S)

Sets Symbol at index position in the Tape.

§Panics

Configuration could panic only if source code is broken - this would be a bug. Configuration controls its inner index so it always valid.

So you could open an issue on GitHub.

Source

pub fn shift(&mut self, movement: Move, default: S)

Shifts the Tape to left or right if Move is Move::Left or Move::Right, otherwise do nothing (when Move::None). If Configuration reachs the begin or the end of the Tape then Tape extends by Tape::insert method, otherwise only changes self index.

Trait Implementations§

Source§

impl<S: Clone + Symbol> Clone for Configuration<S>

Source§

fn clone(&self) -> Configuration<S>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S: Debug + Symbol> Debug for Configuration<S>

Source§

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

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

impl<S: Symbol> Display for Configuration<S>

Source§

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

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

impl<S: PartialEq + Symbol> PartialEq for Configuration<S>

Source§

fn eq(&self, other: &Configuration<S>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<S: Eq + Symbol> Eq for Configuration<S>

Source§

impl<S: Symbol> StructuralPartialEq for Configuration<S>

Auto Trait Implementations§

§

impl<S> Freeze for Configuration<S>

§

impl<S> RefUnwindSafe for Configuration<S>
where S: RefUnwindSafe,

§

impl<S> Send for Configuration<S>
where S: Send,

§

impl<S> Sync for Configuration<S>
where S: Sync,

§

impl<S> Unpin for Configuration<S>
where S: Unpin,

§

impl<S> UnwindSafe for Configuration<S>
where S: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> Symbol for T
where T: Clone + Debug + Display + Eq + PartialEq,