    {-# LANGUAGE CPP       #-}
    {-# LANGUAGE MagicHash #-}
    {-# LANGUAGE RankNTypes #-}
    {-# LANGUAGE BangPatterns #-}

    -- |
    -- Module      : Codec.CBOR.FlatTerm
    -- Copyright   : (c) Duncan Coutts 2015-2017
    -- License     : BSD3-style (see LICENSE.txt)
    --
    -- Maintainer  : duncan@community.haskell.org
    -- Stability   : experimental
    -- Portability : non-portable (GHC extensions)
    --
    -- A simpler form than CBOR for writing out 'Enc.Encoding' values that allows
    -- easier verification and testing. While this library primarily focuses
    -- on taking 'Enc.Encoding' values (independent of any underlying format)
    -- and serializing them into CBOR format, this module offers an alternative
    -- format called 'FlatTerm' for serializing 'Enc.Encoding' values.
    --
    -- The 'FlatTerm' form is very simple and internally mirrors the original
    -- 'Encoding' type very carefully. The intention here is that once you
    -- have 'Enc.Encoding' and 'Dec.Decoding' values for your types, you can
    -- round-trip values through 'FlatTerm' to catch bugs more easily and with
    -- a smaller amount of code to look through.
    --
    -- For that reason, this module is primarily useful for client libraries,
    -- and even then, only for their test suites to offer a simpler form for
    -- doing encoding tests and catching problems in an encoder and decoder.
    --
    module Codec.CBOR.FlatTerm
      ( -- * Types
        FlatTerm      -- :: *
      , TermToken(..) -- :: *

        -- * Functions
      , toFlatTerm    -- :: Encoding -> FlatTerm
      , fromFlatTerm  -- :: Decoder s a -> FlatTerm -> Either String a
      , validFlatTerm -- :: FlatTerm -> Bool
      , decodeTermToken -- Decoder s TermToken
      ) where

    #include "cbor.h"

    import           Codec.CBOR.Encoding (Encoding(..))
    import qualified Codec.CBOR.Encoding as Enc
    import           Codec.CBOR.Decoding as Dec
    import qualified Codec.CBOR.Read     as Read
    import qualified Codec.CBOR.ByteArray        as BA
    import qualified Codec.CBOR.ByteArray.Sliced as BAS

    import           Data.Int
    #if defined(ARCH_32bit)
    import           GHC.Int   (Int64(I64#))
    import           GHC.Word  (Word64(W64#))
    import           GHC.Exts  (Word64#, Int64#)
    #endif
    import           GHC.Word  (Word(W#), Word8(W8#))
    import           GHC.Exts  (Int(I#), Int#, Word#, Float#, Double#)
    import           GHC.Float (Float(F#), Double(D#), float2Double)

    import           Data.Word
    import           Data.Text (Text)
    import qualified Data.Text.Encoding as TE
    import           Data.ByteString (ByteString)
    import qualified Data.ByteString as BS
    import           Control.Monad.ST
    import qualified Control.Monad.ST.Lazy as ST.Lazy

    import Prelude hiding (encodeFloat, decodeFloat)

    --------------------------------------------------------------------------------

    -- | A \"flat\" representation of an 'Enc.Encoding' value,
    -- useful for round-tripping and writing tests.
    --
    -- @since 0.2.0.0
    type FlatTerm = [TermToken]

    -- | A concrete encoding of 'Enc.Encoding' values, one
    -- which mirrors the original 'Enc.Encoding' type closely.
    --
    -- @since 0.2.0.0
    data TermToken
        = TkInt      {-# UNPACK #-} !Int
        | TkInteger                 !Integer
        | TkBytes    {-# UNPACK #-} !ByteString
        | TkBytesBegin
        | TkString   {-# UNPACK #-} !Text
        | TkStringBegin
        | TkListLen  {-# UNPACK #-} !Word
        | TkListBegin
        | TkMapLen   {-# UNPACK #-} !Word
        | TkMapBegin
        | TkBreak
        | TkTag      {-# UNPACK #-} !Word64
        | TkBool                    !Bool
        | TkNull
        | TkSimple   {-# UNPACK #-} !Word8
        | TkFloat16  {-# UNPACK #-} !Float
        | TkFloat32  {-# UNPACK #-} !Float
        | TkFloat64  {-# UNPACK #-} !Double
        deriving (TermToken -> TermToken -> Bool
    (TermToken -> TermToken -> Bool)
    -> (TermToken -> TermToken -> Bool) -> Eq TermToken
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    /= :: TermToken -> TermToken -> Bool
    $c/= :: TermToken -> TermToken -> Bool
    == :: TermToken -> TermToken -> Bool
    $c== :: TermToken -> TermToken -> Bool
    Eq, Eq TermToken
    Eq TermToken =>
    (TermToken -> TermToken -> Ordering)
    -> (TermToken -> TermToken -> Bool)
    -> (TermToken -> TermToken -> Bool)
    -> (TermToken -> TermToken -> Bool)
    -> (TermToken -> TermToken -> Bool)
    -> (TermToken -> TermToken -> TermToken)
    -> (TermToken -> TermToken -> TermToken)
    -> Ord TermToken
    TermToken -> TermToken -> Bool
    TermToken -> TermToken -> Ordering
    TermToken -> TermToken -> TermToken
    forall a.
    Eq a =>
    (a -> a -> Ordering)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> a)
    -> (a -> a -> a)
    -> Ord a
    min :: TermToken -> TermToken -> TermToken
    $cmin :: TermToken -> TermToken -> TermToken
    max :: TermToken -> TermToken -> TermToken
    $cmax :: TermToken -> TermToken -> TermToken
    >= :: TermToken -> TermToken -> Bool
    $c>= :: TermToken -> TermToken -> Bool
    > :: TermToken -> TermToken -> Bool
    $c> :: TermToken -> TermToken -> Bool
    <= :: TermToken -> TermToken -> Bool
    $c<= :: TermToken -> TermToken -> Bool
    < :: TermToken -> TermToken -> Bool
    $c< :: TermToken -> TermToken -> Bool
    compare :: TermToken -> TermToken -> Ordering
    $ccompare :: TermToken -> TermToken -> Ordering
    $cp1Ord :: Eq TermToken
    Ord, Int -> TermToken -> ShowS
    [TermToken] -> ShowS
    TermToken -> String
    (Int -> TermToken -> ShowS)
    -> (TermToken -> String)
    -> ([TermToken] -> ShowS)
    -> Show TermToken
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    showList :: [TermToken] -> ShowS
    $cshowList :: [TermToken] -> ShowS
    show :: TermToken -> String
    $cshow :: TermToken -> String
    showsPrec :: Int -> TermToken -> ShowS
    $cshowsPrec :: Int -> TermToken -> ShowS
    Show)

    --------------------------------------------------------------------------------

    -- | Convert an arbitrary 'Enc.Encoding' into a 'FlatTerm'.
    --
    -- @since 0.2.0.0
    toFlatTerm :: Encoding -- ^ The input 'Enc.Encoding'.
               -> FlatTerm -- ^ The resulting 'FlatTerm'.
    toFlatTerm :: Encoding -> [TermToken]
    toFlatTerm (Encoding tb :: Tokens -> Tokens
    tb) = Tokens -> [TermToken]
    convFlatTerm (Tokens -> Tokens
    tb Tokens
    Enc.TkEnd)

    convFlatTerm :: Enc.Tokens -> FlatTerm
    convFlatTerm :: Tokens -> [TermToken]
    convFlatTerm (Enc.TkWord     w :: Word
    w  ts :: Tokens
    ts)
      | Word
    w Word -> Word -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Word
    forall n. Num n => n
    maxInt                     = Int -> TermToken
    TkInt     (Word -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word
    w) TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
      | Bool
    otherwise                       = Integer -> TermToken
    TkInteger (Word -> Integer
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word
    w) TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkWord64   w :: Word64
    w  ts :: Tokens
    ts)
      | Word64
    w Word64 -> Word64 -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Word64
    forall n. Num n => n
    maxInt                     = Int -> TermToken
    TkInt     (Word64 -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word64
    w) TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
      | Bool
    otherwise                       = Integer -> TermToken
    TkInteger (Word64 -> Integer
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word64
    w) TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkInt      n :: Int
    n  ts :: Tokens
    ts) = Int -> TermToken
    TkInt       Int
    n TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkInt64    n :: Int64
    n  ts :: Tokens
    ts)
      | Int64
    n Int64 -> Int64 -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Int64
    forall n. Num n => n
    maxInt Bool -> Bool -> Bool
    && Int64
    n Int64 -> Int64 -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Int64
    forall n. Num n => n
    minInt      = Int -> TermToken
    TkInt     (Int64 -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int64
    n) TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
      | Bool
    otherwise                       = Integer -> TermToken
    TkInteger (Int64 -> Integer
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int64
    n) TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkInteger  n :: Integer
    n  ts :: Tokens
    ts)
      | Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Integer
    forall n. Num n => n
    maxInt Bool -> Bool -> Bool
    && Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Integer
    forall n. Num n => n
    minInt      = Int -> TermToken
    TkInt (Integer -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Integer
    n) TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
      | Bool
    otherwise                       = Integer -> TermToken
    TkInteger   Integer
    n TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkBytes    bs :: ByteString
    bs ts :: Tokens
    ts) = ByteString -> TermToken
    TkBytes    ByteString
    bs TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkBytesBegin  ts :: Tokens
    ts) = TermToken
    TkBytesBegin  TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkByteArray a :: SlicedByteArray
    a ts :: Tokens
    ts)
      = ByteString -> TermToken
    TkBytes (SlicedByteArray -> ByteString
    BAS.toByteString SlicedByteArray
    a) TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkString   st :: Text
    st ts :: Tokens
    ts) = Text -> TermToken
    TkString   Text
    st TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkStringBegin ts :: Tokens
    ts) = TermToken
    TkStringBegin TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkUtf8ByteArray a :: SlicedByteArray
    a ts :: Tokens
    ts)
      = Text -> TermToken
    TkString (ByteString -> Text
    TE.decodeUtf8 (ByteString -> Text) -> ByteString -> Text
    forall a b. (a -> b) -> a -> b
    $ SlicedByteArray -> ByteString
    BAS.toByteString SlicedByteArray
    a) TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkListLen  n :: Word
    n  ts :: Tokens
    ts) = Word -> TermToken
    TkListLen   Word
    n TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkListBegin   ts :: Tokens
    ts) = TermToken
    TkListBegin   TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkMapLen   n :: Word
    n  ts :: Tokens
    ts) = Word -> TermToken
    TkMapLen    Word
    n TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkMapBegin    ts :: Tokens
    ts) = TermToken
    TkMapBegin    TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkTag      n :: Word
    n  ts :: Tokens
    ts) = Word64 -> TermToken
    TkTag (Word -> Word64
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word
    n) TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkTag64    n :: Word64
    n  ts :: Tokens
    ts) = Word64 -> TermToken
    TkTag       Word64
    n TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkBool     b :: Bool
    b  ts :: Tokens
    ts) = Bool -> TermToken
    TkBool      Bool
    b TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkNull        ts :: Tokens
    ts) = TermToken
    TkNull        TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkUndef       ts :: Tokens
    ts) = Word8 -> TermToken
    TkSimple   23 TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkSimple   n :: Word8
    n  ts :: Tokens
    ts) = Word8 -> TermToken
    TkSimple    Word8
    n TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkFloat16  f :: Float
    f  ts :: Tokens
    ts) = Float -> TermToken
    TkFloat16   Float
    f TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkFloat32  f :: Float
    f  ts :: Tokens
    ts) = Float -> TermToken
    TkFloat32   Float
    f TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkFloat64  f :: Double
    f  ts :: Tokens
    ts) = Double -> TermToken
    TkFloat64   Double
    f TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkBreak       ts :: Tokens
    ts) = TermToken
    TkBreak       TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm (Enc.TkEncoded  bs :: ByteString
    bs ts :: Tokens
    ts) = ByteString -> [TermToken]
    decodePreEncoded ByteString
    bs
                                                       [TermToken] -> [TermToken] -> [TermToken]
    forall a. [a] -> [a] -> [a]
    ++ Tokens -> [TermToken]
    convFlatTerm Tokens
    ts
    convFlatTerm  Enc.TkEnd             = []

    --------------------------------------------------------------------------------

    decodePreEncoded :: BS.ByteString -> FlatTerm
    decodePreEncoded :: ByteString -> [TermToken]
    decodePreEncoded bs0 :: ByteString
    bs0 =
        (forall s. ST s [TermToken]) -> [TermToken]
    forall a. (forall s. ST s a) -> a
    ST.Lazy.runST (ByteString -> ST s [TermToken]
    forall s. ByteString -> ST s [TermToken]
    provideInput ByteString
    bs0)
      where
        provideInput :: BS.ByteString -> ST.Lazy.ST s FlatTerm
        provideInput :: ByteString -> ST s [TermToken]
    provideInput bs :: ByteString
    bs
          | ByteString -> Bool
    BS.null ByteString
    bs = [TermToken] -> ST s [TermToken]
    forall (m :: * -> *) a. Monad m => a -> m a
    return []
          | Bool
    otherwise  = do
              IDecode s TermToken
    next <- ST s (IDecode s TermToken) -> ST s (IDecode s TermToken)
    forall s a. ST s a -> ST s a
    ST.Lazy.strictToLazyST (ST s (IDecode s TermToken) -> ST s (IDecode s TermToken))
    -> ST s (IDecode s TermToken) -> ST s (IDecode s TermToken)
    forall a b. (a -> b) -> a -> b
    $ do
                  -- This will always be a 'Partial' here because decodeTermToken
                  -- always starts by requesting initial input. Only decoders that
                  -- fail or return a value without looking at their input can give
                  -- a different initial result.
                  Read.Partial k :: Maybe ByteString -> ST s (IDecode s TermToken)
    k <- Decoder s TermToken -> ST s (IDecode s TermToken)
    forall s a. Decoder s a -> ST s (IDecode s a)
    Read.deserialiseIncremental Decoder s TermToken
    forall s. Decoder s TermToken
    decodeTermToken
                  Maybe ByteString -> ST s (IDecode s TermToken)
    k (ByteString -> Maybe ByteString
    forall a. a -> Maybe a
    Just ByteString
    bs)
              IDecode s TermToken -> ST s [TermToken]
    forall s. IDecode s TermToken -> ST s [TermToken]
    collectOutput IDecode s TermToken
    next

        collectOutput :: Read.IDecode s TermToken -> ST.Lazy.ST s FlatTerm
        collectOutput :: IDecode s TermToken -> ST s [TermToken]
    collectOutput (Read.Fail _ _ err :: DeserialiseFailure
    err) = String -> ST s [TermToken]
    forall (m :: * -> *) a. MonadFail m => String -> m a
    fail (String -> ST s [TermToken]) -> String -> ST s [TermToken]
    forall a b. (a -> b) -> a -> b
    $ "toFlatTerm: encodePreEncoded "
                                                String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ "used with invalid CBOR: "
                                                String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ DeserialiseFailure -> String
    forall a. Show a => a -> String
    show DeserialiseFailure
    err
        collectOutput (Read.Partial    k :: Maybe ByteString -> ST s (IDecode s TermToken)
    k) = ST s (IDecode s TermToken) -> ST s (IDecode s TermToken)
    forall s a. ST s a -> ST s a
    ST.Lazy.strictToLazyST (Maybe ByteString -> ST s (IDecode s TermToken)
    k Maybe ByteString
    forall a. Maybe a
    Nothing)
                                            ST s (IDecode s TermToken)
    -> (IDecode s TermToken -> ST s [TermToken]) -> ST s [TermToken]
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= IDecode s TermToken -> ST s [TermToken]
    forall s. IDecode s TermToken -> ST s [TermToken]
    collectOutput
        collectOutput (Read.Done bs' :: ByteString
    bs' _ x :: TermToken
    x) = do [TermToken]
    xs <- ByteString -> ST s [TermToken]
    forall s. ByteString -> ST s [TermToken]
    provideInput ByteString
    bs'
                                               [TermToken] -> ST s [TermToken]
    forall (m :: * -> *) a. Monad m => a -> m a
    return (TermToken
    x TermToken -> [TermToken] -> [TermToken]
    forall a. a -> [a] -> [a]
    : [TermToken]
    xs)

    decodeTermToken :: Decoder s TermToken
    decodeTermToken :: Decoder s TermToken
    decodeTermToken = do
        TokenType
    tkty <- Decoder s TokenType
    forall s. Decoder s TokenType
    peekTokenType
        case TokenType
    tkty of
          TypeUInt   -> do Word
    w <- Decoder s Word
    forall s. Decoder s Word
    decodeWord
                           TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return (TermToken -> Decoder s TermToken)
    -> TermToken -> Decoder s TermToken
    forall a b. (a -> b) -> a -> b
    $! Word -> TermToken
    fromWord Word
    w
                        where
                          fromWord :: Word -> TermToken
                          fromWord :: Word -> TermToken
    fromWord w :: Word
    w
                            | Word
    w Word -> Word -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Int -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int
    forall a. Bounded a => a
    maxBound :: Int)
                                        = Int -> TermToken
    TkInt     (Word -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word
    w)
                            | Bool
    otherwise = Integer -> TermToken
    TkInteger (Word -> Integer
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word
    w)

          TypeUInt64 -> do Word64
    w <- Decoder s Word64
    forall s. Decoder s Word64
    decodeWord64
                           TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return (TermToken -> Decoder s TermToken)
    -> TermToken -> Decoder s TermToken
    forall a b. (a -> b) -> a -> b
    $! Word64 -> TermToken
    forall a. Integral a => a -> TermToken
    fromWord64 Word64
    w
                        where
                          fromWord64 :: a -> TermToken
    fromWord64 w :: a
    w
                            | a
    w a -> a -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Int -> a
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int
    forall a. Bounded a => a
    maxBound :: Int)
                                        = Int -> TermToken
    TkInt     (a -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral a
    w)
                            | Bool
    otherwise = Integer -> TermToken
    TkInteger (a -> Integer
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral a
    w)

          TypeNInt   -> do Word
    w <- Decoder s Word
    forall s. Decoder s Word
    decodeNegWord
                           TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return (TermToken -> Decoder s TermToken)
    -> TermToken -> Decoder s TermToken
    forall a b. (a -> b) -> a -> b
    $! Word -> TermToken
    forall a. Integral a => a -> TermToken
    fromNegWord Word
    w
                        where
                          fromNegWord :: a -> TermToken
    fromNegWord w :: a
    w
                            | a
    w a -> a -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Int -> a
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int
    forall a. Bounded a => a
    maxBound :: Int)
                                        = Int -> TermToken
    TkInt     (-1 Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - a -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral a
    w)
                            | Bool
    otherwise = Integer -> TermToken
    TkInteger (-1 Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - a -> Integer
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral a
    w)

          TypeNInt64 -> do Word64
    w <- Decoder s Word64
    forall s. Decoder s Word64
    decodeNegWord64
                           TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return (TermToken -> Decoder s TermToken)
    -> TermToken -> Decoder s TermToken
    forall a b. (a -> b) -> a -> b
    $! Word64 -> TermToken
    forall a. Integral a => a -> TermToken
    fromNegWord64 Word64
    w
                        where
                          fromNegWord64 :: a -> TermToken
    fromNegWord64 w :: a
    w
                            | a
    w a -> a -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Int -> a
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int
    forall a. Bounded a => a
    maxBound :: Int)
                                        = Int -> TermToken
    TkInt     (-1 Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - a -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral a
    w)
                            | Bool
    otherwise = Integer -> TermToken
    TkInteger (-1 Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - a -> Integer
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral a
    w)

          TypeInteger -> do !Integer
    x <- Decoder s Integer
    forall s. Decoder s Integer
    decodeInteger
                            TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Integer -> TermToken
    TkInteger Integer
    x)
          TypeFloat16 -> do !Float
    x <- Decoder s Float
    forall s. Decoder s Float
    decodeFloat
                            TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Float -> TermToken
    TkFloat16 Float
    x)
          TypeFloat32 -> do !Float
    x <- Decoder s Float
    forall s. Decoder s Float
    decodeFloat
                            TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Float -> TermToken
    TkFloat32 Float
    x)
          TypeFloat64 -> do !Double
    x <- Decoder s Double
    forall s. Decoder s Double
    decodeDouble
                            TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Double -> TermToken
    TkFloat64 Double
    x)

          TypeBytes        -> do !ByteString
    x <- Decoder s ByteString
    forall s. Decoder s ByteString
    decodeBytes
                                 TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return (ByteString -> TermToken
    TkBytes ByteString
    x)
          TypeBytesIndef   -> do Decoder s ()
    forall s. Decoder s ()
    decodeBytesIndef
                                 TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return TermToken
    TkBytesBegin
          TypeString       -> do !Text
    x <- Decoder s Text
    forall s. Decoder s Text
    decodeString
                                 TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text -> TermToken
    TkString Text
    x)
          TypeStringIndef  -> do Decoder s ()
    forall s. Decoder s ()
    decodeStringIndef
                                 TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return TermToken
    TkStringBegin

          TypeListLen      -> do !Int
    x <- Decoder s Int
    forall s. Decoder s Int
    decodeListLen
                                 TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return (TermToken -> Decoder s TermToken)
    -> TermToken -> Decoder s TermToken
    forall a b. (a -> b) -> a -> b
    $! Word -> TermToken
    TkListLen (Int -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    x)
          TypeListLen64    -> do !Int
    x <- Decoder s Int
    forall s. Decoder s Int
    decodeListLen
                                 TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return (TermToken -> Decoder s TermToken)
    -> TermToken -> Decoder s TermToken
    forall a b. (a -> b) -> a -> b
    $! Word -> TermToken
    TkListLen (Int -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    x)
          TypeListLenIndef -> do Decoder s ()
    forall s. Decoder s ()
    decodeListLenIndef
                                 TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return TermToken
    TkListBegin
          TypeMapLen       -> do !Int
    x <- Decoder s Int
    forall s. Decoder s Int
    decodeMapLen
                                 TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return (TermToken -> Decoder s TermToken)
    -> TermToken -> Decoder s TermToken
    forall a b. (a -> b) -> a -> b
    $! Word -> TermToken
    TkMapLen (Int -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    x)
          TypeMapLen64     -> do !Int
    x <- Decoder s Int
    forall s. Decoder s Int
    decodeMapLen
                                 TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return (TermToken -> Decoder s TermToken)
    -> TermToken -> Decoder s TermToken
    forall a b. (a -> b) -> a -> b
    $! Word -> TermToken
    TkMapLen (Int -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    x)
          TypeMapLenIndef  -> do Decoder s ()
    forall s. Decoder s ()
    decodeMapLenIndef
                                 TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return TermToken
    TkMapBegin

          TypeTag          -> do !Word
    x <- Decoder s Word
    forall s. Decoder s Word
    decodeTag
                                 TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return (TermToken -> Decoder s TermToken)
    -> TermToken -> Decoder s TermToken
    forall a b. (a -> b) -> a -> b
    $! Word64 -> TermToken
    TkTag (Word -> Word64
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word
    x)
          TypeTag64        -> do !Word64
    x <- Decoder s Word64
    forall s. Decoder s Word64
    decodeTag64
                                 TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return (TermToken -> Decoder s TermToken)
    -> TermToken -> Decoder s TermToken
    forall a b. (a -> b) -> a -> b
    $! Word64 -> TermToken
    TkTag (Word64 -> Word64
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word64
    x)

          TypeBool    -> do !Bool
    x <- Decoder s Bool
    forall s. Decoder s Bool
    decodeBool
                            TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Bool -> TermToken
    TkBool Bool
    x)
          TypeNull    -> do Decoder s ()
    forall s. Decoder s ()
    decodeNull
                            TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return TermToken
    TkNull
          TypeSimple  -> do !Word8
    x <- Decoder s Word8
    forall s. Decoder s Word8
    decodeSimple
                            TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Word8 -> TermToken
    TkSimple Word8
    x)
          TypeBreak   -> do Bool
    _ <- Decoder s Bool
    forall s. Decoder s Bool
    decodeBreakOr
                            TermToken -> Decoder s TermToken
    forall (m :: * -> *) a. Monad m => a -> m a
    return TermToken
    TkBreak
          TypeInvalid -> String -> Decoder s TermToken
    forall (m :: * -> *) a. MonadFail m => String -> m a
    fail "invalid token encoding"

    --------------------------------------------------------------------------------

    -- | Given a 'Dec.Decoder', decode a 'FlatTerm' back into
    -- an ordinary value, or return an error.
    --
    -- @since 0.2.0.0
    fromFlatTerm :: (forall s. Decoder s a)
                                    -- ^ A 'Dec.Decoder' for a serialised value.
                 -> FlatTerm        -- ^ The serialised 'FlatTerm'.
                 -> Either String a -- ^ The deserialised value, or an error.
    fromFlatTerm :: (forall s. Decoder s a) -> [TermToken] -> Either String a
    fromFlatTerm decoder :: forall s. Decoder s a
    decoder ft :: [TermToken]
    ft =
        (forall s. ST s (Either String a)) -> Either String a
    forall a. (forall s. ST s a) -> a
    runST (Decoder s a -> ST s (DecodeAction s a)
    forall s a. Decoder s a -> ST s (DecodeAction s a)
    getDecodeAction Decoder s a
    forall s. Decoder s a
    decoder ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ft)
      where
        go :: FlatTerm -> DecodeAction s a -> ST s (Either String a)
        go :: [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go (TkInt     n :: Int
    n : ts :: [TermToken]
    ts) (ConsumeWord k :: Word# -> ST s (DecodeAction s a)
    k)
            | Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= 0                             = Word# -> ST s (DecodeAction s a)
    k (Word -> Word#
    unW# (Int -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInteger n :: Integer
    n : ts :: [TermToken]
    ts) (ConsumeWord k :: Word# -> ST s (DecodeAction s a)
    k)
            | Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    >= 0                             = Word# -> ST s (DecodeAction s a)
    k (Word -> Word#
    unW# (Integer -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Integer
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInt     n :: Int
    n : ts :: [TermToken]
    ts) (ConsumeWord8 k :: Word# -> ST s (DecodeAction s a)
    k)
            | Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= 0 Bool -> Bool -> Bool
    && Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Int
    forall n. Num n => n
    maxWord8            = Word# -> ST s (DecodeAction s a)
    k (Word -> Word#
    unW# (Int -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInteger n :: Integer
    n : ts :: [TermToken]
    ts) (ConsumeWord8 k :: Word# -> ST s (DecodeAction s a)
    k)
            | Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    >= 0 Bool -> Bool -> Bool
    && Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Integer
    forall n. Num n => n
    maxWord8            = Word# -> ST s (DecodeAction s a)
    k (Word -> Word#
    unW# (Integer -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Integer
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInt     n :: Int
    n : ts :: [TermToken]
    ts) (ConsumeWord16 k :: Word# -> ST s (DecodeAction s a)
    k)
            | Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= 0 Bool -> Bool -> Bool
    && Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Int
    forall n. Num n => n
    maxWord16           = Word# -> ST s (DecodeAction s a)
    k (Word -> Word#
    unW# (Int -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInteger n :: Integer
    n : ts :: [TermToken]
    ts) (ConsumeWord16 k :: Word# -> ST s (DecodeAction s a)
    k)
            | Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    >= 0 Bool -> Bool -> Bool
    && Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Integer
    forall n. Num n => n
    maxWord16           = Word# -> ST s (DecodeAction s a)
    k (Word -> Word#
    unW# (Integer -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Integer
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInt     n :: Int
    n : ts :: [TermToken]
    ts) (ConsumeWord32 k :: Word# -> ST s (DecodeAction s a)
    k)
            -- NOTE: we have to be very careful about this branch
            -- on 32 bit machines, because maxBound :: Int < maxBound :: Word32
            | Int -> Bool
    intIsValidWord32 Int
    n                 = Word# -> ST s (DecodeAction s a)
    k (Word -> Word#
    unW# (Int -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInteger n :: Integer
    n : ts :: [TermToken]
    ts) (ConsumeWord32 k :: Word# -> ST s (DecodeAction s a)
    k)
            | Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    >= 0 Bool -> Bool -> Bool
    && Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Integer
    forall n. Num n => n
    maxWord32           = Word# -> ST s (DecodeAction s a)
    k (Word -> Word#
    unW# (Integer -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Integer
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInt     n :: Int
    n : ts :: [TermToken]
    ts) (ConsumeNegWord k :: Word# -> ST s (DecodeAction s a)
    k)
            | Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    <  0                             = Word# -> ST s (DecodeAction s a)
    k (Word -> Word#
    unW# (Int -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (-1Int -> Int -> Int
    forall a. Num a => a -> a -> a
    -Int
    n))) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInteger n :: Integer
    n : ts :: [TermToken]
    ts) (ConsumeNegWord k :: Word# -> ST s (DecodeAction s a)
    k)
            | Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    <  0                             = Word# -> ST s (DecodeAction s a)
    k (Word -> Word#
    unW# (Integer -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (-1Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    -Integer
    n))) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInt     n :: Int
    n : ts :: [TermToken]
    ts) (ConsumeInt k :: Int# -> ST s (DecodeAction s a)
    k)     = Int# -> ST s (DecodeAction s a)
    k (Int -> Int#
    unI# Int
    n) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInteger n :: Integer
    n : ts :: [TermToken]
    ts) (ConsumeInt k :: Int# -> ST s (DecodeAction s a)
    k)
            | Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Integer
    forall n. Num n => n
    maxInt                        = Int# -> ST s (DecodeAction s a)
    k (Int -> Int#
    unI# (Integer -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Integer
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInt     n :: Int
    n : ts :: [TermToken]
    ts) (ConsumeInt8 k :: Int# -> ST s (DecodeAction s a)
    k)
            | Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Int
    forall n. Num n => n
    minInt8 Bool -> Bool -> Bool
    && Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Int
    forall n. Num n => n
    maxInt8       = Int# -> ST s (DecodeAction s a)
    k (Int -> Int#
    unI# Int
    n) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInteger n :: Integer
    n : ts :: [TermToken]
    ts) (ConsumeInt8 k :: Int# -> ST s (DecodeAction s a)
    k)
            | Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Integer
    forall n. Num n => n
    minInt8 Bool -> Bool -> Bool
    && Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Integer
    forall n. Num n => n
    maxInt8       = Int# -> ST s (DecodeAction s a)
    k (Int -> Int#
    unI# (Integer -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Integer
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInt     n :: Int
    n : ts :: [TermToken]
    ts) (ConsumeInt16 k :: Int# -> ST s (DecodeAction s a)
    k)
            | Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Int
    forall n. Num n => n
    minInt16 Bool -> Bool -> Bool
    && Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Int
    forall n. Num n => n
    maxInt16     = Int# -> ST s (DecodeAction s a)
    k (Int -> Int#
    unI# Int
    n) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInteger n :: Integer
    n : ts :: [TermToken]
    ts) (ConsumeInt16 k :: Int# -> ST s (DecodeAction s a)
    k)
            | Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Integer
    forall n. Num n => n
    minInt16 Bool -> Bool -> Bool
    && Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Integer
    forall n. Num n => n
    maxInt16     = Int# -> ST s (DecodeAction s a)
    k (Int -> Int#
    unI# (Integer -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Integer
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInt     n :: Int
    n : ts :: [TermToken]
    ts) (ConsumeInt32 k :: Int# -> ST s (DecodeAction s a)
    k)
            | Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Int
    forall n. Num n => n
    minInt32 Bool -> Bool -> Bool
    && Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Int
    forall n. Num n => n
    maxInt32     = Int# -> ST s (DecodeAction s a)
    k (Int -> Int#
    unI# Int
    n) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInteger n :: Integer
    n : ts :: [TermToken]
    ts) (ConsumeInt32 k :: Int# -> ST s (DecodeAction s a)
    k)
            | Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Integer
    forall n. Num n => n
    minInt32 Bool -> Bool -> Bool
    && Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Integer
    forall n. Num n => n
    maxInt32     = Int# -> ST s (DecodeAction s a)
    k (Int -> Int#
    unI# (Integer -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Integer
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInt     n :: Int
    n : ts :: [TermToken]
    ts) (ConsumeInteger k :: Integer -> ST s (DecodeAction s a)
    k) = Integer -> ST s (DecodeAction s a)
    k (Int -> Integer
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    n) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInteger n :: Integer
    n : ts :: [TermToken]
    ts) (ConsumeInteger k :: Integer -> ST s (DecodeAction s a)
    k) = Integer -> ST s (DecodeAction s a)
    k Integer
    n ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkListLen n :: Word
    n : ts :: [TermToken]
    ts) (ConsumeListLen k :: Int# -> ST s (DecodeAction s a)
    k)
            | Word
    n Word -> Word -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Word
    forall n. Num n => n
    maxInt                        = Int# -> ST s (DecodeAction s a)
    k (Int -> Int#
    unI# (Word -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkMapLen  n :: Word
    n : ts :: [TermToken]
    ts) (ConsumeMapLen  k :: Int# -> ST s (DecodeAction s a)
    k)
            | Word
    n Word -> Word -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Word
    forall n. Num n => n
    maxInt                        = Int# -> ST s (DecodeAction s a)
    k (Int -> Int#
    unI# (Word -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkTag     n :: Word64
    n : ts :: [TermToken]
    ts) (ConsumeTag     k :: Word# -> ST s (DecodeAction s a)
    k)
            | Word64
    n Word64 -> Word64 -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Word64
    forall n. Num n => n
    maxWord                       = Word# -> ST s (DecodeAction s a)
    k (Word -> Word#
    unW# (Word64 -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word64
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts

        go (TkInt     n :: Int
    n : ts :: [TermToken]
    ts) (ConsumeWordCanonical k :: Word# -> ST s (DecodeAction s a)
    k)
            | Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= 0                             = Word# -> ST s (DecodeAction s a)
    k (Word -> Word#
    unW# (Int -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInteger n :: Integer
    n : ts :: [TermToken]
    ts) (ConsumeWordCanonical k :: Word# -> ST s (DecodeAction s a)
    k)
            | Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    >= 0                             = Word# -> ST s (DecodeAction s a)
    k (Word -> Word#
    unW# (Integer -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Integer
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInt     n :: Int
    n : ts :: [TermToken]
    ts) (ConsumeWord8Canonical k :: Word# -> ST s (DecodeAction s a)
    k)
            | Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= 0 Bool -> Bool -> Bool
    && Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Int
    forall n. Num n => n
    maxWord8            = Word# -> ST s (DecodeAction s a)
    k (Word -> Word#
    unW# (Int -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInteger n :: Integer
    n : ts :: [TermToken]
    ts) (ConsumeWord8Canonical k :: Word# -> ST s (DecodeAction s a)
    k)
            | Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    >= 0 Bool -> Bool -> Bool
    && Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Integer
    forall n. Num n => n
    maxWord8            = Word# -> ST s (DecodeAction s a)
    k (Word -> Word#
    unW# (Integer -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Integer
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInt     n :: Int
    n : ts :: [TermToken]
    ts) (ConsumeWord16Canonical k :: Word# -> ST s (DecodeAction s a)
    k)
            | Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= 0 Bool -> Bool -> Bool
    && Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Int
    forall n. Num n => n
    maxWord16           = Word# -> ST s (DecodeAction s a)
    k (Word -> Word#
    unW# (Int -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInteger n :: Integer
    n : ts :: [TermToken]
    ts) (ConsumeWord16Canonical k :: Word# -> ST s (DecodeAction s a)
    k)
            | Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    >= 0 Bool -> Bool -> Bool
    && Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Integer
    forall n. Num n => n
    maxWord16           = Word# -> ST s (DecodeAction s a)
    k (Word -> Word#
    unW# (Integer -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Integer
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInt     n :: Int
    n : ts :: [TermToken]
    ts) (ConsumeWord32Canonical k :: Word# -> ST s (DecodeAction s a)
    k)
            -- NOTE: we have to be very careful about this branch
            -- on 32 bit machines, because maxBound :: Int < maxBound :: Word32
            | Int -> Bool
    intIsValidWord32 Int
    n                 = Word# -> ST s (DecodeAction s a)
    k (Word -> Word#
    unW# (Int -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInteger n :: Integer
    n : ts :: [TermToken]
    ts) (ConsumeWord32Canonical k :: Word# -> ST s (DecodeAction s a)
    k)
            | Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    >= 0 Bool -> Bool -> Bool
    && Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Integer
    forall n. Num n => n
    maxWord32           = Word# -> ST s (DecodeAction s a)
    k (Word -> Word#
    unW# (Integer -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Integer
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInt     n :: Int
    n : ts :: [TermToken]
    ts) (ConsumeNegWordCanonical k :: Word# -> ST s (DecodeAction s a)
    k)
            | Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    <  0                             = Word# -> ST s (DecodeAction s a)
    k (Word -> Word#
    unW# (Int -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (-1Int -> Int -> Int
    forall a. Num a => a -> a -> a
    -Int
    n))) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInteger n :: Integer
    n : ts :: [TermToken]
    ts) (ConsumeNegWordCanonical k :: Word# -> ST s (DecodeAction s a)
    k)
            | Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    <  0                             = Word# -> ST s (DecodeAction s a)
    k (Word -> Word#
    unW# (Integer -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (-1Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    -Integer
    n))) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInt     n :: Int
    n : ts :: [TermToken]
    ts) (ConsumeIntCanonical k :: Int# -> ST s (DecodeAction s a)
    k)     = Int# -> ST s (DecodeAction s a)
    k (Int -> Int#
    unI# Int
    n) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInteger n :: Integer
    n : ts :: [TermToken]
    ts) (ConsumeInt k :: Int# -> ST s (DecodeAction s a)
    k)
            | Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Integer
    forall n. Num n => n
    maxInt                        = Int# -> ST s (DecodeAction s a)
    k (Int -> Int#
    unI# (Integer -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Integer
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInt     n :: Int
    n : ts :: [TermToken]
    ts) (ConsumeInt8Canonical k :: Int# -> ST s (DecodeAction s a)
    k)
            | Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Int
    forall n. Num n => n
    minInt8 Bool -> Bool -> Bool
    && Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Int
    forall n. Num n => n
    maxInt8       = Int# -> ST s (DecodeAction s a)
    k (Int -> Int#
    unI# Int
    n) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInteger n :: Integer
    n : ts :: [TermToken]
    ts) (ConsumeInt8Canonical k :: Int# -> ST s (DecodeAction s a)
    k)
            | Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Integer
    forall n. Num n => n
    minInt8 Bool -> Bool -> Bool
    && Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Integer
    forall n. Num n => n
    maxInt8       = Int# -> ST s (DecodeAction s a)
    k (Int -> Int#
    unI# (Integer -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Integer
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInt     n :: Int
    n : ts :: [TermToken]
    ts) (ConsumeInt16Canonical k :: Int# -> ST s (DecodeAction s a)
    k)
            | Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Int
    forall n. Num n => n
    minInt16 Bool -> Bool -> Bool
    && Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Int
    forall n. Num n => n
    maxInt16     = Int# -> ST s (DecodeAction s a)
    k (Int -> Int#
    unI# Int
    n) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInteger n :: Integer
    n : ts :: [TermToken]
    ts) (ConsumeInt16Canonical k :: Int# -> ST s (DecodeAction s a)
    k)
            | Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Integer
    forall n. Num n => n
    minInt16 Bool -> Bool -> Bool
    && Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Integer
    forall n. Num n => n
    maxInt16     = Int# -> ST s (DecodeAction s a)
    k (Int -> Int#
    unI# (Integer -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Integer
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInt     n :: Int
    n : ts :: [TermToken]
    ts) (ConsumeInt32Canonical k :: Int# -> ST s (DecodeAction s a)
    k)
            | Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Int
    forall n. Num n => n
    minInt32 Bool -> Bool -> Bool
    && Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Int
    forall n. Num n => n
    maxInt32     = Int# -> ST s (DecodeAction s a)
    k (Int -> Int#
    unI# Int
    n) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInteger n :: Integer
    n : ts :: [TermToken]
    ts) (ConsumeInt32Canonical k :: Int# -> ST s (DecodeAction s a)
    k)
            | Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Integer
    forall n. Num n => n
    minInt32 Bool -> Bool -> Bool
    && Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Integer
    forall n. Num n => n
    maxInt32     = Int# -> ST s (DecodeAction s a)
    k (Int -> Int#
    unI# (Integer -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Integer
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInt     n :: Int
    n : ts :: [TermToken]
    ts) (ConsumeIntegerCanonical k :: Integer -> ST s (DecodeAction s a)
    k) = Integer -> ST s (DecodeAction s a)
    k (Int -> Integer
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    n) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkInteger n :: Integer
    n : ts :: [TermToken]
    ts) (ConsumeIntegerCanonical k :: Integer -> ST s (DecodeAction s a)
    k) = Integer -> ST s (DecodeAction s a)
    k Integer
    n ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkListLen n :: Word
    n : ts :: [TermToken]
    ts) (ConsumeListLenCanonical k :: Int# -> ST s (DecodeAction s a)
    k)
            | Word
    n Word -> Word -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Word
    forall n. Num n => n
    maxInt                        = Int# -> ST s (DecodeAction s a)
    k (Int -> Int#
    unI# (Word -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkMapLen  n :: Word
    n : ts :: [TermToken]
    ts) (ConsumeMapLenCanonical  k :: Int# -> ST s (DecodeAction s a)
    k)
            | Word
    n Word -> Word -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Word
    forall n. Num n => n
    maxInt                        = Int# -> ST s (DecodeAction s a)
    k (Int -> Int#
    unI# (Word -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkTag     n :: Word64
    n : ts :: [TermToken]
    ts) (ConsumeTagCanonical     k :: Word# -> ST s (DecodeAction s a)
    k)
            | Word64
    n Word64 -> Word64 -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Word64
    forall n. Num n => n
    maxWord                       = Word# -> ST s (DecodeAction s a)
    k (Word -> Word#
    unW# (Word64 -> Word
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word64
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts

    #if defined(ARCH_32bit)
        -- 64bit variants for 32bit machines
        go (TkInt       n : ts) (ConsumeWord64    k)
          | n >= 0                                   = k (unW64# (fromIntegral n)) >>= go ts
        go (TkInteger   n : ts) (ConsumeWord64    k)
          | n >= 0                                   = k (unW64# (fromIntegral n)) >>= go ts
        go (TkInt       n : ts) (ConsumeNegWord64 k)
          | n < 0                                    = k (unW64# (fromIntegral (-1-n))) >>= go ts
        go (TkInteger   n : ts) (ConsumeNegWord64 k)
          | n < 0                                    = k (unW64# (fromIntegral (-1-n))) >>= go ts

        go (TkInt       n : ts) (ConsumeInt64     k) = k (unI64# (fromIntegral n)) >>= go ts
        go (TkInteger   n : ts) (ConsumeInt64     k) = k (unI64# (fromIntegral n)) >>= go ts

        go (TkTag       n : ts) (ConsumeTag64     k) = k (unW64# n) >>= go ts

        go (TkInt       n : ts) (ConsumeWord64Canonical    k)
          | n >= 0                                   = k (unW64# (fromIntegral n)) >>= go ts
        go (TkInteger   n : ts) (ConsumeWord64Canonical    k)
          | n >= 0                                   = k (unW64# (fromIntegral n)) >>= go ts
        go (TkInt       n : ts) (ConsumeNegWord64Canonical k)
          | n < 0                                    = k (unW64# (fromIntegral (-1-n))) >>= go ts
        go (TkInteger   n : ts) (ConsumeNegWord64Canonical k)
          | n < 0                                    = k (unW64# (fromIntegral (-1-n))) >>= go ts

        go (TkInt       n : ts) (ConsumeInt64Canonical     k) = k (unI64# (fromIntegral n)) >>= go ts
        go (TkInteger   n : ts) (ConsumeInt64Canonical     k) = k (unI64# (fromIntegral n)) >>= go ts

        go (TkTag       n : ts) (ConsumeTag64Canonical     k) = k (unW64# n) >>= go ts

        -- TODO FIXME (aseipp/dcoutts): are these going to be utilized?
        -- see fallthrough case below if/when fixed.
        go ts (ConsumeListLen64 _)          = unexpected "decodeListLen64" ts
        go ts (ConsumeMapLen64  _)          = unexpected "decodeMapLen64"  ts
        go ts (ConsumeListLen64Canonical _) = unexpected "decodeListLen64Canonical" ts
        go ts (ConsumeMapLen64Canonical  _) = unexpected "decodeMapLen64Canonical"  ts
    #endif

        go (TkFloat16 f :: Float
    f : ts :: [TermToken]
    ts) (ConsumeFloat  k :: Float# -> ST s (DecodeAction s a)
    k)        = Float# -> ST s (DecodeAction s a)
    k (Float -> Float#
    unF# Float
    f) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkFloat32 f :: Float
    f : ts :: [TermToken]
    ts) (ConsumeFloat  k :: Float# -> ST s (DecodeAction s a)
    k)        = Float# -> ST s (DecodeAction s a)
    k (Float -> Float#
    unF# Float
    f) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkFloat16 f :: Float
    f : ts :: [TermToken]
    ts) (ConsumeDouble k :: Double# -> ST s (DecodeAction s a)
    k)        = Double# -> ST s (DecodeAction s a)
    k (Double -> Double#
    unD# (Float -> Double
    float2Double Float
    f)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkFloat32 f :: Float
    f : ts :: [TermToken]
    ts) (ConsumeDouble k :: Double# -> ST s (DecodeAction s a)
    k)        = Double# -> ST s (DecodeAction s a)
    k (Double -> Double#
    unD# (Float -> Double
    float2Double Float
    f)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkFloat64 f :: Double
    f : ts :: [TermToken]
    ts) (ConsumeDouble k :: Double# -> ST s (DecodeAction s a)
    k)        = Double# -> ST s (DecodeAction s a)
    k (Double -> Double#
    unD# Double
    f) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkBytes  bs :: ByteString
    bs : ts :: [TermToken]
    ts) (ConsumeBytes  k :: ByteString -> ST s (DecodeAction s a)
    k)        = ByteString -> ST s (DecodeAction s a)
    k ByteString
    bs ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkBytes  bs :: ByteString
    bs : ts :: [TermToken]
    ts) (ConsumeByteArray k :: ByteArray -> ST s (DecodeAction s a)
    k)     = ByteArray -> ST s (DecodeAction s a)
    k (ByteString -> ByteArray
    BA.fromByteString ByteString
    bs) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkString st :: Text
    st : ts :: [TermToken]
    ts) (ConsumeString k :: Text -> ST s (DecodeAction s a)
    k)        = Text -> ST s (DecodeAction s a)
    k Text
    st ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkString st :: Text
    st : ts :: [TermToken]
    ts) (ConsumeUtf8ByteArray k :: ByteArray -> ST s (DecodeAction s a)
    k) = ByteArray -> ST s (DecodeAction s a)
    k (ByteString -> ByteArray
    BA.fromByteString (ByteString -> ByteArray) -> ByteString -> ByteArray
    forall a b. (a -> b) -> a -> b
    $ Text -> ByteString
    TE.encodeUtf8 Text
    st)
                                                         ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkBool    b :: Bool
    b : ts :: [TermToken]
    ts) (ConsumeBool   k :: Bool -> ST s (DecodeAction s a)
    k)        = Bool -> ST s (DecodeAction s a)
    k Bool
    b ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkSimple  n :: Word8
    n : ts :: [TermToken]
    ts) (ConsumeSimple k :: Word# -> ST s (DecodeAction s a)
    k)        = Word# -> ST s (DecodeAction s a)
    k (Word8 -> Word#
    unW8# Word8
    n) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts

        go (TkFloat16 f :: Float
    f : ts :: [TermToken]
    ts) (ConsumeFloat16Canonical k :: Float# -> ST s (DecodeAction s a)
    k)       = Float# -> ST s (DecodeAction s a)
    k (Float -> Float#
    unF# Float
    f) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkFloat32 f :: Float
    f : ts :: [TermToken]
    ts) (ConsumeFloatCanonical   k :: Float# -> ST s (DecodeAction s a)
    k)       = Float# -> ST s (DecodeAction s a)
    k (Float -> Float#
    unF# Float
    f) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkFloat64 f :: Double
    f : ts :: [TermToken]
    ts) (ConsumeDoubleCanonical  k :: Double# -> ST s (DecodeAction s a)
    k)       = Double# -> ST s (DecodeAction s a)
    k (Double -> Double#
    unD# Double
    f) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkBytes  bs :: ByteString
    bs : ts :: [TermToken]
    ts) (ConsumeBytesCanonical  k :: ByteString -> ST s (DecodeAction s a)
    k)        = ByteString -> ST s (DecodeAction s a)
    k ByteString
    bs ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkBytes  bs :: ByteString
    bs : ts :: [TermToken]
    ts) (ConsumeByteArrayCanonical k :: ByteArray -> ST s (DecodeAction s a)
    k)     = ByteArray -> ST s (DecodeAction s a)
    k (ByteString -> ByteArray
    BA.fromByteString ByteString
    bs) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkString st :: Text
    st : ts :: [TermToken]
    ts) (ConsumeStringCanonical k :: Text -> ST s (DecodeAction s a)
    k)        = Text -> ST s (DecodeAction s a)
    k Text
    st ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkString st :: Text
    st : ts :: [TermToken]
    ts) (ConsumeUtf8ByteArrayCanonical k :: ByteArray -> ST s (DecodeAction s a)
    k) = ByteArray -> ST s (DecodeAction s a)
    k (ByteString -> ByteArray
    BA.fromByteString (ByteString -> ByteArray) -> ByteString -> ByteArray
    forall a b. (a -> b) -> a -> b
    $ Text -> ByteString
    TE.encodeUtf8 Text
    st)
                                                                  ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkSimple  n :: Word8
    n : ts :: [TermToken]
    ts) (ConsumeSimpleCanonical  k :: Word# -> ST s (DecodeAction s a)
    k)       = Word# -> ST s (DecodeAction s a)
    k (Word8 -> Word#
    unW8# Word8
    n) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts

        go (TkBytesBegin  : ts :: [TermToken]
    ts) (ConsumeBytesIndef   da :: ST s (DecodeAction s a)
    da) = ST s (DecodeAction s a)
    da ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkStringBegin : ts :: [TermToken]
    ts) (ConsumeStringIndef  da :: ST s (DecodeAction s a)
    da) = ST s (DecodeAction s a)
    da ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkListBegin   : ts :: [TermToken]
    ts) (ConsumeListLenIndef da :: ST s (DecodeAction s a)
    da) = ST s (DecodeAction s a)
    da ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkMapBegin    : ts :: [TermToken]
    ts) (ConsumeMapLenIndef  da :: ST s (DecodeAction s a)
    da) = ST s (DecodeAction s a)
    da ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkNull        : ts :: [TermToken]
    ts) (ConsumeNull         da :: ST s (DecodeAction s a)
    da) = ST s (DecodeAction s a)
    da ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts

        go (TkListLen n :: Word
    n : ts :: [TermToken]
    ts) (ConsumeListLenOrIndef k :: Int# -> ST s (DecodeAction s a)
    k)
            | Word
    n Word -> Word -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Word
    forall n. Num n => n
    maxInt                               = Int# -> ST s (DecodeAction s a)
    k (Int -> Int#
    unI# (Word -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkListBegin : ts :: [TermToken]
    ts) (ConsumeListLenOrIndef k :: Int# -> ST s (DecodeAction s a)
    k) = Int# -> ST s (DecodeAction s a)
    k (-1#) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkMapLen  n :: Word
    n : ts :: [TermToken]
    ts) (ConsumeMapLenOrIndef  k :: Int# -> ST s (DecodeAction s a)
    k)
            | Word
    n Word -> Word -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Word
    forall n. Num n => n
    maxInt                               = Int# -> ST s (DecodeAction s a)
    k (Int -> Int#
    unI# (Word -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word
    n)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkMapBegin  : ts :: [TermToken]
    ts) (ConsumeMapLenOrIndef  k :: Int# -> ST s (DecodeAction s a)
    k) = Int# -> ST s (DecodeAction s a)
    k (-1#) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go (TkBreak     : ts :: [TermToken]
    ts) (ConsumeBreakOr        k :: Bool -> ST s (DecodeAction s a)
    k) = Bool -> ST s (DecodeAction s a)
    k Bool
    True ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go ts :: [TermToken]
    ts@(_        : _ ) (ConsumeBreakOr        k :: Bool -> ST s (DecodeAction s a)
    k) = Bool -> ST s (DecodeAction s a)
    k Bool
    False ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts

        go ts :: [TermToken]
    ts@(tk :: TermToken
    tk:_) (PeekTokenType k :: TokenType -> ST s (DecodeAction s a)
    k) = TokenType -> ST s (DecodeAction s a)
    k (TermToken -> TokenType
    tokenTypeOf TermToken
    tk) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
        go ts :: [TermToken]
    ts        (PeekTokenType _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "peekTokenType" [TermToken]
    ts

        -- We don't have real bytes so we have to give these two operations
        -- different interpretations: remaining tokens and just 0 for offsets.
        go ts :: [TermToken]
    ts        (PeekAvailable k :: Int# -> ST s (DecodeAction s a)
    k) = Int# -> ST s (DecodeAction s a)
    k (Int -> Int#
    unI# ([TermToken] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length [TermToken]
    ts)) ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
    #if defined(ARCH_32bit)
        go ts        (PeekByteOffset k)= k (unI64# 0) >>= go ts
    #else
        go ts :: [TermToken]
    ts        (PeekByteOffset k :: Int# -> ST s (DecodeAction s a)
    k)= Int# -> ST s (DecodeAction s a)
    k 0# ST s (DecodeAction s a)
    -> (DecodeAction s a -> ST s (Either String a))
    -> ST s (Either String a)
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= [TermToken] -> DecodeAction s a -> ST s (Either String a)
    forall s a.
    [TermToken] -> DecodeAction s a -> ST s (Either String a)
    go [TermToken]
    ts
    #endif

        go _  (Fail msg :: String
    msg) = Either String a -> ST s (Either String a)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Either String a -> ST s (Either String a))
    -> Either String a -> ST s (Either String a)
    forall a b. (a -> b) -> a -> b
    $ String -> Either String a
    forall a b. a -> Either a b
    Left String
    msg
        go [] (Done x :: a
    x)   = Either String a -> ST s (Either String a)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Either String a -> ST s (Either String a))
    -> Either String a -> ST s (Either String a)
    forall a b. (a -> b) -> a -> b
    $ a -> Either String a
    forall a b. b -> Either a b
    Right a
    x
        go ts :: [TermToken]
    ts (Done _)   = Either String a -> ST s (Either String a)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Either String a -> ST s (Either String a))
    -> Either String a -> ST s (Either String a)
    forall a b. (a -> b) -> a -> b
    $ String -> Either String a
    forall a b. a -> Either a b
    Left ("trailing tokens: " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ [TermToken] -> String
    forall a. Show a => a -> String
    show (Int -> [TermToken] -> [TermToken]
    forall a. Int -> [a] -> [a]
    take 5 [TermToken]
    ts))

        ----------------------------------------------------------------------------
        -- Fallthrough cases: unhandled token/DecodeAction combinations

        go ts :: [TermToken]
    ts (ConsumeWord    _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeWord"    [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeWord8   _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeWord8"   [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeWord16  _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeWord16"  [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeWord32  _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeWord32"  [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeNegWord _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeNegWord" [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeInt     _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeInt"     [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeInt8    _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeInt8"    [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeInt16   _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeInt16"   [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeInt32   _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeInt32"   [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeInteger _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeInteger" [TermToken]
    ts

        go ts :: [TermToken]
    ts (ConsumeListLen _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeListLen" [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeMapLen  _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeMapLen"  [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeTag     _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeTag"     [TermToken]
    ts

        go ts :: [TermToken]
    ts (ConsumeWordCanonical    _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeWordCanonical"    [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeWord8Canonical   _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeWord8Canonical"   [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeWord16Canonical  _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeWord16Canonical"  [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeWord32Canonical  _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeWord32Canonical"  [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeNegWordCanonical _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeNegWordCanonical" [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeIntCanonical     _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeIntCanonical"     [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeInt8Canonical    _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeInt8Canonical"    [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeInt16Canonical   _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeInt16Canonical"   [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeInt32Canonical   _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeInt32Canonical"   [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeIntegerCanonical _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeIntegerCanonical" [TermToken]
    ts

        go ts :: [TermToken]
    ts (ConsumeListLenCanonical _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeListLenCanonical" [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeMapLenCanonical  _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeMapLenCanonical"  [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeTagCanonical     _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeTagCanonical"     [TermToken]
    ts

        go ts :: [TermToken]
    ts (ConsumeFloat  _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeFloat"  [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeDouble _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeDouble" [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeBytes  _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeBytes"  [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeByteArray     _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeByteArray"     [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeString _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeString" [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeUtf8ByteArray _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeUtf8ByteArray" [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeBool   _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeBool"   [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeSimple _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeSimple" [TermToken]
    ts

        go ts :: [TermToken]
    ts (ConsumeFloat16Canonical _)       = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeFloat16Canonical"       [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeFloatCanonical   _)       = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeFloatCanonical"         [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeDoubleCanonical  _)       = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeDoubleCanonical"        [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeBytesCanonical  _)        = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeBytesCanonical"         [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeByteArrayCanonical _)     = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeByteArrayCanonical"     [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeStringCanonical _)        = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeStringCanonical"        [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeUtf8ByteArrayCanonical _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeUtf8ByteArrayCanonical" [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeSimpleCanonical  _)       = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeSimpleCanonical"        [TermToken]
    ts

    #if defined(ARCH_32bit)
        -- 64bit variants for 32bit machines
        go ts (ConsumeWord64    _) = unexpected "decodeWord64"    ts
        go ts (ConsumeNegWord64 _) = unexpected "decodeNegWord64" ts
        go ts (ConsumeInt64     _) = unexpected "decodeInt64"     ts
        go ts (ConsumeTag64     _) = unexpected "decodeTag64"     ts
      --go ts (ConsumeListLen64 _) = unexpected "decodeListLen64" ts
      --go ts (ConsumeMapLen64  _) = unexpected "decodeMapLen64"  ts

        go ts (ConsumeWord64Canonical    _) = unexpected "decodeWord64Canonical"    ts
        go ts (ConsumeNegWord64Canonical _) = unexpected "decodeNegWord64Canonical" ts
        go ts (ConsumeInt64Canonical     _) = unexpected "decodeInt64Canonical"     ts
        go ts (ConsumeTag64Canonical     _) = unexpected "decodeTag64Canonical"     ts
      --go ts (ConsumeListLen64Canonical _) = unexpected "decodeListLen64Canonical" ts
      --go ts (ConsumeMapLen64Canonical  _) = unexpected "decodeMapLen64Canonical"  ts
    #endif

        go ts :: [TermToken]
    ts (ConsumeBytesIndef   _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeBytesIndef"   [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeStringIndef  _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeStringIndef"  [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeListLenIndef _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeListLenIndef" [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeMapLenIndef  _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeMapLenIndef"  [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeNull         _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeNull"         [TermToken]
    ts

        go ts :: [TermToken]
    ts (ConsumeListLenOrIndef _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeListLenOrIndef" [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeMapLenOrIndef  _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeMapLenOrIndef"  [TermToken]
    ts
        go ts :: [TermToken]
    ts (ConsumeBreakOr        _) = String -> [TermToken] -> ST s (Either String a)
    forall (m :: * -> *) a b.
    (Monad m, Show a) =>
    String -> [a] -> m (Either String b)
    unexpected "decodeBreakOr"        [TermToken]
    ts

        unexpected :: String -> [a] -> m (Either String b)
    unexpected name :: String
    name []      = Either String b -> m (Either String b)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Either String b -> m (Either String b))
    -> Either String b -> m (Either String b)
    forall a b. (a -> b) -> a -> b
    $ String -> Either String b
    forall a b. a -> Either a b
    Left (String -> Either String b) -> String -> Either String b
    forall a b. (a -> b) -> a -> b
    $ String
    name String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ ": unexpected end of input"
        unexpected name :: String
    name (tok :: a
    tok:_) = Either String b -> m (Either String b)
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Either String b -> m (Either String b))
    -> Either String b -> m (Either String b)
    forall a b. (a -> b) -> a -> b
    $ String -> Either String b
    forall a b. a -> Either a b
    Left (String -> Either String b) -> String -> Either String b
    forall a b. (a -> b) -> a -> b
    $ String
    name String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ ": unexpected token " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ a -> String
    forall a. Show a => a -> String
    show a
    tok

    -- | Map a 'TermToken' to the underlying CBOR 'TokenType'
    tokenTypeOf :: TermToken -> TokenType
    tokenTypeOf :: TermToken -> TokenType
    tokenTypeOf (TkInt n :: Int
    n)
        | Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= 0                = TokenType
    TypeUInt
        | Bool
    otherwise             = TokenType
    TypeNInt
    tokenTypeOf TkInteger{}     = TokenType
    TypeInteger
    tokenTypeOf TkBytes{}       = TokenType
    TypeBytes
    tokenTypeOf TkBytesBegin{}  = TokenType
    TypeBytesIndef
    tokenTypeOf TkString{}      = TokenType
    TypeString
    tokenTypeOf TkStringBegin{} = TokenType
    TypeStringIndef
    tokenTypeOf TkListLen{}     = TokenType
    TypeListLen
    tokenTypeOf TkListBegin{}   = TokenType
    TypeListLenIndef
    tokenTypeOf TkMapLen{}      = TokenType
    TypeMapLen
    tokenTypeOf TkMapBegin{}    = TokenType
    TypeMapLenIndef
    tokenTypeOf TkTag{}         = TokenType
    TypeTag
    tokenTypeOf TkBool{}        = TokenType
    TypeBool
    tokenTypeOf TkNull          = TokenType
    TypeNull
    tokenTypeOf TkBreak         = TokenType
    TypeBreak
    tokenTypeOf TkSimple{}      = TokenType
    TypeSimple
    tokenTypeOf TkFloat16{}     = TokenType
    TypeFloat16
    tokenTypeOf TkFloat32{}     = TokenType
    TypeFloat32
    tokenTypeOf TkFloat64{}     = TokenType
    TypeFloat64

    --------------------------------------------------------------------------------

    -- | Ensure a 'FlatTerm' is internally consistent and was created in a valid
    -- manner.
    --
    -- @since 0.2.0.0
    validFlatTerm :: FlatTerm -- ^ The input 'FlatTerm'
                  -> Bool     -- ^ 'True' if valid, 'False' otherwise.
    validFlatTerm :: [TermToken] -> Bool
    validFlatTerm ts :: [TermToken]
    ts =
       (String -> Bool) -> (() -> Bool) -> Either String () -> Bool
    forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
    either (Bool -> String -> Bool
    forall a b. a -> b -> a
    const Bool
    False) (Bool -> () -> Bool
    forall a b. a -> b -> a
    const Bool
    True) (Either String () -> Bool) -> Either String () -> Bool
    forall a b. (a -> b) -> a -> b
    $ do
         [TermToken]
    ts' <- Loc -> [TermToken] -> Either String [TermToken]
    validateTerm Loc
    TopLevelSingle [TermToken]
    ts
         case [TermToken]
    ts' of
           [] -> () -> Either String ()
    forall (m :: * -> *) a. Monad m => a -> m a
    return ()
           _  -> String -> Either String ()
    forall a b. a -> Either a b
    Left "trailing data"

    -- | A data type used for tracking the position we're at
    -- as we traverse a 'FlatTerm' and make sure it's valid.
    data Loc = TopLevelSingle
             | TopLevelSequence
             | InString   Int     Loc
             | InBytes    Int     Loc
             | InListN    Int Int Loc
             | InList     Int     Loc
             | InMapNKey  Int Int Loc
             | InMapNVal  Int Int Loc
             | InMapKey   Int     Loc
             | InMapVal   Int     Loc
             | InTagged   Word64  Loc
      deriving Int -> Loc -> ShowS
    [Loc] -> ShowS
    Loc -> String
    (Int -> Loc -> ShowS)
    -> (Loc -> String) -> ([Loc] -> ShowS) -> Show Loc
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    showList :: [Loc] -> ShowS
    $cshowList :: [Loc] -> ShowS
    show :: Loc -> String
    $cshow :: Loc -> String
    showsPrec :: Int -> Loc -> ShowS
    $cshowsPrec :: Int -> Loc -> ShowS
    Show

    -- | Validate an arbitrary 'FlatTerm' at an arbitrary location.
    validateTerm :: Loc -> FlatTerm -> Either String FlatTerm
    validateTerm :: Loc -> [TermToken] -> Either String [TermToken]
    validateTerm _loc :: Loc
    _loc (TkInt       _   : ts :: [TermToken]
    ts) = [TermToken] -> Either String [TermToken]
    forall (m :: * -> *) a. Monad m => a -> m a
    return [TermToken]
    ts
    validateTerm _loc :: Loc
    _loc (TkInteger   _   : ts :: [TermToken]
    ts) = [TermToken] -> Either String [TermToken]
    forall (m :: * -> *) a. Monad m => a -> m a
    return [TermToken]
    ts
    validateTerm _loc :: Loc
    _loc (TkBytes     _   : ts :: [TermToken]
    ts) = [TermToken] -> Either String [TermToken]
    forall (m :: * -> *) a. Monad m => a -> m a
    return [TermToken]
    ts
    validateTerm  loc :: Loc
    loc (TkBytesBegin    : ts :: [TermToken]
    ts) = Loc -> Int -> [TermToken] -> Either String [TermToken]
    validateBytes Loc
    loc 0 [TermToken]
    ts
    validateTerm _loc :: Loc
    _loc (TkString    _   : ts :: [TermToken]
    ts) = [TermToken] -> Either String [TermToken]
    forall (m :: * -> *) a. Monad m => a -> m a
    return [TermToken]
    ts
    validateTerm  loc :: Loc
    loc (TkStringBegin   : ts :: [TermToken]
    ts) = Loc -> Int -> [TermToken] -> Either String [TermToken]
    validateString Loc
    loc 0 [TermToken]
    ts
    validateTerm  loc :: Loc
    loc (TkListLen   len :: Word
    len : ts :: [TermToken]
    ts)
        | Word
    len Word -> Word -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Word
    forall n. Num n => n
    maxInt                      = Loc -> Int -> Int -> [TermToken] -> Either String [TermToken]
    validateListN Loc
    loc 0 (Word -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word
    len) [TermToken]
    ts
        | Bool
    otherwise                          = String -> Either String [TermToken]
    forall a b. a -> Either a b
    Left "list len too long (> max int)"
    validateTerm  loc :: Loc
    loc (TkListBegin     : ts :: [TermToken]
    ts) = Loc -> Int -> [TermToken] -> Either String [TermToken]
    validateList  Loc
    loc 0     [TermToken]
    ts
    validateTerm  loc :: Loc
    loc (TkMapLen    len :: Word
    len : ts :: [TermToken]
    ts)
        | Word
    len Word -> Word -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Word
    forall n. Num n => n
    maxInt                      = Loc -> Int -> Int -> [TermToken] -> Either String [TermToken]
    validateMapN  Loc
    loc 0 (Word -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Word
    len) [TermToken]
    ts
        | Bool
    otherwise                          = String -> Either String [TermToken]
    forall a b. a -> Either a b
    Left "map len too long (> max int)"
    validateTerm  loc :: Loc
    loc (TkMapBegin      : ts :: [TermToken]
    ts) = Loc -> Int -> [TermToken] -> Either String [TermToken]
    validateMap   Loc
    loc 0     [TermToken]
    ts
    validateTerm  loc :: Loc
    loc (TkTag       w :: Word64
    w   : ts :: [TermToken]
    ts) = Loc -> [TermToken] -> Either String [TermToken]
    validateTerm  (Word64 -> Loc -> Loc
    InTagged Word64
    w Loc
    loc) [TermToken]
    ts
    validateTerm _loc :: Loc
    _loc (TkBool      _   : ts :: [TermToken]
    ts) = [TermToken] -> Either String [TermToken]
    forall (m :: * -> *) a. Monad m => a -> m a
    return [TermToken]
    ts
    validateTerm _loc :: Loc
    _loc (TkNull          : ts :: [TermToken]
    ts) = [TermToken] -> Either String [TermToken]
    forall (m :: * -> *) a. Monad m => a -> m a
    return [TermToken]
    ts
    validateTerm  loc :: Loc
    loc (TkBreak         : _)  = TermToken -> Loc -> Either String [TermToken]
    forall a. TermToken -> Loc -> Either String a
    unexpectedToken TermToken
    TkBreak Loc
    loc
    validateTerm _loc :: Loc
    _loc (TkSimple  _     : ts :: [TermToken]
    ts) = [TermToken] -> Either String [TermToken]
    forall (m :: * -> *) a. Monad m => a -> m a
    return [TermToken]
    ts
    validateTerm _loc :: Loc
    _loc (TkFloat16 _     : ts :: [TermToken]
    ts) = [TermToken] -> Either String [TermToken]
    forall (m :: * -> *) a. Monad m => a -> m a
    return [TermToken]
    ts
    validateTerm _loc :: Loc
    _loc (TkFloat32 _     : ts :: [TermToken]
    ts) = [TermToken] -> Either String [TermToken]
    forall (m :: * -> *) a. Monad m => a -> m a
    return [TermToken]
    ts
    validateTerm _loc :: Loc
    _loc (TkFloat64 _     : ts :: [TermToken]
    ts) = [TermToken] -> Either String [TermToken]
    forall (m :: * -> *) a. Monad m => a -> m a
    return [TermToken]
    ts
    validateTerm  loc :: Loc
    loc                    []  = Loc -> Either String [TermToken]
    forall a. Loc -> Either String a
    unexpectedEof Loc
    loc

    unexpectedToken :: TermToken -> Loc -> Either String a
    unexpectedToken :: TermToken -> Loc -> Either String a
    unexpectedToken tok :: TermToken
    tok loc :: Loc
    loc = String -> Either String a
    forall a b. a -> Either a b
    Left (String -> Either String a) -> String -> Either String a
    forall a b. (a -> b) -> a -> b
    $ "unexpected token " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ TermToken -> String
    forall a. Show a => a -> String
    show TermToken
    tok
                                  String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ ", in context " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ Loc -> String
    forall a. Show a => a -> String
    show Loc
    loc

    unexpectedEof :: Loc -> Either String a
    unexpectedEof :: Loc -> Either String a
    unexpectedEof loc :: Loc
    loc = String -> Either String a
    forall a b. a -> Either a b
    Left (String -> Either String a) -> String -> Either String a
    forall a b. (a -> b) -> a -> b
    $ "unexpected end of input in context " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ Loc -> String
    forall a. Show a => a -> String
    show Loc
    loc

    validateBytes :: Loc -> Int -> [TermToken] -> Either String [TermToken]
    validateBytes :: Loc -> Int -> [TermToken] -> Either String [TermToken]
    validateBytes _    _ (TkBreak   : ts :: [TermToken]
    ts) = [TermToken] -> Either String [TermToken]
    forall (m :: * -> *) a. Monad m => a -> m a
    return [TermToken]
    ts
    validateBytes ploc :: Loc
    ploc i :: Int
    i (TkBytes _ : ts :: [TermToken]
    ts) = Loc -> Int -> [TermToken] -> Either String [TermToken]
    validateBytes Loc
    ploc (Int
    iInt -> Int -> Int
    forall a. Num a => a -> a -> a
    +1) [TermToken]
    ts
    validateBytes ploc :: Loc
    ploc i :: Int
    i (tok :: TermToken
    tok       : _)  = TermToken -> Loc -> Either String [TermToken]
    forall a. TermToken -> Loc -> Either String a
    unexpectedToken TermToken
    tok (Int -> Loc -> Loc
    InBytes Int
    i Loc
    ploc)
    validateBytes ploc :: Loc
    ploc i :: Int
    i []               = Loc -> Either String [TermToken]
    forall a. Loc -> Either String a
    unexpectedEof       (Int -> Loc -> Loc
    InBytes Int
    i Loc
    ploc)

    validateString :: Loc -> Int -> [TermToken] -> Either String [TermToken]
    validateString :: Loc -> Int -> [TermToken] -> Either String [TermToken]
    validateString _    _ (TkBreak    : ts :: [TermToken]
    ts) = [TermToken] -> Either String [TermToken]
    forall (m :: * -> *) a. Monad m => a -> m a
    return [TermToken]
    ts
    validateString ploc :: Loc
    ploc i :: Int
    i (TkString _ : ts :: [TermToken]
    ts) = Loc -> Int -> [TermToken] -> Either String [TermToken]
    validateString Loc
    ploc (Int
    iInt -> Int -> Int
    forall a. Num a => a -> a -> a
    +1) [TermToken]
    ts
    validateString ploc :: Loc
    ploc i :: Int
    i (tok :: TermToken
    tok        : _)  = TermToken -> Loc -> Either String [TermToken]
    forall a. TermToken -> Loc -> Either String a
    unexpectedToken TermToken
    tok (Int -> Loc -> Loc
    InString Int
    i Loc
    ploc)
    validateString ploc :: Loc
    ploc i :: Int
    i []                = Loc -> Either String [TermToken]
    forall a. Loc -> Either String a
    unexpectedEof       (Int -> Loc -> Loc
    InString Int
    i Loc
    ploc)

    validateListN :: Loc -> Int -> Int -> [TermToken] -> Either String [TermToken]
    validateListN :: Loc -> Int -> Int -> [TermToken] -> Either String [TermToken]
    validateListN    _ i :: Int
    i len :: Int
    len ts :: [TermToken]
    ts | Int
    i Int -> Int -> Bool
    forall a. Eq a => a -> a -> Bool
    == Int
    len = [TermToken] -> Either String [TermToken]
    forall (m :: * -> *) a. Monad m => a -> m a
    return [TermToken]
    ts
    validateListN ploc :: Loc
    ploc i :: Int
    i len :: Int
    len ts :: [TermToken]
    ts = do
        [TermToken]
    ts' <- Loc -> [TermToken] -> Either String [TermToken]
    validateTerm (Int -> Int -> Loc -> Loc
    InListN Int
    i Int
    len Loc
    ploc) [TermToken]
    ts
        Loc -> Int -> Int -> [TermToken] -> Either String [TermToken]
    validateListN Loc
    ploc (Int
    iInt -> Int -> Int
    forall a. Num a => a -> a -> a
    +1) Int
    len [TermToken]
    ts'

    validateList :: Loc -> Int -> [TermToken] -> Either String [TermToken]
    validateList :: Loc -> Int -> [TermToken] -> Either String [TermToken]
    validateList _    _ (TkBreak : ts :: [TermToken]
    ts) = [TermToken] -> Either String [TermToken]
    forall (m :: * -> *) a. Monad m => a -> m a
    return [TermToken]
    ts
    validateList ploc :: Loc
    ploc i :: Int
    i ts :: [TermToken]
    ts = do
        [TermToken]
    ts' <- Loc -> [TermToken] -> Either String [TermToken]
    validateTerm (Int -> Loc -> Loc
    InList Int
    i Loc
    ploc) [TermToken]
    ts
        Loc -> Int -> [TermToken] -> Either String [TermToken]
    validateList Loc
    ploc (Int
    iInt -> Int -> Int
    forall a. Num a => a -> a -> a
    +1) [TermToken]
    ts'

    validateMapN :: Loc -> Int -> Int -> [TermToken] -> Either String [TermToken]
    validateMapN :: Loc -> Int -> Int -> [TermToken] -> Either String [TermToken]
    validateMapN    _ i :: Int
    i len :: Int
    len ts :: [TermToken]
    ts  | Int
    i Int -> Int -> Bool
    forall a. Eq a => a -> a -> Bool
    == Int
    len = [TermToken] -> Either String [TermToken]
    forall (m :: * -> *) a. Monad m => a -> m a
    return [TermToken]
    ts
    validateMapN ploc :: Loc
    ploc i :: Int
    i len :: Int
    len ts :: [TermToken]
    ts  = do
        [TermToken]
    ts'  <- Loc -> [TermToken] -> Either String [TermToken]
    validateTerm (Int -> Int -> Loc -> Loc
    InMapNKey Int
    i Int
    len Loc
    ploc) [TermToken]
    ts
        [TermToken]
    ts'' <- Loc -> [TermToken] -> Either String [TermToken]
    validateTerm (Int -> Int -> Loc -> Loc
    InMapNVal Int
    i Int
    len Loc
    ploc) [TermToken]
    ts'
        Loc -> Int -> Int -> [TermToken] -> Either String [TermToken]
    validateMapN Loc
    ploc (Int
    iInt -> Int -> Int
    forall a. Num a => a -> a -> a
    +1) Int
    len [TermToken]
    ts''

    validateMap :: Loc -> Int -> [TermToken] -> Either String [TermToken]
    validateMap :: Loc -> Int -> [TermToken] -> Either String [TermToken]
    validateMap _    _ (TkBreak : ts :: [TermToken]
    ts) = [TermToken] -> Either String [TermToken]
    forall (m :: * -> *) a. Monad m => a -> m a
    return [TermToken]
    ts
    validateMap ploc :: Loc
    ploc i :: Int
    i ts :: [TermToken]
    ts = do
        [TermToken]
    ts'  <- Loc -> [TermToken] -> Either String [TermToken]
    validateTerm (Int -> Loc -> Loc
    InMapKey Int
    i Loc
    ploc) [TermToken]
    ts
        [TermToken]
    ts'' <- Loc -> [TermToken] -> Either String [TermToken]
    validateTerm (Int -> Loc -> Loc
    InMapVal Int
    i Loc
    ploc) [TermToken]
    ts'
        Loc -> Int -> [TermToken] -> Either String [TermToken]
    validateMap Loc
    ploc (Int
    iInt -> Int -> Int
    forall a. Num a => a -> a -> a
    +1) [TermToken]
    ts''

    --------------------------------------------------------------------------------
    -- Utilities

    maxInt, minInt, maxWord :: Num n => n
    maxInt :: n
    maxInt    = Int -> n
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int
    forall a. Bounded a => a
    maxBound :: Int)
    minInt :: n
    minInt    = Int -> n
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int
    forall a. Bounded a => a
    minBound :: Int)
    maxWord :: n
    maxWord   = Word -> n
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Word
    forall a. Bounded a => a
    maxBound :: Word)

    maxInt8, minInt8, maxWord8 :: Num n => n
    maxInt8 :: n
    maxInt8    = Int8 -> n
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int8
    forall a. Bounded a => a
    maxBound :: Int8)
    minInt8 :: n
    minInt8    = Int8 -> n
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int8
    forall a. Bounded a => a
    minBound :: Int8)
    maxWord8 :: n
    maxWord8   = Word8 -> n
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Word8
    forall a. Bounded a => a
    maxBound :: Word8)

    maxInt16, minInt16, maxWord16 :: Num n => n
    maxInt16 :: n
    maxInt16    = Int16 -> n
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int16
    forall a. Bounded a => a
    maxBound :: Int16)
    minInt16 :: n
    minInt16    = Int16 -> n
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int16
    forall a. Bounded a => a
    minBound :: Int16)
    maxWord16 :: n
    maxWord16   = Word16 -> n
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Word16
    forall a. Bounded a => a
    maxBound :: Word16)

    maxInt32, minInt32, maxWord32 :: Num n => n
    maxInt32 :: n
    maxInt32    = Int32 -> n
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int32
    forall a. Bounded a => a
    maxBound :: Int32)
    minInt32 :: n
    minInt32    = Int32 -> n
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Int32
    forall a. Bounded a => a
    minBound :: Int32)
    maxWord32 :: n
    maxWord32   = Word32 -> n
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Word32
    forall a. Bounded a => a
    maxBound :: Word32)

    -- | Do a careful check to ensure an 'Int' is in the
    -- range of a 'Word32'.
    intIsValidWord32 :: Int -> Bool
    intIsValidWord32 :: Int -> Bool
    intIsValidWord32 n :: Int
    n = Bool
    b1 Bool -> Bool -> Bool
    && Bool
    b2
      where
        -- NOTE: this first comparison must use Int for
        -- the check, not Word32, in case a negative value
        -- is given. Otherwise this check would fail due to
        -- overflow.
        b1 :: Bool
    b1 = Int
    n Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= 0
        -- NOTE: we must convert n to Word32, otherwise,
        -- maxWord32 is inferred as Int, and because
        -- the maxBound of Word32 is greater than Int,
        -- it overflows and this check fails.
        b2 :: Bool
    b2 = (Int -> Word32
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    n :: Word32) Word32 -> Word32 -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Word32
    forall n. Num n => n
    maxWord32

    unI# :: Int -> Int#
    unI# :: Int -> Int#
    unI#   (I#   i# :: Int#
    i#) = Int#
    i#

    unW# :: Word -> Word#
    unW# :: Word -> Word#
    unW#   (W#  w# :: Word#
    w#) = Word#
    w#

    unW8# :: Word8 -> Word#
    unW8# :: Word8 -> Word#
    unW8#  (W8# w# :: Word#
    w#) = Word#
    w#

    unF# :: Float -> Float#
    unF# :: Float -> Float#
    unF#   (F#   f# :: Float#
    f#) = Float#
    f#

    unD# :: Double -> Double#
    unD# :: Double -> Double#
    unD#   (D#   f# :: Double#
    f#) = Double#
    f#

    #if defined(ARCH_32bit)
    unW64# :: Word64 -> Word64#
    unW64# (W64# w#) = w#

    unI64# :: Int64 -> Int64#
    unI64# (I64# i#) = i#
    #endif