Coq: keeping information in a match statement

喜欢而已 提交于 2019-12-05 11:46:46

To remember what the list you are pattern-matching on looks like, you need to simply change the return type of your match like so.

Fixpoint picksome (l:list nat) (H : Acc lt (length l)) {struct H}: list nat.
    refine (
        (match l as m return l = m -> list nat with
            nil       => fun Hyp => nil
          | cons a l' => fun Hyp => cons a (picksome (drop a l') _)
        end) (eq_refl l)
      ).

What this match l as m return l = m -> list nat is saying is that you are performing a pattern matching on l, that you'll call the matching form m and that, given a proof that l equals m, you'll build a list of nats.

Now, the type of the match block will be slightly different: instead of just delivering a list nat, it will deliver a function of type l = l -> list nat. Luckily for us, eq_refl l delivers a proof that l is equal to itself so we can apply the match to that and get back our initial list nat.

Looking at the branches of the match, we can see that:

  • In the nil case, you can ignore the extra Hypothesis which you don't need.

  • In the cons case, it provides you precisely the much needed hypothesis and you can discharge your proof obligation like so:

    apply H.
    rewrite Hyp.
    simpl.
    apply le_lt_n_Sm.
    apply drop_lemma_le.
    

    Defined.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!