Generate parser that runs a received parser on the output of another parser and monadically joins the results

Viewed 234

given the following type and function, meant to parse a field of a CSV field into a string:

type Parser resultType = ParsecT String () Identity resultType
cell :: Parser String 

I have implemented the following function:

customCell :: String -> Parser res  -> Parser res
customCell typeName subparser = 
  cell
    >>= either (const $ unexpected typeName) 
               return . parse (subparser <* eof) ""

Though I cannot stop thinking that I am not using the Monad concept as much as desired and that eventually there is a better way to merge the result of the inner with the outer parser, specially on what regards its failure.

Does anybody know how could I do so, or is this code what is meant to be done?

PS - I now realised that my type simplification is probably not appropriate and that maybe what I want is to replace the underlying Identity Monad by the Either Monad.... Unfortunately, I do not feel enough acquainted with monad transformers yet.

PS2 - What the hell is the underlying monad good for anyway?

2 Answers
Related