Search code examples
coqquantifiersfirst-order-logicdemorgans-law

DeMorgan's law for quantifiers in Coq


I am trying to prove some FOL equivalences. I am having trouble using DeMorgan's laws for quantifiers, in particular

~ (exists x. P(x)) <-> forall x. ~P(x)

I tried applying not_ex_all_not from Coq.Logic.Classical_Pred_Type., and scoured StackOverflow (Coq convert non exist to forall statement, Convert ~exists to forall in hypothesis) but neither came close to solving the issue.

Theorem t3: forall (T: Type), forall p q: T -> Prop, forall r: T -> T -> Prop, 
~(exists (x: T), ((p x) /\ (exists (y: T), ((q y) /\ ~(r x y)))))
<-> forall (x y: T), ((p x) -> (((q y) -> (r x y)))).
Proof.
intros T p q r.
split.
- intros H.
apply not_ex_all_not.

I get this error:

In environment
T : Type
p, q : T → Prop
r : T → T → Prop
H : ¬ (∃ x : T, p x ∧ (∃ y : T, q y ∧ ¬ r x y))
Unable to unify
 "∀ (U : Type) (P : U → Prop), ¬ (∃ n : U, P n) → ∀ n : U, ¬ P n"
with "∀ x y : T, p x → q y → r x y".

I expected DeMorgan's law to be applied to the goal resulting in a negated existential.


Solution

  • Let's observe what we can derive from H:

    ~ (exists x : T, p x /\ (exists y : T, q y /\ ~ r x y))
    => (not exists <-> forall not)
    forall x : T, ~ (p x /\ (exists y : T, q y /\ ~ r x y))
    => (not (A and B) <-> A implies not B)
    forall x : T, p x -> ~ (exists y : T, q y /\ ~ r x y)
    =>
    forall x : T, p x -> forall y : T, ~ (q y /\ ~ r x y)
    =>
    forall x : T, p x -> forall y : T, q y -> ~ (~ r x y)
    

    We end up with a double negation on the conclusion. If you don't mind using a classical axiom, we can apply NNPP to strip it and we're done.

    Here is the equivalent Coq proof:

    Require Import Classical.
    
    (* I couldn't find this lemma in the stdlib, so here is a quick proof. *)
    Lemma not_and_impl_not : forall P Q : Prop, ~ (P /\ Q) <-> (P -> ~ Q).
    Proof. tauto. Qed.
    
    Theorem t3: forall (T: Type), forall p q: T -> Prop, forall r: T -> T -> Prop, 
    ~(exists (x: T), ((p x) /\ (exists (y: T), ((q y) /\ ~(r x y)))))
    <-> forall (x y: T), ((p x) -> (((q y) -> (r x y)))).
    Proof.
    intros T p q r.
    split.
    - intros H x y Hp Hq.
      apply not_ex_all_not with (n := x) in H.
      apply (not_and_impl_not (p x)) in H; try assumption.
      apply not_ex_all_not with (n := y) in H.
      apply (not_and_impl_not (q y)) in H; try assumption.
      apply NNPP in H. assumption.
    

    The above was a forward reasoning. If you want backwards (by applying lemmas to the goal instead of hypotheses), things get a little harder, because you need to build the exact forms before you can apply the lemmas to the goal. This is also why your apply fails. Coq doesn't automatically find where and how to apply the lemma out of the box.

    (And apply is a relatively low-level tactic. There is an advanced Coq feature that allows to apply a propositional lemma to subterms.)

    Require Import Classical.
    
    Lemma not_and_impl_not : forall P Q : Prop, ~ (P /\ Q) <-> (P -> ~ Q).
    Proof. tauto. Qed.
    
    Theorem t3: forall (T: Type), forall p q: T -> Prop, forall r: T -> T -> Prop, 
    ~(exists (x: T), ((p x) /\ (exists (y: T), ((q y) /\ ~(r x y)))))
    <-> forall (x y: T), ((p x) -> (((q y) -> (r x y)))).
    Proof.
    intros T p q r.
    split.
    - intros H x y Hp Hq.
      apply NNPP. revert dependent Hq. apply not_and_impl_not.
      revert dependent y. apply not_ex_all_not.
      revert dependent Hp. apply not_and_impl_not.
      revert dependent x. apply not_ex_all_not. apply H.
    

    Actually, there is an automation tactic called firstorder, which (as you guessed) solves first-order intuitionistic logic. Note that NNPP is still needed since firstorder doesn't handle classical logic.

    Theorem t3: forall (T: Type), forall p q: T -> Prop, forall r: T -> T -> Prop, 
    ~(exists (x: T), ((p x) /\ (exists (y: T), ((q y) /\ ~(r x y)))))
    <-> forall (x y: T), ((p x) -> (((q y) -> (r x y)))).
    Proof.
    intros T p q r.
    split.
    - intros H x y Hp Hq. apply NNPP. firstorder.
    - firstorder. Qed.