예제 #1
0
    def test_get_value_of_function_bool(self):
        """Proper handling of models with functions with bool args."""
        hr = Symbol("hr", FunctionType(REAL, [BOOL, REAL, REAL]))
        hb = Symbol("hb", FunctionType(BOOL, [BOOL, REAL, REAL]))

        hr_0_1 = Function(hr, (TRUE(), Real(0), Real(1)))
        hb_0_1 = Function(hb, (TRUE(), Real(0), Real(1)))
        hbx = Function(hb, (Symbol("x"), Real(0), Real(1)))
        f = GT(hr_0_1, Real(0))
        g = hb_0_1

        for sname in get_env().factory.all_solvers(logic=QF_UFLIRA):
            with Solver(name=sname) as solver:
                # First hr
                solver.add_assertion(f)
                res = solver.solve()
                self.assertTrue(res)
                v = solver.get_value(hr_0_1)
                self.assertIsNotNone(solver.get_value(v))
                # Now hb
                solver.add_assertion(g)
                res = solver.solve()
                self.assertTrue(res)
                v = solver.get_value(hb_0_1)
                self.assertIsNotNone(v in [TRUE(), FALSE()])
                # Hbx
                solver.add_assertion(hbx)
                res = solver.solve()
                self.assertTrue(res)
                v = solver.get_value(hbx)
                self.assertIsNotNone(v in [TRUE(), FALSE()])
                # Get model
                model = solver.get_model()
                self.assertIsNotNone(model)
예제 #2
0
    def get_ordered_zero(self):
        self.zero = None
        self.firste = None
        self.le = None
        res = []
        for g in self.curr._globals:
            gstr = pretty_print_str(g)
            if gstr == "zero":
                gt = g.symbol_type()
                assert (gt in self._enumsorts)
                domain = self._enumsorts[gt]
                eq = EqualsOrIff(g, domain[0])
                res.append(eq)
                self.zero = (domain[0], g)
            elif gstr == "firste":
                gt = g.symbol_type()
                assert (gt in self._enumsorts)
                domain = self._enumsorts[gt]
                eq = EqualsOrIff(g, domain[1])
                res.append(eq)
                self.firste = (domain[1], g)

        for pre in self.curr._le:
            pret = pre.symbol_type()
            orderedt = pret.param_types[0]
            if orderedt in self._enumsorts:
                self._ordered_sorts[orderedt] = pre
                self.le = pre
                domain = self._enumsorts[orderedt]
                #                 end = 2
                end = len(domain)
                for i in range(end):
                    for j in range(i, end):
                        arg1 = domain[i]
                        arg2 = domain[j]
                        rel1 = Function(pre, [arg1, arg2])
                        rel2 = Function(pre, [arg2, arg1])
                        if (i == j):
                            res.append(rel1)
                        elif (i < j):
                            res.append(rel1)
                            res.append(Not(rel2))
                        elif (i > j):
                            res.append(Not(rel1))
                            res.append(rel2)
                        else:
                            assert (0)

        if len(res) == 0:
            return TRUE()
        else:
            print("\nZero axiom: ")
            for v in res:
                print("\t", end="")
                pretty_print(v)
            return And(res)
예제 #3
0
파일: case_splits.py 프로젝트: eytans/TheSy
 def __init__(self, doc: SmtLibDocument):
     self.doc = doc
     sym = get_env().formula_manager.get_symbol
     #fsym = lambda ty: FreshSymbol(ty, template='?c%d')
     nat = Type('Nat')
     skf1 = FreshSymbol(FunctionType(nat, [nat]), template='skf%d')
     self.ctor_pats = {
         nat:
         lambda ph:
         [sym('zero'),
          Function(sym('succ'), [Function(skf1, [ph])])]
     }
예제 #4
0
    def get_ordered_le(self):
        self.reset_ordered_sort()
        for g in self.curr._globals:
            gstr = pretty_print_str(g)
            print(gstr)
            if gstr == "zero":
                gt = g.symbol_type()
                if gt in self._enumsorts:
                    self._ordered_min[gt] = g
            elif gstr == "max":
                gt = g.symbol_type()
                if gt in self._enumsorts:
                    self._ordered_max[gt] = g
        res = []
        for pre in self.curr._le:
            pret = pre.symbol_type()
            orderedt = pret.param_types[0]
            if orderedt in self._enumsorts:
                self._ordered_sorts[orderedt] = pre
                domain = self._enumsorts[orderedt]
                for i in range(len(domain)):
                    for j in range(i, len(domain)):
                        arg1 = domain[i]
                        arg2 = domain[j]
                        rel1 = Function(pre, [arg1, arg2])
                        rel2 = Function(pre, [arg2, arg1])
                        if (i == j):
                            res.append(rel1)
                        elif (i < j):
                            res.append(rel1)
                            res.append(Not(rel2))
                        elif (i > j):
                            res.append(Not(rel1))
                            res.append(rel2)
                        else:
                            assert (0)

#                 for i in range(len(domain)-1, 0, -1):
#                     arg1 = domain[i]
#                     arg2 = domain[i-1]
#                     rel = Function(pre, [arg1, arg2])
#                     res.append(Not(rel))

        if len(res) == 0:
            return TRUE()
        else:
            print("\nOrdering axiom: ")
            for v in res:
                print("\t", end="")
                pretty_print(v)
            return And(res)
예제 #5
0
    def test_substitution_on_functions(self):
        i, r = FreshSymbol(INT), FreshSymbol(REAL)
        f = Symbol("f", FunctionType(BOOL, [INT, REAL]))

        phi = Function(f, [Plus(i, Int(1)), Minus(r, Real(2))])

        phi_sub = substitute(phi, {i: Int(0)}).simplify()
        self.assertEqual(phi_sub, Function(f, [Int(1), Minus(r, Real(2))]))

        phi_sub = substitute(phi, {r: Real(0)}).simplify()
        self.assertEqual(phi_sub, Function(f, [Plus(i, Int(1)), Real(-2)]))

        phi_sub = substitute(phi, {r: Real(0), i: Int(0)}).simplify()
        self.assertEqual(phi_sub, Function(f, [Int(1), Real(-2)]))
예제 #6
0
    def test_function(self):
        f1_type = FunctionType(REAL, [REAL, REAL])
        f2_type = FunctionType(REAL, [])

        p, q = Symbol("p", REAL), Symbol("q", REAL)
        f1_symbol = Symbol("f1", f1_type)
        f2_symbol = Symbol("f2", f2_type)

        f1 = Function(f1_symbol, [p, q])
        f2 = Function(f2_symbol, [])

        f1_string = self.print_to_string(f1)
        f2_string = self.print_to_string(f2)

        self.assertEqual(f1_string, "(f1 p q)")
        self.assertEqual(f2_string, "(f2)")
예제 #7
0
    def test_normalization(self):
        from pysmt.environment import Environment

        env2 = Environment()
        mgr2 = env2.formula_manager

        ty = ArrayType(BOOL, REAL)
        x = FreshSymbol(ty)
        fty = FunctionType(BOOL, (ty, ))
        f = FreshSymbol(fty)
        g = Function(f, (x, ))
        self.assertIsNotNone(g)
        self.assertNotIn(g, mgr2)
        g2 = mgr2.normalize(g)
        self.assertIn(g2, mgr2)
        # Since the types are from two different environments, they
        # should be different.
        x2 = g2.arg(0)
        ty2 = x2.symbol_type()
        self.assertFalse(ty2 is ty, ty)
        fname = g2.function_name()
        fty2 = fname.symbol_type()
        self.assertFalse(fty2 is fty, fty)

        # Test ArrayValue
        h = Array(BVType(4), BV(0, 4))
        h2 = mgr2.normalize(h)
        self.assertEqual(h.array_value_index_type(),
                         h2.array_value_index_type())
        self.assertFalse(h.array_value_index_type() is \
                         h2.array_value_index_type())
예제 #8
0
    def export_rule_def(self, defun):
        name, args, rettype, body = defun
        ftype = FunctionType(rettype, [a.symbol_type() for a in args])
        fsymb = Symbol(name, ftype)
        yield self.export_func(fsymb)

        eqop = Iff if rettype.is_bool_type() else Equals

        for rule in self.export_rules(
                ForAll(args, eqop(Function(fsymb, args), body))):
            yield rule
예제 #9
0
    def action_noop(self):
        f = dict()
        for s in self._states:
            if s in self._pre2nex:
                n = self._pre2nex[s]

                s_type = s.symbol_type()

                args = []
                if s_type.is_function_type():
                    i = 0
                    for paramt in s_type.param_types:
                        i += 1
                        paramt_name = str(i) + ":" + str(paramt)
                        args.append(Symbol(paramt_name, paramt))
                lhs = Function(s, args)
                rhs = Function(n, args)
                eq = ForAll(args, EqualsOrIff(lhs, rhs))
                f[n] = eq
        return f
예제 #10
0
 def test_custom_types(self):
     A = Type("A", 0)
     a, b = Symbol("a", A), Symbol("b", A)
     p = Symbol("p", INT)
     fun = Symbol("g", FunctionType(A, [INT, A]))
     app = Function(fun, [p, b])
     f_a = Equals(a, app)
     for n in self.all_solvers:
         with Solver(name=n, logic=QF_UFLIA) as s:
             s.add_assertion(f_a)
             res = s.solve()
             self.assertTrue(res)
예제 #11
0
 def add_pred(self, formula, name):
     args = []
     types = []
     if formula.is_quantifier():
         for a in formula.quantifier_vars():
             args.append(a)
             types.append(a.symbol_type())
         formula = formula.arg(0)
     ft = FunctionType(BOOL, tuple(types))
     f = Symbol(name, ft)
     lhs = Function(f, args)
     self._predicates[lhs] = formula
예제 #12
0
    def test_get_value_of_function(self):
        """get_value on a function should raise an exception."""
        h = Symbol("h", FunctionType(REAL, [REAL, REAL]))

        h_0_0 = Function(h, (Real(0), Real(1)))
        f = GT(h_0_0, Real(0))
        for sname in get_env().factory.all_solvers(logic=QF_UFLIRA):
            with Solver(name=sname) as solver:
                solver.add_assertion(f)
                res = solver.solve()
                self.assertTrue(res)
                with self.assertRaises(PysmtTypeError):
                    solver.get_value(h)
                self.assertIsNotNone(solver.get_value(h_0_0))
