Ticket #2099 (closed proposal: wontfix)

Opened 5 years ago

Last modified 5 years ago

Storable instance for Complex

Reported by: jedbrown Owned by:
Priority: normal Milestone: Not GHC
Component: libraries/base Version: 6.8.2
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description

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.

Change History

Changed 5 years ago by igloo

  • difficulty set to Unknown
  • milestone set to Not GHC

Changed 5 years ago by simonmar

This is slightly dubious. Storable is for types that have a single direct analogue on the C side: for example, CInt maps to int, Double maps to HsDouble. What does Complex Int64 map to? Perhaps you don't really mean Storable (Complex a), but instead the three instances for CFloat, CDouble and CLongDouble?

Changed 5 years ago by jedbrown

Care to write a RealFloat? instance for Int64?

It is true that what you describe would be sufficient for my needs, but I don't think the general instance is too general. All this instance prescribes is the ordering of real and imaginary components which is quite standard. With regard to Haskell versus C types, how likely is it that Double /= CDouble at some point? I'm curious what the best practice is for high performance numerics libraries where marshaling arrays in and out of each foreign call is not acceptable. This appears to mean that C types must extend arbitrarily deeply into the Haskell world which seems to defeat the point of having separate types. The libraries that I'm familiar with (hmatrix and fft) assume that Double == CDouble so they are broken if this doesn't hold.

Note: There is now 'storable-vector' on Hackage which defines this instance. fft-0.1.1 depends on it and Alberto tells me that hmatrix will soon.

Changed 5 years ago by igloo

A bit off-topic, but you should assume that Double /= CDouble, and convert with realToFrac (in a thin wrapper around the FFI imports, where you are doing things like checking errno and reading results in from pointers, is normally a sensible place to do the conversion). When Double == CDouble, this should be optimised away to no code.

Changed 5 years ago by igloo

  • status changed from new to closed
  • resolution set to wontfix

This proposal seems to be abandoned

Changed 5 years ago by simonmar

  • architecture changed from Multiple to Unknown/Multiple

Changed 5 years ago by simonmar

  • os changed from Multiple to Unknown/Multiple
Note: See TracTickets for help on using tickets.