Class: Steep::Interface::Substitution

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/interface/substitution.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dictionary:, instance_type:, module_type:, self_type:) ⇒ Substitution



9
10
11
12
13
14
# File 'lib/steep/interface/substitution.rb', line 9

def initialize(dictionary:, instance_type:, module_type:, self_type:)
  @dictionary = dictionary
  @instance_type = instance_type
  @module_type = module_type
  @self_type = self_type
end

Instance Attribute Details

#dictionaryObject (readonly)

Returns the value of attribute dictionary.



4
5
6
# File 'lib/steep/interface/substitution.rb', line 4

def dictionary
  @dictionary
end

#instance_typeObject (readonly)

Returns the value of attribute instance_type.



5
6
7
# File 'lib/steep/interface/substitution.rb', line 5

def instance_type
  @instance_type
end

#module_typeObject (readonly)

Returns the value of attribute module_type.



6
7
8
# File 'lib/steep/interface/substitution.rb', line 6

def module_type
  @module_type
end

#self_typeObject (readonly)

Returns the value of attribute self_type.



7
8
9
# File 'lib/steep/interface/substitution.rb', line 7

def self_type
  @self_type
end

Class Method Details

.build(vars, types = nil, instance_type: AST::Types::Instance.new, module_type: AST::Types::Class.new, self_type: AST::Types::Self.new) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/steep/interface/substitution.rb', line 28

def self.build(vars, types = nil, instance_type: AST::Types::Instance.new, module_type: AST::Types::Class.new, self_type: AST::Types::Self.new)
  types ||= vars.map {|var| AST::Types::Var.fresh(var) }

  raise "Invalid substitution: vars.size=#{vars.size}, types.size=#{types.size}" unless vars.size == types.size

  dic = vars.zip(types).each.with_object({}) do |(var, type), d|
    d[var] = type
  end

  new(dictionary: dic, instance_type: instance_type, module_type: module_type, self_type: self_type)
end

.emptyObject



16
17
18
# File 'lib/steep/interface/substitution.rb', line 16

def self.empty
  new(dictionary: {}, instance_type: AST::Types::Instance.new, module_type: AST::Types::Class.new, self_type: AST::Types::Self.new)
end

Instance Method Details

#[](key) ⇒ Object



20
21
22
# File 'lib/steep/interface/substitution.rb', line 20

def [](key)
  dictionary[key] or raise "Unknown variable: #{key}"
end

#add!(v, ty) ⇒ Object



60
61
62
# File 'lib/steep/interface/substitution.rb', line 60

def add!(v, ty)
  merge!(Substitution.new(dictionary: { v => ty }, instance_type: nil, module_type: nil, self_type: nil))
end

#except(vars) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/steep/interface/substitution.rb', line 40

def except(vars)
  self.class.new(
    dictionary: dictionary.reject {|k, _| vars.include?(k) },
    instance_type: instance_type,
    module_type: module_type,
    self_type: self_type
  )
end

#key?(var) ⇒ Boolean



24
25
26
# File 'lib/steep/interface/substitution.rb', line 24

def key?(var)
  dictionary.key?(var)
end

#merge!(s) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/steep/interface/substitution.rb', line 49

def merge!(s)
  dictionary.transform_values! {|ty| ty.subst(s) }
  dictionary.merge!(s.dictionary) do |key, a, b|
    if a == b
      a
    else
      raise "Duplicated key on merge!: #{key}, #{a}, #{b}"
    end
  end
end