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

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. ---------- [1] I ran ghc -c -fglasgow-exts -O -ddump-simpl and renamed identifiers to match, then did a diff, resulting in identical Tidy Core except for an explicit (inlined) reification function unused elsewhere: Main.$WMkA :: GHC.Base.Int -> Main.A [DataConWrapper] [Arity 1 NoCafRefs Str: DmdType Sm] Main.$WMkA = __inline_me (\ (tpl_A1 :: GHC.Base.Int) -> case tpl_A1 of tpl1_A1 { __DEFAULT -> Main.MkA tpl1_A1 }) which I take to mean simply: Main.$WMkA = Main.MkA

On 10/11/07, Dan Weston
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.
newtype and data have different semantics: Haskell guarantees that an A will have the exact same run-time representation as an Int, if A is declared with the newtype as above. It doesn't guarantee that if you use data instead of newtype. It may be that GHC compiles code in the same way with either of your type declarations above, but that's an implementation-dependent choice. Cheers, Tim -- Tim Chevalier * catamorphism.org * Often in error, never in doubt "in a recent future, this is past" -- James Keelaghan

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⑈
participants (3)
-
Dan Weston
-
John Meacham
-
Tim Chevalier