[Haskell-cafe] Num instance for Lazy ByteStrings (was: NumLazyByteString Package License)

Yitzchak Gale gale at sefer.org
Tue Mar 9 09:41:13 EST 2010

Henning Thielemann wrote:
>> Is NumLazyByteString a newtype around Bytestring.Lazy
>> that interprets the bit stream represented by the ByteString
>> as integer?

Thomas DuBuisson wrote:
> Not exactly.  There is not newtype wrapper.  NumLazyByteString is:
> instance Num L.ByteString where
>  ...

Are you absolutely certain that this is the one and only
canonical instance that can ever exist that makes sense?
Otherwise, please use a newtype wrapper.

The reason for this is one of the few remaining major warts
in Haskell - that there is absolutely *no way* to
prevent the export of instances from a module that you import.

So it is considered good practice in current Haskell
never to write an instance if anyone has ever published
one previously, even if you don't foresee ever using the
package in which the instance was defined. Otherwise,
someone someday may write code that indirectly depends
on both modules in some way, and that code will
mysteriously fail to compile.

In practice, that implies: never write an unwrapped
instance for a datatype that you did not define yourself,
and especially not for types that are published and are in
common usage, unless you are absolutely certain that
your instance is truly canonical and universal.

By saying that this is a wart, I am not saying that I think
it is a good idea to write more than one instance for
the same type intentionally. It is not. But there is no way to
prevent it from ever happening inadvertently. So it
is important for there to be a mechanism to prevent the
import of an unwanted instance when that comes up.

Your desire to write this instance is yet another case
that demonstrates why this is clearly a wart.


More information about the Haskell-Cafe mailing list