magic_logic 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5c4a0aaa6c54d1ee66d0c45910f2d060d7913eb1
4
- data.tar.gz: 3710e90bb50ebf6272dcceb9ea20b3b684883ee9
3
+ metadata.gz: ef7642c257a20a87f44b3a7529b59afaa01b8745
4
+ data.tar.gz: 79a4e821e55b3d055bf8594771f7e77075e62022
5
5
  SHA512:
6
- metadata.gz: e485ebfea4853ac66e84bea93978de5f14d44e48958f737df698ee05fe9bb1699eba0f86ad6b08467096b13cbc7f1dbbc1a6dea421baed38129860021294d00b
7
- data.tar.gz: a42e2628b438320dda98333e58014b0e8e62e4a0bf87feae7646c9ff7f738aea9972196d016e8f4df45b83dbcee8c5a2e41a1155aab2935c2ca03ac51596738e
6
+ metadata.gz: a3545a1a98e76f514250e392d76617e808baeaf0630f7c3598e3d61a0171295a0f904ec88ac7b4806a1002cfae62c065d9cf8354afd937da0a1c3502011f4262
7
+ data.tar.gz: 9a51cea1d6ad64ac48f43da2568a91784d69a176a44a2cac911257a80d7bb55084a0a097ca9ea198036f992b38e00c0982a9cab84f2bba7f8f9b38e106ec326e
data/.travis.yml CHANGED
@@ -1,3 +1,11 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2.0
3
+ - 2.2
4
+ - 2.1
5
+ - 2.0.0
6
+ - 1.9.3
7
+ - jruby
8
+ - rbx-2
9
+
10
+ notifications:
11
+ email: false
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in magic_logic.gemspec
3
+ gem 'pry'
4
4
  gemspec
data/README.md CHANGED
@@ -1,6 +1,34 @@
1
1
  # MagicLogic
2
2
 
3
- TODO: Write a gem description
3
+ [![Gem Version](https://badge.fury.io/rb/magic_logic.svg)](http://badge.fury.io/rb/magic_logic) [![Build Status](https://travis-ci.org/gogotanaka/MagicLogic.svg?branch=master)](https://travis-ci.org/gogotanaka/MagicLogic)
4
+
5
+
6
+
7
+ ```rb
8
+ p = P["It's sunny."]
9
+ q = P["It's raining."]
10
+ r = P["It's cloudy."]
11
+
12
+ ~(~p) >= p
13
+ # =>TRUE
14
+
15
+ (p * (p >= q)) >= q
16
+ # =>TRUE
17
+
18
+ ((p >= q) * (q >= r)) >= (p >= r)
19
+ # =>TRUE
20
+
21
+
22
+ (~p * (p + q)) >= (q)
23
+ # =>TRUE
24
+
25
+
26
+ ((p >= q) * (q >= r) * p) >= (r)
27
+ # =>TRUE
28
+
29
+ (p * ~p) >= r
30
+ # =>TRUE
31
+ ```
4
32
 
5
33
  ## Installation
6
34
 
@@ -1,3 +1,3 @@
1
1
  module MagicLogic
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/magic_logic.rb CHANGED
@@ -2,34 +2,34 @@ require "magic_logic/version"
2
2
 
3
3
  module MagicLogic
4
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], :*)
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
20
25
  end
21
26
  end
22
27
  end
23
28
 
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
29
+ def ~@
30
+ if is_neg? then p
31
+ elsif is_form? then vars.map(&:~).inject(_ ope, :*, :+)
32
+ else NEG.new(self)
33
33
  end
34
34
  end
35
35
 
@@ -52,7 +52,7 @@ module MagicLogic
52
52
  end
53
53
 
54
54
  def is_form?(ope=nil)
55
- return is_a?(FORM) && @ope == ope if ope
55
+ return is_a?(FORM) && self.ope == ope if ope
56
56
  is_a?(FORM)
57
57
  end
58
58
 
@@ -68,8 +68,8 @@ module MagicLogic
68
68
  false
69
69
  end
70
70
 
71
- def dpll!
72
- !!!!!!!!!!!!self
71
+ def dpll
72
+ !!!!!!!!!!!!!!!!!!self
73
73
  end
74
74
  end
75
75
 
