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

Bertram Felgenhauer bertram.felgenhauer at googlemail.com
Tue Nov 2 10:28:16 EDT 2010

Simon Peyton-Jones wrote:
> What you really want is to say is something like this.  Suppose my_tree :: Tree String.  Then you'd like to say
> 	my_tree ::: Tree Foo
> meaning "please find a way to convert m_tree to type (Tree Foo), using newtype coercions.  
> The exact syntax is a problem (as usual).  We have the technology now.  The question is how important it is.

I think extending the syntax for contexts would be sufficient:
Write a ~~ b for "a can be converted to b by wrapping / unwrapping
newtypes", which is a conservative approximation of "a and b have the
same representation".

Then we can define

    safeCoerce :: (a ~~ b) => a -> b
    safeCoerce = unsafeCoerce

and your example would become

    safeCoerce my_tree :: Tree Foo

The feature would add convenience to the language when working with
newtypes, and reduce the tension between type safety and performance
(where the choice is between using a newtype and unsafeCoerce, and
just working with the plain underlying type.) So while the pressure
is quite low, I imagine it would become quite a useful feature once
we'd have it, but that's of course speculation.

As far as I can see, the feature is nontrivial: Care has to be taken
to not break abstractions (like "safely"coercing IO to ST), so it's
quite possible that the engineering effort outweighs the potential

Best regards,


More information about the Haskell-Cafe mailing list