{-# 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
data Cardinality
= Shift !Int
| Card !Integer
deriving (Cardinality -> Cardinality -> Bool
(Cardinality -> Cardinality -> Bool)
-> (Cardinality -> Cardinality -> Bool) -> Eq Cardinality
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Cardinality -> Cardinality -> Bool
== :: Cardinality -> Cardinality -> Bool
$c/= :: Cardinality -> Cardinality -> Bool
/= :: Cardinality -> Cardinality -> Bool
Eq, Eq Cardinality
Eq Cardinality
-> (Cardinality -> Cardinality -> Ordering)
-> (Cardinality -> Cardinality -> Bool)
-> (Cardinality -> Cardinality -> Bool)
-> (Cardinality -> Cardinality -> Bool)
-> (Cardinality -> Cardinality -> Bool)
-> (Cardinality -> Cardinality -> Cardinality)
-> (Cardinality -> Cardinality -> Cardinality)
-> Ord 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
$ccompare :: Cardinality -> Cardinality -> Ordering
compare :: Cardinality -> Cardinality -> Ordering
$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
>= :: Cardinality -> Cardinality -> Bool
$cmax :: Cardinality -> Cardinality -> Cardinality
max :: Cardinality -> Cardinality -> Cardinality
$cmin :: Cardinality -> Cardinality -> Cardinality
min :: Cardinality -> Cardinality -> Cardinality
Ord, Int -> Cardinality -> ShowS
[Cardinality] -> ShowS
Cardinality -> String
(Int -> Cardinality -> ShowS)
-> (Cardinality -> String)
-> ([Cardinality] -> ShowS)
-> Show Cardinality
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Cardinality -> ShowS
showsPrec :: Int -> Cardinality -> ShowS
$cshow :: Cardinality -> String
show :: Cardinality -> String
$cshowList :: [Cardinality] -> ShowS
showList :: [Cardinality] -> ShowS
Show)
instance Enum Cardinality where
toEnum :: Int -> Cardinality
toEnum = Int -> Cardinality
forall a b. (Integral a, Num b) => a -> b
fromIntegral
fromEnum :: Cardinality -> Int
fromEnum = Cardinality -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral
succ :: Cardinality -> Cardinality
succ = (Cardinality -> Cardinality -> Cardinality
forall a. Num a => a -> a -> a
+ Cardinality
1)
pred :: Cardinality -> Cardinality
pred = Cardinality -> Cardinality -> Cardinality
forall a. Num a => a -> a -> a
subtract Cardinality
1
enumFrom :: Cardinality -> [Cardinality]
enumFrom Cardinality
x = (Integer -> Cardinality) -> [Integer] -> [Cardinality]
forall a b. (a -> b) -> [a] -> [b]
map Integer -> Cardinality
forall a. Num a => Integer -> a
fromInteger (Integer -> [Integer]
forall a. Enum a => a -> [a]
enumFrom (Cardinality -> Integer
forall a. Integral a => a -> Integer
toInteger Cardinality
x))
enumFromThen :: Cardinality -> Cardinality -> [Cardinality]
enumFromThen Cardinality
x Cardinality
y = (Integer -> Cardinality) -> [Integer] -> [Cardinality]
forall a b. (a -> b) -> [a] -> [b]
map Integer -> Cardinality
forall a. Num a => Integer -> a
fromInteger (Integer -> Integer -> [Integer]
forall a. Enum a => a -> a -> [a]
enumFromThen (Cardinality -> Integer
forall a. Integral a => a -> Integer
toInteger Cardinality
x) (Cardinality -> Integer
forall a. Integral a => a -> Integer
toInteger Cardinality
y))
enumFromTo :: Cardinality -> Cardinality -> [Cardinality]
enumFromTo Cardinality
x Cardinality
y = (Integer -> Cardinality) -> [Integer] -> [Cardinality]
forall a b. (a -> b) -> [a] -> [b]
map Integer -> Cardinality
forall a. Num a => Integer -> a
fromInteger (Integer -> Integer -> [Integer]
forall a. Enum a => a -> a -> [a]
enumFromTo (Cardinality -> Integer
forall a. Integral a => a -> Integer
toInteger Cardinality
x) (Cardinality -> Integer
forall a. Integral a => a -> Integer
toInteger Cardinality
y))
enumFromThenTo :: Cardinality -> Cardinality -> Cardinality -> [Cardinality]
enumFromThenTo Cardinality
x Cardinality
y Cardinality
z = (Integer -> Cardinality) -> [Integer] -> [Cardinality]
forall a b. (a -> b) -> [a] -> [b]
map Integer -> Cardinality
forall a. Num a => Integer -> a
fromInteger (Integer -> Integer -> Integer -> [Integer]
forall a. Enum a => a -> a -> a -> [a]
enumFromThenTo (Cardinality -> Integer
forall a. Integral a => a -> Integer
toInteger Cardinality
x) (Cardinality -> Integer
forall a. Integral a => a -> Integer
toInteger Cardinality
y) (Cardinality -> Integer
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
fromInteger Integer
n = Integer -> Cardinality
Card Integer
n
{-# INLINE fromInteger #-}
Cardinality
x + :: Cardinality -> Cardinality -> Cardinality
+ Cardinality
y = Integer -> Cardinality
forall a. Num a => Integer -> a
fromInteger (Cardinality -> Integer
forall a. Integral a => a -> Integer
toInteger Cardinality
x Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Cardinality -> Integer
forall a. Integral a => a -> Integer
toInteger Cardinality
y)
{-# INLINE (+) #-}
Shift Int
x * :: Cardinality -> Cardinality -> Cardinality
* Shift Int
y = Int -> Cardinality
Shift (Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
y)
Shift Int
x * Card Integer
y = Integer -> Cardinality
Card (Integer
y Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
`shiftL` Int
x)
Card Integer
x * Shift Int
y = Integer -> Cardinality
Card (Integer
x Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
`shiftL` Int
y)
Card Integer
x * Card Integer
y = Integer -> Cardinality
Card (Integer
x Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
y)
{-# INLINE (*) #-}
abs :: Cardinality -> Cardinality
abs = Integer -> Cardinality
Card (Integer -> Cardinality)
-> (Cardinality -> Integer) -> Cardinality -> Cardinality
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Integer
forall a. Num a => a -> a
abs (Integer -> Integer)
-> (Cardinality -> Integer) -> Cardinality -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Cardinality -> Integer
forall a. Integral a => a -> Integer
toInteger
signum :: Cardinality -> Cardinality
signum = Integer -> Cardinality
Card (Integer -> Cardinality)
-> (Cardinality -> Integer) -> Cardinality -> Cardinality
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Integer
forall a. Num a => a -> a
signum (Integer -> Integer)
-> (Cardinality -> Integer) -> Cardinality -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Cardinality -> Integer
forall a. Integral a => a -> Integer
toInteger
negate :: Cardinality -> Cardinality
negate = Integer -> Cardinality
Card (Integer -> Cardinality)
-> (Cardinality -> Integer) -> Cardinality -> Cardinality
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Integer
forall a. Num a => a -> a
negate (Integer -> Integer)
-> (Cardinality -> Integer) -> Cardinality -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Cardinality -> Integer
forall a. Integral a => a -> Integer
toInteger
instance Real Cardinality where
toRational :: Cardinality -> Rational
toRational = Cardinality -> Rational
forall a b. (Integral a, Num b) => a -> b
fromIntegral
instance Integral Cardinality where
toInteger :: Cardinality -> Integer
toInteger = \case
Shift Int
n -> Int -> Integer
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 Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
`shiftR` Int
n), Integer -> Cardinality
Card (Integer
x Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
.&. (Int -> Integer
forall a. Bits a => Int -> a
bit Int
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1)))
Card Integer
n -> let (Integer
q, Integer
r) = Integer
x Integer -> Integer -> (Integer, Integer)
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 = Cardinality -> Integer
forall a. Integral a => a -> Integer
toInteger Cardinality
x'
{-# INLINE quotRem #-}
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
_ = Proxy# (Rep a) -> Cardinality
forall (f :: * -> *). GFinite f => Proxy# f -> Cardinality
gcardinality (Proxy# (Rep a)
forall {k} (a :: k). Proxy# a
proxy# :: Proxy# (Rep a))
{-# INLINE cardinality #-}
default toFinite :: (Generic a, GFinite (Rep a)) => Integer -> a
toFinite = Rep a Any -> a
forall a x. Generic a => Rep a x -> a
forall x. Rep a x -> a
to (Rep a Any -> a) -> (Integer -> Rep a Any) -> Integer -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Rep a Any
forall a. Integer -> Rep a a
forall (f :: * -> *) a. GFinite f => Integer -> f a
toGFinite
{-# INLINE toFinite #-}
default fromFinite :: (Generic a, GFinite (Rep a)) => a -> Integer
fromFinite = Rep a Any -> Integer
forall a. Rep a a -> Integer
forall (f :: * -> *) a. GFinite f => f a -> Integer
fromGFinite (Rep a Any -> Integer) -> (a -> Rep a Any) -> a -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Rep a Any
forall x. a -> Rep a x
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 = V1 a -> Integer -> V1 a
forall a b. a -> b -> a
const (V1 a -> Integer -> V1 a) -> V1 a -> Integer -> V1 a
forall a b. (a -> b) -> a -> b
$ String -> V1 a
forall a. HasCallStack => String -> a
error String
"GFinite: V1 has no inhabitants"
{-# INLINE toGFinite #-}
fromGFinite :: forall a. V1 a -> Integer
fromGFinite = Integer -> V1 a -> Integer
forall a b. a -> b -> a
const (Integer -> V1 a -> Integer) -> Integer -> V1 a -> Integer
forall a b. (a -> b) -> a -> b
$ String -> Integer
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 = U1 a -> Integer -> U1 a
forall a b. a -> b -> a
const U1 a
forall k (p :: k). U1 p
U1
{-# INLINE toGFinite #-}
fromGFinite :: forall a. U1 a -> Integer
fromGFinite = Integer -> U1 a -> Integer
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)
_ = Proxy# a -> Cardinality
forall a. Finite a => Proxy# a -> Cardinality
cardinality (Proxy# a
forall {k} (a :: k). Proxy# a
proxy# :: Proxy# a)
{-# INLINE gcardinality #-}
toGFinite :: forall a. Integer -> K1 _x a a
toGFinite = a -> K1 _x a a
forall k i c (p :: k). c -> K1 i c p
K1 (a -> K1 _x a a) -> (Integer -> a) -> Integer -> K1 _x a a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> a
forall a. Finite a => Integer -> a
toFinite
{-# INLINE toGFinite #-}
fromGFinite :: forall a. K1 _x a a -> Integer
fromGFinite = a -> Integer
forall a. Finite a => a -> Integer
fromFinite (a -> Integer) -> (K1 _x a a -> a) -> K1 _x a a -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. K1 _x a a -> a
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)
_ = Proxy# a -> Cardinality
forall (f :: * -> *). GFinite f => Proxy# f -> Cardinality
gcardinality (Proxy# a
forall {k} (a :: k). Proxy# a
proxy# :: Proxy# a)
{-# INLINE gcardinality #-}
toGFinite :: forall a. Integer -> M1 _x _y a a
toGFinite = a a -> M1 _x _y a a
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (a a -> M1 _x _y a a)
-> (Integer -> a a) -> Integer -> M1 _x _y a a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> a a
forall a. Integer -> a a
forall (f :: * -> *) a. GFinite f => Integer -> f a
toGFinite
{-# INLINE toGFinite #-}
fromGFinite :: forall a. M1 _x _y a a -> Integer
fromGFinite = a a -> Integer
forall a. a a -> Integer
forall (f :: * -> *) a. GFinite f => f a -> Integer
fromGFinite (a a -> Integer)
-> (M1 _x _y a a -> a a) -> M1 _x _y a a -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. M1 _x _y a a -> a a
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)
_ =
Proxy# a -> Cardinality
forall (f :: * -> *). GFinite f => Proxy# f -> Cardinality
gcardinality (Proxy# a
forall {k} (a :: k). Proxy# a
proxy# :: Proxy# a) Cardinality -> Cardinality -> Cardinality
forall a. Num a => a -> a -> a
+ Proxy# b -> Cardinality
forall (f :: * -> *). GFinite f => Proxy# f -> Cardinality
gcardinality (Proxy# b
forall {k} (a :: k). Proxy# a
proxy# :: Proxy# b)
{-# INLINE gcardinality #-}
toGFinite :: forall a. Integer -> (:+:) a b a
toGFinite Integer
n
| Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
cardA = a a -> (:+:) a b a
forall k (f :: k -> *) (g :: k -> *) (p :: k). f p -> (:+:) f g p
L1 (a a -> (:+:) a b a) -> a a -> (:+:) a b a
forall a b. (a -> b) -> a -> b
$ Integer -> a a
forall a. Integer -> a a
forall (f :: * -> *) a. GFinite f => Integer -> f a
toGFinite Integer
n
| Bool
otherwise = b a -> (:+:) a b a
forall k (f :: k -> *) (g :: k -> *) (p :: k). g p -> (:+:) f g p
R1 (b a -> (:+:) a b a) -> b a -> (:+:) a b a
forall a b. (a -> b) -> a -> b
$ Integer -> b a
forall a. Integer -> b a
forall (f :: * -> *) a. GFinite f => Integer -> f a
toGFinite (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
cardA)
where
cardA :: Integer
cardA = Cardinality -> Integer
forall a. Integral a => a -> Integer
toInteger (Proxy# a -> Cardinality
forall (f :: * -> *). GFinite f => Proxy# f -> Cardinality
gcardinality (Proxy# a
forall {k} (a :: k). Proxy# a
proxy# :: Proxy# a))
{-# INLINE toGFinite #-}
fromGFinite :: forall a. (:+:) a b a -> Integer
fromGFinite = \case
L1 a a
x -> a a -> Integer
forall a. a a -> Integer
forall (f :: * -> *) a. GFinite f => f a -> Integer
fromGFinite a a
x
R1 b a
x -> b a -> Integer
forall a. b a -> Integer
forall (f :: * -> *) a. GFinite f => f a -> Integer
fromGFinite b a
x Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Cardinality -> Integer
forall a. Integral a => a -> Integer
toInteger (Proxy# a -> Cardinality
forall (f :: * -> *). GFinite f => Proxy# f -> Cardinality
gcardinality (Proxy# a
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)
_ =
Proxy# a -> Cardinality
forall (f :: * -> *). GFinite f => Proxy# f -> Cardinality
gcardinality (Proxy# a
forall {k} (a :: k). Proxy# a
proxy# :: Proxy# a) Cardinality -> Cardinality -> Cardinality
forall a. Num a => a -> a -> a
* Proxy# b -> Cardinality
forall (f :: * -> *). GFinite f => Proxy# f -> Cardinality
gcardinality (Proxy# b
forall {k} (a :: k). Proxy# a
proxy# :: Proxy# b)
{-# INLINE gcardinality #-}
toGFinite :: forall a. Integer -> (:*:) a b a
toGFinite Integer
n = Integer -> a a
forall a. Integer -> a a
forall (f :: * -> *) a. GFinite f => Integer -> f a
toGFinite (Cardinality -> Integer
forall a. Integral a => a -> Integer
toInteger Cardinality
q) a a -> b a -> (:*:) a b a
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: Integer -> b a
forall a. Integer -> b a
forall (f :: * -> *) a. GFinite f => Integer -> f a
toGFinite (Cardinality -> Integer
forall a. Integral a => a -> Integer
toInteger Cardinality
r)
where
cardB :: Cardinality
cardB = Proxy# b -> Cardinality
forall (f :: * -> *). GFinite f => Proxy# f -> Cardinality
gcardinality (Proxy# b
forall {k} (a :: k). Proxy# a
proxy# :: Proxy# b)
(Cardinality
q, Cardinality
r) = Integer -> Cardinality
Card Integer
n Cardinality -> Cardinality -> (Cardinality, Cardinality)
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) =
Cardinality -> Integer
forall a. Integral a => a -> Integer
toInteger (Proxy# b -> Cardinality
forall (f :: * -> *). GFinite f => Proxy# f -> Cardinality
gcardinality (Proxy# b
forall {k} (a :: k). Proxy# a
proxy# :: Proxy# b) Cardinality -> Cardinality -> Cardinality
forall a. Num a => a -> a -> a
* Integer -> Cardinality
Card (a a -> Integer
forall a. a a -> Integer
forall (f :: * -> *) a. GFinite f => f a -> Integer
fromGFinite a a
q)) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ b a -> Integer
forall a. b a -> Integer
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 (Integer -> Cardinality) -> Integer -> Cardinality
forall a b. (a -> b) -> a -> b
$ Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Char -> Int
forall a. Enum a => a -> Int
fromEnum (Char
forall a. Bounded a => a
maxBound :: Char)) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1
{-# INLINE cardinality #-}
toFinite :: Integer -> Char
toFinite = Int -> Char
forall a. Enum a => Int -> a
toEnum (Int -> Char) -> (Integer -> Int) -> Integer -> Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Int
forall a. Num a => Integer -> a
fromInteger
{-# INLINE toFinite #-}
fromFinite :: Char -> Integer
fromFinite = Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> (Char -> Int) -> Char -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Int
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 (a -> Int
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
| a -> Bool
forall a. Bits a => a -> Bool
isSigned (a
0 :: a) = Integer -> a
forall a. Num a => Integer -> a
fromInteger (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Int -> Integer
forall a. Bits a => Int -> a
bit (a -> Int
forall b. FiniteBits b => b -> Int
finiteBitSize (a
0 :: a) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1))
| Bool
otherwise = Integer -> a
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
| a -> Bool
forall a. Bits a => a -> Bool
isSigned a
x = a -> Integer
forall a. Integral a => a -> Integer
toInteger a
x Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Int -> Integer
forall a. Bits a => Int -> a
bit (a -> Int
forall b. FiniteBits b => b -> Int
finiteBitSize a
x Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
| Bool
otherwise = a -> Integer
forall a. Integral a => a -> Integer
toInteger a
x
instance Finite Word8 where
cardinality :: Proxy# Word8 -> Cardinality
cardinality = Proxy# Word8 -> Cardinality
forall a. (Num a, FiniteBits a) => Proxy# a -> Cardinality
cardinalityDef
{-# INLINE cardinality #-}
toFinite :: Integer -> Word8
toFinite = Integer -> Word8
forall a. (Num a, FiniteBits a) => Integer -> a
toFiniteDef
{-# INLINE toFinite #-}
fromFinite :: Word8 -> Integer
fromFinite = Word8 -> Integer
forall a. (Integral a, FiniteBits a) => a -> Integer
fromFiniteDef
{-# INLINE fromFinite #-}
instance Finite Word16 where
cardinality :: Proxy# Word16 -> Cardinality
cardinality = Proxy# Word16 -> Cardinality
forall a. (Num a, FiniteBits a) => Proxy# a -> Cardinality
cardinalityDef
{-# INLINE cardinality #-}
toFinite :: Integer -> Word16
toFinite = Integer -> Word16
forall a. (Num a, FiniteBits a) => Integer -> a
toFiniteDef
{-# INLINE toFinite #-}
fromFinite :: Word16 -> Integer
fromFinite = Word16 -> Integer
forall a. (Integral a, FiniteBits a) => a -> Integer
fromFiniteDef
{-# INLINE fromFinite #-}
instance Finite Word32 where
cardinality :: Proxy# Word32 -> Cardinality
cardinality = Proxy# Word32 -> Cardinality
forall a. (Num a, FiniteBits a) => Proxy# a -> Cardinality
cardinalityDef
{-# INLINE cardinality #-}
toFinite :: Integer -> Word32
toFinite = Integer -> Word32
forall a. (Num a, FiniteBits a) => Integer -> a
toFiniteDef
{-# INLINE toFinite #-}
fromFinite :: Word32 -> Integer
fromFinite = Word32 -> Integer
forall a. (Integral a, FiniteBits a) => a -> Integer
fromFiniteDef
{-# INLINE fromFinite #-}
instance Finite Word64 where
cardinality :: Proxy# Word64 -> Cardinality
cardinality = Proxy# Word64 -> Cardinality
forall a. (Num a, FiniteBits a) => Proxy# a -> Cardinality
cardinalityDef
{-# INLINE cardinality #-}
toFinite :: Integer -> Word64
toFinite = Integer -> Word64
forall a. (Num a, FiniteBits a) => Integer -> a
toFiniteDef
{-# INLINE toFinite #-}
fromFinite :: Word64 -> Integer
fromFinite = Word64 -> Integer
forall a. (Integral a, FiniteBits a) => a -> Integer
fromFiniteDef
{-# INLINE fromFinite #-}
instance Finite Word where
cardinality :: Proxy# Word -> Cardinality
cardinality = Proxy# Word -> Cardinality
forall a. (Num a, FiniteBits a) => Proxy# a -> Cardinality
cardinalityDef
{-# INLINE cardinality #-}
toFinite :: Integer -> Word
toFinite = Integer -> Word
forall a. (Num a, FiniteBits a) => Integer -> a
toFiniteDef
{-# INLINE toFinite #-}
fromFinite :: Word -> Integer
fromFinite = Word -> Integer
forall a. (Integral a, FiniteBits a) => a -> Integer
fromFiniteDef
{-# INLINE fromFinite #-}
instance Finite Int8 where
cardinality :: Proxy# Int8 -> Cardinality
cardinality = Proxy# Int8 -> Cardinality
forall a. (Num a, FiniteBits a) => Proxy# a -> Cardinality
cardinalityDef
{-# INLINE cardinality #-}
toFinite :: Integer -> Int8
toFinite = Integer -> Int8
forall a. (Num a, FiniteBits a) => Integer -> a
toFiniteDef
{-# INLINE toFinite #-}
fromFinite :: Int8 -> Integer
fromFinite = Int8 -> Integer
forall a. (Integral a, FiniteBits a) => a -> Integer
fromFiniteDef
{-# INLINE fromFinite #-}
instance Finite Int16 where
cardinality :: Proxy# Int16 -> Cardinality
cardinality = Proxy# Int16 -> Cardinality
forall a. (Num a, FiniteBits a) => Proxy# a -> Cardinality
cardinalityDef
{-# INLINE cardinality #-}
toFinite :: Integer -> Int16
toFinite = Integer -> Int16
forall a. (Num a, FiniteBits a) => Integer -> a
toFiniteDef
{-# INLINE toFinite #-}
fromFinite :: Int16 -> Integer
fromFinite = Int16 -> Integer
forall a. (Integral a, FiniteBits a) => a -> Integer
fromFiniteDef
{-# INLINE fromFinite #-}
instance Finite Int32 where
cardinality :: Proxy# Int32 -> Cardinality
cardinality = Proxy# Int32 -> Cardinality
forall a. (Num a, FiniteBits a) => Proxy# a -> Cardinality
cardinalityDef
{-# INLINE cardinality #-}
toFinite :: Integer -> Int32
toFinite = Integer -> Int32
forall a. (Num a, FiniteBits a) => Integer -> a
toFiniteDef
{-# INLINE toFinite #-}
fromFinite :: Int32 -> Integer
fromFinite = Int32 -> Integer
forall a. (Integral a, FiniteBits a) => a -> Integer
fromFiniteDef
{-# INLINE fromFinite #-}
instance Finite Int64 where
cardinality :: Proxy# Int64 -> Cardinality
cardinality = Proxy# Int64 -> Cardinality
forall a. (Num a, FiniteBits a) => Proxy# a -> Cardinality
cardinalityDef
{-# INLINE cardinality #-}
toFinite :: Integer -> Int64
toFinite = Integer -> Int64
forall a. (Num a, FiniteBits a) => Integer -> a
toFiniteDef
{-# INLINE toFinite #-}
fromFinite :: Int64 -> Integer
fromFinite = Int64 -> Integer
forall a. (Integral a, FiniteBits a) => a -> Integer
fromFiniteDef
{-# INLINE fromFinite #-}
instance Finite Int where
cardinality :: Proxy# Int -> Cardinality
cardinality = Proxy# Int -> Cardinality
forall a. (Num a, FiniteBits a) => Proxy# a -> Cardinality
cardinalityDef
{-# INLINE cardinality #-}
toFinite :: Integer -> Int
toFinite = Integer -> Int
forall a. (Num a, FiniteBits a) => Integer -> a
toFiniteDef
{-# INLINE toFinite #-}
fromFinite :: Int -> Integer
fromFinite = Int -> Integer
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)