我获得了以下2个版本的树平展程序,我被要求证明相同的行为。
flatten::Tree->[Int]
flatten (Leaf z) =[z] F.1
flatten (Node x y) =concat (flatten x)(flatten y) F.2
flatten'::Tree->Int->[Int]
flatten' (Leaf z) a =concat [z] a P.1
flatten' (Node x y) a =flatten' x (flatten' y a) P.2
concat->[a]->a->[a]
concat [] a =a C.1
concat (h:t) a =h:concat t a C.2
证明:
flatten' z a = concat (flatten z) a
基本案例:
LHS:
flatten' (Leaf z) a = concat [z] a By P.1
RHS:
concat (flatten z) a = concat (flatten (Leaf z)) a
= concat [z] a By F.1 and C.1
LHS=RHS, hence base case holds
归纳案例:
(唯一可能的感谢下面的那个人解释了感应如何 二叉树工作!)
Assume that:
flatten' x a = concat (flatten x) a Ind. Hyp1
flatten' y a = concat (flatten y) a Ind. Hyp2
Show that:
flatten'(Node x y) a = concat (flatten (Node x y)) a
LHS:
flatten' (Node x y) a =
flatten' x (flatten' y a) By P.2
flatten' x (concat (flatten y) a) By Ind. Hyp2
concat (flatten x) (concat (flatten y) a) By Ind. Hyp1
RHS:
concat (flatten (Node x y)) a =
concat (concat (flatten x) (flatten y)) a By F.2
concat (flatten x) (concat (flatten y) a) By C.2
LHS = RHS, hence inductive step holds. End of proof.
答案 0 :(得分:2)
当引入列表时,您的归纳假设是所需属性保留在列表尾部,并且您必须证明它也保留在整个列表中。
在树上,它只是略有不同:你的归纳假设是,想要的属性保持在两个子树上,你必须证明它也适用于整个树。
Assume that:
forall a, flatten' x a = concat (flatten x) a Ind. Hyp. 1
forall a, flatten' y a = concat (flatten y) a Ind. Hyp. 2
Show that:
forall a, flatten'(Node x y) a = concat (flatten (Node x y)) a
我想你现在可以猜到如何从这里开始,所以我不会破坏这种乐趣。您可能需要依赖concat
的某些基本属性来执行某些子步骤。
最后注意事项:在你的基本案例中,你提到C.1
作为理由 - 你确定你真的使用过它吗?