def propositional_prove(formula: str, assumptions: List[str]):
    from propositions.syntax import Formula
    from propositions.tautology import proof_or_counterexample, prove_in_model, evaluate, all_models
    from propositions.tautology import prove_tautology, encode_as_formula
    from propositions.proofs import InferenceRule

    f = Formula.parse(formula)
    if f is None:
        print_error('Could not parse the given formula')
        return None

    collected_assumptions = []
    for assumption in assumptions:
        a = Formula.parse(assumption)
        if a is None:
            print_error('Could not parse the given assumption')
            return None
        collected_assumptions.append(a)

    f = encode_as_formula(InferenceRule(collected_assumptions, f))

    valid_proofs = []
    for model in all_models(list(f.variables())):
        if not evaluate(f, model):
            valid_proofs.append(prove_in_model(f, model))

    if len(valid_proofs) > 0:
        print_result('Found valid proof:\n' + str(valid_proofs))
        return True

    print_result('No valid proof.\n' + str(proof_or_counterexample(f)))
    return False
Exemple #2
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)