Storable instance for Complex

Simon Marlow simonmarhaskell at
Tue Feb 26 06:30:24 EST 2008

Henning Thielemann wrote:
> On Sun, 17 Feb 2008, Jed Brown wrote:
>> On 15 Feb 2008, lemming at wrote:
>>> On Fri, 15 Feb 2008, Jed Brown wrote:
>>>> I propose that the following instance be added to base:
>>>> instance (RealFloat a, Storable a) => Storable (Complex a) where
>>>> sizeOf z    = 2 * sizeOf (realPart z)
>>>> alignment z = sizeOf (realPart z)
>>>> peek p = do
>>>> [r,i] <- peekArray 2 (castPtr p)
>>>> return (r :+ i)
>>>> poke p (r :+ i) = pokeArray (castPtr p) [r,i]
>>>> This instance is binary compatible with C99, C++ and Fortran complex types.
>>>> It is currently needed by at least two independent packages: hmatrix and
>>>> fft. Since it is natural for user code to use both of these packages, we
>>>> need to move the instance to a common location.
>>> I also need it for signal processing.
>> In light of the fact that several packages need this instance, I created
>> a package storable-complex (on Hackage) which has the instance in
>> Foreign.Storable.Complex.  In the interest of enabling our packages to
>> play nicely *now*, I would like to encourage everyone needing this
>> instance to get it from this common source.  When it (hopefully) makes
>> it into base, we can use a flag in the .cabal file to get it from base
>> instead of this package.
> an instance for pairs would also be nice ... :-)

I see the smiley, but just in case you're serious: that's pretty hard to 
get right.  Presumably you want it to match the C struct layout rules 
according to the C ABI of the current platform, which means implementing 
the layout rules correctly.


More information about the Libraries mailing list