Class type constraining?
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
2007/6/22, Hugo Pacheco <hpacheco@gmail.com>:
class Functor f => C f a b | f a -> b where ftest :: f a -> b
I want to write some function
test :: (C f a b) => (a -> b) test = ftest . undefined
I'm not sure whether this is what you want, but the "obvious" way to make this type-check would seem to be to add a functional dependency and a type signature for 'undefined,' like this:
class Functor f => C f a b | f a -> b, a b -> f where ftest :: f a -> b
test :: (C f a b) => (a -> b) test = ftest . (undefined :: a -> f a)
- Benja
That doesn't help in any sense, my problem is that I need to use the class method, but I take the context from granted, but the compiler still needs a "general" instance for any functor f. hugo
participants (2)
-
Benja Fallenstein -
Hugo Pacheco