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

David Feuer david.feuer at gmail.com
Sat Feb 28 19:41:22 UTC 2015


This is impossible. The type of fmap is

fmap :: Functor f => (a->b) -> f a -> f b

You can use a GADT to effectively restrict what a is, because the
caller won't be able to provide a non-bottom f a if a has the wrong
type. But the caller can choose absolutely any type for b, and there's
nothing you can do about that.

David

On Sat, Feb 28, 2015 at 2:11 PM, silvio <silvio.frischi at gmail.com> wrote:
> 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
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe


More information about the Haskell-Cafe mailing list