Class: Rbprolog::Deduction

Inherits:
Object
  • Object
show all
Defined in:
lib/rbprolog/deduction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sym, *args) ⇒ Deduction

Returns a new instance of Deduction.



5
6
7
8
# File 'lib/rbprolog/deduction.rb', line 5

def initialize(sym, *args)
  @sym = sym
  @args = args
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



3
4
5
# File 'lib/rbprolog/deduction.rb', line 3

def args
  @args
end

#symObject

Returns the value of attribute sym.



3
4
5
# File 'lib/rbprolog/deduction.rb', line 3

def sym
  @sym
end

Instance Method Details

#each_deduce(context, rules, id) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rbprolog/deduction.rb', line 10

def each_deduce(context, rules, id)
  print "#{"\t" * id.size}#{id.join('.')} #{@sym}?(#{@args.join(', ')})"

  rules.select {|rule| rule.sym == @sym}.each_with_index do |rule, rule_index|
    context.scope(self) do |scoped_args|
      puts " => #{@sym}?(#{scoped_args.join(', ')})" if rule_index == 0

      # rules.reject is to avoid endless loop caused by self reference

      rule.each_match(rules.reject {|item| item == rule}, *scoped_args, id + [rule_index]) do |hash|
        context.scope(self) do
          rule.args.each_with_index do |rule_arg, rule_arg_index|
            if Var === scoped_args[rule_arg_index]
              context[scoped_args[rule_arg_index].sym] = Var === rule_arg ? hash[rule_arg.sym] : rule_arg
            end
          end

          yield context.binds
        end
      end
    end
  end
end