Commit fb061c19 authored by Carter Schonwald's avatar Carter Schonwald Committed by Herbert Valerio Riedel
Browse files

Add `Storable` instances for `Complex` and `Ratio`

The actual type-signatures of the new instances are:

  instance Storable a => Storable (Complex a)

  instance (Storable a, Integral a) => Storable (Ratio a)

See also

  https://groups.google.com/d/msg/haskell-core-libraries/mjBSo2CQ3LU/0gwg0QvviOIJ

Addresses #9826

Reviewed By: ekmett

Differential Revision: https://phabricator.haskell.org/D519
parent cc7a735f
......@@ -36,6 +36,8 @@ module Data.Complex
import Data.Typeable
import Data.Data (Data)
import Foreign (Storable, castPtr, peek, poke, pokeElemOff, peekElemOff, sizeOf,
alignment)
infix 6 :+
......@@ -171,3 +173,16 @@ instance (RealFloat a) => Floating (Complex a) where
asinh z = log (z + sqrt (1+z*z))
acosh z = log (z + (z+1) * sqrt ((z-1)/(z+1)))
atanh z = 0.5 * log ((1.0+z) / (1.0-z))
instance Storable a => Storable (Complex a) where
sizeOf a = 2 * sizeOf (realPart a)
alignment a = alignment (realPart a)
peek p = do
q <- return $ castPtr p
r <- peek q
i <- peekElemOff q 1
return (r :+ i)
poke p (r :+ i) = do
q <-return $ (castPtr p)
poke q r
pokeElemOff q 1 i
......@@ -208,6 +208,19 @@ STORABLE(Int32,SIZEOF_INT32,ALIGNMENT_INT32,
STORABLE(Int64,SIZEOF_INT64,ALIGNMENT_INT64,
readInt64OffPtr,writeInt64OffPtr)
instance (Storable a, Integral a) => Storable (Ratio a) where
sizeOf _ = 2 * sizeOf (undefined :: a)
alignment _ = alignment (undefined :: a )
peek p = do
q <- return $ castPtr p
r <- peek q
i <- peekElemOff q 1
return (r % i)
poke p (r :% i) = do
q <-return $ (castPtr p)
poke q r
pokeElemOff q 1 i
-- XXX: here to avoid orphan instance in GHC.Fingerprint
instance Storable Fingerprint where
sizeOf _ = 16
......
......@@ -113,6 +113,10 @@
representing non-negative arbitrary-precision integers. The `GHC.Natural`
module exposes additional GHC-specific primitives. (#9818)
* Add `(Storable a, Integeral a) => Storable (Ratio a)` instance (#9826)
* Add `Storable a => Storable (Complex a)` instance (#9826)
## 4.7.0.1 *Jul 2014*
* Bundled with GHC 7.8.3
......
{-# LANGUAGE BangPatterns #-}
module Main where
--import qualified Data.Vector.Storable as V
import Foreign
import Data.Ratio
import Data.Complex
complexZI :: Complex Int
complexZI = 1 :+ 1
ratio23 :: Ratio Int
ratio23 = 1 % 1
putter :: Storable a => a -> Ptr a -> IO a
putter v !ptr = do poke ptr v ; peek ptr
main =
do
!vComplex <- alloca (putter complexZI)
!vRatio <- alloca (putter ratio23)
if vComplex == complexZI && vRatio == ratio23
then putStrLn "success"
else putStrLn "uh oh, something is wrong with storable"
......@@ -178,3 +178,4 @@ test('T9532', normal, compile_and_run, [''])
test('T9586', normal, compile, [''])
test('T9681', normal, compile_fail, [''])
test('T8089', normal, compile_and_run, [''])
test('T9826',normal, compile_and_run,[''])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment