Пример #1
0
def test_remove_assumption(debug=False):
    from propositions.some_proofs import prove_and_commutativity

    for oldp in [
            DISJUNCTION_COMMUTATIVITY_PROOF,
            prove_and_commutativity(), DISJUNCTION_RIGHT_ASSOCIATIVITY_PROOF,
            DISJUNCTION_ROTATION_PROOF, TRI_AND_PROOF
    ]:
        p = oldp
        assert p.is_valid(), offending_line(p)
        while (True):
            rb = None
            for r in p.rules:
                if r != MP and len(r.assumptions) > 0:
                    rb = r
                    break
            if rb is None:
                break
            pr = prove_from_encoding(rb)
            p = inline_proof(p, pr)
        assert p.is_valid(), offending_line(p)
        if debug:
            print("Testing remove_assumption on:", p)
        pp = remove_assumption(p)
        if debug:
            print("Got:", pp)
        assert pp.statement.assumptions == p.statement.assumptions[:-1]
        assert pp.statement.conclusion == Formula('->',
                                                  p.statement.assumptions[-1],
                                                  p.statement.conclusion)
        assert pp.rules.issubset(p.rules.union({MP, I0, I1, D}))
        assert pp.is_valid(), offending_line(pp)
Пример #2
0
def test_prove_from_opposites(debug=False):
    assumptions = (Formula.parse('(~~p->~~q)'), Formula.parse('p'),
                   Formula.parse('~q'))
    rules = {MP, I1, N, NI}
    pf = Proof(InferenceRule(assumptions, Formula.parse('(q->p)')), rules, [
        Proof.Line(Formula.parse('p')),
        Proof.Line(Formula.parse('(p->(q->p))'), I1, []),
        Proof.Line(Formula.parse('(q->p)'), MP, [0, 1])
    ])
    pnf = Proof(InferenceRule(assumptions, Formula.parse('~(q->p)')), rules, [
        Proof.Line(Formula.parse('(~~p->~~q)')),
        Proof.Line(Formula.parse('((~~p->~~q)->(~q->~p))'), N, []),
        Proof.Line(Formula.parse('(~q->~p)'), MP, [0, 1]),
        Proof.Line(Formula.parse('((~q->~p)->(p->q))'), N, []),
        Proof.Line(Formula.parse('(p->q)'), MP, [2, 3]),
        Proof.Line(Formula.parse('p')),
        Proof.Line(Formula.parse('q'), MP, [5, 4]),
        Proof.Line(Formula.parse('~q')),
        Proof.Line(Formula.parse('~p'), MP, [7, 2]),
        Proof.Line(Formula.parse('(q->(~p->~(q->p)))'), NI, []),
        Proof.Line(Formula.parse('(~p->~(q->p))'), MP, [6, 9]),
        Proof.Line(Formula.parse('~(q->p)'), MP, [8, 10])
    ])
    g = Formula.parse('(p->r)')
    if debug:
        print("Testing prove_from_opposites with assumptions", assumptions,
              "and conclusion", g)
    p = prove_from_opposites(pf, pnf, g)
    assert p.statement.conclusion == g
    assert p.statement.assumptions == assumptions
    assert p.rules == rules.union({I2})
    assert p.is_valid(), offending_line(p)
def __test_prove_inference(prover, rule, rules, debug):
    return test_inference_proof(prover, rule, rules, debug)  # My own util
    if debug:
        print('Testing', prover.__qualname__)
    proof = prover()
    if proof.statement != rule:
        print('*' * 25)
        print('Error: proof.statement != rule')
        print('proof.statement: ', proof.statement)
        print('rule: ', rule)
        print('*' * 25)
    assert proof.statement == rule
    assert proof.rules.issubset(rules), \
           "got " + str(proof.rules) + ", expected " + str(rules)
    if not proof.is_valid() or not (proof):
        print('=' * 35)
        print("INVALID Proof:")
        print(proof)

        invalid_lines = []
        for line_number in range(0, len(proof.lines)):
            if not proof.is_line_valid(line_number):
                invalid_lines.append(line_number)
        if len(invalid_lines) > 0:
            print('')
        print('=' * 35)
        sys.exit(1)

    assert proof.is_valid(), offending_line(proof)
    print('=' * 35)
    print("VALID Proof:")
    print(proof)
    print('=' * 35)
Пример #4
0
def __test_prove_inference(prover, rule, rules, debug):
    if debug:
        print('Testing', prover.__qualname__)
    proof = prover()
    assert proof.statement == rule
    assert proof.rules.issubset(rules), \
           "got " + str(proof.rules) + ", expected " + str(rules)
    assert proof.is_valid(), offending_line(proof)
