|
8 | 8 | -- all distributed nodes are using the same 'RemoteTable'). In this module
|
9 | 9 | -- we implement this mimickry and various extensions.
|
10 | 10 | --
|
11 |
| --- [Dynamic type checking] |
12 |
| --- |
13 |
| --- The paper stipulates that 'Static' values should have a free 'Binary' |
14 |
| --- instance: |
15 |
| --- |
16 |
| --- > instance Binary (Static a) |
17 |
| --- |
18 |
| --- This however is not (runtime) type safe: for instance, what would be the |
19 |
| --- behaviour of |
20 |
| --- |
21 |
| --- > f :: Static Int -> Static Bool |
22 |
| --- > f = decode . encode |
23 |
| --- |
24 |
| --- For this reason we work only with 'Typeable' terms in this module, and |
25 |
| --- implement runtime checks |
26 |
| --- |
27 |
| --- > instance Typeable a => Binary (Static a) |
28 |
| --- |
29 |
| --- The above function 'f' typechecks but throws an exception if executed. The |
30 |
| --- type representation we use, however, is not the standard |
31 |
| --- 'Data.Typeable.TypeRep' from "Data.Typeable" but |
32 |
| --- 'Data.Rank1Typeable.TypeRep' from "Data.Rank1Typeable". This means that we |
33 |
| --- can represent polymorphic static values (see below for an example). |
34 |
| --- |
35 |
| --- Since the runtime mapping ('RemoteTable') contains values of different types, |
36 |
| --- it maps labels ('String's) to 'Data.Rank1Dynamic.Dynamic' values. Again, we |
37 |
| --- use the implementation from "Data.Rank1Dynamic" so that we can store |
38 |
| --- polymorphic dynamic values. |
39 |
| --- |
40 | 11 | -- [Compositionality]
|
41 | 12 | --
|
42 | 13 | -- Static values as described in the paper are not compositional: there is no
|
@@ -252,14 +223,13 @@ import Control.DeepSeq (NFData(rnf), force)
|
252 | 223 | import Data.Rank1Dynamic (Dynamic, toDynamic, fromDynamic, dynApply)
|
253 | 224 | import Data.Rank1Typeable
|
254 | 225 | ( Typeable
|
255 |
| - , typeOf |
256 | 226 | , ANY1
|
257 | 227 | , ANY2
|
258 | 228 | , ANY3
|
259 | 229 | , ANY4
|
260 |
| - , isInstanceOf |
261 | 230 | #if __GLASGOW_HASKELL__ >= 710
|
262 | 231 | , TypeRep
|
| 232 | + , typeOf |
263 | 233 | #endif
|
264 | 234 | )
|
265 | 235 |
|
@@ -322,14 +292,9 @@ newtype Static a = Static StaticLabel
|
322 | 292 | instance NFData (Static a) where
|
323 | 293 | rnf (Static s) = rnf s
|
324 | 294 |
|
325 |
| -instance Typeable a => Binary (Static a) where |
326 |
| - put (Static label) = putStaticLabel label >> put (typeOf (undefined :: a)) |
327 |
| - get = do |
328 |
| - label <- getStaticLabel |
329 |
| - typeRep <- get |
330 |
| - case typeOf (undefined :: a) `isInstanceOf` typeRep of |
331 |
| - Left err -> fail $ "Static.get: type error: " ++ err |
332 |
| - Right () -> return (Static label) |
| 295 | +instance Binary (Static a) where |
| 296 | + put (Static label) = putStaticLabel label |
| 297 | + get = Static <$> getStaticLabel |
333 | 298 |
|
334 | 299 | -- We don't want StaticLabel to be its own Binary instance
|
335 | 300 | putStaticLabel :: StaticLabel -> Put
|
@@ -446,8 +411,8 @@ unstatic rtable (Static label) = do
|
446 | 411 | data Closure a = Closure !(Static (ByteString -> a)) !ByteString
|
447 | 412 | deriving (Eq, Ord, Typeable, Show)
|
448 | 413 |
|
449 |
| -instance Typeable a => Binary (Closure a) where |
450 |
| - put (Closure dec env) = put dec >> put env |
| 414 | +instance Binary (Closure a) where |
| 415 | + put (Closure st env) = put st >> put env |
451 | 416 | get = Closure <$> get <*> get
|
452 | 417 |
|
453 | 418 | #if MIN_VERSION_bytestring(0,10,0)
|
|
0 commit comments