Exemplo n.º 1
0
def test_appendo2():
    for t in [tuple(range(i)) for i in range(5)]:
        for xi, yi in run(0, (x, y), appendo(x, y, t)):
            assert xi + yi == t
        results = run(0, (x, y, z), (appendo, x, y, w), (appendo, w, z, t))
        for xi, yi, zi in results:
            assert xi + yi + zi == t
Exemplo n.º 2
0
def test_goal_ordering():
    # Regression test for https://github.com/logpy/logpy/issues/58

    def lefto(q, p, lst):
        if isvar(lst):
            raise EarlyGoalError()
        return membero((q, p), zip(lst, lst[1:]))

    vals = var()

    # Verify the solution can be computed when we specify the execution
    # ordering.
    rules_greedy = (
        lallgreedy,
        (eq, (var(), var()), vals),
        (lefto, 'green', 'white', vals),
    )

    solution, = run(1, vals, rules_greedy)
    assert solution == ('green', 'white')

    # Verify that attempting to compute the "safe" order does not itself cause
    # the evaluation to fail.
    rules_greedy = (
        lall,
        (eq, (var(), var()), vals),
        (lefto, 'green', 'white', vals),
    )

    solution, = run(1, vals, rules_greedy)
    assert solution == ('green', 'white')
Exemplo n.º 3
0
def test_appendo_reorder():
    # XXX: This test generates goal conjunctions that are non-terminating given
    # the specified goal ordering.  More specifically, it generates
    # `lall(appendo(x, y, w), appendo(w, z, ()))`, for which the first
    # `appendo` produces an infinite stream of results and the second
    # necessarily fails for all values of the first `appendo` yielding
    # non-empty `w` unifications.
    #
    # The only reason it worked before is the `EarlyGoalError`
    # and it's implicit goal reordering, which made this case an out-of-place
    # test for a goal reordering feature that has nothing to do with `appendo`.
    # Furthermore, the `EarlyGoalError` mechanics do *not* fix this general
    # problem, and it's trivial to generate an equivalent situation in which
    # an `EarlyGoalError` is never thrown.
    #
    # In other words, it seems like a nice side effect of `EarlyGoalError`, but
    # it's actually a very costly approach that masks a bigger issue; one that
    # all miniKanren programmers need to think about when developing.

    x, y, z, w = var(), var(), var(), var()
    for t in [tuple(range(i)) for i in range(5)]:
        print(t)
        for xi, yi in run(0, (x, y), appendo(x, y, t)):
            assert xi + yi == t

        results = run(2, (x, y, z, w), appendo(x, y, w), appendo(w, z, t))
        for xi, yi, zi, wi in results:
            assert xi + yi + zi == t
Exemplo n.º 4
0
def test_appendo():
    assert results(appendo((), (1, 2), (1, 2))) == ({}, )
    assert results(appendo((), (1, 2), (1))) == ()
    assert results(appendo((1, 2), (3, 4), (1, 2, 3, 4)))
    assert run(5, x, appendo((1, 2, 3), x, (1, 2, 3, 4, 5))) == ((4, 5), )
    assert run(5, x, appendo(x, (4, 5), (1, 2, 3, 4, 5))) == ((1, 2, 3), )
    assert run(5, x, appendo((1, 2, 3), (4, 5), x)) == ((1, 2, 3, 4, 5), )
Exemplo n.º 5
0
def test_appendo2():
    for t in [tuple(range(i)) for i in range(5)]:
        for xi, yi in run(0, (x, y), appendo(x, y, t)):
            assert xi + yi == t
        results = run(0, (x, y, z), (appendo, x, y, w), (appendo, w, z, t))
        for xi, yi, zi in results:
            assert xi + yi + zi == t
Exemplo n.º 6
0
def test_run():
    x, y, z = map(var, 'xyz')
    assert run(1, x, eq(x, 1)) == (1, )
    assert run(2, x, eq(x, 1)) == (1, )
    assert run(0, x, eq(x, 1)) == (1, )
    assert run(1, x, eq(x, (y, z)), eq(y, 3), eq(z, 4)) == ((3, 4), )
    assert set(run(2, x, conde([eq(x, 1)], [eq(x, 2)]))) == set((1, 2))
