Esempio n. 1
0
    def testBetaConv(self):
        test_data = [
            (Comb(Abs("x", Ta, B0), c), c),
            (Comb(Abs("x", Ta, a), c), a),
        ]

        for t, res in test_data:
            self.assertEqual(t.beta_conv(), res)
Esempio n. 2
0
    def testEquals(self):
        test_data = [
            (Abs("x", Ta, b), Abs("y", Ta, b)),
            (Abs("x", Tb, f(B0)), Abs("y", Tb, f(B0))),
        ]

        for t1, t2 in test_data:
            self.assertEqual(t1, t2)
Esempio n. 3
0
    def testInferTypeFail2(self):
        test_data = [
            Abs("x", None, Abs("y", None, Const("equals", None)(Var("x", None), Var("y", None)))),
            Const("nil", None),
        ]

        for t in test_data:
            self.assertRaisesRegex(TypeInferenceException, "Unspecified type", type_infer, t)
Esempio n. 4
0
    def testAbstractOver(self):
        test_data = [
            (a, a, B0),
            (Abs("b", Ta, a), a, Abs("b", Ta, B1)),
            (Abs("a", Ta, a), a, Abs("a", Ta, B1)),
            (f(a), a, f(B0)),
            (c, a, c),
        ]

        for s, t, res in test_data:
            self.assertEqual(s.abstract_over(t), res)
Esempio n. 5
0
    def testSubst(self):
        test_data = [
            (SVar('a', Ta), c),
            (c, c),
            (f(SVar('a', Ta)), f(c)),
            (Abs("x", Ta, B0), Abs("x", Ta, B0)),
            (Abs("x", Ta, SVar('a', Ta)), Abs("x", Ta, c)),
        ]

        for t, res in test_data:
            self.assertEqual(t.subst(Inst(a=c)), res)
Esempio n. 6
0
    def testOccursVar(self):
        test_data = [
            (a, a, True),
            (a, b, False),
            (f(a), a, True),
            (f(a), b, False),
            (Abs("a", Ta, a), a, True),
            (Abs("a", Ta, b), b, True),
            (Abs("a", Ta, B0), a, False),
        ]

        for s, t, res in test_data:
            self.assertEqual(s.occurs_var(t), res)
Esempio n. 7
0
    def testToInternalVars(self):
        test_data = [
            (Var("x", TVar("a")), Var("_x", TVar("_a"))),
            (Var("x", natT), Var("_x", natT)),
            (Const("x", TVar("a")), Const("x", TVar("_a"))),
            (Const("x", natT), Const("x", natT)),
            (Abs("x", TVar("a"),
                 Var("y",
                     TVar("b"))), Abs("x", TVar("_a"), Var("_y", TVar("_b")))),
        ]

        for t, res in test_data:
            self.assertEqual(matcher.to_internal_vars(t), res)
Esempio n. 8
0
    def testSubstType(self):
        test_data = [
            (Var('a', STa), Var("a", Tb)),
            (Const("c", STa), Const("c", Tb)),
            (Var("f", TFun(STa, Tb))(Var("a",
                                         STa)), Var("f",
                                                    TFun(Tb, Tb))(Var("a",
                                                                      Tb))),
            (Abs("x", STa, B0), Abs("x", Tb, B0)),
            (Abs("x", STa, Var('a', STa)), Abs("x", Tb, Var("a", Tb))),
        ]

        for t, res in test_data:
            self.assertEqual(t.subst_type(TyInst(a=Tb)), res)
Esempio n. 9
0
    def testPrintRename(self):
        test_data = [
            (Const("exists",
                   TFun(TFun(NatType, BoolType),
                        BoolType))(Abs("x", NatType,
                                       nat.less(Bound(0), Var("x", NatType)))),
             "?x1. x1 < x"),
            (Abs("x", NatType, nat.less(Bound(0),
                                        Var("x", NatType))), "%x1. x1 < x"),
        ]

        with global_setting(unicode=False):
            for t, s in test_data:
                self.assertEqual(printer.print_term(t), s)
Esempio n. 10
0
    def testIsOpen(self):
        test_data = [
            (a, False),
            (f(a), False),
            (B0, True),
            (f(B0), True),
            (Abs("x", Ta, B0), False),
            (Abs("x", Ta, B1), True),
            (Abs("x", Ta, "y", Ta, B0), False),
            (Abs("x", Ta, "y", Ta, B1), False),
            (Abs("x", Ta, "y", Ta, Bound(2)), True),
        ]

        for t, res in test_data:
            self.assertEqual(t.is_open(), res)
