<span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; ">nstance Read (F a) where read str= eval str &gt;&gt;= F str</span><div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;"><br>

</span></font></div><div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;">sorry<br></span></font><br><div class="gmail_quote">2010/11/11 Alberto G. Corona <span dir="ltr">&lt;<a href="mailto:agocorona@gmail.com">agocorona@gmail.com</a>&gt;</span><br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">There are some straighforward tricks using the package eval (or hint)l.<div><br></div><div>This is more or less the idea in pseudocode:<br>

<div><br></div><div>type FuncExpr= String</div><div><br></div><div><div>data F a = F FuncExpr a</div>
<div><br></div><div>apply (F _ f) x= f x</div><div><br></div><div>instance Show (F a) where show (F str _)= str</div><div><br></div><div>instance Read (F a) where read (F str f)= eval f &gt;&gt;= F str</div><div><br></div>


<div><br></div><br><div class="gmail_quote">2010/11/11 Jesse Schalken <span dir="ltr">&lt;<a href="mailto:jesseschalken@gmail.com" target="_blank">jesseschalken@gmail.com</a>&gt;</span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div><div></div><div class="h5">
<div><div>Is it possible to serialize and deserialize a function to/from binary form, perhaps using Data.Binary, for example? What about an IO action? If so, is there a way the serialized representation could be architecture-independent?</div>



</div><div><br></div><div>I have been shown how useful it can be to store functions inside data structures, and while looking at data serialization for the purpose of persistence I wondered &quot;since functions are just values in Haskell, why can&#39;t we persist them, too?&quot;.</div>



<div><br></div><div>To me the idea has interesting implications. For example, an arbitrary program could simply be represented by a serialization of `IO ()`. In fact, you could load any program into memory from a file and use Control.Concurrent.forkIO to run it, and later kill it, giving you the beginnings of an operating environment. If such a serialization is architecture independent then to my understanding you have the beginnings of a virtual machine. You could break your program into pieces and store them in a database and load them when needed, or even pull updates to each piece individually from down the web etc, enabling interesting methods of software distribution. It would make very cool stuff possible.</div>



<div><br></div><div>I have had a look at hs-plugins, but it is unclear how to derive a simple pair of functions `(a -&gt; b) -&gt; ByteString` and `ByteString -&gt; Either ParseError (a -&gt; b)`, for example, from the functionality it provides, if it is possible at all. I guess such a thing requires thorough digging into the depths of GHC, (or maybe even LLVM if an architecture independent representation is sought, but I don&#39;t know enough to say.). Perhaps this is more a question for those interested and knowledgable in Haskell compilation (and, to some extent, decompilation).</div>



<div><br></div><div>If not Haskell, are there any languages which provide a simple serialization and deserialization of functions?</div>
<br></div></div><div class="im">_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></div></blockquote></div><br></div></div>
</blockquote></div><br></div>