
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. http://hackage.haskell.org/trac/ghc/ticket/2099 Jed

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.

On 15 Feb 2008, lemming@henning-thielemann.de 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. Note on style: I decided I agree with Aaron about using explicit peek/poke rather than a list. Thus the instance in storable-complex is written: instance (RealFloat a, Storable a) => Storable (Complex a) where sizeOf z = 2 * sizeOf (realPart z) alignment z = sizeOf (realPart z) peek p = do let q = castPtr p r <- peek q i <- peekElemOff q 1 return (r :+ i) poke p (r :+ i) = do let q = (castPtr p) poke q r pokeElemOff q 1 i Is Storable/Complex really a special case, or are there other cases where equivalent instances are defined in different packages? If so, would a language extension allowing the user (importing the same instance from multiple sources) to specify that the instances are actually identical be helpful? Jed

On Sun, 17 Feb 2008, Jed Brown wrote:
On 15 Feb 2008, lemming@henning-thielemann.de 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 ... :-)

Henning Thielemann wrote:
On Sun, 17 Feb 2008, Jed Brown 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.
http://hackage.haskell.org/trac/ghc/ticket/2099 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
On 15 Feb 2008, lemming@henning-thielemann.de wrote: 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. Cheers, Simon

On 2008-02-15, Jed Brown
I propose that the following instance be added to base:
instance (RealFloat a, Storable a) =3D> Storable (Complex a) where sizeOf z =3D 2 * sizeOf (realPart z) alignment z =3D sizeOf (realPart z) peek p =3D do [r,i] <- peekArray 2 (castPtr p) return (r :+ i) poke p (r :+ i) =3D pokeArray (castPtr p) [r,i]
I've been using a slightly different instance: instance (RealFloat a, Storable a) => Storable (Complex a) where sizeOf x = 2 * sizeOf (f x) alignment x = alignment (f x) poke x (a :+ b) = do let y = castPtr x poke y a pokeElemOff y 1 b peek x = do let y = castPtr x a <- peek y b <- peekElemOff y 1 return (a :+ b) f :: Complex a -> a f _ = undefined With a small constant number of elements, I think it makes more sense to just store them explicitly rather than through a list. I didn't even think of using realPart, but it makes sense. -- Aaron Denney -><-

On 15 Feb 2008, wnoise@ofb.net wrote:
On 2008-02-15, Jed Brown
wrote: I propose that the following instance be added to base:
instance (RealFloat a, Storable a) =3D> Storable (Complex a) where sizeOf z =3D 2 * sizeOf (realPart z) alignment z =3D sizeOf (realPart z) peek p =3D do [r,i] <- peekArray 2 (castPtr p) return (r :+ i) poke p (r :+ i) =3D pokeArray (castPtr p) [r,i]
I've been using a slightly different instance:
instance (RealFloat a, Storable a) => Storable (Complex a) where sizeOf x = 2 * sizeOf (f x) alignment x = alignment (f x) poke x (a :+ b) = do let y = castPtr x poke y a pokeElemOff y 1 b peek x = do let y = castPtr x a <- peek y b <- peekElemOff y 1 return (a :+ b)
f :: Complex a -> a f _ = undefined
With a small constant number of elements, I think it makes more sense to just store them explicitly rather than through a list.
I don't really care how it is written. I suspect these generate exactly the same code. The important thing is that an instance with this binary representation is put in one place since the current state prevents certain packages from being used together. Jed
participants (4)
-
Aaron Denney
-
Henning Thielemann
-
Jed Brown
-
Simon Marlow