@@ -1,6 +1,7 @@
1module Control.Distributed.Process.Internal.Closure.Combinators
1module Control.Distributed.Process.Internal.Closure.Combinators
2− ( -- * Generic combinators
3+ -- * Generic combinators
3− closureApply
4+ , closureApply
4 , closureConst
5 , closureConst
5 , closureUnit
6 , closureUnit
6 -- * Arrow combinators for processes
7 -- * Arrow combinators for processes
@@ -28,16 +29,75 @@ module Control.Distributed.Process.Internal.Closure.Combinators
30import Prelude hiding (lookup)
31import Prelude hiding (lookup)
31−import qualified Data.ByteString.Lazy as BS (empty)
32import Data.Binary (encode)
32import Data.Binary (encode)
33import Data.Typeable (typeOf, Typeable)
33import Data.Typeable (typeOf, Typeable)
34+import Data.Tuple (swap)
35+import Control.Applicative ((<$>))
36+import Control.Monad ((>=>))
34import Control.Distributed.Process.Internal.Types
37import Control.Distributed.Process.Internal.Types
35 ( Closure(..)
38 ( Closure(..)
36 , Static(..)
39 , Static(..)
37 , StaticLabel(..)
40 , StaticLabel(..)
40import Control.Distributed.Process.Internal.TypeRep () -- Binary instances
44import Control.Distributed.Process.Internal.TypeRep () -- Binary instances
45+import Control.Distributed.Process.Internal.Closure.TH (remotable, mkClosure)
46+import Control.Distributed.Process.Internal.Primitives (unClosure)
48+--------------------------------------------------------------------------------
50+--------------------------------------------------------------------------------
55+returnProcess :: a -> Process a
56+returnProcess = return
58+kleisliComposeProcess :: (a -> Process b) -> (b -> Process c) -> a -> Process c
59+kleisliComposeProcess = (>=>)
61+firstProcess :: (a -> Process b) -> (a, c) -> Process (b, c)
62+firstProcess f (a, c) = f a >>= \b -> return (b, c)
64+swapProcess :: (a, b) -> Process (b, a)
65+swapProcess = return . swap
67+copyProcess :: a -> Process (a, a)
68+copyProcess x = return (x, x)
70+leftProcess :: (a -> Process b) -> Either a c -> Process (Either b c)
71+leftProcess f (Left a) = Left <$> f a
72+leftProcess _ (Right b) = Right <$> return b
74+mirrorProcess :: Either a b -> Process (Either b a)
75+mirrorProcess (Left x) = Right <$> return x
76+mirrorProcess (Right y) = Left <$> return y
78+untagProcess :: Either a a -> Process a
79+untagProcess (Left x) = return x
80+untagProcess (Right y) = return y
82+applyClosureProcess :: (Typeable a, Typeable b) => (Closure (a -> Process b), a) -> Process b
83+applyClosureProcess (procClosure, a) = do
84+ proc <- unClosure procClosure
90+ , 'kleisliComposeProcess
99+remoteTable :: RemoteTable -> RemoteTable
100+remoteTable = __remoteTable
42--------------------------------------------------------------------------------
102--------------------------------------------------------------------------------
43-- Generic closure combinators --
103-- Generic closure combinators --
@@ -47,15 +107,11 @@ closureApply :: Closure (a -> b) -> Closure a -> Closure b
47closureApply (Closure (Static labelf) envf) (Closure (Static labelx) envx) =
107closureApply (Closure (Static labelf) envf) (Closure (Static labelx) envx) =
48 Closure (Static ClosureApply) $ encode (labelf, envf, labelx, envx)
108 Closure (Static ClosureApply) $ encode (labelf, envf, labelx, envx)
50−closureConst :: forall a b. (Typeable a, Typeable b)
110+closureConst :: (Typeable a, Typeable b) => Closure (a -> b -> a)
51− => Closure (a -> b -> a)
111+closureConst = $(mkClosure 'const)
52−closureConst = Closure (Static ClosureConst) (encode $ typeOf aux)
57closureUnit :: Closure ()
113closureUnit :: Closure ()
58−closureUnit = Closure (Static ClosureUnit) BS.empty
114+closureUnit = $(mkClosure 'idUnit) ()
60--------------------------------------------------------------------------------
116--------------------------------------------------------------------------------
61-- Arrow combinators for processes --
117-- Arrow combinators for processes --
@@ -73,37 +129,19 @@ cpElim = flip closureApply closureUnit
74cpId :: forall a. Typeable a
130cpId :: forall a. Typeable a
75 => CP a a
131 => CP a a
76−cpId = Closure (Static CpId) (encode $ typeOf aux)
132+cpId = $(mkClosure 'returnProcess)
78− aux :: a -> Process a
81cpComp :: forall a b c. (Typeable a, Typeable b, Typeable c)
134cpComp :: forall a b c. (Typeable a, Typeable b, Typeable c)
82 => CP a b -> CP b c -> CP a c
135 => CP a b -> CP b c -> CP a c
83−cpComp f g = comp `closureApply` f `closureApply` g
136+cpComp f g = $(mkClosure 'kleisliComposeProcess) `closureApply` f `closureApply` g
85− comp :: Closure ((a -> Process b) -> (b -> Process c) -> a -> Process c)
86− comp = Closure (Static CpComp) (encode $ typeOf aux)
88− aux :: (a -> Process b) -> (b -> Process c) -> a -> Process c
91cpFirst :: forall a b c. (Typeable a, Typeable b, Typeable c)
138cpFirst :: forall a b c. (Typeable a, Typeable b, Typeable c)
92 => CP a b -> CP (a, c) (b, c)
139 => CP a b -> CP (a, c) (b, c)
93−cpFirst = closureApply first
140+cpFirst = closureApply $(mkClosure 'firstProcess)
95− first :: Closure ((a -> Process b) -> (a, c) -> Process (b, c))
96− first = Closure (Static CpFirst) (encode $ typeOf aux)
98− aux :: (a -> Process b) -> (a, c) -> Process (b, c)
101cpSwap :: forall a b. (Typeable a, Typeable b)
142cpSwap :: forall a b. (Typeable a, Typeable b)
102 => CP (a, b) (b, a)
143 => CP (a, b) (b, a)
103−cpSwap = Closure (Static CpSwap) (encode $ typeOf aux)
144+cpSwap = $(mkClosure 'swapProcess)
105− aux :: (a, b) -> Process (b, a)
108cpSecond :: (Typeable a, Typeable b, Typeable c)
146cpSecond :: (Typeable a, Typeable b, Typeable c)
109 => CP a b -> CP (c, a) (c, b)
147 => CP a b -> CP (c, a) (c, b)
@@ -115,10 +153,7 @@ cpPair f g = cpFirst f `cpComp` cpSecond g
116cpCopy :: forall a. Typeable a
154cpCopy :: forall a. Typeable a
117 => CP a (a, a)
155 => CP a (a, a)
118−cpCopy = Closure (Static CpCopy) (encode $ typeOf aux)
156+cpCopy = $(mkClosure 'copyProcess)
120− aux :: a -> Process (a, a)
123cpFanOut :: (Typeable a, Typeable b, Typeable c)
158cpFanOut :: (Typeable a, Typeable b, Typeable c)
124 => CP a b -> CP a c -> CP a (b, c)
159 => CP a b -> CP a c -> CP a (b, c)
@@ -126,20 +161,11 @@ cpFanOut f g = cpCopy `cpComp` (f `cpPair` g)
127cpLeft :: forall a b c. (Typeable a, Typeable b, Typeable c)
162cpLeft :: forall a b c. (Typeable a, Typeable b, Typeable c)
128 => CP a b -> CP (Either a c) (Either b c)
163 => CP a b -> CP (Either a c) (Either b c)
129−cpLeft = closureApply left
164+cpLeft = closureApply $(mkClosure 'leftProcess)
131− left :: Closure ((a -> Process b) -> Either a c -> Process (Either b c))
132− left = Closure (Static CpLeft) (encode $ typeOf aux)
134− aux :: (a -> Process b) -> Either a c -> Process (Either b c)
137cpMirror :: forall a b. (Typeable a, Typeable b)
166cpMirror :: forall a b. (Typeable a, Typeable b)
138 => CP (Either a b) (Either b a)
167 => CP (Either a b) (Either b a)
139−cpMirror = Closure (Static CpMirror) (encode $ typeOf aux)
168+cpMirror = $(mkClosure 'mirrorProcess)
141− aux :: Either a b -> Process (Either b a)
144cpRight :: forall a b c. (Typeable a, Typeable b, Typeable c)
170cpRight :: forall a b c. (Typeable a, Typeable b, Typeable c)
145 => CP a b -> CP (Either c a) (Either c b)
171 => CP a b -> CP (Either c a) (Either c b)
@@ -151,10 +177,7 @@ cpEither f g = cpLeft f `cpComp` cpRight g
152cpUntag :: forall a. Typeable a
178cpUntag :: forall a. Typeable a
153 => CP (Either a a) a
179 => CP (Either a a) a
154−cpUntag = Closure (Static CpUntag) (encode $ typeOf aux)
180+cpUntag = $(mkClosure 'untagProcess)
156− aux :: Either a a -> Process a
159cpFanIn :: (Typeable a, Typeable b, Typeable c)
182cpFanIn :: (Typeable a, Typeable b, Typeable c)
160 => CP a c -> CP b c -> CP (Either a b) c
183 => CP a c -> CP b c -> CP (Either a b) c