【问题标题】:How do I prove the following lemma in Coq?如何在 Coq 中证明以下引理?
【发布时间】:2019-04-03 21:29:05
【问题描述】:
Lemma rev_firstn : forall (x : list bool) (n : nat),
rev (firstn n x) = firstn n (rev x).

我在这方面花了很多时间。我从一个合理的目标开始,但总是以一个无法证明的目标结束。

这是我目前的做法:

Proof.
  intros. generalize dependent x. induction n. 
  + easy. 
  + induction x.
    - easy. 
    - 

在我的上下文中,我现在有:

IHn : forall x : list bool, rev (firstn n x) = firstn n (rev x)
IHx : rev (firstn (S n) x) = firstn (S n) (rev x)

我的目标是:

rev (firstn (S n) (a :: x)) = firstn (S n) (rev (a :: x))

有没有办法在 IHx 中泛化 x 以便我可以将其专门用于 (a :: x)?由于我不知道执行此操作的正确策略,因此我尝试了以下操作,并最终达到了前面提到的不可能的目标。

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])

这个目标是不可能的,因为对于 n = 0 和 rev x = h :: t,目标 减少到[a] = List.hd (rev (h :: t)) ++ [a]

这个引理实际上是不合理的,还是我只是错过了一些策略?

【问题讨论】:

    标签: coq


    【解决方案1】:

    假设 firstnrev 是我认为的那样,我认为引理不正确。

    rev (firstn 2 [true, false, false])
    = rev [true, false]
    = [false, true]
    

    但是

    firstn 2 (rev [true, false, false])
    = firstn 2 [false, false, true]
    = [false, false]
    

    基本上,rev (firstn n x)x 的第一个n 元素(倒序),但firstn n (rev x)x 的最后一个n 元素(也倒序)。要使这个引理在任何一般性中成立,您将需要 x 最多具有 n 元素。正如 Arthur Azevedo De Amorim 在 cmets 中指出的那样,如果您插入 skipn n 以查看 x 的最后一个 n(最多)元素,您也可以获得此引理的正确版本。

    rev (firstn n (skipn (length x - n)) x) = firstn n (rev x)

    【讨论】:

    • 正确的说法应该是firstn n (rev x) = rev (skipn (length x - n) x)
    • 它在 mathcomp 的 seq 库中得到证明(其中函数被称为 takedropsize,而不是 firstnskipnlength )。
    • 你是对的。我正在养成在脑海中陈述这些不合理陈述的习惯,并在考虑它们之前付出很多努力来证明它们。我想这是 Coq 的一个实用程序:)。谢谢你们!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多