示例#1
0
    def get_extension(self):
        assert self.error is None, "get_extension"
        res = []
        res.append(
            extension.Constant(self.name, self.type, ref_name=self.cname))

        for rule in self.rules:
            res.append(extension.Theorem(rule['name'], Thm([], rule['prop'])))
            res.append(extension.Attribute(rule['name'], 'hint_backward'))

        # Case rule
        Targs, _ = self.type.strip_type()
        vars = []
        for i, Targ in enumerate(Targs):
            vars.append(Var("_a" + str(i + 1), Targ))

        P = Var("P", BoolType)
        pred = Const(self.name, self.type)
        assum0 = pred(*vars)
        assums = []
        for rule in self.rules:
            prop = rule['prop']
            As, C = prop.strip_implies()
            eq_assums = [Eq(var, arg) for var, arg in zip(vars, C.args)]
            assum = Implies(*(eq_assums + As), P)
            for var in reversed(prop.get_vars()):
                assum = Forall(var, assum)
            assums.append(assum)

        prop = Implies(*([assum0] + assums + [P]))
        res.append(extension.Theorem(self.cname + "_cases", Thm([], prop)))

        return res
示例#2
0
 def get_extension(self):
     assert self.error is None, "get_extension"
     res = []
     res.append(
         extension.Constant(self.name, self.type, ref_name=self.cname))
     if self.overloaded:
         res.append(extension.Overload(self.name))
     return res
示例#3
0
 def testCheckedExtend3(self):
     """Axiomatized constant."""
     exts = [
         extension.TConst("nat", 0),
         extension.Constant("id", TFun(Ta,Ta))
     ]
     ext_report = theory.thy.checked_extend(exts)
     self.assertEqual(theory.thy.get_type_sig("nat"), 0)
     self.assertEqual(theory.thy.get_term_sig("id"), TFun(Ta,Ta))
示例#4
0
 def get_extension(self):
     assert self.error is None, "get_extension"
     res = []
     res.append(
         extension.Constant(self.name, self.type, ref_name=self.cname))
     res.append(extension.Theorem(self.cname + "_def", Thm([], self.prop)))
     for attr in self.attributes:
         res.append(extension.Attribute(self.cname + "_def", attr))
     return res
示例#5
0
 def get_extension(self):
     assert self.error is None, "get_extension"
     res = []
     res.append(
         extension.Constant(self.name, self.type, ref_name=self.cname))
     for i, rule in enumerate(self.rules):
         th_name = self.cname + "_def_" + str(i + 1)
         res.append(extension.Theorem(th_name, Thm([], rule['prop'])))
         res.append(extension.Attribute(th_name, "hint_rewrite"))
     return res
示例#6
0
    def add_invariant(self):
        """Add the invariant for the system in GCL."""
        s = Var("s", gcl.stateT)
        invC = Const("inv", TFun(gcl.stateT, BoolType))
        inv_rhs = And(
            *[gcl.convert_term(self.var_map, s, t) for _, t in self.invs])
        prop = Eq(invC(s), inv_rhs)

        exts = [
            extension.Constant("inv", TFun(gcl.stateT, BoolType)),
            extension.Theorem("inv_def", Thm([], prop))
        ]
        theory.thy.unchecked_extend(exts)
示例#7
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)))
示例#8
0
    def testCheckedExtend2(self):
        """Checked extension: proved theorem."""
        id_const = Const("id", TFun(Ta,Ta))
        id_def = Abs("x", Ta, Bound(0))
        id_simps = Eq(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, "subst_type", args=TyInst(a=TVar('a')), prevs=[0])  # id = (%x. x)
        prf.add_item(2, "reflexive", args=x)  # x = x
        prf.add_item(3, "combination", prevs=[1, 2])  # id x = (%x. x) x
        prf.add_item(4, "beta_conv", args=id_def(x))  # (%x. x) x = x
        prf.add_item(5, "transitive", prevs=[3, 4])  # id x = x

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

        ext_report = theory.thy.checked_extend(exts)
        self.assertEqual(theory.get_theorem("id.simps", svar=False), Thm([], id_simps))
        self.assertEqual(ext_report.get_axioms(), [('id_def', Thm([], Eq(id_const, id_def)))])
示例#9
0
    def get_extension(self):
        assert self.error is None, "get_extension"
        res = []

        # Add to type and term signature.
        res.append(extension.TConst(self.name, len(self.args)))
        for constr in self.constrs:
            res.append(
                extension.Constant(constr['name'],
                                   constr['type'],
                                   ref_name=constr['cname']))

        # Add non-equality theorems.
        for constr1, constr2 in itertools.combinations(self.constrs, 2):
            # For each A x_1 ... x_m and B y_1 ... y_n, get the theorem
            # ~ A x_1 ... x_m = B y_1 ... y_n.
            argT1, _ = constr1['type'].strip_type()
            argT2, _ = constr2['type'].strip_type()
            lhs_vars = [Var(nm, T) for nm, T in zip(constr1['args'], argT1)]
            rhs_vars = [Var(nm, T) for nm, T in zip(constr2['args'], argT2)]
            A = Const(constr1['name'], constr1['type'])
            B = Const(constr2['name'], constr2['type'])
            lhs = A(*lhs_vars)
            rhs = B(*rhs_vars)
            neq = Not(Eq(lhs, rhs))
            th_name = "%s_%s_%s_neq" % (self.name, constr1['name'],
                                        constr2['name'])
            res.append(extension.Theorem(th_name, Thm([], neq)))

        # Add injectivity theorems.
        for constr in self.constrs:
            # For each A x_1 ... x_m with m > 0, get the theorem
            # A x_1 ... x_m = A x_1' ... x_m' --> x_1 = x_1' & ... & x_m = x_m'
            if constr['args']:
                argT, _ = constr['type'].strip_type()
                lhs_vars = [Var(nm, T) for nm, T in zip(constr['args'], argT)]
                rhs_vars = [
                    Var(nm + "1", T) for nm, T in zip(constr['args'], argT)
                ]
                A = Const(constr['name'], constr['type'])
                assum = Eq(A(*lhs_vars), A(*rhs_vars))
                concls = [
                    Eq(var1, var2) for var1, var2 in zip(lhs_vars, rhs_vars)
                ]
                concl = And(*concls)
                th_name = "%s_%s_inject" % (self.name, constr['name'])
                res.append(
                    extension.Theorem(th_name, Thm([], Implies(assum, concl))))

        # Add the inductive theorem.
        tvars = [TVar(targ) for targ in self.args]
        T = TConst(self.name, *tvars)
        var_P = Var("P", TFun(T, BoolType))
        ind_assums = []
        for constr in self.constrs:
            A = Const(constr['name'], constr['type'])
            argT, _ = constr['type'].strip_type()
            args = [Var(nm, T2) for nm, T2 in zip(constr['args'], argT)]
            C = var_P(A(*args))
            As = [
                var_P(Var(nm, T2)) for nm, T2 in zip(constr['args'], argT)
                if T2 == T
            ]
            ind_assum = Implies(*(As + [C]))
            for arg in reversed(args):
                ind_assum = Forall(arg, ind_assum)
            ind_assums.append(ind_assum)
        ind_concl = var_P(Var("x", T))
        th_name = self.name + "_induct"
        res.append(
            extension.Theorem(th_name,
                              Thm([], Implies(*(ind_assums + [ind_concl])))))
        res.append(extension.Attribute(th_name, "var_induct"))

        return res