Beispiel #1
0
def test_goaleval():
    x, y = var('x'), var('y')
    g = eq(x, 2)
    assert goaleval(g) == g
    assert callable(goaleval((eq, x, 2)))
    raises(EarlyGoalError, lambda: goaleval((membero, x, y)))
    assert callable(goaleval((lall, (eq, x, 2))))
Beispiel #2
0
def test_goaleval():
    x, y = var("x"), var("y")
    g = eq(x, 2)
    assert goaleval(g) == g
    assert callable(goaleval((eq, x, 2)))
    raises(EarlyGoalError, lambda: goaleval((membero, x, y)))
    assert callable(goaleval((lall, (eq, x, 2))))
Beispiel #3
0
def eq_comm(u, v, eq=None):
    """ Goal for commutative equality

    >>> from logpy import run, var, fact
    >>> from logpy.assoccomm import eq_comm as eq
    >>> from logpy.assoccomm import commutative, associative

    >>> fact(commutative, 'add')    # declare that 'add' is commutative
    >>> fact(associative, 'add')    # declare that 'add' is associative

    >>> x = var()
    >>> run(0, x, eq(('add', 1, 2, 3), ('add', 2, x, 1)))
    (3,)
    """
    eq = eq or eq_comm
    op = var()
    utail = var()
    vtail = var()
    if isvar(u) and isvar(v):
        return (core.eq, u, v)
        raise EarlyGoalError()
    if isinstance(v, tuple) and not isinstance(u, tuple):
        u, v = v, u
    return (conde, ((core.eq, u, v),),
                   ((heado, op, u),
                    (commutative, op),
                    (tailo, utail, u),
                    (conso, op, vtail, v),
                    (seteq, utail, vtail, eq)))
Beispiel #4
0
def eq_comm(u, v, eq=None):
    """ Goal for commutative equality

    >>> from logpy import run, var, fact
    >>> from logpy.assoccomm import eq_comm as eq
    >>> from logpy.assoccomm import commutative, associative

    >>> fact(commutative, 'add')    # declare that 'add' is commutative
    >>> fact(associative, 'add')    # declare that 'add' is associative

    >>> x = var()
    >>> run(0, x, eq(('add', 1, 2, 3), ('add', 2, x, 1)))
    (3,)
    """
    eq = eq or eq_comm
    op = var()
    utail = var()
    vtail = var()
    if isvar(u) and isvar(v):
        return (core.eq, u, v)
        raise EarlyGoalError()
    uop, uargs = op_args(u)
    vop, vargs = op_args(v)
    if not uop and not vop:
        return (core.eq, u, v)
    if vop and not uop:
        uop, uargs = vop, vargs
        v, u = u, v
    return (conde, ((core.eq, u, v),),
                   ((commutative, uop),
                    (buildo, uop, vtail, v),
                    (permuteq, uargs, vtail, eq)))
Beispiel #5
0
def test_reify():
    x, y, z = var(), var(), var()
    s = {x: 1, y: 2, z: (x, y)}
    assert reify(x, s) == 1
    assert reify(10, s) == 10
    assert reify((1, y), s) == (1, 2)
    assert reify((1, (x, (y, 2))), s) == (1, (1, (2, 2)))
    assert reify(z, s) == (1, 2)
Beispiel #6
0
def test_conso():
    x = var()
    y = var()
    assert not results(conso(x, y, ()))
    assert results(conso(1, (2, 3), (1, 2, 3)))
    assert results(conso(x, (2, 3), (1, 2, 3))) == ({x: 1}, )
    assert results(conso(1, (2, 3), x)) == ({x: (1, 2, 3)}, )
    assert results(conso(x, y, (1, 2, 3))) == ({x: 1, y: (2, 3)}, )
    assert results(conso(x, (2, 3), y)) == ({y: (x, 2, 3)}, )
Beispiel #7
0
def test_conso():
    x = var()
    y = var()
    assert not results(conso(x, y, ()))
    assert results(conso(1, (2, 3), (1, 2, 3)))
    assert results(conso(x, (2, 3), (1, 2, 3))) == ({x: 1},)
    assert results(conso(1, (2, 3), x)) == ({x: (1, 2, 3)},)
    assert results(conso(x, y, (1, 2, 3))) == ({x: 1, y: (2, 3)},)
    assert results(conso(x, (2, 3), y)) == ({y: (x, 2, 3)},)
