Safe Haskell | None |
---|
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 IsString
instance.
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!
Documentation
data MultiParams a Source
An empty type used only to signify a multiparameter typeclass in
safeInstance
.
safeInstance :: Name -> Q Type -> Q [Dec] -> Q [Dec]Source
safeInstance
is a more convenient version of safeInstance'
that takes the context and type from a
with the intention
that it be supplied using a type-quote.
Q
Type
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 = ... |]
safeInstance' :: Name -> Cxt -> [Type] -> Q [Dec] -> Q [Dec]Source
produces an instance
of the given class if and only if one doesn't already exist.
safeInstance'
className cxt types methods
See safeInstance
for a simple way to construct the Cxt
and
[
parameters.
Type
]