{-# LANGUAGE AllowAmbiguousTypes  #-}
{-# LANGUAGE ConstraintKinds      #-}
{-# LANGUAGE CPP                  #-}
{-# LANGUAGE TypeInType           #-}
{-# LANGUAGE FlexibleContexts     #-}
{-# LANGUAGE FlexibleInstances    #-}
#if ( __GLASGOW_HASKELL__ >= 806 )
{-# LANGUAGE NoStarIsType               #-}
#endif
{-# LANGUAGE Rank2Types           #-}
{-# LANGUAGE TypeOperators        #-}
{-# LANGUAGE UndecidableInstances #-}
module Time.Rational
       ( Rat (..)
       , type (:%)
#if ( __GLASGOW_HASKELL__ >= 804 )
       , type (%)
       , type (*)
       , type (/)
#endif
       , MulK
       , DivK
#if ( __GLASGOW_HASKELL__ >= 804 )
       , Gcd
       , Normalize
       , DivRat
       , type (>=%)
#endif
        
       , RatioNat
       , KnownRat (..)
#if ( __GLASGOW_HASKELL__ >= 804 )
       , withRuntimeDivRat
#endif
       , KnownDivRat
       ) where
import Data.Kind (Type)
import Data.Proxy (Proxy (..))
import GHC.Natural (Natural)
import GHC.Real (Ratio ((:%)))
#if ( __GLASGOW_HASKELL__ >= 804 )
import GHC.TypeNats (Div, Mod, type (<=?))
#endif
#if ( __GLASGOW_HASKELL__ >= 802 )
import GHC.TypeNats (KnownNat, Nat, natVal)
#else
import GHC.TypeLits (KnownNat, Nat, natVal)
#endif
#if ( __GLASGOW_HASKELL__ >= 804 )
import Unsafe.Coerce (unsafeCoerce)
#endif
#if ( __GLASGOW_HASKELL__ >= 804 )
import qualified GHC.TypeNats
#endif
data Rat = Nat ::% Nat
type family MulK (k1 :: Type) (k2 :: Type) :: Type
type instance MulK Nat Nat = Nat
type instance MulK Rat Rat = Rat
type instance MulK Rat Nat = Rat
type instance MulK Nat Rat = Rat
type family DivK (k1 :: Type) (k2 :: Type) :: Type
type instance DivK Nat Nat = Rat
type instance DivK Rat Rat = Rat
type instance DivK Rat Nat = Rat
type instance DivK Nat Rat = Rat
#if ( __GLASGOW_HASKELL__ >= 804 )
type family (*) (a :: k1) (b :: k2) :: MulK k1 k2
type instance (a :: Nat) * (b :: Nat) = (GHC.TypeNats.*) a b
type instance (a :: Rat) * (b :: Rat) = MulRat a b
type instance (a :: Rat) * (b :: Nat) = MulNatRat b a
type instance (a :: Nat) * (b :: Rat) = MulNatRat a b
type family (/) (a :: k1) (b :: k2) :: DivK k1 k2
type instance (a :: Nat) / (b :: Nat) = a % b
type instance (a :: Rat) / (b :: Rat) = DivRat a b
type instance (a :: Rat) / (b :: Nat) = DivRatNat a b
type instance (a :: Nat) / (b :: Rat) = DivRat (a :% 1) b
#endif
type (:%) = '(::%)
#if ( __GLASGOW_HASKELL__ >= 804 )
type family (m :: Nat) % (n :: Nat) :: Rat where
    a % b = Normalize (a :% b)
infixl 7 %
type family DivRat (m :: Rat) (n :: Rat) :: Rat where
    DivRat (a :% b) (c :% d) = (a * d) % (b * c)
type family MulRat (m :: Rat) (n :: Rat) :: Rat where
    MulRat (a :% b) (c :% d) = (a * c) % (b * d)
type family MulNatRat (n :: Nat) (r :: Rat) :: Rat where
    MulNatRat x (a :% b) = (x * a) % b
type family DivRatNat (r :: Rat) (n :: Nat) :: Rat where
    DivRatNat (a :% b) x = a % (b * x)
type family Gcd (m :: Nat) (n :: Nat) :: Nat where
    Gcd a 0 = a
    Gcd a b = Gcd b (a `Mod` b)
type family Normalize (r :: Rat) :: Rat  where
    Normalize (a :% b) = (a `Div` Gcd a b) :% (b `Div` Gcd a b)
infix 4 >=%
type family (m :: Rat) >=% (n :: Rat) :: Bool where
    (a :% b) >=% (c :% d) = c * b <=? a * d
#endif
type RatioNat = Ratio Natural
class KnownRat (r :: Rat) where
    ratVal :: RatioNat
instance (KnownNat a, KnownNat b) => KnownRat (a :% b) where
#if ( __GLASGOW_HASKELL__ >= 802 )
    ratVal = natVal (Proxy @a) :% natVal (Proxy @b)
#else
    ratVal = fromIntegral (natVal (Proxy @a)) :% fromIntegral (natVal (Proxy @b))
#endif
#if ( __GLASGOW_HASKELL__ >= 804 )
newtype KnownRatDict (unit :: Rat) r = MkKnownRatDict (KnownRat unit => r)
giftRat :: forall (unit :: Rat) r . (KnownRat unit => r) -> RatioNat -> r
giftRat given = unsafeCoerce (MkKnownRatDict given :: KnownRatDict unit r)
{-# INLINE giftRat #-}
withRuntimeDivRat :: forall (a :: Rat) (b :: Rat) r . (KnownRat a, KnownRat b) => (KnownRat (a / b) => r) -> r
withRuntimeDivRat r = giftRat @(a / b) r (ratVal @a / ratVal @b)
{-# INLINE withRuntimeDivRat #-}
#endif
type KnownDivRat a b = ( KnownRat a
                       , KnownRat b
#if ( __GLASGOW_HASKELL__ >= 804 )
                       , KnownRat (a / b)
#endif
                       )