Exemple #1
0
def test_objects_full():
    _unify.add((Foo, Foo, dict), unify_object)
    _unify.add((Bar, Bar, dict), unify_object)
    _reify.add((Foo, dict), reify_object)
    _reify.add((Bar, dict), reify_object)

    assert unify_object(Foo(1, Bar(2)), Foo(1, Bar(var(3))), {}) == {var(3): 2}
    assert reify(Foo(var('a'), Bar(Foo(var('b'), 3))),
                 {var('a'): 1, var('b'): 2}) == Foo(1, Bar(Foo(2, 3)))
Exemple #2
0
def test_objects_full():
    _unify.add((Foo, Foo, dict), unify_object)
    _unify.add((Bar, Bar, dict), unify_object)
    _reify.add((Foo, dict), reify_object)
    _reify.add((Bar, dict), reify_object)

    assert unify_object(Foo(1, Bar(2)), Foo(1, Bar(var(3))), {}) == {var(3): 2}
    assert reify(Foo(var('a'), Bar(Foo(var('b'), 3))),
                 {var('a'): 1, var('b'): 2}) == Foo(1, Bar(Foo(2, 3)))
Exemple #3
0
def test_unify_isinstance_list():
    class Foo2(Foo): pass
    x = var('x')
    y = var('y')
    f, g = Foo2(1, 2), Foo2(x, y)

    _unify.add((Foo, Foo, dict), unify_object)
    _reify.add((Foo, dict), reify_object)

    assert unify(f, g, {})
    assert reify(g, {x: 1, y: 2}) == f
def test_objects_full():
    _unify.add((Foo, Foo, Mapping), _unify_object)
    _unify.add((Bar, Bar, Mapping), _unify_object)
    _reify.add((Foo, Mapping), _reify_object)
    _reify.add((Bar, Mapping), _reify_object)

    x, y = var(), var()
    assert unify(Foo(1, 2), Bar(1), {}) is False
    assert unify(Foo(1, Bar(2)), Foo(1, Bar(x)), {}) == {x: 2}
    assert reify(Foo(x, Bar(Foo(y, 3))), {
        x: 1,
        y: 2
    }) == Foo(1, Bar(Foo(2, 3)))

    class SubFoo(Foo):
        pass

    assert unify(Foo(1, 2), SubFoo(1, 2), {}) is False
Exemple #5
0
def unifiable_with_term(cls):
    _reify.add((cls, Mapping), reify_term)
    _unify.add((cls, cls, Mapping), unify_term)
    return cls
Exemple #6
0
    lambda u, v, s: unify_MetaSymbol(u, metatize(v), s),
)
_unify.add(
    (tf_class_abstractions, TFlowMetaSymbol, Mapping),
    lambda u, v, s: unify_MetaSymbol(metatize(u), v, s),
)
_unify.add(
    (tf_class_abstractions, tf_class_abstractions, Mapping),
    lambda u, v, s: unify_MetaSymbol(metatize(u), metatize(v), s),
)


def _reify_TFlowClasses(o, s):
    meta_obj = metatize(o)
    return reify(meta_obj, s)


_reify.add((tf_class_abstractions, Mapping), _reify_TFlowClasses)


_car.add((tf.Tensor,), lambda x: operator(metatize(x)))
operator.add((tf.Tensor,), lambda x: operator(metatize(x)))

_cdr.add((tf.Tensor,), lambda x: arguments(metatize(x)))
arguments.add((tf.Tensor,), lambda x: arguments(metatize(x)))

etuplize.add(tf_class_abstractions, lambda x, shallow=False: etuplize(metatize(x), shallow))


__all__ = []
Exemple #7
0
)
_unify.add(
    (tt_class_abstractions, TheanoMetaSymbol, Mapping),
    lambda u, v, s: unify_MetaSymbol(metatize(u), v, s),
)
_unify.add(
    (tt_class_abstractions, tt_class_abstractions, Mapping),
    lambda u, v, s: unify_MetaSymbol(metatize(u), metatize(v), s),
)


def _reify_TheanoClasses(o, s):
    meta_obj = metatize(o)
    return reify(meta_obj, s)


_reify.add((tt_class_abstractions, Mapping), _reify_TheanoClasses)

operator.add((tt.Variable, ), lambda x: operator(metatize(x)))
_car.add((tt.Variable, ), lambda x: operator(metatize(x)))

arguments.add((tt.Variable, ), lambda x: arguments(metatize(x)))
_cdr.add((tt.Variable, ), lambda x: arguments(metatize(x)))

term.add((tt.Op, ExpressionTuple), lambda op, args: term(metatize(op), args))

etuplize.add(tt_class_abstractions,
             lambda x, shallow=False: etuplize(metatize(x), shallow))

