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

James Andrew Cook mokus at deepbondi.net
Wed Oct 20 22:39:00 EDT 2010

On Oct 20, 2010, at 5:06 PM, Thomas Schilling <nominolo at googlemail.com> wrote:

> Do we really want to treat every newtype wrappers as a form of 'id'?
> For example:
>    newtype Nat = Nat Integer   -- must always be positive
> A possible rule (doesn't actually typecheck, but you get the idea):
>    forall (x :: Nat). sqrt (x * x) = x
> If we ignore newtyping we get an incorrect rewrite rule.  It depends
> on the exact implementation of which 'id's would be recognised.

That wouldn't be generalized to id, the special treatment would only apply to rule that _mention_ Prelude.id explicitly.  Such rules would implicitly fire when, say, "fmap Nat xs" occurs because Nat would be considered a specialization of id in the (pattern side of) rule "fmap id = id".  Rules mentioning Nat would not be magical in any way.

-- James

More information about the Haskell-Cafe mailing list