
Does Malcolm's Binary library exist for ghc? If not, is there a standard way (ie using Haskell that works on any compiler) to dump a data structure into a file that another program or part of the same program can then pick up and read (cf "serialize" in Java)? I'm reluctant to go with a solution that's compiler-specific.
Using Show & Read is the only really portable way to do this, unfortunately. [snip]
Well, it's not really portable... Try the program at the end of this file, by first executing hugsMain in hugs, exiting and then running main in ghc or ghci, and you'll see: | Cons{hd='a',tl=(Cons{hd='b',tl=Nil})} | *** Exception: Prelude.read: no parse So, at least ghc and hugs disagree... My ghc version is 5.02 and my hugs is February 2000/2001 (both give the same output). Cheers, Jan de Wit ----8<---- module Test where data List a = Nil | Cons { hd :: a, tl :: List a } deriving (Show,Read,Eq) theList = Cons 'a' (Cons 'b' Nil) hugsMain = do writeFile "test.txt" (show theList) main main = do s <- readFile "test.txt" let theListFromFile = read s print theList print theListFromFile print $ theList == theListFromFile writeFile "test.txt" (show theList)

Using Show & Read is the only really portable way to do this, unfortunately.
Well, it's not really portable... Try the program at the end of this file, by first executing hugsMain in hugs, exiting and then running main in ghc or ghci, and you'll see:
| Cons{hd='a',tl=(Cons{hd='b',tl=Nil})} | *** Exception: Prelude.read: no parse
So, at least ghc and hugs disagree...
It appears that nhc98 and DrIFT get it wrong as well, whilst hbc gets it right, like Hugs (although hbc diverges from the Report by adding extra whitespace). Can anyone spot the fault in the derived instance of Read produced by DrIFT? Regards, Malcolm
data List a = Nil | Cons { hd :: a, tl :: List a }
{-* Generated by DrIFT-v1.0 : Look, but Don't Touch. *-} instance (Show a) => Show (List a) where showsPrec d (Nil) = showString "Nil" showsPrec d (Cons aa ab) = showParen (d >= 10) (showString "Cons" . showChar '{' . showString "hd" . showChar '=' . showsPrec 10 aa . showChar ',' . showString "tl" . showChar '=' . showsPrec 10 ab . showChar '}')
instance (Read a) => Read (List a) where readsPrec d input = (\ inp -> [((Nil) , rest) | ("Nil" , rest) <- lex inp]) input ++ readParen (d > 9) (\ inp -> [((Cons aa ab) , rest) | ("Cons" , inp) <- lex inp , ("{" , inp) <- lex inp , ("hd" , inp) <- lex inp , ("=" , inp) <- lex inp , (aa , inp) <- readsPrec 10 inp , ("," , inp) <- lex inp , ("tl" , inp) <- lex inp , ("=" , inp) <- lex inp , (ab , inp) <- readsPrec 10 inp , ("}" , rest) <- lex inp]) input

Can anyone spot the fault in the derived instance of Read produced by DrIFT?
Regards, Malcolm
data List a = Nil | Cons { hd :: a, tl :: List a }
{-* Generated by DrIFT-v1.0 : Look, but Don't Touch. *-} instance (Show a) => Show (List a) where showsPrec d (Nil) = showString "Nil" showsPrec d (Cons aa ab) = showParen (d >= 10) (showString "Cons" . showChar '{' . showString "hd" . showChar '=' . showsPrec 10 aa . showChar ',' . showString "tl" . showChar '=' . showsPrec 10 ab . showChar '}')
instance (Read a) => Read (List a) where readsPrec d input = (\ inp -> [((Nil) , rest) | ("Nil" , rest) <- lex inp]) input ++ readParen (d > 9) (\ inp -> [((Cons aa ab) , rest) | ("Cons" , inp) <- lex inp , ("{" , inp) <- lex inp , ("hd" , inp) <- lex inp , ("=" , inp) <- lex inp , (aa , inp) <- readsPrec 10 inp , ("," , inp) <- lex inp , ("tl" , inp) <- lex inp , ("=" , inp) <- lex inp , (ab , inp) <- readsPrec 10 inp , ("}" , rest) <- lex inp]) input
Since the Report states that the derived Show instances only insert parentheses where needed and that the derived Read instances can read the output produced by show, I would suggest to change all occurences of {showsPrec 10,readsPrec 10} to {showsPrec 0,readsPrec 0} in the above code. Best, Andres -- Andres Loeh, Universiteit Utrecht mailto:andres@cs.uu.nl mailto:mail@andres-loeh.de http://www.andres-loeh.de

Since the Report states that the derived Show instances only insert parentheses where needed and that the derived Read instances can read the output produced by show, I would suggest to change all occurences of {showsPrec 10,readsPrec 10} to {showsPrec 0,readsPrec 0} in the above code.
That seems like a reasonable suggestion, but it doesn't fix the bug. Regards, Malcolm

Since the Report states that the derived Show instances only insert parentheses where needed and that the derived Read instances can read the output produced by show, I would suggest to change all occurences of {showsPrec 10,readsPrec 10} to {showsPrec 0,readsPrec 0} in the above code.
That seems like a reasonable suggestion, but it doesn't fix the bug.
Could you please explain why not? It seems to work fine in my tests here, at least with the given example. Best, Andres -- Andres Loeh, Universiteit Utrecht mailto:andres@cs.uu.nl mailto:mail@andres-loeh.de http://www.andres-loeh.de

| > > Since the Report states that the derived Show instances only insert | > > parentheses where needed and that the derived Read instances can | > > read the output produced by show, I would suggest to change all | > > occurences of {showsPrec 10,readsPrec 10} to {showsPrec 0,readsPrec 0} | > > in the above code. | > | > That seems like a reasonable suggestion, but it doesn't fix the bug. | | Could you please explain why not? It seems to work fine in my tests here, | at least with the given example. Apologies. For some reason, maybe a file version mismatch, the example continued to fail when I first tried your solution. That puzzled me, because indeed your solution is correct, and the example does now work when I run it again. Regards, Malcolm
participants (3)
-
Andres Loeh
-
Jan de Wit
-
Malcolm Wallace