Beispiel #8
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,)
Beispiel #9
0
def test_unify_tuple():
    # Tests that adding facts can be unified with unpacked versions of those
    # facts.
    valido = Relation()
    fact(valido, (0, 1))
    fact(valido, (1, 0))
    fact(valido, (1, 1))
    x = var()
    y = var()
    assert set(run(0, x, valido((x, y)))) == set([0, 1])
    assert set(run(0, (x, y), valido((x, y)))) == set([(0, 1), (1, 0), (1, 1)])
    assert run(0, x, valido((x, x))) == (1, )
Beispiel #10
0
def test_seteq():
    x = var('x')
    y = var('y')
    abc = tuple('abc')
    bca = tuple('bca')
    assert results(seteq(abc, bca))
    assert len(results(seteq(abc, x))) == 6
    assert len(results(seteq(x, abc))) == 6
    assert bca in run(0, x, seteq(abc, x))
    assert results(seteq((1, 2, 3), (3, x, 1))) == ({x: 2}, )

    assert run(0, (x, y), seteq((1, 2, x), (2, 3, y)))[0] == (3, 1)
    assert not run(0, (x, y), seteq((4, 5, x), (2, 3, y)))
Beispiel #11
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), )
Beispiel #12
0
def test_seteq():
    x = var('x')
    y = var('y')
    abc = tuple('abc')
    bca = tuple('bca')
    assert results(seteq(abc, bca))
    assert len(results(seteq(abc, x))) == 6
    assert len(results(seteq(x, abc))) == 6
    assert bca in run(0, x, seteq(abc, x))
    assert results(seteq((1, 2, 3), (3, x, 1))) == ({x: 2},)

    assert run(0, (x, y), seteq((1, 2, x), (2, 3, y)))[0] == (3, 1)
    assert not run(0, (x, y), seteq((4, 5, x), (2, 3, y)))
Beispiel #13
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),)
Beispiel #14
0
def test_eq_assoccomm():
    x, y = var(), var()
    eqac = eq_assoccomm
    ac = 'commassoc_op'
    fact(commutative, ac)
    fact(associative, ac)
    assert results(eqac(1, 1))
    assert results(eqac((1, ), (1, )))
    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, )
Beispiel #15
0
def opo(x, op):
    """ Operation of a tuple

    op((add, 1, 2), x) --> {x: add}
    """
    h = var()
    return conde(((heado, h, x), (operation, h), (eq, h, op)))
Beispiel #16
0
def test_unify_variable_with_itself_should_not_unify():
    # Regression test for https://github.com/logpy/logpy/issues/33
    valido = Relation()
    fact(valido, "a", "b")
    fact(valido, "b", "a")
    x = var()
    assert run(0, x, valido(x, x)) == ()
Beispiel #17
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]
Beispiel #18
0
def test_primo():
    x = var()
    assert set(run(0, x, membero(x, (1,2,3,4,5,6,7,8,9,10,11)),
                           (primo, x))) == set((2, 3, 5, 7, 11))
    assert all(isprime(i) for i in run(5, x, primo(x)))
    assert set(run(0, x, membero(x, (1,2,3,4,5,6,7,8,9,10)), primo(x))) == \
            set((2, 3, 5, 7))
Beispiel #19
0
def eq_assoccomm(u, v):
    """ Associative/Commutative eq

    Works like logic.core.eq but supports associative/commutative expr trees

    tree-format:  (op, *args)
    example:      (add, 1, 2, 3)

    State that operations are associative or commutative with relations

    >>> from logpy.assoccomm import eq_assoccomm as eq
    >>> from logpy.assoccomm import commutative, associative
    >>> from logpy import fact, run, var

    >>> fact(commutative, 'add')    # declare that 'add' is commutative
    >>> fact(associative, 'add')    # declare that 'add' is associative

    >>> x = var()
    >>> e1 = ('add', 1, 2, 3)
    >>> e2 = ('add', 1, x)
    >>> run(0, x, eq(e1, e2))
    (('add', 2, 3), ('add', 3, 2))
    """
    w = var()
    return lall((eq_comm, u, w, eq_assoccomm),
                (eq_assoc, w, v, eq_assoccomm))
