Skip to content

Commit 4625b4e

Browse files
committed
Fresh new name
1 parent ae78d2d commit 4625b4e

13 files changed

+107
-113
lines changed
File renamed without changes.

typelanguage/types.py renamed to rightarrow/annotations.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,18 @@ def enforce(self, val):
4949
return False # TODO: when we actually have nominal (abstract) types, do some check here
5050

5151
# TODO: Make into a higher-kinded type? Maybe that's just a headache?
52-
class ListType(Type):
52+
class List(Type):
5353
def __init__(self, elem_ty):
5454
self.elem_ty = elem_ty
5555

5656
def substitute(self, substitution):
57-
return ListType(self.elem_ty.substitute(substitution))
57+
return List(self.elem_ty.substitute(substitution))
5858

5959
def __str__(self):
6060
return '[%s]' % self.elem_ty
6161

6262
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
6464

6565
def enforce(self, val):
6666
if type(val) != list:
@@ -69,21 +69,21 @@ def enforce(self, val):
6969
return [self.elem_ty.enforce(x) for x in val] # This could be slooooow
7070

7171

72-
class DictType(Type):
72+
class Dict(Type):
7373
def __init__(self, key_ty, value_ty):
7474
self.key_ty = key_ty
7575
self.value_ty = value_ty
7676

7777
def substitute(self, substitution):
78-
return DictType(key_ty=self.key_ty.substitute(substitution),
78+
return Dict(key_ty=self.key_ty.substitute(substitution),
7979
value_ty=self.value_ty.substitute(substitution))
8080

8181

8282
def __str__(self):
8383
return '{%s:%s}' % (self.key_ty, self.value_ty)
8484

8585
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
8787

8888
def enforce(self, val):
8989
if type(val) != dict:
@@ -92,7 +92,7 @@ def enforce(self, val):
9292
return dict([(self.key_ty.enforce(key), self.value_ty.enforce(value)) for key, value in val.items()])
9393

9494

95-
class TypeVariable(Type):
95+
class Variable(Type):
9696
def __init__(self, name):
9797
self.name = name
9898

@@ -106,9 +106,9 @@ def __str__(self):
106106
return '?%s' % self.name
107107

108108
def __eq__(self, other):
109-
return isinstance(other, TypeVariable) and other.name == self.name
109+
return isinstance(other, Variable) and other.name == self.name
110110

111-
class FunctionType(Type):
111+
class Function(Type):
112112
def __init__(self, arg_types, return_type, vararg_type=None, kwonly_arg_types=None, kwarg_type=None):
113113
self.arg_types = arg_types
114114
self.return_type = return_type
@@ -117,7 +117,7 @@ def __init__(self, arg_types, return_type, vararg_type=None, kwonly_arg_types=No
117117
self.kwonly_arg_types = kwonly_arg_types
118118

119119
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],
121121
return_type = self.return_type.substitute(substitution),
122122
vararg_type = None if self.vararg_type is None else self.vararg_type.substitute(substitution),
123123
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):
174174
return '%s -> %s' % (argument_list, self.return_type)
175175

176176
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 \
178178
and other.arg_types == self.arg_types and other.kwonly_arg_types == self.kwonly_arg_types
179179

180-
class TypeApplication(Type):
180+
class Application(Type):
181181
def __init__(self, fn, args):
182182
self.fn = fn
183183
self.args = args
184184

185185
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])
187187

188-
class UnionType(Type):
188+
class Union(Type):
189189
def __init__(self, types):
190190
self.types = types
191191

192192
def __str__(self):
193193
return '|'.join([str(ty) for ty in self.types])
194194

195195
def __eq__(self, other):
196-
return isinstance(other, UnionType) and self.types == other.types
196+
return isinstance(other, Union) and self.types == other.types
197197

198198
def enforce(self, val):
199199
for ty in self.types:
@@ -204,7 +204,7 @@ def enforce(self, val):
204204

205205
raise TypeError('Type check failed: %s does not have type %s' % (val, self))
206206

207-
class ObjectType(Type):
207+
class Object(Type):
208208
def __init__(self, self_ty_name, **field_tys):
209209
self.self_ty_name = self_ty_name
210210
self.field_tys = field_tys
@@ -213,7 +213,7 @@ def __str__(self):
213213
return 'object(%s)' % ','.join([self.self_ty_name] + ['%s:%s' % (name, ty) for name, ty in self.field_tys.items()])
214214

215215
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
217217

218218
def enforce(self, val):
219219
# TODO: bind the self type
@@ -227,12 +227,12 @@ def enforce(self, val):
227227
setattr(newval, field, ty.enforce(getattr(val, field)))
228228
return newval
229229

230-
class AnyType(Type):
230+
class Any(Type):
231231
def __str__(self):
232232
return '??'
233233

