I'm writing a piece of Haskell code that uses the abstraction of Traversable
. Behind this abstraction I want to be able to conceal all regular traversable structures like lists, trees, maps etc, with special case of Data.Functor.Identity.Identity
as an elementary structure containing single value. I would also want to cover a case of an "empty" structure. Does such "empty" traversable instance exist? Maybe it is already provided by any library?
My first (and maybe naive) attempt to define such instance would be as follows. Does it make sense?
data Empty a = Empty
instance Functor Empty where
fmap _ _ = Empty
instance Foldable Empty where
foldr _ init _ = init
instance Traversable Empty where
sequenceA _ = pure Empty
Traversable in Haskell unifies the concept of mapping over a container (getting a similary-shaped container in return) with the concept of "internal iterator" that performs an effect for each element.
Advanced Haskell The fifth one is Traversable. To traverse means to walk across, and that is exactly what Traversable generalises: walking across a structure, collecting results at each stop.
As far as types in base go, Proxy
is precisely that. Const ()
would also work. (There is also U1
, but that is part of the generics machinery, and might feel a tad out of place in other contexts.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With