1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
pub use self::{instructions::*, machine::*, operator::*, programs::*, tapes::*};
pub(crate) mod instructions;
pub(crate) mod machine;
pub(crate) mod operator;
pub(crate) mod programs;
pub(crate) mod tapes;
use crate::{
states::{Stateful, States},
Scope, Symbolic,
};
pub trait Turing<S: Symbolic> {
type Error;
type Scope: Clone + Scope<S>;
fn driver(&mut self) -> &mut Self::Scope;
fn execute(&mut self, program: Program<S>) -> Result<Self::Scope, Self::Error> {
let until = |actor: &Self::Scope| actor.clone().state().state().clone() == States::Invalid;
self.execute_until(program, until)
}
fn execute_once(&mut self, program: Program<S>) -> Result<Self::Scope, Self::Error> {
let head = Head::new(
self.driver().state().clone().into(),
self.driver().scope().clone(),
);
let inst = program
.get(head)
.expect("Failed to fetch the instruction head...")
.clone();
self.driver().set_state(inst.tail().state().clone());
self.driver().set_symbol(inst.tail().symbol().clone());
self.driver()
.shift(*inst.tail().action(), program.default_symbol().clone());
Ok(self.driver().clone())
}
fn execute_until(
&mut self,
program: Program<S>,
until: impl Fn(&Self::Scope) -> bool,
) -> Result<Self::Scope, Self::Error> {
while !until(self.driver()) {
let head = Head::new(
self.driver().state().clone().into(),
self.driver().scope().clone(),
);
let inst = program
.get(head)
.expect("Failed to fetch the instruction head...")
.clone();
self.driver().set_state(inst.tail().state().clone());
self.driver().set_symbol(inst.tail().symbol().clone());
self.driver()
.shift(*inst.tail().action(), program.default_symbol().clone());
}
Ok(self.driver().clone())
}
fn translate(&mut self, program: Program<S>, tape: Tapes<S>) -> Result<Tape<S>, Self::Error> {
self.update(tape);
Ok(self.execute(program)?.tape().clone())
}
fn update(&mut self, tape: Tapes<S>);
}