def unify_single(t1, t2, solution, remaining): """ Unify a single type equation and update the solution and remaining constraints. """ if isinstance(t1, TypeVar) and isinstance(t2, TypeVar): remaining.append((t1, t2)) elif isinstance(t1, TypeVar): if t1 in free(t2): raise error.UnificationError("Cannot unify recursive types") solution[t1].add(t2) remaining.append((t1, t2)) elif isinstance(t2, TypeVar): if t2 in free(t1): raise error.UnificationError("Cannot unify recursive types") solution[t2].add(t1) elif isinstance(t1, TypeConstructor): verify(t1, t2) elif not free(t1) and not free(t2): # No need to recurse, this will be caught by promote() pass else: verify(t1, t2) for arg1, arg2 in zip(t1.parameters, t2.parameters): unify_single(arg1, arg2, solution, remaining)
def tzip(f, a, b, descend=descend): """Map f over two types zip-wise""" from blaze.datashape import verify # TODO: remove circularity if not descend(a) or not descend(b): return a, b verify(a, b) result = zip(*starmap(f, zip(a.parameters, b.parameters))) params1, params2 = result or [(), ()] return (type_constructor(a)(*params1), type_constructor(b)(*params2))
def tzip(f, a, b, descend=descend): """Map f over two types zip-wise""" from blaze.datashape import verify # TODO: remove circularity if not descend(a) or not descend(b): return a, b verify(a, b) params1, params2 = zip(*[ f(arg1, arg2) for arg1, arg2 in zip(a.parameters, b.parameters)]) return (type_constructor(a)(*params1), type_constructor(b)(*params2))
def promote_type_constructor(a, b): """Promote two generic type constructors""" # Verify type constructor equality verify(a, b) # Promote parameters according to flags args = [] for flag, t1, t2 in zip(a.flags, a.parameters, b.parameters): if flag['coercible']: result = promote(t1, t2) else: if t1 != t2: raise error.UnificationError( "Got differing types %s and %s for unpromotable type " "parameter" % (t1, t2)) result = t1 args.append(result) return type(a)(*args)
def unify_single(t1, t2, solution, remaining): """ Unify a single type equation and update the solution and remaining constraints. """ if isinstance(t1, TypeVar) and isinstance(t2, TypeVar): remaining.append((t1, t2)) elif isinstance(t1, TypeVar): if t1 in free(t2): raise error.UnificationError("Cannot unify recursive types") solution[t1].add(t2) remaining.append((t1, t2)) elif isinstance(t2, TypeVar): if t2 in free(t1): raise error.UnificationError("Cannot unify recursive types") solution[t2].add(t1) elif isinstance(t1, TypeConstructor): verify(t1, t2) elif not isinstance(t1, Mono) and not isinstance(t2, Mono): verify(t1, t2) elif getattr(t1, "cls", None) == MEASURE and getattr(t2, "cls", None) == MEASURE: # If both types are measures, verify they can be promoted promote_units(t1, t2) else: verify(t1, t2) for arg1, arg2 in zip(t1.parameters, t2.parameters): unify_single(arg1, arg2, solution, remaining)