
Mark Carroll 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

You can't recreate "newtype" with "data". There's a long discussion of this in the report: check section 4.2.3 http://haskell.org/onlinereport/decls.html#sect4.2.3 John
participants (2)
-
cwitty@newtonlabs.com
-
John Peterson