def testSolve(self): if not z3wrapper.z3_loaded: return context.set_context('nat', vars={ "s": 'nat => nat', "A": 'nat', "B": 'nat' }) test_data = [ ("s 0 = 0 & s 1 = 0 --> s 1 = s 0 * B", True), ("s 1 = s 0 * B & ~~s 0 = A --> s 1 = A * B", True), ("s 1 = s 0 * B & ~s 0 = A --> s 1 + B = (s 0 + 1) * B", True), ("A * B + 1 = 1 + B * A", True), ("s 0 = s 1", False), ("s 0 + s 1 = A --> A + s 2 = B --> s 0 + s 2 + s 1 = B", True), ("s 0 + s 1 = A --> A + s 2 = B --> s 0 + s 2 = B", False), ("(!n. s n = 0) --> s 2 = 0", True), ("(!n. s n = 0) --> s 0 = 1", False), ] for s, res in test_data: t = parser.parse_term(s) self.assertEqual(z3wrapper.solve(t), res)
def testSolveNat(self): if not z3wrapper.z3_loaded: return context.set_context('set', vars={'x': 'nat', 'y': 'nat', 'z': 'nat'}) test_data = [('x - y + z = x + z - y', False), ('x >= y --> x - y + z = x + z - y', True)] for s, res in test_data: t = parser.parse_term(s) self.assertEqual(z3wrapper.solve(t), res)
def verify_subgoal(self, inv_id, rule_id, case_id, hint): """Verify the subgoal from the given hints. In addition to the assumptions given in get_subgoal, we need some additional assumptions, including distinctness of states. """ goal = self.get_subgoal(inv_id, rule_id, case_id, hint) goal = self.replace_states(goal) if z3wrapper.z3_loaded: ans = z3wrapper.solve(goal) else: ans = True return goal, ans
def testSolve(self): if not z3wrapper.z3_loaded: return ctxt = {"s": TFun(natT, natT), "A": natT, "B": natT} test_data = [ ("s 0 = 0 & s 1 = 0 --> s 1 = s 0 * B", True), ("s 1 = s 0 * B & ~~s 0 = A --> s 1 = A * B", True), ("s 1 = s 0 * B & ~s 0 = A --> s 1 + B = (s 0 + 1) * B", True), ("A * B + 1 = 1 + B * A", True), ("s 0 = s 1", False), ("s 0 + s 1 = A --> A + s 2 = B --> s 0 + s 2 + s 1 = B", True), ("s 0 + s 1 = A --> A + s 2 = B --> s 0 + s 2 = B", False), ("(!n. s n = 0) --> s 2 = 0", True), ("(!n. s n = 0) --> s 0 = 1", False), ] for s, res in test_data: t = parser.parse_term(thy, ctxt, s) self.assertEqual(z3wrapper.solve(t), res)
def testSolveReal(self): if not z3wrapper.z3_loaded: return context.set_context('real', vars={ 'a': 'real', 'b': 'real', 'x': 'real', 'f': 'real => real', 'S': 'real set', 'T': 'real set', 'n': 'nat' }) test_data = [ ('max a b = (1/2) * (a + b + abs(a - b))', True), ('(x Mem T --> 0 <= f x) --> S Sub T --> (if x Mem S then f x else 0) <= (if x Mem T then f x else 0)', True), ('{x. (a <= x & x <= b) & ~(a < x & x < b)} Sub {a, b}', True), ('max (if x Mem S then (1::real) else 0) (if x Mem T then 1 else 0) = (if x Mem (S Un T) then 1 else 0)', True), ('min (if x Mem S then (1::real) else 0) (if x Mem T then 1 else 0) = (if x Mem (S Int T) then 1 else 0)', True), ('S Int T = empty_set --> (if x Mem S then (1::real) else 0) + (if x Mem T then 1 else 0) = (if x Mem (S Un T) then 1 else 0)', True), ('S ∪ T = S ∩ T ∪ {x. x ∈ S ∧ ¬x ∈ T} ∪ {x. x ∈ T ∧ ¬x ∈ S}', True), ('(0::real) <= (if x Mem s & 1 / (of_nat n + 1) <= abs (f x) then 1 else 0)', True), ('(0::real) <= of_nat n + 1', True), ('1 / (of_nat n + 1) < b --> 1 < (of_nat n + 1) * b', True), ('1 / (a + 1) < b --> 1 < (a + 1) * b', False), ('a <= of_nat n --> a < of_nat (n + 1)', True), ('~(n = 0) --> of_nat (n - 1) + (1::real) = of_nat n', True), ('(1::real) = 0 --> real_inverse a = b', True), ] for s, res in test_data: t = parser.parse_term(s) self.assertEqual(z3wrapper.solve(t), res)
def verify(): """Verify a program by generating verification conditions, and attempt to solve the conditions using SMT. """ data = json.loads(request.get_data().decode("utf-8")) basic.load_theory('hoare') pre = parser2.cond_parser.parse(data['pre']) post = parser2.cond_parser.parse(data['post']) com = parser2.com_parser.parse(data['com']) com.pre = [pre] com.compute_wp(post) lines = com.get_lines(data['vars']) for line in lines: if line['ty'] == 'vc': vc_hol = line['prop'] line['prop'] = printer.print_term(line['prop']) line['smt'] = z3wrapper.solve(vc_hol) return jsonify({ 'lines': lines, })
def testSolveSet(self): if not z3wrapper.z3_loaded: return context.set_context('set', vars={ 'm': 'nat', 'S': 'nat set', 'T': 'nat set', 'x': 'nat', 'a': "'a", 'A': "'a set" }) test_data = [('x Mem S --> S Sub T --> x Mem T', True), ('m Mem univ', True), ('(?x1. x = x1 & x1 Mem S) --> x Mem S', True), ('(?a1. a = a1 & a1 Mem A) --> a Mem A', True), ('x Mem (diff S T) --> x Mem S', True), ('x Mem S --> x Mem (diff S T)', False)] for s, res in test_data: t = parser.parse_term(s) self.assertEqual(z3wrapper.solve(t), res)