September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
1
Task/Universal-Turing-machine/00META.yaml
Normal file
1
Task/Universal-Turing-machine/00META.yaml
Normal file
|
|
@ -0,0 +1 @@
|
|||
--- {}
|
||||
|
|
@ -1,17 +1,16 @@
|
|||
". noun define -. CRLF NB. Fixed tacit universal Turing machine code...
|
||||
|
||||
utm=.
|
||||
(((":@:(]&:>)@:(6&({::)) ,: (":@] 9&({::))) ,. ':'"_) ,. 2&({::) >@:(((
|
||||
48 + ]) { a."_)@[ ; (] $ ' '"_) , '^'"_) 3&({::))@:([ (0 0 $ 1!:2&2)@:(
|
||||
'A changeless cycle was detected!'"_)^:(-.@:(_1"_ = 1&({::))))@:((((3&(
|
||||
{::) + 8&({::)) ; 1 + 9&({::)) 3 9} ])@:(<@:((0: 0&({::)@]`(<@(1&({::))
|
||||
@])`(2&({::)@])} ])@:(7 3 2&{)) 2} ])@:(<"0@:(6&({::) (<@[ { ]) 0&({::)
|
||||
) 7 8 1} ])@:([ (0 0 $ 1!:2&2)@:(((":@:(]&:>)@:(6&({::)) ,: (":@] 9&({:
|
||||
:))) ,. ':'"_) ,. 2&({::) >@:(((48 + ]) { a."_)@[ ; (] $ ' '"_) , '^'"_
|
||||
) 3&({::))^:(0 = 4&({::) | 9&({::)))@:(<@:(1&({::) ; 3&({::) { 2&({::))
|
||||
6} ])@:(<@:(3&({::) + _1 = 3&({::)) 3} ])@:(<@:(((_1 = 3&({::)) {:: 5&
|
||||
({::)) , 2&({::) , (3&({::) = #@:(2&({::))) {:: 5&({::)) 2} ])^:(-.@:(_
|
||||
1"_ = 1&({::)))^:_)@:((0 ; (({. , ({: % 3:) , 3:)@:$ $ ,)@:(}."1)@:(".;
|
||||
._2)@:(0&({::))) 9 0} ])@:(<@:('' ; 0"_) 5} ])@:(5&(] , a: $~ [))@:(,~)
|
||||
(((":@:(]&:>)@:(6&({::)) ,: (":@] 9&({::))) ,. ':'"_) ,. 2&({::) >@:(((48 + ]
|
||||
) { a."_)@[ ; (] $ ' '"_) , '^'"_) 3&({::))@:([ (0 0 $ 1!:2&2)@:('A changeles
|
||||
s cycle was detected!'"_)^:(-.@:(_1"_ = 1&({::))))@:((((3&({::) + 8&({::)) ;
|
||||
1 + 9&({::)) 3 9} ])@:(<@:((0 (0 {:: ])`(<@:(1 {:: ]))`(2 {:: ])} ])@:(7 3 2&
|
||||
{)) 2} ])@:(<"0@:(6&({::) (<@[ { ]) 0&({::)) 7 8 1} ])@:([ (0 0 $ 1!:2&2)@:((
|
||||
(":@:(]&:>)@:(6&({::)) ,: (":@] 9&({::))) ,. ':'"_) ,. 2&({::) >@:(((48 + ])
|
||||
{ a."_)@[ ; (] $ ' '"_) , '^'"_) 3&({::))^:(0 = 4&({::) | 9&({::)))@:(<@:(1&(
|
||||
{::) ; 3&({::) { 2&({::)) 6} ])@:(<@:(3&({::) + _1 = 3&({::)) 3} ])@:(<@:(((_
|
||||
1 = 3&({::)) {:: 5&({::)) , 2&({::) , (3&({::) = #@:(2&({::))) {:: 5&({::)) 2
|
||||
} ])^:(-.@:(_1"_ = 1&({::)))^:_)@:((0 ; (({. , ({: % 3:) , 3:)@:$ $ ,)@:(}."1
|
||||
)@:(".;._2)@:(0&({::))) 9 0} ])@:(<@:('' ; 0"_) 5} ])@:(,&(;:',,,,,'))@:(,~)
|
||||
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,70 +1,95 @@
|
|||
NB. Structured derivation of the universal Turing machine...
|
||||
NB. Structured derivation of the universal Turing machine...
|
||||
|
||||
o=. @: NB. Composition of verbs (functions)
|
||||
c=. "_ NB. Constant verb (function)
|
||||
f=. &{:: NB. fetch
|
||||
e=. <@: NB. enclose
|
||||
NB.--------------------------------------------------------------------------------------
|
||||
NB. Quick and dirty tacit toolkit...
|
||||
|
||||
NB. utm (dyadic verb)...
|
||||
o=. @:
|
||||
c=."_
|
||||
|
||||
'Q S T P F B M PRINT MOVE C'=. i.10 NB. Using 10 boxes
|
||||
NB. Left: Q - Instruction table, S - Turing machine state
|
||||
NB. Right: T - Data tape, P - Head position pointer, F - Display frequency
|
||||
NB. Local: B - Blank defaults, M - State and tape symbol read, PRINT - Printing symbol
|
||||
NB. MOVE - Tape head moving instruction, C - Step Counter
|
||||
ver=. (0:`)([:^:)
|
||||
|
||||
DisplayTape=. > o (((48 + ]) { a.c)@[ ; ((] $ ' 'c) , '^'c))
|
||||
display=. ((((": o (]&:>) o (M f)) ,: (":@] C f)) ,. ':'c ) ,. (T f DisplayTape P f))
|
||||
NB. Displaying state, symbol, tape / step and pointer
|
||||
amend=. 0: (0 f)@]`(<@(1 f)@])`(2 f@])} ]
|
||||
d=. (fix=. (;:'f.')ver) (train=.(;:'`:')ver&6) (an=. <@:((,'0') (,&<) ]))
|
||||
ver=. (an f. o fix'ver')ver o an f.
|
||||
z=. ((an'')`($ ,)`) (`:6)
|
||||
d=. (a0=. `'') (a1=. (@:[) ((<'&')`) (`:6)) (a2=. (`(<(":0);_)) (`:6))
|
||||
av=. ((an o fix'a0')`) (`(an o fix'a1')) (`(an o fix'a2') ) (`:6)
|
||||
|
||||
NB. execute (monadic verb)...
|
||||
Fetch=. (ver o train ;:'&{::')&.> o i. f.av
|
||||
tie=. ver o train ;:'`'
|
||||
|
||||
FillLeft=. (_1 = P f ) {:: B f NB. Expanding and filling the tape
|
||||
FillRight=. ( P f = # o (T f)) {:: B f NB. with 0's (if necessary)
|
||||
ia=. <@[ { ] NB. Selecting by the indices of an array
|
||||
indices=. (, $~ 1 -.~ $) o (train"0 o ((1 -: L.)S:1 # <S:1) o (tie&'') o fix :: ] @:[)
|
||||
f=. ((ver o train ;:'&{')) o indices o train f.av
|
||||
|
||||
e0=. (FillLeft , T f , FillRight)e T}] NB. Adjusting the tape
|
||||
e1=. (P f + _1 = P f)e P}] NB. and the pointer (if necessary)
|
||||
e2=. (S f ; P f { T f)e M}] NB. Updating the state and reading the tape symbol
|
||||
e3=. [(smoutput o display)^:(0 = F f | C f) NB. Displaying intermediate cycles
|
||||
e4=. (<"0 o (M f ia Q f)) (PRINT,MOVE,S)}] NB. Performing the printing, moving and state actions
|
||||
e5=. (amend o ((PRINT,P,T)&{))e T}] NB. Printing symbol on tape at the pointer position
|
||||
e6=. ((P f + MOVE f) ; 1 + C f) (P,C)}] NB. Updating the pointer (and the counter)
|
||||
'A B'=. 2 Fetch
|
||||
head=. (;:'<@:') {.~ 2 * 1 = # o [
|
||||
h=. train o (indices o train o (A f) (head , (B f) o ] , < o an o [ , (;:'}]')c) ]) f.av
|
||||
|
||||
execute=. e6 o e5 o e4 o e3 o e2 o e1 o e0
|
||||
DropIfNB=. < o ('('"_ , ] , ')'"_) o ((}: ^: ('NB.' -: 3&{. o > o {:)) &. ;:)
|
||||
pipe=. ([ , ' o ' , ])&:>/ o |.
|
||||
|
||||
al=. &(] , (a: $~ [)) NB. Appending local boxes
|
||||
cc=. 'A changeless cycle was detected!'c
|
||||
halt=. _1 c = S f NB. Halting when the current state is _1
|
||||
rt=. ((({. , ({: % 3:) , 3:) o $) $ ,) o (}."1) o (". ;. _2)
|
||||
NB. Reshaping the transition table as a 3D array (state,symbol,action)
|
||||
is=. ". o (, o ": o > , '=. ' , pipe o (DropIfNB;._2) o ". o ('0 ( : 0)'c)) f.av
|
||||
|
||||
m0=. ,~ NB. Dyadic form (e.g., TPF f TuringMachine QS f )
|
||||
m1=. 5 al NB. Appending 5 local boxes (B,M,PRINT,MOVE,C)
|
||||
m2=. ('' ; 0 c)e B}] NB. Initializing local B (empty defaults as 0)
|
||||
m3=. (0 ; rt o (Q f)) (C,Q)}] NB. Setting (the counter and) the transition table
|
||||
m4=. execute^:(-. o halt)^:_ NB. Executing until a halt instruction is issued
|
||||
m5=. [smoutput o cc ^: (-. o halt) NB. or a changeless single cycle is detected
|
||||
m6=. display NB. Displaying (returning) the final status
|
||||
NB.--------------------------------------------------------------------------------------
|
||||
|
||||
utm=. m6 o m5 o m4 o m3 o m2 o m1 o m0 f. NB. Fixing the universal Turing machine code
|
||||
NB. Producing the main (dyadic) verb utm...
|
||||
|
||||
lr=. 5!:5@< NB. Linear representation
|
||||
Note 0
|
||||
NB. X (boxed list)...
|
||||
Q - Instruction table
|
||||
S - Turing machine initial state
|
||||
|
||||
q: o $ o lr'utm' NB. The fixed tacit code length factors
|
||||
2 2 3 71
|
||||
NB. Y (boxed list)...
|
||||
T - Data tape
|
||||
P - Head position pointer
|
||||
F - Display frequency
|
||||
|
||||
(12 71 $ ]) o lr'utm' NB. The fixed tacit code...
|
||||
(((":@:(]&:>)@:(6&({::)) ,: (":@] 9&({::))) ,. ':'"_) ,. 2&({::) >@:(((
|
||||
48 + ]) { a."_)@[ ; (] $ ' '"_) , '^'"_) 3&({::))@:([ (0 0 $ 1!:2&2)@:(
|
||||
'A changeless cycle was detected!'"_)^:(-.@:(_1"_ = 1&({::))))@:((((3&(
|
||||
{::) + 8&({::)) ; 1 + 9&({::)) 3 9} ])@:(<@:((0: 0&({::)@]`(<@(1&({::))
|
||||
@])`(2&({::)@])} ])@:(7 3 2&{)) 2} ])@:(<"0@:(6&({::) (<@[ { ]) 0&({::)
|
||||
) 7 8 1} ])@:([ (0 0 $ 1!:2&2)@:(((":@:(]&:>)@:(6&({::)) ,: (":@] 9&({:
|
||||
:))) ,. ':'"_) ,. 2&({::) >@:(((48 + ]) { a."_)@[ ; (] $ ' '"_) , '^'"_
|
||||
) 3&({::))^:(0 = 4&({::) | 9&({::)))@:(<@:(1&({::) ; 3&({::) { 2&({::))
|
||||
6} ])@:(<@:(3&({::) + _1 = 3&({::)) 3} ])@:(<@:(((_1 = 3&({::)) {:: 5&
|
||||
({::)) , 2&({::) , (3&({::) = #@:(2&({::))) {:: 5&({::)) 2} ])^:(-.@:(_
|
||||
1"_ = 1&({::)))^:_)@:((0 ; (({. , ({: % 3:) , 3:)@:$ $ ,)@:(}."1)@:(".;
|
||||
._2)@:(0&({::))) 9 0} ])@:(<@:('' ; 0"_) 5} ])@:(5&(] , a: $~ [))@:(,~)
|
||||
NB. Local...
|
||||
B - Blank defaults
|
||||
M - State and tape symbol read
|
||||
PRINT - Printing symbol
|
||||
MOVE - Tape head moving instruction
|
||||
C - Step Counter
|
||||
)
|
||||
|
||||
'Q S T P F B M PRINT MOVE C'=. 10 Fetch NB. Fetching 10 Boxes
|
||||
|
||||
DisplayTape=. > o (((48 + ]) { a.c)@[ ; ((] $ ' 'c) , '^'c))
|
||||
display=. ((((": o (]&:>) o M) ,: (":@] C)) ,. ':'c ) ,. (T DisplayTape P))
|
||||
NB. Displaying state, symbol, tape / step and pointer
|
||||
|
||||
amend=. 0 (0 {:: ])`(<@:(1 {:: ]))`(2 {:: ])} ]
|
||||
|
||||
NB. execute (monadic verb)...
|
||||
|
||||
FillLeft=. (_1 = P ) {:: B NB. Expanding and filling the tape
|
||||
FillRight=. ( P = # o T) {:: B NB. with 0's (if necessary)
|
||||
ia=. <@[ { ] NB. Selecting by the indices of an array
|
||||
|
||||
execute is
|
||||
T`(FillLeft , T , FillRight)h NB. Adjusting the tape
|
||||
P`(P + _1 = P) h NB. and the pointer (if necessary)
|
||||
M`(S ; P { T) h NB. Updating the state and reading the tape symbol
|
||||
[ (smoutput o display)^:(0 = F | C) NB. Displaying intermediate cycles
|
||||
(PRINT MOVE S)`(<"0 o (M ia Q))h NB. Performing the printing, moving and state actions
|
||||
T`(amend o ((PRINT P T)f)) h NB. Printing symbol on tape at the pointer position
|
||||
(P C)`((P + MOVE) ; 1 + C) h NB. Updating the pointer and the counter
|
||||
)
|
||||
|
||||
cc=. 'A changeless cycle was detected!'c
|
||||
halt=. _1 c = S NB. Halting when the current state is _1
|
||||
rt=. ((({. , ({: % 3:) , 3:) o $) $ ,) o (}."1) o (". ;. _2)
|
||||
NB. Reshaping the transition table as a 3D array (state,symbol,action)
|
||||
|
||||
utm is NB. Universal Turing Machine (dyadic verb)
|
||||
,~ NB. Appending the arguments in reverse order
|
||||
,&(;:5$',') NB. Appending 5 local boxes (B M PRINT MOVE C)
|
||||
B`('' ; 0 c) h NB. Setting empty blank defaults as 0
|
||||
(C Q)`(0 ; rt o Q)h NB. Setting the counter and the transition table
|
||||
execute^:(-. o halt)^:_ NB. Executing until a halt instruction is issued
|
||||
[ smoutput o cc ^: (-. o halt) NB. or a changeless single cycle is detected
|
||||
display NB. Displaying (returning) the final status
|
||||
)
|
||||
|
||||
utm=. utm f. NB. Fixing the universal Turing machine code
|
||||
|
||||
NB. The simulation code is produced by 77 (-@:[ ]\ 5!:5@<@:]) 'utm'
|
||||
|
|
|
|||
|
|
@ -0,0 +1,85 @@
|
|||
import Base.show
|
||||
|
||||
@enum Move Left=1 Stay Right
|
||||
|
||||
mutable struct MachineState
|
||||
state::String
|
||||
tape::Dict{Int, String}
|
||||
headpos::Int
|
||||
end
|
||||
|
||||
struct Rule
|
||||
instate::String
|
||||
s1::String
|
||||
s2::String
|
||||
move::Move
|
||||
outstate::String
|
||||
end
|
||||
|
||||
struct Program
|
||||
title::String
|
||||
initial::String
|
||||
final::String
|
||||
blank::String
|
||||
rules::Vector{Rule}
|
||||
end
|
||||
|
||||
const testprograms = [
|
||||
(Program("Simple incrementer", "q0", "qf", "B",
|
||||
[Rule("q0", "1", "1", Right, "q0"), Rule("q0", "B", "1", Stay, "qf")]),
|
||||
Dict(1 =>"1", 2 => "1", 3 => "1"), true),
|
||||
(Program("Three-state busy beaver", "a", "halt", "0",
|
||||
[Rule("a", "0", "1", Right, "b"), Rule("a", "1", "1", Left, "c"),
|
||||
Rule("b", "0", "1", Left, "a"), Rule("b", "1", "1", Right, "b"),
|
||||
Rule("c", "0", "1", Left, "b"), Rule("c", "1", "1", Stay, "halt")]),
|
||||
Dict(), true),
|
||||
(Program("Five-state busy beaver", "A", "H", "0",
|
||||
[Rule("A", "0", "1", Right, "B"), Rule("A", "1", "1", Left, "C"),
|
||||
Rule("B", "0", "1", Right, "C"), Rule("B", "1", "1", Right, "B"),
|
||||
Rule("C", "0", "1", Right, "D"), Rule("C", "1", "0", Left, "E"),
|
||||
Rule("D", "0", "1", Left, "A"), Rule("D", "1", "1", Left, "D"),
|
||||
Rule("E", "0", "1", Stay, "H"), Rule("E", "1", "0", Left, "A")]),
|
||||
Dict(), false)]
|
||||
|
||||
function show(io::IO, mstate::MachineState)
|
||||
ibracket(i, curpos, val) = (i == curpos) ? "[$val]" : " $val "
|
||||
print(io, rpad("($(mstate.state))", 12))
|
||||
for i in sort(collect(keys(mstate.tape)))
|
||||
print(io, " $(ibracket(i, mstate.headpos, mstate.tape[i]))")
|
||||
end
|
||||
end
|
||||
|
||||
function turing(program, tape, verbose)
|
||||
println("\n$(program.title)")
|
||||
verbose && println(" State \tTape [head]\n--------------------------------------------------")
|
||||
mstate = MachineState(program.initial, tape, 1)
|
||||
stepcount = 0
|
||||
while true
|
||||
if !haskey(mstate.tape, mstate.headpos)
|
||||
mstate.tape[mstate.headpos] = program.blank
|
||||
end
|
||||
verbose && println(mstate)
|
||||
for rule in program.rules
|
||||
if rule.instate == mstate.state && rule.s1 == mstate.tape[mstate.headpos]
|
||||
mstate.tape[mstate.headpos] = rule.s2
|
||||
if rule.move == Left
|
||||
mstate.headpos -= 1
|
||||
elseif rule.move == Right
|
||||
mstate.headpos += 1
|
||||
end
|
||||
mstate.state = rule.outstate
|
||||
break
|
||||
end
|
||||
end
|
||||
stepcount += 1
|
||||
if mstate.state == program.final
|
||||
break
|
||||
end
|
||||
end
|
||||
verbose && println(mstate)
|
||||
println("Total steps: $stepcount")
|
||||
end
|
||||
|
||||
for (prog, tape, verbose) in testprograms
|
||||
turing(prog, tape, verbose)
|
||||
end
|
||||
|
|
@ -0,0 +1,245 @@
|
|||
package utm.scala
|
||||
|
||||
import scala.annotation.tailrec
|
||||
import scala.language.implicitConversions
|
||||
|
||||
/**
|
||||
* Implementation of Universal Turing Machine in Scala that can simulate an arbitrary
|
||||
* Turing machine on arbitrary input
|
||||
*
|
||||
* @author Abdulla Abdurakhmanov (https://github.com/abdmob/utms)
|
||||
*/
|
||||
class UniversalTuringMachine[S](val rules: List[UTMRule[S]],
|
||||
val initialState: S,
|
||||
val finalStates: Set[S],
|
||||
val blankSymbol: String,
|
||||
val inputTapeVals: Seq[String],
|
||||
printEveryIter: Int = 1) {
|
||||
|
||||
private val initialTape = UTMTape(inputTapeVals, 0, blankSymbol)
|
||||
|
||||
@tailrec
|
||||
private def iterate(state: S, curIteration: Int, tape: UTMTape): UTMTape = {
|
||||
val needToBePrinted = curIteration % printEveryIter == 0
|
||||
|
||||
if (needToBePrinted) {
|
||||
print(s"${curIteration}: ${state}: ")
|
||||
tape.printTape()
|
||||
}
|
||||
|
||||
if (finalStates.contains(state)) {
|
||||
println(s"Finished in the final state: ${state}")
|
||||
tape.printTape()
|
||||
tape
|
||||
}
|
||||
else {
|
||||
rules.find(rule => rule.state == state && rule.fromSymbol == tape.current()) match {
|
||||
case Some(rule) => {
|
||||
val updatedTape = tape.updated(
|
||||
rule.toSymbol,
|
||||
rule.action
|
||||
)
|
||||
|
||||
iterate(
|
||||
rule.toState,
|
||||
curIteration + 1,
|
||||
updatedTape
|
||||
)
|
||||
}
|
||||
case _ => {
|
||||
println(s"Finished: no suitable rules found for ${state}/${tape.current()}")
|
||||
tape.printTape()
|
||||
tape
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def run(): UTMTape = iterate(state = initialState, curIteration = 0, tape = initialTape)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Universal Turing Machine actions
|
||||
*/
|
||||
sealed trait UTMAction
|
||||
case class UTMLeft() extends UTMAction
|
||||
case class UTMRight() extends UTMAction
|
||||
case class UTMStay() extends UTMAction
|
||||
|
||||
/**
|
||||
* Universal Turing Machine rule definition
|
||||
*/
|
||||
case class UTMRule[S](state: S,
|
||||
fromSymbol: String,
|
||||
toSymbol: String,
|
||||
action: UTMAction,
|
||||
toState: S)
|
||||
|
||||
/**
|
||||
* Universal Turing Machine Tape
|
||||
*/
|
||||
case class UTMTape(content: Seq[String], position: Int, blankSymbol: String) {
|
||||
|
||||
private def updateContentAtPos(symbol: String) = {
|
||||
if (position >= content.length) {
|
||||
content :+ symbol
|
||||
}
|
||||
else if (position < 0) {
|
||||
symbol +: content
|
||||
}
|
||||
else
|
||||
content.updated(position, symbol)
|
||||
}
|
||||
|
||||
private[scala] def updated(symbol: String, action: UTMAction): UTMTape = {
|
||||
val updatedTape =
|
||||
this.copy(
|
||||
content = updateContentAtPos(symbol),
|
||||
position = action match {
|
||||
case UTMLeft() => position - 1
|
||||
case UTMRight() => position + 1
|
||||
case UTMStay() => position
|
||||
}
|
||||
)
|
||||
|
||||
if (updatedTape.position < 0) {
|
||||
updatedTape.copy(
|
||||
content = blankSymbol +: updatedTape.content,
|
||||
position = 0
|
||||
)
|
||||
}
|
||||
else if (updatedTape.position >= updatedTape.content.length) {
|
||||
updatedTape.copy(
|
||||
content = updatedTape.content :+ blankSymbol
|
||||
)
|
||||
}
|
||||
else
|
||||
updatedTape
|
||||
}
|
||||
|
||||
|
||||
private[scala] def current(): String = {
|
||||
if (content.isDefinedAt(position))
|
||||
content(position)
|
||||
else
|
||||
blankSymbol
|
||||
}
|
||||
|
||||
def printTape(): Unit = {
|
||||
print("[")
|
||||
if (position < 0)
|
||||
print("˅")
|
||||
content.zipWithIndex.foreach { case (symbol, index) =>
|
||||
if (position == index)
|
||||
print("˅")
|
||||
else
|
||||
print(" ")
|
||||
print(s"$symbol")
|
||||
}
|
||||
if (position >= content.length)
|
||||
print("˅")
|
||||
println("]")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
object UniversalTuringMachine extends App {
|
||||
|
||||
object dsl {
|
||||
|
||||
final val right = UTMRight()
|
||||
final val left = UTMLeft()
|
||||
final val stay = UTMStay()
|
||||
|
||||
implicit def tupleToUTMLRule[S](tuple: (S, String, String, UTMAction, S)): UTMRule[S] =
|
||||
UTMRule[S](tuple._1, tuple._2, tuple._3, tuple._4, tuple._5)
|
||||
}
|
||||
|
||||
main()
|
||||
|
||||
def main(): Unit = {
|
||||
import dsl._
|
||||
|
||||
def createIncrementMachine() = {
|
||||
|
||||
sealed trait IncrementStates
|
||||
case class q0() extends IncrementStates
|
||||
case class qf() extends IncrementStates
|
||||
|
||||
new UniversalTuringMachine[IncrementStates](
|
||||
rules = List(
|
||||
(q0(), "1", "1", right, q0()),
|
||||
(q0(), "B", "1", stay, qf())
|
||||
),
|
||||
initialState = q0(),
|
||||
finalStates = Set(qf()),
|
||||
blankSymbol = "B",
|
||||
inputTapeVals = Seq("1", "1", "1")
|
||||
).run()
|
||||
|
||||
}
|
||||
|
||||
def createThreeStateBusyBeaver() = {
|
||||
|
||||
sealed trait ThreeStateBusyStates
|
||||
case class a() extends ThreeStateBusyStates
|
||||
case class b() extends ThreeStateBusyStates
|
||||
case class c() extends ThreeStateBusyStates
|
||||
case class halt() extends ThreeStateBusyStates
|
||||
|
||||
new UniversalTuringMachine[ThreeStateBusyStates](
|
||||
rules = List(
|
||||
(a(), "0", "1", right, b()),
|
||||
(a(), "1", "1", left, c()),
|
||||
(b(), "0", "1", left, a()),
|
||||
(b(), "1", "1", right, b()),
|
||||
(c(), "0", "1", left, b()),
|
||||
(c(), "1", "1", stay, halt())
|
||||
),
|
||||
initialState = a(),
|
||||
finalStates = Set(halt()),
|
||||
blankSymbol = "0",
|
||||
inputTapeVals = Seq()
|
||||
).run()
|
||||
|
||||
}
|
||||
|
||||
def createFiveState2SymBusyBeaverMachine() = {
|
||||
sealed trait FiveBeaverStates
|
||||
case class FA() extends FiveBeaverStates
|
||||
case class FB() extends FiveBeaverStates
|
||||
case class FC() extends FiveBeaverStates
|
||||
case class FD() extends FiveBeaverStates
|
||||
case class FE() extends FiveBeaverStates
|
||||
case class FH() extends FiveBeaverStates
|
||||
|
||||
new UniversalTuringMachine[FiveBeaverStates](
|
||||
rules = List(
|
||||
(FA(), "0", "1", right, FB()),
|
||||
(FA(), "1", "1", left, FC()),
|
||||
(FB(), "0", "1", right, FC()),
|
||||
(FB(), "1", "1", right, FB()),
|
||||
(FC(), "0", "1", right, FD()),
|
||||
(FC(), "1", "0", left, FE()),
|
||||
(FD(), "0", "1", left, FA()),
|
||||
(FD(), "1", "1", left, FD()),
|
||||
(FE(), "0", "1", stay, FH()),
|
||||
(FE(), "1", "0", left, FA())
|
||||
),
|
||||
initialState = FA(),
|
||||
finalStates = Set(FH()),
|
||||
blankSymbol = "0",
|
||||
inputTapeVals = Seq(),
|
||||
printEveryIter = 100000
|
||||
).run()
|
||||
}
|
||||
|
||||
createIncrementMachine()
|
||||
createThreeStateBusyBeaver()
|
||||
|
||||
// careful here, 47 mln iterations,
|
||||
// so this is commented to save our nature (I checked it for you anyway):
|
||||
// createFiveState2SymBusyBeaverMachine()
|
||||
}
|
||||
}
|
||||
113
Task/Universal-Turing-machine/VBA/universal-turing-machine.vba
Normal file
113
Task/Universal-Turing-machine/VBA/universal-turing-machine.vba
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
Option Base 1
|
||||
Public Enum sett
|
||||
name_ = 1
|
||||
initState
|
||||
endState
|
||||
blank
|
||||
rules
|
||||
End Enum
|
||||
Public incrementer As Variant, threeStateBB As Variant, fiveStateBB As Variant
|
||||
'-- Machine definitions
|
||||
Private Sub init()
|
||||
incrementer = Array("Simple incrementer", _
|
||||
"q0", _
|
||||
"qf", _
|
||||
"B", _
|
||||
Array( _
|
||||
Array("q0", "1", "1", "right", "q0"), _
|
||||
Array("q0", "B", "1", "stay", "qf")))
|
||||
threeStateBB = Array("Three-state busy beaver", _
|
||||
"a", _
|
||||
"halt", _
|
||||
"0", _
|
||||
Array( _
|
||||
Array("a", "0", "1", "right", "b"), _
|
||||
Array("a", "1", "1", "left", "c"), _
|
||||
Array("b", "0", "1", "left", "a"), _
|
||||
Array("b", "1", "1", "right", "b"), _
|
||||
Array("c", "0", "1", "left", "b"), _
|
||||
Array("c", "1", "1", "stay", "halt")))
|
||||
fiveStateBB = Array("Five-state busy beaver", _
|
||||
"A", _
|
||||
"H", _
|
||||
"0", _
|
||||
Array( _
|
||||
Array("A", "0", "1", "right", "B"), _
|
||||
Array("A", "1", "1", "left", "C"), _
|
||||
Array("B", "0", "1", "right", "C"), _
|
||||
Array("B", "1", "1", "right", "B"), _
|
||||
Array("C", "0", "1", "right", "D"), _
|
||||
Array("C", "1", "0", "left", "E"), _
|
||||
Array("D", "0", "1", "left", "A"), _
|
||||
Array("D", "1", "1", "left", "D"), _
|
||||
Array("E", "0", "1", "stay", "H"), _
|
||||
Array("E", "1", "0", "left", "A")))
|
||||
End Sub
|
||||
|
||||
Private Sub show(state As String, headpos As Long, tape As Collection)
|
||||
Debug.Print " "; state; String$(7 - Len(state), " "); "| ";
|
||||
For p = 1 To tape.Count
|
||||
Debug.Print IIf(p = headpos, "[" & tape(p) & "]", " " & tape(p) & " ");
|
||||
Next p
|
||||
Debug.Print
|
||||
End Sub
|
||||
|
||||
'-- a universal turing machine
|
||||
Private Sub UTM(machine As Variant, tape As Collection, Optional countOnly As Long = 0)
|
||||
Dim state As String: state = machine(initState)
|
||||
Dim headpos As Long: headpos = 1
|
||||
Dim counter As Long, rule As Variant
|
||||
Debug.Print machine(name_); vbCrLf; String$(Len(machine(name_)), "=")
|
||||
If Not countOnly Then Debug.Print " State | Tape [head]" & vbCrLf & "---------------------"
|
||||
Do While True
|
||||
If headpos > tape.Count Then
|
||||
tape.Add machine(blank)
|
||||
Else
|
||||
If headpos < 1 Then
|
||||
tape.Add machine(blank), Before:=1
|
||||
headpos = 1
|
||||
End If
|
||||
End If
|
||||
If Not countOnly Then show state, headpos, tape
|
||||
For i = LBound(machine(rules)) To UBound(machine(rules))
|
||||
rule = machine(rules)(i)
|
||||
If rule(1) = state And rule(2) = tape(headpos) Then
|
||||
tape.Remove headpos
|
||||
If headpos > tape.Count Then
|
||||
tape.Add rule(3)
|
||||
Else
|
||||
tape.Add rule(3), Before:=headpos
|
||||
End If
|
||||
If rule(4) = "left" Then headpos = headpos - 1
|
||||
If rule(4) = "right" Then headpos = headpos + 1
|
||||
state = rule(5)
|
||||
Exit For
|
||||
End If
|
||||
Next i
|
||||
counter = counter + 1
|
||||
If counter Mod 100000 = 0 Then
|
||||
Debug.Print counter
|
||||
DoEvents
|
||||
DoEvents
|
||||
End If
|
||||
If state = machine(endState) Then Exit Do
|
||||
Loop
|
||||
DoEvents
|
||||
If countOnly Then
|
||||
Debug.Print "Steps taken: ", counter
|
||||
Else
|
||||
show state, headpos, tape
|
||||
Debug.Print
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Public Sub main()
|
||||
init
|
||||
Dim tap As New Collection
|
||||
tap.Add "1": tap.Add "1": tap.Add "1"
|
||||
UTM incrementer, tap
|
||||
Set tap = New Collection
|
||||
UTM threeStateBB, tap
|
||||
Set tap = New Collection
|
||||
UTM fiveStateBB, tap, countOnly:=-1
|
||||
End Sub
|
||||
Loading…
Add table
Add a link
Reference in a new issue