newtype pattern matching

Martin Norbäck d95mback@dtek.chalmers.se
25 Jan 2002 08:00:24 +0100


fre 2002-01-25 klockan 02.21 skrev Feuer:
> Please reply to dfeuer@cs.brown.edu
> 
> 
> I don't remember if I answered this before, but...
> 
> I don't see the relevance of there being no constructor to match on.  That
> is the case for any tuple type.  It seems that
> 
> newtype T1 [a1 a2 ...] = C1 ...
> is the same as
> data T2 [a1 a2 ...] = C2 !... !... !...
> 
> Except that pattern matching on C1 is like lazy pattern matching on C2.
> Since newtype is supposed to be about efficiency, I am trying to
> understand what makes this more efficient.  I have not yet seen any
> explanation of this.

Those two constructs are not the same

Compare

newtype T1 = C1 Bool
data    T2 = C2 !Bool

the difference is that the constructor C1 does not exist, so only the
following values exist for T1:

C1 True  (which is the represented as True)
C1 False (which is the represented as False)
C1 _|_   (which is represented as non-termination or error)

however, for T2, another value exist, namely _|_.
So, pattern matching on (T2 x) may fail it the value is _|_. You are
right that semantically newtype is the same as always matching lazily on
the constructor.

The case for a tuple type (a,b,c) for instance, is different. Here there
is a constructor, you can even type it by itself, it is (,,), try ":t
(,,)" in hugs or ghci.

Chapter 4.2.3 in the Haskell 98 report clarifies this.

Regards,

	Martin