magic_logic 0.0.2 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ef7642c257a20a87f44b3a7529b59afaa01b8745
4
- data.tar.gz: 79a4e821e55b3d055bf8594771f7e77075e62022
3
+ metadata.gz: ec10ec67c1bca3e1347d588aad80784b6ac80930
4
+ data.tar.gz: 93ad2976ad68b31cbe5a08e0446db3f9f76e4b76
5
5
  SHA512:
6
- metadata.gz: a3545a1a98e76f514250e392d76617e808baeaf0630f7c3598e3d61a0171295a0f904ec88ac7b4806a1002cfae62c065d9cf8354afd937da0a1c3502011f4262
7
- data.tar.gz: 9a51cea1d6ad64ac48f43da2568a91784d69a176a44a2cac911257a80d7bb55084a0a097ca9ea198036f992b38e00c0982a9cab84f2bba7f8f9b38e106ec326e
6
+ metadata.gz: 5a55cb249a433d6f2825776d6893e9848e8dbbdd4f7e478d324176a15d1b3f9513289682559ccfce33164cafb7efd1646352ce286c8396e254020a75b41899a6
7
+ data.tar.gz: 3272d9efaee3521c56fe2565e35566c7c985bd53d057bcc10e125a39f3d0c31a38c983bb6447748aa68c8d9ea97053a9a360edf383aa392224bfd53b5a01e020
@@ -1,78 +1,7 @@
1
- require "magic_logic/version"
1
+ require "magic_logic/utils"
2
+ require "magic_logic/operator"
2
3
 
3
4
  module MagicLogic
4
- module Operator
5
- def _ ope, l, r
6
- ope == :+ ? l : r
7
- end
8
-
9
- [:+, :*].each do |ope|
10
- define_method(ope) do |q|
11
- case q
12
- when Taut then _ ope, $tout, self
13
- when UTaut then _ ope, self, $utout
14
- when self then self
15
- else
16
- if neg?(q)
17
- (_ ope, $tout, $utout)
18
- elsif is_form?(ope.to_sym) && include?(q)
19
- self
20
- elsif q.is_form?(ope) && q.include?(self)
21
- q
22
- else
23
- FORM.new([self, q], ope)
24
- end
25
- end
26
- end
27
- end
28
-
29
- def ~@
30
- if is_neg? then p
31
- elsif is_form? then vars.map(&:~).inject(_ ope, :*, :+)
32
- else NEG.new(self)
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) && self.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
5
  module Base; include Operator; include Utils end
77
6
 
78
7
  # Tautology
@@ -81,7 +10,6 @@ module MagicLogic
81
10
  def ~@; $utout end
82
11
  def +(q); $tout end
83
12
  def *(q); q end
84
- def !@; $tout end
85
13
  def to_s; 'TRUE' end
86
14
  end
87
15
  $tout = Taut.new
@@ -92,7 +20,6 @@ module MagicLogic
92
20
  def ~@; $tout end
93
21
  def +(q); q end
94
22
  def *(q); $utout end
95
- def !@; $utout end
96
23
  def to_s; 'FALSE' end
97
24
  end
98
25
  $utout = UTaut.new
@@ -100,20 +27,19 @@ module MagicLogic
100
27
  class Atom < Struct.new(:p)
101
28
  include Base
102
29
  def to_s; p.to_s end
103
- def !@; self end
104
- def depth; 1 end
105
30
 
106
31
  class << self
107
- alias [] new
32
+ def [](x)
33
+ new(x).tap { |p| $atoms << p; $atoms.uniq! }
34
+ end
108
35
  end
109
36
  end
110
37
  P = Atom
38
+ $atoms = []
111
39
 
112
40
  class NEG < Struct.new(:p)
113
41
  include Base
114
42
  def to_s; "~#{p}" end
115
- def !@; ~(!p) end
116
- def depth; p.depth+1 end
117
43
  end
118
44
 
119
45
  class FORM < Struct.new(:vars, :ope)
@@ -123,37 +49,19 @@ module MagicLogic
123
49
  self.ope = ope
124
50
  end
125
51
 
126
- def to_s; "(#{vars.map(&:to_s).join(_ ope, '|', '&')})" end
127
-
128
- def !@
129
- if is_or? && (and_form = vars.find { |var| var.is_and? })
130
- and_form.vars.map { |a| a + FORM.new((vars - [and_form]), :+) }.inject(:*)
131
- elsif are_there_neg?
132
- is_or? ? $tout : $utout
133
- else
134
- vars.map(&:!).inject(ope)
135
- end
136
- end
137
-
138
- def depth; [p.depth, q.depth].max+1; end
139
-
140
- def include?(p)
141
- vars.include?(p)
142
- end
143
-
144
- private
145
- def are_there_neg?
146
- pvars = vars.reject { |var| var.is_neg? }
147
- nvars = vars.select { |var| var.is_neg? }
148
- pvars.any? { |pvar|
149
- nvars.any? { |nvar| nvar.neg?(pvar) }
150
- }
151
- end
52
+ def to_s; "(#{vars.map(&:to_s).join(_ ope, '|', '&')})" end
53
+ def include?(p); vars.include?(p) end
152
54
  end
153
55
 
154
56
  class ::Array
155
57
  def >>(con)
