[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 :(
> [...]
>   T.lhs:4:12:
>       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
> UndecidableInstances.

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[1] 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.

[1] http://www.cs.nott.ac.uk/~wss/Publications/DataTypesALaCarte.pdf

Live well,

More information about the Haskell-Cafe mailing list