|
|
|
|
|
by lgas
1452 days ago
|
|
I thought it was the case that you can't implement the Monad type class using the built in Data.Set implementation of Sets but there's no reason in principle why you couldn't build a Set Monad that works that way, right? Perhaps I am mistaken so I will investigate further. |
|
In order to be a functor, you need `fmap (f . g) == fmap f . fmap g`.
The problem is that some functions (for instance, Data.Set.showTree) can produce different results for two values that compare equal.
For instance, imagine we have a set containing (only) the lists `[1,2]` and `[2,1]`; let's call it s. How many elements are in `fmap (Data.Set.showTree . Data.Set.fromList) s`? Two, because the result of fromList winds up being order dependent in the structure of the internal tree, even though both Sets represent the same set of values. On the other hand, how many elements in `fmap showTree . fmap fromList`? Just one, because the Set has a chance to "see" the output of fromList and notice that they are equal according to Eq.
Does this violation of the functor laws matter? Maybe; likely not; it depends on your use case.
But mathematically it means that the thing we are working with is not a functor, and so the thing we are working with is also not a monad (as all monads are functors).