[Haskell-cafe] Implicit newtype unwrapping
conor at strictlypositive.org
Thu Dec 3 08:34:21 EST 2009
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?
> 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.
These days, how about
class Newtype n where
type Unpack n
pack :: Unpack n -> n
unpack :: n -> Unpack n
and related machinery?
More information about the Haskell-Cafe