Expose maybeTryCast function

This commit is contained in:
Taylor Fausak 2021-04-23 11:39:20 +00:00 committed by GitHub
parent e03f079c49
commit 7895a07eea
3 changed files with 128 additions and 115 deletions

View File

@ -140,6 +140,7 @@ module Witch
, Witch.Utility.as
, Witch.Utility.over
, Witch.Utility.via
, Witch.Utility.maybeTryCast
, Witch.Utility.tryVia
-- ** Unsafe

View File

@ -59,27 +59,27 @@ instance Cast.Cast Int.Int8 Integer where
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int8 Word.Word8 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int8 Word.Word16 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int8 Word.Word32 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int8 Word.Word64 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int8 Word where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'fromIntegral' when the input is non-negative.
instance TryCast.TryCast Int.Int8 Natural.Natural where
tryCast = maybeTryCast fromNonNegativeIntegral
tryCast = Utility.maybeTryCast fromNonNegativeIntegral
-- | Uses 'fromIntegral'.
instance Cast.Cast Int.Int8 Float where
@ -93,7 +93,7 @@ instance Cast.Cast Int.Int8 Double where
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int16 Int.Int8 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'fromIntegral'.
instance Cast.Cast Int.Int16 Int.Int32 where
@ -113,27 +113,27 @@ instance Cast.Cast Int.Int16 Integer where
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int16 Word.Word8 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int16 Word.Word16 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int16 Word.Word32 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int16 Word.Word64 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int16 Word where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'fromIntegral' when the input is non-negative.
instance TryCast.TryCast Int.Int16 Natural.Natural where
tryCast = maybeTryCast fromNonNegativeIntegral
tryCast = Utility.maybeTryCast fromNonNegativeIntegral
-- | Uses 'fromIntegral'.
instance Cast.Cast Int.Int16 Float where
@ -147,11 +147,11 @@ instance Cast.Cast Int.Int16 Double where
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int32 Int.Int8 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int32 Int.Int16 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'fromIntegral'.
instance Cast.Cast Int.Int32 Int.Int64 where
@ -159,7 +159,7 @@ instance Cast.Cast Int.Int32 Int.Int64 where
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int32 Int where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'fromIntegral'.
instance Cast.Cast Int.Int32 Integer where
@ -167,32 +167,32 @@ instance Cast.Cast Int.Int32 Integer where
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int32 Word.Word8 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int32 Word.Word16 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int32 Word.Word32 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int32 Word.Word64 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int32 Word where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'fromIntegral' when the input is non-negative.
instance TryCast.TryCast Int.Int32 Natural.Natural where
tryCast = maybeTryCast fromNonNegativeIntegral
tryCast = Utility.maybeTryCast fromNonNegativeIntegral
-- | Uses 'fromIntegral' when the input is between -16,777,215 and 16,777,215
-- inclusive.
instance TryCast.TryCast Int.Int32 Float where
tryCast = maybeTryCast $ \s -> if -maxFloat <= s && s <= maxFloat
tryCast = Utility.maybeTryCast $ \s -> if -maxFloat <= s && s <= maxFloat
then Just $ fromIntegral s
else Nothing
@ -204,19 +204,19 @@ instance Cast.Cast Int.Int32 Double where
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int64 Int.Int8 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int64 Int.Int16 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int64 Int.Int32 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int64 Int where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'fromIntegral'.
instance Cast.Cast Int.Int64 Integer where
@ -224,39 +224,39 @@ instance Cast.Cast Int.Int64 Integer where
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int64 Word.Word8 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int64 Word.Word16 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int64 Word.Word32 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int64 Word.Word64 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int.Int64 Word where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'fromIntegral' when the input is non-negative.
instance TryCast.TryCast Int.Int64 Natural.Natural where
tryCast = maybeTryCast fromNonNegativeIntegral
tryCast = Utility.maybeTryCast fromNonNegativeIntegral
-- | Uses 'fromIntegral' when the input is between -16,777,215 and 16,777,215
-- inclusive.
instance TryCast.TryCast Int.Int64 Float where
tryCast = maybeTryCast $ \s -> if -maxFloat <= s && s <= maxFloat
tryCast = Utility.maybeTryCast $ \s -> if -maxFloat <= s && s <= maxFloat
then Just $ fromIntegral s
else Nothing
-- | Uses 'fromIntegral' when the input is between -9,007,199,254,740,991 and
-- 9,007,199,254,740,991 inclusive.
instance TryCast.TryCast Int.Int64 Double where
tryCast = maybeTryCast $ \s -> if -maxDouble <= s && s <= maxDouble
tryCast = Utility.maybeTryCast $ \s -> if -maxDouble <= s && s <= maxDouble
then Just $ fromIntegral s
else Nothing
@ -264,15 +264,15 @@ instance TryCast.TryCast Int.Int64 Double where
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int Int.Int8 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int Int.Int16 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int Int.Int32 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'fromIntegral'.
instance Cast.Cast Int Int.Int64 where
@ -284,39 +284,39 @@ instance Cast.Cast Int Integer where
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int Word.Word8 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int Word.Word16 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int Word.Word32 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int Word.Word64 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Int Word where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'fromIntegral' when the input is non-negative.
instance TryCast.TryCast Int Natural.Natural where
tryCast = maybeTryCast fromNonNegativeIntegral
tryCast = Utility.maybeTryCast fromNonNegativeIntegral
-- | Uses 'fromIntegral' when the input is between -16,777,215 and 16,777,215
-- inclusive.
instance TryCast.TryCast Int Float where
tryCast = maybeTryCast $ \s -> if -maxFloat <= s && s <= maxFloat
tryCast = Utility.maybeTryCast $ \s -> if -maxFloat <= s && s <= maxFloat
then Just $ fromIntegral s
else Nothing
-- | Uses 'fromIntegral' when the input is between -9,007,199,254,740,991 and
-- 9,007,199,254,740,991 inclusive.
instance TryCast.TryCast Int Double where
tryCast = maybeTryCast $ \s ->
tryCast = Utility.maybeTryCast $ \s ->
if (toInteger (maxBound :: Int) <= maxDouble)
|| (-maxDouble <= s && s <= maxDouble)
then Just $ fromIntegral s
@ -326,43 +326,43 @@ instance TryCast.TryCast Int Double where
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Integer Int.Int8 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Integer Int.Int16 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Integer Int.Int32 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Integer Int.Int64 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Integer Int where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Integer Word.Word8 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Integer Word.Word16 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Integer Word.Word32 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Integer Word.Word64 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Integer Word where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'fromInteger' when the input is non-negative.
instance TryCast.TryCast Integer Natural.Natural where
@ -371,19 +371,19 @@ instance TryCast.TryCast Integer Natural.Natural where
-- @fromIntegral@ with @fromInteger@, we can work around the bug.
-- https://mail.haskell.org/pipermail/haskell-cafe/2021-March/133540.html
tryCast =
maybeTryCast $ \s -> if s < 0 then Nothing else Just $ fromInteger s
Utility.maybeTryCast $ \s -> if s < 0 then Nothing else Just $ fromInteger s
-- | Uses 'fromIntegral' when the input is between -16,777,215 and 16,777,215
-- inclusive.
instance TryCast.TryCast Integer Float where
tryCast = maybeTryCast $ \s -> if -maxFloat <= s && s <= maxFloat
tryCast = Utility.maybeTryCast $ \s -> if -maxFloat <= s && s <= maxFloat
then Just $ fromIntegral s
else Nothing
-- | Uses 'fromIntegral' when the input is between -9,007,199,254,740,991 and
-- 9,007,199,254,740,991 inclusive.
instance TryCast.TryCast Integer Double where
tryCast = maybeTryCast $ \s -> if -maxDouble <= s && s <= maxDouble
tryCast = Utility.maybeTryCast $ \s -> if -maxDouble <= s && s <= maxDouble
then Just $ fromIntegral s
else Nothing
@ -411,7 +411,7 @@ instance Cast.Cast Word.Word8 Natural.Natural where
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word.Word8 Int.Int8 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'fromIntegral'.
instance Cast.Cast Word.Word8 Int.Int16 where
@ -445,7 +445,7 @@ instance Cast.Cast Word.Word8 Double where
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word.Word16 Word.Word8 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'fromIntegral'.
instance Cast.Cast Word.Word16 Word.Word32 where
@ -465,11 +465,11 @@ instance Cast.Cast Word.Word16 Natural.Natural where
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word.Word16 Int.Int8 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word.Word16 Int.Int16 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'fromIntegral'.
instance Cast.Cast Word.Word16 Int.Int32 where
@ -499,11 +499,11 @@ instance Cast.Cast Word.Word16 Double where
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word.Word32 Word.Word8 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word.Word32 Word.Word16 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'fromIntegral'.
instance Cast.Cast Word.Word32 Word.Word64 where
@ -511,7 +511,7 @@ instance Cast.Cast Word.Word32 Word.Word64 where
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word.Word32 Word where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'fromIntegral'.
instance Cast.Cast Word.Word32 Natural.Natural where
@ -519,15 +519,15 @@ instance Cast.Cast Word.Word32 Natural.Natural where
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word.Word32 Int.Int8 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word.Word32 Int.Int16 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word.Word32 Int.Int32 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'fromIntegral'.
instance Cast.Cast Word.Word32 Int.Int64 where
@ -535,7 +535,7 @@ instance Cast.Cast Word.Word32 Int.Int64 where
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word.Word32 Int where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'fromIntegral'.
instance Cast.Cast Word.Word32 Integer where
@ -544,7 +544,7 @@ instance Cast.Cast Word.Word32 Integer where
-- | Uses 'fromIntegral' when the input is between -16,777,215 and 16,777,215
-- inclusive.
instance TryCast.TryCast Word.Word32 Float where
tryCast = maybeTryCast
tryCast = Utility.maybeTryCast
$ \s -> if s <= maxFloat then Just $ fromIntegral s else Nothing
-- | Uses 'fromIntegral'.
@ -555,19 +555,19 @@ instance Cast.Cast Word.Word32 Double where
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word.Word64 Word.Word8 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word.Word64 Word.Word16 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word.Word64 Word.Word32 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word.Word64 Word where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'fromIntegral'.
instance Cast.Cast Word.Word64 Natural.Natural where
@ -575,23 +575,23 @@ instance Cast.Cast Word.Word64 Natural.Natural where
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word.Word64 Int.Int8 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word.Word64 Int.Int16 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word.Word64 Int.Int32 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word.Word64 Int.Int64 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word.Word64 Int where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'fromIntegral'.
instance Cast.Cast Word.Word64 Integer where
@ -600,28 +600,28 @@ instance Cast.Cast Word.Word64 Integer where
-- | Uses 'fromIntegral' when the input is between -16,777,215 and 16,777,215
-- inclusive.
instance TryCast.TryCast Word.Word64 Float where
tryCast = maybeTryCast
tryCast = Utility.maybeTryCast
$ \s -> if s <= maxFloat then Just $ fromIntegral s else Nothing
-- | Uses 'fromIntegral' when the input is between -9,007,199,254,740,991 and
-- 9,007,199,254,740,991 inclusive.
instance TryCast.TryCast Word.Word64 Double where
tryCast = maybeTryCast
tryCast = Utility.maybeTryCast
$ \s -> if s <= maxDouble then Just $ fromIntegral s else Nothing
-- Word
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word Word.Word8 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word Word.Word16 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word Word.Word32 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'fromIntegral'.
instance Cast.Cast Word Word.Word64 where
@ -633,23 +633,23 @@ instance Cast.Cast Word Natural.Natural where
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word Int.Int8 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word Int.Int16 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word Int.Int32 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word Int.Int64 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Word Int where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'fromIntegral'.
instance Cast.Cast Word Integer where
@ -658,13 +658,13 @@ instance Cast.Cast Word Integer where
-- | Uses 'fromIntegral' when the input is between -16,777,215 and 16,777,215
-- inclusive.
instance TryCast.TryCast Word Float where
tryCast = maybeTryCast
tryCast = Utility.maybeTryCast
$ \s -> if s <= maxFloat then Just $ fromIntegral s else Nothing
-- | Uses 'fromIntegral' when the input is between -9,007,199,254,740,991 and
-- 9,007,199,254,740,991 inclusive.
instance TryCast.TryCast Word Double where
tryCast = maybeTryCast $ \s ->
tryCast = Utility.maybeTryCast $ \s ->
if (toInteger (maxBound :: Word) <= maxDouble) || (s <= maxDouble)
then Just $ fromIntegral s
else Nothing
@ -673,43 +673,43 @@ instance TryCast.TryCast Word Double where
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Natural.Natural Word.Word8 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Natural.Natural Word.Word16 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Natural.Natural Word.Word32 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Natural.Natural Word.Word64 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Natural.Natural Word where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Natural.Natural Int.Int8 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Natural.Natural Int.Int16 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Natural.Natural Int.Int32 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Natural.Natural Int.Int64 where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'Bits.toIntegralSized'.
instance TryCast.TryCast Natural.Natural Int where
tryCast = maybeTryCast Bits.toIntegralSized
tryCast = Utility.maybeTryCast Bits.toIntegralSized
-- | Uses 'fromIntegral'.
instance Cast.Cast Natural.Natural Integer where
@ -718,13 +718,13 @@ instance Cast.Cast Natural.Natural Integer where
-- | Uses 'fromIntegral' when the input is between -16,777,215 and 16,777,215
-- inclusive.
instance TryCast.TryCast Natural.Natural Float where
tryCast = maybeTryCast
tryCast = Utility.maybeTryCast
$ \s -> if s <= maxFloat then Just $ fromIntegral s else Nothing
-- | Uses 'fromIntegral' when the input is between -9,007,199,254,740,991 and
-- 9,007,199,254,740,991 inclusive.
instance TryCast.TryCast Natural.Natural Double where
tryCast = maybeTryCast
tryCast = Utility.maybeTryCast
$ \s -> if s <= maxDouble then Just $ fromIntegral s else Nothing
-- Float
@ -785,7 +785,7 @@ instance TryCast.TryCast Float Natural.Natural where
-- | Uses 'toRational' when the input is not NaN or infinity.
instance TryCast.TryCast Float Rational where
tryCast = maybeTryCast
tryCast = Utility.maybeTryCast
$ \s -> if isNaN s || isInfinite s then Nothing else Just $ toRational s
-- | Uses 'realToFrac'.
@ -850,7 +850,7 @@ instance TryCast.TryCast Double Natural.Natural where
-- | Uses 'toRational' when the input is not NaN or infinity.
instance TryCast.TryCast Double Rational where
tryCast = maybeTryCast
tryCast = Utility.maybeTryCast
$ \s -> if isNaN s || isInfinite s then Nothing else Just $ toRational s
-- | Uses 'realToFrac'. This necessarily loses some precision.
@ -865,7 +865,7 @@ instance Integral a => Cast.Cast a (Ratio.Ratio a) where
-- | Uses 'Ratio.numerator' when the denominator is 1.
instance (Eq a, Num a) => TryCast.TryCast (Ratio.Ratio a) a where
tryCast = maybeTryCast $ \s ->
tryCast = Utility.maybeTryCast $ \s ->
if Ratio.denominator s == 1 then Just $ Ratio.numerator s else Nothing
-- | Uses 'fromRational'. This necessarily loses some precision.
@ -896,14 +896,14 @@ instance Num a => Cast.Cast a (Complex.Complex a) where
-- | Uses 'Complex.realPart' when the imaginary part is 0.
instance (Eq a, Num a) => TryCast.TryCast (Complex.Complex a) a where
tryCast = maybeTryCast $ \s ->
tryCast = Utility.maybeTryCast $ \s ->
if Complex.imagPart s == 0 then Just $ Complex.realPart s else Nothing
-- NonEmpty
-- | Uses 'NonEmpty.nonEmpty'.
instance TryCast.TryCast [a] (NonEmpty.NonEmpty a) where
tryCast = maybeTryCast NonEmpty.nonEmpty
tryCast = Utility.maybeTryCast NonEmpty.nonEmpty
-- | Uses 'NonEmpty.toList'.
instance Cast.Cast (NonEmpty.NonEmpty a) [a] where
@ -1085,12 +1085,6 @@ instance
fromNonNegativeIntegral :: (Integral s, Num t) => s -> Maybe t
fromNonNegativeIntegral x = if x < 0 then Nothing else Just $ fromIntegral x
maybeTryCast
:: (s -> Maybe t) -> s -> Either (TryCastException.TryCastException s t) t
maybeTryCast f s = case f s of
Nothing -> Left $ TryCastException.TryCastException s Nothing
Just t -> Right t
-- | The maximum integral value that can be unambiguously represented as a
-- 'Float'. Equal to 16,777,215.
maxFloat :: Num a => a

View File

@ -125,6 +125,24 @@ tryInto
-> Either (TryCastException.TryCastException source target) target
tryInto = TryCast.tryCast
-- | This function can be used to implement 'TryCast.tryCast' with a function
-- that returns 'Maybe'. For example:
--
-- > -- Avoid this:
-- > tryCast x = case f x of
-- > Nothing -> Left $ TryCastException x Nothing
-- > Just y -> Right y
-- >
-- > -- Prefer this:
-- > tryCast = maybeTryCast f
maybeTryCast
:: (source -> Maybe target)
-> source
-> Either (TryCastException.TryCastException source target) target
maybeTryCast f s = case f s of
Nothing -> Left $ TryCastException.TryCastException s Nothing
Just t -> Right t
-- | This is similar to 'via' except that it works with 'TryCast.TryCast'
-- instances instead. This function is especially convenient because juggling
-- the types in the 'TryCastException.TryCastException' can be tedious.