Exemple #1
0
    def get_proof_term(self, thy, t):
        if t.arg1 == zero:
            cv = rewr_conv("times_def_1")
        elif t.arg == zero:
            cv = rewr_conv("mult_0_right")
        elif t.arg1 == one:
            cv = rewr_conv("mult_1_left")
        elif t.arg == one:
            cv = rewr_conv("mult_1_right")
        elif is_times(t.arg1):
            cmp = compare_atom(t.arg1.arg, t.arg)
            if cmp == term_ord.GREATER:
                cv = then_conv(swap_times_r(), arg1_conv(norm_mult_atom()))
            elif cmp == term_ord.EQUAL:
                if is_binary(t.arg):
                    cv = then_conv(rewr_conv("mult_assoc"),
                                   arg_conv(nat_conv()))
                else:
                    cv = all_conv()
            else:
                cv = all_conv()
        else:
            cmp = compare_atom(t.arg1, t.arg)
            if cmp == term_ord.GREATER:
                cv = rewr_conv("mult_comm")
            elif cmp == term_ord.EQUAL:
                if is_binary(t.arg):
                    cv = nat_conv()
                else:
                    cv = all_conv()
            else:
                cv = all_conv()

        return cv.get_proof_term(thy, t)
Exemple #2
0
    def get_proof_term(self, thy, t):
        if is_binary(t):
            cv = all_conv()
        elif is_Suc(t):
            cv = then_conv(rewr_conv("add_1_right", sym=True), norm_full())
        elif is_plus(t):
            cv = then_conv(binop_conv(norm_full()), norm_add_polynomial())
        elif is_times(t):
            cv = then_conv(binop_conv(norm_full()), norm_mult_polynomial())
        else:
            cv = all_conv()

        return cv.get_proof_term(thy, t)
Exemple #3
0
 def get_proof_term(self, thy, t):
     if is_conj(t.arg1):
         return then_conv(
             rewr_conv("conj_assoc", sym=True),
             arg_conv(norm_conj_assoc_clauses())).get_proof_term(thy, t)
     else:
         return all_conv().get_proof_term(thy, t)
Exemple #4
0
    def get_proof_term(self, goal, *, args=None, prevs=None):
        th_name = args
        C = goal.prop

        # Check whether rewriting using the theorem has an effect
        assert has_rewrite(th_name, C, sym=self.sym, conds=prevs), \
            "rewrite: unable to apply theorem."

        cv = then_conv(
            top_sweep_conv(rewr_conv(th_name, sym=self.sym, conds=prevs)),
            beta_norm_conv())
        eq_th = cv.eval(C)
        new_goal = eq_th.prop.rhs

        if self.sym:
            macro_name = 'rewrite_goal_sym'
        else:
            macro_name = 'rewrite_goal'
        if new_goal.is_equals() and new_goal.lhs == new_goal.rhs:
            return ProofTerm(macro_name, args=(th_name, C), prevs=prevs)
        else:
            new_goal = ProofTerm.sorry(Thm(goal.hyps, new_goal))
            assert new_goal.prop != goal.prop, "rewrite: unable to apply theorem"
            return ProofTerm(macro_name,
                             args=(th_name, C),
                             prevs=[new_goal] + prevs)
Exemple #5
0
    def get_proof_term(self, goal, *, args=None, prevs=None):
        assert isinstance(prevs,
                          list) and len(prevs) == 1, "rewrite_goal_with_prev"
        pt = prevs[0]
        C = goal.prop

        # In general, we assume pt.th has forall quantification.
        # First, obtain the patterns
        new_names = logic.get_forall_names(pt.prop)
        new_vars, prev_As, prev_C = logic.strip_all_implies(pt.prop, new_names)

        # Fact used must be an equality
        assert len(
            prev_As) == 0 and prev_C.is_equals(), "rewrite_goal_with_prev"

        for new_var in new_vars:
            pt = pt.forall_elim(new_var)

        # Check whether rewriting using the theorem has an effect
        assert has_rewrite(pt.th, C), "rewrite_goal_with_prev"

        cv = then_conv(top_sweep_conv(rewr_conv(pt)), beta_norm_conv())
        eq_th = cv.eval(C)
        new_goal = eq_th.prop.rhs

        prevs = list(prevs)
        if not new_goal.is_reflexive():
            prevs.append(ProofTerm.sorry(Thm(goal.hyps, new_goal)))
        return ProofTerm('rewrite_goal_with_prev', args=C, prevs=prevs)
