def interp_from_unsat_core(clauses1, clauses2, core, interpreted):
    used_syms = used_symbols_clauses(core)
    vars = used_variables_clauses(core)
    if vars:
        #        print "interpolant would require skolem constants"
        return None
    core_consts = used_constants_clauses(core)
    clauses2_consts = used_constants_clauses(clauses2)
    #    print "interp_from_unsat_core core_consts = {}".format(map(str,core_consts))
    #    print "interp_from_unsat_core clauses2_consts = {}".format(map(str,clauses2_consts))
    renaming = dict()
    i = 0
    for v in core_consts:
        if v not in clauses2_consts or v.is_skolem(
        ):  # and v not in interpreted:
            renaming[v] = Variable('V' + str(i), Constant(v).get_sort())
            i += 1


#    print "interp_from_unsat_core core = {}".format(core)
#    print "interp_from_unsat_core renaming = {}".format(renaming)
    renamed_core = substitute_constants_clauses(core, renaming)
    #    print "interp_from_unsat_core renamed_core = {}".format(renamed_core)
    res = simplify_clauses(
        Clauses([Or(*[negate(c) for c in renamed_core.fmlas])]))
    #    print "interp_from_unsat_core res = {}".format(res)
    return res
Exemple #2
0
def interpolant(clauses1,clauses2,axioms,interpreted):
#    print "interpolant clauses1={} clauses2={}".format(clauses1,clauses2)
#    print "axioms = {}".format(axioms)
    foo = and_clauses(clauses1,axioms)
    clauses2 = simplify_clauses(clauses2)
    core = unsat_core(clauses2,foo)
    if core == None:
        return None
#    print "core: %s" % core
    return core, interp_from_unsat_core(clauses2,foo,core,interpreted)
    def get_selected_conjecture(self):
        """
        Return a positive universal conjecture based on the selected facts.

        The result is a Clauses object
        """
        from logic_util import used_constants, free_variables, substitute
        from ivy_logic_utils import negate, Clauses, simplify_clauses

        facts = self.get_active_facts()
        assert len(free_variables(
            *
            facts)) == 0, "conjecture would contain existential quantifiers..."
        sig_symbols = frozenset(il.sig.symbols.values())
        facts_consts = used_constants(*facts)
        subs = {}
        rn = iu.VariableGenerator()
        for c in sorted(facts_consts, key=lambda c: c.name):
            if c.is_numeral() and il.is_uninterpreted_sort(c.sort):
                #                prefix = str(c.sort)[:2].upper() + c.name
                subs[c] = lg.Var(rn(c.sort.name), c.sort)

        literals = [negate(substitute(f, subs)) for f in facts]
        result = Clauses([lg.Or(*literals)])
        result = simplify_clauses(result)

        # now rename again to get a pretty clause, since some
        # variables have been eliminated by simplify_clauses
        # assert len(result.fmlas) == 1
        # clause = result.fmlas[0]
        # subs = {}
        # count = defaultdict(int)
        # for c in free_variables(clause):
        #     prefix = str(c.sort)[0].upper()
        #     count[prefix] += 1
        #     subs[c] = lg.Var(prefix + str(count[prefix]), c.sort)
        # result = Clauses([substitute(clause, subs)])

        # change to negation of conjunction rather than disjunction
        assert len(result.fmlas) == 1
        if type(result.fmlas[0]) is lg.Or:
            result = Clauses(
                [lg.Not(lg.And(*(negate(lit) for lit in result.fmlas[0])))])

        return result
