module Conversion
(
Conversion(..),
convert1,
convert2,
)
where
import BasePrelude
class Conversion a b where
convert :: a -> b
instance Conversion (STM a) (IO a) where
convert = atomically
instance Alternative f => Conversion (Either a b) (f b) where
convert = either (const empty) pure
instance Conversion (Either a b) Bool where
convert = either (const False) (const True)
instance Alternative f => Conversion (Maybe a) (f a) where
convert = maybe empty pure
instance Conversion (Maybe a) Bool where
convert = maybe False (const True)
instance Conversion (Maybe a) (a -> a) where
convert = flip fromMaybe
instance Alternative f => Conversion [a] (f a) where
convert = \case [] -> empty; a : _ -> pure a
instance Conversion [a] Bool where
convert = null
instance Conversion [Maybe a] [a] where
convert = catMaybes
instance Conversion Int Integer where
convert = fromIntegral
instance Alternative f => Conversion Int (f Int8) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int (f Int16) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int (f Int32) where
convert = checkedFromIntegral
instance Conversion Int Int64 where
convert = fromIntegral
instance Alternative f => Conversion Int (f Word) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int (f Word8) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int (f Word16) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int (f Word32) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int (f Word64) where
convert = checkedFromIntegral
instance Conversion Int8 Integer where
convert = fromIntegral
instance Conversion Int8 Int where
convert = fromIntegral
instance Conversion Int8 Int16 where
convert = fromIntegral
instance Conversion Int8 Int32 where
convert = fromIntegral
instance Conversion Int8 Int64 where
convert = fromIntegral
instance Alternative f => Conversion Int8 (f Word) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int8 (f Word8) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int8 (f Word16) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int8 (f Word32) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int8 (f Word64) where
convert = checkedFromIntegral
instance Conversion Int16 Integer where
convert = fromIntegral
instance Conversion Int16 Int where
convert = fromIntegral
instance Alternative f => Conversion Int16 (f Int8) where
convert = checkedFromIntegral
instance Conversion Int16 Int32 where
convert = fromIntegral
instance Conversion Int16 Int64 where
convert = fromIntegral
instance Alternative f => Conversion Int16 (f Word) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int16 (f Word8) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int16 (f Word16) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int16 (f Word32) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int16 (f Word64) where
convert = checkedFromIntegral
instance Conversion Int32 Integer where
convert = fromIntegral
instance Conversion Int32 Int where
convert = fromIntegral
instance Alternative f => Conversion Int32 (f Int8) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int32 (f Int16) where
convert = checkedFromIntegral
instance Conversion Int32 Int64 where
convert = fromIntegral
instance Alternative f => Conversion Int32 (f Word) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int32 (f Word8) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int32 (f Word16) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int32 (f Word32) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int32 (f Word64) where
convert = checkedFromIntegral
instance Conversion Int64 Integer where
convert = fromIntegral
instance Alternative f => Conversion Int64 (f Int) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int64 (f Int8) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int64 (f Int16) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int64 (f Int32) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int64 (f Word) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int64 (f Word8) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int64 (f Word16) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int64 (f Word32) where
convert = checkedFromIntegral
instance Alternative f => Conversion Int64 (f Word64) where
convert = checkedFromIntegral
instance Conversion Word Integer where
convert = fromIntegral
instance Alternative f => Conversion Word (f Int) where
convert = checkedFromIntegral
instance Alternative f => Conversion Word (f Int8) where
convert = checkedFromIntegral
instance Alternative f => Conversion Word (f Int16) where
convert = checkedFromIntegral
instance Alternative f => Conversion Word (f Int32) where
convert = checkedFromIntegral
instance Alternative f => Conversion Word (f Int64) where
convert = checkedFromIntegral
instance Alternative f => Conversion Word (f Word8) where
convert = checkedFromIntegral
instance Alternative f => Conversion Word (f Word16) where
convert = checkedFromIntegral
instance Alternative f => Conversion Word (f Word32) where
convert = checkedFromIntegral
instance Conversion Word Word64 where
convert = fromIntegral
instance Conversion Word8 Integer where
convert = fromIntegral
instance Conversion Word8 Int where
convert = fromIntegral
instance Alternative f => Conversion Word8 (f Int8) where
convert = checkedFromIntegral
instance Conversion Word8 Int16 where
convert = fromIntegral
instance Conversion Word8 Int32 where
convert = fromIntegral
instance Conversion Word8 Int64 where
convert = fromIntegral
instance Conversion Word8 Word where
convert = fromIntegral
instance Conversion Word8 Word16 where
convert = fromIntegral
instance Conversion Word8 Word32 where
convert = fromIntegral
instance Conversion Word8 Word64 where
convert = fromIntegral
instance Conversion Word16 Integer where
convert = fromIntegral
instance Conversion Word16 Int where
convert = fromIntegral
instance Alternative f => Conversion Word16 (f Int8) where
convert = checkedFromIntegral
instance Alternative f => Conversion Word16 (f Int16) where
convert = checkedFromIntegral
instance Conversion Word16 Int32 where
convert = fromIntegral
instance Conversion Word16 Int64 where
convert = fromIntegral
instance Alternative f => Conversion Word16 (f Word) where
convert = checkedFromIntegral
instance Alternative f => Conversion Word16 (f Word8) where
convert = checkedFromIntegral
instance Conversion Word16 Word32 where
convert = fromIntegral
instance Conversion Word16 Word64 where
convert = fromIntegral
instance Conversion Word32 Integer where
convert = fromIntegral
instance Alternative f => Conversion Word32 (f Int) where
convert = checkedFromIntegral
instance Alternative f => Conversion Word32 (f Int8) where
convert = checkedFromIntegral
instance Alternative f => Conversion Word32 (f Int16) where
convert = checkedFromIntegral
instance Alternative f => Conversion Word32 (f Int32) where
convert = checkedFromIntegral
instance Conversion Word32 Int64 where
convert = fromIntegral
instance Conversion Word32 Word where
convert = fromIntegral
instance Alternative f => Conversion Word32 (f Word8) where
convert = checkedFromIntegral
instance Alternative f => Conversion Word32 (f Word16) where
convert = checkedFromIntegral
instance Conversion Word32 Word64 where
convert = fromIntegral
instance Conversion Word64 Integer where
convert = fromIntegral
instance Alternative f => Conversion Word64 (f Int) where
convert = checkedFromIntegral
instance Alternative f => Conversion Word64 (f Int8) where
convert = checkedFromIntegral
instance Alternative f => Conversion Word64 (f Int16) where
convert = checkedFromIntegral
instance Alternative f => Conversion Word64 (f Int32) where
convert = checkedFromIntegral
instance Alternative f => Conversion Word64 (f Int64) where
convert = checkedFromIntegral
instance Alternative f => Conversion Word64 (f Word) where
convert = checkedFromIntegral
instance Alternative f => Conversion Word64 (f Word8) where
convert = checkedFromIntegral
instance Alternative f => Conversion Word64 (f Word16) where
convert = checkedFromIntegral
instance Alternative f => Conversion Word64 (f Word32) where
convert = checkedFromIntegral
instance Alternative f => Conversion Integer (f Int) where
convert = checkedFromIntegral
instance Alternative f => Conversion Integer (f Int8) where
convert = checkedFromIntegral
instance Alternative f => Conversion Integer (f Int16) where
convert = checkedFromIntegral
instance Alternative f => Conversion Integer (f Int32) where
convert = checkedFromIntegral
instance Alternative f => Conversion Integer (f Int64) where
convert = checkedFromIntegral
instance Alternative f => Conversion Integer (f Word) where
convert = checkedFromIntegral
instance Alternative f => Conversion Integer (f Word8) where
convert = checkedFromIntegral
instance Alternative f => Conversion Integer (f Word16) where
convert = checkedFromIntegral
instance Alternative f => Conversion Integer (f Word32) where
convert = checkedFromIntegral
instance Alternative f => Conversion Integer (f Word64) where
convert = checkedFromIntegral
instance Conversion Float Rational where
convert = realToFrac
instance Conversion Float Double where
convert = realToFrac
instance Conversion Double Rational where
convert = realToFrac
isomorphicallyChecked :: (Alternative f, Conversion b a, Eq a) => (a -> b) -> a -> f b
isomorphicallyChecked =
\f a -> f a & \b -> if a == convert b then pure b else empty
checkedFromIntegral :: (Alternative f, Integral a, Integral b) => a -> f b
checkedFromIntegral =
\a -> fromIntegral a & \b -> if fromIntegral b == a then pure b else empty
convert1 :: Conversion (a x) (b x) => a x -> b x
convert1 =
convert
convert2 :: Conversion (a x1 x2) (b x1 x2) => a x1 x2 -> b x1 x2
convert2 =
convert