mapM_ for bytestring

Duncan Coutts duncan.coutts at googlemail.com
Wed Sep 11 16:15:05 CEST 2013


On Wed, 2013-09-11 at 15:56 +0200, Nicolas Trangez wrote:
> On Wed, 2013-09-11 at 14:35 +0100, Duncan Coutts wrote:
> > For mapM etc, personally I think a better solution would be if
> > ByteString and Text and other specialised containers could be an
> > instance of Foldable/Traversable.
> 
> Yes!
> 
> >  Those classes define mapM etc but
> > currently they only work for containers that are polymorphic in their
> > elements, so all specialised containers are excluded.
> 
> Indeed :-( Hence my question a couple of days ago.
> 
> >  I'm sure there
> > must be a solution to that (I'd guess with type families) and that
> > would
> > be much nicer than adding mapM etc to bytestring itself. We would then
> > just provide efficient instances for Foldable/Traversable.
> 
> I looked into this when this thread started, but bumped into at least
> one 'issue': when defining (OTOH) something like
> 
> import qualified Data.ByteString as BS
> import qualified Data.ByteString.Char8 as BS8
> 
> class Foldable t where
>     type Elem c :: *
>     foldr :: (Elem t -> b -> b) -> b -> t -> b
> 
> instance Foldable BS.ByteString where
>     type Elem BS.ByteString = Word8
>     foldr = BS.foldr
> 
> instance Foldable BS8.ByteString where
>     type Elem BS8.ByteString = Char
>     foldr = BS8.foldr
> 
> which fails because BS.ByteString and BS8.ByteString are the same.

Right. I fear there's very little one could do about that. We do have
one single type that is a specialised container for two types. That's
fairly unusual. I'd probably just use Word8, especially since Char stuff
is a bit iffy since it's really "Char8" not a full Char.

Duncan




More information about the Libraries mailing list