Exemplo n.º 7
0
def test_appendo():
    assert results(appendo((), (1, 2), (1, 2))) == ({}, )
    assert results(appendo((), (1, 2), (1))) == ()
    assert results(appendo((1, 2), (3, 4), (1, 2, 3, 4)))
    assert run(5, x, appendo((1, 2, 3), x, (1, 2, 3, 4, 5))) == ((4, 5), )
    assert run(5, x, appendo(x, (4, 5), (1, 2, 3, 4, 5))) == ((1, 2, 3), )
    assert run(5, x, appendo((1, 2, 3), (4, 5), x)) == ((1, 2, 3, 4, 5), )
Exemplo n.º 8
0
def test_assoc_flatten():

    add = "add"
    mul = "mul"

    fact(commutative, add)
    fact(associative, add)
    fact(commutative, mul)
    fact(associative, mul)

    assert (run(
        0,
        True,
        assoc_flatten((mul, 1, (add, 2, 3), (mul, 4, 5)),
                      (mul, 1, (add, 2, 3), 4, 5)),
    ) == (True, ))

    x = var()
    assert (run(
        0,
        x,
        assoc_flatten((mul, 1, (add, 2, 3), (mul, 4, 5)), x),
    ) == ((mul, 1, (add, 2, 3), 4, 5), ))

    assert (run(
        0,
        True,
        assoc_flatten(("op", 1, (add, 2, 3), (mul, 4, 5)),
                      ("op", 1, (add, 2, 3), (mul, 4, 5))),
    ) == (True, ))

    assert run(0, x,
               assoc_flatten(("op", 1, (add, 2, 3), (mul, 4, 5)),
                             x)) == (("op", 1, (add, 2, 3), (mul, 4, 5)), )
Exemplo n.º 9
0
def test_goal_ordering():
    # Regression test for https://github.com/logpy/logpy/issues/58

    def lefto(q, p, lst):
        if isvar(lst):
            raise EarlyGoalError()
        return membero((q, p), zip(lst, lst[1:]))

    vals = var()

    # Verify the solution can be computed when we specify the execution
    # ordering.
    rules_greedy = (
        lallgreedy,
        (eq, (var(), var()), vals),
        (lefto, 'green', 'white', vals),
    )

    solution, = run(1, vals, rules_greedy)
    assert solution == ('green', 'white')

    # Verify that attempting to compute the "safe" order does not itself cause
    # the evaluation to fail.
    rules_greedy = (
        lall,
        (eq, (var(), var()), vals),
        (lefto, 'green', 'white', vals),
    )

    solution, = run(1, vals, rules_greedy)
    assert solution == ('green', 'white')
Exemplo n.º 10
0
def test_membero():
    x = var('x')
    assert set(run(5, x, membero(x, (1, 2, 3)), membero(x, (2, 3, 4)))) \
           == {2, 3}

    assert run(5, x, membero(2, (1, x, 3))) == (2, )
    assert run(0, x, (membero, 1, (1, 2, 3))) == (x, )
    assert run(0, x, (membero, 1, (2, 3))) == ()
Exemplo n.º 11
0
def test_condeseq():
    x = var('x')
    assert set(run(0, x, condeseq(([eq(x, 2)], [eq(x, 3)])))) == {2, 3}
    assert set(run(0, x, condeseq([[eq(x, 2), eq(x, 3)]]))) == set()

    goals = ([eq(x, i)] for i in count())  # infinite number of goals
    assert run(1, x, condeseq(goals)) == (0, )
    assert run(1, x, condeseq(goals)) == (1, )
Exemplo n.º 12
0
def test_membero():
    x = var('x')
    assert set(run(5, x, membero(x, (1, 2, 3)), membero(x, (2, 3, 4)))) \
           == {2, 3}

    assert run(5, x, membero(2, (1, x, 3))) == (2, )
    assert run(0, x, (membero, 1, (1, 2, 3))) == (x, )
    assert run(0, x, (membero, 1, (2, 3))) == ()
Exemplo n.º 13
0
def test_lanyseq():
    x = var()
    g = lany((eq(x, i) for i in range(3)))
    assert list(g({})) == [{x: 0}, {x: 1}, {x: 2}]
    assert list(g({})) == [{x: 0}, {x: 1}, {x: 2}]

    # Test lanyseq with an infinite number of goals.
    assert set(run(3, x, lany((eq(x, i) for i in count())))) == {0, 1, 2}
    assert set(run(3, x, lany((eq(x, i) for i in count())))) == {0, 1, 2}
