Exemple #1
0
def reach_state(state, clauses=None):
    """ Try to reach a state in one step from it's predecessor's
    underapproximation. If reachable, update the state's
    underapproximation with some known reachable state.  Return the
    reachable state. Else return None.
    """
    if not (state.pred != None and state.update != None):
        return None
    pre = join_unders(state.pred)
    if clauses == None:
        clauses = state.clauses
    print "reach_state: clauses = {}".format(clauses)
    axioms = state.domain.background_theory(state.in_scope)
    img = and_clauses(forward_image(pre, axioms, state.update), axioms,
                      clauses)
    m = get_model_clauses(img)
    ignore = lambda s, d=state.domain: s not in d.relations and s not in d.functions
    if m:
        #        print "reach_state model = {}".format(m.model)
        idx = find_true_disjunct(pre, m.eval)
        post = clauses_model_to_clauses(img, ignore, model=m)
        return add_under(
            state, post, state.unders[idx],
            dict((s, [c.skolem() for c in m.sort_universe(s)])
                 for s in m.sorts()))
    return None
Exemple #2
0
def forward_image(pre_fact, action):
    """
    """
    axioms = _ivy_interp.background_theory()
    return ivy_transrel.forward_image(
        pre_fact,
        axioms,
        action.update(_ivy_interp, None),
    )
Exemple #3
0
def forward_image(pre_fact, action):
    """
    """
    axioms = _ivy_interp.background_theory()
    return ivy_transrel.forward_image(
        pre_fact,
        axioms,
        action.update(_ivy_interp, None),
    )
Exemple #4
0
def reach_state(state,clauses=None):
    """ Try to reach a state in one step from it's predecessor's
    underapproximation. If reachable, update the state's
    underapproximation with some known reachable state.  Return the
    reachable state. Else return None.
    """
    if not(state.pred != None and state.update != None):
        return None
    pre = join_unders(state.pred)
    if clauses == None:
        clauses = state.clauses
    print "reach_state: clauses = {}".format(clauses)
    axioms = state.domain.background_theory(state.in_scope)
    img = and_clauses(forward_image(pre,state.update),axioms,clauses)
    m = get_model_clauses(img)
    ignore = lambda s,d=state.domain: s not in d.relations and s not in d.functions
    if m:
#        print "reach_state model = {}".format(m.model)
        idx = find_true_disjunct(pre,m.eval)
        post = clauses_model_to_clauses(img,ignore,model=m)
        return add_under(state,post,state.unders[idx],dict((s,[c.skolem() for c in m.sort_universe(s)]) for s in m.sorts()))
    return None
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 #6
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))