Ejemplo n.º 1
0
def test_evaluate_inference(debug=False):
    from propositions.proofs import InferenceRule

    # Test 1
    rule1 = InferenceRule([Formula.parse('p'), Formula.parse('q')],
                          Formula.parse('r'))
    for model in all_models(['p', 'q', 'r']):
        if debug:
            print('Testing evaluation of inference rule', rule1, 'in model',
                  model)
        assert evaluate_inference(rule1, frozendict(model)) == \
               (not model['p']) or (not model['q']) or model['r']

    # Test 2
    rule2 = InferenceRule([Formula.parse('(x|y)')],
                          Formula.parse('x'))
    for model in all_models(['x', 'y']):
        if debug:
            print('Testing evaluation of inference rule', rule2, 'in model',
                  model)
        assert evaluate_inference(rule2, frozendict(model)) == \
               (not model['y']) or model['x']

    # Test 3
    rule3 = InferenceRule([Formula.parse(s) for s in ['(p->q)', '(q->r)']],
                           Formula.parse('r'))
    for model in all_models(['p', 'q', 'r']):
        if debug:
            print('Testing evaluation of inference rule', rule3, 'in model',
                  model)
        assert evaluate_inference(rule3, frozendict(model)) == \
               (model['p'] and not model['q']) or \
               (model['q'] and not model['r']) or model['r']
Ejemplo n.º 2
0
def test_is_sound_inference(debug=False):
    from propositions.proofs import InferenceRule

    for assumptions,conclusion,tautological in [
            [[], '(~p|p)', True],
            [[], '(p|p)', False],
            [[], '(~p|q)', False],
            [['(~p|q)', 'p'], 'q', True],
            [['(p|q)', 'p'], 'q', False],
            [['(p|q)', '(~p|r)'], '(q|r)', True],
            [['(p->q)', '(q->r)'], 'r', False],
            [['(p->q)', '(q->r)'], '(p->r)', True],
            [['(x|y)'], '(y|x)', True],
            [['x'], '(x|y)', True],
            [['(x&y)'], 'x', True],
            [['x'], '(x&y)', False]]:
        rule = InferenceRule(
            [Formula.parse(assumption) for assumption in assumptions],
            Formula.parse(conclusion))
        if debug:
            print('Testing whether', rule, 'is sound')
        assert is_sound_inference(rule) == tautological

    for rule in [MP, I0, I1, D, I2, N, NI, NN, R,
                 A, NA1, NA2, O1, O2, NO, T, NF,
                 N_ALTERNATIVE, AE1, AE2, OE]:
        if debug:
            print('Testing that', rule, 'is sound')
        assert is_sound_inference(rule)
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
def test_evaluate_inference(debug=False):
    from propositions.proofs import InferenceRule

    # Test 1
    rule1 = InferenceRule(
        [Formula.parse('p'), Formula.parse('q')], Formula.parse('r'))
    for model in all_models(['p', 'q', 'r']):
        if debug:
            print('Testing evaluation of inference rule', rule1, 'in model',
                  model)
        evaluation_result = evaluate_inference(rule1, frozendict(model)) == \
               (not model['p']) or (not model['q']) or model['r']
        if not evaluation_result:
            print("FAILED.")
            print("*" * 25)
            print("EXPECTED:", (not model['p']) or (not model['q'])
                  or model['r'])
            print(
                "REASON:  ",
                "(not {}) or (not {}) or {}".format(model['p'], model['q'],
                                                    model['r']))
            print("*" * 25)

        assert evaluation_result
        # assert evaluate_inference(rule1, frozendict(model)) == \
        #        (not model['p']) or (not model['q']) or model['r']

    # Test 2
    rule2 = InferenceRule([Formula.parse('(x|y)')], Formula.parse('x'))
    for model in all_models(['x', 'y']):
        if debug:
            print('Testing evaluation of inference rule', rule2, 'in model',
                  model)
        assert evaluate_inference(rule2, frozendict(model)) == \
               (not model['y']) or model['x']

    # Test 3
    rule3 = InferenceRule([Formula.parse(s) for s in ['(p->q)', '(q->r)']],
                          Formula.parse('r'))
    for model in all_models(['p', 'q', 'r']):
        if debug:
            print('Testing evaluation of inference rule', rule3, 'in model',
                  model)
        assert evaluate_inference(rule3, frozendict(model)) == \
               (model['p'] and not model['q']) or \
               (model['q'] and not model['r']) or model['r']
def test_is_tautological_inference(debug=False):
    from propositions.proofs import InferenceRule

    for assumptions,conclusion,tautological in [
            [[], '(~p|p)', True],
            [[], '(p|p)', False],
            [[], '(~p|q)', False],
            [['(~p|q)', 'p'], 'q', True],
            [['(p|q)', 'p'], 'q', False],
            [['(p|q)', '(~p|r)'], '(q|r)', True],
            [['(p->q)', '(q->r)'], 'r', False],
            [['(p->q)', '(q->r)'], '(p->r)', True],
            [['(x|y)'], '(y|x)', True],
            [['x'], '(x|y)', True],
            [['(x&y)'], 'x', True],
            [['x'], '(x&y)', False]]:
        rule = InferenceRule(
            [Formula.from_infix(assumption) for assumption in assumptions],
            Formula.from_infix(conclusion))
        if debug:
            print('Testing whether', rule, 'is a tautological inference')
        assert is_tautological_inference(rule) == tautological