Exemple #4
0
    def get_selected_conjecture(self):
        """
        Return a positive universal conjecture based on the selected facts.

        The result is a Clauses object
        """
        from logic_util import used_constants, free_variables, substitute
        from ivy_logic_utils import negate, Clauses, simplify_clauses

        facts = self.get_active_facts()
        assert len(free_variables(*facts)) == 0, "conjecture would contain existential quantifiers..."
        sig_symbols = frozenset(il.sig.symbols.values())
        facts_consts = used_constants(*facts)
        subs = {}
        rn = iu.UniqueRenamer()
        for c in sorted(facts_consts, key=lambda c: c.name):
            if c.is_numeral() and il.is_uninterpreted_sort(c.sort):
                prefix = str(c.sort)[:2].upper() + str(c)
                subs[c] = lg.Var(rn(prefix), c.sort)

        literals = [negate(substitute(f, subs)) for f in facts]
        result = Clauses([lg.Or(*literals)])
        result = simplify_clauses(result)

        # now rename again to get a pretty clause, since some
        # variables have been eliminated by simplify_clauses
        # assert len(result.fmlas) == 1
        # clause = result.fmlas[0]
        # subs = {}
        # count = defaultdict(int)
        # for c in free_variables(clause):
        #     prefix = str(c.sort)[0].upper()
        #     count[prefix] += 1
        #     subs[c] = lg.Var(prefix + str(count[prefix]), c.sort)
        # result = Clauses([substitute(clause, subs)])

        # change to negation of conjunction rather than disjunction
        assert len(result.fmlas) == 1
        if type(result.fmlas[0]) is lg.Or:
            result = Clauses([lg.Not(lg.And(*(negate(lit) for lit in result.fmlas[0])))])

        return result
Exemple #5
0
def interp_from_unsat_core(clauses1,clauses2,core,interpreted):
    used_syms = used_symbols_clauses(core)
    vars = used_variables_clauses(core)
    if vars:
#        print "interpolant would require skolem constants"
        return None
    core_consts = used_constants_clauses(core)
    clauses2_consts = used_constants_clauses(clauses2)
#    print "interp_from_unsat_core core_consts = {}".format(map(str,core_consts))
#    print "interp_from_unsat_core clauses2_consts = {}".format(map(str,clauses2_consts))
    renaming = dict()
    i = 0
    for v in core_consts:
        if v not in clauses2_consts or v.is_skolem(): # and v not in interpreted:
            renaming[v] = Variable('V' + str(i),Constant(v).get_sort())
            i += 1
#    print "interp_from_unsat_core core = {}".format(core)
#    print "interp_from_unsat_core renaming = {}".format(renaming)
    renamed_core = substitute_constants_clauses(core,renaming)
#    print "interp_from_unsat_core renamed_core = {}".format(renamed_core)
    res = simplify_clauses(Clauses([Or(*[negate(c) for c in renamed_core.fmlas])]))
#    print "interp_from_unsat_core res = {}".format(res)
    return res