156
- inject($tout) { |s, p| s * p } >= con
58
+ l = inject($tout) { |s, p| s * p } >= con
59
+ case l.dpll
60
+ when Taut then 'TRUE'
61
+ when UTaut then 'FALSE'
62
+ else 'UNDECIDABLE'
63
+ end
157
64
  end
158
65
  end
159
66
  end
67
+ include MagicLogic
@@ -0,0 +1,42 @@
1
+ module MagicLogic
2
+ module Operator
3
+ def _ ope, l, r
4
+ ope == :+ ? l : r
5
+ end
6
+
7
+ [:+, :*].each do |ope|
8
+ define_method(ope) do |q|
9
+ case q
10
+ when Taut then _ ope, $tout, self
11
+ when UTaut then _ ope, self, $utout
12
+ when self then self
13
+ else
14
+ if neg?(q)
15
+ (_ ope, $tout, $utout)
16
+ elsif is_form?(ope.to_sym) && include?(q)
17
+ self
18
+ elsif q.is_form?(ope) && q.include?(self)
19
+ q
20
+ else
21
+ FORM.new([self, q], ope)
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ def ~@
28
+ if is_neg? then p
29
+ elsif is_form? then vars.map(&:~).inject(_ ope, :*, :+)
30
+ else NEG.new(self)
31
+ end
32
+ end
33
+
34
+ def >=(q)
35
+ (~self + q)
36
+ end
37
+
38
+ def <=>(q)
39
+ (self >= q) * (q >= self)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,66 @@
1
+ module MagicLogic
2
+ module Utils
3
+ ATOM_PREFIX = "__ATOM__PREFIX__"
4
+
5
+ def neg?(p)
6
+ (is_neg? && self.p == p) || (p.is_neg? && p.p == self)
7
+ end
8
+
9
+ def is_neg?
10
+ is_a?(NEG)
11
+ end
12
+
13
+ def is_form?(ope=nil)
14
+ return is_a?(FORM) && self.ope == ope if ope
15
+ is_a?(FORM)
16
+ end
17
+
18
+ def is_or?
19
+ is_form?(:+)
20
+ end
21
+
22
+ def is_and?
23
+ is_form?(:*)
24
+ end
25
+
26
+ def include?(p)
27
+ false
28
+ end
29
+
30
+ def evl
31
+ case self
32
+ when Taut then "(true)"
33
+ when UTaut then "(false)"
34
+ when Atom then "(#{ATOM_PREFIX}#{$atoms.index(self)})"
35
+ when FORM then "(#{vars.map(&:evl).join(_ ope, '||', '&&')})"
36
+ when NEG then "(!#{p.evl})"
37
+ end
38
+ end
39
+
40
+ class ::String
41
+ def sbst!(num, bool)
42
+ gsub!(/#{ATOM_PREFIX}#{num}/, bool.to_s)
43
+ end
44
+ end
45
+
46
+ def dpll
47
+ case self
48
+ when *[Taut, UTaut, Atom]
49
+ self
50
+ else
51
+ #TODO: refactor
52
+ count = $atoms.count
53
+ rslt = (1 .. 2 ** count).map do |i|
54
+ s = evl
55
+ count.times { |j| s.sbst!(j, (i >> j) & 1 == 1) }
56
+ eval(s)
57
+ end
58
+ case rslt.uniq
59
+ when [true] then $tout
60
+ when [false] then $utout
61
+ else self
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -1,3 +1,3 @@
1
1
  module MagicLogic
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -13,65 +13,66 @@ class TestMagicLogic < MiniTest::Unit::TestCase
13
13
  end
14
14
 
15
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))
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
22
  end
23
23
 
24
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 )
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
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 )
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
38
  end
39
39
 
40
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 )
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
47
  end
48
48
 
49
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) )
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 "(P|(Q&R))" , $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
57
  end
58
58
 
59
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 )
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
66
  end
67
67
 
68
68
  def test_no_tautology
69
- assert_to_s("FALSE", $p * $q * ~$p )
70
- assert_to_s("FALSE", ~$p * (~$p >= $p) )
69
+ assert_to_s "FALSE" , $p * $q * ~$p
70
+ assert_to_s "FALSE" , ~$p * (~$p >= $p)
71
+ end
72
+
73
+ def test_prolog
74
+ assert_equal "TRUE" , [$p] >> $p
75
+ assert_equal "TRUE" , [$p >= $q, $q >= $r] >> ($p >= $r)
76
+ assert_equal "UNDECIDABLE" , [$p] >> $q
71
77
  end
72
- #
73
- # def test_hoo
74
- # assert_to_s("TRUE", [$p] >> $p)
75
- # assert_to_s("TRUE", [$p >= $q, $q >= $r] >> $p >= $r)
76
- # end
77
78
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magic_logic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - gogotanaka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-23 00:00:00.000000000 Z
11
+ date: 2015-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -68,6 +68,8 @@ files:
68
68
  - Rakefile
69
69
  - bin/magic_logic
70
70
  - lib/magic_logic.rb
71
+ - lib/magic_logic/operator.rb
72
+ - lib/magic_logic/utils.rb
71
73
  - lib/magic_logic/version.rb
72
74
  - magic_logic.gemspec
73
75
  - test/minitest_helper.rb
@@ -92,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
94
  version: '0'
93
95
  requirements: []
94
96
  rubyforge_project:
95
- rubygems_version: 2.2.2
97
+ rubygems_version: 2.4.5
96
98
  signing_key:
97
99
  specification_version: 4
98
100
  summary: Magic logic