@@ -49,18 +49,18 @@ def enforce(self, val):
49
49
return False # TODO: when we actually have nominal (abstract) types, do some check here
50
50
51
51
# TODO: Make into a higher-kinded type? Maybe that's just a headache?
52
- class ListType (Type ):
52
+ class List (Type ):
53
53
def __init__ (self , elem_ty ):
54
54
self .elem_ty = elem_ty
55
55
56
56
def substitute (self , substitution ):
57
- return ListType (self .elem_ty .substitute (substitution ))
57
+ return List (self .elem_ty .substitute (substitution ))
58
58
59
59
def __str__ (self ):
60
60
return '[%s]' % self .elem_ty
61
61
62
62
def __eq__ (self , other ):
63
- return isinstance (other , ListType ) and other .elem_ty == self .elem_ty
63
+ return isinstance (other , List ) and other .elem_ty == self .elem_ty
64
64
65
65
def enforce (self , val ):
66
66
if type (val ) != list :
@@ -69,21 +69,21 @@ def enforce(self, val):
69
69
return [self .elem_ty .enforce (x ) for x in val ] # This could be slooooow
70
70
71
71
72
- class DictType (Type ):
72
+ class Dict (Type ):
73
73
def __init__ (self , key_ty , value_ty ):
74
74
self .key_ty = key_ty
75
75
self .value_ty = value_ty
76
76
77
77
def substitute (self , substitution ):
78
- return DictType (key_ty = self .key_ty .substitute (substitution ),
78
+ return Dict (key_ty = self .key_ty .substitute (substitution ),
79
79
value_ty = self .value_ty .substitute (substitution ))
80
80
81
81
82
82
def __str__ (self ):
83
83
return '{%s:%s}' % (self .key_ty , self .value_ty )
84
84
85
85
def __eq__ (self , other ):
86
- return isinstance (other , DictType ) and other .key_ty == self .key_ty and other .value_ty == self .value_ty
86
+ return isinstance (other , Dict ) and other .key_ty == self .key_ty and other .value_ty == self .value_ty
87
87
88
88
def enforce (self , val ):
89
89
if type (val ) != dict :
@@ -92,7 +92,7 @@ def enforce(self, val):
92
92
return dict ([(self .key_ty .enforce (key ), self .value_ty .enforce (value )) for key , value in val .items ()])
93
93
94
94
95
- class TypeVariable (Type ):
95
+ class Variable (Type ):
96
96
def __init__ (self , name ):
97
97
self .name = name
98
98
@@ -106,9 +106,9 @@ def __str__(self):
106
106
return '?%s' % self .name
107
107
108
108
def __eq__ (self , other ):
109
- return isinstance (other , TypeVariable ) and other .name == self .name
109
+ return isinstance (other , Variable ) and other .name == self .name
110
110
111
- class FunctionType (Type ):
111
+ class Function (Type ):
112
112
def __init__ (self , arg_types , return_type , vararg_type = None , kwonly_arg_types = None , kwarg_type = None ):
113
113
self .arg_types = arg_types
114
114
self .return_type = return_type
@@ -117,7 +117,7 @@ def __init__(self, arg_types, return_type, vararg_type=None, kwonly_arg_types=No
117
117
self .kwonly_arg_types = kwonly_arg_types
118
118
119
119
def substitute (self , substitution ):
120
- return FunctionType (arg_types = [ty .substitute (substitution ) for ty in self .arg_types ],
120
+ return Function (arg_types = [ty .substitute (substitution ) for ty in self .arg_types ],
121
121
return_type = self .return_type .substitute (substitution ),
122
122
vararg_type = None if self .vararg_type is None else self .vararg_type .substitute (substitution ),
123
123
kwonly_arg_types = None if self .kwonly_arg_types is None else [ty .substitute (substitution ) for ty in self .kwonly_arg_types ],
@@ -174,26 +174,26 @@ def __str__(self):
174
174
return '%s -> %s' % (argument_list , self .return_type )
175
175
176
176
def __eq__ (self , other ):
177
- return isinstance (other , FunctionType ) and other .vararg_type == self .vararg_type and other .kwarg_type == self .kwarg_type \
177
+ return isinstance (other , Function ) and other .vararg_type == self .vararg_type and other .kwarg_type == self .kwarg_type \
178
178
and other .arg_types == self .arg_types and other .kwonly_arg_types == self .kwonly_arg_types
179
179
180
- class TypeApplication (Type ):
180
+ class Application (Type ):
181
181
def __init__ (self , fn , args ):
182
182
self .fn = fn
183
183
self .args = args
184
184
185
185
def substitute (self , substitution ):
186
- return TypeApplication (self .fn .substitute (substitution ), [ty .substitute (substitution ) for ty in self .args ])
186
+ return Application (self .fn .substitute (substitution ), [ty .substitute (substitution ) for ty in self .args ])
187
187
188
- class UnionType (Type ):
188
+ class Union (Type ):
189
189
def __init__ (self , types ):
190
190
self .types = types
191
191
192
192
def __str__ (self ):
193
193
return '|' .join ([str (ty ) for ty in self .types ])
194
194
195
195
def __eq__ (self , other ):
196
- return isinstance (other , UnionType ) and self .types == other .types
196
+ return isinstance (other , Union ) and self .types == other .types
197
197
198
198
def enforce (self , val ):
199
199
for ty in self .types :
@@ -204,7 +204,7 @@ def enforce(self, val):
204
204
205
205
raise TypeError ('Type check failed: %s does not have type %s' % (val , self ))
206
206
207
- class ObjectType (Type ):
207
+ class Object (Type ):
208
208
def __init__ (self , self_ty_name , ** field_tys ):
209
209
self .self_ty_name = self_ty_name
210
210
self .field_tys = field_tys
@@ -213,7 +213,7 @@ def __str__(self):
213
213
return 'object(%s)' % ',' .join ([self .self_ty_name ] + ['%s:%s' % (name , ty ) for name , ty in self .field_tys .items ()])
214
214
215
215
def __eq__ (self , other ):
216
- return isinstance (other , ObjectType ) and self .self_ty_name == other .self_ty_name and self .field_tys == other .field_tys
216
+ return isinstance (other , Object ) and self .self_ty_name == other .self_ty_name and self .field_tys == other .field_tys
217
217
218
218
def enforce (self , val ):
219
219
# TODO: bind the self type
@@ -227,12 +227,12 @@ def enforce(self, val):
227
227
setattr (newval , field , ty .enforce (getattr (val , field )))
228
228
return newval
229
229
230
- class AnyType (Type ):
230
+ class Any (Type ):
231
231
def __str__ (self ):
232
232
return '??'
233
233
234
234
def __eq__ (self , other ):
235
- return isinstance (other , AnyType )
235
+ return isinstance (other , Any )
236
236
237
237
def substitute (self , substitution ):
238
238
return self
@@ -248,7 +248,7 @@ def fresh(prefix=None):
248
248
global used_vars
249
249
prefix = prefix or 'X'
250
250
used_vars [prefix ] = used_vars [prefix ] + 1
251
- return TypeVariable (prefix + str (used_vars [prefix ]))
251
+ return Variable (prefix + str (used_vars [prefix ]))
252
252
253
253
# TODO: Give these names a definition that they can be unfolded to.
254
254
bool_t = NamedType ('bool' )
@@ -260,6 +260,6 @@ def fresh(prefix=None):
260
260
str_t = NamedType ('str' )
261
261
unicode_t = NamedType ('unicode' )
262
262
263
- numeric_t = UnionType ([int_t , long_t , complex_t , float_t ])
263
+ numeric_t = Union ([int_t , long_t , complex_t , float_t ])
264
264
265
- any_t = AnyType ()
265
+ any_t = Any ()
0 commit comments