{-# LANGUAGE NoImplicitPrelude #-}

module Data.Digit.Integral(
-- * Binary
  integralBinaryNoZero
, integralBinary
, integralBinDigits
, binDigitsIntegral
-- * Octal
, integralOctalNoZero
, integralOctal
, integralOctDigits
, octDigitsIntegral
-- * Decimal
, integralDecimal
, integralDecimalNoZero
, integralDecDigits
, decDigitsIntegral
-- * Hexadecimal
, integralHexadecimalNoZero
, integralHexadecimal
, integralHexDigits
, hexDigitsIntegral
-- * HEXADECIMAL
, integralHEXADECIMALNoZero
, integralHEXADECIMAL
, integralHEXDigits
, _HEXDigitsIntegral
-- * HeXaDeCiMaL
, integralHeXaDeCiMaLNoZero
, integralHeXaDeCiMaL
, _HeXDigitsIntegral
, mod10
, addDecDigit
, addDecDigit'
) where

import           Prelude                (Eq, Integral, error, fst, lookup,
                                         quotRem, (*), (+), (-), (==), (>=), mod, divMod)

import           Control.Applicative    (Applicative)
import           Control.Category       (id, (.))
import           Control.Lens           (APrism, Choice, Prism', Review,
                                         clonePrism, outside, prism', unto, over, _1,
                                         ( # ), (.~), (^?!), (^?))
import           Control.Lens.Extras    (is)

import           Data.Bool              (Bool, bool)
import           Data.Either            (Either (..), either)
import           Data.Foldable          (find, foldl')
import           Data.Function          (($),const)
import           Data.Functor           ((<$>))
import           Data.Int               (Int)
import           Data.List.NonEmpty     (NonEmpty)
import           Data.Maybe             (fromMaybe)
import           Data.Ord               ((>))

import           Data.Digit.Binary
import           Data.Digit.Decimal
import           Data.Digit.Hexadecimal.LowerCase
import           Data.Digit.Hexadecimal.UpperCase
import           Data.Digit.Hexadecimal.MixedCase
import           Data.Digit.Octal
import qualified Data.List.NonEmpty     as NonEmpty

-- $setup
-- >>> import Data.Digit

-- |
--
-- >>> 1 ^? integralBinaryNoZero
-- Just BinDigit1
--
-- >>> integralBinaryNoZero # BinDigit1 :: Integer
-- 1
integralBinaryNoZero ::
  (Integral a, BinaryNoZero d) =>
  Prism'
    a
    d
integralBinaryNoZero :: forall a d. (Integral a, BinaryNoZero d) => Prism' a d
integralBinaryNoZero =
  forall b (p :: * -> * -> *) (f :: * -> *) a.
(Eq b, Choice p, Applicative f) =>
(b, APrism a a () ())
-> [(b, APrism a a () ())] -> p a (f a) -> p b (f b)
associatePrism (a
1, forall d. D1 d => Prism' d ()
d1) []

-- |
--
-- >>> 0 ^? integralBinary :: Maybe BinDigit
-- Just BinDigit0
--
-- >>> integralBinary # BinDigit0 :: Integer
-- 0
integralBinary ::
  (Integral a, Binary d) =>
  Prism'
    a
    d
integralBinary :: forall a d. (Integral a, Binary d) => Prism' a d
integralBinary =
  forall b (p :: * -> * -> *) (f :: * -> *) a.
(Eq b, Choice p, Applicative f) =>
(b, APrism a a () ())
-> [(b, APrism a a () ())] -> p a (f a) -> p b (f b)
associatePrism (a
0, forall d. D0 d => Prism' d ()
d0) [(a
1, forall d. D1 d => Prism' d ()
d1)]

-- |
-- >>> integralBinDigits (4 :: Int)
-- Right (BinDigit1 :| [BinDigit0, BinDigit0])
--
-- >>> integralBinDigits (0 :: Int)
-- Right (BinDigit0 :| [])
--
-- >>> integralBinDigits (-1 :: Int)
-- Left (BinDigit0 :| [])
--
-- >>> integralBinDigits (-4 :: Int)
-- Left (BinDigit1 :| [BinDigit1])
integralBinDigits :: Integral a => a -> Either (NonEmpty BinDigit) (NonEmpty BinDigit)
integralBinDigits :: forall a.
Integral a =>
a -> Either (NonEmpty BinDigit) (NonEmpty BinDigit)
integralBinDigits a
n =
  if a
n forall a. Ord a => a -> a -> Bool
>= a
0
  then forall a b. b -> Either a b
Right forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. [a] -> NonEmpty a
NonEmpty.fromList forall a b. (a -> b) -> a -> b
$ forall {s} {a}. (Integral s, D0 a, D1 a) => s -> [a] -> [a]
go a
n []
  else forall a b. a -> Either a b
Left forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. [a] -> NonEmpty a
NonEmpty.fromList forall a b. (a -> b) -> a -> b
$ forall {s} {a}. (Integral s, D0 a, D1 a) => s -> [a] -> [a]
go (-a
n forall a. Num a => a -> a -> a
- a
1) []
  where
    go :: s -> [a] -> [a]
go s
k =
      let
        (s
q, s
r) = forall a. Integral a => a -> a -> (a, a)
quotRem s
k s
2
      in
        (if s
q forall a. Eq a => a -> a -> Bool
== s
0 then forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id else s -> [a] -> [a]
go s
q) forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ((s
r forall s a. HasCallStack => s -> Getting (Endo a) s a -> a
^?! forall a d. (Integral a, Binary d) => Prism' a d
integralBinary) forall a. a -> [a] -> [a]
:)

-- |
-- >>> binDigitsIntegral (Right (BinDigit1 :| [BinDigit0, BinDigit0])) :: Int
-- 4
--
-- >>> binDigitsIntegral (Right (BinDigit0 :| [])) :: Int
-- 0
--
-- >>> binDigitsIntegral (Left (BinDigit0 :| [])) :: Int
-- 0
--
-- >>> binDigitsIntegral (Left (BinDigit1 :| [BinDigit1])) :: Int
-- -3
binDigitsIntegral :: Integral a => Either (NonEmpty BinDigit) (NonEmpty BinDigit) -> a
binDigitsIntegral :: forall a.
Integral a =>
Either (NonEmpty BinDigit) (NonEmpty BinDigit) -> a
binDigitsIntegral = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\NonEmpty BinDigit
n -> -(NonEmpty BinDigit -> a
go NonEmpty BinDigit
n)) NonEmpty BinDigit -> a
go
  where
    go :: NonEmpty BinDigit -> a
go = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\a
b BinDigit
a -> (forall a d. (Integral a, Binary d) => Prism' a d
integralBinary forall t b. AReview t b -> b -> t
# BinDigit
a) forall a. Num a => a -> a -> a
+ a
2 forall a. Num a => a -> a -> a
* a
b) a
0

-- |
--
-- >>> 7 ^? integralOctalNoZero :: Maybe OctDigit
-- Just OctDigit7
--
-- >>> integralOctalNoZero # OctDigit7 :: Integer
-- 7
integralOctalNoZero ::
  (Integral a, OctalNoZero d) =>
  Prism'
    a
    d
integralOctalNoZero :: forall a d. (Integral a, OctalNoZero d) => Prism' a d
integralOctalNoZero =
  forall b (p :: * -> * -> *) (f :: * -> *) a.
(Eq b, Choice p, Applicative f) =>
(b, APrism a a () ())
-> [(b, APrism a a () ())] -> p a (f a) -> p b (f b)
associatePrism (a
1, forall d. D1 d => Prism' d ()
d1) [(a
2, forall d. D2 d => Prism' d ()
d2), (a
3, forall d. D3 d => Prism' d ()
d3), (a
4, forall d. D4 d => Prism' d ()
d4), (a
5, forall d. D5 d => Prism' d ()
d5), (a
6, forall d. D6 d => Prism' d ()
d6), (a
7, forall d. D7 d => Prism' d ()
d7)]

-- |
--
-- >>> 7 ^? integralOctal :: Maybe OctDigit
-- Just OctDigit7
--
-- >>> integralOctal # OctDigit7 :: Integer
-- 7
integralOctal ::
  (Integral a, Octal d) =>
  Prism'
    a
    d
integralOctal :: forall a d. (Integral a, Octal d) => Prism' a d
integralOctal =
  forall b (p :: * -> * -> *) (f :: * -> *) a.
(Eq b, Choice p, Applicative f) =>
(b, APrism a a () ())
-> [(b, APrism a a () ())] -> p a (f a) -> p b (f b)
associatePrism (a
0, forall d. D0 d => Prism' d ()
d0) [(a
1, forall d. D1 d => Prism' d ()
d1), (a
2, forall d. D2 d => Prism' d ()
d2), (a
3, forall d. D3 d => Prism' d ()
d3), (a
4, forall d. D4 d => Prism' d ()
d4), (a
5, forall d. D5 d => Prism' d ()
d5), (a
6, forall d. D6 d => Prism' d ()
d6), (a
7, forall d. D7 d => Prism' d ()
d7)]

-- |
-- >>> integralOctDigits (64 :: Int)
-- Right (OctDigit1 :| [OctDigit0, OctDigit0])
--
-- >>> integralOctDigits (0 :: Int)
-- Right (OctDigit0 :| [])
--
-- >>> integralOctDigits (-1 :: Int)
-- Left (OctDigit0 :| [])
--
-- >>> integralOctDigits (-64 :: Int)
-- Left (OctDigit7 :| [OctDigit7])
integralOctDigits :: Integral a => a -> Either (NonEmpty OctDigit) (NonEmpty OctDigit)
integralOctDigits :: forall a.
Integral a =>
a -> Either (NonEmpty OctDigit) (NonEmpty OctDigit)
integralOctDigits a
n =
  if a
n forall a. Ord a => a -> a -> Bool
>= a
0
  then forall a b. b -> Either a b
Right forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. [a] -> NonEmpty a
NonEmpty.fromList forall a b. (a -> b) -> a -> b
$ forall {s} {a}.
(Integral s, D0 a, D1 a, D2 a, D3 a, D4 a, D5 a, D6 a, D7 a) =>
s -> [a] -> [a]
go a
n []
  else forall a b. a -> Either a b
Left forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. [a] -> NonEmpty a
NonEmpty.fromList forall a b. (a -> b) -> a -> b
$ forall {s} {a}.
(Integral s, D0 a, D1 a, D2 a, D3 a, D4 a, D5 a, D6 a, D7 a) =>
s -> [a] -> [a]
go (-a
n forall a. Num a => a -> a -> a
- a
1) []
  where
    go :: s -> [a] -> [a]
go s
k =
      let
        (s
q, s
r) = forall a. Integral a => a -> a -> (a, a)
quotRem s
k s
8
      in
        (if s
q forall a. Eq a => a -> a -> Bool
== s
0 then forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id else s -> [a] -> [a]
go s
q) forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ((s
r forall s a. HasCallStack => s -> Getting (Endo a) s a -> a
^?! forall a d. (Integral a, Octal d) => Prism' a d
integralOctal) forall a. a -> [a] -> [a]
:)

-- |
-- >>> octDigitsIntegral (Right (OctDigit1 :| [OctDigit0, OctDigit0])) :: Int
-- 64
--
-- >>> octDigitsIntegral (Right (OctDigit0 :| [])) :: Int
-- 0
--
-- >>> octDigitsIntegral (Left (OctDigit0 :| [])) :: Int
-- 0
--
-- >>> octDigitsIntegral (Left (OctDigit7 :| [OctDigit7])) :: Int
-- -63
octDigitsIntegral :: Integral a => Either (NonEmpty OctDigit) (NonEmpty OctDigit) -> a
octDigitsIntegral :: forall a.
Integral a =>
Either (NonEmpty OctDigit) (NonEmpty OctDigit) -> a
octDigitsIntegral = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\NonEmpty OctDigit
n -> -(NonEmpty OctDigit -> a
go NonEmpty OctDigit
n)) NonEmpty OctDigit -> a
go
  where
    go :: NonEmpty OctDigit -> a
go = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\a
b OctDigit
a -> (forall a d. (Integral a, Octal d) => Prism' a d
integralOctal forall t b. AReview t b -> b -> t
# OctDigit
a) forall a. Num a => a -> a -> a
+ a
8 forall a. Num a => a -> a -> a
* a
b) a
0

