The orphan instance problem is well-known in Haskell. This module by no means purports to solve the problem, but provides a workaround that may be significantly less awful than the status quo in some cases.
Say I think that the
Name type should have an
But I don't control either the class or the type, so if I define the
instance, and then the template-haskell package defines one, my code
is going to break.
safeInstance can help me to solve this problem:
safeInstance ''IsString [t| Name |] [d| fromString = mkName |]
This will declare an instance only if one doesn't already exist. Now anyone importing your module is guaranteed to get an instance one way or the other.
This module is still highly experimental. The example given above does work, but anything involving type variables or complex method bodies may be less fortunate. The names of the methods are mangled a bit, so using recursion to define them may not work. Define the method outside the code and then use a simple binding as above.
If you use this code (successfully or unsuccessfully!), go fetch the maintainer address from the cabal file and let me know!
An empty type used only to signify a multiparameter typeclass in
To define an instance
Show a => Show (Wrapper a), you'd use:
safeInstance ''Show [t| Show a => Wrapper a |] [d| show _ = "stuff" |]
To define an instance of a multi-param type class, use the
MultiParams type constructor with a tuple:
safeInstance ''MonadState [t| MonadState s m => MultiParams (s, MaybeT m) |] [d| put = ... |]