Can Haskell functions be serialized?

后端 未结 5 1246
说谎
说谎 2020-12-15 17:03

The best way to do it would be to get the representation of the function (if it can be recovered somehow). Binary serialization is preferred for efficiency reasons.

5条回答
  •  温柔的废话
    2020-12-15 17:41

    A pretty simple and practical, but maybe not as elegant solution would be to (preferably have GHC automatically) compile each function into a separate module of machine-independent bytecode, serialize that bytecode whenever serialization of that function is required, and use the dynamic-loader or plugins packages, to dynamically load them, so even previously unknown functions can be used.

    Since a module notes all its dependencies, those could then be (de)serialized and loaded too. In practice, serializing index numbers and attaching an indexed list of the bytecode blobs would probably be the most efficient.

    I think as long as you compile the modules yourself, this is already possible right now.

    As I said, it would not be very pretty though. Not to mention the generally huge security risk of de-serializing code from insecure sources to run in an unsecured environment. :-)
    (No problem if it is trustworthy, of course.)

    I’m not going to code it up right here, right now though. ;-)

提交回复
热议问题