-- |
-- >>> 9 ^? integralDecimalNoZero :: Maybe DecDigit
-- Just DecDigit9
--
-- >>> integralDecimalNoZero # DecDigit9 :: Integer
-- 9
integralDecimalNoZero ::
  (Integral a, DecimalNoZero d) =>
  Prism'
    a
    d
integralDecimalNoZero :: forall a d. (Integral a, DecimalNoZero d) => Prism' a d
integralDecimalNoZero =
  forall b (p :: * -> * -> *) (f :: * -> *) a.
(Eq b, Choice p, Applicative f) =>
(b, APrism a a () ())
-> [(b, APrism a a () ())] -> p a (f a) -> p b (f b)
associatePrism (a
1, forall d. D1 d => Prism' d ()
d1) [(a
2, forall d. D2 d => Prism' d ()
d2), (a
3, forall d. D3 d => Prism' d ()
d3), (a
4, forall d. D4 d => Prism' d ()
d4), (a
5, forall d. D5 d => Prism' d ()
d5), (a
6, forall d. D6 d => Prism' d ()
d6), (a
7, forall d. D7 d => Prism' d ()
d7), (a
8, forall d. D8 d => Prism' d ()
d8), (a
9, forall d. D9 d => Prism' d ()
d9)]

-- |
-- >>> 9 ^? integralDecimal :: Maybe DecDigit
-- Just DecDigit9
--
-- >>> integralDecimal # DecDigit9 :: Integer
-- 9
integralDecimal ::
  (Integral a, Decimal d) =>
  Prism'
    a
    d
