Example #1
0
def model_if_none(clauses1, implied, model):
    h = model
    if h == None:
        s = z3.Solver()
        z3c = clauses_to_z3(clauses1)
        s.add(z3c)
        if implied != None:
            s.add(not_clauses_to_z3(implied))
        sort_size = 1
        while True:
            s.push()
            for sort in ivy_logic.uninterpreted_sorts():
                s.add(formula_to_z3(sort_size_constraint(sort, sort_size)))
            if s.check() != z3.unsat:
                m = get_model(s)
                print "model = {}, size = {}".format(m, sort_size)
                ##        print "clauses1 = {}".format(clauses1)
                ##        print "z3c = {}".format(str(z3c))
                syms = used_symbols_clauses(clauses1)
                if implied != None:
                    syms.update(used_symbols_clauses(implied))
                h = HerbrandModel(s, m, syms)
                s.pop()
                return h
            sort_size += 1
            s.pop()
    return h
Example #2
0
def rename_distinct(clauses1,clauses2):
    """ rename skolems in clauses1 so they don't occur in clauses2.
    """
#    print "rename_distinct clauses1 = {}".format(clauses1)
#    print "rename_distinct clauses2 = {!r}".format(clauses2)
    used1 = used_symbols_clauses(clauses1)
    used2 = used_symbols_clauses(clauses2)
    rn = UniqueRenamer('',used2)
    map1 = dict()
    for s in used1:
        if is_skolem(s):
            map1[s] = rename(s,rn)
    return rename_clauses(clauses1,map1)
Example #3
0
def get_small_model(clauses, sorts_to_minimize, relations_to_minimize):
    """
    Return a HerbrandModel with a "small" model of clauses.

    sorts_to_minimize is a list of sorts, and relations_to_minimize is
    a list of relations,

    The model minimization occurs in 2 ways:

    First, minimize universe size lexicographically according to the order of
    sorts_to_minimize.

    Second, minimize the number of positive entries in the relations
    according to the order of relations_to_minimize.
    """
    s = z3.Solver()
    s.add(clauses_to_z3(clauses))

    if s.check() == z3.unsat:
        return None

    print "shrinking model {"
    for x in chain(sorts_to_minimize, relations_to_minimize):
        for n in itertools.count(1):
            s.push()
            s.add(formula_to_z3(size_constraint(x, n)))
            if s.check() == z3.sat:
                break
            else:
                s.pop()
    print "} shrinking model"
    m = get_model(s)
    h = HerbrandModel(s,m,used_symbols_clauses(clauses))
    return h
Example #4
0
def exist_quant_map(syms,clauses):
    used = used_symbols_clauses(clauses)
    rn = UniqueRenamer('__',used)
    map1 = dict()
    for s in syms:
        map1[s] = rename(s,rn)
    return map1,rename_clauses(clauses,map1)
Example #5
0
def compose_updates(update1,axioms,update2):
    updated1, clauses1, pre1 = update1
    updated2, clauses2, pre2 = update2
    clauses2 = rename_distinct(clauses2,clauses1)
    pre2 = rename_distinct(pre2,clauses1)
#    print "clauses2 = {}".format(clauses2)
    us1 = set(updated1)
    us2 = set(updated2)
    mid = us1.intersection(us2)
    mid_ax = clauses_using_symbols(mid,axioms)
    used = used_symbols_clauses(and_clauses(clauses1,clauses2))
    rn = UniqueRenamer('__m_',used)
    map1 = dict()
    map2 = dict()
    for v in updated1:
        map2[v] = new(v)
    for mv in mid:
        mvf = rename(mv,rn)
        map1[new(mv)] = mvf
        map2[mv] = mvf
    clauses1 = rename_clauses(clauses1,map1)
    new_clauses = and_clauses(clauses1, rename_clauses(and_clauses(clauses2,mid_ax),map2))
    new_updated = list(us1.union(us2))
