[Haskell-cafe] Ambiguous type variable with subclass instance (also: is there a better way to do this?)

Daniel Fischer daniel.is.fischer at web.de
Thu Sep 17 11:50:27 EDT 2009


Am Donnerstag 17 September 2009 15:40:10 schrieb Andy Gimblett:

>
>  > instance (Enumerated a) => Target a where
>  >     convert n | n `elem` [0..len-1] = Just $ constructors !! n
>  >
>  >               | otherwise = Nothing
>  >
>  >         where len = length constructors

Yes, the second appearance of 'constructors' is at an unspecified type.

instance (Enumerated a) => Target a where
    convert n
       | n < 0     = Nothing
       | otherwise = case drop n constructors of
                        (x:_) -> Just x
                        _ -> Nothing

would make it compile.
But there'd be a risk that Target is unusable, depending on how instance resolution is 
done.

>
> I guess I see roughly what's going on; the question is "which
> constructors instance is meant?", right?  In the "Just" part it's OK,
> because it can be inferred from the function's return type (right?).
> But in the guard we don't have that help, so it could be any
> Enumerated instance?

Exactly.

>
> Any advice appreciated!  Particularly if this is just a dumb approach.
> For context, this is related to deserialisation of binary data (they'll
> actually be Word8's, not Int's) into a variety of data structures.
>
> Hmmm, maybe I should just be using Data.Binary...
>
> Many thanks,
>
> -Andy




More information about the Haskell-Cafe mailing list