integralDecimal :: forall a d. (Integral a, Decimal d) => Prism' a d
integralDecimal =
  forall b (p :: * -> * -> *) (f :: * -> *) a.
(Eq b, Choice p, Applicative f) =>
(b, APrism a a () ())
-> [(b, APrism a a () ())] -> p a (f a) -> p b (f b)
associatePrism (a
0, forall d. D0 d => Prism' d ()
d0) [(a
1, forall d. D1 d => Prism' d ()
d1), (a
2, forall d. D2 d => Prism' d ()
d2), (a
3, forall d. D3 d => Prism' d ()
d3), (a
4, forall d. D4 d => Prism' d ()
d4), (a
5, forall d. D5 d => Prism' d ()
d5), (a
6, forall d. D6 d => Prism' d ()
d6), (a
7, forall d. D7 d => Prism' d ()
d7), (a
8, forall d. D8 d => Prism' d ()
d8), (a
9, forall d. D9 d => Prism' d ()
d9)]

-- |
-- >>> integralDecDigits (100 :: Int)
-- Right (DecDigit1 :| [DecDigit0, DecDigit0])
--
-- >>> integralDecDigits (0 :: Int)
-- Right (DecDigit0 :| [])
--
-- >>> integralDecDigits (-1 :: Int)
-- Left (DecDigit0 :| [])
--
-- >>> integralDecDigits (-100 :: Int)
-- Left (DecDigit9 :| [DecDigit9])
integralDecDigits :: Integral a => a -> Either (NonEmpty DecDigit) (NonEmpty DecDigit)
integralDecDigits :: forall a.
Integral a =>
a -> Either (NonEmpty DecDigit) (NonEmpty DecDigit)
integralDecDigits a
n =
  if a
