Jacques Carette | 18 Sep 14:21 2013
Picon
Picon

Multi-param typeclass vs locally constrained typeclass methods

Could someone please explain what the difference (if any!), in semantics 
is between

class Foo f => Bar f g where
   method1 :: f a -> g a

and

class Bar' g where
   method2 :: Foo f => f a -> g a

?  Maybe the translation of the above to something lower level might 
help.  [Note: "f a -> g a" is just an example, and is not meaningful to 
the question].

The best answer would contain a design guideline for typeclasses, which 
would spell out conditions in which to use each variant.

Jacques
Roman Cheplyaka | 18 Sep 14:54 2013

Re: Multi-param typeclass vs locally constrained typeclass methods

* Jacques Carette <carette <at> mcmaster.ca> [2013-09-18 08:21:51-0400]
> Could someone please explain what the difference (if any!), in
> semantics is between
> 
> class Foo f => Bar f g where
>   method1 :: f a -> g a
> 
> and
> 
> class Bar' g where
>   method2 :: Foo f => f a -> g a

Bar is more flexible than Bar'. If you have n types, you can write n^2
Bar instances (potentially having very different semantics) to convert
between them.

You can only write n Bar' instances, on the other hand. In these
instances you can dispatch based on 'g' but not on 'f'. 'f' is abstract,
and you only can access it through the Foo interface.

So they are quite different.

Roman
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
(Continue reading)

Jacques Carette | 18 Sep 16:27 2013
Picon
Picon

Re: Multi-param typeclass vs locally constrained typeclass methods

On 13-09-18 08:54 AM, Roman Cheplyaka wrote:
> * Jacques Carette <carette <at> mcmaster.ca> [2013-09-18 08:21:51-0400]
>> Could someone please explain what the difference (if any!), in
>> semantics is between
>>
>> class Foo f => Bar f g where
>>    method1 :: f a -> g a
>>
>> and
>>
>> class Bar' g where
>>    method2 :: Foo f => f a -> g a
> Bar is more flexible than Bar'. If you have n types, you can write n^2
> Bar instances (potentially having very different semantics) to convert
> between them.
>
> You can only write n Bar' instances, on the other hand. In these
> instances you can dispatch based on 'g' but not on 'f'. 'f' is abstract,
> and you only can access it through the Foo interface.
>
> So they are quite different.

Right, that makes sense, thanks.

Turns out that, in my case, I frequently want the Bar' case, as I want 
things to be fully polymorphic in Foo.

Jacques

Gmane