@@ -99,27 +99,31 @@ module MagicLogic
99
99
 
100
100
  class Atom < Struct.new(:p)
101
101
  include Base
102
- def to_s; p.to_s end
103
- def !@; self end
104
- def depth; 1 end
102
+ def to_s; p.to_s end
103
+ def !@; self end
104
+ def depth; 1 end
105
+
106
+ class << self
107
+ alias [] new
108
+ end
105
109
  end
110
+ P = Atom
106
111
 
107
112
  class NEG < Struct.new(:p)
108
113
  include Base
109
- def to_s; "~#{p}" end
110
- def !@; ~(!p) end
111
- def depth; p.depth+1 end
114
+ def to_s; "~#{p}" end
115
+ def !@; ~(!p) end
116
+ def depth; p.depth+1 end
112
117
  end
113
118
 
114
- class FORM
119
+ class FORM < Struct.new(:vars, :ope)
115
120
  include Base
116
- attr_reader :vars, :ope
117
121
  def initialize(vars, ope)
118
- @vars = vars.map { |var| var.is_form?(ope) ? var.vars : var }.flatten
119
- @ope = ope
122
+ self.vars = vars.map { |var| var.is_form?(ope) ? var.vars : var }.flatten
123
+ self.ope = ope
120
124
  end
121
125
 
122
- def to_s; "(#{vars.map(&:to_s).join(loope)})" end
126
+ def to_s; "(#{vars.map(&:to_s).join(_ ope, '|', '&')})" end
123
127
 
124
128
  def !@
125
129
  if is_or? && (and_form = vars.find { |var| var.is_and? })
@@ -137,14 +141,6 @@ module MagicLogic
137
141
  vars.include?(p)
138
142
  end
139
143
 
140
- def loope
141
- ope == :* ? '&' : '|'
142
- end
143
-
144
- def reope
145
- is_and? ? :+ : :*
146
- end
147
-
148
144
  private
149
145
  def are_there_neg?
150
146
  pvars = vars.reject { |var| var.is_neg? }
@@ -154,4 +150,10 @@ module MagicLogic
154
150
  }
155
151
  end
156
152
  end
153
+
154
+ class ::Array
155
+ def >>(con)
156
+ inject($tout) { |s, p| s * p } >= con
157
+ end
158
+ end
157
159
  end
@@ -1,4 +1,8 @@
1
1
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
2
  require 'magic_logic'
3
-
3
+ require 'pry'
4
4
  require 'minitest/autorun'
5
+
6
+ def assert_to_s(exp, obj)
7
+ assert_equal(exp, obj.dpll.to_s)
8
+ end
@@ -1,72 +1,77 @@
1
1
  require 'minitest_helper'
2
2
  include MagicLogic
3
3
  class TestMagicLogic < MiniTest::Unit::TestCase
4
- $p = Atom.new(:P)
5
- $q = Atom.new(:Q)
6
- $r = Atom.new(:R)
4
+ $p = P['P']
5
+ $q = P['Q']
6
+ $r = P['R']
7
7
 
8
8
  def setup
9
9
 
10
10
  end
11
11
  def assert_to_s(exp, obj)
12
- assert_equal(exp, ((!!!!!!!obj).to_s))
12
+ assert_equal(exp, obj.dpll.to_s)
13
13
  end
14
14
 
15
15
  def test_utils
16
- assert_equal(true, $p.neg?(~$p))
17
- assert_equal(true, (~$p).neg?($p))
16
+ assert_equal(true, $p.neg?(~$p))
17
+ assert_equal(true, (~$p).neg?($p))
18
18
  assert_equal(false, ($p).neg?($p))
19
19
  assert_equal(false, ($p).neg?($p))
20
- assert_equal(true, ($p + $q).include?($p))
21
- assert_equal(true, ($p + $q).include?($q))
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("((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
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
71
  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
72
77
  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.1
4
+ version: 0.0.2
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-22 00:00:00.000000000 Z
11
+ date: 2015-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -92,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
92
  version: '0'
93
93
  requirements: []
94
94
  rubyforge_project:
95
- rubygems_version: 2.4.5
95
+ rubygems_version: 2.2.2
96
96
  signing_key:
97
97
  specification_version: 4
98
98
  summary: Magic logic