n forall a. Ord a => a -> a -> Bool
>= a
0
  then forall a b. b -> Either a b
Right forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. [a] -> NonEmpty a
NonEmpty.fromList forall a b. (a -> b) -> a -> b
$ forall {s} {a}.
(Integral s, D0 a, D1 a, D2 a, D3 a, D4 a, D5 a, D6 a, D7 a, D8 a,
 D9 a) =>
s -> [a] -> [a]
go a
n []
  else forall a b. a -> Either a b
Left forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. [a] -> NonEmpty a
NonEmpty.fromList forall a b. (a -> b) -> a -> b
$ forall {s} {a}.
(Integral s, D0 a, D1 a, D2 a, D3 a, D4 a, D5 a, D6 a, D7 a, D8 a,
 D9 a) =>
s -> [a] -> [a]
go (-a
n forall a. Num a => a -> a -> a
- a
1) []
  where
    go :: s -> [a] -> [a]
go s
k =
      let
        (s
q, s
r) = forall a. Integral a => a -> a -> (a, a)
quotRem s
k s
10
      in
        (if s
q forall a. Eq a => a -> a -> Bool
== s
0 then forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id else s -> [a] -> [a]
go s
q) forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ((s
r forall s a. HasCallStack => s -> Getting (Endo a) s a -> a
^?! forall a d. (Integral a, Decimal d) => Prism' a d
integralDecimal) forall a. a -> [a] -> [a]
:)

-- |
-- >>> decDigitsIntegral (Right (DecDigit1 :| [DecDigit0, DecDigit0])) :: Int
-- 100
--
-- >>> decDigitsIntegral (Right (DecDigit0 :| [])) :: Int
-- 0
--
-- >>> decDigitsIntegral (Left (DecDigit0 :| [])) :: Int
-- 0
--
-- >>> decDigitsIntegral (Left (DecDigit9 :| [DecDigit9])) :: Int
-- -9
decDigitsIntegral :: Integral a => Either (NonEmpty DecDigit) (NonEmpty DecDigit) -> a
decDigitsIntegral :: forall a.
Integral a =>
Either (NonEmpty DecDigit) (NonEmpty DecDigit) -> a
decDigitsIntegral = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\NonEmpty DecDigit
n -> -(NonEmpty DecDigit -> a
go NonEmpty DecDigit
n)) NonEmpty DecDigit -> a
go
  where
    go :: NonEmpty DecDigit -> a
go = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\a
b DecDigit
a -> (forall a d. (Integral a, Decimal d) => Prism' a d
integralDecimal forall t b. AReview t b -> b -> t
# DecDigit
a) forall a. Num a => a -> a -> a
+ a
10 forall a. Num a => a -> a -> a
* a
b) a
0

-- |
--
-- >>> 15 ^? integralHexadecimalNoZero :: Maybe HexDigit
-- Just HexDigitf
--
-- >>> integralHexadecimalNoZero # HexDigitf :: Integer
-- 15
integralHexadecimalNoZero ::
  (Integral a, HexadecimalNoZero d) =>
  Prism'
    a
    d
integralHexadecimalNoZero :: forall a d. (Integral a, HexadecimalNoZero d) => Prism' a d
integralHexadecimalNoZero =
  forall b (p :: * -> * -> *) (f :: * -> *) a.
(Eq b, Choice p, Applicative f) =>
(b, APrism a a () ())
-> [(b, APrism a a () ())] -> p a (f a) -> p b (f b)
associatePrism (a
1, forall d. D1 d => Prism' d ()
d1) [(a
2, forall d. D2 d => Prism' d ()
d2), (a
3, forall d. D3 d => Prism' d ()
d3), (a
4, forall d. D4 d => Prism' d ()
d4), (a
5, forall d. D5 d => Prism' d ()
d5), (a
6, forall d. D6 d => Prism' d ()
d6), (a
7, forall d. D7 d => Prism' d ()
d7), (a
8, forall d. D8 d => Prism' d ()
d8), (a
9, forall d. D9 d => Prism' d ()
d9), (a
10, forall d. Da d => Prism' d ()
da), (a
11, forall d. Db d => Prism' d ()
db), (a
12, forall d. Dc d => Prism' d ()
dc), (a
13, forall d. Dd d => Prism' d ()
dd), (a
14, forall d. De d => Prism' d ()
de), (a
15, forall d. Df d => Prism' d ()
df)]

-- |
--
-- >>> 15 ^? integralHexadecimal :: Maybe HexDigit
-- Just HexDigitf
--
-- >>> integralHexadecimal # HexDigitf :: Integer
-- 15
integralHexadecimal ::
  (Integral a, Hexadecimal d) =>
  Prism'
    a
    d
integralHexadecimal :: forall a d. (Integral a, Hexadecimal d) => Prism' a d
integralHexadecimal =
  forall b (p :: * -> * -> *) (f :: * -> *) a.
