[Haskell-cafe] Orphan instances

Simon Peyton-Jones simonpj at microsoft.com
Wed Mar 5 06:26:02 EST 2008


| GHC emits warning about orphan instance A (T t), since neither A nor T are
| defined in B. However I can't move the instance to A or T since it depends
| on B. Would it be fine to lift the restriction, such that it is accepted
| to declare the instance in a module where the type or the class or a
| superclass (this is new) is defined?

The issue is this.  Suppose GHC is compiling a module X, somewhere far away from the modules A,T,B which you define.  While compiling X, GHC needs to solve the class constraint
        A (T Int)
Well, in order to make sense of the constraint, GHC will have read the interfaces for module A and T, so that it knows that A is indeed a class, and T is indeed a type constructor.

But so far nothing says that it needs to read the interface for B.  But suppose that B indeed appears in the transitive closure of X's imports.  Then the programmer expects GHC to find the instance in B.

To ensure that it does, GHC reads the interface of every orphan module in the transitive closure of X's imports.  (An orphan module is one like B that defines an instance whose head does not mention something defined locally.)  The warning is just so that you know this is going to happen.  It's something to avoid when unnecessary, but not a bug.


I think you can see from this explanation that simply declaring that B isn't an orphan module after all isn't going to help!

Simon


| -----Original Message-----
| From: haskell-cafe-bounces at haskell.org [mailto:haskell-cafe-bounces at haskell.org] On Behalf Of Henning Thielemann
| Sent: 05 March 2008 06:53
| To: Haskell Cafe
| Subject: [Haskell-cafe] Orphan instances
|
|
| Consider the following modules
|
|
| module A
|
| class A t
|
|
| module T
|
| import A
|
| data T a
|
|
| module B
|
| import A
| import T
|
| class A t => B t
|
| instance B t => A (T t)
|
|
|
| GHC emits warning about orphan instance A (T t), since neither A nor T are
| defined in B. However I can't move the instance to A or T since it depends
| on B. Would it be fine to lift the restriction, such that it is accepted
| to declare the instance in a module where the type or the class or a
| superclass (this is new) is defined?
|
|
| In my example class B provides conversion toInteger (that is Integral),
| class A provides conversion toRational (that is Real) and T is Ratio.
| _______________________________________________
| Haskell-Cafe mailing list
| Haskell-Cafe at haskell.org
| http://www.haskell.org/mailman/listinfo/haskell-cafe


More information about the Haskell-Cafe mailing list