<blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote"> newtype Good = Good Int<br><br>
Now, when a value of type Bad is stored at runtime, it will simply<br>
store an Int, with no tag<br></blockquote>
<br><br>Did you mean to say Good in the above sentence?<br><br>What are the run time implication of newtype vs data where types are unwrapped and wrapped from on type to another?<br><br>Thanks<br><br>Daryoush<br><br><div class="gmail_quote">
On Tue, Oct 28, 2008 at 1:08 PM, Brent Yorgey <span dir="ltr"><<a href="mailto:byorgey@seas.upenn.edu">byorgey@seas.upenn.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div></div><div class="Wj3C7c">On Tue, Oct 28, 2008 at 12:13:11PM -0700, Daryoush Mehrtash wrote:<br>
> I Haskell School of Expression (p172), it says:<br>
><br>
> A newtype declaration is just like a data declaration, except that it can<br>
> > only be used to defined data types with single constructor. The new data<br>
> > type is different from the analogous one created by a data declaration, in<br>
> > that there is no computational overhead in having the constructor.... You<br>
> > can think of a newtype as defining a "new type" with exactly the same<br>
> > structure, behaviour, and performance as the underlying type.<br>
> ><br>
><br>
><br>
> What is (or where do you see) the computational overhead of the "data"<br>
> declrations?<br>
<br>
</div></div>Consider the data declaration<br>
<br>
data Foo = Bar Int | Baz Char String<br>
<br>
When a value of type Foo is stored in memory, there will actually be<br>
some memory set aside for the tag (either Bar or Baz) since pattern<br>
matching may need to be performed at runtime. This is different from<br>
a C union type, where if you want such a tag you must store it<br>
yourself.<br>
<br>
However, consider<br>
<br>
data Bad = Bad Int<br>
<br>
Some memory will still be set aside to hold the 'Bad' tag, although in<br>
this case we can see that isn't necessary: a value of type Bad can<br>
only have one possible constructor. The solution is to use a newtype:<br>
<br>
newtype Good = Good Int<br>
<br>
Now, when a value of type Bad is stored at runtime, it will simply<br>
store an Int, with no tag. The benefit of declaring a newtype like<br>
this is that the typechecker will ensure that you cannot mix up Goods<br>
and Ints, although at runtime they will have the same representation.<br>
<br>
So, to answer your question, the only computational overhead with a<br>
data declaration is the extra memory and time to store and process the<br>
constructor tags. It usually isn't noticeable, but sometimes the<br>
difference can be important.<br>
<br>
-Brent<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br><br clear="all"><br>