@@ -9,7 +9,7 @@ module Control.Distributed.Process.Internal.Closure.TH
9 , mkClosure
9 , mkClosure
12−import Prelude hiding (lookup)
12+import Prelude hiding (succ, any)
13import Control.Applicative ((<$>))
13import Control.Applicative ((<$>))
14import Language.Haskell.TH
14import Language.Haskell.TH
15 ( -- Q monad and operations
15 ( -- Q monad and operations
@@ -39,9 +39,14 @@ import Language.Haskell.TH
43import Data.Binary (encode)
42import Data.Binary (encode)
43+import Data.Generics (everywhereM, mkM, gmapM)
44import Data.Rank1Dynamic (toDynamic)
44import Data.Rank1Dynamic (toDynamic)
45+import Data.Rank1Typeable
45import Control.Distributed.Static
50import Control.Distributed.Static
46 ( RemoteTable
51 ( RemoteTable
47 , registerStatic
52 , registerStatic
@@ -147,7 +152,7 @@ generateDefs n = do
147 static <- generateStatic origName typVars typ
152 static <- generateStatic origName typVars typ
148 let dyn = case typVars of
153 let dyn = case typVars of
149 [] -> [| toDynamic $(varE origName) |]
154 [] -> [| toDynamic $(varE origName) |]
150− _ -> [| toDynamic $(varE origName) |]
155+ _ -> [| toDynamic ($(varE origName) :: $(monomorphize typVars typ)) |]
151 return ( static
156 return ( static
152 , [ [| registerStatic $(stringE (show origName)) $dyn |] ]
157 , [ [| registerStatic $(stringE (show origName)) $dyn |] ]
@@ -159,7 +164,34 @@ generateDefs n = do
159 return ( sdict
164 return ( sdict
160 , [ [| registerStatic $(stringE (show dictName)) $dyn |] ]
165 , [ [| registerStatic $(stringE (show dictName)) $dyn |] ]
168+-- | Turn a polymorphic type into a monomorphic type using ANY and co
169+monomorphize :: [TyVarBndr] -> Type -> Q Type
171+ let subst = zip (map tyVarBndrName tvs) anys
172+ in everywhereM (mkM (applySubst subst))
175+ anys = map typVar (iterate succ zero)
177+ typVar :: Q Type -> Q Type
178+ typVar t = [t| TypVar $t |]
183+ succ :: Q Type -> Q Type
184+ succ t = [t| Succ $t |]
186+ applySubst :: [(Name, Q Type)] -> Type -> Q Type
187+ applySubst s (VarT n) =
189+ Nothing -> return (VarT n)
191+ applySubst s t = gmapM (mkM (applySubst s)) t
163-- | Generate a static value
195-- | Generate a static value
164generateStatic :: Name -> [TyVarBndr] -> Type -> Q [Dec]
196generateStatic :: Name -> [TyVarBndr] -> Type -> Q [Dec]
165generateStatic n xs typ = do
197generateStatic n xs typ = do
@@ -174,8 +206,7 @@ generateStatic n xs typ = do
176 typeable :: TyVarBndr -> Pred
208 typeable :: TyVarBndr -> Pred
177− typeable (PlainTV v) = ClassP (mkName "Typeable") [VarT v]
209+ typeable tv = ClassP (mkName "Typeable") [VarT (tyVarBndrName tv)]
178− typeable (KindedTV v _) = ClassP (mkName "Typeable") [VarT v]
180-- | Generate a serialization dictionary with name 'n' for type 'typ'
211-- | Generate a serialization dictionary with name 'n' for type 'typ'
181generateDict :: Name -> Type -> Q [Dec]
212generateDict :: Name -> Type -> Q [Dec]
@@ -219,3 +250,8 @@ getType name = do
219-- | Variation on 'funD' which takes a single expression to define the function
250-- | Variation on 'funD' which takes a single expression to define the function
220sfnD :: Name -> Q Exp -> Q Dec
251sfnD :: Name -> Q Exp -> Q Dec
221sfnD n e = funD n [clause [] (normalB e) []]
252sfnD n e = funD n [clause [] (normalB e) []]
254+-- | The name of a type variable binding occurrence
255+tyVarBndrName :: TyVarBndr -> Name
256+tyVarBndrName (PlainTV n) = n
257+tyVarBndrName (KindedTV n _) = n