def merge_typevar_sets(constraints, solution): """ Update the solution of sets that contain only type variables with a new type variable along with new coercion constraints. Consider the example: x, y, z = dshapes('A, B, int32', 'C, D, float32', 'X, Y, float32') with x ⊆ z and y ⊆ z: >>> A, B, C, D, X, Y = map(TypeVar, 'ABCDXY') >>> constraints = [(A, X), (C, X), (B, Y), (D, Y)] >>> solution = {X: set([A, C]), Y: set([B, D])} >>> merge_typevar_sets(constraints, solution) >>> solution[X] set([TypeVar(A_C)]) >>> solution[Y] set([TypeVar(B_D)]) """ for src_var, typeset in list(dict_iteritems(solution)): if len(typeset) > 1 and all(isinstance(v, TypeVar) for v in typeset): new_var = TypeVar("_".join(sorted(v.symbol for v in typeset))) solution[src_var] = set([new_var]) solution[new_var] = set() for v in typeset: constraints.append((v, new_var)) constraints.remove((v, src_var))
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
def blaze_args(args, kwargs): """Build blaze arrays from inputs to a blaze kernel""" args = [blaze.array(a) for a in args] kwargs = dict((v, blaze.array(k)) for k, v in dict_iteritems(kwargs)) return args, kwargs