Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"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
like image 962
Eryk Ciepiela Avatar asked Apr 28 '19 19:04

Eryk Ciepiela


People also ask

What is the traversable Typeclass used for?

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.

What is traversable Haskell?

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.


1 Answers

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

like image 180
duplode Avatar answered Sep 28 '22 11:09

duplode