def parseToTree(symbol, node): if symbol not in operatorList or symbol == "~": if symbol == ")": while 1: node = node.parent if node.symbol != "~": break if node.symbol == None: if node.parent != None and node.parent.symbol != "~": node.parent.right = node.left else: node.parent.left = node.left return node else: return node elif node.symbol == None or node.symbol == "~": if symbol == "(": node.left = Formula(None, None, node) elif symbol == "~": node.left = Negation(None, symbol, node) else: node.left = Atom(None, symbol, node) return node.left else: if symbol == "(": node.right = Formula(None, None, node) elif symbol == "~": node.right = Negation(None, symbol, node) else: node.right = Atom(None, symbol, node) return node.right elif symbol in operatorList and symbol != "~": while 1: node = node.parent if node.symbol != "~": break if symbol == "^": node = Conjunction(node) elif symbol == "v": node = Disjunction(node) elif symbol == "=>": node = Implication(node) else: node = Biconditional(node) node.left.parent = node if node.parent: if not node.parent.symbol or node.parent.symbol == "~": node.parent.left = node else: node.parent.right = node return node return node
def implication(self, expr1, expr2): # Creating an implication return Implication(expr1, expr2)
a, ])) t.test2(Conjunction([ a, Not(a), ])) t.test2(Conjunction([ Not(a), Not(a), ])) t.test2(Disjunction([a, b])) t.test2(Implication(a, b)) t.test2(Equivalence(a, b)) t.test2( Disjunction([Negation(Implication(a, b)), Negation(Implication(b, a))])) t.test2(Conjunction([Implication(a, b), Implication(Negation(a), c)])) t.test2( Equivalence(Conjunction([a, Negation(b)]), Disjunction([a, Implication(b, a)]))) # veeela premennych (no mozno nie az tak vela) nvars = 17
a, Not(a), ])) t.test2( Conjunction([ Not(a), Not(a), ])) t.test2( Disjunction( [ a, b ] ) ) t.test2( Implication( a, b ) ) t.test2( Equivalence( a, b ) ) t.test2( Disjunction([ Negation(Implication(a,b)), Negation(Implication(b,a)) ])) t.test2( Conjunction([ Implication(a,b),
def toCNF(node, step): if not node: return None if step == 0: if node.symbol == "<=>": parent = node.parent if parent != None: if parent.left == node: pos = "left" else: pos = "right" left = copy.copy(node.left) right = copy.copy(node.right) node = Conjunction(node) node.parent = parent if parent != None: if pos == "left": node.parent.left = node else: node.parent.right = node node.left = Implication(None, None, node) node.right = Implication(None, None, node) node = node.left node.left = left node.left.parent = node node.right = right node.right.parent = node node = node.parent node = node.right node.left = right node.left.parent = node node.right = left node.right.parent = node node = node.parent elif step == 1: if node.symbol == "=>": parent = node.parent if parent == None: left = copy.copy(node.left) node = Disjunction(node) else: if parent.left == node: pos = "left" else: pos = "right" left = copy.copy(node.left) node = Disjunction(node) if pos == "left": node.parent.left = node else: node.parent.right = node node.left = Negation(None, None, node) node.right.parent = node node = node.left node.left = left node.left.parent = node node = node.parent elif step == 2: if node.symbol == "~": if type(node.left) == Negation: if node.parent == None: node = node.left.left node.parent = None else: if node.parent.left == node: node.parent.left = node.left.left else: node.parent.right = node.left.left parent = node.parent node = node.left.left node.parent = parent node = node.parent elif type(node.left) == Atom: pass else: parent = node.parent if parent != None: if parent.left == node: pos = "left" else: pos = "right" node = node.left left = copy.copy(node.left) right = copy.copy(node.right) if type(node) == Conjunction: node = Disjunction(node) else: node = Conjunction(node) node.parent = parent if parent != None: if pos == "left": node.parent.left = node else: node.parent.right = node node.left = Negation(None, None, node) node.right = Negation(None, None, node) left.parent = node.left right.parent = node.right node.left.left = left node.right.left = right elif step == 3: if node.symbol == "v" and type(node.left) == Conjunction: parent = node.parent if parent != None: if parent.left == node: pos = "left" else: pos = "right" A = copy.copy(node.left.left) B = copy.copy(node.left.right) C1 = copy.copy(node.right) C2 = copy.copy(node.right) node = Conjunction(None, None, parent) node.left = Disjunction(None, None, node, A, C1) A.parent = node.left C1.parent = node.left node.right = Disjunction(None, None, node, B, C2) B.parent = node.right C2.parent = node.right if parent != None: if pos == "left": node.parent.left = node else: node.parent.right = node elif node.symbol == "v" and type(node.right) == Conjunction: parent = node.parent if parent != None: if parent.left == node: pos = "left" else: pos = "right" A1 = copy.copy(node.left) A2 = copy.copy(node.left) B = copy.copy(node.right.left) C = copy.copy(node.right.right) node = Conjunction(None, None, parent) node.left = Disjunction(None, None, node, A1, B) A1.parent = node.left B.parent = node.left node.right = Disjunction(None, None, node, A2, C) A2.parent = node.right C.parent = node.right if parent != None: if pos == "left": node.parent.left = node else: node.parent.right = node toCNF(node.left, step) toCNF(node.right, step) node = findRoot(node) return node
t.testSignedForm(Or([a, b]), BETA, [T(a), T(b)]) t.testSignedForm(And([a, b, c, d]), ALPHA, [T(a), T(b), T(c), T(d)]) t.testSignedForm(Or([a, b, c, d]), BETA, [T(a), T(b), T(c), T(d)]) t.testSignedForm(Or([a, Not(b), And(c, d)]), BETA, [T(a), T(Not(b)), T(And([c, d]))]) t.testSignedForm(Impl(a, b), BETA, [F(a), T(b)]) t.testSignedForm(Equivalence(a, b), ALPHA, [T(Impl(a, b)), T(Impl(b, a))]) correctRules = t.tested == t.passed t.testTableau(True, [F(Implication(a, a))]) t.testTableau(True, [F(Implication(Var('a'), Var('a')))]) t.testTableau(True, [F(Or(a, Not(a)))]) t.testTableau(True, [T(a), F(a)]) t.testTableau(True, [T(a), F(a), T(a)]) t.testTableau(True, [T(a), F(a), T(b)]) t.testTableau(False, [T(Or(a, b)), F(a)]) t.testTableau(True, [T(And(a, b)), F(a)])
t.test(Conjunction([Variable('a'), Variable('b')]), '(a&b)', [ (interps2[0], False), (interps2[1], False), (interps2[2], False), (interps2[3], True), ]) t.test(Disjunction([Variable('a'), Variable('b')]), '(a|b)', [ (interps2[0], False), (interps2[1], True), (interps2[2], True), (interps2[3], True), ]) t.test(Implication(Variable('a'), Variable('b')), '(a=>b)', [ (interps2[0], True), (interps2[1], True), (interps2[2], False), (interps2[3], True), ]) t.test(Equivalence(Variable('a'), Variable('b')), '(a<=>b)', [ (interps2[0], True), (interps2[1], False), (interps2[2], False), (interps2[3], True), ]) t.test( Disjunction([
'a': False }, True), ]), (Conjunction([Variable('a'), Variable('b')]), '(a&b)', 1, ['a', 'b'], [ (valuations2[0], False), (valuations2[1], False), (valuations2[2], False), (valuations2[3], True), ]), (Disjunction([Variable('a'), Variable('b')]), '(a|b)', 1, ['a', 'b'], [ (valuations2[0], False), (valuations2[1], True), (valuations2[2], True), (valuations2[3], True), ]), (Implication(Variable('a'), Variable('b')), '(a->b)', 1, ['a', 'b'], [ (valuations2[0], True), (valuations2[1], True), (valuations2[2], False), (valuations2[3], True), ]), (Equivalence(Variable('a'), Variable('b')), '(a<->b)', 1, ['a', 'b'], [ (valuations2[0], True), (valuations2[1], False), (valuations2[2], False), (valuations2[3], True), ]), (Disjunction([ Negation(Implication(Variable('a'), Variable('b'))), Negation(Implication(Variable('b'), Variable('a'))) ]), '(-(a->b)|-(b->a))', 5, ['a', 'b'], [
t.test(Conjunction([Variable('a'), Variable('b')]), '(a&b)', [ (valuations2[0], False), (valuations2[1], False), (valuations2[2], False), (valuations2[3], True), ]) t.test(Disjunction([Variable('a'), Variable('b')]), '(a|b)', [ (valuations2[0], False), (valuations2[1], True), (valuations2[2], True), (valuations2[3], True), ]) t.test(Implication(Variable('a'), Variable('b')), '(a->b)', [ (valuations2[0], True), (valuations2[1], True), (valuations2[2], False), (valuations2[3], True), ]) t.test(Equivalence(Variable('a'), Variable('b')), '(a<->b)', [ (valuations2[0], True), (valuations2[1], False), (valuations2[2], False), (valuations2[3], True), ]) t.test( Disjunction([
(interps2[2], False), (interps2[3], True), ]) t.test( Disjunction( [ Variable('a'), Variable('b') ] ), '(a|b)', [ (interps2[0], False), (interps2[1], True), (interps2[2], True), (interps2[3], True), ]) t.test( Implication( Variable('a'), Variable('b') ), '(a=>b)', [ (interps2[0], True), (interps2[1], True), (interps2[2], False), (interps2[3], True), ]) t.test( Equivalence( Variable('a'), Variable('b') ), '(a<=>b)', [ (interps2[0], True), (interps2[1], False), (interps2[2], False),
(interps2[2], True), (interps2[3], False), ]) print("Testing Negation.originalFormula") a = Variable('a') na = Negation(a) nna = Negation(na) t.compare(nna.originalFormula(), na, "Negation.originalFormula") print("Testing Implication rightSide / leftSide") a = Variable('a') b = Variable('b') na = Negation(a) nab = Implication(na, b) t.compare(nab.leftSide(), na, "Implication.leftSide") t.compare(nab.rightSide(), b, "Implication.rightSide") print("Testing Equivalence rightSide / leftSide") a = Variable('a') b = Variable('b') na = Negation(a) nab = Equivalence(na, b) t.compare(nab.leftSide(), na, "Equivalence.leftSide") t.compare(nab.rightSide(), b, "Equivalence.rightSide") t.status() # vim: set sw=4 ts=4 sts=4 sw :
t.testSignedForm(And([a, b, c, d]), ALPHA, [(True, a), (True, b), (True, c), (True, d)]) t.testSignedForm(Or([a, b, c, d]), BETA, [(True, a), (True, b), (True, c), (True, d)]) t.testSignedForm(Or([a, Not(b), And(c, d)]), BETA, [(True, a), (True, Not(b)), (True, And([c, d]))]) t.testSignedForm(Impl(a, b), BETA, [(False, a), (True, b)]) t.testSignedForm(Equivalence(a, b), ALPHA, [(True, Impl(a, b)), (True, Impl(b, a))]) t.testTableau(True, [(False, Implication(a, a))]) t.testTableau(True, [(False, Or(a, Not(a)))]) t.testTableau(True, [(True, a), (False, a)]) t.testTableau(False, [(True, Or(a, b)), (False, a)]) t.testTableau(True, [(True, And(a, b)), (False, a)]) demorgan1 = Equivalence(Not(And([a, b])), Or([Not(a), Not(b)])) t.testTableau(True, [(False, demorgan1)]) demorgan2 = Equivalence(Not(Or([a, b])), And([Not(a), Not(b)])) t.testTableau(True, [(False, demorgan2)])