[Haskell-cafe] rewrite rules to specialize function according to type class?

Roman Leshchinskiy rl at cse.unsw.edu.au
Tue Feb 15 12:23:27 CET 2011


Max Bolingbroke wrote:
> 2011/2/15 Simon Peyton-Jones <simonpj at microsoft.com>:
>
>> but currently any pragmas in a class decl are treated as attaching to
>> the *default method*, not to the method selector:
>
> I see. I didn't realise that that was what was happening. Personally I
> find this a bit surprising, but I can see the motivation. Of course, a
> sensible alternative design would be to have them control the selectors,
> and then you could declare that you want your default methods to be
> inlined like this:
>
> {{{
> class MyClass a where
>   foo :: a -> a
>   foo = default_foo
>
> {-# INLINE default_foo #-}
> default_foo = ... big expression ...
> }}}

I wouldn't necessarily expect this to guarantee inlining for the same
reason that the following code doesn't guarantee that foo gets rewritten
to <big>:

foo = bar
{-# INLINE bar #-}
bar = <big>

It might work with the current implementation (I'm not even sure if it
does) but it would always look dodgy to me.

Also, what if I write:

class MyClass a where
  foo :: a -> a
  foo x = default_foo x

I assume this wouldn't guarantee inlining?

> In any event, perhaps it would be worth warning if you write an INLINE
> pragma for some identifier in a class declaration where no corresponding
> default method has been declared, in just the same way you would if you
> wrote an INLINE pragma for a non-existant binding?

+1

Roman






More information about the Haskell-Cafe mailing list