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_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_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_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_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_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_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_simplify_call_with_lambda_arg(): # (LP.P(x))(Lx.x | a) -> x | a tree = Call(Lambda("P", Call(Var("P"), Var("x"))), Lambda("x", Or(Var("x"), Var("a")))) assert tree.simplify() == Or(Var("x"), Var("a"))
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_simplify_call(): tree = Call(Lambda("x", Var("x")), Var("j")) assert tree.simplify() == Var("j")
from montague.ast import ( Call, ComplexType, Lambda, SentenceNode, TYPE_ENTITY, TYPE_TRUTH_VALUE, Var, ) from montague.main import ShellState, execute_command, HELP_MESSAGE TEST_LEXICON = { "good": SentenceNode( "good", Lambda("x", Call(Var("Good"), Var("x"))), ComplexType(TYPE_ENTITY, TYPE_TRUTH_VALUE), ), "bad": SentenceNode( "bad", Lambda("x", Call(Var("Bad"), Var("x"))), ComplexType(TYPE_ENTITY, TYPE_TRUTH_VALUE), ), } @pytest.fixture def shell_state(): return ShellState(lexicon=TEST_LEXICON)