예제 #1
0
def test_variables_classification():
    tw = tarskiworld.create_small_world()
    x = tw.variable('x', tw.Object)
    y = tw.variable('y', tw.Object)
    s = neg(land(tw.Cube(x), exists(y, land(tw.Tet(x), tw.LeftOf(x, y)))))
    free = free_variables(s)
    assert len(free) == 1 and symref(free[0]) == symref(x)
    assert len(all_variables(s)) == 2
def test_detect_free_variables():
    tw = tarskiworld.create_small_world()
    x = tw.variable('x', tw.Object)
    y = tw.variable('y', tw.Object)
    s = neg(land(tw.Cube(x), exists(y, land(tw.Tet(x), tw.LeftOf(x, y)))))
    v = CollectFreeVariables(tw)
    s.accept(v)
    assert len(list(v.free_variables)) == 1
def test_nnf_LPL_page_321_antecedent():
    tw = tarskiworld.create_small_world()
    x = tw.variable('x', tw.Object)
    y = tw.variable('y', tw.Object)
    s = forall(x, neg(land(tw.Cube(x), exists(y, land(tw.Tet(x), tw.LeftOf(x, y))))))
    result = NNFTransformation.rewrite(s)
    gamma = forall(x, lor(neg(tw.Cube(x)), forall(y, lor(neg(tw.Tet(x)), neg(tw.LeftOf(x, y))))))
    assert str(result.nnf) == str(gamma)
예제 #4
0
def create_small_world_elements(numobjects=3):
    lang = tarskiworld.create_small_world()
    x, y = lang.variable('x', lang.Object), lang.variable('y', lang.Object)
    _ = [
        lang.constant(f'obj{i}', lang.Object)
        for i in range(1, numobjects + 1)
    ]
    return lang, x, y
예제 #5
0
def test_quantifier_elimination_fails_due_to_no_constants():
    tw = tarskiworld.create_small_world()
    x = tw.variable('x', tw.Object)
    y = tw.variable('y', tw.Object)
    s1 = exists(y, land(tw.Dodec(y), tw.BackOf(x, y)))
    s2 = land(tw.Cube(x), exists(y, land(tw.Tet(y), tw.LeftOf(x, y))))
    phi = forall(x, implies(s2, s1))
    with pytest.raises(TransformationError):
        QuantifierElimination.rewrite(tw, phi, QuantifierEliminationMode.All)
예제 #6
0
def test_symbol_ref_in_sets_equality_is_exact_syntactic_match():
    tw = tarskiworld.create_small_world()
    x = tw.variable('x', tw.Object)
    y = tw.variable('y', tw.Object)
    x_ref = symref(x)
    y_ref = symref(y)
    S = set()
    S.add(x_ref)
    assert y_ref not in S
    assert x_ref in S
    S.remove(x_ref)
    assert len(S) == 0
def test_prenex_lpl_page_321():
    tw = tarskiworld.create_small_world()
    x = tw.variable('x', tw.Object)

    y = tw.variable('y', tw.Object)
    s1 = exists(y, land(tw.Dodec(y), tw.BackOf(x, y)))
    s2 = land(tw.Cube(x), exists(y, land(tw.Tet(y), tw.LeftOf(x, y))))
    phi = forall(x, implies(s2, s1))
    result = PrenexTransformation.rewrite(tw, phi)
    yp = tw.variable("y'", tw.Object)
    gamma = NNFTransformation.rewrite(exists(yp, forall(
        x, y, implies(land(tw.Cube(x), land(tw.Tet(y), tw.LeftOf(x, y))), land(tw.Dodec(yp), tw.BackOf(x, yp)))))).nnf

    assert str(result.prenex) == str(gamma)
