Using the commutativity of the AND operator in Coq - coq

I’m trying to prove something in Coq using the commutativity of the logic AND operator. I coded this short example:
Axiom ax1 : A /\ B.
Theorem th1 : B /\ A.
Proof.
pose proof (ax1) as H.
symmetry.
apply H.
Qed.
I use axiom (ax1) in my proof, and I get stuck on the symmetry command. This is my current goal:
1 subgoal
H : A /\ B
______________________________________(1/1)
B /\ A
When I use the symmetry command, I get the following error:
Tactic failure: The relation and is not a declared symmetric relation. Maybe you need to require the Coq.Classes.RelationClasses library.
My current solution is to destruct hypothesis H, split the goal, and apply the right sub-hypothesis to the right subgoal. It takes a lot of space and is a bit frustrating to not be able to use AND commutativity.
So I’ve some questions: is symmetry the right command to use with a commutative relation? If yes, how can I fix my code for it to work? If not, is there a way to use the commutativity of the AND operator?

You can make the symmetry tactic work for any relation… as long as you show first that it is symmetric. This is done in the standard library for equality (which is why symmetry works for it out of the box), but not for and.
So if you want to use it, you have to do it yourself, like so:
Require Import RelationClasses.
Instance and_comm : Symmetric and.
Proof.
intros A B [].
now split.
Qed.
The RelationClasses module of the standard library declares the Symmetric typeclass, which is used by Coq when you call the symmetry tactic. Next we prove that and is indeed symmetric, and declare this as an instance of the Symmetric class. Once this is done, symmetry works as you expect it to.

The symmetry tactic is specifically for reasoning about equalities (a = b iff b = a).
I don't know of general machinery for commutative operators, but you can find the lemma you need with something like this:
Search _ (?a /\ ?b <-> ?b /\ ?a)
(I cheated: originally, I used -> and only switched to <-> when that didn't find anything!)
There's exactly one lemma of this form in the base library and it's called and_comm. You should be able to solve your remaining proof obligation with it: apply and_comm.

Related

How does one import the lia tactic given that the omega tactic was reprecated in Coq?

I got the error:
Error: The reference lia was not found in the current environment.
how do I fix it?
code:
Require Import Lia.
Theorem t:
forall n: nat, 1 + n > n.
Proof.
Show Proof.
intro.
Show Proof.
lia.
Show Proof.
Qed.
Do Require Import Lia. at the top. e.g.
Require Import Lia.
Theorem t:
forall n: nat, 1 + n > n.
Proof.
Show Proof.
intro.
Show Proof.
lia.
Show Proof.
Qed.
note this is a replacement for Omega
Edit:
As Ana Borges kindly pointed out:
Note that according to the Coq changelog, omega was only deprecated in Coq 8.12 and removed in 8.14. If you are using an older version of Coq, you can still make use of it. However, even in 8.11 the recommendation was to switch to lia, and unless you have a very good reason I would recommend updating your Coq installation anyway (the latest is 8.15.2).

Is a problem being undecidable equivalent to saying it's in NP-hard?

I understand how to prove that the halting problem is undecidable. However, I'm confused as to why it is NP-hard. Is a problem being undecidable equivalent to saying it's in NP-hard?
A problem is said to be "NP-hard" if there is a poly-time reduction from every NP problem to it.
The halting problem is indeed NP-hard, because of the following reason:
Let L be an NP problem, since L is in NP, it is decidable - thus there is a deterministic turing machine that decides L, denote it as M. Now we can create M', that simulates M and halts i.f.f M accepts x (where x is the input). And the result of the reduction on input x is (M', x).
Denote the reduction as R and indeed x is in L i.f.f R(x) is in H.
(M(x) = 1 => M'(x) halts => (M', x) is in H.
M(x) = 0 => M'(x) doesn't halt => (M', x) is not in H)
Why is the reduction polynomial? the size of the description of M may be very large, but it's FIXED. So it takes constant time to create M' (one can look at it as M' being "hardcoded" in R), so the reduction is linear and so polynomial.
Because of the transitivity of reduction (i.e., if there is a (polynomial/ computational) reduction from L1 to L2 and from L2 to L3 (MUST BE SAME REDUCTION TYPE!) there is a reduction from L1 to L3) it is enough to show a reduction from a known NP-hard problem to a new problem to prove that the new problem is NP-hard, and that's the common way to do so. In this example, it was convenient to prove directly from the definiton.
As of your other question, the answer is no. there exists an undecidable problem which is not NP-hard.
You can see a proof here:
https://math.stackexchange.com/questions/642726/are-all-undecidable-problems-np-hard
Note that if P=NP, every problem (except for the trivial sets) is NP-hard. so The proof assumes P != NP.

lapack C code for matrix inversion

I am programming in c to implement the matrix inversion by calling zgetrf and zgetri routines. Here I tried two different matrices, M and N. The weird part is that M works perfectly but when I added N in the code, it shows segmentation fault. I already use matlab to calculate the result, and the availability of N is totally fine. Anyone could help me out? Thank you.
code screenshot

Can someone explain me this boolean algebra equation

Can someone explain me this boolean algebra equation, using the table method and telling me explanation?
xy->z
I know the boolean logical operator "->", but what does xy mean? Is this only x, only y, or is it a mistake in the following equation?
xy is the logical AND of x and y. In your notation the 'v' symbol is used as logical OR. Usually '+' is used as OR as well.
Thus the left side of your equation reads "(x and y) or ([not x] implies [y and z])"

coq proof : tactic absurd, how does it works?

I am trying to understand a proof in coq. I wrote it long ago during a course but now I'm blocked by the absurd command.
Here is the proof :
Theorem Thm_2 : (~psi -> ~phi) -> (phi -> psi).
Proof.
intro.
intro.
cut (psi \/ ~psi).
intro.
elim H1.
intro.
exact H2.
intro.
absurd phi.
cut (~psi).
exact H.
exact H2.
exact H0.
apply classic.
Qed.
When I use the absurd phi tactic, my current goal is psi. And the absurd command transforms it in two goals : ~ phi and phi.
My problem is I can't figure nor remember the logic behind this step...
Thank you for your help !
(it seems I can't add a Hello at the beginning of my message... sorry)
If you can prove phi and ~ phi, then you can prove False (remember, ~ phi := phi -> False)
If you can prove False, then you can prove anything, including your goal at that point
So absurd phi applies False elimination, and have you prove False by means of proving both phi and ~ phi.