class Upl::Variable

A variable, either from an existing term_t or a new one

Attributes

name[R]
term_t[R]
to_term_t[R]

Public Class Methods

[]( *names ) click to toggle source
# File lib/upl/variable.rb, line 40
def self.[]( *names )
  vars = names.map{|name| new name: name}
  if vars.size == 1 then vars.first else vars end
end
copy(term_t) click to toggle source
# File lib/upl/variable.rb, line 15
def self.copy term_t
  inst = new term_t

  inst.attributed? and inst.attribute
  inst.to_s

  inst
end
new(term_t = nil, name: nil) click to toggle source
# File lib/upl/variable.rb, line 4
def initialize term_t = nil, name: nil
  @term_t = term_t || self.class.to_term
  @name = name
end
to_term() click to toggle source

TODO remove bit of a hack to create empty variables for a functor

# File lib/upl/variable.rb, line 36
def self.to_term
  Extern.PL_new_term_ref
end

Public Instance Methods

===(value;) click to toggle source
# File lib/upl/variable.rb, line 32
def === value; unify value end
_string() click to toggle source
# File lib/upl/variable.rb, line 47
def _string
  @_string ||= begin
    rv = Extern::PL_get_chars \
      term_t,
      (str_ref = Fiddle::Pointer[0].ref),
      # need cvt_variable for normal variables, and cvt_write for clpfd variables
      Extern::Convert::CVT_VARIABLE | Extern::Convert::CVT_WRITE | Extern::Convert::REP_UTF8 | Extern::Convert::BUF_MALLOC
      # | Extern::CVT_ALL

    str_ref.ptr.free = Extern::swipl_free_fn
    # TODO might need to force utf8 encoding here?
    # Just use CVT_UTF8
    str_ref.ptr.to_s
  end
end
attribute() click to toggle source
# File lib/upl/variable.rb, line 71
def attribute
  @attribute ||= begin
    rv = Extern::PL_get_attr term_t, (val = Extern.PL_new_term_ref)
    rv == 1 or raise "can't get attribute for variable"
    Tree.of_term val
  end
end
attributed?() click to toggle source
# File lib/upl/variable.rb, line 63
def attributed?
  if instance_variable_defined? :@attributed
    @attributed
  else
    @attributed = (Extern::PL_is_attvar term_t) == 1
  end
end
pretty_print(pp) click to toggle source
# File lib/upl/variable.rb, line 79
def pretty_print pp
  if attributed?
    attribute.pretty_print pp
  else
    if name
      pp.text name
      pp.text '='
    end
    pp.text to_s
  end
end
to_ruby() click to toggle source

create a ruby represetation of the term_t

# File lib/upl/variable.rb, line 13
def to_ruby; Tree.of_term term_t end
to_s() click to toggle source
# File lib/upl/variable.rb, line 45
def to_s; _string end
unify(value) click to toggle source
# File lib/upl/variable.rb, line 24
def unify value
  warn "don't pass a term_t in here" if Fiddle::Pointer === value
  case Upl::Extern::PL_unify term_t, value.to_term_t
  when Upl::Extern::TRUE;  true
  when Upl::Extern::FALSE; false
  end
end