File size: 3,344 Bytes
c0aca40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""The zero exponent: inside the trusted base x^0 is the constant 1, also when the base is 0.

The trusted base reads a power with exponent 0 as the empty product, so it is 1 for every base, which
is what eval_term and the polynomial builder already did. Z3 5.x leaves (^ x 0.0) unconstrained where
the base is 0, so the two directions of the verification disagreed on exactly that point and every
problem containing a zero exponent ended as UNKNOWN. Both SMT emitters therefore substitute the
constant 1. The tests pin the convention and the emitters; they do not pin what a Z3 release does with
an undefined power, which is why that behaviour is not asserted anywhere.
"""

from fractions import Fraction as F

import z3

from lgmath import exact, pipeline, smt


def power(base, exponent):
    return ("pow", base, ("num", F(exponent)))


def test_the_exact_reading_takes_the_zero_exponent_as_one():
    for base in (("var", "x"), ("num", F(0)), ("add", ("num", F(1)), ("neg", ("num", F(1))))):
        node = power(base, 0)
        for x in (F(0), F(5), F(-7, 3)):
            assert exact.eval_formula(("eq", node, ("num", F(1))), {"x": x}), base
            assert not exact.eval_formula(("ne", node, ("num", F(1))), {"x": x}), base
    assert exact.eval_formula(("eq", power(("var", "x"), 0), ("num", F(1))), {"x": F(0)})
    assert not exact.eval_formula(("eq", power(("var", "x"), 0), ("num", F(0))), {"x": F(0)})


def test_both_smt_emitters_write_the_constant_one():
    for base in (("var", "x"), ("num", F(0))):
        node = power(base, 0)
        text = smt.term_smt(node)
        assert text == "1.0", text
        assert "^" not in text, text
        value = smt.z3_term(node, {})
        solver = z3.Solver()
        solver.add(value != z3.RealVal("1/1"))
        assert solver.check() == z3.unsat, base


def test_a_non_zero_exponent_is_still_a_power():
    text = smt.term_smt(power(("var", "x"), 3))
    assert "^" in text or "*" in text, text
    assert smt.term_smt(power(("var", "x"), 0)) != text
    env = {"x": z3.Real("x")}
    solver = z3.Solver()
    solver.add(smt.z3_term(power(("var", "x"), 3), env) != env["x"] ** 3)
    assert solver.check() == z3.unsat


def test_problems_with_a_zero_exponent_reach_a_verdict():
    proved = pipeline.run("prove x^0 = 1")["final"]
    assert proved["status"] in ("VERIFIED", "SINGLE_PROVER"), proved["reasons"]
    assert proved["answer"]["verdict"] == "proved"
    identity = pipeline.run("solve x^0 = 1 for x")["final"]
    assert identity["status"] in ("VERIFIED", "SINGLE_PROVER"), identity["reasons"]
    assert identity["answer_text"] == "(-inf, +inf)", identity["answer_text"]
    single = pipeline.run("solve x^0 = x for x")["final"]
    assert single["answer_text"] == "{1}", single["answer_text"]
    numeric = pipeline.run("evaluate 0^0")["final"]
    assert numeric["answer_text"] == "1", numeric["answer_text"]


def test_the_zero_exponent_does_not_hide_a_real_condition():
    shifted = pipeline.run("solve (x - 1)^0 = x for x")["final"]
    assert shifted["status"] in ("VERIFIED", "SINGLE_PROVER"), shifted["reasons"]
    assert shifted["answer_text"] == "{1}", shifted["answer_text"]
    false_on_one_point = pipeline.run("prove forall x. x^0 != 0")["final"]
    assert false_on_one_point["answer"]["verdict"] == "proved", false_on_one_point["answer_text"]