## Turing Machine

A Turing Machine is a theoretical machine that resembles a very primitive computing device. A TM is composed of an infinitely long tape, and a head that can read and write from and to it. Every 'step', the Turing Machine reads from the tape, and based on the symbol and the current machine 'state', moves the head forward or backward along the tape, and changes the tape, if need be. While this seems extremely limited, these are the fundamental functions of computation, and using only these functions, a large-enough Turing Machine is capable of solving any problem that a supercomputer can solve. The Wikipedia article on Turing Machines is an excellent place to learn more about them.

Compatibility: PennMUSH.

### Instructions

Copy and paste the below code into a compatible MUSH or MUX.

### MUSHCode for Turing Machine

@create Turing Machine
@lock/Basic Turing Machine==me
@lset Turing Machine/Basic=no_inherit
@set Turing Machine = VISUAL
@set Turing Machine = !NO_COMMAND
&ACT_LOOP Turing Machine=@switch [and(gt(v(TM_CREDIT),0),hasattr(me,TM_Q[u(TM_STATE)]))]=1,{@trigger me/ACT_STEP;&TM_CREDIT me = [dec(v(TM_CREDIT))];@wait 5=@trigger me/ACT_LOOP},{@emit The Turing Machine stops running.;&TM_CREDIT me=0}
&APAYMENT Turing Machine=@assert [eq(v(TM_CREDIT),0)];&TM_CREDIT me=10;@trigger me/ACT_LOOP
@set Turing Machine/APAYMENT=no_command prefixmatch
&COM_CLEAR Turing Machine=\$+tm/clear: think [iter(lnum(100),if(hasattr(me,TM_Q##),attrib_set(me/TM_Q##),ibreak()))];@emit Transition table cleared.
&COM_EXAMPLE Turing Machine=\$+tm/example:&TM_CREDIT me=0;&TM_STATE me=0;&TM_HEAD me=0;&TM_TAPE me=10001_____;&TM_OTAPE me=10001_____;&TM_Q0 me=1xR1;&TM_Q1 me=0xR1 1xR2;&TM_Q2 me=__LA;&TM_Q3 me;@emit The Turing Machine has been programmed to accept input that matches the%rregular expression '10*1'. Use 'give 1 to Turing Machine' to try it on the%rstring on the tape already. Use '+tm/tape <string>' to create your own tape%rfor the machine to solve (make sure you end it with at least one '_' symbol).%rTo look at the configuration, use '+tm/view'.
&COM_HELP1 Turing Machine=\$+tm/help:@pemit %#=[u(HELP_DEFAULT)]
&COM_HELP2 Turing Machine=\$+tm/help *:@switch [hasattr(me,HELP_%0)]=1,@pemit %#=[u(HELP_%0)],@pemit %#=No such help topic.
&COM_RESET Turing Machine=\$+tm/reset:&TM_HEAD me=0;&TM_STATE me=0;&TM_TAPE me=[v(TM_OTAPE)];&TM_CREDIT me=0;@emit Turing Machine reset.
&COM_SET Turing Machine=\$+tm/set *=*:@switch [and(regmatch(%0,^\[0-9\]\[0-9\]?\$),regmatch(%1,\(\\b\[a-zA-Z0-9_\]\[a-zA-Z0-9_\]\[LR\]\(\[0-9\]+|\[AR\]\)\\b\)+))]=1,{&TM_Q%0 me=%1;@emit Q%0 set.},@pemit %#=There was an error in your state string. See +tm/help for more info.
&COM_TAPE Turing Machine=\$+tm/tape *:@switch [regmatch(%0,^\[a-zA-Z0-9_\]+\$)]=1,{&TM_TAPE me=%0;&TM_OTAPE me=%0;&TM_HEAD me=[min(dec(strlen(%0)),v(TM_HEAD))];@emit Tape set.},@pemit %#=Strings may only contain a-z, A-Z, 0-9, and _.
&COM_UNSET Turing Machine=\$+tm/unset *:@switch [regmatch(%0,^\[0-9\]\[0-9\]?\$)]=1,{&TM_Q%0 me;@emit Q%0 cleared.},@pemit %#=No such state.
&COM_VIEW Turing Machine=\$+tm/view:@pemit %#=Turing Machine%rState: [v(TM_STATE)]%rTape: [u(VAR_STAT)][iter(lnum(100),if(hasattr(me,TM_Q##),%r Q[ljust(##,2)]: [v(TM_Q##)],ibreak()))]
&COST Turing Machine=1
@set Turing Machine/COST=no_command prefixmatch
&DESCRIBE Turing Machine=It's [art(money(1))] [money(1)]-operated Turing Machine[switch(v(TM_STATE),A,%bin the [ansi(hg,accept)] state,R,%bin the [ansi(hr,reject)] state,)].%rThe tape currently reads: [u(VAR_STAT)]%rUse '+tm/help' to find out about how to use it.
@set Turing Machine/DESCRIBE=no_command visual prefixmatch public nearby
&HELP_AUTHOR Turing Machine=%rThis program was written by Markus from M*U*S*H. You're free to use it however%ryou like - study it, modify it, sell it - I don't care. I've set the object%rVISUAL, so you should be able to @decompile it or examine it and get all the%rsource code. If you like it or have any comments about it, send me some @mail.%r
&HELP_BUGS Turing Machine=%r+tm/view and +tm/clear only work on sequential states (for efficiency).%r
&HELP_COMMANDS Turing Machine=%r+tm/view: Look at the state of the TM and the transition table.%r+tm/reset: Reset the state, head, and tape.%r+tm/clear: Clear the transition table.%r+tm/tape <string>: Initialize the (finite) tape. The tape may only contain%r[space(19)]the characters 0, 1, and _.%r+tm/set <Q>=<transitions>: Set transitions for state Q. See '+tm/help transitions'%r+tm/unset <Q>: Clear the transitions for Q.%r+tm/example: Set the machine up to an example configuration.%r
&HELP_DEFAULT Turing Machine=%rTopics:%r overview - What are Turing Machines (TMs) anyway?%r commands - Commands for this TM%r transitions - Programming this TM%r bugs - Problems I already know about%r author - Copyright and Stuff%r
&HELP_OVERVIEW Turing Machine=A Turing Machine is a theoretical machine that resembles a very primitive%rcomputing device. A TM is composed of an infinitely long tape, and a head%rthat can read and write from and to it. Every 'step', the Turing Machine reads%rfrom the tape, and based on the symbol and the current machine 'state', moves%rthe head forward or backward along the tape, and changes the tape, if need be.%rWhile this seems extremely limited, these are the fundamental functions of%rcomputation, and using only these functions, a large-enough Turing Machine is%rcapable of solving any problem that a supercomputer can solve. The Wikipedia%rarticle on Turing Machines is an excellent place to learn more about them.
&HELP_TRANSITIONS Turing Machine=%rA transition for a state is represented as a list of strings in the form WXYZ.%rW is the character that is currently on the tape under the head. X is the%rcharacter that should be written to the tape (a-z, A-Z, 0-9, or _) Y is the%rdirection that the head should move after writing (should be L or R) Z is the%rstate of the machine after moving the head. This should either be a number%rfrom 0-99, A (the accept state), or R (the reject state). Should the state in%rquestion be undefined, it is assumed to be a transition to the Reject state.%rLook at +tm/example to see a working example program.%r
&OPAYMENT Turing Machine=drops [art(money(1))] [money(1)] into the Turing Machine.
@set Turing Machine/OPAYMENT=no_command prefixmatch
&PAYMENT Turing Machine=You drop [art(money(1))] [money(1)] into the Turing Machine.
@set Turing Machine/PAYMENT=no_command prefixmatch
&TM_CREDIT Turing Machine=0
@set Turing Machine/TM_CREDIT=no_command