HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulebase-compat-batteries-0.14.1Haskell2010

Foreign.Compat

  • 23 types
  • 3 classes
  • 114 values
classclass Eq a => Bits a where
#

The Bits class defines bitwise operations over integral types.

  • Bits are numbered from 0 with bit 0 being the least significant bit.

Methods

  • (.&.) :: a -> a -> ainfixl 7

    Bitwise "and"

  • (.|.) :: a -> a -> ainfixl 5

    Bitwise "or"

  • xor :: a -> a -> ainfixl 6

    Bitwise "xor"

  • complement :: a -> a

    Reverse all the bits in the argument

  • shift :: a -> Int -> ainfixl 8

    shift x i shifts x left by i bits if i is positive, or right by -i bits otherwise. Right shifts perform sign extension on signed number types; i.e. they fill the top bits with 1 if the x is negative and with 0 otherwise.

    An instance can define either this unified shift or shiftL and shiftR, depending on which is more convenient for the type in question.

  • rotate :: a -> Int -> ainfixl 8

    rotate x i rotates x left by i bits if i is positive, or right by -i bits otherwise.

    For unbounded types like Integer, rotate is equivalent to shift.

    An instance can define either this unified rotate or rotateL and rotateR, depending on which is more convenient for the type in question.

  • zeroBits :: a

    zeroBits is the value with all bits unset.

    The following laws ought to hold (for all valid bit indices n):

    This method uses clearBit (bit 0) 0 as its default implementation (which ought to be equivalent to zeroBits for types which possess a 0th bit).

  • bit :: Int -> a

    bit i is a value with the ith bit set and all other bits clear.

    Can be implemented using bitDefault if a is also an instance of Num.

    See also zeroBits.

  • setBit :: a -> Int -> a

    x `setBit` i is the same as x .|. bit i

  • clearBit :: a -> Int -> a

    x `clearBit` i is the same as x .&. complement (bit i)

  • complementBit :: a -> Int -> a

    x `complementBit` i is the same as x `xor` bit i

  • testBit :: a -> Int -> Bool

    x `testBit` i is the same as x .&. bit n /= 0

    In other words it returns True if the bit at offset @n is set.

    Can be implemented using testBitDefault if a is also an instance of Num.

  • bitSizeMaybe :: a -> Maybe Int

    Return the number of bits in the type of the argument. The actual value of the argument is ignored. Returns Nothing for types that do not have a fixed bitsize, like Integer.

  • bitSize :: a -> Int

    Return the number of bits in the type of the argument. The actual value of the argument is ignored. The function bitSize is undefined for types that do not have a fixed bitsize, like Integer.

    Default implementation based upon bitSizeMaybe provided since 4.12.0.0.

  • isSigned :: a -> Bool

    Return True if the argument is a signed type. The actual value of the argument is ignored

  • shiftL :: a -> Int -> ainfixl 8

    Shift the argument left by the specified number of bits (which must be non-negative). Some instances may throw an Overflow exception if given a negative input.

    An instance can define either this and shiftR or the unified shift, depending on which is more convenient for the type in question.

  • unsafeShiftL :: a -> Int -> a

    Shift the argument left by the specified number of bits. The result is undefined for negative shift amounts and shift amounts greater or equal to the bitSize.

    Defaults to shiftL unless defined explicitly by an instance.

  • shiftR :: a -> Int -> ainfixl 8

    Shift the first argument right by the specified number of bits. The result is undefined for negative shift amounts and shift amounts greater or equal to the bitSize. Some instances may throw an Overflow exception if given a negative input.

    Right shifts perform sign extension on signed number types; i.e. they fill the top bits with 1 if the x is negative and with 0 otherwise.

    An instance can define either this and shiftL or the unified shift, depending on which is more convenient for the type in question.

  • unsafeShiftR :: a -> Int -> a

    Shift the first argument right by the specified number of bits, which must be non-negative and smaller than the number of bits in the type.

    Right shifts perform sign extension on signed number types; i.e. they fill the top bits with 1 if the x is negative and with 0 otherwise.

    Defaults to shiftR unless defined explicitly by an instance.

  • rotateL :: a -> Int -> ainfixl 8

    Rotate the argument left by the specified number of bits (which must be non-negative).

    An instance can define either this and rotateR or the unified rotate, depending on which is more convenient for the type in question.

  • rotateR :: a -> Int -> ainfixl 8

    Rotate the argument right by the specified number of bits (which must be non-negative).

    An instance can define either this and rotateL or the unified rotate, depending on which is more convenient for the type in question.

  • popCount :: a -> Int

    Return the number of set bits in the argument. This number is known as the population count or the Hamming weight.

    Can be implemented using popCountDefault if a is also an instance of Num.