#    print "pre1 before = {}".format(pre1)
    pre1 = and_clauses(pre1,diff_frame(updated1,updated2,None,new))  # keep track of post-state of assertion failure
#    print "pre1 = {}".format(pre1)
    new_pre = or_clauses(pre1,and_clauses(clauses1,rename_clauses(and_clauses(pre2,mid_ax),map2)))
#    print "new_pre = {}".format(new_pre)
    return (new_updated,new_clauses,new_pre)
Example #6
0
def get_model_clauses(clauses1):
    s = z3.Solver()
    z3c = clauses_to_z3(clauses1)
    s.add(z3c)
    if s.check() == z3.unsat:
        return None
    m = get_model(s)
    return HerbrandModel(s,m,used_symbols_clauses(clauses1))
Example #7
0
def action_to_state(update):
    """ convert from the "action" style to the "state" style """
    updated,tr,pre = update
    renaming = dict()
    for s in updated:
        renaming[s] = old(s)
    for s in used_symbols_clauses(tr):
        if is_new(s):
            renaming[s] = new_of(s)
    return (updated,rename_clauses(tr,renaming),pre)
Example #8
0
def action_to_state(update):
    """ convert from the "action" style to the "state" style """
    assert isinstance(state,SemActionValue)
    updated,tr,pre = update.comps
    renaming = dict()
    for s in updated:
        renaming[s] = old(s)
    for s in used_symbols_clauses(tr):
        if is_new(s):
            renaming[s] = new_of(s)
    return SemStateValue(updated,rename_clauses(tr,renaming),pre)
Example #9
0
def get_model_clauses(clauses1):
    s = z3.Solver()
    z3c = clauses_to_z3(clauses1)
    s.add(z3c)
    iu.dbg('"before check"')
    res = s.check()
    iu.dbg('"after check"')
    if res == z3.unsat:
        return None
    m = get_model(s)
    return HerbrandModel(s,m,used_symbols_clauses(clauses1))
Example #10
0
def state_to_action(update):
    """ convert from the "state" style to the "action" style """
    updated,postcond,pre = update
    postcond,pre = clausify(postcond), clausify(pre)
    renaming = dict()
    for s in updated:
        renaming[s] = new(s)
    for s in used_symbols_clauses(postcond):
        if is_old(s):
            renaming[s] = old_of(s)
    return (updated,rename_clauses(postcond,renaming),pre)
Example #11
0
def state_to_action(update):
    """ convert from the "state" style to the "action" style """
    assert isinstance(update,SemStateValue)
    updated,postcond,pre = update.comps
    postcond,pre = clausify(postcond), clausify(pre)
    renaming = dict()
    for s in updated:
        renaming[s] = new(s)
    for s in used_symbols_clauses(postcond):
        if is_old(s):
            renaming[s] = old_of(s)
    return SemActionValue(updated,rename_clauses(postcond,renaming),pre)
Example #12
0
    def witness(self,node):
        g = self
        for lit in node.fmla:
#            print lit
            if is_equality_lit(lit) and isinstance(lit.atom.args[0],Variable):
                self.add_witness_constraint(node,lit.atom.args[1])
                return lit.atom.args[1]
        uc = used_symbols_clauses(g.state)
        fmlas = [n.fmla for n in g.all_nodes]
        for f in fmlas:
            uc.update(used_symbols_clause(f))
        nc = unused_constant(uc,node.sort)
#        print "type(nc) = {}".format(type(nc))
        self.add_witness_constraint(node,nc)
        self.split(node,eq_lit(Variable('X',node.sort),nc))
        return nc 
