Class: Rbprolog::Deduction

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logic, sym, *args) ⇒ Deduction

Returns a new instance of Deduction.



7
8
9
10
11
12
# File 'lib/rbprolog/deduction.rb', line 7

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

  @args = args
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



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

def args
  @args
end

#symObject

Returns the value of attribute sym.



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

def sym
  @sym
end

Instance Method Details

#eachObject



14
15
16
17
18
# File 'lib/rbprolog/deduction.rb', line 14

def each
  each_deduce(Context.new, @logic.rules, []) do |hash|
    yield hash
  end
end

#each_deduce(context, rules, id) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rbprolog/deduction.rb', line 20

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

  rules.select {|rule| rule.sym == @sym}.each_with_index do |rule, i|
    context.scope(self) do
      puts " => #{@sym}?(#{@args.map {|arg| context.deduce(arg).to_s}.join(', ')})" if i == 0

      rule.each_deduce(rules, *@args.map {|arg| context.deduce(arg)}, id + [i]) do |hash|
        context.scope(self) do
          rule.args.each_with_index do |rule_arg, rule_arg_index|
            deduced_arg = context.deduce(@args[rule_arg_index])
            if Var === deduced_arg
              context.binds[deduced_arg.sym] = Var === rule_arg ? hash[rule_arg.sym] : rule_arg
            end
          end

          yield context.binds
        end
      end
    end
  end
end