class SFRP::Poly::Typing

Public Class Methods

new(tconst_str = nil, arg_typings = [], &block) click to toggle source
# File lib/sfrp/poly/typing.rb, line 4
def initialize(tconst_str = nil, arg_typings = [], &block)
  @tconst_str = tconst_str
  @arg_typings = arg_typings
  @parent = nil
  block.call(self) if block
end

Public Instance Methods

mono?() click to toggle source
# File lib/sfrp/poly/typing.rb, line 39
def mono?
  !variable? && arg_typings.all?(&:mono?)
end
tconst_str() click to toggle source
# File lib/sfrp/poly/typing.rb, line 11
def tconst_str
  root == self ? @tconst_str : root.tconst_str
end
to_s(vars = nil) click to toggle source
# File lib/sfrp/poly/typing.rb, line 59
def to_s(vars = nil)
  vars ||= variables
  to_type_annot(vars).to_s
end
to_type_annot(vars) click to toggle source
# File lib/sfrp/poly/typing.rb, line 48
def to_type_annot(vars)
  if variable?
    idx = vars.index { |v| v.same?(self) }
    raise unless idx
    TypeAnnotationVar.new('a' + idx.to_s)
  else
    args = arg_typings.map { |t| t.to_type_annot(vars) }
    TypeAnnotationType.new(tconst_str, args)
  end
end
unify(other) click to toggle source
# File lib/sfrp/poly/typing.rb, line 15
def unify(other)
  return self if same?(other)
  return root.unify(other) unless root == self
  if variable? && other.variable?
    @parent = other
  elsif variable? && !other.variable?
    raise UnifyError.new(self, other) if occur?(other)
    @parent = other
  elsif !variable? && other.variable?
    other.unify(self)
  else
    unless tconst_str == other.tconst_str && argc == other.argc
      raise UnifyError.new(self, other)
    end
    arg_typings.zip(other.arg_typings) { |a, b| a.unify(b) }
    @parent = other
  end
end
unique_str() click to toggle source
# File lib/sfrp/poly/typing.rb, line 34
def unique_str
  raise unless mono?
  "#{tconst_str}[#{arg_typings.map(&:unique_str).join(', ')}]"
end
variables() click to toggle source
# File lib/sfrp/poly/typing.rb, line 43
def variables
  return [self] if variable?
  arg_typings.flat_map(&:variables)
end

Protected Instance Methods

arg_typings() click to toggle source
# File lib/sfrp/poly/typing.rb, line 74
def arg_typings
  root == self ? @arg_typings : root.arg_typings
end
argc() click to toggle source
# File lib/sfrp/poly/typing.rb, line 70
def argc
  arg_typings.size
end
occur?(other) click to toggle source
# File lib/sfrp/poly/typing.rb, line 89
def occur?(other)
  raise unless variable?
  return true if same?(other)
  arg_typings.any? { |t| occur?(t) }
end
root() click to toggle source
# File lib/sfrp/poly/typing.rb, line 66
def root
  @parent ? (@parent = @parent.root) : self
end
same?(other) click to toggle source
# File lib/sfrp/poly/typing.rb, line 82
def same?(other)
  return true if root == other.root
  return false if variable? || other.variable?
  return false unless tconst_str == other.tconst_str && argc == other.argc
  arg_typings.zip(other.arg_typings).all? { |a, b| a.same?(b) }
end
variable?() click to toggle source
# File lib/sfrp/poly/typing.rb, line 78
def variable?
  tconst_str.nil?
end