Sorry for the misinformation. I should'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"><<a href="mailto:byorgey@seas.upenn.edu">byorgey@seas.upenn.edu</a>></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>
> Perhaps this is what you're looking for:<br>
> {-# LANGUAGE ExistentialQuantification #-}<br>
> import Data.Binary<br>
> import Data.ByteString.Lazy as B ( readFile, writeFile )<br>
> import Codec.Compression.GZip ( compress, decompress )<br>
><br>
> data Thing = forall a. (Binary a, Show a, Eq a) => Thing a<br>
><br>
> instance Binary Thing where<br>
> get = get<br>
> put (Thing a) = put a<br>
><br>
> instance Show Thing where<br>
> show (Thing a) = show a<br>
><br>
> readThing :: FilePath -> IO Thing<br>
> readThing f = return . decode . decompress =<< B.readFile<br>
> f<br>
><br>
> writeThing :: FilePath -> Thing -> IO ()<br>
> writeThing f = B.writeFile f . compress . encode<br>
><br>
> doSomething :: Thing -> m Thing<br>
> doSomething = undefined<br>
><br>
> main = do<br>
> a <- readThing "file1.txt"<br>
> a' <- doSomething a<br>
> writeThing "file2.txt" a'<br>
><br>
> It compiles on my machine (GHC 7.2.1) but I haven't tested it. It<br>
> 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 'doSomething' to<br>
do anything interesting at all. I am actually surprised that<br>
'readThing' 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 'cast' to<br>
recover the type, but that doesn'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>