 
 import Prelude hiding (Enum, fromEnum)
 
+import Control.Applicative ((<$>), (<*>))
 import Control.Exception (finally)
 import Data.Bits ((.|.))
 import Data.Function (on)


  alignment _ = alignment (undefined :: Word32)

  peek ptr = agg `fmap` peekBE (castPtr ptr :: Ptr Word32)
    where
    agg 1 = Average
    agg 2 = Sum

    coerce :: Double -> Word64
    coerce x = unsafePerformIO $ with x $ \p ->
      peek (castPtr p) :: IO Word64

peekWord32beAsNum :: Num a => Ptr Word32 -> IO a
peekWord32beAsNum ptr = fromIntegral <$> peekBE ptr

peekWord32beAsFloat :: Ptr Word32 -> IO Float
peekWord32beAsFloat ptr = coerce <$> peekBE ptr
  where
  coerce :: Word32 -> Float
  coerce x = unsafePerformIO $ with x $ \p ->
    peek (castPtr p) :: IO Float

peekWord64beAsDouble :: Ptr Word64 -> IO Double
peekWord64beAsDouble ptr = coerce <$> peekBE ptr
  where
  coerce :: Word64 -> Double
  coerce x = unsafePerformIO $ with x $ \p ->
    peek (castPtr p) :: IO Double
