Class: Rbprolog::Deduction
- Inherits:
-
Object
- Object
- Rbprolog::Deduction
- Includes:
- Enumerable
- Defined in:
- lib/rbprolog/deduction.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#sym ⇒ Object
Returns the value of attribute sym.
Instance Method Summary collapse
- #each ⇒ Object
- #each_deduce(context, rules, id) ⇒ Object
-
#initialize(logic, sym, *args) ⇒ Deduction
constructor
A new instance of Deduction.
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
#args ⇒ Object
Returns the value of attribute args.
5 6 7 |
# File 'lib/rbprolog/deduction.rb', line 5 def args @args end |
#sym ⇒ Object
Returns the value of attribute sym.
5 6 7 |
# File 'lib/rbprolog/deduction.rb', line 5 def sym @sym end |
Instance Method Details
#each ⇒ Object
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 |