Hi,
At work we had a discussion about whether it was possible to
serialize existential types like
data Foo = forall a. Serializable a => Foo
This may be relevant for Cloud Haskell. On one hand values need to
have a type which is Serializable in order to be sent, and on the
other hand an alternative implementation of the Closure type would be
possible.
In [1] and also pasted below, I'm submitting a solution. It uses a
table to look up "get" methods of Binary instances by Typeable
fingerprint.
If the remote tables of Cloud Haskell included the appropriate
"get" methods, perhaps it would be possible to define Closure as
data Closure b = forall a. Closure (Static (a -> b)) a
Best,
Facundo
[1]
http://hpaste.org/85654
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Control.Distributed.Process.Serializable
( fingerprint, Serializable, encodeFingerprint, decodeFingerprint )
import Data.Binary
import Data.Map ( Map, fromList, lookup )
import Data.Typeable ( Typeable(typeOf), cast )
import Data.Typeable.Internal ( TypeRep(TypeRep) )
import GHC.Fingerprint ( Fingerprint(Fingerprint) )
data Foo = forall a. Serializable a => Foo a
stable :: Map Fingerprint SomeGet
stable = fromList
[ (mkSMapEntry (undefined :: Bool))
, (mkSMapEntry (undefined :: [Int]))
]
data SomeGet = forall a. Serializable a => SomeGet (Get a)
mkSMapEntry :: forall a. Serializable a => a -> (Fingerprint,SomeGet)
mkSMapEntry a = (fingerprint a,SomeGet (get :: Get a))
instance Binary Foo where
put (Foo a) = put (encodeFingerprint$ fingerprint a) >> put a
get = do
fp<-get
case Data.Map.lookup (decodeFingerprint fp) stable of
Just (SomeGet someget) -> fmap Foo someget
Nothing -> error "Binary Foo: fingerprint unknown"
main = case decode$ encode$ Foo xs of
Foo xs'-> case cast xs' of
Just xs'' -> print$ xs'' == xs
where
xs = [ 2 :: Int ]