[Haskell-cafe] data vs newtype (was: OOP'er with (hopefully) trivial questions)

David Menendez dave at zednenem.com
Mon Dec 17 10:39:03 EST 2007

On Dec 17, 2007 8:51 AM, Bayley, Alistair <
Alistair_Bayley at invescoperpetual.co.uk> 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


    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.

Dave Menendez <dave at zednenem.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20071217/8d3466bc/attachment.htm

More information about the Haskell-Cafe mailing list