Esempio n. 11
0
    def testSubstBound(self):
        test_data = [
            (Abs("x", Ta, B0), c, c),
            (Abs("x", Ta, a), c, a),
            (Abs("x", Ta, "y", Tb, B0), c, Abs("y", Tb, B0)),
            (Abs("x", Ta, "y", Tb, B1), c, Abs("y", Tb, c)),
            (Abs("x", Ta, "y", Tb, f2(B1, B0)), c, Abs("y", Tb, f2(c, B0))),
            (Abs("x", Ta, B0), B0, B0),
        ]

        for t, s, res in test_data:
            self.assertEqual(t.subst_bound(s), res)
Esempio n. 12
0
    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"})
Esempio n. 13
0
    def testReprTerm(self):
        test_data = [
            (a, "Var(a,'a)"),
            (f, "Var(f,'a => 'b)"),
            (c, "Const(c,'a)"),
            (f(a), "Comb(Var(f,'a => 'b),Var(a,'a))"),
            (f2(a,
                a), "Comb(Comb(Var(f2,'a => 'a => 'b),Var(a,'a)),Var(a,'a))"),
            (f(g(a)), "Comb(Var(f,'a => 'b),Comb(Var(g,'a => 'a),Var(a,'a)))"),
            (Abs("x", Ta, b), "Abs(x,'a,Var(b,'b))"),
            (Abs("x", Ta, B0), "Abs(x,'a,Bound 0)"),
            (Abs("x", Ta, "y", Ta, B0), "Abs(x,'a,Abs(y,'a,Bound 0))"),
            (Abs("x", Ta, "y", Ta, B1), "Abs(x,'a,Abs(y,'a,Bound 1))"),
        ]

        for t, repr_t in test_data:
            self.assertEqual(repr(t), repr_t)
Esempio n. 14
0
    def testCheckTerm(self):
        test_data = [
            x,
            Term.mk_equals(x, y),
            Term.mk_equals(f, f),
            Term.mk_implies(A, B),
            Abs("x", Ta, Term.mk_equals(x, y)),
        ]

        for t in test_data:
            self.assertEqual(thy.check_term(t), None)
Esempio n. 15
0
    def testCheckedGetTypeFail(self):
        test_data = [
            Comb(a, a),
            Comb(f, f),
            f(a, a),
            f(b),
            Comb(Abs("x", Ta, B0), b),
        ]

        for t in test_data:
            self.assertRaises(TypeCheckException, t.checked_get_type)
Esempio n. 16
0
    def testCheckTerm(self):
        test_data = [
            x,
            Eq(x, y),
            Eq(f, f),
            Implies(A, B),
            Abs("x", Ta, Eq(x, y)),
        ]

        for t in test_data:
            self.assertEqual(theory.thy.check_term(t), None)
Esempio n. 17
0
    def testSubst(self):
        test_data = [
            (a, {
                "a": c
            }, c),
            (c, {
                "a": c
            }, c),
            (f(a), {
                "a": c
            }, f(c)),
            (Abs("x", Ta, B0), {
                "a": c
            }, Abs("x", Ta, B0)),
            (Abs("x", Ta, a), {
                "a": c
            }, Abs("x", Ta, c)),
        ]

        for t, inst, res in test_data:
            self.assertEqual(t.subst(inst), res)
Esempio n. 18
0
    def testCheckedGetType(self):
        test_data = [
            (a, Ta),
            (c, Ta),
            (f(a), Tb),
            (f2(a, a), Tb),
            (f(g(a)), Tb),
            (Comb(Abs("x", Ta, B0), a), Ta),
        ]

        for t, T in test_data:
            self.assertEqual(t.checked_get_type(), T)
Esempio n. 19
0
    def testSubstType(self):
        test_data = [
            (a, {
                "a": Tb
            }, Var("a", Tb)),
            (c, {
                "a": Tb
            }, Const("c", Tb)),
            (f(a), {
                "a": Tb
            }, Comb(Var("f", TFun(Tb, Tb)), Var("a", Tb))),
            (Abs("x", Ta, B0), {
                "a": Tb
            }, Abs("x", Tb, B0)),
            (Abs("x", Ta, a), {
                "a": Tb
            }, Abs("x", Tb, Var("a", Tb))),
        ]

        for t, tyinst, res in test_data:
            self.assertEqual(t.subst_type(tyinst), res)