Beispiel #20
0
def eq_assoc(u, v, eq=core.eq, n=None):
    """ Goal for associative equality

    >>> from logpy import run, var, fact
    >>> from logpy.assoccomm import eq_assoc as eq

    >>> fact(commutative, 'add')    # declare that 'add' is commutative
    >>> fact(associative, 'add')    # declare that 'add' is associative

    >>> x = var()
    >>> run(0, x, eq(('add', 1, 2, 3), ('add', 1, x)))
    (('add', 2, 3),)
    """
    op = var()
    if isinstance(u, tuple) and isinstance(v, tuple):
        return conde([(core.eq, u, v)],
                     [(heado, op, u), (heado, op, v), (associative, op),
                      lambda s: assocunify(u, v, s, eq, n)])

    if isinstance(u, tuple) or isinstance(v, tuple):
        if isinstance(v, tuple):
            v, u = u, v
        return conde([(core.eq, u, v)],
                     [(heado, op, u), (associative, op),
                      lambda s: assocunify(u, v, s, eq, n)])

    return (core.eq, u, v)
Beispiel #21
0
def test_unify_variable_with_itself_should_unify():
    valido = Relation()
    fact(valido, 0, 1)
    fact(valido, 1, 0)
    fact(valido, 1, 1)
    x = var()
    assert run(0, x, valido(x, x)) == (1, )
Beispiel #22
0
def test_unify():
    assert unify(1, 1, {}) == {}
    assert unify(1, 2, {}) == False
    assert unify(var(1), 2, {}) == {var(1): 2}
    assert unify(2, var(1), {}) == {var(1): 2}
    assert unify((1, 2), (1, 2), {}) == {}
    assert unify((1, 2), (1, 2, 3), {}) == False
    assert unify((1, var(1)), (1, 2), {}) == {var(1): 2}
    assert unify((1, var(1)), (1, 2), {var(1): 3}) == False
Beispiel #23
0
def test_bindstar():
    x = var('x')
    stream = tuple({x: i} for i in range(5))
    success = lambda s: (s,)
    assert tuple(bindstar(stream, success)) == stream
    assert tuple(bindstar(stream, eq(x, 3))) == ({x: 3},)
    assert tuple(bindstar(stream, success, eq(x, 3))) == ({x: 3},)
    assert tuple(bindstar(stream, eq(x, 2), eq(x, 3))) == ()
Beispiel #24
0
def test_buildo():
    x = var('x')
    assert results(
        buildo('add', (1, 2, 3), x), {}) == ({x: ('add', 1, 2, 3)}, )
    assert results(
        buildo(x, (1, 2, 3), ('add', 1, 2, 3)), {}) == ({x: 'add'}, )
    assert results(
        buildo('add', x, ('add', 1, 2, 3)), {}) == ({x: (1, 2, 3)}, )
Beispiel #25
0
def test_seteq():
    x = var('x')
    abc = tuple('abc')
    bca = tuple('bca')
    assert results(seteq(abc, bca))
    assert len(results(seteq(abc, x))) == 6
    assert len(results(seteq(x, abc))) == 6
    assert bca in run(0, x, seteq(abc, x))
    assert results(seteq((1, 2, 3), (3, x, 1))) == ({x: 2},)
Beispiel #26
0
def test_buildo_object():
    x = var('x')
    assert results(buildo(Add, (1, 2, 3), x), {}) == \
            ({x: add(1, 2, 3)}, )
    print(results(buildo(x, (1, 2, 3), add(1, 2, 3)), {}))
    assert results(buildo(x, (1, 2, 3), add(1, 2, 3)), {}) == \
            ({x: Add}, )
    assert results(buildo(Add, x, add(1, 2, 3)), {}) == \
            ({x: (1, 2, 3)}, )
