Пример #1
0
def test_fact():
    rel = Relation()
    fact(rel, 1, 2)
    assert (1, 2) in rel.facts
    assert (10, 10) not in rel.facts

    facts(rel, (2, 3), (3, 4))
    assert (2, 3) in rel.facts
    assert (3, 4) in rel.facts
Пример #2
0
def test_eq_assoccomm():
    x, y = var(), var()
    eqac = eq_assoccomm
    ac = 'commassoc_op'
    fact(commutative, ac)
    fact(associative, ac)
    assert results(eqac((ac, (ac, 1, x), y), (ac, 2, (ac, 3, 1))))
    assert results((eqac, 1, 1))
    assert results(eqac((a, (a, 1, 2), 3), (a, 1, 2, 3)))
    assert results(eqac((ac, (ac, 1, 2), 3), (ac, 1, 2, 3)))
    assert results(eqac((ac, 3, (ac, 1, 2)), (ac, 1, 2, 3)))
    assert run(0, x, eqac((ac, 3, (ac, 1, 2)), (ac, 1, x, 3))) == (2,)
Пример #3
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),)
Пример #4
0
from logpy.assoccomm import (
    unify_assoc,
    unify_comm,
    eq_assoc,
    eq_comm,
    operation,
    associative,
    commutative,
    eq_assoccomm,
    conde,
)

a = "assoc_op"
c = "comm_op"
x = var()
fact(associative, a)
fact(commutative, c)


def test_assoc():
    assert tuple(unify_assoc((a, 1, 2, 3), (a, 1, 2, 3), {}))
    assert tuple(unify_assoc((a, 1, 2, 3), (a, (a, 1, 2), 3), {}))
    assert tuple(unify_assoc((a, 1, 2, 3), (a, 1, x, 3), {})) == ({x: 2},)
    assert tuple(unify_assoc((a, 1, 2, 3), (a, 1, x), {})) == ({x: (a, 2, 3)},)
    assert tuple(unify_assoc((a, 1, 2, 3), (a, x, 3), {})) == ({x: (a, 1, 2)},)
    assert tuple(unify_assoc((a, x, 3), (a, 1, 2, 3), {})) == ({x: (a, 1, 2)},)


def test_eq_assoccomm():
    A, B = ((a, 1, 2), 3), ((a, 1, 2), 3)
    assert tuple(eq_assoccomm((a, 1, 2), (a, 1, 2))({}))
Пример #5
0
def test_relation():
    parent = Relation()
    fact(parent, "Homer", "Bart")
    fact(parent, "Homer", "Lisa")
    fact(parent, "Marge", "Bart")
    fact(parent, "Marge", "Lisa")
    fact(parent, "Abe", "Homer")
    fact(parent, "Jackie", "Marge")

    x = var('x')
    assert set(run(5, x, parent("Homer", x))) == set(("Bart", "Lisa"))
    assert set(run(5, x, parent(x, "Bart")))  == set(("Homer", "Marge"))

    def grandparent(x, z):
        y = var()
        return conde((parent(x, y), parent(y, z)))

    assert set(run(5, x, grandparent(x, "Bart") )) == set(("Abe", "Jackie"))