Exemple #6
0
    def get_proof_term(self, thy, goal, *, args=None, prevs=None):
        assert isinstance(prevs,
                          list) and len(prevs) == 1, "rewrite_goal_with_prev"
        pt = prevs[0]

        init_As = goal.hyps
        C = goal.prop
        cv = then_conv(top_sweep_conv(rewr_conv(pt, match_vars=False)),
                       top_conv(beta_conv()))

        eq_th = cv.eval(thy, C)
        new_goal = eq_th.prop.rhs

        new_As = list(set(eq_th.hyps) - set(init_As))
        new_As_pts = [ProofTerm.sorry(Thm(init_As, A)) for A in new_As]
        if Term.is_equals(new_goal) and new_goal.lhs == new_goal.rhs:
            return ProofTermDeriv('rewrite_goal_with_prev',
                                  thy,
                                  args=C,
                                  prevs=[pt] + new_As_pts)
        else:
            new_goal_pts = ProofTerm.sorry(Thm(init_As, new_goal))
            return ProofTermDeriv('rewrite_goal_with_prev',
                                  thy,
                                  args=C,
                                  prevs=[pt, new_goal_pts] + new_As_pts)
Exemple #7
0
    def get_proof_term(self, args, pts):
        assert isinstance(args, Term), "rewrite_goal_macro: signature"

        goal = args
        eq_pt = pts[0]

        new_names = get_forall_names(eq_pt.prop)
        new_vars, _, _ = strip_all_implies(eq_pt.prop, new_names)

        for new_var in new_vars:
            eq_pt = eq_pt.forall_elim(new_var)

        pts = pts[1:]

        cv = then_conv(top_sweep_conv(rewr_conv(eq_pt, sym=self.sym)),
                       beta_norm_conv())
        pt = cv.get_proof_term(goal)  # goal = th.prop
        pt = pt.symmetric()           # th.prop = goal
        if pt.prop.lhs.is_reflexive():
            pt = pt.equal_elim(refl(pt.prop.lhs.rhs))
        else:
            pt = pt.equal_elim(pts[0])
            pts = pts[1:]

        for A in pts:
            pt = pt.implies_intr(A.prop).implies_elim(A)
        return pt
Exemple #8
0
def interval_union_subset(t):
    """Given t of the form I1 Un I2, return a theorem of the form
    I1 Un I2 SUB I.

    """
    assert t.is_comb('union', 2), "interval_union_subset"

    I1, I2 = t.args
    a, b = I1.args
    c, d = I2.args
    if is_closed_interval(I1) and is_closed_interval(I2):
        pt = apply_theorem('closed_interval_union',
                           inst=Inst(a=a, b=b, c=c, d=d))
        return pt.on_prop(
            arg_conv(
                then_conv(arg1_conv(const_min_conv()),
                          arg_conv(const_max_conv()))))
    elif is_open_interval(I1) and is_ropen_interval(I2):
        if eval_hol_expr(c) <= eval_hol_expr(a):
            pt = apply_theorem('open_ropen_interval_union1',
                               auto.auto_solve(real.less_eq(c, a)),
                               inst=Inst(b=b, d=d))
        else:
            pt = apply_theorem('open_ropen_interval_union2',
                               auto.auto_solve(real.less(a, c)),
                               inst=Inst(b=b, d=d))
        return pt.on_prop(arg_conv(arg_conv(const_max_conv())))
    else:
        raise NotImplementedError

    return pt
Exemple #9
0
    def get_proof_term(self, thy, args, pts):
        assert len(pts) == 1 and isinstance(
            args, str), "rewrite_fact_macro: signature"

        th_name = args
        eq_pt = ProofTerm.theorem(thy, th_name)
        cv = then_conv(top_sweep_conv(rewr_conv(eq_pt)), top_conv(beta_conv()))
        return pts[0].on_prop(thy, cv)