Beispiel #27
0
def test_buildo_object():
    x = var('x')
    assert results(buildo(Add, (1,2,3), x), {}) == \
            ({x: add(1, 2, 3)},)
    print(results(buildo(x, (1, 2, 3), add(1, 2, 3)), {}))
    assert results(buildo(x, (1,2,3), add(1,2,3)), {}) == \
            ({x: Add},)
    assert results(buildo(Add, x, add(1,2,3)), {}) == \
            ({x: (1,2,3)},)
Beispiel #28
0
def test_permuteq():
    x = var('x')
    assert results(permuteq((1, 2, 2), (2, 1, 2)))
    assert not results(permuteq((1, 2), (2, 1, 2)))
    assert not results(permuteq((1, 2, 3), (2, 1, 2)))
    assert not results(permuteq((1, 2, 1), (2, 1, 2)))

    assert set(run(0, x, permuteq(x, (1, 2, 2)))) == set(
        ((1, 2, 2), (2, 1, 2), (2, 2, 1)))
Beispiel #29
0
def test_permuteq():
    x = var('x')
    assert results(permuteq((1,2,2), (2,1,2)))
    assert not results(permuteq((1,2), (2,1,2)))
    assert not results(permuteq((1,2,3), (2,1,2)))
    assert not results(permuteq((1,2,1), (2,1,2)))

    assert set(run(0, x, permuteq(x, (1,2,2)))) == set(
            ((1,2,2), (2,1,2), (2,2,1)))
Beispiel #30
0
def test_buildo_object():
    x = var('x')
    assert results(buildo(add, (1,2,3), x, op_registry), {}) == \
            ({x: add(1, 2, 3)},)
    print results(buildo(x, (1,2,3), add(1,2,3), op_registry), {})
    assert results(buildo(x, (1,2,3), add(1,2,3), op_registry), {}) == \
            ({x: add},)
    assert results(buildo(add, x, add(1,2,3), op_registry), {}) == \
            ({x: (1,2,3)},)
Beispiel #31
0
def test_buildo():
    x = var('x')
    assert results(buildo('add', (1, 2, 3), x), {}) == ({
        x: ('add', 1, 2, 3)
    }, )
    assert results(buildo(x, (1, 2, 3), ('add', 1, 2, 3)), {}) == ({
        x: 'add'
    }, )
    assert results(buildo('add', x, ('add', 1, 2, 3)), {}) == ({
        x: (1, 2, 3)
    }, )
Beispiel #32
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)))
Beispiel #33
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)))
Beispiel #34
0
def eq_assoccomm(u, v):
    """ Associative/Commutative eq

    Works like logic.core.eq but supports associative/commutative expr trees

    tree-format:  (op, *args)
    example:      (add, 1, 2, 3)

    State that operations are associative or commutative with relations

    >>> from logpy.assoccomm import eq_assoccomm as eq
    >>> from logpy.assoccomm import commutative, associative
    >>> from logpy import fact, run, var

    >>> fact(commutative, 'add')    # declare that 'add' is commutative
    >>> fact(associative, 'add')    # declare that 'add' is associative

    >>> x = var()
    >>> e1 = ('add', 1, 2, 3)
    >>> e2 = ('add', 1, x)
    >>> run(0, x, eq(e1, e2))
    (('add', 2, 3), ('add', 3, 2))
    """
    try:
        uop, uargs = op_args(u)
        vop, vargs = op_args(v)
    except ValueError:
        return (eq, u, v)

    if uop and not vop and not isvar(v):
        return fail
    if vop and not uop and not isvar(u):
        return fail
    if uop and vop and not uop == vop:
        return fail
    if uop and not (uop,) in associative.facts:
        return (eq, u, v)
    if vop and not (vop,) in associative.facts:
        return (eq, u, v)

    if uop and vop:
        u, v = (u, v) if len(uargs) >= len(vargs) else (v, u)
        n = min(map(len, (uargs, vargs)))  # length of shorter tail
    else:
        n = None
    if vop and not uop:
        u, v = v, u
    w = var()
    return (lall, (eq_assoc, u, w, eq_assoccomm, n),
                  (eq_comm, v, w, eq_assoccomm))