예제 #8
0
def test_cnf_conversion_easy():
    tw = tarskiworld.create_small_world()

    obj1 = tw.constant('obj1', tw.Object)
    obj2 = tw.constant('obj2', tw.Object)
    _ = tw.constant('obj3', tw.Object)

    s1 = land(tw.Cube(obj1), neg(tw.Tet(obj2)))
    s2 = land(tw.Cube(obj2), neg(tw.Tet(obj1)))
    phi = lor(s1, s2)
    c1 = lor(tw.Cube(obj1), tw.Cube(obj2))
    c2 = lor(tw.Cube(obj1), neg(tw.Tet(obj1)))
    c3 = lor(neg(tw.Tet(obj2)), tw.Cube(obj2))
    c4 = lor(neg(tw.Tet(obj2)), neg(tw.Tet(obj1)))
    psi = land(land(c1, c2), land(c3, c4))
    result = CNFTransformation.rewrite(tw, phi)
    assert str(result.cnf) == str(psi)
예제 #9
0
def test_universal_elimination_works():
    tw = tarskiworld.create_small_world()
    x = tw.variable('x', tw.Object)

    y = tw.variable('y', tw.Object)

    _ = tw.constant('obj1', tw.Object)
    _ = tw.constant('obj2', tw.Object)
    _ = tw.constant('obj3', tw.Object)

    s1 = exists(y, land(tw.Dodec(y), tw.BackOf(x, y)))
    s2 = land(tw.Cube(x), exists(y, land(tw.Tet(y), tw.LeftOf(x, y))))
    phi = forall(x, implies(s2, s1))
    # print(str(phi))
    result = remove_quantifiers(tw, phi, QuantifierEliminationMode.Forall)
    result2 = remove_quantifiers(tw, result, QuantifierEliminationMode.Forall)
    assert str(result) == str(result2)
def test_cnf_conversion_complex():
    tw = tarskiworld.create_small_world()
    x = tw.variable('x', tw.Object)

    y = tw.variable('y', tw.Object)

    obj1 = tw.constant('obj1', tw.Object)
    obj2 = tw.constant('obj2', tw.Object)
    obj3 = tw.constant('obj3', tw.Object)

    s1 = exists(y, land(tw.Dodec(y), tw.BackOf(x, y)))
    s2 = land(tw.Cube(x), exists(y, land(tw.Tet(y), tw.LeftOf(x, y))))
    phi = forall(x, implies(s2, s1))
    result = UniversalQuantifierElimination.rewrite(tw, phi)
    result2 = CNFTransformation.rewrite(tw, result.universal_free.formula)
    # print(result2.cnf)
    # print('\n'.join( [ ','.join([str(l) for l in c]) for c in result2.clauses ] ) )
    # print(len(result2.clauses))
    assert len(result2.clauses) == 34
def test_universal_elimination_works():
    tw = tarskiworld.create_small_world()
    x = tw.variable('x', tw.Object)

    y = tw.variable('y', tw.Object)

    obj1 = tw.constant('obj1', tw.Object)
    obj2 = tw.constant('obj2', tw.Object)
    obj3 = tw.constant('obj3', tw.Object)

    s1 = exists(y, land(tw.Dodec(y), tw.BackOf(x, y)))
    s2 = land(tw.Cube(x), exists(y, land(tw.Tet(y), tw.LeftOf(x, y))))
    phi = forall(x, implies(s2, s1))
    # print(str(phi))
    result = UniversalQuantifierElimination.rewrite(tw, phi)
    # print(str(result.universal_free))
    result2 = UniversalQuantifierElimination.rewrite(tw, result.universal_free)
    # print(str(result2.universal_free))
    assert str(result.universal_free) == str(result2.universal_free)
예제 #12
0
def test_detect_free_variables():
    tw = tarskiworld.create_small_world()
    x = tw.variable('x', tw.Object)
    y = tw.variable('y', tw.Object)
    s = neg(land(tw.Cube(x), exists(y, land(tw.Tet(x), tw.LeftOf(x, y)))))
    assert len(free_variables(s)) == 1