{-# LANGUAGE UnicodeSyntax #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}

-- | Parsers for integral numbers written in positional numeral systems.
module Data.Textual.Integral
  (
  -- * Positional numeral systems
    PositionalSystem(..)
  , BitSystem(..)
  , Binary(..)
  , Octal(..)
  , Decimal(..)
  , Hexadecimal(..)
  , LowHex(..)
  , UpHex(..)
  -- * Single digits
  , digitIn
  , nzDigitIn
  , binDigit
  , nzBinDigit
  , octDigit
  , nzOctDigit
  , decDigit
  , nzDecDigit
  , hexDigit
  , nzHexDigit
  , lowHexDigit
  , nzLowHexDigit
  , upHexDigit
  , nzUpHexDigit
  -- * Numbers
  , nonNegative
  , nnCompact
  , nnUpTo
  , nncUpTo
  , nnBounded
  , nncBounded
  , nnBits
  , nncBits
  , nnBitsUpTo
  , nncBitsUpTo
  , nnbBits
  , nncbBits
  , nonPositive
  , npCompact
  , npUpTo
  , npcUpTo
  , npBounded
  , npcBounded
  , npBits
  , npcBits
  , npBitsUpTo
  , npcBitsUpTo
  , npbBits
  , npcbBits
  , Sign(..)
  , applySign
  , optMinus
  , optSign
  , number'
  , number
  , compact'
  , compact
  , numberUpTo'
  , numberUpTo
  , compactUpTo'
  , compactUpTo
  , bounded'
  , bounded
  , cBounded'
  , cBounded
  , bits'
  , bits
  , cBits'
  , cBits
  , bitsUpTo'
  , bitsUpTo
  , cBitsUpTo'
  , cBitsUpTo
  , bBits'
  , bBits
  , cbBits'
  , cbBits
  ) where

import Data.Typeable (Typeable)
import Data.Int
import Data.Word
import Data.Bits (Bits(..))
import Control.Applicative
import Text.Printer.Integral (
         PositionalSystem(..), BitSystem(..),
         Binary(..), Octal(..), Decimal(..), Hexadecimal(..),
         LowHex(..), UpHex(..))
import Text.Parser.Combinators ((<?>))
import qualified Text.Parser.Combinators as PC
import Text.Parser.Char (CharParsing)
import qualified Text.Parser.Char as PC

-- | Parse a digit of the specified positional numeral system.
digitIn  (PositionalSystem s, Num α, CharParsing μ)  s  μ α
digitIn :: forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s  =  s -> Char -> α
forall α. Num α => s -> Char -> α
forall s α. (PositionalSystem s, Num α) => s -> Char -> α
unsafeFromDigitIn s
s (Char -> α) -> μ Char -> μ α
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Char -> Bool) -> μ Char
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m Char
PC.satisfy (s -> Char -> Bool
forall s. PositionalSystem s => s -> Char -> Bool
isDigitIn s
s)
          μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digit"
{-# INLINE digitIn #-}

-- | Parse a non-zero digit of the specified positional numeral system.
nzDigitIn  (PositionalSystem s, Num α, CharParsing μ)  s  μ α
nzDigitIn :: forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
nzDigitIn s
s  =  s -> Char -> α
forall α. Num α => s -> Char -> α
forall s α. (PositionalSystem s, Num α) => s -> Char -> α
unsafeFromDigitIn s
s (Char -> α) -> μ Char -> μ α
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Char -> Bool) -> μ Char
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m Char
PC.satisfy (s -> Char -> Bool
forall s. PositionalSystem s => s -> Char -> Bool
isNzDigitIn s
s)
            μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> String
