``Orphan instances'' should be avoided anyway.

Jonathan Cast jonathanccast at fastmail.fm
Tue Aug 12 13:42:56 EDT 2008


On Tue, 2008-08-12 at 17:38 +0000, kahl at cas.mcmaster.ca wrote:
> Henning Thielemann <lemming at henning-thielemann.de> wrote:
>  > 
>  > On Tue, 12 Aug 2008 kahl at cas.mcmaster.ca wrote:
>  > 
>  > > I just would like to point out that there is nothing inherently bad about
>  > > what GHC calls ``orphan instances''.
>  > >
>  > > From a code structuring point of view,
>  > > I frequently ``consider orphan'' instances
>  > > useful for separation of concerns.
>  > 
>  > The problem is, that if you have a main instance of a class for a type and
>  > this one is not bundled with either the type or the class, then you are
>  > able to import the type and the class without the main instance (that is,
>  > you can accidentally miss that instance)
> 
> Or on purpose --- this is in fact another use of ``orphan instances''
> I forgot to mention.
> 
>  > , and thus you are able to define another instance.
> 
> Indeed --- this is the only way to have different instances
> for the same class, as long as we do not have something like
> the ``named instances'' of our Haskell-2001 paper (shameless plug ;-).
> 
>  > This will likely cause clash with the main instance
>  > sooner or later, if other modules import your custom instance and the main
>  > one.
> 
> If there are several instances,
> there is very likely no ``main instance''.

If there is no main instance, there should very likely be no instance at
all.  We already have named instances:

data ShowDict alpha = ShowDict alpha {
    namedShows :: alpha -> String -> String
    }

show :: ?namedShow :: ShowDict alpha => alpha -> String
show x = namedShows ?namedShow x ""

Confusing this with type classes seems mostly redundant to me.

jcc




More information about the Libraries mailing list