{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TemplateHaskell #-}
module Data.Array.Accelerate.Classes.ToFloating (
ToFloating(..),
) where
import Data.Array.Accelerate.Smart
import Data.Array.Accelerate.Type
import Data.Array.Accelerate.Classes.Floating
import Data.Array.Accelerate.Classes.Num
import Language.Haskell.TH hiding ( Exp )
import Control.Monad
import Prelude hiding ( Num, Floating )
class ToFloating a b where
toFloating :: (Num a, Floating b) => Exp a -> Exp b
$(runQ $ do
let
digItOut :: Name -> Q [Name]
digItOut name = do
TyConI (DataD _ _ _ _ cons _) <- reify name
let
dig (NormalC _ [(_, AppT (ConT n) (VarT _))]) = digItOut n
dig (ForallC _ _ (GadtC _ [(_, AppT (ConT n) (VarT _))] _)) = digItOut n
dig (GadtC _ _ (AppT (ConT _) (ConT n))) = return [n]
dig _ = error "Unexpected case generating ToFloating instances"
concat `fmap` mapM dig cons
thToFloating :: Name -> Name -> Q Dec
thToFloating a b =
let
ty = AppT (AppT (ConT (mkName "ToFloating")) (ConT a)) (ConT b)
dec = ValD (VarP (mkName "toFloating")) (NormalB (VarE (mkName f))) []
f | a == b = "id"
| otherwise = "mkToFloating"
in
instanceD (return []) (return ty) [return dec]
as <- digItOut ''NumType
bs <- digItOut ''FloatingType
sequence [ thToFloating a b | a <- as, b <- bs ]
)