def test_simplify_every_child(lexicon): # (LP.LQ.Ax.P(x) -> Q(x))(Lx.Child(x)) -> LQ.Ax.Child(x) -> Q(x) tree = Call(lexicon["every"][0].formula, lexicon["child"][0].formula) assert tree.simplify() == Lambda( "Q", ForAll("x", IfThen(Call(Var("Child"), Var("x")), Call(Var("Q"), Var("x")))))
def test_shell_display_formula(shell_state): with patch("montague.main.translate_sentence") as mock_translate_sentence: mock_translate_sentence.return_value = [ SentenceNode("good", Call(Var("Good"), Var("j")), TYPE_TRUTH_VALUE) ] response = execute_command("John is good", shell_state) assert "Denotation: Good(j)" in response assert "Type: t" in response
def test_translate_the_child(lexicon): nodes = translate_sentence("the child", lexicon) assert len(nodes) == 1 node = nodes[0] assert node.text == "the child" assert node.formula == Iota("x", Call(Var("Child"), Var("x"))) assert node.type == TYPE_ENTITY
def test_translate_john_is_bad(lexicon): nodes = translate_sentence("John is bad", lexicon) assert len(nodes) == 1 node = nodes[0] assert node.text == "John is bad" assert node.formula == Call(Var("Bad"), Var("john")) assert node.type == TYPE_TRUTH_VALUE
def test_translate_every_child_is_good(lexicon): nodes = translate_sentence("every child is good", lexicon) assert len(nodes) == 1 node = nodes[0] assert node.text == "every child is good" assert node.formula == ForAll( "x", IfThen(Call(Var("Child"), Var("x")), Call(Var("Good"), Var("x")))) assert node.type == TYPE_TRUTH_VALUE
def test_translate_is_good(lexicon): # import pdb; pdb.set_trace() nodes = translate_sentence("is good", lexicon) assert len(nodes) == 1 node = nodes[0] assert node.text == "is good" assert node.formula == Lambda("x", Call(Var("Good"), Var("x"))) assert node.type == TYPE_ET
def test_translate_unknown_word_in_sentence(lexicon): nodes = translate_sentence("John is whorlious", lexicon) assert len(nodes) == 2 # TODO [2019-05-20]: For now, this is just wrong (but wrong in the expected way). assert nodes[0].text == "John is whorlious" assert nodes[0].formula == Call(Var("Whorlious"), Var("john")) assert nodes[0].type == TYPE_TRUTH_VALUE assert nodes[1].text == "John is whorlious" assert nodes[1].formula == Call(Var("John"), Var("whorlious")) assert nodes[1].type == TYPE_TRUTH_VALUE
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_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_translate_unknown_word(lexicon): nodes = translate_sentence("Mikhail", lexicon) assert len(nodes) == 3 assert nodes[0].text == "Mikhail" assert nodes[0].formula == Var("mikhail") assert nodes[0].type == TYPE_ENTITY assert nodes[1].text == "Mikhail" assert nodes[1].formula == Lambda("x", Call(Var("Mikhail"), Var("x"))) assert nodes[1].type == TYPE_ET assert nodes[2].text == "Mikhail" assert nodes[2].formula == Lambda( "x", Lambda("y", Call(Call(Var("Mikhail"), Var("x")), Var("y")))) assert nodes[2].type == ComplexType(TYPE_ENTITY, TYPE_ET)
def test_exists_to_str(): tree = Exists("x", Call(Var("P"), Var("x"))) assert str(tree) == "∃ x.P(x)" assert tree.ascii_str() == "Ex.P(x)"
def test_for_all_to_str(): tree = ForAll("x", Call(Var("P"), Var("x"))) assert str(tree) == "∀ x.P(x)" assert tree.ascii_str() == "Ax.P(x)"
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_if_and_only_if_to_str(): assert str(IfAndOnlyIf(Var("a"), Var("b"))) == "a <-> b"
def test_if_then_to_str(): assert str(IfThen(Var("a"), Var("b"))) == "a -> b"
def test_simple_replace_variable(): assert Var("x").replace_variable("x", Var("y")) == Var("y")
def test_and_to_str(): assert str(And(Var("a"), Var("b"))) == "a & b"
def test_variable_to_str(): assert str(Var("a")) == "a"
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_nested_exists_and_for_all_to_str(): assert str(And(ForAll("x", Var("x")), Exists("x", Var("x")))) == "[∀ x.x] & [∃ x.x]"
def test_replace_predicate(): tree = Call(Var("P"), Var("x")) assert tree.replace_variable("P", Var("Good")) == Call(Var("Good"), Var("x"))
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_not_to_str(): assert str(Not(Var("x"))) == "~x" assert str(Not(Or(Var("x"), Var("y")))) == "~[x | y]"
def test_or_to_str(): assert str(Or(Var("a"), Var("b"))) == "a | b"
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_satisfiers_does_not_create_assignment(): satisfiers(Var("j"), test_model, "some_nonexistent_variable") assert "some_nonexistent_variable" not in test_model.assignments
def test_iota_to_str(): tree = Iota("x", Var("x")) assert str(tree) == "ιx.x" assert tree.ascii_str() == "ix.x"
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")) )