我已经向这个证据提出了一个不同的问题,所以我很抱歉重复。只是没有人会回答,因为另一个人有答案。
我试图通过结构归纳证明以下陈述:
foldr f st (xs++yx) = f (foldr f st xs) (foldr f st ys) (foldr.3)
这些是foldr的定义:
foldr f st [] = st (foldr.1)
foldr f st x:xs = f x (foldr f st xs) (foldr.2)
现在我想开始处理将空列表传递给xs的基本情况。我有这个,但我不知道它是否正确。
foldr f st ([]++ys) = f (foldr f st []) (foldr f st ys)
LHS:
foldr f st ([]++ys)
= foldr f st ys by (++) and by (foldr.1)
RHS:
f (foldr f st []) (foldr f st ys) =
= f st (foldr f st ys) by (foldr.1)
= foldr f st ys by def of st = 0 and f = (+)
LHS = RHS, therefore base case holds
现在这就是我的归纳步骤:
Assume that:
foldr f st (xs ++ ys) = f (foldr f st xs) (foldr f st ys) (ind. hyp)
Show that:
foldr f st (x:xs ++ ys) = f (foldr f st x:xs) (foldr f st ys)
LHS:
foldr f st (x:xs ++ ys)
= f x (foldr f st (xs++yx)) (by foldr.2)
= f x (f (foldr f st xs) (foldr f st ys) ) (by ind. hyp)
= f (f x (foldr f st xs)) (foldr f st ys) (by assosiativity of f)
RHS:
f (foldr f st x:xs) (foldr f st ys)
= f (f x (foldr f st xs)) (foldr f st ys) (by foldr.2)
LHS = RHS, therefore inductive step holds. End of proof.
我不确定此证明是否有效。我需要一些帮助来确定它是否正确,如果不正确 - 它的哪一部分不是。
答案 0 :(得分:4)
在您开始证明某事之前,请查看一些示例,例如
xs = ys = [] ; st = True ; f _ _ = False
答案 1 :(得分:2)
更新:问题被多次编辑。以下问题在问题revision 1中有意义。
这是不可能证明的。您需要f
成为幺半群:f a (f b c) == f (f a b) c
f a st == a
和f st a == a
。
可证明的陈述是foldr f st (xs++ys) == foldr f (foldr f st ys) xs
假设f
st
定义了一个monoid,我们得到以下语句:
foldr f st ([]++ys) ==
-- by definition of neutral element of list monoid, []++ys == ys
== foldr f st ys
-- by definition of neutral element of `f` monoid, f st a == a
== f st (foldr f st ys)
-- by definition of foldr, (foldr f st []) == st
== f (foldr f st []) (foldr f st ys)
然后,
foldr f st ((x:xs)++ys) ==
-- by associativity of list monoid, (x:xs)++ys == x:(xs++ys)
== foldr f st (x:(xs++ys))
-- by definition of foldr, foldr f st (x:t) == f x (foldr f st t)
== f x (foldr f st (xs++ys))
-- by induction, foldr f st (xs++ys) == f (foldr f st xs) (foldr f st ys)
== f x (f (foldr f st xs) (foldr f st ys))
-- by associativity of monoid `f`, f x (f y z) == f (f x y) z
== f (f x (foldr f st xs)) (foldr f st ys)
-- by definition of foldr, f x (foldr f st t) == foldr f st (x:t)
== f (foldr f st (x:xs)) (foldr f st ys)
实质上,这是幺半群同态的证明。列表是一个自由的幺半群,同态存在,并且正是你正在寻找的形式 - 它是f
的变形。