#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 702
#endif
module Data.Vector.Unboxed.Deriving
(
derivingUnbox
) where
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative
#endif
import Control.Arrow
import Control.Monad
import Data.Char (isAlphaNum)
import qualified Data.Vector.Generic as G
import qualified Data.Vector.Generic.Mutable as M
import Data.Vector.Unboxed.Base (MVector (..), Vector (..), Unbox)
import Language.Haskell.TH
newPatExp :: String -> Q (Pat, Exp)
newPatExp = fmap (VarP &&& VarE) . newName
data Common = Common
{ mvName, vName :: Name
, i, n, mv, mv', v :: (Pat, Exp) }
common :: String -> Q Common
common name = do
let valid c = c == '_' || c == '\'' || c == '#' || isAlphaNum c
unless (all valid name) $ do
fail (show name ++ " is not a valid constructor suffix!")
let mvName = mkName ("MV_" ++ name)
let vName = mkName ("V_" ++ name)
i <- newPatExp "idx"
n <- newPatExp "len"
mv <- first (ConP mvName . (:[])) <$> newPatExp "mvec"
mv' <- first (ConP mvName . (:[])) <$> newPatExp "mvec'"
v <- first (ConP vName . (:[])) <$> newPatExp "vec"
return Common {..}
capture :: Name -> Name
#if __GLASGOW_HASKELL__ == 704
capture = mkName . nameBase
#else
capture = id
#endif
liftE :: Exp -> Exp -> Exp
liftE e = InfixE (Just e) (VarE 'liftM) . Just
wrap :: Name -> [(Pat, Exp)] -> (Exp -> Exp) -> [Dec]
wrap fun (unzip -> (pats, exps)) coerce = [inline, method] where
name = capture fun
#if MIN_VERSION_template_haskell(2,8,0)
inline = PragmaD (InlineP name Inline FunLike AllPhases)
#else
inline = PragmaD ( InlineP name (InlineSpec True False Nothing) )
#endif
body = coerce $ foldl AppE (VarE fun) exps
method = FunD name [Clause pats (NormalB body) []]
derivingUnbox
:: String
-> TypeQ
-> ExpQ
-> ExpQ
-> DecsQ
derivingUnbox name argsQ toRepQ fromRepQ = do
Common {..} <- common name
toRep <- toRepQ
fromRep <- fromRepQ
a <- second (AppE toRep) <$> newPatExp "val"
args <- argsQ
(cxts, typ, rep) <- case args of
ForallT _ cxts (ArrowT `AppT` typ `AppT` rep) -> return (cxts, typ, rep)
ArrowT `AppT` typ `AppT` rep -> return ([], typ, rep)
_ -> fail "Expecting a type of the form: cxts => typ -> rep"
s <- VarT <$> newName "s"
let newtypeMVector = NewtypeInstD [] ''MVector [s, typ]
#if MIN_VERSION_template_haskell(2,11,0)
Nothing
#endif
(NormalC mvName [(
#if MIN_VERSION_template_haskell(2,11,0)
Bang NoSourceUnpackedness NoSourceStrictness
#else
NotStrict
#endif
, ConT ''MVector `AppT` s `AppT` rep
)]) []
let mvCon = ConE mvName
let instanceMVector = InstanceD cxts
(ConT ''M.MVector `AppT` ConT ''MVector `AppT` typ) $ concat
[ wrap 'M.basicLength [mv] id
, wrap 'M.basicUnsafeSlice [i, n, mv] (AppE mvCon)
, wrap 'M.basicOverlaps [mv, mv'] id
, wrap 'M.basicUnsafeNew [n] (liftE mvCon)
#if MIN_VERSION_vector(0,11,0)
, wrap 'M.basicInitialize [mv] id
#endif
, wrap 'M.basicUnsafeReplicate [n, a] (liftE mvCon)
, wrap 'M.basicUnsafeRead [mv, i] (liftE fromRep)
, wrap 'M.basicUnsafeWrite [mv, i, a] id
, wrap 'M.basicClear [mv] id
, wrap 'M.basicSet [mv, a] id
, wrap 'M.basicUnsafeCopy [mv, mv'] id
, wrap 'M.basicUnsafeMove [mv, mv'] id
, wrap 'M.basicUnsafeGrow [mv, n] (liftE mvCon) ]
let newtypeVector = NewtypeInstD [] ''Vector [typ]
#if MIN_VERSION_template_haskell(2,11,0)
Nothing
#endif
(NormalC vName [(
#if MIN_VERSION_template_haskell(2,11,0)
Bang NoSourceUnpackedness NoSourceStrictness
#else
NotStrict
#endif
, ConT ''Vector `AppT` rep
)]) []
let vCon = ConE vName
let instanceVector = InstanceD cxts
(ConT ''G.Vector `AppT` ConT ''Vector `AppT` typ) $ concat
[ wrap 'G.basicUnsafeFreeze [mv] (liftE vCon)
, wrap 'G.basicUnsafeThaw [v] (liftE mvCon)
, wrap 'G.basicLength [v] id
, wrap 'G.basicUnsafeSlice [i, n, v] (AppE vCon)
, wrap 'G.basicUnsafeIndexM [v, i] (liftE fromRep)
, wrap 'G.basicUnsafeCopy [mv, v] id
, wrap 'G.elemseq [v, a] id ]
return [ InstanceD cxts (ConT ''Unbox `AppT` typ) []
, newtypeMVector, instanceMVector
, newtypeVector, instanceVector ]
#undef __GLASGOW_HASKELL__