Folding and steerable

In exploring the deeper Applicative I came to Traversable , although I already knew Foldable from LYHGG , I did not see the first, but I started reading the Haskell wiki about Traversable .

While reading, I realized why Foldable.fold parallel to Traversable.sequenceA and Foldable.foldMap parallel to Traversable.traverse .

I also saw that each Traversable also Foldable and Functor , and sequenceA and traversal have a default implementation in terms of each other:

 traverse f = sequenceA . fmap f sequenceA = traverse id 

So, as I saw in LYHGG that foldMap is the minimum complete definition for Foldable , I thought it was parallel to traverse , so fold (which is parallel to sequenceA ) would also be the minimum full definition (this is not) ... Foldable not Functor as Traversable , so we cannot apply this:

 foldMap f = fold . fmap f fold = foldMap id -- this is ok 

Why not every Foldable a Functor , and which will be an instance of Foldable , which is actually not a Functor ?

+5
source share
1 answer

As dfeuer says, Set is a good example of Foldable , which is not Functor .

Consider the type of Set.map :

 map :: Ord b => (a -> b) -> Set a -> Set b 

Note that this is almost fmap , but this requires the additional restriction of Ord b . Since you have this limitation, you cannot make it an instance of Functor .

Note that Set not a Haskell functor, even with this restriction. Given the smart settings for Eq instances, we may break the law that fmap f . fmap g === fmap (f . g) fmap f . fmap g === fmap (f . g) . See this question for a broader discussion:

Sets, Functors, and Confusion Eq

As noted there, Set is a (endo) functor in the β€œ Hask subcategory” with ordered types as sets and order-preserving mappings as morphisms.

Thus, even if this is not obvious, the fact that we cannot make Set functor actually hints at a genuine mathematical problem, and not just about the limitation of our machinery.

+6
source

Source: https://habr.com/ru/post/1244625/


All Articles