If not (a and not b) and if (not a and b) - logic

Can anyone explain why these two statements aren't equal?
if not(a and not b):
// do some stuff
if (not a and b):
// do some stuff
I tried to make my program more understandable by changing the first statement to the second but it doesn't work. I don't totally understand why.

You should look into De Morgan's Thereom, half of which is (a):
not(p and q) -> not(p) or not(q)
In terms of how that applies to your situation, just replace p with a and q with not(b):
not(a and not b) -> not(a) or not(not(b))
-> not(a) or b
(a) The other half is:
not(p or q) -> not(p) and not(q)

if not(a and not b) is the same as if (not a) or b, not what you wrote.

You also need to flip the 'and' to 'or' due to De Morgan's law
if not(a and not b)
becomes
if (not a or b)

Related

How to easily prove the following in Coq such as using only assumptions?

Is there an easy way to prove the following in Coq such as using only assumptions?
(P -> (Q /\ R)) -> (~Q) -> ~P
The question is a bit vague... Do you wonder if it is possible (yes), what the answer is (see Arthur's comment above), or how to think about solving these problems?
In the latter case, remember that the goal is to create a "lambda-term" with the specified type. You can either use "tactics" which are helping you construct the term "from the outside and inwards. It is good to do it by hand a couple of times to understand what is going on and what the tactics really do, which I think is why you are given this exercise.
If you look at your example,
(P -> (Q /\ R)) -> (~Q) -> ~P
you can see that it is a function of three (!) arguments. It is because the last type ~P really means P -> False, so the types of the arguments to the function that you need to create are
P -> (Q /\ R)
Q -> False
P
and the function should construct a term of type
False
You can create a term fun A B C => _ where A, B, C has the types above, (this is what the tactic intros does), and you need to come up with a term that should go into the hole _ by combining the terms A, B, C and the raw gallina constructions.
In this case, when you have managed to create a term of type Q /\ R you will have to "destruct" it to get the term of type Q, (Hint: for that you will have to use the match construction).
Hope this helps without spoiling the fun!

NOR-conjunction within OR-conjunctions

Let's save I have the logical expression
a OR b NOR c OR d
Is this interpreted as (a OR b) NOR (c OR d) or as a OR (b NOR c) OR d ?
It is a specific programming problem I face, thus the question on stackoverflow.
As far as I know, it would be interpreted as:
A or b or (Not c) or d

(LOGIC) Determine whether a statement is correct using truth-tables

I'm trying to study for my midterm and I need help figuring out what to do for this problem. It says:
Determine whether the following statement is correct, using any
legitimate truth-table technique.
~A ∨ (B → C), E → (B & A), C → E  |= C ↔ A
I make a truth table for each statement but I don't how the main connective correlates with the other connectives in the other statements.
I think I have to make a joint table but I really don't know where to begin. If anyone can help me understand it would be greatly appreciated!
A,B,C |= D means that if A,B,C are all true then D is true as well. But, this is exactly what the expression (A & B & C) -> D says. Thus A,B,C |= D is true if and only if (A & B & C) -> D is a tautology. In other words, the connective -> captures the meaning of |=. For your problem, you can make a truth table for the compound expression
[(~A ∨ (B → C)) & (E → (B & A)) & (C → E)] -> (C ↔ A)
and see if it is a tautology

Euclid's algorithm using until

I'm a beginner in Haskell, just started now learning about folds and what not, in college, first year.
One of the problems I'm facing now is to define Euclid's algorithm using the until function.
Here's the Euclid's recursive definition (EDIT: just to show how euclid works, I'm trying to define euclid's without the recursive. Just using until):
gcd a b = if b == 0 then a else gcd b (a `mod` b)
Here's what i have using until:
gcd a b = until (==0) (mod a ) b
Obviously this doesn't make any sense since it's always going to return 0, as that is my stopping point instead of printing the value of a when b == 0. I can't for the life of me though figure out how to get the value of a.
Any help is appreciated.
Thank you in advance guys.
Hints:
Now
until :: (a -> Bool) -> (a -> a) -> a -> a
so we need a function that we can apply repeatedly until a condition holds, but we have two numbers a and b, so how can we do that?
The solution is to make the two numbers into one value, (a,b), so think of gcd this way:
uncurriedGCD (a,b) = if b == 0 then (a,a) else uncurriedGCD (b,a `mod` b)
Now you can make two functions, next & check and use them with until.
Helpers for until:
next (a,b) = (b,a `mod` b)
check (a,b) = b == 0
This means that we now could have written uncurriedGCD using until.
Answer:
For example:
ghci> until check next (6,4)
(2,0)
ghci> until check next (12,18)
(6,0)
So we can define:
gcd a b = c where (c,_) = until check next (a,b)
giving:
ghci> gcd 20 44
4
ghci> gcd 60 108
12
What the Euclid's algorithm says is this: for (a, b), computing (b, mod a b) until (the new) b equals zero. This can be translated directly to an implementation using until like this:
myGcd a b = until (\(x, y) -> y == 0) (\(x, y) -> (y, x `mod` y)) (a, b)

Robust distance comparing predicate

I need a robust predicate defined by the following code:
CompareResult compareDistance(Point a, Point b, Point c, Point d) {
if (distance(a, b) > distance(c, d))
return Larger;
else if (distance(a, b) == distance(c, d))
return Equal;
else
return Smaller;
}
Due to the floating point arithmetic limitations we can't compute distance exactly (even its square), so if we just directly implement this code, the predicate will not be robust. I tried to find it in CGAL library, but couldn't.
Somewhat close to the predicate I need is compare_distance_to_point(Point p, Point q, Point r) predicate. It returns Smaller if distance(p, q) < distance(p, r), Equal if distance(p, q) == distance(p, r) and Larger otherwise. The first thought is to shift c and d by (c - a) vector, so we could call compare_distance_to_point(a, b, d + (c - a)), but this will violate robustness again. So, does anyone have an idea for adapting it?
If you take a kernel with exact predicates such as
Exact_predicates_inexact_constructions_kernel,
you can use the functor Compare_distance_3 which is a model of the concept CompareDistance_3.
Kernel::Compare_distance_3 cmp;
return cmp(a,b,c,d);

Resources