<div class="gmail_quote">+1</div><div class="gmail_quote"><br></div><div class="gmail_quote">I&#39;m more than happy to strip the Data.Default instance from Data.Tagged, I&#39;ve already had a couple of users complain about how &#39;heavy&#39; a dependency it is, since Data.Default has to provide all the definitions for lots of other libraries that predate it, including the MTL.</div>
<div class="gmail_quote"><br></div><div class="gmail_quote">-Edward</div><div class="gmail_quote"><br></div><div class="gmail_quote">On Tue, Oct 26, 2010 at 12:15 PM, Bas van Dijk <span dir="ltr">&lt;<a href="mailto:v.dijk.bas@gmail.com">v.dijk.bas@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;">Dear all,<br>
<br>
Users of the sizeOf or alignment methods (:: a -&gt; Int) of the Storable<br>
class are pushed to use &#39;undefined&#39; in their programs. Take the<br>
following function from Foreign.Marshal.Alloc as an example:<br>
<br>
malloc :: Storable a =&gt; IO (Ptr a)<br>
malloc  = doMalloc undefined<br>
  where<br>
    doMalloc       :: Storable b =&gt; b -&gt; IO (Ptr b)<br>
    doMalloc dummy  = mallocBytes (sizeOf dummy)<br>
<br>
I find the use of &#39;undefined&#39; ugly; its only purpose is to help the<br>
type-checker by carrying around a type variable. It also makes the job<br>
of an optimizing compiler harder because, in order to avoid generating<br>
unnecessary code, the compiler needs to find out if undefined isn&#39;t<br>
used. More importantly however, &#39;undefined&#39; is dangerous; The<br>
type-checker will never complain when you accidentally use &#39;undefined&#39;<br>
in the wrong place increasing the change that your program will crash.<br>
Also, instance writers for the Storable class need to be careful not<br>
to evaluate the argument of sizeOf because it may be undefined.<br>
<br>
The use of &#39;undefined&#39; is not only required by the Storable class.<br>
Users of the HasResolution class from Data.Fixed also need to use<br>
&#39;undefined&#39; in order to get the resolution of a fixed value:<br>
<br>
class HasResolution a where<br>
    resolution :: p a -&gt; Integer<br>
<br>
I would like to propose solving this. My proposal consists of 3 sub-proposals:<br>
<br>
1) Add module Data.Tagged.<br>
2) Modify the sizeOf and alignment methods of the Storable class.<br>
3) Modify the HasResolution class.<br>
<br>
What follows are more detailed explanations of the proposals:<br>
<br>
<br>
1) Add module Data.Tagged.<br>
<br>
My proposal is to move the Data.Tagged module from Edward A. Kmett&#39;s<br>
tagged package to base.<br>
See: <a href="http://hackage.haskell.org/package/tagged" target="_blank">http://hackage.haskell.org/package/tagged</a><br>
The only modification that needs to be done is to drop the Default<br>
instance for Proxy because this will otherwise require that<br>
Data.Default be moved to base as well which isn&#39;t my intention. When<br>
this proposal is accepted Data.Default can provide the instance<br>
instead. </blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><br>
2) Modify the sizeOf and alignment methods of the Storable class.<br>
<br>
I would like to replace the following:<br>
<br>
class Storable a where<br>
   sizeOf      :: a -&gt; Int<br>
   alignment   :: a -&gt; Int<br>
<br>
with:<br>
<br>
class Storable a where<br>
   sizeOf      :: SizeOf a<br>
   alignment   :: Alignment a<br>
<br>
type SizeOf a = Tagged a Int<br>
type Alignment a = Tagged a Int<br>
<br>
To retrieve the actual size of type &#39;a&#39; use:<br>
untag (sizeOf :: SizeOf a)<br>
where: untag :: Tagged s b -&gt; b from Data.Tagged.<br>
<br>
See the following for the haddock documentation:<br>
<a href="http://code.haskell.org/~basvandijk/doc/ghc/html/libraries/base-4.3.0.0/Foreign-Storable.html" target="_blank">http://code.haskell.org/~basvandijk/doc/ghc/html/libraries/base-4.3.0.0/Foreign-Storable.html</a><br>

<br>
Here&#39;s the definition of the previous malloc function to give you an<br>
impression how code looks when my proposals are accepted:<br>
<br>
malloc :: forall a. Storable a =&gt; IO (Ptr a)<br>
malloc = mallocBytes (untag (sizeOf :: SizeOf a))<br>
<br>
(Note that this does require the ScopedTypeVariables language extension.)<br>
<br>
<br>
3) Modify the HasResolution class.<br>
<br>
I would like to modify the HasResolution class in the same way. So replacing:<br>
<br>
class HasResolution a where<br>
    resolution :: p a -&gt; Integer<br>
<br>
with:<br>
<br>
class HasResolution a where<br>
    resolution :: Resolution a<br>
<br>
type Resolution a = Tagged a Integer<br>
<br>
See the following for the haddock documentation:<br>
<a href="http://code.haskell.org/~basvandijk/doc/ghc/html/libraries/base-4.3.0.0/Data-Fixed.html" target="_blank">http://code.haskell.org/~basvandijk/doc/ghc/html/libraries/base-4.3.0.0/Data-Fixed.html</a><br>
<br>
Note that Fixed also gets a HasResolution instance:<br>
<br>
instance HasResolution a =&gt; HasResolution (Fixed a) where<br>
    resolution = retag (resolution :: Resolution a)<br>
<br>
where: retag :: Tagged s b -&gt; Tagged t b from Data.Tagged.<br>
<br>
<br>
There&#39;s a possible 4th proposal that I&#39;m thinking about: The Bits<br>
class from Data.Bits has the bitSize :: a -&gt; Int method. Maybe it<br>
would be a good idea to replace that as well with:<br>
bitSize :: BitSize a; type BitSize a = Tagged a Int<br>
However I think bitSize is more often applied to an actual value than<br>
to &#39;undefined&#39; so I need to investigate that a little further.<br>
<br>
<br>
A patch for the base package is attached to the ticket. I also<br>
attached patches for the ghc compiler, bytestring, binary, vector and<br>
dph. The latter are just some packages that were inside my ghc<br>
repository. This email is CCed to the maintainers of these packages.<br>
<br>
<br>
Deadline: 3 weeks from now (Tuesday 16 November 2010) but I will shift<br>
it when the discussion demands it.<br>
<br>
Ticket: <a href="http://hackage.haskell.org/trac/ghc/ticket/4443" target="_blank">http://hackage.haskell.org/trac/ghc/ticket/4443</a><br>
<br>
<br>
Regards,<br>
<font color="#888888"><br>
Bas<br>
</font></blockquote></div><br>