-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.py
1203 lines (1056 loc) · 35.2 KB
/
base.py
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from types import FunctionType
orig_zip = zip
zip = None
def ezip(first, second):
assert len(first) == len(second), "Length mismatch between:\n%s\n%s" % (
first, second)
return orig_zip(first, second)
SUPERS = {}
DATATYPES = {}
CTORS = {} # :: {type: str}
class Structured(object):
pass
class CopiedCtors(object):
pass
_deferred_type_parses = {}
def _make_ctor(name, members, superclass):
nms = tuple(nm for nm, t in members)
ix = len(DATATYPES)
def __init__(self, *args, **tvars):
self._ix = ix
for i, nm in enumerate(nms):
setattr(self, nm, args[i])
attrs = dict(__slots__=(nms + ('_ix',)), __init__=__init__,
__types__=tuple(t for nm, t in members))
ctor = type(name, (superclass,), attrs)
SUPERS[ctor] = superclass
superclass.ctors.append(ctor)
CTORS[ctor] = name
return ctor
def DT(*members, **opts):
members = list(members)
name = members.pop(0)
t = type(name, (Structured,), {'ctors': [], '_opts': opts})
ctor = _make_ctor(name, members, t)
_dt_form(t, None)
DATATYPES[name] = t
return ctor
def ADT(*ctors, **opts):
ctors = list(ctors)
tname = ctors.pop(0)
derivedFrom = None
derivedSubsts = None
if isinstance(tname, tuple):
if len(tname) == 3:
tname, derivedFrom, subs = tname
derivedSubsts = dict((extrinsic(FormSpec,s), extrinsic(FormSpec,d))
for s, d in subs.iteritems())
else:
tname, derivedFrom = tname
t = type(tname, (Structured,), {'ctors': [], '_opts': opts})
data = [t]
ctor_ix = 0
def setup_ctor(name, members):
d = _make_ctor(name, members, t)
d.__module__ = tname
d._ctor_ix = ctor_ix
return d
tvars = None
if derivedFrom:
tvars = {}
shortcut = CopiedCtors()
data.append(shortcut)
for ctor in derivedFrom.ctors:
# copy ctor
members = []
fields = extrinsic(FormSpec, ctor).fields
for field, val in ezip(fields, ctor.__slots__[:-1]):
newType = derive_copied_ctor_type(field.type, derivedFrom, t,
derivedSubsts or {}, tvars)
members.append((val, newType))
ctor_nm = ctor.__name__
d = setup_ctor(ctor_nm, members)
key = '%s.%s' % (tname, ctor_nm)
CTORS[d] = ctor_nm #key
ctor_ix += 1
setattr(shortcut, ctor_nm, d)
# restore name->tv mapping
tvars = dict((extrinsic(Name, tv), tv) for tv in tvars.itervalues())
while ctors:
ctor = ctors.pop(0)
members = []
while ctors and not isinstance(ctors[0], basestring):
members.append(ctors.pop(0))
d = setup_ctor(ctor, members)
ctor_ix += 1
data.append(d)
_dt_form(t, tvars)
DATATYPES[tname] = t
return tuple(data)
def _dt_form(dt, deriveeTVars):
assert not deriveeTVars
# Envs
EnvInfo = DT('EnvInfo', ('envName', str), ('envType', 'Type'),
('envStack', '[a]'))
_ENVS = set()
def new_env(name, t):
assert isinstance(name, basestring)
if t is not None:
tvars = {}
t = parse_new_type(t, tvars)
e = EnvInfo(name, t, [])
_ENVS.add(e)
return e
def in_env(e, initial, func):
stack = e.envStack
stack.append(initial)
count = len(stack)
ret = func()
assert len(stack) == count, 'Imbalanced env %s stack' % e.envName
stack.pop()
return ret
def env(e):
assert len(e.envStack), 'Not in env %s at present' % e.envName
return e.envStack[-1]
def have_env(e):
return bool(e.envStack)
def display_envs(verbose=False):
for e in _ENVS:
if e.envStack:
print col('Purple', e.envName + ':')
if verbose:
for s in e.envStack:
print repr(s)
else:
print repr(e.envStack[-1])
# stupid debugger shortcut
class DumpEnvs(object):
def __repr__(self):
display_envs()
return '<DumpEnvs>'
dumpenvs = DumpEnvs()
# for more readable stack dumps etc.
class DumpList(list):
def __repr__(self):
return ''.join('%r\n' % (e,) for e in self)
TVARS = new_env('TVARS', None)
NEWTYPEVARS = new_env('NEWTYPEVARS', None)
# Extrinsics
ExtInfo = DT('ExtInfo', ('label', str), ('t', 'Type'), ('stack', [{'a': 't'}]),
('captures', [{'a': 't'}]))
def new_extrinsic(label, t, omni=False):
if t is not None:
tvars = {}
t = parse_new_type(t, tvars)
return ExtInfo(label, t, [{}] if omni else [], [])
def extrinsic(ext, obj):
assert ext.stack, "Not in extrinsic %s" % (ext.label,)
record = ext.stack[-1]
if obj not in record:
try:
desc = repr(obj)
except:
desc = '<%s instance>' % (type(obj),)
assert False, '%s has no %s' % (desc, ext.label)
return record[obj]
def scope_extrinsic(ext, func):
new = ext.stack[-1].copy() if len(ext.stack) else {}
ext.stack.append(new)
ret = func()
n = ext.stack.pop()
assert n is new, "Extrinsic stack imbalance"
return ret
def capture_extrinsic(ext, cap, func):
assert isinstance(cap, dict)
ext.captures.append(cap)
ret = func()
off = ext.captures.pop()
assert off is cap, "Imbalanced capture"
return ret
def capture_scoped(exts, captures, func):
# Inlined to avoid polluting stack trace
check = []
for ext in exts:
assert ext not in captures
cap = captures[ext] = {}
ext.captures.append(cap)
new = ext.stack[-1].copy() if len(ext.stack) else {}
ext.stack.append(new)
check.append((cap, new))
ret = func()
for ext, (offcap, offnew) in ezip(exts[::-1], check[::-1]):
cap = ext.captures.pop()
assert offcap is cap, "Imbalanced capture"
n = ext.stack.pop()
assert offnew is n, "Extrinsic stack imbalance"
return ret
def in_extrinsic_scope(ext):
return bool(ext.stack)
def add_extrinsic(ext, obj, val):
assert not isinstance(obj, value_types), "%s on value %r" % (ext.label,obj)
assert ext.stack, "Not in extrinsic %s" % (ext.label,)
map = ext.stack[-1]
assert obj not in map, "%r already has %s extrinsic" % (obj, ext.label)
map[obj] = val
if len(ext.captures) > 0:
cap = ext.captures[-1]
assert obj not in cap
cap[obj] = val
def update_extrinsic(ext, obj, val):
assert not isinstance(obj, value_types), "%s on value %r" % (ext.label,obj)
assert ext.stack, "Not in extrinsic %s" % (ext.label,)
map = ext.stack[-1]
assert obj in map, "%r doesn't have %s extrinsic yet" % (obj, ext.label)
map[obj] = val
if len(ext.captures) > 0:
cap = ext.captures[-1]
assert obj in cap
cap[obj] = val
def remove_extrinsic(ext, obj):
assert not isinstance(obj, value_types), "%s on value %r" % (ext.label,obj)
assert ext.stack, "Not in extrinsic %s" % (ext.label,)
map = ext.stack[-1]
assert obj in map, "%r doesn't have %s extrinsic" % (obj, ext.label)
del map[obj]
if len(ext.captures) > 0:
cap = ext.captures[-1]
assert obj in cap
del cap[obj]
def has_extrinsic(ext, obj):
assert not isinstance(obj, value_types), "%s on value %r" % (ext.label,obj)
assert ext.stack, "Not in extrinsic %s" % (ext.label,)
return obj in ext.stack[-1]
Name = new_extrinsic('Name', None, omni=True)
FormSpec = new_extrinsic('FormSpec', None, omni=True)
TrueRepresentation = new_extrinsic('TrueRepresentation', None, omni=True)
value_types = (basestring, bool, int, float, tuple, type(None))
# Forms
Field = DT('Field', ('type', 'Type'))
Ctor = DT('Ctor', ('fields', [Field]))
DTOpts = DT('DTOpts', ('valueType', bool),
('garbageCollected', bool))
DataType = DT('DataType', ('ctors', [Ctor]),
('tvars', ['TypeVar']),
('opts', DTOpts))
DtList = DT('DtList', ('dts', [DataType]))
del _dt_form
def _ctor_form(ctor):
fields = []
for i in xrange(len(ctor.__types__)):
nm = ctor.__slots__[i]
t = ctor.__types__[i]
if _deferred_type_parses is None \
and not isinstance(t, (Type, TForward)):
t = parse_type(t)
field = Field(t)
add_extrinsic(Name, field, nm)
fields.append(field)
form = Ctor(fields)
add_extrinsic(Name, form, ctor.__name__)
add_extrinsic(TrueRepresentation, form, ctor)
add_extrinsic(FormSpec, ctor, form)
del ctor.__types__
return form
def _dt_form(dt, tvs):
if tvs is None:
tvs = {}
def do_ctor(ctor):
form = _ctor_form(ctor)
if _deferred_type_parses is not None:
ctorForms = _deferred_type_parses.setdefault(dt, [])
ctorForms.append(form)
return form
ctors = in_env(TVARS, tvs,
lambda: in_env(NEWTYPEVARS, None,
lambda: map(do_ctor, dt.ctors)))
valueType = dt._opts.get('value', False)
gc = not valueType # temp
opts = DTOpts(valueType, gc)
del dt._opts
form = DataType(ctors, tvs.values(), opts)
add_extrinsic(Name, form, dt.__name__)
add_extrinsic(TrueRepresentation, form, dt)
add_extrinsic(FormSpec, dt, form)
return form
def _restore_forms():
for ctorName in CTORS.itervalues():
_dt_form(DATATYPES[ctorName], None)
_restore_forms()
# Type representations
TypeVar = DT('TypeVar')
PrimType, PInt, PFloat, PStr, PChar, PBool = ADT('PrimType',
'PInt', 'PFloat', 'PStr', 'PChar', 'PBool')
ArrayKind, AGC, ABoxed, ARaw = ADT('ArrayKind',
'AGC', 'ABoxed', 'ARaw', value=True)
ParamMeta = DT('ParamMeta', ('held', bool))
FuncMeta = DT('FuncMeta', ('params', [ParamMeta]),
('requiredEnvs', ['*Env']),
('envParam', bool))
def plain_meta(params):
return FuncMeta(params, [], True)
def basic_meta(params):
return FuncMeta(params, [], False)
def copy_meta(meta):
return FuncMeta(map(copy_param_meta, meta.params),
meta.requiredEnvs[:], meta.envParam)
def plain_param_meta():
return ParamMeta(False)
def copy_param_meta(pm):
return ParamMeta(pm.held)
def metas_equal(m1, m2):
for p1, p2 in ezip(m1.params, m2.params):
if p1.held != p2.held:
return False
return m1.envParam == m2.envParam
Type, TVar, TPrim, TTuple, TFunc, TData, TCtor, TArray, TWeak \
= ADT('Type',
'TVar', ('typeVar', '*TypeVar'),
'TPrim', ('primType', PrimType),
'TTuple', ('tupleTypes', ['Type']),
'TFunc', ('paramTypes', ['Type']), ('result', 'Result(Type)'),
('meta', FuncMeta),
'TData', ('data', '*DataType'), ('appTypes', ['Type']),
'TCtor', ('ctor', '*Ctor'), ('appTypes', ['Type']),
'TArray', ('elemType', 'Type'), ('arrayKind', ArrayKind),
'TWeak', ('refType', 'Type'))
Result, Ret, Void, Bottom = ADT('Result',
'Ret', ('type', 't'),
'Void',
'Bottom')
def TInt():
return TPrim(PInt())
def TFloat():
return TPrim(PFloat())
def TBool():
return TPrim(PBool())
def TStr():
return TPrim(PStr())
def TChar():
return TPrim(PChar())
def parse_new_type(t, tvars):
return in_env(NEWTYPEVARS, None, lambda:
in_env(TVARS, tvars, lambda: parse_type(t)))
def vanilla_tdata(form):
return TData(form, map(TVar, form.tvars))
def parse_type(t):
if type(t) is type and issubclass(t, Structured):
form = extrinsic(FormSpec, t)
if isinstance(form, Ctor):
form = extrinsic(FormSpec, SUPERS[t])
return vanilla_tdata(form)
elif isinstance(t, basestring):
toks = list(tokenize_type(t))
try:
ct = consume_type(toks)
assert not toks, "%s remaining" % (toks,)
except AssertionError, e:
e.args = ('%s while parsing %r' % (e.args[0], repr(t),),)
raise
return ct
elif t is int:
return TInt()
elif t is float:
return TFloat()
elif t is str:
return TStr()
elif t is bool:
return TBool()
elif t is None:
return None
elif isinstance(t, tuple):
return TTuple(map(parse_type, t))
elif isinstance(t, list):
assert len(t) == 1
elemT = parse_type(t[0])
return TArray(elemT, AGC())
elif isinstance(t, set):
assert len(t) == 1
return _apply_set_type(parse_type(list(t)[0]))
elif isinstance(t, dict):
assert len(t) == 1
[(key, val)] = t.iteritems()
return _apply_dict_type(parse_type(key), parse_type(val))
elif t is type or t is object or t is file:
# MAGIC!
return t
assert False, "Unknown type repr of type %r: %r" % (type(t), t)
types_by_name = dict(str=TStr, int=TInt, float=TFloat, bool=TBool)
array_kind_literals = {'[': AGC, 'r[': ARaw, 'x[': ABoxed}
def _type_by_name(t):
if len(t) == 1:
tvars = env(TVARS)
tvar = tvars.get(t)
if tvar is None:
assert have_env(NEWTYPEVARS), "Tried to create TypeVar %s" % t
tvar = TypeVar()
add_extrinsic(Name, tvar, t)
tvars[t] = tvar
return TVar(tvar)
elif t in DATATYPES:
return vanilla_tdata(extrinsic(FormSpec, DATATYPES[t]))
elif t in types_by_name:
return types_by_name[t]()
elif t == 'void':
return None
else:
return TForward(t, [])
def consume_type(toks):
isParamList = False
paramMetas = None
tok = toks.pop(0)
if tok == '*':
t = TWeak(consume_type(toks))
elif tok == 't(' or tok == '(':
# tuple literal or parameter list
isParamList = (tok == '(')
if isParamList:
paramMetas = []
ts = []
while toks[0] != ')':
ts.append(consume_type(toks))
peek = toks[0]
# consume trailing `held` if present
pMeta = None
if isParamList and peek == 'held':
toks.pop(0)
pMeta = ParamMeta(True)
peek = toks[0]
# consume comma if next
if peek == ',':
toks.pop(0)
else:
assert peek == ')', "Expected comma or ), not " + peek
if isParamList:
paramMetas.append(pMeta or plain_param_meta())
toks.pop(0)
t = TTuple(ts)
elif tok in ('[', 'r[', 'x['):
t = TArray(consume_type(toks), array_kind_literals[tok]())
assert toks.pop(0) == ']', 'Unbalanced %s]' % (tok,)
elif tok[0] in slashW:
t = _type_by_name(tok)
if toks and toks[0] == '(':
# application
apps = []
toks.pop(0)
while toks[0] != ')':
apps.append(consume_type(toks))
if toks[0] == ',':
toks.pop(0)
else:
assert toks[0]==')', "Expected comma or ), not " + toks[0]
toks.pop(0)
assert not t.appTypes or len(t.appTypes)==len(apps),"Bad app count"
t.appTypes = apps
else:
assert False, "Unexpected " + tok
# might be followed by infix arrow
if toks and toks[0] == '->':
toks.pop(0)
if isParamList:
params = t.tupleTypes
else:
params, paramMetas = [t], [plain_param_meta()]
if len(params) == 1 and params[0] is None:
params, paramMetas = [], []
meta = plain_meta(paramMetas)
t = TFunc(params, consume_result(toks), meta)
if toks and toks[0] == 'noenv':
toks.pop(0)
meta.envParam = False
assert len(t.paramTypes) == len(t.meta.params), "Bad meta params"
return t
def consume_result(toks):
tok = toks[0]
if tok == 'void':
toks.pop(0)
return Void()
elif tok == 'noreturn':
toks.pop(0)
return Bottom()
else:
return Ret(consume_type(toks))
slashW = 'abcdefghjijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
def tokenize_type(s):
word = ''
lastDash = False
for c in s:
if lastDash:
assert c == '>', "Broken arrow"
yield '->'
lastDash = False
elif c in slashW:
word += c
elif c == '(' and word == 't':
yield 't('
word = ''
elif c == '[' and word in 'rx':
yield word+c
word = ''
else:
if word:
yield word
word = ''
if c in '*,:(){}[]':
yield c
elif c == '-':
lastDash = True
elif c != ' ':
assert False, "Unexpected char in " + s
assert not lastDash
if word:
yield word
TForward = DT('TForward', ('name', str), ('appTypes', [Type]))
def t_ADT(adt):
return vanilla_tdata(extrinsic(FormSpec, adt))
def t_DT(dt):
return t_ADT(SUPERS[dt])
def _apply_list_type(t):
listT = parse_type('List')
return TData(listT, [t])
def _apply_set_type(t):
setT = parse_type('Set')
return TData(setT, [t])
def _apply_dict_type(k, v):
dictT = parse_type('Dict')
return TData(dictT, [k, v])
# Parse the types in TypeVar, Type fields
def _parse_deferred():
global _deferred_type_parses
for dtForm, ctorForms in _deferred_type_parses.iteritems():
tvars = {}
for ctor in ctorForms:
def parse():
for field in ctor.fields:
field.type = parse_type(field.type)
in_env(NEWTYPEVARS, None, lambda: in_env(TVARS, tvars, parse))
extrinsic(FormSpec, dtForm).tvars = tvars.values()
_deferred_type_parses = None
_parse_deferred()
def derive_copied_ctor_type(t, old_dt, new_dt, dtSubsts, tvars):
substNames = dict((extrinsic(Name, dt), repl)
for dt, repl in dtSubsts.iteritems())
def _derive_tvar(tv):
if tv not in tvars:
orig = tv
tv = TypeVar()
add_extrinsic(Name, tv, extrinsic(Name, orig))
tvars[orig] = tv
else:
tv = tvars[tv]
return TVar(tv)
def _derive_data(dt, ts):
if dt is old_dt:
dt = new_dt
elif dt in dtSubsts:
dt = dtSubsts[dt]
return TData(dt, map(copy, ts))
def copy(t):
if isinstance(t, TForward):
nm = t.name
if nm == old_dt.__name__:
nm = new_dt.__name__
elif nm in substNames:
nm = substNames[dt].__name__
return TForward(nm, map(copy, t.appTypes))
return match(t,
('TVar(tv)', _derive_tvar),
('TPrim(PInt())', TInt),
('TPrim(PFloat())', TFloat),
('TPrim(PBool())', TBool),
('TPrim(PStr())', TStr),
('TPrim(PChar())', TChar),
('TTuple(ts)', lambda ts: TTuple(map(copy, ts))),
('TFunc(args, ret, meta)', lambda args, ret:
TFunc(map(copy, args), copy_result(ret), copy_meta(meta))),
('TData(data, apps)', _derive_data),
('TArray(t, k)', lambda t, k: TArray(copy(t), copy_array_kind(k))),
('TWeak(t)', lambda t: TWeak(copy(t))))
def copy_result(r):
return match(r, ('Ret(t)', lambda t: Ret(copy(t))),
('Void()', Void),
('Bottom()', Bottom))
def copy_array_kind(k):
return match(k, ('AGC()', AGC),
('ABoxed()', ABoxed),
('ARaw()', ARaw))
return copy(t)
# Typeclasses
TypeClassInfo = DT('TypeClassInfo', ('name', str),
('spec', {str: (int, Type)}),
('impls', {'*DataType': ['a']}))
def new_typeclass(name, *args):
spec = {}
impls = {}
info = TypeClassInfo(name, spec, impls)
def make_impl(i, specT, nm):
# Limitation: First argument :: 'a to do the lookup
# Really ought to use specT to figure it out
def lookup(*args):
assert len(args) == len(specT.paramTypes)
t = type(args[0])
if t not in impls:
t = SUPERS[t]
tnm = t.__name__
assert t in impls, "%s is not a part of %s" % (tnm, name)
func = impls[t][i]
assert func is not None, "%s.%s.%s has no impl" % (name, tnm, nm)
return func(*args)
lookup.__name__ = '_' + nm + '_typeclass_lookup'
return lookup
for i, entry in enumerate(args):
if len(entry) == 3:
nm, t, default = entry
else:
nm, t = entry
default = None
assert nm not in spec
tvars = {}
t = parse_new_type(t, tvars)
assert match(t.paramTypes[0], ("TVar(tv)",lambda tv: tv is tvars['a']))
spec[nm] = (i, t, default)
setattr(info, nm, make_impl(i, t, nm))
return info
def impl(cls, targetT):
def decorator(func):
fnm = func.__name__
suffix = '_' + targetT.__name__
assert fnm.endswith(suffix), "%s impl for %s must be named *%s" % (
cls.name, targetT, suffix)
fnm = fnm[:-len(suffix)]
assert fnm in cls.spec, "Unknown impl method: %s" % (fnm,)
if targetT not in cls.impls:
default_impl(cls, targetT)
i, specT, default = cls.spec[fnm]
cls.impls[targetT][i] = func
return None
return decorator
def default_impl(cls, targetT):
assert targetT not in cls.impls, "default_impl() would clobber existing"
vtable = [None] * len(cls.spec)
for i, t, default in cls.spec.itervalues():
vtable[i] = default
cls.impls[targetT] = vtable
# Global options
GenOpts = DT('GenOpts', ('color', None),
('profile', bool),
('dumpViews', bool),
('dumpSource', bool),
('dumpTypes', bool),
('dumpInsts', bool),
('dumpBlocks', bool))
GENOPTS = new_env('GENOPTS', GenOpts)
def default_gen_opts():
return GenOpts(None, False, False, False, False, False, False)
import re
_col_re = re.compile(r'\^(\w+)\^?')
del re
_col_shorts = {'N': 'Normal', 'DG': 'DarkGray', 'LG': 'LightGray'}
def col(c, s):
colors = have_env(GENOPTS) and env(GENOPTS).color
if colors:
c = _col_shorts.get(c, c)
s = '%s%s%s' % (getattr(colors, c), s, colors.Normal)
return s
def fmtcol(s, *args):
colors = have_env(GENOPTS) and env(GENOPTS).color
if colors:
def colorize(m):
c = m.group(1)
c = _col_shorts.get(c, c)
return getattr(colors, c)
s = _col_re.sub(colorize, s)
else:
s = _col_re.sub('', s)
return s.format(*args)
def mark(s):
return col('Red', s)
def colorless(f):
opts = env(GENOPTS)
hadColor = opts.color
opts.color = None
ret = f()
opts.color = hadColor
return ret
# Pretty printing
# (naive quadratic version)
PrettyPrinted = new_extrinsic('PrettyPrinted', None)
def pretty_brief(name, o):
if name == 'Bind':
o = o.target
name = type(o).__name__
if name == 'Builtin':
return col('Yellow', extrinsic(Name, o))
elif name == 'Ctor':
return fmtcol('^Brown{0}^N', extrinsic(Name, o))
elif name in ('Var', 'GlobalVar'):
return "'%r" % (o,)
elif name == 'Lit':
o = o.literal
name = type(o).__name__
if name == 'IntLit':
return col('Cyan', 'i%d' % (o.val,))
elif name == 'FloatLit':
return col('Cyan', 'i%f' % (o.val,))
elif name == 'StrLit':
return fmtcol('^Cyan^s{0!r}^N', o.val)
else:
assert False
elif name == 'TPrim' or name == 'CPrim':
c = 'Cyan' if name == 'TPrim' else 'LightCyan'
return col(c, repr(o.primType))
elif name == 'TupleLit':
return 't%r' % (tuple(o.vals),)
elif name == 'DataType':
return col('Brown', extrinsic(Name, o))
elif name == 'Ctor':
return col('Yellow', extrinsic(Name, o))
return None
def short_id(o):
return fmtcol('^LG@x{0:x}^N', id(o) % 0xfffff)
def __repr__(o):
if not in_extrinsic_scope(PrettyPrinted):
return scope_extrinsic(PrettyPrinted, lambda: repr(o))
t = type(o)
name = t.__name__
if has_extrinsic(PrettyPrinted, o):
if has_extrinsic(Name, o):
name = fmtcol('{0} ^Green"{1}"^N', name, extrinsic(Name, o))
return '<%s %s>' % (name, short_id(o))
add_extrinsic(PrettyPrinted, o, None)
brief = pretty_brief(name, o)
if brief is not None:
return brief
if has_extrinsic(Name, o):
name = fmtcol('{0} ^Green"{1}"^N {2}', name, extrinsic(Name, o),
short_id(o))
if len(t.__slots__) > 1:
params = (repr(getattr(o, s)) for s in t.__slots__[:-1])
comma = col('Blue', ', ')
name = fmtcol('{0}^Blue(^N{1}^Blue)^N', name, comma.join(params))
return name
Structured.__repr__ = __repr__
# Type annotations
def annot(t):
return lambda func: func
# Matching
named_match_dispatch = {}
def _match_try_ctor(atom, ast):
reqCtor, args = ast['ctor'], ast['args']
dt = atom.__class__
foundCtor = CTORS.get(dt)
if foundCtor is not None and foundCtor == reqCtor:
slots = dt.__slots__
n = len(slots) - 1
assert len(args) == n, "Ctor %s takes %d args: %s (%d given)" % (
ctor, n, ', '.join(slots[:n]), len(args))
# Found a matching constructor; now match its args recursively
# Unlike the main match loop, if any fail here everything fails
ctor_args = []
for attrNm, arg in orig_zip(slots, args):
sub_args = match_try(getattr(atom, attrNm), arg)
if sub_args is None:
return None
ctor_args += sub_args
return ctor_args
named_matcher = named_match_dispatch.get(reqCtor)
if named_matcher is not None:
return named_matcher(atom, args)
return None
def match_try(atom, ast):
t = ast['t']
if t == 'ctor':
return _match_try_ctor(atom, ast)
elif t == 'name':
name = ast['name']
if name == 'True':
assert isinstance(atom, bool)
return [] if atom else None
elif name == 'False':
assert isinstance(atom, bool)
return None if atom else []
return [(name, atom)]
elif t == 'wildcard':
return []
elif t == 'const':
return [] if ast['val'] == atom else None
elif t is tuple or t is list:
if not isinstance(atom, t) or len(atom) != len(ast['pats']):
return None
tuple_args = []
for a, node in orig_zip(atom, ast['pats']):
args = match_try(a, node)
if args is None:
return None
tuple_args += args
return tuple_args
elif t == 'or':
# First that doesn't fail
for case in ast['pats']:
or_args = match_try(atom, case)
if or_args is not None:
return or_args
return None
elif t == 'and':
and_args = []
for case in ast['pats']:
case_args = match_try(atom, case)
if case_args is None:
return None
and_args += case_args
return and_args
elif t == 'capture':
# capture right side
capture_args = match_try(atom, ast['pat'])
if capture_args is None:
return None
capture_args.insert(0, (ast['name'], atom))
return capture_args
assert False, "Unknown match case: %s" % ast
match_asts = {}
def match(atom, *cases):
# Block form
if len(cases) == 0:
return BlockMatcher(atom)
elif len(cases) == 1 and isinstance(cases[0], basestring):
# shortcut for single-case
cases = ((cases[0], lambda *ss: ss[0] if len(ss) == 1 else ss),)
# Try all the cases, find the first that doesn't fail
for (case, f) in cases:
call_args = match_try(atom, _get_match_case(case))
if call_args is not None:
return f(*(v for k, v in call_args))
case_list = ''.join('* %s -> %s\n' % (p, f) for p, f in cases)
assert False, "Match failed.\nVALUE:\n%r\nCASES:\n%s" % (atom, case_list)
class BlockMatcher(object):
badNames = ['ret', 'result']
def __init__(self, atom):
self._atom = atom
self._cases = []
def ret(self, result):
self.success = result
def result(self):
if not hasattr(self, 'success'):
case_list = ''.join('* %s\n' % p for p in self._cases)
assert False, "Match failed.\nVALUE:\n%r\nCASES:\n%s" % (
self._atom, case_list)
return self.success
def __call__(self, pat):
self._cases.append(pat)
args = match_try(self._atom, _get_match_case(pat))
if args is None:
return False
for name, val in args:
assert name not in BlockMatcher.badNames, "Bad arg name: %s" % name
assert not hasattr(self, name), "Duplicate name %s" % name
setattr(self, name, val)
return True
def matches(atom, case):
return match_try(atom, _get_match_case(case)) is not None
def _get_match_case(pat):
ast = match_asts.get(pat)
if ast is None:
match_asts[pat] = ast = parse_match_pat(pat)
return ast
def parse_match_pat(pat):
try:
toks = tokenize_match_pat(pat)
ast, eof = consume_match_pat(toks.next(), toks)
except AssertionError, e:
rest = ' '.join(repr(t) for t in toks)
e.args = ('%s while parsing %r (rest: %s)' % (e.args[0], pat, rest),)
raise
assert eof == '$EOF', "Trailing characters in " + pat
return ast
def _next_token(rest):
try:
return rest.next()
except StopIteration:
return '$EOF'
def consume_match_pat(first, rest):
tok = _next_token(rest)
if first == '(' or first == '[':
ending = ')' if first == '(' else ']'
pats = []
while tok != ending:
pat, tok = consume_match_pat(tok, rest)