证明反转=rev
我有一些任务要做,但不知道该怎么做:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以通过结构归纳法做出一个草率的证明,但如果你想要一个正确处理底部的证明,那就不那么简单了。
You can make a sloppy proof by structural induction, but if you want to a proof that handles bottom correctly it is less trivial.
就职。基本情况很简单。归纳步骤不应该太难。
Induction. The base case is trivial. The inductive step shouldn't be too hard.
我不会尝试直接证明等价性,而是为每个函数证明(使用归纳法)它实际上颠倒了列表。如果它们都反转列表,那么它们是等价的。
证明草图:
我们想要证明 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
由于任何列表反转函数只能在给定有限列表的情况下产生任何输出,因此我们可以将此代码转换为 Coq(其中列表始终是有限的)并在那里证明所需的语句(忽略底部)。
这个证明不是我自己的 - 它是标准库中的证明的稍微修改版本。
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.