Esempio n. 1
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
Esempio n. 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()
    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])))
Esempio n. 3
0
 def negation(self, expr):
     # Creating a negation
     return Negation(expr)
Esempio n. 4
0
 def test2(self, f):
     self.test(f)
     self.test(Negation(f))
Esempio n. 5
0
        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
    t.test2(Conjunction([Var(str(i)) for i in range(nvars)]))

    t.test2(Disjunction([Var(str(i)) for i in range(nvars)]))

    # some fun
Esempio n. 6
0
 def test2(self, f):
     self.test(copy.deepcopy(f))
     self.test(Negation(f))
Esempio n. 7
0
    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)
                ]),
Esempio n. 8
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
Esempio n. 9
0
File: cv03.py Progetto: stiffix/udvl
        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
}, {
Esempio n. 10
0
}, {
    '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),
    ]),
    (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),
Esempio n. 11
0
        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),
])

valuations2 = [{
    'a': False,
    'b': False
}, {
    'a': False,
    'b': True
}, {
Esempio n. 12
0
        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  },
            { 'a': True , 'b': False },
            { 'a': True , 'b': True  }]

t.test(
        Conjunction( [ Variable('a'), Variable('b') ] ),
        '(a&b)',
        [
Esempio n. 13
0
        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  },
            { 'a': True , 'b': False },
            { 'a': True , 'b': True  }]

t.test(
        Conjunction( [ Variable('a'), Variable('b') ] ),
        '(a&b)',
        [