turing_machine 0.0.2

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: 6270dca8cb1652af9e145537e52bb199d0cd7190
4
+ data.tar.gz: 3f57b50ac59a9849fe4d59640d166aeaf8166446
5
+ SHA512:
6
+ metadata.gz: 440063906e7969e16d560a98878e9ee2e3eb85916ffd06cb60cf438d6d4d7b836b6693da4e07d9edc607b509c2445d1104ae5b453d18a36f0c9ff527bbf56536
7
+ data.tar.gz: fd2ee8011de8838ddb7ab259ae9b2ae2ed42d3664a2401202a4441e094cef67e4b5e974da29c651b334822db38cd2e0f043b045f86f86109149809fb72fdca23
data/.coco.yml ADDED
@@ -0,0 +1,2 @@
1
+ :excludes:
2
+ - lib/turing_machine/version.rb
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ TODO
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.2.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in turing_machine.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 lkdjiin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ TuringMachine
2
+ =============
3
+
4
+ > A man provided with paper, pencil, and rubber, and subject to strict discipline, is in effect a universal machine.
5
+ > — Alan Turing
6
+
7
+ ![Photo of Alan Turing](alan-turing2.jpg)
8
+
9
+ Currently, in version 0.0.2, you could just run the hardcoded 3 states busy beaver
10
+ algorithm. There is nothing to tweak.
11
+ I wish to be able to run any instruction sets in a very near future.
12
+
13
+ ## Installation
14
+
15
+ Install it with:
16
+
17
+ $ gem install turing_machine
18
+
19
+ ## Usage
20
+
21
+ $ turing_machine
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/turing_machine/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
30
+
31
+ ## License
32
+
33
+ MIT, see LICENSE.TXT
34
+
35
+ ## Questions and/or Comments
36
+
37
+ Feel free to email [Xavier Nayrac](mailto:xavier.nayrac@gmail.com)
38
+ with any questions, or contact me on [twitter](https://twitter.com/lkdjiin).
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
data/alan-turing2.jpg ADDED
Binary file
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'turing_machine'
4
+
5
+ include TuringMachine
6
+
7
+ instructions = {
8
+ ['0', 'A'] => {write: '1', move: 'R', next_state: 'B'},
9
+ ['1', 'A'] => {write: '1', move: 'L', next_state: 'C'},
10
+ ['0', 'B'] => {write: '1', move: 'L', next_state: 'A'},
11
+ ['1', 'B'] => {write: '1', move: 'R', next_state: 'B'},
12
+ ['0', 'C'] => {write: '1', move: 'L', next_state: 'B'},
13
+ ['1', 'C'] => {write: '1', move: 'R', next_state: 'HALT'},
14
+ }
15
+
16
+ initial_state = 'A'
17
+
18
+ instance = Instance.new(instructions, initial_state)
19
+
20
+ loop do
21
+ puts instance.to_s
22
+ break if instance.halted?
23
+ instance.proceed
24
+ end
@@ -0,0 +1,9 @@
1
+ require "turing_machine/version"
2
+ require 'turing_machine/tape'
3
+ require 'turing_machine/state_register'
4
+ require 'turing_machine/instruction'
5
+ require 'turing_machine/instance'
6
+
7
+ module TuringMachine
8
+ # Your code goes here...
9
+ end
@@ -0,0 +1,57 @@
1
+ module TuringMachine
2
+
3
+ # Public: An instance of a Turing machine.
4
+ class Instance
5
+
6
+ def initialize(instructions, initial_state)
7
+ @instruction = Instruction.new(instructions)
8
+ @state = StateRegister.new(initial_state)
9
+ @tape = Tape.new
10
+ @sequence = 1
11
+ end
12
+
13
+ def to_s
14
+ "#{'%3d' % @sequence} #{@tape} #{@state}#{instr_to_s}\n " +
15
+ ' ' * @tape.index + '^'
16
+ end
17
+
18
+ def proceed
19
+ current = action
20
+ update_sequence
21
+ update_tape(current)
22
+ update_state(current)
23
+ end
24
+
25
+ def halted?
26
+ @state.current == 'HALT'
27
+ end
28
+
29
+ private
30
+
31
+ def update_sequence
32
+ @sequence += 1
33
+ end
34
+
35
+ def update_tape(current_action)
36
+ @tape.head = current_action[:write]
37
+ current_action[:move] == 'L' ? @tape.shift_left : @tape.shift_right
38
+ end
39
+
40
+ def update_state(current_action)
41
+ @state.change(current_action[:next_state])
42
+ end
43
+
44
+ def action
45
+ @instruction.for(@tape.head, @state.current)
46
+ end
47
+
48
+ def instr_to_s
49
+ if halted?
50
+ ''
51
+ else
52
+ " -> " + action[:write] + action[:move] + action[:next_state]
53
+ end
54
+ end
55
+ end
56
+
57
+ end
@@ -0,0 +1,15 @@
1
+ module TuringMachine
2
+
3
+ # Public: The instruction table of a Turing machine.
4
+ class Instruction
5
+
6
+ def initialize(table)
7
+ @table = table
8
+ end
9
+
10
+ def for(symbol, state)
11
+ @table[[symbol, state]]
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ module TuringMachine
2
+
3
+ # Public: The state register of a Turing machine.
4
+ class StateRegister
5
+
6
+ def initialize(state)
7
+ @state = state
8
+ end
9
+
10
+ def current
11
+ @state
12
+ end
13
+
14
+ def change(new_state)
15
+ @state = new_state
16
+ end
17
+
18
+ def to_s
19
+ @state.to_s
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,40 @@
1
+ module TuringMachine
2
+
3
+ # Public: The tape of a Turing machine, combined with the head.
4
+ class Tape
5
+
6
+ def initialize
7
+ # @symbols = [ '0' ]
8
+ # @index = 0
9
+ @symbols = Array.new(10) { '0' }
10
+ @index = 4
11
+ end
12
+
13
+ attr_reader :index
14
+
15
+ # def size
16
+ # @symbols.size
17
+ # end
18
+
19
+ def head
20
+ @symbols[@index]
21
+ end
22
+
23
+ def head=(symbol)
24
+ @symbols[@index] = symbol
25
+ end
26
+
27
+ def shift_left
28
+ @index -= 1
29
+ end
30
+
31
+ def shift_right
32
+ @index += 1
33
+ end
34
+
35
+ def to_s
36
+ @symbols.join
37
+ end
38
+ end
39
+
40
+ end
@@ -0,0 +1,3 @@
1
+ module TuringMachine
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ include TuringMachine
4
+
5
+ instructions = {
6
+ ['0', 'A'] => {write: '1', move: 'R', next_state: 'B'},
7
+ ['1', 'A'] => {write: '1', move: 'L', next_state: 'C'},
8
+ ['0', 'B'] => {write: '1', move: 'L', next_state: 'A'},
9
+ ['1', 'B'] => {write: '1', move: 'R', next_state: 'B'},
10
+ ['0', 'C'] => {write: '1', move: 'L', next_state: 'B'},
11
+ ['1', 'C'] => {write: '1', move: 'R', next_state: 'HALT'},
12
+ }
13
+
14
+ initial_state = 'A'
15
+
16
+ describe Instance do
17
+ it 'produces an output' do
18
+ instance = Instance.new(instructions, initial_state)
19
+ expected = " 1 0000000000 A -> 1RB\n" +
20
+ " ^"
21
+ expect(instance.to_s).to eq expected
22
+ end
23
+
24
+ it 'computes a step' do
25
+ instance = Instance.new(instructions, initial_state)
26
+ instance.proceed
27
+ expected = " 2 0000100000 B -> 1LA\n" +
28
+ " ^"
29
+ expect(instance.to_s).to eq expected
30
+ end
31
+
32
+ context 'when halted' do
33
+
34
+ it 'tells if the machine is halted' do
35
+ instructions = {
36
+ ['0', 'A'] => {write: '1', move: 'R', next_state: 'HALT'}
37
+ }
38
+ instance = Instance.new(instructions, initial_state)
39
+ expect(instance.halted?).to eq false
40
+ instance.proceed
41
+ expect(instance.halted?).to eq true
42
+ end
43
+
44
+ it 'outputs especially' do
45
+ instructions = {
46
+ ['0', 'A'] => {write: '1', move: 'R', next_state: 'HALT'}
47
+ }
48
+ instance = Instance.new(instructions, initial_state)
49
+ instance.proceed
50
+ expected = " 2 0000100000 HALT\n" +
51
+ " ^"
52
+ expect(instance.to_s).to eq expected
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ include TuringMachine
4
+
5
+ describe Instruction do
6
+
7
+ let(:instruction) do
8
+ Instruction.new({
9
+ ['0', 'A'] => {write: '1', move: 'R', next_state: 'B'},
10
+ ['1', 'A'] => {write: '1', move: 'L', next_state: 'C'},
11
+ })
12
+ end
13
+
14
+ it 'finds actions' do
15
+ hash = instruction.for('0', 'A')
16
+ expect(hash[:write]).to eq '1'
17
+ expect(hash[:move]).to eq 'R'
18
+ expect(hash[:next_state]).to eq 'B'
19
+ end
20
+
21
+ end
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'coco'
3
+ require 'turing_machine'
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ include TuringMachine
4
+
5
+ describe StateRegister do
6
+
7
+ let(:state) { StateRegister.new('A') }
8
+
9
+ it 'has a current starting state' do
10
+ expect(state.current).to eq 'A'
11
+ end
12
+
13
+ it 'has a string representation' do
14
+ expect("#{state}").to eq 'A'
15
+ end
16
+
17
+ it 'could change the state' do
18
+ state.change('B')
19
+ expect(state.current).to eq 'B'
20
+ end
21
+
22
+ end
data/spec/tape_spec.rb ADDED
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ include TuringMachine
4
+
5
+ describe Tape do
6
+
7
+ let(:tape) { Tape.new }
8
+
9
+ describe '.new' do
10
+
11
+ # it 'has a single cell' do
12
+ # expect(tape.size).to eq 1
13
+ # end
14
+
15
+ it 'has a initial current symbol of "0"' do
16
+ expect(tape.head).to eq '0'
17
+ end
18
+
19
+ end
20
+
21
+ describe '#head=' do
22
+
23
+ it 'writes to the tape' do
24
+ tape.head = '1'
25
+ expect(tape.head).to eq '1'
26
+ end
27
+
28
+ end
29
+
30
+ describe 'moves' do
31
+
32
+ it 'moves to the cell to the left' do
33
+ tape.head = '1'
34
+ tape.shift_left
35
+ expect(tape.head).to eq '0'
36
+ end
37
+
38
+ it 'moves to the cell to the right' do
39
+ tape.head = '1'
40
+ tape.shift_right
41
+ expect(tape.head).to eq '0'
42
+ end
43
+
44
+ end
45
+
46
+ it 'has a string representation' do
47
+ expect("#{tape}").to eq '0000000000'
48
+ end
49
+
50
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe TuringMachine do
4
+ it 'has a version number' do
5
+ expect(TuringMachine::VERSION).not_to be nil
6
+ end
7
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'turing_machine/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "turing_machine"
8
+ spec.version = TuringMachine::VERSION
9
+ spec.authors = ["Xavier Nayrac"]
10
+ spec.email = ["xavier.nayrac@gmail.com"]
11
+ spec.summary = %q{Turing machine}
12
+ spec.description = %q{Turing machine}
13
+ spec.homepage = "http://lkdjiin.github.com/turing_machine/"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.1"
24
+ spec.add_development_dependency "reek", "~> 1.6"
25
+ spec.add_development_dependency "flay", "~> 2.6"
26
+ spec.add_development_dependency "coco", "~> 0.12"
27
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: turing_machine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Xavier Nayrac
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-01 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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: '3.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: reek
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: flay
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.6'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: coco
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.12'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.12'
97
+ description: Turing machine
98
+ email:
99
+ - xavier.nayrac@gmail.com
100
+ executables:
101
+ - turing_machine
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".coco.yml"
106
+ - ".gitignore"
107
+ - ".rspec"
108
+ - ".travis.yml"
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - alan-turing2.jpg
114
+ - bin/turing_machine
115
+ - lib/turing_machine.rb
116
+ - lib/turing_machine/instance.rb
117
+ - lib/turing_machine/instruction.rb
118
+ - lib/turing_machine/state_register.rb
119
+ - lib/turing_machine/tape.rb
120
+ - lib/turing_machine/version.rb
121
+ - spec/instance_spec.rb
122
+ - spec/instruction_spec.rb
123
+ - spec/spec_helper.rb
124
+ - spec/state_register_spec.rb
125
+ - spec/tape_spec.rb
126
+ - spec/turing_machine_spec.rb
127
+ - turing_machine.gemspec
128
+ homepage: http://lkdjiin.github.com/turing_machine/
129
+ licenses:
130
+ - MIT
131
+ metadata: {}
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubyforge_project:
148
+ rubygems_version: 2.4.5
149
+ signing_key:
150
+ specification_version: 4
151
+ summary: Turing machine
152
+ test_files:
153
+ - spec/instance_spec.rb
154
+ - spec/instruction_spec.rb
155
+ - spec/spec_helper.rb
156
+ - spec/state_register_spec.rb
157
+ - spec/tape_spec.rb
158
+ - spec/turing_machine_spec.rb