efficiency

Feuer feuer@his.com
Tue, 15 Jan 2002 14:39:35 -0800


Well, I guess I was wrong about this.  I was confused by the place where the
Haskell Report says:

    Unlike algebraic datatypes, the newtype constructor N is unlifted, so
    that N _|_ is the same as _|_.

What I didn't realize was that matching against N is irrefutable.  What I still
don't understand is _why_ it is irrefutable.  How (if at all) does this make them
more efficient?  In particular, it seems it would be possible to simulate the
newtype with a datatype:

The report says that if
data D2=D2 !Int
newtype N = N Int

d2 (D2 i) = 42
n (N i) = 42

then
d2 _|_ = _|_      d2 (D2 _|_) = _|_
n _|_ = 42        n (N_|_) = 42

If d2 were rewritten
d2' ~(D2 i) = 42
then it would act just like n.

I guess the question is whether the code for d2' is as efficient as the code for
n.  I don't see why it wouldn't be, but of course I could be wrong.