Exemplo n.º 14
0
def test_assoccomm_objects():

    fact(commutative, Add)
    fact(associative, Add)

    x = var()

    assert run(0, True, eq_assoccomm(add(1, 2, 3), add(3, 1, 2))) == (True,)
    assert run(0, x, eq_assoccomm(add(1, 2, 3), add(1, 2, x))) == (3,)
    assert run(0, x, eq_assoccomm(add(1, 2, 3), add(x, 2, 1))) == (3,)
Exemplo n.º 15
0
def test_ground_order():
    x, y, z = var(), var(), var()
    assert run(0, x, ground_order((y, [1, z], 1), x)) == ([1, [1, z], y], )
    a, b, c = var(), var(), var()
    assert run(0, (a, b, c), ground_order((y, [1, z], 1),
                                          (a, b, c))) == ((1, [1, z], y), )
    res = run(0, z, ground_order([cons(x, y), (x, y)], z))
    assert res == ([(x, y), cons(x, y)], )
    res = run(0, z, ground_order([(x, y), cons(x, y)], z))
    assert res == ([(x, y), cons(x, y)], )
Exemplo n.º 16
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, )
    assert set(run(0, x, eq_comm(add(1, 2), x))) == set((add(1, 2), add(2, 1)))
    assert set(run(0, x, eq_assoccomm(add(1, 2, 3), add(1, x)))) == set(
        (add(2, 3), add(3, 2)))
Exemplo n.º 17
0
def test_lanyseq():
    x = var('x')
    g = lanyseq(((eq, x, i) for i in range(3)))
    assert list(goaleval(g)({})) == [{x: 0}, {x: 1}, {x: 2}]
    assert list(goaleval(g)({})) == [{x: 0}, {x: 1}, {x: 2}]

    # Test lanyseq with an infinite number of goals.
    assert set(run(3, x, lanyseq(((eq, x, i) for i in count())))) == {0, 1, 2}
    assert set(run(3, x, (lanyseq, ((eq, x, i) for i in count())))) == \
           {0, 1, 2}
Exemplo n.º 18
0
def test_lall(lall_impl):
    x, y = var('x'), var('y')
    assert results(lall_impl((eq, x, 2))) == ({x: 2}, )
    assert results(lall_impl((eq, x, 2), (eq, x, 3))) == ()
    assert results(lall_impl()) == ({}, )

    assert run(0, x, lall_impl((eq, y, (1, 2)), (membero, x, y)))
    assert run(0, x, lall_impl()) == (x, )
    with pytest.raises(EarlyGoalError):
        run(0, x, lall_impl(membero(x, y)))
Exemplo n.º 19
0
def test_seteq():
    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)))
Exemplo n.º 20
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, )

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

    assert set(run(0, x, eq_assoccomm(add(1, 2, 3), add(1, x)))) == \
        set((add(2, 3), add(3, 2)))
Exemplo n.º 21
0
Arquivo: prime.py Projeto: logpy/logpy
def test_primo():
    if not sg:
        pytest.skip("Test missing required library: sympy.ntheory.generate")

    x = var()
    res = (set(run(0, x, (membero, x, (20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
                                       30)), (primo, x))))

    assert {23, 29} == res

    assert ((run(5, x, primo(x)))) == (2, 3, 5, 7, 11)
Exemplo n.º 22
0
def test_conde():
    x = var()
    assert results(conde([eq(x, 2)], [eq(x, 3)])) == ({x: 2}, {x: 3})
    assert results(conde([eq(x, 2), eq(x, 3)])) == ()

    assert set(run(0, x, conde([eq(x, 2)], [eq(x, 3)]))) == {2, 3}
    assert set(run(0, x, conde([eq(x, 2), eq(x, 3)]))) == set()

    goals = ([eq(x, i)] for i in count())  # infinite number of goals
    assert run(1, x, conde(goals)) == (0, )
    assert run(1, x, conde(goals)) == (1, )
Exemplo n.º 23
0
def test_seteq():
    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)))
Exemplo n.º 24
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, )
Exemplo n.º 25
0
def test_lall_errors():
    class SomeException(Exception):
        pass

    def bad_relation():
        def _bad_relation(s):
            raise SomeException("some exception")

        return lall(_bad_relation)

    with raises(SomeException):
        run(0, var(), bad_relation())
Exemplo n.º 26
0
def test_primo():
    if not sg:
        pytest.skip("Test missing required library: sympy.ntheory.generate")

    x = var()
    res = (set(
        run(0, x, (membero, x, (20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30)),
            (primo, x))))

    assert {23, 29} == res

    assert ((run(5, x, primo(x)))) == (2, 3, 5, 7, 11)
