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_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_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_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_combine_every_child(lexicon): every = lexicon["every"][0] child = lexicon["child"][0] node = combine(every, child) assert node.text == "every child" assert node.formula == Call(every.formula, child.formula) assert node.type == ComplexType(TYPE_ET, TYPE_TRUTH_VALUE)
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_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_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_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_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_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_the_man_is_good_is_true(): formula = Call(Var("Good"), Iota("x", Call(Var("Man"), Var("x")))) assert interpret_formula(formula, test_model)
def test_the_man_is_john(): formula = Iota("x", Call(Var("Man"), Var("x"))) assert interpret_formula(formula, test_model) == John
def test_someone_is_alien_is_false(): formula = Exists("x", Call(Var("Alien"), Var("x"))) assert not interpret_formula(formula, test_model)
def test_someone_is_bad_is_true(): formula = Exists("x", Call(Var("Bad"), Var("x"))) assert interpret_formula(formula, test_model)
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_satisfiers_alien_set(): sset = satisfiers(Call(Var("Alien"), Var("x")), test_model, "x") assert sset == set()
def test_satisfiers_bad_set(): sset = satisfiers(Call(Var("Bad"), Var("x")), test_model, "x") assert sset == {Mary}
def test_the_human_is_undefined(): formula = Iota("x", Call(Var("Human"), Var("x"))) assert interpret_formula(formula, test_model) is None
def test_satisfiers_good_set(): sset = satisfiers(Call(Var("Good"), Var("x")), test_model, "x") assert sset == {John}
def test_john_is_good_is_true(): formula = Call(Var("Good"), Var("j")) assert interpret_formula(formula, test_model) assert not interpret_formula(Not(formula), test_model)
def test_satisfiers_human_set(): sset = satisfiers(Call(Var("Human"), Var("x")), test_model, "x") assert sset == {John, Mary}
def test_john_is_bad_is_false(): formula = Call(Var("Bad"), Var("j")) assert not interpret_formula(formula, test_model) assert interpret_formula(Not(formula), test_model)
def test_replace_predicate(): tree = Call(Var("P"), Var("x")) assert tree.replace_variable("P", Var("Good")) == Call(Var("Good"), Var("x"))
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_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_everyone_is_bad_is_false(): formula = ForAll("x", Call(Var("Bad"), Var("x"))) assert not interpret_formula(formula, test_model)
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_everyone_is_human_is_true(): formula = ForAll("x", Call(Var("Human"), Var("x"))) assert interpret_formula(formula, test_model)