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: nil, module_type: nil, self_type: nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/steep/interface/substitution.rb', line 92

def self.build(vars, types = nil, instance_type: nil, module_type: nil, self_type: nil)
  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| #$ Hash[Symbol, AST::Types::t]
    type or raise
    d[var] = type
  end

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

.emptyObject



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

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

Instance Method Details

#[](key) ⇒ Object



67
68
69
# File 'lib/steep/interface/substitution.rb', line 67

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

#add!(v, ty) ⇒ Object



154
155
156
# File 'lib/steep/interface/substitution.rb', line 154

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

#apply?(type) ⇒ Boolean

Returns:

  • (Boolean)


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

def apply?(type)
  case type
  when AST::Types::Var
    key?(type.name)
  when AST::Types::Self
    !self_type.is_a?(AST::Types::Self)
  when AST::Types::Instance
    !instance_type.is_a?(AST::Types::Instance)
  when AST::Types::Class
    !module_type.is_a?(AST::Types::Class)
  when AST::Types::Name::Applying
    type.args.any? {|ty| apply?(ty) }
  else
    type.each_child.any? {|t| apply?(t) }
  end
end

#domainObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/steep/interface/substitution.rb', line 42

def domain
  set = Set.new

  set.merge(dictionary.keys)
  set << AST::Types::Self.instance if self_type
  set << AST::Types::Class.instance if module_type
  set << AST::Types::Instance.instance if instance_type

  set
end

#empty?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
# File 'lib/steep/interface/substitution.rb', line 35

def empty?
  dictionary.empty? &&
    instance_type.is_a?(AST::Types::Instance) &&
    module_type.is_a?(AST::Types::Class) &&
    self_type.is_a?(AST::Types::Self)
end

#except(vars) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/steep/interface/substitution.rb', line 105

def except(vars)
  self.class.new(
    dictionary: dictionary.dup,
    instance_type: instance_type,
    module_type: module_type,
    self_type: self_type
  ).except!(vars)
end

#except!(vars) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/steep/interface/substitution.rb', line 114

def except!(vars)
  vars.each do |var|
    dictionary.delete(var)
  end

  self
end

#key?(var) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/steep/interface/substitution.rb', line 71

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

#merge(s) ⇒ Object



147
148
149
150
151
152
# File 'lib/steep/interface/substitution.rb', line 147

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, overwrite: false) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/steep/interface/substitution.rb', line 122

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

  @instance_type = instance_type.subst(s) if instance_type
  @module_type = module_type.subst(s) if module_type
  @self_type = self_type.subst(s) if self_type

  self
end

#to_sObject



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

def to_s
  a = [] #: Array[String]

  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

#update(self_type: self_type(), instance_type: instance_type(), module_type: module_type()) ⇒ Object



143
144
145
# File 'lib/steep/interface/substitution.rb', line 143

def update(self_type: self_type(), instance_type: instance_type(), module_type: module_type())
  Substitution.new(dictionary: dictionary.dup, instance_type: instance_type, self_type: self_type, module_type: module_type)
end