Class: Steep::Subtyping::Constraints
- Inherits:
-
Object
- Object
- Steep::Subtyping::Constraints
show all
- Defined in:
- lib/steep/subtyping/constraints.rb
Defined Under Namespace
Classes: UnsatisfiableConstraint, UnsatisfiedInvariantError
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(unknowns:) ⇒ Constraints
Returns a new instance of Constraints.
78
79
80
81
82
83
84
85
|
# File 'lib/steep/subtyping/constraints.rb', line 78
def initialize(unknowns:)
@dictionary = {}
@vars = Set.new
unknowns.each do |var|
dictionary[var] = [Set.new, Set.new]
end
end
|
Instance Attribute Details
#dictionary ⇒ Object
Returns the value of attribute dictionary.
75
76
77
|
# File 'lib/steep/subtyping/constraints.rb', line 75
def dictionary
@dictionary
end
|
#vars ⇒ Object
Returns the value of attribute vars.
76
77
78
|
# File 'lib/steep/subtyping/constraints.rb', line 76
def vars
@vars
end
|
Class Method Details
.empty ⇒ Object
87
88
89
|
# File 'lib/steep/subtyping/constraints.rb', line 87
def self.empty
new(unknowns: [])
end
|
Instance Method Details
#add(var, sub_type: nil, super_type: nil) ⇒ Object
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/steep/subtyping/constraints.rb', line 104
def add(var, sub_type: nil, super_type: nil)
subs, supers = dictionary[var]
if super_type && !super_type.is_a?(AST::Types::Top)
supers << eliminate_variable(super_type, to: AST::Types::Top.new)
end
if sub_type && !sub_type.is_a?(AST::Types::Bot)
subs << eliminate_variable(sub_type, to: AST::Types::Bot.new)
end
super_fvs = supers.each.with_object(Set.new) do |type, fvs|
fvs.merge(type.free_variables)
end
sub_fvs = subs.each.with_object(Set.new) do |type, fvs|
fvs.merge(type.free_variables)
end
unless super_fvs.disjoint?(unknowns) || sub_fvs.disjoint?(unknowns)
raise UnsatisfiedInvariantError.new(
reason: UnsatisfiedInvariantError::UnknownsFreeVariableNotDisjoint.new(
var: var,
lower_bound: sub_type,
upper_bound: super_type
),
constraints: self
)
end
end
|
#each ⇒ Object
253
254
255
256
257
258
259
260
261
|
# File 'lib/steep/subtyping/constraints.rb', line 253
def each
if block_given?
dictionary.each_key do |var|
yield var, lower_bound(var), upper_bound(var)
end
else
enum_for :each
end
end
|
#eliminate_variable(type, to:) ⇒ Object
#empty? ⇒ Boolean
173
174
175
|
# File 'lib/steep/subtyping/constraints.rb', line 173
def empty?
dictionary.keys.empty?
end
|
#has_constraint?(var) ⇒ Boolean
248
249
250
251
|
# File 'lib/steep/subtyping/constraints.rb', line 248
def has_constraint?(var)
lower, upper = dictionary[var]
!lower.empty? || !upper.empty?
end
|
#lower_bound(var) ⇒ Object
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/steep/subtyping/constraints.rb', line 189
def lower_bound(var)
lower_bound, _ = dictionary[var]
case lower_bound.size
when 0
AST::Types::Bot.new
when 1
lower_bound.first
else
AST::Types::Intersection.build(types: lower_bound.to_a)
end
end
|
#solution(checker, variance:, variables:, self_type:) ⇒ Object
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
# File 'lib/steep/subtyping/constraints.rb', line 202
def solution(checker, variance:, variables:, self_type:)
vars = []
types = []
dictionary.each_key do |var|
if variables.include?(var)
if has_constraint?(var)
upper_bound = upper_bound(var)
lower_bound = lower_bound(var)
relation = Relation.new(sub_type: lower_bound, super_type: upper_bound)
checker.check(relation, self_type: self_type, constraints: self.class.empty).yield_self do |result|
if result.success?
vars << var
type = case
when variance.contravariant?(var)
upper_bound
when variance.covariant?(var)
lower_bound
else
if lower_bound.level.join > upper_bound.level.join
upper_bound
else
lower_bound
end
end
types << type
else
raise UnsatisfiableConstraint.new(var: var,
sub_type: lower_bound,
super_type: upper_bound,
result: result)
end
end
else
vars << var
types << AST::Types::Any.new
end
end
end
Interface::Substitution.build(vars, types)
end
|
#to_s ⇒ Object
263
264
265
266
267
268
269
|
# File 'lib/steep/subtyping/constraints.rb', line 263
def to_s
strings = each.map do |var, lower_bound, upper_bound|
"#{lower_bound} <: #{var} <: #{upper_bound}"
end
"#{unknowns.to_a.join(",")}/#{vars.to_a.join(",")} |- { #{strings.join(", ")} }"
end
|
#unknown?(var) ⇒ Boolean
165
166
167
|
# File 'lib/steep/subtyping/constraints.rb', line 165
def unknown?(var)
dictionary.key?(var)
end
|
#unknowns ⇒ Object
169
170
171
|
# File 'lib/steep/subtyping/constraints.rb', line 169
def unknowns
Set.new(dictionary.keys)
end
|
#upper_bound(var) ⇒ Object
177
178
179
180
181
182
183
184
185
186
187
|
# File 'lib/steep/subtyping/constraints.rb', line 177
def upper_bound(var)
_, upper_bound = dictionary[var]
case upper_bound.size
when 0
AST::Types::Top.new
when 1
upper_bound.first
else
AST::Types::Union.build(types: upper_bound.to_a)
end
end
|