How do I model a boolean AND-evaluation in Promela non-atomically? - promela

How do I model a boolean AND-evaluation in Promela non-atomically ?
I want to model the statement while (flag[j] == true && victim == i) where the AND-evaluation should be done non-atomically.
How is this possible?

loop:
if
:: flag[j] != true -> goto done
:: else
fi;
if
:: victim != i -> goto done
:: else
fi;
/* body of 'while' */
goto loop;
done:
/* after 'while' */
Note, although I don't have a Spin environment in front of me, what makes you think the conditional is atomic in the first place? I'd test with something like:
byte i = 0
i++ >= 0 && i-- >= 0
never { assert (1 == i) }
But, either way, the first code above shows how to make it non-atomic if need be.

Related

C question in logical OR: 2 operands evaluated (0) false, but the result works as TRUE range

My doubt is about the basic theory of "or logical operator". Especifically, logical OR returns true only if either one operand is true.
For instance, in this OR expression (x<O || x> 8) using x=5 when I evalute the 2 operand, I interpret it as both of them are false.
But I have an example that does not fit wiht it rule. On the contrary the expression works as range between 0 and 8, both included.
Following the code:
#include <stdio.h>
int main(void)
{
int x ; //This is the variable for being evaluated
do
{
printf("Imput a figure between 1 and 8 : ");
scanf("%i", &x);
}
while ( x < 1 || x > 8); // Why this expression write in this way determinate the range???
{
printf("Your imput was ::: %d ",x);
printf("\n");
}
printf("\n");
}
I have modified my first question. I really appreciate any helpo in order to clarify my doubt
In advance, thank you very much. Otto
It's not a while loop; it's a do ... while loop. The formatting makes it hard to see. Reformatted:
#include <stdio.h>
int main(void) {
int x;
// Execute the code in the `do { }` block once, no matter what.
// Keep executing it again and again, so long as the condition
// in `while ( )` is true.
do {
printf("Imput a figure between 1 and 8 : ");
scanf("%i", &x);
} while (x < 1 || x > 8);
// This creates a new scope. While perfectly valid C,
// this does absolutely nothing in this particular case here.
{
printf("Your imput was ::: %d ",x);
printf("\n");
}
printf("\n");
}
The block with the two printf calls is not part of the loop. The while (x < 1 || x > 8) makes it so that the code in the do { } block runs, so long as x < 1 or x > 8. In other words, it runs until x is between 1 and 8. This has the effect of asking the user to input a number again and again, until they finally input a number that's between 1 and 8.

Group typo3 condition

I need such a condition in ts:
([treeLevel = 0] && [globalVar = GP:R > 0]) || [PIDinRootline = {$pages.2018}]
I wanna show block if page has treelevel=0 and the get var R > 0, or if page id = $pages.2018
It looks like the similar code in php:
if(($treeLevel == 0 && $r > 0) || (pid == number))
The all expression in first brackets should be right, or in second.
Is it exist the method to group it like the previous record or I can only use userfunc?
There is no grouping in TS conditions, but if you need this particular condition from your post I think it is not needed because brackets around && are useless in this case.
(p && q) || r
is exactly the same as
p && q || r
Did you tested it?

Dafny no terms to trigger on predicate

I have the following snippet Dafny code for a tic tac toe game to check if player 1 has a winning row on the board:
predicate isWinRowForPlayer1(board: array2<int>)
reads board
requires board.Length0 == board.Length1 == 3 && isValidBoard(board)
{
exists i :: 0 <= i < board.Length0 ==> (forall j :: 0 <= j < board.Length1 ==> board[i, j] == 1)
}
Currently I am getting a /!\ No terms found to trigger on. error on the body of this predicate and all other predicates I have in my program (for winColumn, winDiag, ... etc)
Would appreciate if someone can help me to fix this
Here is one way to do it: introduce a helper function to hold the forall quantifier. Dafny will then use this helper function as the trigger for the outer exists quantifier, fixing the warning.
predicate RowIsWinRowForPlayer1(board: array2<int>, row: int)
reads board
requires 0 <= row < board.Length0
{
forall j :: 0 <= j < board.Length1 ==> board[row, j] == 1
}
predicate isWinRowForPlayer1(board: array2<int>)
reads board
requires board.Length0 == board.Length1 == 3 && isValidBoard(board)
{
exists i :: 0 <= i < board.Length0 ==> RowIsWinRowForPlayer1(board, i)
}
See this answer for more information about triggers.

Increment issue while colliding objects in unity3d

I am creating a basic game for test purpose in unity3D where i will counter the number of objects whenever the collision occurs on the defined object. I have created objects named birds1 upto birds8 While hitting the objects with name birds1, birds2. I am trying to increment the counter, it should increment the value of f by 1, if the hit the bird here the counter increments by random number instead of increment by 1. I have set a value that the counter should not exceed a limit of 8 so it stops at a limit of 8. Below is my code i am posting the code that i am using. Please can anyone help me with this that where is my code running wrong.
public void OnTriggerEnter2D(Collider2D target)
{
if (target.gameObject.name == "bird1" || target.gameObject.name == "bird2" || target.gameObject.name == "bird3" || target.gameObject.name == "bird4" || target.gameObject.name == "bird5" || target.gameObject.name == "bird6" || target.gameObject.name == "bird7" || target.gameObject.name == "bird8" && f < 8)
{
f++;
propPAnel8.SetActive(true);
BirdText.text = "x " + f;
if (f == 8)
{
bound8.isTrigger = true;
}
}
}
OnTriggerEnter2D will be called multiple times, especially if you have colliders with multiple contact points: That is why the increment in f it appears seemingly random, however, in this case, it is not.
You get increments as much as your contact points during the collision. What are you trying to achieve with the increment of 1? Maybe you need a boolean instead and setting isTrigger property of your Collider2D to false would help, but that depends on your game logic.

multiple instance if statement in xcode (iphone)

what is the proper way to do If statement with two variable in Xcode for Iphone
currently I have
if (minute >0) && (second == 0) {
minute = minute - 1;
second = 59;
}
The same was you would do it in any C/C++/Objective-C compiler, and most Algol derived languages, and extra set of parenthesis in order to turn to seperate boolean statements and an operator into a single compound statement:
if ((minute > 0) && (second == 0)) {
minute = minute - 1;
second = 59;
}
You'll need another set of parenthesis:
if ((minute >0) && (second == 0)) {
minute = minute - 1;
second = 59;
}
Or you could also write:
if (minute > 0 && second == 0)
Which is what you'll start doing eventually anyway, and I think (subjective) is easier to read. Operator precedence insures this works...