Exemple #10
0
 def get_proof_term(self, t):
     if t.is_conj():
         return then_conv(
             binop_conv(norm_conj_assoc()),
             norm_conj_assoc_clauses()
         ).get_proof_term(t)
     else:
         return all_conv().get_proof_term(t)
Exemple #11
0
 def get_proof_term(self, thy, t):
     n = t.arg  # remove Suc
     if n == zero:
         return all_conv().get_proof_term(thy, t)
     elif n == one:
         return rewr_conv("one_Suc").get_proof_term(thy, t)
     elif n.head == bit0:
         return rewr_conv("bit0_Suc").get_proof_term(thy, t)
     else:
         return then_conv(rewr_conv("bit1_Suc"),
                          arg_conv(self)).get_proof_term(thy, t)
Exemple #12
0
    def get_proof_term(self, thy, t):
        if not (is_plus(t) and is_binary(t.arg1) and is_binary(t.arg)):
            raise ConvException
        n1, n2 = t.arg1, t.arg  # two summands
        if n1 == zero:
            cv = rewr_conv("plus_def_1")
        elif n2 == zero:
            cv = rewr_conv("add_0_right")
        elif n1 == one:
            cv = then_conv(rewr_conv("add_1_left"), Suc_conv())
        elif n2 == one:
            cv = then_conv(rewr_conv("add_1_right"), Suc_conv())
        elif n1.head == bit0 and n2.head == bit0:
            cv = then_conv(rewr_conv("bit0_bit0_add"), arg_conv(self))
        elif n1.head == bit0 and n2.head == bit1:
            cv = then_conv(rewr_conv("bit0_bit1_add"), arg_conv(self))
        elif n1.head == bit1 and n2.head == bit0:
            cv = then_conv(rewr_conv("bit1_bit0_add"), arg_conv(self))
        else:
            cv = every_conv(rewr_conv("bit1_bit1_add"),
                            arg_conv(arg_conv(self)), arg_conv(Suc_conv()))

        return cv.get_proof_term(thy, t)
Exemple #13
0
    def get_proof_term(self, thy, t):
        if t.arg1 == zero:
            cv = rewr_conv("plus_def_1")
        elif t.arg == zero:
            cv = rewr_conv("add_0_right")
        elif is_plus(t.arg1):
            cmp = compare_monomial(t.arg1.arg, t.arg)
            if cmp == term_ord.GREATER:
                cv = then_conv(swap_add_r(), arg1_conv(norm_add_monomial()))
            elif cmp == term_ord.EQUAL:
                cv = then_conv(rewr_conv("add_assoc"),
                               arg_conv(combine_monomial(thy)))
            else:
                cv = all_conv()
        else:
            cmp = compare_monomial(t.arg1, t.arg)
            if cmp == term_ord.GREATER:
                cv = rewr_conv("add_comm")
            elif cmp == term_ord.EQUAL:
                cv = combine_monomial(thy)
            else:
                cv = all_conv()

        return cv.get_proof_term(thy, t)
Exemple #14
0
    def get_proof_term(self, thy, t):
        n1, n2 = t.arg1, t.arg  # two summands
        if n1 == zero:
            cv = rewr_conv("times_def_1")
        elif n2 == zero:
            cv = rewr_conv("mult_0_right")
        elif n1 == one:
            cv = rewr_conv("mult_1_left")
        elif n2 == one:
            cv = rewr_conv("mult_1_right")
        elif n1.head == bit0 and n2.head == bit0:
            cv = then_conv(rewr_conv("bit0_bit0_mult"),
                           arg_conv(arg_conv(self)))
        elif n1.head == bit0 and n2.head == bit1:
            cv = then_conv(rewr_conv("bit0_bit1_mult"), arg_conv(self))
        elif n1.head == bit1 and n2.head == bit0:
            cv = then_conv(rewr_conv("bit1_bit0_mult"), arg_conv(self))
        else:
            cv = every_conv(rewr_conv("bit1_bit1_mult"),
                            arg_conv(arg1_conv(add_conv())),
                            arg_conv(arg_conv(arg_conv(self))),
                            arg_conv(add_conv()))

        return cv.get_proof_term(thy, t)