234234
def __eq__(self, other):
235-
return isinstance(other, AnyType)
235+
return isinstance(other, Any)
236236

237237
def substitute(self, substitution):
238238
return self
@@ -248,7 +248,7 @@ def fresh(prefix=None):
248248
global used_vars
249249
prefix = prefix or 'X'
250250
used_vars[prefix] = used_vars[prefix] + 1
251-
return TypeVariable(prefix + str(used_vars[prefix]))
251+
return Variable(prefix + str(used_vars[prefix]))
252252

253253
# TODO: Give these names a definition that they can be unfolded to.
254254
bool_t = NamedType('bool')
@@ -260,6 +260,6 @@ def fresh(prefix=None):
260260
str_t = NamedType('str')
261261
unicode_t = NamedType('unicode')
262262

263-
numeric_t = UnionType([int_t, long_t, complex_t, float_t])
263+
numeric_t = Union([int_t, long_t, complex_t, float_t])
264264

265-
any_t = AnyType()
265+
any_t = Any()

typelanguage/constraintgen.py renamed to rightarrow/constraintgen.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import copy
44
from collections import namedtuple
55

6-
from typelanguage import types
6+
from rightarrow.annotations import *
77

88
class Constraint(object):
99
"A type constraint of the form `S <: T`"
@@ -81,15 +81,15 @@ def fn_env(arguments):
8181

8282
for arg in arguments.args:
8383
if isinstance(arg, ast.Name) and isinstance(arg.ctx, ast.Param):
84-
new_env[arg.id] = types.fresh() # TODO: ??
84+
new_env[arg.id] = fresh() # TODO: ??
8585
else:
8686
raise Exception('Arg is not a name in Param context!? %s' % arg)
8787

8888
if arguments.vararg:
89-
new_env[arguments.vararg] = types.fresh() # TODO: sub/superty of list
89+
new_env[arguments.vararg] = fresh() # TODO: sub/superty of list
9090

9191
if arguments.kwarg:
92-
new_env[arguments.kwarg] = types.fresh() # TODO: sub/superty of dict
92+
new_env[arguments.kwarg] = fresh() # TODO: sub/superty of dict
9393

9494
return new_env
9595

@@ -99,7 +99,7 @@ def union(left, right):
9999
elif right is None:
100100
return left
101101
else:
102-
return types.UnionType([right, left])
102+
return Union([right, left])
103103

