Esempio n. 1
0
def to_guard(p, assume):
    program = parser.parse(p)
    global_defs, products, ltl_blocks = program.to_table()
    (ltl, ) = ltl_blocks
    f = ltl.formula
    t = logic.Table()
    logic.add_variables_to_table(t, global_defs, pid=0, assume_context='sys')
    guard, primed = f.to_guard(t,
                               pid=0,
                               assume=assume,
                               primed=False,
                               negated=False)
    return guard, primed
Esempio n. 2
0
def test_collect_primed_vars():
    # add var to symbol table
    pid = 'global'
    player = 'sys'
    t = logic.Table()
    t.add_var(pid, 'y', 'y', 'bool', 'bool', True, player)
    # primed var
    e = expr_parser.parse("y' < 2")
    primed = logic.collect_primed_vars(e.expr, t, pid, player)
    (r, ) = primed
    scope, node = r
    assert scope == 'global', scope
    assert str(node) == 'y', node
    # prefix operator for "next"
    e = expr_parser.parse("(X y) < 2")
    primed = logic.collect_primed_vars(e.expr, t, pid, player)
    (r, ) = primed
    scope, node = r
    assert scope == 'global', scope
    assert str(node) == 'y', node
Esempio n. 3
0
def scaffold():
    e = "(x == y)'"
    tree = expr_parser.parse(e).expr
    print(repr(tree))
    t = logic.Table()
    t.add_var(pid=0,
              name='x',
              flatname='pid0_x',
              dom='bool',
              free=False,
              owner='sys')
    t.add_var(pid=0,
              name='y',
              flatname='pid0_y',
              dom='bool',
              free=False,
              owner='env')
    if logic.collect_primed_vars(tree, t, pid=0, player='sys'):
        print('has next var')
    else:
        print('does not have next var')
Esempio n. 4
0
def test_constrain_global_declarative_vars():
    t = logic.Table()
    y = logic.AST.VarDef('y', 'bool', owner='env', free=True)
    y.insert_logic_var(t, 'sys', 'global')
    z = logic.AST.VarDef('z', 'bool', owner='env', free=True)
    z.insert_logic_var(t, 'sys', 'global')
    w = logic.AST.VarDef('w', 'bool', owner='sys', free=True)
    w.insert_logic_var(t, 'sys', 'global')
    # global_defs = [y, z, w]
    r = logic.freeze_declarative_vars(t, 'env')
    s = ('(((X pidglobal_y) <-> pidglobal_y)) &'
         ' (((X pidglobal_z) <-> pidglobal_z))')
    assert r == s, r
    # env must freeze
    c = '''
    free env bit x;

    proctype foo(){
        do
        :: atomic{ skip; x' == x }
        od
    }
    '''
    assert logic.synthesize(c)
Esempio n. 5
0
def test_else():
    c = '''
    bit x;
    bit y;

    active sys proctype foo(){
        do
        :: x == 0
        :: y == 1
        :: else
        od
    }
    '''
    program = parser.parse(c)
    vardefs, groups, ltlblocks = program.to_table()
    t = logic.Table()
    logic.add_variables_to_table(t,
                                 vardefs,
                                 pid='global',
                                 assume_context='sys')
    (proc, ) = groups
    g = proc.to_pg()
    for u, v, d in g.edges_iter(data=True):
        c = d['stmt']
        if not isinstance(c, logic.AST.Else):
            continue
        print c.to_logic(t, pid=0, assume='sys')
        print c.to_guard(t, pid=0, assume='sys')
    # realizability
    c = '''
    bit x;
    bit y;

    active sys proctype foo(){
        do
        :: true;
        :: else; false
        od
    }
    '''
    r = logic.synthesize(c)
    assert r
    c = '''
    bit x;
    bit y;

    active sys proctype foo(){
        do
        :: true; false
        :: else;
        od
    }
    '''
    r = logic.synthesize(c)
    assert not r
    c = '''
    bit x;
    bit y;

    active sys proctype foo(){
        do
        :: false;
        :: else;
        od
    }
    '''
    r = logic.synthesize(c)
    assert r
    c = '''
    bit x;
    bit y;

    active sys proctype foo(){
        do
        :: false;
        :: else; false
        od
    }
    '''
    r = logic.synthesize(c)
    assert not r