(Eq b, Choice p, Applicative f) =>
(b, APrism a a () ())
-> [(b, APrism a a () ())] -> p a (f a) -> p b (f b)
associatePrism (a
0, forall d. D0 d => Prism' d ()
d0) [(a
1, forall d. D1 d => Prism' d ()
d1), (a
2, forall d. D2 d => Prism' d ()
d2), (a
3, forall d. D3 d => Prism' d ()
d3), (a
4, forall d. D4 d => Prism' d ()
d4), (a
5, forall d. D5 d => Prism' d ()
d5), (a
6, forall d. D6 d => Prism' d ()
d6), (a
7, forall d. D7 d => Prism' d ()
d7), (a
8, forall d. D8 d => Prism' d ()
d8), (a
9, forall d. D9 d => Prism' d ()
d9), (a
10, forall d. Da d => Prism' d ()
da), (a
11, forall d. Db d => Prism' d ()
db), (a
12, forall d. Dc d => Prism' d ()
dc), (a
13, forall d. Dd d => Prism' d ()
dd), (a
14, forall d. De d => Prism' d ()
de), (a
15, forall d. Df d => Prism' d ()
df)]

-- |
-- >>> integralHexDigits (256 :: Int)
-- Right (HexDigit1 :| [HexDigit0, HexDigit0])
--
-- >>> integralHexDigits (0 :: Int)
-- Right (HexDigit0 :| [])
--
-- >>> integralHexDigits (-1 :: Int)
-- Left (HexDigit0 :| [])
--
-- >>> integralHexDigits (-256 :: Int)
-- Left (HexDigitf :| [HexDigitf])
integralHexDigits :: Integral a => a -> Either (NonEmpty HexDigit) (NonEmpty HexDigit)
integralHexDigits :: forall a.
Integral a =>
a -> Either (NonEmpty HexDigit) (NonEmpty HexDigit)
integralHexDigits a
n =
  if a
n forall a. Ord a => a -> a -> Bool
>= a
0
  then forall a b. b -> Either a b
Right forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. [a] -> NonEmpty a
NonEmpty.fromList forall a b. (a -> b) -> a -> b
$ forall {s} {a}.
(Integral s, D0 a, D1 a, D2 a, D3 a, D4 a, D5 a, D6 a, D7 a, D8 a,
 D9 a, Da a, Db a, Dc a, Dd a, De a, Df a) =>
s -> [a] -> [a]
go a
n []
  else forall a b. a -> Either a b
Left forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. [a] -> NonEmpty a
NonEmpty.fromList forall a b. (a -> b) -> a -> b
$ forall {s} {a}.
(Integral s, D0 a, D1 a, D2 a, D3 a, D4 a, D5 a, D6 a, D7 a, D8 a,
 D9 a, Da a, Db a, Dc a, Dd a, De a, Df a) =>
s -> [a] -> [a]
go (-a
n forall a. Num a => a -> a -> a
- a
1) []
  where
    go :: s -> [a] -> [a]
go s
k =
      let
        (s
q, s
r) = forall a. Integral a => a -> a -> (a, a)
quotRem s
k s
16
      in
        (if s
q forall a. Eq a => a -> a -> Bool
== s
0 then forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id else s -> [a] -> [a]
go s
q) forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ((s
r forall s a. HasCallStack => s -> Getting (Endo a) s a -> a
^?! forall a d. (Integral a, Hexadecimal d) => Prism' a d
integralHexadecimal) forall a. a -> [a] -> [a]
:)

-- |
-- >>> hexDigitsIntegral (Right (HexDigit1 :| [HexDigit0, HexDigit0])) :: Int
-- 256
--
-- >>> hexDigitsIntegral (Right (HexDigit0 :| [])) :: Int
-- 0
--
-- >>> hexDigitsIntegral (Left (HexDigit0 :| [])) :: Int
-- 0
--
-- >>> hexDigitsIntegral (Left (HexDigitf :| [HexDigitf])) :: Int
-- -255
hexDigitsIntegral :: Integral a => Either (NonEmpty HexDigit) (NonEmpty HexDigit) -> a
hexDigitsIntegral :: forall a.
Integral a =>
Either (NonEmpty HexDigit) (NonEmpty HexDigit) -> a
hexDigitsIntegral = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\NonEmpty HexDigit
n -> -(NonEmpty HexDigit -> a
go NonEmpty HexDigit
n)) NonEmpty HexDigit -> a
go
  where
    go :: NonEmpty HexDigit -> a
go = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\a
b HexDigit
a -> (forall a d. (Integral a, Hexadecimal d) => Prism' a d
integralHexadecimal forall t b. AReview t b -> b -> t
# HexDigit
a) forall a. Num a => a -> a -> a
+ a
16 forall a. Num a => a -> a -> a
* a
b) a
0

-- |
--
-- >>> 15 ^? integralHEXADECIMALNoZero :: Maybe HEXDigit
-- Just HEXDigitF
--
-- >>> integralHEXADECIMALNoZero # HEXDigitF :: Integer
-- 15
integralHEXADECIMALNoZero ::
  (Integral a, HEXADECIMALNoZero d) =>
  Prism'
    a
    d
integralHEXADECIMALNoZero :: forall a d. (Integral a, HEXADECIMALNoZero d) => Prism' a d
integralHEXADECIMALNoZero =
  forall b (p :: * -> * -> *) (f :: * -> *) a.
