Example #1
0
def verify_method_signature(type, signature):
    """Verify a method signature in the context of the defining type"""
    typebound = set([t.symbol for t in free(type)])
    sigbound = set([t.symbol for argtype in signature.argtypes
                                 for t in free(argtype)])
    for t in free(signature.restype):
        if t.symbol not in typebound and t.symbol not in sigbound:
            raise TypeError("Type variable %s is not bound by the type or "
                            "argument types" % (t,))
Example #2
0
def verify_method_signature(type, signature):
    """Verify a method signature in the context of the defining type"""
    typebound = set([t.symbol for t in free(type)])
    sigbound = set(
        [t.symbol for argtype in signature.argtypes for t in free(argtype)])
    for t in free(signature.restype):
        if t.symbol not in typebound and t.symbol not in sigbound:
            raise TypeError("Type variable %s is not bound by the type or "
                            "argument types" % (t, ))
Example #3
0
def unify(constraints, concrete=True):
    """
    Unify a set of constraints. If `concrete` is set to True, the result
    may not have any remaining free variables.
    """
    cs = [(to_blaze(left), to_blaze(right)) for left, right in constraints]
    result, remaining_constraints = blaze_unify(cs)

    if concrete:
        #if remaining:
        #    raise TypeError("Result is not concrete after unification")
        for result_type in result:
            if free(result_type):
                raise TypeError(
                    "Result type stil has free variables: %s" % (result_type,))

    return [resolve_type(t) for t in result]
Example #4
0
def unify(constraints, concrete=True):
    """
    Unify a set of constraints. If `concrete` is set to True, the result
    may not have any remaining free variables.
    """
    cs = [(to_blaze(left), to_blaze(right)) for left, right in constraints]
    result, remaining_constraints = blaze_unify(cs)

    if concrete:
        #if remaining:
        #    raise TypeError("Result is not concrete after unification")
        for result_type in result:
            if free(result_type):
                raise TypeError("Result type stil has free variables: %s" %
                                (result_type, ))

    return [resolve_type(t) for t in result]
Example #5
0
def resolve_in_scope(ty, scope):
    """
    Resolve a parsed type in the current scope. For example, if we parse
    Foo[X], look up Foo in the current scope and reconstruct it with X.
    """
    def resolve(t):
        if isinstance(type(t), TypeConstructor):
            name = type(t).name

            # Get the @jit class (e.g. Int)
            if hasattr(t, 'impl'):
                impl = t.impl  # already resolved!
            else:
                impl = scope.get(name) or lookup_builtin_type(name)

            if impl is None:
                raise TypeError(
                    "Type constructor %r is not in the current scope" %
                    (name, ))

            # Get the TypeConstructor for the @jit class (e.g.
            # Int[nbits, unsigned])
            ctor = impl.type.__class__

            return ctor(*t.parameters)

        elif isinstance(t, TypeVar) and t.symbol[0].isupper():
            # Resolve bare types, e.g. a name like 'NoneType' is parsed as a
            # TypeVar
            if t.symbol == 'NoneType':
                assert t.symbol in scope
            if scope.get(t.symbol):
                cls = scope[t.symbol]
                return cls[()]
            return t

        return t

    freevars = dict((v.symbol, v) for v in free(ty))
    return ds.tmap(resolve, ty)
Example #6
0
def resolve_in_scope(ty, scope):
    """
    Resolve a parsed type in the current scope. For example, if we parse
    Foo[X], look up Foo in the current scope and reconstruct it with X.
    """
    def resolve(t):
        if isinstance(type(t), TypeConstructor):
            name = type(t).name

            # Get the @jit class (e.g. Int)
            if hasattr(t, 'impl'):
                impl = t.impl # already resolved!
            else:
                impl = scope.get(name) or lookup_builtin_type(name)

            if impl is None:
                raise TypeError(
                    "Type constructor %r is not in the current scope" % (name,))

            # Get the TypeConstructor for the @jit class (e.g.
            # Int[nbits, unsigned])
            ctor = impl.type.__class__

            return ctor(*t.parameters)

        elif isinstance(t, TypeVar) and t.symbol[0].isupper():
            # Resolve bare types, e.g. a name like 'NoneType' is parsed as a
            # TypeVar
            if t.symbol == 'NoneType':
                assert t.symbol in scope
            if scope.get(t.symbol):
                cls = scope[t.symbol]
                return cls[()]
            return t

        return t

    freevars = dict((v.symbol, v) for v in free(ty))
    return ds.tmap(resolve, ty)
Example #7
0
def bound(self):
    freevars = free(self.impl.type)
    # assert len(freevars) == len(key)

    # TODO: Parameterization by type terms
    return dict((t.symbol, v) for t, v in zip(freevars, self.parameters))
Example #8
0
def bound(self):
    freevars = free(self.impl.type)
    # assert len(freevars) == len(key)

    # TODO: Parameterization by type terms
    return dict((t.symbol, v) for t, v in zip(freevars, self.parameters))