Unfortunately — for historical reasons — you need to implement readsPrec:
λ> :t readsPrec
readsPrec :: Read a => Int -> ReadS a
λ> :i ReadS
type ReadS :: * -> *
type ReadS a = String -> [(a, String)]
-- Defined in ‘Text.ParserCombinators.ReadP’
So, most likely:
instance Read Fruit where
readsPrec _ c =
let (s, r) = span (/= ' ') c
in case c of
"XYZ" -> Foo
-- ⁝ got to buy groceries now
I wonder if that last catch-all case could come back to bite you in a creative read/show sequence in a nested data type.
The -- ⁝ got to buy groceries now was actually me going to the shop to buy groceries.
instance Read Fruit where
readsPrec _ c =
let (s, r) = span (/= ' ') c
in case c of
"APPLE" -> [(APPLE, r)]
"ORANGE" -> [(ORANGE, r)]
_ -> [(OTHER, r)]
This should work: we are parsing a word and returning a constructor plus the rest of the stream. Again I wonder if that _ -> is a good idea, as read should complementshow.