[Haskell-cafe] Serialization of (a -> b) and IO a

C. McCann cam at uptoisomorphism.net
Thu Nov 11 11:34:34 EST 2010

On Thu, Nov 11, 2010 at 10:53 AM, Ketil Malde <ketil at malde.org> wrote:
> ..and you are able to tell the difference.  Am I wrong in thinking that
> this could be made to work if serialization was to/from an opaque type
> instead of (Byte)String, so that the *only* operations would be
> serialization and deserialization (and possibly storing to/from file)?

This was my first thought as well! However, reading to/from a file
would of course be in IO, at which point you'd be free to read the
file back in through normal means to get at the representation. So in
that respect, this is equivalent to (a -> b) -> IO String.

Outside of IO, it would pretty much have to be limited to serializing
and deserializing. You'd be able to create opaque tokens representing
functions, pass them around, and/or extract the function in order to
apply it. Conveniently, it turns out that Haskell already has support
for this, you can implement it as follows:

> module Serialize.Pure (OpaqueFunction, serialize, deserialize) where
> newtype OpaqueFunction a b = Opaque { deserialize :: a -> b }
> serialize = Opaque

Toss in some existential types as desired, if you want to hide the
function's actual type.

I suppose one could object that this isn't actually serializing
anything at all; to which I would respond that, in pure code, how do
you expect to tell the difference?

- C.

More information about the Haskell-Cafe mailing list