Esempio n. 20
0
    def testInferType(self):
        test_data = [
            # A1 --> A2
            (Const("implies", None)(Var("A1", None), Var("A2", None)),
             Term.mk_implies(Var("A1", boolT), Var("A2", boolT))),
            # A1 = A2
            (Const("equals", None)(Var("A1", boolT), Var("A2", None)),
             Term.mk_equals(Var("A1", boolT), Var("A2", boolT))),
            # a = b
            (Const("equals", None)(Var("a", None), Var("b", None)),
             Const("equals", TFun(Ta, Ta, boolT))(Var("a", Ta), Var("b", Ta))),
            # %x. P x
            (Abs("x", None,
                 Var("P", None)(Bound(0))),
             Abs("x", Ta,
                 Var("P", TFun(Ta, boolT))(Bound(0)))),
            # %x y. x = y
            (Abs("x", Ta,
                 Abs("y", None,
                     Const("equals", None)(Bound(1), Bound(0)))),
             Abs(
                 "x", Ta,
                 Abs("y", Ta,
                     Const("equals", TFun(Ta, Ta, boolT))(Bound(1),
                                                          Bound(0))))),
            # [a]
            (Const("cons", None)(Var("a", None), Const("nil", None)),
             list.cons(Ta)(Var("a", Ta), Const("nil", listT(Ta)))),
        ]

        for t, res in test_data:
            self.assertEqual(type_infer(thy, ctxt, t), res)
Esempio n. 21
0
def nnf(fm):
    """Negation normal form of a formula."""
    if fm.is_conj():
        return And(nnf(fm.arg1), nnf(fm.arg))
    elif fm.is_disj():
        return Or(nnf(fm.arg1), nnf(fm.arg))
    elif fm.is_implies():
        return Or(nnf(Not(fm.arg1)), nnf(fm.arg))
    elif fm.is_equals():
        return Or(And(nnf(fm.arg1), nnf(fm.arg)),
                  And(nnf(Not(fm.arg1)), nnf(Not(fm.arg))))
    elif fm.is_not():
        p = fm.arg
        if p.is_not():
            return nnf(p.arg)
        elif p.is_conj():
            return Or(nnf(Not(p.arg1)), nnf(Not(p.arg)))
        elif p.is_disj():
            return And(nnf(Not(p.arg1)), nnf(Not(p.arg)))
        elif p.is_implies():
            return And(nnf(p.arg1), nnf(Not(p.arg)))
        elif p.is_equals():
            return Or(And(nnf(p.arg1), nnf(Not(p.arg))),
                      And(nnf(Not(p.arg1)), nnf(p.arg)))
        elif p.is_forall():
            assert p.arg.is_abs()
            return term.exists(p.arg.var_T)(Abs(p.arg.var_name, p.arg.var_T,
                                                nnf(Not(p.arg.body))))
        elif p.is_exists():
            assert p.arg.is_abs()
            return term.forall(p.arg.var_T)(Abs(p.arg.var_name, p.arg.var_T,
                                                nnf(Not(p.arg.body))))
        else:
            return fm
    elif fm.is_forall() or fm.is_exists():
        assert fm.arg.is_abs()
        return fm.fun(Abs(fm.arg.var_name, fm.arg.var_T, nnf(fm.arg.body)))
    else:
        return fm
Esempio n. 22
0
    def testCheckTermFail(self):
        test_data = [
            Const("random", Ta),
            Const("equals", TFun(Ta, Tb, BoolType)),
            Const("equals", TFun(Ta, Ta, Tb)),
            Const("implies", TFun(Ta, Ta, BoolType)),
            Comb(Const("random", Tab), x),
            f(Const("random", Ta)),
            Abs("x", Ta, Const("random", Ta)),
        ]

        for t in test_data:
            self.assertRaises(TheoryException, theory.thy.check_term, t)
Esempio n. 23
0
    def testPrintTheoryExtension(self):
        thy_ext = TheoryExtension()

        id_const = Const("id", TFun(Ta, Ta))
        id_def = Abs("x", Ta, Bound(0))
        id_simps = Term.mk_equals(id_const(x), x)

        thy_ext.add_extension(Constant("id", id_def))
        thy_ext.add_extension(Theorem("id.simps", Thm([], id_simps)))

        str_thy_ext = "\n".join(
            ["Constant id = %x. x", "Theorem id.simps: |- equals (id x) x"])

        self.assertEqual(str(thy_ext), str_thy_ext)
Esempio n. 24
0
def to_internal_vars(pat):
    """Add underscore to each variable in the pattern."""
    if pat.is_var():
        return Var("_" + pat.name, to_internal_tvars(pat.T))
    elif pat.is_const():
        return Const(pat.name, to_internal_tvars(pat.T))
    elif pat.is_comb():
        return to_internal_vars(pat.fun)(to_internal_vars(pat.arg))
    elif pat.is_abs():
        return Abs(pat.var_name, to_internal_tvars(pat.var_T),
                   to_internal_vars(pat.body))
    elif pat.is_bound():
        return pat
    else:
        raise TypeError
Esempio n. 25
0
    def testUncheckedExtend(self):
        """Unchecked extension."""
        id_const = Const("id", TFun(Ta,Ta))
        id_def = Abs("x", Ta, Bound(0))

        exts = [
            extension.Constant("id", TFun(Ta, Ta)),
            extension.Theorem("id_def", Thm([], Eq(id_const, id_def))),
            extension.Theorem("id.simps", Thm([], Eq(id_const, x)))
        ]

        self.assertEqual(theory.thy.unchecked_extend(exts), None)
        self.assertEqual(theory.thy.get_term_sig("id"), TFun(Ta, Ta))
        self.assertEqual(theory.get_theorem("id_def", svar=False), Thm([], Eq(id_const, id_def)))
        self.assertEqual(theory.get_theorem("id.simps", svar=False), Thm([], Eq(id_const, x)))
