Example #1
0
def test_eq_assoccomm():
    A, B = ((a, 1, 2), 3), ((a, 1, 2), 3)
    assert tuple(eq_assoccomm((a, 1, 2), (a, 1, 2))({}))
    assert tuple(eq_assoccomm(3, 3)({}))
    assert tuple(eq_assoccomm((1, 2), (1, 2))({}))
    assert tuple(conde(((eq_assoccomm, (a, 1, 2), (a, 1, 2)), (eq_assoccomm, 3, 3)))({}))
    assert tuple(conde((eq_assoccomm, a, b) for a, b in zip(A, B))({}))
Example #2
0
def test_eq_comm_object():
    x = var('x')
    fact(commutative, Add)
    fact(associative, Add)

    assert run(0, x, eq_comm(add(1, 2, 3), add(3, 1, x))) == (2, )

    print(set(run(0, x, eq_comm(add(1, 2), x))))
    assert set(run(0, x, eq_comm(add(1, 2), x))) == set((add(1, 2), add(2, 1)))

    print(set(run(0, x, eq_assoccomm(add(1, 2, 3), add(1, x)))))
    assert set(run(0, x, eq_assoccomm(add(1, 2, 3), add(1, x)))) == \
            set((add(2, 3), add(3, 2)))
Example #3
0
def test_eq_comm_object():
    x = var('x')
    fact(commutative, Add)
    fact(associative, Add)

    assert run(0, x, eq_comm(add(1, 2, 3), add(3, 1, x))) == (2, )

    print(set(run(0, x, eq_comm(add(1, 2), x))))
    assert set(run(0, x, eq_comm(add(1, 2), x))) == set((add(1, 2), add(2, 1)))

    print(set(run(0, x, eq_assoccomm(add(1, 2, 3), add(1, x)))))
    assert set(run(0, x, eq_assoccomm(add(1, 2, 3), add(1, x)))) == \
            set((add(2, 3), add(3, 2)))
Example #4
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]
Example #5
0
def test_expr():
    add = 'add'
    mul = 'mul'
    fact(commutative, add)
    fact(associative, add)
    fact(commutative, mul)
    fact(associative, mul)

    x, y = var('x'), var('y')

    pattern = (mul, (add, 1, x), y)                # (1 + x) * y
    expr    = (mul, 2, (add, 3, 1))                # 2 * (3 + 1)
    assert run(0, (x,y), eq_assoccomm(pattern, expr)) == ((3, 2),)
Example #6
0
def test_expr():
    add = 'add'
    mul = 'mul'
    fact(commutative, Add)
    fact(associative, Add)
    fact(commutative, Mul)
    fact(associative, Mul)

    x, y = var('x'), var('y')

    pattern = (mul, (add, 1, x), y)  # (1 + x) * y
    expr = (mul, 2, (add, 3, 1))  # 2 * (3 + 1)
    assert run(0, (x, y), eq_assoccomm(pattern, expr)) == ((3, 2), )
Example #7
0
def test_objects():
    from logpy.unify import seq_registry, reify
    class add(object):
        def __init__(self, *args):
            self.args = tuple(args)
    def seq_add(x):
        return (type(x),) + x.args
    seq_registry.append((add, seq_add))

    assert seq_add(add(1, 2, 3)) == (add, 1, 2, 3)
    assert goaleval(eq_assoccomm(add(1, 2, 3), add(3, 1, 2)))({})

    x = var('x')

    assert tuple(goaleval(eq_assoccomm(add(1, 2, 3), add(1, 2, x)))({})) == ({x: 3},)

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

    seq_registry.pop()
Example #8
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
Example #9
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
Example #10
0
from logpy import run, var, fact
import logpy.assoccomm as la

# Define mathematical operations
add = 'addition'
mul = 'multiplication'

# Declare that these operations are commutative
# using the facts system
fact(la.commutative, mul)
fact(la.commutative, add)
fact(la.associative, mul)
fact(la.associative, add)

# Define some variables
a, b, c = var('a'), var('b'), var('c')

# Generate expressions
expression_orig = (add, (mul, 3, -2), (mul, (add, 1, (mul, 2, 3)), -1))
expression1 = (add, (mul, (add, 1, (mul, 2, a)), b), (mul, 3, c))
expression2 = (add, (mul, c, 3), (mul, b, (add, (mul, 2, a), 1)))
expression3 = (add, (add, (mul, (mul, 2, a), b), b), (mul, 3, c))

# Compare expressions
print(run(0, (a, b, c), la.eq_assoccomm(expression1, expression_orig)))
print(run(0, (a, b, c), la.eq_assoccomm(expression2, expression_orig)))
print(run(0, (a, b, c), la.eq_assoccomm(expression3, expression_orig)))