Pattern synonyms deriving read?

160 Views Asked by At

Pattern synonyms provide a shorthand way to express a value; also they can provide an abstract name to avoid a client module breaking into the data decl. Here's a not very useful one, as an example for discussion:

data MyNum = MkNum Int

pattern Zero :: MyNum
pattern Zero = MkNum 0

What I can do to help data capture is provide a Read instance for MyNum. How do I get a Read instance for Zero? (Zero is a pseudo-data constructor, not a type constructor, so that question is making a category error.)

All I can think of is to avoid deriving (Read, ...) for MyNum and then hand-craft

instance Read MyNum where
  ... parse "MkNum ..."
  ... parse "Zero"

For parsing, Zero should be just another constructor for MyNum. It could be derived -- or could it?

I don't think any of the advanced deriving mechanisms would help here. Because the type is MyNum and the only associated constructor ie MkNum.

I could provide a function readZero :: String -> MyNum. But I can't overload read, therefore I can't embed Zero inside a long String literal that I'm reading to make a data structure.

Any ideas?

0

There are 0 best solutions below