|
| 1 | +-- | |
| 2 | +-- Module : System.Random.GFinite |
| 3 | +-- Copyright : (c) Andrew Lelechenko 2020 |
| 4 | +-- License : BSD-style (see the file LICENSE in the 'random' repository) |
| 5 | +-- Maintainer : libraries@haskell.org |
| 6 | +-- |
| 7 | + |
| 8 | +{-# LANGUAGE DefaultSignatures #-} |
| 9 | +{-# LANGUAGE FlexibleContexts #-} |
| 10 | +{-# LANGUAGE LambdaCase #-} |
| 11 | +{-# LANGUAGE MagicHash #-} |
| 12 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 13 | +{-# LANGUAGE TypeOperators #-} |
| 14 | + |
| 15 | +module System.Random.GFinite |
| 16 | + ( Cardinality(..) |
| 17 | + , Finite(..) |
| 18 | + , GFinite(..) |
| 19 | + ) where |
| 20 | + |
| 21 | +import Data.Bits |
| 22 | +import Data.Int |
| 23 | +import Data.Void |
| 24 | +import Data.Word |
| 25 | +import GHC.Exts (Proxy#, proxy#) |
| 26 | +import GHC.Generics |
| 27 | + |
| 28 | +-- | Cardinality of a set. |
| 29 | +data Cardinality |
| 30 | + = Shift !Int -- ^ Shift n is equivalent to Card (bit n) |
| 31 | + | Card !Integer |
| 32 | + deriving (Eq, Ord, Show) |
| 33 | + |
| 34 | +instance Enum Cardinality where |
| 35 | + toEnum = fromIntegral |
| 36 | + fromEnum = fromIntegral |
| 37 | + |
| 38 | +instance Num Cardinality where |
| 39 | + fromInteger 1 = Shift 0 -- () |
| 40 | + fromInteger 2 = Shift 1 -- Bool |
| 41 | + fromInteger n = Card n |
| 42 | + {-# INLINE fromInteger #-} |
| 43 | + |
| 44 | + x + y = fromInteger (toInteger x + toInteger y) |
| 45 | + {-# INLINE (+) #-} |
| 46 | + |
| 47 | + Shift x * Shift y = Shift (x + y) |
| 48 | + Shift x * Card y = Card (y `shiftL` x) |
| 49 | + Card x * Shift y = Card (x `shiftL` y) |
| 50 | + Card x * Card y = Card (x * y) |
| 51 | + {-# INLINE (*) #-} |
| 52 | + |
| 53 | + abs = Card . abs . toInteger |
| 54 | + signum = Card . signum . toInteger |
| 55 | + negate = Card . negate . toInteger |
| 56 | + |
| 57 | +instance Real Cardinality where |
| 58 | + toRational = fromIntegral |
| 59 | + |
| 60 | +instance Integral Cardinality where |
| 61 | + toInteger = \case |
| 62 | + Shift n -> bit n |
| 63 | + Card n -> n |
| 64 | + {-# INLINE toInteger #-} |
| 65 | + |
| 66 | + quotRem x' = \case |
| 67 | + Shift n -> (Card (x `shiftR` n), Card (x .&. (bit n - 1))) |
| 68 | + Card n -> let (q, r) = x `quotRem` n in (Card q, Card r) |
| 69 | + where |
| 70 | + x = toInteger x' |
| 71 | + {-# INLINE quotRem #-} |
| 72 | + |
| 73 | +-- | A type class for data with a finite number of inhabitants. |
| 74 | +-- This type class is used |
| 75 | +-- in default implementations of 'System.Random.Stateful.Uniform' |
| 76 | +-- and 'System.Random.Stateful.UniformRange'. |
| 77 | +-- |
| 78 | +-- Users are not supposed to write instances of 'Finite' manually. |
| 79 | +-- There is a default implementation in terms of 'Generic' instead. |
| 80 | +-- |
| 81 | +-- >>> :set -XDeriveGeneric -XDeriveAnyClass |
| 82 | +-- >>> import GHC.Generics (Generic) |
| 83 | +-- >>> data MyBool = MyTrue | MyFalse deriving (Generic, Finite) |
| 84 | +-- >>> data Action = Code MyBool | Eat (Maybe Bool) | Sleep deriving (Generic, Finite) |
| 85 | +-- |
| 86 | +class Finite a where |
| 87 | + cardinality :: Proxy# a -> Cardinality |
| 88 | + toFinite :: Integer -> a |
| 89 | + fromFinite :: a -> Integer |
| 90 | + |
| 91 | + default cardinality :: (Generic a, GFinite (Rep a)) => Proxy# a -> Cardinality |
| 92 | + cardinality _ = gcardinality (proxy# :: Proxy# (Rep a)) |
| 93 | + |
| 94 | + default toFinite :: (Generic a, GFinite (Rep a)) => Integer -> a |
| 95 | + toFinite = to . toGFinite |
| 96 | + |
| 97 | + default fromFinite :: (Generic a, GFinite (Rep a)) => a -> Integer |
| 98 | + fromFinite = fromGFinite . from |
| 99 | + |
| 100 | +class GFinite f where |
| 101 | + gcardinality :: Proxy# f -> Cardinality |
| 102 | + toGFinite :: Integer -> f a |
| 103 | + fromGFinite :: f a -> Integer |
| 104 | + |
| 105 | +instance GFinite V1 where |
| 106 | + gcardinality _ = 0 |
| 107 | + {-# INLINE gcardinality #-} |
| 108 | + toGFinite = const $ error "GFinite: V1 has no inhabitants" |
| 109 | + {-# INLINE toGFinite #-} |
| 110 | + fromGFinite = const $ error "GFinite: V1 has no inhabitants" |
| 111 | + {-# INLINE fromGFinite #-} |
| 112 | + |
| 113 | +instance GFinite U1 where |
| 114 | + gcardinality _ = 1 |
| 115 | + {-# INLINE gcardinality #-} |
| 116 | + toGFinite = const U1 |
| 117 | + {-# INLINE toGFinite #-} |
| 118 | + fromGFinite = const 0 |
| 119 | + {-# INLINE fromGFinite #-} |
| 120 | + |
| 121 | +instance Finite a => GFinite (K1 _x a) where |
| 122 | + gcardinality _ = cardinality (proxy# :: Proxy# a) |
| 123 | + {-# INLINE gcardinality #-} |
| 124 | + toGFinite = K1 . toFinite |
| 125 | + {-# INLINE toGFinite #-} |
| 126 | + fromGFinite = fromFinite . unK1 |
| 127 | + {-# INLINE fromGFinite #-} |
| 128 | + |
| 129 | +instance GFinite a => GFinite (M1 _x _y a) where |
| 130 | + gcardinality _ = gcardinality (proxy# :: Proxy# a) |
| 131 | + {-# INLINE gcardinality #-} |
| 132 | + toGFinite = M1 . toGFinite |
| 133 | + {-# INLINE toGFinite #-} |
| 134 | + fromGFinite = fromGFinite . unM1 |
| 135 | + {-# INLINE fromGFinite #-} |
| 136 | + |
| 137 | +instance (GFinite a, GFinite b) => GFinite (a :+: b) where |
| 138 | + gcardinality _ = |
| 139 | + gcardinality (proxy# :: Proxy# a) + gcardinality (proxy# :: Proxy# b) |
| 140 | + {-# INLINE gcardinality #-} |
| 141 | + |
| 142 | + toGFinite n |
| 143 | + | n < cardA = L1 $ toGFinite n |
| 144 | + | otherwise = R1 $ toGFinite (n - cardA) |
| 145 | + where |
| 146 | + cardA = toInteger (gcardinality (proxy# :: Proxy# a)) |
| 147 | + {-# INLINE toGFinite #-} |
| 148 | + |
| 149 | + fromGFinite = \case |
| 150 | + L1 x -> fromGFinite x |
| 151 | + R1 x -> fromGFinite x + toInteger (gcardinality (proxy# :: Proxy# a)) |
| 152 | + {-# INLINE fromGFinite #-} |
| 153 | + |
| 154 | +instance (GFinite a, GFinite b) => GFinite (a :*: b) where |
| 155 | + gcardinality _ = |
| 156 | + gcardinality (proxy# :: Proxy# a) * gcardinality (proxy# :: Proxy# b) |
| 157 | + {-# INLINE gcardinality #-} |
| 158 | + |
| 159 | + toGFinite n = toGFinite (toInteger q) :*: toGFinite (toInteger r) |
| 160 | + where |
| 161 | + cardB = gcardinality (proxy# :: Proxy# b) |
| 162 | + (q, r) = Card n `quotRem` cardB |
| 163 | + {-# INLINE toGFinite #-} |
| 164 | + |
| 165 | + fromGFinite (q :*: r) = |
| 166 | + toInteger (gcardinality (proxy# :: Proxy# a) * Card (fromGFinite q)) + fromGFinite r |
| 167 | + {-# INLINE fromGFinite #-} |
| 168 | + |
| 169 | +instance Finite Void |
| 170 | +instance Finite () |
| 171 | +instance Finite Bool |
| 172 | +instance Finite Ordering |
| 173 | + |
| 174 | +instance Finite Char where |
| 175 | + cardinality _ = Card $ toInteger (fromEnum (maxBound :: Char)) + 1 |
| 176 | + {-# INLINE cardinality #-} |
| 177 | + toFinite = toEnum . fromInteger |
| 178 | + {-# INLINE toFinite #-} |
| 179 | + fromFinite = toInteger . fromEnum |
| 180 | + {-# INLINE fromFinite #-} |
| 181 | + |
| 182 | +cardinalityDef :: forall a. (Num a, FiniteBits a) => Proxy# a -> Cardinality |
| 183 | +cardinalityDef _ = Shift (finiteBitSize (0 :: a)) |
| 184 | + |
| 185 | +toFiniteDef :: forall a. (Num a, FiniteBits a) => Integer -> a |
| 186 | +toFiniteDef n |
| 187 | + | isSigned (0 :: a) = fromInteger (n - bit (finiteBitSize (0 :: a) - 1)) |
| 188 | + | otherwise = fromInteger n |
| 189 | + |
| 190 | +fromFiniteDef :: (Integral a, FiniteBits a) => a -> Integer |
| 191 | +fromFiniteDef x |
| 192 | + | isSigned x = toInteger x + bit (finiteBitSize x - 1) |
| 193 | + | otherwise = toInteger x |
| 194 | + |
| 195 | +instance Finite Word8 where |
| 196 | + cardinality = cardinalityDef |
| 197 | + {-# INLINE cardinality #-} |
| 198 | + toFinite = toFiniteDef |
| 199 | + {-# INLINE toFinite #-} |
| 200 | + fromFinite = fromFiniteDef |
| 201 | + {-# INLINE fromFinite #-} |
| 202 | +instance Finite Word16 where |
| 203 | + cardinality = cardinalityDef |
| 204 | + {-# INLINE cardinality #-} |
| 205 | + toFinite = toFiniteDef |
| 206 | + {-# INLINE toFinite #-} |
| 207 | + fromFinite = fromFiniteDef |
| 208 | + {-# INLINE fromFinite #-} |
| 209 | +instance Finite Word32 where |
| 210 | + cardinality = cardinalityDef |
| 211 | + {-# INLINE cardinality #-} |
| 212 | + toFinite = toFiniteDef |
| 213 | + {-# INLINE toFinite #-} |
| 214 | + fromFinite = fromFiniteDef |
| 215 | + {-# INLINE fromFinite #-} |
| 216 | +instance Finite Word64 where |
| 217 | + cardinality = cardinalityDef |
| 218 | + {-# INLINE cardinality #-} |
| 219 | + toFinite = toFiniteDef |
| 220 | + {-# INLINE toFinite #-} |
| 221 | + fromFinite = fromFiniteDef |
| 222 | + {-# INLINE fromFinite #-} |
| 223 | +instance Finite Word where |
| 224 | + cardinality = cardinalityDef |
| 225 | + {-# INLINE cardinality #-} |
| 226 | + toFinite = toFiniteDef |
| 227 | + {-# INLINE toFinite #-} |
| 228 | + fromFinite = fromFiniteDef |
| 229 | + {-# INLINE fromFinite #-} |
| 230 | +instance Finite Int8 where |
| 231 | + cardinality = cardinalityDef |
| 232 | + {-# INLINE cardinality #-} |
| 233 | + toFinite = toFiniteDef |
| 234 | + {-# INLINE toFinite #-} |
| 235 | + fromFinite = fromFiniteDef |
| 236 | + {-# INLINE fromFinite #-} |
| 237 | +instance Finite Int16 where |
| 238 | + cardinality = cardinalityDef |
| 239 | + {-# INLINE cardinality #-} |
| 240 | + toFinite = toFiniteDef |
| 241 | + {-# INLINE toFinite #-} |
| 242 | + fromFinite = fromFiniteDef |
| 243 | + {-# INLINE fromFinite #-} |
| 244 | +instance Finite Int32 where |
| 245 | + cardinality = cardinalityDef |
| 246 | + {-# INLINE cardinality #-} |
| 247 | + toFinite = toFiniteDef |
| 248 | + {-# INLINE toFinite #-} |
| 249 | + fromFinite = fromFiniteDef |
| 250 | + {-# INLINE fromFinite #-} |
| 251 | +instance Finite Int64 where |
| 252 | + cardinality = cardinalityDef |
| 253 | + {-# INLINE cardinality #-} |
| 254 | + toFinite = toFiniteDef |
| 255 | + {-# INLINE toFinite #-} |
| 256 | + fromFinite = fromFiniteDef |
| 257 | + {-# INLINE fromFinite #-} |
| 258 | +instance Finite Int where |
| 259 | + cardinality = cardinalityDef |
| 260 | + {-# INLINE cardinality #-} |
| 261 | + toFinite = toFiniteDef |
| 262 | + {-# INLINE toFinite #-} |
| 263 | + fromFinite = fromFiniteDef |
| 264 | + {-# INLINE fromFinite #-} |
| 265 | + |
| 266 | +instance Finite a => Finite (Maybe a) |
| 267 | +instance (Finite a, Finite b) => Finite (Either a b) |
| 268 | +instance (Finite a, Finite b) => Finite (a, b) |
| 269 | +instance (Finite a, Finite b, Finite c) => Finite (a, b, c) |
| 270 | +instance (Finite a, Finite b, Finite c, Finite d) => Finite (a, b, c, d) |
| 271 | +instance (Finite a, Finite b, Finite c, Finite d, Finite e) => Finite (a, b, c, d, e) |
| 272 | +instance (Finite a, Finite b, Finite c, Finite d, Finite e, Finite f) => Finite (a, b, c, d, e, f) |
0 commit comments