[Haskell-cafe] data vs newtype (was: newbie problems)

David Menendez zednenem at psualum.com
Mon Jul 5 13:35:48 EDT 2004

Mark Carroll writes:

> The way I see it, you use "type" for genuine synonyms where you don't
> care about the distinction, "newtype" where you want to make a
> separate type with a single constructor, and "data" where you want to
> make a separate type with multiple constructors. My memory might be
> failing, though.

Also, data constructors declared with "newtype" can only take one

I was going to ask what the difference was between, for example:

    newtype Nat1 = Nat1 Int
    data    Nat2 = Nat2 Int

but I found a section in the Haskell Report that addresses it. [1]

Essentially, (Nat1 undefined) is equivalent to undefined, but (Nat2
undefined) is not. On the other hand, given:

    data Nat3   = Nat3 !Int
    f1 (Nat1 i) = 42
    f2 (Nat2 i) = 42
    f3 (Nat3 i) = 42

(f2 undefined) and (f3 undefined) are undefined, but (f1 undefined) =
42. On the other hand, (f1 (Nat1 undefined)) = 42 and (f2 (Nat2
undefined)) = 42, but (f3 (Nat3 undefined)) is undefined.

On the other hand, I can't think of a situation where you would want to
use Nat2 or Nat3 instead of Nat1.

[1] <http://www.haskell.org/onlinereport/decls.html#datatype-renaming>
David Menendez <zednenem at psualum.com> <http://www.eyrie.org/~zednenem/>

More information about the Haskell-Cafe mailing list