Class: Rbprolog::Context

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bindsObject

Returns the value of attribute binds.



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

def binds
  @binds
end

Instance Method Details

#deduce(v) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rbprolog/context.rb', line 19

def deduce(v)
  if Var === v
    unless @binds[v.sym]
      @stacks.last << v.sym
      @binds[v.sym] = Var.new(v.sym)
    end

    @binds[v.sym]
  else
    v
  end
end

#match!(v1, v2) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/rbprolog/context.rb', line 10

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) ⇒ Boolean

Returns:

  • (Boolean)


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

def match?(v1, v2)
  v1 = deduce(v1)
  Var === v1 || Var === v2 || v1 == v2
end

#scope(predicate, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rbprolog/context.rb', line 32

def scope(predicate, &block)
  @stacks ||= []
  @stacks.push([])

  @binds ||= {}

  mirror = @binds.clone

  result = yield

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

  result
end