ntm 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5ceeff2579a8e75cee61eb5fec157cc7607360fa
4
+ data.tar.gz: 73bd5125c4be9fe12f49569b9245877b06cb4873
5
+ SHA512:
6
+ metadata.gz: d40383f4a79d1c1f5dfe334aa0aef49b4f74e9fcc2b83c8811fa43b490299bdb830f67ebe7816efe8f6592ed766d9492a91a669d50a2026cb5f23af25b9245f8
7
+ data.tar.gz: ed693121a924b11d77768e17451c0496f4851ccf46dac6f89eb83d1c9e86bdb036566990c08b5332c134edd01f82902c3d4b5e45de649a2328e060481ace6081
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ntm.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Bayram Kuliyev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,116 @@
1
+ # Ntm
2
+
3
+ Ntm simulates nondeterministic Turing machines. It may also be used to simulate deterministic Turing machines.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ntm'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ntm
20
+
21
+ ## Ntm examples
22
+ #### Example 1
23
+ state|symbol|move
24
+ -----|------|--------
25
+ q<sub>0</sub> | 0 |(q<sub>1</sub>,0,R), (q<sub>1</sub>,1,R)
26
+ q<sub>1</sub> | 1 |(q<sub>2</sub>,0,R), (q<sub>2</sub>,1,R)
27
+ ```ruby
28
+ require "ntm"
29
+
30
+ ntm = Ntm.new(initial_state: 0) do
31
+ given({state:0, symbol: '0'}) do
32
+ transition(state:1, symbol:'0', move: :right)
33
+ transition(state:1, symbol:'1', move: :right)
34
+ end
35
+
36
+ given({state:1, symbol: '1'}) do
37
+ transition(state:2, symbol:'0', move: :right)
38
+ transition(state:2, symbol:'1', move: :right)
39
+ end
40
+ end
41
+
42
+ configurations = ntm.run({max_depth:250, tape_content: "011" })
43
+
44
+ configurations.each do |conf|
45
+ conf.path.each do |p|
46
+ instr = p[:instr]
47
+ print p[:config] + " (#{instr[:state]},#{instr[:symbol]},#{instr[:move][0]}) => "
48
+ end
49
+ puts conf.to_s
50
+ end
51
+ ```
52
+ #### Output: four possible compuatations
53
+ [q0]011 (1,0,r) => 0[q1]11 (2,0,r) => 00[q2]1
54
+
55
+ [q0]011 (1,0,r) => 0[q1]11 (2,1,r) => 01[q2]1
56
+
57
+ [q0]011 (1,1,r) => 1[q1]11 (2,0,r) => 10[q2]1
58
+
59
+ [q0]011 (1,1,r) => 1[q1]11 (2,1,r) => 11[q2]1
60
+
61
+ #### Example 2: a deterministic TM accepting language the language {0<sup>n</sup>1<sup>n</sup> | n > 0 } - n 0s followed by n 1s
62
+ state|symbol|move
63
+ -----|------|--------
64
+ q<sub>0</sub> | 0 |(q<sub>1</sub>,X,R)
65
+ q<sub>0</sub> | Y |(q<sub>3</sub>,Y,R)
66
+ q<sub>1</sub> | 0 |(q<sub>1</sub>,0,R)
67
+ q<sub>1</sub> | 1 |(q<sub>2</sub>,Y,L)
68
+ q<sub>1</sub> | Y |(q<sub>1</sub>,Y,R)
69
+ q<sub>2</sub> | 0 |(q<sub>2</sub>,0,L)
70
+ q<sub>2</sub> | X |(q<sub>0</sub>,X,R)
71
+ q<sub>2</sub> | Y |(q<sub>2</sub>,Y,L)
72
+ q<sub>3</sub> | Y |(q<sub>3</sub>,Y,R)
73
+ q<sub>3</sub> | # |(q<sub>4</sub>,#,R)
74
+
75
+ Accept state: q<sub>4</sub>
76
+ ```ruby
77
+ require "ntm"
78
+
79
+ ntm = Ntm.new(initial_state: 0) do
80
+ given({state:0, symbol:'0'}) { transition(state:1, symbol:'X', move: :right) }
81
+ given({state:0, symbol:'Y'}) { transition(state:3, symbol:'Y', move: :right) }
82
+ given({state:1, symbol:'0'}) { transition(state:1, symbol:'0', move: :right) }
83
+ given({state:1, symbol:'1'}) { transition(state:2, symbol:'Y', move: :left) }
84
+ given({state:1, symbol:'Y'}) { transition(state:1, symbol:'Y', move: :right) }
85
+ given({state:2, symbol:'0'}) { transition(state:2, symbol:'0', move: :left) }
86
+ given({state:2, symbol:'X'}) { transition(state:0, symbol:'X', move: :right) }
87
+ given({state:2, symbol:'Y'}) { transition(state:2, symbol:'Y', move: :left) }
88
+ given({state:3, symbol:'Y'}) { transition(state:3, symbol:'Y', move: :right) }
89
+ given({state:3, symbol:'#'}) { transition(state:4, symbol:'#', move: :right) }
90
+ end
91
+
92
+ configurations = ntm.run({max_depth:250, tape_content: "0011", accept_states:[4] })
93
+
94
+ configurations.each do |conf|
95
+ conf.path.each do |p|
96
+ instr = p[:instr]
97
+ print p[:config] + " (#{instr[:state]},#{instr[:symbol]},#{instr[:move][0]}) => "
98
+ end
99
+ puts conf.to_s
100
+ end
101
+ ```
102
+ #### Output
103
+ [q0]0011 (1,X,r) => X[q1]011 (1,0,r) => X0[q1]11 (2,Y,l) => X[q2]0Y1 (2,0,l) => [q2]X0Y1 (0,X,r) => X[q0]0Y1 (1,X,r) => XX[q1]Y1 (1,Y,r) => XXY[q1]1 (2,Y,l) => XX[q2]YY (2,Y,l) => X[q2]XYY (0,X,r) => XX[q0]YY (3,Y,r) => XXY[q3]Y (3,Y,r) => XXYY[q3] (4,#,r) => XXYY#[q4]
104
+ ## Development
105
+
106
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
107
+
108
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
109
+
110
+ ## Contributing
111
+
112
+ 1. Fork it ( https://github.com/[my-github-username]/ntm/fork )
113
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
114
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
115
+ 4. Push to the branch (`git push origin my-new-feature`)
116
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/Users/bayram/.rbenv/shims/ruby ruby
2
+
3
+ require "bundler/setup"
4
+ require "ntm"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/ntm.rb ADDED
@@ -0,0 +1,157 @@
1
+ require "ntm/version"
2
+ require 'ntm/tape'
3
+ require 'ntm/configuration'
4
+
5
+ class Ntm
6
+
7
+ DEFAULT_MAX_DEPTH = 10
8
+ #attr_reader :current_state
9
+ attr_reader :instructions
10
+
11
+ def initialize(options={}, &block)
12
+ tape = Tape.new(content: options[:tape_content], head_position: options[:head_position].to_i)
13
+ @initial_state = options[:initial_state] || 0
14
+ @initial_config = Configuration.new(state: @initial_state, tape: tape)
15
+ #puts "initial state: #{initial_state}"
16
+ @config_queue = [@initial_config]
17
+ @instructions = {}
18
+ @result_of_computation = []
19
+
20
+ @input = nil
21
+
22
+ instance_eval(&block) if block_given?
23
+ end
24
+
25
+
26
+ def transition(options)
27
+ raise StandardError, 'Missing output state of the transition function!' unless options[:state]
28
+ raise StandardError, 'Missing write-symbol of the transition function!' unless options[:symbol]
29
+ raise StandardError, 'Missing head-move direction (right/left) of the transition function!' unless options[:move]
30
+
31
+ add_instruction(@input, {state:options[:state], symbol:options[:symbol], move:options[:move]})
32
+ end
33
+
34
+ def given(options, &block)
35
+ raise StandardError, 'Missing input state of the transition function!' unless options[:state]
36
+ raise StandardError, 'Missing input symbol of the transition function!' unless options[:symbol]
37
+
38
+ @input = { state:options[:state], symbol: options[:symbol] }
39
+ instance_eval(&block)
40
+ end
41
+
42
+
43
+
44
+ def reset
45
+ @config_queue = [@initial_config]
46
+ @result_of_computation = []
47
+ end
48
+
49
+
50
+ # add a single instruction to the instructions set
51
+ # instructions must be in the following format:
52
+ #
53
+ # given is a hash {state:1, symbol: '0'}
54
+ # transition is a hash {state:2, symbol: '1', move: :right}
55
+ #
56
+ def add_instruction(given, transition)
57
+ if instructions[given]
58
+ instructions[given] << transition
59
+ else
60
+ instructions[given] = [transition]
61
+ end
62
+ end
63
+
64
+
65
+ def instructions_count
66
+ count = 0
67
+ instructions.each_value { |instrs| count += instrs.size }
68
+ count
69
+ end
70
+
71
+
72
+ def run(options = {})
73
+
74
+ max_depth = options[:max_depth] || DEFAULT_MAX_DEPTH
75
+ max_depth = max_depth.to_i
76
+ accept_states = options[:accept_states] || []
77
+
78
+ raise StandardError, 'Maximum depth must be a positive number!' unless max_depth > 0
79
+
80
+
81
+ if options[:tape_content] || options[:head_position]
82
+ tape_content = options[:tape_content] || @initial_config.tape.content
83
+ head_position = options[:head_position] || @initial_config.tape.head_position
84
+ tape = Tape.new(content: tape_content, head_position: head_position.to_i)
85
+ @config_queue = [Configuration.new(state: @initial_state, tape: tape)]
86
+ end
87
+
88
+ loop do
89
+ break if @config_queue.empty?
90
+
91
+ config = deque_configuration
92
+
93
+ if accept_states.include?(config.state)
94
+ @result_of_computation << config.dup
95
+ next
96
+ end
97
+
98
+ instrs = instructions[{state: config.state, symbol: config.tape.read}]
99
+ if instrs == nil
100
+ @result_of_computation << config.dup
101
+ next
102
+ end
103
+
104
+ instrs.each do |instr|
105
+ new_config = run_instruction(instr, config)
106
+ # increase the depth
107
+ new_config.depth = config.depth + 1
108
+ # add the path
109
+ new_config.path = config.path.dup << {instr:instr, config:config.to_s}
110
+ #puts "#{new_config.depth} = #{new_config.to_s}"
111
+ if new_config.depth >= max_depth
112
+ @result_of_computation << new_config.dup
113
+ else
114
+ enque_configuration(new_config)
115
+ end
116
+ end
117
+ end
118
+
119
+ @result_of_computation.dup
120
+ end
121
+
122
+
123
+
124
+
125
+ def enque_configuration(config)
126
+ @config_queue.push(config.dup)
127
+ end
128
+
129
+
130
+ def deque_configuration
131
+ raise StandardError, 'Empty queue!' if @config_queue.size == 0
132
+ @config_queue.delete_at(0)
133
+ end
134
+
135
+ # runs the instruction in context of the given configuration
136
+ # generates a new configuration
137
+ def run_instruction(instr, config)
138
+ conf = config.dup
139
+ # write a symbol to the tape
140
+ conf.tape.write(instr[:symbol])
141
+ # go to a new state
142
+ conf.state = instr[:state]
143
+ # move the tape-head
144
+ if instr[:move] == :left
145
+ conf.tape.move_left
146
+ elsif instr[:move] == :right
147
+ conf.tape.move_right
148
+ else
149
+ raise StandardError, 'Tape-head may move only Right or Left!'
150
+ end
151
+
152
+ conf
153
+ end
154
+
155
+
156
+ private :run_instruction, :enque_configuration, :deque_configuration
157
+ end
@@ -0,0 +1,26 @@
1
+ #require "tape"
2
+
3
+ class Configuration
4
+ attr_accessor :state, :tape, :depth, :path
5
+
6
+ def initialize(options = {})
7
+ @tape = options[:tape] || Tape.new
8
+ @state = options[:state].to_i
9
+ @depth = options[:depth].to_i || 1
10
+ @path = []
11
+ end
12
+
13
+ def to_s
14
+ conf = @tape.instance_variable_get(:@content).dup
15
+ conf.insert(@tape.head_position, "[q#{@state}]").collect!{|e| e ? e : Tape::BLANK }.join
16
+ end
17
+
18
+ def dup
19
+ conf = Configuration.new
20
+ conf.state = @state
21
+ conf.tape = @tape.dup
22
+ conf.depth = @depth
23
+ conf.path = @path.dup
24
+ conf
25
+ end
26
+ end
data/lib/ntm/tape.rb ADDED
@@ -0,0 +1,37 @@
1
+ class Tape
2
+
3
+ BLANK = '#'
4
+
5
+ attr_reader :head_position
6
+
7
+ def initialize(options = {} )
8
+ @head_position = options[:head_position].to_i
9
+ @content = (options[:content].to_s.empty?) ? [BLANK]: options[:content].split("")
10
+ end
11
+
12
+ def write(symbol)
13
+ @content[@head_position] = symbol
14
+ end
15
+
16
+ def read
17
+ @content[@head_position] || BLANK
18
+ end
19
+
20
+ def move_left
21
+ raise StandardError, 'The machine moved off the left-hand of the tape!' if @head_position == 0
22
+ @head_position -= 1
23
+ end
24
+
25
+ def move_right
26
+ @head_position += 1
27
+ end
28
+
29
+ def reset
30
+ @content = [BLANK]
31
+ @head_position = 0
32
+ end
33
+
34
+ def dup
35
+ Tape.new({head_position: @head_position, content: @content.dup.join})
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ class Ntm
2
+ VERSION = "0.1.0"
3
+ end
data/ntm.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ntm/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ntm"
8
+ spec.version = Ntm::VERSION
9
+ spec.authors = ["fade2black"]
10
+ spec.email = ["bkuliyev@gmail.com"]
11
+
12
+
13
+ spec.summary = %q{Create and run a nondeterministic Turing machine.}
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.8"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency 'rspec'
25
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ntm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - fade2black
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-04-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - bkuliyev@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/ntm.rb
73
+ - lib/ntm/configuration.rb
74
+ - lib/ntm/tape.rb
75
+ - lib/ntm/version.rb
76
+ - ntm.gemspec
77
+ homepage: ''
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.2.2
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Create and run a nondeterministic Turing machine.
101
+ test_files: []