
On 17 Dec 2007, at 7:39 AM, David Menendez wrote:
On Dec 17, 2007 8:51 AM, Bayley, Alistair
wrote: As an aside, I was wondering exactly what the differences are between newtype and data i.e. between newtype A a = A a
and
data A a = A a
According to: http://www.haskell.org/onlinereport/decls.html#sect4.2.3 newtype is, umm, stricter than data i.e. newtype A undefined = undefined, but data A undefined = A undefined. Other than that, newtype just seems to be an optimization hint. Is that a more-or-less correct interpretation?
Pretty much. Newtype is nice to have, but I don't think there's any program you can write that couldn't be rewritten to use data (with a possible loss of efficiency).
The difference that surprised me is the difference between
newtype A a = A a
and
data A a = A !a
If we define a function like this,
seqA (A a) = ()
Under the first definition of A,
seqA undefined = ()
Under the second,
seqA undefined = undefined
The difference is that pattern-matching a newtype doesn't do any evaluation.
So there is a program (or, rather, type) you can write with newtype that can't be written with data: newtype T = T T jcc