[Haskell-cafe] Implicit newtype unwrapping

Conor McBride conor at strictlypositive.org
Thu Dec 3 08:34:21 EST 2009


Hi Martijn

On 3 Dec 2009, at 00:16, Martijn van Steenbergen wrote:

> So here's a totally wild idea Sjoerd and I came up with.
>
> What if newtypes were unwrapped implicitly?

Subtyping.

> What advantages and disadvantages would it have?

The typechecker being psychic; the fact that it isn't.
It's very easy to add forms of subtyping and make a mess
of type and instance inference.

> In what cases would this lead to ambiguous code?

If      f :: x -> ZipList y
we get  traverse f :: t x -> [t y]
but it is not clear whether to attach the unpacking to
f or to the result, and that will determine the idiom
in which the traversal occurs.

And that's before you start mixing the sugar of newtypes
with the fertiliser of GADTs...

But even if it's dangerous to unpack newtypes silently,
it's rather nice to do it systematically, via a type class.
Here are old posts of mine which mention this and then
show off a bit.

  http://www.mail-archive.com/haskell-cafe@haskell.org/msg37213.html
  http://www.haskell.org/pipermail/libraries/2008-January/008917.html

These days, how about

  class Newtype n where
    type Unpack n
    pack :: Unpack n -> n
    unpack :: n -> Unpack n

and related machinery?

Cheers

Conor



More information about the Haskell-Cafe mailing list