Exemple #15
0
    def get_proof_term(self, args, pts):
        assert isinstance(args, str), "rewrite_fact_macro: signature"

        th_name = args
        eq_pt = ProofTerm.theorem(th_name)

        assert len(pts) == len(eq_pt.assums) + 1, "rewrite_fact_macro: signature"

        # Check rewriting using the theorem has an effect
        if not has_rewrite(th_name, pts[0].prop, sym=self.sym, conds=pts[1:]):
            raise InvalidDerivationException("rewrite_fact using %s" % th_name)

        cv = then_conv(top_sweep_conv(rewr_conv(eq_pt, sym=self.sym, conds=pts[1:])),
                       beta_norm_conv())
        res = pts[0].on_prop(cv)
        if res == pts[0]:
            raise InvalidDerivationException("rewrite_fact using %s" % th_name)
        return res
Exemple #16
0
    def get_proof_term(self, thy, args, pts):
        assert isinstance(args, tuple) and len(args) == 2 and \
               isinstance(args[0], str) and isinstance(args[1], Term), "rewrite_goal_macro: signature"

        name, goal = args
        eq_pt = ProofTerm.theorem(thy, name)
        if self.backward:
            eq_pt = ProofTerm.symmetric(eq_pt)
        cv = then_conv(top_conv(rewr_conv(eq_pt)), top_conv(beta_conv()))
        pt = cv.get_proof_term(thy, goal)  # goal = th.prop
        pt = ProofTerm.symmetric(pt)  # th.prop = goal
        if Term.is_equals(pt.prop.lhs) and pt.prop.lhs.lhs == pt.prop.lhs.rhs:
            pt = ProofTerm.equal_elim(pt, ProofTerm.reflexive(pt.prop.lhs.lhs))
        else:
            pt = ProofTerm.equal_elim(pt, pts[0])  # goal
            pts = pts[1:]

        for A in pts:
            pt = ProofTerm.implies_elim(ProofTerm.implies_intr(A.prop, pt), A)
        return pt
Exemple #17
0
    def get_proof_term(self, thy, args, pts):
        assert isinstance(args, Term), "rewrite_goal_macro: signature"

        goal = args
        eq_pt = pts[0]
        pts = pts[1:]
        if self.backward:
            eq_pt = ProofTerm.symmetric(eq_pt)
        cv = then_conv(top_sweep_conv(rewr_conv(eq_pt, match_vars=False)),
                       top_conv(beta_conv()))
        pt = cv.get_proof_term(thy, goal)  # goal = th.prop
        pt = ProofTerm.symmetric(pt)  # th.prop = goal
        if Term.is_equals(pt.prop.lhs) and pt.prop.lhs.lhs == pt.prop.lhs.rhs:
            pt = ProofTerm.equal_elim(pt, ProofTerm.reflexive(pt.prop.lhs.rhs))
        else:
            pt = ProofTerm.equal_elim(pt, pts[0])
            pts = pts[1:]

        for A in pts:
            pt = ProofTerm.implies_elim(ProofTerm.implies_intr(A.prop, pt), A)
        return pt
Exemple #18
0
    def get_proof_term(self, thy, goal, args=None, prevs=None):
        th_name = args

        init_As = goal.hyps
        C = goal.prop
        cv = then_conv(top_conv(rewr_conv(th_name)), top_conv(beta_conv()))
        eq_th = cv.eval(thy, C)
        new_goal = eq_th.prop.rhs

        new_As = list(eq_th.hyps)
        new_As_pts = [ProofTerm.sorry(Thm(init_As, A)) for A in new_As]
        if Term.is_equals(new_goal) and new_goal.lhs == new_goal.rhs:
            return ProofTermDeriv('rewrite_goal',
                                  thy,
                                  args=(th_name, C),
                                  prevs=new_As_pts)
        else:
            new_goal_pts = ProofTerm.sorry(Thm(init_As, new_goal))
            return ProofTermDeriv('rewrite_goal',
                                  thy,
                                  args=(th_name, C),
                                  prevs=[new_goal_pts] + new_As_pts)
