[Haskell] Class type constraining?

Hugo Pacheco hpacheco at gmail.com
Fri Jun 22 11:06:07 EDT 2007


Hi,
I have a set of functor definitions with corresponding instances

newtype Id a = Ident {unIdent :: a}       deriving Eq
newtype K b a = Const {unConst :: b}       deriving Eq


instance Functor Id where
   fmap f (Ident x) = Ident $ f x
instance Functor (K a) where
   fmap f (Const x) = Const x
(...)

And a class that creates a representation b for a functor f a:

class Functor f => C f a b | f a -> b where
   ftest :: f a -> b

instance C Id a a
instance C (K b) a b

I want to write some function

test :: (C f a b) => (a -> b)
test = ftest . undefined

But, as expected, the type checker claims not to satisfy the context, since
the function may be valid for any representable functor, as long as it has
an instance.
What I would want is to constrain the set of functors that the class applies
to, for which I have all the required instances. Is it possible in Haskell?

Sorry if this explanation is confusing.
Thanks in advance,
hugo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell/attachments/20070622/64f3e0b6/attachment.htm


More information about the Haskell mailing list