Beispiel #35
0
def eq_assoccomm(u, v):
    """ Associative/Commutative eq

    Works like logic.core.eq but supports associative/commutative expr trees

    tree-format:  (op, *args)
    example:      (add, 1, 2, 3)

    State that operations are associative or commutative with relations

    >>> from logpy.assoccomm import eq_assoccomm as eq
    >>> from logpy.assoccomm import commutative, associative
    >>> from logpy import fact, run, var

    >>> fact(commutative, 'add')    # declare that 'add' is commutative
    >>> fact(associative, 'add')    # declare that 'add' is associative

    >>> x = var()
    >>> e1 = ('add', 1, 2, 3)
    >>> e2 = ('add', 1, x)
    >>> run(0, x, eq(e1, e2))
    (('add', 2, 3), ('add', 3, 2))
    """
    for typ, fn in seq_registry:
        if isinstance(u, typ):
            u = fn(u)
        if isinstance(v, typ):
            v = fn(v)
    if isinstance(u, tuple) and not isinstance(v, tuple) and not isvar(v):
        return fail
    if isinstance(v, tuple) and not isinstance(u, tuple) and not isvar(u):
        return fail
    if isinstance(u, tuple) and isinstance(v, tuple) and not u[0] == v[0]:
        return fail
    if isinstance(u, tuple) and not (u[0],) in associative.facts:
        return (eq, u, v)
    if isinstance(v, tuple) and not (v[0],) in associative.facts:
        return (eq, u, v)

    if isinstance(u, tuple) and isinstance(v, tuple):
        u, v = (u, v) if len(u) >= len(v) else (v, u)
        n = len(v)-1  # length of shorter tail
    else:
        n = None
    if isinstance(v, tuple) and not isinstance(u, tuple):
        u, v = v, u
    w = var()
    return lall((eq_assoc, u, w, eq_assoccomm, n),
                (eq_comm, v, w, eq_assoccomm))
Beispiel #36
0
def eq_assoc(u, v, eq=core.eq):
    """ Goal for associative equality

    >>> from logpy import run, var
    >>> from logpy.assoccomm import eq_assoc as eq

    >>> fact(commutative, 'add')    # declare that 'add' is commutative
    >>> fact(associative, 'add')    # declare that 'add' is associative

    >>> x = var()
    >>> run(0, eq(('add', 1, 2, 3), ('add', 1, x)))
    (('add', 2, 3),)
    """
    op = var()
    return conde([(core.eq, u, v)],
                 [(heado, op, u), (heado, op, v), (associative, op),
                  lambda s: assocunify(u, v, s, eq)])
Beispiel #37
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"))
Beispiel #38
0
def eq_assoccomm(u, v):
    """ Associative/Commutative eq

    Works like logic.core.eq but supports associative/commutative expr trees

    tree-format:  (op, *args)
    example:      (add, 1, 2, 3)

    State that operations are associative or commutative with relations

    >>> from logpy.assoccomm import eq_assoccomm as eq
    >>> from logpy.assoccomm import commutative, associative
    >>> from logpy import fact, run, var

    >>> fact(commutative, 'add')    # declare that 'add' is commutative
    >>> fact(associative, 'add')    # declare that 'add' is associative

    >>> x = var()
    >>> e1 = ('add', 1, 2, 3)
    >>> e2 = ('add', 1, x)
    >>> run(0, x, eq(e1, e2))
    (('add', 2, 3), ('add', 3, 2))
    """
    if isinstance(u, tuple) and not isinstance(v, tuple) and not isvar(v):
        return fail
    if isinstance(v, tuple) and not isinstance(u, tuple) and not isvar(u):
        return fail
    if isinstance(u, tuple) and isinstance(v, tuple) and not u[0] == v[0]:
        return fail
    if isinstance(u, tuple) and not (u[0],) in associative.facts:
        return (eq, u, v)
    if isinstance(v, tuple) and not (v[0],) in associative.facts:
        return (eq, u, v)

    if isinstance(u, tuple) and isinstance(v, tuple):
        u, v = (u, v) if len(u) >= len(v) else (v, u)
        n = len(v)-1  # length of shorter tail
    else:
        n = None
    if isinstance(v, tuple) and not isinstance(u, tuple):
        u, v = v, u
    w = var()
    return lall((eq_assoc, u, w, eq_assoccomm, n),
                (eq_comm, v, w, eq_assoccomm))
