{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE Rank2Types #-}
module Quantum.Synthesis.ToReal where
import Quantum.Synthesis.ArcTan2
import Data.Number.FixedPrec
class ToReal a where
to_real :: (Floating r, ArcTan2 r) => a -> r
instance ToReal Rational where
to_real = fromRational
instance ToReal Integer where
to_real = fromInteger
instance ToReal Int where
to_real = fromIntegral
instance ToReal Double where
to_real = fromRational . toRational
instance ToReal Float where
to_real = fromRational . toRational
instance (Precision e) => ToReal (FixedPrec e) where
to_real = fromRational . toRational
dynamic_fixedprec :: forall a r.(ToReal r) => Integer -> (forall e.(Precision e) => FixedPrec e -> a) -> r -> a
dynamic_fixedprec d f x = loop d (undefined :: P0)
where
loop :: forall e.(Precision e) => Integer -> e -> a
loop d e
| d >= 1000 = loop (d-1000) (undefined :: PPlus1000 e)
| d >= 100 = loop (d-100) (undefined :: PPlus100 e)
| d >= 10 = loop (d-10) (undefined :: PPlus10 e)
| d > 0 = loop (d-1) (undefined :: PPlus1 e)
| otherwise = f (to_real x :: FixedPrec e)
dynamic_fixedprec2 :: forall a r s.(ToReal r, ToReal s) => Integer -> (forall e.(Precision e) => FixedPrec e -> FixedPrec e -> a) -> r -> s -> a
dynamic_fixedprec2 d f x y = loop d (undefined :: P0)
where
loop :: forall e.(Precision e) => Integer -> e -> a
loop d e
| d >= 1000 = loop (d-1000) (undefined :: PPlus1000 e)
| d >= 100 = loop (d-100) (undefined :: PPlus100 e)
| d >= 10 = loop (d-10) (undefined :: PPlus10 e)
| d > 0 = loop (d-1) (undefined :: PPlus1 e)
| otherwise = f (to_real x :: FixedPrec e) (to_real y :: FixedPrec e)