Search code examples
coqlogical-foundations

Logic: In_app_iff exercize


Trying to solve In_app_iff excersize from Logic chapter I came to this monstrosity:

(* Lemma used later *)
Lemma list_nil_app : forall (A : Type) (l : list A),
    l ++ [] = l.
Proof.
  intros A l. induction l as [| n l' IHl'].
  - simpl. reflexivity.
  - simpl. rewrite -> IHl'. reflexivity.
Qed.

(** **** Exercise: 2 stars, standard (In_app_iff)  *)
Lemma In_app_iff : forall A l l' (a:A),
  In a (l++l') <-> In a l \/ In a l'.
Proof.
  intros A l l' a. split.
  + induction l as [| h t IHl].
    ++ (* l = [] *) destruct l' as [| h' t'].
       +++ (* l' = [] *) simpl. intros H. exfalso. apply H.
       +++ (* l' = h'::t' *) simpl. intros [H1 | H2].
          * right. left. apply H1.
          * right. right. apply H2.
    ++ (* l = h::t *) destruct l' as [| h' t'].
      +++ (* l' = [] *) simpl. intros [H1 | H2].
          * left. left. apply H1.
          * left. right. rewrite list_nil_app in H2. apply H2.
      +++ (* l' = h'::t' *) intros H. simpl in H. simpl. destruct H as [H1 | H2].
          * left. left. apply H1.
          * apply IHl in H2. destruct H2 as [H21 | H22].
            ** left. right. apply H21.
            ** simpl in H22. destruct H22 as [H221 | H222].
               *** right. left. apply H221.
               *** right. right. apply H222.
  + induction l as [| h t IHl].
    ++ (* l = [] *) simpl. intros [H1 | H2].
      +++ exfalso. apply H1.
      +++ apply H2.
    ++ (* l = h::t *) destruct l' as [| h' t'].
      +++ simpl. intros [H1 | H2].
          ++++ rewrite list_nil_app. apply H1.
          ++++ exfalso. apply H2.
      +++ simpl. intros [H1 | H2].
          ++++ destruct H1 as [H11 | H12].
              +++++ left. apply H11.
              +++++

Here is what I got at the end:

A : Type
h : A
t : list A
h' : A
t' : list A
a : A
IHl : In a t \/ In a (h' :: t') -> In a (t ++ h' :: t')
H12 : In a t
============================
h = a \/ In a (t ++ h' :: t')

How can I get from H12 and IHl the fact that In a (t ++ h' :: t')?

Because H12 is in disjunction. And it's enough to infer the conclusion.

apply H12 in IHl. doesn't work.

Please, help.


Solution

  • There are different ways to go about this.

    Here the conclusion of IHl is one of the clauses of the goal, so backward reasoning will work quite nicely.

    right. (* We will prove the right hand side of the disjunct. *)
    apply IHl.
    left.
    apply H12.
    

    Forward reasoning is also possible, though a bit more verbose. Use assert to prove the assumption actually needed by IHl:

    assert (preIHl : In a t \/ In a (h' :: t')).
    - ...
    - apply IHl in preIHl.
      apply preIHl.