haskelltraversable

"Empty" traversable - does it make sense, is it provided in any library?


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

Solution

  • 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.)