Class: Rubylog::Predicate

Inherits:
Object show all
Defined in:
lib/rubylog/predicate.rb

Direct Known Subclasses

Primitive, Procedure

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(functor, arity) ⇒ Predicate

Returns a new instance of Predicate.



5
6
7
8
# File 'lib/rubylog/predicate.rb', line 5

def initialize functor, arity
  @functor = functor
  @arity = arity
end

Instance Attribute Details

#arityObject (readonly)

Returns the value of attribute arity.



4
5
6
# File 'lib/rubylog/predicate.rb', line 4

def arity
  @arity
end

#functorObject (readonly)

Returns the value of attribute functor.



4
5
6
# File 'lib/rubylog/predicate.rb', line 4

def functor
  @functor
end

Instance Method Details

#add_functor_to(subjects) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rubylog/predicate.rb', line 15

def add_functor_to subjects
  if arity == 0
    # We treat nullary predicates differently. Do not even create functors.
    Rubylog::NullaryPredicates[functor] = self
    return
  end

  predicate = self

  [subjects].flatten.each do |subject|
    raise ArgumentError, "#{subject.inspect} is not a class or module" unless subject.is_a? Module
    subject.class_eval do
      f = predicate.functor
      define_method f do |*args, &block|
      args << block if block
      Rubylog::Structure.new predicate, f, self, *args 
      end

      f_bang = :"#{f}!"
      define_method f_bang do |*args, &block|
      args << block if block
      predicate.assert Rubylog::Structure.new(predicate, f, self, *args), :true
      self
      end

      f_qmark = :"#{f}?"
      define_method f_qmark do |*args, &block|
      args << block if block
      Rubylog::Structure.new(predicate, f, self, *args).true?
      end
    end
  end

end

#call(*args) ⇒ Object

Yields for each solution of the predicate



11
12
13
# File 'lib/rubylog/predicate.rb', line 11

def call *args
  raise "abstract method called on #{self.inspect}"
end