[Haskell-cafe] Alternative to newtypes or orphan instances

Erik Hesselink hesselink at gmail.com
Mon Aug 24 10:06:52 UTC 2015

I meant that presumably, there are library functions like 'f :: C a =>
...' that you want to use with type T. But copying class C to C2, and
only giving an instance C2 T doesn't allow you to do that.


On 24 August 2015 at 11:54, Clinton Mead <clintonmead at gmail.com> wrote:
> The original class still exists, I can't see how making a new class based on
> the old on affects that. Won't existing functions in modules which import
> the old class instead of the new class continue to work?
> On Monday, 24 August 2015, Erik Hesselink <hesselink at gmail.com> wrote:
>> On 24 August 2015 at 09:18, Clinton Mead <clintonmead at gmail.com> wrote:
>> > A second approach is an orphan instance. The recommendation here is to
>> > put
>> > the orphan instances in their own module, so the user can choose to
>> > import
>> > them.
>> >
>> > This may works ok if your user is writing an executable. But what if
>> > your
>> > user is writing a library themselves. But once, you, or your user,
>> > directly
>> > uses one of the instances, they need to import it, and they pollute the
>> > global instance namespace for anyone that uses their package.
>> For this reason, I think the recommended course of action is to make a
>> canonical place for the instance, so that everyone can use it. For
>> example, if you have a library 'foo' providing T, and a library 'bar'
>> providing C, put the instance in a new package 'foo-bar' (or
>> 'bar-foo'). Then everyone can use that one instance, since Haskell is
>> built on the assumption that every type has one unique instance per
>> class.
>> > I want to suggest a third option:
>> >
>> > (3) Copying the class.
>> This would make a new distinct class, which means you can't call any
>> methods which have the original class as the context (f :: C a => a ->
>> a) since that class won't exist for type T (you are trying to avoid
>> defining that orphan instance). So I don't think this is usable in
>> most cases, unless I'm missing something.
>> Erik

More information about the Haskell-Cafe mailing list