def test_prove_tautology(debug=False):
    for f, m in [('(p->p)', {
            'p': True
    }), ('(p->p)', {
            'p': False
    }), ('(p->p)', {}), ('((~q->~p)->(p->q))', {
            'p': True,
            'q': False
    }), ('((~q->~p)->(p->q))', {
            'p': False
    }), ('((~q->~p)->(p->q))', {})]:
        f = Formula.parse(f)
        if debug:
            print("Testing prove_tautology on formula", f, "and model", m)
        p = prove_tautology(f, frozendict(m))
        assert p.statement.conclusion == f
        assert p.statement.assumptions == tuple(formulae_capturing_model(m))
        assert p.rules == AXIOMATIC_SYSTEM
        assert p.is_valid(), offending_line(p)

    for t in [
            '((~q->~p)->(p->q))',
            '(~~p->p)',
            '(p->~~p)',
            '((~p->~q)->((p->~q)->~q))',
            #'((p1->(p2->(p3->p4)))->(p3->(p2->(p1->p4))))',
            '((p2->(p3->p4))->(p3->(p2->p4)))',
            #'(((((p->q)->(~r->~s))->r)->t)->((t->p)->(s->p)))',
            #'(((((r->q)->(~r->~q))->r)->t)->((t->r)->(q->r)))',
            '(~~~~x13->~~x13)'
    ]:
        t = Formula.parse(t)
        if debug:
            print("Testing prove_tautology on formula", t)
        p = prove_tautology(t)
        if debug:
            if len(p.lines) < 20:
                print("Proof is", p)
            else:
                print("Proof has", len(p.lines), "lines.")
        assert len(p.statement.assumptions) == 0
        assert p.statement.conclusion == t
        assert p.rules == AXIOMATIC_SYSTEM
        assert p.is_valid(), offending_line(p)
Пример #6
0
def test_prove_in_model_full(debug=False):
    for (f, m, a, cp) in [ ('x', {'x':True}, ['x'], ''),
                           ('x',{'x':False}, ['~x'], '~'),
                           ('~x', {'x':False}, ['~x'], ''),
                           ('~x', {'x':True}, ['x'], '~'),
                           ('x', {'x':True, 'z5':False}, ['x', '~z5'], ''),
                           ('(p->~p)', {'p':True}, ['p'], '~'),
                           ('(p->~p)', {'p':False}, ['~p'], ''),
                           ('(p->q)', {'p':True, 'q':True}, ['p','q'], ''),
                           ('(p->q)', {'p':True, 'q':False}, ['p','~q'], '~'),
                           ('(p->q)', {'p':False, 'q':True}, ['~p','q'], ''),
                           ('(p->q)', {'p':False, 'q':False}, ['~p', '~q'], ''),
                           ('~~~~y7', {'y7':True}, ['y7'], ''),
                           ('~~~~y7', {'y7':False}, ['~y7'], '~'),
                           ('~(~p->~q)', {'p':True, 'q':True}, ['p','q'], '~'),
                           ('~(~p->~q)', {'p':False, 'q':True}, ['~p','q'], ''),
                           ('((p1->p2)->(p3->p4))',
                            {'p1':True, 'p2':True, 'p3':True, 'p4':True},
                            ['p1','p2','p3','p4'], ''),
                           ('((p1->p2)->(p3->p4))',
                            {'p1':True, 'p2':True, 'p3':True, 'p4':False},
                            ['p1','p2','p3','~p4'], '~'),
                           ('((p1->p2)->(p3->p4))',
                            {'p1':True, 'p2':False, 'p3':True, 'p4':False},
                            ['p1','~p2','p3','~p4'], ''),
                           ('(~(~x->~~y)->(z->(~x->~~~z)))',
                            {'z':True, 'x':False, 'y':False},
                            ['~x','~y','z'], '~'),
                           ('T', {}, [], ''),
                           ('F', {}, [], '~'),
                           ('(p|q)', {'p': True, 'q': True}, ['p', 'q'], ''),
                           ('(p|q)', {'p': True, 'q': False}, ['p', '~q'], ''),
                           ('(p|q)', {'p': False, 'q': True}, ['~p', 'q'], ''),
                           ('(p|q)',
                            {'p': False, 'q': False}, ['~p', '~q'], '~'),
                           ('(p&q)', {'p': True, 'q': True}, ['p', 'q'], ''),
                           ('(p&q)', {'p': True, 'q': False}, ['p', '~q'], '~'),
                           ('(p&q)', {'p': False, 'q': True}, ['~p', 'q'], '~'),
                           ('(p&q)',
                            {'p': False, 'q': False}, ['~p', '~q'], '~'),
                           ('~(~(q|p)&(r->~(s|q)))',
                            {'p': False, 'q': False, 'r': False, 's': False},
                            ['~p', '~q', '~r', '~s'], '~'),
                           ('~(~(q|p)&(r->~(s|q)))',
                            {'p': False, 'q': False, 'r': True, 's': True},
                            ['~p', '~q', 'r', 's'], '')
                           ]:
        c = Formula.parse(cp+f)
        f = Formula.parse(f)
        a = (Formula.parse(v) for v in a)
        if debug:
            print('Testing prove_in_model_full on formula',f, 'in model', m)
        p = prove_in_model_full(f, frozendict(m))
        assert p.statement == InferenceRule(a, c)
        assert p.rules == AXIOMATIC_SYSTEM_FULL
        assert p.is_valid(), offending_line(p)
