-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathunion.lua
More file actions
624 lines (470 loc) · 12.3 KB
/
Copy pathunion.lua
File metadata and controls
624 lines (470 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
133
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
--ANALYZE
local tostring = tostring
local setmetatable = _G.setmetatable
local table = _G.table
local type = _G.type
local ipairs = _G.ipairs
local Nil = require("nattlua.types.symbol").Nil
local True = require("nattlua.types.symbol").True
local False = require("nattlua.types.symbol").False
local error_messages = require("nattlua.error_messages")
local table_concat = _G.table.concat
local table_remove = _G.table.remove
local table_sort = require("nattlua.other.sort")
local table_clear = require("nattlua.other.tablex").clear
local assert = _G.assert
local math_max = _G.math.max
local math_huge = _G.math.huge
--[[#local type { TSymbol } = require("nattlua.types.symbol")]]
--[[#local type { TNumber } = require("nattlua.types.number")]]
local META = require("nattlua.types.base")()
--[[#local type TBaseType = META.TBaseType]]
--[[#type META.@Name = "TUnion"]]
--[[#type TUnion = META.@Self]]
--[[#type TUnion.LiteralDataCache = Map<|string, TBaseType|>]]
--[[#type TUnion.suppress = boolean]]
--[[#type TUnion.left_right_source = {left = TBaseType, right = TBaseType} | false]]
--[[#type TUnion.parent_table = {table = TBaseType, key = string} | false]]
META.Type = "union"
META:GetSet("Data", false--[[# as List<|TBaseType|>]])
function META:GetHashForMutationTracking()
return tostring(self)
end
function META.Equal(
a--[[#: TUnion]],
b--[[#: TBaseType]],
visited--[[#: nil | Map<|TBaseType, boolean|>]]
)
visited = visited or {}
if visited[a] then return true, "circular reference detected" end
if b.Type ~= "union" and a:GetCardinality() == 1 and a.Data[1] then
return a.Data[1]:Equal(b, visited)
end
if a.Type ~= b.Type then return false, "types differ" end
local b = b
local len = #a.Data
if len ~= #b.Data then return false, "length mismatch" end
for i = 1, len do
local a = assert(a.Data[i])
local ok = false
local reasons = {}
for i = 1, len do
local b = b.Data[i]
local reason
ok, reason = a:Equal(b, visited)
if ok then break end
table.insert(reasons, reason--[[# as string]])
end
if a.Type == "table" then visited[a] = true end
if not ok then
return false, "union value mismatch: " .. table.concat(reasons, "\n")
end
end
return true, "all union values match"
end
function META:GetHash(visited)--[[#: string]]
local data = self.Data
if #data == 1 then
return (data[1]--[[# as any]]):GetHash()
end
visited = visited or {}
if visited[self] then return visited[self]--[[# as string]] end
visited[self] = "*circular*"
local types = {}
local len = #data
for i = 1, len do
types[i] = data[i]:GetHash(visited)
end
table_sort(types)
visited[self] = table_concat(types, "|")
return visited[self]--[[# as string]]
end
local sort = function(a--[[#: string]], b--[[#: string]])
return a < b
end
function META:__tostring()
if self.suppress then return "current_union" end
local s = {}
self.suppress = true
local data = self.Data
local len = #data
for i = 1, len do
s[i] = tostring(data[i])
end
if not s[1] then
self.suppress = false
return "|"
end
self.suppress = false
if len == 1 then return (s[1]--[[# as string]]) .. "|" end
table_sort(s, sort)
return table_concat(s, " | ")
end
local ANY_TYPE = {}
local STRING_TYPE = {}
local NUMBER_TYPE = {}
local function hash(obj)
if obj.Type ~= "table" and obj.Type ~= "function" and obj.Type ~= "tuple" then
return obj:GetHash(nil)
end
end
local function add(self--[[#: TUnion]], obj--[[#: any]])
local s = hash(obj)
if s then self.LiteralDataCache[s] = obj end
self.Data[#self.Data + 1] = obj
end
local function remove(self--[[#: TUnion]], index--[[#: number]])
local obj = assert(self.Data[index])
table_remove(self.Data, index)
local s = hash(obj)
if s then self.LiteralDataCache[s] = nil end
end
local function find_index(self--[[#: TUnion]], obj--[[#: any]])
local data = self.Data
local len = #data
local obj_type = obj.Type
for i = 1, len do
local v = data[i]--[[# as any]]
-- Check type first before expensive Equal call
if v.Type == obj_type and v:Equal(obj) then
if obj_type ~= "function" or v:GetFunctionBodyNode() == obj:GetFunctionBodyNode() then
return i
end
end
end
return nil
end
function META:AddType(e--[[#: TBaseType]])
if e.Type == "union" then
for i = 1, #e.Data do
self:AddType(e.Data[i])
end
return self
end
do
local s = hash(e)
if s and self.LiteralDataCache[s] then return self end
end
if find_index(self, e) then return self end
add(self, e)
return self
end
function META:GetData()
return self.Data
end
function META:GetCardinality()
return #self.Data
end
function META:RemoveType(e--[[#: TBaseType]])
if e.Type == "union" then
for i, v in ipairs(e.Data) do
self:RemoveType(v)
end
return self
end
local index = find_index(self, e)
if index then remove(self, index) end
return self
end
function META:Clear()
do
(table_clear--[[# as any]])(self.Data)
end
do
(table_clear--[[# as any]])(self.LiteralDataCache)
end
end
function META:HasTuples()
for _, obj in ipairs(self.Data) do
if obj.Type == "tuple" then return true end
end
return false
end
function META:GetTupleLength()
local len = 0
for _, obj in ipairs(self.Data) do
if obj.Type == "union" or obj.Type == "tuple" then
len = math_max(len, obj:GetTupleLength())
else
len = math_max(len, 1)
end
end
return len
end
function META:GetAtTupleIndex(i--[[#: number]])
if i > self:GetTupleLength() then return nil end
local obj = self:GetAtTupleIndexUnion(i)--[[# as any]]
if obj then
if obj.Type == "union" then
return obj:GetAtTupleIndexUnion(i)
elseif obj.Type == "tuple" then
return obj:GetWithNumber(i)
end
end
return obj
end
function META:GetAtTupleIndexUnion(i--[[#: number]])
if not self:HasTuples() then return self:Simplify() end
local val--[[#: any]]
local errors = {}
local is_inf = false
for _, obj in ipairs(self.Data) do
if obj.Type == "tuple" then
local found, err = (obj--[[# as any]]):GetWithNumber(i)
if found then
if (obj--[[# as any]]):GetElementCount() == math_huge then is_inf = true end
if found.Type == "union" then
found, err = found:GetAtTupleIndexUnion(1)
elseif found.Type == "tuple" then
found, err = found:GetAtTupleIndex(1)
end
end
if found then
if val then val = self.New({val, found}) else val = found end
else
if val then val = self.New({val, Nil()}) else val = Nil() end
end
elseif i == 1 then
if val then val = self.New({val, obj}) else val = obj end
else
if val then val = self.New({val, Nil()}) else val = Nil() end
end
end
if
(
val.Type == "symbol" or
val.Type == "union" and
val:GetCardinality() == 1
)
and
val:IsNil()
then
return nil
end
if not val then return false, errors end
if val.Type == "union" and val:GetCardinality() == 1 then return val.Data[1] end
return val, is_inf
end
function META:IsTypeObjectSubsetOf(typ--[[#: TBaseType]])
local errors
for i, obj in ipairs(self.Data) do
local ok, reason = typ:IsSubsetOf(obj)
if ok then return obj end
errors = errors or {}
errors[i] = reason
end
return false, errors
end
function META:HasTypeObject(obj--[[#: TBaseType]])
for i, v in ipairs(self.Data) do
local ok, reason = obj:IsSubsetOf(v)
if ok then return v end
end
return false
end
function META:IsEmpty()
return self.Data[1] == nil
end
function META:RemoveCertainlyFalsy()
local copy = META.New()
for _, obj in ipairs(self.Data) do
if not obj:IsCertainlyFalse() then add(copy, obj) end
end
return copy:Simplify()
end
function META:GetTruthy()
local copy = META.New()
for _, obj in ipairs(self.Data) do
if obj:IsTruthy() then add(copy, obj) end
end
return copy
end
function META:GetFalsy()
local copy = META.New()
for _, obj in ipairs(self.Data) do
if obj:IsFalsy() then add(copy, obj) end
end
return copy
end
function META:IsType(typ--[[#: string]])
if self:IsEmpty() then return false end
for _, obj in ipairs(self.Data) do
if obj.Type ~= typ then return false end
end
return true
end
function META:IsTypeExceptNil(typ--[[#: string]])
if self:IsEmpty() then return false end
for _, obj in ipairs(self.Data) do
if obj.Type == "symbol" and obj:IsNil() then
elseif obj.Type == typ then
return true
end
end
return false
end
function META:HasType(typ--[[#: string]])
return self:GetType(typ) ~= false
end
function META:IsNil()
for _, obj in ipairs(self.Data) do
if obj:IsNil() then return true end
end
return false
end
function META:CanBeNil()
for _, obj in ipairs(self.Data) do
if obj:CanBeNil() then return true end
end
return false
end
function META:GetType(typ--[[#: string]])
for _, obj in ipairs(self.Data) do
if obj.Type == typ then return obj end
end
return false
end
function META:IsTargetSubsetOfChild(target--[[#: TBaseType]])
local errors = {}
for i, obj in ipairs(self.Data) do
local ok, reason = target:IsSubsetOf(obj)
if ok then return true end
errors[i] = reason
end
return false, error_messages.subset(target, self, errors)
end
function META.IsSubsetOf(a--[[#: TUnion]], b--[[#: any]])
if a.suppress then return true, "suppressed" end
if b.Type == "tuple" then b = b:GetWithNumber(1) end
if b.Type == "any" then return true end
if a:IsEmpty() then
return false,
error_messages.because(error_messages.subset(a, b), {"union is empty"})
end
for _, a_val in ipairs(a.Data) do
a.suppress = true
local b_val, reason
local ok
if b.Type == "union" then
b_val, reason = b:IsTypeObjectSubsetOf(a_val)
else
ok, reason = a_val:IsSubsetOf(b)
if ok then
b_val = b
else
b_val = false
reason = reason
end
end
a.suppress = false
if not b_val then
return false, error_messages.because(error_messages.subset(b, a_val), reason)
end
a.suppress = true
local ok, reason = a_val:IsSubsetOf(b_val)
a.suppress = false
if not ok then
return false, error_messages.because(error_messages.subset(a_val, b_val), reason)
end
end
return true
end
function META:Union(union--[[#: TUnion]])
local copy = self:Copy()
for _, e in ipairs(union.Data) do
copy:AddType(e)
end
return copy
end
local function copy_val(
val--[[#: TBaseType]],
map--[[#: Map<|any, any|>]],
copy_tables--[[#: nil | boolean]]
)
if not val then return val end
-- if it's already copied
if map[val] then return map[val] end
if val.Type == "table" and not copy_tables then
return val
else
map[val] = (val--[[# as any]]):Copy(map, copy_tables)
end
return map[val]
end
function META:Copy(map--[[#: Map<|any, any|> | nil]], copy_tables--[[#: nil | boolean]])--[[#: TUnion]]
map = map or {}
if map[self] then return map[self]--[[# as TUnion]] end -- TODO map[self] doesn't make return map[self] not nil
local copy = META.New()
map[self] = copy
for i, obj in ipairs(self.Data) do
add(copy, copy_val(obj, map, copy_tables))
end
copy:CopyInternalsFrom(self)
return copy
end
function META:IsTruthy()
for _, v in ipairs(self.Data--[[# as List<|TBaseType|>]]) do
if v:IsTruthy() then return true end
end
return false
end
function META:IsFalsy()
for _, v in ipairs(self.Data--[[# as List<|TBaseType|>]]) do
if v:IsFalsy() then return true end
end
return false
end
function META:IsLiteral()
for _, obj in ipairs(self.Data--[[# as List<|TBaseType|>]]) do
if not obj:IsLiteral() then return false end
end
return true
end
function META:SetLeftRightSource(l, r)
self.left_right_source = {left = l, right = r}
end
function META:GetLeftRightSource()
return self.left_right_source
end
function META:SetParentTable(tbl, key)
self.parent_table = {table = tbl, key = key}
end
function META:GetParentTable()
return self.parent_table
end
function META.New(data--[[#: nil | List<|TBaseType|>]])
local self = META.NewObject(
{
Type = "union",
Data = {},
LiteralDataCache = {},
suppress = false,
left_right_source = false,
parent_table = false,
Contract = false,
Upvalue = false,
}
)
if data then for _, v in ipairs(data) do
self:AddType(v)
end end
return self
end
function META:Simplify()
return #self.Data == 1 and self.Data[1] or self
end
function META:IsNumeric()
for _, obj in ipairs(self.Data--[[# as List<|TBaseType|>]]) do
if not obj:IsNumeric() then return false end
end
return true
end
return {
Union = META.New,
Nilable = function(typ--[[#: TBaseType]])
return META.New({typ, Nil()})
end,
Boolean = function()
return META.New({True(), False()})
end,
}