Exemple #1
0
def unify(constraints, broadcasting=None):
    """
    Unify a set of constraints and return a concrete solution

        >>> import blaze
        >>> d1 = blaze.dshape('10, int32')
        >>> d2 = blaze.dshape('T, float32')
        >>> [result], constraints = unify([(d1, d2)], [True])
        >>> result
        dshape("10, float32")
        >>> constraints
        []
    """
    S = IdentityDict()
    constraints = [(simplify(ds1, S), simplify(ds2, S))
                        for ds1, ds2 in constraints]

    # Compute a solution to a set of constraints
    constraints, b_env = normalize(constraints, broadcasting)
    logger.debug("Normalized constraints: %s", constraints)

    solution, remaining = unify_constraints(constraints, S)
    logger.debug("Initial solution: %s", solution)

    seed_typesets(remaining, solution)
    merge_typevar_sets(remaining, solution)

    # Compute a type substitution with concrete types from the solution
    # TODO: incorporate broadcasting environment during reification
    substitution = reify(solution)
    logger.debug("Substitution: %s", substitution)

    # Reify and promote the datashapes
    result = [substitute(substitution, ds2) for ds1, ds2 in constraints]
    return result, remaining
    def test_simplify_implements(self):
        ds = dshape('10, A : numeric')
        self.assertEqual(str(ds), '10, A : numeric')

        solution = IdentityDict()
        ds = simplify(ds, solution)

        self.assertEqual(str(ds), '10, A')
        A = ds.parameters[-1]
        [x] = solution[A]
        self.assertEqual(set(x), set(numeric))