Very interesting, I did not know that!

I thought newtype was an optimization of data, and that "newtype" was bad 
terminology. But if newtype is just a wrapper around a type, then the name is 
choosen well.

I'm a bit confused why then one needs a data-constructor-like tag to construct 
a newtype value then? Is this to avoid having to add a type signature (for type 
inference)? I find this a bit weird since 

newtype Foo = Foo Int
bar = Foo 123

does not safe a lot of keystrokes ;) compared to

-- Incorrect Haskell follows
newtype Foo = Int
bar = 123::Foo


-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Wolfgang Jeltsch
Sent: Monday, December 17, 2007 5:39 PM
To: [email protected]
Subject: Re: [Haskell-cafe] OOP'er with (hopefully) trivial questions.....

Am Montag, 17. Dezember 2007 13:04 schrieb Jed Brown:
> […]

> When your type only has one constructor, newtype is preferred over data, but
> they are semantically equivalent.

They are *not* semantically equivalent, as has already been said more or less.  
data adds an extra level of indirection.  With 

    data A a = MkA a,

_|_ (i.e., undefinedness) is different from MkA _|_.  If I don’t know anything 
about a value of A a then this is not the same as knowing that the value is 
at least an application of MkA.

newtype just creates wrapper types and it’s very unfortunate that it uses 
syntax similar to data because it’s very different.  With

    newtype A a = MkA a,

you just create a wrapper type A a for each type a.  Applying the constructor 
just means casting from a to A a, and pattern matching just means casting 
from A a to a.  Type-casting _|_ yields botton, that’s why MkA _|_ is _|_ and 
pattern matching _|_ against A x doesn’t fail but assigns _|_ to x.

> […]

Best wishes,
Wolfgang
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to