'And', 'Or' operators in Minizinc? - boolean

This is probably an easy answer, like no there are is no 'or' or 'and' operators in Minizinc, but I can't seem to find anything related to it on google.
In most programming languages you can do this:
boolean s = (a != 1 && b != 2);
But nowhere can I find a Minizinc version of this statement, like:
constraint a != 1 and b != 2;
Cheers

The disjunction (or) and conjunction (and) are written in MiniZinc as \/ and /\ respectively.
So your example should be written as
constraint a != 1 /\ b != 2;
See a little more here: https://www.minizinc.org/doc-2.5.5/en/modelling2.html?highlight=conjunction

Related

Implementing an interface for a plain old recursive data type

I'm fighting with Idris syntax, it seems.
module Test
data Nat = Z | S Nat
Eq Nat where
Z == Z = True
S n1 == S n2 = n1 == n2
_ == _ = False
This complains with the following error (v1.1.1):
.\.\Test.idr:5:8: error: expected: "#",
"with", argument expression,
constraint argument,
function right hand side,
implicit function argument,
with pattern
Eq Nat where
^
Type checking .\.\Test.idr
I don't understand why, I basically used the same syntax as the docs.
When I write an Eq implementation for a custom, non-recursive type, such as Bool, it compiles just fine.
You need to wrap S n patterns in parenthesis. After doing that, your will get compiler errors because Nat is already defined in Prelude. So to compile your code just replace Nat with Natural (or anything else). Though, Z and S constructors are also defined in Prelude so you either need to rename everything to be able to test in REPL easily or use %hide directive.
But at least this code compiles:
module Test
data Natural = Z | S Natural
Eq Natural where
Z == Z = True
(S n1) == (S n2) = n1 == n2
_ == _ = False

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.

Comparing not then and vs and then not

I'm just wondering if there's a difference between an if or a while statement if the condition is either (!a && !b) (let's call this statement 1) or !(a && b) (let's call this statement 2).
I was thinking about it, and we have four combinations of a and b possible, and I think that the condition would be different if a != b. I'm just hoping someone can check my logic.
If a and b are both true, then statements 1 and 2 are both false. If a and b are both false, then statements 1 and 2 are both true. ?However, if a is true and b is false, or the inverse, then statement 1 is false, but statement 2 is true. Is this correct?
You are actually asking about a fundamental law of Boolean Algebra: De Morgan's Laws are very useful to know when re-working or simplifying conditionals.
De Morgan's Laws
!(a && b) = (!a || !b)
!(a || b) = (!a && !b)
Your intuition that your statements 1 and 2 might not be equivalent is correct. Working through the four possibilities of a and b above shows that the actual equivalencies are those given by De Morgan's Laws.
Key: ∧ is logical AND (&&); ∨ is logical OR (||).
Image source: http://ndp.jct.ac.il/tutorials/mavomath/node15.html
!(a && b) = !a || !b this does not equal !a && !b. In the first case if a or b is false then the result is true. In the second case both a and b must be false for the result to be true.
So as you say they have different results when a != b.

How are these boolean expressions (truth tables) equivalent?

I am trying to better understand boolean equivalence but this example has me a little stuck.
I am referring to this website: http://chortle.ccsu.edu/java5/Notes/chap40B/ch40B_9.html
It makes sense, but doesn't at the same time... it says that they are equivalent, but the true/false values don't add up/align in a way that makes them out to be equivalent as the table shows they are. Could someone explain this to me?
!(A && B) <-- first expression
(C || D) <-- second expression
The last columns refers to the equivalency of the two expressions, which yes, they are equivalent according to the table. However, I just don't get how the two expressions are equivalent. If A = F, B = F --> T, wouldn't C = F, D = F --> T as well?
A B C D
--------------------
F F T T T
F T T F T
T F F T T
T T F F F
You are confusing yourself when trying to reduce it from the actual expression to single letter variables. On referring the actual link, it would appear that the variables you use can be mapped to the original expressions as follows:
A = speed > 2000
B = memory > 512
C = speed <= 2000
D = memory <= 512
If you look at it, C equals !A and D equals !B. So the expression (C || D) is effectively !((!A) || (!B)). By De Morgan's Law, that is the same as !(A && B).
This table is explaining that !(A && B) is equivalent to !A || !B. The columns C and D appear to be defined as C = !A and D = !B. The last column is C || D
So A = F, B = F certainly implies !(A && B). In this case C = D = T, and so also C || D = T.

Coffee Existential Operator different compliations [duplicate]

This question already has an answer here:
How does CoffeeScript's existential operator work?
(1 answer)
Closed 8 years ago.
Similar to this topic:
CoffeeScript Existential Operator and this
The coffeescript, I'm having a problem using the elvis operator:
foo = ->
y = foo()
console.log 'y is null' unless y?
console.log 'x is null' unless x?
Compiles to:
var foo, y;
foo = function() {};
y = foo();
if (y == null) {
console.log('y is null');
}
if (typeof x === "undefined" || x === null) {
console.log('x is null');
}
Output:
y is null
x is null
So the problem is that since y is assigned earlier that coffee takes the shortcut and assumes that y cannot be undefined. However, it is valid to return undefined from a function.
Is there a "safer" way to check that y is also not undefined?
UPDATED
Clarified Examples and Explanation:
From the comments, in the first if statement (y == null) is using double equal instead of ( x === null) triple equal, as in the second if statement. Clever.
? operator always checks if the value is neither null nor undefined.
y == null is absolutely correct way to check that the value of y is either null or undefined in JavaScript.
For example, the following CofeeScript code checks only for null values:
do_something() if y is null
which compiles to
if (y === null) do_something();
So, while y? (y == null in JS) checks for both null and undefined, y isnt null (y !== null in JS) checks only for null.
Check this answer for more detailed information.
See this answer for more info about equality checks in JavaScript.