[Haskell-cafe] How to do this with associated types?

Ryan Ingram ryani.spam at gmail.com
Mon Jul 26 13:30:28 EDT 2010


On Sun, Jul 25, 2010 at 1:53 PM, Alexey Karakulov <ankarakulov at gmail.com> wrote:
> Suppose I have one piece of code like this:
>
>> class Result r e | r -> e where
>>    failure :: e -> r a
>>    success :: a -> r a
>
> Maybe instance is discarding failure information:
>
>> instance Result Maybe e where
>>     failure _ = Nothing
>>     success x = Just x

I would argue that this shouldn't successfully compile.

Your class declaration claims that given r, you can uniquely determine e.

But you've effectively defined

instance Result Maybe ()
instance Result Maybe Int
... many more instances ...

which means that isn't the case.

I wonder if there is a potential type safety failure in the compiler
with instances of this form?

  -- ryan


More information about the Haskell-Cafe mailing list