{-|
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 :: forall a b. (Integral a, Real b, Fractional b) => b -> a
fractionalToIntegral b
num = forall a b. (RealFrac a, Integral b) => a -> b
round Rational
newNum where
  newNum :: Rational
  newNum :: Rational
newNum = 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 :: forall b. (Real b, Fractional b) => b -> Integer
fromFractional = forall a b. (Integral a, Real b, Fractional b) => b -> a
fractionalToIntegral

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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