Exemplo n.º 1
0
def samplePos(mod, candInv, coincide, actname):
    lcs = mod.labeled_conjs
    conjs = [Clauses([lc.formula]) for lc in lcs]
    fcs = [icheck.ConjChecker(c, invert=False) for c in lcs]
    negateci, negateCoincide = negate_clauses(candInv), negate_clauses(
        coincide)
    assert isinstance(negateci, Clauses) and isinstance(
        negateCoincide, Clauses), "negation causes type change"
    preclause = and_clauses(negateci, negateCoincide, *conjs)
    print "preclause: ", preclause
    print "not coincide: ", negateCoincide
    # print "<plearn> checking for action+ ", actname
    res = sampleUtil(mod, preclause, fcs, actname)
    a = raw_input('tried for pos sample')
    return res
Exemplo n.º 2
0
    def isValid(self):
        ''' Checks if the current instance is a valid samplePoint. 
			i.e. for negative sample it checks if the samplePoint satisfies negation of current invariant (fcs) in post state
			Assumptions : safety Condition and Candidate Inv and Coincide clause is Universally Quantified.
		'''
        global module, candInv, coincide
        if self.label == '0':  # nagative sample
            fcs = [icheck.ConjChecker(c)
                   for c in module.labeled_conjs]  # inverts the fmla
            fmlas = [
                fc.cond().fmlas for fc in fcs
            ]  # fc.cond().fmlas gives a list of ivy predicate logic object.
        else:  # positive sample
            # return True # It will cause repeatation of some positive samples, but it will not affect the correctness of algorithm
            # comment the above return stmt to check validity of positive samples
            negateci, negateCoincide = negate_clauses(candInv), negate_clauses(
                coincide)
            condition = and_clauses(negateci, negateCoincide)
            fmlas = [condition.fmlas]
            # assert len(fmlas) == 1, ""
        for fmla in fmlas:  # requires satisfying atleast one fmla, as they are in disjunction
            isfmlatrue = True
            for pred in fmla:
                ret = self.solveIvyfmla(pred)
                assert isinstance(
                    ret, logic.Const), "return value is not a Const object"
                assert isinstance(
                    ret.sort, BooleanSort), "return is not a boolean formla"
                assert ret.name in ["0", "1"
                                    ], "return value is not in correct format"
                # print "<plearn> pred: {} \t\t result: {}".format(pred, ret.name)
                if ret.name == "0":
                    isfmlatrue = False
                    break
            if isfmlatrue:
                return True
        return False
def clauses_imply_formula_cex(clauses, fmla):
    if clauses_imply_formula(clauses, fmla):
        return True
    return CounterExample(
        conjoin(clauses, negate_clauses(formula_to_clauses(fmla))))
Exemplo n.º 4
0
    """
    tactic_name = convert_from_camel_case(tactic_cls.__name__)
    tactic_label = tactic_name.replace('_', ' ')

    # register the tactic with the ui
    ui_extensions_api.goal_node_actions.register(lambda goal: [(
        tactic_label, ui_extensions_api.apply_goal_tactic, tactic_name)])

    # return unmodified
    return tactic_cls


if __name__ == '__main__':
    from proof import AnalysisSession

    session = AnalysisSession('../src/ivy/test.ivy')
    set_context(session)

    a = get_big_action()
    s0 = arg_initial_node()
    s1 = arg_add_action_node(s0, a)
    arg_is_covered(s1, s0)
    arg_add_facts(s0, arg_get_fact(s0))
    forward_image(arg_get_fact(s0), a)
    backward_image(arg_get_fact(s0), a)

    c = get_safety_property()
    g = goal_at_arg_node(negate_clauses(c), s1)
    d = get_diagram(g)
    x = refine_or_reverse(g)
Exemplo n.º 5
0
def clauses_imply_formula_cex(clauses,fmla):
    if clauses_imply_formula(clauses,fmla):
        return True
    return CounterExample(conjoin(clauses,negate_clauses(formula_to_clauses(fmla))))
Exemplo n.º 6
0
    tactic_name = convert_from_camel_case(tactic_cls.__name__)
    tactic_label = tactic_name.replace("_", " ")

    # register the tactic with the ui
    ui_extensions_api.goal_node_actions.register(
        lambda goal: [(tactic_label, ui_extensions_api.apply_goal_tactic, tactic_name)]
    )

    # return unmodified
    return tactic_cls


if __name__ == "__main__":
    from proof import AnalysisSession

    session = AnalysisSession("../src/ivy/test.ivy")
    set_context(session)

    a = get_big_action()
    s0 = arg_initial_node()
    s1 = arg_add_action_node(s0, a)
    arg_is_covered(s1, s0)
    arg_add_facts(s0, arg_get_fact(s0))
    forward_image(arg_get_fact(s0), a)
    backward_image(arg_get_fact(s0), a)

    c = get_safety_property()
    g = goal_at_arg_node(negate_clauses(c), s1)
    d = get_diagram(g)
    x = refine_or_reverse(g)
Exemplo n.º 7
0
Arquivo: tactics.py Projeto: asyaf/ivy
    def apply(self):
        frames = ta._ivy_ag.states
        bad_states = negate_clauses(get_safety_property())
        action = get_big_action()
        ta._ivy_ag.actions[repr(action)] = action

        init_frame = last_frame = frames[0]

        # TODO: test conjecture in initial

        with InterruptContext() as ic:
            while not ic.interrupted:

                # 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 check_cover(frames[i + 1], frames[i]):
                        return True

                # add new frame

                last_frame = ta.arg_add_action_node(last_frame, action, None)
                push_goal(goal_at_arg_node(bad_states, last_frame))
                self.step(label='new frame')

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

                while not ic.interrupted:
                    current_goal = top_goal()
                    if current_goal is None:
                        # goal stack is empty
                        break

                    if remove_if_refuted(current_goal):
                        continue

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

                    push_diagram(current_goal, False)

                    dg = top_goal()

                    if refine_or_reverse(dg, False):
                        pass
                        # dg is proved,

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

                    else:
                        # new goal pushed
                        pass

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

            assert ic.interrupted
            print "UPDR Interrupted by SIGINT"
            return None
Exemplo n.º 8
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))
Exemplo n.º 9
0
    def apply(self):
        frames = ta._ivy_ag.states
        bad_states = negate_clauses(get_safety_property())
        action = get_big_action()
        ta._ivy_ag.actions[repr(action)] = action

        init_frame = last_frame = frames[0]

        # TODO: test conjecture in initial

        with InterruptContext() as ic:
            while not ic.interrupted:

                # 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 check_cover(frames[i + 1], frames[i]):
                        return True

                # add new frame

                last_frame = ta.arg_add_action_node(last_frame, action, None)
                push_goal(goal_at_arg_node(bad_states, last_frame))
                self.step(label='new frame')

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

                while not ic.interrupted:
                    current_goal = top_goal()
                    if current_goal is None:
                        # goal stack is empty
                        break

                    if remove_if_refuted(current_goal):
                        continue

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

                    push_diagram(current_goal, False)

                    dg = top_goal()

                    if refine_or_reverse(dg, False):
                        pass
                        # dg is proved,

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

                    else:
                        # new goal pushed
                        pass

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

            assert ic.interrupted
            print "UPDR Interrupted by SIGINT"
            return None
Exemplo n.º 10
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))