[MLton] Serialization?

Stephen Weeks MLton@mlton.org
Tue, 5 Jul 2005 09:38:42 -0700


> I mean something like: val serialize: 'a -> string
> and                    val deserialize: string -> 'a
...
> I could require the user to specify how to serialize their type, but
> it seems to me that MLton already has all the information required.

Indeed.  Years ago MLton had functions

  val deserialize: Word8Vector.vector -> 'a
  val serialize: 'a -> Word8Vector.vector

The vestiges of these are still in the sources
(basis-library/mlton/mlton.sig, runtime/gc.h, and probably C code in
some old revision of gc.c).  It was implemented in
C code by doing pretty much what the copying GC does, but only with a
single root.  It was all pretty simple code.  I dropped it because the
semantics was unclear.  I ran into problems because MLton's optimizer
would, for example, lift a ref cell out of an object (because it
proved the ref cell was global).  Then, there was no connection
between the object and the ref cell at run time (other than the
code).  Needless to say, the serializing the object would or wouldn't
pick up the ref cell depending on whether the optimization kicked in,
which is not an acceptable state to be in.

There may be other optimizations that interfere as well.  Not that I
think serialization is impossible to specify or get right.  Just that
some work needs to be done to think about the semantics, tweak the
optimizer to respect the semantics, and make sure the runtime knows
enough.