@@ -17,7 +17,6 @@ module Control.Distributed.Static
17 , staticLabel
17 , staticLabel
18 , staticApply
18 , staticApply
19 , staticDuplicate
19 , staticDuplicate
21 -- * Eliminating static values
20 -- * Eliminating static values
22 , RemoteTable
21 , RemoteTable
23 , initRemoteTable
22 , initRemoteTable
@@ -27,18 +26,6 @@ module Control.Distributed.Static
27 , Closure(Closure)
26 , Closure(Closure)
28 , unclosure
27 , unclosure
29 , staticClosure
28 , staticClosure
42 -- * Combinators on static values
29 -- * Combinators on static values
43 , staticCompose
30 , staticCompose
44 , staticSplit
31 , staticSplit
@@ -49,14 +36,13 @@ module Control.Distributed.Static
49 , closureCompose
36 , closureCompose
50 , closureSplit
37 , closureSplit
51 -- * Re-exports
38 -- * Re-exports
53 , toDynamic
41 , toDynamic
58−import Prelude hiding (id, (.), const, flip, fst, snd)
44+import Prelude hiding (const, fst, snd)
59−import qualified Prelude (const, flip, fst, snd)
45+import qualified Prelude (const, fst, snd)
60import Data.Binary
46import Data.Binary
61 ( Binary(get, put)
47 ( Binary(get, put)
@@ -70,9 +56,8 @@ import Data.ByteString.Lazy (ByteString, empty)
70import Data.Map (Map)
56import Data.Map (Map)
71import qualified Data.Map as Map (lookup, empty, insert)
57import qualified Data.Map as Map (lookup, empty, insert)
72import Control.Applicative ((<$>), (<*>))
58import Control.Applicative ((<$>), (<*>))
73−import Control.Category (Category(id, (.)))
74−import qualified Control.Arrow as Arrow (first, second, (***), app)
59+import qualified Control.Arrow as Arrow ((***), app)
75−import Data.Rank1Dynamic (Dynamic, toDynamic, fromDynamic, dynApply, dynTypeRep)
60+import Data.Rank1Dynamic (Dynamic, toDynamic, fromDynamic, dynApply)
76import Data.Rank1Typeable
61import Data.Rank1Typeable
77 ( Typeable
62 ( Typeable
@@ -141,24 +126,6 @@ staticApply (Static f) (Static x) = Static (StaticApply f x)
141staticDuplicate :: forall a. Static a -> Static (Static a)
126staticDuplicate :: forall a. Static a -> Static (Static a)
142staticDuplicate (Static x) = Static (StaticDuplicate x)
127staticDuplicate (Static x) = Static (StaticDuplicate x)
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)))
150−typeOfStaticLabel :: StaticLabel -> TypeRep
151−typeOfStaticLabel = undefined
153−typeOfStaticLabel (StaticLabel _ typ)
155−typeOfStaticLabel (StaticApply f x)
156− = fromJust $ funResultTy (typeOfStaticLabel f) (typeOfStaticLabel x)
157−typeOfStaticLabel (StaticDuplicate _ typ)
162--------------------------------------------------------------------------------
129--------------------------------------------------------------------------------
163-- Eliminating static values --
130-- Eliminating static values --
164--------------------------------------------------------------------------------
131--------------------------------------------------------------------------------
@@ -169,84 +136,54 @@ newtype RemoteTable = RemoteTable (Map String Dynamic)
169-- | Initial remote table
136-- | Initial remote table
170initRemoteTable :: RemoteTable
137initRemoteTable :: RemoteTable
171initRemoteTable =
138initRemoteTable =
172− registerStatic "$id" (toDynamic identity)
173− . registerStatic "$compose" (toDynamic compose)
139+ registerStatic "$compose" (toDynamic compose)
174 . registerStatic "$const" (toDynamic const)
140 . 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)
180 . registerStatic "$split" (toDynamic split)
141 . registerStatic "$split" (toDynamic split)
181− . registerStatic "$unit" (toDynamic ())
182 . registerStatic "$app" (toDynamic app)
142 . registerStatic "$app" (toDynamic app)
183 . registerStatic "$decodeEnvPair" (toDynamic decodeEnvPair)
143 . registerStatic "$decodeEnvPair" (toDynamic decodeEnvPair)
184 $ RemoteTable Map.empty
144 $ RemoteTable Map.empty
186− identity :: ANY -> ANY
189 compose :: (ANY2 -> ANY3) -> (ANY1 -> ANY2) -> ANY1 -> ANY3
146 compose :: (ANY2 -> ANY3) -> (ANY1 -> ANY2) -> ANY1 -> ANY3
190 compose = (.)
147 compose = (.)
192 const :: ANY1 -> ANY2 -> ANY1
149 const :: ANY1 -> ANY2 -> ANY1
193 const = Prelude.const
150 const = Prelude.const
195− flip :: (ANY1 -> ANY2 -> ANY3) -> ANY2 -> ANY1 -> ANY3
198− fst :: (ANY1, ANY2) -> ANY1
201− snd :: (ANY1, ANY2) -> ANY2
204− first :: (ANY1 -> ANY2) -> (ANY1, ANY3) -> (ANY2, ANY3)
207− second :: (ANY1 -> ANY2) -> (ANY3, ANY1) -> (ANY3, ANY2)
208− second = Arrow.second
210 split :: (ANY1 -> ANY3) -> (ANY2 -> ANY4) -> (ANY1, ANY2) -> (ANY3, ANY4)
152 split :: (ANY1 -> ANY3) -> (ANY2 -> ANY4) -> (ANY1, ANY2) -> (ANY3, ANY4)
211 split = (Arrow.***)
153 split = (Arrow.***)
213− decodeEnvPair :: ByteString -> (ByteString, ByteString)
214− decodeEnvPair = decode
216 app :: (ANY1 -> ANY2, ANY1) -> ANY2
155 app :: (ANY1 -> ANY2, ANY1) -> ANY2
217 app = Arrow.app
156 app = Arrow.app
158+ decodeEnvPair :: ByteString -> (ByteString, ByteString)
159+ decodeEnvPair = decode
219-- | Register a static label
161-- | Register a static label
220registerStatic :: String -> Dynamic -> RemoteTable -> RemoteTable
162registerStatic :: String -> Dynamic -> RemoteTable -> RemoteTable
221registerStatic label dyn (RemoteTable rtable)
163registerStatic label dyn (RemoteTable rtable)
222 = RemoteTable (Map.insert label dyn rtable)
164 = RemoteTable (Map.insert label dyn rtable)
224-- Pseudo-type: RemoteTable -> Static a -> a
166-- Pseudo-type: RemoteTable -> Static a -> a
225−resolveStaticLabel :: RemoteTable -> StaticLabel -> Maybe Dynamic
167+resolveStaticLabel :: RemoteTable -> StaticLabel -> Either String Dynamic
226resolveStaticLabel (RemoteTable rtable) (StaticLabel label) =
168resolveStaticLabel (RemoteTable rtable) (StaticLabel label) =
227− Map.lookup label rtable
169+ case Map.lookup label rtable of
170+ Nothing -> Left $ "Invalid static label '" ++ label ++ "'"
228resolveStaticLabel rtable (StaticApply label1 label2) = do
172resolveStaticLabel rtable (StaticApply label1 label2) = do
229 f <- resolveStaticLabel rtable label1
173 f <- resolveStaticLabel rtable label1
230 x <- resolveStaticLabel rtable label2
174 x <- resolveStaticLabel rtable label2
231− case f `dynApply` x of
175+ f `dynApply` x
232− Left _err -> Nothing
234resolveStaticLabel rtable (StaticDuplicate label) = do
176resolveStaticLabel rtable (StaticDuplicate label) = do
235 x <- resolveStaticLabel rtable label -- Resolve only to get type info
177 x <- resolveStaticLabel rtable label -- Resolve only to get type info
236− case toDynamic mkStatic `dynApply` x of
178+ toDynamic mkStatic `dynApply` x
237− Left _err -> Nothing
240 mkStatic :: ANY -> Static ANY
180 mkStatic :: ANY -> Static ANY
241 mkStatic _ = Static label
181 mkStatic _ = Static label
243−unstatic :: Typeable a => RemoteTable -> Static a -> Maybe a
183+unstatic :: Typeable a => RemoteTable -> Static a -> Either String a
244−unstatic rtable (Static static) =
184+unstatic rtable (Static static) = do
245− case resolveStaticLabel rtable static of
185+ dyn <- resolveStaticLabel rtable static
247− Just dyn -> case fromDynamic dyn of
186+ fromDynamic dyn
248− Left _err -> Nothing
251--------------------------------------------------------------------------------
188--------------------------------------------------------------------------------
252-- Closures --
189-- Closures --
@@ -261,7 +198,7 @@ instance Typeable a => Binary (Closure a) where
261 get = Closure <$> get <*> get
198 get = Closure <$> get <*> get
263-- | Resolve a closure
200-- | Resolve a closure
264−unclosure :: Typeable a => RemoteTable -> Closure a -> Maybe a
201+unclosure :: Typeable a => RemoteTable -> Closure a -> Either String a
265unclosure rtable (Closure static env) = do
202unclosure rtable (Closure static env) = do
266 f <- unstatic rtable static
203 f <- unstatic rtable static
267 return (f env)
204 return (f env)
@@ -274,11 +211,6 @@ staticClosure static = Closure (staticConst static) empty
274-- Predefined static values --
211-- Predefined static values --
275--------------------------------------------------------------------------------
212--------------------------------------------------------------------------------
277−-- | Static version of 'id'
278−idStatic :: (Typeable a)
280−idStatic = staticLabel "$id"
282-- | Static version of ('Prelude..')
214-- | Static version of ('Prelude..')
283composeStatic :: (Typeable a, Typeable b, Typeable c)
215composeStatic :: (Typeable a, Typeable b, Typeable c)
284 => Static ((b -> c) -> (a -> b) -> a -> c)
216 => Static ((b -> c) -> (a -> b) -> a -> c)
@@ -289,40 +221,11 @@ constStatic :: (Typeable a, Typeable b)
289 => Static (a -> b -> a)
221 => Static (a -> b -> a)
290constStatic = staticLabel "$const"
222constStatic = staticLabel "$const"
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"
297−-- | Static version of 'fst'
298−fstStatic :: (Typeable a, Typeable b)
299− => Static ((a, b) -> a)
300−fstStatic = staticLabel "$fst"
302−-- | Static version of 'snd'
303−sndStatic :: (Typeable a, Typeable b)
304− => Static ((a, b) -> b)
305−sndStatic = staticLabel "$snd"
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"
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"
317-- | Static version of ('Arrow.***')
224-- | Static version of ('Arrow.***')
318splitStatic :: (Typeable a, Typeable a', Typeable b, Typeable b')
225splitStatic :: (Typeable a, Typeable a', Typeable b, Typeable b')
319 => Static ((a -> b) -> (a' -> b') -> (a, a') -> (b, b'))
226 => Static ((a -> b) -> (a' -> b') -> (a, a') -> (b, b'))
320splitStatic = staticLabel "$split"
227splitStatic = staticLabel "$split"
322−-- | Static version of @()@
323−unitStatic :: Static ()
324−unitStatic = staticLabel "$unit"
326-- | Static version of 'Arrow.app'
229-- | Static version of 'Arrow.app'
327appStatic :: (Typeable a, Typeable b)
230appStatic :: (Typeable a, Typeable b)
328 => Static ((a -> b, a) -> b)
231 => Static ((a -> b, a) -> b)