@@ -23,8 +23,6 @@ module Control.Distributed.Process.Internal.Closure.BuiltIn
2323 , cpNewChan
2424 ) where
2525
26−import Prelude hiding (snd)
27−import qualified Prelude (snd)
2826import Data.ByteString.Lazy (ByteString)
2927import Data.Binary (decode, encode)
3028import Data.Rank1Typeable (Typeable, ANY, ANY1, ANY2, ANY3, ANY4)
@@ -65,60 +63,42 @@ import Control.Distributed.Process.Internal.Primitives
6563
6664remoteTable :: RemoteTable -> RemoteTable
6765remoteTable =
68− registerStatic "$decodeDict" (toDynamic decodeDict)
69− . registerStatic "$sdictUnit" (toDynamic dictUnit)
70− . registerStatic "$sdictProcessId" (toDynamic dictProcessId)
71− . registerStatic "$sdictSendPort_" (toDynamic sdictSendPort_)
72− . registerStatic "$returnProcess" (toDynamic returnProcess)
73− . registerStatic "$seqProcess" (toDynamic seqProcess)
74− . registerStatic "$bindProcess" (toDynamic bindProcess)
75− . registerStatic "$decodeProcessId" (toDynamic decodeProcessId)
66+ registerStatic "$decodeDict" (toDynamic (decodeDict :: SerializableDict ANY -> ByteString -> ANY))
67+ . registerStatic "$sdictUnit" (toDynamic (SerializableDict :: SerializableDict ()))
68+ . registerStatic "$sdictProcessId" (toDynamic (SerializableDict :: SerializableDict ProcessId))
69+ . registerStatic "$sdictSendPort_" (toDynamic (sdictSendPort_ :: SerializableDict ANY -> SerializableDict (SendPort ANY)))
70+ . registerStatic "$returnProcess" (toDynamic (return :: ANY -> Process ANY))
71+ . registerStatic "$seqProcess" (toDynamic ((>>) :: Process ANY1 -> Process ANY2 -> Process ANY2))
72+ . registerStatic "$bindProcess" (toDynamic ((>>=) :: Process ANY1 -> (ANY1 -> Process ANY2) -> Process ANY2))
73+ . registerStatic "$decodeProcessId" (toDynamic (decode :: ByteString -> ProcessId))
7674 . registerStatic "$link" (toDynamic link)
7775 . registerStatic "$unlink" (toDynamic unlink)
78− . registerStatic "$sendDict" (toDynamic sendDict)
79− . registerStatic "$expectDict" (toDynamic expectDict)
80− . registerStatic "$newChanDict" (toDynamic newChanDict)
81− . registerStatic "$cpSplit" (toDynamic cpSplit)
82− . registerStatic "$snd" (toDynamic snd)
76+ . registerStatic "$sendDict" (toDynamic (sendDict :: SerializableDict ANY -> ProcessId -> ANY -> Process ()))
77+ . registerStatic "$expectDict" (toDynamic (expectDict :: SerializableDict ANY -> Process ANY))
78+ . registerStatic "$newChanDict" (toDynamic (newChanDict :: SerializableDict ANY -> Process (SendPort ANY, ReceivePort ANY)))
79+ . registerStatic "$cpSplit" (toDynamic (cpSplit :: (ANY1 -> Process ANY3) -> (ANY2 -> Process ANY4) -> (ANY1, ANY2) -> Process (ANY3, ANY4)))
80+ . registerStatic "$snd" (toDynamic (snd :: (ANY1, ANY2) -> ANY2))
8381 where
84− decodeDict :: SerializableDict ANY -> ByteString -> ANY
82+ decodeDict :: forall a. SerializableDict a -> ByteString -> a
8583 decodeDict SerializableDict = decode
8684
87− dictUnit :: SerializableDict ()
88− dictUnit = SerializableDict
89−
90− dictProcessId :: SerializableDict ProcessId
91− dictProcessId = SerializableDict
92−
93− sdictSendPort_ :: SerializableDict ANY -> SerializableDict (SendPort ANY)
85+ sdictSendPort_ :: forall a. SerializableDict a -> SerializableDict (SendPort a)
9486 sdictSendPort_ SerializableDict = SerializableDict
9587
96− returnProcess :: ANY -> Process ANY
97− returnProcess = return
98−
99− seqProcess :: Process ANY1 -> Process ANY2 -> Process ANY2
100− seqProcess = (>>)
101−
102− bindProcess :: Process ANY1 -> (ANY1 -> Process ANY2) -> Process ANY2
103− bindProcess = (>>=)
104−
105− decodeProcessId :: ByteString -> ProcessId
106− decodeProcessId = decode
107−
108− sendDict :: SerializableDict ANY -> ProcessId -> ANY -> Process ()
88+ sendDict :: forall a. SerializableDict a -> ProcessId -> a -> Process ()
10989 sendDict SerializableDict = send
11090
111− expectDict :: SerializableDict ANY -> Process ANY
91+ expectDict :: forall a. SerializableDict a -> Process a
11292 expectDict SerializableDict = expect
11393
114− newChanDict :: SerializableDict ANY -> Process (SendPort ANY, ReceivePort ANY)
94+ newChanDict :: forall a. SerializableDict a -> Process (SendPort a, ReceivePort a)
11595 newChanDict SerializableDict = newChan
11696
117− cpSplit :: (ANY1 -> Process ANY3) -> (ANY2 -> Process ANY4) -> (ANY1, ANY2) -> Process (ANY3, ANY4)
118− cpSplit f g (a, b) = f a >>= \c -> g b >>= \d -> return (c, d)
119−
120− snd :: (ANY1, ANY2) -> ANY2
121− snd = Prelude.snd
97+ cpSplit :: forall a b c d. (a -> Process c) -> (b -> Process d) -> (a, b) -> Process (c, d)
98+ cpSplit f g (a, b) = do
99+ c <- f a
100+ d <- g b
101+ return (c, d)
122102
123103--------------------------------------------------------------------------------
124104-- Static dictionaries and associated operations --
@@ -164,13 +144,19 @@ type CP a b = Closure (a -> Process b)
164144returnProcessStatic :: Typeable a => Static (a -> Process a)
165145returnProcessStatic = staticLabel "$returnProcess"
166146
147+-- | 'CP' version of 'Control.Category.id'
167148idCP :: Typeable a => CP a a
168149idCP = staticClosure returnProcessStatic
169150
151+-- | 'CP' version of ('Control.Arrow.***')
170152splitCP :: (Typeable a, Typeable b, Typeable c, Typeable d)
171153 => CP a c -> CP b d -> CP (a, b) (c, d)
172−splitCP = undefined
154+splitCP p q = cpSplitStatic `closureApplyStatic` p `closureApply` q
155+ where
156+ cpSplitStatic :: Static ((a -> Process c) -> (b -> Process d) -> (a, b) -> Process (c, d))
157+ cpSplitStatic = staticLabel "$cpSplit"
173158
159+-- | 'CP' version of 'Control.Monad.return'
174160returnCP :: forall a. Serializable a
175161 => Static (SerializableDict a) -> a -> Closure (Process a)
176162returnCP dict x = Closure decoder (encode x)
@@ -180,6 +166,7 @@ returnCP dict x = Closure decoder (encode x)
180166 `staticCompose`
181167 staticDecode dict
182168
169+-- | 'CP' version of ('Control.Monad.>>')
183170seqCP :: (Typeable a, Typeable b)
184171 => Closure (Process a) -> Closure (Process b) -> Closure (Process b)
185172seqCP p q = seqProcessStatic `closureApplyStatic` p `closureApply` q
@@ -188,8 +175,9 @@ seqCP p q = seqProcessStatic `closureApplyStatic` p `closureApply` q
188175 => Static (Process a -> Process b -> Process b)
189176 seqProcessStatic = staticLabel "$seqProcess"
190177
178+-- | (Not quite the) 'CP' version of ('Control.Monad.>>=')
191179bindCP :: forall a b. (Typeable a, Typeable b)
192− => Closure (Process a) -> Closure (a -> Process b) -> Closure (Process b)
180+ => Closure (Process a) -> CP a b -> Closure (Process b)
193181bindCP x f = bindProcessStatic `closureApplyStatic` x `closureApply` f
194182 where
195183 bindProcessStatic :: (Typeable a, Typeable b)
@@ -203,23 +191,23 @@ bindCP x f = bindProcessStatic `closureApplyStatic` x `closureApply` f
203191decodeProcessIdStatic :: Static (ByteString -> ProcessId)
204192decodeProcessIdStatic = staticLabel "$decodeProcessId"
205193
206−-- | Closure version of 'link'
194+-- | 'CP' version of 'link'
207195cpLink :: ProcessId -> Closure (Process ())
208196cpLink = Closure (linkStatic `staticCompose` decodeProcessIdStatic) . encode
209197 where
210198 linkStatic :: Static (ProcessId -> Process ())
211199 linkStatic = staticLabel "$link"
212200
213−-- | Closure version of 'unlink'
201+-- | 'CP' version of 'unlink'
214202cpUnlink :: ProcessId -> Closure (Process ())
215203cpUnlink = Closure (unlinkStatic `staticCompose` decodeProcessIdStatic) . encode
216204 where
217205 unlinkStatic :: Static (ProcessId -> Process ())
218206 unlinkStatic = staticLabel "$unlink"
219207
220−-- | Closure version of 'send'
208+-- | 'CP' version of 'send'
221209cpSend :: forall a. Typeable a
222− => Static (SerializableDict a) -> ProcessId -> Closure (a -> Process ())
210+ => Static (SerializableDict a) -> ProcessId -> CP a ()
223211cpSend dict pid = Closure decoder (encode pid)
224212 where
225213 decoder :: Static (ByteString -> a -> Process ())
@@ -231,14 +219,14 @@ cpSend dict pid = Closure decoder (encode pid)
231219 => Static (SerializableDict a -> ProcessId -> a -> Process ())
232220 sendDictStatic = staticLabel "$sendDict"
233221
234−-- | Closure version of 'expect'
222+-- | 'CP' version of 'expect'
235223cpExpect :: Typeable a => Static (SerializableDict a) -> Closure (Process a)
236224cpExpect dict = staticClosure (expectDictStatic `staticApply` dict)
237225 where
238226 expectDictStatic :: Typeable a => Static (SerializableDict a -> Process a)
239227 expectDictStatic = staticLabel "$expectDict"
240228
241−-- | Closure version of 'newChan'
229+-- | 'CP' version of 'newChan'
242230cpNewChan :: Typeable a
243231 => Static (SerializableDict a)
244232 -> Closure (Process (SendPort a, ReceivePort a))