证明foldr f st(xs ++ ys)= f(foldr f st xs)(foldr f st ys)

时间:2014-11-17 03:28:23

标签: haskell induction

我试图通过结构归纳证明以下陈述:

foldr f st (xs++yx) = f (foldr f st xs) (foldr f st ys)        (foldr.3)

但是我甚至不确定如何定义foldr,所以我被困住了,因为没有为我提供任何定义。我现在相信foldr可以定义为

foldr f st [] = st                                             (foldr.1)
foldr f st x:xs = f x (foldr f st xs)                          (foldr.2)

现在我想开始处理将空列表传递给foldr的基本情况。我有这个,但我不认为这是正确的。

foldr f st ([]++[]) = f (foldr f st []) (foldr f st [])
LHS:
    foldr f st ([]++[]) = foldr f st []                        by (++)
    foldr f st [] = st                                         by (foldr.1)
RHS:
    f (foldr f st []) (foldr f st []) = f st st                by (foldr.1)
    = st                                                       by definition of identity, st = 0

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)    (inductive step)

LHS:
    foldr f st (x:xs ++ ys) = f x (foldr f st xs) (foldr f st ys)  (by foldr.2)
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)
  = f x (foldr f st xs) (foldr f st ys)

LHS = RHS, therefore inductive step holds. End of proof.

我不确定此证明是否有效。我需要一些帮助来确定它是否正确,如果不正确 - 它的哪一部分不是。

1 个答案:

答案 0 :(得分:2)

首先:您可以通过API文档找到许多基本Haskell函数的定义,该文档可在Hackage上找到。 base的文档是herefoldr导出foldr :: (a -> b -> b) -> b -> [a] -> b foldr k z = go where go [] = z go (y:ys) = y `k` go ys ,其中包含指向Prelude的链接:

foldr f st [] = st
foldr f st (y:ys) = f y (foldr f st ys) 

出于效率原因,它的定义是这样的;查找"工人包装。"它相当于

f

第二:在您所需的证明中,a -> a -> a的类型必须为a -> b -> b,这不如xs = ys = []一般。

让我们完成基本案例(foldr f st ([]++[]) = f (foldr f st []) (foldr f st []) -- Definition of ++ foldr f st [] = f (foldr f st []) (foldr f st []) -- Equation 1 of foldr st = f st st )。

st

这个等式一般不成立。要继续进行校对,您必须假设ff的身份。

我相信,在非基本情况下,您还必须假设f是关联的。这两个假设相结合,表明st和{{1}}形成了一个幺半群。您是否想要证明its source code的某些内容?