def test_map_anyo_misc(): q_lv = var("q") res = run(0, q_lv, map_anyo(eq, [1, 2, 3], [1, 2, 3])) # TODO: Remove duplicate results assert len(res) == 7 res = run(0, q_lv, map_anyo(eq, [1, 2, 3], [1, 3, 3])) assert len(res) == 0 def one_to_threeo(x, y): return conde([eq(x, 1), eq(y, 3)]) res = run(0, q_lv, map_anyo(one_to_threeo, [1, 2, 4, 1, 4, 1, 1], q_lv)) assert res[0] == [3, 2, 4, 3, 4, 3, 3] assert (len( run(4, q_lv, map_anyo(math_reduceo, [etuple(mul, 2, var("x"))], q_lv))) == 0) test_res = run(4, q_lv, map_anyo(math_reduceo, [etuple(add, 2, 2), 1], q_lv)) assert test_res == ([etuple(mul, 2, 2), 1], ) test_res = run(4, q_lv, map_anyo(math_reduceo, [1, etuple(add, 2, 2)], q_lv)) assert test_res == ([1, etuple(mul, 2, 2)], ) test_res = run(4, q_lv, map_anyo(math_reduceo, q_lv, var("z"))) assert all(isinstance(r, list) for r in test_res) test_res = run(4, q_lv, map_anyo(math_reduceo, q_lv, var("z"), tuple)) assert all(isinstance(r, tuple) for r in test_res) x, y, z = var(), var(), var() def test_bin(a, b): return conde([eq(a, 1), eq(b, 2)]) res = run(10, (x, y), map_anyo(test_bin, x, y, null_type=tuple)) exp_res_form = ( ((1, ), (2, )), ((x, 1), (x, 2)), ((1, 1), (2, 2)), ((x, y, 1), (x, y, 2)), ((1, x), (2, x)), ((x, 1, 1), (x, 2, 2)), ((1, 1, 1), (2, 2, 2)), ((x, y, z, 1), (x, y, z, 2)), ((1, x, 1), (2, x, 2)), ((x, 1, y), (x, 2, y)), ) for a, b in zip(res, exp_res_form): s = unify(a, b) assert s is not False assert all(isvar(i) for i in reify((x, y, z), s))
def test_map_anyo_reverse(): """Test `map_anyo` in "reverse" (i.e. specify the reduced form and generate the un-reduced form).""" # Unbounded reverse q_lv = var() rev_input = [etuple(mul, 2, 1)] test_res = run(4, q_lv, map_anyo(math_reduceo, q_lv, rev_input)) assert test_res == ( [etuple(add, 1, 1)], [etuple(log, etuple(exp, etuple(add, 1, 1)))], # [etuple(log, etuple(exp, etuple(mul, 2, 1)))], [ etuple(log, etuple(exp, etuple(log, etuple(exp, etuple(add, 1, 1))))) ], # [etuple(log, etuple(exp, etuple(log, etuple(exp, etuple(mul, 2, 1)))))], [ etuple( log, etuple( exp, etuple( log, etuple(exp, etuple(log, etuple(exp, etuple(add, 1, 1))))), ), ) ], ) # Guided reverse test_res = run( 4, q_lv, map_anyo(math_reduceo, [etuple(add, q_lv, 1)], [etuple(mul, 2, 1)]), ) assert test_res == (1, )
def test_map_anyo_types(): """Make sure that `map_anyo` preserves the types between its arguments.""" q_lv = var() res = run(1, q_lv, map_anyo(lambda x, y: eq(x, y), [1], q_lv)) assert res[0] == [1] res = run(1, q_lv, map_anyo(lambda x, y: eq(x, y), (1, ), q_lv)) assert res[0] == (1, ) res = run(1, q_lv, map_anyo(lambda x, y: eq(x, y), q_lv, (1, ))) assert res[0] == (1, ) res = run(1, q_lv, map_anyo(lambda x, y: eq(x, y), q_lv, [1])) assert res[0] == [1] res = run(1, q_lv, map_anyo(lambda x, y: eq(x, y), [1, 2], [1, 2])) assert len(res) == 1 res = run(1, q_lv, map_anyo(lambda x, y: eq(x, y), [1, 2], [1, 3])) assert len(res) == 0 res = run(1, q_lv, map_anyo(lambda x, y: eq(x, y), [1, 2], (1, 2))) assert len(res) == 0
def test_map_anyo(test_input, test_output): """Test `map_anyo` with fully ground terms (i.e. no logic variables).""" q_lv = var() test_res = run( 0, q_lv, map_anyo(math_reduceo, test_input, q_lv), ) assert len(test_res) == len(test_output) test_res = sorted(test_res) test_output = sorted(test_output) # Make sure the first result matches. # TODO: This is fairly implementation-specific (i.e. dependent on the order # in which `condeseq` returns results). if len(test_output) > 0: assert test_res[0] == test_output[0] # Make sure all the results match. # TODO: If we want to avoid fixing the output order, convert the lists to # tuples and add everything to a set, then compare. assert test_res == test_output