1-- | Template Haskell support
2{-# LANGUAGE TemplateHaskell, CPP #-}
3module Control.Distributed.Process.Internal.Closure.TH
14import Prelude hiding (succ, any)
15import Control.Applicative ((<$>))
16import Language.Haskell.TH
17 ( -- Q monad and operations
26 -- Algebraic data types
29 , Type(AppT, ForallT, VarT, ArrowT)
31 , TyVarBndr(PlainTV, KindedTV)
32#if ! MIN_VERSION_template_haskell(2,10,0)
38 -- Lifted constructors
47 -- .. Top-level declarations
51import Data.Maybe (catMaybes)
52import Data.Binary (encode)
53import Data.Generics (everywhereM, mkM, gmapM)
54import Data.Rank1Dynamic (toDynamic)
55import Data.Rank1Typeable
60import Control.Distributed.Static
69import Control.Distributed.Process.Internal.Types (Process)
70import Control.Distributed.Process.Serializable
71 ( SerializableDict(SerializableDict)
73import Control.Distributed.Process.Internal.Closure.BuiltIn (staticDecode)
75#if MIN_VERSION_template_haskell(2,10,0)
79--------------------------------------------------------------------------------
81--------------------------------------------------------------------------------
83-- | Create the closure, decoder, and metadata definitions for the given list
85remotable :: [Name] -> Q [Dec]
87 types <- mapM getType ns
88 (closures, inserts) <- unzip <$> mapM generateDefs types
89 rtable <- createMetaData (mkName "__remoteTable") (concat inserts)
90 return $ concat closures ++ rtable
92-- | Like 'remotable', but parameterized by the declaration of a function
93-- instead of the function name. So where for 'remotable' you'd do
100-- with 'remotableDecl' you would instead do
103-- > [d| f :: T1 -> T2 ;
108-- 'remotableDecl' creates the function specified as well as the various
109-- dictionaries and static versions that 'remotable' also creates.
110-- 'remotableDecl' is sometimes necessary when you want to refer to, say,
111-- @$(mkClosure 'f)@ within the definition of @f@ itself.
113-- NOTE: 'remotableDecl' creates @__remoteTableDecl@ instead of @__remoteTable@
114-- so that you can use both 'remotable' and 'remotableDecl' within the same
116remotableDecl :: [Q [Dec]] -> Q [Dec]
117remotableDecl qDecs = do
118 decs <- concat <$> sequence qDecs
119 let types = catMaybes (map typeOf decs)
120 (closures, inserts) <- unzip <$> mapM generateDefs types
121 rtable <- createMetaData (mkName "__remoteTableDecl") (concat inserts)
122 return $ decs ++ concat closures ++ rtable
124 typeOf :: Dec -> Maybe (Name, Type)
125 typeOf (SigD name typ) = Just (name, typ)
128-- | Construct a static value.
130-- If @f : forall a1 .. an. T@
131-- then @$(mkStatic 'f) :: forall a1 .. an. Static T@.
132-- Be sure to pass 'f' to 'remotable'.
133mkStatic :: Name -> Q Exp
134mkStatic = varE . staticName
136-- | If @f : T1 -> T2@ is a monomorphic function
137-- then @$(functionSDict 'f) :: Static (SerializableDict T1)@.
139-- Be sure to pass 'f' to 'remotable'.
140functionSDict :: Name -> Q Exp
141functionSDict = varE . sdictName
143-- | If @f : T1 -> Process T2@ is a monomorphic function
144-- then @$(functionTDict 'f) :: Static (SerializableDict T2)@.
146-- Be sure to pass 'f' to 'remotable'.
147functionTDict :: Name -> Q Exp
148functionTDict = varE . tdictName
150-- | If @f : T1 -> T2@ then @$(mkClosure 'f) :: T1 -> Closure T2@.
152-- TODO: The current version of mkClosure is too polymorphic
153-- (@forall a. Binary a => a -> Closure T2).
154mkClosure :: Name -> Q Exp
156 [| closure ($(mkStatic n) `staticCompose` staticDecode $(functionSDict n))
160-- | Make a 'Closure' from a static function. This is useful for
161-- making a closure for a top-level @Process ()@ function, because
162-- using 'mkClosure' would require adding a dummy @()@ argument.
164mkStaticClosure :: Name -> Q Exp
165mkStaticClosure n = [| staticClosure $( mkStatic n ) |]
167--------------------------------------------------------------------------------
168-- Internal (Template Haskell) --
169--------------------------------------------------------------------------------
171-- | Generate the code to add the metadata to the CH runtime
172createMetaData :: Name -> [Q Exp] -> Q [Dec]
173createMetaData name is =
174 sequence [ sigD name [t| RemoteTable -> RemoteTable |]
175 , sfnD name (compose is)
178generateDefs :: (Name, Type) -> Q ([Dec], [Q Exp])
179generateDefs (origName, fullType) = do
180 proc <- [t| Process |]
181 let (typVars, typ') = case fullType of ForallT vars [] mono -> (vars, mono)
184 -- The main "static" entry
185 (static, register) <- makeStatic typVars typ'
187 -- If n :: T1 -> T2, static serializable dictionary for T1
188 -- TODO: we should check if arg is an instance of Serializable, but we cannot
189 -- http://hackage.haskell.org/trac/ghc/ticket/7066
190 (sdict, registerSDict) <- case (typVars, typ') of
191 ([], ArrowT `AppT` arg `AppT` _res) ->
192 makeDict (sdictName origName) arg
196 -- If n :: T1 -> Process T2, static serializable dictionary for T2
197 -- TODO: check if T2 is serializable (same as above)
198 (tdict, registerTDict) <- case (typVars, typ') of
199 ([], ArrowT `AppT` _arg `AppT` (proc' `AppT` res)) | proc' == proc ->
200 makeDict (tdictName origName) res
204 return ( concat [static, sdict, tdict]
205 , concat [register, registerSDict, registerTDict]
208 makeStatic :: [TyVarBndr] -> Type -> Q ([Dec], [Q Exp])
209 makeStatic typVars typ = do
210 static <- generateStatic origName typVars typ
211 let dyn = case typVars of
212 [] -> [| toDynamic $(varE origName) |]
213 _ -> [| toDynamic ($(varE origName) :: $(monomorphize typVars typ)) |]
215 , [ [| registerStatic $(showFQN origName) $dyn |] ]
218 makeDict :: Name -> Type -> Q ([Dec], [Q Exp])
219 makeDict dictName typ = do
220 sdict <- generateDict dictName typ
221 let dyn = [| toDynamic (SerializableDict :: SerializableDict $(return typ)) |]
223 , [ [| registerStatic $(showFQN dictName) $dyn |] ]
226-- | Turn a polymorphic type into a monomorphic type using ANY and co
227monomorphize :: [TyVarBndr] -> Type -> Q Type
229 let subst = zip (map tyVarBndrName tvs) anys
230 in everywhereM (mkM (applySubst subst))
233 anys = map typVar (iterate succ zero)
235 typVar :: Q Type -> Q Type
236 typVar t = [t| TypVar $t |]
241 succ :: Q Type -> Q Type
242 succ t = [t| Succ $t |]
244 applySubst :: [(Name, Q Type)] -> Type -> Q Type
245 applySubst s (VarT n) =
247 Nothing -> return (VarT n)
249 applySubst s t = gmapM (mkM (applySubst s)) t
251-- | Generate a static value
252generateStatic :: Name -> [TyVarBndr] -> Type -> Q [Dec]
253generateStatic n xs typ = do
254 staticTyp <- [t| Static |]
256 [ sigD (staticName n) $ do
257 txs <- sequence $ map typeable xs
260 (staticTyp `AppT` typ))
261 , sfnD (staticName n) [| staticLabel $(showFQN n) |]
264 typeable :: TyVarBndr -> Q Pred
265 typeable tv = conT (mkName "Typeable") `appT` varT (tyVarBndrName tv)
267-- | Generate a serialization dictionary with name 'n' for type 'typ'
268generateDict :: Name -> Type -> Q [Dec]
269generateDict n typ = do
271 [ sigD n $ [t| Static (SerializableDict $(return typ)) |]
272 , sfnD n [| staticLabel $(showFQN n) |]
275staticName :: Name -> Name
276staticName n = mkName $ nameBase n ++ "__static"
278sdictName :: Name -> Name
279sdictName n = mkName $ nameBase n ++ "__sdict"
281tdictName :: Name -> Name
282tdictName n = mkName $ nameBase n ++ "__tdict"
284--------------------------------------------------------------------------------
285-- Generic Template Haskell auxiliary functions --
286--------------------------------------------------------------------------------
288-- | Compose a set of expressions
289compose :: [Q Exp] -> Q Exp
292compose (e:es) = [| $e . $(compose es) |]
294-- | Literal string as an expression
295stringE :: String -> Q Exp
296stringE = litE . stringL
298-- | Look up the "original name" (module:name) and type of a top-level function
299getType :: Name -> Q (Name, Type)
303#if MIN_VERSION_template_haskell(2,11,0)
304 VarI origName typ _ -> return (origName, typ)
306 VarI origName typ _ _ -> return (origName, typ)
308 _ -> fail $ show name ++ " not found"
310-- | Variation on 'funD' which takes a single expression to define the function
311sfnD :: Name -> Q Exp -> Q Dec
312sfnD n e = funD n [clause [] (normalB e) []]
314-- | The name of a type variable binding occurrence
315tyVarBndrName :: TyVarBndr -> Name
316tyVarBndrName (PlainTV n) = n
317tyVarBndrName (KindedTV n _) = n
319-- | Fully qualified name; that is, the name and the _current_ module
321-- We ignore the module part of the Name argument (which may or may not exist)
322-- because we construct various names (`staticName`, `sdictName`, `tdictName`)
323-- and those names certainly won't have Module components.
324showFQN :: Name -> Q Exp
327 stringE (loc_module loc ++ "." ++ nameBase n)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list