I uploadad <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/RefSerialize">RefSerialize</a>&nbsp; to&nbsp; Hackage .<br><br>Read, Show and Data.Binary do not check for repeated references to the same data address. As a result, the data is serialized multiple times when serialized. This is a waste of space in the filesystem&nbsp; and&nbsp; also a waste of serialization time. but the worst consequence is that, when the serialized data is read, it allocates multiple copies in memory for the same object referenced multiple times. Because multiple referenced data is very typical in a pure language such is Haskell, this means that the resulting data loose the beatiful economy of space and processing time that referential transparency permits.<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>This package allows the serialization and deserialization of large data structures without duplication of data, with<br>the result of optimized performance and memory usage. It is also useful for debugging purposes.<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>There are automatic derived instances for instances of Read/Show, lists and strings. the deserializer contains a almos complete set of Parsec.Token parsers for deserialization. <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;Every instance of Show/Read is also a instance of Data.RefSerialize<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;The serialized string has the form &quot;expr( var1, ...varn) where&nbsp; var1=value1,..valn=valueN &quot; so that the<br>string can ve EVALuated.<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;See demo.hs and tutorial. I presumably will add a entry in <a href="http://haskell-web.blogspot.com">haskell-web.blogspot.com</a><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; To develop: -derived instances for Data.Binary<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -serialization to/from ByteStings<br><br>I wrote this module because I needed to serialize lists of verisions of the same data with slight modifications between each version.<br><br><br>This is a short tutorial (in tutorial.txt)<br>
<br><br><br>runW applies showp, the serialization parser of the instance Int for the RefSerialize class<br><br>Data.RefSerialize&gt;let x= 5 :: Int<br>Data.RefSerialize&gt;runW $ showp x<br>&quot;5&quot;<br><br>every instance of Read and Show is an instance of RefSerialize. <br>
<br>rshowp is derived from showp, it labels the serialized data with a variable name<br><br>Data.RefSerialize&gt;runW $ rshowp x<br>&quot; v8 where {v8= 5; }&quot;<br><br>Data.RefSerialize&gt;runW $ rshowp [2::Int,3::Int]<br>
&quot; v6 where {v6= [ v9,&nbsp; v10]; v9= 2; v10= 3; }&quot;<br><br>while showp does a normal show serialization<br><br>Data.RefSerialize&gt;runW $ showp [x,x]<br>&quot;[5, 5]&quot;<br><br>rshowp variables are serialized memory references: no piece of data that point to the same addrees is serialized but one time<br>
<br>Data.RefSerialize&gt;runW $ rshowp [x,x]<br>&quot; v9 where {v6= 5; v9= [ v6, v6]; }&quot;<br><br>This happens recursively<br><br>Data.RefSerialize&gt;let xs= [x,x] in str = runW $ rshowp [xs,xs]<br>Data.RefSerialize&gt;str<br>
&quot; v8 where {v8= [ v10, v10]; v9= 5; v10= [ v9, v9]; }&quot;<br><br>the rshowp serialized data is read with rreadp. The showp serialized data is read by readp<br><br>Data.RefSerialize&gt;let xss= runR rreadp str :: [[Int]]<br>
Data.RefSerialize&gt;print xss<br>[[5,5],[5,5]]<br><br>this is the deserialized data<br><br>the deserialized data keep the references!! pointers are restored! That is the whole point!<br><br>Data.RefSerialize&gt;varName xss !! 0 == varName xss !! 1<br>
True<br><br><br>rShow= runW rshowp<br>rRead= runR rreadp<br><br>Data.RefSerialize&gt;rShow x<br>&quot; v11 where {v11= 5; }&quot;<br><br><br>In the definition of a referencing parser non referencing parsers can be used and viceversa. Use a referencing parser<br>
when the piece of data is being referenced many times inside the serialized data.<br><br>by default the referencing parser is constructed by:<br><br>rshowp= insertVar showp<br>&nbsp;&nbsp; rreadp= readVar readp<br>but this can be redefined. See for example the instance of [] in RefSerialize.hs<br>
<br>This is an example of a showp parser for a simple data structure.<br><br>data S= S Int Int deriving ( Show, Eq)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br><br>instance&nbsp; Serialize S&nbsp; where<br>&nbsp;&nbsp;&nbsp; showp (S x y)= do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xs &lt;- rshowp x&nbsp; -- rshowp parsers can be inside showp parser<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ys &lt;- rshowp y<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return $ &quot;S &quot;++xs++&quot; &quot;++ys<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br><br>&nbsp;&nbsp;&nbsp; readp =&nbsp; do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; symbol &quot;S&quot;&nbsp;&nbsp;&nbsp;&nbsp; -- I included a (almost) complete Parsec for deserialization<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x &lt;- rreadp&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; y &lt;- rreadp<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return $ S x y<br><br>there is a mix between referencing and no referencing parser here:&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp; <br>Data.RefSerialize&gt;putStrLn $ runW $ showp $ S x x<br>
S&nbsp; v23 v23 where {v23= 5; }&nbsp;&nbsp;&nbsp;&nbsp; <br><br><br>(I corrected some errors in this file here)<br><br><br>