@@ -1,18 +1,8 @@
1-- | Derived (TH generated) closures
1-- | Derived (TH generated) closures
2{-# LANGUAGE MagicHash #-}
2{-# LANGUAGE MagicHash #-}
3module Control.Distributed.Process.Internal.Closure.Derived
3module Control.Distributed.Process.Internal.Closure.Derived
4− ( -- * Static functionals
10− -- * Creating closures
14− -- * Closure versions of CH primitives
4+ ( -- * Closure versions of CH primitives
16 , cpUnlink
6 , cpUnlink
18 , cpExpect
8 , cpExpect
@@ -20,17 +10,12 @@ module Control.Distributed.Process.Internal.Closure.Derived
20 , cpReturn
10 , cpReturn
23− -- * Serialization dictionaries (and their static versions)
27− , sdictProcessId__static
28 -- * Runtime support
13 -- * Runtime support
29 , __remoteTable
14 , __remoteTable
32−import Data.Binary (encode, decode)
17+import Data.Binary (encode)
33−import Data.ByteString.Lazy (ByteString, empty)
18+import Data.ByteString.Lazy (ByteString)
34import Data.Typeable (Typeable, typeOf, TypeRep, typeRepTyCon, TyCon)
19import Data.Typeable (Typeable, typeOf, TypeRep, typeRepTyCon, TyCon)
35import Control.Applicative ((<$>))
20import Control.Applicative ((<$>))
36import Control.Monad (join)
21import Control.Monad (join)
@@ -55,13 +40,24 @@ import Control.Distributed.Process.Internal.Primitives
55import Control.Distributed.Process.Internal.Closure.TH (remotable, mkStatic)
40import Control.Distributed.Process.Internal.Closure.TH (remotable, mkStatic)
56import Control.Distributed.Process.Internal.MessageT (getLocalNode)
41import Control.Distributed.Process.Internal.MessageT (getLocalNode)
57import Control.Distributed.Process.Internal.Closure.Resolution (resolveClosure)
42import Control.Distributed.Process.Internal.Closure.Resolution (resolveClosure)
43+import Control.Distributed.Process.Internal.Closure.Static
52+ , sdictProcessId__static
54+import Control.Distributed.Process.Internal.Closure.MkClosure (mkClosure)
58import Control.Distributed.Process.Internal.Dynamic
55import Control.Distributed.Process.Internal.Dynamic
59 ( Dynamic(Dynamic)
56 ( Dynamic(Dynamic)
61 , unsafeCoerce#
58 , unsafeCoerce#
62 , dynTypeRep
59 , dynTypeRep
64−import qualified Control.Arrow as Arrow (first, second, (***))
66--------------------------------------------------------------------------------
62--------------------------------------------------------------------------------
67-- Setup: A number of functions that we will pass to 'remotable' --
63-- Setup: A number of functions that we will pass to 'remotable' --
@@ -78,25 +74,8 @@ bindProcess = (>>=)
78joinProcess :: Process (Process a) -> Process a
74joinProcess :: Process (Process a) -> Process a
79joinProcess = join
75joinProcess = join
81−---- Functionals ---------------------------------------------------------------
83−compose :: (b -> c) -> (a -> b) -> a -> c
86−first :: (a -> b) -> (a, c) -> (b, c)
89−second :: (a -> b) -> (c, a) -> (c, b)
92−split :: (a -> b) -> (a' -> b') -> (a, a') -> (b, b')
95---- Variations on standard or CH functions with an explicit dictionary arg ----
77---- Variations on standard or CH functions with an explicit dictionary arg ----
97−decodeDict :: SerializableDict a -> ByteString -> a
98−decodeDict SerializableDict = decode
100sendDict :: SerializableDict a -> ProcessId -> a -> Process ()
79sendDict :: SerializableDict a -> ProcessId -> a -> Process ()
101sendDict SerializableDict = send
80sendDict SerializableDict = send
@@ -105,20 +84,6 @@ expectDict SerializableDict = expect
106---- Serialization dictionaries ------------------------------------------------
85---- Serialization dictionaries ------------------------------------------------
108−-- | Serialization dictionary for '()'
110−-- Use @$(mkStatic sdictUnit)@ (instead of 'sdictUnit__static')
111−-- to refer to the static dictionary.
112−sdictUnit :: SerializableDict ()
113−sdictUnit = SerializableDict
115−-- | Serialization dictionary for 'ProcessId'
117−-- Use @$(mkStatic sdictProcessId)@ (instead of 'sdictProcessId__static')
118−-- to refer to the static dictionary.
119−sdictProcessId :: SerializableDict ProcessId
120−sdictProcessId = SerializableDict
122-- | Specialized serialization dictionary required in 'cpBind'
87-- | Specialized serialization dictionary required in 'cpBind'
123sdictBind :: SerializableDict (((StaticLabel, ByteString), (StaticLabel, ByteString)), TypeRep)
88sdictBind :: SerializableDict (((StaticLabel, ByteString), (StaticLabel, ByteString)), TypeRep)
124sdictBind = SerializableDict
89sdictBind = SerializableDict
@@ -142,8 +107,8 @@ unDynamic (pdyn, typ) = do
142 ++ " against expected type "
107 ++ " against expected type "
143 ++ show typ
108 ++ show typ
145−bindDyn :: (Process Dynamic, Process Dynamic) -> Process Dynamic
110+bindDyn :: () -> (Process Dynamic, Process Dynamic) -> Process Dynamic
146−bindDyn (px, pf) = do
111+bindDyn () (px, pf) = do
149 case dynBind tyConProcess bindProcess x f of
114 case dynBind tyConProcess bindProcess x f of
@@ -162,23 +127,13 @@ remotable [ -- Monadic operations
162 'returnProcess
127 'returnProcess
163 , 'bindProcess
128 , 'bindProcess
164 , 'joinProcess
129 , 'joinProcess
165− -- Functionals (predefined)
167− -- Functionals (defined above)
172 -- CH primitives
130 -- CH primitives
174 , 'unlink
132 , 'unlink
175− -- Explicit dictionaries
133+ -- Explicit dictionaries
177 , 'sendDict
134 , 'sendDict
178 , 'expectDict
135 , 'expectDict
179 -- Serialization dictionaries
136 -- Serialization dictionaries
182 , 'sdictBind
137 , 'sdictBind
183 -- Specialized processes
138 -- Specialized processes
184 , 'unClosure
139 , 'unClosure
@@ -186,76 +141,17 @@ remotable [ -- Monadic operations
186 , 'bindDyn
141 , 'bindDyn
189−--------------------------------------------------------------------------------
190−-- Static versions of the functionals --
191−-- (We give these explicit names because they are useful outside this module) --
192−--------------------------------------------------------------------------------
194−-- | Static version of 'const'
195−staticConst :: (Typeable a, Typeable b) => Static (a -> b -> a)
196−staticConst = $(mkStatic 'const)
198−-- | Static version of ('Prelude..')
199−staticCompose :: (Typeable a, Typeable b, Typeable c)
200− => Static (b -> c) -> Static (a -> b) -> Static (a -> c)
201−staticCompose f x = $(mkStatic 'compose) `staticApply` f `staticApply` x
203−-- | Static version of 'Control.Arrow.first'
204−staticFirst :: (Typeable a, Typeable b, Typeable c)
205− => Static ((a -> b) -> (a, c) -> (b, c))
206−staticFirst = $(mkStatic 'first)
208−-- | Static version of 'Control.Arrow.second'
209−staticSecond :: (Typeable a, Typeable b, Typeable c)
210− => Static ((a -> b) -> (c, a) -> (c, b))
211−staticSecond = $(mkStatic 'second)
213−-- | Static version of ('Control.Arrow.***')
214−staticSplit :: (Typeable a, Typeable b, Typeable c, Typeable d)
215− => Static (a -> c) -> Static (b -> d) -> Static ((a, b) -> (c, d))
216−staticSplit f g = $(mkStatic 'split) `staticApply` f `staticApply` g
218−--------------------------------------------------------------------------------
219−-- Creating closures --
220−--------------------------------------------------------------------------------
222−staticDecode :: Typeable a => Static (SerializableDict a) -> Static (ByteString -> a)
223−staticDecode dict = $(mkStatic 'decodeDict) `staticApply` dict
225−staticClosure :: forall a. Typeable a => Static a -> Closure a
226−staticClosure static = Closure decoder empty
228− decoder :: Static (ByteString -> a)
229− decoder = staticConst `staticApply` static
231−toClosure :: forall a. Serializable a
232− => Static (SerializableDict a) -> a -> Closure a
233−toClosure dict x = Closure decoder (encode x)
235− decoder :: Static (ByteString -> a)
236− decoder = $(mkStatic 'decodeDict) `staticApply` dict
238--------------------------------------------------------------------------------
144--------------------------------------------------------------------------------
239-- Closure versions of CH primitives --
145-- Closure versions of CH primitives --
240--------------------------------------------------------------------------------
146--------------------------------------------------------------------------------
242-- | Closure version of 'link'
148-- | Closure version of 'link'
243cpLink :: ProcessId -> Closure (Process ())
149cpLink :: ProcessId -> Closure (Process ())
244−cpLink pid = Closure decoder (encode pid)
150+cpLink = $(mkClosure 'link)
246− decoder :: Static (ByteString -> Process ())
247− decoder = $(mkStatic 'link)
249− ($(mkStatic 'decodeDict) `staticApply` $(mkStatic 'sdictProcessId))
251-- | Closure version of 'unlink'
152-- | Closure version of 'unlink'
252cpUnlink :: ProcessId -> Closure (Process ())
153cpUnlink :: ProcessId -> Closure (Process ())
253−cpUnlink pid = Closure decoder (encode pid)
154+cpUnlink = $(mkClosure 'unlink)
255− decoder :: Static (ByteString -> Process ())
256− decoder = $(mkStatic 'unlink)
258− ($(mkStatic 'decodeDict) `staticApply` $(mkStatic 'sdictProcessId))
260-- | Closure version of 'send'
156-- | Closure version of 'send'
261cpSend :: forall a. Typeable a
157cpSend :: forall a. Typeable a
@@ -265,7 +161,7 @@ cpSend dict pid = Closure decoder (encode pid)
265 decoder :: Static (ByteString -> a -> Process ())
161 decoder :: Static (ByteString -> a -> Process ())
266 decoder = ($(mkStatic 'sendDict) `staticApply` dict)
162 decoder = ($(mkStatic 'sendDict) `staticApply` dict)
267 `staticCompose`
163 `staticCompose`
268− ($(mkStatic 'decodeDict) `staticApply` $(mkStatic 'sdictProcessId))
164+ staticDecode $(mkStatic 'sdictProcessId)
270-- | Closure version of 'expect'
166-- | Closure version of 'expect'
271cpExpect :: Typeable a => Static (SerializableDict a) -> Closure (Process a)
167cpExpect :: Typeable a => Static (SerializableDict a) -> Closure (Process a)
@@ -283,7 +179,7 @@ cpReturn dict x = Closure decoder (encode x)
283 decoder :: Static (ByteString -> Process a)
179 decoder :: Static (ByteString -> Process a)
284 decoder = $(mkStatic 'returnProcess)
180 decoder = $(mkStatic 'returnProcess)
285 `staticCompose`
181 `staticCompose`
286− ($(mkStatic 'decodeDict) `staticApply` dict)
182+ staticDecode dict
288-- | Not-quite-monadic bind ('>>=')
184-- | Not-quite-monadic bind ('>>=')
289cpBind :: forall a b. Typeable b
185cpBind :: forall a b. Typeable b
@@ -303,7 +199,7 @@ cpBind (Closure (Static xlabel) xenv) (Closure (Static flabel) fenv) =
305 aux1 :: Static (ByteString -> (((StaticLabel, ByteString), (StaticLabel, ByteString)), TypeRep))
201 aux1 :: Static (ByteString -> (((StaticLabel, ByteString), (StaticLabel, ByteString)), TypeRep))
306− aux1 = $(mkStatic 'decodeDict) `staticApply` $(mkStatic 'sdictBind)
202+ aux1 = staticDecode $(mkStatic 'sdictBind)
308 aux2 :: Static ((StaticLabel, ByteString) -> Process Dynamic)
204 aux2 :: Static ((StaticLabel, ByteString) -> Process Dynamic)
309 aux2 = $(mkStatic 'unClosure)
205 aux2 = $(mkStatic 'unClosure)
@@ -312,7 +208,7 @@ cpBind (Closure (Static xlabel) xenv) (Closure (Static flabel) fenv) =
312 aux3 = aux2 `staticSplit` aux2
208 aux3 = aux2 `staticSplit` aux2
314 aux4 :: Static ((Process Dynamic, Process Dynamic) -> Process Dynamic)
210 aux4 :: Static ((Process Dynamic, Process Dynamic) -> Process Dynamic)
315− aux4 = $(mkStatic 'bindDyn)
211+ aux4 = $(mkStatic 'bindDyn) `staticApply` staticUnit
317 aux5 :: Static (((StaticLabel, ByteString), (StaticLabel, ByteString)) -> Process Dynamic)
213 aux5 :: Static (((StaticLabel, ByteString), (StaticLabel, ByteString)) -> Process Dynamic)
318 aux5 = aux4 `staticCompose` aux3
214 aux5 = aux4 `staticCompose` aux3