[Haskell-cafe] a really juvenile question .. hehehehe ;^)
Cale Gibbard
cgibbard at gmail.com
Mon Oct 6 08:58:20 EDT 2008
2008/10/6 Don Stewart <dons at galois.com>:
> dagit:
>> data and newtype vary in one more subtle way, and that's how/when they
>> evaluate to bottom. Most of the time they behave identically, but in the
>> right cases they act sightly differently. newtype is usually regarded as
>> more efficient than data. This is because the compiler can choose to
>> optimize away the newtype so that it only exists at type check time. I
>> think this is also possible with data in some, but not all, uses.
>
> The compiler *must* optimise away the use. They're sort of 'virtual'
> data, guaranteed to have no runtime cost.
I'm not sure that I'd want to be that emphatic about what an
implementation *must* do regarding something so operational.
The informal semantics of pattern matching in the Report says:
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 _|_.
That is, constructors associated with newtype serve only to change the
type of a value.
This clearly has an implementation which introduces no overhead, which
one can expect from good implementations of the language. There are
obviously implementations of these semantics which do introduce
overhead as well though, so I would be hesitant to make any
requirement like that.
We can say however that newtypes have no additional runtime cost in
GHC regardless of the optimisation level you pick.
- Cale
More information about the Haskell-Cafe
mailing list