module Data.Complex.Generic.TH where
import Data.Typeable (typeOf, typeOf1)
import Language.Haskell.TH
import Data.Complex.Generic.Class
import Data.Complex.Generic.Default
deriveComplexRF :: Name -> Name -> Name -> Name -> Q [Dec]
deriveComplexRF cTy' rTy' mkRectI' rectI' = [d|
instance ComplexRect ($(cTy) $(rTy)) $(rTy) where
mkRect = $(mkRectI)
rect = $(rectI)
real = realDefault
imag = imagDefault
realPart = realPartDefault
imagPart = imagPartDefault
conjugate = conjugateDefault
magnitudeSquared = magnitudeSquaredDefault
sqr = sqrDefault
(.*) = rmulDefault
(*.) = mulrDefault
instance ComplexPolar ($(cTy) $(rTy)) $(rTy) where
mkPolar = mkPolarDefault
cis = cisDefault
polar = polarDefault
magnitude = magnitudeDefaultRF
phase = phaseDefaultRF
instance Num ($(cTy) $(rTy)) where
(+) = addDefault
() = subDefault
(*) = mulDefault
negate = negateDefault
fromInteger = fromIntegerDefault
abs = absDefault
signum = signumDefault
instance Fractional ($(cTy) $(rTy)) where
(/) = divDefaultRF
fromRational = fromRationalDefault
instance Floating ($(cTy) $(rTy)) where
pi = piDefault
exp = expDefault
log = logDefault
sqrt = sqrtDefault
sin = sinDefault
cos = cosDefault
tan = tanDefault
sinh = sinhDefault
cosh = coshDefault
tanh = tanhDefault
asin = asinDefault
acos = acosDefault
atan = atanDefault
asinh = asinhDefault
acosh = acoshDefault
atanh = atanhDefault
|]
where
cTy = conT cTy'
rTy = conT rTy'
mkRectI = varE mkRectI'
rectI = varE rectI'
deriveComplexF :: Name -> Name -> Name -> Name -> Q [Dec]
deriveComplexF cTy' rTy' mkRectI' rectI' = [d|
instance ComplexRect ($(cTy) $(rTy)) $(rTy) where
mkRect = $(mkRectI)
rect = $(rectI)
real = realDefault
imag = imagDefault
realPart = realPartDefault
imagPart = imagPartDefault
conjugate = conjugateDefault
magnitudeSquared = magnitudeSquaredDefault
sqr = sqrDefault
(.*) = rmulDefault
(*.) = mulrDefault
instance ComplexPolar ($(cTy) $(rTy)) $(rTy) where
mkPolar = mkPolarDefault
cis = cisDefault
polar = polarDefault
magnitude = magnitudeDefault
phase = phaseDefault
instance Num ($(cTy) $(rTy)) where
(+) = addDefault
() = subDefault
(*) = mulDefault
negate = negateDefault
fromInteger = fromIntegerDefault
abs = absDefault
signum = signumDefault
instance Fractional ($(cTy) $(rTy)) where
(/) = divDefault
fromRational = fromRationalDefault
instance Floating ($(cTy) $(rTy)) where
pi = piDefault
exp = expDefault
log = logDefault
sqrt = sqrtDefault
sin = sinDefault
cos = cosDefault
tan = tanDefault
sinh = sinhDefault
cosh = coshDefault
tanh = tanhDefault
asin = asinDefault
acos = acosDefault
atan = atanDefault
asinh = asinhDefault
acosh = acoshDefault
atanh = atanhDefault
|]
where
cTy = conT cTy'
rTy = conT rTy'
mkRectI = varE mkRectI'
rectI = varE rectI'
deriveComplexN :: Name -> Name -> Name -> Name -> Q [Dec]
deriveComplexN cTy' rTy' mkRectI' rectI' = [d|
instance ComplexRect ($(cTy) $(rTy)) $(rTy) where
mkRect = $(mkRectI)
rect = $(rectI)
real = realDefault
imag = imagDefault
realPart = realPartDefault
imagPart = imagPartDefault
conjugate = conjugateDefault
magnitudeSquared = magnitudeSquaredDefault
sqr = sqrDefault
(.*) = rmulDefault
(*.) = mulrDefault
instance Num ($(cTy) $(rTy)) where
(+) = addDefault
() = subDefault
(*) = mulDefault
negate = negateDefault
fromInteger = fromIntegerDefault
abs = error $ "Num.abs: not implementable for " ++ show (typeOf (undefined :: ($(cTy) $(rTy))))
signum = error $ "Num.signum: not implementable for " ++ show (typeOf (undefined :: ($(cTy) $(rTy))))
|]
where
cTy = conT cTy'
rTy = conT rTy'
mkRectI = varE mkRectI'
rectI = varE rectI'