(Eq b, Choice p, Applicative f) =>
(b, APrism a a () ())
-> [(b, APrism a a () ())] -> p a (f a) -> p b (f b)
associatePrism (a
1, forall d. D1 d => Prism' d ()
d1) [(a
2, forall d. D2 d => Prism' d ()
d2), (a
3, forall d. D3 d => Prism' d ()
d3), (a
4, forall d. D4 d => Prism' d ()
d4), (a
5, forall d. D5 d => Prism' d ()
d5), (a
6, forall d. D6 d => Prism' d ()
d6), (a
7, forall d. D7 d => Prism' d ()
d7), (a
8, forall d. D8 d => Prism' d ()
d8), (a
9, forall d. D9 d => Prism' d ()
d9), (a
10, forall d. DA d => Prism' d ()
dA), (a
11, forall d. DB d => Prism' d ()
dB), (a
12, forall d. DC d => Prism' d ()
dC), (a
13, forall d. DD d => Prism' d ()
dD), (a
14, forall d. DE d => Prism' d ()
dE), (a
15, forall d. DF d => Prism' d ()
dF)]


-- |
--
-- >>> 15 ^? integralHEXADECIMAL :: Maybe HEXDigit
-- Just HEXDigitF
--
-- >>> integralHEXADECIMAL # HEXDigitF :: Integer
-- 15
integralHEXADECIMAL ::
  (Integral a, HEXADECIMAL d) =>
  Prism'
    a
    d
integralHEXADECIMAL :: forall a d. (Integral a, HEXADECIMAL d) => Prism' a d
integralHEXADECIMAL =
  forall b (p :: * -> * -> *) (f :: * -> *) a.
(Eq b, Choice p, Applicative f) =>
(b, APrism a a () ())
-> [(b, APrism a a () ())] -> p a (f a) -> p b (f b)
associatePrism (a
0, forall d. D0 d => Prism' d ()
d0) [(a
1, forall d. D1 d => Prism' d ()
d1), (a
2, forall d. D2 d => Prism' d ()
d2), (a
3, forall d. D3 d => Prism' d ()
d3), (a
4, forall d. D4 d => Prism' d ()
d4), (a
5, forall d. D5 d => Prism' d ()
d5), (a
6, forall d. D6 d => Prism' d ()
d6), (a
7, forall d. D7 d => Prism' d ()
d7), (a
8, forall d. D8 d => Prism' d ()
d8), (a
9, forall d. D9 d => Prism' d ()
d9), (a
10, forall d. DA d => Prism' d ()
dA), (a
11, forall d. DB d => Prism' d ()
dB), (a
12, forall d. DC d => Prism' d ()
dC), (a
13, forall d. DD d => Prism' d ()
dD), (a
14, forall d. DE d => Prism' d ()
dE), (a
15, forall d. DF d => Prism' d ()
dF)]

-- |
-- >>> integralHEXDigits (256 :: Int)
-- Right (HEXDigit1 :| [HEXDigit0, HEXDigit0])
--
-- >>> integralHEXDigits (0 :: Int)
-- Right (HEXDigit0 :| [])
--
-- >>> integralHEXDigits (-1 :: Int)
-- Left (HEXDigit0 :| [])
--
-- >>> integralHEXDigits (-256 :: Int)
-- Left (HEXDigitF :| [HEXDigitF])
integralHEXDigits :: Integral a => a -> Either (NonEmpty HEXDigit) (NonEmpty HEXDigit)
integralHEXDigits :: forall a.
Integral a =>
a -> Either (NonEmpty HEXDigit) (NonEmpty HEXDigit)
integralHEXDigits a
n =
  if a
n forall a. Ord a => a -> a -> Bool
>= a
0
  then forall a b. b -> Either a b
Right forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. [a] -> NonEmpty a
NonEmpty.fromList forall a b. (a -> b) -> a -> b
$ forall {s} {a}.
(Integral s, D0 a, D1 a, D2 a, D3 a, D4 a, D5 a, D6 a, D7 a, D8 a,
 D9 a, DA a, DB a, DC a, DD a, DE a, DF a) =>
s -> [a] -> [a]
go a
n []
  else forall a b. a -> Either a b
Left forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. [a] -> NonEmpty a
NonEmpty.fromList forall a b. (a -> b) -> a -> b
$ forall {s} {a}.
(Integral s, D0 a, D1 a, D2 a, D3 a, D4 a, D5 a, D6 a, D7 a, D8 a,
 D9 a, DA a, DB a, DC a, DD a, DE a, DF a) =>
s -> [a] -> [a]
go (-a
n forall a. Num a => a -> a -> a
- a
1) []
  where
    go :: s -> [a] -> [a]
go s
k =
      let
        (s
q, s
r) = forall a. Integral a => a -> a -> (a, a)
quotRem s
k s
16
      in
        (if s
q forall a. Eq a => a -> a -> Bool
== s
0 then forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id else s -> [a] -> [a]
go s
q) forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ((s
r forall s a. HasCallStack => s -> Getting (Endo a) s a -> a
^?! forall a d. (Integral a, HEXADECIMAL d) => Prism' a d
integralHEXADECIMAL) forall a. a -> [a] -> [a]
:)