Esempio n. 26
0
def simplify(fm):
    """Simplify formula.
    
    Remove true, false, and vacuous forall/exists quantification.

    """
    if fm.is_not():
        return simplify1(Not(simplify(fm.arg)))
    elif fm.is_conj() or fm.is_disj() or fm.is_implies() or fm.is_equals():
        return simplify1(fm.head(simplify(fm.arg1), simplify(fm.arg)))
    elif fm.is_forall() or fm.is_exists():
        assert fm.arg.is_abs()
        return simplify1(
            fm.fun(Abs(fm.arg.var_name, fm.arg.var_T, simplify(fm.arg.body))))
    else:
        return fm
Esempio n. 27
0
    def testUncheckedExtend(self):
        """Unchecked extension."""
        thy = Theory.EmptyTheory()
        thy_ext = TheoryExtension()

        id_const = Const("id", TFun(Ta,Ta))
        id_def = Abs("x", Ta, Bound(0))
        id_simps = Term.mk_equals(id_const(x), x)

        thy_ext.add_extension(Constant("id", id_def))        
        thy_ext.add_extension(Theorem("id.simps", Thm([], id_simps)))

        self.assertEqual(thy.unchecked_extend(thy_ext), None)
        self.assertEqual(thy.get_term_sig("id"), TFun(Ta, Ta))
        self.assertEqual(thy.get_theorem("id_def"), Thm.mk_equals(id_const, id_def))
        self.assertEqual(thy.get_theorem("id.simps"), Thm([], id_simps))
Esempio n. 28
0
    def testCheckedExtend2(self):
        """Checked extension: proved theorem."""
        thy = Theory.EmptyTheory()
        thy_ext = TheoryExtension()

        id_const = Const("id", TFun(Ta,Ta))
        id_def = Abs("x", Ta, Bound(0))
        id_simps = Term.mk_equals(id_const(x), x)

        # Proof of |- id x = x from |- id = (%x. x)
        prf = Proof()
        prf.add_item(0, "theorem", args="id_def")  # id = (%x. x)
        prf.add_item(1, "reflexive", args=x)  # x = x
        prf.add_item(2, "combination", prevs=[0, 1])  # id x = (%x. x) x
        prf.add_item(3, "beta_conv", args=id_def(x))  # (%x. x) x = x
        prf.add_item(4, "transitive", prevs=[2, 3])  # id x = x

        thy_ext.add_extension(Constant("id", id_def))
        thy_ext.add_extension(Theorem("id.simps", Thm([], id_simps), prf))

        ext_report = thy.checked_extend(thy_ext)
        self.assertEqual(thy.get_theorem("id.simps"), Thm([], id_simps))
        self.assertEqual(ext_report.get_axioms(), [])
Esempio n. 29
0
    def testGetType(self):
        test_data = [
            (a, Ta),
            (f, Tab),
            (c, Ta),
            (f(a), Tb),
            (f2(a, a), Tb),
            (f(g(a)), Tb),
            (Abs("x", Ta, b), Tab),
            (Abs("x", Ta, B0), Taa),
            (Abs("x", Ta, "y", Tb, B0), TFun(Ta, Tb, Tb)),
            (Abs("x", Ta, "y", Tb, B1), TFun(Ta, Tb, Ta)),
            (Abs("x", Ta, "y", Ta, f(B0)), TFun(Ta, Ta, Tb)),
            (Abs("x", Ta, "y", Ta, f2(B1, B0)), TFun(Ta, Ta, Tb)),
        ]

        for t, T in test_data:
            self.assertEqual(t.get_type(), T)
Esempio n. 30
0
    def testPrintTerm(self):
        test_data = [
            (a, "a"),
            (f, "f"),
            (c, "c"),
            (f(a), "f a"),
            (f2(a, a), "f2 a a"),
            (f(g(a)), "f (g a)"),
            (Abs("x", Ta, b), "%x. b"),
            (Abs("x", Ta, B0), "%x. x"),
            (Abs("x", Ta, "y", Ta, B0), "%x. %y. y"),
            (Abs("x", Ta, "y", Ta, B1), "%x. %y. x"),
            (Abs("x", Ta, "y", Ta, f(B0)), "%x. %y. f y"),
            (Abs("x", Ta, "y", Ta, f2(B1, B0)), "%x. %y. f2 x y"),
        ]

        for t, str_t in test_data:
            self.assertEqual(str(t), str_t)