Skip to content

Commit

Permalink
Support false
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbert committed Apr 10, 2020
1 parent a6bd0a5 commit 0a953a3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/ruby-prolog/ruby-prolog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Predicate
attr_reader :id, :name
attr_accessor :db, :clauses

def initialize(db, name)
@id = (@@id_counter += 1)
def initialize(db, name, explicit_id: nil)
@id = explicit_id || (@@id_counter += 1)
@db = db
@name = name
@clauses = []
Expand Down Expand Up @@ -52,6 +52,7 @@ def si(*rhs)
goals = rhs.map do |x|
case x
when TempClause then x.to_goal
when false then Goal.new(0, 'false', [])
else x
end
end
Expand Down Expand Up @@ -259,8 +260,12 @@ class Database
attr_reader :by_name, :by_id

def initialize
@by_name = {}
@by_id = {}
@by_name = {
'false' => Predicate.new(self, 'false', explicit_id: 0)
}
@by_id = {
0 => @by_name['false']
}
@listing_enabled = false
@listing = {}
end
Expand Down
16 changes: 16 additions & 0 deletions test/lib/ruby-prolog/ruby-prolog_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@
_( two.query {_= foo[:X] } ).must_equal [{X: 10}, {X: 30}]
end

it 'supports false' do
db = RubyProlog.new do
foo[:_] << [false]
foo['x'].fact

bar[:_] << [:CUT, false]
bar['x'].fact

baz[false].fact
end

_( db.query{ foo['x'] } ).must_equal [{}]
_( db.query{ bar['x'] } ).must_equal []
_( db.query{ baz[false] } ).must_equal [{}]
end

it 'should be able to query simple family trees.' do

c = RubyProlog.new do
Expand Down

0 comments on commit 0a953a3

Please sign in to comment.