Exemplo n.º 27
0
def test_lall_errors():
    """Make sure we report the originating exception when it isn't just an
    `EarlyGoalError`.
    """

    class SomeException(Exception):
        pass

    def bad_relation():
        def _bad_relation():
            raise SomeException("some exception")
        return (lall, (_bad_relation,))

    with raises(SomeException):
        run(0, var(), (bad_relation,))
Exemplo n.º 28
0
def test_rembero():

    q_lv = var()
    assert ([], ) == run(0, q_lv, rembero(1, [1], q_lv))
    assert ([], [1]) == run(0, q_lv, rembero(1, q_lv, []))

    expected_res = (
        [5, 1, 2, 3, 4],
        [1, 5, 2, 3, 4],
        [1, 2, 5, 3, 4],
        [1, 2, 3, 5, 4],
        [1, 2, 3, 4],
        [1, 2, 3, 4, 5],
    )
    assert expected_res == run(0, q_lv, rembero(5, q_lv, [1, 2, 3, 4]))
Exemplo n.º 29
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, )
Exemplo n.º 30
0
def test_heado():
    x, y, z = var(), var(), var()
    assert (x, 1) in results(heado(x, (1, 2, 3)))[0].items()
    assert (x, 1) in results(heado(1, (x, 2, 3)))[0].items()
    assert results(heado(x, ())) == ()

    assert run(0, x, heado(x, z), conso(1, y, z)) == (1, )
Exemplo n.º 31
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)) == ()
Exemplo n.º 32
0
def evalo(program: ast.AST, value: Var, replace_var: str = None):
    replace_var = replace_var or DEFAULT_REPLACE_VAR
    if value.token != replace_var:
        raise RuntimeError("Value {} should have token {}".format(
            value, replace_var))

    program = strip_ast(program)
    program = replace_ast_name_with_lvar(program, replace_var=replace_var)

    goals, env = eval_programo(program, [], value)
    res = run(1, value, goals)

    r = res[0]
    logger.info("Final result: {}".format(ast_dump_if_possible(res)))
    if isvar(r):
        logger.info("No results found for {}: {}".format(value, r))
        return r
    logger.info("Found {} to be {}".format(ast_dump_if_possible(value),
                                           ast_dump_if_possible(r)))
    # TODO: This only works on the last assign
    if isinstance(r, ast.Name):
        evaluated_value = literal_lookup(r.id, env)
        logger.info("Found evaluated value {}".format(
            ast_dump_if_possible(evaluated_value)))
        return evaluated_value
Exemplo n.º 33
0
def test_tailo():
    x, y, z = var(), var(), var()

    assert (x, (2, 3)) in results(tailo(x, (1, 2, 3)))[0].items()
    assert (x, ()) in results(tailo(x, (1, )))[0].items()
    assert results(tailo(x, ())) == ()

    assert run(0, y, tailo(y, z), conso(x, (1, 2), z)) == ((1, 2), )
Exemplo n.º 34
0
def test_permuteq():
    assert results(permuteq((1, 2), (2, 1)))
    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)))
Exemplo n.º 35
0
def test_permuteq():
    assert results(permuteq((1, 2), (2, 1)))
    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)))
Exemplo n.º 36
0
def test_ifa():
    x, y = var(), var()

    assert run(0, (x, y), ifa(lall(eq(x, True), eq(y, 1)),
                              eq(y, 2))) == ((True, 1), )
    assert run(0, y, eq(x, False),
               ifa(lall(eq(x, True), eq(y, 1)), lall(eq(y, 2)))) == (2, )
    assert (run(
        0,
        y,
        eq(x, False),
        ifa(lall(eq(x, True), eq(y, 1)), lall(eq(x, True), eq(y, 2))),
    ) == ())

    assert run(
        0,
        y,
        eq(x, True),
        ifa(lall(eq(x, True), eq(y, 1)), lall(eq(x, True), eq(y, 2))),
    ) == (1, )
Exemplo n.º 37
0
def test_uneval_membero():
    x, y = var(), var()
    assert set(run(100, x, membero(y, ((1, 2, 3), (4, 5, 6))),
                   membero(x, y))) == {
                       1,
                       2,
                       3,
                       4,
                       5,
                       6,
                   }
