instance Read a, given a Parsec parser

Submitted by dtlin on Fri, 10/07/2005 - 1:46pm.

Given any type a, for which there exists an parsecRead :: Parser a, the instance Read a can be defined really easily.
But I didn't find it in the Parsec docs.  Maybe I wasn't looking hard enough.

{- This code is dedicated to the public domain. -}
import Text.ParserCombinators.Parsec
class ParsecRead a where
  parsecRead :: Parser a
instance (ParsecRead a) => Read a where
  readsPrec _ = either (const []) id . parse parsecRead' "" where
    parsecRead' = do a <- parsecRead
                     rest <- getInput
                     return [(a, rest)]

It could be used like this:
data Foo = Foo
instance ParsecRead Foo where
  parsecRead = string "foo" >> return Foo
-- instance Read Foo

Defining instance Read a requires GHC's -fallow-undecidable-instances, though.
Well, you could always just repeat the readsPrec definition for everything you want, but it seems to me that this should exist just because it's convenient and common enough.

Submitted by jgoerzen on Tue, 10/11/2005 - 11:45am.

I like it.

Would you be interested in stating a copyright/license for it so I can include it in MissingH?

Submitted by dtlin on Thu, 10/13/2005 - 8:34am.

I'd be glad to.  Can I just add a comment saying "This code is dedicated to the public domain", or do I need to do anything fancier?

Submitted by jgoerzen on Wed, 10/19/2005 - 9:48am.

That will probably be OK, though usually something like the BSD license accomplishes much the same but makes everyone feel a little more comfortable.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.