Casting + eta reduction

Brent Yorgey byorgey at seas.upenn.edu
Mon Jul 12 13:08:02 EDT 2010


On Thu, Jul 08, 2010 at 09:30:23PM -0500, Louis Wasserman wrote:
> Consider
> 
> newtype Foo = Foo Int
> 
> lift :: (Int -> a) -> Foo -> a
> lift f (Foo x) = f x
> 
> Now, I'd expect this to compile with -O2 down to something like
> 
> lift f = f `cast` (Foo -> a)
> 
> but it doesn't.
> 
> It seems that GeneralizedNewtypeDeriving assumes that these two things *are*
> equivalent, and it just directly casts the class dictionary.  The
> implication would be that that GeneralizedNewtypeDeriving gives more
> efficient instances than you could *possibly* get if you wrote them by hand,
> which is very sad.

It's true.  For more, see this thread:

  http://www.mail-archive.com/haskell-cafe@haskell.org/msg72300.html

Of course that thread is more about how sometimes GND gives you
*wrong* code.  That's currently being worked on and will hopefully be
fixed at some point.  But as you point out, it's also worth thinking
about the flip side: how to optimize away non-type-class-related
functions like your example.

-Brent


More information about the Glasgow-haskell-users mailing list