Why is pattern matching on newtypes lazy? Does this add to efficiency somehow? If not, it seems to be just another rule to keep straight.
tis 2002-01-22 klockan 15.52 skrev Feuer:
Why is pattern matching on newtypes lazy? Does this add to efficiency somehow? If not, it seems to be just another rule to keep straight.
That's the difference between newtype and data. Newtypes are unboxed, so there is no constructor to match on. Pattern matching on newtypes is only a type-checker thing, the constructor doesn't exist. Regards, Martin -- [ http://www.dtek.chalmers.se/~d95mback/ ] [ PGP: 0x453504F1 ] [ UIN: 4439498 ] Opinions expressed above are mine, and not those of my future employees. SIGBORE: Signature boring error, core dumped
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. On 22 Jan 2002, Martin [ISO-8859-1] Norb�ck wrote:
tis 2002-01-22 klockan 15.52 skrev Feuer:
Why is pattern matching on newtypes lazy? Does this add to efficiency somehow? If not, it seems to be just another rule to keep straight.
That's the difference between newtype and data. Newtypes are unboxed, so there is no constructor to match on. Pattern matching on newtypes is only a type-checker thing, the constructor doesn't exist.
Regards,
Martin
-- [ http://www.dtek.chalmers.se/~d95mback/ ] [ PGP: 0x453504F1 ] [ UIN: 4439498 ] Opinions expressed above are mine, and not those of my future employees. SIGBORE: Signature boring error, core dumped
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
participants (2)
-
Feuer -
Martin Norbäck