def test_combine_proofs(debug=False):
    # test1
    p = Formula('p')
    lp = Proof.Line(p)
    q = Formula('q')
    nq = Formula('~',q)
    lnq = Proof.Line(nq)
    pfp = Proof(InferenceRule([p,nq],p), AXIOMATIC_SYSTEM, [lp])
    pfnq = Proof(InferenceRule([p,nq],nq), AXIOMATIC_SYSTEM, [lnq])
    h1 = Formula.parse('~(p->q)')

    #test2
    np = Formula('~',p)
    lnp = Proof.Line(np)
    lq = Proof.Line(q)
    nnq = Formula('~',nq)
    rip = Formula('->',Formula('r'),p)
    linesp = [lp,
              Proof.Line(Formula('->',p,rip),I1,[]),
              Proof.Line(rip, MP, [0,1])]
    linesq = [lq,
              Proof.Line(Formula('->',q,nnq),NN,[]),
              Proof.Line(nnq, MP, [0,1])]                            
    pfp2 = Proof(InferenceRule([p,q],rip), {MP,NN,I1, NI}, linesp)
    pfnq2 = Proof(InferenceRule([p,q],nnq), {MP,NN,I1, NI}, linesq)
    h2 = Formula.parse('~((r->p)->~q)')
    
    # Variant for test1.5
    pfp15 = Proof(InferenceRule([p,nq],rip), AXIOMATIC_SYSTEM, linesp)
    h15 = Formula.parse('~((r->p)->q)')

    # test3
    pp3 = Formula.parse('(x->y)')
    pq3 = Formula.parse('(~x->y)')
    y = Formula('y')
    pfp3 = Proof(InferenceRule([y],pp3), {MP, R, I0, I1},
                 [Proof.Line(y),
                  Proof.Line(Formula.parse('(y->(x->y))'),I1,[]),
                  Proof.Line(Formula.parse('(x->y)'),MP,[0,1])])
    pfq3 = Proof(InferenceRule([y],pq3), {MP, R, I0, I1},
                 [Proof.Line(y),
                  Proof.Line(Formula.parse('(y->(~x->y))'),I1,[]),
                  Proof.Line(Formula.parse('(~x->y)'),MP,[0,1])])
    h3 = y
 
    for pp, pnq, h, r in [(pfp,pfnq,h1,NI),(pfp15,pfnq, h15, NI),
                          (pfp2,pfnq2, h2, NI), (pfp3,pfq3,h3,R)]:
        if debug:
            print('Testing combine_proof of', h,'from', pp.statement, 'and',
                  pnq.statement, 'using rule', r)
        pnpiq = combine_proofs(pp, pnq, h, r)
        assert pnpiq.rules == pp.rules
        assert pnpiq.statement.conclusion == h
        assert pnpiq.statement.assumptions == pp.statement.assumptions
        assert pnpiq.is_valid(), offending_line(pnpiq)
def test_model_or_inconsistency(debug=False):
    for s in [{'p'}, {'p', '~p'}, {'(p->p)'}, {'~(p->p)'},
              {'(x->y)', 'x', '~y'}, {'(x->y)', 'x', '~z'}]:
        s = {Formula.parse(f) for f in s}
        if debug:
            print("Testing model_or_inconsistency on", s)
        r = model_or_inconsistency(s)
        if type(r) is Proof:
            assert r.statement.conclusion == Formula.parse('~(p->p)')
            assert set(r.statement.assumptions) == s
            assert r.rules == AXIOMATIC_SYSTEM
            assert r.is_valid(), offending_line(r)
        else:
            assert is_model(r)
            for f in s:
                assert evaluate(f, r)
Пример #9
0
def test_reduce_assumption(debug=False):
    for f, m, v in [ ('(y->x)', {'x':True}, 'y'),
                     ('(p->p)', {}, 'p'),
                     ('(p->(r->q))', {'p':True, 'q':True}, 'r')]:
        f = Formula.parse(f)
        m[v]=True
        pt = prove_in_model(f, frozendict(m))
        m[v]=False
        pf = prove_in_model(f, frozendict(m))
        if debug:
            print("testing reduce assumption on", pt.statement, "and",
                  pf.statement)
        p = reduce_assumption(pt,pf)
        assert p.statement.conclusion  == pf.statement.conclusion
        assert p.statement.assumptions[:] == pt.statement.assumptions[:-1]
        assert p.rules == AXIOMATIC_SYSTEM
        assert p.is_valid(), offending_line(p)