Exemplo n.º 38
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)
Exemplo n.º 39
0
def test_nullo_itero():
    assert isvar(run(0, y, nullo([]))[0])
    assert isvar(run(0, y, nullo(None))[0])
    assert run(0, y, nullo(y))[0] is None
    assert run(0, y, (conso, var(), y, [1]), nullo(y))[0] == []
    assert run(0, y, (conso, var(), y, (1,)), nullo(y))[0] == ()

    assert run(1, y, conso(1, x, y), itero(y))[0] == [1]
    assert run(1, y, conso(1, x, y), conso(2, z, x), itero(y))[0] == [1, 2]

    # Make sure that the remaining results end in logic variables
    res_2 = run(2, y, conso(1, x, y), conso(2, z, x), itero(y))[1]
    assert res_2[:2] == [1, 2]
    assert isvar(res_2[-1])
Exemplo n.º 40
0
def test_nullo_itero():
    assert isvar(run(0, y, nullo([]))[0])
    assert isvar(run(0, y, nullo(None))[0])
    assert run(0, y, nullo(y))[0] is None
    assert run(0, y, (conso, var(), y, [1]), nullo(y))[0] == []
    assert run(0, y, (conso, var(), y, (1, )), nullo(y))[0] == ()

    assert run(1, y, conso(1, x, y), itero(y))[0] == [1]
    assert run(1, y, conso(1, x, y), conso(2, z, x), itero(y))[0] == [1, 2]

    # Make sure that the remaining results end in logic variables
    res_2 = run(2, y, conso(1, x, y), conso(2, z, x), itero(y))[1]
    assert res_2[:2] == [1, 2]
    assert isvar(res_2[-1])
Exemplo n.º 41
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), )
Exemplo n.º 42
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(x, (1, )))
    assert results(eqac((1, ), x))
    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 not results(eqac((ac, 1, 1), ('other_op', 1, 1)))
    assert run(0, x, eqac((ac, 3, (ac, 1, 2)), (ac, 1, x, 3))) == (2, )
Exemplo n.º 43
0
def test_uneval_membero():
    assert set(run(100, x,
                   (membero, y, ((1, 2, 3), (4, 5, 6))),
                   (membero, x, y))) == \
           {1, 2, 3, 4, 5, 6}
Exemplo n.º 44
0
def test_conso_early():
    assert (run(0, x, (conso, x, y, z), (eq, z, (1, 2, 3))) == (1, ))
Exemplo n.º 45
0
def test_run_output_reify():
    x = var()
    assert run(0, (1, 2, x), eq(x, 3)) == ((1, 2, 3), )
Exemplo n.º 46
0
def test_heado():
    assert (x, 1) in results(heado(x, (1, 2, 3)))[0].items()
    assert (x, 1) in results(heado(1, (x, 2, 3)))[0].items()
    assert results(heado(x, ())) == ()

    assert run(0, x, (heado, x, z), (conso, 1, y, z)) == (1, )
Exemplo n.º 47
0
def test_short_circuit():
    def badgoal(s):
        raise NotImplementedError()

    x = var('x')
    tuple(run(5, x, fail, badgoal))  # Does not raise exception
Exemplo n.º 48
0
def test_tailo():
    assert (x, (2, 3)) in results((tailo, x, (1, 2, 3)))[0].items()
    assert (x, ()) in results((tailo, x, (1, )))[0].items()
    assert results((tailo, x, ())) == ()

    assert run(0, y, (tailo, y, z), (conso, x, (1, 2), z)) == ((1, 2), )
Exemplo n.º 49
0
def test_safe_reordering_lall(lall_impl):
    x, y = var('x'), var('y')
    assert run(0, x, lall_impl((membero, x, y), (eq, y, (1, 2)))) == (1, 2)
Exemplo n.º 50
0
def test_dict():
    x = var()
    assert run(0, x, eq({1: x}, {1: 2})) == (2, )
Exemplo n.º 51
0
def test_lany_is_early_safe():
    x = var()
    y = var()
    assert run(0, x, lany((membero, x, y), (eq, x, 2))) == (2, )
Exemplo n.º 52
0
""" Example using SymPy to construct a prime number goal """

from kanren.core import (isvar, success, fail, assoc, goaleval, var, run,
        membero, condeseq, eq)
from sympy.ntheory.generate import prime, isprime
import itertools as it

def primo(x):
    """ x is a prime number """
    if isvar(x):
        return condeseq([(eq, x, p)] for p in it.imap(prime, it.count(1)))
    else:
        return success if isprime(x) else fail

x = var()
print((set(run(0, x, (membero, x, (20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30)),
                    (primo, x)))))
# set([29, 33])

print((run(5, x, primo(x))))
# (2, 3, 5, 7, 11)