If newtype = data !, then why use does Haskell' need newtype

John Meacham john at repetae.net
Thu Oct 11 18:47:11 EDT 2007


On Thu, Oct 11, 2007 at 02:32:25PM -0700, Dan Weston wrote:
> I presume that the two statements below are equivalent [1]:
> 
> newtype A = MkA  Int
> data    A = MkA !Int
> 
> So does Haskell' still need newtype? It seems like a needless keyword.

they are not the same:

> newtype A = MkA  Int
> data    B = MkB !Int


f (MkA x) = 3
g (MkB x) = 3

f _|_ = 3
g _|_ = _|_


newtype construction/deconstruction is defined to be a nop, data
deconstruction always requires evaluation. just because the value inside
the data type is guarenteed not to be bottom, it doesn't mean
deconstruction/construction is a nop.

That said, ghc is quite clever and figured out it can unbox that data
type for you in this particular case, but such a transformation is not
necessarily valid in general.

        John

-- 
John Meacham - ⑆repetae.net⑆john⑈


More information about the Haskell-prime mailing list