class Rbprolog::Context

Attributes

binds[RW]

Public Class Methods

new() click to toggle source
# File lib/rbprolog/context.rb, line 5
def initialize
  @scopes = []
  @binds = {}
end

Public Instance Methods

[](sym) click to toggle source
# File lib/rbprolog/context.rb, line 24
def [](sym)
  @binds[sym]
end
[]=(sym, value) click to toggle source
# File lib/rbprolog/context.rb, line 28
def []=(sym, value)
  @binds[sym] = value
end
deduce(v) click to toggle source
# File lib/rbprolog/context.rb, line 32
def deduce(v)
  if Var === v
    unless @binds[v.sym]
      @scopes.last << v.sym
      @binds[v.sym] = Var.new(v.sym)
    end

    @binds[v.sym]
  else
    v
  end
end
match!(v1, v2) click to toggle source
# File lib/rbprolog/context.rb, line 15
def match!(v1, v2)
  if match?(v1, v2)
    @binds[v1.sym] = v2 if Var === v1 && !(Var === v2)
    true
  else
    false
  end
end
match?(v1, v2) click to toggle source
# File lib/rbprolog/context.rb, line 10
def match?(v1, v2)
  v1 = deduce(v1)
  Var === v1 || Var === v2 || v1 == v2
end
scope(predicate) { |args.map {|arg| deduce}| ... } click to toggle source
# File lib/rbprolog/context.rb, line 45
def scope(predicate, &block)
  @scopes.push([])

  mirror = @binds.clone

  result = yield predicate.args.map {|arg| self.deduce(arg)}

  @scopes.pop.each {|bind| @binds.delete bind}
  @binds.merge!(mirror)

  result
end