def T5(F, G): """ (F->G) -> (!G -> !F) """ if type(F) is Node: nf = Node(F.left, F.right, _not=not F._not) else: nf = Variable(F.symbol, _not=not F._not) if type(G) is Node: ng = Node(G.left, G.right, _not=not G._not) else: ng = Variable(G.symbol, _not=not G._not) f1 = Node(F, G) f2 = Node(ng, nf) msg = "T5 for " + str(F) + ", " + str(G) return Node(f1, f2, msg=msg)
def T3(F, G): """ !F -> (F->G) """ if type(F) is Node: nf = Node(F.left, F.right, _not=not F._not) else: nf = Variable(F.symbol, _not=not F._not) fg = Node(F, G) msg = "T3 for " + str(F) + ", " + str(G) return Node(nf, fg, msg=msg)
def T6(F, G): """ F -> (!G -> !(F->G) ) """ if type(G) is Node: ng = Node(G.left, G.right, _not=not G._not) else: ng = Variable(G.symbol, _not=not G._not) nFG = Node(F, G, _not=True) f2 = Node(ng, nFG) msg = "T6 for " + str(F) + ", " + str(G) return Node(F, f2, msg=msg)
def T7(F, G): """ (F->G) -> ( (!F->G) ->G ) """ if type(F) is Node: nf = Node(F.left, F.right, _not=not F._not) else: nf = Variable(F.symbol, _not=not F._not) fg = Node(F, G) nf_g = Node(nf, G) nfg_G = Node(nf_g, G) # (!F->G)->G msg = "T7 for " + str(F) + ", " + str(G) return Node(fg, nfg_G, msg=msg)
def variable(self, name): from formula import Variable logger.debug(f"Parsing variable term with name {str(name)}") return Variable(self.system, str(name))
for interpretation, result in cases: print(" interpretation %s:" % (repr(interpretation), )) self.compare(formula.eval(interpretation), result, "eval") def status(self): print("TESTED %d" % (self.tested, )) print("PASSED %d" % (self.passed, )) if self.tested == self.passed: print("OK") else: print("ERROR") t = Tester() t.test(Variable('a'), 'a', [ ({ 'a': True }, True), ({ 'a': False }, False), ]) t.test(Negation(Variable('a')), '-a', [ ({ 'a': True }, False), ({ 'a': False }, True),
def testTableau(self, expect_closed, sfs): self.case += 1 self.tested += 1 strSfs = [sf.toString() for sf in sfs] print("CASE %d: %s" % (self.case, '; '.join(strSfs))) tableau = Node(False, Variable("")) try: start = now() tableau = TableauBuilder().build(sfs) duration = now() - start except KeyboardInterrupt: raise KeyboardInterrupt() except: printException() if stopOnError: raise FailedTestException() return if not isinstance(tableau, Tableau): print('FAILED: not a tableau.Tableau: %s' % type(tableau)) print() return if not isinstance(tableau.root, Node): print('FAILED: not a tableau.Node: %s' % type(tableau)) print() return closed = tableau.isClosed() badStructure = False try: self.testTableauStructure(tableau.root, [], strSfs) except BadTableauException as err: badStructure = str(err) except: printException() if stopOnError: raise FailedTestException() return size = tableau.size() self.time += duration self.size += size if closed: self.closed += 1 passed = closed == expect_closed and not badStructure if passed: self.passed += 1 print('PASSED: time: %12.9f tableau size: %3d %s' % (duration, size, self.closedToString(closed))) else: print('FAILED: ') if not passed or showAll: print('=====TABLEAU=====\n%s\n%s' % (tableau.toString(), '=' * 13)) if not passed: if closed != expect_closed: print('Tableau is %s, but should be %s' % (self.closedToString(closed), self.closedToString(expect_closed))) if badStructure: print(badStructure) if stopOnError: raise FailedTestException() print('')
}, { 'a': True, 'b': False, 'c': True }, { 'a': False, 'b': True, 'c': True }, { 'a': True, 'b': True, 'c': True }] testFormulas = [ (Variable('a'), 'a', 0, ['a'], [ ({ 'a': True }, True), ({ 'a': False }, False), ]), (Negation(Variable('a')), '-a', 1, ['a'], [ ({ 'a': True }, False), ({ 'a': False }, True), ]),
def Ad_Theorem(F): ''' Adequacy theorem implementation :param F: Propositional calculus formula: Variable or Node :return: formal proof |- F ''' global proof symbols = list(F.used_symbols()) symbolsCount = len(symbols) N = 1 << symbolsCount vector = 0 # why not int for vector? n = 0 while n < len(symbols): vector = 0 while vector < N: X_n = symbols[n] values = { symbols[k]: 0 if vector & (1 << k) == 0 else 1 for k in range(len(symbols)) } values_not = values.copy() values[X_n] = 1 values[X_n] = 0 C1 = Calmar_Theorem(F, values) C2 = Calmar_Theorem(F, values_not) hypo = [ Variable(k, _not=True if v == 0 else False) for k, v in values.items() ] deductC1 = build_deduction(hypo, Variable(X_n), F, C1) hypo = [ Variable(k, _not=True if v == 0 else False) for k, v in values.items() ] deductC2 = build_deduction(hypo, Variable(X_n, _not=True), F, C2) if len(C1) == 0 or len(C2) == 0: vector += 1 continue ld1 = deductC1[-1] ld2 = deductC2[-1] t7 = build_T7(Variable(X_n), F) T7 = t7[-1] mp1 = MP(ld1, T7) mp2 = MP(ld2, mp1) proof.extend(deductC1) proof.extend(deductC2) proof.extend(t7) proof.extend([mp1, mp2]) curLen = len(proof) #for debug vector += 1 n += 1 return proof
def Calmar_Theorem(F, values): ''' Calmar's Theorem implementation :param F: Propositional calculus formula: Variable or Node :return: ''' if type(F) == Variable: v = Variable(F.symbol, _not=True if values[F.symbol] == 1 else 0) return [v] if F._not: # F = !G G = Node(None, None) G.left = F.left # Copy G, but F = !G G.right = F.right if F.calculate(values) == 1: G.msg = "Calmar's theorem, 1.a) F^(alpha) = G^(alpha) => hypoth |- G^() = F^()" return [G] else: # !f = !!g t2 = build_T2(G) T2 = t2[-1] nng = MP(G, T2) res = t2 res.append(nng) return res else: # F = G->H res = [] G = F.left H = F.right if G.calculate(values) == 0: nG = notFormula(G) t3 = build_T3(G, H) T = t3[-1] mp = MP(nG, T) res = t3 res.append(mp) elif H.calculate(values) == 1: # h->(g->h) h = H hgh = axiom.A1(H, G) gh = MP(h, hgh) return [hgh, gh] else: # F = !(G->H) # have G, !H. proof !(G->H) nH = notFormula(H) t6 = build_T6(G, H) T6 = t6[-1] f1 = MP(G, T6) f2 = MP(nH, f1) res = t6 res.extend([f1, f2]) return res
proof.extend(deductC2) proof.extend(t7) proof.extend([mp1, mp2]) curLen = len(proof) #for debug vector += 1 n += 1 return proof if __name__ == '__main__': f = Variable('F') g = Variable('G') nf = notFormula(f) nnf = notFormula(nf) Ad_Theorem(Node(nnf, f)) t4 = build_T4(f, g) T4 = t4[-1] t5 = build_T5(f, g) t6 = build_T6(f, g) t7 = build_T7(f, g) T7 = t7[-1] print('no exception? it is small victory')
print(" interpretation %s:" % (repr(interpretation),)) self.compare(parsed.eval(interpretation), result, "eval") def status(self): print("TESTED %d" % (self.tested,)) print("PASSED %d" % (self.passed,)) if self.tested == self.passed: print("OK") else: print("ERROR") t = Tester() t.test( Variable('a'), 'a', [ ({'a':True}, True), ({'a':False}, False), ]) t.test( Negation(Variable('a')), '-a', [ ({'a':True}, False), ({'a':False}, True), ]) interps2 = [{ 'a': False, 'b': False }, { 'a': False, 'b': True },
pass return False def if_Axiom(F): """ Check if F is Axiom """ return if_A1(F) or if_A2(F) or if_A3(F) if __name__ == '__main__': f1 = Variable('F') f2 = Node(Variable('F'), Variable('G')) print(MP(f1, f2)) exit() f1 = "(A->B)" f2 = "(A->!(B->!C))" tests = [ "F->(G->F)", "G->(F->G)", "F->(F->F)", "!F->((A->B)->!F)", "(A->(B->C))->((A->B)->(A->C))", "({0}->(B->{1}))->(({0}->B)->({0}->{1}))".format(f1, f2), "(!G->!F)->((!G->F)->G)", "(!G->!G)->((!G->G)->G)", "(!(f1)->!(f2))->((!(f1)->(f2))->(f1))".replace('f1', f1).replace('f2', f2) ]