Search code examples
haskelltypesparsec

Type Problems chaining CaseOf Statements with Parsec


I'm learning haskell, and my current project is writing a parser to read a text file representation of a database.

At the moment, I'm setting up the code for reading individual fields of tables. In the text file, fields look either like this:

name type flags format

or this:

name type       format

This gives the trouble of having to account for cases of there being a flag or not being a flag. I solved this in my main function like this:

main = case parse fieldsWithFlags "(test)" testLine of
        Left err  -> noFlags
        Right res -> print res
   where noFlags = case parse fieldsWithoutFlags "(test)" testLine of
                        Left err -> print err
                        Right res -> print res

If I understand correctly, this says "If it's a line that doesn't have flags, try to parse it as such; otherwise, return an error." It prints the correct results for any "testLine" I throw at it, and returns errors if both options fail. However, when I try to pull this out into its own function, like this:

field :: Either ParseError Field
field = case parse fieldsWithFlags "(test)" testLine of
            Left err  -> noFlags
            Right res -> return Right res
        where noFlags = case parse fieldsWithoutFlags "(test)" testLine of
                           Left err -> return Left err
                           Right res -> return Right res
main = case field of
       Left err -> print err
       Right res -> print res

GHC gives me:

haskellParsing.hs:58:26:
Couldn't match expected type `Either ParseError Field'
with actual type `b0 -> Either b0 b0'
In the expression: noFlags
In a case alternative: Left err -> noFlags
In the expression:
  case parse fieldsWithFlags "(test)" testLine of {
    Left err -> noFlags
    Right res -> return Right res }

I've played around with this a lot, but just can't get it working. I'm sure there's a much more clear-headed way of doing this, so any suggestions would be welcome - but I also want to understand why this isn't working.

Full code is at: http://pastebin.com/ajG6BkPU

Thanks!


Solution

  • You don't need the return in your cases. Once you wrap something in Left or Right it is in Either; since you only need a Either ParseError Field, the Left and Right do not need an extra return.

    Also, you should be able to simplify your parseFields significantly. You can write a new parser that looks like this:

    fields = try fieldsWithFlags <|> fieldsWithoutFlags
    

    what this does is run the first one and, if it fails, backtrack and run the second one. The try is important because this is what enables the backtracking behavior. You have to backtrack because fieldsWithFlags consumes some of the input that you care about.

    Now you should be able to just use fields in your main function.