Class: RubyProlog::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-prolog/ruby-prolog.rb

Instance Method Summary collapse

Constructor Details

#initializeEnvironment

Returns a new instance of Environment.



94
95
96
# File 'lib/ruby-prolog/ruby-prolog.rb', line 94

def initialize
  @table = {}
end

Instance Method Details

#[](t) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/ruby-prolog/ruby-prolog.rb', line 124

def [](t)
  t, env = dereference(t)
  return case t
         when Goal then Goal.new(t.pred, env[t.args])
         when Cons then Cons.new(env[t[0]], env[t[1]])
         when Array then t.collect {|e| env[e]}
         else t
         end
end

#clearObject



110
111
112
# File 'lib/ruby-prolog/ruby-prolog.rb', line 110

def clear
  @table.clear
end

#delete(x) ⇒ Object



106
107
108
# File 'lib/ruby-prolog/ruby-prolog.rb', line 106

def delete(x)
  @table.delete(x) {|k| raise "#{k} not found in #{inspect}"}
end

#dereference(t) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/ruby-prolog/ruby-prolog.rb', line 114

def dereference(t)
  env = self
  while Symbol === t
    p = env.get(t)
    break if p.nil?
    t, env = p
  end
  return [t, env]
end

#get(x) ⇒ Object



102
103
104
# File 'lib/ruby-prolog/ruby-prolog.rb', line 102

def get(x)
  return @table[x]
end

#put(x, pair) ⇒ Object



98
99
100
# File 'lib/ruby-prolog/ruby-prolog.rb', line 98

def put(x, pair)
  @table[x] = pair
end