<blockquote style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;" class="gmail_quote">&nbsp;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">&lt;<a href="mailto:byorgey@seas.upenn.edu">byorgey@seas.upenn.edu</a>&gt;</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>
&gt; I Haskell School of Expression &nbsp;(p172), it says:<br>
&gt;<br>
&gt; A newtype declaration is just like a data declaration, except that it can<br>
&gt; &gt; only be used to defined data types with single constructor. &nbsp;The new data<br>
&gt; &gt; type is different from the analogous one created by a data declaration, in<br>
&gt; &gt; that there is no computational overhead in having the constructor.... You<br>
&gt; &gt; can think of a newtype as defining &nbsp;a &quot;new type&quot; with exactly the same<br>
&gt; &gt; structure, behaviour, and performance as the underlying type.<br>
&gt; &gt;<br>
&gt;<br>
&gt;<br>
&gt; What is (or where do you see) the computational overhead of the &quot;data&quot;<br>
&gt; declrations?<br>
<br>
</div></div>Consider the data declaration<br>
<br>
 &nbsp;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. &nbsp;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>
 &nbsp;data Bad = Bad Int<br>
<br>
Some memory will still be set aside to hold the &#39;Bad&#39; tag, although in<br>
this case we can see that isn&#39;t necessary: a value of type Bad can<br>
only have one possible constructor. &nbsp;The solution is to use a newtype:<br>
<br>
 &nbsp;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. &nbsp;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. &nbsp;It usually isn&#39;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>