Read Instances for Data.Map and Data.Set
Hi folks, I was really annoyed by the fact that for Data.Map and Data.Set are no Read instances declared, but Show instances are! I believe there should be some kind of unwritten rule that in the standart lib the Show and Read instances come pairwise and are fully compatible. Anyway since there was no response to S. Alexander Jacobson post [1], I decided to write these instances at least for GHC in the style of the other instances in GHC.Read. For the {a,b,...} syntax I have used a modified version of @list@ from GHC.Read Is there a chance of having this in the libs? Cheers, Georg [1] http://www.haskell.org//pipermail/haskell/2005-February/015380.html import qualified Data.Map as Map import qualified Data.Set as Set import Text.Read import qualified Text.Read.Lex as L import GHC.Read instance (Ord k, Read k, Read e) => Read (Map.Map k e) where readPrec = do elements <- set (readPair readPrec readPrec) return $ Map.fromList elements -- ^ @(readPrec p1 p2)@ parses a pair of things with the syntax @a:=b@ -- where @a@ is parsed by @p1@, -- and @b@ is parsed by @p2@ readPair :: ReadPrec a -> ReadPrec b -> ReadPrec (a,b) readPair reada readb = do a <- reset reada L.Symbol ":=" <- lexP b <- reset readb return (a,b) instance (Ord a, Read a) => Read (Set.Set a) where readPrec = do elements <- set (readPrec) return $ Set.fromList elements set :: ReadPrec a -> ReadPrec [a] -- ^ @(set p)@ parses a list of things parsed by @p@, -- using the curly-braces syntax. set readx = parens ( do L.Punc "{" <- lexP (setRest False +++ setNext) ) where setRest started = do L.Punc c <- lexP case c of "}" -> return [] "," | started -> setNext _ -> pfail setNext = do x <- reset readx xs <- setRest True return (x:xs) -- ---- Georg Martius, Tel: (+49 34297) 89434 ---- ------- http://www.flexman.homeip.net ---------
On Wed, Oct 19, 2005 at 08:20:10PM +0200, Georg Martius wrote:
Hi folks,
I was really annoyed by the fact that for Data.Map and Data.Set are no Read instances declared, but Show instances are! I believe there should be some kind of unwritten rule that in the standart lib the Show and Read instances come pairwise and are fully compatible.
I've been annoyed by this too. I wrote my own instances at one point. Frederik
Georg Martius wrote:
Anyway since there was no response to S. Alexander Jacobson post [1], I decided to write these instances at least for GHC in the style of the other instances in GHC.Read.
Who feels responsible for including something into Data.Set and Data.Map (recently I've proposed a change for Set.intersection and others also made suggestions)? The Read instances should be in "their" modules (to avoid "orphans"). But Set and Map should also remain "portable"!
import GHC.Read
This module isn't even listed under http://www.haskell.org/ghc/docs/latest/html/libraries/index.html Cheers Christian Simon, clicking on any module does not work, currently Not Found The requested URL /ghc/docs/latest/html/libraries/base/Control.Arrow.html was not found on this server. Apache/2.0.46 (Red Hat) Server at www.haskell.org Port 80
Christian Maeder wrote:
Simon, clicking on any module does not work, currently
Not Found
The requested URL /ghc/docs/latest/html/libraries/base/Control.Arrow.html was not found on this server. Apache/2.0.46 (Red Hat) Server at www.haskell.org Port 80
Sorry, it works now after I cleared my cache.
Georg Martius <mai99dgf@studserv.uni-leipzig.de> writes:
I was really annoyed by the fact that for Data.Map and Data.Set are no Read instances declared, but Show instances are! I believe there should be some kind of unwritten rule that in the standart lib the Show and Read instances come pairwise and are fully compatible.
FWIW, earlier versions of Haskell had a single class Text with methods for both showing and reading, so it was harder to ignore their complementary roles. Regards, Malcolm
On 10/19/05, Georg Martius <mai99dgf@studserv.uni-leipzig.de> wrote:
I was really annoyed by the fact that for Data.Map and Data.Set are no Read instances declared, but Show instances are! I believe there should be some kind of unwritten rule that in the standart lib the Show and Read instances come pairwise and are fully compatible.
If there was this unwritten rule, should there not be other rules designed to ensure that reads and shows with interacting types are compatible? For example, I could imagine a type that would read and show on it's own, but a map of these objects wouldn't read and show correctly. I'm not suggesting this, but a possible solution would be to use parenthesis around every type and disallow a type to show to unmatched internal parenthesis. Regards, David
participants (5)
-
Christian Maeder -
David Sankel -
Frederik Eaton -
Georg Martius -
Malcolm Wallace