@@ -1,6 +1,9 @@
1-- | Derived (TH generated) closures
1-- | Derived (TH generated) closures
2module Control.Distributed.Process.Internal.Closure.Derived
2module Control.Distributed.Process.Internal.Closure.Derived
3 ( remoteTable
3 ( remoteTable
4+ -- * Process primitives
4 -- * Generic combinators
7 -- * Generic combinators
5 , closureApply
8 , closureApply
6 , closureConst
9 , closureConst
@@ -23,28 +26,32 @@ module Control.Distributed.Process.Internal.Closure.Derived
23 , cpEither
26 , cpEither
27 -- * Derived process operators
29 -- * Derived process operators
32+ -- * Serialization dictionaries
33+ , serializableDictUnit
32import Prelude hiding (lookup)
36import Prelude hiding (lookup)
33−import Data.Binary (encode)
34−import Data.Typeable (typeOf, Typeable)
37+import Data.Typeable (Typeable)
35import Data.Tuple (swap)
38import Data.Tuple (swap)
36import Control.Applicative ((<$>))
39import Control.Applicative ((<$>))
37import Control.Monad ((>=>))
40import Control.Monad ((>=>))
38import Control.Distributed.Process.Internal.Types
41import Control.Distributed.Process.Internal.Types
39 ( Closure(..)
42 ( Closure(..)
43 , RemoteTable
44 , RemoteTable
45+ , SerializableDict(..)
45import Control.Distributed.Process.Internal.TypeRep () -- Binary instances
48import Control.Distributed.Process.Internal.TypeRep () -- Binary instances
46import Control.Distributed.Process.Internal.Closure.TH (remotable, mkClosure)
49import Control.Distributed.Process.Internal.Closure.TH (remotable, mkClosure)
47−import Control.Distributed.Process.Internal.Primitives (unClosure)
50+import Control.Distributed.Process.Internal.Primitives
54+import Control.Distributed.Process.Internal.Closure.BuiltIn (closureApply)
49--------------------------------------------------------------------------------
56--------------------------------------------------------------------------------
50-- TH stuff --
57-- TH stuff --
@@ -80,10 +87,8 @@ untagProcess :: Either a a -> Process a
80untagProcess (Left x) = return x
87untagProcess (Left x) = return x
81untagProcess (Right y) = return y
88untagProcess (Right y) = return y
83−applyClosureProcess :: (Typeable a, Typeable b) => (Closure (a -> Process b), a) -> Process b
90+serializableDictUnit :: SerializableDict ()
84−applyClosureProcess (procClosure, a) = do
91+serializableDictUnit = SerializableDict
85− proc <- unClosure procClosure
88remotable [ 'const
93remotable [ 'const
@@ -95,18 +100,29 @@ remotable [ 'const
95 , 'leftProcess
100 , 'leftProcess
96 , 'mirrorProcess
101 , 'mirrorProcess
97 , 'untagProcess
102 , 'untagProcess
105+ , 'serializableDictUnit
100remoteTable :: RemoteTable -> RemoteTable
108remoteTable :: RemoteTable -> RemoteTable
101remoteTable = __remoteTable
109remoteTable = __remoteTable
103--------------------------------------------------------------------------------
111--------------------------------------------------------------------------------
104−-- Generic closure combinators --
112+-- Process primitives --
105--------------------------------------------------------------------------------
113--------------------------------------------------------------------------------
107−closureApply :: Closure (a -> b) -> Closure a -> Closure b
115+-- | Closure version of 'link'
108−closureApply (Closure (Static labelf) envf) (Closure (Static labelx) envx) =
116+linkClosure :: ProcessId -> Closure (Process ())
109− Closure (Static ClosureApply) $ encode (labelf, envf, labelx, envx)
117+linkClosure = $(mkClosure 'link)
119+-- | Closure version of 'unlink'
120+unlinkClosure :: ProcessId -> Closure (Process ())
121+unlinkClosure = $(mkClosure 'unlink)
123+--------------------------------------------------------------------------------
124+-- Generic closure combinators --
125+--------------------------------------------------------------------------------
111closureConst :: (Typeable a, Typeable b) => Closure (a -> b -> a)
127closureConst :: (Typeable a, Typeable b) => Closure (a -> b -> a)
112closureConst = $(mkClosure 'const)
128closureConst = $(mkClosure 'const)
@@ -128,47 +144,40 @@ cpElim :: Typeable a
128 => CP () a -> Closure (Process a)
144 => CP () a -> Closure (Process a)
129cpElim = flip closureApply closureUnit
145cpElim = flip closureApply closureUnit
131−cpId :: forall a. Typeable a
147+cpId :: Typeable a => CP a a
133cpId = $(mkClosure 'returnProcess)
148cpId = $(mkClosure 'returnProcess)
135−cpComp :: forall a b c. (Typeable a, Typeable b, Typeable c)
150+cpComp :: (Typeable a, Typeable b, Typeable c) => CP a b -> CP b c -> CP a c
136− => CP a b -> CP b c -> CP a c
137cpComp f g = $(mkClosure 'kleisliComposeProcess) `closureApply` f `closureApply` g
151cpComp f g = $(mkClosure 'kleisliComposeProcess) `closureApply` f `closureApply` g
139−cpFirst :: forall a b c. (Typeable a, Typeable b, Typeable c)
153+cpFirst :: (Typeable a, Typeable b, Typeable c) => CP a b -> CP (a, c) (b, c)
140− => CP a b -> CP (a, c) (b, c)
141cpFirst = closureApply $(mkClosure 'firstProcess)
154cpFirst = closureApply $(mkClosure 'firstProcess)
143−cpSwap :: forall a b. (Typeable a, Typeable b)
156+cpSwap :: (Typeable a, Typeable b) => CP (a, b) (b, a)
145cpSwap = $(mkClosure 'swapProcess)
157cpSwap = $(mkClosure 'swapProcess)
147−cpSecond :: (Typeable a, Typeable b, Typeable c)
159+cpSecond :: (Typeable a, Typeable b, Typeable c) => CP a b -> CP (c, a) (c, b)
148− => CP a b -> CP (c, a) (c, b)
149cpSecond f = cpSwap `cpComp` cpFirst f `cpComp` cpSwap
160cpSecond f = cpSwap `cpComp` cpFirst f `cpComp` cpSwap
151cpPair :: (Typeable a, Typeable a', Typeable b, Typeable b')
162cpPair :: (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')
153cpPair f g = cpFirst f `cpComp` cpSecond g
164cpPair f g = cpFirst f `cpComp` cpSecond g
155−cpCopy :: forall a. Typeable a
166+cpCopy :: Typeable a => CP a (a, a)
157cpCopy = $(mkClosure 'copyProcess)
167cpCopy = $(mkClosure 'copyProcess)
159−cpFanOut :: (Typeable a, Typeable b, Typeable c)
169+cpFanOut :: (Typeable a, Typeable b, Typeable c)
160 => CP a b -> CP a c -> CP a (b, c)
170 => CP a b -> CP a c -> CP a (b, c)
161cpFanOut f g = cpCopy `cpComp` (f `cpPair` g)
171cpFanOut f g = cpCopy `cpComp` (f `cpPair` g)
163−cpLeft :: forall a b c. (Typeable a, Typeable b, Typeable c)
173+cpLeft :: (Typeable a, Typeable b, Typeable c)
164 => CP a b -> CP (Either a c) (Either b c)
174 => CP a b -> CP (Either a c) (Either b c)
165cpLeft = closureApply $(mkClosure 'leftProcess)
175cpLeft = closureApply $(mkClosure 'leftProcess)
167−cpMirror :: forall a b. (Typeable a, Typeable b)
177+cpMirror :: (Typeable a, Typeable b) => CP (Either a b) (Either b a)
168− => CP (Either a b) (Either b a)
169cpMirror = $(mkClosure 'mirrorProcess)
178cpMirror = $(mkClosure 'mirrorProcess)
171−cpRight :: forall a b c. (Typeable a, Typeable b, Typeable c)
180+cpRight :: (Typeable a, Typeable b, Typeable c)
172 => CP a b -> CP (Either c a) (Either c b)
181 => CP a b -> CP (Either c a) (Either c b)
173cpRight f = cpMirror `cpComp` cpLeft f `cpComp` cpMirror
182cpRight f = cpMirror `cpComp` cpLeft f `cpComp` cpMirror
@@ -176,24 +185,13 @@ cpEither :: (Typeable a, Typeable a', Typeable b, Typeable b')
176 => CP a b -> CP a' b' -> CP (Either a a') (Either b b')
185 => CP a b -> CP a' b' -> CP (Either a a') (Either b b')
177cpEither f g = cpLeft f `cpComp` cpRight g
186cpEither f g = cpLeft f `cpComp` cpRight g
179−cpUntag :: forall a. Typeable a
188+cpUntag :: Typeable a => CP (Either a a) a
180− => CP (Either a a) a
181cpUntag = $(mkClosure 'untagProcess)
189cpUntag = $(mkClosure 'untagProcess)
183cpFanIn :: (Typeable a, Typeable b, Typeable c)
191cpFanIn :: (Typeable a, Typeable b, Typeable c)
184 => CP a c -> CP b c -> CP (Either a b) c
192 => CP a c -> CP b c -> CP (Either a b) c
185cpFanIn f g = (f `cpEither` g) `cpComp` cpUntag
193cpFanIn f g = (f `cpEither` g) `cpComp` cpUntag
187−cpApply :: forall a b. (Typeable a, Typeable b)
189−cpApply = Closure (Static CpApply) $ encode ( typeOf aux
190− , typeOf (undefined :: a)
191− , typeOf (undefined :: Process b)
194− aux :: (Closure (a -> Process b), a) -> Process b
197--------------------------------------------------------------------------------
195--------------------------------------------------------------------------------
198-- Some derived operators for processes --
196-- Some derived operators for processes --
199--------------------------------------------------------------------------------
197--------------------------------------------------------------------------------