Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/predicate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def constants
def &(other)
return self if other.tautology? or other==self
return other if tautology?

Predicate.new(expr & other.expr)
end

Expand Down
2 changes: 2 additions & 0 deletions lib/predicate/nodes/and.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ def operator_symbol
end

def &(other)
return contradiction if in_contradiction?(self, other)

case other
when Tautology then self
when Contradiction then other
Expand Down
5 changes: 1 addition & 4 deletions lib/predicate/nodes/eq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ def operator_symbol
end

def &(other)
return super unless free_variables == other.free_variables
case other
when Eq
return self if constants == other.constants
return contradiction
when In
return super unless free_variables == other.free_variables
return super unless var_against_literal_value? && other.var_against_literal_value?
mine, hers = self.right.value, other.right.value
return self if hers.include?(mine)
Expand Down
8 changes: 8 additions & 0 deletions lib/predicate/nodes/expr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def identifier?
sexpr_type == :identifier
end

def in_contradiction?(me, other)
mine = me.constants
yours = other.constants
(mine.keys & yours.keys).any?{|k| mine[k] != yours[k] }
end

def !
sexpr([:not, self])
end
Expand All @@ -41,6 +47,8 @@ def &(other)
return other if other.contradiction?
return self if other.tautology?
return other & self if other.dyadic_priority > self.dyadic_priority
return contradiction if in_contradiction?(self, other)

sexpr([:and, self, other])
end

Expand Down
12 changes: 11 additions & 1 deletion spec/nodes/and/test_and.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,15 @@ class Predicate
end
end

context 'with an eq yielding a contradiction' do
let(:right) {
Factory.eq(:x, 5)
}

it 'returns a contradiction' do
expect(subject).to be_a(Contradiction)
end
end

end
end
end
6 changes: 6 additions & 0 deletions spec/nodes/eq/test_and.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class Predicate
it{ should be_a(Contradiction) }
end

context 'with an and leading to a contradiction' do
let(:right){ Factory.eq(:y, 4) & Factory.eq(:x, 3) }

it{ should be_a(Contradiction) }
end

context 'with an IN on same variable and literal' do
let(:right){ Factory.in(:x, [2,4]) }

Expand Down