[Haskell-cafe] GATD and pattern matching
wren ng thornton
wren at freegeek.org
Sat Jun 12 04:19:27 EDT 2010
Felipe Lessa wrote:
> Well, I guess it can't be compiled at all :(
> Duplicate instance declarations:
> instance [incoherent] (Show a) => MaybeShow a
> -- Defined at T.lhs:4:12-32
> instance [incoherent] MaybeShow a -- Defined at T.lhs:7:12-22
Indeed, it can't be compiled because contexts don't work like that :)
> I think this solution still requires OverlappingInstances and
The standard solution for this kind of thing is to use a newtype wrapper
to capture the Show constraint, define the Nothing instance for @a@, and
use IncoherentInstances. It's ugly, but it works... mostly.
It works best if you're in an architecture where everything is a
functor--- e.g., if you're using the Data Types a la Carte trick.
That way you can enforce the discipline of using the newtype wrapper,
since you'll have to be using some kind of functor wrapper to get things
to typecheck. Otherwise, it can be too easy to forget to tag something
as Showable, and then you'll end up using the default Nothing instance
when you didn't mean to.
More information about the Haskell-Cafe