Giter Club home page Giter Club logo

turingmachine's Introduction

Turing Machines in R

Specification

The TuRingMachine repo uses a single tape Turing machine ("TM") specified as a 7-tuple:

TM := <Q, L, b, S, d, q(0), F>

Where:

  • Q is a finite, non-empty set of states;

  • L is the set of tape alphabet symbols: {0, 1};

  • b (in L) is the blank symbol: {0} (the only symbol allowed to occur on the tape infinitely often at any step during the computation);

  • S is the set of input symbols that are allowed to appear in the initial tape contents: {0, 1};

  • q(0) (belonging to Q) is the initial state: {0};

  • F (subset of Q) is the set of final states or accepting states. The initial tape contents is said to be accepted by TM if it eventually halts in a state from F.

  • d is the transition function, which takes as inputs:

    • State q(x) (in Q)
    • Alphabet symbol l (in L)

    And outputs:

    • State q(y) (in Q)
    • Alphabet symbol l (in L)
    • A move direction:
      • "L" (i.e. move the tape head one position to the left), and
      • "R" (i.e. move right)

Specifically the Turing machine instructions are encoded using Penrose's schema:

  • 0 for 0
  • 10 for 1
  • 110 for R, move the tape right
  • 1110 for L, move the tape left
  • 11110 for H, the halting move

Initialisation

Any TuRingMachine in this repo is initialised as:

function_name(input, blank_symbol, instruction_set, tape_moves = 999)

Where:

  • function_name is a tuRingMachine function within /R
  • input is an atomic vector
  • instruction_set is a data.frame that has the following column entries:
    • current_state
    • tape_symbol
    • next_state
    • print_symbol
    • move_tape

Here the instruction_set is used by the TM as the transition function.

Output

There are two types of tuRingMachine functions for each type of operation:

  1. The base function will output a data.frame with the following columns:
  • input as an atomic vector
  • output as an atomic vector
  • status providing a binary outcome "Accepted" or "Rejected"
  • moves, as an integer, being the number of times the tape was moved by the TM
  1. The equivalent function with _log appended to the function name will output a data.frame with the following columns:
  • input as an atomic vector
  • current_state for each tape move from the instruction_set
  • tape_symbol as the current tape symbol read by the TM for each tape move
  • tape_position as the position along the input that the TM is for each tape move

This allows you to track the internals of the TM.

Usage

Examples of using a turRingMachine function is given in /examples.

Pre-configured instruction sets are given in /data.

Using Penrose's schema we find that the Turing machine that adds one in binary ("XN + 1") is represented by the 450,813,704,461,563,958,982,113,775,643,437,908th Turing machine, as shown in /examples/TuringMachines.R.

We also find that Turing machine 177,642 (under Penrose's encoding schema) corresponds to a machine that adds one to a unary input ("UN + 1"), again shown in /examples/TuringMachines.R.

Acknowledgement

This repo is an R implementation of the Turing machines specified in Chapter 2 "Algorithms and Turing Machines" in Roger Penrose's book "The Emperor's New Mind".

License

tuRingMachine is open source software licensed as MIT.

turingmachine's People

Contributors

alexgarbiak avatar agarbiak avatar

Stargazers

 avatar Jimmy Briggs avatar  avatar

Watchers

 avatar

turingmachine's Issues

Add validation for R/UN_gcd.R

Add input validation for UN_gcd() and UN_gcd_log() similar to UN_x2.R:

tuRingMachine/R/UN_x2.R

Lines 14 to 44 in 839e0b1

# Ensure input symbols match tape_language
if (
sum(!(input %in% tape_language)) > 0
) {
unrecognised_symbols <- unique(input[!(input %in% tape_language)])
stop(
paste0(
"Input contains symbols that are not specified in the instruction set.",
"The unrecognised symbols are: ",
paste(unrecognised_symbols, collapse = ", "),
"."
)
)
}
# Ensure input is correctly specified for UN_ function
if (
sum(input %in% "1") != length(input)
) {
invalid_symbols <- unique(
input[!(input %in% "1")]
)
stop(
paste0(
"Input contains non-unary symbols. UN_ functions require unary inputs.",
"The invalid symbols are: ",
paste(invalid_symbols, collapse = ", "),
"."
)
)
}

Add user input validation

Ensure that user input is valid for the binary_add_one() function before executing the Turing machine.
Suggest that a lookup is performed against the instruction_set object for the tape alphabet symbols as a standard feature.
Additional validation will be required for specific tuRingMachine functions.

input <- c(
blank_symbol, # Pre-tape input
strsplit(as.character(input), split="")[[1]],
blank_symbol # End of tape input
)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.