-- |
-- >>> _HEXDigitsIntegral (Right (HEXDigit1 :| [HEXDigit0, HEXDigit0])) :: Int
-- 256
--
-- >>> _HEXDigitsIntegral (Right (HEXDigit0 :| [])) :: Int
-- 0
--
-- >>> _HEXDigitsIntegral (Left (HEXDigit0 :| [])) :: Int
-- 0
--
-- >>> _HEXDigitsIntegral (Left (HEXDigitF :| [HEXDigitF])) :: Int
-- -255
_HEXDigitsIntegral :: Integral a => Either (NonEmpty HEXDigit) (NonEmpty HEXDigit) -> a
_HEXDigitsIntegral :: forall a.
Integral a =>
Either (NonEmpty HEXDigit) (NonEmpty HEXDigit) -> a
_HEXDigitsIntegral = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\NonEmpty HEXDigit
n -> -(NonEmpty HEXDigit -> a
go NonEmpty HEXDigit
n)) NonEmpty HEXDigit -> a
go
  where
    go :: NonEmpty HEXDigit -> a
go = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\a
b HEXDigit
a -> (forall a d. (Integral a, HEXADECIMAL d) => Prism' a d
integralHEXADECIMAL forall t b. AReview t b -> b -> t
# HEXDigit
a) forall a. Num a => a -> a -> a
+ a
16 forall a. Num a => a -> a -> a
* a
b) a
0

-- |
--
-- >>> 15 ^? integralHeXaDeCiMaLNoZero :: Maybe HeXDigit
-- Just HeXDigitF
--
-- >>> integralHeXaDeCiMaLNoZero # HeXDigitF :: Integer
-- 15
integralHeXaDeCiMaLNoZero ::
  (Integral a, HeXaDeCiMaLNoZero d) =>
  Review
    a
    d
integralHeXaDeCiMaLNoZero :: forall a d. (Integral a, HeXaDeCiMaLNoZero d) => Review a d
integralHeXaDeCiMaLNoZero =
  forall (p :: * -> * -> *) (f :: * -> *) b t s a.
(Profunctor p, Bifunctor p, Functor f) =>
(b -> t) -> Optic p f s t a b
unto
    (forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. D1 d => Prism' d ()
d1 forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
1 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. D2 d => Prism' d ()
d2 forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
2 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. D3 d => Prism' d ()
d3 forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
3 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. D4 d => Prism' d ()
d4 forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
4 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. D5 d => Prism' d ()
d5 forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
5 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. D6 d => Prism' d ()
d6 forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
6 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. D7 d => Prism' d ()
d7 forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
7 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. D8 d => Prism' d ()
d8 forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
8 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. D9 d => Prism' d ()
d9 forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
9 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. Da d => Prism' d ()
da forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
10 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. DA d => Prism' d ()
dA forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
10 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. Db d => Prism' d ()
db forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
11 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. DB d => Prism' d ()
dB forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
11 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. Dc d => Prism' d ()
dc forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
12 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. DC d => Prism' d ()
dC forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
12 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. Dd d => Prism' d ()
dd forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
13 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. DD d => Prism' d ()
dD forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
13 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. De d => Prism' d ()
de forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
14 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. DE d => Prism' d ()
dE forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
14 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. Df d => Prism' d ()
df forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
15 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. DF d => Prism' d ()
dF forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
15 forall a b. (a -> b) -> a -> b
$
     forall a. HasCallStack => [Char] -> a
error [Char]
"incomplete pattern")

-- |
--
-- >>> 15 ^? integralHeXaDeCiMaL :: Maybe HeXDigit
-- Just HeXDigitF
--
-- >>> integralHeXaDeCiMaL # HeXDigitF :: Integer
-- 15
integralHeXaDeCiMaL ::
  (Integral a, HeXaDeCiMaL d) =>
  Review
    a
    d
integralHeXaDeCiMaL :: forall a d. (Integral a, HeXaDeCiMaL d) => Review a d
integralHeXaDeCiMaL =
  forall (p :: * -> * -> *) (f :: * -> *) b t s a.
(Profunctor p, Bifunctor p, Functor f) =>
(b -> t) -> Optic p f s t a b
unto
    (forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. D0 d => Prism' d ()
d0 forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
0 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. D1 d => Prism' d ()
d1 forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
1 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. D2 d => Prism' d ()
d2 forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
2 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. D3 d => Prism' d ()
d3 forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
3 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. D4 d => Prism' d ()
d4 forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
4 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. D5 d => Prism' d ()
d5 forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
5 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. D6 d => Prism' d ()
d6 forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
6 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. D7 d => Prism' d ()
d7 forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
7 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. D8 d => Prism' d ()
d8 forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
8 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. D9 d => Prism' d ()
d9 forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
9 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. Da d => Prism' d ()
da forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
10 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. DA d => Prism' d ()
dA forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
10 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. Db d => Prism' d ()
db forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
11 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. DB d => Prism' d ()
dB forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
11 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. Dc d => Prism' d ()
dc forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
12 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. DC d => Prism' d ()
dC forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
12 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. Dd d => Prism' d ()
dd forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
13 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. DD d => Prism' d ()
dD forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
13 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. De d => Prism' d ()
de forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
14 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. DE d => Prism' d ()
dE forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
14 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. Df d => Prism' d ()
df forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
15 forall a b. (a -> b) -> a -> b
$
     forall (p :: * -> * -> *) s t a b r.
Representable p =>
APrism s t a b -> Lens (p t r) (p s r) (p b r) (p a r)
outside forall d. DF d => Prism' d ()
dF forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a b. a -> b -> a
const a
15 forall a b. (a -> b) -> a -> b
$
     forall a. HasCallStack => [Char] -> a
error [Char]
"incomplete pattern")

