@@ -1,6 +1,9 @@
11-- | Derived (TH generated) closures
22module Control.Distributed.Process.Internal.Closure.Derived
33 ( remoteTable
4+ -- * Process primitives
5+ , linkClosure
6+ , unlinkClosure
47 -- * Generic combinators
58 , closureApply
69 , closureConst
@@ -23,28 +26,32 @@ module Control.Distributed.Process.Internal.Closure.Derived
2326 , cpEither
2427 , cpUntag
2528 , cpFanIn
26− , cpApply
2729 -- * Derived process operators
2830 , cpBind
2931 , cpSeq
32+ -- * Serialization dictionaries
33+ , serializableDictUnit
3034 ) where
3135
3236import Prelude hiding (lookup)
33−import Data.Binary (encode)
34−import Data.Typeable (typeOf, Typeable)
37+import Data.Typeable (Typeable)
3538import Data.Tuple (swap)
3639import Control.Applicative ((<$>))
3740import Control.Monad ((>=>))
3841import Control.Distributed.Process.Internal.Types
3942 ( Closure(..)
40− , Static(..)
41− , StaticLabel(..)
4243 , Process
4344 , RemoteTable
45+ , SerializableDict(..)
46+ , ProcessId
4447 )
4548import Control.Distributed.Process.Internal.TypeRep () -- Binary instances
4649import Control.Distributed.Process.Internal.Closure.TH (remotable, mkClosure)
47−import Control.Distributed.Process.Internal.Primitives (unClosure)
50+import Control.Distributed.Process.Internal.Primitives
51+ ( link
52+ , unlink
53+ )
54+import Control.Distributed.Process.Internal.Closure.BuiltIn (closureApply)
4855
4956--------------------------------------------------------------------------------
5057-- TH stuff --
@@ -80,10 +87,8 @@ untagProcess :: Either a a -> Process a
8087untagProcess (Left x) = return x
8188untagProcess (Right y) = return y
8289
83−applyClosureProcess :: (Typeable a, Typeable b) => (Closure (a -> Process b), a) -> Process b
84−applyClosureProcess (procClosure, a) = do
85− proc <- unClosure procClosure
86− proc a
90+serializableDictUnit :: SerializableDict ()
91+serializableDictUnit = SerializableDict
8792
8893remotable [ 'const
8994 , 'idUnit
@@ -95,18 +100,29 @@ remotable [ 'const
95100 , 'leftProcess
96101 , 'mirrorProcess
97102 , 'untagProcess
103+ , 'link
104+ , 'unlink
105+ , 'serializableDictUnit
98106 ]
99107
100108remoteTable :: RemoteTable -> RemoteTable
101109remoteTable = __remoteTable
102110
103111--------------------------------------------------------------------------------
104−-- Generic closure combinators --
112+-- Process primitives --
105113--------------------------------------------------------------------------------
106114
107−closureApply :: Closure (a -> b) -> Closure a -> Closure b
108−closureApply (Closure (Static labelf) envf) (Closure (Static labelx) envx) =
109− Closure (Static ClosureApply) $ encode (labelf, envf, labelx, envx)
115+-- | Closure version of 'link'
116+linkClosure :: ProcessId -> Closure (Process ())
117+linkClosure = $(mkClosure 'link)
118+
119+-- | Closure version of 'unlink'
120+unlinkClosure :: ProcessId -> Closure (Process ())
121+unlinkClosure = $(mkClosure 'unlink)
122+
123+--------------------------------------------------------------------------------
124+-- Generic closure combinators --
125+--------------------------------------------------------------------------------
110126
111127closureConst :: (Typeable a, Typeable b) => Closure (a -> b -> a)
112128closureConst = $(mkClosure 'const)
@@ -128,47 +144,40 @@ cpElim :: Typeable a
128144 => CP () a -> Closure (Process a)
129145cpElim = flip closureApply closureUnit
130146
131−cpId :: forall a. Typeable a
132− => CP a a
147+cpId :: Typeable a => CP a a
133148cpId = $(mkClosure 'returnProcess)
134149
135−cpComp :: forall a b c. (Typeable a, Typeable b, Typeable c)
136− => CP a b -> CP b c -> CP a c
150+cpComp :: (Typeable a, Typeable b, Typeable c) => CP a b -> CP b c -> CP a c
137151cpComp f g = $(mkClosure 'kleisliComposeProcess) `closureApply` f `closureApply` g
138152
139−cpFirst :: forall a b c. (Typeable a, Typeable b, Typeable c)
140− => CP a b -> CP (a, c) (b, c)
153+cpFirst :: (Typeable a, Typeable b, Typeable c) => CP a b -> CP (a, c) (b, c)
141154cpFirst = closureApply $(mkClosure 'firstProcess)
142155
143−cpSwap :: forall a b. (Typeable a, Typeable b)
144− => CP (a, b) (b, a)
156+cpSwap :: (Typeable a, Typeable b) => CP (a, b) (b, a)
145157cpSwap = $(mkClosure 'swapProcess)
146158
147−cpSecond :: (Typeable a, Typeable b, Typeable c)
148− => CP a b -> CP (c, a) (c, b)
159+cpSecond :: (Typeable a, Typeable b, Typeable c) => CP a b -> CP (c, a) (c, b)
149160cpSecond f = cpSwap `cpComp` cpFirst f `cpComp` cpSwap
150161
151162cpPair :: (Typeable a, Typeable a', Typeable b, Typeable b')
152− => CP a b -> CP a' b' -> CP (a, a') (b, b')
163+ => CP a b -> CP a' b' -> CP (a, a') (b, b')
153164cpPair f g = cpFirst f `cpComp` cpSecond g
154165
155−cpCopy :: forall a. Typeable a
156− => CP a (a, a)
166+cpCopy :: Typeable a => CP a (a, a)
157167cpCopy = $(mkClosure 'copyProcess)
158168
159−cpFanOut :: (Typeable a, Typeable b, Typeable c)
169+cpFanOut :: (Typeable a, Typeable b, Typeable c)
160170 => CP a b -> CP a c -> CP a (b, c)
161171cpFanOut f g = cpCopy `cpComp` (f `cpPair` g)
162172
163−cpLeft :: forall a b c. (Typeable a, Typeable b, Typeable c)
173+cpLeft :: (Typeable a, Typeable b, Typeable c)
164174 => CP a b -> CP (Either a c) (Either b c)
165175cpLeft = closureApply $(mkClosure 'leftProcess)
166176
167−cpMirror :: forall a b. (Typeable a, Typeable b)
168− => CP (Either a b) (Either b a)
177+cpMirror :: (Typeable a, Typeable b) => CP (Either a b) (Either b a)
169178cpMirror = $(mkClosure 'mirrorProcess)
170179
171−cpRight :: forall a b c. (Typeable a, Typeable b, Typeable c)
180+cpRight :: (Typeable a, Typeable b, Typeable c)
172181 => CP a b -> CP (Either c a) (Either c b)
173182cpRight f = cpMirror `cpComp` cpLeft f `cpComp` cpMirror
174183
@@ -176,24 +185,13 @@ cpEither :: (Typeable a, Typeable a', Typeable b, Typeable b')
176185 => CP a b -> CP a' b' -> CP (Either a a') (Either b b')
177186cpEither f g = cpLeft f `cpComp` cpRight g
178187
179−cpUntag :: forall a. Typeable a
180− => CP (Either a a) a
188+cpUntag :: Typeable a => CP (Either a a) a
181189cpUntag = $(mkClosure 'untagProcess)
182190
183191cpFanIn :: (Typeable a, Typeable b, Typeable c)
184192 => CP a c -> CP b c -> CP (Either a b) c
185193cpFanIn f g = (f `cpEither` g) `cpComp` cpUntag
186194
187−cpApply :: forall a b. (Typeable a, Typeable b)
188− => CP (CP a b, a) b
189−cpApply = Closure (Static CpApply) $ encode ( typeOf aux
190− , typeOf (undefined :: a)
191− , typeOf (undefined :: Process b)
192− )
193− where
194− aux :: (Closure (a -> Process b), a) -> Process b
195− aux = undefined
196−
197195--------------------------------------------------------------------------------
198196-- Some derived operators for processes --
199197--------------------------------------------------------------------------------