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>
impl<S: Symbol> Configuration<S>
Sourcepub fn new(tape: Tape<S>, index: usize, state: State) -> Result<Self, String>
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.
Sourcepub fn new_nrm(tape: Tape<S>) -> Result<Self, String>
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?
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}
Sourcepub fn new_std(tape: Tape<S>) -> Result<Self, String>
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.
Sourcepub fn destruct(self) -> (Tape<S>, usize, State)
pub fn destruct(self) -> (Tape<S>, usize, State)
Destructs Configuration
into (Tape<S>, usize, State)
. May be used
only with owned values.
Sourcepub fn tape(&self) -> &Tape<S>
pub fn tape(&self) -> &Tape<S>
Returns the Tape
reference of the Configuration
.
Zero cost method.
Examples found in repository?
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}
Sourcepub fn into_tape(self) -> Tape<S>
pub fn into_tape(self) -> Tape<S>
Returns the Tape
copy of the Configuration
.
Sourcepub fn get_symbol(&self) -> &S
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.
Sourcepub fn index(&self) -> usize
pub fn index(&self) -> usize
Returns the current Tape
index. This value is always in tape bounds.
Sourcepub fn set_symbol(&mut self, symbol: S)
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.
Sourcepub fn shift(&mut self, movement: Move, default: S)
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>
impl<S: Clone + Symbol> Clone for Configuration<S>
Source§fn clone(&self) -> Configuration<S>
fn clone(&self) -> Configuration<S>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more