Beispiel #39
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"))

    foo = Relation('foo')
    assert 'foo' in str(foo)
Beispiel #40
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()
Beispiel #41
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
Beispiel #42
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
Beispiel #43
0
def test_conde():
    x = var('x')
    assert results(conde([eq(x, 2)], [eq(x, 3)])) == ({x: 2}, {x: 3})
    assert results(conde([eq(x, 2), eq(x, 3)])) == ()
Beispiel #44
0
def test_uneval_membero():
    x, y = var('x'), var('y')
    assert set(run(100, x, (membero, y, ((1,2,3),(4,5,6))), (membero, x, y))) == \
           set((1,2,3,4,5,6))
Beispiel #45
0
def test_early():
    x, y = var(), var()
    assert run(0, x, lallearly((eq, y, (1, 2)), (membero, x, y)))
    assert run(0, x, lallearly((membero, x, y), (eq, y, (1, 2))))
Beispiel #46
0
def test_lany_is_early_safe():
    x = var()
    y = var()
    assert run(0, x, lany((membero, x, y), (eq, x, 2))) == (2, )
Beispiel #47
0
def test_dict():
    x = var()
    assert run(0, x, eq({1: x}, {1: 2})) == (2, )
Beispiel #48
0
def test_eq():
    x = var('x')
    assert tuple(eq(x, 2)({})) == ({x: 2}, )
    assert tuple(eq(x, 2)({x: 3})) == ()
Beispiel #49
0
def test_lany():
    x = var('x')
    assert len(tuple(lany(eq(x, 2), eq(x, 3))({}))) == 2
    assert len(tuple(lany((eq, x, 2), (eq, x, 3))({}))) == 2
Beispiel #50
0
def test_lall():
    x = var('x')
    assert results(lall((eq, x, 2))) == ({x: 2}, )
    assert results(lall((eq, x, 2), (eq, x, 3))) == ()
Beispiel #51
0
def test_earlysafe():
    x, y = var('x'), var('y')
    assert earlysafe((eq, 2, 2))
    assert earlysafe((eq, 2, 3))
    assert earlysafe((membero, x, (1, 2, 3)))
    assert not earlysafe((membero, x, y))
Beispiel #52
0
def test_earlyorder():
    x, y = var(), var()
    assert earlyorder((eq, 2, x)) == ((eq, 2, x), )
    assert earlyorder((eq, 2, x), (eq, 3, x)) == ((eq, 2, x), (eq, 3, x))
    assert earlyorder((membero, x, y),
                      (eq, y, (1, 2, 3)))[0] == (eq, y, (1, 2, 3))
Beispiel #53
0
def test_run_output_reify():
    x = var()
    assert run(0, (1, 2, x), eq(x, 3)) == ((1, 2, 3), )
Beispiel #54
0
def test_membero():
    x = var('x')
    assert set(run(5, x, membero(x, (1, 2, 3)), membero(x, (2, 3, 4)))) == set(
        (2, 3))

    assert run(5, x, membero(2, (1, x, 3))) == (2, )
# to install logpy use:
# pip install logic

import itertools as it
import logpy.core as lc
from sympy.ntheory.generate import prime, isprime

# Check if the elements of x are prime 
def check_prime(x):
    if lc.isvar(x):
        return lc.condeseq([(lc.eq, x, p)] for p in map(prime, it.count(1)))
    else:
        return lc.success if isprime(x) else lc.fail

# Declate the variable
x = lc.var()

# Print first 7 prime numbers
print('\nList of first 7 prime numbers:')
print(lc.run(20, x, check_prime(x)))
Beispiel #56
0
def test_short_circuit():
    def badgoal(s):
        raise NotImplementedError()

    x = var('x')
    tuple(run(5, x, fail, badgoal))  # Does not raise exception