开发者

Is this always true: fmap (foldr f z) . sequenceA = foldr (liftA2 f) (pure z)

开发者 https://www.devze.com 2023-03-06 19:44 出处:网络
开发者_开发百科import Prelude hiding (foldr) import Control.Applicative import Data.Foldable import Data.Traversable
开发者_开发百科
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)

I have a strong suspicion that the expressions left and right are equal, but how to prove it?


Here's a start at least:

\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

If you can prove that fmap toList . sequenceA = sequenceA . toList, and that your original claim holds for t = [] you should be good to go.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号