Exemple #1
0
def test_eliminate_existential_witness_assumption(debug=False):
    assumptions = [Schema('Ax[(R(x)->(Q(x)&~Q(x)))]'),
                   Schema('Ex[R(x)]'), Schema('R(c17)')]
    prover = Prover(assumptions, '(Q(c17)&~Q(c17))')
    step1 = prover.add_assumption('R(c17)')
    step2 = prover.add_assumption('Ax[(R(x)->(Q(x)&~Q(x)))]')
    step3 = prover.add_universal_instantiation(
        '(R(c17)->(Q(c17)&~Q(c17)))', step2, 'c17')
    prover.add_tautological_inference('(Q(c17)&~Q(c17))', [step1, step3])

    if (debug):
        print('Testing eliminate_existential_witness_assumption for the '
              'following proof:\n',
              prover.proof)
    eliminated = eliminate_existential_witness_assumption(prover.proof, 'c17')
    assert eliminated.assumptions == Prover.AXIOMS + assumptions[:-1]
    # Will be tested with the course staff's implementation of is_tautology
    assert is_tautology(
        Formula('~', eliminated.conclusion).propositional_skeleton())
    if debug:
        print('Verifying returned proof (' +
              '{:,}'.format(len(eliminated.lines)),
              'lines) of', eliminated.conclusion)
    # Will be tested with the course staff's implementation of is_valid
    assert eliminated.is_valid()
    def verify_t_justification(self, line):
        """ Returns whether the line with the given number is a tautology """
        assert line < len(self.lines)
        justification = self.lines[line].justification
        assert justification[0] == 'T'
        assert len(justification) == 1

        l = self.lines[line]
        z_skel = l.formula.propositional_skeleton()  # get the z form
        return is_tautology(z_skel)  # check if it's a tautology
Exemple #3
0
 def verify_t_justification(self, line):
     """ Returns whether the line with the given number is a tautology """
     assert line < len(self.lines)
     justification = self.lines[line].justification
     assert justification[0] == 'T'
     assert len(justification) == 1
     # Task 9.7
     formula = self.lines[line].formula
     prop_formula = PropositionalFormula.from_infix(
         formula.propositional_skeleton().infix())
     assert type(prop_formula) is PropositionalFormula
     return is_tautology(prop_formula)
Exemple #4
0
def test_combine_contradictions(debug=False):
    common_assumptions = [
        Schema('(~Ax[R(x)]->Ex[~R(x)])', {'x', 'R'}),
        Schema('Ex[~R(x)]'),
        Schema('Ax[R(x)]')
    ]

    # Contradiction from 'Ex[~R(x)]' and 'Ax[(~R(x)->(Q()&~Q()))]'
    prover_true = Prover(common_assumptions + ['Ax[(~R(x)->(Q()&~Q()))]'],
                         '(Q()&~Q())')
    step1 = prover_true.add_assumption('Ex[~R(x)]')
    step2 = prover_true.add_assumption('Ax[(~R(x)->(Q()&~Q()))]')
    step3 = prover_true.add_instantiated_assumption(
        '((Ax[(~R(x)->(Q()&~Q()))]&Ex[~R(x)])->(Q()&~Q()))', Prover.ES, {
            'R(v)': '~R(v)',
            'Q()': '(Q()&~Q())'
        })
    prover_true.add_tautological_inference('(Q()&~Q())', [step1, step2, step3])

    # Contradiction from 'Ax[R(x)]' and '~Ax[(~R(x)->(Q()&~Q()))]'
    prover_false = Prover(common_assumptions + ['~Ax[(~R(x)->(Q()&~Q()))]'],
                          '(Ax[R(x)]&~Ax[R(x)])')
    step1 = prover_false.add_assumption('Ax[R(x)]')
    step2 = prover_false.add_assumption('~Ax[(~R(x)->(Q()&~Q()))]')
    step3 = prover_false.add_instantiated_assumption(
        '(~Ax[(~R(x)->(Q()&~Q()))]->Ex[~(~R(x)->(Q()&~Q()))])',
        common_assumptions[0], {'R(v)': '(~R(v)->(Q()&~Q()))'})
    step4 = prover_false.add_tautological_inference('Ex[~(~R(x)->(Q()&~Q()))]',
                                                    [step2, step3])
    step5 = prover_false.add_instantiated_assumption('(Ax[R(x)]->R(x))',
                                                     Prover.UI, {'c': 'x'})
    step6 = prover_false.add_tautological_inference(
        '(~(~R(x)->(Q()&~Q()))->~Ax[R(x)])', [step5])
    step7 = prover_false.add_existential_derivation('~Ax[R(x)]', step4, step6)
    step8 = prover_false.add_tautological_inference('(Ax[R(x)]&~Ax[R(x)])',
                                                    [step1, step7])

    if (debug):
        print('Testing combine_contradictions for the following two proofs:\n',
              prover_true.proof, '\n', prover_false.proof)
    combined = combine_contradictions(prover_true.proof, prover_false.proof)
    assert combined.assumptions == Prover.AXIOMS + common_assumptions
    # Will be tested with the course staff's implementation of is_tautology
    assert is_tautology(
        Formula('~', combined.conclusion).propositional_skeleton())
    if debug:
        print(
            'Verifying returned proof (' + '{:,}'.format(len(combined.lines)),
            'lines) of', combined.conclusion)
    # Will be tested with the course staff's implementation of is_valid
    assert combined.is_valid()
Exemple #5
0
def test_model_or_inconsistent(debug=False):
    for six_element_group_primitives in [
            SIX_ELEMENT_COMMUTATIVE_GROUP_PRIMITIVES,
            SIX_ELEMENT_NON_COMMUTATIVE_GROUP_PRIMITIVES
    ]:
        for commutativity in [{
                COMMUTATIVITY_AXIOM
        }.union(SIX_ELEMENT_COMMUTATIVITY_CLOSURE), {
                NON_COMMUTATIVITY
        }.union(SIX_ELEMENT_NON_COMMUTATIVE_GROUP_NON_COMMUTATIVITY_CLOSURE)]:
            sentences = {
                Formula.parse(sentence)
                for sentence in six_element_group_primitives.union(
                    {ZERO_AXIOM, NEGATION_AXIOM}, SIX_ELEMENT_NEGATION_CLOSURE,
                    commutativity)
            }
            if (debug):
                print(
                    'Testing model_or_inconsistent for the six-element',
                    'non-commutative' if six_element_group_primitives
                    == SIX_ELEMENT_NON_COMMUTATIVE_GROUP_PRIMITIVES else
                    'commutative', 'group with the zero, negation, and ',
                    'commutativity' if COMMUTATIVITY_AXIOM in commutativity
                    else 'non-commutativity', 'axioms')
            result = model_or_inconsistent(sentences, SIX_ELEMENTS)
            if type(result) is Model:
                if debug:
                    print('Verifying returned model:', result)
                # Will be tested with the course staff's implementation of is_model_of
                assert result.is_model_of(
                    {str(sentence)
                     for sentence in sentences})
            else:
                assert type(result) is Proof
                assert set(result.assumptions).issubset(
                    {Schema(str(s))
                     for s in sentences}.union(Prover.AXIOMS))
                # Will be tested with the course staff's implementation of is_tautology
                assert is_tautology(
                    Formula('~', result.conclusion).propositional_skeleton())
                if debug:
                    print(
                        'Verifying returned proof (' +
                        '{:,}'.format(len(result.lines)), 'lines) of',
                        result.conclusion)
                # Will be tested with the course staff's implementation of is_valid
                assert result.is_valid()