[Haskell-cafe] is it possible to implement Functor for ByteString and Text

silvio silvio.frischi at gmail.com
Sat Feb 28 19:11:05 UTC 2015


I have recently heard that some people want to burn bridges (introducing
Foldable and Traversable to Prelude) and I've been wondering if it was
possible somehow allow Text and Bytestring like containers to make use
of those functions. Something along the lines of

import qualified Data.ByteString as BS

newtype ByteString' a = ByteString' BS.ByteString

type ByteString = ByteString' Word8

instance (ByteString' a ~ ByteString' Word8) => Functor (ByteString')
    where
        fmap f (ByteString' bs) = ByteString' $ BS.map f bs


Or if DataContexts worked as you would expect.

newtype (Word8 ~ a) => ByteString' a = ByteString' BS.ByteString

However I couldn't find a solution and I was just wondering if it is
possible.

P.S. Using GADTS it does actually work for Foldable, but only because it
doesn't have to output any ByteStrings. It doesn't work for Functor for
instance.

data ByteString' a where
	ByteString' :: BS.ByteString -> ByteString' Word8

type ByteString = ByteString' Word8

instance Foldable ByteString' where
	foldr f ini (ByteString' bs) = BS.foldr f ini bs


Silvio


More information about the Haskell-Cafe mailing list