[Haskell-cafe] Implicit newtype unwrapping

David Menendez dave at zednenem.com
Thu Dec 3 16:35:14 EST 2009

On Wed, Dec 2, 2009 at 7:16 PM, Martijn van Steenbergen
<martijn at van.steenbergen.nl> wrote:
> So here's a totally wild idea Sjoerd and I came up with.
> What if newtypes were unwrapped implicitly?

As several have suggested, this creates ambiguity.

But it might be handy to have a way to declare a scope in which the
newtype is transparent. E.g.,

newtype N = N T

f :: T -> T -> T
f = ...

open N in
    -- in this block, N is treated as a synonym for T
    g :: N -> N -> N
    g = f

This is similar to the restricted type synonyms feature in Hugs, and I
think it's straightforward to encode in GHC's System FC.

>From my perspective, the primary advantage to a feature like this is
that it avoids the need to convert [N] to [T], which under the current
system effectively requires mapping an identity function over the
entire list.

But that also exposes the danger of this idea, where GADTs and type
families are involved.

data G where
    A :: G N
    B :: G T

a :: G N -> N
a ~A = N  -- should never fail, because A is the only (non-bottom)
value of type G N.

Now, what stops me from writing something like this?

open N in
    a B

(See the discussion at
<http://hackage.haskell.org/trac/ghc/ticket/1496> for how this problem
crops up with generalized newtype deriving.)

Dave Menendez <dave at zednenem.com>

More information about the Haskell-Cafe mailing list