Basic Logic operators both AND and OR - logic

Hi I have tree conditions
A, B and C
Now, I want to apply these conditions I such a way, that if any of these conditions is true, or a combination, the whole outcome is true.
If I do
A || B || C
then as soon as A is true, B and C are not evaluated
if I do
A && B && C
it's only true if ALL of them are true.
Is there a special notation for fulfilling my wishes?

Using the Or (||) operator will give you the correct answer because it does not need to evaluate the other conditions but if you want B and C to be evaluated even if A is True then you should nest the If statements such as:
if A == True
do something;
if B == True
do something;
if C == True
do something;
Or just do three separate If statements.

You have answered your own question.
You want a situation whereby if EITHER A OR B OR C is true, or if a combination such as A AND B are true then the whole expression will evaluate to true.
That is A || B || C. If you only care about requiring ANY of the conditions to be true, then there is no need to evaluate all the conditions because as long as ONE condition is true, then your whole expression or outcome is true.
If you care about the specific combinations being true TOGETHER then you can group them using parenthesis as such:
If I want A AND B to be true at the same time OR C: (A && B) || C
If I want A AND C to be true at the same time OR B: (A && C) || B

Related

Conditional to compare three values

I am trying to determine if there is a way for me to write an if statement that will result in true if three values are equal to each other.
EX:
if a == b == c
puts "true"
end
Instead of having to write:
if a == b && a == c && b == c
You don't need all 3, if a equal b and c equal b it implies a equal c.
If you need it more scalable 3+ arguments, you can do:
Set[a, b, c].size == 1
Original answer was:
Set[a, b, c].one?
But it one? does not count nil or false elements. So Set[nil, nil].one? will be false.
Thanks to Sagar Pandya for pointing it out in the comments.
What about this?
[a, b, c].uniq.size == 1
As #Bodacious mentioned a == b && b == c would work due to the transitive property.
But if you have many items, you could do something like:
values = [a, b, c]
if values.all? { |value| value == value[0] }
puts "true"
end
The all? method returns true if a block returns true for every item. So in that block, check to see if every item is equal to the first item.

Technical Term for this Rule Combination Pattern

I've been working on a program that takes a list of rules and tests combinations of them to operate a simple controller. The rules can only be true.
One rule would generate one controller:
A: If Cond1 Then True
If A then Activate
Two rules can generate 2 controllers:
A: If Cond1 Then True
B: If Cond2 Then True
If A and B then Activate
If A or B then Activate
Three rules generate 8 controllers:
A: If Cond1 Then True
B: If Cond2 Then True
C: If Cond3 Then True
A and B and C
A or B or C
(A and B) or C
A or (B and C)
(A and C) or B
(A and B) or (A and C)
(B and C) or (A and C)
(A and B) or (B and C)
Is there a formal name for this procedure? What field of study does this type of program fall under? All I've been able to find is that each controller might be described as using "fuzzy logic".
Truth tables exist for each of the controllers in the questions. The desired output could be obtained by filtering the output of a program that generated truth tables.
More about generating truth tables here:
Algorithm for generating all possible boolean functions of n variables

Wolfram Alpha and Logic - NOR transformation

i have some difficulties interpret some WolframAlpha logic.
I have this logical expression: !(a || b || c)
WA says, that it's minimal NOR-Form ist a NOR b NOR c.
But if you type it in, the truth tables are different.
However if you search for this (!a nor b) nor c
you'll get the correct answer.
Is this an WolframAlpha bug or do I just misinterpret the result?
Here are the links:
!(a || b || c) http://www.wolframalpha.com/input/?i=!%28a+||+b+||+c%29
a NOR b NOR c http://www.wolframalpha.com/input/?i=a+nor+b+nor+c
Thanks!
If we consider NOR as a binary operator, then we need to treat an expression like a NOR b NOR c as either (a NOR b) NOR c or a NOR (b NOR c). Either way, it's not the same as !(a || b || c). This is in fact how WA treats the formula when you ask it about a NOR b NOR c.
However, suppose we consider NOR as a “variable” arity operator, that takes any number of arguments. Thus we treat a NOR b NOR c as NOR(a, b, c), where the NOR function returns true if and only if all of its arguments are false. Then a NOR b NOR c is the same as !(a || b || c). This seems to be what WA thinks when you ask it about !(a || b || c).
It does seem like a bug in Wolfram Alpha that it uses different definitions of a NOR b NOR c in these two cases.

What does the semantic entailment relation mean (M |= A)?

I read many acticles about it. They described it as :
In logics, meaning is often described by a satisfaction relation
M |= A
that describes when a situation M satisfies a formula A.
So, I also searched some examples. I found the examples following :
True |= False = false
False |= True = true
I don't understand at all. What does it mean in these cases?
(assuming that you talk about propositional logic (it is similar for other logics such as pred. logic))
for two formulas A and B:
A |= B
"B evaluates to true under all evaluations that evaluate A to true"
for a set of formulas M and a formula B:
M |= B
"for every evaluation: B evaluates to true if only all elements of M
evaluate to true"
coming to your examples:
true |= false
is incorrect since evaluations exist
false |= A
is correct for any formula A, since 'false' is never evaluated to 'true'
under any evaluation
rgrds.

Conditions with common logic: question of style, readability, efficiency,

I have conditional logic that requires pre-processing that is common to each of the conditions (instantiating objects, database lookups etc). I can think of 3 possible ways to do this, but each has a flaw:
Option 1
if A
prepare processing
do A logic
else if B
prepare processing
do B logic
else if C
prepare processing
do C logic
// else do nothing
end
The flaw with option 1 is that the expensive code is redundant.
Option 2
prepare processing // not necessary unless A, B, or C
if A
do A logic
else if B
do B logic
else if C
do C logic
// else do nothing
end
The flaw with option 2 is that the expensive code runs even when neither A, B or C is true
Option 3
if (A, B, or C)
prepare processing
end
if A
do A logic
else if B
do B logic
else if C
do C logic
end
The flaw with option 3 is that the conditions for A, B, C are being evaluated twice. The evaluation is also costly.
Now that I think about it, there is a variant of option 3 that I call option 4:
Option 4
if (A, B, or C)
prepare processing
if A
set D
else if B
set E
else if C
set F
end
end
if D
do A logic
else if E
do B logic
else if F
do C logic
end
While this does address the costly evaluations of A, B, and C, it makes the whole thing more ugly and I don't like it.
How would you rank the options, and are there any others that I am not seeing?
Can't you do
if (A, B, or C)
prepare processing
if A
do A logic
else if B
do B logic
else if C
do C logic
end
? Maybe I misunderstood.
Edit: zzz, your edits messed me up. If you don't want it to evaluate A,B,C twice then do
x = func returnCase() //returns a,b, or c
if x != None
prepare processing
do Case
Doesn't this solve the redundancy:
if A
prepareprocessingfunction()
do A logic
else if B
prepareprocessingfunction()
do B logic
else if C
prepareprocessingfunction()
do C logic
// else do nothing
end
prepareprocessingfunction() {
prepare processing
}

Resources