magic_logic 0.0.1

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: 5c4a0aaa6c54d1ee66d0c45910f2d060d7913eb1
4
+ data.tar.gz: 3710e90bb50ebf6272dcceb9ea20b3b684883ee9
5
+ SHA512:
6
+ metadata.gz: e485ebfea4853ac66e84bea93978de5f14d44e48958f737df698ee05fe9bb1699eba0f86ad6b08467096b13cbc7f1dbbc1a6dea421baed38129860021294d00b
7
+ data.tar.gz: a42e2628b438320dda98333e58014b0e8e62e4a0bf87feae7646c9ff7f738aea9972196d016e8f4df45b83dbcee8c5a2e41a1155aab2935c2ca03ac51596738e
data/.gitignore ADDED
@@ -0,0 +1,14 @@
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
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 magic_logic.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 gogotanaka
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,31 @@
1
+ # MagicLogic
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'magic_logic'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install magic_logic
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/magic_logic/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ end
7
+
8
+ task :default => :test
9
+
data/bin/magic_logic ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'magic_logic'
@@ -0,0 +1,3 @@
1
+ module MagicLogic
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,157 @@
1
+ require "magic_logic/version"
2
+
3
+ module MagicLogic
4
+ module Operator
5
+ def ~@
6
+ if is_neg? then p
7
+ elsif is_form? then vars.map(&:~).inject(reope)
8
+ else NEG.new(self)
9
+ end
10
+ end
11
+
12
+ def *(q)
13
+ case q
14
+ when Taut then self
15
+ when UTaut then $utout
16
+ when self then self
17
+ else
18
+ if neg?(q) then $utout
19
+ else FORM.new([self, q], :*)
20
+ end
21
+ end
22
+ end
23
+
24
+ def +(q)
25
+ case q
26
+ when Taut then $tout
27
+ when UTaut then self
28
+ when self then self
29
+ else
30
+ if neg?(q) then $tout
31
+ else FORM.new([self, q], :+)
32
+ end
33
+ end
34
+ end
35
+
36
+ def >=(q)
37
+ (~self + q)
38
+ end
39
+
40
+ def <=>(q)
41
+ (self >= q) * (q >= self)
42
+ end
43
+ end
44
+
45
+ module Utils
46
+ def neg?(p)
47
+ (is_neg? && self.p == p) || (p.is_neg? && p.p == self)
48
+ end
49
+
50
+ def is_neg?
51
+ is_a?(NEG)
52
+ end
53
+
54
+ def is_form?(ope=nil)
55
+ return is_a?(FORM) && @ope == ope if ope
56
+ is_a?(FORM)
57
+ end
58
+
59
+ def is_or?
60
+ is_form?(:+)
61
+ end
62
+
63
+ def is_and?
64
+ is_form?(:*)
65
+ end
66
+
67
+ def include?(p)
68
+ false
69
+ end
70
+
71
+ def dpll!
72
+ !!!!!!!!!!!!self
73
+ end
74
+ end
75
+
76
+ module Base; include Operator; include Utils end
77
+
78
+ # Tautology
79
+ class Taut
80
+ include Base
81
+ def ~@; $utout end
82
+ def +(q); $tout end
83
+ def *(q); q end
84
+ def !@; $tout end
85
+ def to_s; 'TRUE' end
86
+ end
87
+ $tout = Taut.new
88
+
89
+ # Non Tautology
90
+ class UTaut
91
+ include Base
92
+ def ~@; $tout end
93
+ def +(q); q end
94
+ def *(q); $utout end
95
+ def !@; $utout end
96
+ def to_s; 'FALSE' end
97
+ end
98
+ $utout = UTaut.new
99
+
100
+ class Atom < Struct.new(:p)
101
+ include Base
102
+ def to_s; p.to_s end
103
+ def !@; self end
104
+ def depth; 1 end
105
+ end
106
+
107
+ class NEG < Struct.new(:p)
108
+ include Base
109
+ def to_s; "~#{p}" end
110
+ def !@; ~(!p) end
111
+ def depth; p.depth+1 end
112
+ end
113
+
114
+ class FORM
115
+ include Base
116
+ attr_reader :vars, :ope
117
+ def initialize(vars, ope)
118
+ @vars = vars.map { |var| var.is_form?(ope) ? var.vars : var }.flatten
119
+ @ope = ope
120
+ end
121
+
122
+ def to_s; "(#{vars.map(&:to_s).join(loope)})" end
123
+
124
+ def !@
125
+ if is_or? && (and_form = vars.find { |var| var.is_and? })
126
+ and_form.vars.map { |a| a + FORM.new((vars - [and_form]), :+) }.inject(:*)
127
+ elsif are_there_neg?
128
+ is_or? ? $tout : $utout
129
+ else
130
+ vars.map(&:!).inject(ope)
131
+ end
132
+ end
133
+
134
+ def depth; [p.depth, q.depth].max+1; end
135
+
136
+ def include?(p)
137
+ vars.include?(p)
138
+ end
139
+
140
+ def loope
141
+ ope == :* ? '&' : '|'
142
+ end
143
+
144
+ def reope
145
+ is_and? ? :+ : :*
146
+ end
147
+
148
+ private
149
+ def are_there_neg?
150
+ pvars = vars.reject { |var| var.is_neg? }
151
+ nvars = vars.select { |var| var.is_neg? }
152
+ pvars.any? { |pvar|
153
+ nvars.any? { |nvar| nvar.neg?(pvar) }
154
+ }
155
+ end
156
+ end
157
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'magic_logic/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "magic_logic"
8
+ spec.version = MagicLogic::VERSION
9
+ spec.authors = ["gogotanaka"]
10
+ spec.email = ["mail@tanakakazuki.com"]
11
+ spec.summary = %q{ Magic logic }
12
+ spec.description = %q{ Magic logic }
13
+ spec.homepage = ""
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 "minitest"
24
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'magic_logic'
3
+
4
+ require 'minitest/autorun'
@@ -0,0 +1,72 @@
1
+ require 'minitest_helper'
2
+ include MagicLogic
3
+ class TestMagicLogic < MiniTest::Unit::TestCase
4
+ $p = Atom.new(:P)
5
+ $q = Atom.new(:Q)
6
+ $r = Atom.new(:R)
7
+
8
+ def setup
9
+
10
+ end
11
+ def assert_to_s(exp, obj)
12
+ assert_equal(exp, ((!!!!!!!obj).to_s))
13
+ end
14
+
15
+ def test_utils
16
+ assert_equal(true, $p.neg?(~$p))
17
+ assert_equal(true, (~$p).neg?($p))
18
+ assert_equal(false, ($p).neg?($p))
19
+ assert_equal(false, ($p).neg?($p))
20
+ assert_equal(true, ($p + $q).include?($p))
21
+ assert_equal(true, ($p + $q).include?($q))
22
+ end
23
+
24
+ def test_0_1
25
+ assert_to_s("TRUE", $p + $tout)
26
+ assert_to_s("TRUE", $tout + $p)
27
+ assert_to_s("P", $p + $utout)
28
+ assert_to_s("P", $utout + $p)
29
+ assert_to_s("TRUE", $p + ~$p)
30
+ assert_to_s("TRUE", ~$p + $p)
31
+
32
+ assert_to_s("P", $p * $tout)
33
+ assert_to_s("P", $tout * $p)
34
+ assert_to_s("FALSE", $p * $utout)
35
+ assert_to_s("FALSE", $utout * $p)
36
+ assert_to_s("FALSE", $p * ~$p)
37
+ assert_to_s("FALSE", ~$p * $p)
38
+ end
39
+
40
+ def test_basis
41
+ assert_to_s("P", $p)
42
+ assert_to_s("(P|Q)", $p + $q)
43
+ assert_to_s("(P&Q)", $p * $q)
44
+ assert_to_s("~P", ~$p)
45
+ assert_to_s("(~P|Q)", $p >= $q)
46
+ assert_to_s("((~P|Q)&(~Q|P))", $p <=> $q)
47
+ end
48
+
49
+ def test_main
50
+ assert_to_s("(~P&~Q)", ~($p + $q))
51
+ assert_to_s("(~P|~Q)", ~($p * $q))
52
+ assert_to_s("P", ~(~$p))
53
+ assert_to_s("((Q|P)&(R|P))", $p + ($q * $r))
54
+ assert_to_s("(P&Q&R)", $p * ($q * $r))
55
+ assert_to_s("(P&(~P|Q))", $p * ($p >= $q))
56
+ assert_to_s("P", (~$p >= $p))
57
+ end
58
+
59
+ def test_tautology
60
+ assert_to_s("TRUE", ~(~$p) >= $p)
61
+ assert_to_s("TRUE", ($p * ($p >= $q)) >= $q)
62
+ assert_to_s("TRUE", (($p >= $q) * ($q >= $r)) >= ($p >= $r))
63
+ assert_to_s("TRUE", (~$p * ($p + $q)) >= ($q))
64
+ assert_to_s("TRUE", (($p >= $q) * ($q >= $r) * $p) >= ($r))
65
+ assert_to_s("TRUE", ($p * ~$p) >= $r)
66
+ end
67
+
68
+ def test_no_tautology
69
+ assert_to_s("FALSE", $p * $q * ~$p)
70
+ assert_to_s("FALSE", ~$p * (~$p >= $p))
71
+ end
72
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: magic_logic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - gogotanaka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-22 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: minitest
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: " Magic logic "
56
+ email:
57
+ - mail@tanakakazuki.com
58
+ executables:
59
+ - magic_logic
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/magic_logic
70
+ - lib/magic_logic.rb
71
+ - lib/magic_logic/version.rb
72
+ - magic_logic.gemspec
73
+ - test/minitest_helper.rb
74
+ - test/test_magic_logic.rb
75
+ homepage: ''
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.4.5
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Magic logic
99
+ test_files:
100
+ - test/minitest_helper.rb
101
+ - test/test_magic_logic.rb