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

Anupam Jain ajnsit at gmail.com
Tue Nov 8 14:49:58 CET 2011


Hi all,

I was trying to do something very simple with GADTs when I ran into this
problem -

-- My datatype
data T o where
  Only ∷ o → T o
  TT ∷ T o1 → (o1 → o2) → T o2

-- Show instance for debugging
instance Show o ⇒ Show (T o) where
  show (Only o) = "Only " ⊕ (show o)
  show (TT t1 f) = "TT (" ⊕ (show t1) ⊕ ")"


When I try to compile this I get the following -

Could not deduce (Show o1) arising from a use of `show'
    from the context (Show o)



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.

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?

Thanks,
Anupam Jain
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20111108/cf1a9405/attachment.htm>


More information about the Haskell-Cafe mailing list