예제 #13
0
    def test_function(self):
        f1_type = FunctionType(REAL, [REAL, REAL])
        f2_type = FunctionType(REAL, [])

        p, q = Symbol("p", REAL), Symbol("q", REAL)
        f1_symbol = Symbol("f1", f1_type)
        f2_symbol = Symbol("f2", f2_type)

        f1 = Function(f1_symbol, [p, q])
        f2 = Function(f2_symbol, [])

        self.assertEqual(f1.to_smtlib(daggify=False), "(f1 p q)")
        self.assertEqual(f2.to_smtlib(daggify=False), "f2")

        self.assertEqual(f1.to_smtlib(daggify=True),
                         "(let ((.def_0 (f1 p q))) .def_0)")
        self.assertEqual(f2.to_smtlib(daggify=True), "f2")
예제 #14
0
    def test_functions(self):
        vi = Symbol("At", INT)
        vr = Symbol("Bt", REAL)

        f = Symbol("f", FunctionType(INT, [REAL]))
        g = Symbol("g", FunctionType(REAL, [INT]))

        tc = get_env().stc

        self.assertEqual(tc.walk(Function(f, [vr])), INT)
        self.assertEqual(tc.walk(Function(g, [vi])), REAL)
        self.assertEqual(tc.walk(Function(f, [Function(g, [vi])])), INT)
        self.assertEqual(tc.walk(LE(Plus(vi, Function(f, [Real(4)])), Int(8))), BOOL)
        self.assertEqual(tc.walk(LE(Plus(vr, Function(g, [Int(4)])), Real(8))), BOOL)

        with self.assertRaises(PysmtTypeError):
            LE(Plus(vr, Function(g, [Real(4)])), Real(8))

        with self.assertRaises(PysmtTypeError):
            LE(Plus(vi, Function(f, [Int(4)])), Int(8))
예제 #15
0
    def test_function(self):
        f1_type = FunctionType(REAL, [REAL, REAL])
        f2_type = FunctionType(REAL, [])

        p,q = Symbol("p", REAL), Symbol("q", REAL)
        f1_symbol = Symbol("f1", f1_type)
        f2_symbol = Symbol("f2", f2_type)

        f1 = Function(f1_symbol, [p,q])
        f2 = Function(f2_symbol, [])

        self.assertEqual(f1.to_smtlib(daggify=False), "(f1 p q)")
        self.assertEqual(f2.to_smtlib(daggify=False), "f2")

        self.assertEqual(f1.to_smtlib(daggify=True), "(let ((.def_0 (f1 p q))) .def_0)")
        self.assertEqual(f2.to_smtlib(daggify=True), "f2")
예제 #16
0
파일: case_splits.py 프로젝트: eytans/TheSy
    def _generalize_pattern(self, term):
        head, args = term.function_name(), term.args()
        pat_idxs = [
            i for i, a in enumerate(args) if a.is_function_application()
        ]
        if len(pat_idxs) > 1:
            args1pat = list(args)
            phs = []
            for i in pat_idxs[1:]:
                ph = FreshSymbol(get_type(args[i]), template='?splt%d')
                phs.append(ph)
                args1pat[i] = ph

            headpat = SmtLibSExpression(Function(head, args1pat))
            #print("=|>", pat_idxs, headpat, phs)

            for ph in phs:
                cases = list(self._get_cases(ph))
                if len(cases) > 1:
                    yield [
                        headpat,
                        SExpression(['potential_split', ph] + cases)
                    ]
예제 #17
0
    def get_quorum_axiom(self):
        res = []
        pre = None
        for g in self.curr._globals:
            name = str(g)
            if name.startswith("member:"):
                pre = g
                break
        if pre != None:
            pret = pre.symbol_type()
            assert (len(pret.param_types) == 2)
            tA = pret.param_types[0]
            tQ = pret.param_types[1]

            if tA in self._enumsorts and tQ in self._enumsorts:
                domainA = self._enumsorts[tA]
                domainQ = self._enumsorts[tQ]
                assert (str(tQ).startswith("quorum:"))

                qMap = {}

                if (len(domainA) == 3) and (len(domainQ) == 3):
                    qMap[0] = set([0, 1])
                    qMap[1] = set([0, 2])
                    qMap[2] = set([1, 2])
                elif (len(domainA) == 4) and (len(domainQ) == 4):
                    qMap[0] = set([0, 1, 2])
                    qMap[1] = set([0, 1, 3])
                    qMap[2] = set([0, 2, 3])
                    qMap[3] = set([1, 2, 3])
                elif (len(domainA) == 5) and (len(domainQ) == 10):
                    qMap[0] = set([0, 1, 2])
                    qMap[1] = set([0, 1, 3])
                    qMap[2] = set([0, 1, 4])
                    qMap[3] = set([0, 2, 3])
                    qMap[4] = set([0, 2, 4])
                    qMap[5] = set([0, 3, 4])
                    qMap[6] = set([1, 2, 3])
                    qMap[7] = set([1, 2, 4])
                    qMap[8] = set([1, 3, 4])
                    qMap[9] = set([2, 3, 4])
                else:
                    assert (0)

                for k, v in qMap.items():
                    arg2 = domainQ[k]
                    for j in range(len(domainA)):
                        arg1 = domainA[j]
                        rel = Function(pre, [arg1, arg2])
                        val = j in v
                        if val:
                            res.append(rel)
                        else:
                            res.append(Not(rel))
        if len(res) == 0:
            return TRUE()
        else:
            print("\nQuorum axiom: ")
            for v in res:
                print("\t", end="")
                pretty_print(v)
            return And(res)
