[Haskell-cafe] Instances of constrained datatypes
Cale Gibbard
cgibbard at gmail.com
Wed Apr 6 17:52:37 EDT 2005
I don't believe you can, but it would be nice. There are certain
types, such as Set, where it's not really possible to just remove the
constraint from the data declaration, and yet it would be nice if sets
could be instances of Monad and Functor. Currently, to be an instance
of Functor or Monad, your type has to be a functor defined on the
whole category of types.
Could this issue be fixed somehow? Constrained instances would make
various typeclass-based libraries more applicable. What would it break
to allow instances where the types of functions defined by the
typeclass are further restricted? I suppose that checking that types
are correct becomes more difficult and non-local, because functions
which are defined using the typeclass won't already have that
constraint for obvious reasons. Still, the constraint is in the
instance, which must be around when the functions actually get
applied. There are probably bad interactions with the module system,
but I'm not certain.
People must have talked about this before... was a consensus reached
that I'm not aware of?
- Cale
On Apr 6, 2005 2:10 AM, Arjun Guha <guhaarju at grinnell.edu> wrote:
> This is a contrived example, but contains the essence of what I'd like
> to do. Suppose I have this datatype:
>
> > data (Eq v) => EqList v = EqList [v]
>
> I'd like to make it an instance of Functor. However, fmap takes an
> arbitrary function of type a -> b. I need an Eq constraint on a and
> b. Is there any way to do this without creating my own `EqFunctor'
> class with explicitly-kinded quantification:
>
> > class (Eq a) => EqFunctor (f :: * -> *) a where
> > eqfmap:: (Eq b) => (a -> b) -> f a -> f b
>
> Thanks.
>
> -Arjun
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
More information about the Haskell-Cafe
mailing list