{-|
Module      : Monomer.Core.FromFractional
Copyright   : (c) 2018 Francisco Vallarino
License     : BSD-3-Clause (see the LICENSE file)
Maintainer  : fjvallarino@gmail.com
Stability   : experimental
Portability : non-portable

Conversions from Fractional to several types. Used by dial, numericField,
slider and other numeric related widgets.
-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Monomer.Core.FromFractional (
  FromFractional(..)
) where

import Data.Int
import Data.Fixed
import Data.Word
import Foreign.C.Types

fractionalToIntegral :: (Integral a, Real b, Fractional b) => b -> a
fractionalToIntegral :: b -> a
fractionalToIntegral b
num = Rational -> a
forall a b. (RealFrac a, Integral b) => a -> b
round Rational
newNum where
  newNum :: Rational
  newNum :: Rational
newNum = b -> Rational
forall a b. (Real a, Fractional b) => a -> b
realToFrac b
num

-- | Converts a Fractional number to the target type.
class Real a => FromFractional a where
  fromFractional :: (Real b, Fractional b) => b -> a

instance FromFractional Integer where
  fromFractional :: b -> Integer
fromFractional = b -> Integer
forall a b. (Integral a, Real b, Fractional b) => b -> a
fractionalToIntegral

instance FromFractional Int where
  fromFractional :: b -> Int
fromFractional = b -> Int
forall a b. (Integral a, Real b, Fractional b) => b -> a
fractionalToIntegral

instance FromFractional Int8 where
  fromFractional :: b -> Int8
fromFractional = b -> Int8
forall a b. (Integral a, Real b, Fractional b) => b -> a
fractionalToIntegral

instance FromFractional Int16 where
  fromFractional :: b -> Int16
fromFractional = b -> Int16
forall a b. (Integral a, Real b, Fractional b) => b -> a
fractionalToIntegral

instance FromFractional Int32 where
  fromFractional :: b -> Int32
fromFractional = b -> Int32
forall a b. (Integral a, Real b, Fractional b) => b -> a
fractionalToIntegral

instance FromFractional Int64 where
  fromFractional :: b -> Int64
fromFractional = b -> Int64
forall a b. (Integral a, Real b, Fractional b) => b -> a
fractionalToIntegral

instance FromFractional Word where
  fromFractional :: b -> Word
fromFractional = b -> Word
forall a b. (Integral a, Real b, Fractional b) => b -> a
fractionalToIntegral

instance FromFractional Word8 where
  fromFractional :: b -> Word8
fromFractional = b -> Word8
forall a b. (Integral a, Real b, Fractional b) => b -> a
fractionalToIntegral

instance FromFractional Word16 where
  fromFractional :: b -> Word16
fromFractional = b -> Word16
forall a b. (Integral a, Real b, Fractional b) => b -> a
fractionalToIntegral

instance FromFractional Word32 where
  fromFractional :: b -> Word32
fromFractional = b -> Word32
forall a b. (Integral a, Real b, Fractional b) => b -> a
fractionalToIntegral

instance FromFractional Word64 where
  fromFractional :: b -> Word64
fromFractional = b -> Word64
forall a b. (Integral a, Real b, Fractional b) => b -> a
fractionalToIntegral

instance FromFractional Float where
  fromFractional :: b -> Float
fromFractional = b -> Float
forall a b. (Real a, Fractional b) => a -> b
realToFrac

instance FromFractional Double where
  fromFractional :: b -> Double
fromFractional = b -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac

instance FromFractional CFloat where
  fromFractional :: b -> CFloat
fromFractional = b -> CFloat
forall a b. (Real a, Fractional b) => a -> b
realToFrac

instance FromFractional CDouble where
  fromFractional :: b -> CDouble
fromFractional = b -> CDouble
forall a b. (Real a, Fractional b) => a -> b
realToFrac

instance FromFractional Rational where
  fromFractional :: b -> Rational
fromFractional = b -> Rational
forall a b. (Real a, Fractional b) => a -> b
realToFrac

instance HasResolution a => FromFractional (Fixed a) where
  fromFractional :: b -> Fixed a
fromFractional = b -> Fixed a
forall a b. (Real a, Fractional b) => a -> b
realToFrac