Writing (A && C) || (B && C) conditional shorter - perl

(E.G. In Perl) When either condition A or condition B have the same consequence
if (A){
# Consequence X
}elsif (B){
# Consequence X
}
we can write
if ( A || B ) {
# Consequence X
}
How about we have the following condition: Either when A and C are true, or B and C are true, consequence C follows.
This can be written very long:
if ( A && C){
# Consequence X
} elsif (B && C ){
# consequence X
}
My question is, is there any way to write this shorter?
Something like this:
if ( (A && C) || (B && C) )
is syntactically ok ???

Yes.
if ( A && C){
# Consequence X
} elsif (B && C ){
# consequence X
}
is the same as:
if ( (A && C) || (B && C) ){
#Consequence X
}
And this avoids evaluating C twice:
if ( (A || B) && C){
#Consequence X
}
BTW, this is more like a logical question, the logic here isn't limited to Perl.

Try the following:
if (C && (A || B)) {
}

Related

Rewrite Boolean Expression without Parentheses

I have a black box processing system that accepts boolean expressions but does not support parentheses. I want to input a boolean expression like this:
A && (B || C)
However, when I input it as:
A && B || C
it evaluates expressions as if I had entered:
(A && B) || C
Is there any way to rewrite my expression to get the desired behavior of A && (B || C) without parentheses? Thanks!
There's a relation between precedence but, you can use associative and distributive laws to resolve this.
A && B || A && C
Truth tables match.
Be careful when implementing this. Not all compilers and languages use the same precedence and evaluation order.
Edit: No left-right precedence, sorry.
Use the distributive law and the precedence of && to check if either both A and B is true or both A and C is true.
Solution
A && B || A && C
Distributive Law
x ^ (y v z) = (x ^ y) v (x ^ z)
x && (y || z) = (x && y) || (x && z)
function original(A, B, C) {
return A && (B || C);
}
function mySolution(A, B, C) {
return A && B || C && A;
}
console.log(original(true, true, true) == mySolution(true, true, true));
console.log(original(true, true, false) == mySolution(true, true, false));
console.log(original(true, false, true) == mySolution(true, false, true));
console.log(original(true, false, false) == mySolution(true, false, false));
console.log(original(false, true, true) == mySolution(false, true, true));
console.log(original(false, true, false) == mySolution(false, true, false));
console.log(original(false, false, true) == mySolution(false, false, true));
console.log(original(false, false, false) == mySolution(false, false, false));

Simplifying a boolean expression with 4 variables

I can't figure this one out, I'm going insane looking through my textbook.
The expression is: (!w && !x && !y && z) || (w && !x && !y && z) || (x && z) || (x && y && !z)
Using distributive and compliment laws, I have narrowed it down to:
(!x && !y && z) || (x && z) || (x && y && !z)
But I can't narrow it any further! The answer is supposed to be (x && y) || ( !y && z) but I can't figure out what law would put me there. My teacher told me to use distributive but wouldn't help me further. That doesn't make any sense since there's nothing I can factor out and get rid of. What law am I supposed to use next?
You can expand xz to x(y + !y)z => xyz + x!yz.
Now you have !x!yz + xyz + x!yz + xy!z.
Then !x!yz + x!yz simplifies to !yz, and xyz + xy!z simplifies to xy,
giving the minimal expression.

Triggers, #instances in Z3

My problem comes from a large Boogie file. I'll just explain the relevant parts here.
I declare a new type Msg for messages which have 7 fields tID, stype, bal, acc, mbal, mval, val. These fields are declared as functions in Boogie. Then I declared 72 Msg constants M1, ..., M72 and corresponding values of their fields. You can see the following example:
type Msg;
const unique M1: Msg;
function tID (Msg) returns (int);
function stype (Msg) returns (Str);
function bal (Msg) returns (int);
function acc (Msg) returns (Proc);
function mbal (Msg) returns (int);
function mval (Msg) returns (Val);
function val (Msg) returns (Val);
axiom (tID (M1) == 1);
axiom (stype (M1) == s1a);
axiom (bal (M1) == 1);
axiom (acc (M1) == ProcNull);
axiom (mbal (M1) == -2);
axiom (mval (M1) == ValNull);
axiom (val (M1) == ValNull);
where Proc, Str are some types and s1a, ProcNull, ValNull are some constants. Then I declare an axiom which says that two messages m1, m2 are equal if and only if every field of m1, m2. This axiom uses a trigger MsgComp(m1, m2).
function MsgComp(m1, m2: Msg): bool { true }
axiom (forall m1, m2: Msg :: {MsgComp(m1, m2)}
m1 == m2 <==>
(tID(m1) == tID(m2) && stype(m1) == stype(m2) &&
acc(m1) == acc(m2) && bal(m1) == bal(m2) &&
mbal(m1) == mbal(m2) && mval(m1) == mval(m2) &&
val(m1) == val(m2)));
Then, I declare a new variable m and assign arbitrary values to its fields such that m equals M1 or M2. I don't say explicitly m == M1 || m == M2but we can prove this assertion (line 786) with the above axiom.
assert (MsgComp(cMsg, M1) && MsgComp(cMsg, M2) && (cMsg == M1 || cMsg == M2));
However, if I want to check whether m is one of M1, ..., M72, Boogie shows that the corresponding assertion might not hold.
assert (MsgComp(cMsg, M1) && ... && MsgComp(cMsg, M72) && (cMsg == M1 || ... || cMsg == M72));
And I found that if I check that assertion with at most 32 constants, Boogie can verify it. Why can't Boogie or Z3 verify it with more constants?
On my machine, the check does not succeed even for the case of two constants.
I was able to get both checks to pass by changing them to the following form
assert MsgComp(cMsg, M1) && MsgComp(cMsg, M2) ==> (cMsg == M1 || cMsg == M2);
(Notice that the && in the middle has been changed to ==>.)
This also works for the assertion with the 72 values.
I don't know why this change matters. It looks like the conjuncts are ignored or simplified away before triggering happens.

