Sorry for the misinformation. I should&#39;ve ran the code.<br>-deech<br><br><div class="gmail_quote">On Fri, Nov 4, 2011 at 11:22 AM, Brent Yorgey <span dir="ltr">&lt;<a href="mailto:byorgey@seas.upenn.edu">byorgey@seas.upenn.edu</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="HOEnZb"><div class="h5">On Fri, Nov 04, 2011 at 08:52:36AM -0500, aditya siram wrote:<br>
&gt; Perhaps this is what you&#39;re looking for:<br>
&gt; {-# LANGUAGE ExistentialQuantification #-}<br>
&gt; import Data.Binary<br>
&gt; import Data.ByteString.Lazy as B ( readFile, writeFile )<br>
&gt; import Codec.Compression.GZip ( compress, decompress )<br>
&gt;<br>
&gt; data Thing = forall a. (Binary a, Show a, Eq a) =&gt; Thing a<br>
&gt;<br>
&gt; instance Binary Thing where<br>
&gt;     get = get<br>
&gt;     put (Thing a) = put a<br>
&gt;<br>
&gt; instance Show Thing where<br>
&gt;     show (Thing a) = show a<br>
&gt;<br>
&gt; readThing :: FilePath -&gt; IO Thing<br>
&gt; readThing f = return . decode . decompress =&lt;&lt; B.readFile<br>
&gt; f<br>
&gt;<br>
&gt; writeThing :: FilePath -&gt; Thing -&gt; IO ()<br>
&gt; writeThing f = B.writeFile f . compress . encode<br>
&gt;<br>
&gt; doSomething :: Thing -&gt; m Thing<br>
&gt; doSomething = undefined<br>
&gt;<br>
&gt; main = do<br>
&gt;  a &lt;- readThing &quot;file1.txt&quot;<br>
&gt;  a&#39; &lt;- doSomething a<br>
&gt;  writeThing &quot;file2.txt&quot; a&#39;<br>
&gt;<br>
&gt; It compiles on my machine (GHC 7.2.1) but I haven&#39;t tested it. It<br>
&gt; uses the<br>
<br>
</div></div>This will not work.  The problem is that once you have a Thing you<br>
cannot do anything with it, because you have no information about what<br>
type is inside.  In other words you cannot implement &#39;doSomething&#39; to<br>
do anything interesting at all.  I am actually surprised that<br>
&#39;readThing&#39; type checks -- I am not sure what type it thinks the read<br>
thing has, or how it can guarantee that it satisfies the given<br>
constraints.<br>
<br>
I tried adding a Typeable constraint to Thing and using &#39;cast&#39; to<br>
recover the type, but that doesn&#39;t really work either.  You would<br>
really have to do something like changing the Binary instance for<br>
Thing so that it also serializes/deserializes a TypeRep along with the<br>
value, and then does some sort of unsafe cast after reading.<br>
<br>
You may want to take a look at how xmonad handles this problem -- it<br>
allows arbitrary user-extensible state and layouts, which it needs to<br>
serialize and deserialize when restarting itself.<br>
<span class="HOEnZb"><font color="#888888"><br>
-Brent<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
</div></div></blockquote></div><br>