papa-0.7.0: src/Papa/Base/Data/IntCast.hs
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
-- | Conversions between integral types that cannot lose information,
-- checked by the type checker.
--
-- 'intCast' converts only where every value of the source type is a value of
-- the target type; any other conversion is a compile error that names both
-- types. For a conversion that may not fit, use
-- 'Papa.Base.Data.Integral.toIntegralSized', which checks the value and
-- returns a 'Data.Maybe.Maybe'. "Prelude"'s @fromIntegral@, which wraps around
-- (@fromIntegral (300 :: Int) :: Word8@ is @44@) or throws (to 'Natural' from a
-- negative number), is in "Papa.Unsafe.Numeric".
--
-- The relation holds on every platform that GHC supports: 'Int' and 'Word'
-- are at least 32 and at most 64 bits wide, so @'Int32' → 'Int' → 'Int64'@
-- and @'Word32' → 'Word' → 'Word64'@ are lossless, but @'Int64' → 'Int'@ (lossless
-- only where 'Int' has 64 bits) is not, and goes through
-- 'Papa.Base.Data.Integral.toIntegralSized'.
--
-- After the @int-cast@ package (Herbert Valerio Riedel, BSD-3-Clause), whose
-- relation is computed per platform with CPP.
--
-- @since 0.7.0
module Papa.Base.Data.IntCast (
intCast,
IntCast,
IsIntSubType,
) where
import Data.Bool (Bool (False, True))
import Data.Int (Int, Int16, Int32, Int64, Int8)
import Data.Kind (Constraint)
import Data.Word (Word, Word16, Word32, Word64, Word8)
import GHC.TypeLits (ErrorMessage (ShowType, Text, (:$$:), (:<>:)), TypeError)
import Numeric.Natural (Natural)
import Prelude (Integer, Integral, Num)
import qualified Prelude as P
-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import qualified Papa.Unsafe.Numeric as U
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- | Whether every value of the integral type @a@ is a value of the integral
-- type @b@, on every platform: 'True' for the pairs below, and for any type
-- to itself; 'False' for every other pair.
--
-- @since 0.7.0
type family IsIntSubType a b :: Bool where
IsIntSubType a a = 'True
-- Signed: Int8 < Int16 < Int32 < Int < Int64 < Integer
IsIntSubType Int8 Int16 = 'True
IsIntSubType Int8 Int32 = 'True
IsIntSubType Int8 Int = 'True
IsIntSubType Int8 Int64 = 'True
IsIntSubType Int8 Integer = 'True
IsIntSubType Int16 Int32 = 'True
IsIntSubType Int16 Int = 'True
IsIntSubType Int16 Int64 = 'True
IsIntSubType Int16 Integer = 'True
IsIntSubType Int32 Int = 'True
IsIntSubType Int32 Int64 = 'True
IsIntSubType Int32 Integer = 'True
IsIntSubType Int Int64 = 'True
IsIntSubType Int Integer = 'True
IsIntSubType Int64 Integer = 'True
-- Unsigned: Word8 < Word16 < Word32 < Word < Word64 < Natural < Integer
IsIntSubType Word8 Word16 = 'True
IsIntSubType Word8 Word32 = 'True
IsIntSubType Word8 Word = 'True
IsIntSubType Word8 Word64 = 'True
IsIntSubType Word8 Natural = 'True
IsIntSubType Word8 Integer = 'True
IsIntSubType Word16 Word32 = 'True
IsIntSubType Word16 Word = 'True
IsIntSubType Word16 Word64 = 'True
IsIntSubType Word16 Natural = 'True
IsIntSubType Word16 Integer = 'True
IsIntSubType Word32 Word = 'True
IsIntSubType Word32 Word64 = 'True
IsIntSubType Word32 Natural = 'True
IsIntSubType Word32 Integer = 'True
IsIntSubType Word Word64 = 'True
IsIntSubType Word Natural = 'True
IsIntSubType Word Integer = 'True
IsIntSubType Word64 Natural = 'True
IsIntSubType Word64 Integer = 'True
IsIntSubType Natural Integer = 'True
-- Unsigned into a wider signed type
IsIntSubType Word8 Int16 = 'True
IsIntSubType Word8 Int32 = 'True
IsIntSubType Word8 Int = 'True
IsIntSubType Word8 Int64 = 'True
IsIntSubType Word16 Int32 = 'True
IsIntSubType Word16 Int = 'True
IsIntSubType Word16 Int64 = 'True
IsIntSubType Word32 Int64 = 'True
IsIntSubType a b = 'False
-- | The constraint of 'intCast': satisfied where 'IsIntSubType' is 'True, and
-- a compile error naming both types where it is 'False.
type family IntCastCheck a b (ok :: Bool) :: Constraint where
IntCastCheck a b 'True = ()
IntCastCheck a b 'False =
TypeError
( 'Text "intCast: not every value of type "
':<>: 'ShowType a
':<>: 'Text " fits in type "
':<>: 'ShowType b
':$$: 'Text "Use toIntegralSized, which checks the value and returns a Maybe."
)
-- | Every value of @a@ is a value of @b@.
--
-- @since 0.7.0
type IntCast a b = IntCastCheck a b (IsIntSubType a b)
-- | Convert an integral value to a type that holds every value of its type.
-- The value is unchanged: @'P.toInteger' . 'intCast' == 'P.toInteger'@.
--
-- ==== __Examples__
--
-- >>> intCast (200 :: Word8) :: Int
-- 200
--
-- >>> intCast (-5 :: Int32) :: Integer
-- -5
--
-- A conversion that may lose information does not compile:
--
-- >>> intCast (300 :: Int) :: Word8
-- ...
-- ...not every value of type Int fits in type Word8...
-- ...
--
-- >>> intCast (-1 :: Int) :: Natural
-- ...
-- ...not every value of type Int fits in type Natural...
-- ...
--
-- >>> check $ withTests 200 $ property $ do x <- forAll (Gen.int32 Range.linearBounded); toInteger (intCast x :: Int64) === toInteger x
-- ...passed 200 tests.
-- True
--
-- @since 0.7.0
intCast :: (Integral a, Num b, IntCast a b) => a -> b
intCast =
P.fromIntegral
{-# INLINE intCast #-}