An example of a Foldable which is not a Functor (or not Traversable)?

后端 未结 3 403
刺人心
刺人心 2020-12-02 16:45

A Foldable instance is likely to be some sort of container, and so is likely to be a Functor as well. Indeed, this says

A Foldable<

3条回答
  •  -上瘾入骨i
    2020-12-02 17:05

    Reading Beautiful folding I realized that any Foldable can be made a Functor by wrapping it into

    data Store f a b = Store (f a) (a -> b)
    

    with a simple smart contructor:

    store :: f a -> Store f a a
    store x = Store x id
    

    (This is just a variant of the Store comonad data type.)

    Now we can define

    instance Functor (Store f a) where
        fmap f (Store x g)   = Store x (f . g)
    
    instance (F.Foldable f) => F.Foldable (Store f a) where
        foldr f z (Store x g)    = F.foldr (f . g) z x
    

    This way, we can make both Data.Set.Set and Sjoerd Visscher's Weird a functor. (However, since the structure doesn't memoize its values, repeatedly folding over it could be very inefficient, if the function that we used in fmap is complex.)


    Update: This also provides an example of a structure that is a functor, foldable but not traversable. To make Store traversable, we would need to make (->) r traversable. So we'd need to implement

    sequenceA :: Applicative f => (r -> (f a)) -> f (r -> a)
    

    Let's take Either b for f. Then we'd need to implement

    sequenceA' :: (r -> Either b a) -> Either b (r -> a)
    

    Clearly, there is no such function (you can verify with Djinn). So we can neither realize sequenceA.

提交回复
热议问题