Class: RubyProlog::Goal

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pred_id, pred_name, args) ⇒ Goal

Returns a new instance of Goal.



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

def initialize(pred_id, pred_name, args)
  @pred_id, @pred_name, @args = pred_id, pred_name, args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



100
101
102
# File 'lib/ruby-prolog/ruby-prolog.rb', line 100

def args
  @args
end

#pred_idObject (readonly)

Returns the value of attribute pred_id.



100
101
102
# File 'lib/ruby-prolog/ruby-prolog.rb', line 100

def pred_id
  @pred_id
end

#pred_nameObject (readonly)

Returns the value of attribute pred_name.



100
101
102
# File 'lib/ruby-prolog/ruby-prolog.rb', line 100

def pred_name
  @pred_name
end

Instance Method Details

#inspectObject



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

def inspect
  return @pred_name.to_s + @args.inspect.to_s
end

#to_prologObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ruby-prolog/ruby-prolog.rb', line 110

def to_prolog
  args_out = @args.map do |arg|
    case arg
    when Symbol
      if arg == :_
        "_"
      elsif /[[:upper:]]/.match(arg.to_s[0])
        arg.to_s
      else
        "_#{arg.to_s}"
      end
    when String
      "'#{arg}'"
    when Cons, Goal
      arg.to_prolog
    when Numeric
      arg.to_s
    else
      raise "Unknown argument: #{arg.inspect}"
    end
  end.join(', ')

  if @pred_name == :not_
    "\\+ #{args_out}"
  else
    "#{@pred_name}(#{args_out})"
  end
end