104104
def constraints_stmt(stmt, env=None):
105105
"""
@@ -122,8 +122,8 @@ def constraints_stmt(stmt, env=None):
122122
constraints += cs.constraints
123123
return_type = union(return_type, cs.return_type)
124124

125-
env[stmt.name] = types.FunctionType(arg_types=[arg_env[arg.id] for arg in stmt.args.args],
126-
return_type=return_type)
125+
env[stmt.name] = Function(arg_types=[arg_env[arg.id] for arg in stmt.args.args],
126+
return_type=return_type)
127127

128128
return ConstrainedEnv(env=env, constraints=constraints)
129129

@@ -136,8 +136,8 @@ def constraints_stmt(stmt, env=None):
136136
expr_result = constraints_expr(stmt.value, env=env)
137137
return ConstrainedEnv(env=env, constraints=expr_result.constraints, return_type=expr_result.type)
138138
else:
139-
result = types.fresh()
140-
return ConstrainedEnv(env=env, constraints=[Constraint(subtype=result, supertype=types.NamedType('NoneType'))])
139+
result = fresh()
140+
return ConstrainedEnv(env=env, constraints=[Constraint(subtype=result, supertype=NamedType('NoneType'))])
141141

142142
elif isinstance(stmt, ast.Assign):
143143
if len(stmt.targets) > 1:
@@ -149,7 +149,7 @@ def constraints_stmt(stmt, env=None):
149149
# For an assignment, we actually generate a fresh variable so that it can be the union of all things assigned
150150
# to it. We do not do any typestate funkiness.
151151
if target not in env:
152-
env[target] = types.fresh()
152+
env[target] = fresh()
153153

154154
return ConstrainedEnv(env=env,
155155
constraints = expr_result.constraints + [Constraint(subtype=expr_result.type,
@@ -163,7 +163,7 @@ def constraints_expr(expr, env=None):
163163

164164
if isinstance(expr, ast.Name) and isinstance(expr.ctx, ast.Load):
165165
if expr.id in ['False', 'True']: # Unlike other literals, these are actually just global identifiers
166-
return ConstrainedType(type=types.bool_t)
166+
return ConstrainedType(type=bool_t)
167167
elif expr.id in env:
168168
return ConstrainedType(type=env[expr.id])
169169
else:
@@ -172,31 +172,31 @@ def constraints_expr(expr, env=None):
172172
elif isinstance(expr, ast.Num):
173173
# The python ast module already chose the type of the num
174174
if isinstance(expr.n, int):
175-
return ConstrainedType(type=types.int_t)
175+
return ConstrainedType(type=int_t)
176176
elif isinstance(expr.n, long):
177-
return ConstrainedType(type=types.long_t)
177+
return ConstrainedType(type=long_t)
178178
elif isinstance(expr.n, float):
179-
return ConstrainedType(type=types.float_t)
179+
return ConstrainedType(type=float_t)
180180
elif isinstance(expr.n, complex):
181-
return ConstrainedType(type=types.complex_t)
181+
return ConstrainedType(type=complex_t)
182182

183183
elif isinstance(expr, ast.Str):
184-
return ConstrainedType(type=types.str_t)
184+
return ConstrainedType(type=str_t)
185185

186186
elif isinstance(expr, ast.List):
187-
return ConstrainedType(type=types.ListType(elem_ty=types.fresh()))
187+
return ConstrainedType(type=List(elem_ty=fresh()))
188188

189189
elif isinstance(expr, ast.BinOp):
190190
left = constraints_expr(expr.left, env=env)
191191
right = constraints_expr(expr.right, env=env)
192-
ty = types.fresh()
192+
ty = fresh()
193193

194194
if isinstance(expr.op, ast.Mult):
195195
# TODO: consider whether all types should match (forces coercions to be explicit; a good thing)
196196
# Note: though strings and bools can be used in mult, forget it!
197-
op_constraints = [Constraint(subtype=left.type, supertype=types.numeric_t),
198-
Constraint(subtype=right.type, supertype=types.numeric_t),
199-
Constraint(subtype=ty, supertype=types.numeric_t)]
197+
op_constraints = [Constraint(subtype=left.type, supertype=numeric_t),
198+
Constraint(subtype=right.type, supertype=numeric_t),
199+
Constraint(subtype=ty, supertype=numeric_t)]
200200
else:
201201
raise NotImplementedError('BinOp') # TODO: just use function application constraint gen
202202

typelanguage/constraintsolve.py renamed to rightarrow/constraintsolve.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import logging
44
import copy
55

6-
from typelanguage import constraintgen
7-
from typelanguage.types import *
6+
from rightarrow import constraintgen
7+
from rightarrow.types import *
88

99
logger = logging.getLogger(__name__)
1010

@@ -37,7 +37,7 @@ def reconcile(constraint):
3737
return {}
3838
else:
3939
return Refutation('Cannot reconcile different atomic types: %s' % constraint)
40-
elif isinstance(constraint.supertype, TypeVariable):
40+
elif isinstance(constraint.supertype, Variable):
4141
return {constraint.supertype.name: contraint.subtype}
4242
else:
4343
return Refutation('Cannot reconcile atomic type with non-atomic type: %s' % constraint)
@@ -48,12 +48,12 @@ def reconcile(constraint):
4848
return {}
4949
else:
5050
return Refutation('Cannot reconcile different atomic types: %s' % constraint)
51-
elif isinstance(constraint.subtype, TypeVariable):
51+
elif isinstance(constraint.subtype, Variable):
5252
return {constraint.subtype.name: constraint.supertype}
5353
else:
5454
return Refutation('Cannot reconcile non-atomic type with atomic type: %s' % constraint)
5555

56-
elif isinstance(constraint.supertype, UnionType):
56+
elif isinstance(constraint.supertype, Union):
5757
# Lots of stuff could happen here; unsure if there's research to bring to bear
5858
if constraint.subtype in constraint.supertype.types:
5959
return {}

typelanguage/enforce.py renamed to rightarrow/enforce.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

2-
from typelanguage.parser import TypeParser
2+
from rightarrow.parser import Parser
33

44
def check(ty, val):
55
"Checks that `val` adheres to type `ty`"
66

77
if isinstance(ty, basestring):
8-
ty = TypeParser().parse(ty)
8+
ty = Parser().parse(ty)
99

1010
return ty.enforce(val)
1111

typelanguage/lexer.py renamed to rightarrow/lexer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
logger = logging.getLogger(__name__)
77

8-
class TypeLexer(object):
8+
class Lexer(object):
99
'''
1010
A Lexical analyzer for Python Typelanguage.
1111
'''
@@ -70,6 +70,6 @@ def t_error(self, t):
7070

7171
if __name__ == '__main__':
7272
logging.basicConfig()
73-
lexer = TypeLexer(debug=True)
73+
lexer = Lexer(debug=True)
7474
for token in lexer.tokenize(sys.stdin.read()):
7575
print '%-20s%s' % (token.value, token.type)

0 commit comments

Comments
 (0)