Difference between revisions of "NewType"
(newtype and data comparison.) |
(No difference)
|
Revision as of 09:00, 25 January 2009
newtype is not exactly like data. #haskell is frequently being questioned about the difference.
Firstly, as mentioned in most tutorials, newtype can handle only a single data constructor, and a single field in that single data constructor.
Secondly, newtype is strict on its field.
So how is:
newtype NewType = NewType Int
different from:
data NewData = NewData Int
The number of bottoms differ. Since data is lazy, it introduces a new bottom in addition to the bottom that existed in the Int itself.
NewData can be _|_ or it can be (NewData _|_). NewType cannot be _|_ but can only be (NewType _|_).
So, lets try another comparison:
How is:
newtype NewType = NewType Int
different from:
data NewData = NewData !Int
Here, the number of bottoms is the same, but they are not quite the same bottoms. NewType's bottom is: (NewType _|_) (the bottom is inside the pattern), whereas NewData's bottom is (_|_) (the bottom is the whole pattern).
To demonstrate this difference, we can use the following code;
case (NewType undefined) of
(NewType _) -> "Happy happy joy joy :-)"
case (NewData undefined) of
(DataType _) -> "Won't ever get here :-("
Since the newtype bottom is inside the pattern, and we catch that with (_) and never use it, we don't actually hit the bottom. But the NewData bottom is on the whole pattern, so even matching the single data constructor that way is a bottom.