[Haskell-cafe] Re: Improving MPTC usability when fundeps aren't appropriate?

Heinrich Apfelmus apfelmus at quantentunnel.de
Wed Aug 5 13:09:43 EDT 2009


Daniel Peebles wrote:
> 
> I've been playing with multiparameter typeclasses recently and have
> written a few "uncallable methods" in the process. For example, in
> 
> class Moo a b where
>   moo :: a -> a
> 
> the moo function is effectively impossible to call (no amount of type
> annotations can tell the compiler what you intended b to be there).
> Some might suggest adding an a -> b functional dependency, but in some
> cases that is not appropriate, as there are multiple possible
> instances.

You can factor out  moo  into a type class involving only  a . And if
you can't do that, then you've got a problem with ambiguous instances
anyway.

> Another solution would be to artificially force moo to take
> a "dummy" b so that the compiler can figure out which instance you
> meant. That's what I've been doing in the mean time, but wouldn't it
> be simpler and less hackish to add a some form of "instance
> annotation", like a type annotation, that would make it possible to
> specify what instance you wanted when it's ambiguous? I'm not sure
> what syntax might be appropriate here, but it could also be seen as
> "opening" a particular instance, so something "open"-like might be
> good.

I don't think that the syntax for such a feature will be very different
from a dummy argument.

Also note that instead of using the actual type  b  as argument, as in

    moo :: b -> a -> a

    moo (undefined :: Foo) ...   -- usage

, you can use a phantom type

    data Instance a = I

    moo :: Instance b -> a -> a

    bar = I :: Instance Bar
    moo bar ...                  -- usage



Regards,
apfelmus

--
http://apfelmus.nfshost.com



More information about the Haskell-Cafe mailing list