def testCheckProof5(self): """Empty instantiation.""" theory.thy.add_theorem("trivial", Thm([], Implies(A,A))) x_eq_y = Eq(x,y) prf = Proof() prf.add_item(0, "theorem", args="trivial") prf.add_item(1, "substitution", args=Inst(), prevs=[0]) rpt = ProofReport() th = Thm([], Implies(SVar('A', BoolType), SVar('A', BoolType))) self.assertEqual(theory.check_proof(prf, rpt), th) self.assertEqual(rpt.steps_stat(), (1, 1, 0)) self.assertEqual(rpt.th_names, {"trivial"})
def testCheckProof5(self): """Empty instantiation.""" thy = Theory.EmptyTheory() thy.add_theorem("trivial", Thm.mk_implies(A,A)) x_eq_y = Term.mk_equals(x,y) prf = Proof() prf.add_item(0, "theorem", args="trivial") prf.add_item(1, "substitution", args={}, prevs=[0]) rpt = ProofReport() th = Thm.mk_implies(A,A) self.assertEqual(thy.check_proof(prf, rpt), th) self.assertEqual(rpt.steps_stat(), (1, 1, 0)) self.assertEqual(rpt.th_names, {"trivial"})
def testEvalSem5(self): com = While(Lambda(s, Not(Eq(s(zero), Nat(3)))), assn_true, incr_one) st = mk_const_fun(NatType, zero) st2 = fun_upd_of_seq(0, 3) goal = Sem(com, st, st2) prf = imp.eval_Sem_macro().get_proof_term(goal, []).export() rpt = ProofReport() self.assertEqual(theory.check_proof(prf, rpt), Thm([], goal))
def testCheckProof2(self): """Proof of |- A --> A.""" prf = Proof(A) prf.add_item(1, "implies_intr", args=A, prevs=[0]) rpt = ProofReport() self.assertEqual(theory.check_proof(prf, rpt), Thm([], Implies(A,A))) self.assertEqual(rpt.steps, 2)
def testEvalSem5(self): com = While(abs(s, logic.neg(eq(s(zero), nat.to_binary(3)))), assn_true, incr_one) st = mk_const_fun(natT, zero) st2 = fun_upd_of_seq(0, 3) goal = Sem(com, st, st2) prf = hoare.eval_Sem_macro().get_proof_term(thy, goal, []).export() rpt = ProofReport() self.assertEqual(thy.check_proof(prf, rpt), Thm([], goal))
def testCheckProof(self): """Proof of [A, A --> B] |- B.""" A_to_B = Implies(A, B) prf = Proof(A_to_B, A) prf.add_item(2, "implies_elim", prevs=[0, 1]) rpt = ProofReport() self.assertEqual(theory.check_proof(prf, rpt), Thm([A_to_B, A], B)) self.assertEqual(rpt.steps, 3)
def process_file(input, output): basic.load_theory('hoare') dn = os.path.dirname(os.path.realpath(__file__)) with open(os.path.join(dn, 'examples/' + input + '.json'), encoding='utf-8') as a: data = json.load(a) output = json_output.JSONTheory(output, ["hoare"], "Generated from " + input) content = data['content'] eval_count = 0 vcg_count = 0 for run in content[:5]: if run['ty'] == 'eval': com = parse_com(run['com']) st1 = mk_const_fun(NatType, nat.zero) for k, v in sorted(run['init'].items()): st1 = mk_fun_upd(st1, Nat(str_to_nat(k)), Nat(v)) st2 = mk_const_fun(NatType, nat.zero) for k, v in sorted(run['final'].items()): st2 = mk_fun_upd(st2, Nat(str_to_nat(k)), Nat(v)) Sem = imp.Sem(natFunT) goal = Sem(com, st1, st2) prf = ProofTerm("eval_Sem", goal, []).export() rpt = ProofReport() th = theory.check_proof(prf, rpt) output.add_theorem("eval" + str(eval_count), th, prf) eval_count += 1 elif run['ty'] == 'vcg': com = parse_com(run['com']) pre = Lambda(st, parse_cond(run['pre'])) post = Lambda(st, parse_cond(run['post'])) Valid = imp.Valid(natFunT) goal = Valid(pre, com, post) prf = imp.vcg_solve(goal).export() rpt = ProofReport() th = theory.check_proof(prf, rpt) output.add_theorem("vcg" + str(vcg_count), th, prf) vcg_count += 1 else: raise TypeError output.export_json()
def testCheckProofGap(self): """Check proof with gap.""" prf = Proof() prf.add_item(0, "sorry", th = Thm([], Implies(A,B))) prf.add_item(1, "sorry", th = Thm([], A)) prf.add_item(2, "implies_elim", prevs=[0, 1]) rpt = ProofReport() self.assertEqual(theory.check_proof(prf, rpt), Thm([], B)) self.assertEqual(rpt.gaps, [Thm([], Implies(A, B)), Thm([], A)])
def testApplyTheorem(self): thy = basic.load_theory('logic_base') A = Var("A", boolT) B = Var("B", boolT) th = Thm([logic.mk_conj(A, B)], A) prf = Proof(logic.mk_conj(A, B)) prf.add_item(1, "apply_theorem", args="conjD1", prevs=[0]) rpt = ProofReport() self.assertEqual(thy.check_proof(prf, rpt), th) self.assertEqual(rpt.prim_steps, 4) # Reset data for the next check prf = Proof(logic.mk_conj(A, B)) prf.add_item(1, "apply_theorem", args="conjD1", prevs=[0]) rpt = ProofReport() self.assertEqual(thy.check_proof(prf, rpt, check_level=1), th) self.assertEqual(rpt.prim_steps, 1) self.assertEqual(rpt.macro_steps, 1)
def testBetaNorm(self): thy = basic.load_theory('logic_base') t = Term.mk_abs(x, f(x)) prf = Proof(Term.mk_equals(t(x), y)) prf.add_item(1, "beta_norm", prevs=[0]) prf.add_item(2, "implies_intr", args=Term.mk_equals(t(x), y), prevs=[1]) th = Thm.mk_implies(Term.mk_equals(t(x), y), Term.mk_equals(f(x), y)) rpt = ProofReport() self.assertEqual(thy.check_proof(prf, rpt), th) self.assertEqual(rpt.prim_steps, 8) rpt2 = ProofReport() self.assertEqual(thy.check_proof(prf, rpt2, check_level=1), th) self.assertEqual(rpt2.prim_steps, 2) self.assertEqual(rpt2.macro_steps, 1)
def testRewriteGoal(self): thy = basic.load_theory('nat') n = Var("n", nat.natT) eq = Term.mk_equals zero = nat.zero plus = nat.mk_plus prf = Proof() prf.add_item(0, "rewrite_goal", args=("plus_def_1", eq(plus(zero, zero), zero))) th = Thm([], eq(plus(zero, zero), zero)) rpt = ProofReport() self.assertEqual(thy.check_proof(prf, rpt), th) self.assertEqual(rpt.prim_steps, 9) rpt2 = ProofReport() self.assertEqual(thy.check_proof(prf, rpt2, check_level=1), th) self.assertEqual(rpt2.prim_steps, 0) self.assertEqual(rpt2.macro_steps, 1)
def testCheckProofMacro(self): """Proof checking with simple macro.""" thy = Theory.EmptyTheory() thy.add_proof_macro("beta_conv_rhs", beta_conv_rhs_macro()) t = Comb(Abs("x", Ta, Bound(0)), x) prf = Proof() prf.add_item(0, "reflexive", args=t) prf.add_item(1, "beta_conv_rhs", prevs=[0]) th = Thm.mk_equals(t,x) # Check obtaining signature self.assertEqual(thy.get_proof_rule_sig("beta_conv_rhs"), Term) # Check proof without trusting beta_conv_rhs rpt = ProofReport() self.assertEqual(thy.check_proof(prf, rpt), th) self.assertEqual(rpt.steps_stat(), (0, 3, 0)) self.assertEqual(rpt.macros_expand, {"beta_conv_rhs"}) # Check proof while trusting beta_conv_rhs rpt = ProofReport() self.assertEqual(thy.check_proof(prf, rpt, check_level=1), th) self.assertEqual(rpt.steps_stat(), (0, 1, 1)) self.assertEqual(rpt.macros_eval, {"beta_conv_rhs"})
def testCheckProof4(self): """Proof of |- x = y --> x = y by instantiating an existing theorem.""" theory.thy.add_theorem("trivial", Thm([], Implies(A,A))) x_eq_y = Eq(x,y) prf = Proof() prf.add_item(0, "theorem", args="trivial") prf.add_item(1, "substitution", args=Inst(A=x_eq_y), prevs=[0]) rpt = ProofReport() th = Thm([], Implies(x_eq_y,x_eq_y)) self.assertEqual(theory.check_proof(prf, rpt), th) self.assertEqual(rpt.steps, 2)
def testCheckProof3(self): """Proof of [x = y, y = z] |- f z = f x.""" x_eq_y = Eq(x,y) y_eq_z = Eq(y,z) prf = Proof(x_eq_y, y_eq_z) prf.add_item(2, "transitive", prevs=[0, 1]) prf.add_item(3, "symmetric", prevs=[2]) prf.add_item(4, "reflexive", args=f) prf.add_item(5, "combination", prevs=[4, 3]) rpt = ProofReport() th = Thm([x_eq_y, y_eq_z], Eq(f(z),f(x))) self.assertEqual(theory.check_proof(prf, rpt), th) self.assertEqual(rpt.steps, 6)
def testCheckProof4(self): """Proof of |- x = y --> x = y by instantiating an existing theorem.""" thy = Theory.EmptyTheory() thy.add_theorem("trivial", Thm.mk_implies(A,A)) x_eq_y = Term.mk_equals(x,y) prf = Proof() prf.add_item(0, "theorem", args="trivial") prf.add_item(1, "substitution", args={"A" : x_eq_y}, prevs=[0]) rpt = ProofReport() th = Thm.mk_implies(x_eq_y,x_eq_y) self.assertEqual(thy.check_proof(prf, rpt), th) self.assertEqual(rpt.steps, 2)
def testArgCombination(self): thy = basic.load_theory('logic_base') macro = logic_macro.arg_combination_macro() x_eq_y = Term.mk_equals(x, y) fx_eq_fy = Term.mk_equals(f(x), f(y)) th = Thm.assume(x_eq_y) res = Thm([x_eq_y], fx_eq_fy) self.assertEqual(macro.eval(thy, f, [th]), res) prf = Proof(x_eq_y) prf.add_item(1, "arg_combination", args=f, prevs=[0]) rpt = ProofReport() self.assertEqual(thy.check_proof(prf, rpt), res) self.assertEqual(rpt.macros_expand, {"arg_combination"}) self.assertEqual(rpt.prim_steps, 3)
def testFunCombination(self): thy = basic.load_theory('logic_base') macro = logic_macro.fun_combination_macro() f_eq_g = Term.mk_equals(f, g) fx_eq_gx = Term.mk_equals(f(x), g(x)) th = Thm.assume(f_eq_g) res = Thm([f_eq_g], fx_eq_gx) self.assertEqual(macro.eval(thy, x, [th]), res) prf = Proof(f_eq_g) prf.add_item(1, "fun_combination", args=x, prevs=[0]) rpt = ProofReport() self.assertEqual(thy.check_proof(prf, rpt), res) self.assertEqual(rpt.macros_expand, {"fun_combination"}) self.assertEqual(rpt.prim_steps, 3)
def testStepCount(self): rpt = ProofReport() self.assertEqual(rpt.steps, 0) for i in range(10): rpt.apply_primitive_deriv() rpt.apply_theorem("th1") rpt.apply_theorem("th2") rpt.expand_macro("macro1") rpt.eval_macro("macro2") self.assertEqual(rpt.steps, 13) self.assertEqual(rpt.steps_stat(), (2, 10, 1)) self.assertEqual(rpt.th_names, {"th1", "th2"}) self.assertEqual(rpt.macros_expand, {"macro1"}) self.assertEqual(rpt.macros_eval, {"macro2"})