[Haskell-cafe] OOP'er with (hopefully) trivial questions.....
g9ks157k at acme.softbase.org
Mon Dec 17 11:38:30 EST 2007
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.
More information about the Haskell-Cafe