{-# LANGUAGE ConstraintKinds   #-}
{-# LANGUAGE FlexibleContexts  #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies      #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
-- |
-- Module      : Data.Array.Accelerate.Classes.Fractional
-- Copyright   : [2016..2020] The Accelerate Team
-- License     : BSD3
--
-- Maintainer  : Trevor L. McDonell <trevor.mcdonell@gmail.com>
-- Stability   : experimental
-- Portability : non-portable (GHC extensions)
--

module Data.Array.Accelerate.Classes.Fractional (

  Fractional,
  (P./), P.recip, P.fromRational,

) where

import Data.Array.Accelerate.Smart
import Data.Array.Accelerate.Type

import Data.Array.Accelerate.Classes.Num

import Prelude                                                      ( (.) )
import qualified Prelude                                            as P


-- | Conversion from a 'Rational'.
--
-- A floating point literal representations the application of the function
-- 'fromRational' to a value of type 'Rational'. We export this specialised
-- version where the return type is fixed to an 'Exp' term in order to improve
-- type checking in Accelerate modules when @RebindableSyntax@ is enabled.
--
-- fromRational :: Fractional a => Rational -> Exp a
-- fromRational = P.fromRational


-- | Fractional numbers, supporting real division
--
type Fractional a = (Num a, P.Fractional (Exp a))


instance P.Fractional (Exp Half) where
  / :: Exp Half -> Exp Half -> Exp Half
(/)          = Exp Half -> Exp Half -> Exp Half
forall t. (Elt t, IsFloating (EltR t)) => Exp t -> Exp t -> Exp t
mkFDiv
  recip :: Exp Half -> Exp Half
recip        = Exp Half -> Exp Half
forall t. (Elt t, IsFloating (EltR t)) => Exp t -> Exp t
mkRecip
  fromRational :: Rational -> Exp Half
fromRational = Half -> Exp Half
forall e. (HasCallStack, Elt e) => e -> Exp e
constant (Half -> Exp Half) -> (Rational -> Half) -> Rational -> Exp Half
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> Half
forall a. Fractional a => Rational -> a
P.fromRational

instance P.Fractional (Exp Float) where
  / :: Exp Float -> Exp Float -> Exp Float
(/)          = Exp Float -> Exp Float -> Exp Float
forall t. (Elt t, IsFloating (EltR t)) => Exp t -> Exp t -> Exp t
mkFDiv
  recip :: Exp Float -> Exp Float
recip        = Exp Float -> Exp Float
forall t. (Elt t, IsFloating (EltR t)) => Exp t -> Exp t
mkRecip
  fromRational :: Rational -> Exp Float
fromRational = Float -> Exp Float
forall e. (HasCallStack, Elt e) => e -> Exp e
constant (Float -> Exp Float)
-> (Rational -> Float) -> Rational -> Exp Float
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> Float
forall a. Fractional a => Rational -> a
P.fromRational

instance P.Fractional (Exp Double) where
  / :: Exp Double -> Exp Double -> Exp Double
(/)          = Exp Double -> Exp Double -> Exp Double
forall t. (Elt t, IsFloating (EltR t)) => Exp t -> Exp t -> Exp t
mkFDiv
  recip :: Exp Double -> Exp Double
recip        = Exp Double -> Exp Double
forall t. (Elt t, IsFloating (EltR t)) => Exp t -> Exp t
mkRecip
  fromRational :: Rational -> Exp Double
fromRational = Double -> Exp Double
forall e. (HasCallStack, Elt e) => e -> Exp e
constant (Double -> Exp Double)
-> (Rational -> Double) -> Rational -> Exp Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> Double
forall a. Fractional a => Rational -> a
P.fromRational

instance P.Fractional (Exp CFloat) where
  / :: Exp CFloat -> Exp CFloat -> Exp CFloat
(/)          = Exp CFloat -> Exp CFloat -> Exp CFloat
forall t. (Elt t, IsFloating (EltR t)) => Exp t -> Exp t -> Exp t
mkFDiv
  recip :: Exp CFloat -> Exp CFloat
recip        = Exp CFloat -> Exp CFloat
forall t. (Elt t, IsFloating (EltR t)) => Exp t -> Exp t
mkRecip
  fromRational :: Rational -> Exp CFloat
fromRational = CFloat -> Exp CFloat
forall e. (HasCallStack, Elt e) => e -> Exp e
constant (CFloat -> Exp CFloat)
-> (Rational -> CFloat) -> Rational -> Exp CFloat
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> CFloat
forall a. Fractional a => Rational -> a
P.fromRational

instance P.Fractional (Exp CDouble) where
  / :: Exp CDouble -> Exp CDouble -> Exp CDouble
(/)          = Exp CDouble -> Exp CDouble -> Exp CDouble
forall t. (Elt t, IsFloating (EltR t)) => Exp t -> Exp t -> Exp t
mkFDiv
  recip :: Exp CDouble -> Exp CDouble
recip        = Exp CDouble -> Exp CDouble
forall t. (Elt t, IsFloating (EltR t)) => Exp t -> Exp t
mkRecip
  fromRational :: Rational -> Exp CDouble
fromRational = CDouble -> Exp CDouble
forall e. (HasCallStack, Elt e) => e -> Exp e
constant (CDouble -> Exp CDouble)
-> (Rational -> CDouble) -> Rational -> Exp CDouble
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> CDouble
forall a. Fractional a => Rational -> a
P.fromRational