Example #13
0
    def minimize_conjecture(self, button=None, bound=None):
        import ivy_transrel
        import ivy_solver
        from proof import ProofGoal
        from ivy_logic_utils import Clauses, and_clauses, dual_clauses, used_symbols_clauses, negate
        from ivy_solver import unsat_core
        from logic_util import free_variables, substitute

        if self.bmc_conjecture(bound=bound):
            # found a BMC counter-example
            return

        with self.ui_parent.run_context():
            step_action = im.module.actions['ext']

            n_steps = self.current_bound

            ag = self.parent.new_ag()
            with ag.context as ac:
                post = ac.new_state(ag.init_cond)
            if 'initialize' in im.module.actions:
                init_action = im.module.actions['initialize']
                post = ag.execute(init_action, None, None, 'initialize')
            for n in range(n_steps):
                post = ag.execute(step_action, None, None, 'ext')
            axioms = im.module.background_theory()
            post_clauses = and_clauses(post.clauses, axioms)

            used_names = (
                frozenset(x.name for x in il.sig.symbols.values()) |
                frozenset(x.name for x in used_symbols_clauses(post_clauses))
            )
            facts = self.get_active_facts()
            assert not any(
                c.is_skolem() and c.name in used_names for c in lu.used_constants(*facts)
            )
            core = unsat_core(Clauses(facts), post_clauses)
            if core is None:
                core = Clauses([]) ## can happen if we are proving true
#            assert core is not None, "bmc_conjecture returned False but unsat core is None"
            core_formulas = frozenset(core.fmlas)
            self.set_facts([fact for fact in facts if fact in core_formulas])
            self.highlight_selected_facts()
            self.ui_parent.text_dialog("BMC found the following possible conjecture:",
                                       str(self.get_selected_conjecture()))
Example #14
0
def compose_updates(update1,axioms,update2):
    updated1, clauses1, pre1 = update1
    updated2, clauses2, pre2 = update2
    clauses2 = rename_distinct(clauses2,clauses1)
    pre2 = rename_distinct(pre2,clauses1)
#    print "clauses2 = {}".format(clauses2)
    us1 = set(updated1)
    us2 = set(updated2)
    mid = us1.intersection(us2)
    mid_ax = clauses_using_symbols(mid,axioms)
    used = used_symbols_clauses(and_clauses(clauses1,clauses2))
    rn = UniqueRenamer('__m_',used)
    map1 = dict()
    map2 = dict()
    for v in updated1:
        map2[v] = new(v)
    for mv in mid:
        mvf = rename(mv,rn)
        map1[new(mv)] = mvf
        map2[mv] = mvf
#    iu.dbg('clauses1')
#    iu.dbg('clauses1.annot')
    clauses1 = rename_clauses(clauses1,map1)
    annot_op = lambda x,y: x.compose(y) if x is not None and y is not None else None
    new_clauses = and_clauses(clauses1, rename_clauses(and_clauses(clauses2,mid_ax),map2),annot_op=annot_op)
    new_updated = list(us1.union(us2))
#    print "pre1 before = {}".format(pre1)
#    iu.dbg('pre1.annot')
#    iu.dbg('pre1')
    pre1 = and_clauses(pre1,diff_frame(updated1,updated2,None,new))  # keep track of post-state of assertion failure
#    print "pre1 = {}".format(pre1)
    temp = and_clauses(clauses1,rename_clauses(and_clauses(pre2,mid_ax),map2),annot_op=my_annot_op)
#    iu.dbg('temp.annot')
    new_pre = or_clauses(pre1,temp)
#    iu.dbg('new_pre.annot')
#    print "new_pre = {}".format(new_pre)
#    iu.dbg('new_clauses')
#    iu.dbg('new_clauses.annot')
    return (new_updated,new_clauses,new_pre)
Example #15
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
Example #16
0
def bind_olds_clauses(clauses):
    subst = dict((s,old_of(s)) for s in used_symbols_clauses(clauses) if is_old(s))
    return rename_clauses(clauses,subst)
Example #17
0
def forward_clauses(clauses,inflex):
    return lu.rename_clauses(clauses, dict((x,tr.new(x)) for x in lu.used_symbols_clauses(clauses)
        if x != '=' and x not in inflex))