gpt4 book ai didi

haskell - 这总是正确的: fmap (foldr f z) .sequenceA=foldr(liftA2f)(purez)

转载 作者:行者123 更新时间:2023-12-02 06:34:52 25 4
gpt4 key购买 nike

import Prelude hiding (foldr)

import Control.Applicative
import Data.Foldable
import Data.Traversable

left, right :: (Applicative f, Traversable t) => (a -> b -> b) -> b -> t (f a) -> f b
left f z = fmap (foldr f z) . sequenceA
right f z = foldr (liftA2 f) (pure z)

我强烈怀疑表达式 left 和 right 相等,但是如何证明呢?

最佳答案

至少这是一个开始:

\f z -> fmap (foldr f z) . sequenceA
== (definition of Foldable foldr)
\f z -> fmap (foldr f z . toList) . sequenceA
== (distributivity of fmap)
\f z -> fmap (foldr f z) . fmap toList . sequenceA
== (need to prove this step, but it seems intuitive to me)
\f z -> fmap (foldr f z) . sequenceA . toList

\f z -> foldr (liftA2 f) (pure z)
== (definition of Foldable foldr)
\f z -> foldr (liftA2 f) (pure z) . toList

如果你能证明fmap toList 。序列A = 序列A 。 toList,并且您最初的声明适用于 t = [] 您应该可以开始了。

关于haskell - 这总是正确的: fmap (foldr f z) .sequenceA=foldr(liftA2f)(purez),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6057059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com