-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscheme_types.py
More file actions
565 lines (408 loc) · 18.3 KB
/
scheme_types.py
File metadata and controls
565 lines (408 loc) · 18.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
from __future__ import annotations
import copy
from abc import abstractmethod
from dataclasses import InitVar, dataclass, field
from typing import (Any, Callable, ClassVar, Dict, List, Mapping, Optional,
Tuple, Type, cast)
import sexp
from sexp import SSym
from visitor import Visitor
@dataclass(frozen=True)
class SchemeObjectType:
def symbol(self) -> Optional[sexp.SSym]:
return None
def __str__(self) -> str:
return 'object'
def join(self, other: SchemeObjectType) -> SchemeObjectType:
if self == other:
return self
return SchemeObject
def __lt__(self, other: object) -> bool:
return issubclass(type(self), type(other))
def join_with(self, other: object) -> SchemeObjectType:
assert isinstance(other, SchemeObjectType)
if self < other:
return other
elif other < self:
return self
else:
return SchemeObject
SchemeObject = SchemeObjectType()
@dataclass(frozen=True)
class SchemeBottomType(SchemeObjectType):
pass
SchemeBottom = SchemeBottomType()
class SchemeValueType(SchemeObjectType):
def type_name(self) -> sexp.SSym:
raise NotImplementedError
@dataclass(frozen=True)
class SchemeNumType(SchemeValueType):
def symbol(self) -> Optional[sexp.SSym]:
return SSym('number')
def __str__(self) -> str:
return 'number'
def type_name(self) -> sexp.SSym:
return sexp.SSym('number')
SchemeNum = SchemeNumType()
@dataclass(frozen=True)
class SchemeBoolType(SchemeValueType):
def symbol(self) -> Optional[sexp.SSym]:
return SSym('bool')
def __str__(self) -> str:
return 'bool'
def type_name(self) -> sexp.SSym:
return sexp.SSym('bool')
SchemeBool = SchemeBoolType()
@dataclass(frozen=True)
class SchemeSymType(SchemeValueType):
def symbol(self) -> Optional[sexp.SSym]:
return SSym('symbol')
def __str__(self) -> str:
return 'symbol'
def type_name(self) -> sexp.SSym:
return sexp.SSym('symbol')
SchemeSym = SchemeSymType()
@dataclass(frozen=True)
class SchemeVectType(SchemeValueType):
length: Optional[int]
def symbol(self) -> Optional[sexp.SSym]:
return SSym('vector')
def __str__(self) -> str:
if self.length is not None:
return f'vector[{self.length}]'
return 'vector'
def join(self, other: SchemeObjectType) -> SchemeObjectType:
if isinstance(other, SchemeVectType):
if self.length == other.length:
return SchemeVectType(self.length)
return SchemeVectType(None)
return SchemeObject
def __init__(self, length: Optional[int]):
# https://docs.python.org/3/library/dataclasses.html#frozen-instances
object.__setattr__(
self,
'length',
None if length is not None and length > 4 else length)
def type_name(self) -> sexp.SSym:
return sexp.SSym('vector')
def __lt__(self, other: object) -> bool:
if isinstance(other, SchemeVectType):
return other.length is None or self.length == other.length
return super().__lt__(other)
def join_with(self, other: object) -> SchemeObjectType:
if isinstance(other, SchemeVectType):
length = self.length if self.length == other.length else None
return SchemeVectType(length)
return super().join_with(other)
@dataclass(frozen=True)
class SchemeFunctionType(SchemeValueType):
arity: Optional[int]
return_type: SchemeObjectType = SchemeObject
def __lt__(self, other: object) -> bool:
if isinstance(other, SchemeFunctionType):
return (
(other.arity is None or self.arity == other.arity)
and self.return_type < other.return_type
)
return super().__lt__(other)
def join_with(self, other: object) -> SchemeObjectType:
if isinstance(other, SchemeFunctionType):
arity = self.arity if self.arity == other.arity else None
return_type = self.return_type.join_with(other.return_type)
return SchemeFunctionType(arity, return_type)
return super().join_with(other)
def type_name(self) -> sexp.SSym:
return sexp.SSym('function')
def symbol(self) -> Optional[sexp.SSym]:
return SSym('function')
def __str__(self) -> str:
if self.arity is not None:
return f'function[{self.arity}, {self.return_type}]'
return 'function[{self.return_type}]'
def join(self, other: SchemeObjectType) -> SchemeObjectType:
if isinstance(other, SchemeFunctionType):
if self.arity == other.arity:
return SchemeFunctionType(self.arity)
return SchemeFunctionType(None)
return SchemeObject
@dataclass(frozen=True)
class SchemeQuotedType(SchemeObjectType):
expr_type: Type[sexp.SExp]
TypeTuple = Tuple[SchemeObjectType, ...]
@dataclass
class SExpWrapper:
expr: sexp.SExp
def __eq__(self, other: object) -> bool:
return hash(self) == hash(other)
def __hash__(self) -> int:
return hash(id(self.expr))
def get_type(value: sexp.Value) -> SchemeObjectType:
visitor = CallArgsTypeAnalyzer()
visitor.visit(value)
return visitor.arg_types[0]
class CallArgsTypeAnalyzer(Visitor):
def __init__(self) -> None:
self.arg_types: List[SchemeObjectType] = []
def visit_SFunction(self, func: sexp.SFunction) -> None:
self.arg_types.append(SchemeFunctionType(len(func.params)))
def visit_SNum(self, num: sexp.SNum) -> None:
self.arg_types.append(SchemeNum)
def visit_SBool(self, sbool: sexp.SBool) -> None:
self.arg_types.append(SchemeBool)
def visit_SSym(self, sym: sexp.SSym) -> None:
self.arg_types.append(SchemeSym)
def visit_SVect(self, vect: sexp.SVect) -> None:
self.arg_types.append(SchemeVectType(len(vect.items)))
class FunctionTypeAnalyzer(Visitor):
def __init__(self, param_types: Mapping[sexp.SSym, SchemeObjectType],
global_env: Dict[sexp.SSym, sexp.Value]):
self._param_types = param_types
self._expr_types: Dict[SExpWrapper, SchemeObjectType] = {}
self._expr_values: Dict[SExpWrapper, sexp.Value] = {}
self._global_env = global_env
self._function_type: Optional[SchemeFunctionType] = None
# Keep track of whether we've hit a lambda
self._inside_function = False
def get_function_type(self) -> SchemeFunctionType:
assert self._function_type is not None
return self._function_type
def get_expr_types(self) -> Dict[SExpWrapper, SchemeObjectType]:
return copy.copy(self._expr_types)
def get_expr_type(self, expr: sexp.SExp) -> SchemeObjectType:
return self._expr_types[SExpWrapper(expr)]
def set_expr_type(self, expr: sexp.SExp, type_: SchemeObjectType) -> None:
self._expr_types[SExpWrapper(expr)] = type_
def expr_type_known(self, expr: sexp.SExp) -> bool:
return SExpWrapper(expr) in self._expr_types
def get_expr_values(self) -> Dict[SExpWrapper, sexp.Value]:
return copy.copy(self._expr_values)
def get_expr_value(self, expr: sexp.SExp) -> sexp.Value:
return self._expr_values[SExpWrapper(expr)]
def expr_value_known(self, expr: sexp.SExp) -> bool:
return SExpWrapper(expr) in self._expr_values
def set_expr_value(self, expr: sexp.SExp, value: sexp.Value) -> None:
self._expr_values[SExpWrapper(expr)] = value
def visit_SBegin(self, begin: sexp.SBegin) -> None:
assert len(begin.exprs) != 0, 'begin bodies must not be empty'
super().visit_SBegin(begin)
self.set_expr_type(begin, self.get_expr_type(begin.exprs[-1]))
def visit_SFunction(self, func: sexp.SFunction) -> None:
if self._inside_function:
self.set_expr_type(func, SchemeFunctionType(len(func.params)))
# Lambda bodies will be analyzed separately when they're called
else:
self._inside_function = True
for param in func.params:
super().visit(param)
super().visit(func.body)
self._function_type = SchemeFunctionType(
len(func.params),
self.get_expr_type(list(func.body)[-1])
)
self.set_expr_type(func, self._function_type)
def visit_SNum(self, num: sexp.SNum) -> None:
self.set_expr_type(num, SchemeNum)
self.set_expr_value(num, num)
def visit_SBool(self, sbool: sexp.SBool) -> None:
self.set_expr_type(sbool, SchemeBool)
self.set_expr_value(sbool, sbool)
def visit_SSym(self, sym: sexp.SSym) -> None:
if sym in self._param_types:
self.set_expr_type(sym, self._param_types[sym])
elif sym in _BUILTINS_FUNC_TYPES:
self.set_expr_type(sym, _BUILTINS_FUNC_TYPES[sym])
elif sym in self._global_env:
func = self._global_env[sym]
assert isinstance(func, sexp.SFunction)
self.set_expr_type(sym, SchemeFunctionType(len(func.params)))
else:
self.set_expr_type(sym, SchemeObject)
def visit_SVect(self, vect: sexp.SVect) -> None:
super().visit_SVect(vect)
self.set_expr_type(vect, SchemeVectType(len(vect.items)))
def visit_Quote(self, quote: sexp.Quote) -> None:
type_: SchemeObjectType
if isinstance(quote.expr, sexp.SPair) and quote.expr.is_list():
# Quoted lists are turned into pairs
self.set_expr_type(quote, SchemeVectType(2))
elif isinstance(quote.expr, sexp.SSym):
self.set_expr_type(quote, SchemeSym)
else:
self.set_expr_type(quote, SchemeQuotedType(type(quote.expr)))
def visit_SCall(self, call: sexp.SCall) -> None:
super().visit_SCall(call)
if (isinstance(call.func, sexp.SSym)
and call.func in _builtin_const_exprs):
_builtin_const_exprs[call.func](self).eval_expr(call)
elif self.expr_type_known(call.func):
func_type = self.get_expr_type(call.func)
if isinstance(func_type, SchemeFunctionType):
self.set_expr_type(call, func_type.return_type)
else:
self.set_expr_type(call, SchemeObject)
else:
self.set_expr_type(call, SchemeObject)
def visit_SConditional(self, cond: sexp.SConditional) -> None:
super().visit_SConditional(cond)
then_type = self.get_expr_type(cond.then_expr)
else_type = self.get_expr_type(cond.else_expr)
if (self.get_expr_type(cond.test) == SchemeBool
and self.expr_value_known(cond.test)):
expr_val = self.get_expr_value(cond.test)
assert isinstance(expr_val, sexp.SBool)
if expr_val.value:
self.set_expr_type(cond, then_type)
else:
self.set_expr_type(cond, else_type)
else:
self.set_expr_type(cond, then_type.join_with(else_type))
_BUILTINS_FUNC_TYPES: Dict[sexp.SSym, SchemeObjectType] = {
sexp.SSym('inst/typeof'): SchemeFunctionType(1, SchemeSym),
sexp.SSym('inst/trap'): SchemeFunctionType(0, SchemeBottom),
sexp.SSym('inst/trace'): SchemeFunctionType(1, SchemeNum),
sexp.SSym('inst/display'): SchemeFunctionType(1, SchemeNum),
sexp.SSym('inst/newline'): SchemeFunctionType(0, SchemeNum),
sexp.SSym('inst/breakpoint'): SchemeFunctionType(0, SchemeNum),
sexp.SSym('inst/alloc'): SchemeFunctionType(1, SchemeVectType(None)),
sexp.SSym('inst/load'): SchemeFunctionType(2, SchemeObject),
sexp.SSym('inst/store'): SchemeFunctionType(3, SchemeVectType(None)),
sexp.SSym('inst/length'): SchemeFunctionType(1, SchemeNum),
sexp.SSym('inst/+'): SchemeFunctionType(2, SchemeNum),
sexp.SSym('inst/-'): SchemeFunctionType(2, SchemeNum),
sexp.SSym('inst/*'): SchemeFunctionType(2, SchemeNum),
sexp.SSym('inst//'): SchemeFunctionType(2, SchemeNum),
sexp.SSym('inst/%'): SchemeFunctionType(2, SchemeNum),
sexp.SSym('inst/number='): SchemeFunctionType(2, SchemeBool),
sexp.SSym('inst/symbol='): SchemeFunctionType(2, SchemeBool),
sexp.SSym('inst/pointer='): SchemeFunctionType(2, SchemeBool),
sexp.SSym('inst/number<'): SchemeFunctionType(2, SchemeBool),
sexp.SSym('trap'): SchemeFunctionType(0, SchemeBottom),
sexp.SSym('trace'): SchemeFunctionType(1, SchemeNum),
sexp.SSym('display'): SchemeFunctionType(1, SchemeNum),
sexp.SSym('breakpoint'): SchemeFunctionType(0, SchemeNum),
sexp.SSym('assert'): SchemeFunctionType(1, SchemeNum),
sexp.SSym('typeof'): SchemeFunctionType(1, SchemeSym),
sexp.SSym('not'): SchemeFunctionType(1, SchemeBool),
sexp.SSym('number?'): SchemeFunctionType(1, SchemeBool),
sexp.SSym('symbol?'): SchemeFunctionType(1, SchemeBool),
sexp.SSym('vector?'): SchemeFunctionType(1, SchemeBool),
sexp.SSym('function?'): SchemeFunctionType(1, SchemeBool),
sexp.SSym('bool?'): SchemeFunctionType(1, SchemeBool),
sexp.SSym('pair?'): SchemeFunctionType(1, SchemeBool),
sexp.SSym('nil?'): SchemeFunctionType(1, SchemeBool),
sexp.SSym('+'): SchemeFunctionType(2, SchemeNum),
sexp.SSym('-'): SchemeFunctionType(2, SchemeNum),
sexp.SSym('*'): SchemeFunctionType(2, SchemeNum),
sexp.SSym('/'): SchemeFunctionType(2, SchemeNum),
sexp.SSym('%'): SchemeFunctionType(2, SchemeNum),
sexp.SSym('pointer='): SchemeFunctionType(2, SchemeBool),
sexp.SSym('symbol='): SchemeFunctionType(2, SchemeBool),
sexp.SSym('number='): SchemeFunctionType(2, SchemeBool),
sexp.SSym('number<'): SchemeFunctionType(2, SchemeBool),
sexp.SSym('vector-length'): SchemeFunctionType(1, SchemeNum),
sexp.SSym('vector-index'): SchemeFunctionType(2, SchemeObject),
sexp.SSym('vector-set!'): SchemeFunctionType(3, SchemeVectType(None)),
sexp.SSym('vector-make/recur'): (
SchemeFunctionType(4, SchemeVectType(None))),
sexp.SSym('vector-make'): SchemeFunctionType(2, SchemeVectType(None)),
sexp.SSym('list'): SchemeFunctionType(1, SchemeVectType(None)),
sexp.SSym('cons'): SchemeVectType(2)
}
class BuiltinCallTypeEvaler:
expected_arg_types: ClassVar[Tuple[SchemeObjectType, ...]]
base_return_type: ClassVar[SchemeObjectType]
def __init__(self, expr_types: FunctionTypeAnalyzer):
self.expr_types = expr_types
def eval_expr(self, call: sexp.SCall) -> None:
self.expr_types.set_expr_type(call, self.base_return_type)
if len(call.args) != len(self.expected_arg_types):
return
call_args: List[CallArg] = []
for arg, expected_type in zip(call.args, self.expected_arg_types):
arg_type = self.expr_types.get_expr_type(arg)
if not (arg_type < expected_type):
return
arg_value = (self.expr_types.get_expr_value(arg)
if self.expr_types.expr_value_known(arg) else None)
call_args.append(CallArg(arg_type, arg_value))
self._eval_expr_impl(call, *call_args)
def _eval_expr_impl(self, call: sexp.SCall, *args: CallArg) -> None:
pass
@dataclass(eq=False)
class CallArg:
type_: SchemeObjectType
value: Optional[sexp.Value]
_DecoratorType = Callable[[Type[BuiltinCallTypeEvaler]],
Type[BuiltinCallTypeEvaler]]
def _register_const_call_expr(func_name: str) -> _DecoratorType:
def decorator(cls: Type[BuiltinCallTypeEvaler]
) -> Type[BuiltinCallTypeEvaler]:
_builtin_const_exprs[sexp.SSym(func_name)] = cls
return cls
return decorator
_builtin_const_exprs: Dict[sexp.SSym, Type[BuiltinCallTypeEvaler]] = {}
@_register_const_call_expr('typeof')
class Typeof(BuiltinCallTypeEvaler):
expected_arg_types = (SchemeValueType(),)
base_return_type = SchemeSym
def _eval_expr_impl(self, call: sexp.SCall, *args: CallArg) -> None:
[arg] = args
if isinstance(arg.type_, SchemeValueType):
self.expr_types.set_expr_value(call, arg.type_.type_name())
@_register_const_call_expr('not')
class Not(BuiltinCallTypeEvaler):
expected_arg_types = (SchemeBool,)
base_return_type = SchemeBool
def _eval_expr_impl(self, call: sexp.SCall, *args: CallArg) -> None:
[arg] = args
if isinstance(arg.value, sexp.SBool):
self.expr_types.set_expr_value(
call, sexp.SBool(not arg.value.value))
class TypeQuery(BuiltinCallTypeEvaler):
expected_arg_types = (SchemeObject,)
base_return_type = SchemeBool
query_type: SchemeObjectType
def _eval_expr_impl(self, call: sexp.SCall, *args: CallArg) -> None:
[type_query_arg] = args
# The type being SchemeObject probably indicates that
# we don't know the type, so we don't know for sure if the
# query is false.
if type_query_arg.type_ == SchemeObject:
return
self.expr_types.set_expr_value(
call,
sexp.SBool(type_query_arg.type_ < self.query_type))
@_register_const_call_expr('number?')
class IsNumber(TypeQuery):
query_type = SchemeNum
@_register_const_call_expr('symbol?')
class IsSymbol(TypeQuery):
query_type = SchemeSym
@_register_const_call_expr('vector?')
class IsVector(TypeQuery):
query_type = SchemeVectType(None)
@_register_const_call_expr('function?')
class IsFunction(TypeQuery):
query_type = SchemeFunctionType(None)
@_register_const_call_expr('bool?')
class IsBool(TypeQuery):
query_type = SchemeBool
@_register_const_call_expr('symbol=')
class SymbolEq(BuiltinCallTypeEvaler):
expected_arg_types = (SchemeSym, SchemeSym)
base_return_type = SchemeBool
def _eval_expr_impl(self, call: sexp.SCall, *args: CallArg) -> None:
[first, second] = args
if first.value is not None and second.value is not None:
self.expr_types.set_expr_value(
call,
sexp.SBool(first.value == second.value))
@_register_const_call_expr('vector-make')
class VectorMake(BuiltinCallTypeEvaler):
expected_arg_types = (SchemeNum, SchemeObject)
base_return_type = SchemeVectType(None)
def _eval_expr_impl(self, call: sexp.SCall, *args: CallArg) -> None:
[size_arg, _] = args
size = (size_arg.value.value
if isinstance(size_arg.value, sexp.SNum) else None)
self.expr_types.set_expr_type(call, SchemeVectType(size))