{-# LANGUAGE TemplateHaskell, CPP #-}
module Control.Distributed.Process.Internal.Closure.TH
(
remotable
, remotableDecl
, mkStatic
, functionSDict
, functionTDict
, mkClosure
, mkStaticClosure
) where
import Prelude hiding (succ, any)
import Control.Applicative ((<$>))
import Language.Haskell.TH
(
Q
, reify
, Loc(loc_module)
, location
, Name
, mkName
, nameBase
, Dec(SigD)
, Exp
, Type(AppT, ForallT, VarT, ArrowT)
, Info(VarI)
, TyVarBndr(PlainTV, KindedTV)
, Pred
#if MIN_VERSION_template_haskell(2,10,0)
, conT
, appT
#else
, classP
#endif
, varT
, stringL
, normalB
, clause
, varE
, litE
, funD
, sigD
)
import Data.Maybe (catMaybes)
import Data.Binary (encode)
import Data.Generics (everywhereM, mkM, gmapM)
import Data.Rank1Dynamic (toDynamic)
import Data.Rank1Typeable
( Zero
, Succ
, TypVar
)
import Control.Distributed.Static
( RemoteTable
, registerStatic
, Static
, staticLabel
, closure
, staticCompose
, staticClosure
)
import Control.Distributed.Process.Internal.Types (Process)
import Control.Distributed.Process.Serializable
( SerializableDict(SerializableDict)
)
import Control.Distributed.Process.Internal.Closure.BuiltIn (staticDecode)
remotable :: [Name] -> Q [Dec]
remotable ns = do
types <- mapM getType ns
(closures, inserts) <- unzip <$> mapM generateDefs types
rtable <- createMetaData (mkName "__remoteTable") (concat inserts)
return $ concat closures ++ rtable
remotableDecl :: [Q [Dec]] -> Q [Dec]
remotableDecl qDecs = do
decs <- concat <$> sequence qDecs
let types = catMaybes (map typeOf decs)
(closures, inserts) <- unzip <$> mapM generateDefs types
rtable <- createMetaData (mkName "__remoteTableDecl") (concat inserts)
return $ decs ++ concat closures ++ rtable
where
typeOf :: Dec -> Maybe (Name, Type)
typeOf (SigD name typ) = Just (name, typ)
typeOf _ = Nothing
mkStatic :: Name -> Q Exp
mkStatic = varE . staticName
functionSDict :: Name -> Q Exp
functionSDict = varE . sdictName
functionTDict :: Name -> Q Exp
functionTDict = varE . tdictName
mkClosure :: Name -> Q Exp
mkClosure n =
[| closure ($(mkStatic n) `staticCompose` staticDecode $(functionSDict n))
. encode
|]
mkStaticClosure :: Name -> Q Exp
mkStaticClosure n = [| staticClosure $( mkStatic n ) |]
createMetaData :: Name -> [Q Exp] -> Q [Dec]
createMetaData name is =
sequence [ sigD name [t| RemoteTable -> RemoteTable |]
, sfnD name (compose is)
]
generateDefs :: (Name, Type) -> Q ([Dec], [Q Exp])
generateDefs (origName, fullType) = do
proc <- [t| Process |]
let (typVars, typ') = case fullType of ForallT vars [] mono -> (vars, mono)
_ -> ([], fullType)
(static, register) <- makeStatic typVars typ'
(sdict, registerSDict) <- case (typVars, typ') of
([], ArrowT `AppT` arg `AppT` _res) ->
makeDict (sdictName origName) arg
_ ->
return ([], [])
(tdict, registerTDict) <- case (typVars, typ') of
([], ArrowT `AppT` _arg `AppT` (proc' `AppT` res)) | proc' == proc ->
makeDict (tdictName origName) res
_ ->
return ([], [])
return ( concat [static, sdict, tdict]
, concat [register, registerSDict, registerTDict]
)
where
makeStatic :: [TyVarBndr] -> Type -> Q ([Dec], [Q Exp])
makeStatic typVars typ = do
static <- generateStatic origName typVars typ
let dyn = case typVars of
[] -> [| toDynamic $(varE origName) |]
_ -> [| toDynamic ($(varE origName) :: $(monomorphize typVars typ)) |]
return ( static
, [ [| registerStatic $(showFQN origName) $dyn |] ]
)
makeDict :: Name -> Type -> Q ([Dec], [Q Exp])
makeDict dictName typ = do
sdict <- generateDict dictName typ
let dyn = [| toDynamic (SerializableDict :: SerializableDict $(return typ)) |]
return ( sdict
, [ [| registerStatic $(showFQN dictName) $dyn |] ]
)
monomorphize :: [TyVarBndr] -> Type -> Q Type
monomorphize tvs =
let subst = zip (map tyVarBndrName tvs) anys
in everywhereM (mkM (applySubst subst))
where
anys :: [Q Type]
anys = map typVar (iterate succ zero)
typVar :: Q Type -> Q Type
typVar t = [t| TypVar $t |]
zero :: Q Type
zero = [t| Zero |]
succ :: Q Type -> Q Type
succ t = [t| Succ $t |]
applySubst :: [(Name, Q Type)] -> Type -> Q Type
applySubst s (VarT n) =
case lookup n s of
Nothing -> return (VarT n)
Just t -> t
applySubst s t = gmapM (mkM (applySubst s)) t
generateStatic :: Name -> [TyVarBndr] -> Type -> Q [Dec]
generateStatic n xs typ = do
staticTyp <- [t| Static |]
sequence
[ sigD (staticName n) $ do
txs <- sequence $ map typeable xs
return (ForallT xs
txs
(staticTyp `AppT` typ))
, sfnD (staticName n) [| staticLabel $(showFQN n) |]
]
where
typeable :: TyVarBndr -> Q Pred
typeable tv =
#if MIN_VERSION_template_haskell(2,10,0)
conT (mkName "Typeable") `appT` varT (tyVarBndrName tv)
#else
classP (mkName "Typeable") [varT (tyVarBndrName tv)]
#endif
generateDict :: Name -> Type -> Q [Dec]
generateDict n typ = do
sequence
[ sigD n $ [t| Static (SerializableDict $(return typ)) |]
, sfnD n [| staticLabel $(showFQN n) |]
]
staticName :: Name -> Name
staticName n = mkName $ nameBase n ++ "__static"
sdictName :: Name -> Name
sdictName n = mkName $ nameBase n ++ "__sdict"
tdictName :: Name -> Name
tdictName n = mkName $ nameBase n ++ "__tdict"
compose :: [Q Exp] -> Q Exp
compose [] = [| id |]
compose [e] = e
compose (e:es) = [| $e . $(compose es) |]
stringE :: String -> Q Exp
stringE = litE . stringL
getType :: Name -> Q (Name, Type)
getType name = do
info <- reify name
case info of
#if MIN_VERSION_template_haskell(2,11,0)
VarI origName typ _ -> return (origName, typ)
#else
VarI origName typ _ _ -> return (origName, typ)
#endif
_ -> fail $ show name ++ " not found"
sfnD :: Name -> Q Exp -> Q Dec
sfnD n e = funD n [clause [] (normalB e) []]
tyVarBndrName :: TyVarBndr -> Name
tyVarBndrName (PlainTV n) = n
tyVarBndrName (KindedTV n _) = n
showFQN :: Name -> Q Exp
showFQN n = do
loc <- location
stringE (loc_module loc ++ "." ++ nameBase n)