Universal Turing machine: Difference between revisions

D entry: added third TM, changed output formatting a little
(→‎{{header|D}}: added third test; some small changes)
(D entry: added third TM, changed output formatting a little)
Line 303:
This is typed less strongly than the Ada entry. The constructor of UTM contains many run-time tests that in a strongly static typed version are (implicitly) done at compile-time.
<lang d>import std.stdio, std.algorithm, std.string, std.conv, std.array,
std.exception;
 
struct UTM {
Line 329:
 
static struct TapeHead {
const Symbol[] symbols;
immutable Symbol blank;
Symbol[] tape;
Line 334 ⟶ 335:
 
this(in ref TuringMachine t) pure /*nothrow*/ {
this.symbols = t.symbols;
this.blank = t.blank;
this.tape = t.input.dup; // Not nothrow.
Line 365 ⟶ 367:
else
this.position--;
}
 
void stay() const pure nothrow {
// Do nothing.
}
 
Line 371 ⟶ 377:
case Direction.left: left(); break;
case Direction.right: right(); break;
case Direction.stay: breakstay(); // do nothingbreak;
}
}
 
string toString() const {
return format("...%(%)...", this.tape)
~ '\n'
~ format("%" ~ text(this.position + 14) ~ "s", "^")
~ '\n';
}
Line 384 ⟶ 390:
 
this(const ref TuringMachine tm_) {
immutable errMsg = "Invalid input.";
enforce(!tm_.runningStates.empty, errMsg);
enforce(!tm_.haltStates.empty, errMsg);
enforce(!tm_.symbols.empty, errMsg);
enforce(tm_.rules.length, errMsg);
enforce(tm_.runningStates.canFind(tm_.initialState), errMsg);
enforce(tm_.symbols.canFind(tm_.blank), errMsg);
const allStates = tm_.runningStates ~ tm_.haltStates;
foreach (const s; tm_.rules.keys.to!(dchar[])().sort())
enforce(tm_.runningStates.canFind(s), errMsg);
foreach (const aa; tm_.rules.byValue)
foreach (/*const*/ s, const rule; aa) {
enforce(tm_.symbols.canFind(s), errMsg);
enforce(tm_.symbols.canFind(rule.toWrite), errMsg);
enforce(allStates.canFind(rule.nextState), errMsg);
}
 
this.tm = tm_;
this.head = TapeHead(this.tm);
Line 393 ⟶ 416:
if (!tm.runningStates.canFind(state))
throw new Exception("Unknown state.");
autoimmutable symbol = this.head.read();
autoimmutable rule = this.tm.rules[state][symbol];
this.head.write(rule.toWrite);
this.head.move(rule.direction);
Line 405 ⟶ 428:
void main() {
with (UTM) {
alias R = Rule;
writeln("Incrementer:");
TuringMachine tm1;
Line 413 ⟶ 437:
tm1.runningStates = ['A'];
with (Direction)
tm1.rules = ['A': [0: RuleR(1, left, 'H'),
1: RuleR(1, right, 'A')]];
tm1.input = [1, 1, 1];
UTM(tm1);
Line 425 ⟶ 449:
tm2.initialState = 'A';
tm2.haltStates = ['H'];
tm2.runningStates = ['A', 'B', 'C'];
with (Direction)
tm2.rules = ['A': [0: RuleR(1, right, 'B'),
1: RuleR(1, left, 'C')],
'B': [0: RuleR(1, left, 'A'),
1: RuleR(1, right, 'B')],
'C': [0: RuleR(1, left, 'B'),
1: RuleR(1, stay, 'H')]];
UTM(tm2);
 
writeln("\nSorting stress test (12212212121212):");
TuringMachine tm3;
Line 443 ⟶ 467:
tm3.runningStates = ['A', 'B', 'C', 'D', 'E'];
with (Direction)
tm3.rules = ['A' : [1 : RuleR(1, right, 'A'),
2 : Rule2: R(3, right, 'B'),
0 : Rule0: R(0, left, 'E')],
'B' : [1 : RuleR(1, right, 'B'),
2 : Rule2: R(2, right, 'B'),
0 : Rule0: R(0, left, 'C')],
'C' : [1 : RuleR(2, left, 'D'),
2 : Rule2: R(2, left, 'C'),
3 : Rule3: R(2, left, 'E')],
'D' : [1 : RuleR(1, left, 'D'),
2 : 2: RuleR(2, left, 'D'),
3 : 3: RuleR(1, right, 'A')],
'E' : [1 : RuleR(1, left, 'E'),
0 : 0: RuleR(0, right, 'H')]];
tm3.input = [1, 2, 2, 1, 2, 2, 1, 2, 1, 2, 1, 2, 1, 2];
UTM(tm3.input = [1,2,2,1,2,2,1,2,1,2,1,2,1,2]);
UTM(tm3);
}
}</lang>
<pre>Incrementer:
...111...
^
111
^
111
^
1110
^
...111...
1111
^
...111...
^
...1110...
^
...1111...
^
 
Busy beaver machine (3-state, 2-symbol):
...0...
0
^
10
^
11
^
011
^
0111
^
01111
^
11111
^
11111
^
11111
^
...10...
11111
^
...11...
111110
^
111111
^
111111
^
...011...
111111
^
 
...0111...
^
...01111...
^
...11111...
^
...11111...
^
...11111...
^
...11111...
^
...111110...
^
...111111...
^
...111111...
^
...111111...
^
 
Sorting stress test (12212212121212):
...12212212121212...
^
12212212121212
^
13212212121212
^
13212212121212
^
...12212212121212...
13212212121212
^
13212212121212
 
[...]
 
111111222222220
^
...0111111222222220...
111111222222220
^
...0111111222222220...
111111222222220
^</pre>
^
111111222222220
^
0111111222222220
^
0111111222222220
^</pre>
 
=={{header|Erlang}}==