Esempio n. 6
0
def test_array():
    # single array parsed
    c = '''sys int(0, 3) x[3];'''
    program = parser.parse(c)
    (x, ), _, ltlblocks = program.to_table()
    assert x.length == 3, x.length
    # single array inserted to table
    t = logic.Table()
    x.insert_logic_var(t, assume_context='sys', pid='global')
    assert 'x' in t.scopes['global'], t
    d = t.scopes['global']['x']
    assert d['length'] == 3, d
    # array ref with constant index
    c = '''
    sys int(0, 3) x[4];

    ltl { x[2] == 0}
    '''
    program = parser.parse(c)
    vardefs, _, ltlblocks = program.to_table()
    t = logic.Table()
    logic.add_variables_to_table(t,
                                 vardefs,
                                 pid='global',
                                 assume_context='sys')
    ltl = next(iter(ltlblocks))
    f = ltl.formula
    s, context = f.to_logic(t, pid='global')
    assert context == 'bool', context
    assert s == '(pidglobal_x2 = 0)', s
    s, pr = f.to_guard(t,
                       pid='global',
                       assume='sys',
                       primed=False,
                       negated=False)
    assert not pr
    assert s == '(pidglobal_x2 = 0)', s
    s, pr = f.to_guard(t,
                       pid='global',
                       assume='sys',
                       primed=True,
                       negated=False)
    assert pr
    assert s == 'True', s
    s, pr = f.to_guard(t,
                       pid='global',
                       assume='sys',
                       primed=True,
                       negated=True)
    assert pr
    assert s == 'False', s
    # array ref with index an expr containing a var
    c = '''
    sys int(0, 3) x[3];
    sys int(0, 2) y;

    ltl { x[y] == 0 }
    '''
    program = parser.parse(c)
    vardefs, groups, ltlblocks = program.to_table()
    t = logic.Table()
    logic.add_variables_to_table(t,
                                 vardefs,
                                 pid='global',
                                 assume_context='sys')
    ltl = next(iter(ltlblocks))
    f = ltl.formula
    s, context = f.to_logic(t, pid='global')
    assert context == 'bool', context
    correct = '(ite( {y} = 2, {x}2, ite( {y} = 1, {x}1, {x}0)) = 0)'.format(
        x='pidglobal_x', y='pidglobal_y')
    assert s == correct, s
    s, pr = f.to_guard(t,
                       pid='global',
                       assume='sys',
                       primed=False,
                       negated=False)
    assert not pr
    assert s == correct
    s, pr = f.to_guard(t,
                       pid='global',
                       assume='sys',
                       primed=True,
                       negated=False)
    assert pr
    assert s == 'True'
    s, pr = f.to_guard(t,
                       pid='global',
                       assume='sys',
                       primed=True,
                       negated=True)
    assert pr
    assert s == 'False'
    # array ref with index a primed controlled var
    # raise exception if primed controlled index
    c = '''
    sys int(0, 3) x[3];
    sys int(0, 2) y;

    ltl { x[y'] == 0 }
    '''
    program = parser.parse(c)
    vardefs, groups, ltlblocks = program.to_table()
    t = logic.Table()
    logic.add_variables_to_table(t,
                                 vardefs,
                                 pid='global',
                                 assume_context='sys')
    ltl = next(iter(ltlblocks))
    f = ltl.formula
    s, context = f.to_logic(t, pid='global')
    correct = (
        '(ite( (X {y}) = 2, {x}2, ite( (X {y}) = 1, {x}1, {x}0)) = 0)').format(
            x='pidglobal_x', y='pidglobal_y')
    assert s == correct, s
    f.to_guard(t, pid='global', assume='sys', primed=True, negated=True)
    with assert_raises(AssertionError):
        f.to_guard(t, pid='global', assume='sys', primed=False, negated=True)
    t.scopes['global']['x']['owner'] = 'env'
    with assert_raises(AssertionError):
        f.to_guard(t, pid='global', assume='sys', primed=False, negated=True)
    # realizability test
    c = '''
    active sys proctype foo(){
        int(0, 3) x[5] = 3;
        int(0, 4) y;

        x[3] = 1;

        do
        :: x[3] == 1
        od
    }
    '''
    r = logic.synthesize(c)
    assert r
    c = '''
    active sys proctype foo(){
        int(0, 3) x[5] = 3;
        int(0, 4) y;

        x[3] = 1;

        do
        :: x[4] == 3
        od
    }
    '''
    r = logic.synthesize(c)
    assert r
    c = '''
    active sys proctype foo(){
        int(0, 3) x[5] = 3;
        int(0, 4) y;

        x[3] = 1;

        do
        :: x[4] == 2
        od
    }
    '''
    r = logic.synthesize(c)
    assert not r