Exemple #6
0
    print "lsyms = {}".format(lsyms)
    print "gsyms = {}".format(gsyms)

    init = state.clauses
    init = forward_clauses(init, inflex)
    print "init = {}".format(init)

    error = forward_clauses(error, inflex)
    print "error = {}".format(error)

    axioms += forward_clauses(axioms, inflex)

    init_z3 = sv.clauses_to_z3(init)
    print "init_z3 = {}".format(init_z3)
    for a in updates:
        print lu.simplify_clauses(a[1])
    rho_z3 = z3.Or(
        *[sv.clauses_to_z3(lu.simplify_clauses(a[1])) for a in updates])
    print "rho_z3 = {}".format(rho_z3)
    bad_z3 = sv.clauses_to_z3(error)
    print "bad_z3 = {}".format(bad_z3)
    background_z3 = sv.clauses_to_z3(axioms)
    print "background_z3 = {}".format(background_z3)
    #relations_z3 = [ns(rel) for rel in sig.relations if tr.is_new(rel) or rel in inflex]
    relations_z3 = [
        x for x in gsyms
        if not z3.is_const(x) or (z3.is_const(x) and x.sort() == z3.BoolSort())
    ]
    relations_z3 += [
        x for _, x in lsyms
        if not z3.is_const(x) or (z3.is_const(x) and x.sort() == z3.BoolSort())
def interactive_updr():
    frames = ta._ivy_ag.states
    if len(frames) != 1:
        raise InteractionError(
            "Interactive UPDR can only be started when the ARG " +
            "contains nothing but the initial state.")

    bad_states = negate_clauses(ta.get_safety_property())
    action = ta.get_big_action()
    ta._ivy_ag.actions[repr(action)] = action

    init_frame = last_frame = frames[0]

    # TODO: test conjecture in initial

    while True:

        # the property is true in all frames and all "clauses" are pushed
        # the goal stack is empty

        # check if we found an infuctive invariant
        for i in range(len(frames) - 1):
            if t.check_cover(frames[i + 1], frames[i]):
                ta.step(msg="Inductive invariant found at frame {}".format(i),
                        i=i)
                # return True

        # add new frame

        last_frame = ta.arg_add_action_node(last_frame, action, None)
        ta.push_goal(ta.goal_at_arg_node(bad_states, last_frame))
        ta.step(msg="Added new frame")

        # push facts to last frame
        t.recalculate_facts(last_frame,
                            ta.arg_get_conjuncts(ta.arg_get_pred(last_frame)))

        while True:
            current_goal = ta.top_goal()
            if current_goal is None:
                # goal stack is empty
                break

            if t.remove_if_refuted(current_goal):
                continue

            if current_goal.node == init_frame:
                # no invariant
                print "No Invariant!"
                # return False

            dg = ta.get_diagram(current_goal, False)
            options = OrderedDict()
            for c in simplify_clauses(dg.formula).conjuncts():
                options[str(c)] = c
            user_selection = (yield UserSelectMultiple(
                options=options,
                title="Generalize Diagram",
                prompt="Choose which literals to take as the refutation goal",
                default=options.values()))
            assert user_selection is not None
            ug = ta.goal_at_arg_node(Clauses(list(user_selection)),
                                     current_goal.node)
            ta.push_goal(ug)
            ta.step(msg='Pushed user selected goal', ug=ug)

            goal = ta.top_goal()
            preds, action = ta.arg_get_preds_action(goal.node)
            assert action != 'join'
            assert len(preds) == 1
            pred = preds[0]
            axioms = ta._ivy_interp.background_theory()
            theory = and_clauses(
                ivy_transrel.forward_image(pred.clauses, axioms,
                                           action.update(ta._ivy_interp,
                                                         None)), axioms)
            goal_clauses = simplify_clauses(goal.formula)
            assert len(goal_clauses.defs) == 0

            s = z3.Solver()
            s.add(clauses_to_z3(theory))
            s.add(clauses_to_z3(goal_clauses))
            is_sat = s.check()
            if is_sat == z3.sat:
                bi = ta.backward_image(goal.formula, action)
                x, y = False, ta.goal_at_arg_node(bi, pred)
            elif is_sat == z3.unsat:
                user_selection, user_is_sat = yield UserSelectCore(
                    theory=theory,
                    constrains=goal_clauses.fmlas,
                    title="Refinement",
                    prompt="Choose the literals to use",
                )
                assert user_is_sat is False
                core = Clauses(user_selection)
                x, y = True, ivy_transrel.interp_from_unsat_core(
                    goal_clauses, theory, core, None)
            else:
                assert False, is_sat

            t.custom_refine_or_reverse(goal, x, y, False)

        # propagate phase
        for i in range(1, len(frames)):
            facts_to_check = (set(ta.arg_get_conjuncts(frames[i - 1])) -
                              set(ta.arg_get_conjuncts(frames[i])))
            t.recalculate_facts(frames[i], list(facts_to_check))
Exemple #8
0
    print "lsyms = {}".format(lsyms)
    print "gsyms = {}".format(gsyms)

    init = state.clauses
    init = forward_clauses(init,inflex)
    print "init = {}".format(init)

    error = forward_clauses(error,inflex)
    print "error = {}".format(error)

    axioms += forward_clauses(axioms,inflex)

    init_z3 = sv.clauses_to_z3(init)
    print "init_z3 = {}".format(init_z3)
    for a in updates:
        print lu.simplify_clauses(a[1])
    rho_z3 = z3.Or(*[sv.clauses_to_z3(lu.simplify_clauses(a[1])) for a in updates])
    print "rho_z3 = {}".format(rho_z3)
    bad_z3 = sv.clauses_to_z3(error)
    print "bad_z3 = {}".format(bad_z3)
    background_z3 = sv.clauses_to_z3(axioms)
    print "background_z3 = {}".format(background_z3)
    #relations_z3 = [ns(rel) for rel in sig.relations if tr.is_new(rel) or rel in inflex]
    relations_z3 =  [ x for x in gsyms if not z3.is_const(x) or (z3.is_const(x) and x.sort() == z3.BoolSort()) ]
    relations_z3 += [ x for _,x in lsyms if not z3.is_const(x) or (z3.is_const(x) and x.sort() == z3.BoolSort()) ]
    print "relations_z3 = {}".format(relations_z3)

    ### HACK: remove skolem predicates from the list of predicate symbols
    #relations_z3 = [ x for x in relations_z3 if not x in skolems ]

    pdr = pd.PDR(init_z3, rho_z3, bad_z3, background_z3, gsyms, lsyms, relations_z3, True)
Exemple #9
0
def interactive_updr():
    frames = ta._ivy_ag.states
    if len(frames) != 1:
        raise InteractionError(
            "Interactive UPDR can only be started when the ARG " + "contains nothing but the initial state."
        )

    bad_states = negate_clauses(ta.get_safety_property())
    action = ta.get_big_action()
    ta._ivy_ag.actions[repr(action)] = action

    init_frame = last_frame = frames[0]

    # TODO: test conjecture in initial

    while True:

        # the property is true in all frames and all "clauses" are pushed
        # the goal stack is empty

        # check if we found an infuctive invariant
        for i in range(len(frames) - 1):
            if t.check_cover(frames[i + 1], frames[i]):
                ta.step(msg="Inductive invariant found at frame {}".format(i), i=i)
                # return True

        # add new frame

        last_frame = ta.arg_add_action_node(last_frame, action, None)
        ta.push_goal(ta.goal_at_arg_node(bad_states, last_frame))
        ta.step(msg="Added new frame")

        # push facts to last frame
        t.recalculate_facts(last_frame, ta.arg_get_conjuncts(ta.arg_get_pred(last_frame)))

        while True:
            current_goal = ta.top_goal()
            if current_goal is None:
                # goal stack is empty
                break

            if t.remove_if_refuted(current_goal):
                continue

            if current_goal.node == init_frame:
                # no invariant
                print "No Invariant!"
                # return False

            dg = ta.get_diagram(current_goal, False)
            options = OrderedDict()
            for c in simplify_clauses(dg.formula).conjuncts():
                options[str(c)] = c
            user_selection = (
                yield UserSelectMultiple(
                    options=options,
                    title="Generalize Diagram",
                    prompt="Choose which literals to take as the refutation goal",
                    default=options.values(),
                )
            )
            assert user_selection is not None
            ug = ta.goal_at_arg_node(Clauses(list(user_selection)), current_goal.node)
            ta.push_goal(ug)
            ta.step(msg="Pushed user selected goal", ug=ug)

            goal = ta.top_goal()
            preds, action = ta.arg_get_preds_action(goal.node)
            assert action != "join"
            assert len(preds) == 1
            pred = preds[0]
            axioms = ta._ivy_interp.background_theory()
            theory = and_clauses(
                ivy_transrel.forward_image(pred.clauses, axioms, action.update(ta._ivy_interp, None)), axioms
            )
            goal_clauses = simplify_clauses(goal.formula)
            assert len(goal_clauses.defs) == 0

            s = z3.Solver()
            s.add(clauses_to_z3(theory))
            s.add(clauses_to_z3(goal_clauses))
            is_sat = s.check()
            if is_sat == z3.sat:
                bi = ta.backward_image(goal.formula, action)
                x, y = False, ta.goal_at_arg_node(bi, pred)
            elif is_sat == z3.unsat:
                user_selection, user_is_sat = yield UserSelectCore(
                    theory=theory,
                    constrains=goal_clauses.fmlas,
                    title="Refinement",
                    prompt="Choose the literals to use",
                )
                assert user_is_sat is False
                core = Clauses(user_selection)
                x, y = True, ivy_transrel.interp_from_unsat_core(goal_clauses, theory, core, None)
            else:
                assert False, is_sat

            t.custom_refine_or_reverse(goal, x, y, False)

        # propagate phase
        for i in range(1, len(frames)):
            facts_to_check = set(ta.arg_get_conjuncts(frames[i - 1])) - set(ta.arg_get_conjuncts(frames[i]))
            t.recalculate_facts(frames[i], list(facts_to_check))