[Haskell-cafe] Are newtypes optimised and how much?

Daniel Fischer daniel.is.fischer at web.de
Wed Oct 20 19:31:23 EDT 2010

On Thursday 21 October 2010 01:11:25, wren ng thornton wrote:
> On 10/20/10 7:07 PM, wren ng thornton wrote:
> > On 10/20/10 7:09 AM, Simon Peyton-Jones wrote:
> >> Yes, you can freely use Foo/unFoo. There's no runtime penalty. (In
> >> the jargon of GHC's intermediate language, Foo and unFoo translate to
> >> *type-safe casts*, which generate no executable code.
> >
> > When does the conversion to type-safe casts occur relative to other
> > optimizations (namely, rewrite rules)?
> That is, I know that rewrite rules operate on the source language not on
> Core, but to what extent does that mean that type-safe casts inhibit the
> firing of rules?

Pretty much completely, I'm afraid.
There's a rewrite rule for realToFrac :: Double -> Float.
Build yourself a benchmark for that transformation that runs some 
measurable time (say 0.1 seconds or so).
Now use a newtype wrapper around Float instead (and don't add a rewrite 
rule for it). Ouch.

More information about the Haskell-Cafe mailing list