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

Modulebinary-0.8.9.3Haskell2010

Data.Binary.Get

The Get monad. A monad for efficiently building structures from encoded lazy ByteStrings.

Primitives are available to decode words of various sizes, both big and little endian.

Let's decode binary data representing illustrated here. In this example the values are in little endian.

+------------------+--------------+-----------------+
| 32 bit timestamp | 32 bit price | 16 bit quantity |
+------------------+--------------+-----------------+

A corresponding Haskell value looks like this:

data Trade = Trade
  { timestamp :: !Word32
  , price     :: !Word32
  , qty       :: !Word16
  } deriving (Show)
 

The fields in Trade are marked as strict (using !) since we don't need laziness here. In practise, you would probably consider using the UNPACK pragma as well. https://downloads.haskell.org/ghc/latest/docs/users_guide/exts/pragmas.html#unpack-pragma

Now, let's have a look at a decoder for this format.

getTrade :: Get Trade
getTrade = do
  timestamp <- getWord32le
  price     <- getWord32le
  quantity  <- getWord16le
  return $! Trade timestamp price quantity
 

Or even simpler using applicative style:

getTrade' :: Get Trade
getTrade' = Trade <$> getWord32le <*> getWord32le <*> getWord16le
 

There are two kinds of ways to execute this decoder, the lazy input method and the incremental input method. Here we will use the lazy input method.

Let's first define a function that decodes many Trades.

getTrades :: Get [Trade]
getTrades = do
  empty <- isEmpty
  if empty
    then return []
    else do trade <- getTrade
            trades <- getTrades
            return (trade:trades)
 

Finally, we run the decoder:

lazyIOExample :: IO [Trade]
lazyIOExample = do
  input <- BL.readFile "trades.bin"
  return (runGet getTrades input)
 

This decoder has the downside that it will need to read all the input before it can return. On the other hand, it will not return anything until it knows it could decode without any decoder errors.

You could also refactor to a left-fold, to decode in a more streaming fashion, and get the following decoder. It will start to return data without knowing that it can decode all input.

incrementalExample :: BL.ByteString -> [Trade]
incrementalExample input0 = go decoder input0
  where
    decoder = runGetIncremental getTrade
    go :: Decoder Trade -> BL.ByteString -> [Trade]
    go (Done leftover _consumed trade) input =
      trade : go decoder (BL.chunk leftover input)
    go (Partial k) input                     =
      go (k . takeHeadChunk $ input) (dropHeadChunk input)
    go (Fail _leftover _consumed msg) _input =
      error msg

takeHeadChunk :: BL.ByteString -> Maybe BS.ByteString
takeHeadChunk lbs =
  case lbs of
    (BL.Chunk bs _) -> Just bs
    _ -> Nothing

