证明反转=rev

发布于 2024-11-05 23:00:09 字数 286 浏览 6 评论 0原文

我有一些任务要做,但不知道该怎么做:

reverse, rev :: [a] [a]

reverse [] = []
reverse (x:xs) = reverse xs ++ [x]

rev = aux [] where
    aux ys [] = ys
    aux ys (x:xs) = aux (x:ys) xs

“证明:reverse=rev”,

我们将不胜感激您的帮助。谢谢。

附言。我可以用一些例子来做到这一点,但我认为这不专业

I have some task to do, but don't know how to do it:

reverse, rev :: [a] [a]

reverse [] = []
reverse (x:xs) = reverse xs ++ [x]

rev = aux [] where
    aux ys [] = ys
    aux ys (x:xs) = aux (x:ys) xs

"Prove that : reverse=rev"

Your help would be appreciated. Thank you.

PS. I can do it using some example but i think thats not professional

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

她如夕阳 2024-11-12 23:00:09

你可以通过结构归纳法做出一个草率的证明,但如果你想要一个正确处理底部的证明,那就不那么简单了。

You can make a sloppy proof by structural induction, but if you want to a proof that handles bottom correctly it is less trivial.

独行侠 2024-11-12 23:00:09

就职。基本情况很简单。归纳步骤不应该太难。

Induction. The base case is trivial. The inductive step shouldn't be too hard.

悍妇囚夫 2024-11-12 23:00:09

我不会尝试直接证明等价性,而是为每个函数证明(使用归纳法)它实际上颠倒了列表。如果它们都反转列表,那么它们是等价的。

证明草图:

我们想要证明 rev 适用于所有列表:

基本情况长度为 0 的列表:
证明 rev [] 计算正确

归纳情况
证明对于任何 n,rev 可以反转任何长度为 n 的列表,假设 rev 可以反转任何长度最多为 n-1 的列表

Instead of trying to prove equivalence directly, I would for each function prove (using induction) that it actually reverses the list. If both of them reverse lists, then they are equivalent.

Proof sketch:

We want to prove that rev works for all lists:

base case lists of length 0:
prove that rev [] computes correctly

inductive case:
prove that for any n, rev can reverses any list of length n, assuming rev can reverse any list of length up to n-1

世界和平 2024-11-12 23:00:09

由于任何列表反转函数只能在给定有限列表的情况下产生任何输出,因此我们可以将此代码转换为 Coq(其中列表始终是有限的)并在那里证明所需的语句(忽略底部)。

这个证明不是我自己的 - 它是标准库中的证明的稍微修改版本。

Open Scope list_scope.

Require List.
Require Import FunctionalExtensionality.

Section equivalence.

  Variable A : Type.

  (* The reverse function is already defined in the standard library as List.rev. *)
  Notation reverse := (@List.rev A).

  Fixpoint aux (ys l2 : list A) :=
    match l2 with
      nil => ys
      | x :: xs => aux (x :: ys) xs
    end.

  Definition rev : list A -> list A
    := aux nil.

  Lemma aux_rev : forall l l', aux l' l = reverse l ++ l'.
  Proof.
    induction l; simpl; auto; intros.
    rewrite <- List.app_assoc; firstorder.
  Qed.

  Theorem both_equal : reverse = rev.
    extensionality xs.
    unfold rev.
    rewrite aux_rev.
    now rewrite List.app_nil_r.
  Qed.

End equivalence.

Since any list reversing function can only produce any output if given finite lists, we can translate this code into Coq (in which lists are always finite) and prove the desired statement there (ignoring bottom).

This proof is not my own - it is a slightly modified version of a proof from the standard library.

Open Scope list_scope.

Require List.
Require Import FunctionalExtensionality.

Section equivalence.

  Variable A : Type.

  (* The reverse function is already defined in the standard library as List.rev. *)
  Notation reverse := (@List.rev A).

  Fixpoint aux (ys l2 : list A) :=
    match l2 with
      nil => ys
      | x :: xs => aux (x :: ys) xs
    end.

  Definition rev : list A -> list A
    := aux nil.

  Lemma aux_rev : forall l l', aux l' l = reverse l ++ l'.
  Proof.
    induction l; simpl; auto; intros.
    rewrite <- List.app_assoc; firstorder.
  Qed.

  Theorem both_equal : reverse = rev.
    extensionality xs.
    unfold rev.
    rewrite aux_rev.
    now rewrite List.app_nil_r.
  Qed.

End equivalence.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文