I've been playing with Haskell for about a month. For my first "real" Haskell project I'm writing a parts-of-speech tagger. As part of this project I have a type called Tag
that represents a parts-of-speech tag, implemented as follows:
data Tag = CC | CD | DT | EX | FW | IN | JJ | JJR | JJS ...
The above is a long list of standardized parts-of-speech tags which I've intentionally truncated. However, in this standard set of tags there are two that end in a dollar sign ($): PRP$ and NNP$. Because I can't have type constructors with $ in their name, I've elected to rename them PRPS and NNPS.
This is all well and good, but I'd like to read tags from strings in a lexicon and convert them to my Tag
type. Trying this fails:
instance Read Tag where
readsPrec _ input =
(\inp -> [((NNPS), rest) | ("NNP$", rest) <- lex inp]) input
The Haskell lexer chokes on the $. Any ideas how to pull this off?
Implementing Show was fairly straightforward. It would be great if there were some similar strategy for Read.
instance Show Tag where
showsPrec _ NNPS = showString "NNP$"
showsPrec _ PRPS = showString "PRP$"
showsPrec _ tag = shows tag
Show
andRead
instances, instead of using the instances that get derived automatically, is if your data type hides its internal representation (likeData.Set.Set
and such, which spit out afromList
call) or works with literals, e.g. an instance ofNum
spitting out an integer literal it corresponds to. – Tasse