It sounds trivial but I can't find out what is supposed to do.

following is my type definition:

data CDeq q a = Shallow (q a)
              | Deep{ hq   ⦂ q a
                    , susp ⦂ CDeq q (q a)
                    , tq   ⦂ q a }

and I want it to have an instance of Show.

since GHC doesn't allow me to deriving here, I just try to write myself one:

instance (Show a, Show ????) => Show (CDeq q a) where
    ....

but I got stuck.

I don't know how to represent that for all type v, (q v) can be shown in haskell.

I can't just do following simply:

instance (Show a, Show (q a)) => Show (CDeq q a) where
    ....

since to show CDeq q (q a), Show (q (q a)) is required, then Show (q (q (q a))) is required, then on and on.

So I am wondering is there a syntax such that I can express the meaning I stated up there?

I once thought forall could be the solution to this, but it doesn't work:

instance (Show a, forall v. Show (q v)) => Show (CDeq q a)
share|improve this question
up vote 8 down vote accepted

There's a class Show1 in Prelude.Extras to represent "for all type v, (q v) can be shown".

class Show1 f where
  showsPrec1 :: Show a => Int -> f a -> ShowS
  default showsPrec1 :: (Show (f a), Show a) => Int -> f a -> ShowS
  showsPrec1 = showsPrec
  ...

You can use this to write a show instance for CDeq q a.

instance (Show a, Show1 q) => Show (CDeq q a) where
    ....

Where you would use show or showsPrec on a q x you'll instead use show1 or showsPrec1.

If you use these, you should also provide instances for CDeq q.

instance (Show1 q) => Show1 (CDeq q) where
    showsPrec1 = showsPrec
share|improve this answer
1  
does that imply no syntax help or proper solution for that? what should i do if i define instance for another class without such variation sibling? – HuStmpHrrr Aug 7 '15 at 22:13
2  
There's nothing I know of. There's a Data.Constraint.Forall module in the constraints package. There isn't much documentation to go with it; the only person I'm sure would be able to answer whether it can replace classes like Show1 is Edward Kmett. He left a tantalizing hint that it might be possible in this answer to a related question. – Cirdec Aug 7 '15 at 23:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.