Class: Rubylog::Procedure

Inherits:
Predicate show all
Includes:
Enumerable
Defined in:
lib/rubylog/procedure.rb

Instance Attribute Summary

Attributes inherited from Predicate

#arity, #functor

Instance Method Summary collapse

Methods inherited from Predicate

#add_functor_to

Constructor Details

#initialize(functor, arity, rules = Array.new) ⇒ Procedure

Returns a new instance of Procedure.



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

def initialize functor, arity, rules=Array.new
  super functor, arity
  @rules = rules
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



14
15
16
# File 'lib/rubylog/procedure.rb', line 14

def method_missing name, *args, &block
  @rules.send name, *args, &block
end

Instance Method Details

#assert(head, body = :true) ⇒ Object

Asserts a rule with a given head and body.



41
42
43
# File 'lib/rubylog/procedure.rb', line 41

def assert head, body=:true
  push Rubylog::Rule.new(head, body)
end

#call(*args) ⇒ Object

accepts the *args of the called structure



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rubylog/procedure.rb', line 19

def call *args
  # catch cuts
  catch :rubylog_cut do

    # for each rule
    each do |rule|
      # compile
      rule = rule.rubylog_match_variables

      # unify the head with the arguments
      rule.head.args.rubylog_unify(args) do
        # call the body
        rule.body.prove do
          yield 
        end
      end
    end
  end
end

#inspectObject



10
11
12
# File 'lib/rubylog/procedure.rb', line 10

def inspect
  "#{Rubylog::DSL::Indicators.humanize_indicator([functor,arity])}: #{@rules.inspect}"
end