Logical operator outside parentheses

I can't figure out what the logical operator "not" (~/!), does in front of a parenthesis.
Here is the task:
I have a = 1, b = 2 and c = 2.
and this if statement:
if (a ~= b | a ~= c) &&~(a ~= b && a ~= c && b ~= c)
What does it mean having a logical operator "not" before the parenthesis (look at the bold ~)?
It plays the role of a logic-negation. It'll return the logical negation of (a ~= b && a ~= c && b ~= c). Meaning if (a ~= b && a ~= c && b ~= c) is true, then ~(a ~= b && a ~= c && b ~= c) is false. and vice versa.
not(A) is equivalent to ~A. The symbol ~ acts like the not(A) logical operator if A is a logical expression. The same symbol ~ placed before = acts like the ne(A) relational operator.
Matlab employs short-circuiting behavior when dealing with logical operators && and ||. Meaning that if we are evaluating e.g expr1 && expr2, then expr2 is not evaluated if expr1 is logical 0 (false). Same way happens when we evaluate e.g expr1 || expr2, expr2 is not evaluated if expr1 is logical 1 (true).
The symbols | and || perform different operations in a MATLAB application. The symbol | is an element-wise OR operator. The short-circuit OR operator is ||. meaning that if we have e.g expr1 || expr2, both expr1 and expr2 are evaluated. Same thing happens with the & element-wise AND operator.
Now, let's have a look at the expression you asked for:
if (a ~= b | a ~= c) &&~(a ~= b && a ~= c && b ~= c)
Knowing that: a = 1, b = 2 and c = 2.
expr1 = (a ~= b | a ~= c)
= (1 ~= 2 | 1 ~= 2)
= 1 (true)
expr1 is true, so Matlab proceeds to evaluate expr2:
expr2 = ~(a ~= b && a ~= c && b ~= c)
= ~(1 ~= 2 && a ~= c && b ~= c)
= ~(1 && 1 ~= 2 && b ~= c)
= ~(1 && 1 && 2 ~= 2)
= ~(1 && 1 && 0)
= ~(0)
= 1 (true)
So the final result of expr1 && expr2 is 1 (true).
Hope it's useful!
I'll try to complement the Academia's answer - the evaluation order is important and every logical operator has it's precedence. In Matlab the negation operator '~' not() has higher precedence than other logical operators. In this case the expression a ~= b && a ~= c && b ~= c will be evaluated before the negation operator as a whole because it was placed inside the parenthesis. This expression can be read as "all three variables have different values". By placing it inside the parenthesis and negating it, you get "not all three variables have different values". Without the parenthesis you'd get not(a) ~= b && a ~= c && b ~= c. Note also that comparison operators have higher precedence than &&.
Hope it helps.

XOR Objective-c

Is there a way to put a condition that return true only if one of the two component return true?
BOOL var1
BOOL var2
something shorter than if((var1 && !var2) || (!var1 && var2))
Thank you
As Objective-C is a superset of C, you simply use the XOR operator from C, which is ^.
XOR
if(!!var1 != !!var2)
{
NSLog(#"XOR condition");
}
Exclamation marks convert vars to BOOL (real conversion, not casting)
So this solution works even if your variables are not BOOL.
!!(0|nil) ≡ 0
!!(any other number|object) ≡ 1
This is useful in cases when you want to be sure that only one of vars is nonnil.
You could add more clearness to the code that Ishu suggests by doing this:
#define XOR !=
and then you just write:
if (var1 XOR var2) {
...
}
truth table output:
[T XOR T => F; T != T => F],
[T XOR F => T; T != F => T],
[F XOR T => T; F != T => T] and
[F XOR F => F; F != F => F]
A macro could be an option. It will keep the same behaviour, but now in a more readable way.
#define XOR(x,y) (((x) && !(y)) || (!(x) && (y)))
if(XOR(var1, var2) {
}
try (int)var1 ^ (int)var2
YES and NO are actually defined as following:
#define YES (BOOL)1
#define NO (BOOL)0
if ((!var1)==var2)
{
NSLog(#"Yes");
}