[Haskell-cafe] Safe Haskell and instance coherence

Mikhail Glushenkov the.dead.shall.rise at gmail.com
Wed Oct 17 23:49:24 CEST 2012

Hello David,

On Wed, Oct 17, 2012 at 6:02 PM, David Mazieres expires 2013-01-15 PST
<mazieres-sebg4mvthwuzk9r27pqmr8jndi at temporary-address.scs.stanford.edu>
> Can you elaborate on how this can be used to break the data structure
> invariant?  If in safe Haskell you import two modules that have
> overlapping instances, you will not be able to use the two instances.
> Modules that import only one instance will be able to use that
> instance.

Please take a look at the code example I provided:


I don't use overlapping instances or any other extensions besides Safe
Haskell. By defining two orphan Ord instances for U I'm able to
construct a value of type Set U that contains two equal elements:

> test
fromList [X,Y,X]

This is what I meant by "breaking the data structure invariant". This
shouldn't normally be possible: the documentation for Data.Set.insert
says: "If the set already contains an element equal to the given
value, it is replaced with the new value."

Regarding your Monoid example, it will still be possible to make it
work even if instance coherence is enforced by using a newtype wrapper
(in fact, Data.Monoid already includes Sum and Product newtype
wrappers that provide this functionality).

()  ascii ribbon campaign - against html e-mail
/\  www.asciiribbon.org   - against proprietary attachments

More information about the Haskell-Cafe mailing list