[Haskell-cafe] Question about typeclass & constraints

jean-christophe mincke jeanchristophe.mincke at gmail.com
Tue Apr 8 07:25:17 UTC 2014


Hello

Consider the following data, class and instance:

class PP m where
    create :: a -> m a

data A a = A a

instance PP A where
    create a = A a

And then:

class CB a where
    fb :: a -> Int

data B m a = B (m a)

If I try to define an instance of PP for B with the following
implementation:

instance (PP m) => PP (B m) where
    create a =  let _ = fb a
                in B (create a)

GHC issues the (expected) error: Could not deduce (CB a) arising from a use
of 'fb' from the context (PP m).

So the workaround I am using is to define a new class PP' where the type a
is made more explicit. That allows the constraint (CB a) to be stated in an
instance declaration.

class PP' m a where
    create' :: a -> m a

instance (PP m) => PP' m a where
    create' = create

instance (PP m, CB a) => PP' (B m) a where
    create' a = let _ = fb a
                in B (create a)

 So, my question is: is there a way to achieve the same results without
having to create the new class PP'?

Thank you for your help

J-C
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140408/2eab8263/attachment.html>


More information about the Haskell-Cafe mailing list