newtype | data

Carl R. Witty cwitty@newtonlabs.com
05 Oct 2001 11:53:05 -0700


Mark Carroll <mark@chaos.x-philes.com> writes:

> Why does "newtype" exist, instead of letting people always use "data" and
> still get maximum efficiency? After all, surely the implementation is an
> implementation detail - a compiler could see the use of "data" with a
> unary constructor and implement it as it does "newtype", instead of making
> the programmer worry about how things are actually represented?
> 
> I'm obviously missing something obvious here; I'm hoping to learn what.
> (-:

newtype is strict; data is not.

Given 

> data T1 = T1 Int
> newtype T2 = T2 Int
> data T3 = T3 !Int

you get the following results.

Data> (T1 undefined) `seq` ()
()
Data> (T2 undefined) `seq` ()
*** Exception: Prelude.undefined
Data> (T3 undefined) `seq` ()
*** Exception: Prelude.undefined

I can't think of a semantic difference between newtype and data with a
single unary strict constructor; I suppose it would be possible to
remove newtype from the language and make people declare things like
T3 instead.

Carl Witty