dropHeadChunk :: BL.ByteString -> BL.ByteString
dropHeadChunk lbs =
  case lbs of
    (BL.Chunk _ lbs') -> lbs'
    _ -> BL.Empty
 

The lazyIOExample uses lazy I/O to read the file from the disk, which is not suitable in all applications, and certainly not if you need to read from a socket which has higher likelihood to fail. To address these needs, use the incremental input method like in incrementalExample. For an example of how to read incrementally from a Handle, see the implementation of decodeFileOrFail.

  • 3 types
  • 49 values
  • Packagebinary-0.8.9.3
  • Exports52
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceGet.hs

The Get monad

1 declaration
newtypenewtype Get a
#
Instances6Monad, Functor, MonadFail, Applicative, Alternative, MonadPlus
  • Monad GetDefined in binary-0.8.9.3 · Data.Binary.Get.Internal
  • Functor GetDefined in binary-0.8.9.3 · Data.Binary.Get.Internal
  • MonadFail GetDefined in binary-0.8.9.3 · Data.Binary.Get.Internal
  • Applicative GetDefined in binary-0.8.9.3 · Data.Binary.Get.Internal
  • Alternative GetDefined in binary-0.8.9.3 · Data.Binary.Get.Internal
  • MonadPlus GetDefined in binary-0.8.9.3 · Data.Binary.Get.Internal

The lazy input interface

3 declarations

The lazy interface consumes a single lazy ByteString. It's the easiest interface to get started with, but it doesn't support interleaving I/O and parsing, unless lazy I/O is used.

There is no way to provide more input other than the initial data. To be able to incrementally give more data, see the incremental input interface.

valuerunGet :: Get a -> ByteString -> a
#

The simplest interface to run a Get decoder. If the decoder runs into an error, calls fail, or runs out of input, it will call error.

The incremental input interface

2 declarations

The incremental interface gives you more control over how input is provided during parsing. This lets you e.g. interleave parsing and I/O.

The incremental interface consumes a strict ByteString at a time, each being part of the total amount of input. If your decoder needs more input to finish it will return a Partial with a continuation. If there is no more input, provide it Nothing.

Fail will be returned if it runs into an error, together with a message, the position and the remaining input. If it succeeds it will return Done with the resulting value, the position and the remaining input.

datadata Decoder a
#

A decoder procuced by running a Get monad.

Constructors

  • Fail !ByteString !ByteOffset String

    The decoder ran into an error. The decoder either used fail or was not provided enough input. Contains any unconsumed input and the number of bytes consumed.

  • Partial (Maybe ByteString -> Decoder a)

    The decoder has consumed the available input and needs more to continue. Provide Just if more input is available and Nothing otherwise, and you will get a new Decoder.

  • Done !ByteString !ByteOffset a

    The decoder has successfully finished. Except for the output value you also get any unused input as well as the number of bytes consumed.

Providing input

Decoding

8 declarations
valueskip :: Int -> Get ()
#

Skip ahead n bytes. Fails if fewer than n bytes are available.

valueisEmpty :: Get Bool
#

Test whether all input has been consumed, i.e. there are no remaining undecoded bytes.

valueisolate
  1. :: Int

    The number of bytes that must be consumed

  2. -> Get a

    The decoder to isolate

  3. -> Get a
#

Isolate a decoder to operate with a fixed number of bytes, and fail if fewer bytes were consumed, or more bytes were attempted to be consumed. If the given decoder fails, isolate will also fail. Offset from bytesRead will be relative to the start of isolate, not the absolute of the input.

valuelookAhead :: Get a -> Get a
#

Run the given decoder, but without consuming its input. If the given decoder fails, then so will this function.

valuelookAheadM :: Get (Maybe a) -> Get (Maybe a)
#

Run the given decoder, and only consume its input if it returns Just. If Nothing is returned, the input will be unconsumed. If the given decoder fails, then so will this function.

valuelookAheadE :: Get (Either a b) -> Get (Either a b)
#

Run the given decoder, and only consume its input if it returns Right. If Left is returned, the input will be unconsumed. If the given decoder fails, then so will this function.

valuelabel :: String -> Get a -> Get a
#

Label a decoder. If the decoder fails, the label will be appended on a new line to the error message string.

ByteStrings

An efficient get method for strict ByteStrings. Fails if fewer than n bytes are left in the input. If n <= 0 then the empty string is returned.

Get a lazy ByteString that is terminated with a NUL byte. The returned string does not contain the NUL byte. Fails if it reaches the end of input without finding a NUL.

Get the remaining bytes as a lazy ByteString. Note that this can be an expensive function to use as it forces reading all input and keeping the string in-memory.

Decoding Words

Big-endian decoding

Little-endian decoding

Host-endian, unaligned decoding

valuegetWordhost :: Get Word
#

O(1). Read a single native machine word. The word is read in host order, host endian form, for the machine you're on. On a 64 bit machine the Word is an 8 byte value, on a 32 bit machine, 4 bytes.

Decoding Ints

Big-endian decoding

Little-endian decoding

Host-endian, unaligned decoding

Decoding Floats/Doubles

Deprecated functions

3 declarations

Deprecated. Use runGetIncremental instead. This function will be removed.

DEPRECATED. Provides compatibility with previous versions of this library. Run a Get monad and return a tuple with three values. The first value is the result of the decoder. The second and third are the unused input, and the number of consumed bytes.

valueremaining :: Get Int64
#

Deprecated. This will force all remaining input, don't use it.

DEPRECATED. Get the number of bytes of remaining input. Note that this is an expensive function to use as in order to calculate how much input remains, all input has to be read and kept in-memory. The decoder keeps the input as a strict bytestring, so you are likely better off by calculating the remaining input in another way.