On Mon, Jan 10, 2011 at 3:45 PM, Aaron Gray <span dir="ltr">&lt;<a href="mailto:aaronngray.lists@gmail.com">aaronngray.lists@gmail.com</a>&gt;</span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5"><div class="gmail_quote">On 10 January 2011 13:49, John Lato <span dir="ltr">&lt;<a href="mailto:jwlato@gmail.com" target="_blank">jwlato@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">From: Aaron Gray &lt;<a href="mailto:aaronngray.lists@gmail.com" target="_blank">aaronngray.lists@gmail.com</a>&gt;<div>


<br>

On 9 January 2011 21:30, Henning Thielemann<br>
&lt;<a href="mailto:lemming@henning-thielemann.de" target="_blank">lemming@henning-thielemann.de</a>&gt;wrote:<br>
<br>
&gt;<br>
&gt; On Sun, 9 Jan 2011, Aaron Gray wrote:<br>
&gt;<br>
&gt;  I am trying to work out how to use GHC.Ptr, Foreign.Storable,<br>
&gt;&gt; Data.Storable.Endian, and<br>
&gt;&gt; am looking for good examples of usage.<br>
&gt;&gt;<br>
&gt;<br>
&gt; What do you intend to do with them?<br>
&gt;<br>
&gt;<br>
An (ABC) ActionScript Byte Code backend for Haskell.<br>
<br>
Basically I need to write little-endian binary to a file, and was wondering<br>
the best way to do this; I need various types including a 24bit type.<br></div></blockquote><div><br></div><div>You could use my &quot;word24&quot; package[1] (GHC only) to provide non-aligned 24-bit word and int types with Storable instances.  You should be able to write a binary instance (or whatever blaze-builder needs) fairly simply from this.  Little-endian only ATM, but BE could be added if necessary.</div>



<div><br></div><font color="#888888"><div>John Lato</div><div>[1] <a href="http://hackage.haskell.org/package/word24" target="_blank">http://hackage.haskell.org/package/word24</a></div></font></div>
</blockquote></div><br></div></div><div>This is interesting, what does the following line do :-</div><div><br></div><div>    <span style="font-family:monospace;font-size:medium;white-space:pre-wrap"><span style="color:blue">data</span> <span>Int24</span> <span style="color:red">=</span> <span>I24</span><span>#</span> <span>Int</span><span>#</span> <span style="color:blue">deriving</span> <span style="color:red">(</span><span>Eq</span><span style="color:red">,</span> <span>Ord</span><span style="color:red">)</span></span></div>


<div><span style="white-space:pre-wrap"><span><font face="arial, helvetica, sans-serif"><br></font></span></span></div><div><span style="white-space:pre-wrap"><span><font face="arial, helvetica, sans-serif">regarding the I24# and Int#, are these inbuilt ?</font></span></span></div>
</blockquote><div><br></div><div>The <span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; white-space: pre-wrap; ">Int# is built-in to GHC; it&#39;s an unboxed Int type (C long int, see </span><a href="http://www.haskell.org/ghc/docs/7.0-latest/html/users_guide/primitives.html#glasgow-unboxed">http://www.haskell.org/ghc/docs/7.0-latest/html/users_guide/primitives.html#glasgow-unboxed</a>)<span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; white-space: pre-wrap; ">.</span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; white-space: pre-wrap; ">  I24# is not built-in; it&#39;s just a normal constructor.  Although it has a hash suffix, that&#39;s just a convention (as documented under the MagicHash pages); it behaves exactly like any other constructor.</span></div>
<div><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; white-space: pre-wrap; "><br></span></div><div><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; white-space: pre-wrap; ">This means that an Int24 works just like an Int, Int16, etc.  It can be lazy, but will also be unboxed by GHC when appropriate.  Arithmetic on the unboxed type will result in machine ops.  This isn&#39;t strictly necessary; Int24 could be nearly equivalently defined as</span></div>
<div><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; white-space: pre-wrap; "><br></span></div><div><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; white-space: pre-wrap; ">data Int24 = Int24 {-</span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; white-space: pre-wrap; "># UNPACK </span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; white-space: pre-wrap; ">#-}</span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; white-space: pre-wrap; "> !</span><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; white-space: pre-wrap; ">Int32</span></div>
<div><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; white-space: pre-wrap; "><br></span></div><div><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; white-space: pre-wrap; ">but using unboxed types is perhaps more efficient.  The UNPACK pragma wasn&#39;t available when I wrote Int24, which is why I did it this way.  I may revisit this decision in the future; unboxed types are tied closely to GHC, and the UNPACK/strict combination would be more portable and easier to maintain.</span></div>
<div><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; white-space: pre-wrap; "><br></span></div><div><span class="Apple-style-span" style="font-family: arial, helvetica, sans-serif; white-space: pre-wrap; ">John</span></div>
</div>