예제 #18
0
파일: examples.py 프로젝트: zenbhang/pysmt
def get_full_example_formulae(environment=None):
    """Return a list of Examples using the given environment."""

    if environment is None:
        environment = get_env()

    with environment:
        x = Symbol("x", BOOL)
        y = Symbol("y", BOOL)
        p = Symbol("p", INT)
        q = Symbol("q", INT)
        r = Symbol("r", REAL)
        s = Symbol("s", REAL)
        aii = Symbol("aii", ARRAY_INT_INT)
        ari = Symbol("ari", ArrayType(REAL, INT))
        arb = Symbol("arb", ArrayType(REAL, BV8))
        abb = Symbol("abb", ArrayType(BV8, BV8))
        nested_a = Symbol("a_arb_aii",
                          ArrayType(ArrayType(REAL, BV8), ARRAY_INT_INT))

        rf = Symbol("rf", FunctionType(REAL, [REAL, REAL]))
        rg = Symbol("rg", FunctionType(REAL, [REAL]))

        ih = Symbol("ih", FunctionType(INT, [REAL, INT]))
        ig = Symbol("ig", FunctionType(INT, [INT]))

        bf = Symbol("bf", FunctionType(BOOL, [BOOL]))
        bg = Symbol("bg", FunctionType(BOOL, [BOOL]))

        bv8 = Symbol("bv1", BV8)
        bv16 = Symbol("bv2", BV16)

        result = [
            # Formula, is_valid, is_sat, is_qf
            Example(hr="(x & y)",
                    expr=And(x, y),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_BOOL),
            Example(hr="(x <-> y)",
                    expr=Iff(x, y),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_BOOL),
            Example(hr="((x | y) & (! (x | y)))",
                    expr=And(Or(x, y), Not(Or(x, y))),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.QF_BOOL),
            Example(hr="(x & (! y))",
                    expr=And(x, Not(y)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_BOOL),
            Example(hr="(False -> True)",
                    expr=Implies(FALSE(), TRUE()),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BOOL),

            #
            #  LIA
            #
            Example(hr="((q < p) & (x -> y))",
                    expr=And(GT(p, q), Implies(x, y)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_IDL),
            Example(hr="(((p + q) = 5) & (q < p))",
                    expr=And(Equals(Plus(p, q), Int(5)), GT(p, q)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_LIA),
            Example(hr="((q <= p) | (p <= q))",
                    expr=Or(GE(p, q), LE(p, q)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_IDL),
            Example(hr="(! (p < (q * 2)))",
                    expr=Not(LT(p, Times(q, Int(2)))),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_LIA),
            Example(hr="(p < (p - (5 - 2)))",
                    expr=GT(Minus(p, Minus(Int(5), Int(2))), p),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.QF_IDL),
            Example(hr="((x ? 7 : ((p + -1) * 3)) = q)",
                    expr=Equals(
                        Ite(x, Int(7), Times(Plus(p, Int(-1)), Int(3))), q),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_LIA),
            Example(hr="(p < (q + 1))",
                    expr=LT(p, Plus(q, Int(1))),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_LIA),

            #
            # LRA
            #
            Example(hr="((s < r) & (x -> y))",
                    expr=And(GT(r, s), Implies(x, y)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_RDL),
            Example(hr="(((r + s) = 28/5) & (s < r))",
                    expr=And(Equals(Plus(r, s), Real(Fraction("5.6"))),
                             GT(r, s)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_LRA),
            Example(hr="((s <= r) | (r <= s))",
                    expr=Or(GE(r, s), LE(r, s)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_RDL),
            Example(hr="(! ((r * 2.0) < (s * 2.0)))",
                    expr=Not(LT(Div(r, Real((1, 2))), Times(s, Real(2)))),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_LRA),
            Example(hr="(! (r < (r - (5.0 - 2.0))))",
                    expr=Not(GT(Minus(r, Minus(Real(5), Real(2))), r)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_RDL),
            Example(hr="((x ? 7.0 : ((s + -1.0) * 3.0)) = r)",
                    expr=Equals(
                        Ite(x, Real(7), Times(Plus(s, Real(-1)), Real(3))), r),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_LRA),

            #
            # EUF
            #
            Example(hr="(bf(x) <-> bg(x))",
                    expr=Iff(Function(bf, (x, )), Function(bg, (x, ))),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_UF),
            Example(hr="(rf(5.0, rg(r)) = 0.0)",
                    expr=Equals(Function(rf, (Real(5), Function(rg, (r, )))),
                                Real(0)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_UFLRA),
            Example(hr="((rg(r) = (5.0 + 2.0)) <-> (rg(r) = 7.0))",
                    expr=Iff(Equals(Function(rg, [r]), Plus(Real(5), Real(2))),
                             Equals(Function(rg, [r]), Real(7))),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_UFLRA),
            Example(
                hr="((r = (s + 1.0)) & (rg(s) = 5.0) & (rg((r - 1.0)) = 7.0))",
                expr=And([
                    Equals(r, Plus(s, Real(1))),
                    Equals(Function(rg, [s]), Real(5)),
                    Equals(Function(rg, [Minus(r, Real(1))]), Real(7))
                ]),
                is_valid=False,
                is_sat=False,
                logic=pysmt.logics.QF_UFLRA),

            #
            # BV
            #
            Example(hr="((1_32 & 0_32) = 0_32)",
                    expr=Equals(BVAnd(BVOne(32), BVZero(32)), BVZero(32)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),
            Example(hr="((! 2_3) = 5_3)",
                    expr=Equals(BVNot(BV("010")), BV("101")),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),
            Example(hr="((7_3 xor 0_3) = 0_3)",
                    expr=Equals(BVXor(BV("111"), BV("000")), BV("000")),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.QF_BV),
            Example(hr="((bv1::bv1) u< 0_16)",
                    expr=BVULT(BVConcat(bv8, bv8), BVZero(16)),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.QF_BV),
            Example(hr="(1_32[0:7] = 1_8)",
                    expr=Equals(BVExtract(BVOne(32), end=7), BVOne(8)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),
            Example(hr="(0_8 u< (((bv1 + 1_8) * 5_8) u/ 5_8))",
                    expr=BVUGT(
                        BVUDiv(BVMul(BVAdd(bv8, BVOne(8)), BV(5, width=8)),
                               BV(5, width=8)), BVZero(8)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),
            Example(hr="(0_16 u<= bv2)",
                    expr=BVUGE(bv16, BVZero(16)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),
            Example(hr="(0_16 s<= bv2)",
                    expr=BVSGE(bv16, BVZero(16)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),
            Example(
                hr="((0_32 u< (5_32 u% 2_32)) & ((5_32 u% 2_32) u<= 1_32))",
                expr=And(
                    BVUGT(BVURem(BV(5, width=32), BV(2, width=32)),
                          BVZero(32)),
                    BVULE(BVURem(BV(5, width=32), BV(2, width=32)),
                          BVOne(32))),
                is_valid=True,
                is_sat=True,
                logic=pysmt.logics.QF_BV),
            Example(hr="((((1_32 + (- 1_32)) << 1_32) >> 1_32) = 1_32)",
                    expr=Equals(
                        BVLShr(BVLShl(BVAdd(BVOne(32), BVNeg(BVOne(32))), 1),
                               1), BVOne(32)),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.QF_BV),
            Example(hr="((1_32 - 1_32) = 0_32)",
                    expr=Equals(BVSub(BVOne(32), BVOne(32)), BVZero(32)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            # Rotations
            Example(hr="(((1_32 ROL 1) ROR 1) = 1_32)",
                    expr=Equals(BVRor(BVRol(BVOne(32), 1), 1), BVOne(32)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            # Extensions
            Example(hr="((0_5 ZEXT 11) = (0_1 SEXT 15))",
                    expr=Equals(BVZExt(BVZero(5), 11), BVSExt(BVZero(1), 15)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),
            Example(hr="((bv2 - bv2) = 0_16)",
                    expr=Equals(BVSub(bv16, bv16), BVZero(16)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),
            Example(hr="((bv2 - bv2)[0:7] = bv1)",
                    expr=Equals(BVExtract(BVSub(bv16, bv16), 0, 7), bv8),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),
            Example(hr="((bv2[0:7] bvcomp bv1) = 1_1)",
                    expr=Equals(BVComp(BVExtract(bv16, 0, 7), bv8), BVOne(1)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),
            Example(hr="((bv2 bvcomp bv2) = 0_1)",
                    expr=Equals(BVComp(bv16, bv16), BVZero(1)),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.QF_BV),
            Example(hr="(bv2 s< bv2)",
                    expr=BVSLT(bv16, bv16),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.QF_BV),
            Example(hr="(bv2 s< 0_16)",
                    expr=BVSLT(bv16, BVZero(16)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),
            Example(hr="((bv2 s< 0_16) | (0_16 s<= bv2))",
                    expr=Or(BVSGT(BVZero(16), bv16), BVSGE(bv16, BVZero(16))),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),
            Example(hr="(bv2 u< bv2)",
                    expr=BVULT(bv16, bv16),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.QF_BV),
            Example(hr="(bv2 u< 0_16)",
                    expr=BVULT(bv16, BVZero(16)),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.QF_BV),
            Example(hr="((bv2 | 0_16) = bv2)",
                    expr=Equals(BVOr(bv16, BVZero(16)), bv16),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),
            Example(hr="((bv2 & 0_16) = 0_16)",
                    expr=Equals(BVAnd(bv16, BVZero(16)), BVZero(16)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),
            Example(hr="((0_16 s< bv2) & ((bv2 s/ 65535_16) s< 0_16))",
                    expr=And(BVSLT(BVZero(16), bv16),
                             BVSLT(BVSDiv(bv16, SBV(-1, 16)), BVZero(16))),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),
            Example(hr="((0_16 s< bv2) & ((bv2 s% 1_16) s< 0_16))",
                    expr=And(BVSLT(BVZero(16), bv16),
                             BVSLT(BVSRem(bv16, BVOne(16)), BVZero(16))),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.QF_BV),
            Example(hr="((bv2 u% 1_16) = 0_16)",
                    expr=Equals(BVURem(bv16, BVOne(16)), BVZero(16)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),
            Example(hr="((bv2 s% 1_16) = 0_16)",
                    expr=Equals(BVSRem(bv16, BVOne(16)), BVZero(16)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),
            Example(hr="((bv2 s% (- 1_16)) = 0_16)",
                    expr=Equals(BVSRem(bv16, BVNeg(BVOne(16))), BVZero(16)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),
            Example(hr="((bv2 a>> 0_16) = bv2)",
                    expr=Equals(BVAShr(bv16, BVZero(16)), bv16),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),
            Example(hr="((0_16 s<= bv2) & ((bv2 a>> 1_16) = (bv2 >> 1_16)))",
                    expr=And(
                        BVSLE(BVZero(16), bv16),
                        Equals(BVAShr(bv16, BVOne(16)),
                               BVLShr(bv16, BVOne(16)))),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),
            #
            # Quantification
            #
            Example(hr="(forall y . (x -> y))",
                    expr=ForAll([y], Implies(x, y)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.BOOL),
            Example(hr="(forall p, q . ((p + q) = 0))",
                    expr=ForAll([p, q], Equals(Plus(p, q), Int(0))),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.LIA),
            Example(
                hr="(forall r, s . (((0.0 < r) & (0.0 < s)) -> ((r - s) < r)))",
                expr=ForAll([r, s],
                            Implies(And(GT(r, Real(0)), GT(s, Real(0))),
                                    (LT(Minus(r, s), r)))),
                is_valid=True,
                is_sat=True,
                logic=pysmt.logics.LRA),
            Example(hr="(exists x, y . (x -> y))",
                    expr=Exists([x, y], Implies(x, y)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.BOOL),
            Example(hr="(exists p, q . ((p + q) = 0))",
                    expr=Exists([p, q], Equals(Plus(p, q), Int(0))),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.LIA),
            Example(hr="(exists r . (forall s . (r < (r - s))))",
                    expr=Exists([r], ForAll([s], GT(Minus(r, s), r))),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.LRA),
            Example(hr="(forall r . (exists s . (r < (r - s))))",
                    expr=ForAll([r], Exists([s], GT(Minus(r, s), r))),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.LRA),
            Example(hr="(x & (forall r . ((r + s) = 5.0)))",
                    expr=And(x, ForAll([r], Equals(Plus(r, s), Real(5)))),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.LRA),
            Example(hr="(exists x . ((x <-> (5.0 < s)) & (s < 3.0)))",
                    expr=Exists([x],
                                (And(Iff(x, GT(s, Real(5))), LT(s, Real(3))))),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.LRA),

            #
            # UFLIRA
            #
            Example(hr="((p < ih(r, q)) & (x -> y))",
                    expr=And(GT(Function(ih, (r, q)), p), Implies(x, y)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_UFLIRA),
            Example(
                hr=
                "(((p - 3) = q) -> ((p < ih(r, (q + 3))) | (ih(r, p) <= p)))",
                expr=Implies(
                    Equals(Minus(p, Int(3)), q),
                    Or(GT(Function(ih, (r, Plus(q, Int(3)))), p),
                       LE(Function(ih, (r, p)), p))),
                is_valid=True,
                is_sat=True,
                logic=pysmt.logics.QF_UFLIRA),
            Example(
                hr=
                "(((ToReal((p - 3)) = r) & (ToReal(q) = r)) -> ((p < ih(ToReal((p - 3)), (q + 3))) | (ih(r, p) <= p)))",
                expr=Implies(
                    And(Equals(ToReal(Minus(p, Int(3))), r),
                        Equals(ToReal(q), r)),
                    Or(
                        GT(
                            Function(
                                ih,
                                (ToReal(Minus(p, Int(3))), Plus(q, Int(3)))),
                            p), LE(Function(ih, (r, p)), p))),
                is_valid=True,
                is_sat=True,
                logic=pysmt.logics.QF_UFLIRA),
            Example(
                hr=
                "(! (((ToReal((p - 3)) = r) & (ToReal(q) = r)) -> ((p < ih(ToReal((p - 3)), (q + 3))) | (ih(r, p) <= p))))",
                expr=Not(
                    Implies(
                        And(Equals(ToReal(Minus(p, Int(3))), r),
                            Equals(ToReal(q), r)),
                        Or(
                            GT(
                                Function(ih, (ToReal(Minus(
                                    p, Int(3))), Plus(q, Int(3)))), p),
                            LE(Function(ih, (r, p)), p)))),
                is_valid=False,
                is_sat=False,
                logic=pysmt.logics.QF_UFLIRA),
            Example(
                hr=
                """("Did you know that any string works? #yolo" & "10" & "|#somesolverskeepthe||" & " ")""",
                expr=And(Symbol("Did you know that any string works? #yolo"),
                         Symbol("10"), Symbol("|#somesolverskeepthe||"),
                         Symbol(" ")),
                is_valid=False,
                is_sat=True,
                logic=pysmt.logics.QF_BOOL),

            #
            # Arrays
            #
            Example(hr="((q = 0) -> (aii[0 := 0] = aii[0 := q]))",
                    expr=Implies(
                        Equals(q, Int(0)),
                        Equals(Store(aii, Int(0), Int(0)),
                               Store(aii, Int(0), q))),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_ALIA),
            Example(hr="(aii[0 := 0][0] = 0)",
                    expr=Equals(Select(Store(aii, Int(0), Int(0)), Int(0)),
                                Int(0)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_ALIA),
            Example(hr="((Array{Int, Int}(0)[1 := 1] = aii) & (aii[1] = 0))",
                    expr=And(Equals(Array(INT, Int(0), {Int(1): Int(1)}), aii),
                             Equals(Select(aii, Int(1)), Int(0))),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.get_logic_by_name("QF_ALIA*")),
            Example(hr="((Array{Int, Int}(0)[1 := 3] = aii) & (aii[1] = 3))",
                    expr=And(Equals(Array(INT, Int(0), {Int(1): Int(3)}), aii),
                             Equals(Select(aii, Int(1)), Int(3))),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.get_logic_by_name("QF_ALIA*")),
            Example(hr="((Array{Real, Int}(10) = ari) & (ari[6/5] = 0))",
                    expr=And(Equals(Array(REAL, Int(10)), ari),
                             Equals(Select(ari, Real((6, 5))), Int(0))),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.get_logic_by_name("QF_AUFBVLIRA*")),
            Example(
                hr=
                "((Array{Real, Int}(0)[1.0 := 10][2.0 := 20][3.0 := 30][4.0 := 40] = ari) & (! ((ari[0.0] = 0) & (ari[1.0] = 10) & (ari[2.0] = 20) & (ari[3.0] = 30) & (ari[4.0] = 40))))",
                expr=And(
                    Equals(
                        Array(
                            REAL, Int(0), {
                                Real(1): Int(10),
                                Real(2): Int(20),
                                Real(3): Int(30),
                                Real(4): Int(40)
                            }), ari),
                    Not(
                        And(Equals(Select(ari, Real(0)), Int(0)),
                            Equals(Select(ari, Real(1)), Int(10)),
                            Equals(Select(ari, Real(2)), Int(20)),
                            Equals(Select(ari, Real(3)), Int(30)),
                            Equals(Select(ari, Real(4)), Int(40))))),
                is_valid=False,
                is_sat=False,
                logic=pysmt.logics.get_logic_by_name("QF_AUFBVLIRA*")),
            Example(
                hr=
                "((Array{Real, Int}(0)[1.0 := 10][2.0 := 20][3.0 := 30][4.0 := 40][5.0 := 50] = ari) & (! ((ari[0.0] = 0) & (ari[1.0] = 10) & (ari[2.0] = 20) & (ari[3.0] = 30) & (ari[4.0] = 40) & (ari[5.0] = 50))))",
                expr=And(
                    Equals(
                        Array(
                            REAL, Int(0), {
                                Real(1): Int(10),
                                Real(2): Int(20),
                                Real(3): Int(30),
                                Real(4): Int(40),
                                Real(5): Int(50)
                            }), ari),
                    Not(
                        And(Equals(Select(ari, Real(0)), Int(0)),
                            Equals(Select(ari, Real(1)), Int(10)),
                            Equals(Select(ari, Real(2)), Int(20)),
                            Equals(Select(ari, Real(3)), Int(30)),
                            Equals(Select(ari, Real(4)), Int(40)),
                            Equals(Select(ari, Real(5)), Int(50))))),
                is_valid=False,
                is_sat=False,
                logic=pysmt.logics.get_logic_by_name("QF_AUFBVLIRA*")),
            Example(
                hr=
                "((a_arb_aii = Array{Array{Real, BV{8}}, Array{Int, Int}}(Array{Int, Int}(7))) -> (a_arb_aii[arb][42] = 7))",
                expr=Implies(
                    Equals(nested_a,
                           Array(ArrayType(REAL, BV8), Array(INT, Int(7)))),
                    Equals(Select(Select(nested_a, arb), Int(42)), Int(7))),
                is_valid=True,
                is_sat=True,
                logic=pysmt.logics.get_logic_by_name("QF_AUFBVLIRA*")),
            Example(hr="(abb[bv1 := y_][bv1 := z_] = abb[bv1 := z_])",
                    expr=Equals(
                        Store(Store(abb, bv8, Symbol("y_", BV8)), bv8,
                              Symbol("z_", BV8)),
                        Store(abb, bv8, Symbol("z_", BV8))),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_ABV),
            Example(hr="((r / s) = (r * s))",
                    expr=Equals(Div(r, s), Times(r, s)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_NRA),
            Example(hr="(2.0 = (r * r))",
                    expr=Equals(Real(2), Times(r, r)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_NRA),
            Example(hr="((p ^ 2) = 0)",
                    expr=Equals(Pow(p, Int(2)), Int(0)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_NIA),
            Example(hr="((r ^ 2.0) = 0.0)",
                    expr=Equals(Pow(r, Real(2)), Real(0)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_NRA),
            Example(hr="((r * r * r) = 25.0)",
                    expr=Equals(Times(r, r, r), Real(25)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_NRA),
            Example(hr="((5.0 * r * 5.0) = 25.0)",
                    expr=Equals(Times(Real(5), r, Real(5)), Real(25)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_LRA),
            Example(hr="((p * p * p) = 25)",
                    expr=Equals(Times(p, p, p), Int(25)),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.QF_NIA),
            Example(hr="((5 * p * 5) = 25)",
                    expr=Equals(Times(Int(5), p, Int(5)), Int(25)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_LIA),
            Example(hr="(((1 - 1) * p * 1) = 0)",
                    expr=Equals(Times(Minus(Int(1), Int(1)), p, Int(1)),
                                Int(0)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_LIA),

            # Huge Fractions:
            Example(
                hr=
                "((r * 1606938044258990275541962092341162602522202993782792835301376/7) = -20480000000000000000000000.0)",
                expr=Equals(Times(r, Real(Fraction(2**200, 7))),
                            Real(-200**11)),
                is_valid=False,
                is_sat=True,
                logic=pysmt.logics.QF_LRA),
            Example(hr="(((r + 5.0 + s) * (s + 2.0 + r)) = 0.0)",
                    expr=Equals(
                        Times(Plus(r, Real(5), s), Plus(s, Real(2), r)),
                        Real(0)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_NRA),
            Example(
                hr=
                "(((p + 5 + q) * (p - (q - 5))) = ((p * p) + (10 * p) + 25 + (-1 * q * q)))",
                expr=Equals(
                    Times(Plus(p, Int(5), q), Minus(p, Minus(q, Int(5)))),
                    Plus(Times(p, p), Times(Int(10), p), Int(25),
                         Times(Int(-1), q, q))),
                is_valid=True,
                is_sat=True,
                logic=pysmt.logics.QF_NIA),
        ]
    return result
예제 #19
0
파일: problem.py 프로젝트: aman-goel/ic3po
def symmetry_cube(self, cube, fIdx, reducePremise, dest=None):
#     cube_str = pretty_serialize(Not(cube), mode=0)
    cube_str = pretty_serialize(Not(cube), mode=0)
    print("(clause)")
    print("\t%s" % cube_str)
    cvars = cube.get_enum_constants()
    crels = cube.get_free_variables()
    print("(relations)")
    for r in crels:
        print("\t%s" % pretty_serialize(r))
#     assert(0)
        
    subs = dict()
#         print(cvars)

    enum2qvar = dict()
#     for v in cvars:
    for v in sorted(cvars, key=str):
        enumsort = v.constant_type()
        if self.ordered == "partial" and enumsort in self.system._ordered_sorts:
            continue
        if self.ordered == "zero" and str(enumsort).startswith("epoch"):
#             continue
            assert(enumsort in self.system._enumsorts)
            domain = self.system._enumsorts[enumsort]
            zero = self.system.zero[1]
            firste = self.system.firste[1]
            le = self.system.le
            if v == domain[0]:
                subs[v] = zero
                continue
            elif v == domain[1]:
                subs[v] = firste
                continue
            else:
                rhs = firste
                end = 2
                try:
                    end = domain.index(v)
                except ValueError:
                    assert(0)
                for idx in range(end-1, 1, -2):
                    v2 = domain[idx]
                    if v2 in cvars:
                        rhs = v2
                        break
                gt = Not(Function(le, [v, rhs]))
                print("\tadding condition: %s" % pretty_print_str(gt))
                cube = And(cube, gt)
#                 neq = And(Not(EqualsOrIff(v, zero)), Not(EqualsOrIff(v, firste)))
#                 cube = And(cube, neq)
#         if (self.quorums != "none" and 
#             (str(enumsort).startswith("node") or str(enumsort).startswith("acceptor"))):
#             continue
        if (self.quorums != "none" and 
            (str(enumsort).startswith("quorum"))):
            continue
        if enumsort in self.system._enum2qvar:
            qvar = []
            if enumsort in enum2qvar:
                qvar = enum2qvar[enumsort]
            idx = len(qvar)
            qv = self.system._enum2qvar[enumsort][idx]
            qvar.append(qv)
            enum2qvar[enumsort] = qvar
            subs[v] = qv
            
    antecedent = dict()
    qvars = set()
    fullsorts = []
#         print(enum2qvar)
#         print(self.system._enum2qvar)
    minSz = 3
    if experimentGeneralize:
        minSz = 2
    for enumsort, qvar in enum2qvar.items():
        if  (
#                  False and
#                 (str(enumsort).startswith("acceptor") 
#                  or str(enumsort).startswith("node")
#                 or str(enumsort).startswith("quorum")
#                 ) and 
#                 (str(enumsort).startswith("quorum")) and 
            (len(qvar) >= minSz) and 
            (len(qvar) == len(self.system._enum2qvar[enumsort]))):
            fullsorts.append([enumsort, qvar])
        antecedent[enumsort] = []
        for i in range(len(qvar) - 1):
            qvi = qvar[i]
            qvars.add(qvi)
            for j in range(i+1, len(qvar)):
                if i != j:
                    deq = Not(EqualsOrIff(qvi, qvar[j]))
                    antecedent[enumsort].append(deq)
#                         print("adding: %s" % deq)
        qvars.add(qvar[-1])
    
    self.print_fullsorts(fullsorts)
    
    cubeSym = cube

    isorts = dict()
    ivars = set()
    if cubeSym.is_exists():
        for v in cubeSym.quantifier_vars():
            if v not in subs:
                vs = v.symbol_type()
                if vs not in isorts:
                    isorts[vs] = list()
                idx = len(isorts[vs])
                name = str(vs) + ":i" + str(idx)
                isymbol = Symbol(name, vs)
                isorts[vs].append(isymbol)
                subs[v] = isymbol
                ivars.add(isymbol)
                qvars.add(isymbol)
        cubeSym = cubeSym.args()[0]
                
    cubeSym = cubeSym.simple_substitute(subs)
    cubeSet = flatten_cube(cubeSym)
    
#         self.reduce_antecedent(cubeSet, qvars, antecedent, fIdx)
    
    print("(cube: std)")
    for c in cubeSet:
        print("\t%s" % pretty_serialize(c, mode=0))

    if common.gopts.opt > 0 and reducePremise:
        push_time()
        antecedent, reduceSz, varSet = self.reduce_antecedent2(cubeSym, qvars, enum2qvar, fIdx)
        self.update_time_stat("time-antecedent", pop_time())
        print()
    
    if self.ordered == "partial":
        cubeSet = self.boost_ordered(cubeSet, antecedent, qvars, fIdx)

    eqMap = dict()
    if common.gopts.const > 0:
        eqMap, cubeSet, antecedent, fullsorts = self.propagate_eq(cubeSet, antecedent, ivars, qvars, fullsorts)

    if True:
        cubeSetSimple = cubeSet.copy()
        for v in antecedent.values():
            for c in v:
                cubeSetSimple.add(c)
                
        cubeSimple = And(cubeSetSimple)
        if len(qvars) != 0:
            cubeSimple = Exists(qvars, cubeSimple)
                
        cubesOut = list()
        if (self.system.gen == "univ") or (len(fullsorts) == 0) or (len(crels) <= 1) or (len(cubeSet) < minSz):
            cubesOut.append((cubeSimple, "univ"))
        else:
            uqvars = set()        
            
            cubeSet2 = cubeSet.copy()
            for fs in fullsorts:
                enumsort = fs[0]
                qvar = fs[1]
                qvarSet = set()
                for q in qvar:
                    qvarSet.add(q)
                
                qvart = fs[0]
                uSymbol = Symbol("V:"+str(qvart), qvart)
                qv2cubes = {}
                qv2ucubes = {}
                for q in qvar:
                    qv2cubes[q] = set()
                    qv2ucubes[q] = set()
                
                ucubes = set()
                for c in cubeSet:
                    cvars = c.get_free_variables()
                    cqvars = cvars.intersection(qvarSet)
#                     cqvars = c.get_filtered_nodes(qvarSet)
                    for q in cqvars:
                        tmp_subs = {}
                        tmp_subs[q] = uSymbol
                        uc = c.substitute(tmp_subs)
                        ucubes.add(uc)
                        qv2cubes[q].add(c)
                        qv2ucubes[q].add(uc)
                print("qv2cubes #%d" % len(qv2cubes))
                for k, v in qv2cubes.items():
                    print("\t%s -> %s" % (pretty_serialize(k), pretty_print_set(v, mode=0)))
                print("qv2ucubes #%d" % len(qv2ucubes))
                for k, v in qv2ucubes.items():
                    print("\t%s -> %s" % (pretty_serialize(k), pretty_print_set(v, mode=0)))
                
                ucubes2qv = {}
                emptyIdx = 1
                for qv, ucubes in qv2ucubes.items():
                    uIdx = emptyIdx
                    uc = TRUE()
                    if len(ucubes) == 0:
                        emptyIdx += 1
                    elif qv in self.system.curr._states:
                        emptyIdx += 1
                        print("\t(creating separate cell for %s)" % pretty_serialize(qv))
                    else:
                        uIdx = 0
                        ucubes_sorted = sorted(ucubes, key=str)
                        uc = And(ucubes_sorted)
                    k = (uIdx, uc)
                    if k not in ucubes2qv:
                        ucubes2qv[k] = set()
                    ucubes2qv[k].add(qv)
                print("ucubes2qv #%d" % len(ucubes2qv))
                singles = []
                multiples = []
                for k, v in ucubes2qv.items():
                    if len(v) == 1:
                        singles.append(k)
                    elif len(v) >= minSz:
                        multiples.append(k)
                    print("\t%s -> %s" % (pretty_serialize(k[1]), pretty_print_set(v, mode=0)))

                print("(partition) #%d %s -> { " % (len(ucubes2qv), enumsort), end="")
                for v in ucubes2qv.values():
                    for d in v:
                        print("%s, " % pretty_serialize(d), end="")
                    print("| ", end="")
                print("}")

                nsingles = len(singles)
                nmultiples = len(multiples)
                ncells = len(ucubes2qv)
                print("\t#%d singles, #%d multiples (out of #%d cells)" % (nsingles, nmultiples, ncells))

                if len(ucubes2qv) == 1:
                    for rhs in ucubes2qv.values():
                        if len(rhs) != len(qvar):
                            print("found single part with incomplete instances")
                            assert(0)
                    if qvar[0].is_symbol():
                        uqvars.add(qvar[0])
                    antecedent.pop(enumsort, None)
                    
                    for cubes in qv2cubes.values():
                        for cube in cubes:
                            cubeSet2.discard(cube)
                    for _, uc in ucubes2qv.keys():
                        tmp_subs = {}
                        tmp_subs[uSymbol] = qvar[0]
                        uc = uc.simple_substitute(tmp_subs)
                        ucList = flatten_cube(uc)
                        for v in ucList:
                            cubeSet2.add(v)                
                    for q in qvar:
                        qvars.discard(q)
                    cubeSet = cubeSet2
                elif (ncells == (nsingles + nmultiples) 
                      and nmultiples == 1
                      and nsingles == 1
                      ):
#                 elif (len(ucubes2qv) == 2 
#                       and (len(qvar) > minSz)
# #                           and (not self.system.is_epr())
#                     ):
#                     elif len(ucubes2qv) == 2 and (len(qvar) > minSz) and (not self.system.is_epr()):
                    qsingle = []
                    qmulti = None
                    for k in singles:
                        assert(k in ucubes2qv)
                        qg = ucubes2qv[k]
                        assert(len(qg) == 1)
                        for q in qg:
                            qsingle.append(q)
                    for k in multiples:
                        assert(k in ucubes2qv)
                        qg = ucubes2qv[k]
                        assert(len(qg) >= minSz)
                        if len(qg) == (len(qvar) - len(qsingle)):
                            qmulti = qg

                    if len(qsingle) != 0 and qmulti != None:
                        ucsingle = {}
                        ucmulti = set()
                        
                        for qs in qsingle:
                            ucsingle[qs] = set()
                            tmp_subs = {}
                            tmp_subs[uSymbol] = qs
                            for uc in qv2ucubes[qs]:
                                uc = uc.simple_substitute(tmp_subs)
                                ucsingle[qs].add(uc)
                        qm = sorted(qmulti, key=str)[0]
                        tmp_subs = {}
                        tmp_subs[uSymbol] = qm
                        for uc in qv2ucubes[qm]:
                            uc = uc.simple_substitute(tmp_subs)
                            ucmulti.add(uc)
                            
                        print("ucsingle:")
                        for k, rhs in ucsingle.items():
                            print("\t%s:" % pretty_serialize(k))
                            for v in rhs:
                                print("\t\t%s" % pretty_serialize(v))
                        print("ucmulti:")
                        for v in ucmulti:
                            print("\t%s" % pretty_serialize(v))
                            
                        if qm.is_symbol():
                            uqvars.add(qm)
                        if enumsort in antecedent:
                            newAnt = []
                            for v in antecedent[enumsort]:
                                vvars = v.get_free_variables()
                                vmvars = vvars.intersection(qmulti)
                                if len(vmvars) == 0:
                                    newAnt.append(v)
                            antecedent.pop(enumsort, None)
                            if len(newAnt) != 0:
                                antecedent[enumsort] = newAnt
                        
                        for q in qmulti:
                            for cube in qv2cubes[q]:
                                cubeSet2.discard(cube)
#                             for v in ucsingle:
#                                 cubeSet2.add(v)                
                        for q in qmulti:
                            qvars.discard(q)
                        eqM = []
                        for qs in qsingle:
                            eqM.append(EqualsOrIff(qs, qm))
                        sEqm = Or(eqM)
                        for v in ucmulti:
                            vNew = Or(sEqm, v)
                            cubeSet2.add(vNew)
                        cubeSet = cubeSet2
                
            eqvars2 = set()
            forwardArcs = 0
            reverseArcs = 0
            if len(uqvars) != 0:
                if (self.system.gen != "fe"):
                    for u in qvars:
                        ut = u.symbol_type()
                        for e in uqvars:
                            et = e.symbol_type()
                            if (self.system.gen == "fef"):
                                if ut != et or True:
                                    eqvars2.add(u)
                            else:
                                print("\t(epr check: forward)", end="")
                                if not self.system.allowed_arc(ut, et):
                                    forwardArcs += 1
                                    print("\t(epr check: reverse)", end="")
                                    if not self.system.allowed_arc(et, ut):
                                        reverseArcs += 1
                                    eqvars2.add(u)
                for u in eqvars2:
                    if u in qvars:
                        qvars.remove(u)

            cubeSet = cubeSet2
            for v in antecedent.values():
                for c in v:
                    cubeSet.add(c)
            
            cubesOut = list()
            if (self.system.gen == "fef") and (len(uqvars) != 0) and (len(eqvars2) != 0):
                fancy = "fef"

                cubeSym = None
                count = 0
                innerVars = list(eqvars2)
                while True or (len(innerVars) != 0):
                    count += 1
                    preCube = set()
                    postCube = set()
                    postVars = set()
                    for q in uqvars:
                        postVars.add(q)
                    for q in innerVars:
                        postVars.add(q)
                    for c in cubeSet:
                        argvars = c.get_free_variables()
                        argvars = argvars.intersection(postVars)
                        if len(argvars) == 0:
                            preCube.add(c)
                        else:
                            postCube.add(c)
                    postCube = sorted(postCube, key=str)
                    preCube = sorted(preCube, key=str)
                    cubeSym = And(postCube)
                    if len(innerVars) != 0:
                        cubeSym = Exists(innerVars, cubeSym)
                    if len(uqvars) != 0:
                        cubeSym = ForAll(uqvars, cubeSym)
                    if len(preCube) != 0:
                        cubeSym = And(And(preCube), cubeSym)
                    if len(qvars) != 0:
                        cubeSym = Exists(qvars, cubeSym)
                    print("(#%d: quantifier-reduced) " % count)
                    print("\t%s" % pretty_serialize(Not(cubeSym), mode=0))

#                     notCubeSym_formula = self.get_formula_qf(Not(cubeSym))
#                     print("\t(quantifier-free version)")
#                     notCubeSym_formulaList = flatten_and(notCubeSym_formula)
#                     for v in notCubeSym_formulaList:
#                         print("\t\t%s" % pretty_serialize(v))
                    
                    solver = self.get_framesolver(fIdx)
                    cubeSym_formula = self.get_formula_qf(cubeSym)
                    print("\t(#%d: quantifier-rearrangement) " % count, end="")
                    result = self.solve_formula(solver, cubeSym_formula)
                    if not result:
                        cubesOut.append((cubeSym, fancy))
                        break
                    if len(innerVars) != 0:
                        u = innerVars.pop(0)
                        qvars.add(u)
                    else:
                        break
                assert(len(cubesOut) != 0)
                cubesOut.reverse()
                
#                 if len(cubesOut) == 0:
#                     preCube = set()
#                     postCube = set()
#                     postVars = set()
#                     for q in uqvars:
#                         postVars.add(q)
#                     for q in innerVars:
#                         postVars.add(q)
#                     for c in cubeSet:
#                         argvars = c.get_free_variables()
#                         argvars = argvars.intersection(postVars)
#                         if False and len(argvars) == 0:
#                             preCube.add(c)
#                         else:
#                             postCube.add(c)
#                     postCube = sorted(postCube, key=str)
#                     preCube = sorted(preCube, key=str)
#                     cubeSym2 = And(postCube)
#                     if len(uqvars) != 0:
#                         cubeSym2 = ForAll(uqvars, cubeSym2)
#                     if len(innerVars) != 0:
#                         cubeSym2 = Exists(innerVars, cubeSym2)
#                     if len(preCube) != 0:
#                         cubeSym2 = And(And(preCube), cubeSym2)
#                     if len(qvars) != 0:
#                         cubeSym2 = Exists(qvars, cubeSym2)
#                     cubesOut.append((cubeSym2, fancy))
            else:
                preCube = set()
                postCube = set()
                if len(uqvars) == 0:
                    postCube = cubeSet
                else:
                    postVars = set()
                    for q in uqvars:
                        postVars.add(q)
                    for q in eqvars2:
                        postVars.add(q)
                    for c in cubeSet:
                        argvars = c.get_free_variables()
                        argvars = argvars.intersection(postVars)
                        if len(argvars) == 0:
                            preCube.add(c)
                        else:
                            postCube.add(c)
                postCube = sorted(postCube, key=str)
                preCube = sorted(preCube, key=str)
                cubeSym = And(postCube)
                if len(eqvars2) != 0:
                    cubeSym = Exists(eqvars2, cubeSym)
                if len(uqvars) != 0:
                    cubeSym = ForAll(uqvars, cubeSym)
                if len(preCube) != 0:
                    cubeSym = And(And(preCube), cubeSym)
                if len(qvars) != 0: 
                    cubeSym = Exists(qvars, cubeSym)
                
                fancy = "univ"
                if (len(uqvars) != 0):
                    fancy = "epr"
                cubesOut.append((cubeSym, fancy))
                
                if fancy:
                    if len(eqvars2) != 0:
                        cubeSym2 = And(postCube)
                        if len(uqvars) != 0:
                            cubeSym2 = ForAll(uqvars, cubeSym2)
                        if len(eqvars2) != 0:
                            cubeSym2 = Exists(eqvars2, cubeSym2)
                        if len(preCube) != 0:
                            cubeSym2 = And(And(preCube), cubeSym2)
                        if len(qvars) != 0:
                            cubeSym2 = Exists(qvars, cubeSym2)
                        print("(epr reduced)")
                        print("\t%s" % pretty_serialize(Not(cubeSym), mode=0))
                        print("(non-epr version)")
                        print("\t%s" % pretty_serialize(Not(cubeSym2), mode=0))
                        
                        result = False
                        if (reverseArcs != 0):
                            print("\tBoth verions not allowed!")
                            if (self.system.gen == "epr_strict"):
                                result = True
                            
                        if not result:
                            solver = self.get_framesolver(fIdx)
                            print("(epr-reduction) ", end="")
                            cubeSym_formula = self.get_formula_qf(cubeSym)
                            result = self.solve_formula(solver, cubeSym_formula)
                        if result:
                            print("\tEPR-reduction is not allowed!")
                            cubesOut.pop()
                            
                            if (self.system.gen == "epr_strict") or (self.system.gen == "epr_loose"):
                                print("\tLearning universal version instead.")
                                cubesOut.append((cubeSimple, "univ"))
                            else:
                                print("\tLearning non-epr version instead.")
                                cubesOut.append((cubeSym2, "non-epr"))
    #                         assert(0)

        if common.gopts.const > 0:
            for i in range(len(cubesOut)):
                cubeTop = cubesOut[i]
                cubeEq = self.propagate_eq_post(cubeTop[0])
                cubesOut[i] = (cubeEq, cubeTop[1])
    
        print("(boosted clause)")
        print("\t%s" % pretty_serialize(Not(cubesOut[0][0]), mode=0))
        
        print("---------------------------")
        print("(original clause)")
        print("\t%s" % cube_str)
        print("(learnt sym-boosted clause)")
        print("\t%s" % pretty_serialize(Not(cubesOut[0][0]), mode=0))
        print("---------------------------")
    else:
        cubesOut = get_uniform(self, fullsorts, cubeSet, qvars, antecedent)
    
    return cubesOut
예제 #20
0
 def test_function_smtlib_print(self):
     f_t = FunctionType(BOOL, [BOOL])
     f0 = Symbol('f 0', f_t)
     f0_of_false = Function(f0, [Bool(False)])
     s = to_smtlib(f0_of_false, False)
     self.assertEqual(s, '(|f 0| false)')
예제 #21
0
 def _construct_k_inductive_query(self, euf):
     return Or(
         [And(guard, GT(Function(euf, self._characteristic_functional._pysmt_program_variables_argument), arith))
          for (guard, arith) in self._upper_bound_dnf_for_k_inductive_query])
예제 #22
0
def probably_expr_to_pysmt(expr,
                           pysmt_infinity_variable=None,
                           treat_minus_as_monus=False,
                           monus_euf=None,
                           toReal=False):
    """
    Convert a Probably expression to a pysmt expression.
    Infinity must not occur in a composed arithmetic expression.

    :param expr: The Propbably expression that is to be converted.
    :param pysmt_infinity_variable: The pysmt variable used to represent infinity.
    :param toReal: Whether variables and NatLitExpr shall be cast to Real or not.
    :param treat_minus_as_monus: Whether to repalace every expression of the form a - b by monus_euf(a,b)
    :param monus_euf: The uninterpreted function that is to be used for monus.
    :return: The resulting PySMT expression.
    """

    env = get_env()
    if isinstance(expr, BoolLitExpr):
        if expr.value == True:
            return TRUE()

        elif expr.value == False:
            return FALSE()

        else:
            raise Exception("Unkown expression value.")

    elif isinstance(expr, NatLitExpr):
        return Int(expr.value) if not toReal else Real(expr.value)

    elif isinstance(expr, FloatLitExpr):
        # This value might be infinity
        if expr.is_infinite():
            if pysmt_infinity_variable == None:
                raise Exception(
                    "If infinity occurs in an arithmetic expression, then pysmt_infinity_variable must not be none."
                )
            return pysmt_infinity_variable

        else:
            return Real(expr.to_fraction())

    elif isinstance(expr, VarExpr):
        return env.formula_manager.get_symbol(
            expr.var) if not toReal else ToReal(
                env.formula_manager.get_symbol(expr.var))

    elif isinstance(expr, TickExpr):
        if not isinstance(expr.expr, NatLitExpr):
            raise Exception(
                "Currently we allow for constants in tick(..) expressions only."
            )
        else:
            return probably_expr_to_pysmt(expr.expr, pysmt_infinity_variable,
                                          treat_minus_as_monus, monus_euf,
                                          toReal)

    elif isinstance(expr, BinopExpr):
        if expr.operator == Binop.OR:
            return Or(
                probably_expr_to_pysmt(expr.lhs, pysmt_infinity_variable,
                                       treat_minus_as_monus, monus_euf,
                                       toReal),
                probably_expr_to_pysmt(expr.rhs, pysmt_infinity_variable,
                                       treat_minus_as_monus, monus_euf,
                                       toReal))

        if expr.operator == Binop.AND:
            return And(
                probably_expr_to_pysmt(expr.lhs, pysmt_infinity_variable,
                                       treat_minus_as_monus, monus_euf,
                                       toReal),
                probably_expr_to_pysmt(expr.rhs, pysmt_infinity_variable,
                                       treat_minus_as_monus, monus_euf,
                                       toReal))

        if expr.operator == Binop.LEQ:
            return LE(
                probably_expr_to_pysmt(expr.lhs, pysmt_infinity_variable,
                                       treat_minus_as_monus, monus_euf,
                                       toReal),
                probably_expr_to_pysmt(expr.rhs, pysmt_infinity_variable,
                                       treat_minus_as_monus, monus_euf,
                                       toReal))

        if expr.operator == Binop.LE:
            return LT(
                probably_expr_to_pysmt(expr.lhs, pysmt_infinity_variable,
                                       treat_minus_as_monus, monus_euf,
                                       toReal),
                probably_expr_to_pysmt(expr.rhs, pysmt_infinity_variable,
                                       treat_minus_as_monus, monus_euf,
                                       toReal))

        if expr.operator == Binop.EQ:
            lhs = probably_expr_to_pysmt(expr.lhs, pysmt_infinity_variable,
                                         treat_minus_as_monus, monus_euf,
                                         toReal)
            rhs = probably_expr_to_pysmt(expr.rhs, pysmt_infinity_variable,
                                         treat_minus_as_monus, monus_euf,
                                         toReal)
            return EqualsOrIff(lhs, rhs)

        if expr.operator == Binop.PLUS:
            summand_1 = probably_expr_to_pysmt(expr.lhs,
                                               pysmt_infinity_variable,
                                               treat_minus_as_monus, monus_euf,
                                               toReal)
            summand_2 = probably_expr_to_pysmt(expr.rhs,
                                               pysmt_infinity_variable,
                                               treat_minus_as_monus, monus_euf,
                                               toReal)

            if summand_1 == pysmt_infinity_variable or summand_2 == pysmt_infinity_variable:
                raise Exception(
                    "Infinity must not occur in a composed arithmetic expression."
                )

            return Plus(summand_1, summand_2)

        if expr.operator == Binop.MINUS:
            min_1 = probably_expr_to_pysmt(expr.lhs, pysmt_infinity_variable,
                                           treat_minus_as_monus, monus_euf,
                                           toReal)
            min_2 = probably_expr_to_pysmt(expr.rhs, pysmt_infinity_variable,
                                           treat_minus_as_monus, monus_euf,
                                           toReal)

            if min_1 == pysmt_infinity_variable or min_2 == pysmt_infinity_variable:
                raise Exception(
                    "Infinity must not occur in a composed arithmetic expression."
                )

            # Do we have to treat minus as monus?
            if treat_minus_as_monus:
                if monus_euf == None:
                    raise Exception(
                        "If minus is to be treated as monus, then a monus_euf has to be provided."
                    )

                monus_expr = Function(monus_euf, (min_1, min_2))

                if toReal:
                    encountered_real_monus_pairs.add((min_1, min_2))
                else:
                    encountered_monus_pairs.add((min_1, min_2))

                return monus_expr
            else:
                return Minus(min_1, min_2)

        if expr.operator == Binop.TIMES:
            fac_1 = probably_expr_to_pysmt(expr.lhs, pysmt_infinity_variable,
                                           treat_minus_as_monus, monus_euf,
                                           toReal)
            fac_2 = probably_expr_to_pysmt(expr.rhs, pysmt_infinity_variable,
                                           treat_minus_as_monus, monus_euf,
                                           toReal)

            if fac_1 == pysmt_infinity_variable or fac_2 == pysmt_infinity_variable:
                raise Exception(
                    "Infinity must not occur in a composed arithmetic expression."
                )

            return Times(fac_1, fac_2)

    elif isinstance(expr, UnopExpr):
        if expr.operator == Unop.NEG:
            return Not(
                probably_expr_to_pysmt(expr.expr, pysmt_infinity_variable,
                                       treat_minus_as_monus, monus_euf,
                                       toReal))

        elif expr.operator == Unop.IVERSON:
            return probably_expr_to_pysmt(expr.expr, pysmt_infinity_variable,
                                          treat_minus_as_monus, monus_euf,
                                          toReal)

        else:
            raise Exception("Unsupported Unop Operator")

    else:
        raise Exception("Invalid expression {expr}")
예제 #23
0
    def _prepare_first_formulae(self):
        """
        Prepare formulae for checking 1-inductivity, i.e., whether Phi(I) <= I holds.
        """

        # Let I be the candidate upper bound.
        # The first goal is to generate formulae that encode P_1 = Phi(I)

        # Get P_1
        first_bmc_euf = self._bmc_formula_generator.get_eufs()[0]
        # K-inductive query involves P_1
        self._k_inductive_query = self._construct_k_inductive_query(first_bmc_euf)

        # Loop terminate formulae for P_1
        self._loop_terminated_formulae = self._bmc_formula_generator.get_loop_terminate_formulae().copy()

        # Loop execute formulae for P_2 are the same as for BMC
        self._loop_execute_formulae = self._bmc_formula_generator.get_loop_execute_formulae().copy()

        # Formulae ensuring that K_1 is the minimum of P_1 and I
        self._pointwise_minimum_formulae = set()

        arg = substitution_to_argument_tuple(self._characteristic_functional.get_pysmt_program_variables(), {})
        for (guard_P, prob_sub_tick_triple) in self._characteristic_functional.get_loop_execute_guard_and_prob_sub_pairs():
            for (guard_I, arith_I) in self._upper_bound_dnf:
                # guardP AND guard_I and P_1(sub) <= arithI   implies    K_1(sub) = P_1(sub)
                self._pointwise_minimum_formulae.add(self._simplifier.simplify(Implies(And([guard_P, guard_I, LE(Function(first_bmc_euf, arg), arith_I)]),
                                                            Equals(Function(self._eufs[0], arg), Function(first_bmc_euf, arg)))))

                self._pointwise_minimum_formulae.add(self._simplifier.simplify(
                    Implies(And([guard_P, guard_I, GT(Function(first_bmc_euf, arg), arith_I)]),
                            Equals(Function(self._eufs[0], arg), arith_I))))

                #self._pointwise_minimum_formulae.add(self._simplifier.simplify(
                #    Implies(And([guard_P, guard_I, GT(Function(first_bmc_euf, arg), arith_I)]),
                #    Equals(Function(self._eufs[0], arg), Function(first_bmc_euf, arg)))))


        for (guard_P, arith_P) in self._characteristic_functional.get_loop_terminated_guard_and_arith_exp_pairs():
            for (guard_I, arith_I) in self._upper_bound_dnf:
                # guardP AND guard_I and P_1(sub) <= arithI   implies    K_1(sub) = P_1(sub)
                self._pointwise_minimum_formulae.add(self._simplifier.simplify(Implies(And([guard_P, guard_I, LE(arith_P, arith_I)]),
                                                            Equals(Function(self._eufs[0], arg), arith_P))))

                self._pointwise_minimum_formulae.add(
                    self._simplifier.simplify(Implies(And([guard_P, guard_I, GT(arith_P, arith_I)]),
                                                      Equals(Function(self._eufs[0], arg), arith_I))))

                #self._pointwise_minimum_formulae.add(
                #    self._simplifier.simplify(Implies(And([guard_P, guard_I, GT(arith_P, arith_I)]),
                #                                      Equals(Function(self._eufs[0], arg), arith_P))))

        # Next, wee need P_2 to encode the DNF of I ..
        second_bmc_euf = self._bmc_formula_generator.get_eufs()[1]
        self._continuation_formulae = { Implies(guard,
                           Equals(Function(second_bmc_euf, self._characteristic_functional.get_pysmt_program_variables_argument()), arith))
                           for (guard, arith) in self._upper_bound_dnf}

        # and to apply the loop_execute_substitutions
        new_continuation_formulae = set()

        for sub in self._characteristic_functional.get_loop_execute_substitutions():
             new_continuation_formulae.update(substitute_all_formulae(self._continuation_formulae, sub, self._euf_substituter, self._simplify_formulae, self._simplifier))

        self._continuation_formulae = new_continuation_formulae

        # Increment BMC unrolling depth for monus formulae
        # In contrast to BMC, we need the next level of monus/rmonus formulae since the 1-induction check already
        # involves loop execute formulae (for P_1 and P_2).
        # TODO: Optimize and prohibit bmc from generating loop_terminate and zero_step_not_terminate formulae
        self._first_monus_formulae = self._bmc_formula_generator.get_monus_formulae().copy()
        self._first_rmonus_formulae = self._bmc_formula_generator.get_rmonus_formulae().copy()
        self._incremental_bmc._increment_unrolling_depth(True)

        # Now P_1 (self._loop_execute_formulae + self._loop_terminated_formulae + self._continuation_formurlae) encodes Phi(I).
        # Recall that Psi_I(I) = Phi(I) min I

        logger.debug("\n" * 2)
        logger.debug("Loop terminated formulae: \n %s" % [form.serialize() for form in self._loop_terminated_formulae])
        logger.debug("Loop execute formulae: \n %s" % [form.serialize() for form in self._loop_execute_formulae])
        logger.debug("Continuation formulae: \n %s" % [form.serialize() for form in
                                                                   self._continuation_formulae])
        logger.debug("The pointwise minimum formulae are: \n %s" % [form.serialize() for form in
                                                       self._pointwise_minimum_formulae])
예제 #24
0
def get_example_formulae(environment=None):
    if environment is None:
        environment = get_env()

    with environment:
        x = Symbol("x", BOOL)
        y = Symbol("y", BOOL)
        p = Symbol("p", INT)
        q = Symbol("q", INT)
        r = Symbol("r", REAL)
        s = Symbol("s", REAL)

        rf = Symbol("rf", FunctionType(REAL, [REAL, REAL]))
        rg = Symbol("rg", FunctionType(REAL, [REAL]))

        ih = Symbol("ih", FunctionType(INT, [REAL, INT]))
        ig = Symbol("ig", FunctionType(INT, [INT]))

        bv8 = Symbol("bv1", BV8)
        bv16 = Symbol("bv2", BV16)

        result = [
            # Formula, is_valid, is_sat, is_qf
            # x /\ y
            Example(expr=And(x, y),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_BOOL),

            # x <-> y
            Example(expr=Iff(x, y),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_BOOL),

            # (x \/ y )  /\ ! ( x \/ y )
            Example(expr=And(Or(x, y), Not(Or(x, y))),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.QF_BOOL),

            # (x /\ !y)
            Example(expr=And(x, Not(y)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_BOOL),

            # False -> True
            Example(expr=Implies(FALSE(), TRUE()),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BOOL),

            #
            #  LIA
            #
            # (p > q) /\ x -> y
            Example(expr=And(GT(p, q), Implies(x, y)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_IDL),

            # (p + q) = 5 /\ (p > q)
            Example(expr=And(Equals(Plus(p, q), Int(5)), GT(p, q)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_LIA),

            # (p >= q) \/ ( p <= q)
            Example(expr=Or(GE(p, q), LE(p, q)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_IDL),

            # !( p < q * 2 )
            Example(expr=Not(LT(p, Times(q, Int(2)))),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_LIA),

            # p - (5 - 2) > p
            Example(expr=GT(Minus(p, Minus(Int(5), Int(2))), p),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.QF_IDL),

            # x ? 7: (p + -1) * 3 = q
            Example(expr=Equals(
                Ite(x, Int(7), Times(Plus(p, Int(-1)), Int(3))), q),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_LIA),
            Example(expr=LT(p, Plus(q, Int(1))),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_LIA),
            #
            # LRA
            #
            # (r > s) /\ x -> y
            Example(expr=And(GT(r, s), Implies(x, y)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_RDL),

            # (r + s) = 5.6 /\ (r > s)
            Example(expr=And(Equals(Plus(r, s), Real(Fraction("5.6"))),
                             GT(r, s)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_LRA),

            # (r >= s) \/ ( r <= s)
            Example(expr=Or(GE(r, s), LE(r, s)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_RDL),

            # !( (r / (1/2)) < s * 2 )
            Example(expr=Not(LT(Div(r, Real((1, 2))), Times(s, Real(2)))),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_LRA),

            # ! ( r - (5 - 2) > r )
            Example(expr=Not(GT(Minus(r, Minus(Real(5), Real(2))), r)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_RDL),

            # x ? 7: (s + -1) * 3 = r
            Example(expr=Equals(
                Ite(x, Real(7), Times(Plus(s, Real(-1)), Real(3))), r),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_LRA),

            #
            # EUF
            #

            # rf(5, rg(2)) = 0
            Example(expr=Equals(Function(rf, (Real(5), Function(rg, (r, )))),
                                Real(0)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_UFLRA),

            # (rg(r) = 5 + 2) <-> (rg(r) = 7)
            Example(expr=Iff(Equals(Function(rg, [r]), Plus(Real(5), Real(2))),
                             Equals(Function(rg, [r]), Real(7))),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_UFLRA),

            # (r = s + 1) & (rg(s) = 5) & (rg(r - 1) = 7)
            Example(expr=And([
                Equals(r, Plus(s, Real(1))),
                Equals(Function(rg, [s]), Real(5)),
                Equals(Function(rg, [Minus(r, Real(1))]), Real(7))
            ]),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.QF_UFLRA),

            #
            # BV
            #

            # bv_one & bv_zero == bv_zero
            Example(expr=Equals(BVAnd(BVOne(32), BVZero(32)), BVZero(32)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            # ~(010) == 101
            Example(expr=Equals(BVNot(BV("010")), BV("101")),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            # "111" xor "000" == "000"
            Example(expr=Equals(BVXor(BV("111"), BV("000")), BV("000")),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.QF_BV),

            # bv8 :: bv8 < bv_zero
            Example(expr=BVULT(BVConcat(bv8, bv8), BVZero(16)),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.QF_BV),

            # bv_one[:7] == bv_one
            Example(expr=Equals(BVExtract(BVOne(32), end=7), BVOne(8)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            # (((bv8 + bv_one) * bv(5)) / bv(5)) > bv(0)
            Example(expr=BVUGT(
                BVUDiv(BVMul(BVAdd(bv8, BVOne(8)), BV(5, width=8)),
                       BV(5, width=8)), BVZero(8)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            # bv16 >=u bv(0)
            Example(expr=BVUGE(bv16, BVZero(16)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            # bv16 >=s bv(0)
            Example(expr=BVSGE(bv16, BVZero(16)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            # (BV(5) rem BV(2) > bv_zero) /\ (BV(5) rem BV(2) < bv_one)
            Example(expr=And(
                BVUGT(BVURem(BV(5, width=32), BV(2, width=32)), BVZero(32)),
                BVULE(BVURem(BV(5, width=32), BV(2, width=32)), BVOne(32))),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            # ((bv_one + (- bv_one)) << 1) >> 1 == bv_one
            Example(expr=Equals(
                BVLShr(BVLShl(BVAdd(BVOne(32), BVNeg(BVOne(32))), 1), 1),
                BVOne(32)),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.QF_BV),

            # bv_one - bv_one == bv_zero
            Example(expr=Equals(BVSub(BVOne(32), BVOne(32)), BVZero(32)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            # Rotations
            Example(expr=Equals(BVRor(BVRol(BVOne(32), 1), 1), BVOne(32)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            # Extensions
            Example(expr=Equals(BVZExt(BVZero(5), 11), BVSExt(BVZero(1), 15)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            # bv16 - bv16 = 0_16
            Example(expr=Equals(BVSub(bv16, bv16), BVZero(16)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            # (bv16 - bv16)[0:7] = bv8
            Example(expr=Equals(BVExtract(BVSub(bv16, bv16), 0, 7), bv8),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            # (bv16[0,7] comp bv8) = bv1
            Example(expr=Equals(BVComp(BVExtract(bv16, 0, 7), bv8), BVOne(1)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            # (bv16 comp bv16) = bv0
            Example(expr=Equals(BVComp(bv16, bv16), BVZero(1)),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.QF_BV),

            # (bv16 s< bv16)
            Example(expr=BVSLT(bv16, bv16),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.QF_BV),

            # (bv16 s< 0_16)
            Example(expr=BVSLT(bv16, BVZero(16)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            # (bv16 u< bv16)
            Example(expr=BVULT(bv16, bv16),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.QF_BV),

            # (bv16 s< 0_16)
            Example(expr=BVULT(bv16, BVZero(16)),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.QF_BV),

            # (bv16 | 0_16) = bv16
            Example(expr=Equals(BVOr(bv16, BVZero(16)), bv16),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            # (bv16 & 0_16) = 0_16
            Example(expr=Equals(BVAnd(bv16, BVZero(16)), BVZero(16)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            # 0_16 s< bv16 & ((bv16 s/ -1) s< 0)
            Example(expr=And(BVSLT(BVZero(16), bv16),
                             BVSLT(BVSDiv(bv16, SBV(-1, 16)), BVZero(16))),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            # 0_16 s< bv16 & ((bv16 s% -1) s< 0)
            Example(expr=And(BVSLT(BVZero(16), bv16),
                             BVSLT(BVSRem(bv16, BVOne(16)), BVZero(16))),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.QF_BV),

            # bv16 u% 1 = 0_16
            Example(expr=Equals(BVURem(bv16, BVOne(16)), BVZero(16)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            # bv16 s% 1 = 0_16
            Example(expr=Equals(BVSRem(bv16, BVOne(16)), BVZero(16)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            # bv16 s% -1 = 0_16
            Example(expr=Equals(BVSRem(bv16, BVNeg(BVOne(16))), BVZero(16)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            # bv16 a>> 0 = bv16
            Example(expr=Equals(BVAShr(bv16, BVZero(16)), bv16),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            # 0 s<= bv16 & bv16 a>> 1 = bv16 >> 1
            Example(expr=And(
                BVSLE(BVZero(16), bv16),
                Equals(BVAShr(bv16, BVOne(16)), BVLShr(bv16, BVOne(16)))),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_BV),

            #
            # Quantification
            #

            # forall y . x -> y
            Example(expr=ForAll([y], Implies(x, y)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.BOOL),

            # forall p,q . p + q = 0
            Example(expr=ForAll([p, q], Equals(Plus(p, q), Int(0))),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.LIA),

            # forall r,s . ((r > 0) & (s > 0)) -> (r - s < r)
            Example(expr=ForAll([r, s],
                                Implies(And(GT(r, Real(0)), GT(s, Real(0))),
                                        (LT(Minus(r, s), r)))),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.LRA),

            # exists x,y . x -> y
            Example(expr=Exists([x, y], Implies(x, y)),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.BOOL),

            # exists p,q . p + q = 0
            Example(expr=Exists([p, q], Equals(Plus(p, q), Int(0))),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.LIA),

            # exists r . forall s .  (r - s > r)
            Example(expr=Exists([r], ForAll([s], GT(Minus(r, s), r))),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.LRA),

            # forall r . exists s .  (r - s > r)
            Example(expr=ForAll([r], Exists([s], GT(Minus(r, s), r))),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.LRA),

            # x /\ forall r. (r + s = 5)
            Example(expr=And(x, ForAll([r], Equals(Plus(r, s), Real(5)))),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.LRA),

            #
            # UFLIRA
            #

            # ih(r,q) > p /\ (x -> y)
            Example(expr=And(GT(Function(ih, (r, q)), p), Implies(x, y)),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_UFLIRA),

            # ( (p - 3) = q ) -> ( ih(r, q + 3) > p \/ ih(r, p) <= p )
            Example(expr=Implies(
                Equals(Minus(p, Int(3)), q),
                Or(GT(Function(ih, (r, Plus(q, Int(3)))), p),
                   LE(Function(ih, (r, p)), p))),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_UFLIRA),

            # ( (ToReal(p - 3) = r) /\ (ToReal(q) = r) ) ->
            #     ( ( ih(ToReal(p - 3), q + 3) > p ) \/ (ih(r, p) <= p) )
            Example(expr=Implies(
                And(Equals(ToReal(Minus(p, Int(3))), r), Equals(ToReal(q), r)),
                Or(
                    GT(
                        Function(ih,
                                 (ToReal(Minus(p, Int(3))), Plus(q, Int(3)))),
                        p), LE(Function(ih, (r, p)), p))),
                    is_valid=True,
                    is_sat=True,
                    logic=pysmt.logics.QF_UFLIRA),

            # ! ( (ToReal(p - 3) = r /\ ToReal(q) = r) ->
            #        ( ih(ToReal(p - 3), q + 3) > p  \/
            #          ih(r,p) <= p ) )
            Example(expr=Not(
                Implies(
                    And(Equals(ToReal(Minus(p, Int(3))), r),
                        Equals(ToReal(q), r)),
                    Or(
                        GT(
                            Function(
                                ih,
                                (ToReal(Minus(p, Int(3))), Plus(q, Int(3)))),
                            p), LE(Function(ih, (r, p)), p)))),
                    is_valid=False,
                    is_sat=False,
                    logic=pysmt.logics.QF_UFLIRA),

            # Test complex names
            Example(expr=And(
                Symbol("Did you know that any string works? #yolo"),
                Symbol("10"), Symbol("|#somesolverskeepthe||"), Symbol(" ")),
                    is_valid=False,
                    is_sat=True,
                    logic=pysmt.logics.QF_BOOL),
        ]
        return result