Class: Steep::Interface::Substitution

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

Defined Under Namespace

Classes: InvalidSubstitutionError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Substitution.



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

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.



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

def dictionary
  @dictionary
end

#instance_typeObject (readonly)

Returns the value of attribute instance_type.



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

def instance_type
  @instance_type
end

#module_typeObject (readonly)

Returns the value of attribute module_type.



18
19
20
# File 'lib/steep/interface/substitution.rb', line 18

def module_type
  @module_type
end

#self_typeObject (readonly)

Returns the value of attribute self_type.



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

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



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/steep/interface/substitution.rb', line 54

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 InvalidSubstitutionError.new(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



28
29
30
# File 'lib/steep/interface/substitution.rb', line 28

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



46
47
48
# File 'lib/steep/interface/substitution.rb', line 46

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

#add!(v, ty) ⇒ Object



94
95
96
# File 'lib/steep/interface/substitution.rb', line 94

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

#except(vars) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/steep/interface/substitution.rb', line 66

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

Returns:

  • (Boolean)


50
51
52
# File 'lib/steep/interface/substitution.rb', line 50

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

#merge(s) ⇒ Object



87
88
89
90
91
92
# File 'lib/steep/interface/substitution.rb', line 87

def merge(s)
  Substitution.new(dictionary: dictionary.dup,
                   instance_type: instance_type,
                   module_type: module_type,
                   self_type: self_type).merge!(s)
end

#merge!(s) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/steep/interface/substitution.rb', line 75

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
  self
end

#to_sObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/steep/interface/substitution.rb', line 32

def to_s
  a = []

  dictionary.each do |x, ty|
    a << "#{x} -> #{ty}"
  end

  a << "[instance_type] -> #{instance_type}"
  a << "[module_type] -> #{module_type}"
  a << "[self_type] -> #{self_type}"

  "{ #{a.join(", ")} }"
end