Class: Automata::Turing
- Inherits:
-
StateDiagram
- Object
- StateDiagram
- Automata::Turing
- Defined in:
- lib/automata/turing.rb
Overview
Turing Machine
Instance Attribute Summary collapse
-
#inputAlphabet ⇒ Object
Returns the value of attribute inputAlphabet.
-
#reject ⇒ Object
Returns the value of attribute reject.
-
#tape ⇒ Object
Returns the value of attribute tape.
-
#tapeAlphabet ⇒ Object
Returns the value of attribute tapeAlphabet.
Attributes inherited from StateDiagram
#accept, #alphabet, #start, #states, #transitions
Instance Method Summary collapse
-
#accepts?(input) ⇒ Boolean
Determines whether the TM halts in an accept state.
-
#feed(input) ⇒ Hash
Runs the input on the turing machine and returns a Hash describing the machine’s final state after running.
-
#has_transition?(state, read) ⇒ Boolean
Determines whether or not any transition states exist given a beginning state and input symbol pair.
-
#initialize(params = {}) ⇒ Turing
constructor
A new instance of Turing.
-
#rejects?(input) ⇒ Boolean
Determines whether the TM halts in an reject state.
-
#transition(state, symbol) ⇒ Array
Determines the transition states, if any, from a given beginning state and input symbol pair.
Constructor Details
#initialize(params = {}) ⇒ Turing
Check that each state is connected. Iterate through each states to verify the graph is not disjoint.
Returns a new instance of Turing.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/automata/turing.rb', line 10 def initialize(params={}) yaml = {} yaml = YAML::load_file(params[:file]) if params.has_key? :file super(params) @inputAlphabet = params[:inputAlphabet] || yaml['inputAlphabet'] @tapeAlphabet = params[:tapeAlphabet] || yaml['tapeAlphabet'] @tape = Tape.new @accept = false @reject = false end |
Instance Attribute Details
#inputAlphabet ⇒ Object
Returns the value of attribute inputAlphabet.
4 5 6 |
# File 'lib/automata/turing.rb', line 4 def inputAlphabet @inputAlphabet end |
#reject ⇒ Object
Returns the value of attribute reject.
4 5 6 |
# File 'lib/automata/turing.rb', line 4 def reject @reject end |
#tape ⇒ Object
Returns the value of attribute tape.
4 5 6 |
# File 'lib/automata/turing.rb', line 4 def tape @tape end |
#tapeAlphabet ⇒ Object
Returns the value of attribute tapeAlphabet.
4 5 6 |
# File 'lib/automata/turing.rb', line 4 def tapeAlphabet @tapeAlphabet end |
Instance Method Details
#accepts?(input) ⇒ Boolean
Determines whether the TM halts in an accept state.
61 62 63 64 |
# File 'lib/automata/turing.rb', line 61 def accepts?(input) resp = feed(input) resp[:accept] end |
#feed(input) ⇒ Hash
Runs the input on the turing machine and returns a Hash describing the machine’s final state after running.
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 |
# File 'lib/automata/turing.rb', line 31 def feed(input) @tape = Tape.new(input) @accept = false @reject = false stateHead = @start.to_s input.each_char do |symbol| toState = transition(stateHead, symbol) if @accept || @reject break else stateHead = toState end end resp = { input: input, accept: @accept, reject: @reject, head: stateHead, tape: @tape.memory, output: @tape.output } resp end |
#has_transition?(state, read) ⇒ Boolean
Determines whether or not any transition states exist given a beginning state and input symbol pair.
99 100 101 102 |
# File 'lib/automata/turing.rb', line 99 def has_transition?(state, read) return false unless @transitions.include? state @transitions[state].has_key? read end |
#rejects?(input) ⇒ Boolean
Determines whether the TM halts in an reject state.
70 71 72 73 |
# File 'lib/automata/turing.rb', line 70 def rejects?(input) resp = feed(input) resp[:reject] end |
#transition(state, symbol) ⇒ Array
Turing machines have two unique states:
-
ACCEPT
Unique accept state -
REJECT
Unique reject state
Determines the transition states, if any, from a given beginning state and input symbol pair.
84 85 86 87 88 89 90 91 92 |
# File 'lib/automata/turing.rb', line 84 def transition(state, symbol) actions = @transitions[state][symbol] @tape.transition(symbol, actions['write'], actions['move']) # Flip the bits for halting states @accept = true if actions['to'] == 'ACCEPT' @reject = true if actions['to'] == 'REJECT' @head = actions['to'] end |