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
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))
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))
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), )
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))
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, )
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))) == ()
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}
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)))
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)))
def test_conso(): 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)}, ) assert run(1, y, conso(1, x, y)) == (LCons(1, x), ) assert list(run(1, y, conso(1, x, y))[0]) == [1] assert list(run(1, y, conso(1, x, y), conso(2, z, x))[0]) == [1, 2]
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, )
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)))
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)))
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, )
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)) == ()
def test_permuteq(): 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)))
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)))
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},)
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"))
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)
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,)
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),)
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), )
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, )
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))))
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))
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,)
def test_run_output_reify(): x = var() assert run(0, (1, 2, x), eq(x, 3)) == ((1, 2, 3),)
def test_eq_assoc(): assert run(0, x, eq_assoc((c, 1, 2, 3), (c, x, 3))) == ((c, 1, 2),)
def test_short_circuit(): def badgoal(s): raise NotImplementedError() x = var("x") tuple(run(5, x, fail, badgoal)) # Does not raise exception
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))
def test_dict(): x = var() assert run(0, x, eq({1: x}, {1: 2})) == (2, )
def test_deep_commutativity(): x, y = var('x'), var('y') e1 = (c, (c, 1, x), y) e2 = (c, 2, (c, 3, 1)) assert run(0, (x, y), eq_comm(e1, e2)) == ((3, 2), )
def test_deep_commutativity(): x, y = var("x"), var("y") e1 = (c, (c, 1, x), y) e2 = (c, 2, (c, 3, 1)) assert run(0, (x, y), eq_comm(e1, e2)) == ((3, 2),)
def test_lany_is_early_safe(): x = var() y = var() assert run(0, x, lany((membero, x, y), (eq, x, 2))) == (2,)
def test_dict(): x = var() assert run(0, x, eq({1: x}, {1: 2})) == (2,)
def test_conso_early(): x, y, z = var(), var(), var() assert (run(0, x, (conso, x, y, z), (eq, z, (1, 2, 3))) == (1, ))
#!/usr/bin/python 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() # Check if an element in the list is a prime number list_nums = (23, 4, 27, 17, 13, 10, 21, 29, 3, 32, 11, 19) print('\nList of primes in the list:') print(set(lc.run(0, x, (lc.membero, x, list_nums), (check_prime, x)))) # Print first 7 prime numbers print('\nList of first 7 prime numbers:') print(lc.run(7, x, check_prime(x)))
def test_lany_is_early_safe(): x = var() y = var() assert run(0, x, lany((membero, x, y), (eq, x, 2))) == (2, )
""" Example using SymPy to construct a prime number goal """ from logpy.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)
def test_short_circuit(): def badgoal(s): raise NotImplementedError() x = var('x') tuple(run(5, x, fail, badgoal)) # Does not raise exception
def test_appendo(): x = var('x') 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), )
""" Example using SymPy to construct a prime number goal """ from logpy.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)
def test_typo(): x = var('x') assert results(typo(3, int)) assert not results(typo(3.3, int)) assert run(0, x, membero(x, (1, 'cat', 2.2, 'hat')), (typo, x, str)) ==\ ('cat', 'hat')
def test_eq_comm(): assert set(run(0, x, eq_comm((c, 1, 2, 3), (c, x, 3)))) == set(((c, 1, 2), (c, 2, 1)))
def test_run_output_reify(): x = var() assert run(0, (1, 2, x), eq(x, 3)) == ((1, 2, 3), )
# 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)))
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, )