1module Control.Distributed.Process.Internal.Closure.BuiltIn
4 -- * Static dictionaries and associated operations
9 -- * Some static values
11 -- * The CP type and associated combinators
18 -- * CP versions of Cloud Haskell primitives
24 -- * Support for some CH operations
28import Data.ByteString.Lazy (ByteString)
29import Data.Binary (decode, encode)
30import Data.Rank1Typeable (Typeable, ANY, ANY1, ANY2, ANY3, ANY4)
31import Data.Rank1Dynamic (toDynamic)
32import Control.Distributed.Static
45import Control.Distributed.Process.Serializable
46 ( SerializableDict(..)
49import Control.Distributed.Process.Internal.Types
54 , ProcessMonitorNotification(ProcessMonitorNotification)
56import Control.Distributed.Process.Internal.Primitives
69--------------------------------------------------------------------------------
71--------------------------------------------------------------------------------
73remoteTable :: RemoteTable -> RemoteTable
75 registerStatic "$decodeDict" (toDynamic (decodeDict :: SerializableDict ANY -> ByteString -> ANY))
76 . registerStatic "$sdictUnit" (toDynamic (SerializableDict :: SerializableDict ()))
77 . registerStatic "$sdictProcessId" (toDynamic (SerializableDict :: SerializableDict ProcessId))
78 . registerStatic "$sdictSendPort_" (toDynamic (sdictSendPort_ :: SerializableDict ANY -> SerializableDict (SendPort ANY)))
79 . registerStatic "$returnProcess" (toDynamic (return :: ANY -> Process ANY))
80 . registerStatic "$seqProcess" (toDynamic ((>>) :: Process ANY1 -> Process ANY2 -> Process ANY2))
81 . registerStatic "$bindProcess" (toDynamic ((>>=) :: Process ANY1 -> (ANY1 -> Process ANY2) -> Process ANY2))
82 . registerStatic "$decodeProcessId" (toDynamic (decode :: ByteString -> ProcessId))
83 . registerStatic "$link" (toDynamic link)
84 . registerStatic "$unlink" (toDynamic unlink)
85 . registerStatic "$sendDict" (toDynamic (sendDict :: SerializableDict ANY -> ProcessId -> ANY -> Process ()))
86 . registerStatic "$expectDict" (toDynamic (expectDict :: SerializableDict ANY -> Process ANY))
87 . registerStatic "$newChanDict" (toDynamic (newChanDict :: SerializableDict ANY -> Process (SendPort ANY, ReceivePort ANY)))
88 . registerStatic "$cpSplit" (toDynamic (cpSplit :: (ANY1 -> Process ANY3) -> (ANY2 -> Process ANY4) -> (ANY1, ANY2) -> Process (ANY3, ANY4)))
89 . registerStatic "$snd" (toDynamic (snd :: (ANY1, ANY2) -> ANY2))
90 . registerStatic "$delay" (toDynamic delay)
92 decodeDict :: forall a. SerializableDict a -> ByteString -> a
93 decodeDict SerializableDict = decode
95 sdictSendPort_ :: forall a. SerializableDict a -> SerializableDict (SendPort a)
96 sdictSendPort_ SerializableDict = SerializableDict
98 sendDict :: forall a. SerializableDict a -> ProcessId -> a -> Process ()
99 sendDict SerializableDict = send
101 expectDict :: forall a. SerializableDict a -> Process a
102 expectDict SerializableDict = expect
104 newChanDict :: forall a. SerializableDict a -> Process (SendPort a, ReceivePort a)
105 newChanDict SerializableDict = newChan
107 cpSplit :: forall a b c d. (a -> Process c) -> (b -> Process d) -> (a, b) -> Process (c, d)
108 cpSplit f g (a, b) = do
113--------------------------------------------------------------------------------
114-- Static dictionaries and associated operations --
115--------------------------------------------------------------------------------
117-- | Static decoder, given a static serialization dictionary.
119-- See module documentation of "Control.Distributed.Process.Closure" for an
121staticDecode :: Typeable a => Static (SerializableDict a) -> Static (ByteString -> a)
122staticDecode dict = decodeDictStatic `staticApply` dict
124 decodeDictStatic :: Typeable a => Static (SerializableDict a -> ByteString -> a)
125 decodeDictStatic = staticLabel "$decodeDict"
127-- | Serialization dictionary for '()'
128sdictUnit :: Static (SerializableDict ())
129sdictUnit = staticLabel "$sdictUnit"
131-- | Serialization dictionary for 'ProcessId'
132sdictProcessId :: Static (SerializableDict ProcessId)
133sdictProcessId = staticLabel "$sdictProcessId"
135-- | Serialization dictionary for 'SendPort'
136sdictSendPort :: Typeable a
137 => Static (SerializableDict a) -> Static (SerializableDict (SendPort a))
138sdictSendPort = staticApply (staticLabel "$sdictSendPort_")
140--------------------------------------------------------------------------------
142--------------------------------------------------------------------------------
144sndStatic :: Static ((a, b) -> b)
145sndStatic = staticLabel "$snd"
147--------------------------------------------------------------------------------
148-- The CP type and associated combinators --
149--------------------------------------------------------------------------------
151-- | @CP a b@ is a process with input of type @a@ and output of type @b@
152type CP a b = Closure (a -> Process b)
154returnProcessStatic :: Typeable a => Static (a -> Process a)
155returnProcessStatic = staticLabel "$returnProcess"
157-- | 'CP' version of 'Control.Category.id'
158idCP :: Typeable a => CP a a
159idCP = staticClosure returnProcessStatic
161-- | 'CP' version of ('Control.Arrow.***')
162splitCP :: (Typeable a, Typeable b, Typeable c, Typeable d)
163 => CP a c -> CP b d -> CP (a, b) (c, d)
164splitCP p q = cpSplitStatic `closureApplyStatic` p `closureApply` q
166 cpSplitStatic :: Static ((a -> Process c) -> (b -> Process d) -> (a, b) -> Process (c, d))
167 cpSplitStatic = staticLabel "$cpSplit"
169-- | 'CP' version of 'Control.Monad.return'
170returnCP :: forall a. Serializable a
171 => Static (SerializableDict a) -> a -> Closure (Process a)
172returnCP dict x = closure decoder (encode x)
174 decoder :: Static (ByteString -> Process a)
175 decoder = returnProcessStatic
179-- | 'CP' version of ('Control.Monad.>>')
180seqCP :: (Typeable a, Typeable b)
181 => Closure (Process a) -> Closure (Process b) -> Closure (Process b)
182seqCP p q = seqProcessStatic `closureApplyStatic` p `closureApply` q
184 seqProcessStatic :: (Typeable a, Typeable b)
185 => Static (Process a -> Process b -> Process b)
186 seqProcessStatic = staticLabel "$seqProcess"
188-- | (Not quite the) 'CP' version of ('Control.Monad.>>=')
189bindCP :: forall a b. (Typeable a, Typeable b)
190 => Closure (Process a) -> CP a b -> Closure (Process b)
191bindCP x f = bindProcessStatic `closureApplyStatic` x `closureApply` f
193 bindProcessStatic :: (Typeable a, Typeable b)
194 => Static (Process a -> (a -> Process b) -> Process b)
195 bindProcessStatic = staticLabel "$bindProcess"
197--------------------------------------------------------------------------------
198-- CP versions of Cloud Haskell primitives --
199--------------------------------------------------------------------------------
201decodeProcessIdStatic :: Static (ByteString -> ProcessId)
202decodeProcessIdStatic = staticLabel "$decodeProcessId"
204-- | 'CP' version of 'link'
205cpLink :: ProcessId -> Closure (Process ())
206cpLink = closure (linkStatic `staticCompose` decodeProcessIdStatic) . encode
208 linkStatic :: Static (ProcessId -> Process ())
209 linkStatic = staticLabel "$link"
211-- | 'CP' version of 'unlink'
212cpUnlink :: ProcessId -> Closure (Process ())
213cpUnlink = closure (unlinkStatic `staticCompose` decodeProcessIdStatic) . encode
215 unlinkStatic :: Static (ProcessId -> Process ())
216 unlinkStatic = staticLabel "$unlink"
218-- | 'CP' version of 'send'
219cpSend :: forall a. Typeable a
220 => Static (SerializableDict a) -> ProcessId -> CP a ()
221cpSend dict pid = closure decoder (encode pid)
223 decoder :: Static (ByteString -> a -> Process ())
224 decoder = (sendDictStatic `staticApply` dict)
226 decodeProcessIdStatic
228 sendDictStatic :: Typeable a
229 => Static (SerializableDict a -> ProcessId -> a -> Process ())
230 sendDictStatic = staticLabel "$sendDict"
232-- | 'CP' version of 'expect'
233cpExpect :: Typeable a => Static (SerializableDict a) -> Closure (Process a)
234cpExpect dict = staticClosure (expectDictStatic `staticApply` dict)
236 expectDictStatic :: Typeable a => Static (SerializableDict a -> Process a)
237 expectDictStatic = staticLabel "$expectDict"
239-- | 'CP' version of 'newChan'
240cpNewChan :: Typeable a
241 => Static (SerializableDict a)
242 -> Closure (Process (SendPort a, ReceivePort a))
243cpNewChan dict = staticClosure (newChanDictStatic `staticApply` dict)
245 newChanDictStatic :: Typeable a
246 => Static (SerializableDict a -> Process (SendPort a, ReceivePort a))
247 newChanDictStatic = staticLabel "$newChanDict"
249--------------------------------------------------------------------------------
250-- Support for spawn --
251--------------------------------------------------------------------------------
253-- | @delay them p@ is a process that waits for a signal (a message of type @()@)
254-- from 'them' (origin is not verified) before proceeding as @p@. In order to
255-- avoid waiting forever, @delay them p@ monitors 'them'. If it receives a
256-- monitor message instead it simply terminates.
257delay :: ProcessId -> Process () -> Process ()
260 let sameRef (ProcessMonitorNotification ref' _ _) = ref == ref'
262 match $ \() -> unmonitor ref >> p
263 , matchIf sameRef $ \_ -> return ()
266-- | 'CP' version of 'delay'
267cpDelay :: ProcessId -> Closure (Process ()) -> Closure (Process ())
268cpDelay = closureApply . cpDelay'
270 cpDelay' :: ProcessId -> Closure (Process () -> Process ())
271 cpDelay' pid = closure decoder (encode pid)
273 decoder :: Static (ByteString -> Process () -> Process ())
274 decoder = delayStatic `staticCompose` decodeProcessIdStatic
276 delayStatic :: Static (ProcessId -> Process () -> Process ())
277 delayStatic = staticLabel "$delay"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list