Serialization of existential types

49 views
Skip to first unread message

Facundo Domínguez

unread,
Apr 12, 2013, 3:16:52 PM4/12/13
to parallel...@googlegroups.com
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 ]

Tim Watson

unread,
Apr 15, 2013, 6:45:13 PM4/15/13
to parallel...@googlegroups.com
Hi Facundo,

This looks rather interesting, and might possibly find a home along with the corollary Closure handling API proposed at https://groups.google.com/forum/?fromgroups=#!topic/cloud-haskell-developers/Xb39WZSCdTI - would you care to submit a pull request or add a ticket to https://cloud-haskell.atlassian.net/ so that we can track and discuss the idea?

Cheers,
Tim
Reply all
Reply to author
Forward
0 new messages