Tracing Prelude.read exceptions

Hi, I got quite used to a sequence providing simple data persistence : 1) Store my data to a file: writeFile fileName (show someData) 2) Some time later read this data back: line <- readFile fileName let someData = read line :: SomeDataType Having this done hundreds of times I now got stuck with step 2) trying to read moderately complex structure back. I get read exception in run-time: fromList *** Exception: Prelude.read: no parse I have checked and rechecked my types, data files, etc. - and still no idea. So my question: Is there any way to trace Prelude.read exceptions to see exactly on what data element read fails in run-time? Thanks!

On Sun, Dec 11, 2011 at 4:19 PM, dokondr
Hi, I got quite used to a sequence providing simple data persistence : 1) Store my data to a file: writeFile fileName (show someData)
2) Some time later read this data back: line <- readFile fileName let someData = read line :: SomeDataType
I can't help you with your question, but I suggest using safecopy [1] even for simple cases. It's *much* faster and also safer. Also, it's easy to use, just use L.writeFile fileName $ S.runPutLazy $ safePut someData and ret <- S.runGetLazy safeGet <$> L.readFile fileName case ret of Left err -> print err Right someData -> ... where import Control.Applicative ((<$>)) import qualified Data.ByteString.Lazy as L -- [2] import qualified Data.Serialize.Get as S -- [3] import qualified Data.Serialize.Put as S -- [4] Cheers, [1] http://hackage.haskell.org/package/safecopy [2] http://hackage.haskell.org/packages/archive/bytestring/0.9.2.0/doc/html/Data... [3] http://hackage.haskell.org/packages/archive/cereal/0.3.4.0/doc/html/Data-Ser... [4] http://hackage.haskell.org/packages/archive/cereal/0.3.4.0/doc/html/Data-Ser... -- Felipe.

I suggest switching from 'read' to a real parser that can give you proper error messages. I use Text.Parse from the polyparse package, which is designed to parse back exactly the format produced by derived Show instances. To derive the Parse class from your datatypes, the tool DRiFT is handy.
'runParser parse' will give you Either String a, where the string contains any error message.
Regards, Malcolm
On 11/12/2011, at 18:19, dokondr
Hi, I got quite used to a sequence providing simple data persistence : 1) Store my data to a file: writeFile fileName (show someData)
2) Some time later read this data back: line <- readFile fileName let someData = read line :: SomeDataType
Having this done hundreds of times I now got stuck with step 2) trying to read moderately complex structure back. I get read exception in run-time: fromList *** Exception: Prelude.read: no parse
I have checked and rechecked my types, data files, etc. - and still no idea.
So my question: Is there any way to trace Prelude.read exceptions to see exactly on what data element read fails in run-time?
Thanks!
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (3)
-
dokondr
-
Felipe Almeida Lessa
-
Malcolm Wallace