"non-zero " String -> String -> String
forall a. [a] -> [a] -> [a]
++ s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digit"
{-# INLINE nzDigitIn #-}

-- | Parse a binary digit.
binDigit  (Num α, CharParsing μ)  μ α
binDigit :: forall α (μ :: * -> *). (Num α, CharParsing μ) => μ α
binDigit = Binary -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn Binary
Binary
{-# INLINE binDigit #-}

-- | Parse a non-zero binary digit (/'1'/).
nzBinDigit  (Num α, CharParsing μ)  μ α
nzBinDigit :: forall α (μ :: * -> *). (Num α, CharParsing μ) => μ α
nzBinDigit = Binary -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
nzDigitIn Binary
Binary
{-# INLINE nzBinDigit #-}

-- | Parse a decimal digit.
decDigit  (Num α, CharParsing μ)  μ α
decDigit :: forall α (μ :: * -> *). (Num α, CharParsing μ) => μ α
decDigit = Decimal -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn Decimal
Decimal
{-# INLINE decDigit #-}

-- | Parse a non-zero decimal digit.
nzDecDigit  (Num α, CharParsing μ)  μ α
nzDecDigit :: forall α (μ :: * -> *). (Num α, CharParsing μ) => μ α
nzDecDigit = Decimal -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
nzDigitIn Decimal
Decimal
{-# INLINE nzDecDigit #-}

-- | Parse an octal digit.
octDigit  (Num α, CharParsing μ)  μ α
octDigit :: forall α (μ :: * -> *). (Num α, CharParsing μ) => μ α
octDigit = Octal -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn Octal
Octal
{-# INLINE octDigit #-}

-- | Parse a non-zero octal digit.
nzOctDigit  (Num α, CharParsing μ)  μ α
nzOctDigit :: forall α (μ :: * -> *). (Num α, CharParsing μ) => μ α
nzOctDigit = Octal -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
nzDigitIn Octal
Octal
{-# INLINE nzOctDigit #-}

-- | Parse a hexadecimal digit.
hexDigit  (Num α, CharParsing μ)  μ α
hexDigit :: forall α (μ :: * -> *). (Num α, CharParsing μ) => μ α
hexDigit = Hexadecimal -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn Hexadecimal
Hexadecimal
{-# INLINE hexDigit #-}

-- | Parse a non-zero hexadecimal digit.
nzHexDigit  (Num α, CharParsing μ)  μ α
nzHexDigit :: forall α (μ :: * -> *). (Num α, CharParsing μ) => μ α
nzHexDigit = Hexadecimal -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
nzDigitIn Hexadecimal
Hexadecimal
{-# INLINE nzHexDigit #-}

-- | Parse a lower case hexadecimal digit.
lowHexDigit  (Num α, CharParsing μ)  μ α
lowHexDigit :: forall α (μ :: * -> *). (Num α, CharParsing μ) => μ α
lowHexDigit = LowHex -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn LowHex
LowHex
{-# INLINE lowHexDigit #-}

-- | Parse a non-zero lower case hexadecimal digit.
nzLowHexDigit  (Num α, CharParsing μ)  μ α
nzLowHexDigit :: forall α (μ :: * -> *). (Num α, CharParsing μ) => μ α
nzLowHexDigit = LowHex -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
nzDigitIn LowHex
LowHex
{-# INLINE nzLowHexDigit #-}

-- | Parse an upper case hexadecimal digit.
upHexDigit  (Num α, CharParsing μ)  μ α
upHexDigit :: forall α (μ :: * -> *). (Num α, CharParsing μ) => μ α
upHexDigit = UpHex -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn UpHex
UpHex
{-# INLINE upHexDigit #-}

-- | Parse a non-zero upper case hexadecimal digit.
nzUpHexDigit  (Num α, CharParsing μ)  μ α
nzUpHexDigit :: forall α (μ :: * -> *). (Num α, CharParsing μ) => μ α
nzUpHexDigit = UpHex -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
nzDigitIn UpHex
UpHex
{-# INLINE nzUpHexDigit #-}

-- | Parse a non-negative number written in the specified positional
--   numeral system.
nonNegative  (PositionalSystem s, Num α, Monad μ, CharParsing μ)  s  μ α
nonNegative :: forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
s -> μ α
nonNegative s
s = μ α
digit μ α -> (α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= α -> μ α
go μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits"
  where go :: α -> μ α
go !α
r = μ α -> μ (Maybe α)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ α
digit μ (Maybe α) -> (Maybe α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                  Just α
d   α -> μ α
go (α
r α -> α -> α
forall a. Num a => a -> a -> a
* α
radix α -> α -> α
forall a. Num a => a -> a -> a
+ α
d)
                  Maybe α
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
r
        radix :: α
radix = s -> α
forall α. Num α => s -> α
forall s α. (PositionalSystem s, Num α) => s -> α
radixIn s
s
        digit :: μ α
digit = s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE nonNegative  (Monad μ, CharParsing μ)  Decimal  μ Int #-}
{-# SPECIALIZE nonNegative  (Monad μ, CharParsing μ)  Decimal  μ Int8 #-}
{-# SPECIALIZE nonNegative  (Monad μ, CharParsing μ)  Decimal  μ Int16 #-}
{-# SPECIALIZE nonNegative  (Monad μ, CharParsing μ)  Decimal  μ Int32 #-}
{-# SPECIALIZE nonNegative  (Monad μ, CharParsing μ)  Decimal  μ Int64 #-}
{-# SPECIALIZE nonNegative  (Monad μ, CharParsing μ)  Decimal  μ Word #-}
{-# SPECIALIZE nonNegative  (Monad μ, CharParsing μ)  Decimal  μ Word8 #-}
{-# SPECIALIZE nonNegative  (Monad μ, CharParsing μ)  Decimal  μ Word16 #-}
{-# SPECIALIZE nonNegative  (Monad μ, CharParsing μ)  Decimal  μ Word32 #-}
{-# SPECIALIZE nonNegative  (Monad μ, CharParsing μ)  Decimal  μ Word64 #-}
{-# SPECIALIZE nonNegative  (Num α, Monad μ, CharParsing μ)  Binary  μ α #-}
{-# SPECIALIZE nonNegative  (Num α, Monad μ, CharParsing μ)  Octal  μ α #-}
{-# SPECIALIZE nonNegative  (Num α, Monad μ, CharParsing μ)  Decimal  μ α #-}
{-# SPECIALIZE nonNegative  (Num α, Monad μ, CharParsing μ)  Hexadecimal  μ α #-}
{-# SPECIALIZE nonNegative  (Num α, Monad μ, CharParsing μ)  LowHex  μ α #-}
{-# SPECIALIZE nonNegative  (Num α, Monad μ, CharParsing μ)  UpHex  μ α #-}

-- | Parse a non-negative number written in the specified positional
--   numeral system. Leading zeroes are not allowed.
nnCompact  (PositionalSystem s, Num α, Monad μ, CharParsing μ)  s  μ α
nnCompact :: forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
s -> μ α
nnCompact s
s = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits") (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ s -> μ Word
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s μ Word -> (Word -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                Word
0  μ Char -> μ (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ((Char -> Bool) -> μ Char
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m Char
PC.satisfy ((Char -> Bool) -> μ Char) -> (Char -> Bool) -> μ Char
forall a b. (a -> b) -> a -> b
$ s -> Char -> Bool
forall s. PositionalSystem s => s -> Char -> Bool
isDigitIn s
s) μ (Maybe Char) -> (Maybe Char -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                      Just Char
_   String -> μ α
forall a. String -> μ a
forall (m :: * -> *) a. Parsing m => String -> m a
PC.unexpected String
"leading zero"
                      Maybe Char
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
0
                Word
r  α -> μ α
go (α -> μ α) -> α -> μ α
forall a b. (a -> b) -> a -> b
$ Word -> α
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word
r  Word)
  where go :: α -> μ α
go !α
r = μ α -> μ (Maybe α)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ α
digit μ (Maybe α) -> (Maybe α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                  Just α
d   α -> μ α
go (α
r α -> α -> α
forall a. Num a => a -> a -> a
* α
radix α -> α -> α
forall a. Num a => a -> a -> a
+ α
d)
                  Maybe α
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
r
        radix :: α
radix = s -> α
forall α. Num α => s -> α
forall s α. (PositionalSystem s, Num α) => s -> α
radixIn s
s
        digit :: μ α
digit = s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE nnCompact  (Monad μ, CharParsing μ)  Decimal  μ Int #-}
{-# SPECIALIZE nnCompact  (Monad μ, CharParsing μ)  Decimal  μ Int8 #-}
{-# SPECIALIZE nnCompact  (Monad μ, CharParsing μ)  Decimal  μ Int16 #-}
{-# SPECIALIZE nnCompact  (Monad μ, CharParsing μ)  Decimal  μ Int32 #-}
{-# SPECIALIZE nnCompact  (Monad μ, CharParsing μ)  Decimal  μ Int64 #-}
{-# SPECIALIZE nnCompact  (Monad μ, CharParsing μ)  Decimal  μ Word #-}
{-# SPECIALIZE nnCompact  (Monad μ, CharParsing μ)  Decimal  μ Word8 #-}
{-# SPECIALIZE nnCompact  (Monad μ, CharParsing μ)  Decimal  μ Word16 #-}
{-# SPECIALIZE nnCompact  (Monad μ, CharParsing μ)  Decimal  μ Word32 #-}
{-# SPECIALIZE nnCompact  (Monad μ, CharParsing μ)  Decimal  μ Word64 #-}
{-# SPECIALIZE nnCompact  (Num α, Monad μ, CharParsing μ)  Binary  μ α #-}
{-# SPECIALIZE nnCompact  (Num α, Monad μ, CharParsing μ)  Octal  μ α #-}
{-# SPECIALIZE nnCompact  (Num α, Monad μ, CharParsing μ)  Decimal  μ α #-}
{-# SPECIALIZE nnCompact  (Num α, Monad μ, CharParsing μ)  Hexadecimal  μ α #-}
{-# SPECIALIZE nnCompact  (Num α, Monad μ, CharParsing μ)  LowHex  μ α #-}
{-# SPECIALIZE nnCompact  (Num α, Monad μ, CharParsing μ)  UpHex  μ α #-}

moreThan  CharParsing μ  Int  μ α
moreThan :: forall (μ :: * -> *) α. CharParsing μ => Int -> μ α
moreThan Int
n = String -> μ α
forall a. String -> μ a
forall (m :: * -> *) a. Parsing m => String -> m a
PC.unexpected
           (String -> μ α) -> String -> μ α
forall a b. (a -> b) -> a -> b
$ String
"more than " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
n String -> String -> String
forall a. [a] -> [a] -> [a]
++
             case Int
n of
               Int
1  String
" digit"
               Int
_  String
" digits"
{-# INLINE moreThan #-}

-- | Parse a non-negative number written in the specified positional
--   numeral system (up to /n/ digits).
nnUpTo  (PositionalSystem s, Num α, Monad μ, CharParsing μ)  s  Int  μ α
nnUpTo :: forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
s -> Int -> μ α
nnUpTo s
_ Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = μ α
forall a. μ a
forall (f :: * -> *) a. Alternative f => f a
empty
nnUpTo s
s Int
n = μ α
digit μ α -> (α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> α -> μ α
forall {t}. (Eq t, Num t) => t -> α -> μ α
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits"
  where go :: t -> α -> μ α
go t
0 !α
r = μ Char -> μ (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ((Char -> Bool) -> μ Char
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m Char
PC.satisfy ((Char -> Bool) -> μ Char) -> (Char -> Bool) -> μ Char
forall a b. (a -> b) -> a -> b
$ s -> Char -> Bool
forall s. PositionalSystem s => s -> Char -> Bool
isDigitIn s
s) μ (Maybe Char) -> (Maybe Char -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                    Just Char
_   Int -> μ α
forall (μ :: * -> *) α. CharParsing μ => Int -> μ α
moreThan Int
n
                    Maybe Char
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
r
        go t
l !α
r = μ α -> μ (Maybe α)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ α
digit μ (Maybe α) -> (Maybe α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                    Just α
d   t -> α -> μ α
go (t
l t -> t -> t
forall a. Num a => a -> a -> a
- t
1) (α
r α -> α -> α
forall a. Num a => a -> a -> a
* α
radix α -> α -> α
forall a. Num a => a -> a -> a
+ α
d)
                    Maybe α
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
r
        radix :: α
radix   = s -> α
forall α. Num α => s -> α
forall s α. (PositionalSystem s, Num α) => s -> α
radixIn s
s
        digit :: μ α
digit   = s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE nnUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Int #-}
{-# SPECIALIZE nnUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Int8 #-}
{-# SPECIALIZE nnUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Int16 #-}
{-# SPECIALIZE nnUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Int32 #-}
{-# SPECIALIZE nnUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Int64 #-}
{-# SPECIALIZE nnUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Word #-}
{-# SPECIALIZE nnUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Word8 #-}
{-# SPECIALIZE nnUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Word16 #-}
{-# SPECIALIZE nnUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Word32 #-}
{-# SPECIALIZE nnUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Word64 #-}
{-# SPECIALIZE nnUpTo  (Num α, Monad μ, CharParsing μ)  Binary  Int  μ α #-}
{-# SPECIALIZE nnUpTo  (Num α, Monad μ, CharParsing μ)  Octal  Int  μ α #-}
{-# SPECIALIZE nnUpTo  (Num α, Monad μ, CharParsing μ)  Decimal  Int  μ α #-}
{-# SPECIALIZE nnUpTo  (Num α, Monad μ, CharParsing μ)  Hexadecimal  Int  μ α #-}
{-# SPECIALIZE nnUpTo  (Num α, Monad μ, CharParsing μ)  LowHex  Int  μ α #-}
{-# SPECIALIZE nnUpTo  (Num α, Monad μ, CharParsing μ)  UpHex  Int  μ α #-}

-- | Parse a non-negative number written in the specified positional
--   numeral system (up to /n/ digits). Leading zeroes are not allowed.
nncUpTo  (PositionalSystem s, Num α, Monad μ, CharParsing μ)  s  Int  μ α
nncUpTo :: forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
s -> Int -> μ α
nncUpTo s
_ Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = μ α
forall a. μ a
forall (f :: * -> *) a. Alternative f => f a
empty
nncUpTo s
s Int
n = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits") (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ s -> μ Word
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s μ Word -> (Word -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                Word
0  μ Char -> μ (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ((Char -> Bool) -> μ Char
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m Char
PC.satisfy ((Char -> Bool) -> μ Char) -> (Char -> Bool) -> μ Char
forall a b. (a -> b) -> a -> b
$ s -> Char -> Bool
forall s. PositionalSystem s => s -> Char -> Bool
isDigitIn s
s) μ (Maybe Char) -> (Maybe Char -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                      Just Char
_   String -> μ α
forall a. String -> μ a
forall (m :: * -> *) a. Parsing m => String -> m a
PC.unexpected String
"leading zero"
                      Maybe Char
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
0
                Word
r  Int -> α -> μ α
forall {t}. (Eq t, Num t) => t -> α -> μ α
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (α -> μ α) -> α -> μ α
forall a b. (a -> b) -> a -> b
$ Word -> α
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word
r  Word)
  where go :: t -> α -> μ α
go t
0 !α
r = μ Char -> μ (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ((Char -> Bool) -> μ Char
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m Char
PC.satisfy ((Char -> Bool) -> μ Char) -> (Char -> Bool) -> μ Char
forall a b. (a -> b) -> a -> b
$ s -> Char -> Bool
forall s. PositionalSystem s => s -> Char -> Bool
isDigitIn s
s) μ (Maybe Char) -> (Maybe Char -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                    Just Char
_   Int -> μ α
forall (μ :: * -> *) α. CharParsing μ => Int -> μ α
moreThan Int
n
                    Maybe Char
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
r
        go t
l !α
r = μ α -> μ (Maybe α)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ α
digit μ (Maybe α) -> (Maybe α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                    Just α
d   t -> α -> μ α
go (t
l t -> t -> t
forall a. Num a => a -> a -> a
- t
1) (α
r α -> α -> α
forall a. Num a => a -> a -> a
* α
radix α -> α -> α
forall a. Num a => a -> a -> a
+ α
d)
                    Maybe α
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
r
        radix :: α
radix   = s -> α
forall α. Num α => s -> α
forall s α. (PositionalSystem s, Num α) => s -> α
radixIn s
s
        digit :: μ α
digit   = s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE nncUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Int #-}
{-# SPECIALIZE nncUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Int8 #-}
{-# SPECIALIZE nncUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Int16 #-}
{-# SPECIALIZE nncUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Int32 #-}
{-# SPECIALIZE nncUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Int64 #-}
{-# SPECIALIZE nncUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Word #-}
{-# SPECIALIZE nncUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Word8 #-}
{-# SPECIALIZE nncUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Word16 #-}
{-# SPECIALIZE nncUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Word32 #-}
{-# SPECIALIZE nncUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Word64 #-}
{-# SPECIALIZE nncUpTo  (Num α, Monad μ, CharParsing μ)  Binary  Int  μ α #-}
{-# SPECIALIZE nncUpTo  (Num α, Monad μ, CharParsing μ)  Octal  Int  μ α #-}
{-# SPECIALIZE nncUpTo  (Num α, Monad μ, CharParsing μ)  Decimal  Int  μ α #-}
{-# SPECIALIZE nncUpTo  (Num α, Monad μ, CharParsing μ)  Hexadecimal  Int  μ α #-}
{-# SPECIALIZE nncUpTo  (Num α, Monad μ, CharParsing μ)  LowHex  Int  μ α #-}
{-# SPECIALIZE nncUpTo  (Num α, Monad μ, CharParsing μ)  UpHex  Int  μ α #-}

-- | Parse a non-negative number written in the specified positional
--   numeral system, failing on overflow.
nnBounded  (PositionalSystem s, Ord α, Bounded α, Integral α,
             Monad μ, CharParsing μ)  s  μ α
nnBounded :: forall s α (μ :: * -> *).
(PositionalSystem s, Ord α, Bounded α, Integral α, Monad μ,
 CharParsing μ) =>
s -> μ α
nnBounded s
s = μ α
digit μ α -> (α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= α -> μ α
go μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits"
  where (α
q, α
r) = α -> α -> (α, α)
forall a. Integral a => a -> a -> (a, a)
quotRem α
forall a. Bounded a => a
maxBound α
radix
        go :: α -> μ α
go !α
n  = μ α -> μ (Maybe α)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ α
digit μ (Maybe α) -> (Maybe α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                   Just α
n1  if α
n α -> α -> Bool
forall a. Ord a => a -> a -> Bool
< α
q Bool -> Bool -> Bool
|| (α
n α -> α -> Bool
forall a. Eq a => a -> a -> Bool
== α
q Bool -> Bool -> Bool
&& α
n1 α -> α -> Bool
forall a. Ord a => a -> a -> Bool
<= α
r)
                             then α -> μ α
go (α
n α -> α -> α
forall a. Num a => a -> a -> a
* α
radix α -> α -> α
forall a. Num a => a -> a -> a
+ α
n1)
                             else String -> μ α
forall a. String -> μ a
forall (m :: * -> *) a. Parsing m => String -> m a
PC.unexpected String
"out of bounds"
                   Maybe α
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
n
        radix :: α
radix  = s -> α
forall α. Num α => s -> α
forall s α. (PositionalSystem s, Num α) => s -> α
radixIn s
s
        digit :: μ α
digit  = s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE nnBounded  (Monad μ, CharParsing μ)  Decimal  μ Int #-}
{-# SPECIALIZE nnBounded  (Monad μ, CharParsing μ)  Decimal  μ Int8 #-}
{-# SPECIALIZE nnBounded  (Monad μ, CharParsing μ)  Decimal  μ Int16 #-}
{-# SPECIALIZE nnBounded  (Monad μ, CharParsing μ)  Decimal  μ Int32 #-}
{-# SPECIALIZE nnBounded  (Monad μ, CharParsing μ)  Decimal  μ Int64 #-}
{-# SPECIALIZE nnBounded  (Monad μ, CharParsing μ)  Decimal  μ Word #-}
{-# SPECIALIZE nnBounded  (Monad μ, CharParsing μ)  Decimal  μ Word8 #-}
{-# SPECIALIZE nnBounded  (Monad μ, CharParsing μ)  Decimal  μ Word16 #-}
{-# SPECIALIZE nnBounded  (Monad μ, CharParsing μ)  Decimal  μ Word32 #-}
{-# SPECIALIZE nnBounded  (Monad μ, CharParsing μ)  Decimal  μ Word64 #-}
{-# SPECIALIZE nnBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  Binary  μ α #-}
{-# SPECIALIZE nnBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  Octal  μ α #-}
{-# SPECIALIZE nnBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  Decimal  μ α #-}
{-# SPECIALIZE nnBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  Hexadecimal  μ α #-}
{-# SPECIALIZE nnBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  LowHex  μ α #-}
{-# SPECIALIZE nnBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  UpHex  μ α #-}

-- | Parse a non-negative number written in the specified positional
--   numeral system, failing on overflow. Leading zeroes are not allowed.
nncBounded  (PositionalSystem s, Ord α, Bounded α, Integral α,
              Monad μ, CharParsing μ)  s  μ α
nncBounded :: forall s α (μ :: * -> *).
(PositionalSystem s, Ord α, Bounded α, Integral α, Monad μ,
 CharParsing μ) =>
s -> μ α
nncBounded s
s = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits") (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ μ α
digit μ α -> (α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                 α
0  μ Char -> μ (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ((Char -> Bool) -> μ Char
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m Char
PC.satisfy ((Char -> Bool) -> μ Char) -> (Char -> Bool) -> μ Char
forall a b. (a -> b) -> a -> b
$ s -> Char -> Bool
forall s. PositionalSystem s => s -> Char -> Bool
isDigitIn s
s) μ (Maybe Char) -> (Maybe Char -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                       Just Char
_   String -> μ α
forall a. String -> μ a
forall (m :: * -> *) a. Parsing m => String -> m a
PC.unexpected String
"leading zero"
                       Maybe Char
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
0
                 α
n  α -> μ α
go α
n
  where (α
q, α
r) = α -> α -> (α, α)
forall a. Integral a => a -> a -> (a, a)
quotRem α
forall a. Bounded a => a
maxBound α
radix
        go :: α -> μ α
go !α
n  = μ α -> μ (Maybe α)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ α
digit μ (Maybe α) -> (Maybe α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                   Just α
d   if α
n α -> α -> Bool
forall a. Ord a => a -> a -> Bool
< α
q Bool -> Bool -> Bool
|| (α
n α -> α -> Bool
forall a. Eq a => a -> a -> Bool
== α
q Bool -> Bool -> Bool
&& α
d α -> α -> Bool
forall a. Ord a => a -> a -> Bool
<= α
r)
                             then α -> μ α
go (α
n α -> α -> α
forall a. Num a => a -> a -> a
* α
radix α -> α -> α
forall a. Num a => a -> a -> a
+ α
d)
                             else String -> μ α
forall a. String -> μ a
forall (m :: * -> *) a. Parsing m => String -> m a
PC.unexpected String
"out of bounds"
                   Maybe α
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
n
        radix :: α
radix  = s -> α
forall α. Num α => s -> α
forall s α. (PositionalSystem s, Num α) => s -> α
radixIn s
s
        digit :: μ α
digit  = s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE nncBounded  (Monad μ, CharParsing μ)  Decimal  μ Int #-}
{-# SPECIALIZE nncBounded  (Monad μ, CharParsing μ)  Decimal  μ Int8 #-}
{-# SPECIALIZE nncBounded  (Monad μ, CharParsing μ)  Decimal  μ Int16 #-}
{-# SPECIALIZE nncBounded  (Monad μ, CharParsing μ)  Decimal  μ Int32 #-}
{-# SPECIALIZE nncBounded  (Monad μ, CharParsing μ)  Decimal  μ Int64 #-}
{-# SPECIALIZE nncBounded  (Monad μ, CharParsing μ)  Decimal  μ Word #-}
{-# SPECIALIZE nncBounded  (Monad μ, CharParsing μ)  Decimal  μ Word8 #-}
{-# SPECIALIZE nncBounded  (Monad μ, CharParsing μ)  Decimal  μ Word16 #-}
{-# SPECIALIZE nncBounded  (Monad μ, CharParsing μ)  Decimal  μ Word32 #-}
{-# SPECIALIZE nncBounded  (Monad μ, CharParsing μ)  Decimal  μ Word64 #-}
{-# SPECIALIZE nncBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  Binary  μ α #-}
{-# SPECIALIZE nncBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  Octal  μ α #-}
{-# SPECIALIZE nncBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  Decimal  μ α #-}
{-# SPECIALIZE nncBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  Hexadecimal  μ α #-}
{-# SPECIALIZE nncBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  LowHex  μ α #-}
{-# SPECIALIZE nncBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  UpHex  μ α #-}

-- | Parse a non-negative binary number written in the specified
--   positional numeral system.
nnBits  (BitSystem s, Num α, Bits α, Monad μ, CharParsing μ)  s  μ α
nnBits :: forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
s -> μ α
nnBits s
s = μ α
digit μ α -> (α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= α -> μ α
go μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits"
  where go :: α -> μ α
go !α
r     = μ α -> μ (Maybe α)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ α
digit μ (Maybe α) -> (Maybe α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                      Just α
d   α -> μ α
go ((α
r α -> Int -> α
forall a. Bits a => a -> Int -> a
`shiftL` Int
digitBits) α -> α -> α
forall a. Bits a => a -> a -> a
.|. α
d)
                      Maybe α
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
r
        digitBits :: Int
digitBits = s -> Int
forall s. BitSystem s => s -> Int
digitBitsIn s
s
        digit :: μ α
digit     = s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Binary  μ Int #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Binary  μ Int8 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Binary  μ Int16 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Binary  μ Int32 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Binary  μ Int64 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Binary  μ Word #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Binary  μ Word8 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Binary  μ Word16 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Binary  μ Word32 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Binary  μ Word64 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Octal  μ Int #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Octal  μ Int8 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Octal  μ Int16 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Octal  μ Int32 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Octal  μ Int64 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Octal  μ Word #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Octal  μ Word8 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Octal  μ Word16 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Octal  μ Word32 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Octal  μ Word64 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int8 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int16 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int32 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int64 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word8 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word16 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word32 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word64 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  LowHex  μ Int #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  LowHex  μ Int8 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  LowHex  μ Int16 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  LowHex  μ Int32 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  LowHex  μ Int64 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  LowHex  μ Word #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  LowHex  μ Word8 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  LowHex  μ Word16 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  LowHex  μ Word32 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  LowHex  μ Word64 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  UpHex  μ Int #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  UpHex  μ Int8 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  UpHex  μ Int16 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  UpHex  μ Int32 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  UpHex  μ Int64 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  UpHex  μ Word #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  UpHex  μ Word8 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  UpHex  μ Word16 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  UpHex  μ Word32 #-}
{-# SPECIALIZE nnBits  (Monad μ, CharParsing μ)  UpHex  μ Word64 #-}
{-# SPECIALIZE nnBits  (Num α, Bits α, Monad μ, CharParsing μ)  Binary  μ α #-}
{-# SPECIALIZE nnBits  (Num α, Bits α, Monad μ, CharParsing μ)  Octal  μ α #-}
{-# SPECIALIZE nnBits  (Num α, Bits α, Monad μ, CharParsing μ)  Hexadecimal  μ α #-}
{-# SPECIALIZE nnBits  (Num α, Bits α, Monad μ, CharParsing μ)  LowHex  μ α #-}
{-# SPECIALIZE nnBits  (Num α, Bits α, Monad μ, CharParsing μ)  UpHex  μ α #-}

-- | Parse a non-negative binary number written in the specified
--   positional numeral system. Leading zeroes are not allowed.
nncBits  (BitSystem s, Num α, Bits α, Monad μ, CharParsing μ)  s  μ α
nncBits :: forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
s -> μ α
nncBits s
s = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits") (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ μ α
digit μ α -> (α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
              α
0  μ Char -> μ (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ((Char -> Bool) -> μ Char
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m Char
PC.satisfy ((Char -> Bool) -> μ Char) -> (Char -> Bool) -> μ Char
forall a b. (a -> b) -> a -> b
$ s -> Char -> Bool
forall s. PositionalSystem s => s -> Char -> Bool
isDigitIn s
s) μ (Maybe Char) -> (Maybe Char -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                    Just Char
_   String -> μ α
forall a. String -> μ a
forall (m :: * -> *) a. Parsing m => String -> m a
PC.unexpected String
"leading zero"
                    Maybe Char
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
0 
              α
r  α -> μ α
go α
r
  where go :: α -> μ α
go !α
r     = μ α -> μ (Maybe α)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ α
digit μ (Maybe α) -> (Maybe α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                      Just α
d   α -> μ α
go ((α
r α -> Int -> α
forall a. Bits a => a -> Int -> a
`shiftL` Int
digitBits) α -> α -> α
forall a. Bits a => a -> a -> a
.|. α
d)
                      Maybe α
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
r
        digitBits :: Int
digitBits = s -> Int
forall s. BitSystem s => s -> Int
digitBitsIn s
s
        digit :: μ α
digit     = s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Binary  μ Int #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Binary  μ Int8 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Binary  μ Int16 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Binary  μ Int32 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Binary  μ Int64 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Binary  μ Word #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Binary  μ Word8 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Binary  μ Word16 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Binary  μ Word32 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Binary  μ Word64 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Octal  μ Int #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Octal  μ Int8 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Octal  μ Int16 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Octal  μ Int32 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Octal  μ Int64 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Octal  μ Word #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Octal  μ Word8 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Octal  μ Word16 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Octal  μ Word32 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Octal  μ Word64 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int8 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int16 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int32 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int64 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word8 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word16 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word32 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word64 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  LowHex  μ Int #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  LowHex  μ Int8 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  LowHex  μ Int16 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  LowHex  μ Int32 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  LowHex  μ Int64 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  LowHex  μ Word #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  LowHex  μ Word8 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  LowHex  μ Word16 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  LowHex  μ Word32 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  LowHex  μ Word64 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  UpHex  μ Int #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  UpHex  μ Int8 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  UpHex  μ Int16 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  UpHex  μ Int32 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  UpHex  μ Int64 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  UpHex  μ Word #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  UpHex  μ Word8 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  UpHex  μ Word16 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  UpHex  μ Word32 #-}
{-# SPECIALIZE nncBits  (Monad μ, CharParsing μ)  UpHex  μ Word64 #-}
{-# SPECIALIZE nncBits  (Num α, Bits α, Monad μ, CharParsing μ)  Binary  μ α #-}
{-# SPECIALIZE nncBits  (Num α, Bits α, Monad μ, CharParsing μ)  Octal  μ α #-}
{-# SPECIALIZE nncBits  (Num α, Bits α, Monad μ, CharParsing μ)  Hexadecimal  μ α #-}
{-# SPECIALIZE nncBits  (Num α, Bits α, Monad μ, CharParsing μ)  LowHex  μ α #-}
{-# SPECIALIZE nncBits  (Num α, Bits α, Monad μ, CharParsing μ)  UpHex  μ α #-}

-- | Parse a non-negative binary number written in the specified
--   positional numeral system (up to /n/ digits).
nnBitsUpTo  (BitSystem s, Num α, Bits α, Monad μ, CharParsing μ)
            s  Int  μ α
nnBitsUpTo :: forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
s -> Int -> μ α
nnBitsUpTo s
_ Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = μ α
forall a. μ a
forall (f :: * -> *) a. Alternative f => f a
empty
nnBitsUpTo s
s Int
n = μ α
digit μ α -> (α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> α -> μ α
forall {t}. (Num t, Eq t) => t -> α -> μ α
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits"
  where go :: t -> α -> μ α
go t
0 !α
r   = μ Char -> μ (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ((Char -> Bool) -> μ Char
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m Char
PC.satisfy ((Char -> Bool) -> μ Char) -> (Char -> Bool) -> μ Char
forall a b. (a -> b) -> a -> b
$ s -> Char -> Bool
forall s. PositionalSystem s => s -> Char -> Bool
isDigitIn s
s) μ (Maybe Char) -> (Maybe Char -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                      Just Char
_   Int -> μ α
forall (μ :: * -> *) α. CharParsing μ => Int -> μ α
moreThan Int
n
                      Maybe Char
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
r
        go t
l !α
r   = μ α -> μ (Maybe α)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ α
digit μ (Maybe α) -> (Maybe α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                      Just α
d   t -> α -> μ α
go (t
l t -> t -> t
forall a. Num a => a -> a -> a
- t
1) ((α
r α -> Int -> α
forall a. Bits a => a -> Int -> a
`shiftL` Int
digitBits) α -> α -> α
forall a. Bits a => a -> a -> a
.|. α
d)
                      Maybe α
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
r
        digitBits :: Int
digitBits = s -> Int
forall s. BitSystem s => s -> Int
digitBitsIn s
s
        digit :: μ α
digit     = s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Int #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Int8 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Int16 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Int32 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Int64 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Word #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Word8 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Word16 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Word32 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Word64 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Int #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Int8 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Int16 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Int32 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Int64 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Word #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Word8 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Word16 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Word32 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Word64 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Int #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Int8 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Int16 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Int32 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Int64 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Word #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Word8 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Word16 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Word32 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Word64 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Int #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Int8 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Int16 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Int32 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Int64 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Word #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Word8 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Word16 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Word32 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Word64 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Int #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Int8 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Int16 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Int32 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Int64 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Word #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Word8 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Word16 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Word32 #-}
{-# SPECIALIZE nnBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Word64 #-}
{-# SPECIALIZE nnBitsUpTo  (Num α, Bits α, Monad μ, CharParsing μ)  Binary  Int  μ α #-}
{-# SPECIALIZE nnBitsUpTo  (Num α, Bits α, Monad μ, CharParsing μ)  Octal  Int  μ α #-}
{-# SPECIALIZE nnBitsUpTo  (Num α, Bits α, Monad μ, CharParsing μ)  Hexadecimal  Int  μ α #-}
{-# SPECIALIZE nnBitsUpTo  (Num α, Bits α, Monad μ, CharParsing μ)  LowHex  Int  μ α #-}
{-# SPECIALIZE nnBitsUpTo  (Num α, Bits α, Monad μ, CharParsing μ)  UpHex  Int  μ α #-}

-- | Parse a non-negative binary number written in the specified
--   positional numeral system (up to /n/ digits). Leading zeroes are not
--   allowed.
nncBitsUpTo  (BitSystem s, Num α, Bits α, Monad μ, CharParsing μ)
             s  Int  μ α
nncBitsUpTo :: forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
s -> Int -> μ α
nncBitsUpTo s
_ Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = μ α
forall a. μ a
forall (f :: * -> *) a. Alternative f => f a
empty
nncBitsUpTo s
s Int
n = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits") (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ s -> μ Word
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s μ Word -> (Word -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                    Word
0  μ Char -> μ (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ((Char -> Bool) -> μ Char
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m Char
PC.satisfy ((Char -> Bool) -> μ Char) -> (Char -> Bool) -> μ Char
forall a b. (a -> b) -> a -> b
$ s -> Char -> Bool
forall s. PositionalSystem s => s -> Char -> Bool
isDigitIn s
s) μ (Maybe Char) -> (Maybe Char -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                          Just Char
_   String -> μ α
forall a. String -> μ a
forall (m :: * -> *) a. Parsing m => String -> m a
PC.unexpected String
"leading zero"
                          Maybe Char
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
0 
                    Word
r  Int -> α -> μ α
forall {t}. (Num t, Eq t) => t -> α -> μ α
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (α -> μ α) -> α -> μ α
forall a b. (a -> b) -> a -> b
$ Word -> α
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word
r  Word)
  where go :: t -> α -> μ α
go t
0 !α
r   = μ Char -> μ (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ((Char -> Bool) -> μ Char
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m Char
PC.satisfy ((Char -> Bool) -> μ Char) -> (Char -> Bool) -> μ Char
forall a b. (a -> b) -> a -> b
$ s -> Char -> Bool
forall s. PositionalSystem s => s -> Char -> Bool
isDigitIn s
s) μ (Maybe Char) -> (Maybe Char -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                      Just Char
_   Int -> μ α
forall (μ :: * -> *) α. CharParsing μ => Int -> μ α
moreThan Int
n
                      Maybe Char
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
r
        go t
l !α
r   = μ α -> μ (Maybe α)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ α
digit μ (Maybe α) -> (Maybe α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                      Just α
d   t -> α -> μ α
go (t
l t -> t -> t
forall a. Num a => a -> a -> a
- t
1) ((α
r α -> Int -> α
forall a. Bits a => a -> Int -> a
`shiftL` Int
digitBits) α -> α -> α
forall a. Bits a => a -> a -> a
.|. α
d)
                      Maybe α
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
r
        digitBits :: Int
digitBits = s -> Int
forall s. BitSystem s => s -> Int
digitBitsIn s
s
        digit :: μ α
digit     = s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Int #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Int8 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Int16 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Int32 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Int64 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Word #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Word8 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Word16 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Word32 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Word64 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Int #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Int8 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Int16 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Int32 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Int64 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Word #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Word8 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Word16 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Word32 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Word64 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Int #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Int8 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Int16 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Int32 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Int64 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Word #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Word8 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Word16 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Word32 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Word64 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Int #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Int8 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Int16 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Int32 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Int64 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Word #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Word8 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Word16 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Word32 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Word64 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Int #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Int8 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Int16 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Int32 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Int64 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Word #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Word8 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Word16 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Word32 #-}
{-# SPECIALIZE nncBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Word64 #-}
{-# SPECIALIZE nncBitsUpTo  (Num α, Bits α, Monad μ, CharParsing μ)  Binary  Int  μ α #-}
{-# SPECIALIZE nncBitsUpTo  (Num α, Bits α, Monad μ, CharParsing μ)  Octal  Int  μ α #-}
{-# SPECIALIZE nncBitsUpTo  (Num α, Bits α, Monad μ, CharParsing μ)  Hexadecimal  Int  μ α #-}
{-# SPECIALIZE nncBitsUpTo  (Num α, Bits α, Monad μ, CharParsing μ)  LowHex  Int  μ α #-}
{-# SPECIALIZE nncBitsUpTo  (Num α, Bits α, Monad μ, CharParsing μ)  UpHex  Int  μ α #-}

-- | Parse a non-negative binary number written in the specified
--   positional numeral system, failing on overflow.
nnbBits  (BitSystem s, Ord α, Bounded α, Num α, Bits α,
           Monad μ, CharParsing μ)
         s  μ α
nnbBits :: forall s α (μ :: * -> *).
(BitSystem s, Ord α, Bounded α, Num α, Bits α, Monad μ,
 CharParsing μ) =>
s -> μ α
nnbBits s
s = μ α
digit μ α -> (α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= α -> μ α
go μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits"
  where q :: α
q = α
forall a. Bounded a => a
maxBound α -> Int -> α
forall a. Bits a => a -> Int -> a
`shiftR` Int
digitBits
        r :: α
r = α
forall a. Bounded a => a
maxBound α -> α -> α
forall a. Bits a => a -> a -> a
.&. s -> α
forall α. Num α => s -> α
forall s α. (BitSystem s, Num α) => s -> α
digitMaskIn s
s
        go :: α -> μ α
go !α
n = μ α -> μ (Maybe α)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ α
digit μ (Maybe α) -> (Maybe α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                  Just α
d   if α
n α -> α -> Bool
forall a. Ord a => a -> a -> Bool
< α
q Bool -> Bool -> Bool
|| (α
n α -> α -> Bool
forall a. Eq a => a -> a -> Bool
== α
q Bool -> Bool -> Bool
&& α
d α -> α -> Bool
forall a. Ord a => a -> a -> Bool
<= α
r)
                            then α -> μ α
go ((α
n α -> Int -> α
forall a. Bits a => a -> Int -> a
`shiftL` Int
digitBits) α -> α -> α
forall a. Bits a => a -> a -> a
.|. α
d)
                            else String -> μ α
forall a. String -> μ a
forall (m :: * -> *) a. Parsing m => String -> m a
PC.unexpected String
"out of bounds"
                  Maybe α
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
n
        digitBits :: Int
digitBits = s -> Int
forall s. BitSystem s => s -> Int
digitBitsIn s
s
        digit :: μ α
digit     = s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Binary  μ Int #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Binary  μ Int8 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Binary  μ Int16 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Binary  μ Int32 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Binary  μ Int64 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Binary  μ Word #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Binary  μ Word8 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Binary  μ Word16 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Binary  μ Word32 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Binary  μ Word64 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Octal  μ Int #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Octal  μ Int8 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Octal  μ Int16 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Octal  μ Int32 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Octal  μ Int64 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Octal  μ Word #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Octal  μ Word8 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Octal  μ Word16 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Octal  μ Word32 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Octal  μ Word64 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int8 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int16 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int32 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int64 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word8 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word16 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word32 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word64 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  LowHex  μ Int #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  LowHex  μ Int8 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  LowHex  μ Int16 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  LowHex  μ Int32 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  LowHex  μ Int64 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  LowHex  μ Word #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  LowHex  μ Word8 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  LowHex  μ Word16 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  LowHex  μ Word32 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  LowHex  μ Word64 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  UpHex  μ Int #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  UpHex  μ Int8 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  UpHex  μ Int16 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  UpHex  μ Int32 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  UpHex  μ Int64 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  UpHex  μ Word #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  UpHex  μ Word8 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  UpHex  μ Word16 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  UpHex  μ Word32 #-}
{-# SPECIALIZE nnbBits  (Monad μ, CharParsing μ)  UpHex  μ Word64 #-}
{-# SPECIALIZE nnbBits  (Ord α, Bounded α, Num α, Bits α, Monad μ, CharParsing μ)  Binary  μ α #-}
{-# SPECIALIZE nnbBits  (Ord α, Bounded α, Num α, Bits α, Monad μ, CharParsing μ)  Octal  μ α #-}
{-# SPECIALIZE nnbBits  (Ord α, Bounded α, Num α, Bits α, Monad μ, CharParsing μ)  Hexadecimal  μ α #-}
{-# SPECIALIZE nnbBits  (Ord α, Bounded α, Num α, Bits α, Monad μ, CharParsing μ)  LowHex  μ α #-}
{-# SPECIALIZE nnbBits  (Ord α, Bounded α, Num α, Bits α, Monad μ, CharParsing μ)  UpHex  μ α #-}

-- | Parse a non-negative binary number written in the specified
--   positional numeral system, failing on overflow. Leading zeroes are not
--   allowed.
nncbBits  (BitSystem s, Ord α, Bounded α, Num α, Bits α,
            Monad μ, CharParsing μ)
          s  μ α
nncbBits :: forall s α (μ :: * -> *).
(BitSystem s, Ord α, Bounded α, Num α, Bits α, Monad μ,
 CharParsing μ) =>
s -> μ α
nncbBits s
s = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits") (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ s -> μ Word
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s μ Word -> (Word -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
               Word
0  μ Char -> μ (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ((Char -> Bool) -> μ Char
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m Char
PC.satisfy ((Char -> Bool) -> μ Char) -> (Char -> Bool) -> μ Char
forall a b. (a -> b) -> a -> b
$ s -> Char -> Bool
forall s. PositionalSystem s => s -> Char -> Bool
isDigitIn s
s) μ (Maybe Char) -> (Maybe Char -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                     Just Char
_   String -> μ α
forall a. String -> μ a
forall (m :: * -> *) a. Parsing m => String -> m a
PC.unexpected String
"leading zero"
                     Maybe Char
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
0
               Word
n  α -> μ α
go (α -> μ α) -> α -> μ α
forall a b. (a -> b) -> a -> b
$ Word -> α
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word
n  Word)
  where q :: α
q = α
forall a. Bounded a => a
maxBound α -> Int -> α
forall a. Bits a => a -> Int -> a
`shiftR` Int
digitBits
        r :: α
r = α
forall a. Bounded a => a
maxBound α -> α -> α
forall a. Bits a => a -> a -> a
.&. s -> α
forall α. Num α => s -> α
forall s α. (BitSystem s, Num α) => s -> α
digitMaskIn s
s
        go :: α -> μ α
go !α
n = μ α -> μ (Maybe α)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ α
digit μ (Maybe α) -> (Maybe α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                  Just α
d   if α
n α -> α -> Bool
forall a. Ord a => a -> a -> Bool
< α
q Bool -> Bool -> Bool
|| (α
n α -> α -> Bool
forall a. Eq a => a -> a -> Bool
== α
q Bool -> Bool -> Bool
&& α
d α -> α -> Bool
forall a. Ord a => a -> a -> Bool
<= α
r)
                            then α -> μ α
go ((α
n α -> Int -> α
forall a. Bits a => a -> Int -> a
`shiftL` Int
digitBits) α -> α -> α
forall a. Bits a => a -> a -> a
.|. α
d)
                            else String -> μ α
forall a. String -> μ a
forall (m :: * -> *) a. Parsing m => String -> m a
PC.unexpected String
"out of bounds"
                  Maybe α
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
n
        digitBits :: Int
digitBits = s -> Int
forall s. BitSystem s => s -> Int
digitBitsIn s
s
        digit :: μ α
digit     = s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Binary  μ Int #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Binary  μ Int8 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Binary  μ Int16 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Binary  μ Int32 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Binary  μ Int64 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Binary  μ Word #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Binary  μ Word8 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Binary  μ Word16 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Binary  μ Word32 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Binary  μ Word64 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Octal  μ Int #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Octal  μ Int8 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Octal  μ Int16 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Octal  μ Int32 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Octal  μ Int64 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Octal  μ Word #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Octal  μ Word8 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Octal  μ Word16 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Octal  μ Word32 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Octal  μ Word64 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int8 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int16 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int32 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int64 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word8 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word16 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word32 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word64 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  LowHex  μ Int #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  LowHex  μ Int8 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  LowHex  μ Int16 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  LowHex  μ Int32 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  LowHex  μ Int64 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  LowHex  μ Word #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  LowHex  μ Word8 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  LowHex  μ Word16 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  LowHex  μ Word32 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  LowHex  μ Word64 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  UpHex  μ Int #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  UpHex  μ Int8 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  UpHex  μ Int16 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  UpHex  μ Int32 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  UpHex  μ Int64 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  UpHex  μ Word #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  UpHex  μ Word8 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  UpHex  μ Word16 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  UpHex  μ Word32 #-}
{-# SPECIALIZE nncbBits  (Monad μ, CharParsing μ)  UpHex  μ Word64 #-}
{-# SPECIALIZE nncbBits  (Ord α, Bounded α, Num α, Bits α, Monad μ, CharParsing μ)  Binary  μ α #-}
{-# SPECIALIZE nncbBits  (Ord α, Bounded α, Num α, Bits α, Monad μ, CharParsing μ)  Octal  μ α #-}
{-# SPECIALIZE nncbBits  (Ord α, Bounded α, Num α, Bits α, Monad μ, CharParsing μ)  Hexadecimal  μ α #-}
{-# SPECIALIZE nncbBits  (Ord α, Bounded α, Num α, Bits α, Monad μ, CharParsing μ)  LowHex  μ α #-}
{-# SPECIALIZE nncbBits  (Ord α, Bounded α, Num α, Bits α, Monad μ, CharParsing μ)  UpHex  μ α #-}

-- | Parse a non-positive number written in the specified positional
--   numeral system. For example, parsing \"123\" as a decimal would produce
--   /-123/, not /123/.
nonPositive  (PositionalSystem s, Num α, Monad μ, CharParsing μ)  s  μ α
nonPositive :: forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
s -> μ α
nonPositive s
s = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits") (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ do
                  Int
r  s -> μ Int
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
                  α -> μ α
go (α -> μ α) -> α -> μ α
forall a b. (a -> b) -> a -> b
$ Int -> α
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> α) -> Int -> α
forall a b. (a -> b) -> a -> b
$ Int -> Int
forall a. Num a => a -> a
negate (Int
r  Int)
  where go :: α -> μ α
go !α
r = μ α -> μ (Maybe α)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ α
digit μ (Maybe α) -> (Maybe α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                  Just α
d   α -> μ α
go (α
r α -> α -> α
forall a. Num a => a -> a -> a
* α
radix α -> α -> α
forall a. Num a => a -> a -> a
- α
d)
                  Maybe α
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
r
        radix :: α
radix = s -> α
forall α. Num α => s -> α
forall s α. (PositionalSystem s, Num α) => s -> α
radixIn s
s
        digit :: μ α
digit = s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE nonPositive  (Monad μ, CharParsing μ)  Decimal  μ Int #-}
{-# SPECIALIZE nonPositive  (Monad μ, CharParsing μ)  Decimal  μ Int8 #-}
{-# SPECIALIZE nonPositive  (Monad μ, CharParsing μ)  Decimal  μ Int16 #-}
{-# SPECIALIZE nonPositive  (Monad μ, CharParsing μ)  Decimal  μ Int32 #-}
{-# SPECIALIZE nonPositive  (Monad μ, CharParsing μ)  Decimal  μ Int64 #-}
{-# SPECIALIZE nonPositive  (Monad μ, CharParsing μ)  Decimal  μ Word #-}
{-# SPECIALIZE nonPositive  (Monad μ, CharParsing μ)  Decimal  μ Word8 #-}
{-# SPECIALIZE nonPositive  (Monad μ, CharParsing μ)  Decimal  μ Word16 #-}
{-# SPECIALIZE nonPositive  (Monad μ, CharParsing μ)  Decimal  μ Word32 #-}
{-# SPECIALIZE nonPositive  (Monad μ, CharParsing μ)  Decimal  μ Word64 #-}
{-# SPECIALIZE nonPositive  (Num α, Monad μ, CharParsing μ)  Binary  μ α #-}
{-# SPECIALIZE nonPositive  (Num α, Monad μ, CharParsing μ)  Octal  μ α #-}
{-# SPECIALIZE nonPositive  (Num α, Monad μ, CharParsing μ)  Decimal  μ α #-}
{-# SPECIALIZE nonPositive  (Num α, Monad μ, CharParsing μ)  Hexadecimal  μ α #-}
{-# SPECIALIZE nonPositive  (Num α, Monad μ, CharParsing μ)  LowHex  μ α #-}
{-# SPECIALIZE nonPositive  (Num α, Monad μ, CharParsing μ)  UpHex  μ α #-}

-- | Parse a non-positive number written in the specified positional
--   numeral system. Leading zeroes are not allowed.
npCompact  (PositionalSystem s, Num α, Monad μ, CharParsing μ)  s  μ α
npCompact :: forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
s -> μ α
npCompact s
s = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits") (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ s -> μ Int
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s μ Int -> (Int -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                Int
0  μ Char -> μ (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ((Char -> Bool) -> μ Char
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m Char
PC.satisfy ((Char -> Bool) -> μ Char) -> (Char -> Bool) -> μ Char
forall a b. (a -> b) -> a -> b
$ s -> Char -> Bool
forall s. PositionalSystem s => s -> Char -> Bool
isDigitIn s
s) μ (Maybe Char) -> (Maybe Char -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                      Just Char
_   String -> μ α
forall a. String -> μ a
forall (m :: * -> *) a. Parsing m => String -> m a
PC.unexpected String
"leading zero"
                      Maybe Char
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
0
                Int
r  α -> μ α
go (α -> μ α) -> α -> μ α
forall a b. (a -> b) -> a -> b
$ Int -> α
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> α) -> Int -> α
forall a b. (a -> b) -> a -> b
$ Int -> Int
forall a. Num a => a -> a
negate (Int
r  Int)
  where go :: α -> μ α
go !α
r = μ α -> μ (Maybe α)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ α
digit μ (Maybe α) -> (Maybe α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                  Just α
d   α -> μ α
go (α
r α -> α -> α
forall a. Num a => a -> a -> a
* α
radix α -> α -> α
forall a. Num a => a -> a -> a
- α
d)
                  Maybe α
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
r
        radix :: α
radix = s -> α
forall α. Num α => s -> α
forall s α. (PositionalSystem s, Num α) => s -> α
radixIn s
s
        digit :: μ α
digit = s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE npCompact  (Monad μ, CharParsing μ)  Decimal  μ Int #-}
{-# SPECIALIZE npCompact  (Monad μ, CharParsing μ)  Decimal  μ Int8 #-}
{-# SPECIALIZE npCompact  (Monad μ, CharParsing μ)  Decimal  μ Int16 #-}
{-# SPECIALIZE npCompact  (Monad μ, CharParsing μ)  Decimal  μ Int32 #-}
{-# SPECIALIZE npCompact  (Monad μ, CharParsing μ)  Decimal  μ Int64 #-}
{-# SPECIALIZE npCompact  (Monad μ, CharParsing μ)  Decimal  μ Word #-}
{-# SPECIALIZE npCompact  (Monad μ, CharParsing μ)  Decimal  μ Word8 #-}
{-# SPECIALIZE npCompact  (Monad μ, CharParsing μ)  Decimal  μ Word16 #-}
{-# SPECIALIZE npCompact  (Monad μ, CharParsing μ)  Decimal  μ Word32 #-}
{-# SPECIALIZE npCompact  (Monad μ, CharParsing μ)  Decimal  μ Word64 #-}
{-# SPECIALIZE npCompact  (Num α, Monad μ, CharParsing μ)  Binary  μ α #-}
{-# SPECIALIZE npCompact  (Num α, Monad μ, CharParsing μ)  Octal  μ α #-}
{-# SPECIALIZE npCompact  (Num α, Monad μ, CharParsing μ)  Decimal  μ α #-}
{-# SPECIALIZE npCompact  (Num α, Monad μ, CharParsing μ)  Hexadecimal  μ α #-}
{-# SPECIALIZE npCompact  (Num α, Monad μ, CharParsing μ)  LowHex  μ α #-}
{-# SPECIALIZE npCompact  (Num α, Monad μ, CharParsing μ)  UpHex  μ α #-}

-- | Parse a non-positive number written in the specified positional
--   numeral system (up to /n/ digits).
npUpTo  (PositionalSystem s, Num α, Monad μ, CharParsing μ)  s  Int  μ α
npUpTo :: forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
s -> Int -> μ α
npUpTo s
_ Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = μ α
forall a. μ a
forall (f :: * -> *) a. Alternative f => f a
empty
npUpTo s
s Int
n = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits") (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ do
               Int
r  s -> μ Int
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
               Int -> α -> μ α
forall {t}. (Eq t, Num t) => t -> α -> μ α
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (α -> μ α) -> α -> μ α
forall a b. (a -> b) -> a -> b
$ Int -> α
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> α) -> Int -> α
forall a b. (a -> b) -> a -> b
$ Int -> Int
forall a. Num a => a -> a
negate (Int
r  Int)
  where go :: t -> α -> μ α
go t
0 !α
r = μ Char -> μ (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ((Char -> Bool) -> μ Char
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m Char
PC.satisfy ((Char -> Bool) -> μ Char) -> (Char -> Bool) -> μ Char
forall a b. (a -> b) -> a -> b
$ s -> Char -> Bool
forall s. PositionalSystem s => s -> Char -> Bool
isDigitIn s
s) μ (Maybe Char) -> (Maybe Char -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                    Just Char
_   Int -> μ α
forall (μ :: * -> *) α. CharParsing μ => Int -> μ α
moreThan Int
n
                    Maybe Char
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
r
        go t
l !α
r = μ α -> μ (Maybe α)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ α
digit μ (Maybe α) -> (Maybe α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                    Just α
d   t -> α -> μ α
go (t
l t -> t -> t
forall a. Num a => a -> a -> a
- t
1) (α
r α -> α -> α
forall a. Num a => a -> a -> a
* α
radix α -> α -> α
forall a. Num a => a -> a -> a
- α
d)
                    Maybe α
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
r
        radix :: α
radix   = s -> α
forall α. Num α => s -> α
forall s α. (PositionalSystem s, Num α) => s -> α
radixIn s
s
        digit :: μ α
digit   = s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE npUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Int #-}
{-# SPECIALIZE npUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Int8 #-}
{-# SPECIALIZE npUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Int16 #-}
{-# SPECIALIZE npUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Int32 #-}
{-# SPECIALIZE npUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Int64 #-}
{-# SPECIALIZE npUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Word #-}
{-# SPECIALIZE npUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Word8 #-}
{-# SPECIALIZE npUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Word16 #-}
{-# SPECIALIZE npUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Word32 #-}
{-# SPECIALIZE npUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Word64 #-}
{-# SPECIALIZE npUpTo  (Num α, Monad μ, CharParsing μ)  Binary  Int  μ α #-}
{-# SPECIALIZE npUpTo  (Num α, Monad μ, CharParsing μ)  Octal  Int  μ α #-}
{-# SPECIALIZE npUpTo  (Num α, Monad μ, CharParsing μ)  Decimal  Int  μ α #-}
{-# SPECIALIZE npUpTo  (Num α, Monad μ, CharParsing μ)  Hexadecimal  Int  μ α #-}
{-# SPECIALIZE npUpTo  (Num α, Monad μ, CharParsing μ)  LowHex  Int  μ α #-}
{-# SPECIALIZE npUpTo  (Num α, Monad μ, CharParsing μ)  UpHex  Int  μ α #-}

-- | Parse a non-positive number written in the specified positional
--   numeral system (up to /n/ digits). Leading zeroes are not allowed.
npcUpTo  (PositionalSystem s, Num α, Monad μ, CharParsing μ)  s  Int  μ α
npcUpTo :: forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
s -> Int -> μ α
npcUpTo s
_ Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = μ α
forall a. μ a
forall (f :: * -> *) a. Alternative f => f a
empty
npcUpTo s
s Int
n = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits") (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ s -> μ Int
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s μ Int -> (Int -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                Int
0  μ Char -> μ (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ((Char -> Bool) -> μ Char
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m Char
PC.satisfy ((Char -> Bool) -> μ Char) -> (Char -> Bool) -> μ Char
forall a b. (a -> b) -> a -> b
$ s -> Char -> Bool
forall s. PositionalSystem s => s -> Char -> Bool
isDigitIn s
s) μ (Maybe Char) -> (Maybe Char -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                      Just Char
_   String -> μ α
forall a. String -> μ a
forall (m :: * -> *) a. Parsing m => String -> m a
PC.unexpected String
"leading zero"
                      Maybe Char
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
0
                Int
r  Int -> α -> μ α
forall {t}. (Eq t, Num t) => t -> α -> μ α
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (α -> μ α) -> α -> μ α
forall a b. (a -> b) -> a -> b
$ Int -> α
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> α) -> Int -> α
forall a b. (a -> b) -> a -> b
$ Int -> Int
forall a. Num a => a -> a
negate (Int
r  Int)
  where go :: t -> α -> μ α
go t
0 !α
r = μ Char -> μ (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ((Char -> Bool) -> μ Char
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m Char
PC.satisfy ((Char -> Bool) -> μ Char) -> (Char -> Bool) -> μ Char
forall a b. (a -> b) -> a -> b
$ s -> Char -> Bool
forall s. PositionalSystem s => s -> Char -> Bool
isDigitIn s
s) μ (Maybe Char) -> (Maybe Char -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                    Just Char
_   Int -> μ α
forall (μ :: * -> *) α. CharParsing μ => Int -> μ α
moreThan Int
n
                    Maybe Char
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
r
        go t
l !α
r = μ α -> μ (Maybe α)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ α
digit μ (Maybe α) -> (Maybe α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                    Just α
d   t -> α -> μ α
go (t
l t -> t -> t
forall a. Num a => a -> a -> a
- t
1) (α
r α -> α -> α
forall a. Num a => a -> a -> a
* α
radix α -> α -> α
forall a. Num a => a -> a -> a
- α
d)
                    Maybe α
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
r
        radix :: α
radix   = s -> α
forall α. Num α => s -> α
forall s α. (PositionalSystem s, Num α) => s -> α
radixIn s
s
        digit :: μ α
digit   = s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE npcUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Int #-}
{-# SPECIALIZE npcUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Int8 #-}
{-# SPECIALIZE npcUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Int16 #-}
{-# SPECIALIZE npcUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Int32 #-}
{-# SPECIALIZE npcUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Int64 #-}
{-# SPECIALIZE npcUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Word #-}
{-# SPECIALIZE npcUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Word8 #-}
{-# SPECIALIZE npcUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Word16 #-}
{-# SPECIALIZE npcUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Word32 #-}
{-# SPECIALIZE npcUpTo  (Monad μ, CharParsing μ)  Decimal  Int  μ Word64 #-}
{-# SPECIALIZE npcUpTo  (Num α, Monad μ, CharParsing μ)  Binary  Int  μ α #-}
{-# SPECIALIZE npcUpTo  (Num α, Monad μ, CharParsing μ)  Octal  Int  μ α #-}
{-# SPECIALIZE npcUpTo  (Num α, Monad μ, CharParsing μ)  Decimal  Int  μ α #-}
{-# SPECIALIZE npcUpTo  (Num α, Monad μ, CharParsing μ)  Hexadecimal  Int  μ α #-}
{-# SPECIALIZE npcUpTo  (Num α, Monad μ, CharParsing μ)  LowHex  Int  μ α #-}
{-# SPECIALIZE npcUpTo  (Num α, Monad μ, CharParsing μ)  UpHex  Int  μ α #-}

-- | Parse a non-positive number written in the specified positional
--   numeral system, failing on overflow.
npBounded  (PositionalSystem s, Ord α, Bounded α, Integral α,
             Monad μ, CharParsing μ)
           s  μ α
npBounded :: forall s α (μ :: * -> *).
(PositionalSystem s, Ord α, Bounded α, Integral α, Monad μ,
 CharParsing μ) =>
s -> μ α
npBounded s
s = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits") (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ do
                Int
n  s -> μ Int
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
                α -> μ α
go (α -> μ α) -> α -> μ α
forall a b. (a -> b) -> a -> b
$ Int -> α
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> α) -> Int -> α
forall a b. (a -> b) -> a -> b
$ Int -> Int
forall a. Num a => a -> a
negate (Int
n  Int)
  where (α
q, α
r1) = α -> α -> (α, α)
forall a. Integral a => a -> a -> (a, a)
quotRem α
forall a. Bounded a => a
minBound α
radix
        !r :: α
r      = α -> α
forall a. Num a => a -> a
negate α
r1
        go :: α -> μ α
go !α
n   = μ α -> μ (Maybe α)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ α
digit μ (Maybe α) -> (Maybe α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                    Just α
d   if α
n α -> α -> Bool
forall a. Ord a => a -> a -> Bool
> α
q Bool -> Bool -> Bool
|| (α
n α -> α -> Bool
forall a. Eq a => a -> a -> Bool
== α
q Bool -> Bool -> Bool
&& α
d α -> α -> Bool
forall a. Ord a => a -> a -> Bool
<= α
r)
                              then α -> μ α
go (α
n α -> α -> α
forall a. Num a => a -> a -> a
* α
radix α -> α -> α
forall a. Num a => a -> a -> a
- α
d)
                              else String -> μ α
forall a. String -> μ a
forall (m :: * -> *) a. Parsing m => String -> m a
PC.unexpected String
"out of bounds"
                    Maybe α
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
n
        radix :: α
radix   = s -> α
forall α. Num α => s -> α
forall s α. (PositionalSystem s, Num α) => s -> α
radixIn s
s
        digit :: μ α
digit   = s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE npBounded  (Monad μ, CharParsing μ)  Decimal  μ Int #-}
{-# SPECIALIZE npBounded  (Monad μ, CharParsing μ)  Decimal  μ Int8 #-}
{-# SPECIALIZE npBounded  (Monad μ, CharParsing μ)  Decimal  μ Int16 #-}
{-# SPECIALIZE npBounded  (Monad μ, CharParsing μ)  Decimal  μ Int32 #-}
{-# SPECIALIZE npBounded  (Monad μ, CharParsing μ)  Decimal  μ Int64 #-}
{-# SPECIALIZE npBounded  (Monad μ, CharParsing μ)  Decimal  μ Word #-}
{-# SPECIALIZE npBounded  (Monad μ, CharParsing μ)  Decimal  μ Word8 #-}
{-# SPECIALIZE npBounded  (Monad μ, CharParsing μ)  Decimal  μ Word16 #-}
{-# SPECIALIZE npBounded  (Monad μ, CharParsing μ)  Decimal  μ Word32 #-}
{-# SPECIALIZE npBounded  (Monad μ, CharParsing μ)  Decimal  μ Word64 #-}
{-# SPECIALIZE npBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  Binary  μ α #-}
{-# SPECIALIZE npBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  Octal  μ α #-}
{-# SPECIALIZE npBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  Decimal  μ α #-}
{-# SPECIALIZE npBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  Hexadecimal  μ α #-}
{-# SPECIALIZE npBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  LowHex  μ α #-}
{-# SPECIALIZE npBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  UpHex  μ α #-}

-- | Parse a non-positive number written in the specified positional
--   numeral system, failing on overflow. Leading zeroes are not allowed.
npcBounded  (PositionalSystem s, Ord α, Bounded α, Integral α,
              Monad μ, CharParsing μ)
            s  μ α
npcBounded :: forall s α (μ :: * -> *).
(PositionalSystem s, Ord α, Bounded α, Integral α, Monad μ,
 CharParsing μ) =>
s -> μ α
npcBounded s
s = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits") (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ s -> μ Int
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s μ Int -> (Int -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                 Int
0  μ Char -> μ (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ((Char -> Bool) -> μ Char
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m Char
PC.satisfy ((Char -> Bool) -> μ Char) -> (Char -> Bool) -> μ Char
forall a b. (a -> b) -> a -> b
$ s -> Char -> Bool
forall s. PositionalSystem s => s -> Char -> Bool
isDigitIn s
s) μ (Maybe Char) -> (Maybe Char -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                       Just Char
_   String -> μ α
forall a. String -> μ a
forall (m :: * -> *) a. Parsing m => String -> m a
PC.unexpected String
"leading zero"
                       Maybe Char
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
0
                 Int
n  α -> μ α
go (α -> μ α) -> α -> μ α
forall a b. (a -> b) -> a -> b
$ Int -> α
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> α) -> Int -> α
forall a b. (a -> b) -> a -> b
$ Int -> Int
forall a. Num a => a -> a
negate (Int
n  Int)
  where (α
q, α
r1) = α -> α -> (α, α)
forall a. Integral a => a -> a -> (a, a)
quotRem α
forall a. Bounded a => a
minBound α
radix
        !r :: α
r      = α -> α
forall a. Num a => a -> a
negate α
r1
        go :: α -> μ α
go !α
n   = μ α -> μ (Maybe α)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ α
digit μ (Maybe α) -> (Maybe α -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                    Just α
d   if α
n α -> α -> Bool
forall a. Ord a => a -> a -> Bool
> α
q Bool -> Bool -> Bool
|| (α
n α -> α -> Bool
forall a. Eq a => a -> a -> Bool
== α
q Bool -> Bool -> Bool
&& α
d α -> α -> Bool
forall a. Ord a => a -> a -> Bool
<= α
r)
                              then α -> μ α
go (α
n α -> α -> α
forall a. Num a => a -> a -> a
* α
radix α -> α -> α
forall a. Num a => a -> a -> a
- α
d)
                              else String -> μ α
forall a. String -> μ a
forall (m :: * -> *) a. Parsing m => String -> m a
PC.unexpected String
"out of bounds"
                    Maybe α
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
n
        radix :: α
radix   = s -> α
forall α. Num α => s -> α
forall s α. (PositionalSystem s, Num α) => s -> α
radixIn s
s
        digit :: μ α
digit   = s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE npcBounded  (Monad μ, CharParsing μ)  Decimal  μ Int #-}
{-# SPECIALIZE npcBounded  (Monad μ, CharParsing μ)  Decimal  μ Int8 #-}
{-# SPECIALIZE npcBounded  (Monad μ, CharParsing μ)  Decimal  μ Int16 #-}
{-# SPECIALIZE npcBounded  (Monad μ, CharParsing μ)  Decimal  μ Int32 #-}
{-# SPECIALIZE npcBounded  (Monad μ, CharParsing μ)  Decimal  μ Int64 #-}
{-# SPECIALIZE npcBounded  (Monad μ, CharParsing μ)  Decimal  μ Word #-}
{-# SPECIALIZE npcBounded  (Monad μ, CharParsing μ)  Decimal  μ Word8 #-}
{-# SPECIALIZE npcBounded  (Monad μ, CharParsing μ)  Decimal  μ Word16 #-}
{-# SPECIALIZE npcBounded  (Monad μ, CharParsing μ)  Decimal  μ Word32 #-}
{-# SPECIALIZE npcBounded  (Monad μ, CharParsing μ)  Decimal  μ Word64 #-}
{-# SPECIALIZE npcBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  Binary  μ α #-}
{-# SPECIALIZE npcBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  Octal  μ α #-}
{-# SPECIALIZE npcBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  Decimal  μ α #-}
{-# SPECIALIZE npcBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  Hexadecimal  μ α #-}
{-# SPECIALIZE npcBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  LowHex  μ α #-}
{-# SPECIALIZE npcBounded  (Bounded α, Integral α, Monad μ, CharParsing μ)  UpHex  μ α #-}

-- | Parse a non-positive two\'s complement binary number written in
--   the specified positional numeral system.
npBits  (BitSystem s, Num α, Bits α, Monad μ, CharParsing μ)  s  μ α
npBits :: forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
s -> μ α
npBits s
s = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits") (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ do
             Int
r  μ Int
digit
             α -> μ α
forall {b}. (Bits b, Num b) => b -> μ b
go (α -> μ α) -> α -> μ α
forall a b. (a -> b) -> a -> b
$ Int -> α
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> α) -> Int -> α
forall a b. (a -> b) -> a -> b
$ Int -> Int
forall a. Num a => a -> a
negate (Int
r  Int)
  where go :: b -> μ b
go !b
r     = μ Int -> μ (Maybe Int)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ Int
digit μ (Maybe Int) -> (Maybe Int -> μ b) -> μ b
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                      Just Int
d1  b -> μ b
go ((b
r b -> Int -> b
forall a. Bits a => a -> Int -> a
`shiftL` Int
digitBits) b -> b -> b
forall a. Num a => a -> a -> a
+ b
d)
                        where !d :: b
d = Int -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> b) -> Int -> b
forall a b. (a -> b) -> a -> b
$ Int -> Int
forall a. Num a => a -> a
negate Int
d1
                      Maybe Int
Nothing  b -> μ b
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return b
r
        digitBits :: Int
digitBits = s -> Int
forall s. BitSystem s => s -> Int
digitBitsIn s
s
        digit :: μ Int
digit     = s -> μ Int
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Binary  μ Int #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Binary  μ Int8 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Binary  μ Int16 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Binary  μ Int32 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Binary  μ Int64 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Binary  μ Word #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Binary  μ Word8 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Binary  μ Word16 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Binary  μ Word32 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Binary  μ Word64 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Octal  μ Int #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Octal  μ Int8 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Octal  μ Int16 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Octal  μ Int32 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Octal  μ Int64 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Octal  μ Word #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Octal  μ Word8 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Octal  μ Word16 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Octal  μ Word32 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Octal  μ Word64 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int8 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int16 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int32 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int64 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word8 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word16 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word32 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word64 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  LowHex  μ Int #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  LowHex  μ Int8 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  LowHex  μ Int16 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  LowHex  μ Int32 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  LowHex  μ Int64 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  LowHex  μ Word #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  LowHex  μ Word8 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  LowHex  μ Word16 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  LowHex  μ Word32 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  LowHex  μ Word64 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  UpHex  μ Int #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  UpHex  μ Int8 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  UpHex  μ Int16 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  UpHex  μ Int32 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  UpHex  μ Int64 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  UpHex  μ Word #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  UpHex  μ Word8 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  UpHex  μ Word16 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  UpHex  μ Word32 #-}
{-# SPECIALIZE npBits  (Monad μ, CharParsing μ)  UpHex  μ Word64 #-}
{-# SPECIALIZE npBits  (Num α, Bits α, Monad μ, CharParsing μ)  Binary  μ α #-}
{-# SPECIALIZE npBits  (Num α, Bits α, Monad μ, CharParsing μ)  Octal  μ α #-}
{-# SPECIALIZE npBits  (Num α, Bits α, Monad μ, CharParsing μ)  Hexadecimal  μ α #-}
{-# SPECIALIZE npBits  (Num α, Bits α, Monad μ, CharParsing μ)  LowHex  μ α #-}
{-# SPECIALIZE npBits  (Num α, Bits α, Monad μ, CharParsing μ)  UpHex  μ α #-}

-- | Parse a non-positive two\'s complement binary number written in
--   the specified positional numeral system (up to /n/ digits).
npBitsUpTo  (BitSystem s, Num α, Bits α, Monad μ, CharParsing μ)
            s  Int  μ α
npBitsUpTo :: forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
s -> Int -> μ α
npBitsUpTo s
_ Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = μ α
forall a. μ a
forall (f :: * -> *) a. Alternative f => f a
empty
npBitsUpTo s
s Int
n = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits") (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ do
                   Int
r  μ Int
digit
                   Int -> α -> μ α
forall {t} {t}. (Bits t, Num t, Num t, Eq t) => t -> t -> μ t
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (α -> μ α) -> α -> μ α
forall a b. (a -> b) -> a -> b
$ Int -> α
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> α) -> Int -> α
forall a b. (a -> b) -> a -> b
$ Int -> Int
forall a. Num a => a -> a
negate (Int
r  Int)
  where go :: t -> t -> μ t
go t
0 !t
r   = μ Char -> μ (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ((Char -> Bool) -> μ Char
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m Char
PC.satisfy ((Char -> Bool) -> μ Char) -> (Char -> Bool) -> μ Char
forall a b. (a -> b) -> a -> b
$ s -> Char -> Bool
forall s. PositionalSystem s => s -> Char -> Bool
isDigitIn s
s) μ (Maybe Char) -> (Maybe Char -> μ t) -> μ t
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                      Just Char
_   Int -> μ t
forall (μ :: * -> *) α. CharParsing μ => Int -> μ α
moreThan Int
n
                      Maybe Char
Nothing  t -> μ t
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return t
r
        go t
l !t
r   = μ Int -> μ (Maybe Int)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ Int
digit μ (Maybe Int) -> (Maybe Int -> μ t) -> μ t
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                      Just Int
d1  t -> t -> μ t
go (t
l t -> t -> t
forall a. Num a => a -> a -> a
- t
1) ((t
r t -> Int -> t
forall a. Bits a => a -> Int -> a
`shiftL` Int
digitBits) t -> t -> t
forall a. Num a => a -> a -> a
+ t
d)
                        where !d :: t
d = Int -> t
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> t) -> Int -> t
forall a b. (a -> b) -> a -> b
$ Int -> Int
forall a. Num a => a -> a
negate Int
d1
                      Maybe Int
Nothing  t -> μ t
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return t
r
        digitBits :: Int
digitBits = s -> Int
forall s. BitSystem s => s -> Int
digitBitsIn s
s
        digit :: μ Int
digit     = s -> μ Int
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Int #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Int8 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Int16 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Int32 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Int64 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Word #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Word8 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Word16 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Word32 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Word64 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Int #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Int8 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Int16 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Int32 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Int64 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Word #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Word8 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Word16 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Word32 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Word64 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Int #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Int8 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Int16 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Int32 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Int64 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Word #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Word8 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Word16 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Word32 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Word64 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Int #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Int8 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Int16 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Int32 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Int64 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Word #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Word8 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Word16 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Word32 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Word64 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Int #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Int8 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Int16 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Int32 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Int64 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Word #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Word8 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Word16 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Word32 #-}
{-# SPECIALIZE npBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Word64 #-}
{-# SPECIALIZE npBitsUpTo  (Num α, Bits α, Monad μ, CharParsing μ)  Binary  Int  μ α #-}
{-# SPECIALIZE npBitsUpTo  (Num α, Bits α, Monad μ, CharParsing μ)  Octal  Int  μ α #-}
{-# SPECIALIZE npBitsUpTo  (Num α, Bits α, Monad μ, CharParsing μ)  Hexadecimal  Int  μ α #-}
{-# SPECIALIZE npBitsUpTo  (Num α, Bits α, Monad μ, CharParsing μ)  LowHex  Int  μ α #-}
{-# SPECIALIZE npBitsUpTo  (Num α, Bits α, Monad μ, CharParsing μ)  UpHex  Int  μ α #-}

-- | Parse a non-positive two\'s complement binary number written in
--   the specified positional numeral system. Leading zeroes are not allowed.
npcBits  (BitSystem s, Num α, Bits α, Monad μ, CharParsing μ)  s  μ α
npcBits :: forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
s -> μ α
npcBits s
s = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits") (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ μ Int
digit μ Int -> (Int -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
              Int
0  μ Char -> μ (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ((Char -> Bool) -> μ Char
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m Char
PC.satisfy ((Char -> Bool) -> μ Char) -> (Char -> Bool) -> μ Char
forall a b. (a -> b) -> a -> b
$ s -> Char -> Bool
forall s. PositionalSystem s => s -> Char -> Bool
isDigitIn s
s) μ (Maybe Char) -> (Maybe Char -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                    Just Char
_   String -> μ α
forall a. String -> μ a
forall (m :: * -> *) a. Parsing m => String -> m a
PC.unexpected String
"leading zero"
                    Maybe Char
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
0
              Int
r  α -> μ α
forall {b}. (Bits b, Num b) => b -> μ b
go (α -> μ α) -> α -> μ α
forall a b. (a -> b) -> a -> b
$ Int -> α
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> α) -> Int -> α
forall a b. (a -> b) -> a -> b
$ Int -> Int
forall a. Num a => a -> a
negate (Int
r  Int)
  where go :: b -> μ b
go !b
r     = μ Int -> μ (Maybe Int)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ Int
digit μ (Maybe Int) -> (Maybe Int -> μ b) -> μ b
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                      Just Int
d1  b -> μ b
go ((b
r b -> Int -> b
forall a. Bits a => a -> Int -> a
`shiftL` Int
digitBits) b -> b -> b
forall a. Num a => a -> a -> a
+ b
d)
                        where !d :: b
d = Int -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> b) -> Int -> b
forall a b. (a -> b) -> a -> b
$ Int -> Int
forall a. Num a => a -> a
negate Int
d1
                      Maybe Int
Nothing  b -> μ b
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return b
r
        digitBits :: Int
digitBits = s -> Int
forall s. BitSystem s => s -> Int
digitBitsIn s
s
        digit :: μ Int
digit     = s -> μ Int
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Binary  μ Int #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Binary  μ Int8 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Binary  μ Int16 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Binary  μ Int32 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Binary  μ Int64 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Binary  μ Word #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Binary  μ Word8 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Binary  μ Word16 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Binary  μ Word32 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Binary  μ Word64 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Octal  μ Int #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Octal  μ Int8 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Octal  μ Int16 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Octal  μ Int32 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Octal  μ Int64 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Octal  μ Word #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Octal  μ Word8 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Octal  μ Word16 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Octal  μ Word32 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Octal  μ Word64 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int8 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int16 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int32 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int64 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word8 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word16 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word32 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word64 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  LowHex  μ Int #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  LowHex  μ Int8 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  LowHex  μ Int16 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  LowHex  μ Int32 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  LowHex  μ Int64 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  LowHex  μ Word #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  LowHex  μ Word8 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  LowHex  μ Word16 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  LowHex  μ Word32 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  LowHex  μ Word64 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  UpHex  μ Int #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  UpHex  μ Int8 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  UpHex  μ Int16 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  UpHex  μ Int32 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  UpHex  μ Int64 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  UpHex  μ Word #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  UpHex  μ Word8 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  UpHex  μ Word16 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  UpHex  μ Word32 #-}
{-# SPECIALIZE npcBits  (Monad μ, CharParsing μ)  UpHex  μ Word64 #-}
{-# SPECIALIZE npcBits  (Num α, Bits α, Monad μ, CharParsing μ)  Binary  μ α #-}
{-# SPECIALIZE npcBits  (Num α, Bits α, Monad μ, CharParsing μ)  Octal  μ α #-}
{-# SPECIALIZE npcBits  (Num α, Bits α, Monad μ, CharParsing μ)  Hexadecimal  μ α #-}
{-# SPECIALIZE npcBits  (Num α, Bits α, Monad μ, CharParsing μ)  LowHex  μ α #-}
{-# SPECIALIZE npcBits  (Num α, Bits α, Monad μ, CharParsing μ)  UpHex  μ α #-}

-- | Parse a non-positive two\'s complement binary number written in
--   the specified positional numeral system (up to /n/ digits).
--   Leading zeroes are not allowed.
npcBitsUpTo  (BitSystem s, Num α, Bits α, Monad μ, CharParsing μ)
             s  Int  μ α
npcBitsUpTo :: forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
s -> Int -> μ α
npcBitsUpTo s
_ Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = μ α
forall a. μ a
forall (f :: * -> *) a. Alternative f => f a
empty
npcBitsUpTo s
s Int
n = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits") (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ μ Int
digit μ Int -> (Int -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                    Int
0  μ Char -> μ (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ((Char -> Bool) -> μ Char
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m Char
PC.satisfy ((Char -> Bool) -> μ Char) -> (Char -> Bool) -> μ Char
forall a b. (a -> b) -> a -> b
$ s -> Char -> Bool
forall s. PositionalSystem s => s -> Char -> Bool
isDigitIn s
s) μ (Maybe Char) -> (Maybe Char -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                          Just Char
_   String -> μ α
forall a. String -> μ a
forall (m :: * -> *) a. Parsing m => String -> m a
PC.unexpected String
"leading zero"
                          Maybe Char
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
0
                    Int
r  Int -> α -> μ α
forall {t} {t}. (Bits t, Num t, Num t, Eq t) => t -> t -> μ t
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (α -> μ α) -> α -> μ α
forall a b. (a -> b) -> a -> b
$ Int -> α
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> α) -> Int -> α
forall a b. (a -> b) -> a -> b
$ Int -> Int
forall a. Num a => a -> a
negate (Int
r  Int)
  where go :: t -> t -> μ t
go t
0 !t
r   = μ Char -> μ (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ((Char -> Bool) -> μ Char
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m Char
PC.satisfy ((Char -> Bool) -> μ Char) -> (Char -> Bool) -> μ Char
forall a b. (a -> b) -> a -> b
$ s -> Char -> Bool
forall s. PositionalSystem s => s -> Char -> Bool
isDigitIn s
s) μ (Maybe Char) -> (Maybe Char -> μ t) -> μ t
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                      Just Char
_   Int -> μ t
forall (μ :: * -> *) α. CharParsing μ => Int -> μ α
moreThan Int
n
                      Maybe Char
Nothing  t -> μ t
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return t
r
        go t
l !t
r   = μ Int -> μ (Maybe Int)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ Int
digit μ (Maybe Int) -> (Maybe Int -> μ t) -> μ t
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                      Just Int
d1  t -> t -> μ t
go (t
l t -> t -> t
forall a. Num a => a -> a -> a
- t
1) ((t
r t -> Int -> t
forall a. Bits a => a -> Int -> a
`shiftL` Int
digitBits) t -> t -> t
forall a. Num a => a -> a -> a
+ t
d)
                        where !d :: t
d = Int -> t
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> t) -> Int -> t
forall a b. (a -> b) -> a -> b
$ Int -> Int
forall a. Num a => a -> a
negate Int
d1
                      Maybe Int
Nothing  t -> μ t
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return t
r
        digitBits :: Int
digitBits = s -> Int
forall s. BitSystem s => s -> Int
digitBitsIn s
s
        digit :: μ Int
digit     = s -> μ Int
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Int #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Int8 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Int16 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Int32 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Int64 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Word #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Word8 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Word16 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Word32 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Binary  Int  μ Word64 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Int #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Int8 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Int16 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Int32 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Int64 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Word #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Word8 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Word16 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Word32 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Octal  Int  μ Word64 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Int #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Int8 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Int16 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Int32 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Int64 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Word #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Word8 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Word16 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Word32 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  Hexadecimal  Int  μ Word64 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Int #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Int8 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Int16 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Int32 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Int64 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Word #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Word8 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Word16 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Word32 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  LowHex  Int  μ Word64 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Int #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Int8 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Int16 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Int32 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Int64 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Word #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Word8 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Word16 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Word32 #-}
{-# SPECIALIZE npcBitsUpTo  (Monad μ, CharParsing μ)  UpHex  Int  μ Word64 #-}
{-# SPECIALIZE npcBitsUpTo  (Num α, Bits α, Monad μ, CharParsing μ)  Binary  Int  μ α #-}
{-# SPECIALIZE npcBitsUpTo  (Num α, Bits α, Monad μ, CharParsing μ)  Octal  Int  μ α #-}
{-# SPECIALIZE npcBitsUpTo  (Num α, Bits α, Monad μ, CharParsing μ)  Hexadecimal  Int  μ α #-}
{-# SPECIALIZE npcBitsUpTo  (Num α, Bits α, Monad μ, CharParsing μ)  LowHex  Int  μ α #-}
{-# SPECIALIZE npcBitsUpTo  (Num α, Bits α, Monad μ, CharParsing μ)  UpHex  Int  μ α #-}

-- | Parse a non-positive two\'s complement binary number written in
--   the specified positional numeral system, failing on overflow.
npbBits   s μ α
        . (BitSystem s, Ord α, Bounded α, Num α, Bits α,
           Monad μ, CharParsing μ)
         s  μ α
npbBits :: forall s (μ :: * -> *) α.
(BitSystem s, Ord α, Bounded α, Num α, Bits α, Monad μ,
 CharParsing μ) =>
s -> μ α
npbBits s
s = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits") (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ do
              Int
n  μ Int
digit
              α -> μ α
go (α -> μ α) -> α -> μ α
forall a b. (a -> b) -> a -> b
$ Int -> α
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> α) -> Int -> α
forall a b. (a -> b) -> a -> b
$ Int -> Int
forall a. Num a => a -> a
negate (Int
n  Int)
  where q1 :: α
q1 = α
forall a. Bounded a => a
minBound α -> Int -> α
forall a. Bits a => a -> Int -> a
`shiftR` Int
digitBits
        r :: Int
r  = Int -> Int
forall a. Num a => a -> a
negate (s -> α -> Int
forall α. Bits α => s -> α -> Int
forall s α. (BitSystem s, Bits α) => s -> α -> Int
lastDigitIn s
s (α
forall a. Bounded a => a
minBound  α)) Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. s -> Int
forall α. Num α => s -> α
forall s α. (BitSystem s, Num α) => s -> α
digitMaskIn s
s
        q :: α
q  = if Int
r Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 then α
q1 else α
q1 α -> α -> α
forall a. Num a => a -> a -> a
+ α
1
        go :: α -> μ α
go !α
n     = μ Int -> μ (Maybe Int)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ Int
digit μ (Maybe Int) -> (Maybe Int -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                      Just Int
d1  if α
n α -> α -> Bool
forall a. Ord a => a -> a -> Bool
> α
q Bool -> Bool -> Bool
|| (α
n α -> α -> Bool
forall a. Eq a => a -> a -> Bool
== α
q Bool -> Bool -> Bool
&& Int
d1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
r)
                                then α -> μ α
go ((α
n α -> Int -> α
forall a. Bits a => a -> Int -> a
`shiftL` Int
digitBits) α -> α -> α
forall a. Num a => a -> a -> a
+ α
d)
                                else String -> μ α
forall a. String -> μ a
forall (m :: * -> *) a. Parsing m => String -> m a
PC.unexpected String
"out of bounds"
                        where !d :: α
d = Int -> α
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> α) -> Int -> α
forall a b. (a -> b) -> a -> b
$ Int -> Int
forall a. Num a => a -> a
negate Int
d1
                      Maybe Int
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
n
        digitBits :: Int
digitBits = s -> Int
forall s. BitSystem s => s -> Int
digitBitsIn s
s
        digit :: μ Int
digit     = s -> μ Int
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Binary  μ Int #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Binary  μ Int8 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Binary  μ Int16 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Binary  μ Int32 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Binary  μ Int64 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Binary  μ Word #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Binary  μ Word8 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Binary  μ Word16 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Binary  μ Word32 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Binary  μ Word64 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Octal  μ Int #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Octal  μ Int8 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Octal  μ Int16 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Octal  μ Int32 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Octal  μ Int64 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Octal  μ Word #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Octal  μ Word8 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Octal  μ Word16 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Octal  μ Word32 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Octal  μ Word64 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int8 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int16 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int32 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int64 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word8 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word16 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word32 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word64 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  LowHex  μ Int #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  LowHex  μ Int8 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  LowHex  μ Int16 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  LowHex  μ Int32 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  LowHex  μ Int64 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  LowHex  μ Word #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  LowHex  μ Word8 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  LowHex  μ Word16 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  LowHex  μ Word32 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  LowHex  μ Word64 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  UpHex  μ Int #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  UpHex  μ Int8 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  UpHex  μ Int16 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  UpHex  μ Int32 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  UpHex  μ Int64 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  UpHex  μ Word #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  UpHex  μ Word8 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  UpHex  μ Word16 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  UpHex  μ Word32 #-}
{-# SPECIALIZE npbBits  (Monad μ, CharParsing μ)  UpHex  μ Word64 #-}
{-# SPECIALIZE npbBits  (Ord α, Bounded α, Num α, Bits α, Monad μ, CharParsing μ)  Binary  μ α #-}
{-# SPECIALIZE npbBits  (Ord α, Bounded α, Num α, Bits α, Monad μ, CharParsing μ)  Octal  μ α #-}
{-# SPECIALIZE npbBits  (Ord α, Bounded α, Num α, Bits α, Monad μ, CharParsing μ)  Hexadecimal  μ α #-}
{-# SPECIALIZE npbBits  (Ord α, Bounded α, Num α, Bits α, Monad μ, CharParsing μ)  LowHex  μ α #-}
{-# SPECIALIZE npbBits  (Ord α, Bounded α, Num α, Bits α, Monad μ, CharParsing μ)  UpHex  μ α #-}

-- | Parse a non-positive two\'s complement binary number written in
--   the specified positional numeral system, failing on overflow.
--   Leading zeroes are not allowed.
npcbBits   s μ α
         . (BitSystem s, Ord α, Bounded α, Num α, Bits α,
            Monad μ, CharParsing μ)
         s  μ α
npcbBits :: forall s (μ :: * -> *) α.
(BitSystem s, Ord α, Bounded α, Num α, Bits α, Monad μ,
 CharParsing μ) =>
s -> μ α
npcbBits s
s = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" digits") (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ μ Int
digit μ Int -> (Int -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
               Int
0  μ Char -> μ (Maybe Char)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional ((Char -> Bool) -> μ Char
forall (m :: * -> *). CharParsing m => (Char -> Bool) -> m Char
PC.satisfy ((Char -> Bool) -> μ Char) -> (Char -> Bool) -> μ Char
forall a b. (a -> b) -> a -> b
$ s -> Char -> Bool
forall s. PositionalSystem s => s -> Char -> Bool
isDigitIn s
s) μ (Maybe Char) -> (Maybe Char -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                     Just Char
_   String -> μ α
forall a. String -> μ a
forall (m :: * -> *) a. Parsing m => String -> m a
PC.unexpected String
"leading zero"
                     Maybe Char
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
0
               Int
n  α -> μ α
go (α -> μ α) -> α -> μ α
forall a b. (a -> b) -> a -> b
$ Int -> α
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> α) -> Int -> α
forall a b. (a -> b) -> a -> b
$ Int -> Int
forall a. Num a => a -> a
negate (Int
n  Int)
  where q1 :: α
q1 = α
forall a. Bounded a => a
minBound α -> Int -> α
forall a. Bits a => a -> Int -> a
`shiftR` Int
digitBits
        r :: Int
r  = Int -> Int
forall a. Num a => a -> a
negate (s -> α -> Int
forall α. Bits α => s -> α -> Int
forall s α. (BitSystem s, Bits α) => s -> α -> Int
lastDigitIn s
s (α
forall a. Bounded a => a
minBound  α)) Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. s -> Int
forall α. Num α => s -> α
forall s α. (BitSystem s, Num α) => s -> α
digitMaskIn s
s
        q :: α
q  = if Int
r Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 then α
q1 else α
q1 α -> α -> α
forall a. Num a => a -> a -> a
+ α
1
        go :: α -> μ α
go !α
n     = μ Int -> μ (Maybe Int)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional μ Int
digit μ (Maybe Int) -> (Maybe Int -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
                      Just Int
d1  if α
n α -> α -> Bool
forall a. Ord a => a -> a -> Bool
> α
q Bool -> Bool -> Bool
|| (α
n α -> α -> Bool
forall a. Eq a => a -> a -> Bool
== α
q Bool -> Bool -> Bool
&& Int
d1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
r)
                                then α -> μ α
go ((α
n α -> Int -> α
forall a. Bits a => a -> Int -> a
`shiftL` Int
digitBits) α -> α -> α
forall a. Num a => a -> a -> a
+ α
d)
                                else String -> μ α
forall a. String -> μ a
forall (m :: * -> *) a. Parsing m => String -> m a
PC.unexpected String
"out of bounds"
                        where !d :: α
d = Int -> α
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> α) -> Int -> α
forall a b. (a -> b) -> a -> b
$ Int -> Int
forall a. Num a => a -> a
negate Int
d1
                      Maybe Int
Nothing  α -> μ α
forall a. a -> μ a
forall (m :: * -> *) a. Monad m => a -> m a
return α
n
        digitBits :: Int
digitBits = s -> Int
forall s. BitSystem s => s -> Int
digitBitsIn s
s
        digit :: μ Int
digit     = s -> μ Int
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, CharParsing μ) =>
s -> μ α
digitIn s
s
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Binary  μ Int #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Binary  μ Int8 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Binary  μ Int16 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Binary  μ Int32 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Binary  μ Int64 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Binary  μ Word #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Binary  μ Word8 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Binary  μ Word16 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Binary  μ Word32 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Binary  μ Word64 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Octal  μ Int #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Octal  μ Int8 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Octal  μ Int16 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Octal  μ Int32 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Octal  μ Int64 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Octal  μ Word #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Octal  μ Word8 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Octal  μ Word16 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Octal  μ Word32 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Octal  μ Word64 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int8 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int16 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int32 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Int64 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word8 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word16 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word32 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  Hexadecimal  μ Word64 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  LowHex  μ Int #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  LowHex  μ Int8 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  LowHex  μ Int16 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  LowHex  μ Int32 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  LowHex  μ Int64 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  LowHex  μ Word #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  LowHex  μ Word8 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  LowHex  μ Word16 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  LowHex  μ Word32 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  LowHex  μ Word64 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  UpHex  μ Int #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  UpHex  μ Int8 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  UpHex  μ Int16 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  UpHex  μ Int32 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  UpHex  μ Int64 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  UpHex  μ Word #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  UpHex  μ Word8 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  UpHex  μ Word16 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  UpHex  μ Word32 #-}
{-# SPECIALIZE npcbBits  (Monad μ, CharParsing μ)  UpHex  μ Word64 #-}
{-# SPECIALIZE npcbBits  (Ord α, Bounded α, Num α, Bits α, Monad μ, CharParsing μ)  Binary  μ α #-}
{-# SPECIALIZE npcbBits  (Ord α, Bounded α, Num α, Bits α, Monad μ, CharParsing μ)  Octal  μ α #-}
{-# SPECIALIZE npcbBits  (Ord α, Bounded α, Num α, Bits α, Monad μ, CharParsing μ)  Hexadecimal  μ α #-}
{-# SPECIALIZE npcbBits  (Ord α, Bounded α, Num α, Bits α, Monad μ, CharParsing μ)  LowHex  μ α #-}
{-# SPECIALIZE npcbBits  (Ord α, Bounded α, Num α, Bits α, Monad μ, CharParsing μ)  UpHex  μ α #-}

-- | Sign of a number.
data Sign = NonNegative | NonPositive
            deriving (Typeable, Sign -> Sign -> Bool
(Sign -> Sign -> Bool) -> (Sign -> Sign -> Bool) -> Eq Sign
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Sign -> Sign -> Bool
== :: Sign -> Sign -> Bool
$c/= :: Sign -> Sign -> Bool
/= :: Sign -> Sign -> Bool
Eq, Int -> Sign -> String -> String
[Sign] -> String -> String
Sign -> String
(Int -> Sign -> String -> String)
-> (Sign -> String) -> ([Sign] -> String -> String) -> Show Sign
forall a.
(Int -> a -> String -> String)
-> (a -> String) -> ([a] -> String -> String) -> Show a
$cshowsPrec :: Int -> Sign -> String -> String
showsPrec :: Int -> Sign -> String -> String
$cshow :: Sign -> String
show :: Sign -> String
$cshowList :: [Sign] -> String -> String
showList :: [Sign] -> String -> String
Show, ReadPrec [Sign]
ReadPrec Sign
Int -> ReadS Sign
ReadS [Sign]
(Int -> ReadS Sign)
-> ReadS [Sign] -> ReadPrec Sign -> ReadPrec [Sign] -> Read Sign
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS Sign
readsPrec :: Int -> ReadS Sign
$creadList :: ReadS [Sign]
readList :: ReadS [Sign]
$creadPrec :: ReadPrec Sign
readPrec :: ReadPrec Sign
$creadListPrec :: ReadPrec [Sign]
readListPrec :: ReadPrec [Sign]
Read)

-- | Negate the supplied value if the sign is 'NonPositive' and return it
--   as it is otherwise.
applySign  Num α  Sign  α  α
applySign :: forall α. Num α => Sign -> α -> α
applySign Sign
NonNegative α
a = α
a
applySign Sign
NonPositive α
a = α -> α
forall a. Num a => a -> a
negate α
a

-- | Optional minus sign.
optMinus  CharParsing μ  μ Sign
optMinus :: forall (μ :: * -> *). CharParsing μ => μ Sign
optMinus  =  (Sign -> μ Sign
forall a. a -> μ a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sign
NonPositive μ Sign -> μ Char -> μ Sign
forall a b. μ a -> μ b -> μ a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Char -> μ Char
forall (m :: * -> *). CharParsing m => Char -> m Char
PC.char Char
'-') μ Sign -> μ Sign -> μ Sign
forall a. μ a -> μ a -> μ a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Sign -> μ Sign
forall a. a -> μ a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sign
NonNegative
         μ Sign -> String -> μ Sign
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> String
"optional minus sign"
{-# INLINE optMinus #-}

-- | Optional minus or plus sign.
optSign  CharParsing μ  μ Sign
optSign :: forall (μ :: * -> *). CharParsing μ => μ Sign
optSign  =  (Sign -> μ Sign
forall a. a -> μ a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sign
NonPositive μ Sign -> μ Char -> μ Sign
forall a b. μ a -> μ b -> μ a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Char -> μ Char
forall (m :: * -> *). CharParsing m => Char -> m Char
PC.char Char
'-')
        μ Sign -> μ Sign -> μ Sign
forall a. μ a -> μ a -> μ a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Sign -> μ Sign
forall a. a -> μ a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sign
NonNegative μ Sign -> μ Char -> μ Sign
forall a b. μ a -> μ b -> μ a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Char -> μ Char
forall (m :: * -> *). CharParsing m => Char -> m Char
PC.char Char
'+')
        μ Sign -> μ Sign -> μ Sign
forall a. μ a -> μ a -> μ a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Sign -> μ Sign
forall a. a -> μ a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Sign
NonNegative
        μ Sign -> String -> μ Sign
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> String
"optional sign"
{-# INLINABLE optSign #-}

-- | Parse a number written in the specified positional numeral system.
--   The supplied parser is used to determine the sign of the number.
number'  (PositionalSystem s, Num α, Monad μ, CharParsing μ)
         μ Sign  s  μ α
number' :: forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
μ Sign -> s -> μ α
number' μ Sign
neg s
s = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s) (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ μ Sign
neg μ Sign -> (Sign -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
  Sign
NonNegative  s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
s -> μ α
nonNegative s
s
  Sign
NonPositive  s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
s -> μ α
nonPositive s
s
{-# INLINE number' #-}

-- | A shorthand for 'number'' 'optMinus'.
number  (PositionalSystem s, Num α, Monad μ, CharParsing μ)  s  μ α
number :: forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
s -> μ α
number = μ Sign -> s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
μ Sign -> s -> μ α
number' μ Sign
forall (μ :: * -> *). CharParsing μ => μ Sign
optMinus
{-# INLINE number #-}

-- | Parse a number written in the specified positional numeral system.
--   The supplied parser is used to determine the sign of the number.
--   Leading zeroes are not allowed.
compact'  (PositionalSystem s, Num α, Monad μ, CharParsing μ)
          μ Sign  s  μ α
compact' :: forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
μ Sign -> s -> μ α
compact' μ Sign
neg s
s = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s) (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ μ Sign
neg μ Sign -> (Sign -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
  Sign
NonNegative  s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
s -> μ α
nnCompact s
s
  Sign
NonPositive  s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
s -> μ α
npCompact s
s
{-# INLINE compact' #-}

-- | A shorthand for 'compact'' 'optMinus'.
compact  (PositionalSystem s, Num α, Monad μ, CharParsing μ)  s  μ α
compact :: forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
s -> μ α
compact = μ Sign -> s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
μ Sign -> s -> μ α
compact' μ Sign
forall (μ :: * -> *). CharParsing μ => μ Sign
optMinus
{-# INLINE compact #-}

-- | Parse a number written in the specified positional numeral system
--   (up to /n/ digits). The supplied parser is used to determine the sign of
--   the number.
numberUpTo'  (PositionalSystem s, Num α, Monad μ, CharParsing μ)
             μ Sign  s  Int  μ α
numberUpTo' :: forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
μ Sign -> s -> Int -> μ α
numberUpTo' μ Sign
neg s
s Int
n = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s) (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ μ Sign
neg μ Sign -> (Sign -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
  Sign
NonNegative  s -> Int -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
s -> Int -> μ α
nnUpTo s
s Int
n
  Sign
NonPositive  s -> Int -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
s -> Int -> μ α
npUpTo s
s Int
n
{-# INLINE numberUpTo' #-}

-- | A shorthand for 'numberUpTo'' 'optMinus'.
numberUpTo  (PositionalSystem s, Num α, Monad μ, CharParsing μ)
            s  Int  μ α
numberUpTo :: forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
s -> Int -> μ α
numberUpTo = μ Sign -> s -> Int -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
μ Sign -> s -> Int -> μ α
numberUpTo' μ Sign
forall (μ :: * -> *). CharParsing μ => μ Sign
optMinus
{-# INLINE numberUpTo #-}

-- | Parse a number written in the specified positional numeral system
--   (up to /n/ digits). The supplied parser is used to determine the sign of
--   the number. Leading zeroes are not allowed.
compactUpTo'  (PositionalSystem s, Num α, Monad μ, CharParsing μ)
              μ Sign  s  Int  μ α
compactUpTo' :: forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
μ Sign -> s -> Int -> μ α
compactUpTo' μ Sign
neg s
s Int
n = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s) (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ μ Sign
neg μ Sign -> (Sign -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
  Sign
NonNegative  s -> Int -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
s -> Int -> μ α
nncUpTo s
s Int
n
  Sign
NonPositive  s -> Int -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
s -> Int -> μ α
npcUpTo s
s Int
n
{-# INLINE compactUpTo' #-}

-- | A shorthand for 'compactUpTo'' 'optMinus'.
compactUpTo  (PositionalSystem s, Num α, Monad μ, CharParsing μ)
            s  Int  μ α
compactUpTo :: forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
s -> Int -> μ α
compactUpTo = μ Sign -> s -> Int -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Num α, Monad μ, CharParsing μ) =>
μ Sign -> s -> Int -> μ α
compactUpTo' μ Sign
forall (μ :: * -> *). CharParsing μ => μ Sign
optMinus
{-# INLINE compactUpTo #-}

-- | Parse a number written in the specified positional numeral system,
--   failing on overflow. The supplied parser is used to determine the sign
--   of the number.
bounded'  (PositionalSystem s, Ord α, Bounded α, Integral α,
            Monad μ, CharParsing μ)
          μ Sign  s  μ α
bounded' :: forall s α (μ :: * -> *).
(PositionalSystem s, Ord α, Bounded α, Integral α, Monad μ,
 CharParsing μ) =>
μ Sign -> s -> μ α
bounded' μ Sign
neg s
s = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s) (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ μ Sign
neg μ Sign -> (Sign -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
  Sign
NonNegative  s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Ord α, Bounded α, Integral α, Monad μ,
 CharParsing μ) =>
s -> μ α
nnBounded s
s
  Sign
NonPositive  s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Ord α, Bounded α, Integral α, Monad μ,
 CharParsing μ) =>
s -> μ α
npBounded s
s
{-# INLINE bounded' #-}

-- | A shorthand for 'bounded'' 'optMinus'.
bounded  (PositionalSystem s, Ord α, Bounded α, Integral α,
           Monad μ, CharParsing μ)  s  μ α
bounded :: forall s α (μ :: * -> *).
(PositionalSystem s, Ord α, Bounded α, Integral α, Monad μ,
 CharParsing μ) =>
s -> μ α
bounded = μ Sign -> s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Ord α, Bounded α, Integral α, Monad μ,
 CharParsing μ) =>
μ Sign -> s -> μ α
bounded' μ Sign
forall (μ :: * -> *). CharParsing μ => μ Sign
optMinus
{-# INLINE bounded #-}

-- | Parse a number written in the specified positional numeral system,
--   failing on overflow. The supplied parser is used to determine the sign
--   of the number. Leading zeroes are not allowed.
cBounded'  (PositionalSystem s, Ord α, Bounded α, Integral α,
             Monad μ, CharParsing μ)
           μ Sign  s  μ α
cBounded' :: forall s α (μ :: * -> *).
(PositionalSystem s, Ord α, Bounded α, Integral α, Monad μ,
 CharParsing μ) =>
μ Sign -> s -> μ α
cBounded' μ Sign
neg s
s = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s) (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ μ Sign
neg μ Sign -> (Sign -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
  Sign
NonNegative  s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Ord α, Bounded α, Integral α, Monad μ,
 CharParsing μ) =>
s -> μ α
nncBounded s
s
  Sign
NonPositive  s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Ord α, Bounded α, Integral α, Monad μ,
 CharParsing μ) =>
s -> μ α
npcBounded s
s
{-# INLINE cBounded' #-}

-- | A shorthand for 'cBounded'' 'optMinus'.
cBounded  (PositionalSystem s, Ord α, Bounded α, Integral α,
            Monad μ, CharParsing μ)  s  μ α
cBounded :: forall s α (μ :: * -> *).
(PositionalSystem s, Ord α, Bounded α, Integral α, Monad μ,
 CharParsing μ) =>
s -> μ α
cBounded = μ Sign -> s -> μ α
forall s α (μ :: * -> *).
(PositionalSystem s, Ord α, Bounded α, Integral α, Monad μ,
 CharParsing μ) =>
μ Sign -> s -> μ α
cBounded' μ Sign
forall (μ :: * -> *). CharParsing μ => μ Sign
optMinus
{-# INLINE cBounded #-}

-- | Parse a (two\'s complement) binary number written in the specified
--   positional numeral system. The supplied parser is used to determine
--   the sign of the number.
bits'  (BitSystem s, Num α, Bits α, Monad μ, CharParsing μ)
       μ Sign  s  μ α
bits' :: forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
μ Sign -> s -> μ α
bits' μ Sign
neg s
s = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s) (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ μ Sign
neg μ Sign -> (Sign -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
  Sign
NonNegative  s -> μ α
forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
s -> μ α
nnBits s
s
  Sign
NonPositive  s -> μ α
forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
s -> μ α
npBits s
s
{-# INLINE bits' #-}

-- | A shorthand for 'bits'' 'optMinus'.
bits  (BitSystem s, Num α, Bits α, Monad μ, CharParsing μ)  s  μ α
bits :: forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
s -> μ α
bits = μ Sign -> s -> μ α
forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
μ Sign -> s -> μ α
bits' μ Sign
forall (μ :: * -> *). CharParsing μ => μ Sign
optMinus
{-# INLINE bits #-}

-- | Parse a (two\'s complement) binary number written in the specified
--   positional numeral system. The supplied parser is used to determine
--   the sign of the number. Leading zeroes are not allowed.
cBits'  (BitSystem s, Num α, Bits α, Monad μ, CharParsing μ)
        μ Sign  s  μ α
cBits' :: forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
μ Sign -> s -> μ α
cBits' μ Sign
neg s
s = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s) (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ μ Sign
neg μ Sign -> (Sign -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
  Sign
NonNegative  s -> μ α
forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
s -> μ α
nncBits s
s
  Sign
NonPositive  s -> μ α
forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
s -> μ α
npcBits s
s
{-# INLINE cBits' #-}

-- | A shorthand for 'cBits'' 'optMinus'.
cBits  (BitSystem s, Num α, Bits α, Monad μ, CharParsing μ)  s  μ α
cBits :: forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
s -> μ α
cBits = μ Sign -> s -> μ α
forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
μ Sign -> s -> μ α
cBits' μ Sign
forall (μ :: * -> *). CharParsing μ => μ Sign
optMinus
{-# INLINE cBits #-}

-- | Parse a (two\'s complement) binary number written in the specified
--   positional numeral system (up to /n/ digits). The supplied parser is
--   used to determine the sign of the number.
bitsUpTo'  (BitSystem s, Num α, Bits α, Monad μ, CharParsing μ)
           μ Sign  s  Int  μ α
bitsUpTo' :: forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
μ Sign -> s -> Int -> μ α
bitsUpTo' μ Sign
neg s
s Int
n = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s) (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ μ Sign
neg μ Sign -> (Sign -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
  Sign
NonNegative  s -> Int -> μ α
forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
s -> Int -> μ α
nnBitsUpTo s
s Int
n
  Sign
NonPositive  s -> Int -> μ α
forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
s -> Int -> μ α
npBitsUpTo s
s Int
n
{-# INLINE bitsUpTo' #-}

-- | A shorthand for 'bitsUpTo'' 'optMinus'.
bitsUpTo  (BitSystem s, Num α, Bits α, Monad μ, CharParsing μ)
          s  Int  μ α
bitsUpTo :: forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
s -> Int -> μ α
bitsUpTo = μ Sign -> s -> Int -> μ α
forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
μ Sign -> s -> Int -> μ α
bitsUpTo' μ Sign
forall (μ :: * -> *). CharParsing μ => μ Sign
optMinus
{-# INLINE bitsUpTo #-}

-- | Parse a (two\'s complement) binary number written in the specified
--   positional numeral system (up to /n/ digits). The supplied parser is
--   used to determine the sign of the number. Leading zeroes are not
--   allowed.
cBitsUpTo'  (BitSystem s, Num α, Bits α, Monad μ, CharParsing μ)
            μ Sign  s  Int  μ α
cBitsUpTo' :: forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
μ Sign -> s -> Int -> μ α
cBitsUpTo' μ Sign
neg s
s Int
n = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s) (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ μ Sign
neg μ Sign -> (Sign -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
  Sign
NonNegative  s -> Int -> μ α
forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
s -> Int -> μ α
nncBitsUpTo s
s Int
n
  Sign
NonPositive  s -> Int -> μ α
forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
s -> Int -> μ α
npcBitsUpTo s
s Int
n
{-# INLINE cBitsUpTo' #-}

-- | A shorthand for 'cBitsUpTo'' 'optMinus'.
cBitsUpTo  (BitSystem s, Num α, Bits α, Monad μ, CharParsing μ)
           s  Int  μ α
cBitsUpTo :: forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
s -> Int -> μ α
cBitsUpTo = μ Sign -> s -> Int -> μ α
forall s α (μ :: * -> *).
(BitSystem s, Num α, Bits α, Monad μ, CharParsing μ) =>
μ Sign -> s -> Int -> μ α
cBitsUpTo' μ Sign
forall (μ :: * -> *). CharParsing μ => μ Sign
optMinus
{-# INLINE cBitsUpTo #-}

-- | Parse a (two\'s complement) binary number written in the specified
--   positional numeral system, failing on overflow. The supplied parser is
--   used to determine the sign of the number.
bBits'  (BitSystem s, Ord α, Bounded α, Num α, Bits α,
          Monad μ, CharParsing μ)
        μ Sign  s  μ α
bBits' :: forall s α (μ :: * -> *).
(BitSystem s, Ord α, Bounded α, Num α, Bits α, Monad μ,
 CharParsing μ) =>
μ Sign -> s -> μ α
bBits' μ Sign
neg s
s = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s) (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ μ Sign
neg μ Sign -> (Sign -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
  Sign
NonNegative  s -> μ α
forall s α (μ :: * -> *).
(BitSystem s, Ord α, Bounded α, Num α, Bits α, Monad μ,
 CharParsing μ) =>
s -> μ α
nnbBits s
s
  Sign
NonPositive  s -> μ α
forall s (μ :: * -> *) α.
(BitSystem s, Ord α, Bounded α, Num α, Bits α, Monad μ,
 CharParsing μ) =>
s -> μ α
npbBits s
s
{-# INLINE bBits' #-}

-- | A shorthand for 'bBits'' 'optMinus'.
bBits  (BitSystem s, Ord α, Bounded α, Num α, Bits α,
         Monad μ, CharParsing μ)
       s  μ α
bBits :: forall s α (μ :: * -> *).
(BitSystem s, Ord α, Bounded α, Num α, Bits α, Monad μ,
 CharParsing μ) =>
s -> μ α
bBits = μ Sign -> s -> μ α
forall s α (μ :: * -> *).
(BitSystem s, Ord α, Bounded α, Num α, Bits α, Monad μ,
 CharParsing μ) =>
μ Sign -> s -> μ α
bBits' μ Sign
forall (μ :: * -> *). CharParsing μ => μ Sign
optMinus
{-# INLINE bBits #-}

-- | Parse a (two\'s complement) binary number written in the specified
--   positional numeral system, failing on overflow. The supplied parser is
--   used to determine the sign of the number. Leading zeroes are not
--   allowed.
cbBits'  (BitSystem s, Ord α, Bounded α, Num α, Bits α,
           Monad μ, CharParsing μ)
         μ Sign  s  μ α
cbBits' :: forall s α (μ :: * -> *).
(BitSystem s, Ord α, Bounded α, Num α, Bits α, Monad μ,
 CharParsing μ) =>
μ Sign -> s -> μ α
cbBits' μ Sign
neg s
s = (μ α -> String -> μ α
forall a. μ a -> String -> μ a
forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> s -> String
forall s. PositionalSystem s => s -> String
systemName s
s) (μ α -> μ α) -> μ α -> μ α
forall a b. (a -> b) -> a -> b
$ μ Sign
neg μ Sign -> (Sign -> μ α) -> μ α
forall a b. μ a -> (a -> μ b) -> μ b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
  Sign
NonNegative  s -> μ α
forall s α (μ :: * -> *).
(BitSystem s, Ord α, Bounded α, Num α, Bits α, Monad μ,
 CharParsing μ) =>
s -> μ α
nncbBits s
s
  Sign
NonPositive  s -> μ α
forall s (μ :: * -> *) α.
(BitSystem s, Ord α, Bounded α, Num α, Bits α, Monad μ,
 CharParsing μ) =>
s -> μ α
npcbBits s
s
{-# INLINE cbBits' #-}

-- | A shorthand for 'cbBits'' 'optMinus'.
cbBits  (BitSystem s, Ord α, Bounded α, Num α, Bits α,
          Monad μ, CharParsing μ)
        s  μ α
cbBits :: forall s α (μ :: * -> *).
(BitSystem s, Ord α, Bounded α, Num α, Bits α, Monad μ,
 CharParsing μ) =>
s -> μ α
cbBits = μ Sign -> s -> μ α
forall s α (μ :: * -> *).
(BitSystem s, Ord α, Bounded α, Num α, Bits α, Monad μ,
 CharParsing μ) =>
μ Sign -> s -> μ α
cbBits' μ Sign
forall (μ :: * -> *). CharParsing μ => μ Sign
optMinus
{-# INLINE cbBits #-}