Exemple #19
0
    def get_proof_term(self, args, pts):
        assert len(pts) == 2, "rewrite_fact_with_prev"

        eq_pt, pt = pts

        # In general, we assume eq_pt has forall quantification
        # First, obtain the patterns
        new_names = get_forall_names(eq_pt.prop)
        new_vars, eq_As, eq_C = strip_all_implies(eq_pt.prop, new_names)

        # First fact must be an equality
        assert len(eq_As) == 0 and eq_C.is_equals(), "rewrite_fact_with_prev"

        for new_var in new_vars:
            eq_pt = eq_pt.forall_elim(new_var)

        # Check rewriting using eq_pt has an effect
        cv1 = top_sweep_conv(rewr_conv(eq_pt))
        assert not cv1.eval(pt.prop).is_reflexive(), "rewrite_fact_with_prev"

        cv = then_conv(cv1, beta_norm_conv())
        return pt.on_prop(cv)
Exemple #20
0
    def get_proof_term(self, args, pts):
        assert isinstance(args, tuple) and len(args) == 2 and \
               isinstance(args[0], str) and isinstance(args[1], Term), "rewrite_goal: signature"

        name, goal = args
        eq_pt = ProofTerm.theorem(name)

        if len(pts) == len(eq_pt.assums):
            rewr_cv = rewr_conv(eq_pt, sym=self.sym, conds=pts)
        else:
            assert len(pts) == len(eq_pt.assums) + 1, "rewrite_goal: wrong number of prevs"
            rewr_cv = rewr_conv(eq_pt, sym=self.sym, conds=pts[1:])

        cv = then_conv(top_sweep_conv(rewr_cv), beta_norm_conv())
        pt = cv.get_proof_term(goal)  # goal = th.prop
        pt = pt.symmetric()           # th.prop = goal
        if pt.prop.lhs.is_equals() and pt.prop.lhs.lhs == pt.prop.lhs.rhs:
            pt = pt.equal_elim(refl(pt.prop.lhs.lhs))
        else:
            pt = pt.equal_elim(pts[0])  # goal

        return pt
Exemple #21
0
    def search(self, state, id, prevs):
        if len(prevs) != 1:
            return []

        prev_th = state.get_proof_item(prevs[0]).th
        cur_th = state.get_proof_item(id).th

        if not prev_th.prop.is_equals():
            return []

        pt = ProofTermAtom(prevs[0], prev_th)
        cv = then_conv(top_sweep_conv(rewr_conv(pt, match_vars=False)),
                       top_conv(beta_conv()))
        eq_th = cv.eval(state.thy, cur_th.prop)
        new_goal = eq_th.prop.rhs

        new_As = list(set(eq_th.hyps) - set(cur_th.hyps))
        if cur_th.prop != new_goal:
            if Term.is_equals(new_goal) and new_goal.lhs == new_goal.rhs:
                return [{"_goal": new_As}]
            else:
                return [{"_goal": [new_goal] + new_As}]
        else:
            return []
Exemple #22
0
 def get_proof_term(self, thy, args, pts):
     eq_pt, pt = pts
     cv = then_conv(top_sweep_conv(rewr_conv(eq_pt, match_vars=False)),
                    top_conv(beta_conv()))
     return pt.on_prop(thy, cv)
Exemple #23
0

def Sem(T):
    return Const("Sem", TFun(comT(T), T, T, boolT))


def Valid(T):
    return Const("Valid", TFun(TFun(T, boolT), comT(T), TFun(T, boolT), boolT))


def Entail(T):
    return Const("Entail", TFun(TFun(T, boolT), TFun(T, boolT), boolT))


# Normalize evaluation of function as well as arithmetic.
norm_cv = then_conv(top_conv(function.fun_upd_eval_conv()), nat.norm_full())

# Normalize a condition.
norm_cond_cv = every_conv(norm_cv, top_conv(nat.nat_eq_conv()),
                          logic.norm_bool_expr())


def eval_Sem(thy, com, st):
    """Evaluates the effect of program com on state st."""
    f, args = com.strip_comb()
    T = st.get_type()
    if f.is_const_name("Skip"):
        return apply_theorem(thy, "Sem_Skip", tyinst={"a": T}, inst={"s": st})
    elif f.is_const_name("Assign"):
        a, b = args
        Ta = a.get_type()