Exemplo n.º 1
0
    def state_eq(vs1, vs2):
        """
        Generate a formula expressing the variables in vs1,vs2 are the same
        """
        if __debug__:
            assert is_list(vs1) and all(is_expr_var(v) for v in vs1), vs1
            assert is_list(vs2) and all(is_expr_var(v) for v in vs2), vs2
            assert len(vs1) == len(vs2)

        eqts = [v1 == v2 for v1, v2 in zip(vs1, vs2)]
        return myAnd(eqts)
Exemplo n.º 2
0
    def __init__(self, init_conds, defs, input_vars, assumes):
        """
        This class models a program using
        1. initial condition
        2. transition (definitions of updated variables)
        3. assumptions

        Input variables:
        - init_cond: list of initial conditions
        e.g. [Block == Off,Reset == On,WaterPres == wp_init_val,
        Overridden == False,SafetyInjection == On,Pressure == TooLow]

        - defs: a dictionary consisting variables being updated by
        the transition

        - input_vars: variables that are INDEPDENT.
        SCR programs don't have these, because input (monitored) vars
        are dependent due to OIA

        - assumes: list of assumptions
        Two types of assumptions:
        (1) state assumes: those for each *state*
        e.g.
        And(0 <= WaterPres,WaterPres < 2000): WaterPres is in range 0,2000
        at any state

        (2) trans assumes: those for each *transition*
        e.g.
        One Input Assumption asserts only 1 var can changed at a time
        or
        And(pre(WaterPres) - 10 <= WaterPres,
        WaterPres <= pre(WaterPres) + 10)
        """

        if __debug__:
            assert is_list(init_conds) and \
                all(is_state(c) for c in init_conds), init_conds

            assert is_dict(defs) and \
                all(is_expr(v) for v in defs.values()), defs

            assert is_list(input_vars) and \
                all(is_expr_var(v) for v in input_vars), input_vars

            assert is_list(assumes) and \
                all(is_expr(a) for a in assumes), assumes

        self.defs = defs
        self.init_conds = init_conds
        self.input_vars = input_vars

        self.assumes_state = []
        self.assumes_trans = []

        for a in assumes:
            Prog.append_f(a, self.assumes_state, self.assumes_trans)

        #Known invariants (lemmas). Use add_inv() to add an inv as lemma
        self.invs_state = []
        self.invs_trans = []
Exemplo n.º 3
0
def pre(v):
    """
    Return a new variable of the same sort as v called v_pre

    >>> from z3 import *
    >>> x = Bool('x')
    >>> pre(x)
    x_pre

    """

    if __debug__:
        assert is_expr_var(v) and not is_pre(v), v

    key_v = fhash(v)
    try:
        pre_v = pre_vars_dict[key_v]
    except KeyError:
        pre_v = mk_var(str(v) + pre_kw, v.sort())
        pre_vars_dict[key_v] = pre_v

        cur_vars_dict[fhash(pre_v)] = v


    assert pre_v.sort().kind() == v.sort().kind(), \
        'perhaps pre_vars_dict needs reset ?'

    return pre_v
Exemplo n.º 4
0
def cur_f(f):
    """
    Convert a formula f with pre vars to a formula with cur vars

    >>> from z3 import *
    >>> x,y,z = Ints('x y z')

    >>> cur_f(pre(z) == 9)
    z == 9

    >>> cur_f(And(pre(y) == 4, pre(z) == 9) )
    And(y == 4, z == 9)

    >>> cur_f(pre(y) == z)
    Traceback (most recent call last):
    ...
    AssertionError: y_pre == z
    """
    if __debug__:
        assert f is None or is_pre_f(f), f

    if f is None:
        return None
    if is_expr_var(f):
        return cur(f)
    else:
        vss = [(v, cur(v)) for v in get_vars(f)]
        return substitute(f, *vss)
Exemplo n.º 5
0
def cur(v):
    """
    Given a pre variable v, return the non-pre version of.
    For example, if v  =  myvar_pre, then cur(v) gives myvar
    """
    if __debug__:
        assert is_expr_var(v) and is_pre(v), v

    return cur_vars_dict[fhash(v)]
Exemplo n.º 6
0
def atC(v, when_c=None):
    """
    Analogous to atT

    Return And(pre(v) != v, when_c)

    EXAMPLES:

    >>> from z3 import *
    >>> x,y = Bools('x y')
    >>> atC(x)
    x_pre != x

    >>> atC(x,when_c = pre(x) == pre(y))
    And(x_pre != x, x_pre == y_pre)
    """
    if __debug__:
        assert is_expr_var(v) and not is_pre(v), v
        assert not when_c or is_pre_f(when_c), when_c

    f = myAnd(pre(v) != v, when_c)
    return f
Exemplo n.º 7
0
def pre_f(f):
    """
    Convert a formula f with cur vars to a formula with pre vars

    >>> from z3 import *
    >>> x,y,z = Ints('x y z')

    >>> pre_f(z == 9)
    z_pre == 9

    >>> pre_f(And(y == 4, z == 9) )
    And(y_pre == 4, z_pre == 9)

    """
    if __debug__:
        assert f is None or is_cur_f(f), f

    if f is None:
        return None
    elif is_expr_var(f):
        return pre(f)
    else:
        vss = [(v, pre(v)) for v in get_vars(f)]
        return substitute(f, *vss)
Exemplo n.º 8
0
    def get_influence_vs(v, defs, rs):
        """
        Return a list of variables that influences v
        (i.e. the definition of v depends on these variables)

        >>> from z3 import Bools, BoolVal
        >>> from scr_miscs import mk_OIA

        >>> s,t = Bools('s t')
        >>> x,y,z = Bools('x y z')
        >>> vs = [x,y,z]
        >>> o = mk_OIA(vs)
        >>> vv = [o,o,And(o,s)]
        >>> vs2 = [t]
        >>> vv2 = [BoolVal(True)]
        >>> vs_k = map(fhash,vs + vs2)
        >>> vs_v =vv + vv2
        >>> defs = OrderedDict(zip(vs_k,vs_v))
        >>> print Prog.get_influence_vs(x,defs,rs=[])
        [s, y, z]

        #>>> print Prog.get_influence_vs(x,defs,assumes=[x==s],rs=[])
        #[s, y, z]

        #>>> print Prog.get_influence_vs(x,defs,assumes=[y==t],rs=[])
        #[s, y, z, t]


        """
        if __debug__:
            assert is_expr_var(v), v
            assert is_dict(defs), defs
            assert is_list(rs), rs

        if is_pre(v):
            v = cur(v)

        #return if already in the result set
        if expr_member(v, rs):
            return rs

        try:
            vs = get_vars(defs[fhash(v)])
            #print vs

        except KeyError:
            return rs

        rs = rs + [v]

        #convert v_pre to v
        vs = [cur(v_) if is_pre(v_) else v_ for v_ in vs]
        vs = vset(vs, fhash)

        for v_ in vs:
            rs_ = Prog.get_influence_vs(v_, defs, rs)
            rs = rs + rs_

        rs = rs + vs
        rs = vset(rs, fhash)

        #remove myself
        v_idx = map(fhash, rs).index(fhash(v))
        rs = rs[:v_idx] + rs[v_idx + 1:]
        return sorted(rs, key=str)