__all__ = []
Exemple #8
0
        if len(res_same) == len(u) and all(res_same):
            # Everything is equal and there are no logic variables
            yield u
            return

        if getattr(u, "_parent", None) and all(res_same):
            # If we simply swapped-out logic variables, then we don't want to
            # lose the parent etuple information.
            res = etuple(*res)
            res._parent = u._parent
            yield res
            return

        yield etuple(*res)

    _reify.add((ExpressionTuple, Mapping), _reify_ExpressionTuple)

    def _reify_KwdPair(u, s):
        arg = yield _reify(u.arg, s)
        value = yield _reify(u.value, s)

        yield construction_sentinel

        yield KwdPair(arg, value)

    _reify.add((KwdPair, Mapping), _reify_KwdPair)


@dispatch(object)
def rator(x):
    return car(x)
Exemple #9
0
def unifiable_with_term(cls):
    _reify.add((cls, dict), reify_term)
    _unify.add((cls, cls, dict), unify_term)
    return cls
Exemple #10
0
    try:
        rands = o.rands
    except NotImplementedError:
        return o

    new_rands = reify(rands, s)

    if rands == new_rands:
        return o
    else:
        newobj = type(o)(*new_rands, obj=obj)
        return newobj


_reify.add((MetaSymbol, dict), _reify_MetaSymbol)

_tuple__reify = _reify.dispatch(tuple, dict)

_reify.add((ExpressionTuple, dict), lambda x, s: _tuple__reify(x, s))

_isvar = isvar.dispatch(object)

isvar.add((MetaSymbol, ),
          lambda x: _isvar(x) or (not isinstance(x.obj, Var) and isvar(x.obj)))

# We don't want to lose special functionality (and caching) because `cdr` uses
# `islice`.
_cdr.add((ExpressionTuple, ), itemgetter(slice(1, None)))

    if isinstance(o.obj, Var):
        obj = s.get(o.obj, o.obj)
    else:
        obj = None

    rands = o.rands()
    new_rands = reify(rands, s)

    if rands == new_rands:
        return o
    else:
        newobj = type(o)(*new_rands, obj=obj)
        return newobj


_reify.add((MetaSymbol, dict), _reify_MetaSymbol)


def _reify_TheanoClasses(o, s):
    meta_obj = MetaSymbol.from_obj(o)
    return reify(meta_obj, s)


_reify.add((tt_class_abstractions, dict), _reify_TheanoClasses)

_isvar = isvar.dispatch(object)

isvar.add((MetaSymbol, ),
          lambda x: _isvar(x) or (not isinstance(x.obj, Var) and isvar(x.obj)))

Exemple #12
0
    try:
        rands = o.rands
    except NotImplementedError:
        return o

    new_rands = reify(rands, s)

    if rands == new_rands:
        return o
    else:
        newobj = type(o)(*new_rands, obj=obj)
        return newobj


_reify.add((MetaSymbol, Mapping), _reify_MetaSymbol)

# def car_MetaSymbol(x):
#     """Return the operator/head/CAR of a meta symbol."""
#     return type(x)


def car_MetaVariable(x):
    """Return the operator/head/CAR of a meta variable."""
    try:
        return x.base_operator
    except NotImplementedError:
        raise ConsError("Not a cons pair.")


# _car.add((MetaSymbol,), car_MetaSymbol)
Exemple #13
0
def test_reify_nonstandard_object():
    _reify.add((ast.AST, Mapping), _reify_object)
    x = var()
    assert reify(ast.Num(n=1), {}).n == 1
    assert reify(ast.Num(n=x), {}).n == x
    assert reify(ast.Num(n=x), {x: 2}).n == 2
Exemple #14
0
        return NotImplemented

    def __hash__(self):
        return hash((Var, self.token))


def _reify_ConstrainedState(u, S):
    u_res = walk(u, S.data)

    if u_res is u:
        yield ConstrainedVar(u_res, S)
    else:
        yield _reify(u_res, S)


_reify.add((Var, ConstrainedState), _reify_ConstrainedState)


class DisequalityStore(ConstraintStore):
    """A disequality constraint (i.e. two things do not unify)."""

    op_str = "neq"

    def __init__(self, lvar_constraints=None):
        super().__init__(lvar_constraints)

    def post_unify_check(self,
                         lvar_map,
                         lvar=None,
                         value=None,
                         old_state=None):
Exemple #15
0
_unify.add(
    (TheanoMetaSymbol, tt_class_abstractions, dict),
    lambda u, v, s: unify_MetaSymbol(u, metatize(v), s),
)
_unify.add(
    (tt_class_abstractions, TheanoMetaSymbol, dict),
    lambda u, v, s: unify_MetaSymbol(metatize(u), v, s),
)
_unify.add(
    (tt_class_abstractions, tt_class_abstractions, dict),
    lambda u, v, s: unify_MetaSymbol(metatize(u), metatize(v), s),
)


def _reify_TheanoClasses(o, s):
    meta_obj = metatize(o)
    return reify(meta_obj, s)


_reify.add((tt_class_abstractions, dict), _reify_TheanoClasses)

operator.add((tt.Variable,), lambda x: operator(metatize(x)))

arguments.add((tt.Variable,), lambda x: arguments(metatize(x)))

term.add((tt.Op, ExpressionTuple), lambda op, args: term(metatize(op), args))

etuplize.add(tt_class_abstractions, lambda x, shallow=False: etuplize(metatize(x), shallow))

__all__ = []