Class: SFRP::Poly::Typing

Inherits:
Object
  • Object
show all
Defined in:
lib/sfrp/poly/typing.rb

Instance Method Summary collapse

Constructor Details

#initialize(tconst_str = nil, arg_typings = [], &block) ⇒ Typing

Returns a new instance of Typing.



4
5
6
7
8
9
# 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

Instance Method Details

#mono?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/sfrp/poly/typing.rb', line 39

def mono?
  !variable? && arg_typings.all?(&:mono?)
end

#tconst_strObject



11
12
13
# File 'lib/sfrp/poly/typing.rb', line 11

def tconst_str
  root == self ? @tconst_str : root.tconst_str
end

#to_s(vars = nil) ⇒ Object



59
60
61
62
# 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) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# 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) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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_strObject



34
35
36
37
# File 'lib/sfrp/poly/typing.rb', line 34

def unique_str
  raise unless mono?
  "#{tconst_str}[#{arg_typings.map(&:unique_str).join(', ')}]"
end

#variablesObject



43
44
45
46
# File 'lib/sfrp/poly/typing.rb', line 43

def variables
  return [self] if variable?
  arg_typings.flat_map(&:variables)
end