Пример #1
0
def prove(theory, consequence):
    """ Dokaze, ci z theory (zoznam formul) vyplyva formula consequence. """
    # mali by sme skontrolovat, ci teoria sama o sebe dava zmysel
    # ale ak by aj bola nekonzistentna, tak z nej logicky vyplyva
    # vsetko, takze to tu kontrolovat nebudeme

    f = Conjunction([Conjunction(theory), Negation(consequence)])
    c = f.toCnf()

    varMap = cnf.VariableMap()
    c.extendVarMap(varMap)

    fn = "problem_cnf.txt"
    of = open(fn, "w")
    c.writeToFile(of, varMap)
    of.close()

    solver = sat.SatSolver()
    ok, sol = solver.solve(fn, "problem_out.txt")
    if ok:
        print("!!! {}   NEVYPLYVA Z   {} lebo:".format(
            consequence.toString(), ', '.join([x.toString() for x in theory])))
        print("  {}".format(repr(sol)))
        revMap = varMap.reverse()
        print("  {}".format(repr([revMap[i] for i in sol])))
    else:
        print("{}   VYPLYVA Z   {}".format(
            consequence.toString(), ', '.join([x.toString() for x in theory])))
Пример #2
0
def prove(theory, consequence):
    """ Dokaze, ci z theory (zoznam formul) vyplyva formula consequence. """
    # mali by sme skontrolovat, ci teoria sama o sebe dava zmysel
    # ale ak by aj bola nekonzistentna, tak z nej logicky vyplyva
    # vsetko, takze to tu kontrolovat nebudeme

    f = Conjunction([
            Conjunction(theory),
            Negation(consequence)
        ])
    c = f.toCnf()

    varMap = cnf.VariableMap()
    varMap.extend(c)

    fn = "problem_cnf.txt"
    of = open(fn, "w")
    c.writeToFile(of, varMap)
    of.close()

    solver = sat.SatSolver()
    ok, sol = solver.solve(fn, "problem_out.txt")
    if ok:
        print("!!! {}   NEVYPLYVA Z   {} lebo:".format(
            consequence.toString(),
            ', '.join([x.toString() for x in theory])))
        print("  {}".format(repr(sol)))
        revMap = varMap.reverse()
        print("  {}".format(repr([str(cnf.Literal.fromInt(i,varMap)) for i in sol])))
    else:
        print("{}   VYPLYVA Z   {}".format(
            consequence.toString(),
            ', '.join([x.toString() for x in theory])))
Пример #3
0
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
Пример #4
0
 def conjunction(self, expr1, expr2):
     # Creating a conjuction
     return Conjunction(expr1, expr2)
Пример #5
0
def And(*args):
    if len(args) == 1 and type(args[0]) is list:
        return Conjunction(args[0])
    return Conjunction(args)
Пример #6
0
        return Disjunction(args[0])
    return Disjunction(args)


a = Var('a')
b = Var('b')
c = Var('c')
d = Var('d')

try:
    t.test2(a)

    t.test2(Not(a))

    t.test2(Conjunction([
        a,
        b,
    ]))

    t.test2(Conjunction([
        Not(a),
        a,
    ]))

    t.test2(Conjunction([
        a,
        Not(a),
    ]))

    t.test2(Conjunction([
        Not(a),
        Not(a),
Пример #7
0
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
Пример #8
0
interps2 = [{
    'a': False,
    'b': False
}, {
    'a': False,
    'b': True
}, {
    'a': True,
    'b': False
}, {
    'a': True,
    'b': True
}]

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),
Пример #9
0
    # obrázku je pes.
    ax1 = Implication(
        Var('obrazok'),
        And([
            Or([Var('macka'), Var('pes')]),
            Or([Not(Var('macka')), Not(Var('pes'))]),
        ]),
    )
    ax2 = Implication(Var('macka'), Var('roztrha'))
    ax3 = Implication(Var('roztrha'), Var('smutna'))
    conclusion = Implication(
        And([Var('obrazok'), Not(Var('smutna'))]),
        Var('pes'),
    )

    cax1 = Conjunction([ax1, ax2, ax3])
    t.testTableau(True, [T(Conjunction([cax1, Not(conclusion)]))])
    t.testTableau(True, [F(Implication(cax1, conclusion))])
    t.testTableau(True, [T(cax1), F(conclusion)])
    t.testTableau(True, [T(ax1), T(ax2), T(ax3), F(conclusion)])
    t.testTableau(False, [T(cax1)])
    t.testTableau(False, [F(conclusion)])

    # Bez práce nie sú koláče. Ak niekto nemá ani koláče, ani chleba, tak bude
    # hladný. Na chlieb treba múku. Dokážte, že ak niekto nemá múku a je
    # najedený (nie je hladný), tak pracoval.
    ax1 = Implication(Var('kolace'), Var('praca'))

    ax2 = Implication(And([Not(Var('kolace')),
                           Not(Var('chlieb'))]), Var('hlad'))
    ax3 = Implication(Var('chlieb'), Var('muka'))
Пример #10
0
     ({
         'a': True
     }, True),
     ({
         'a': False
     }, False),
 ]),
 (Negation(Variable('a')), '-a', 1, ['a'], [
     ({
         'a': True
     }, False),
     ({
         '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),
Пример #11
0
t.test(
        Negation(Variable('a')), '-a',
        [
            ({'a':True}, False),
            ({'a':False}, True),
        ])


interps2 = [{ 'a': False, 'b': False },
            { 'a': False, 'b': True  },
            { 'a': True , 'b': False },
            { 'a': True , 'b': True  }]

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),