-- |
-- >>> _HeXDigitsIntegral (Right (HeXDigit1 :| [HeXDigit0, HeXDigit0])) :: Int
-- 256
--
-- >>> _HeXDigitsIntegral (Right (HeXDigit0 :| [])) :: Int
-- 0
--
-- >>> _HeXDigitsIntegral (Left (HeXDigit0 :| [])) :: Int
-- 0
--
-- >>> _HeXDigitsIntegral (Left (HeXDigitF :| [HeXDigitF])) :: Int
-- -255
_HeXDigitsIntegral :: Integral a => Either (NonEmpty HeXDigit) (NonEmpty HeXDigit) -> a
_HeXDigitsIntegral :: forall a.
Integral a =>
Either (NonEmpty HeXDigit) (NonEmpty HeXDigit) -> a
_HeXDigitsIntegral = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\NonEmpty HeXDigit
n -> -(NonEmpty HeXDigit -> a
go NonEmpty HeXDigit
n)) NonEmpty HeXDigit -> a
go
  where
    go :: NonEmpty HeXDigit -> a
go = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\a
b HeXDigit
a -> (forall a d. (Integral a, HeXaDeCiMaL d) => Review a d
integralHeXaDeCiMaL forall t b. AReview t b -> b -> t
# HeXDigit
a) forall a. Num a => a -> a -> a
+ a
16 forall a. Num a => a -> a -> a
* a
b) a
0

mod10 ::
  Integral a =>
  a
  -> DecDigit
mod10 :: forall a. Integral a => a -> DecDigit
mod10 a
n =
  let r :: a
r = a
n forall a. Integral a => a -> a -> a
`mod` a
10
  in forall a. a -> Maybe a -> a
fromMaybe (forall a. Integral a => a -> DecDigit
mod10 a
r) (a
r forall s a. s -> Getting (First a) s a -> Maybe a
^? forall a d. (Integral a, Decimal d) => Prism' a d
integralDecimal)

addDecDigit ::
  DecDigit
  -> DecDigit
  -> (Bool, DecDigit)
addDecDigit :: DecDigit -> DecDigit -> (Bool, DecDigit)
addDecDigit DecDigit
a DecDigit
b =
  let (Int
x, Int
r) =
        (forall a d. (Integral a, Decimal d) => Prism' a d
integralDecimal forall t b. AReview t b -> b -> t
# DecDigit
a forall a. Num a => a -> a -> a
+ forall a d. (Integral a, Decimal d) => Prism' a d
integralDecimal forall t b. AReview t b -> b -> t
# DecDigit
b) forall a. Integral a => a -> a -> (a, a)
`divMod` Int
10
  in  (Int
x forall a. Ord a => a -> a -> Bool
> Int
0, forall a. Integral a => a -> DecDigit
mod10 (Int
r :: Int))

addDecDigit' ::
  DecDigit
  -> DecDigit
  -> (DecDigit, DecDigit)
addDecDigit' :: DecDigit -> DecDigit -> (DecDigit, DecDigit)
addDecDigit' DecDigit
a DecDigit
b =
  forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over forall s t a b. Field1 s t a b => Lens s t a b
_1 (forall a. a -> a -> Bool -> a
bool forall d. D0 d => d
x0 forall d. D1 d => d
x1) (DecDigit -> DecDigit -> (Bool, DecDigit)
addDecDigit DecDigit
a DecDigit
b)

---- not exported
associatePrism ::
  (Eq b, Choice p, Applicative f) =>
  (b, APrism a a () ())
  -> [(b, APrism a a () ())]
  -> p a (f a)
  -> p b (f b)
associatePrism :: forall b (p :: * -> * -> *) (f :: * -> *) a.
(Eq b, Choice p, Applicative f) =>
(b, APrism a a () ())
-> [(b, APrism a a () ())] -> p a (f a) -> p b (f b)
associatePrism (b, APrism a a () ())
def [(b, APrism a a () ())]
z =
  forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism'
    (\a
d -> forall a b. (a, b) -> a
fst (forall a. a -> Maybe a -> a
fromMaybe (b, APrism a a () ())
def (forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (\(b
_, APrism a a () ()
w) -> forall s t a b. APrism s t a b -> s -> Bool
is APrism a a () ()
w a
d) [(b, APrism a a () ())]
z)))
    (\b
i -> (\APrism a a () ()
p -> forall s t a b. APrism s t a b -> Prism s t a b
clonePrism APrism a a () ()
p forall t b. AReview t b -> b -> t
# ()) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup b
i ((b, APrism a a () ())
defforall a. a -> [a] -> [a]
:[(b, APrism a a () ())]
z))