[Haskell-cafe] Proper round-trip HughesPJ/Parsec for Doubles?

Andy Gimblett haskell at gimbo.org.uk
Tue Feb 23 08:44:50 EST 2010


Hi all,

Short version: How can I pretty print and parse values of type Double  
such that those operations are each other's inverse?

Long version: I'm writing and QuickCheck-testing a parser using the  
approach set out here:

http://lstephen.wordpress.com/2007/07/29/parsec-parser-testing-with-quickcheck/

That is, each syntactic category gets a pretty-printer and a parser  
and an Arbitrary instance, and QuickCheck checks that (parse .  
prettyPrint) == id, basically.  Somewhat unsurprisingly, this  
sometimes fails for floating point values (I'm using Doubles).

Now, I know that floats are in some sense imprecise, and comparing for  
equality is fraught with peril, but it seems that if x==x then it  
ought to be at least _possible_ to arrange matters such that (parse .  
prettyPrint x) == x as well.  At worst, pretty-printing the underlying  
binary representation!?  So my feeling is that my parser could be  
improved.

At the moment I'm working around it by defining a type class which  
checks for equality within some margin of error, and using that  
instead of Eq - but it's messier than I'd like, so I wondered if there  
was something obvious I'm missing.

As hpaste.org seems to be down, I'll attach a code example here instead.

Thanks!

-Andy

--
Andy Gimblett
http://gimbo.org.uk/

-------------- next part --------------
A non-text attachment was scrubbed...
Name: TestParse.hs
Type: application/octet-stream
Size: 1527 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20100223/76987856/TestParse.obj
-------------- next part --------------




More information about the Haskell-Cafe mailing list