Ejemplo n.º 1
0
def test_objects():
    from logpy import variables, reify, assoccomm

    assoccomm.op_registry.extend(op_registry)

    fact(commutative, add)
    fact(associative, add)
    assert tuple(goaleval(eq_assoccomm(add(1, 2, 3), add(3, 1, 2)))({}))
    assert tuple(goaleval(eq_assoccomm(add(1, 2, 3), add(3, 1, 2)))({}))

    x = var('x')

    print tuple(goaleval(eq_assoccomm(add(1, 2, 3), add(1, 2, x)))({}))
    assert reify(x, tuple(goaleval(eq_assoccomm(add(1, 2, 3), add(1, 2,
        x)))({}))[0]) == 3

    assert reify(x, next(goaleval(eq_assoccomm(add(1, 2, 3), add(x, 2,
        1)))({}))) == 3

    v = add(1,2,3)
    with variables(v):
        x = add(5, 6)
        print reify(v, next(goaleval(eq_assoccomm(v, x))({})))
        assert reify(v, next(goaleval(eq_assoccomm(v, x))({}))) == x

    del assoccomm.op_registry[-1]
Ejemplo n.º 2
0
def test_reification_of_computations():
    d = Computation((1, 2), (0,)) + Computation((0, 4), (5, 6))

    with variables(0):
        assert set(run(0, d, membero(0, (10, 11)))) == set((
                Computation((1, 2), (10,)) + Computation((10, 4), (5, 6)),
                Computation((1, 2), (11,)) + Computation((11, 4), (5, 6))))
Ejemplo n.º 3
0
def test_computation():
    c = Computation((1, 2), (3,))
    d = Computation((1, 0), (3,))

    assert run(0, 0, eq(c, d)) == ()

    with variables(0):
        assert run(0, 0, eq(c, d)) == (2,)
Ejemplo n.º 4
0
def test_composite_computation():
    c = Computation((1, 2), (3,)) + Computation((3, 4), (5, 6))
    d = Computation((1, 2), (0,)) + Computation((0, 4), (5, 6))
    e = Computation((0, 4), (5, 6)) + Computation((1, 2), (0,))

    assert run(0, 0, eq(c, d)) == ()

    with variables(0):
        assert run(0, 0, eq(c, d)) == (3,)
Ejemplo n.º 5
0
def test_run_objects_with_context_manager():
    f = Foo(1, 1234)
    g = Foo(1, 2)
    _unify.add((Foo, Foo, dict), unify_object)
    _reify.add((Foo, dict), reify_object)
    with variables(1234):
        assert unify_object(f, g, {})
        assert run(1, 1234, (eq, f, g)) == (2,)
        assert run(1, Foo(1234, 1234), (eq, f, g)) == (Foo(2, 2),)
Ejemplo n.º 6
0
def test_run_objects_with_context_manager():
    f = Foo(1, 1234)
    g = Foo(1, 2)
    _unify.add((Foo, Foo, dict), unify_object)
    _reify.add((Foo, dict), reify_object)
    with variables(1234):
        assert unify_object(f, g, {})
        assert run(1, 1234, (eq, f, g)) == (2, )
        assert run(1, Foo(1234, 1234), (eq, f, g)) == (Foo(2, 2), )
Ejemplo n.º 7
0
def test_run_objects_with_context_manager():
    f = Foo(1, 1234)
    g = Foo(1, 2)
    unify_dispatch[(Foo, Foo)] = unify_object
    reify_dispatch[Foo] = reify_object
    with variables(1234):
        assert unify_object(f, g, {})
        assert run(1, 1234, (eq, f, g)) == (2,)
        assert run(1, Foo(1234, 1234), (eq, f, g)) == (Foo(2, 2),)

    del reify_dispatch[Foo]
    del unify_dispatch[(Foo, Foo)]
Ejemplo n.º 8
0
def compile(inputs, outputs, *assumptions, **kwargs):
    """ A very simple greedy scheme."""

    strategy = kwargs.get('strategy', greedy)
    c = Identity(*outputs)

    # Is this computation a leaf in our tree?  Do its inputs match ours?
    isleaf = lambda comp: set(remove(constant_arg, comp.inputs)) == set(inputs)

    with assuming(*assumptions):
        with variables(*vars):
            stream = strategy(children, objective, isleaf, c) # all valid computations
            result = next(stream)                           # first valid computtion

    return result
