[Haskell] Instances That Ignore Type Constraints? (HList-related)

Ralf Lammel ralfla at microsoft.com
Thu Oct 27 00:41:47 EDT 2005


'isBoxed (Carton func)' fails because there is no way whatsoever that any expression context will disambiguate the x y type variables of func's type. That is, the type of `isBoxed (Box func)` does not expose x (and y -- the latter being irrelevant because of the fundep); so there is no way that the type system could ever figure out which x (and y) was meant; hence overloading will never ever get resolved (check out the hugs type error, which tells you exactly that).

One could argue in favor of incoherent choice of instances.
(I don't!)

Say, if you had:

{-# OPTIONS -fallow-undecidable-instances #-}
{-# OPTIONS -fallow-incoherent-instances #-}
instance Func x x -- or some other generic instance

It will work fine:

ghci> isBoxed (Carton func)

However, why would you want to wrap such a function anyhow, if you are not going to use it? :-) Perhaps, this is a case of superficial, say unintended polymorphism.

If you *really* wanted to box (or to carton) a polymorphic type-class bounded function, then you may need to do something like this:

data WFunc = WFunc (forall x y. Func x y => (x -> y))
hugs-or-ghci> isBoxed (Carton (WFunc func))

This works fine ... but this is probably *not* what you wanted in the first place. Also, this technique is extremely limiting because now you would need to fabricate wrappers like WFunc for each new polymorphic expression (when the constraints or the type differ). 

I am keen to hear more about your problem, and to help accordingly.


> -----Original Message-----
> From: haskell-bounces at haskell.org [mailto:haskell-bounces at haskell.org] On
> Behalf Of Jared Warren
> Sent: Wednesday, October 26, 2005 7:01 PM
> To: Haskell Mailing List
> Subject: [Haskell] Instances That Ignore Type Constraints? (HList-related)
> > data Box x = Box x
> > data Carton x = Carton x
> > class Func x y | x -> y where func :: x -> y
> > class IsBoxed f b | f -> b where isBoxed :: f -> b
> > instance IsBoxed (Box x) HTrue where isBoxed _ = hTrue
> > instance IsBoxed (Carton x) HFalse where isBoxed _ = hFalse
> `isBoxed (Box func)` fails in GHC with "Ambiguous type variables
> `x', `y' in the constraint: `Func x y' arising from use of `func'...".

More information about the Haskell mailing list