git.iko.soy/distributed-process
on master help sign in
Clean up static
commit6e8a3330fd1ec5f8f7e4785799967a61020eeb54 authorEdsko de Vries <edsko@edsko.net> dateFri 10 Aug 2012, 10:16 (14 years ago) parentf97c3de
6 files changed+49−158
distributed-process/src/Control/Distributed/Process.hs+2−1 distributed-process/src/Control/Distributed/Process/Closure.hs+2−26 distributed-process/src/Control/Distributed/Process/Internal/Closure/BuiltIn.hs+15 distributed-process/src/Control/Distributed/Process/Internal/Primitives.hs+4−5 distributed-process/src/Control/Distributed/Process/Node.hs+6−9 distributed-static/src/Control/Distributed/Static.hs+20−117
distributed-process/src/Control/Distributed/Process.hs+2−1
@@ -137,6 +137,7 @@ import Control.Distributed.Process.Serializable (SerializableDict)
137137import Control.Distributed.Process.Internal.Closure.BuiltIn
138138 ( sdictUnit
139139 , sdictSendPort
140+ , sndStatic
140141 , idCP
141142 , seqCP
142143 , bindCP
@@ -147,7 +148,7 @@ import Control.Distributed.Process.Internal.Closure.BuiltIn
147148 , cpSend
148149 , cpNewChan
149150 )
150−import Control.Distributed.Static (closureCompose, sndStatic, staticClosure)
151+import Control.Distributed.Static (closureCompose, staticClosure)
151152
152153{-
153154import Control.Distributed.Process.Internal.Closure.CP
distributed-process/src/Control/Distributed/Process/Closure.hs+2−26
@@ -228,17 +228,6 @@ module Control.Distributed.Process.Closure
228228 -- * Primitive operations on static values
229229 , staticApply
230230 , staticDuplicate
231− -- * Static values
232− , idStatic
233− , composeStatic
234− , constStatic
235− , flipStatic
236− , fstStatic
237− , sndStatic
238− , firstStatic
239− , secondStatic
240− , splitStatic
241− , unitStatic
242231 -- * Combinators on static values
243232 , staticCompose
244233 -- * Serialization dictionaries (and their static versions)
@@ -272,21 +261,9 @@ import Control.Distributed.Static
272261 ( -- Introducing static values
273262 staticApply
274263 , staticDuplicate
275− -- Static values
276− , idStatic
277− , composeStatic
278− , constStatic
279− , flipStatic
280− , fstStatic
281− , sndStatic
282− , firstStatic
283− , secondStatic
284− , splitStatic
285− , unitStatic
286264 -- Combinators on static values
287265 , staticCompose
288266 , staticClosure
289− , closureSplit
290267 )
291268
292269
@@ -304,10 +281,9 @@ import Control.Distributed.Process.Internal.Closure.TH
304281 , functionTDict
305282 )
306283import Control.Distributed.Process.Internal.Closure.BuiltIn
307− ( -- Remote table
308− remoteTable
284+ (
309285 -- Static dictionaries and associated operations
310− , staticDecode
286+ staticDecode
311287 , sdictUnit
312288 , sdictProcessId
313289 , sdictSendPort
distributed-process/src/Control/Distributed/Process/Internal/Closure/BuiltIn.hs+15
@@ -6,6 +6,8 @@ module Control.Distributed.Process.Internal.Closure.BuiltIn
66 , sdictUnit
77 , sdictProcessId
88 , sdictSendPort
9+ -- * Some static values
10+ , sndStatic
911 -- * The CP type and associated combinators
1012 , CP
1113 , idCP
@@ -21,6 +23,8 @@ module Control.Distributed.Process.Internal.Closure.BuiltIn
2123 , cpNewChan
2224 ) where
2325
26+import Prelude hiding (snd)
27+import qualified Prelude (snd)
2428import Data.ByteString.Lazy (ByteString)
2529import Data.Binary (decode, encode)
2630import Data.Rank1Typeable (Typeable, ANY, ANY1, ANY2, ANY3, ANY4)
@@ -75,6 +79,7 @@ remoteTable =
7579 . registerStatic "$expectDict" (toDynamic expectDict)
7680 . registerStatic "$newChanDict" (toDynamic newChanDict)
7781 . registerStatic "$cpSplit" (toDynamic cpSplit)
82+ . registerStatic "$snd" (toDynamic snd)
7883 where
7984 decodeDict :: SerializableDict ANY -> ByteString -> ANY
8085 decodeDict SerializableDict = decode
@@ -112,6 +117,9 @@ remoteTable =
112117 cpSplit :: (ANY1 -> Process ANY3) -> (ANY2 -> Process ANY4) -> (ANY1, ANY2) -> Process (ANY3, ANY4)
113118 cpSplit f g (a, b) = f a >>= \c -> g b >>= \d -> return (c, d)
114119
120+ snd :: (ANY1, ANY2) -> ANY2
121+ snd = Prelude.snd
122+
115123--------------------------------------------------------------------------------
116124-- Static dictionaries and associated operations --
117125--------------------------------------------------------------------------------
@@ -139,6 +147,13 @@ sdictSendPort :: Typeable a
139147 => Static (SerializableDict a) -> Static (SerializableDict (SendPort a))
140148sdictSendPort = staticApply (staticLabel "$sdictSendPort_")
141149
150+--------------------------------------------------------------------------------
151+-- Static values --
152+--------------------------------------------------------------------------------
153+
154+sndStatic :: Static ((a, b) -> b)
155+sndStatic = staticLabel "$snd"
156+
142157--------------------------------------------------------------------------------
143158-- The CP type and associated combinators --
144159--------------------------------------------------------------------------------
distributed-process/src/Control/Distributed/Process/Internal/Primitives.hs+4−5
@@ -66,14 +66,13 @@ import Prelude hiding (catch)
6666#endif
6767
6868import Data.Binary (decode)
69−import Data.Typeable (Typeable, typeOf)
7069import Data.Time.Clock (getCurrentTime)
7170import Data.Time.Format (formatTime)
7271import System.Locale (defaultTimeLocale)
7372import Control.Monad.Reader (ask)
7473import Control.Monad.IO.Class (MonadIO, liftIO)
7574import Control.Applicative ((<$>))
76−import Control.Exception (Exception, throw, throwIO, SomeException)
75+import Control.Exception (Exception, throwIO, SomeException)
7776import qualified Control.Exception as Ex (catch, mask)
7877import Control.Concurrent.MVar (modifyMVar)
7978import Control.Concurrent.Chan (writeChan)
@@ -90,7 +89,7 @@ import Control.Concurrent.STM
9089import Control.Distributed.Process.Internal.CQueue (dequeue, BlockSpec(..))
9190import Control.Distributed.Process.Serializable (Serializable, fingerprint)
9291import Data.Accessor ((^.), (^:), (^=))
93−import Control.Distributed.Static (Closure(Closure), fromDynamic, dynTypeRep)
92+import Control.Distributed.Static (Typeable, Closure)
9493import qualified Control.Distributed.Static as Static (unclosure)
9594import Control.Distributed.Process.Internal.Types
9695 ( NodeId(..)
@@ -506,8 +505,8 @@ unClosure :: forall a. Typeable a => Closure a -> Process a
506505unClosure closure = do
507506 rtable <- remoteTable . processNode <$> ask
508507 case Static.unclosure rtable closure of
509− Nothing -> fail $ "Could not resolve closure " ++ show closure
510− Just x -> return x
508+ Left err -> fail $ "Could not resolve closure: " ++ err
509+ Right x -> return x
511510
512511--------------------------------------------------------------------------------
513512-- Auxiliary functions --
distributed-process/src/Control/Distributed/Process/Node.hs+6−9
@@ -31,7 +31,7 @@ import qualified Data.List as List (delete, (\\))
3131import Data.Set (Set)
3232import qualified Data.Set as Set (empty, insert, delete, member, (\\), fromList)
3333import Data.Foldable (forM_)
34−import Data.Maybe (isJust, fromMaybe)
34+import Data.Maybe (isJust)
3535import Data.Typeable (Typeable)
3636import Control.Category ((>>>))
3737import Control.Applicative ((<$>))
@@ -69,7 +69,7 @@ import qualified Network.Transport as NT
6969import Data.Accessor (Accessor, accessor, (^.), (^=), (^:))
7070import qualified Data.Accessor.Container as DAC (mapDefault, mapMaybe)
7171import System.Random (randomIO)
72−import Control.Distributed.Static (RemoteTable, Closure(Closure), fromDynamic)
72+import Control.Distributed.Static (RemoteTable, Closure)
7373import qualified Control.Distributed.Static as Static (unclosure)
7474import Control.Distributed.Process.Internal.Types
7575 ( NodeId(..)
@@ -121,11 +121,6 @@ import Control.Distributed.Process.Internal.Primitives (expect, register, finall
121121import qualified Control.Distributed.Process.Internal.Closure.BuiltIn as BuiltIn (remoteTable)
122122import qualified Control.Distributed.Static as Static (initRemoteTable)
123123
124−-- import Control.Distributed.Process.Internal.Dynamic (fromDynamic)
125−-- import Control.Distributed.Process.Internal.Closure.Resolution (resolveClosure)
126−-- import qualified Control.Distributed.Process.Internal.Closure.Static as Static (__remoteTable)
127−-- import qualified Control.Distributed.Process.Internal.Closure.CP as CP (__remoteTable)
128−
129124--------------------------------------------------------------------------------
130125-- Initialization --
131126--------------------------------------------------------------------------------
@@ -509,7 +504,9 @@ ncEffectSpawn pid cProc ref = do
509504 mProc <- unClosure cProc
510505 -- If the closure does not exist, we spawn a process that throws an exception
511506 -- This allows the remote node to find out what's happening
512− let proc = fromMaybe (fail $ "Error: unknown closure " ++ show cProc) mProc
507+ let proc = case mProc of
508+ Left err -> fail $ "Error: Could not resolve closure: " ++ err
509+ Right p -> p
513510 node <- ask
514511 pid' <- liftIO $ forkProcess node proc
515512 liftIO $ sendMessage node
@@ -603,7 +600,7 @@ isLocal :: LocalNode -> Identifier -> Bool
603600isLocal nid ident = nodeOf ident == localNodeId nid
604601
605602-- | Lookup a local closure
606−unClosure :: Typeable a => Closure a -> NC (Maybe a)
603+unClosure :: Typeable a => Closure a -> NC (Either String a)
607604unClosure closure = do
608605 rtable <- remoteTable <$> ask
609606 return (Static.unclosure rtable closure)
distributed-static/src/Control/Distributed/Static.hs+20−117
@@ -17,7 +17,6 @@ module Control.Distributed.Static
1717 , staticLabel
1818 , staticApply
1919 , staticDuplicate
20−-- , staticTypeOf
2120 -- * Eliminating static values
2221 , RemoteTable
2322 , initRemoteTable
@@ -27,18 +26,6 @@ module Control.Distributed.Static
2726 , Closure(Closure)
2827 , unclosure
2928 , staticClosure
30− -- * Static values
31− , idStatic
32− , composeStatic
33− , constStatic
34− , flipStatic
35− , fstStatic
36− , sndStatic
37− , firstStatic
38− , secondStatic
39− , splitStatic
40− , unitStatic
41− , appStatic
4229 -- * Combinators on static values
4330 , staticCompose
4431 , staticSplit
@@ -49,14 +36,13 @@ module Control.Distributed.Static
4936 , closureCompose
5037 , closureSplit
5138 -- * Re-exports
39+ , Typeable
5240 , Dynamic
5341 , toDynamic
54− , fromDynamic
55− , dynTypeRep
5642 ) where
5743
58−import Prelude hiding (id, (.), const, flip, fst, snd)
59−import qualified Prelude (const, flip, fst, snd)
44+import Prelude hiding (const, fst, snd)
45+import qualified Prelude (const, fst, snd)
6046import Data.Binary
6147 ( Binary(get, put)
6248 , Put
@@ -70,9 +56,8 @@ import Data.ByteString.Lazy (ByteString, empty)
7056import Data.Map (Map)
7157import qualified Data.Map as Map (lookup, empty, insert)
7258import Control.Applicative ((<$>), (<*>))
73−import Control.Category (Category(id, (.)))
74−import qualified Control.Arrow as Arrow (first, second, (***), app)
75−import Data.Rank1Dynamic (Dynamic, toDynamic, fromDynamic, dynApply, dynTypeRep)
59+import qualified Control.Arrow as Arrow ((***), app)
60+import Data.Rank1Dynamic (Dynamic, toDynamic, fromDynamic, dynApply)
7661import Data.Rank1Typeable
7762 ( Typeable
7863 , typeOf
@@ -141,24 +126,6 @@ staticApply (Static f) (Static x) = Static (StaticApply f x)
141126staticDuplicate :: forall a. Static a -> Static (Static a)
142127staticDuplicate (Static x) = Static (StaticDuplicate x)
143128
144−{-
145−-- | @staticTypeOf (x :: a)@ mimicks @static (undefined :: a)@ -- even if
146−-- @x@ is not static, the type of @x@ always is.
147−staticTypeOf :: forall a. Typeable a => a -> Static a
148−staticTypeOf _ = Static (StaticLabel "$undefined" (typeOf (undefined :: a)))
149−
150−typeOfStaticLabel :: StaticLabel -> TypeRep
151−typeOfStaticLabel = undefined
152−{-
153−typeOfStaticLabel (StaticLabel _ typ)
154− = typ
155−typeOfStaticLabel (StaticApply f x)
156− = fromJust $ funResultTy (typeOfStaticLabel f) (typeOfStaticLabel x)
157−typeOfStaticLabel (StaticDuplicate _ typ)
158− = typ
159−-}
160−-}
161−
162129--------------------------------------------------------------------------------
163130-- Eliminating static values --
164131--------------------------------------------------------------------------------
@@ -169,84 +136,54 @@ newtype RemoteTable = RemoteTable (Map String Dynamic)
169136-- | Initial remote table
170137initRemoteTable :: RemoteTable
171138initRemoteTable =
172− registerStatic "$id" (toDynamic identity)
173− . registerStatic "$compose" (toDynamic compose)
139+ registerStatic "$compose" (toDynamic compose)
174140 . registerStatic "$const" (toDynamic const)
175− . registerStatic "$flip" (toDynamic flip)
176− . registerStatic "$fst" (toDynamic fst)
177− . registerStatic "$snd" (toDynamic snd)
178− . registerStatic "$first" (toDynamic first)
179− . registerStatic "$second" (toDynamic second)
180141 . registerStatic "$split" (toDynamic split)
181− . registerStatic "$unit" (toDynamic ())
182142 . registerStatic "$app" (toDynamic app)
183143 . registerStatic "$decodeEnvPair" (toDynamic decodeEnvPair)
184144 $ RemoteTable Map.empty
185145 where
186− identity :: ANY -> ANY
187− identity = id
188−
189146 compose :: (ANY2 -> ANY3) -> (ANY1 -> ANY2) -> ANY1 -> ANY3
190147 compose = (.)
191148
192149 const :: ANY1 -> ANY2 -> ANY1
193150 const = Prelude.const
194151
195− flip :: (ANY1 -> ANY2 -> ANY3) -> ANY2 -> ANY1 -> ANY3
196− flip = Prelude.flip
197−
198− fst :: (ANY1, ANY2) -> ANY1
199− fst = Prelude.fst
200−
201− snd :: (ANY1, ANY2) -> ANY2
202− snd = Prelude.snd
203−
204− first :: (ANY1 -> ANY2) -> (ANY1, ANY3) -> (ANY2, ANY3)
205− first = Arrow.first
206−
207− second :: (ANY1 -> ANY2) -> (ANY3, ANY1) -> (ANY3, ANY2)
208− second = Arrow.second
209−
210152 split :: (ANY1 -> ANY3) -> (ANY2 -> ANY4) -> (ANY1, ANY2) -> (ANY3, ANY4)
211153 split = (Arrow.***)
212154
213− decodeEnvPair :: ByteString -> (ByteString, ByteString)
214− decodeEnvPair = decode
215−
216155 app :: (ANY1 -> ANY2, ANY1) -> ANY2
217156 app = Arrow.app
218157
158+ decodeEnvPair :: ByteString -> (ByteString, ByteString)
159+ decodeEnvPair = decode
160+
219161-- | Register a static label
220162registerStatic :: String -> Dynamic -> RemoteTable -> RemoteTable
221163registerStatic label dyn (RemoteTable rtable)
222164 = RemoteTable (Map.insert label dyn rtable)
223165
224166-- Pseudo-type: RemoteTable -> Static a -> a
225−resolveStaticLabel :: RemoteTable -> StaticLabel -> Maybe Dynamic
167+resolveStaticLabel :: RemoteTable -> StaticLabel -> Either String Dynamic
226168resolveStaticLabel (RemoteTable rtable) (StaticLabel label) =
227− Map.lookup label rtable
169+ case Map.lookup label rtable of
170+ Nothing -> Left $ "Invalid static label '" ++ label ++ "'"
171+ Just d -> Right d
228172resolveStaticLabel rtable (StaticApply label1 label2) = do
229173 f <- resolveStaticLabel rtable label1
230174 x <- resolveStaticLabel rtable label2
231− case f `dynApply` x of
232− Left _err -> Nothing
233− Right y -> Just y
175+ f `dynApply` x
234176resolveStaticLabel rtable (StaticDuplicate label) = do
235177 x <- resolveStaticLabel rtable label -- Resolve only to get type info
236− case toDynamic mkStatic `dynApply` x of
237− Left _err -> Nothing
238− Right y -> Just y
178+ toDynamic mkStatic `dynApply` x
239179 where
240180 mkStatic :: ANY -> Static ANY
241181 mkStatic _ = Static label
242182
243−unstatic :: Typeable a => RemoteTable -> Static a -> Maybe a
244−unstatic rtable (Static static) =
245− case resolveStaticLabel rtable static of
246− Nothing -> Nothing
247− Just dyn -> case fromDynamic dyn of
248− Left _err -> Nothing
249− Right x -> Just x
183+unstatic :: Typeable a => RemoteTable -> Static a -> Either String a
184+unstatic rtable (Static static) = do
185+ dyn <- resolveStaticLabel rtable static
186+ fromDynamic dyn
250187
251188--------------------------------------------------------------------------------
252189-- Closures --
@@ -261,7 +198,7 @@ instance Typeable a => Binary (Closure a) where
261198 get = Closure <$> get <*> get
262199
263200-- | Resolve a closure
264−unclosure :: Typeable a => RemoteTable -> Closure a -> Maybe a
201+unclosure :: Typeable a => RemoteTable -> Closure a -> Either String a
265202unclosure rtable (Closure static env) = do
266203 f <- unstatic rtable static
267204 return (f env)
@@ -274,11 +211,6 @@ staticClosure static = Closure (staticConst static) empty
274211-- Predefined static values --
275212--------------------------------------------------------------------------------
276213
277−-- | Static version of 'id'
278−idStatic :: (Typeable a)
279− => Static (a -> a)
280−idStatic = staticLabel "$id"
281−
282214-- | Static version of ('Prelude..')
283215composeStatic :: (Typeable a, Typeable b, Typeable c)
284216 => Static ((b -> c) -> (a -> b) -> a -> c)
@@ -289,40 +221,11 @@ constStatic :: (Typeable a, Typeable b)
289221 => Static (a -> b -> a)
290222constStatic = staticLabel "$const"
291223
292−-- | Static version of 'flip'
293−flipStatic :: (Typeable a, Typeable b, Typeable c)
294− => Static ((a -> b -> c) -> b -> a -> c)
295−flipStatic = staticLabel "$flip"
296−
297−-- | Static version of 'fst'
298−fstStatic :: (Typeable a, Typeable b)
299− => Static ((a, b) -> a)
300−fstStatic = staticLabel "$fst"
301−
302−-- | Static version of 'snd'
303−sndStatic :: (Typeable a, Typeable b)
304− => Static ((a, b) -> b)
305−sndStatic = staticLabel "$snd"
306−
307−-- | Static version of 'Arrow.first'
308−firstStatic :: (Typeable a, Typeable b, Typeable c)
309− => Static ((a -> b) -> (a, c) -> (b, c))
310−firstStatic = staticLabel "$first"
311−
312−-- | Static version of 'Arrow.second'
313−secondStatic :: (Typeable a, Typeable b, Typeable c)
314− => Static ((a -> b) -> (c, a) -> (c, b))
315−secondStatic = staticLabel "$second"
316−
317224-- | Static version of ('Arrow.***')
318225splitStatic :: (Typeable a, Typeable a', Typeable b, Typeable b')
319226 => Static ((a -> b) -> (a' -> b') -> (a, a') -> (b, b'))
320227splitStatic = staticLabel "$split"
321228
322−-- | Static version of @()@
323−unitStatic :: Static ()
324−unitStatic = staticLabel "$unit"
325−
326229-- | Static version of 'Arrow.app'
327230appStatic :: (Typeable a, Typeable b)
328231 => Static ((a -> b, a) -> b)