Ejemplo n.º 9
0
def test_objects():
    from logpy import variables, reify

    fact(commutative, Add)
    fact(associative, Add)
    assert tuple(goaleval(eq_assoccomm(add(1, 2, 3), add(3, 1, 2)))({}))
    assert tuple(goaleval(eq_assoccomm(add(1, 2, 3), add(3, 1, 2)))({}))

    x = var('x')

    print(tuple(goaleval(eq_assoccomm(add(1, 2, 3), add(1, 2, x)))({})))
    assert reify(x, tuple(goaleval(eq_assoccomm(
        add(1, 2, 3), add(1, 2, x)))({}))[0]) == 3

    assert reify(x, next(goaleval(eq_assoccomm(
        add(1, 2, 3), add(x, 2, 1)))({}))) == 3

    v = add(1, 2, 3)
    with variables(v):
        x = add(5, 6)
        print(reify(v, next(goaleval(eq_assoccomm(v, x))({}))))
        assert reify(v, next(goaleval(eq_assoccomm(v, x))({}))) == x
Ejemplo n.º 10
0
def test_objects():
    from logpy import variables, reify, assoccomm

    fact(commutative, Add)
    fact(associative, Add)
    assert tuple(goaleval(eq_assoccomm(add(1, 2, 3), add(3, 1, 2)))({}))
    assert tuple(goaleval(eq_assoccomm(add(1, 2, 3), add(3, 1, 2)))({}))

    x = var('x')

    print(tuple(goaleval(eq_assoccomm(add(1, 2, 3), add(1, 2, x)))({})))
    assert reify(
        x,
        tuple(goaleval(eq_assoccomm(add(1, 2, 3), add(1, 2, x)))({}))[0]) == 3

    assert reify(x,
                 next(goaleval(eq_assoccomm(add(1, 2, 3), add(x, 2,
                                                              1)))({}))) == 3

    v = add(1, 2, 3)
    with variables(v):
        x = add(5, 6)
        print(reify(v, next(goaleval(eq_assoccomm(v, x))({}))))
        assert reify(v, next(goaleval(eq_assoccomm(v, x))({}))) == x
Ejemplo n.º 11
0
def test_composite_commutativity():
    c = Computation((1, 2), (3,)) + Computation((3, 4), (5, 6))
    e = Computation((0, 4), (5, 6)) + Computation((1, 2), (0,))

    with variables(0):
        assert run(0, 0, eqac(c, e)) == (3,)
Ejemplo n.º 12
0
            Account('Carl', 'Marx', 2, 3),
            Account('John', 'Rockefeller', 3, 1000))

# variables are arbitrary Python objects, not LogPy Var objects
first = 'FIRST'
last = 'LAST'
ident = -1111
balance = -2222
newbalance = -3333
vars = {first, last, ident, balance, newbalance}


# Describe a couple of transformations on accounts
source = Account(first, last, ident, balance)
target = Account(first, last, ident, newbalance)

theorists = ('Adam', 'Carl')
# Give $10 to theorists
theorist_bonus = lall((membero, source, accounts),
                      (membero, first, theorists),
                      (add, 10, balance, newbalance))

# Take $10 from anyone with more than $100
tax_the_rich = lall((membero, source, accounts),
                    (gt, balance, 100),
                    (sub, balance, 10, newbalance))

with variables(*vars):
    print run(0, target, tax_the_rich)
    print run(0, target, theorist_bonus)
Ejemplo n.º 13
0
def test_unify():
    with variables(A, B):
        s = {A: X, B: Y}
        assert unify(gemm, vgemm, {}) == s
        assert reify(vgemm, s) == gemm
        assert run(1, vgemm, eq(vsyrk, syrk)) == (gemm,)
Ejemplo n.º 14
0
unifiable(Account)  # Register Account class

accounts = (Account('Adam', 'Smith', 1,
                    20), Account('Carl', 'Marx', 2,
                                 3), Account('John', 'Rockefeller', 3, 1000))

# variables are arbitrary Python objects, not LogPy Var objects
first = 'FIRST'
last = 'LAST'
ident = -1111
balance = -2222
newbalance = -3333
vars = {first, last, ident, balance, newbalance}

# Describe a couple of transformations on accounts
source = Account(first, last, ident, balance)
target = Account(first, last, ident, newbalance)

theorists = ('Adam', 'Carl')
# Give $10 to theorists
theorist_bonus = lall((membero, source, accounts), (membero, first, theorists),
                      (add, 10, balance, newbalance))

# Take $10 from anyone with more than $100
tax_the_rich = lall((membero, source, accounts), (gt, balance, 100),
                    (sub, balance, 10, newbalance))

with variables(*vars):
    print run(0, target, tax_the_rich)
    print run(0, target, theorist_bonus)