Instances68Bits, …
  • Bits IntegerDefined in ghc-internal-9.1003.0 · GHC.Internal.Bits
  • Bits NaturalDefined in ghc-internal-9.1003.0 · GHC.Internal.Bits
  • Bits EventTypeDefined in ghc-internal-9.1003.0 · GHC.Internal.Event.EPoll
  • Bits EventDefined in ghc-internal-9.1003.0 · GHC.Internal.Event.Poll
  • Bits CBoolDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CCharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CIntDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CIntMaxDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CIntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CLLongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CLongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CPtrdiffDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CSCharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CShortDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CSigAtomicDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CSizeDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CUCharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CUIntDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CUIntMaxDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CUIntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CULLongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CULongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CUShortDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits CWcharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Bits IntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Bits WordPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Bits Int16Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Bits Int32Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Bits Int64Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Bits Int8Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Bits CBlkCntDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CBlkSizeDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CClockIdDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CDevDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CFsBlkCntDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CFsFilCntDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CGidDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CIdDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CInoDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CKeyDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CModeDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CNfdsDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CNlinkDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits COffDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CPidDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CRLimDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CSocklenDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CSsizeDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CTcflagDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits CUidDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits FdDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Bits Word16Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Bits Word32Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Bits Word64Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Bits Word8Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Bits BoolDefined in ghc-internal-9.1003.0 · GHC.Internal.Bits

    Interpret Bool as 1-bit bit-field

  • Bits IntDefined in ghc-internal-9.1003.0 · GHC.Internal.Bits
  • Bits WordDefined in ghc-internal-9.1003.0 · GHC.Internal.Bits
  • Bits CAttributesDefined in unix-2.8.7.0 · System.Posix.Files.Common
  • Bits StatxFlagsDefined in unix-2.8.7.0 · System.Posix.Files.Common
  • Bits StatxMaskDefined in unix-2.8.7.0 · System.Posix.Files.Common
  • Bits a => Bits (And a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Bits (Iff a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Bits (Ior a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Bits (Xor a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Bits (Identity a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.Identity
  • Bits a => Bits (Down a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Ord
  • Bits a => Bits (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.Const
valuevoid :: IO a -> IO ()
#

Deprecated. use void instead

Discard the return value of an IO action

valuepopCountDefault :: (Bits a, Num a) => a -> Int
#

Default implementation for popCount.

This implementation is intentionally naive. Instances are expected to provide an optimized implementation for their size.

valuetoIntegralSized :: (Integral a, Integral b, Bits a, Bits b) => a -> Maybe b
#

Attempt to convert an Integral type a to an Integral type b using the size of the types as measured by Bits methods.

A simpler version of this function is:

toIntegral :: (Integral a, Integral b) => a -> Maybe b
toIntegral x
  | toInteger x == toInteger y = Just y
  | otherwise                  = Nothing
  where
    y = fromIntegral x

This version requires going through Integer, which can be inefficient. However, toIntegralSized is optimized to allow GHC to statically determine the relative type sizes (as measured by bitSizeMaybe and isSigned) and avoid going through Integer for many types. (The implementation uses fromIntegral, which is itself optimized with rules for base types but may go through Integer for some type pairs.)

value(.^.) :: Bits a => a -> a -> a
#

Infix version of xor.

valueoneBits :: FiniteBits a => a
#

A more concise version of complement zeroBits.

Example1 expression
complement (zeroBits :: Word) == (oneBits :: Word)True
Example1 expression
complement (oneBits :: Word) == (zeroBits :: Word)True

Note

The constraint on oneBits is arguably too strong. However, as some types (such as Natural) have undefined complement, this is the only safe choice.

classclass Bits b => FiniteBits b where
#

The FiniteBits class denotes types with a finite, fixed number of bits.

Methods

Instances63FiniteBits, …
newtypenewtype And a
#

Monoid under bitwise AND.

Example1 expression
getAnd (And 0xab <> And 0x12) :: Word82

Constructors

Instances9Bounded, Enum, Eq, Read, Show, Semigroup, …
  • Bounded a => Bounded (And a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Enum a => Enum (And a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Eq a => Eq (And a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Read a => Read (And a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Show a => Show (And a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Semigroup (And a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • FiniteBits a => Monoid (And a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits

    This constraint is arguably too strong. However, as some types (such as Natural) have undefined complement, this is the only safe choice.

  • Bits a => Bits (And a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • FiniteBits a => FiniteBits (And a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
newtypenewtype Iff a
#

Monoid under bitwise 'equality'; defined as 1 if the corresponding bits match, and 0 otherwise.

Example1 expression
getIff (Iff 0xab <> Iff 0x12) :: Word870

Constructors

Instances9Bounded, Enum, Eq, Read, Show, Semigroup, …
  • Bounded a => Bounded (Iff a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Enum a => Enum (Iff a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Eq a => Eq (Iff a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Read a => Read (Iff a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Show a => Show (Iff a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • FiniteBits a => Semigroup (Iff a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits

    This constraint is arguably too strong. However, as some types (such as Natural) have undefined complement, this is the only safe choice.

  • FiniteBits a => Monoid (Iff a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits

    This constraint is arguably too strong. However, as some types (such as Natural) have undefined complement, this is the only safe choice.

  • Bits a => Bits (Iff a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • FiniteBits a => FiniteBits (Iff a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
newtypenewtype Ior a
#

Monoid under bitwise inclusive OR.

Example1 expression
getIor (Ior 0xab <> Ior 0x12) :: Word8187

Constructors

Instances9Bounded, Enum, Eq, Read, Show, Semigroup, …
  • Bounded a => Bounded (Ior a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Enum a => Enum (Ior a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Eq a => Eq (Ior a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Read a => Read (Ior a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Show a => Show (Ior a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Semigroup (Ior a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Monoid (Ior a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Bits (Ior a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • FiniteBits a => FiniteBits (Ior a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
newtypenewtype Xor a
#

Monoid under bitwise XOR.

Example1 expression
getXor (Xor 0xab <> Xor 0x12) :: Word8185

Constructors

Instances9Bounded, Enum, Eq, Read, Show, Semigroup, …
  • Bounded a => Bounded (Xor a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Enum a => Enum (Xor a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Eq a => Eq (Xor a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Read a => Read (Xor a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Show a => Show (Xor a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Semigroup (Xor a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Monoid (Xor a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • Bits a => Bits (Xor a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
  • FiniteBits a => FiniteBits (Xor a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Bits
datadata Int
#

A fixed-precision integer type with at least the range [-2^29 .. 2^29-1]. The exact range for a given implementation can be determined by using Prelude.minBound and Prelude.maxBound from the Prelude.Bounded class.

Instances34Bounded, Enum, Integral, Data, Num, Read, …
datadata Word
#

A Word is an unsigned integral type, with the same size as Int.

Instances31Bounded, Enum, Integral, Data, Num, Read, …
datadata Word32
#

32-bit unsigned integer type

Instances21Bounded, Enum, Eq, Integral, Data, Num, …
  • Bounded Word32Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Enum Word32Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Eq Word32Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Integral Word32Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Data Word32Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Num Word32Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Ord Word32Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Read Word32Defined in ghc-internal-9.1003.0 · GHC.Internal.Read
  • Real Word32Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Show Word32Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Ix Word32Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Bits Word32Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • FiniteBits Word32Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Storable Word32Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • PrintfArg Word32Defined in base-4.20.2.0 · Text.Printf
  • NFData Word32Defined in deepseq-1.5.0.0 · Control.DeepSeq
  • Mantissa Word32Defined in bytestring-0.12.2.0 · Data.ByteString.Builder.RealFloat.Internal
  • Lift Word32Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • IArray UArray Word32Defined in array-0.5.8.0 · Data.Array.Base
  • MArray IOUArray Word32 IODefined in array-0.5.8.0 · Data.Array.IO.Internals
  • MArray (STUArray s) Word32 (ST s)Defined in array-0.5.8.0 · Data.Array.Base
datadata Word64
#

64-bit unsigned integer type

Instances21Bounded, Enum, Eq, Integral, Data, Num, …
  • Bounded Word64Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Enum Word64Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Eq Word64Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Integral Word64Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Data Word64Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Num Word64Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Ord Word64Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Read Word64Defined in ghc-internal-9.1003.0 · GHC.Internal.Read
  • Real Word64Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Show Word64Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Ix Word64Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Bits Word64Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • FiniteBits Word64Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Storable Word64Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • PrintfArg Word64Defined in base-4.20.2.0 · Text.Printf
  • NFData Word64Defined in deepseq-1.5.0.0 · Control.DeepSeq
  • Mantissa Word64Defined in bytestring-0.12.2.0 · Data.ByteString.Builder.RealFloat.Internal
  • Lift Word64Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • IArray UArray Word64Defined in array-0.5.8.0 · Data.Array.Base
  • MArray IOUArray Word64 IODefined in array-0.5.8.0 · Data.Array.IO.Internals
  • MArray (STUArray s) Word64 (ST s)Defined in array-0.5.8.0 · Data.Array.Base
datadata Word16
#

16-bit unsigned integer type

Instances20Bounded, Enum, Eq, Integral, Data, Num, …
  • Bounded Word16Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Enum Word16Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Eq Word16Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Integral Word16Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Data Word16Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Num Word16Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Ord Word16Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Read Word16Defined in ghc-internal-9.1003.0 · GHC.Internal.Read
  • Real Word16Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Show Word16Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Ix Word16Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Bits Word16Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • FiniteBits Word16Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Storable Word16Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • PrintfArg Word16Defined in base-4.20.2.0 · Text.Printf
  • NFData Word16Defined in deepseq-1.5.0.0 · Control.DeepSeq
  • Lift Word16Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • IArray UArray Word16Defined in array-0.5.8.0 · Data.Array.Base
  • MArray IOUArray Word16 IODefined in array-0.5.8.0 · Data.Array.IO.Internals
  • MArray (STUArray s) Word16 (ST s)Defined in array-0.5.8.0 · Data.Array.Base
datadata Word8
#

8-bit unsigned integer type

Instances20Bounded, Enum, Eq, Integral, Data, Num, …
  • Bounded Word8Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Enum Word8Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Eq Word8Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Integral Word8Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Data Word8Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Num Word8Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Ord Word8Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Read Word8Defined in ghc-internal-9.1003.0 · GHC.Internal.Read
  • Real Word8Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Show Word8Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Ix Word8Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Bits Word8Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • FiniteBits Word8Defined in ghc-internal-9.1003.0 · GHC.Internal.Word
  • Storable Word8Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • PrintfArg Word8Defined in base-4.20.2.0 · Text.Printf
  • NFData Word8Defined in deepseq-1.5.0.0 · Control.DeepSeq
  • Lift Word8Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • IArray UArray Word8Defined in array-0.5.8.0 · Data.Array.Base
  • MArray IOUArray Word8 IODefined in array-0.5.8.0 · Data.Array.IO.Internals
  • MArray (STUArray s) Word8 (ST s)Defined in array-0.5.8.0 · Data.Array.Base
datadata StablePtr a
#

A stable pointer is a reference to a Haskell expression that is guaranteed not to be affected by garbage collection, i.e., it will neither be deallocated nor will the value of the stable pointer itself change during garbage collection (ordinary references may be relocated during garbage collection). Consequently, stable pointers can be passed to foreign code, which can treat it as an opaque reference to a Haskell value.

The StablePtr 0 is reserved for representing NULL in foreign code.

A value of type StablePtr a is a stable pointer to a Haskell expression of type a.

Instances5IArray, Eq, Storable, MArray
datadata Int64
#

64-bit signed integer type

Instances20Bounded, Enum, Eq, Integral, Data, Num, …
  • Bounded Int64Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Enum Int64Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Eq Int64Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Integral Int64Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Data Int64Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Num Int64Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Ord Int64Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Read Int64Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Real Int64Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Show Int64Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Ix Int64Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Bits Int64Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • FiniteBits Int64Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Storable Int64Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • PrintfArg Int64Defined in base-4.20.2.0 · Text.Printf
  • NFData Int64Defined in deepseq-1.5.0.0 · Control.DeepSeq
  • Lift Int64Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • IArray UArray Int64Defined in array-0.5.8.0 · Data.Array.Base
  • MArray IOUArray Int64 IODefined in array-0.5.8.0 · Data.Array.IO.Internals
  • MArray (STUArray s) Int64 (ST s)Defined in array-0.5.8.0 · Data.Array.Base
datadata Int32
#

32-bit signed integer type

Instances20Bounded, Enum, Eq, Integral, Data, Num, …
  • Bounded Int32Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Enum Int32Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Eq Int32Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Integral Int32Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Data Int32Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Num Int32Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Ord Int32Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Read Int32Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Real Int32Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Show Int32Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Ix Int32Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Bits Int32Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • FiniteBits Int32Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Storable Int32Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • PrintfArg Int32Defined in base-4.20.2.0 · Text.Printf
  • NFData Int32Defined in deepseq-1.5.0.0 · Control.DeepSeq
  • Lift Int32Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • IArray UArray Int32Defined in array-0.5.8.0 · Data.Array.Base
  • MArray IOUArray Int32 IODefined in array-0.5.8.0 · Data.Array.IO.Internals
  • MArray (STUArray s) Int32 (ST s)Defined in array-0.5.8.0 · Data.Array.Base
datadata Int16
#

16-bit signed integer type

Instances20Bounded, Enum, Eq, Integral, Data, Num, …
  • Bounded Int16Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Enum Int16Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Eq Int16Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Integral Int16Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Data Int16Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Num Int16Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Ord Int16Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Read Int16Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Real Int16Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Show Int16Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Ix Int16Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Bits Int16Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • FiniteBits Int16Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Storable Int16Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • PrintfArg Int16Defined in base-4.20.2.0 · Text.Printf
  • NFData Int16Defined in deepseq-1.5.0.0 · Control.DeepSeq
  • Lift Int16Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • IArray UArray Int16Defined in array-0.5.8.0 · Data.Array.Base
  • MArray IOUArray Int16 IODefined in array-0.5.8.0 · Data.Array.IO.Internals
  • MArray (STUArray s) Int16 (ST s)Defined in array-0.5.8.0 · Data.Array.Base
datadata Int8
#

8-bit signed integer type

Instances20Bounded, Enum, Eq, Integral, Data, Num, …
  • Bounded Int8Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Enum Int8Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Eq Int8Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Integral Int8Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Data Int8Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Num Int8Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Ord Int8Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Read Int8Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Real Int8Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Show Int8Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Ix Int8Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Bits Int8Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • FiniteBits Int8Defined in ghc-internal-9.1003.0 · GHC.Internal.Int
  • Storable Int8Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • PrintfArg Int8Defined in base-4.20.2.0 · Text.Printf
  • NFData Int8Defined in deepseq-1.5.0.0 · Control.DeepSeq
  • Lift Int8Defined in template-haskell-2.22.0.0 · Language.Haskell.TH.Syntax
  • IArray UArray Int8Defined in array-0.5.8.0 · Data.Array.Base
  • MArray IOUArray Int8 IODefined in array-0.5.8.0 · Data.Array.IO.Internals
  • MArray (STUArray s) Int8 (ST s)Defined in array-0.5.8.0 · Data.Array.Base
classclass Storable a where
#

The member functions of this class facilitate writing values of primitive types to raw memory (which may have been allocated with the above mentioned routines) and reading values from blocks of raw memory. The class, furthermore, includes support for computing the storage requirements and alignment restrictions of storable types.

Memory addresses are represented as values of type Ptr a, for some a which is an instance of class Storable. The type argument to Ptr helps provide some valuable type safety in FFI code (you can't mix pointers of different types without an explicit cast), while helping the Haskell type system figure out which marshalling method is needed for a given pointer.

All marshalling between Haskell and a foreign language ultimately boils down to translating Haskell data structures into the binary representation of a corresponding data structure of the foreign language and vice versa. To code this marshalling in Haskell, it is necessary to manipulate primitive data types stored in unstructured memory blocks. The class Storable facilitates this manipulation on all types for which it is instantiated, which are the standard basic types of Haskell, the fixed size Int types (Int8, Int16, Int32, Int64), the fixed size Word types (Word8, Word16, Word32, Word64), StablePtr, all types from Foreign.C.Types, as well as Ptr.

Methods

  • sizeOf :: a -> Int

    Computes the storage requirements (in bytes) of the argument. The value of the argument is not used.

  • alignment :: a -> Int

    Computes the alignment constraint of the argument. An alignment constraint x is fulfilled by any address divisible by x. The alignment must be a power of two if this instance is to be used with alloca or allocaArray. The value of the argument is not used.

  • peekElemOff :: Ptr a -> Int -> IO a

    Read a value from a memory area regarded as an array of values of the same kind. The first argument specifies the start address of the array and the second the index into the array (the first element of the array has index 0). The following equality holds,

    peekElemOff addr idx = IOExts.fixIO $ \result ->
      peek (addr `plusPtr` (idx * sizeOf result))

    Note that this is only a specification, not necessarily the concrete implementation of the function.

  • pokeElemOff :: Ptr a -> Int -> a -> IO ()

    Write a value to a memory area regarded as an array of values of the same kind. The following equality holds:

    pokeElemOff addr idx x =
      poke (addr `plusPtr` (idx * sizeOf x)) x
  • peekByteOff :: Ptr b -> Int -> IO a

    Read a value from a memory location given by a base address and offset. The following equality holds:

    peekByteOff addr off = peek (addr `plusPtr` off)
  • pokeByteOff :: Ptr b -> Int -> a -> IO ()

    Write a value to a memory location given by a base address and offset. The following equality holds:

    pokeByteOff addr off x = poke (addr `plusPtr` off) x
  • peek :: Ptr a -> IO a

    Read a value from the given memory location.

    Note that the peek and poke functions might require properly aligned addresses to function correctly. This is architecture dependent; thus, portable code should ensure that when peeking or poking values of some type a, the alignment constraint for a, as given by the function alignment is fulfilled.

  • poke :: Ptr a -> a -> IO ()

    Write the given value to the given memory location. Alignment restrictions might apply; see peek.

Instances88Storable, …
  • Storable EventDefined in ghc-internal-9.1003.0 · GHC.Internal.Event.EPoll
  • Storable EventDefined in ghc-internal-9.1003.0 · GHC.Internal.Event.Poll
  • Storable PollFdDefined in ghc-internal-9.1003.0 · GHC.Internal.Event.Poll
  • Storable FingerprintDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable CBoolDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CCharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CClockDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CDoubleDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CFloatDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CIntDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CIntMaxDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CIntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CLLongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CLongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CPtrdiffDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CSCharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CSUSecondsDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CShortDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CSigAtomicDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CSizeDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CTimeDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CUCharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CUIntDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CUIntMaxDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CUIntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CULLongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CULongDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CUSecondsDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CUShortDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable CWcharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.C.Types
  • Storable IntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Storable WordPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Storable FLockDefined in ghc-internal-9.1003.0 · GHC.Internal.IO.Handle.Lock.LinuxOFD
  • Storable Int16Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable Int32Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable Int64Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable Int8Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable IoSubSystemDefined in ghc-internal-9.1003.0 · GHC.Internal.RTS.Flags
  • Storable CBlkCntDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CBlkSizeDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CCcDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CClockIdDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CDevDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CFsBlkCntDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CFsFilCntDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CGidDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CIdDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CInoDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CKeyDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CModeDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CNfdsDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CNlinkDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable COffDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CPidDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CRLimDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CSocklenDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CSpeedDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CSsizeDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CTcflagDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CTimerDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable CUidDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable FdDefined in ghc-internal-9.1003.0 · GHC.Internal.System.Posix.Types
  • Storable Word16Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable Word32Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable Word64Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable Word8Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable BoolDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable CharDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable DoubleDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable FloatDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable IntDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable WordDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable CTimespecDefined in time-1.12.2 · Data.Time.Clock.Internal.CTimespec
  • Storable CTimevalDefined in time-1.12.2 · Data.Time.Clock.Internal.CTimeval
  • Storable DirEntDefined in unix-2.8.7.0 · System.Posix.Directory.Common
  • Storable CAttributesDefined in unix-2.8.7.0 · System.Posix.Files.Common
  • Storable CTimeSpecDefined in unix-2.8.7.0 · System.Posix.Files.Common
  • Storable CTimeValDefined in unix-2.8.7.0 · System.Posix.Files.Common
  • Storable ()Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable (ConstPtr a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable (FunPtr a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable (Ptr a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable (StablePtr a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable a => Storable (Complex a)Defined in base-4.20.2.0 · Data.Complex
  • Storable a => Storable (Identity a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.Identity
  • Storable a => Storable (Down a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Ord
  • (Storable a, Integral a) => Storable (Ratio a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Storable
  • Storable a => Storable (Const a b)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Functor.Const
datadata Ptr a
#

A value of type Ptr a represents a pointer to an object, or an array of objects, which may be marshalled to or from Haskell values of type a.

The type a will often be an instance of class Storable which provides the marshalling operations. However this is not essential, and you can provide your own operations to access the pointer. For example you might write small foreign functions to get or set the fields of a C struct.

Instances20NFData1, IArray, Generic1, Data, Show, Foldable, …
valuecastStablePtrToPtr :: StablePtr a -> Ptr ()
#

Coerce a stable pointer to an address. No guarantees are made about the resulting value, except that the original stable pointer can be recovered by castPtrToStablePtr. In particular, the address might not refer to an accessible memory location and any attempt to pass it to the member functions of the class Storable leads to undefined behaviour.

valuenew :: Storable a => a -> IO (Ptr a)
#

Allocate a block of memory and marshal a value into it (the combination of malloc and poke). The size of the area allocated is determined by the sizeOf method from the instance of Storable for the appropriate type.

The memory may be deallocated using free or finalizerFree when no longer required.

datadata FunPtr a
#

A value of type FunPtr a is a pointer to a function callable from foreign code. The type a will normally be a foreign type, a function type with zero or more arguments where

A value of type FunPtr a may be a pointer to a foreign function, either returned by another foreign function or imported with a a static address import like

foreign import ccall "stdlib.h &free"
  p_free :: FunPtr (Ptr a -> IO ())

or a pointer to a Haskell function created using a wrapper stub declared to produce a FunPtr of the correct type. For example:

type Compare = Int -> Int -> Bool
foreign import ccall "wrapper"
  mkCompare :: Compare -> IO (FunPtr Compare)

Calls to wrapper stubs like mkCompare allocate storage, which should be released with freeHaskellFunPtr when no longer required.

To convert FunPtr values to corresponding Haskell functions, one can define a dynamic stub for the specific foreign type, e.g.

type IntFunction = CInt -> IO ()
foreign import ccall "dynamic"
  mkFun :: FunPtr IntFunction -> IntFunction
Instances9NFData1, IArray, Eq, Ord, Show, Storable, …
valuenewForeignPtr :: FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
#

Turns a plain memory reference into a foreign pointer, and associates a finalizer with the reference. The finalizer will be executed after the last reference to the foreign object is dropped. There is no guarantee of promptness, however the finalizer will be executed before the program exits.

valuealloca :: Storable a => (Ptr a -> IO b) -> IO b
#

alloca f executes the computation f, passing as argument a pointer to a temporarily allocated block of memory sufficient to hold values of type a.

The memory is freed when f terminates (either normally or via an exception), so the pointer passed to f must not be used after this.

valueallocaBytes :: Int -> (Ptr a -> IO b) -> IO b
#

allocaBytes n f executes the computation f, passing as argument a pointer to a temporarily allocated block of memory of n bytes. The block of memory is sufficiently aligned for any of the basic foreign types that fits into a memory block of the allocated size.

The memory is freed when f terminates (either normally or via an exception), so the pointer passed to f must not be used after this.

valueallocaBytesAligned :: Int -> Int -> (Ptr a -> IO b) -> IO b
#

allocaBytesAligned size align f executes the computation f, passing as argument a pointer to a temporarily allocated block of memory of size bytes and aligned to align bytes. The value of align must be a power of two.

The memory is freed when f terminates (either normally or via an exception), so the pointer passed to f must not be used after this.

valuemalloc :: Storable a => IO (Ptr a)
#

Allocate a block of memory that is sufficient to hold values of type a. The size of the area allocated is determined by the sizeOf method from the instance of Storable for the appropriate type.

The memory may be deallocated using free or finalizerFree when no longer required.

valuemallocBytes :: Int -> IO (Ptr a)
#

Allocate a block of memory of the given number of bytes. The block of memory is sufficiently aligned for any of the basic foreign types that fits into a memory block of the allocated size.

The memory may be deallocated using free or finalizerFree when no longer required.

valuerealloc :: Storable b => Ptr a -> IO (Ptr b)
#

Resize a memory area that was allocated with malloc or mallocBytes to the size needed to store values of type b. The returned pointer may refer to an entirely different memory area, but will be suitably aligned to hold values of type b. The contents of the referenced memory area will be the same as of the original pointer up to the minimum of the original size and the size of values of type b.

If the argument to realloc is nullPtr, realloc behaves like malloc.

valuereallocBytes :: Ptr a -> Int -> IO (Ptr a)
#

Resize a memory area that was allocated with malloc or mallocBytes to the given size. The returned pointer may refer to an entirely different memory area, but will be sufficiently aligned for any of the basic foreign types that fits into a memory block of the given size. The contents of the referenced memory area will be the same as of the original pointer up to the minimum of the original size and the given size.

If the pointer argument to reallocBytes is nullPtr, reallocBytes behaves like malloc. If the requested size is 0, reallocBytes behaves like free.

valuecopyArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
#

Copy the given number of elements from the second array (source) into the first array (destination); the copied areas may not overlap

valuemoveArray :: Storable a => Ptr a -> Ptr a -> Int -> IO ()
#

Copy the given number of elements from the second array (source) into the first array (destination); the copied areas may overlap

valuenewArray :: Storable a => [a] -> IO (Ptr a)
#

Write a list of storable elements into a newly allocated, consecutive sequence of storable values (like new, but for multiple elements).

valuenewArray0 :: Storable a => a -> [a] -> IO (Ptr a)
#

Write a list of storable elements into a newly allocated, consecutive sequence of storable values, where the end is fixed by the given end marker

valuepeekArray :: Storable a => Int -> Ptr a -> IO [a]
#

Convert an array of given length into a Haskell list. The implementation is tail-recursive and so uses constant stack space.

valuepeekArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO [a]
#

Convert an array terminated by the given end marker into a Haskell list

valuepokeArray0 :: Storable a => a -> Ptr a -> [a] -> IO ()
#

Write the list elements consecutive into memory and terminate them with the given marker element

valuewithArray :: Storable a => [a] -> (Ptr a -> IO b) -> IO b
#

Temporarily store a list of storable values in memory (like with, but for multiple elements).

valuethrowIf
  1. :: (a -> Bool)

    error condition on the result of the IO action

  2. -> (a -> String)

    computes an error message from erroneous results of the IO action

  3. -> IO a

    the IO action to be executed

  4. -> IO a
#

Execute an IO action, throwing a userError if the predicate yields True when applied to the result returned by the IO action. If no exception is raised, return the result of the computation.

valuefreePool :: Pool -> IO ()
#

Deallocate a memory pool and everything which has been allocated in the pool itself.

valuepooledMalloc :: Storable a => Pool -> IO (Ptr a)
#

Allocate space for storable type in the given pool. The size of the area allocated is determined by the sizeOf method from the instance of Storable for the appropriate type.

valuepooledMallocArray0 :: Storable a => Pool -> Int -> IO (Ptr a)
#

Allocate storage for the given number of elements of a storable type in the pool, but leave room for an extra element to signal the end of the array.

valuepooledNew :: Storable a => Pool -> a -> IO (Ptr a)
#

Allocate storage for a value in the given pool and marshal the value into this storage.

valuepooledNewArray :: Storable a => Pool -> [a] -> IO (Ptr a)
#

Allocate consecutive storage for a list of values in the given pool and marshal these values into it.

valuepooledNewArray0 :: Storable a => Pool -> a -> [a] -> IO (Ptr a)
#

Allocate consecutive storage for a list of values in the given pool and marshal these values into it, terminating the end with the given marker.

valuepooledReallocBytes :: Pool -> Ptr a -> Int -> IO (Ptr a)
#

Adjust the storage area for an element in the pool to the given size. Note that the previously allocated space is still retained in the same Pool and will only be freed when the entire Pool is freed.

valuewithPool :: (Pool -> IO b) -> IO b
#

Execute an action with a fresh memory pool, which gets automatically deallocated (including its contents) after the action has finished.

valuecopyBytes
  1. :: Ptr a

    Destination

  2. -> Ptr a

    Source

  3. -> Int

    Size in bytes

  4. -> IO ()
#

Copies the given number of bytes from the second area (source) into the first (destination); the copied areas may not overlap

valuefillBytes :: Ptr a -> Word8 -> Int -> IO ()
#

Fill a given number of bytes in memory area with a byte value.

valuemoveBytes
  1. :: Ptr a

    Destination

  2. -> Ptr a

    Source

  3. -> Int

    Size in bytes

  4. -> IO ()
#

Copies the given number of bytes from the second area (source) into the first (destination); the copied areas may overlap

valuetoBool :: (Eq a, Num a) => a -> Bool
#

Convert a Boolean in numeric representation to a Haskell value

valuewith :: Storable a => a -> (Ptr a -> IO b) -> IO b
#

with val f executes the computation f, passing as argument a pointer to a temporarily allocated block of memory into which val has been marshalled (the combination of alloca and poke).

The memory is freed when f terminates (either normally or via an exception), so the pointer passed to f must not be used after this.

valuewithMany :: (a -> (b -> res) -> res) -> [a] -> ([b] -> res) -> res
#

Replicates a withXXX combinator over a list of objects, yielding a list of marshalled objects

valuefreeHaskellFunPtr :: FunPtr a -> IO ()
#

Release the storage associated with the given FunPtr, which must have been obtained from a wrapper stub. This should be called whenever the return value from a foreign import wrapper function is no longer required; otherwise, the storage it uses will leak.

This function adds a finalizer to the given foreign object. The finalizer will run before all other finalizers for the same object which have already been registered.

valuefinalizeForeignPtr :: ForeignPtr a -> IO ()
#

Causes the finalizers associated with a foreign pointer to be run immediately. The foreign pointer must not be used again after this function is called. If the foreign pointer does not support finalizers, this is a no-op.

valuemallocForeignPtr :: Storable a => IO (ForeignPtr a)
#

Allocate some memory and return a ForeignPtr to it. The memory will be released automatically when the ForeignPtr is discarded.

mallocForeignPtr is equivalent to

   do { p <- malloc; newForeignPtr finalizerFree p }

although it may be implemented differently internally: you may not assume that the memory returned by mallocForeignPtr has been allocated with malloc.

GHC notes: mallocForeignPtr has a heavily optimised implementation in GHC. It uses pinned memory in the garbage collected heap, so the ForeignPtr does not require a finalizer to free the memory. Use of mallocForeignPtr and associated functions is strongly recommended in preference to newForeignPtr with a finalizer.

valueplusForeignPtr :: ForeignPtr a -> Int -> ForeignPtr b
#

Advances the given address by the given offset in bytes.

The new ForeignPtr shares the finalizer of the original, equivalent from a finalization standpoint to just creating another reference to the original. That is, the finalizer will not be called before the new ForeignPtr is unreachable, nor will it be called an additional time due to this call, and the finalizer will be called with the same address that it would have had this call not happened, *not* the new address.

valuetouchForeignPtr :: ForeignPtr a -> IO ()
#

This function ensures that the foreign object in question is alive at the given place in the sequence of IO actions. However, this comes with a significant caveat: the contract above does not hold if GHC can demonstrate that the code preceding touchForeignPtr diverges (e.g. by looping infinitely or throwing an exception). For this reason, you are strongly advised to use instead withForeignPtr where possible.

Also, note that this function should not be used to express dependencies between finalizers on ForeignPtrs. For example, if the finalizer for a ForeignPtr F1 calls touchForeignPtr on a second ForeignPtr F2, then the only guarantee is that the finalizer for F2 is never started before the finalizer for F1. They might be started together if for example both F1 and F2 are otherwise unreachable, and in that case the scheduler might end up running the finalizer for F2 first.

In general, it is not recommended to use finalizers on separate objects with ordering constraints between them. To express the ordering robustly requires explicit synchronisation using MVars between the finalizers, but even then the runtime sometimes runs multiple finalizers sequentially in a single thread (for performance reasons), so synchronisation between finalizers could result in artificial deadlock. Another alternative is to use explicit reference counting.

valuewithForeignPtr :: ForeignPtr a -> (Ptr a -> IO b) -> IO b
#

This is a way to look at the pointer living inside a foreign object. This function takes a function which is applied to that pointer. The resulting IO action is then executed. The foreign object is kept alive at least during the whole action, even if it is not used directly inside. Note that it is not safe to return the pointer from the action and use it after the action completes. All uses of the pointer should be inside the withForeignPtr bracket. The reason for this unsafeness is the same as for unsafeForeignPtrToPtr below: the finalizer may run earlier than expected, because the compiler can only track usage of the ForeignPtr object, not a Ptr object made from it.

This function is normally used for marshalling data to or from the object pointed to by the ForeignPtr, using the operations from the Storable class.

valuealignPtr :: Ptr a -> Int -> Ptr a
#

Given an arbitrary address and an alignment constraint, alignPtr yields the next higher address that fulfills the alignment constraint. An alignment constraint x is fulfilled by any address divisible by x. This operation is idempotent.

valuecastFunPtrToPtr :: FunPtr a -> Ptr b
#

Casts a FunPtr to a Ptr.

Note: this is valid only on architectures where data and function pointers range over the same set of addresses, and should only be used for bindings to external libraries whose interface already relies on this assumption.

valuecastPtrToFunPtr :: Ptr a -> FunPtr b
#

Casts a Ptr to a FunPtr.

Note: this is valid only on architectures where data and function pointers range over the same set of addresses, and should only be used for bindings to external libraries whose interface already relies on this assumption.

valueminusPtr :: Ptr a -> Ptr b -> Int
#

Computes the offset required to get from the second to the first argument. We have

p2 == p1 `plusPtr` (p2 `minusPtr` p1)
valuenullPtr :: Ptr a
#

The constant nullPtr contains a distinguished value of Ptr that is not associated with a valid memory location.

valueplusPtr :: Ptr a -> Int -> Ptr b
#

Advances the given address by the given offset in bytes.

newtypenewtype Pool
#

A memory pool.

newtypenewtype IntPtr
#

A signed integral type that can be losslessly converted to and from Ptr. This type is also compatible with the C99 type intptr_t, and can be marshalled to and from that type safely.

Constructors

Instances14Bounded, Enum, Eq, Integral, Data, Num, …
  • Bounded IntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Enum IntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Eq IntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Integral IntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Data IntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Num IntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Ord IntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Read IntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Real IntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Show IntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Ix IntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Bits IntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • FiniteBits IntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Storable IntPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
newtypenewtype WordPtr
#

An unsigned integral type that can be losslessly converted to and from Ptr. This type is also compatible with the C99 type uintptr_t, and can be marshalled to and from that type safely.

Constructors

Instances14Bounded, Enum, Eq, Integral, Data, Num, …
  • Bounded WordPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Enum WordPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Eq WordPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Integral WordPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Data WordPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Num WordPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Ord WordPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Read WordPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Real WordPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Show WordPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Ix WordPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Bits WordPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • FiniteBits WordPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
  • Storable WordPtrDefined in ghc-internal-9.1003.0 · GHC.Internal.Foreign.Ptr
typetype FinalizerPtr a = FunPtr (Ptr a -> IO ())
#

A finalizer is represented as a pointer to a foreign function that, at finalisation time, gets as an argument a plain pointer variant of the foreign pointer that the finalizer is associated with.

Note that the foreign function must either use the ccall or the capi calling convention.

datadata ForeignPtr a
#

The type ForeignPtr represents references to objects that are maintained in a foreign language, i.e., that are not part of the data structures usually managed by the Haskell storage manager. The essential difference between ForeignPtrs and vanilla memory references of type Ptr a is that the former may be associated with finalizers. A finalizer is a routine that is invoked when the Haskell storage manager detects that - within the Haskell heap and stack - there are no more references left that are pointing to the ForeignPtr. Typically, the finalizer will, then, invoke routines in the foreign language that free the resources bound by the foreign object.

The ForeignPtr is parameterised in the same way as Ptr. The type argument of ForeignPtr should normally be an instance of class Storable.

Instances4Eq, Data, Ord, Show
  • Eq (ForeignPtr a)Defined in ghc-internal-9.1003.0 · GHC.Internal.ForeignPtr
  • Data a => Data (ForeignPtr a)Defined in ghc-internal-9.1003.0 · GHC.Internal.Data.Data
  • Ord (ForeignPtr a)Defined in ghc-internal-9.1003.0 · GHC.Internal.ForeignPtr
  • Show (ForeignPtr a)Defined in ghc-internal-9.1003.0 · GHC.Internal.ForeignPtr