Exemple #1
0
def test_appendo():
    reset_names()
    w, x, y, z = variables("w, x, y, z")

    goal = conj(same(z, (x, y)), appendo(x, y, [1, 2, 3]))
    assert list(run(z, goal)) == [([], [1, 2, 3]), ([1], [2, 3]), ([1,
                                                                    2], [3]),
                                  ([1, 2, 3], [])]

    goal = conj(same(z, (x, y)), appendo([1, 2, 3], x, y))
    assert list(run(3, z, goal)) == [([], [1, 2, 3]), (['_0'], [1, 2, 3,
                                                                '_0']),
                                     (['_0', '_1'], [1, 2, 3, '_0', '_1'])]

    goal = conj(same(z, (x, y)), appendo(x, [1, 2, 3], y))
    assert list(run(3, z, goal)) == [([], [1, 2, 3]), (['_0'], ['_0', 1, 2,
                                                                3]),
                                     (['_0', '_1'], ['_0', '_1', 1, 2, 3])]

    goal = conj(same(z, (w, x, y)), appendo(w, x, y))
    assert list(run(6, z, goal)) == [([], [], []), ([], ['_0'], ['_0']),
                                     (['_0'], [], ['_0']),
                                     ([], ['_0', '_1'], ['_0', '_1']),
                                     (['_0'], ['_1'], ['_0', '_1']),
                                     (['_0', '_1'], [], ['_0', '_1'])]
Exemple #2
0
def test_disj():
    x = variables("x")
    goal = disj(same("olive", x), same("oil", x))
    assert set(goal(Substitution())) == {
        Substitution({x: "olive"}),
        Substitution({x: "oil"})
    }
Exemple #3
0
def test_ifte_disj():
    x, y = variables("x, y")
    goal = ifte(disj(same(True, x), same(False, x)), same(False, y),
                same(True, y))
    assert list(goal(Substitution())) == [
        Substitution({
            x: True,
            y: False
        }),
        Substitution({
            x: False,
            y: False
        })
    ]
Exemple #4
0
def test_disj_x_never():
    x = variables("x")
    goal = disj(same("olive", x), never)
    s_inf = goal(Substitution())
    assert next(s_inf) == Substitution({x: "olive"})
    s_inf = assert_suspended(s_inf)
    s_inf = assert_suspended(s_inf)
Exemple #5
0
def test_fresh_multiple_conditions():
    reset_names()
    goal = fresh(lambda x, y, z: (same(x, y), same(x, z)))
    r = goal(Substitution())
    x, y, z = variables('x1, y2, z3')
    allowed_results = [[Substitution({
        x: y,
        y: z
    })], [Substitution({
        x: y,
        z: x
    })], [Substitution({
        x: z,
        z: y
    })], [Substitution({
        x: z,
        y: z
    })]]
    assert list(r) in allowed_results
Exemple #6
0
 def cdro(l, d):
     return fresh(lambda a: same((a, d), l))
Exemple #7
0
def test_ifte_FAIL():
    y = variables("y")
    goal = ifte(fail, same(False, y), same(True, y))
    assert list(goal(Substitution())) == [Substitution({y: True})]
Exemple #8
0
def test_ifte_succeed():
    y = variables("y")
    goal = ifte(succeed, same(False, y), same(True, y))
    assert list(goal(Substitution())) == [Substitution({y: False})]
Exemple #9
0
def test_reify_run_goal_inf():
    x = variables("x")
    goal = disj(same("olive", x), same("oil", x))
    results = map(reify(x), run_goal(goal))
    assert list(results) == ['olive', 'oil']
Exemple #10
0
def test_reify_some():
    x = variables("x")
    goal = disj(same("olive", x), same("oil", x))
    subs = take(5, goal(Substitution()))
    results = map(reify(x), subs)
    assert list(results) == ['olive', 'oil']
Exemple #11
0
def test_fresh_one_var():
    reset_names()
    goal = fresh(lambda x: same(x, 1))
    r = goal(Substitution())
    assert list(r) == [Substitution({variables('x1'): 1})]
Exemple #12
0
 def nullo(x):
     return same(x, ())
Exemple #13
0
def test_same():
    x, y = variables("x, y")
    assert same.__doc__ == "produce a goal that succeeds if u and v are equivalent"
    assert set(same(True, False)(Substitution())) == set()
    assert set(same(x, y)(Substitution())) == {Substitution({x: y})}
Exemple #14
0
 def caro(l, a):
     return fresh(lambda d: (same(a, l[0]), same(d, l[1:])))
Exemple #15
0
 def teacupo(t):
     return disj(same('tea', t), same('cup', t))
Exemple #16
0
def test_run_fresh_ignore_query_var():
    q = variables('q')
    r = run(q, fresh(lambda x: same(x, 'pea')))
    assert list(r) == ['_0']
Exemple #17
0
def test_run_fresh():
    q = variables('q')
    r = run(q, fresh(lambda x: same([x], q)))
    assert list(r) == [['_0']]
Exemple #18
0
def test_fresh_multiple_vars():
    reset_names()
    goal = fresh(lambda x, y, z: same(x, y))
    r = goal(Substitution())
    assert list(r) == [Substitution({variables('x1'): variables('y2')})]
Exemple #19
0
def test_ifte_cond_suspended():
    y = variables("y")
    goal = ifte(suspend(succeed), same(False, y), same(True, y))
    s_inf = goal(Substitution())
    s_inf = assert_suspended(s_inf)
    assert list(s_inf) == [Substitution({y: False})]
Exemple #20
0
 def cdro(l, d):
     return fresh(lambda a: (same(a, l[0]), same(d, l[1:])))
Exemple #21
0
def test_ifte_g2_suspended():
    y = variables("y")
    goal = ifte(fail, same(False, y), suspend(same(True, y)))
    s_inf = goal(Substitution())
    s_inf = assert_suspended(s_inf)
    assert list(s_inf) == [Substitution({y: True})]
Exemple #22
0
def test_conj_succeed():
    x = variables("x")
    goal = conj(same(42, x), succeed)
    assert list(goal(Substitution())) == [Substitution({x: 42})]
Exemple #23
0
def test_symeq_integers():
    a, x, y, z = variables("a, x, y, z")
    goal = conj(same(a, (x, y, z)), rangeo(-1, y, 2), rangeo(1, z, 3),
                symeq(x + y, z))
    assert list(run(a, goal)) == [(2, -1, 1), (1, 0, 1), (3, -1, 2), (0, 1, 1),
                                  (2, 0, 2), (1, 1, 2)]
Exemple #24
0
def test_run_same_order_invariant():
    q = variables('q')
    assert list(run(q, same(q, 'pea'))) == list(run(q, same('pea', q)))
Exemple #25
0
def test_conj():
    x = variables("x")
    goal = conj(same("olive", x), same("oil", x))
    assert set(goal(Substitution())) == set()
Exemple #26
0
def test_run_same():
    q = variables('q')
    assert list(run(q, same(q, 'pea'))) == ['pea']