-
Notifications
You must be signed in to change notification settings - Fork 2
/
subtyping.grace
503 lines (394 loc) · 16 KB
/
subtyping.grace
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
//typechcker module for inheritator2
//adapted from typed.grace by Tim Jones
//TODO MUST RIP OUT ASSUMPTIONS TO separate obejct
import "combinator-collections" as c
use c.abbreviations
import "errors" as errors
use errors.exports
import "utility" as utility
use utility.exports
method check (left) isSubtypeOf (right) {
def leftObjectType = makeObjectType(left)
def rightObjectType = makeObjectType(right)
//print "checking: {left} isSubtypeOf {right}"
leftObjectType.isSubtypeOf(rightObjectType)
}
method check (left) isTypeEquals (right) {
def leftObjectType = makeObjectType(left)
def rightObjectType = makeObjectType(right)
//print "checking: {left} isTypeEquals {right}"
//print "objectType: {leftObjectType} isTypeEquals {rightObjectType}"
leftObjectType == (rightObjectType)
}
method makeObjectType(obj) {
//print "MAKEOBJYEPE:{obj}"
match (obj.kind)
case { "ngUnknown" -> unknownObjectType }
case { "ngImplicitUnknown" -> unknownObjectType }
case { "ngTypeType" -> obj.value }
case { "ngInterface" -> objectType(obj) }
case { "objectContext" -> objectConstructorType(obj,obj.body,obj.evilCtxt) }
}
//from Tim
type ObjectType = interface {
methods -> Set[[MethodType]]
methodNamed(name : String)
ifAbsent[[T]](onAbsent : Action[[T]]) -> MethodType | T
isUnknown -> Boolean
isStructural -> Boolean
isSubtypeOf(other : ObjectType) -> Boolean
// Used for redispatch in isSubtypeOf(), and does not actually represent a
// calculation of whether this type is a supertype of the given one.
// b.reverseSubtypeOf(a) IMPLIES b.issubtypeOf(a )
reverseSubtypeOf(other : ObjectType) -> Boolean
// b.reverseNotSubtypeOf(a) IMPLIES NOT b.issubtypeOf(a)
reverseNotSubtypeOf(other : ObjectType) -> Boolean
|(other : ObjectType) -> ObjectType
&(other : ObjectType) -> ObjectType
}
var otCount := 0
class abstractObjectType {
def methods is public = empty
otCount := otCount + 1
def otID is public = otCount
method asString {error "abstract!!!!!"}
method methodNamed(name) ifAbsent (block) {
for (methods) do { meth ->
if (meth.name == name) then {
return meth
}
}
block.apply
}
method reverseSubtypeOf(other : ObjectType) -> Boolean {
def rv = isStructural && methods.isEmpty
//print "ABST {self} {other}"
//print "ABST REV {rv}"
rv
}
method reverseNotSubtypeOf(other : ObjectType) -> Boolean {
false
}
method isSubtypeOf(oType : ObjectType) -> Boolean {
if (self == oType) then {return true}
// Let the given type have a say.
oType.reverseSubtypeOf(self).orElse {
oType.isStructural.andAlso {
//print "sub: {self} {oType}"
def rv = isSubtypeOf(oType) withAssumptions(dictionary)
//print "SUB: {self} {oType} {rv}"
rv
}
}
}
method isSubtypeOf(oType : ObjectType)
withAssumptions(assumptions :
MutableDictionary[[ObjectType, MutableSet[[ObjectType]] ]])
-> Boolean {
//print "ISTOWA {self} {oType}"
if (oType.reverseSubtypeOf(self)) then {return true}
if (oType.reverseNotSubtypeOf(self)) then {return false}
if (oType.isUnknown) then {return true}
//print "ISTOWA got this far"
def against = assumptions.at(self)
ifAbsent { def newSet = list
assumptions.at(self) put(newSet)
newSet }
if (against.contains(oType)) then {return true}
against.add(oType)
for (oType.methods) do { oMeth ->
//print "METH {oMeth.name}"
def sMeth = methodNamed(oMeth.name) ifAbsent { return false }
if (sMeth.typeParameters.size != oMeth.typeParameters.size)
then {return false}
def sParamTypes = sMeth.parametersObjectTypes
def oParamTypes = oMeth.parametersObjectTypes
assert {sParamTypes.size == oParamTypes.size}
because "two methods have the same name but different numbers of parameters"
for (sParamTypes) and(oParamTypes) do { sParam, oParam ->
if (!oParam.isSubtypeOf(sParam)
withAssumptions(assumptions)) then {
return false
}
}
//print "METH RET {sMeth.returnObjectType} {oMeth.returnObjectType}"
if (!sMeth.returnObjectType.isSubtypeOf(oMeth.returnObjectType)
withAssumptions(assumptions) )
then { return false }
}
true
}
def isStructural : Boolean is public = false
def isUnknown : Boolean is public = false
def hashCache = cache {
var acc := 0
for (methods) do { m -> acc := (acc + m.name.hash) % (2 ^ 64) }
acc
}
method hash { ^ hashCache }
method !=(other) { !(self == other) }
method ==(other) {
// print "type=="
// print "self: {self}"
// print "self: {isUnknown} {isStructural} {hash}"
// print "other: {other}"
// print "other: {other.isUnknown} {other.isStructural} {other.hash}"
if (isUnknown && other.isUnknown) then {return true}
if (isStructural != other.isStructural) then {return false}
if (hash != other.hash) then {return false}
return (equalsOther(other))
}
method equalsOther(other) { other.equalsAbstractObjectType(self) }
method equalsAbstractObjectType(other) { error "shouldn't happen" }
method equalsStructuralObjectType(other) { false }
method equalsSingletonObjectType(other) { false }
}
//build an object type from an interpreter level interface object
//(which will be the result of an interface construcctor in the source)
//
class objectType( ngInterface ) {
inherit abstractObjectType
method equalsOther(other) {
//print "ot=OTHER"
other.equalsStructuralObjectType(self) }
method equalsStructuralObjectType(other) {
//print "ot.eSOT"
//print "ctxt {ctxt.dbg} other {other.ctxt.dbg} {ctxt == other.ctxt}"
//print "value {value} other {other.value}"
//print "value {value.nodeID} other {other.value.nodeID} {(value == other.value) }"
//def rv = (ctxt == other.ctxt) && (value == other.value)
//def rv = (value == other.value)
///print "removed assertion is evil"
///assert { (value.nodeID == other.value.nodeID) == (value == other.value) }
def rv = (value == other.value)
//print "ot.eSOT rv = {rv}"
rv
}
def ctxt is public = ngInterface.context
def value is public = ngInterface.value
def methods is public = //TODO rename as methodTypes sometime?
for (ngInterface.value.signatures)
map { sig -> methodType( sig, ctxt ) }
def isStructural : Boolean is public = true
def isUnknown : Boolean is public = false
method asString {
match (methods.size)
case { 0 -> return "interface \{\}"}
case { 1 -> return "interface ot:{otID} value:{value.nodeID} \{ {methods.at(1)} \}" }
case { _ -> }
var rv := "interface ot:{otID} value:{value.nodeID} \{\n "
for (methods) do { meth ->
rv := rv ++ "{meth.name}" ++ "\n "
}
rv := rv ++ "}"
rv
}
}
class objectConstructorType( ngObjectContext, body', ctxt' ) {
inherit abstractObjectType
//VERY VERY BROKEN. DOESN"T DEAL WITH INHERITANCE!!
method reverseSubtypeOf(_) {print "FUCKWITT"}
method reverseNotSubtypeOf(_) {print "FUCKWITT TOO"}
method equalsOther(other) {
//print "ot=OTHER"
other.equalsStructuralObjectType(self) }
method equalsStructuralObjectType(other) {
//print "ot.eSOT"
//print "ctxt {ctxt.dbg} other {other.ctxt.dbg} {ctxt == other.ctxt}"
//print "value {value} other {other.value}"
//print "value {value.nodeID} other {other.value.nodeID} {(value == other.value) }"
//def rv = (ctxt == other.ctxt) && (value == other.value)
//def rv = (value == other.value)
assert { (value.nodeID == other.value.nodeID) == (value == other.value) }
def rv = (value == other.value)
//print "ot.eSOT rv = {rv}"
rv
}
def ctxt is public = ctxt'
def value is public = body'
def methods is public = list //TODO rename as methodTypes sometime?
//the most broken of the broken shit...
//for now: just dumps in all methods / no fields!!!
for (value) do { node ->
def MightBeMethodNode = interface {
signature -> Signature
}
match (node)
case { m : MightBeMethodNode ->
// print "DJT got method {m}"
methods.add( methodType( m.signature, ctxt ) ) }
case { _ -> }
//TODO other kinds of attributes in methods
}
def isStructural : Boolean is public = true
def isUnknown : Boolean is public = false
method asString {
match (methods.size)
case { 0 -> return "objectCX \{\}"}
case { 1 -> return "objectCX ot:{otID} \{ {methods.at(1)} \}" }
case { _ -> }
var rv := "objectCX ot:{otID} \{\n "
for (methods) do { meth ->
rv := rv ++ "{meth}" ++ "\n "
}
rv := rv ++ "}"
rv
}
}
class blockType( ngBlock, parameters, inferredReturnObjectType, ctxt' ) {
inherit abstractObjectType
//VERY VERY BROKEN. DOESN"T DEAL WITH INHERITANCE!!
method reverseSubtypeOf(_) {print "FUCKWITT"}
method reverseNotSubtypeOf(_) {print "FUCKWITT TOO"}
method equalsOther(other) {
//print "ot=OTHER"
other.equalsStructuralObjectType(self) }
method equalsStructuralObjectType(other) {
//print "ot.eSOT"
//print "ctxt {ctxt.dbg} other {other.ctxt.dbg} {ctxt == other.ctxt}"
//print "value {value} other {other.value}"
//print "value {value.nodeID} other {other.value.nodeID} {(value == other.value) }"
//def rv = (ctxt == other.ctxt) && (value == other.value)
//def rv = (value == other.value)
assert { (value.nodeID == other.value.nodeID) == (value == other.value) }
def rv = (value == other.value)
//print "ot.eSOT rv = {rv}"
rv
}
def ctxt is public = ctxt'
def value is public = ngBlock //????almost certainly wrong...
def methods is public = list //TODO rename as methodTypes sometime?
//the most broken of the broken shit...
//for now: just dumps in all methods / no fields!!!
// HEREHEREH
def suffix = match (parameters.size)
case { 0 -> "" }
case { 1 -> "(_)" }
case { 2 -> "(_,_)" }
case { 3 -> "(_,_,_)" }
case { 4 -> "(_,_,_,_)" }
case { 5 -> "(_,_,_,_,_)" }
case { _ -> error "CANT BE BOTHERED TO APPLY MORE VARARGS" }
def pots = for (parameters)
map { p -> makeObjectType (p.typeAnnotation.eval(ctxt.withoutCreatio)) }
//KJX work backwards from here!
methods.add( blockMethodType("apply" ++ suffix, pots, inferredReturnObjectType) )
methods.add( blockMethodType("match" ++ suffix,
list(unknownObjectType) repeated(parameters.size),
booleanType) )
def isStructural : Boolean is public = true
def isUnknown : Boolean is public = false
method asString { "blockCX [[{parameters.size}]] {methods}" }
}
class singletonObjectType {
inherit abstractObjectType
method equalsOther(other) { other.equalsSingletonObjectType(self) }
method equalsSingletonObjectType(other) {
// print "TRUUUUUUMMMMMMMMMMMMP"
// print (numberType.asString == numberType.asString)
// print (asString)
// print (other.asString)
// def a = asString
// def s = self.asString
// def o = other.asString
// print "SOT {a == s} {a == o}"
asString == other.asString }
//def hashCache = cache { asString.hash }
method hash { 42 }
}
def unknownObjectType is public = object {
inherit singletonObjectType
def methods is public = empty
method isUnknown { true }
method isStructural { false }
method isSubtypeOf(_ : ObjectType) -> Boolean { true }
method isSubtypeOf(_ : ObjectType) withAssumptions(_)-> Boolean { true }
method reverseSubtypeOf(_ : ObjectType) -> Boolean { true }
method reverseNotSubtypeOf(_ : ObjectType) -> Boolean { false }
method asString { "unknownObjectType" }
}
//TODO - almost all of the below types should go away
//I'm not sure why Tim started with them
//and then I kept on - kjx
def doneType is public = object {
inherit singletonObjectType
def methods is public = empty
method isUnknown { false }
method isStructural { false }
method isSubtypeOf(other : ObjectType) -> Boolean { // Let other have a say.
other.reverseSubtypeOf(self).orElse { self == other } }
method reverseSubtypeOf(other : ObjectType) -> Boolean { self == other }
method reverseNotSubtypeOf(other : ObjectType) -> Boolean { self != other }
method asString { "doneObjectType" }
}
def numberType is public = object {
inherit singletonObjectType
def methods is public = empty
method isUnknown { false }
method isStructural { false }
method isSubtypeOf(other : ObjectType) -> Boolean { // Let other have a say.
//print "Number ISTO {other}"
other.isSupertypeOf(self).orElse { self == other } }
method isSupertypeOf(other : ObjectType) -> Boolean { self == other }
method asString { "numberType" }
method reverseSubtypeOf(other : ObjectType) -> Boolean { self == other }
method reverseNotSubtypeOf(other : ObjectType) -> Boolean { self != other }
}
def stringType is public = object {
inherit singletonObjectType
def methods is public = empty
method isUnknown { false }
method isStructural { false }
method isSubtypeOf(other : ObjectType) -> Boolean { // Let other have a say.
//print "String ISTO {other}"
other.isSupertypeOf(self).orElse { self == other } }
method isSupertypeOf(other : ObjectType) -> Boolean { self == other }
method asString { "stringType" }
method reverseSubtypeOf(other : ObjectType) -> Boolean { self == other }
method reverseNotSubtypeOf(other : ObjectType) -> Boolean { self != other }
}
def booleanType is public = object {
inherit singletonObjectType
def methods is public = empty
method isUnknown { false }
method isStructural { false }
method isSubtypeOf(other : ObjectType) -> Boolean { // Let other have a say.
//print "String ISTO {other}"
other.isSupertypeOf(self).orElse { self == other } }
method isSupertypeOf(other : ObjectType) -> Boolean { self == other }
method asString { "booleanType" }
method reverseSubtypeOf(other : ObjectType) -> Boolean { self == other }
method reverseNotSubtypeOf(other : ObjectType) -> Boolean { self != other }
}
//from tim, not sure this is right
type MethodType = interface {
name -> String
typeParamters -> Sequence[[Parameter]]
parametersObjectTypes -> Sequence[[ObjectType]]
returnObjectType -> ObjectType
//isSpecialisationOf(other : MethodType) -> Boolean
//isPublic -> Boolean
//isPublic := (value : Boolean) -> Done
}
class methodType ( signatureNode, ctxt ) {
method name { signatureNode.name }
//def returnObjectType is public =
// makeObjectType(signatureNode.returnType.eval(ctxt.withoutCreatio))
method returnObjectType {
makeObjectType(signatureNode.returnType.eval(ctxt.withoutCreatio)) }
method typeParameters { signatureNode.typeParameters }
//def parametersObjectTypes is public =
// for (signatureNode.parameters)
// map { p -> makeObjectType (p.typeAnnotation.eval(ctxt.withoutCreatio)) }
method parametersObjectTypes {
for (signatureNode.parameters)
map { p -> makeObjectType (p.typeAnnotation.eval(ctxt.withoutCreatio)) } }
method asString { "method {name} [[{typeParameters.size}]] ({parametersObjectTypes.size}) {returnObjectType}" }
}
class blockMethodType(name', parametersObjectTypes', returnObjectType') {
method name { name' }
method typeParameters { empty }
method returnObjectType { returnObjectType' }
method parametersObjectTypes { parametersObjectTypes' }
method asString {"bM {name} [[{parametersObjectTypes.size}]]"}
}