The Read typeclass doesn't declare read
directly; instead, it defines readsPrec
, which supports precedence (this is important when read
ing a value of a complex data type involving elements of other types). The definition you get when you use deriving (Read)
looks roughly like
instance (Read a) => Read (Tree a) where
readsPrec d r = readParen (d > app_prec)
(\r -> [(Leaf m,t) |
("Leaf",s) <- lex r,
(m,t) <- readsPrec (app_prec+1) s]) r
++ readParen (d > up_prec)
(\r -> [(u:^:v,w) |
(u,s) <- readsPrec (up_prec+1) r,
(":^:",t) <- lex s,
(v,w) <- readsPrec (up_prec+1) t]) r
where app_prec = 10
up_prec = 5
(this obviously for a Tree
data type, but similar rules apply for other user-defined ADTs). (Also, the above is a slight lie: GHC actually uses a different implementation, but the above is the kind of thing you should do unless you're willing to dig around inside of GHC.)
read
is defined in terms of readsPrec
and readList
(the other method in Read
, which is defaulted for every type except Char
where it's used to read [Char]
as a string instead of as a list of Char
).
If the standard derivation isn't sufficient, for a type like yours that is simply a bucket of Int
s you can ignore the precedence parameter.
BTW, Read
and Show
are rather slow; you may want to consider other ways to do I/O with your data.