Пример #10
0
def test_proof_or_counterexample(debug=False):
    for f in [ 'x', '(y->y)', '((x->y)->(x->y))', '((x->y)->z)',
               '((~p->~q)->((p->~q)->~q))', '((~p->~r)->((p->~q)->~q))',
               '((~p->~q)->((~p->q)->p))', '((q->~p)->((~~~p->r)->(q->r)))',
               '((q->p)->((~q->p)->p))', '((p->~q)->(q->~p))',
               '((p->q)->(~p->~q))']:
        if debug:
            print("Testing proof_or_counterexample on", f)
        f = Formula.parse(f)
        p = proof_or_counterexample(f)
        if type(p) is Proof:
            assert len(p.statement.assumptions) == 0
            assert p.statement.conclusion == f
            assert p.rules == AXIOMATIC_SYSTEM
            assert p.is_valid(), offending_line(p)
        else:
            assert not evaluate(f,p)
Пример #11
0
def test_prove_from_encoding(debug=True):
    from propositions.some_proofs import prove_and_commutativity
    from propositions.tautology import encode_as_formula

    for p in [
            DISJUNCTION_COMMUTATIVITY_PROOF,
            prove_and_commutativity(), DISJUNCTION_RIGHT_ASSOCIATIVITY_PROOF
    ]:
        for r in p.rules:
            if (debug):
                print("\nTesting prove_from_encoding on:", r)
            pp = prove_from_encoding(r)
            if (debug):
                print("Got:", pp)
            assert pp.statement == r
            newrule = InferenceRule([], encode_as_formula(r))
            assert pp.rules == {newrule, MP}
            assert pp.is_valid(), offending_line(pp)
Пример #12
0
def test_prove_sound_inference(debug=False):
    for a,c in [ ([], '(p->p)'),
                 (['p'], 'p'),
                 (['p'], '~~p'),
                 (['~~p'], 'p'),
                 (['p'], '(q->p)'),
                 (['(p->q)'], '(~q->~p)'),
                 (['p', 'q'], '~(p->~q)'),
                 (['(p->q)', '(q->r)'], '(p->r)'),
                 (['p','(p->q)', '(q->r)', '~r'],'x'),
                 (['p', '~p'], 'q')]:
        r = InferenceRule((Formula.parse(f) for f in a), Formula.parse(c))
        if debug:
            print("Testing prove_sound_inference on", r)
        p = prove_sound_inference(r)
        assert p.statement == r
        assert p.rules == AXIOMATIC_SYSTEM
        assert p.is_valid(), offending_line(p)
Пример #13
0
def test_prove_by_way_of_contradiction(debug=False):
    assumptions = (Formula.parse('(~r->p)'), Formula.parse('~p'),
                   Formula.parse('~r'))
    p = Proof(InferenceRule(assumptions, Formula.parse('~(p->p)')), {MP, NI}, [
        Proof.Line(Formula.parse('~r')),
        Proof.Line(Formula.parse('(~r->p)')),
        Proof.Line(Formula.parse('p'), MP, [0, 1]),
        Proof.Line(Formula.parse('(p->(~p->~(p->p)))'), NI, []),
        Proof.Line(Formula.parse('(~p->~(p->p))'), MP, [2, 3]),
        Proof.Line(Formula.parse('~p')),
        Proof.Line(Formula.parse('~(p->p)'), MP, [5, 4])
    ])
    if debug:
        print("Testing prove_by_way_of_contradiction on proof of", p.statement)
    p = prove_by_way_of_contradiction(p)
    assert p.statement.conclusion == Formula.parse('r')
    assert p.statement.assumptions == assumptions[:-1]
    assert p.rules == {MP, I0, I1, D, N, NI}
    assert p.is_valid(), offending_line(p)
Пример #14
0
def test_prove_corollary(debug=False):
    x = Formula('x')
    y = Formula('y')

    pf = Proof(InferenceRule([x], x), AXIOMATIC_SYSTEM, [Proof.Line(x)])
    g1 = Formula.parse('~~x')
    r1 = NN

    g2 = Formula.parse('(y->~~x)')
    r2 = I1

    g3 = Formula.parse('((~y->~~x)->~~x)')
    r3 = R

    for g, r in [(g1, r1), (g2, r2), (g3, r3)]:
        if debug:
            print('Testing prove_corollary of', g, 'from proof of',
                  pf.statement, 'using rule', r)
        pp = prove_corollary(pf, g, r)
        assert pp.rules == pf.rules
        assert pp.statement.assumptions == pf.statement.assumptions
        assert pp.statement.conclusion == g
        assert pp.is_valid(), offending_line(pp)
        pf = pp