Ejemplo n.º 1
0
Archivo: util.py Proyecto: garaud/blaze
def dshapes(*args):
    """
    Parse all datashapes a single context. This means two datashapes
    'A, B, int32' and 'X, B, float32' will now share type variable 'B'.
    """
    result = [dshape(arg) for arg in args]
    S = IdentityDict()
    for t in result:
        for typevar in free(t):
            S.setdefault(typevar.symbol, typevar)

    def f(t):
        if isinstance(t, TypeVar):
            return S[t.symbol]
        return t

    return [tmap(f, t) for t in result]
Ejemplo n.º 2
0
def reify(solution, S=None):
    """
    Reify a typing solution, returning a new solution with types as concrete
    types as opposed to type sets.

    Parameters
    ----------
    solution : { TypeVar : set([ Type ]) }
        Typing solution

    Returns: { TypeVar : Type }
        Returns a solution reduced to concrete types only.
    """
    if S is None:
        S = IdentityDict()

    seen = set()
    queue = deque(dict_iteritems(solution))
    while queue:
        typevar, t = queue.popleft()
        t = frozenset(t)
        if typevar in S:
            continue

        typeset = solution[typevar]
        freevars = IdentityDict.fromkeys(chain(*[free(t) for t in typeset]))

        if not typeset:
            S[typevar] = typevar
            typeset.add(typevar)
            continue
        elif freevars and (typevar, t) not in seen:
            # Reify dependencies first
            queue.append((typevar, t))
            seen.add((typevar, t))
        elif freevars:
            typeset = set(substitute(S, t) for t in typeset)

        S[typevar] = promote_units(*typeset)

    return S