Skip to content

Commit

Permalink
Make Closure opaque
Browse files Browse the repository at this point in the history
  • Loading branch information
edsko committed Aug 14, 2012
1 parent 0ea5304 commit 8dd5f2f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
Expand Up @@ -33,7 +33,8 @@ import Control.Distributed.Static
, Static
, staticLabel
, staticApply
, Closure(Closure)
, Closure
, closure
, closureApplyStatic
, closureApply
, staticCompose
Expand Down Expand Up @@ -159,7 +160,7 @@ splitCP p q = cpSplitStatic `closureApplyStatic` p `closureApply` q
-- | 'CP' version of 'Control.Monad.return'
returnCP :: forall a. Serializable a
=> Static (SerializableDict a) -> a -> Closure (Process a)
returnCP dict x = Closure decoder (encode x)
returnCP dict x = closure decoder (encode x)
where
decoder :: Static (ByteString -> Process a)
decoder = returnProcessStatic
Expand Down Expand Up @@ -193,22 +194,22 @@ decodeProcessIdStatic = staticLabel "$decodeProcessId"

-- | 'CP' version of 'link'
cpLink :: ProcessId -> Closure (Process ())
cpLink = Closure (linkStatic `staticCompose` decodeProcessIdStatic) . encode
cpLink = closure (linkStatic `staticCompose` decodeProcessIdStatic) . encode
where
linkStatic :: Static (ProcessId -> Process ())
linkStatic = staticLabel "$link"

-- | 'CP' version of 'unlink'
cpUnlink :: ProcessId -> Closure (Process ())
cpUnlink = Closure (unlinkStatic `staticCompose` decodeProcessIdStatic) . encode
cpUnlink = closure (unlinkStatic `staticCompose` decodeProcessIdStatic) . encode
where
unlinkStatic :: Static (ProcessId -> Process ())
unlinkStatic = staticLabel "$unlink"

-- | 'CP' version of 'send'
cpSend :: forall a. Typeable a
=> Static (SerializableDict a) -> ProcessId -> CP a ()
cpSend dict pid = Closure decoder (encode pid)
cpSend dict pid = closure decoder (encode pid)
where
decoder :: Static (ByteString -> a -> Process ())
decoder = (sendDictStatic `staticApply` dict)
Expand Down
Expand Up @@ -52,7 +52,8 @@ import Control.Distributed.Static
, registerStatic
, Static
, staticLabel
, Closure(Closure)
, Closure
, closure
, staticCompose
)
import Control.Distributed.Process.Internal.Types (Process)
Expand Down Expand Up @@ -97,7 +98,7 @@ functionTDict = varE . tdictName

mkClosure :: Name -> Q Exp
mkClosure n =
[| Closure ($(mkStatic n) `staticCompose` staticDecode $(functionSDict n))
[| closure ($(mkStatic n) `staticCompose` staticDecode $(functionSDict n))
. encode
|]

Expand Down
14 changes: 10 additions & 4 deletions distributed-static/src/Control/Distributed/Static.hs
Expand Up @@ -206,7 +206,8 @@ module Control.Distributed.Static
, staticSplit
, staticConst
-- * Closures
, Closure(Closure)
, Closure
, closure
-- * Derived closure combinators
, staticClosure
, closureApplyStatic
Expand Down Expand Up @@ -345,6 +346,11 @@ instance Typeable a => Binary (Closure a) where
put (Closure static env) = put static >> put env
get = Closure <$> get <*> get

closure :: Static (ByteString -> a) -- ^ Decoder
-> ByteString -- ^ Encoded closure environment
-> Closure a
closure = Closure

-- | Resolve a closure
unclosure :: Typeable a => RemoteTable -> Closure a -> Either String a
unclosure rtable (Closure static env) = do
Expand All @@ -353,7 +359,7 @@ unclosure rtable (Closure static env) = do

-- | Convert a static value into a closure.
staticClosure :: Typeable a => Static a -> Closure a
staticClosure static = Closure (staticConst static) empty
staticClosure static = closure (staticConst static) empty

--------------------------------------------------------------------------------
-- Predefined static values --
Expand Down Expand Up @@ -406,7 +412,7 @@ staticConst x = constStatic `staticApply` x
closureApplyStatic :: (Typeable a, Typeable b)
=> Static (a -> b) -> Closure a -> Closure b
closureApplyStatic f (Closure decoder env) =
Closure (f `staticCompose` decoder) env
closure (f `staticCompose` decoder) env

decodeEnvPairStatic :: Static (ByteString -> (ByteString, ByteString))
decodeEnvPairStatic = staticLabel "$decodeEnvPair"
Expand All @@ -415,7 +421,7 @@ decodeEnvPairStatic = staticLabel "$decodeEnvPair"
closureApply :: forall a b. (Typeable a, Typeable b)
=> Closure (a -> b) -> Closure a -> Closure b
closureApply (Closure fdec fenv) (Closure xdec xenv) =
Closure decoder (encode (fenv, xenv))
closure decoder (encode (fenv, xenv))
where
decoder :: Static (ByteString -> b)
decoder = appStatic
Expand Down

0 comments on commit 8dd5f2f

Please sign in to comment.