[英]Structural induction on binary trees
Consider the following function definitions:考虑以下函数定义:
data Tree a = Leaf a | Node a (Tree a) (Tree a)
sumLeaves :: Tree a -> Integer
sumLeaves (Leaf x) = 1
sumLeaves (Node _ lt rt) = sumLeaves lt + sumLeaves rt
sumNodes :: Tree a -> Integer
sumNodes (Leaf x) = 0
sumNodes (Node _ lt rt) = 1 + sumNodes lt + sumNodes rt
Task: Prove by structural induction that for all finite trees t:: Tree a holds:任务:通过结构归纳证明对于所有有限树 t:: Tree a 成立:
sumLeaves t = sumNodes t + 1 sumLeaves t = sumNodes t + 1
I now have:我现在有:
sumLeaves t = sumNodes t + 1
I.A. t = (Tree 0) = (Leaf 0)
sumLeaves (Leaf 0) = 1 sumNodes (Leaf 0) + 1
0 + 1 = 1
1 = 1
I.V.: We assume that the statement holds for an arbitrary t.
IS:是:
Now what is the induction step, I really have problems with solving this last step.现在什么是归纳步骤,我真的很难解决这最后一步。
Similar to the induction over natural numbers, where the induction step is assuming that some hypothesis holds for all numbers less than n , and then proving the hypothesis for n, structural induction step assumes that hypothesis holds for all structures that are substructures of a given structure .类似于对自然数的归纳,其中归纳步骤假设某个假设对所有小于 n的数字都成立,然后证明对 n 的假设,结构归纳步骤假设假设对作为给定结构的子结构的所有结构都成立.
In your case, correct assumption would be that the statement holds for trees lt
and rt
that are subtrees of the arbitrary t@(Node x lt rt)
, and then proving the statement for t
.在您的情况下,正确的假设是该语句适用于作为任意t@(Node x lt rt)
子树的树lt
和rt
,然后证明t
的语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.