def test_misc(self): a, b = self.a, self.b i_, i_a, i_b, i_ab = self.i_, self.i_a, self.i_b, self.i_ab material_implication = PLEquivalence( [PLOr([PLNot(a), b]), PLNot(PLAnd([a, PLNot(b)])), PLImplies([a, b])] ) # the equivalence is valid (i.e. satisfied for every interpretation) assert material_implication.truth(i_) assert material_implication.truth(i_a) assert material_implication.truth(i_b) assert material_implication.truth(i_ab) a_and_false_and_true = PLAnd([a, PLFalse(), PLTrue()]) assert not a_and_false_and_true.truth(i_) assert not a_and_false_and_true.truth(i_a) assert not a_and_false_and_true.truth(i_b) assert not a_and_false_and_true.truth(i_ab) a_or_false_or_true = PLOr([a, PLFalse(), PLTrue()]) assert a_or_false_or_true.truth(i_) assert a_or_false_or_true.truth(i_a) assert a_or_false_or_true.truth(i_b) assert a_or_false_or_true.truth(i_ab)
def test_nnf(): parser = PLParser() sa, sb = "A", "B" a, b = PLAtomic(sa), PLAtomic(sb) i_ = {} i_a = {sa: True} i_b = {sb: True} i_ab = {sa: True, sb: True} not_a_and_b = parser("!(A&B)") nnf_not_a_and_b = parser("!A | !B") assert not_a_and_b.to_nnf() == nnf_not_a_and_b assert nnf_not_a_and_b == nnf_not_a_and_b.to_nnf() dup = parser("!(A | A)") nnf_dup = dup.to_nnf() assert nnf_dup == PLAnd([PLNot(a), PLNot(a)]) material_implication = parser("!A | B <-> !(A & !B) <-> A->B") nnf_material_implication = parser( "((!A | B) & (!A | B) & (!A | B)) | ((A & !B) & (A & !B) & (A & !B))" ) nnf_m = material_implication.to_nnf() assert nnf_m == nnf_material_implication.to_nnf() assert ( nnf_m.truth(i_) == material_implication.truth(i_) == nnf_material_implication.truth(i_) == True ) assert ( nnf_m.truth(i_a) == material_implication.truth(i_a) == nnf_material_implication.truth(i_a) == True ) assert ( nnf_m.truth(i_b) == material_implication.truth(i_b) == nnf_material_implication.truth(i_b) == True ) assert ( nnf_m.truth(i_ab) == material_implication.truth(i_ab) == nnf_material_implication.truth(i_ab) == True )
def test_and(self): a, b = self.a, self.b i_, i_a, i_b, i_ab = self.i_, self.i_a, self.i_b, self.i_ab a_and_b = PLAnd([a, b]) assert not a_and_b.truth(i_) assert not a_and_b.truth(i_a) assert not a_and_b.truth(i_b) assert a_and_b.truth(i_ab) not_a_and_not_b = PLAnd([PLNot(a), PLNot(b)]) assert not_a_and_not_b.truth(i_) assert not not_a_and_not_b.truth(i_a) assert not not_a_and_not_b.truth(i_b) assert not not_a_and_not_b.truth(i_ab)
def p_propositional(self, p): """propositional : propositional EQUIVALENCE propositional | propositional IMPLIES propositional | propositional OR propositional | propositional AND propositional | NOT propositional | FALSE | TRUE | ATOM""" if len(p)==4: if p[2] == Symbols.EQUIVALENCE.value: p[0] = PLEquivalence([p[1], p[3]]) elif p[2] == Symbols.IMPLIES.value: p[0] = PLImplies([p[1], p[3]]) elif p[2] == Symbols.OR.value: p[0] = PLOr([p[1], p[3]]) elif p[2] == Symbols.AND.value: p[0] = PLAnd([p[1], p[3]]) else: raise ValueError # else: # p[0] = p[2] elif len(p)==3: p[0] = PLNot(p[2]) elif len(p)==2: if p[1]==Symbols.TRUE.value: p[0] = PLTrue() elif p[1]==Symbols.FALSE.value: p[0] = PLFalse() else: p[0] = PLAtomic(p[1]) else: raise ValueError
def test_nnf(): parser = PLParser() sa, sb = "A", "B" a, b = PLAtomic(sa), PLAtomic(sb) i_ = PLInterpretation(set()) i_a = PLInterpretation({sa}) i_b = PLInterpretation({sb}) i_ab = PLInterpretation({sa, sb}) not_a_and_b = parser("!(A&B)") nnf_not_a_and_b = parser("!A | !B") assert not_a_and_b.to_nnf() == nnf_not_a_and_b assert nnf_not_a_and_b == nnf_not_a_and_b.to_nnf() dup = parser("!(A | A)") nnf_dup = dup.to_nnf() assert nnf_dup == PLNot(a) material_implication = parser("!A | B <-> !(A & !B) <-> A->B") nnf_material_implication = parser( "((!A | B) & (!A | B) & (!A | B)) | ((A & !B) & (A & !B) & (A & !B))") nnf_m = material_implication.to_nnf() assert nnf_m == nnf_material_implication.to_nnf() assert nnf_m.truth(i_) == material_implication.truth( i_) == nnf_material_implication.truth(i_) == True assert nnf_m.truth(i_a) == material_implication.truth( i_a) == nnf_material_implication.truth(i_a) == True assert nnf_m.truth(i_b) == material_implication.truth( i_b) == nnf_material_implication.truth(i_b) == True assert nnf_m.truth(i_ab) == material_implication.truth( i_ab) == nnf_material_implication.truth(i_ab) == True
def prop_not(self, args): if len(args) == 1: return args[0] else: f = args[-1] for _ in args[:-1]: f = PLNot(f) return f
def test_parser(): parser = PLParser() sa, sb = "A", "B" a, b = PLAtomic(sa), PLAtomic(sb) a_and_b = parser("A & B") true_a_and_b = PLAnd([a, b]) assert a_and_b == true_a_and_b material_implication = parser("!A | B <-> !(A & !B) <-> A->B") true_material_implication = PLEquivalence( [PLOr([PLNot(a), b]), PLNot(PLAnd([a, PLNot(b)])), PLImplies([a, b])] ) assert material_implication == true_material_implication true_a_and_false_and_true = PLAnd([a, PLFalse(), PLTrue()]) a_and_false_and_true = parser("A & false & true") assert a_and_false_and_true == true_a_and_false_and_true
def _transform_delta(f: Formula, formula2AtomicFormula): """From a Propositional Formula to a Propositional Formula with non-propositional subformulas replaced with a "freezed" atomic formula.""" t = type(f) if t == PLNot: return PLNot(_transform_delta(f, formula2AtomicFormula)) # elif isinstance(f, PLBinaryOperator): #PLAnd, PLOr, PLImplies, PLEquivalence elif t == PLAnd or t == PLOr or t == PLImplies or t == PLEquivalence: return t([ _transform_delta(subf, formula2AtomicFormula) for subf in f.formulas ]) elif t == PLTrue or t == PLFalse: return f else: return formula2AtomicFormula[f]
def test_parser(): parser = LDLfParser() a, b = PLAtomic("A"), PLAtomic("B") tt = LDLfLogicalTrue() ff = LDLfLogicalFalse() true = PLTrue() false = PLFalse() r_true = RegExpPropositional(true) r_false = RegExpPropositional(false) assert tt == parser("tt") assert ff == parser("ff") assert LDLfDiamond(r_true, tt) == parser("<true>tt") assert LDLfDiamond(r_false, tt) == parser("<false>tt") assert parser("!tt & <!A&B>tt") == LDLfAnd([ LDLfNot(tt), LDLfDiamond(RegExpPropositional(PLAnd([PLNot(a), b])), tt) ]) assert parser("[true*]([true]ff | <!A>tt | <(true)*><B>tt)") == LDLfBox( RegExpStar(r_true), LDLfOr([ LDLfBox(r_true, ff), LDLfDiamond(RegExpPropositional(PLNot(a)), tt), LDLfDiamond(RegExpStar(r_true), (LDLfDiamond(RegExpPropositional(b), tt))), ]), ) assert parser("[A&B&A]ff <-> <A&B&A>tt") == LDLfEquivalence([ LDLfBox(RegExpPropositional(PLAnd([a, b, a])), ff), LDLfDiamond(RegExpPropositional(PLAnd([a, b, a])), tt), ]) assert parser("<A+B>tt") == LDLfDiamond( RegExpUnion([RegExpPropositional(a), RegExpPropositional(b)]), tt) assert parser("<A;B>tt") == LDLfDiamond( RegExpSequence([RegExpPropositional(a), RegExpPropositional(b)]), tt) assert parser("<A+(B;A)>end") == LDLfDiamond( RegExpUnion([ RegExpPropositional(a), RegExpSequence([RegExpPropositional(b), RegExpPropositional(a)]), ]), LDLfEnd(), ) assert parser("!(<(!(A<->D))+((B;C)*)+(?!last)>[(true)*]end)") == LDLfNot( LDLfDiamond( RegExpUnion([ RegExpPropositional(PLNot(PLEquivalence([a, PLAtomic("D")]))), RegExpStar( RegExpSequence([ RegExpPropositional(PLAtomic("B")), RegExpPropositional(PLAtomic("C")), ])), RegExpTest(LDLfNot(LDLfLast())), ]), LDLfBox(RegExpStar(RegExpPropositional(PLTrue())), LDLfEnd()), ))
def p_formula_not(self, p): 'formula : NOT formula' p[0] = PLNot(p[2])