Yves Parès yves.pa...@gmail.com writes:
I had for long thought that data and newtype were equivalent, but then I
spotted some differences when it comes to strictness.
data Test = Test Int
newtype TestN = TestN Int
Interesting. I'd thought that
data Test = Test !Int
and
newtype Test
An interesting use case for this is that while
data Void = Void Void
has infinitely many values (undefined, Void undefined, Void (Void
undefined) etc), the newtype version
newtype Void = Void Void
has only one, bottom. This is a way to define the empty datatype
without extensions.
Erik
Hello,
I had for long thought that data and newtype were equivalent, but then I
spotted some differences when it comes to strictness.
Those can be summed up as:
data Test = Test Int
newtype TestN = TestN Int
pm (Test _) = 12 -- Strict (pm undefined = undefined)
pm2 t = t `seq` 12 -- Strict
* Yves Parès yves.pa...@gmail.com [2012-01-22 11:32:30+0100]
These make me think that pattern matching against a newtype is always lazy
(irrefutable). Am I right?
Yes.
Is there some litterature expliciting in a less empiric way than I did the
differences like this between data and newtype?
Yves Parès wrote:
Is there some litterature expliciting in a less empiric way than I did the
differences like this between data and newtype? I've never come against
such documentation through all my learning of Haskell, yet I think it's an
important point.
Roman Cheplyaka wrote:
See the
Thanks, that's clearer to me now.
It confirmed my thoughts:
Matching the pattern con pat against a value, where con is a constructor
defined by newtype, depends on the value:
- If the value is of the form con v, then pat is matched against v.
- If the value is ⊥, then pat is matched against ⊥.
Big sum up of everything:
If TestN is a newtype constructor, then
'TestN undefined' and 'undefined' are exactly the same thing.
2012/1/22 Yitzchak Gale g...@sefer.org
Yves Parès wrote:
Is there some litterature expliciting in a less empiric way than I did
the
differences like this
* Yves Parès yves.pa...@gmail.com [2012-01-22 15:23:51+0100]
Big sum up of everything:
If TestN is a newtype constructor, then
'TestN undefined' and 'undefined' are exactly the same thing.
To be precise, the former is a type-restricted version of the latter.
--
Roman I. Cheplyaka ::