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
256
257
258
259
260
261
262
263
264
|
# File 'lib/steep/subtyping/constraints.rb', line 256
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/steep/subtyping/constraints.rb', line 134
def eliminate_variable(type, to:)
case type
when AST::Types::Name
type.args.map do |ty|
eliminate_variable(ty, to: AST::Types::Any.new)
end.yield_self do |args|
AST::Types::Name.new(
name: type.name,
args: args
)
end
when AST::Types::Union
type.types.map do |ty|
eliminate_variable(ty, to: AST::Types::Any.new)
end.yield_self do |types|
AST::Types::Union.build(types: types)
end
when AST::Types::Intersection
type.types.map do |ty|
eliminate_variable(ty, to: AST::Types::Any.new)
end.yield_self do |types|
AST::Types::Intersection.build(types: types)
end
when AST::Types::Var
if vars.member?(type.name)
to
else
type
end
else
type
end
end
|
#empty? ⇒ Boolean
176
177
178
|
# File 'lib/steep/subtyping/constraints.rb', line 176
def empty?
dictionary.keys.empty?
end
|
#has_constraint?(var) ⇒ Boolean
251
252
253
254
|
# File 'lib/steep/subtyping/constraints.rb', line 251
def has_constraint?(var)
lower, upper = dictionary[var]
!lower.empty? || !upper.empty?
end
|
#lower_bound(var) ⇒ Object
192
193
194
195
196
197
198
199
200
201
202
203
|
# File 'lib/steep/subtyping/constraints.rb', line 192
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:) ⇒ Object
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
247
248
249
|
# File 'lib/steep/subtyping/constraints.rb', line 205
def solution(checker, variance:, variables:)
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, 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
266
267
268
269
270
271
272
|
# File 'lib/steep/subtyping/constraints.rb', line 266
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
168
169
170
|
# File 'lib/steep/subtyping/constraints.rb', line 168
def unknown?(var)
dictionary.key?(var)
end
|
#unknowns ⇒ Object
172
173
174
|
# File 'lib/steep/subtyping/constraints.rb', line 172
def unknowns
Set.new(dictionary.keys)
end
|
#upper_bound(var) ⇒ Object
180
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/steep/subtyping/constraints.rb', line 180
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
|