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. I suspect that some things like recursion still won't work, because of how the names are mangled. Let me know how you get on!
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 = ... |]