-- |
-- Module      :  System.Random.GFinite
-- Copyright   :  (c) Andrew Lelechenko 2020
-- License     :  BSD-style (see the file LICENSE in the 'random' repository)
-- Maintainer  :  libraries@haskell.org
--

{-# LANGUAGE DefaultSignatures    #-}
{-# LANGUAGE FlexibleContexts     #-}
{-# LANGUAGE LambdaCase           #-}
{-# LANGUAGE MagicHash            #-}
{-# LANGUAGE ScopedTypeVariables  #-}
{-# LANGUAGE TypeOperators        #-}

module System.Random.GFinite
  ( Cardinality(..)
  , Finite(..)
  , GFinite(..)
  ) where

import Data.Bits
import Data.Int
import Data.Void
import Data.Word
import GHC.Exts (Proxy#, proxy#)
import GHC.Generics

-- | Cardinality of a set.
data Cardinality
  = Shift !Int -- ^ Shift n is equivalent to Card (bit n)
  | Card  !Integer
  deriving (Cardinality -> Cardinality -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Cardinality -> Cardinality -> Bool
$c/= :: Cardinality -> Cardinality -> Bool
== :: Cardinality -> Cardinality -> Bool
$c== :: Cardinality -> Cardinality -> Bool
Eq, Eq Cardinality
Cardinality -> Cardinality -> Bool
Cardinality -> Cardinality -> Ordering
Cardinality -> Cardinality -> Cardinality
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Cardinality -> Cardinality -> Cardinality
$cmin :: Cardinality -> Cardinality -> Cardinality
max :: Cardinality -> Cardinality -> Cardinality
$cmax :: Cardinality -> Cardinality -> Cardinality
>= :: Cardinality -> Cardinality -> Bool
$c>= :: Cardinality -> Cardinality -> Bool
> :: Cardinality -> Cardinality -> Bool
$c> :: Cardinality -> Cardinality -> Bool
<= :: Cardinality -> Cardinality -> Bool
$c<= :: Cardinality -> Cardinality -> Bool
< :: Cardinality -> Cardinality -> Bool
$c< :: Cardinality -> Cardinality -> Bool
compare :: Cardinality -> Cardinality -> Ordering
$ccompare :: Cardinality -> Cardinality -> Ordering
Ord, Int -> Cardinality -> ShowS
[Cardinality] -> ShowS
Cardinality -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Cardinality] -> ShowS
$cshowList :: [Cardinality] -> ShowS
show :: Cardinality -> String
$cshow :: Cardinality -> String
showsPrec :: Int -> Cardinality -> ShowS
$cshowsPrec :: Int -> Cardinality -> ShowS
Show)

-- | This is needed only as a superclass of 'Integral'.
instance Enum Cardinality where
  toEnum :: Int -> Cardinality
toEnum = forall a b. (Integral a, Num b) => a -> b
fromIntegral
  fromEnum :: Cardinality -> Int
fromEnum = forall a b. (Integral a, Num b) => a -> b
fromIntegral
  succ :: Cardinality -> Cardinality
succ = (forall a. Num a => a -> a -> a
+ Cardinality
1)
  pred :: Cardinality -> Cardinality
pred = forall a. Num a => a -> a -> a
subtract Cardinality
1
  enumFrom :: Cardinality -> [Cardinality]
enumFrom Cardinality
x           = forall a b. (a -> b) -> [a] -> [b]
map forall a. Num a => Integer -> a
fromInteger (forall a. Enum a => a -> [a]
enumFrom (forall a. Integral a => a -> Integer
toInteger Cardinality
x))
  enumFromThen :: Cardinality -> Cardinality -> [Cardinality]
enumFromThen Cardinality
x Cardinality
y     = forall a b. (a -> b) -> [a] -> [b]
map forall a. Num a => Integer -> a
fromInteger (forall a. Enum a => a -> a -> [a]
enumFromThen (forall a. Integral a => a -> Integer
toInteger Cardinality
x) (forall a. Integral a => a -> Integer
toInteger Cardinality
y))
  enumFromTo :: Cardinality -> Cardinality -> [Cardinality]
enumFromTo Cardinality
x Cardinality
y       = forall a b. (a -> b) -> [a] -> [b]
map forall a. Num a => Integer -> a
fromInteger (forall a. Enum a => a -> a -> [a]
enumFromTo (forall a. Integral a => a -> Integer
toInteger Cardinality
x) (forall a. Integral a => a -> Integer
toInteger Cardinality
y))
  enumFromThenTo :: Cardinality -> Cardinality -> Cardinality -> [Cardinality]
enumFromThenTo Cardinality
x Cardinality
y Cardinality
z = forall a b. (a -> b) -> [a] -> [b]
map forall a. Num a => Integer -> a
fromInteger (forall a. Enum a => a -> a -> a -> [a]
enumFromThenTo (forall a. Integral a => a -> Integer
toInteger Cardinality
x) (forall a. Integral a => a -> Integer
toInteger Cardinality
y) (forall a. Integral a => a -> Integer
toInteger Cardinality
z))

instance Num Cardinality where
  fromInteger :: Integer -> Cardinality
fromInteger Integer
1 = Int -> Cardinality
Shift Int
0  -- ()
  fromInteger Integer
2 = Int -> Cardinality
Shift Int
1  -- Bool
  fromInteger Integer
n = Integer -> Cardinality
Card Integer
n
  {-# INLINE fromInteger #-}

  Cardinality
x + :: Cardinality -> Cardinality -> Cardinality
+ Cardinality
y = forall a. Num a => Integer -> a
fromInteger (forall a. Integral a => a -> Integer
toInteger Cardinality
x forall a. Num a => a -> a -> a
+ forall a. Integral a => a -> Integer
toInteger Cardinality
y)
  {-# INLINE (+) #-}

  Shift Int
x * :: Cardinality -> Cardinality -> Cardinality
* Shift Int
y = Int -> Cardinality
Shift (Int
x forall a. Num a => a -> a -> a
+ Int
y)
  Shift Int
x * Card  Integer
y = Integer -> Cardinality
Card (Integer
y forall a. Bits a => a -> Int -> a
`shiftL` Int
x)
  Card  Integer
x * Shift Int
y = Integer -> Cardinality
Card (Integer
x forall a. Bits a => a -> Int -> a
`shiftL` Int
y)
  Card  Integer
x * Card  Integer
y = Integer -> Cardinality
Card (Integer
x forall a. Num a => a -> a -> a
* Integer
y)
  {-# INLINE (*) #-}

  abs :: Cardinality -> Cardinality
abs    = Integer -> Cardinality
Card forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => a -> a
abs    forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Integral a => a -> Integer
toInteger
  signum :: Cardinality -> Cardinality
signum = Integer -> Cardinality
Card forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => a -> a
signum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Integral a => a -> Integer
toInteger
  negate :: Cardinality -> Cardinality
negate = Integer -> Cardinality
Card forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => a -> a
negate forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Integral a => a -> Integer
toInteger

-- | This is needed only as a superclass of 'Integral'.
instance Real Cardinality where
  toRational :: Cardinality -> Rational
toRational = forall a b. (Integral a, Num b) => a -> b
fromIntegral

instance Integral Cardinality where
  toInteger :: Cardinality -> Integer
toInteger = \case
    Shift Int
n -> forall a. Bits a => Int -> a
bit Int
n
    Card  Integer
n -> Integer
n
  {-# INLINE toInteger #-}

  quotRem :: Cardinality -> Cardinality -> (Cardinality, Cardinality)
quotRem Cardinality
x' = \case
    Shift Int
n -> (Integer -> Cardinality
Card (Integer
x forall a. Bits a => a -> Int -> a
`shiftR` Int
n), Integer -> Cardinality
Card (Integer
x forall a. Bits a => a -> a -> a
.&. (forall a. Bits a => Int -> a
bit Int
n forall a. Num a => a -> a -> a
- Integer
1)))
    Card  Integer
n -> let (Integer
q, Integer
r) = Integer
x forall a. Integral a => a -> a -> (a, a)
`quotRem` Integer
n in (Integer -> Cardinality
Card Integer
q, Integer -> Cardinality
Card Integer
r)
    where
      x :: Integer
x = forall a. Integral a => a -> Integer
toInteger Cardinality
x'
  {-# INLINE quotRem #-}

-- | A type class for data with a finite number of inhabitants.
-- This type class is used
-- in default implementations of 'System.Random.Stateful.Uniform'.
--
-- Users are not supposed to write instances of 'Finite' manually.
-- There is a default implementation in terms of 'Generic' instead.
--
-- >>> :set -XDeriveGeneric -XDeriveAnyClass
-- >>> import GHC.Generics (Generic)
-- >>> data MyBool = MyTrue | MyFalse deriving (Generic, Finite)
-- >>> data Action = Code MyBool | Eat (Maybe Bool) | Sleep deriving (Generic, Finite)
--
class Finite a where
  cardinality :: Proxy# a -> Cardinality
  toFinite :: Integer -> a
  fromFinite :: a -> Integer

  default cardinality :: (Generic a, GFinite (Rep a)) => Proxy# a -> Cardinality
  cardinality Proxy# a
_ = forall (f :: * -> *). GFinite f => Proxy# f -> Cardinality
gcardinality (forall {k} (a :: k). Proxy# a
proxy# :: Proxy# (Rep a))
  {-# INLINE cardinality #-}

  default toFinite :: (Generic a, GFinite (Rep a)) => Integer -> a
  toFinite = forall a x. Generic a => Rep a x -> a
to forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. GFinite f => Integer -> f a
toGFinite
  {-# INLINE toFinite #-}

  default fromFinite :: (Generic a, GFinite (Rep a)) => a -> Integer
  fromFinite = forall (f :: * -> *) a. GFinite f => f a -> Integer
fromGFinite forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a x. Generic a => a -> Rep a x
from
  {-# INLINE fromFinite #-}

class GFinite f where
  gcardinality :: Proxy# f -> Cardinality
  toGFinite :: Integer -> f a
  fromGFinite :: f a -> Integer

instance GFinite V1 where
  gcardinality :: Proxy# V1 -> Cardinality
gcardinality Proxy# V1
_ = Cardinality
0
  {-# INLINE gcardinality #-}
  toGFinite :: forall a. Integer -> V1 a
toGFinite = forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall a. HasCallStack => String -> a
error String
"GFinite: V1 has no inhabitants"
  {-# INLINE toGFinite #-}
  fromGFinite :: forall a. V1 a -> Integer
fromGFinite = forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall a. HasCallStack => String -> a
error String
"GFinite: V1 has no inhabitants"
  {-# INLINE fromGFinite #-}

instance GFinite U1 where
  gcardinality :: Proxy# U1 -> Cardinality
gcardinality Proxy# U1
_ = Cardinality
1
  {-# INLINE gcardinality #-}
  toGFinite :: forall a. Integer -> U1 a
toGFinite = forall a b. a -> b -> a
const forall k (p :: k). U1 p
U1
  {-# INLINE toGFinite #-}
  fromGFinite :: forall a. U1 a -> Integer
fromGFinite = forall a b. a -> b -> a
const Integer
0
  {-# INLINE fromGFinite #-}

instance Finite a => GFinite (K1 _x a) where
  gcardinality :: Proxy# (K1 _x a) -> Cardinality
gcardinality Proxy# (K1 _x a)
_ = forall a. Finite a => Proxy# a -> Cardinality
cardinality (forall {k} (a :: k). Proxy# a
proxy# :: Proxy# a)
  {-# INLINE gcardinality #-}
  toGFinite :: forall a. Integer -> K1 _x a a
toGFinite = forall k i c (p :: k). c -> K1 i c p
K1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Finite a => Integer -> a
toFinite
  {-# INLINE toGFinite #-}
  fromGFinite :: forall a. K1 _x a a -> Integer
fromGFinite = forall a. Finite a => a -> Integer
fromFinite forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k i c (p :: k). K1 i c p -> c
unK1
  {-# INLINE fromGFinite #-}

instance GFinite a => GFinite (M1 _x _y a) where
  gcardinality :: Proxy# (M1 _x _y a) -> Cardinality
gcardinality Proxy# (M1 _x _y a)
_ = forall (f :: * -> *). GFinite f => Proxy# f -> Cardinality
gcardinality (forall {k} (a :: k). Proxy# a
proxy# :: Proxy# a)
  {-# INLINE gcardinality #-}
  toGFinite :: forall a. Integer -> M1 _x _y a a
toGFinite = forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. GFinite f => Integer -> f a
toGFinite
  {-# INLINE toGFinite #-}
  fromGFinite :: forall a. M1 _x _y a a -> Integer
fromGFinite = forall (f :: * -> *) a. GFinite f => f a -> Integer
fromGFinite forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k i (c :: Meta) (f :: k -> *) (p :: k). M1 i c f p -> f p
unM1
  {-# INLINE fromGFinite #-}

instance (GFinite a, GFinite b) => GFinite (a :+: b) where
  gcardinality :: Proxy# (a :+: b) -> Cardinality
gcardinality Proxy# (a :+: b)
_ =
    forall (f :: * -> *). GFinite f => Proxy# f -> Cardinality
gcardinality (forall {k} (a :: k). Proxy# a
proxy# :: Proxy# a) forall a. Num a => a -> a -> a
+ forall (f :: * -> *). GFinite f => Proxy# f -> Cardinality
gcardinality (forall {k} (a :: k). Proxy# a
proxy# :: Proxy# b)
  {-# INLINE gcardinality #-}

  toGFinite :: forall a. Integer -> (:+:) a b a
toGFinite Integer
n
    | Integer
n forall a. Ord a => a -> a -> Bool
< Integer
cardA = forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. GFinite f => Integer -> f a
toGFinite Integer
n
    | Bool
otherwise = forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a. GFinite f => Integer -> f a
toGFinite (Integer
n forall a. Num a => a -> a -> a
- Integer
cardA)
    where
      cardA :: Integer
cardA = forall a. Integral a => a -> Integer
toInteger (forall (f :: * -> *). GFinite f => Proxy# f -> Cardinality
gcardinality (forall {k} (a :: k). Proxy# a
proxy# :: Proxy# a))
  {-# INLINE toGFinite #-}

  fromGFinite :: forall a. (:+:) a b a -> Integer
fromGFinite = \case
     L1 a a
x -> forall (f :: * -> *) a. GFinite f => f a -> Integer
fromGFinite a a
x
     R1 b a
x -> forall (f :: * -> *) a. GFinite f => f a -> Integer
fromGFinite b a
x forall a. Num a => a -> a -> a
+ forall a. Integral a => a -> Integer
toInteger (forall (f :: * -> *). GFinite f => Proxy# f -> Cardinality
gcardinality (forall {k} (a :: k). Proxy# a
proxy# :: Proxy# a))
  {-# INLINE fromGFinite #-}

instance (GFinite a, GFinite b) => GFinite (a :*: b) where
  gcardinality :: Proxy# (a :*: b) -> Cardinality
gcardinality Proxy# (a :*: b)
_ =
    forall (f :: * -> *). GFinite f => Proxy# f -> Cardinality
gcardinality (forall {k} (a :: k). Proxy# a
proxy# :: Proxy# a) forall a. Num a => a -> a -> a
* forall (f :: * -> *). GFinite f => Proxy# f -> Cardinality
gcardinality (forall {k} (a :: k). Proxy# a
proxy# :: Proxy# b)
  {-# INLINE gcardinality #-}

  toGFinite :: forall a. Integer -> (:*:) a b a
toGFinite Integer
n = forall (f :: * -> *) a. GFinite f => Integer -> f a
toGFinite (forall a. Integral a => a -> Integer
toInteger Cardinality
q) forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: forall (f :: * -> *) a. GFinite f => Integer -> f a
toGFinite (forall a. Integral a => a -> Integer
toInteger Cardinality
r)
    where
      cardB :: Cardinality
cardB = forall (f :: * -> *). GFinite f => Proxy# f -> Cardinality
gcardinality (forall {k} (a :: k). Proxy# a
proxy# :: Proxy# b)
      (Cardinality
q, Cardinality
r) = Integer -> Cardinality
Card Integer
n forall a. Integral a => a -> a -> (a, a)
`quotRem` Cardinality
cardB
  {-# INLINE toGFinite #-}

  fromGFinite :: forall a. (:*:) a b a -> Integer
fromGFinite (a a
q :*: b a
r) =
    forall a. Integral a => a -> Integer
toInteger (forall (f :: * -> *). GFinite f => Proxy# f -> Cardinality
gcardinality (forall {k} (a :: k). Proxy# a
proxy# :: Proxy# b) forall a. Num a => a -> a -> a
* Integer -> Cardinality
Card (forall (f :: * -> *) a. GFinite f => f a -> Integer
fromGFinite a a
q)) forall a. Num a => a -> a -> a
+ forall (f :: * -> *) a. GFinite f => f a -> Integer
fromGFinite b a
r
  {-# INLINE fromGFinite #-}

instance Finite Void
instance Finite ()
instance Finite Bool
instance Finite Ordering

instance Finite Char where
  cardinality :: Proxy# Char -> Cardinality
cardinality Proxy# Char
_ = Integer -> Cardinality
Card forall a b. (a -> b) -> a -> b
$ forall a. Integral a => a -> Integer
toInteger (forall a. Enum a => a -> Int
fromEnum (forall a. Bounded a => a
maxBound :: Char)) forall a. Num a => a -> a -> a
+ Integer
1
  {-# INLINE cardinality #-}
  toFinite :: Integer -> Char
toFinite = forall a. Enum a => Int -> a
toEnum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => Integer -> a
fromInteger
  {-# INLINE toFinite #-}
  fromFinite :: Char -> Integer
fromFinite = forall a. Integral a => a -> Integer
toInteger forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Enum a => a -> Int
fromEnum
  {-# INLINE fromFinite #-}

cardinalityDef :: forall a. (Num a, FiniteBits a) => Proxy# a -> Cardinality
cardinalityDef :: forall a. (Num a, FiniteBits a) => Proxy# a -> Cardinality
cardinalityDef Proxy# a
_ = Int -> Cardinality
Shift (forall b. FiniteBits b => b -> Int
finiteBitSize (a
0 :: a))

toFiniteDef :: forall a. (Num a, FiniteBits a) => Integer -> a
toFiniteDef :: forall a. (Num a, FiniteBits a) => Integer -> a
toFiniteDef Integer
n
    | forall a. Bits a => a -> Bool
isSigned (a
0 :: a) = forall a. Num a => Integer -> a
fromInteger (Integer
n forall a. Num a => a -> a -> a
- forall a. Bits a => Int -> a
bit (forall b. FiniteBits b => b -> Int
finiteBitSize (a
0 :: a) forall a. Num a => a -> a -> a
- Int
1))
    | Bool
otherwise = forall a. Num a => Integer -> a
fromInteger Integer
n

fromFiniteDef :: (Integral a, FiniteBits a) => a -> Integer
fromFiniteDef :: forall a. (Integral a, FiniteBits a) => a -> Integer
fromFiniteDef a
x
    | forall a. Bits a => a -> Bool
isSigned a
x = forall a. Integral a => a -> Integer
toInteger a
x forall a. Num a => a -> a -> a
+ forall a. Bits a => Int -> a
bit (forall b. FiniteBits b => b -> Int
finiteBitSize a
x forall a. Num a => a -> a -> a
- Int
1)
    | Bool
otherwise = forall a. Integral a => a -> Integer
toInteger a
x

instance Finite Word8 where
  cardinality :: Proxy# Word8 -> Cardinality
cardinality = forall a. (Num a, FiniteBits a) => Proxy# a -> Cardinality
cardinalityDef
  {-# INLINE cardinality #-}
  toFinite :: Integer -> Word8
toFinite = forall a. (Num a, FiniteBits a) => Integer -> a
toFiniteDef
  {-# INLINE toFinite #-}
  fromFinite :: Word8 -> Integer
fromFinite = forall a. (Integral a, FiniteBits a) => a -> Integer
fromFiniteDef
  {-# INLINE fromFinite #-}
instance Finite Word16 where
  cardinality :: Proxy# Word16 -> Cardinality
cardinality = forall a. (Num a, FiniteBits a) => Proxy# a -> Cardinality
cardinalityDef
  {-# INLINE cardinality #-}
  toFinite :: Integer -> Word16
toFinite = forall a. (Num a, FiniteBits a) => Integer -> a
toFiniteDef
  {-# INLINE toFinite #-}
  fromFinite :: Word16 -> Integer
fromFinite = forall a. (Integral a, FiniteBits a) => a -> Integer
fromFiniteDef
  {-# INLINE fromFinite #-}
instance Finite Word32 where
  cardinality :: Proxy# Word32 -> Cardinality
cardinality = forall a. (Num a, FiniteBits a) => Proxy# a -> Cardinality
cardinalityDef
  {-# INLINE cardinality #-}
  toFinite :: Integer -> Word32
toFinite = forall a. (Num a, FiniteBits a) => Integer -> a
toFiniteDef
  {-# INLINE toFinite #-}
  fromFinite :: Word32 -> Integer
fromFinite = forall a. (Integral a, FiniteBits a) => a -> Integer
fromFiniteDef
  {-# INLINE fromFinite #-}
instance Finite Word64 where
  cardinality :: Proxy# Word64 -> Cardinality
cardinality = forall a. (Num a, FiniteBits a) => Proxy# a -> Cardinality
cardinalityDef
  {-# INLINE cardinality #-}
  toFinite :: Integer -> Word64
toFinite = forall a. (Num a, FiniteBits a) => Integer -> a
toFiniteDef
  {-# INLINE toFinite #-}
  fromFinite :: Word64 -> Integer
fromFinite = forall a. (Integral a, FiniteBits a) => a -> Integer
fromFiniteDef
  {-# INLINE fromFinite #-}
instance Finite Word where
  cardinality :: Proxy# Word -> Cardinality
cardinality = forall a. (Num a, FiniteBits a) => Proxy# a -> Cardinality
cardinalityDef
  {-# INLINE cardinality #-}
  toFinite :: Integer -> Word
toFinite = forall a. (Num a, FiniteBits a) => Integer -> a
toFiniteDef
  {-# INLINE toFinite #-}
  fromFinite :: Word -> Integer
fromFinite = forall a. (Integral a, FiniteBits a) => a -> Integer
fromFiniteDef
  {-# INLINE fromFinite #-}
instance Finite Int8 where
  cardinality :: Proxy# Int8 -> Cardinality
cardinality = forall a. (Num a, FiniteBits a) => Proxy# a -> Cardinality
cardinalityDef
  {-# INLINE cardinality #-}
  toFinite :: Integer -> Int8
toFinite = forall a. (Num a, FiniteBits a) => Integer -> a
toFiniteDef
  {-# INLINE toFinite #-}
  fromFinite :: Int8 -> Integer
fromFinite = forall a. (Integral a, FiniteBits a) => a -> Integer
fromFiniteDef
  {-# INLINE fromFinite #-}
instance Finite Int16 where
  cardinality :: Proxy# Int16 -> Cardinality
cardinality = forall a. (Num a, FiniteBits a) => Proxy# a -> Cardinality
cardinalityDef
  {-# INLINE cardinality #-}
  toFinite :: Integer -> Int16
toFinite = forall a. (Num a, FiniteBits a) => Integer -> a
toFiniteDef
  {-# INLINE toFinite #-}
  fromFinite :: Int16 -> Integer
fromFinite = forall a. (Integral a, FiniteBits a) => a -> Integer
fromFiniteDef
  {-# INLINE fromFinite #-}
instance Finite Int32 where
  cardinality :: Proxy# Int32 -> Cardinality
cardinality = forall a. (Num a, FiniteBits a) => Proxy# a -> Cardinality
cardinalityDef
  {-# INLINE cardinality #-}
  toFinite :: Integer -> Int32
toFinite = forall a. (Num a, FiniteBits a) => Integer -> a
toFiniteDef
  {-# INLINE toFinite #-}
  fromFinite :: Int32 -> Integer
fromFinite = forall a. (Integral a, FiniteBits a) => a -> Integer
fromFiniteDef
  {-# INLINE fromFinite #-}
instance Finite Int64 where
  cardinality :: Proxy# Int64 -> Cardinality
cardinality = forall a. (Num a, FiniteBits a) => Proxy# a -> Cardinality
cardinalityDef
  {-# INLINE cardinality #-}
  toFinite :: Integer -> Int64
toFinite = forall a. (Num a, FiniteBits a) => Integer -> a
toFiniteDef
  {-# INLINE toFinite #-}
  fromFinite :: Int64 -> Integer
fromFinite = forall a. (Integral a, FiniteBits a) => a -> Integer
fromFiniteDef
  {-# INLINE fromFinite #-}
instance Finite Int where
  cardinality :: Proxy# Int -> Cardinality
cardinality = forall a. (Num a, FiniteBits a) => Proxy# a -> Cardinality
cardinalityDef
  {-# INLINE cardinality #-}
  toFinite :: Integer -> Int
toFinite = forall a. (Num a, FiniteBits a) => Integer -> a
toFiniteDef
  {-# INLINE toFinite #-}
  fromFinite :: Int -> Integer
fromFinite = forall a. (Integral a, FiniteBits a) => a -> Integer
fromFiniteDef
  {-# INLINE fromFinite #-}

instance Finite a => Finite (Maybe a)
instance (Finite a, Finite b) => Finite (Either a b)
instance (Finite a, Finite b) => Finite (a, b)
instance (Finite a, Finite b, Finite c) => Finite (a, b, c)
instance (Finite a, Finite b, Finite c, Finite d) => Finite (a, b, c, d)
instance (Finite a, Finite b, Finite c, Finite d, Finite e) => Finite (a, b, c, d, e)
instance (Finite a, Finite b, Finite c, Finite d, Finite e, Finite f) => Finite (a, b, c, d, e, f)