Skip to content

Instantly share code, notes, and snippets.

@t0yv0
Created May 31, 2011 13:20
Show Gist options
  • Select an option

  • Save t0yv0/1000485 to your computer and use it in GitHub Desktop.

Select an option

Save t0yv0/1000485 to your computer and use it in GitHub Desktop.
An attempt to figure out an easy way to serialize function types.
{-# OPTIONS -XExistentialQuantification -XMultiParamTypeClasses #-}
-- Serialization requires being able to marshall and unmarshall.
class Serializable a where
serialize :: a -> String
deserialize :: String -> (a, String)
-- Static assertion that `a` encodes `b -> c`.
class Encoding a b c where
decode :: a -> b -> c
-- Represents a serializable function by an encoding `t`. To make a
-- datatype serializable, replace all `a -> b` by `F a b`.
data F a b =
forall t. (Serializable t, Encoding t a b) =>
F { unF :: t }
-- Proof that all function encodings are serializable.
instance Serializable (F a b) where
-- Serialization is delegated to the encoding.
serialize (F x) = serialize x
-- Deserialization *FAILS* because we do not know the type `t` of
-- the encoding. Is it possible to *READ* the type `t` from the
-- data stream too? And coerce it to `Serializable t`?
deserialize s = undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment