{-# 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 :: Prism' a d
integralBinaryNoZero =
  (a, APrism d d () ())
-> [(a, APrism d d () ())] -> p d (f d) -> p a (f a)
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, APrism d d () ()
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 :: Prism' a d
integralBinary =
  (a, APrism d d () ())
-> [(a, APrism d d () ())] -> p d (f d) -> p a (f a)
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, APrism d d () ()
forall d. D0 d => Prism' d ()
d0) [(a
1, APrism d d () ()
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 :: a -> Either (NonEmpty BinDigit) (NonEmpty BinDigit)
integralBinDigits a
n =
  if a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
0
  then NonEmpty BinDigit -> Either (NonEmpty BinDigit) (NonEmpty BinDigit)
forall a b. b -> Either a b
Right (NonEmpty BinDigit
 -> Either (NonEmpty BinDigit) (NonEmpty BinDigit))
-> ([BinDigit] -> NonEmpty BinDigit)
-> [BinDigit]
-> Either (NonEmpty BinDigit) (NonEmpty BinDigit)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [BinDigit] -> NonEmpty BinDigit
forall a. [a] -> NonEmpty a
NonEmpty.fromList ([BinDigit] -> Either (NonEmpty BinDigit) (NonEmpty BinDigit))
-> [BinDigit] -> Either (NonEmpty BinDigit) (NonEmpty BinDigit)
forall a b. (a -> b) -> a -> b
$ a -> [BinDigit] -> [BinDigit]
forall s d. (D0 d, D1 d, Integral s) => s -> [d] -> [d]
go a
n []
  else NonEmpty BinDigit -> Either (NonEmpty BinDigit) (NonEmpty BinDigit)
forall a b. a -> Either a b
Left (NonEmpty BinDigit
 -> Either (NonEmpty BinDigit) (NonEmpty BinDigit))
-> ([BinDigit] -> NonEmpty BinDigit)
-> [BinDigit]
-> Either (NonEmpty BinDigit) (NonEmpty BinDigit)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [BinDigit] -> NonEmpty BinDigit
forall a. [a] -> NonEmpty a
NonEmpty.fromList ([BinDigit] -> Either (NonEmpty BinDigit) (NonEmpty BinDigit))
-> [BinDigit] -> Either (NonEmpty BinDigit) (NonEmpty BinDigit)
forall a b. (a -> b) -> a -> b
$ a -> [BinDigit] -> [BinDigit]
forall s d. (D0 d, D1 d, Integral s) => s -> [d] -> [d]
go (-a
n a -> a -> a
forall a. Num a => a -> a -> a
- a
1) []
  where
    go :: s -> [d] -> [d]
go s
k =
      let
        (s
q, s
r) = s -> s -> (s, s)
forall a. Integral a => a -> a -> (a, a)
quotRem s
k s
2
      in
        (if s
q s -> s -> Bool
forall a. Eq a => a -> a -> Bool
== s
0 then [d] -> [d]
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id else s -> [d] -> [d]
go s
q) ([d] -> [d]) -> ([d] -> [d]) -> [d] -> [d]
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ((s
r s -> Getting (Endo d) s d -> d
forall s a. HasCallStack => s -> Getting (Endo a) s a -> a
^?! Getting (Endo d) s d
forall a d. (Integral a, Binary d) => Prism' a d
integralBinary) d -> [d] -> [d]
forall a. a -> [a] -> [a]
:)

-- |
-- >>> binDigitsIntegral (Right (BinDigit1 :| [BinDigit0, BinDigit0])) :: Int
-- 4
--
-- >>> binDigitsIntegral (Right (BinDigit0 :| [])) :: Int
-- 0
--
-- >>> binDigitsIntegral (Left (BinDigit0 :| [])) :: Int
-- -1
--
-- >>> binDigitsIntegral (Left (BinDigit1 :| [BinDigit1])) :: Int
-- -4
binDigitsIntegral :: Integral a => Either (NonEmpty BinDigit) (NonEmpty BinDigit) -> a
binDigitsIntegral :: Either (NonEmpty BinDigit) (NonEmpty BinDigit) -> a
binDigitsIntegral = (NonEmpty BinDigit -> a)
-> (NonEmpty BinDigit -> a)
-> Either (NonEmpty BinDigit) (NonEmpty BinDigit)
-> a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\NonEmpty BinDigit
n -> -(NonEmpty BinDigit -> a
go NonEmpty BinDigit
n) a -> a -> a
forall a. Num a => a -> a -> a
- a
1) NonEmpty BinDigit -> a
go
  where
    go :: NonEmpty BinDigit -> a
go = (a -> BinDigit -> a) -> a -> NonEmpty BinDigit -> a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\a
b BinDigit
a -> (Tagged BinDigit (Identity BinDigit) -> Tagged a (Identity a)
forall a d. (Integral a, Binary d) => Prism' a d
integralBinary (Tagged BinDigit (Identity BinDigit) -> Tagged a (Identity a))
-> BinDigit -> a
forall t b. AReview t b -> b -> t
# BinDigit
a) a -> a -> a
forall a. Num a => a -> a -> a
+ a
2 a -> a -> a
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 :: Prism' a d
integralOctalNoZero =
  (a, APrism d d () ())
-> [(a, APrism d d () ())] -> p d (f d) -> p a (f a)
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, APrism d d () ()
forall d. D1 d => Prism' d ()
d1) [(a
2, APrism d d () ()
forall d. D2 d => Prism' d ()
d2), (a
3, APrism d d () ()
forall d. D3 d => Prism' d ()
d3), (a
4, APrism d d () ()
forall d. D4 d => Prism' d ()
d4), (a
5, APrism d d () ()
forall d. D5 d => Prism' d ()
d5), (a
6, APrism d d () ()
forall d. D6 d => Prism' d ()
d6), (a
7, APrism d d () ()
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 :: Prism' a d
integralOctal =
  (a, APrism d d () ())
-> [(a, APrism d d () ())] -> p d (f d) -> p a (f a)
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, APrism d d () ()
forall d. D0 d => Prism' d ()
d0) [(a
1, APrism d d () ()
forall d. D1 d => Prism' d ()
d1), (a
2, APrism d d () ()
forall d. D2 d => Prism' d ()
d2), (a
3, APrism d d () ()
forall d. D3 d => Prism' d ()
d3), (a
4, APrism d d () ()
forall d. D4 d => Prism' d ()
d4), (a
5, APrism d d () ()
forall d. D5 d => Prism' d ()
d5), (a
6, APrism d d () ()
forall d. D6 d => Prism' d ()
d6), (a
7, APrism d d () ()
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 :: a -> Either (NonEmpty OctDigit) (NonEmpty OctDigit)
integralOctDigits a
n =
  if a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
0
  then NonEmpty OctDigit -> Either (NonEmpty OctDigit) (NonEmpty OctDigit)
forall a b. b -> Either a b
Right (NonEmpty OctDigit
 -> Either (NonEmpty OctDigit) (NonEmpty OctDigit))
-> ([OctDigit] -> NonEmpty OctDigit)
-> [OctDigit]
-> Either (NonEmpty OctDigit) (NonEmpty OctDigit)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [OctDigit] -> NonEmpty OctDigit
forall a. [a] -> NonEmpty a
NonEmpty.fromList ([OctDigit] -> Either (NonEmpty OctDigit) (NonEmpty OctDigit))
-> [OctDigit] -> Either (NonEmpty OctDigit) (NonEmpty OctDigit)
forall a b. (a -> b) -> a -> b
$ a -> [OctDigit] -> [OctDigit]
forall s d.
(D0 d, D1 d, D2 d, D3 d, D4 d, D5 d, D6 d, D7 d, Integral s) =>
s -> [d] -> [d]
go a
n []
  else NonEmpty OctDigit -> Either (NonEmpty OctDigit) (NonEmpty OctDigit)
forall a b. a -> Either a b
Left (NonEmpty OctDigit
 -> Either (NonEmpty OctDigit) (NonEmpty OctDigit))
-> ([OctDigit] -> NonEmpty OctDigit)
-> [OctDigit]
-> Either (NonEmpty OctDigit) (NonEmpty OctDigit)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [OctDigit] -> NonEmpty OctDigit
forall a. [a] -> NonEmpty a
NonEmpty.fromList ([OctDigit] -> Either (NonEmpty OctDigit) (NonEmpty OctDigit))
-> [OctDigit] -> Either (NonEmpty OctDigit) (NonEmpty OctDigit)
forall a b. (a -> b) -> a -> b
$ a -> [OctDigit] -> [OctDigit]
forall s d.
(D0 d, D1 d, D2 d, D3 d, D4 d, D5 d, D6 d, D7 d, Integral s) =>
s -> [d] -> [d]
go (-a
n a -> a -> a
forall a. Num a => a -> a -> a
- a
1) []
  where
    go :: s -> [d] -> [d]
go s
k =
      let
        (s
q, s
r) = s -> s -> (s, s)
forall a. Integral a => a -> a -> (a, a)
quotRem s
k s
8
      in
        (if s
q s -> s -> Bool
forall a. Eq a => a -> a -> Bool
== s
0 then [d] -> [d]
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id else s -> [d] -> [d]
go s
q) ([d] -> [d]) -> ([d] -> [d]) -> [d] -> [d]
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ((s
r s -> Getting (Endo d) s d -> d
forall s a. HasCallStack => s -> Getting (Endo a) s a -> a
^?! Getting (Endo d) s d
forall a d. (Integral a, Octal d) => Prism' a d
integralOctal) d -> [d] -> [d]
forall a. a -> [a] -> [a]
:)

-- |
-- >>> octDigitsIntegral (Right (OctDigit1 :| [OctDigit0, OctDigit0])) :: Int
-- 64
--
-- >>> octDigitsIntegral (Right (OctDigit0 :| [])) :: Int
-- 0
--
-- >>> octDigitsIntegral (Left (OctDigit0 :| [])) :: Int
-- -1
--
-- >>> octDigitsIntegral (Left (OctDigit7 :| [OctDigit7])) :: Int
-- -64
octDigitsIntegral :: Integral a => Either (NonEmpty OctDigit) (NonEmpty OctDigit) -> a
octDigitsIntegral :: Either (NonEmpty OctDigit) (NonEmpty OctDigit) -> a
octDigitsIntegral = (NonEmpty OctDigit -> a)
-> (NonEmpty OctDigit -> a)
-> Either (NonEmpty OctDigit) (NonEmpty OctDigit)
-> a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\NonEmpty OctDigit
n -> -(NonEmpty OctDigit -> a
go NonEmpty OctDigit
n) a -> a -> a
forall a. Num a => a -> a -> a
- a
1) NonEmpty OctDigit -> a
go
  where
    go :: NonEmpty OctDigit -> a
go = (a -> OctDigit -> a) -> a -> NonEmpty OctDigit -> a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\a
b OctDigit
a -> (Tagged OctDigit (Identity OctDigit) -> Tagged a (Identity a)
forall a d. (Integral a, Octal d) => Prism' a d
integralOctal (Tagged OctDigit (Identity OctDigit) -> Tagged a (Identity a))
-> OctDigit -> a
forall t b. AReview t b -> b -> t
# OctDigit
a) a -> a -> a
forall a. Num a => a -> a -> a
+ a
8 a -> a -> a
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 :: Prism' a d
integralDecimalNoZero =
  (a, APrism d d () ())
-> [(a, APrism d d () ())] -> p d (f d) -> p a (f a)
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, APrism d d () ()
forall d. D1 d => Prism' d ()
d1) [(a
2, APrism d d () ()
forall d. D2 d => Prism' d ()
d2), (a
3, APrism d d () ()
forall d. D3 d => Prism' d ()
d3), (a
4, APrism d d () ()
forall d. D4 d => Prism' d ()
d4), (a
5, APrism d d () ()
forall d. D5 d => Prism' d ()
d5), (a
6, APrism d d () ()
forall d. D6 d => Prism' d ()
d6), (a
7, APrism d d () ()
forall d. D7 d => Prism' d ()
d7), (a
8, APrism d d () ()
forall d. D8 d => Prism' d ()
d8), (a
9, APrism d d () ()
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 :: Prism' a d
integralDecimal =
  (a, APrism d d () ())
-> [(a, APrism d d () ())] -> p d (f d) -> p a (f a)
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, APrism d d () ()
forall d. D0 d => Prism' d ()
d0) [(a
1, APrism d d () ()
forall d. D1 d => Prism' d ()
d1), (a
2, APrism d d () ()
forall d. D2 d => Prism' d ()
d2), (a
3, APrism d d () ()
forall d. D3 d => Prism' d ()
d3), (a
4, APrism d d () ()
forall d. D4 d => Prism' d ()
d4), (a
5, APrism d d () ()
forall d. D5 d => Prism' d ()
d5), (a
6, APrism d d () ()
forall d. D6 d => Prism' d ()
d6), (a
7, APrism d d () ()
forall d. D7 d => Prism' d ()
d7), (a
8, APrism d d () ()
forall d. D8 d => Prism' d ()
d8), (a
9, APrism d d () ()
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 :: a -> Either (NonEmpty DecDigit) (NonEmpty DecDigit)
integralDecDigits a
n =
  if a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
0
  then NonEmpty DecDigit -> Either (NonEmpty DecDigit) (NonEmpty DecDigit)
forall a b. b -> Either a b
Right (NonEmpty DecDigit
 -> Either (NonEmpty DecDigit) (NonEmpty DecDigit))
-> ([DecDigit] -> NonEmpty DecDigit)
-> [DecDigit]
-> Either (NonEmpty DecDigit) (NonEmpty DecDigit)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [DecDigit] -> NonEmpty DecDigit
forall a. [a] -> NonEmpty a
NonEmpty.fromList ([DecDigit] -> Either (NonEmpty DecDigit) (NonEmpty DecDigit))
-> [DecDigit] -> Either (NonEmpty DecDigit) (NonEmpty DecDigit)
forall a b. (a -> b) -> a -> b
$ a -> [DecDigit] -> [DecDigit]
forall s d.
(D0 d, D1 d, D2 d, D3 d, D4 d, D5 d, D6 d, D7 d, D8 d, D9 d,
 Integral s) =>
s -> [d] -> [d]
go a
n []
  else NonEmpty DecDigit -> Either (NonEmpty DecDigit) (NonEmpty DecDigit)
forall a b. a -> Either a b
Left (NonEmpty DecDigit
 -> Either (NonEmpty DecDigit) (NonEmpty DecDigit))
-> ([DecDigit] -> NonEmpty DecDigit)
-> [DecDigit]
-> Either (NonEmpty DecDigit) (NonEmpty DecDigit)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [DecDigit] -> NonEmpty DecDigit
forall a. [a] -> NonEmpty a
NonEmpty.fromList ([DecDigit] -> Either (NonEmpty DecDigit) (NonEmpty DecDigit))
-> [DecDigit] -> Either (NonEmpty DecDigit) (NonEmpty DecDigit)
forall a b. (a -> b) -> a -> b
$ a -> [DecDigit] -> [DecDigit]
forall s d.
(D0 d, D1 d, D2 d, D3 d, D4 d, D5 d, D6 d, D7 d, D8 d, D9 d,
 Integral s) =>
s -> [d] -> [d]
go (-a
n a -> a -> a
forall a. Num a => a -> a -> a
- a
1) []
  where
    go :: s -> [d] -> [d]
go s
k =
      let
        (s
q, s
r) = s -> s -> (s, s)
forall a. Integral a => a -> a -> (a, a)
quotRem s
k s
10
      in
        (if s
q s -> s -> Bool
forall a. Eq a => a -> a -> Bool
== s
0 then [d] -> [d]
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id else s -> [d] -> [d]
go s
q) ([d] -> [d]) -> ([d] -> [d]) -> [d] -> [d]
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ((s
r s -> Getting (Endo d) s d -> d
forall s a. HasCallStack => s -> Getting (Endo a) s a -> a
^?! Getting (Endo d) s d
forall a d. (Integral a, Decimal d) => Prism' a d
integralDecimal) d -> [d] -> [d]
forall a. a -> [a] -> [a]
:)

-- |
-- >>> decDigitsIntegral (Right (DecDigit1 :| [DecDigit0, DecDigit0])) :: Int
-- 100
--
-- >>> decDigitsIntegral (Right (DecDigit0 :| [])) :: Int
-- 0
--
-- >>> decDigitsIntegral (Left (DecDigit0 :| [])) :: Int
-- -1
--
-- >>> decDigitsIntegral (Left (DecDigit9 :| [DecDigit9])) :: Int
-- -100
decDigitsIntegral :: Integral a => Either (NonEmpty DecDigit) (NonEmpty DecDigit) -> a
decDigitsIntegral :: Either (NonEmpty DecDigit) (NonEmpty DecDigit) -> a
decDigitsIntegral = (NonEmpty DecDigit -> a)
-> (NonEmpty DecDigit -> a)
-> Either (NonEmpty DecDigit) (NonEmpty DecDigit)
-> a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\NonEmpty DecDigit
n -> -(NonEmpty DecDigit -> a
go NonEmpty DecDigit
n) a -> a -> a
forall a. Num a => a -> a -> a
- a
1) NonEmpty DecDigit -> a
go
  where
    go :: NonEmpty DecDigit -> a
go = (a -> DecDigit -> a) -> a -> NonEmpty DecDigit -> a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\a
b DecDigit
a -> (Tagged DecDigit (Identity DecDigit) -> Tagged a (Identity a)
forall a d. (Integral a, Decimal d) => Prism' a d
integralDecimal (Tagged DecDigit (Identity DecDigit) -> Tagged a (Identity a))
-> DecDigit -> a
forall t b. AReview t b -> b -> t
# DecDigit
a) a -> a -> a
forall a. Num a => a -> a -> a
+ a
10 a -> a -> a
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 :: Prism' a d
integralHexadecimalNoZero =
  (a, APrism d d () ())
-> [(a, APrism d d () ())] -> p d (f d) -> p a (f a)
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, APrism d d () ()
forall d. D1 d => Prism' d ()
d1) [(a
2, APrism d d () ()
forall d. D2 d => Prism' d ()
d2), (a
3, APrism d d () ()
forall d. D3 d => Prism' d ()
d3), (a
4, APrism d d () ()
forall d. D4 d => Prism' d ()
d4), (a
5, APrism d d () ()
forall d. D5 d => Prism' d ()
d5), (a
6, APrism d d () ()
forall d. D6 d => Prism' d ()
d6), (a
7, APrism d d () ()
forall d. D7 d => Prism' d ()
d7), (a
8, APrism d d () ()
forall d. D8 d => Prism' d ()
d8), (a
9, APrism d d () ()
forall d. D9 d => Prism' d ()
d9), (a
10, APrism d d () ()
forall d. Da d => Prism' d ()
da), (a
11, APrism d d () ()
forall d. Db d => Prism' d ()
db), (a
12, APrism d d () ()
forall d. Dc d => Prism' d ()
dc), (a
13, APrism d d () ()
forall d. Dd d => Prism' d ()
dd), (a
14, APrism d d () ()
forall d. De d => Prism' d ()
de), (a
15, APrism d d () ()
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 :: Prism' a d
integralHexadecimal =
  (a, APrism d d () ())
-> [(a, APrism d d () ())] -> p d (f d) -> p a (f a)
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, APrism d d () ()
forall d. D0 d => Prism' d ()
d0) [(a
1, APrism d d () ()
forall d. D1 d => Prism' d ()
d1), (a
2, APrism d d () ()
forall d. D2 d => Prism' d ()
d2), (a
3, APrism d d () ()
forall d. D3 d => Prism' d ()
d3), (a
4, APrism d d () ()
forall d. D4 d => Prism' d ()
d4), (a
5, APrism d d () ()
forall d. D5 d => Prism' d ()
d5), (a
6, APrism d d () ()
forall d. D6 d => Prism' d ()
d6), (a
7, APrism d d () ()
forall d. D7 d => Prism' d ()
d7), (a
8, APrism d d () ()
forall d. D8 d => Prism' d ()
d8), (a
9, APrism d d () ()
forall d. D9 d => Prism' d ()
d9), (a
10, APrism d d () ()
forall d. Da d => Prism' d ()
da), (a
11, APrism d d () ()
forall d. Db d => Prism' d ()
db), (a
12, APrism d d () ()
forall d. Dc d => Prism' d ()
dc), (a
13, APrism d d () ()
forall d. Dd d => Prism' d ()
dd), (a
14, APrism d d () ()
forall d. De d => Prism' d ()
de), (a
15, APrism d d () ()
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 :: a -> Either (NonEmpty HexDigit) (NonEmpty HexDigit)
integralHexDigits a
n =
  if a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
0
  then NonEmpty HexDigit -> Either (NonEmpty HexDigit) (NonEmpty HexDigit)
forall a b. b -> Either a b
Right (NonEmpty HexDigit
 -> Either (NonEmpty HexDigit) (NonEmpty HexDigit))
-> ([HexDigit] -> NonEmpty HexDigit)
-> [HexDigit]
-> Either (NonEmpty HexDigit) (NonEmpty HexDigit)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [HexDigit] -> NonEmpty HexDigit
forall a. [a] -> NonEmpty a
NonEmpty.fromList ([HexDigit] -> Either (NonEmpty HexDigit) (NonEmpty HexDigit))
-> [HexDigit] -> Either (NonEmpty HexDigit) (NonEmpty HexDigit)
forall a b. (a -> b) -> a -> b
$ a -> [HexDigit] -> [HexDigit]
forall s d.
(D0 d, D1 d, D2 d, D3 d, D4 d, D5 d, D6 d, D7 d, D8 d, D9 d, Da d,
 Db d, Dc d, Dd d, De d, Df d, Integral s) =>
s -> [d] -> [d]
go a
n []
  else NonEmpty HexDigit -> Either (NonEmpty HexDigit) (NonEmpty HexDigit)
forall a b. a -> Either a b
Left (NonEmpty HexDigit
 -> Either (NonEmpty HexDigit) (NonEmpty HexDigit))
-> ([HexDigit] -> NonEmpty HexDigit)
-> [HexDigit]
-> Either (NonEmpty HexDigit) (NonEmpty HexDigit)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [HexDigit] -> NonEmpty HexDigit
forall a. [a] -> NonEmpty a
NonEmpty.fromList ([HexDigit] -> Either (NonEmpty HexDigit) (NonEmpty HexDigit))
-> [HexDigit] -> Either (NonEmpty HexDigit) (NonEmpty HexDigit)
forall a b. (a -> b) -> a -> b
$ a -> [HexDigit] -> [HexDigit]
forall s d.
(D0 d, D1 d, D2 d, D3 d, D4 d, D5 d, D6 d, D7 d, D8 d, D9 d, Da d,
 Db d, Dc d, Dd d, De d, Df d, Integral s) =>
s -> [d] -> [d]
go (-a
n a -> a -> a
forall a. Num a => a -> a -> a
- a
1) []
  where
    go :: s -> [d] -> [d]
go s
k =
      let
        (s
q, s
r) = s -> s -> (s, s)
forall a. Integral a => a -> a -> (a, a)
quotRem s
k s
16
      in
        (if s
q s -> s -> Bool
forall a. Eq a => a -> a -> Bool
== s
0 then [d] -> [d]
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id else s -> [d] -> [d]
go s
q) ([d] -> [d]) -> ([d] -> [d]) -> [d] -> [d]
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ((s
r s -> Getting (Endo d) s d -> d
forall s a. HasCallStack => s -> Getting (Endo a) s a -> a
^?! Getting (Endo d) s d
forall a d. (Integral a, Hexadecimal d) => Prism' a d
integralHexadecimal) d -> [d] -> [d]
forall a. a -> [a] -> [a]
:)

-- |
-- >>> hexDigitsIntegral (Right (HexDigit1 :| [HexDigit0, HexDigit0])) :: Int
-- 256
--
-- >>> hexDigitsIntegral (Right (HexDigit0 :| [])) :: Int
-- 0
--
-- >>> hexDigitsIntegral (Left (HexDigit0 :| [])) :: Int
-- -1
--
-- >>> hexDigitsIntegral (Left (HexDigitf :| [HexDigitf])) :: Int
-- -256
hexDigitsIntegral :: Integral a => Either (NonEmpty HexDigit) (NonEmpty HexDigit) -> a
hexDigitsIntegral :: Either (NonEmpty HexDigit) (NonEmpty HexDigit) -> a
hexDigitsIntegral = (NonEmpty HexDigit -> a)
-> (NonEmpty HexDigit -> a)
-> Either (NonEmpty HexDigit) (NonEmpty HexDigit)
-> a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\NonEmpty HexDigit
n -> -(NonEmpty HexDigit -> a
go NonEmpty HexDigit
n) a -> a -> a
forall a. Num a => a -> a -> a
- a
1) NonEmpty HexDigit -> a
go
  where
    go :: NonEmpty HexDigit -> a
go = (a -> HexDigit -> a) -> a -> NonEmpty HexDigit -> a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\a
b HexDigit
a -> (Tagged HexDigit (Identity HexDigit) -> Tagged a (Identity a)
forall a d. (Integral a, Hexadecimal d) => Prism' a d
integralHexadecimal (Tagged HexDigit (Identity HexDigit) -> Tagged a (Identity a))
-> HexDigit -> a
forall t b. AReview t b -> b -> t
# HexDigit
a) a -> a -> a
forall a. Num a => a -> a -> a
+ a
16 a -> a -> a
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 :: Prism' a d
integralHEXADECIMALNoZero =
  (a, APrism d d () ())
-> [(a, APrism d d () ())] -> p d (f d) -> p a (f a)
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, APrism d d () ()
forall d. D1 d => Prism' d ()
d1) [(a
2, APrism d d () ()
forall d. D2 d => Prism' d ()
d2), (a
3, APrism d d () ()
forall d. D3 d => Prism' d ()
d3), (a
4, APrism d d () ()
forall d. D4 d => Prism' d ()
d4), (a
5, APrism d d () ()
forall d. D5 d => Prism' d ()
d5), (a
6, APrism d d () ()
forall d. D6 d => Prism' d ()
d6), (a
7, APrism d d () ()
forall d. D7 d => Prism' d ()
d7), (a
8, APrism d d () ()
forall d. D8 d => Prism' d ()
d8), (a
9, APrism d d () ()
forall d. D9 d => Prism' d ()
d9), (a
10, APrism d d () ()
forall d. DA d => Prism' d ()
dA), (a
11, APrism d d () ()
forall d. DB d => Prism' d ()
dB), (a
12, APrism d d () ()
forall d. DC d => Prism' d ()
dC), (a
13, APrism d d () ()
forall d. DD d => Prism' d ()
dD), (a
14, APrism d d () ()
forall d. DE d => Prism' d ()
dE), (a
15, APrism d d () ()
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 :: Prism' a d
integralHEXADECIMAL =
  (a, APrism d d () ())
-> [(a, APrism d d () ())] -> p d (f d) -> p a (f a)
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, APrism d d () ()
forall d. D0 d => Prism' d ()
d0) [(a
1, APrism d d () ()
forall d. D1 d => Prism' d ()
d1), (a
2, APrism d d () ()
forall d. D2 d => Prism' d ()
d2), (a
3, APrism d d () ()
forall d. D3 d => Prism' d ()
d3), (a
4, APrism d d () ()
forall d. D4 d => Prism' d ()
d4), (a
5, APrism d d () ()
forall d. D5 d => Prism' d ()
d5), (a
6, APrism d d () ()
forall d. D6 d => Prism' d ()
d6), (a
7, APrism d d () ()
forall d. D7 d => Prism' d ()
d7), (a
8, APrism d d () ()
forall d. D8 d => Prism' d ()
d8), (a
9, APrism d d () ()
forall d. D9 d => Prism' d ()
d9), (a
10, APrism d d () ()
forall d. DA d => Prism' d ()
dA), (a
11, APrism d d () ()
forall d. DB d => Prism' d ()
dB), (a
12, APrism d d () ()
forall d. DC d => Prism' d ()
dC), (a
13, APrism d d () ()
forall d. DD d => Prism' d ()
dD), (a
14, APrism d d () ()
forall d. DE d => Prism' d ()
dE), (a
15, APrism d d () ()
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 :: a -> Either (NonEmpty HEXDigit) (NonEmpty HEXDigit)
integralHEXDigits a
n =
  if a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
0
  then NonEmpty HEXDigit -> Either (NonEmpty HEXDigit) (NonEmpty HEXDigit)
forall a b. b -> Either a b
Right (NonEmpty HEXDigit
 -> Either (NonEmpty HEXDigit) (NonEmpty HEXDigit))
-> ([HEXDigit] -> NonEmpty HEXDigit)
-> [HEXDigit]
-> Either (NonEmpty HEXDigit) (NonEmpty HEXDigit)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [HEXDigit] -> NonEmpty HEXDigit
forall a. [a] -> NonEmpty a
NonEmpty.fromList ([HEXDigit] -> Either (NonEmpty HEXDigit) (NonEmpty HEXDigit))
-> [HEXDigit] -> Either (NonEmpty HEXDigit) (NonEmpty HEXDigit)
forall a b. (a -> b) -> a -> b
$ a -> [HEXDigit] -> [HEXDigit]
forall s d.
(D0 d, D1 d, D2 d, D3 d, D4 d, D5 d, D6 d, D7 d, D8 d, D9 d, DA d,
 DB d, DC d, DD d, DE d, DF d, Integral s) =>
s -> [d] -> [d]
go a
n []
  else NonEmpty HEXDigit -> Either (NonEmpty HEXDigit) (NonEmpty HEXDigit)
forall a b. a -> Either a b
Left (NonEmpty HEXDigit
 -> Either (NonEmpty HEXDigit) (NonEmpty HEXDigit))
-> ([HEXDigit] -> NonEmpty HEXDigit)
-> [HEXDigit]
-> Either (NonEmpty HEXDigit) (NonEmpty HEXDigit)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [HEXDigit] -> NonEmpty HEXDigit
forall a. [a] -> NonEmpty a
NonEmpty.fromList ([HEXDigit] -> Either (NonEmpty HEXDigit) (NonEmpty HEXDigit))
-> [HEXDigit] -> Either (NonEmpty HEXDigit) (NonEmpty HEXDigit)
forall a b. (a -> b) -> a -> b
$ a -> [HEXDigit] -> [HEXDigit]
forall s d.
(D0 d, D1 d, D2 d, D3 d, D4 d, D5 d, D6 d, D7 d, D8 d, D9 d, DA d,
 DB d, DC d, DD d, DE d, DF d, Integral s) =>
s -> [d] -> [d]
go (-a
n a -> a -> a
forall a. Num a => a -> a -> a
- a
1) []
  where
    go :: s -> [d] -> [d]
go s
k =
      let
        (s
q, s
r) = s -> s -> (s, s)
forall a. Integral a => a -> a -> (a, a)
quotRem s
k s
16
      in
        (if s
q s -> s -> Bool
forall a. Eq a => a -> a -> Bool
== s
0 then [d] -> [d]
forall k (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id else s -> [d] -> [d]
go s
q) ([d] -> [d]) -> ([d] -> [d]) -> [d] -> [d]
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ((s
r s -> Getting (Endo d) s d -> d
forall s a. HasCallStack => s -> Getting (Endo a) s a -> a
^?! Getting (Endo d) s d
forall a d. (Integral a, HEXADECIMAL d) => Prism' a d
integralHEXADECIMAL) d -> [d] -> [d]
forall a. a -> [a] -> [a]
:)

-- |
-- >>> HEXDigitsIntegral (Right (HEXDigit1 :| [HEXDigit0, HEXDigit0])) :: Int
-- 256
--
-- >>> HEXDigitsIntegral (Right (HEXDigit0 :| [])) :: Int
-- 0
--
-- >>> HEXDigitsIntegral (Left (HEXDigit0 :| [])) :: Int
-- -1
--
-- >>> HEXDigitsIntegral (Left (HEXDigitF :| [HEXDigitF])) :: Int
-- -256
_HEXDigitsIntegral :: Integral a => Either (NonEmpty HEXDigit) (NonEmpty HEXDigit) -> a
_HEXDigitsIntegral :: Either (NonEmpty HEXDigit) (NonEmpty HEXDigit) -> a
_HEXDigitsIntegral = (NonEmpty HEXDigit -> a)
-> (NonEmpty HEXDigit -> a)
-> Either (NonEmpty HEXDigit) (NonEmpty HEXDigit)
-> a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\NonEmpty HEXDigit
n -> -(NonEmpty HEXDigit -> a
go NonEmpty HEXDigit
n) a -> a -> a
forall a. Num a => a -> a -> a
- a
1) NonEmpty HEXDigit -> a
go
  where
    go :: NonEmpty HEXDigit -> a
go = (a -> HEXDigit -> a) -> a -> NonEmpty HEXDigit -> a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\a
b HEXDigit
a -> (Tagged HEXDigit (Identity HEXDigit) -> Tagged a (Identity a)
forall a d. (Integral a, HEXADECIMAL d) => Prism' a d
integralHEXADECIMAL (Tagged HEXDigit (Identity HEXDigit) -> Tagged a (Identity a))
-> HEXDigit -> a
forall t b. AReview t b -> b -> t
# HEXDigit
a) a -> a -> a
forall a. Num a => a -> a -> a
+ a
16 a -> a -> a
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 :: Review a d
integralHeXaDeCiMaLNoZero =
  (d -> a) -> Optic p f a a d d
forall (p :: * -> * -> *) (f :: * -> *) b t s a.
(Profunctor p, Bifunctor p, Functor f) =>
(b -> t) -> Optic p f s t a b
unto
    (APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. D1 d => Prism' d ()
d1 (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
1 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. D2 d => Prism' d ()
d2 (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
2 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. D3 d => Prism' d ()
d3 (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
3 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. D4 d => Prism' d ()
d4 (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
4 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. D5 d => Prism' d ()
d5 (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
5 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. D6 d => Prism' d ()
d6 (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
6 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. D7 d => Prism' d ()
d7 (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
7 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. D8 d => Prism' d ()
d8 (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
8 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. D9 d => Prism' d ()
d9 (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
9 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. Da d => Prism' d ()
da (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
10 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. DA d => Prism' d ()
dA (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
10 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. Db d => Prism' d ()
db (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
11 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. DB d => Prism' d ()
dB (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
11 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. Dc d => Prism' d ()
dc (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
12 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. DC d => Prism' d ()
dC (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
12 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. Dd d => Prism' d ()
dd (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
13 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. DD d => Prism' d ()
dD (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
13 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. De d => Prism' d ()
de (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
14 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. DE d => Prism' d ()
dE (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
14 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. Df d => Prism' d ()
df (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
15 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. DF d => Prism' d ()
dF (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
15 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     [Char] -> d -> a
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 :: Review a d
integralHeXaDeCiMaL =
  (d -> a) -> Optic p f a a d d
forall (p :: * -> * -> *) (f :: * -> *) b t s a.
(Profunctor p, Bifunctor p, Functor f) =>
(b -> t) -> Optic p f s t a b
unto
    (APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. D0 d => Prism' d ()
d0 (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
0 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. D1 d => Prism' d ()
d1 (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
1 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. D2 d => Prism' d ()
d2 (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
2 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. D3 d => Prism' d ()
d3 (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
3 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. D4 d => Prism' d ()
d4 (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
4 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. D5 d => Prism' d ()
d5 (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
5 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. D6 d => Prism' d ()
d6 (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
6 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. D7 d => Prism' d ()
d7 (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
7 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. D8 d => Prism' d ()
d8 (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
8 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. D9 d => Prism' d ()
d9 (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
9 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. Da d => Prism' d ()
da (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
10 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. DA d => Prism' d ()
dA (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
10 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. Db d => Prism' d ()
db (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
11 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. DB d => Prism' d ()
dB (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
11 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. Dc d => Prism' d ()
dc (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
12 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. DC d => Prism' d ()
dC (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
12 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. Dd d => Prism' d ()
dd (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
13 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. DD d => Prism' d ()
dD (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
13 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. De d => Prism' d ()
de (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
14 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. DE d => Prism' d ()
dE (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
14 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. Df d => Prism' d ()
df (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
15 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     APrism d d () () -> Lens (d -> a) (d -> a) (() -> a) (() -> a)
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 APrism d d () ()
forall d. DF d => Prism' d ()
dF (((() -> a) -> Identity (() -> a))
 -> (d -> a) -> Identity (d -> a))
-> (() -> a) -> (d -> a) -> d -> a
forall s t a b. ASetter s t a b -> b -> s -> t
.~ a -> () -> a
forall a b. a -> b -> a
const a
15 ((d -> a) -> d -> a) -> (d -> a) -> d -> a
forall a b. (a -> b) -> a -> b
$
     [Char] -> d -> a
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
-- -1
--
-- >>> HeXDigitsIntegral (Left (HeXDigitF :| [HeXDigitF])) :: Int
-- -256
_HeXDigitsIntegral :: Integral a => Either (NonEmpty HeXDigit) (NonEmpty HeXDigit) -> a
_HeXDigitsIntegral :: Either (NonEmpty HeXDigit) (NonEmpty HeXDigit) -> a
_HeXDigitsIntegral = (NonEmpty HeXDigit -> a)
-> (NonEmpty HeXDigit -> a)
-> Either (NonEmpty HeXDigit) (NonEmpty HeXDigit)
-> a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (\NonEmpty HeXDigit
n -> -(NonEmpty HeXDigit -> a
go NonEmpty HeXDigit
n) a -> a -> a
forall a. Num a => a -> a -> a
- a
1) NonEmpty HeXDigit -> a
go
  where
    go :: NonEmpty HeXDigit -> a
go = (a -> HeXDigit -> a) -> a -> NonEmpty HeXDigit -> a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\a
b HeXDigit
a -> (Optic' Tagged Identity a HeXDigit
forall a d. (Integral a, HeXaDeCiMaL d) => Review a d
integralHeXaDeCiMaL Optic' Tagged Identity a HeXDigit -> HeXDigit -> a
forall t b. AReview t b -> b -> t
# HeXDigit
a) a -> a -> a
forall a. Num a => a -> a -> a
+ a
16 a -> a -> a
forall a. Num a => a -> a -> a
* a
b) a
0

mod10 ::
  Integral a =>
  a
  -> DecDigit
mod10 :: a -> DecDigit
mod10 a
n =
  let r :: a
r = a
n a -> a -> a
forall a. Integral a => a -> a -> a
`mod` a
10
  in DecDigit -> Maybe DecDigit -> DecDigit
forall a. a -> Maybe a -> a
fromMaybe (a -> DecDigit
forall a. Integral a => a -> DecDigit
mod10 a
r) (a
r a -> Getting (First DecDigit) a DecDigit -> Maybe DecDigit
forall s a. s -> Getting (First a) s a -> Maybe a
^? Getting (First DecDigit) a DecDigit
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) =
        (Tagged DecDigit (Identity DecDigit) -> Tagged Int (Identity Int)
forall a d. (Integral a, Decimal d) => Prism' a d
integralDecimal (Tagged DecDigit (Identity DecDigit) -> Tagged Int (Identity Int))
-> DecDigit -> Int
forall t b. AReview t b -> b -> t
# DecDigit
a Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Tagged DecDigit (Identity DecDigit) -> Tagged Int (Identity Int)
forall a d. (Integral a, Decimal d) => Prism' a d
integralDecimal (Tagged DecDigit (Identity DecDigit) -> Tagged Int (Identity Int))
-> DecDigit -> Int
forall t b. AReview t b -> b -> t
# DecDigit
b) Int -> Int -> (Int, Int)
forall a. Integral a => a -> a -> (a, a)
`divMod` Int
10
  in  (Int
x Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0, Int -> DecDigit
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 =
  ASetter (Bool, DecDigit) (DecDigit, DecDigit) Bool DecDigit
-> (Bool -> DecDigit) -> (Bool, DecDigit) -> (DecDigit, DecDigit)
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter (Bool, DecDigit) (DecDigit, DecDigit) Bool DecDigit
forall s t a b. Field1 s t a b => Lens s t a b
_1 (DecDigit -> DecDigit -> Bool -> DecDigit
forall a. a -> a -> Bool -> a
bool DecDigit
forall d. D0 d => d
x0 DecDigit
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 :: (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 =
  (a -> b) -> (b -> Maybe a) -> Prism b b a a
forall b s a. (b -> s) -> (s -> Maybe a) -> Prism s s a b
prism'
    (\a
d -> (b, APrism a a () ()) -> b
forall a b. (a, b) -> a
fst ((b, APrism a a () ())
-> Maybe (b, APrism a a () ()) -> (b, APrism a a () ())
forall a. a -> Maybe a -> a
fromMaybe (b, APrism a a () ())
def (((b, APrism a a () ()) -> Bool)
-> [(b, APrism a a () ())] -> Maybe (b, APrism a a () ())
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (\(b
_, APrism a a () ()
w) -> APrism a a () () -> a -> Bool
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 -> APrism a a () () -> Prism a a () ()
forall s t a b. APrism s t a b -> Prism s t a b
clonePrism APrism a a () ()
p (Tagged () (Identity ()) -> Tagged a (Identity a)) -> () -> a
forall t b. AReview t b -> b -> t
# ()) (APrism a a () () -> a) -> Maybe (APrism a a () ()) -> Maybe a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> b -> [(b, APrism a a () ())] -> Maybe (APrism a a () ())
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup b
i ((b, APrism a a () ())
def(b, APrism a a () ())
-> [(b, APrism a a () ())] -> [(b, APrism a a () ())]
forall a. a -> [a] -> [a]
:[(b, APrism a a () ())]
z))