The dlist package contains the DList
data type, which has lots of instances, but not Foldable
or Traversable
. In my mind, these are two of the most "list-like" type classes. Is there a performance reason that DList
is not an instance of these classes?
Also, the package does implement foldr
and unfoldr
, but none of the other folding functions.
DList a
is a newtype wrapper around[a] -> [a]
, which has ana
in a contravariant position, so it cannot implementFoldable
orTraversable
, or evenFunctor
directly. The only way to implement them is to convert to and from regular lists (see thefoldr
implementation), which defeats the performance advantage of difference lists.One alternative you should consider instead of
DList
is to use Church-encoded lists. The idea is that you represent a list as an opaque value that knows how to execute afoldr
over a list. This requires using theRankNTypes
extension:The downside to this is that there is no efficient
tail
operation for aChurchList
—folding aChurchList
is cheap, but taking repeated tails is costly...