def test_complex_quantified_sequent(self): sequent = Sequent( [Existential( "alpha", Conjunction( Universal( "beta", Atom("Predicate", ("alpha", "beta")) ), Atom("AnotherPredicate", ("alpha",)) ))], []) with patch("json.load", lambda *args: self.names): decomp = sequent.decompose() self.assertEqual( Sequent( [ Conjunction( Universal("beta", Atom("Predicate", ("Adrian", "beta"))), Atom("AnotherPredicate", ("Adrian",)) ) ], [] ), decomp[0][0] )
def test_quantifier_namespace_saturation(self): a = Universal("x", Atom("Saturated", ("x", "NAME", "NAME"))) b = Universal("x", Atom("Saturated", ("x", "x", "NAME"))) self.assertNotEqual(a, b) c = Existential("a", Universal("b", Atom("Saturated", ("a", "b", "b")))) d = Existential("a", Universal("b", Atom("Saturated", ("a", "a", "b")))) self.assertNotEqual(c, d)
def test_sequent_left_universal_principal(self): universal = Universal("x", Atom("Proposition", ("x",))) sequent = Sequent([universal], []) principal = sequent.principal self.assertEqual("ant", principal.side) self.assertEqual(0, principal.index) self.assertEqual(LeftUniversal("x", Atom("Proposition", ("x",))), principal.proposition)
def test_right_existential(self): """|~ exists(x)(Predicate(x)) """ with patch("json.load", lambda *args: self.names): sequent = Sequent([], [Existential("alpha", self.alpha)]) decomp = sequent.decompose() self.assertEqual(Sequent([], [Atom("Predicate", ("Adrian",))]), decomp[0][0]) self.assertEqual(Sequent([], [Atom("Predicate", ("Eve",))]), decomp[1][0])
def test_left_universal(self): """forall(x)(Predicate(x)) |~""" with patch("json.load", lambda *args: self.names): sequent = Sequent([Universal("alpha", self.alpha)], []) decomp = sequent.decompose() self.assertEqual(Sequent([Atom("Predicate", ("Adrian",))], []), decomp[0][0]) self.assertEqual(Sequent([Atom("Predicate", ("Eve",))], []), decomp[1][0])
def test_nested_quantifier_with_unquantified(self): string = "(exists(x)(Tasty(x) and exists(y)(Eats(y; x))))" actual = String(string).to_proposition() expected = Existential("x", Conjunction( Atom("Tasty", ("x",)), Existential("y", Atom("Eats", ("y", "x"))))) self.assertEqual(expected, actual)
def test_complex_existential_from_string(self): e = "(exists(x)(Cute(x) and Cat(x)))" convert = String(e).to_proposition() self.assertEqual( Existential("x", Conjunction( Atom("Cute", ("x",)), Atom("Cat", ("x",)))), convert)
def test_nested_quantified_sequents(self): """forall(x)(exists(y)(Predicate(x; y)) |~""" sequent = Sequent([ Universal( "alpha", Existential("beta", Atom("Predicate", ("alpha", "beta")))) ], []) with patch("json.load", lambda *args: self.names): decomp = sequent.decompose() self.assertEqual( Sequent( [Existential("beta", Atom("Predicate", ("Adrian", "beta")))], []), decomp[0][0]) self.assertEqual( Sequent([Existential("beta", Atom("Predicate", ("Eve", "beta")))], []), decomp[1][0])
def test_string_to_sequent(self): string = "Predicate(alpha), (Relation(alpha; beta) and Relation(beta; delta)) " \ "|~ (Relation(beta; gamma) implies Predicate(delta)), Predicate(delta) or Predicate(zeta)" sequent = String(string).to_sequent() self.assertEqual( Sequent( [ Atom("Predicate", ("alpha",)), Conjunction( Atom("Relation", ("alpha", "beta")), Atom("Relation", ("beta", "delta")) ) ], [ Conditional( Atom("Relation", ("beta", "gamma")), Atom("Predicate", ("delta",)) ), Disjunction( Atom("Predicate", ("delta",)), Atom("Predicate", ("zeta",)) ) ] ), sequent )
class TestBinary(unittest.TestCase): types = Conditional, Conjunction, Disjunction left = Atom("Predicate", ("alpha", "beta")) right = Atom("Predicate", ("beta", "gamma")) def test_binary_init(self): for t in self.types: prop = t(self.left, self.right) self.assertEqual(self.left, prop.left) self.assertEqual(self.right, prop.right) self.assertEqual(("alpha", "beta", "gamma"), prop.names) def test_binary_from_string(self): for t in self.types: string = f"(Predicate(alpha; beta) {t.string} Predicate(beta; gamma))" prop = String(string).to_proposition() self.assertEqual(self.left, prop.left) self.assertEqual(self.right, prop.right) self.assertEqual(string, str(prop)) self.assertEqual(("alpha", "beta", "gamma"), prop.names)
class TestAtoms(unittest.TestCase): prop = Atom("Predicate", ("alpha", "beta", "gamma")) def test_atom_init(self): atom = self.prop self.assertEqual(atom.names, ("alpha", "beta", "gamma")) self.assertEqual("Predicate(alpha; beta; gamma)", str(atom)) def test_atom_from_string(self): string = "Predicate(alpha; beta; gamma)" prop = String(string).to_proposition() self.assertEqual(self.prop, prop)
class TestUnary(unittest.TestCase): types = Negation, atom = Atom("Predicate", ("alpha", "beta")) def test_unary_init(self): for t in self.types: prop = t(self.atom) self.assertEqual(self.atom, prop.prop) self.assertEqual(f"({prop.string} Predicate(alpha; beta))", str(prop)) self.assertEqual(("alpha", "beta"), prop.names) def test_unary_from_string(self): for t in self.types: string = f"({t.string} Predicate(alpha; beta))" prop = String(string).to_proposition() self.assertEqual(self.atom, prop.prop) self.assertEqual(string, str(prop)) self.assertEqual(("alpha", "beta"), prop.names)
def test_nested_two_name_complex_quantifier(self): nest = Universal("w", Existential("x", Disjunction( Atom("Nested", ("x", "w")), Atom("Nested", ("w", "x")))) ) self.assertEqual( Existential("x", Disjunction( Atom("Nested", ("x", "w")), Atom("Nested", ("w", "x")))), nest.prop ) self.assertEqual( Disjunction( Atom("Nested", ("x", "w")), Atom("Nested", ("w", "x"))), nest.prop.prop)
def test_nested_complex_quantifier_init(self): nest = Universal("x", Existential("y", Conjunction( Atom("Nested", ("x",)), Atom("Nested", ("y",)))) ) self.assertEqual( Existential("y", Conjunction( Atom("Nested", ("x",)), Atom("Nested", ("y",)))), nest.prop ) self.assertEqual("x", nest.var) self.assertEqual([], nest.names) self.assertEqual( Conjunction( Atom("Nested", ("x",)), Atom("Nested", ("y",))), nest.prop.prop ) self.assertEqual("y", nest.prop.var)
def test_nested_instantiated(self): uni = Universal("x", Existential("y", Atom("Nested", ("x", "y")))) inst = uni.instantiate("x", "alpha") self.assertEqual(Existential("y", Atom("Nested", ("alpha", "y"))), inst)
def test_nested_two_name_atomic_quantifier(self): nest = Universal("w", Existential("x", Atom("Nested", ("x", "w")))) self.assertEqual(Existential("x", Atom("Nested", ("x", "w"))), nest.prop) self.assertEqual(Atom("Nested", ("x", "w")), nest.prop.prop)
def test_nested_atomic_quantifier_init(self): nest = Universal("x", Existential("y", Atom("Nested", ("x", "y")))) self.assertEqual(Existential("y", Atom("Nested", ("x", "y"))), nest.prop) self.assertEqual("x", nest.var) self.assertEqual([], nest.names) self.assertEqual(Atom("Nested", ("x", "y")), nest.prop.prop)
def test_nested_quantifier_from_string(self): string = "(forall(x)(exists(y)(Nested(x; y))))" actual = String(string).to_proposition() expected = Universal("x", Existential("y", Atom("Nested", ("x", "y")))) self.assertEqual(expected, actual)
def test_universal_predicate_from_string(self): u = "(forall(y)(EatsDoughnuts(y)))" convert = String(u).to_proposition() self.assertEqual(Universal("y", Atom("EatsDoughnuts", ("y",))), convert)
def test_existential_predicate_from_string(self): e = "(exists(x)(LikesFudge(x)))" convert = String(e).to_proposition() self.assertEqual(Existential("x", Atom("LikesFudge", ("x",))), convert)
def _atom(self): if "(" not in self.data: return Atom(self.data) *extra, prop, names = self.data[:-1].split("(") names = names.strip("()") return Atom(prop, names.split("; "))
class TestInvertibleDecomp(unittest.TestCase): rules = {k: v for k, v in Settings()["Sequent Rules"].items()} alpha = Atom("Predicate", ("alpha", )) beta = Atom("Predicate", ("beta", )) names = ["Adrian", "Eve"] def setUp(self) -> None: Rules.change_multiple("", "Invertible") def tearDown(self) -> None: for k, v in self.rules.items(): Settings()["Sequent Rules"][k] = v def test_inv_left_conditional(self): """Predicate(alpha) implies Predicate(beta) |~ """ sequent = Sequent([Conditional(self.alpha, self.beta)], []) decomp = sequent.decompose()[0] self.assertEqual(Sequent([], [self.alpha]), decomp[0]) self.assertEqual(Sequent([self.beta], []), decomp[1]) def test_inv_right_conditional(self): """|~ Predicate(alpha) implies Predicate(beta)""" sequent = Sequent([], [Conditional(self.alpha, self.beta)]) decomp = sequent.decompose()[0][0] self.assertEqual(Sequent([self.alpha], [self.beta]), decomp) def test_inv_left_conjunction(self): """Predicate(alpha) and Predicate(beta) |~""" sequent = Sequent([Conjunction(self.alpha, self.beta)], []) decomp = sequent.decompose()[0][0] self.assertEqual(Sequent([self.alpha, self.beta], []), decomp) def test_inv_right_conjunction(self): """|~ Predicate(alpha) and Predicate(beta)""" sequent = Sequent([], [Conjunction(self.alpha, self.beta)]) decomp = sequent.decompose()[0] self.assertEqual(Sequent([], [self.alpha]), decomp[0]) self.assertEqual(Sequent([], [self.beta]), decomp[1]) def test_inv_left_disjunction(self): """Predicate(alpha) or Predicate(beta) |~""" sequent = Sequent([Disjunction(self.alpha, self.beta)], []) decomp = sequent.decompose()[0] self.assertEqual(Sequent([self.alpha], []), decomp[0]) self.assertEqual(Sequent([self.beta], []), decomp[1]) def test_inv_right_disjunction(self): """|~ Predicate(alpha) or Predicate(beta)""" sequent = Sequent([], [Disjunction(self.alpha, self.beta)]) decomp = sequent.decompose()[0][0] self.assertEqual(Sequent([], [self.alpha, self.beta]), decomp) def test_inv_left_negation(self): """not Predicate(alpha) |~""" sequent = Sequent([Negation(self.alpha)], []) decomp = sequent.decompose()[0][0] self.assertEqual(Sequent([], [self.alpha]), decomp) def test_inv_right_negation(self): """|~ not Predicate(alpha)""" sequent = Sequent([], [Negation(self.alpha)]) decomp = sequent.decompose()[0][0] self.assertEqual(Sequent([self.alpha], []), decomp) def test_right_universal(self): """|~ forall(x)(Predicate(x))""" with patch("json.load", lambda *args: self.names): sequent = Sequent([], [Existential("alpha", self.alpha)]) decomp = sequent.decompose() self.assertEqual(Sequent([], [Atom("Predicate", ("Adrian", ))]), decomp[0][0]) self.assertEqual(Sequent([], [Atom("Predicate", ("Eve", ))]), decomp[1][0]) def test_left_existential(self): """exists(x)(Predicate(x)) |~""" with patch("json.load", lambda *args: self.names): sequent = Sequent([Existential("alpha", self.alpha)], []) decomp = sequent.decompose() self.assertEqual(Sequent([Atom("Predicate", ("Adrian", ))], []), decomp[0][0]) self.assertEqual(Sequent([Atom("Predicate", ("Eve", ))], []), decomp[1][0])
def test_nested_quantifier_string(self): expected = "forall(x)(exists(y)(Nested(x; y)))" actual = str(Universal("x", Existential("y", Atom("Nested", ("x", "y"))))) self.assertEqual(expected, actual)
def test_function(): return Atom("test")
def test_complex_quantifier_eq_same_object_multi_predicate(self): a = Universal("x", Conjunction(Atom("Tough", ("x", "x")), Atom("Hard", ("x", "x")))) b = Universal("y", Conjunction(Atom("Tough", ("y", "y")), Atom("Hard", ("y", "y")))) self.assertEqual(a, b)
class TestNonInvertibleDecomp(unittest.TestCase): rules = {k: v for k, v in Settings()["Sequent Rules"].items()} alpha = Atom("Predicate", ("alpha", )) beta = Atom("Predicate", ("beta", )) names = ["Adrian", "Eve"] def setUp(self) -> None: Rules.change_multiple("", "NonInvertible") def tearDown(self) -> None: for k, v in self.rules.items(): Settings()["Sequent Rules"][k] = v def test_left_universal(self): """forall(x)(Predicate(x)) |~""" with patch("json.load", lambda *args: self.names): sequent = Sequent([Universal("alpha", self.alpha)], []) decomp = sequent.decompose() self.assertEqual(Sequent([Atom("Predicate", ("Adrian", ))], []), decomp[0][0]) self.assertEqual(Sequent([Atom("Predicate", ("Eve", ))], []), decomp[1][0]) def test_right_existential(self): """|~ exists(x)(Predicate(x)) """ with patch("json.load", lambda *args: self.names): sequent = Sequent([], [Existential("alpha", self.alpha)]) decomp = sequent.decompose() self.assertEqual(Sequent([], [Atom("Predicate", ("Adrian", ))]), decomp[0][0]) self.assertEqual(Sequent([], [Atom("Predicate", ("Eve", ))]), decomp[1][0]) def test_nested_quantified_sequents(self): """forall(x)(exists(y)(Predicate(x; y)) |~""" sequent = Sequent([ Universal( "alpha", Existential("beta", Atom("Predicate", ("alpha", "beta")))) ], []) with patch("json.load", lambda *args: self.names): decomp = sequent.decompose() self.assertEqual( Sequent( [Existential("beta", Atom("Predicate", ("Adrian", "beta")))], []), decomp[0][0]) self.assertEqual( Sequent([Existential("beta", Atom("Predicate", ("Eve", "beta")))], []), decomp[1][0]) def test_complex_quantified_sequent(self): sequent = Sequent([ Existential( "alpha", Conjunction( Universal("beta", Atom("Predicate", ("alpha", "beta"))), Atom("AnotherPredicate", ("alpha", )))) ], []) with patch("json.load", lambda *args: self.names): decomp = sequent.decompose() self.assertEqual( Sequent([ Conjunction( Universal("beta", Atom("Predicate", ("Adrian", "beta"))), Atom("AnotherPredicate", ("Adrian", ))) ], []), decomp[0][0])
def test_quantifier_eq_with_same_predicate_different_names(self): a = Existential("x", Atom("Simple", ("x",))) b = Existential("y", Atom("Simple", ("y",))) self.assertEqual(a, b)
def test_complex_quantifier_eq_same_object_predicate_different_names(self): a = Universal("x", Conjunction(Atom("Tough", ("x",)), Atom("Hard", ("x",)))) b = Universal("y", Conjunction(Atom("Tough", ("y",)), Atom("Hard", ("y",)))) self.assertEqual(a, b)
class TestQuantifiers(unittest.TestCase): types = Universal, Existential test_atom = Atom("Predicate", ("alpha", "x", "beta")) result_atom = Atom("Predicate", ("alpha", "nothing", "beta")) unary = Negation, binary = Conditional, Conjunction, Disjunction def test_universal_init(self): universal = Universal("x", self.test_atom) self.assertEqual("x", universal.var) self.assertEqual(self.test_atom, universal.prop) def test_existential_init(self): existential = Existential("x", self.test_atom) self.assertEqual("x", existential.var) self.assertEqual(self.test_atom, existential.prop) def test_instantiate_atom(self): universal = Universal("x", self.test_atom) test = universal.instantiate(universal.var, "nothing") self.assertEqual(self.result_atom, test) def test_instantiate_unary(self): for c in self.unary: universal = Universal("x", c(self.test_atom)) test = universal.instantiate(universal.var, "nothing") self.assertEqual(c(self.result_atom), test) def test_instantiate_binary(self): for c in self.binary: universal = Universal("x", c(self.test_atom, self.test_atom)) test = universal.instantiate(universal.var, "nothing") self.assertEqual(c(self.result_atom, self.result_atom), test) def test_existential_predicate_from_string(self): e = "(exists(x)(LikesFudge(x)))" convert = String(e).to_proposition() self.assertEqual(Existential("x", Atom("LikesFudge", ("x",))), convert) def test_universal_predicate_from_string(self): u = "(forall(y)(EatsDoughnuts(y)))" convert = String(u).to_proposition() self.assertEqual(Universal("y", Atom("EatsDoughnuts", ("y",))), convert) def test_complex_existential_from_string(self): e = "(exists(x)(Cute(x) and Cat(x)))" convert = String(e).to_proposition() self.assertEqual( Existential("x", Conjunction( Atom("Cute", ("x",)), Atom("Cat", ("x",)))), convert) def test_quantifier_eq_with_same_predicate_different_names(self): a = Existential("x", Atom("Simple", ("x",))) b = Existential("y", Atom("Simple", ("y",))) self.assertEqual(a, b) def test_complex_quantifier_eq_same_object_predicate_different_names(self): a = Universal("x", Conjunction(Atom("Tough", ("x",)), Atom("Hard", ("x",)))) b = Universal("y", Conjunction(Atom("Tough", ("y",)), Atom("Hard", ("y",)))) self.assertEqual(a, b) def test_complex_quantifier_eq_same_object_multi_predicate(self): a = Universal("x", Conjunction(Atom("Tough", ("x", "x")), Atom("Hard", ("x", "x")))) b = Universal("y", Conjunction(Atom("Tough", ("y", "y")), Atom("Hard", ("y", "y")))) self.assertEqual(a, b) def test_nested_atomic_quantifier_init(self): nest = Universal("x", Existential("y", Atom("Nested", ("x", "y")))) self.assertEqual(Existential("y", Atom("Nested", ("x", "y"))), nest.prop) self.assertEqual("x", nest.var) self.assertEqual([], nest.names) self.assertEqual(Atom("Nested", ("x", "y")), nest.prop.prop) def test_nested_complex_quantifier_init(self): nest = Universal("x", Existential("y", Conjunction( Atom("Nested", ("x",)), Atom("Nested", ("y",)))) ) self.assertEqual( Existential("y", Conjunction( Atom("Nested", ("x",)), Atom("Nested", ("y",)))), nest.prop ) self.assertEqual("x", nest.var) self.assertEqual([], nest.names) self.assertEqual( Conjunction( Atom("Nested", ("x",)), Atom("Nested", ("y",))), nest.prop.prop ) self.assertEqual("y", nest.prop.var) def test_nested_two_name_atomic_quantifier(self): nest = Universal("w", Existential("x", Atom("Nested", ("x", "w")))) self.assertEqual(Existential("x", Atom("Nested", ("x", "w"))), nest.prop) self.assertEqual(Atom("Nested", ("x", "w")), nest.prop.prop) def test_nested_two_name_complex_quantifier(self): nest = Universal("w", Existential("x", Disjunction( Atom("Nested", ("x", "w")), Atom("Nested", ("w", "x")))) ) self.assertEqual( Existential("x", Disjunction( Atom("Nested", ("x", "w")), Atom("Nested", ("w", "x")))), nest.prop ) self.assertEqual( Disjunction( Atom("Nested", ("x", "w")), Atom("Nested", ("w", "x"))), nest.prop.prop) def test_nested_instantiated(self): uni = Universal("x", Existential("y", Atom("Nested", ("x", "y")))) inst = uni.instantiate("x", "alpha") self.assertEqual(Existential("y", Atom("Nested", ("alpha", "y"))), inst) def test_quantifier_namespace_saturation(self): a = Universal("x", Atom("Saturated", ("x", "NAME", "NAME"))) b = Universal("x", Atom("Saturated", ("x", "x", "NAME"))) self.assertNotEqual(a, b) c = Existential("a", Universal("b", Atom("Saturated", ("a", "b", "b")))) d = Existential("a", Universal("b", Atom("Saturated", ("a", "a", "b")))) self.assertNotEqual(c, d) def test_nested_quantifier_string(self): expected = "forall(x)(exists(y)(Nested(x; y)))" actual = str(Universal("x", Existential("y", Atom("Nested", ("x", "y"))))) self.assertEqual(expected, actual) def test_nested_quantifier_from_string(self): string = "(forall(x)(exists(y)(Nested(x; y))))" actual = String(string).to_proposition() expected = Universal("x", Existential("y", Atom("Nested", ("x", "y")))) self.assertEqual(expected, actual) def test_nested_quantifier_with_unquantified(self): string = "(exists(x)(Tasty(x) and exists(y)(Eats(y; x))))" actual = String(string).to_proposition() expected = Existential("x", Conjunction( Atom("Tasty", ("x",)), Existential("y", Atom("Eats", ("y", "x"))))) self.assertEqual(expected, actual)
import os import json from Objects.Sequents import Sequent # Propositions from Propositions.BaseClasses import Atom from Propositions.Propositions import Negation, Conditional, Conjunction, Disjunction atom = Atom('Proposition') negation = Negation(atom) conditional = Conditional(atom, atom) conjunction = Conjunction(atom, atom) disjunction = Disjunction(atom, atom) propositions_list = [atom, negation, conditional, conjunction, disjunction] # Proposition Strings atom_str = 'Proposition' neg_str = '(not Proposition)' cond_str = '(Proposition implies Proposition)' conj_str = '(Proposition and Proposition)' disj_str = '(Proposition or Proposition)' proposition_strings_list = [atom_str, neg_str, cond_str, conj_str, disj_str] # Reflexive Sequents # Note: do not attempt to decompose universal sequents non-invertibly. empty_sequent = Sequent([], []) reflexive_atomic_sequent = Sequent([atom], [atom]) reflexive_negation_sequent = Sequent([negation], [negation]) reflexive_conditional_sequent = Sequent([conditional], [conditional])