{-# Language CPP #-}
-- |
-- Module      : Basement.Compat.NumLiteral
-- License     : BSD-style
-- Maintainer  : Foundation
--
-- Literal support for Integral and Fractional
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
module Basement.Compat.NumLiteral
    ( Integral(..)
    , Fractional(..)
    , HasNegation(..)
    ) where

import           Prelude (Int, Integer, Rational, Float, Double)
import           Data.Word (Word8, Word16, Word32, Word64, Word)
import           Data.Int (Int8, Int16, Int32, Int64)
import           Basement.Compat.C.Types
import qualified Prelude
import           Basement.Compat.Natural
import           Foreign.Ptr (IntPtr)

-- | Integral Literal support
--
-- e.g. 123 :: Integer
--      123 :: Word8
class Integral a where
    fromInteger :: Integer -> a

-- | Fractional Literal support
--
-- e.g. 1.2  :: Double
--      0.03 :: Float
class Fractional a where
    fromRational :: Rational -> a

-- | Negation support
--
-- e.g. -(f x)
class HasNegation a where
    negate :: a -> a

instance Integral Integer where
    fromInteger :: Integer -> Integer
fromInteger a :: Integer
a = Integer
a
instance Integral Natural where
    fromInteger :: Integer -> Natural
fromInteger a :: Integer
a = Integer -> Natural
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral Int where
    fromInteger :: Integer -> Int
fromInteger a :: Integer
a = Integer -> Int
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral Word where
    fromInteger :: Integer -> Word
fromInteger a :: Integer
a = Integer -> Word
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral Word8 where
    fromInteger :: Integer -> Word8
fromInteger a :: Integer
a = Integer -> Word8
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral Word16 where
    fromInteger :: Integer -> Word16
fromInteger a :: Integer
a = Integer -> Word16
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral Word32 where
    fromInteger :: Integer -> Word32
fromInteger a :: Integer
a = Integer -> Word32
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral Word64 where
    fromInteger :: Integer -> Word64
fromInteger a :: Integer
a = Integer -> Word64
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral Int8 where
    fromInteger :: Integer -> Int8
fromInteger a :: Integer
a = Integer -> Int8
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral Int16 where
    fromInteger :: Integer -> Int16
fromInteger a :: Integer
a = Integer -> Int16
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral Int32 where
    fromInteger :: Integer -> Int32
fromInteger a :: Integer
a = Integer -> Int32
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral Int64 where
    fromInteger :: Integer -> Int64
fromInteger a :: Integer
a = Integer -> Int64
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral IntPtr where
    fromInteger :: Integer -> IntPtr
fromInteger a :: Integer
a = Integer -> IntPtr
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a

instance Integral Float where
    fromInteger :: Integer -> Float
fromInteger a :: Integer
a = Integer -> Float
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral Double where
    fromInteger :: Integer -> Double
fromInteger a :: Integer
a = Integer -> Double
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a

instance Integral CChar where
    fromInteger :: Integer -> CChar
fromInteger a :: Integer
a = Integer -> CChar
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral CSChar where
    fromInteger :: Integer -> CSChar
fromInteger a :: Integer
a = Integer -> CSChar
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral CUChar where
    fromInteger :: Integer -> CUChar
fromInteger a :: Integer
a = Integer -> CUChar
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral CShort where
    fromInteger :: Integer -> CShort
fromInteger a :: Integer
a = Integer -> CShort
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral CUShort where
    fromInteger :: Integer -> CUShort
fromInteger a :: Integer
a = Integer -> CUShort
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral CInt where
    fromInteger :: Integer -> CInt
fromInteger a :: Integer
a = Integer -> CInt
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral CUInt where
    fromInteger :: Integer -> CUInt
fromInteger a :: Integer
a = Integer -> CUInt
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral CLong where
    fromInteger :: Integer -> CLong
fromInteger a :: Integer
a = Integer -> CLong
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral CULong where
    fromInteger :: Integer -> CULong
fromInteger a :: Integer
a = Integer -> CULong
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral CPtrdiff where
    fromInteger :: Integer -> CPtrdiff
fromInteger a :: Integer
a = Integer -> CPtrdiff
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral CSize where
    fromInteger :: Integer -> CSize
fromInteger a :: Integer
a = Integer -> CSize
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral CWchar where
    fromInteger :: Integer -> CWchar
fromInteger a :: Integer
a = Integer -> CWchar
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral CSigAtomic where
    fromInteger :: Integer -> CSigAtomic
fromInteger a :: Integer
a = Integer -> CSigAtomic
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral CLLong where
    fromInteger :: Integer -> CLLong
fromInteger a :: Integer
a = Integer -> CLLong
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral CULLong where
    fromInteger :: Integer -> CULLong
fromInteger a :: Integer
a = Integer -> CULLong
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
#if MIN_VERSION_base(4, 10, 0)
instance Integral CBool where
    fromInteger :: Integer -> CBool
fromInteger a :: Integer
a = Integer -> CBool
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
#endif
instance Integral CIntPtr where
    fromInteger :: Integer -> CIntPtr
fromInteger a :: Integer
a = Integer -> CIntPtr
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral CUIntPtr where
    fromInteger :: Integer -> CUIntPtr
fromInteger a :: Integer
a = Integer -> CUIntPtr
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral CIntMax where
    fromInteger :: Integer -> CIntMax
fromInteger a :: Integer
a = Integer -> CIntMax
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral CUIntMax where
    fromInteger :: Integer -> CUIntMax
fromInteger a :: Integer
a = Integer -> CUIntMax
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral CClock where
    fromInteger :: Integer -> CClock
fromInteger a :: Integer
a = Integer -> CClock
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral CTime where
    fromInteger :: Integer -> CTime
fromInteger a :: Integer
a = Integer -> CTime
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral CUSeconds where
    fromInteger :: Integer -> CUSeconds
fromInteger a :: Integer
a = Integer -> CUSeconds
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral CSUSeconds where
    fromInteger :: Integer -> CSUSeconds
fromInteger a :: Integer
a = Integer -> CSUSeconds
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral COff where
    fromInteger :: Integer -> COff
fromInteger a :: Integer
a = Integer -> COff
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral CFloat where
    fromInteger :: Integer -> CFloat
fromInteger a :: Integer
a = Integer -> CFloat
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a
instance Integral CDouble where
    fromInteger :: Integer -> CDouble
fromInteger a :: Integer
a = Integer -> CDouble
forall a. Num a => Integer -> a
Prelude.fromInteger Integer
a

instance HasNegation Integer where
    negate :: Integer -> Integer
negate = Integer -> Integer
forall a. Num a => a -> a
Prelude.negate
instance HasNegation Int where
    negate :: Int -> Int
negate = Int -> Int
forall a. Num a => a -> a
Prelude.negate
instance HasNegation Int8 where
    negate :: Int8 -> Int8
negate = Int8 -> Int8
forall a. Num a => a -> a
Prelude.negate
instance HasNegation Int16 where
    negate :: Int16 -> Int16
negate = Int16 -> Int16
forall a. Num a => a -> a
Prelude.negate
instance HasNegation Int32 where
    negate :: Int32 -> Int32
negate = Int32 -> Int32
forall a. Num a => a -> a
Prelude.negate
instance HasNegation Int64 where
    negate :: Int64 -> Int64
negate = Int64 -> Int64
forall a. Num a => a -> a
Prelude.negate
instance HasNegation Word where
    negate :: Word -> Word
negate = Word -> Word
forall a. Num a => a -> a
Prelude.negate
instance HasNegation Word8 where
    negate :: Word8 -> Word8
negate = Word8 -> Word8
forall a. Num a => a -> a
Prelude.negate
instance HasNegation Word16 where
    negate :: Word16 -> Word16
negate = Word16 -> Word16
forall a. Num a => a -> a
Prelude.negate
instance HasNegation Word32 where
    negate :: Word32 -> Word32
negate = Word32 -> Word32
forall a. Num a => a -> a
Prelude.negate
instance HasNegation Word64 where
    negate :: Word64 -> Word64
negate = Word64 -> Word64
forall a. Num a => a -> a
Prelude.negate

instance HasNegation Float where
    negate :: Float -> Float
negate = Float -> Float
forall a. Num a => a -> a
Prelude.negate
instance HasNegation Double where
    negate :: Double -> Double
negate = Double -> Double
forall a. Num a => a -> a
Prelude.negate

instance HasNegation CChar where
    negate :: CChar -> CChar
negate = CChar -> CChar
forall a. Num a => a -> a
Prelude.negate
instance HasNegation CSChar where
    negate :: CSChar -> CSChar
negate = CSChar -> CSChar
forall a. Num a => a -> a
Prelude.negate
instance HasNegation CShort where
    negate :: CShort -> CShort
negate = CShort -> CShort
forall a. Num a => a -> a
Prelude.negate
instance HasNegation CInt where
    negate :: CInt -> CInt
negate = CInt -> CInt
forall a. Num a => a -> a
Prelude.negate
instance HasNegation CLong where
    negate :: CLong -> CLong
negate = CLong -> CLong
forall a. Num a => a -> a
Prelude.negate
instance HasNegation CPtrdiff where
    negate :: CPtrdiff -> CPtrdiff
negate = CPtrdiff -> CPtrdiff
forall a. Num a => a -> a
Prelude.negate
instance HasNegation CWchar where
    negate :: CWchar -> CWchar
negate = CWchar -> CWchar
forall a. Num a => a -> a
Prelude.negate
instance HasNegation CLLong where
    negate :: CLLong -> CLLong
negate = CLLong -> CLLong
forall a. Num a => a -> a
Prelude.negate
instance HasNegation CIntMax where
    negate :: CIntMax -> CIntMax
negate = CIntMax -> CIntMax
forall a. Num a => a -> a
Prelude.negate

instance HasNegation CFloat where
    negate :: CFloat -> CFloat
negate = CFloat -> CFloat
forall a. Num a => a -> a
Prelude.negate
instance HasNegation CDouble where
    negate :: CDouble -> CDouble
negate = CDouble -> CDouble
forall a. Num a => a -> a
Prelude.negate

instance Fractional Rational where
    fromRational :: Rational -> Rational
fromRational a :: Rational
a = Rational -> Rational
forall a. Fractional a => Rational -> a
Prelude.fromRational Rational
a
instance Fractional Float where
    fromRational :: Rational -> Float
fromRational a :: Rational
a = Rational -> Float
forall a. Fractional a => Rational -> a
Prelude.fromRational Rational
a
instance Fractional Double where
    fromRational :: Rational -> Double
fromRational a :: Rational
a = Rational -> Double
forall a. Fractional a => Rational -> a
Prelude.fromRational Rational
a

instance Fractional CFloat where
    fromRational :: Rational -> CFloat
fromRational a :: Rational
a = Rational -> CFloat
forall a. Fractional a => Rational -> a
Prelude.fromRational Rational
a
instance Fractional CDouble where
    fromRational :: Rational -> CDouble
fromRational a :: Rational
a = Rational -> CDouble
forall a. Fractional a => Rational -> a
Prelude.fromRational Rational
a