[Haskell] Swapping parameters and type classes

Brent Yorgey byorgey at gmail.com
Sun Sep 16 16:38:50 EDT 2007


On 9/16/07, Mads Lindstrøm <mads_lindstroem at yahoo.dk> wrote:
>
> Hi all
>
> If I have this type:
>
>   data Foo a b = ...
>
> and this class
>
>   class Bar (x :: * -> *) where ...
>
> I can imagine two ways to make Foo an instance of Bar. Either I must
> "apply" the 'a' or the 'b' in (Foo a b). Otherwise it will not have the
> right kind. To "apply" the 'a' I can do:
>
>   instance Bar (Foo a) where ...
>
> But what if I want to "apply" the 'b' ? How do I do that ?


One easy way would be to create a newtype with the type parameters swapped:

  newtype Oof b a = Oof (Foo a b)
  instance Bar (Oof b) where ...

Of course, if you want to partially apply the second parameter of a
function, you use 'flip'.  I thought for a while about whether there's some
sort of typeclass hackery which is directly parallel to the use of 'flip',
but couldn't come up with anything.  Anyone?

-Brent
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell/attachments/20070916/13b08da6/attachment.htm


More information about the Haskell mailing list