def test_parsing_lambda(): assert parse_formula("Lx.Ly.[x & y]") == Lambda( "x", Lambda("y", And(Var("x"), Var("y")))) assert parse_formula("L x.L y.[x & y]") == Lambda( "x", Lambda("y", And(Var("x"), Var("y")))) assert parse_formula("λx.λy.[x & y]") == Lambda( "x", Lambda("y", And(Var("x"), Var("y"))))
def test_parsing_binary_precedence(): assert parse_formula("x & y | z -> m") == IfThen( Or(And(Var("x"), Var("y")), Var("z")), Var("m")) assert parse_formula("x | y -> m & z") == IfThen(Or(Var("x"), Var("y")), And(Var("m"), Var("z"))) assert parse_formula("[x | y] & z") == And(Or(Var("x"), Var("y")), Var("z"))
def test_simplify_super_nested_call(): # (LP.P(a, b))(Lx.Ly.x & y) -> a & b tree = Call( Lambda("P", Call(Call(Var("P"), Var("a")), Var("b"))), Lambda("x", Lambda("y", And(Var("x"), Var("y")))), ) assert tree.simplify() == And(Var("a"), Var("b"))
def test_replace_variable_in_quantifiers(): tree = ForAll( "x", Or(And(ForAll("b", Var("b")), Exists("b", Var("b"))), Exists("y", Var("b"))), ) assert tree.replace_variable("b", Var("bbb")) == ForAll( "x", Or(And(ForAll("b", Var("b")), Exists("b", Var("b"))), Exists("y", Var("bbb"))), )
def test_parsing_call(): assert parse_formula("Happy(x)") == Call(Var("Happy"), Var("x")) assert parse_formula("Between(x, y & z, [Capital(france)])") == Call( Call(Call(Var("Between"), Var("x")), And(Var("y"), Var("z"))), Call(Var("Capital"), Var("france")), ) assert parse_formula("(Lx.x)(j)") == Call(Lambda("x", Var("x")), Var("j")) assert parse_formula("((Lx.Ly.x & y) (a)) (b)") == Call( Call(Lambda("x", Lambda("y", And(Var("x"), Var("y")))), Var("a")), Var("b"))
def test_recursive_replace_variable(): # BFP(x, Lx.x, x & y) tree = Call( Call( Call(Var("BFP"), Var("x")), Lambda("x", Var("x")), # This should not be replaced. ), And(Var("x"), Var("y")), ) assert tree.replace_variable("x", Var("j")) == Call( Call(Call(Var("BFP"), Var("j")), Lambda("x", Var("x"))), And(Var("j"), Var("y")) )
def test_call_to_str(): assert ( str(Call(Call(Var("P"), And(Var("a"), Var("b"))), Lambda("x", Var("x")))) == "P(a & b, λx.x)" ) assert str(Call(Var("P"), Var("x"))) == "P(x)"
def test_lambda_to_str(): tree = Lambda("x", And(Var("a"), Var("x"))) assert str(tree) == "λx.a & x" assert tree.ascii_str() == "Lx.a & x" # This formula is semantically invalid but that doesn't matter. assert str(And(Lambda("x", Var("x")), Lambda("y", Var("y")))) == "[λx.x] & [λy.y]"
def test_and_to_str(): assert str(And(Var("a"), Var("b"))) == "a & b"
def test_replace_variable_in_iota(): tree = Iota("x", And(Var("x"), Var("y"))) assert tree.replace_variable("x", Var("a")) == tree assert tree.replace_variable("y", Var("b")) == Iota("x", And(Var("x"), Var("b")))
def test_mary_is_bad_and_john_is_good_is_true(): formula = And(Call(Var("Bad"), Var("m")), Call(Var("Good"), Var("j"))) assert interpret_formula(formula, test_model)
def test_simplify_nested_call(): # (Lx.Ly.x & y)(a)(b) -> a & b tree = Call( Call(Lambda("x", Lambda("y", And(Var("x"), Var("y")))), Var("a")), Var("b")) assert tree.simplify() == And(Var("a"), Var("b"))
def test_parsing_for_all(): assert parse_formula("Ax.x & y") == ForAll("x", And(Var("x"), Var("y"))) assert parse_formula("A x.x & y") == ForAll("x", And(Var("x"), Var("y"))) assert parse_formula("∀x.x & y") == ForAll("x", And(Var("x"), Var("y")))
def test_binary_operators_to_str(): assert str(And(Or(Var("a"), Var("b")), Var("c"))) == "[a | b] & c" assert str(Or(And(Var("a"), Var("b")), Var("c"))) == "a & b | c" assert str(Or(Var("a"), Or(Var("b"), Var("c")))) == "a | b | c" assert str(And(Var("a"), And(Var("b"), Var("c")))) == "a & b & c"
def test_replace_variable_in_and_or(): tree = And(Or(Var("x"), Var("y")), Var("z")) assert tree.replace_variable("x", Var("x'")) == And( Or(Var("x'"), Var("y")), Var("z") )
def test_nested_exists_and_for_all_to_str(): assert str(And(ForAll("x", Var("x")), Exists("x", Var("x")))) == "[∀ x.x] & [∃ x.x]"
def test_parsing_conjunction(): assert parse_formula("a & a'") == And(Var("a"), Var("a'")) assert parse_formula("a & b & c") == And(Var("a"), And(Var("b"), Var("c")))