def test_prspeed(): program = parse_pgcl(""" nat x; nat y; nat m; nat n; while ((x + 3 <= n)) { if (y < m) { { y := y + 1; } [1/2] { y := y + 0; } } else { { x := x + 0; } [1/4] { { x := x + 1; } [1/3] { { x := x + 2; } [1/2] { x := x + 3; } } } } tick(1); } """) tf = one_loop_wp_transformer(program, program.instructions) assert str( tf ) == '位饾惞. lfp 饾憢. [(x + 3) <= n] * (([y < m] * (((((饾憢)[y/y + 1]) + (tick(1))) * 1/2) + ((((饾憢)[y/y + 0]) + (tick(1))) * (1.0 - 1/2)))) + ([not (y < m)] * (((((饾憢)[x/x + 0]) + (tick(1))) * 1/4) + ((((((饾憢)[x/x + 1]) + (tick(1))) * 1/3) + ((((((饾憢)[x/x + 2]) + (tick(1))) * 1/2) + ((((饾憢)[x/x + 3]) + (tick(1))) * (1.0 - 1/2))) * (1.0 - 1/3))) * (1.0 - 1/4))))) + [not ((x + 3) <= n)] * 饾惞' snf = normalize_expectation_transformer(program, tf.body) assert str( snf ) == '位饾憢. [((x + 3) <= n) & (y < m)] * 1/2 * ((饾憢)[y/y + 1] + tick(1)) + [((x + 3) <= n) & (y < m)] * (1.0 - 1/2) * ((饾憢)[y/y + 0] + tick(1)) + [((x + 3) <= n) & not (y < m)] * 1/4 * ((饾憢)[x/x + 0] + tick(1)) + [((x + 3) <= n) & not (y < m)] * (1/3 * (1.0 - 1/4)) * ((饾憢)[x/x + 1] + tick(1)) + [((x + 3) <= n) & not (y < m)] * ((1/2 * (1.0 - 1/3)) * (1.0 - 1/4)) * ((饾憢)[x/x + 2] + tick(1)) + [((x + 3) <= n) & not (y < m)] * (((1.0 - 1/2) * (1.0 - 1/3)) * (1.0 - 1/4)) * ((饾憢)[x/x + 3] + tick(1))'
def __init__(self, program: Program, transformer: LoopExpectationTransformer): assert len( transformer.init) == 0, "initial assignments are not allowed" self.body = normalize_expectation_transformer(program, transformer.body) self.done = transformer.done
def test_zero_conf_parameterized(): program = parse_pgcl(""" # Variable free is either 0 or 1. Free=1 means hosts received answer address free. Free=1 means a collision occurred. # If free=0 holds on termination, then the host erroneously assumes that address is free. # (inital state constraint: free=0). nat free; # answerReceived = 1 (= 0) if host does (not) receive an answer. nat answerReceived; # count keeps track of the number of times the host requested an answer. # (initial state constraint: count = 0) nat count; nat maxCount; while(count < maxCount & free = 0){ {answerReceived := 0}[0.8]{answerReceived := 1} if(answerReceived=1){ {free:=1}[0.5]{free:=0} count := 0 }{ count:=count+1 } } """) tf = one_loop_wp_transformer(program, program.instructions) assert str( tf ) == "位饾惞. lfp 饾憢. [(count < maxCount) & (free = 0)] * (((([0 = 1] * ((((饾憢)[answerReceived/0, free/1, count/0]) * 0.5) + (((饾憢)[answerReceived/0, free/0, count/0]) * (1.0 - 0.5)))) + ([not (0 = 1)] * ((饾憢)[answerReceived/0, count/count + 1]))) * 0.8) + ((([1 = 1] * ((((饾憢)[answerReceived/1, free/1, count/0]) * 0.5) + (((饾憢)[answerReceived/1, free/0, count/0]) * (1.0 - 0.5)))) + ([not (1 = 1)] * ((饾憢)[answerReceived/1, count/count + 1]))) * (1.0 - 0.8))) + [not ((count < maxCount) & (free = 0))] * 饾惞" snf = normalize_expectation_transformer(program, tf.body) assert str( snf ) == '位饾憢. [((count < maxCount) & (free = 0)) & (0 = 1)] * (0.5 * 0.8) * (饾憢)[answerReceived/0, free/1, count/0] + [((count < maxCount) & (free = 0)) & (0 = 1)] * ((1.0 - 0.5) * 0.8) * (饾憢)[answerReceived/0, free/0, count/0] + [((count < maxCount) & (free = 0)) & not (0 = 1)] * 0.8 * (饾憢)[answerReceived/0, count/count + 1] + [((count < maxCount) & (free = 0)) & (1 = 1)] * (0.5 * (1.0 - 0.8)) * (饾憢)[answerReceived/1, free/1, count/0] + [((count < maxCount) & (free = 0)) & (1 = 1)] * ((1.0 - 0.5) * (1.0 - 0.8)) * (饾憢)[answerReceived/1, free/0, count/0] + [((count < maxCount) & (free = 0)) & not (1 = 1)] * (1.0 - 0.8) * (饾憢)[answerReceived/1, count/count + 1]'
def test_dueling_cowboys_count(): program = parse_pgcl(""" # we have a variable player: player=i (i=0 or i=1) means it's player i's turn nat player; # and a variable shot: shot=0 means the current player did not shoot the opponent. shot=1 means he did nat shot; nat c; # In this model, player 0 wins with a higher probability while(shot=0){ if(player = 0){ {shot := 1}[0.6]{player := 1} }{ {shot := 1}[0.4]{player := 0} } c := c + 1 } """) tf = one_loop_wp_transformer(program, program.instructions) assert str( tf ) == "位饾惞. lfp 饾憢. [shot = 0] * (([player = 0] * ((((饾憢)[shot/1, c/c + 1]) * 0.6) + (((饾憢)[player/1, c/c + 1]) * (1.0 - 0.6)))) + ([not (player = 0)] * ((((饾憢)[shot/1, c/c + 1]) * 0.4) + (((饾憢)[player/0, c/c + 1]) * (1.0 - 0.4))))) + [not (shot = 0)] * 饾惞" snf = normalize_expectation_transformer(program, tf.body) assert str( snf ) == '位饾憢. [(shot = 0) & (player = 0)] * 0.6 * (饾憢)[shot/1, c/c + 1] + [(shot = 0) & (player = 0)] * (1.0 - 0.6) * (饾憢)[player/1, c/c + 1] + [(shot = 0) & not (player = 0)] * 0.4 * (饾憢)[shot/1, c/c + 1] + [(shot = 0) & not (player = 0)] * (1.0 - 0.4) * (饾憢)[player/0, c/c + 1]'
def test_loop_forever(): program = parse_pgcl(""" while(True){skip} """) tf = one_loop_wp_transformer(program, program.instructions) assert str(tf) == "位饾惞. lfp 饾憢. [True] * ((饾憢)[]) + [not True] * 饾惞" snf = normalize_expectation_transformer(program, tf.body) assert str(snf) == '位饾憢. [True] * 1.0 * (饾憢)[]'
def test_unfair_random_walk(): program = parse_pgcl(""" nat x; while((not x<=0)){ {x := x+1 }[0.1]{ x := x-1} } """) tf = one_loop_wp_transformer(program, program.instructions) assert str( tf ) == "位饾惞. lfp 饾憢. [not x <= 0] * ((((饾憢)[x/x + 1]) * 0.1) + (((饾憢)[x/x - 1]) * (1.0 - 0.1))) + [not (not x <= 0)] * 饾惞" snf = normalize_expectation_transformer(program, tf.body) assert str( snf ) == '位饾憢. [not x <= 0] * 0.1 * (饾憢)[x/x + 1] + [not x <= 0] * (1.0 - 0.1) * (饾憢)[x/x - 1]'
def test_geometric_monus_2(): program = parse_pgcl(""" nat c; nat f; while(f=1){ {f := 0}[0.5]{ c := c - 1 + 2} } """) tf = one_loop_wp_transformer(program, program.instructions) assert str( tf ) == "位饾惞. lfp 饾憢. [f = 1] * ((((饾憢)[f/0]) * 0.5) + (((饾憢)[c/(c - 1) + 2]) * (1.0 - 0.5))) + [not (f = 1)] * 饾惞" snf = normalize_expectation_transformer(program, tf.body) assert str( snf ) == '位饾憢. [f = 1] * 0.5 * (饾憢)[f/0] + [f = 1] * (1.0 - 0.5) * (饾憢)[c/(c - 1) + 2]'
def test_branchy(): code = """ while (c < 6) { {c := 3} [0.8] {c := 7} if (c=2) { d:=10 } {d := 20} if (f=1) { f:=0 } {f := 1} } """ program = parse_pgcl(code) tf = one_loop_wp_transformer(program, program.instructions) assert str( tf ) == '位饾惞. lfp 饾憢. [c < 6] * (((([3 = 2] * (([f = 1] * ((饾憢)[c/3, d/10, f/0])) + ([not (f = 1)] * ((饾憢)[c/3, d/10, f/1])))) + ([not (3 = 2)] * (([f = 1] * ((饾憢)[c/3, d/20, f/0])) + ([not (f = 1)] * ((饾憢)[c/3, d/20, f/1]))))) * 0.8) + ((([7 = 2] * (([f = 1] * ((饾憢)[c/7, d/10, f/0])) + ([not (f = 1)] * ((饾憢)[c/7, d/10, f/1])))) + ([not (7 = 2)] * (([f = 1] * ((饾憢)[c/7, d/20, f/0])) + ([not (f = 1)] * ((饾憢)[c/7, d/20, f/1]))))) * (1.0 - 0.8))) + [not (c < 6)] * 饾惞' snf = normalize_expectation_transformer(program, tf.body) assert str( snf ) == '位饾憢. [(c < 6) & ((3 = 2) & (f = 1))] * 0.8 * (饾憢)[c/3, d/10, f/0] + [(c < 6) & ((3 = 2) & not (f = 1))] * 0.8 * (饾憢)[c/3, d/10, f/1] + [(c < 6) & (not (3 = 2) & (f = 1))] * 0.8 * (饾憢)[c/3, d/20, f/0] + [(c < 6) & (not (3 = 2) & not (f = 1))] * 0.8 * (饾憢)[c/3, d/20, f/1] + [(c < 6) & ((7 = 2) & (f = 1))] * (1.0 - 0.8) * (饾憢)[c/7, d/10, f/0] + [(c < 6) & ((7 = 2) & not (f = 1))] * (1.0 - 0.8) * (饾憢)[c/7, d/10, f/1] + [(c < 6) & (not (7 = 2) & (f = 1))] * (1.0 - 0.8) * (饾憢)[c/7, d/20, f/0] + [(c < 6) & (not (7 = 2) & not (f = 1))] * (1.0 - 0.8) * (饾憢)[c/7, d/20, f/1]'
def test_linear01(): program = parse_pgcl(""" nat x; while (2 <= x) { { x := x - 1; } [1/3] { x := x - 2; } tick(1); } """) tf = one_loop_wp_transformer(program, program.instructions) assert str( tf ) == '位饾惞. lfp 饾憢. [2 <= x] * (((((饾憢)[x/x - 1]) + (tick(1))) * 1/3) + ((((饾憢)[x/x - 2]) + (tick(1))) * (1.0 - 1/3))) + [not (2 <= x)] * 饾惞' snf = normalize_expectation_transformer(program, tf.body) assert str( snf ) == '位饾憢. [2 <= x] * 1/3 * ((饾憢)[x/x - 1] + tick(1)) + [2 <= x] * (1.0 - 1/3) * ((饾憢)[x/x - 2] + tick(1))'
def test_ber_ert(): program = parse_pgcl(""" nat x; nat n; nat r; while (x < n) { r := 1 : 1/2 + 0 : 1/2; x := x + r; tick(1); } """) tf = one_loop_wp_transformer(program, program.instructions) assert str( tf ) == '位饾惞. lfp 饾憢. [x < n] * ((1/2 * (((饾憢)[r/1, x/x + 1]) + (tick(1)))) + (1/2 * (((饾憢)[r/0, x/x + 0]) + (tick(1))))) + [not (x < n)] * 饾惞' snf = normalize_expectation_transformer(program, tf.body) assert str( snf ) == '位饾憢. [x < n] * 1/2 * ((饾憢)[r/1, x/x + 1] + tick(1)) + [x < n] * 1/2 * ((饾憢)[r/0, x/x + 0] + tick(1))'
def test_complete_binary_tree(): program = parse_pgcl(""" nat a; nat b; nat c; nat maxA; nat maxB; while (a < maxA & b < maxB) { {a:=a+1} [0.5] {b := b+1} c:=c+1 } """) tf = one_loop_wp_transformer(program, program.instructions) assert str( tf ) == "位饾惞. lfp 饾憢. [(a < maxA) & (b < maxB)] * ((((饾憢)[a/a + 1, c/c + 1]) * 0.5) + (((饾憢)[b/b + 1, c/c + 1]) * (1.0 - 0.5))) + [not ((a < maxA) & (b < maxB))] * 饾惞" snf = normalize_expectation_transformer(program, tf.body) assert str( snf ) == '位饾憢. [(a < maxA) & (b < maxB)] * 0.5 * (饾憢)[a/a + 1, c/c + 1] + [(a < maxA) & (b < maxB)] * (1.0 - 0.5) * (饾憢)[b/b + 1, c/c + 1]'
def test_geometric_flipping(): program = parse_pgcl(""" nat c; nat f; nat k; while(f=1){ if(k=0){ {f := 0}[0.5]{ c := c +1 }; k := 1 }{ k :=0 } } """) tf = one_loop_wp_transformer(program, program.instructions) assert str( tf ) == "位饾惞. lfp 饾憢. [f = 1] * (([k = 0] * ((((饾憢)[f/0, k/1]) * 0.5) + (((饾憢)[c/c + 1, k/1]) * (1.0 - 0.5)))) + ([not (k = 0)] * ((饾憢)[k/0]))) + [not (f = 1)] * 饾惞" snf = normalize_expectation_transformer(program, tf.body) assert str( snf ) == '位饾憢. [(f = 1) & (k = 0)] * 0.5 * (饾憢)[f/0, k/1] + [(f = 1) & (k = 0)] * (1.0 - 0.5) * (饾憢)[c/c + 1, k/1] + [(f = 1) & not (k = 0)] * 1.0 * (饾憢)[k/0]'
def test_brp_simple_parameterized(): program = parse_pgcl(""" # The number of total packages to send nat toSend; # Number of packages sent nat sent; # The maximal number of retransmission tries nat maxFailed; # The number of failed retransmission tries nat failed; while(failed < maxFailed & sent < toSend){ { # Transmission of current packages successful failed := 0; sent := sent + 1; } [0.9] { # Transmission not successful failed := failed +1; } } """) tf = one_loop_wp_transformer(program, program.instructions) assert str( tf ) == "位饾惞. lfp 饾憢. [(failed < maxFailed) & (sent < toSend)] * ((((饾憢)[failed/0, sent/sent + 1]) * 0.9) + (((饾憢)[failed/failed + 1]) * (1.0 - 0.9))) + [not ((failed < maxFailed) & (sent < toSend))] * 饾惞" snf = normalize_expectation_transformer(program, tf.body) assert str( snf ) == '位饾憢. [(failed < maxFailed) & (sent < toSend)] * 0.9 * (饾憢)[failed/0, sent/sent + 1] + [(failed < maxFailed) & (sent < toSend)] * (1.0 - 0.9) * (饾憢)[failed/failed + 1]'