[Haskell-cafe] Putting constraints on "internal" type variables in GADTs

Felipe Almeida Lessa felipe.lessa at gmail.com
Tue Nov 8 14:59:34 CET 2011

On Tue, Nov 8, 2011 at 11:49 AM, Anupam Jain <ajnsit at gmail.com> wrote:
> While I understand why I get this error, I have no idea how to fix it! I
> cannot put a Show constraint on o1 because that variable is not exposed in
> the type of the expression.

That means 'o1' is an existencial variable.

> I can work around this by changing my data type declaration to include Show
> constraints but I don't want to restrict my data type to only Showable
> things just so I could have a "Show" instance for debugging -
> Only ∷ Show o ⇒ o → T o
> TT ∷ (Show o1, Show o2) ⇒ T o1 → (o1 → o2) → T o2
> What else can I do to declare a Show instance for my datatype?

That's the only way of showing o1.  Note that you don't need 'Show o2'
constraint on 'TT', so this would suffice:

  data T o where
    Only :: o -> T o
    TT :: Show o1 => T o1 -> (o1 -> o2) -> T o2

  instance Show o => Show (T o) where

Without the inner Show constraint on TT you can't do any showing with
o1.  Since it is an existencial, it could be anything, even things
that don't have Show instances.

I think you may do something more complicated with the new
ConstraintKinds extesions, something like

  data T c o where
    Only :: o -> T o
    TT :: c o1 => T o1 -> (o1 -> o2) -> T o2

  instance Show o => Show (T Show o) where

This is completely untested.  And even if it works, I don't know if it
is useful =).



More information about the Haskell-Cafe mailing list