Lemma rev_firstn : forall (x : list bool) (n : nat),
rev (firstn n x) = firstn n (rev x).
I have spent quite a bit of time on this. I start out with a sensible goal but always end up with a goal that is impossible to prove.
Here is my current approach:
Proof.
intros. generalize dependent x. induction n.
+ easy.
+ induction x.
- easy.
-
In my context, I now have:
IHn : forall x : list bool, rev (firstn n x) = firstn n (rev x)
IHx : rev (firstn (S n) x) = firstn (S n) (rev x)
and my goal is:
rev (firstn (S n) (a :: x)) = firstn (S n) (rev (a :: x))
Is there a way to generalize x in IHx so that I can specialize it to (a :: x)? Since I don't know the right tactic to do this, I try the following, and end up with the previously mentioned impossible goal.
Proof.
intros. generalize dependent x. induction n.
+ easy.
+ induction x.
- easy.
- assert (rev_cons : forall (b : bool) (l : list bool),
rev (b :: l) = rev l ++ [b]).
{ easy. } rewrite firstn_cons.
rewrite rev_cons. rewrite rev_cons. specialize (@IHn x).
rewrite IHn.
Goal: firstn n (rev x) ++ [a] = firstn (S n) (rev x ++ [a])
This goal is impossible because for n = 0 and rev x = h :: t, the goal
reduces to [a] = List.hd (rev (h :: t)) ++ [a]
.
Is this lemma actually unsound, or am I just missing some tactics?